Completely overhauled how GDT is defined to make it much easier to add more entries to the GDT in the future
This commit is contained in:
parent
760069bbb9
commit
f581e6b6fe
@ -1,57 +1,89 @@
|
|||||||
#include "gdt.h"
|
#include "gdt.h"
|
||||||
|
|
||||||
|
constexpr tss_struct::tss_struct() :
|
||||||
|
link(0),
|
||||||
|
_reserved0(0),
|
||||||
|
esp0(0),
|
||||||
|
ss0(0),
|
||||||
|
_reserved1(0),
|
||||||
|
esp1(0),
|
||||||
|
ss1(0),
|
||||||
|
_reserved2(0),
|
||||||
|
esp2(0),
|
||||||
|
ss2(0),
|
||||||
|
_reserved3(0),
|
||||||
|
cr3(0),
|
||||||
|
eip(0),
|
||||||
|
eflags(0),
|
||||||
|
eax(0),
|
||||||
|
ebx(0),
|
||||||
|
ecx(0),
|
||||||
|
edx(0),
|
||||||
|
esp(0),
|
||||||
|
ebp(0),
|
||||||
|
esi(0),
|
||||||
|
edi(0),
|
||||||
|
es(0),
|
||||||
|
_reserved4(0),
|
||||||
|
cs(0),
|
||||||
|
_reserved5(0),
|
||||||
|
ss(0),
|
||||||
|
_reserved6(0),
|
||||||
|
ds(0),
|
||||||
|
_reserved7(0),
|
||||||
|
fs(0),
|
||||||
|
_reserved8(0),
|
||||||
|
gs(0),
|
||||||
|
_reserved9(0),
|
||||||
|
ldtr(0),
|
||||||
|
_reserved10(0),
|
||||||
|
_reserved11(0),
|
||||||
|
iopb(0)
|
||||||
|
{}
|
||||||
|
|
||||||
|
tss_struct tss = tss_struct();
|
||||||
|
|
||||||
|
constexpr gdt_entry::gdt_entry(uint32_t limit, uint32_t base, bool rw, bool exec, bool system, uint8_t ring) :
|
||||||
|
limit_lo(limit & 0xffff),
|
||||||
|
limit_hi((limit & 0xf0000) >> 16),
|
||||||
|
base_lo(base & 0xffff),
|
||||||
|
base_mid((base & 0xff0000) >> 16),
|
||||||
|
base_hi((base & 0xff000000) >> 24),
|
||||||
|
__ignored__(0),
|
||||||
|
accessed(0),
|
||||||
|
granularity(1),
|
||||||
|
size(1),
|
||||||
|
present(1),
|
||||||
|
read_write(rw),
|
||||||
|
executable(exec),
|
||||||
|
system_segment(system),
|
||||||
|
privilege(ring),
|
||||||
|
direction(0)
|
||||||
|
{}
|
||||||
|
|
||||||
|
constexpr gdt_entry::gdt_entry() :
|
||||||
|
limit_lo(0),
|
||||||
|
base_lo(0),
|
||||||
|
base_mid(0),
|
||||||
|
accessed(0),
|
||||||
|
read_write(0),
|
||||||
|
direction(0),
|
||||||
|
executable(0),
|
||||||
|
system_segment(0),
|
||||||
|
privilege(0),
|
||||||
|
present(0),
|
||||||
|
limit_hi(0),
|
||||||
|
__ignored__(0),
|
||||||
|
size(0),
|
||||||
|
granularity(0),
|
||||||
|
base_hi(0)
|
||||||
|
{}
|
||||||
|
|
||||||
gdt_entry gdt[] = {
|
gdt_entry gdt[] = {
|
||||||
(gdt_entry){ // Null Segment
|
gdt_entry(), // Null Segment
|
||||||
.limit_lo = 0,
|
gdt_entry(0xfffff, 0, 1, 1, 1, 0), // Code Segment
|
||||||
.base_lo = 0,
|
gdt_entry(0xfffff, 0, 1, 0, 1, 0), // Data Segment
|
||||||
.base_mid = 0,
|
gdt_entry(sizeof(tss), &tss, 0, 1, 0, 0) // Task State Segment 1
|
||||||
.accessed = 0,
|
|
||||||
.read_write = 0,
|
|
||||||
.direction = 0,
|
|
||||||
.executable = 0,
|
|
||||||
.system_segment = 0,
|
|
||||||
.privilege = 0,
|
|
||||||
.present = 0,
|
|
||||||
.limit_hi = 0,
|
|
||||||
.__ignored__ = 0,
|
|
||||||
.size = 0,
|
|
||||||
.granularity = 0,
|
|
||||||
.base_hi = 0,
|
|
||||||
},
|
|
||||||
(gdt_entry){ // Code Segment
|
|
||||||
.limit_lo = 0xffff,
|
|
||||||
.base_lo = 0,
|
|
||||||
.base_mid = 0,
|
|
||||||
.accessed = 0,
|
|
||||||
.read_write = 1,
|
|
||||||
.direction = 0,
|
|
||||||
.executable = 1,
|
|
||||||
.system_segment = 1,
|
|
||||||
.privilege = 0,
|
|
||||||
.present = 1,
|
|
||||||
.limit_hi = 0xf,
|
|
||||||
.__ignored__ = 0,
|
|
||||||
.size = 1,
|
|
||||||
.granularity = 1,
|
|
||||||
.base_hi = 0
|
|
||||||
},
|
|
||||||
(gdt_entry){ // Data Segment
|
|
||||||
.limit_lo = 0xffff,
|
|
||||||
.base_lo = 0,
|
|
||||||
.base_mid = 0,
|
|
||||||
.accessed = 0,
|
|
||||||
.read_write = 1,
|
|
||||||
.direction = 0,
|
|
||||||
.executable = 0,
|
|
||||||
.system_segment = 1,
|
|
||||||
.privilege = 0,
|
|
||||||
.present = 1,
|
|
||||||
.limit_hi = 0xf,
|
|
||||||
.__ignored__ = 0,
|
|
||||||
.size = 1,
|
|
||||||
.granularity = 1,
|
|
||||||
.base_hi = 0
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
gdt_descr descr = (gdt_descr){
|
gdt_descr descr = (gdt_descr){
|
||||||
@ -59,14 +91,10 @@ gdt_descr descr = (gdt_descr){
|
|||||||
.offset = gdt,
|
.offset = gdt,
|
||||||
};
|
};
|
||||||
|
|
||||||
__attribute__((far)) void far_call() {}
|
|
||||||
|
|
||||||
void init_gdt() {
|
void init_gdt() {
|
||||||
asm volatile("lgdt %0;"
|
asm volatile("lgdt %0;"
|
||||||
"mov $0x10, %%eax;"
|
"mov $0x10, %%eax;"
|
||||||
"mov %%eax, %%ss;"
|
"mov %%eax, %%ss;"
|
||||||
"mov $0x10, %%eax;"
|
"mov $0x10, %%eax;"
|
||||||
"mov %%eax, %%ds" : : "m" (descr));
|
"mov %%eax, %%ds" : : "m" (descr));
|
||||||
|
|
||||||
far_call();
|
|
||||||
}
|
}
|
@ -3,6 +3,48 @@
|
|||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
struct __attribute__((packed)) tss_struct {
|
||||||
|
uint16_t link;
|
||||||
|
uint16_t _reserved0;
|
||||||
|
uint32_t esp0;
|
||||||
|
uint16_t ss0;
|
||||||
|
uint16_t _reserved1;
|
||||||
|
uint32_t esp1;
|
||||||
|
uint16_t ss1;
|
||||||
|
uint16_t _reserved2;
|
||||||
|
uint32_t esp2;
|
||||||
|
uint16_t ss2;
|
||||||
|
uint16_t _reserved3;
|
||||||
|
uint32_t cr3;
|
||||||
|
uint32_t eip;
|
||||||
|
uint32_t eflags;
|
||||||
|
uint32_t eax;
|
||||||
|
uint32_t ebx;
|
||||||
|
uint32_t ecx;
|
||||||
|
uint32_t edx;
|
||||||
|
uint32_t esp;
|
||||||
|
uint32_t ebp;
|
||||||
|
uint32_t esi;
|
||||||
|
uint32_t edi;
|
||||||
|
uint16_t es;
|
||||||
|
uint16_t _reserved4;
|
||||||
|
uint16_t cs;
|
||||||
|
uint16_t _reserved5;
|
||||||
|
uint16_t ss;
|
||||||
|
uint16_t _reserved6;
|
||||||
|
uint16_t ds;
|
||||||
|
uint16_t _reserved7;
|
||||||
|
uint16_t fs;
|
||||||
|
uint16_t _reserved8;
|
||||||
|
uint16_t gs;
|
||||||
|
uint16_t _reserved9;
|
||||||
|
uint16_t ldtr;
|
||||||
|
uint16_t _reserved10;
|
||||||
|
uint16_t _reserved11;
|
||||||
|
uint16_t iopb;
|
||||||
|
constexpr tss_struct();
|
||||||
|
};
|
||||||
|
|
||||||
struct __attribute__((packed)) gdt_entry {
|
struct __attribute__((packed)) gdt_entry {
|
||||||
uint32_t limit_lo : 16;
|
uint32_t limit_lo : 16;
|
||||||
uint32_t base_lo : 16;
|
uint32_t base_lo : 16;
|
||||||
@ -22,6 +64,9 @@ struct __attribute__((packed)) gdt_entry {
|
|||||||
uint32_t granularity : 1;
|
uint32_t granularity : 1;
|
||||||
|
|
||||||
uint32_t base_hi : 8;
|
uint32_t base_hi : 8;
|
||||||
|
|
||||||
|
constexpr gdt_entry(uint32_t limit, uint32_t base, bool rw, bool exec, bool system, uint8_t ring);
|
||||||
|
constexpr gdt_entry();
|
||||||
};
|
};
|
||||||
|
|
||||||
struct __attribute__((packed)) gdt_descr {
|
struct __attribute__((packed)) gdt_descr {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user