diff options
author | Marvin Borner | 2021-07-10 22:45:33 +0200 |
---|---|---|
committer | Marvin Borner | 2021-07-10 22:45:33 +0200 |
commit | 91f534979f4790d5cfc0514eb110f3b1f4fb219b (patch) | |
tree | 896617b4f345f263271f4d939e49b3d28729ea04 | |
parent | 4f1ae4fa4250369919a975f5568425f6791b2489 (diff) |
Reduced GDT magic numbers and added documentation
-rw-r--r-- | src/loader/ent.asm | 4 | ||||
-rw-r--r-- | src/loader/gdt.c | 107 | ||||
-rw-r--r-- | src/loader/inc/gdt.h | 2 |
3 files changed, 69 insertions, 44 deletions
diff --git a/src/loader/ent.asm b/src/loader/ent.asm index c518aad..87b5e53 100644 --- a/src/loader/ent.asm +++ b/src/loader/ent.asm @@ -20,10 +20,10 @@ _start: lgdt [gdt] ; Load GDT - jmp 0x18:.reload_cs + jmp 0x18:.reload_cs ; Use third GDT segment for code .reload_cs: - mov eax, 0x20 + mov eax, 0x20 ; Use fourth GDT segment for data/other mov ds, ax mov es, ax mov fs, ax diff --git a/src/loader/gdt.c b/src/loader/gdt.c index ea91c33..61f186d 100644 --- a/src/loader/gdt.c +++ b/src/loader/gdt.c @@ -2,50 +2,75 @@ #include <gdt.h> +#define GDT_MAX_LIMIT 0xffff // General max access limit without extra nibble +#define GDT_EXTRA_MAX_LIMIT (0x0f) // Extra limit nibble +#define GDT_PRESENT (1 << 7) // Makes the descriptor valid +#define GDT_DESCRIPTOR (1 << 4) // Should be set for code/data segments +#define GDT_EXECUTABLE (1 << 3) // Makes the segment executable (for code segments) +#define GDT_READWRITE (1 << 1) // Makes the segment read/writeable +#define GDT_SIZE (0x40) // Enable 32-Bit +#define GDT_GRANULARITY (0x80) // Enable 4K granularity for limit + static struct gdt_desc gdt_descs[] = { + // NULL descriptor - Offset 0 { 0 }, - { .limit = 0xffff, - .base_low = 0x0000, - .base_mid = 0x00, - .access = 0x9a, - .granularity = 0x00, - .base_hi = 0x00 }, - - { .limit = 0xffff, - .base_low = 0x0000, - .base_mid = 0x00, - .access = 0x92, - .granularity = 0x00, - .base_hi = 0x00 }, - - { .limit = 0xffff, - .base_low = 0x0000, - .base_mid = 0x00, - .access = 0x9a, - .granularity = 0xcf, - .base_hi = 0x00 }, - - { .limit = 0xffff, - .base_low = 0x0000, - .base_mid = 0x00, - .access = 0x92, - .granularity = 0xcf, - .base_hi = 0x00 }, - - { .limit = 0x0000, - .base_low = 0x0000, - .base_mid = 0x00, - .access = 0x9a, - .granularity = 0x20, - .base_hi = 0x00 }, - - { .limit = 0x0000, - .base_low = 0x0000, - .base_mid = 0x00, - .access = 0x92, - .granularity = 0x00, - .base_hi = 0x00 }, + /** + * These 16-Bit access segments get used by 16-Bit real mode emulation + * + * Limit + extra limit gets multiplied by 1 (no granularity/size bit) + */ + + // Standard code segment // Offset 1 + // Access from (0) to (0xffff * 1 = 64KiB) + { + .limit = GDT_MAX_LIMIT, + .base_low = 0x0000, + .base_mid = 0x00, + .access = GDT_DESCRIPTOR | GDT_PRESENT | GDT_READWRITE | GDT_EXECUTABLE, + .flags = 0x00, + .base_hi = 0x00, + }, + + // Standard data segment // Offset 2 + // Access from (0) to (0xffff * 1 = 64KiB) + { + .limit = GDT_MAX_LIMIT, + .base_low = 0x0000, + .base_mid = 0x00, + .access = GDT_DESCRIPTOR | GDT_PRESENT | GDT_READWRITE, + .flags = 0x00, + .base_hi = 0x00, + }, + + /** + * These 32-Bit full access segments get uses by the bootloader to load + * ELF programs no matter whether they want to be loaded to 0-4GiB addr + * + * Limit + extra limit gets multiplied by 4096 (<< 12) with added 1s + */ + + // Full access code segment // Offset 3 + // Access from (0) to ((0xfffff << 12) + 0xfff = 4GiB) + { + .limit = GDT_MAX_LIMIT, + .base_low = 0x0000, + .base_mid = 0x00, + .access = GDT_DESCRIPTOR | GDT_PRESENT | GDT_READWRITE | GDT_EXECUTABLE, + .flags = GDT_GRANULARITY | GDT_SIZE | GDT_EXTRA_MAX_LIMIT, + .base_hi = 0x00, + }, + + // Full access data segment // Offset 4 + // Access from (0) to ((0xfffff << 12) + 0xfff = 4GiB) + { + .limit = GDT_MAX_LIMIT, + .base_low = 0x0000, + .base_mid = 0x00, + .access = GDT_DESCRIPTOR | GDT_PRESENT | GDT_READWRITE, + .flags = GDT_GRANULARITY | GDT_SIZE | GDT_EXTRA_MAX_LIMIT, + .base_hi = 0x00, + }, }; REAL struct gdtr gdt = { diff --git a/src/loader/inc/gdt.h b/src/loader/inc/gdt.h index 1ff20b7..556d1e8 100644 --- a/src/loader/inc/gdt.h +++ b/src/loader/inc/gdt.h @@ -16,7 +16,7 @@ struct gdt_desc { u16 base_low; u8 base_mid; u8 access; - u8 granularity; + u8 flags; u8 base_hi; } PACKED; |