From 43c10da9cf77bb7676db3733499c1b578826dca9 Mon Sep 17 00:00:00 2001 From: Ada Christine Date: Sun, 13 Feb 2022 13:10:49 +0000 Subject: splitting out descriptor declarations --- kc/core/cpu/cpu.c | 65 +++++++++--------------------------------------- kc/core/cpu/descriptor.h | 55 ++++++++++++++++++++++++++++++++++++++++ kc/core/panic.c | 8 +++--- 3 files changed, 72 insertions(+), 56 deletions(-) create mode 100644 kc/core/cpu/descriptor.h (limited to 'kc') diff --git a/kc/core/cpu/cpu.c b/kc/core/cpu/cpu.c index 28b69ff..7b48e30 100644 --- a/kc/core/cpu/cpu.c +++ b/kc/core/cpu/cpu.c @@ -5,6 +5,7 @@ #include "msr.h" #include "task.h" #include "memory.h" +#include "descriptor.h" #include #include @@ -25,58 +26,6 @@ #define MSR_CSTAR 0xc0000083 #define MSR_SFMASK 0xc0000084 -struct dtr64 -{ - uint16_t limit; - uint64_t base; -} __attribute__((packed)); - -struct segment_descriptor -{ - uint16_t limit0; - uint16_t base0; - uint8_t base16; - uint8_t access; - uint8_t flags_limit16; - uint8_t base24; -} __attribute__((packed)); - -enum segment_descriptor_type -{ - CODE64_SUPER_SEG, - CODE64_USER_SEG, - DATA_SEG, - TASK64_SEG, -}; - -struct gate_descriptor -{ - uint16_t offset0; - uint16_t selector; - uint8_t ist; - uint8_t access; - uint16_t offset16; - uint64_t offset32; -} __attribute__((packed)); - -enum gate_descriptor_type -{ - INT64_GATE, - TRAP64_GATE, -}; - -struct task64_segment -{ - uint32_t reserved0; - uint64_t rsp0; - uint64_t rsp1; - uint64_t rsp2; - uint64_t reserved1; - uint64_t ist[7]; - uint16_t reserved2; - uint16_t iomap_base; -}; - static struct segment_descriptor gdt[GDT_ENTRIES]; static struct gate_descriptor idt[IDT_ENTRIES]; static struct task64_segment tss; @@ -126,11 +75,18 @@ static void set_gdt(int index, }; } -static void set_idt(int vec, +static void set_idt( + int vec, uint16_t selector, uint64_t offset, enum gate_descriptor_type type) { + // TODO: panic() on attempt to set an idte outside of bounds? + if (vec >= IDT_ENTRIES) + { + return; + } + idt[vec].offset0 = offset & 0xffff; idt[vec].selector = selector; idt[vec].offset16 = (offset >> 16) & 0xffff; @@ -138,6 +94,9 @@ static void set_idt(int vec, switch (type) { + case EMPTY_GATE: + idt[vec].access = 0x0; + break; case INT64_GATE: idt[vec].access = 0x8e; break; diff --git a/kc/core/cpu/descriptor.h b/kc/core/cpu/descriptor.h new file mode 100644 index 0000000..802c09c --- /dev/null +++ b/kc/core/cpu/descriptor.h @@ -0,0 +1,55 @@ +#pragma once + +struct dtr64 +{ + uint16_t limit; + uint64_t base; +} __attribute__((packed)); + +struct segment_descriptor +{ + uint16_t limit0; + uint16_t base0; + uint8_t base16; + uint8_t access; + uint8_t flags_limit16; + uint8_t base24; +} __attribute__((packed)); + +enum segment_descriptor_type +{ + CODE64_SUPER_SEG, + CODE64_USER_SEG, + DATA_SEG, + TASK64_SEG, +}; + +struct gate_descriptor +{ + uint16_t offset0; + uint16_t selector; + uint8_t ist; + uint8_t access; + uint16_t offset16; + uint64_t offset32; +} __attribute__((packed)); + +enum gate_descriptor_type +{ + EMPTY_GATE, + INT64_GATE, + TRAP64_GATE, +}; + +struct task64_segment +{ + uint32_t reserved0; + uint64_t rsp0; + uint64_t rsp1; + uint64_t rsp2; + uint64_t reserved1; + uint64_t ist[7]; + uint16_t reserved2; + uint16_t iomap_base; +}; + diff --git a/kc/core/panic.c b/kc/core/panic.c index b0d9daf..6fd5581 100644 --- a/kc/core/panic.c +++ b/kc/core/panic.c @@ -12,9 +12,11 @@ static char const *reason_strings[] = { noreturn void panic(enum panic_reason reason) { // TODO: kprintf() and friends - kputs("kernel panic: "); - kputs(reason_strings[reason]); - kputs("\n"); + if (reason > sizeof(reason_strings) / sizeof(*reason_strings)) + { + reason = 0; + } + kprintf("kernel panic: %s\n", reason_strings[reason]); halt(); } -- cgit v1.3.1-1-g115d