From a5ff8a6a9288e4386140fe6db934edc49264374d Mon Sep 17 00:00:00 2001 From: Julien CLEMENT Date: Tue, 13 Jul 2021 22:31:01 +0200 Subject: [PATCH] feat(gdt): add gdt kernel entries creation Signed-off-by: Julien CLEMENT --- k/gdt.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ k/gdt.h | 5 ++++- k/k.c | 1 + 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/k/gdt.c b/k/gdt.c index 7901109..7677c59 100644 --- a/k/gdt.c +++ b/k/gdt.c @@ -4,10 +4,58 @@ static struct gdt gdt = { 0 }; static void create_kernel_ds() { + struct gdt_entry *entry = gdt.entries + KERNEL_DS_INDEX; + + uint32_t limit = 0xffff; + uint32_t base = 0x0; + + entry->granularity = 1; + entry->db = 1; + entry->l = 0; + entry->available = 0; + + entry->present = 1; + entry->desc_priv = 0; + entry->desc_type = 1; + entry->ex = 0; + entry->dc = 0; + entry->rw = 1; + entry->accessed = 0; + + entry->limit_1 = limit & 0xffff; + entry->limit_2 = (limit >> 16) & 0xf; + + entry->base_1 = base & 0xffff; + entry->base_2 = (base >> 16) & 0xff; + entry->base_3 = (base >> 24) & 0xff; } static void create_kernel_cs() { + struct gdt_entry *entry = gdt.entries + KERNEL_CS_INDEX; + + uint32_t limit = 0xffff; + uint32_t base = 0x0; + + entry->granularity = 1; + entry->db = 1; + entry->l = 0; + entry->available = 0; + + entry->present = 1; + entry->desc_priv = 0; + entry->desc_type = 1; + entry->ex = 1; + entry->dc = 0; + entry->rw = 1; + entry->accessed = 0; + + entry->limit_1 = limit & 0xffff; + entry->limit_2 = (limit >> 16) & 0xf; + + entry->base_1 = base & 0xffff; + entry->base_2 = (base >> 16) & 0xff; + entry->base_3 = (base >> 24) & 0xff; } static void create_gdt() @@ -62,13 +110,18 @@ static void load_cs() static void reload_segment_selectors() { + printf("Before load_ds\r\n"); load_ds(); load_cs(); } void init_gdt() { + printf("Before create_gdt\r\n"); create_gdt(); + printf("Before load_gdt\r\n"); load_gdt(); + printf("Before reload_segment_selectors\r\n"); reload_segment_selectors(); + printf("All good\r\n"); } diff --git a/k/gdt.h b/k/gdt.h index 269fb60..9e1adbb 100644 --- a/k/gdt.h +++ b/k/gdt.h @@ -14,7 +14,10 @@ struct gdt_entry uint16_t limit_1 : 16; uint16_t base_1 : 16; uint8_t base_2 : 8; - uint8_t seg_type : 4; + uint8_t accessed : 1; + uint8_t rw : 1; + uint8_t dc : 1; + uint8_t ex : 1; uint8_t desc_type : 1; uint8_t desc_priv : 2; uint8_t present : 1; diff --git a/k/k.c b/k/k.c index 3fce470..a66b1e0 100644 --- a/k/k.c +++ b/k/k.c @@ -30,6 +30,7 @@ static void k_init(void) { init_serial(); + init_gdt(); } void k_main(unsigned long magic, multiboot_info_t *info)