forked from PoliEcho-holding-org/kern
The great 64-bit rewrite
* We did loose tasking and only have a paging skeleton, but hey it took like 4 months so i'm happy that there's something :p
This commit is contained in:
+43
-47
@@ -8,7 +8,7 @@
|
||||
#define DPL_USER 3
|
||||
|
||||
/*
|
||||
* Segment selector explanation (why kcode is 0x08?)
|
||||
* Segment selector explanation (why is kcode 0x08?)
|
||||
*
|
||||
* Each segment selector must be 16 bits. These bits
|
||||
* have the following layout: (example is 0x08)
|
||||
@@ -17,7 +17,7 @@
|
||||
* | '--- Table indicator
|
||||
* '------ Index in table
|
||||
*
|
||||
* Bits 0-1 are the privilege level. x86 has four,
|
||||
* Bits 0-1 are the privilege level. x86(_64) has four,
|
||||
* where 00 is the highest and 11 is the lowest. Only
|
||||
* these two are widely used. 00 being kernel space &
|
||||
* 11 being userspace. Bit 3 is the table indicator.
|
||||
@@ -30,37 +30,23 @@
|
||||
* s 0 for the GDT and the index is one, because it
|
||||
* is the second entry in our GDT (after null desc.)
|
||||
*/
|
||||
#define SEGMENT_SEL(_idx, _tab, _dpl) (\
|
||||
(((_dpl) ) & 3) | \
|
||||
(((_tab) << 2) & 4) | \
|
||||
(((_idx) << 3) ) \
|
||||
)
|
||||
|
||||
/*
|
||||
* Entries:
|
||||
* 0 Null desc.
|
||||
* 1 Kernel code desc.
|
||||
* 2 Kernel data desc.
|
||||
* 3 User code desc.
|
||||
* 4 User data desc.
|
||||
* 3 User data desc.
|
||||
* 4 User code desc.
|
||||
* 5 Task State Segment
|
||||
*/
|
||||
#define GDT_ENTRIES 6
|
||||
#define GDT_NULL SEGMENT_SEL(0, 0, 0)
|
||||
#define GDT_KERNEL_CODE SEGMENT_SEL(1, 0, DPL_KERNEL)
|
||||
#define GDT_KERNEL_DATA SEGMENT_SEL(2, 0, DPL_KERNEL)
|
||||
#define GDT_USER_CODE SEGMENT_SEL(3, 0, DPL_USER)
|
||||
#define GDT_USER_DATA SEGMENT_SEL(4, 0, DPL_USER)
|
||||
#define GDT_TSS SEGMENT_SEL(5, 0, DPL_KERNEL)
|
||||
|
||||
#define GDT_DESC(_lim, _base, _access, _flags) (struct gdt_entry) { \
|
||||
.base_low = (_base) & 0xFFFF, \
|
||||
.base_mid = ((_base) >> 16) & 0xFF, \
|
||||
.base_high = ((_base) >> 24) & 0xFF, \
|
||||
.limit = (_lim) & 0xFFFF, \
|
||||
.flags = ((_flags) & 0xF0) | (((_lim) >> 16) & 0x0F), \
|
||||
.access = (_access) \
|
||||
}
|
||||
#define GDT_ENTRIES 7
|
||||
#define GDT_NULL 0x00
|
||||
#define GDT_KERNEL_CODE 0x08
|
||||
#define GDT_KERNEL_DATA 0x10
|
||||
#define GDT_USER_DATA 0x18
|
||||
#define GDT_USER_CODE 0x20
|
||||
#define GDT_TSS 0x28
|
||||
|
||||
/*
|
||||
* GDT descriptor/entry explanation
|
||||
@@ -95,34 +81,44 @@ struct gdt_entry {
|
||||
|
||||
struct gdt_pointer {
|
||||
uint16_t limit;
|
||||
uint32_t base;
|
||||
uint64_t base;
|
||||
} __packed;
|
||||
|
||||
struct tss_entry {
|
||||
uint16_t length;
|
||||
uint16_t base_low;
|
||||
uint8_t base_mid;
|
||||
uint8_t access;
|
||||
uint8_t flags;
|
||||
uint8_t base_high;
|
||||
uint32_t base_upper32;
|
||||
uint32_t __reserved;
|
||||
} __packed;
|
||||
|
||||
struct tss_pointer {
|
||||
uint16_t prev_task, __reserved0;
|
||||
uint32_t esp0;
|
||||
uint16_t ss0, __reserved1;
|
||||
uint32_t esp1;
|
||||
uint16_t ss1, __reserved2;
|
||||
uint32_t esp2;
|
||||
uint16_t ss2, __reserved3;
|
||||
uint32_t cr3;
|
||||
uint32_t eip;
|
||||
uint32_t eflags;
|
||||
uint32_t eax, ecx, edx, ebx, esp, ebp, esi, edi;
|
||||
uint16_t es, __reserved4;
|
||||
uint16_t cs, __reserved5;
|
||||
uint16_t ss, __reserved6;
|
||||
uint16_t ds, __reserved7;
|
||||
uint16_t fs, __reserved8;
|
||||
uint16_t gs, __reserved9;
|
||||
uint16_t ldtr;
|
||||
uint32_t __reserved10;
|
||||
uint32_t __reserved0;
|
||||
uint64_t rsp0;
|
||||
uint64_t rsp1;
|
||||
uint64_t rsp2;
|
||||
uint64_t __reserved1;
|
||||
uint64_t ist1;
|
||||
uint64_t ist2;
|
||||
uint64_t ist3;
|
||||
uint64_t ist4;
|
||||
uint64_t ist5;
|
||||
uint64_t ist6;
|
||||
uint64_t ist7;
|
||||
uint64_t __reserved2;
|
||||
uint16_t __reserved3;
|
||||
uint16_t iopb;
|
||||
uint32_t ssp;
|
||||
} __packed;
|
||||
|
||||
void tss_esp0(uint32_t esp0);
|
||||
struct gdt_entries {
|
||||
// GDT_ENTRIES - 2 to leave space for TSS
|
||||
struct gdt_entry entries[GDT_ENTRIES - 2];
|
||||
struct tss_entry tss;
|
||||
} __packed;
|
||||
|
||||
int gdt_init(void);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user