From d963772c6633a0610898aaba2ae90d461e8f2de8 Mon Sep 17 00:00:00 2001 From: nishi Date: Fri, 7 Jul 2023 23:40:27 +0000 Subject: should be working, should be git-svn-id: https://svn.vegaa.systems/svn/vega-Vega/trunk@7 a8a8aea2-181d-ee11-89e8-15fd0e089fc4 --- sys/arch/amd64/gdt.c | 82 ++++++++++++++++++++++++++ sys/arch/amd64/idt.c | 68 +++++++++++++++++++++ sys/arch/amd64/machdep.c | 68 +++++++++++++++++++++ sys/arch/amd64/trap.S | 150 +++++++++++++++++++++++++++++++++++++++++++++++ sys/arch/amd64/trap.c | 104 ++++++++++++++++++++++++++++++++ 5 files changed, 472 insertions(+) create mode 100644 sys/arch/amd64/gdt.c create mode 100644 sys/arch/amd64/idt.c create mode 100644 sys/arch/amd64/machdep.c create mode 100644 sys/arch/amd64/trap.S create mode 100644 sys/arch/amd64/trap.c (limited to 'sys/arch') diff --git a/sys/arch/amd64/gdt.c b/sys/arch/amd64/gdt.c new file mode 100644 index 0000000..446306a --- /dev/null +++ b/sys/arch/amd64/gdt.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2023 Ian Marco Moffett and the VegaOS team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of VegaOS nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* $Id$ */ + +#include + +struct gdt_entry g_dmmy_gdt[256] = { + /* Null */ + {0}, + + /* Kernel code (0x8) */ + { + .limit = 0x0000, + .base_low = 0x0000, + .base_mid = 0x00, + .access = 0x9A, + .granularity = 0x20, + .base_hi = 0x00 + }, + + /* Kernel data (0x10) */ + { + .limit = 0x0000, + .base_low = 0x0000, + .base_mid = 0x00, + .access = 0x92, + .granularity = 0x00, + .base_hi = 0x00 + }, + + /* User code (0x18) */ + { + .limit = 0x0000, + .base_low = 0x0000, + .base_mid = 0x00, + .access = 0xFA, + .granularity = 0xAF, + .base_hi = 0x00 + }, + + /* User data (0x20) */ + { + .limit = 0x0000, + .base_low = 0x0000, + .base_mid = 0x00, + .access = 0xF2, + .granularity = 0x00, + .base_hi = 0x00 + }, +}; + +struct gdtr g_early_gdtr = { + .limit = sizeof(struct gdt_entry) * 256 - 1, + .offset = (uintptr_t)&g_dmmy_gdt[0] +}; diff --git a/sys/arch/amd64/idt.c b/sys/arch/amd64/idt.c new file mode 100644 index 0000000..46eb1f8 --- /dev/null +++ b/sys/arch/amd64/idt.c @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 Ian Marco Moffett and the VegaOS team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of VegaOS nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* $Id$ */ + +#include + +static struct idt_entry idt[256]; +static struct idtr idtr = { + .limit = sizeof(struct idt_entry) * 256 - 1, + .offset = (uintptr_t)&idt[0] +}; + +void +idt_load(void) +{ + LIDT(idtr); +} + +void +idt_set_desc(uint8_t vec, uint8_t type, uintptr_t isr, uint8_t ist) +{ + struct idt_entry *desc; + + if (vec >= 255) { + /* Invalid vector */ + return; + } + + desc = &idt[vec]; + desc->off_lo = __SHIFTOUT(isr, __MASK(16)); + desc->off_mid = __SHIFTOUT(isr, __MASK(16) << 16); + desc->off_hi = __SHIFTOUT(isr, __MASK(32) << 32); + desc->segsel = 0x8; /* Kernel CS */ + desc->type = type; + desc->dpl = 3; + desc->p = 1; + desc->zero = 0; + desc->zero1 = 0; + desc->reserved = 0; + desc->ist = ist; +} diff --git a/sys/arch/amd64/machdep.c b/sys/arch/amd64/machdep.c new file mode 100644 index 0000000..dd2cdc7 --- /dev/null +++ b/sys/arch/amd64/machdep.c @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 Ian Marco Moffett and the VegaOS team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of VegaOS nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* $Id$ */ + +#include +#include +#include +#include + +#define ISR(func) ((uintptr_t)func) + +__weak void +interrupts_init(struct processor *processor) +{ + __USE(processor); + idt_set_desc(0x0, IDT_TRAP_GATE_FLAGS, ISR(arith_err), 0); + idt_set_desc(0x2, IDT_TRAP_GATE_FLAGS, ISR(nmi), 0); + idt_set_desc(0x3, IDT_TRAP_GATE_FLAGS, ISR(breakpoint_handler), 0); + idt_set_desc(0x4, IDT_TRAP_GATE_FLAGS, ISR(overflow), 0); + idt_set_desc(0x5, IDT_TRAP_GATE_FLAGS, ISR(bound_range), 0); + idt_set_desc(0x6, IDT_TRAP_GATE_FLAGS, ISR(invl_op), 0); + idt_set_desc(0x8, IDT_TRAP_GATE_FLAGS, ISR(double_fault), 0); + idt_set_desc(0xA, IDT_TRAP_GATE_FLAGS, ISR(invl_tss), 0); + idt_set_desc(0xB, IDT_TRAP_GATE_FLAGS, ISR(segnp), 0); + idt_set_desc(0xD, IDT_TRAP_GATE_FLAGS, ISR(general_prot), 0); + idt_set_desc(0xE, IDT_TRAP_GATE_FLAGS, ISR(page_fault), 0); + idt_load(); +} + +void +processor_halt(void) +{ + __ASMV("cli; hlt"); +} + +__weak void +processor_init(struct processor *processor) +{ + gdt_load(processor->gdtr); + interrupts_init(processor); +} diff --git a/sys/arch/amd64/trap.S b/sys/arch/amd64/trap.S new file mode 100644 index 0000000..31792c5 --- /dev/null +++ b/sys/arch/amd64/trap.S @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2023 Ian Marco Moffett and the VegaOS team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of VegaOS nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* $Id$ */ + +#include +#include + +.text +.globl breakpoint_handler +breakpoint_handler: + push $0 + push_trapframe $TRAP_BREAKPOINT + + handle_trap + + /* TODO */ + cli + hlt + +.globl arith_err +arith_err: + push $0 + push_trapframe $TRAP_ARITH_ERR + + handle_trap + + /* TODO */ + cli + hlt + +.globl overflow +overflow: + push $0 + push_trapframe $TRAP_OVERFLOW + + handle_trap + + /* TODO */ + cli + hlt + +.globl bound_range +bound_range: + push $0 + push_trapframe $TRAP_BOUND_RANGE + + handle_trap + + /* TODO */ + cli + hlt + +.globl invl_op +invl_op: + push $0 + push_trapframe $TRAP_INVLOP + + handle_trap + + /* TODO */ + cli + hlt + +.globl double_fault +double_fault: + push_trapframe $TRAP_DOUBLE_FAULT + + handle_trap + + /* TODO */ + cli + hlt + +.globl invl_tss +invl_tss: + push_trapframe $TRAP_INVLTSS + + handle_trap + + /* TODO */ + cli + hlt + +.globl segnp +segnp: + push_trapframe $TRAP_SEGNP + + handle_trap + + /* TODO */ + cli + hlt + +.globl general_prot +general_prot: + push_trapframe $TRAP_PROTFLT + + handle_trap + + /* TODO */ + cli + hlt + +.globl page_fault +page_fault: + push_trapframe $TRAP_PAGEFLT + + handle_trap + + /* TODO */ + cli + hlt + +.globl nmi +nmi: + push $0 + push_trapframe $TRAP_NMI + + handle_trap + + /* TODO */ + cli + hlt diff --git a/sys/arch/amd64/trap.c b/sys/arch/amd64/trap.c new file mode 100644 index 0000000..93ae17c --- /dev/null +++ b/sys/arch/amd64/trap.c @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2023 Ian Marco Moffett and the VegaOS team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of VegaOS nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* $Id$ */ + +#include +#include +#include +#include +#include + +static const char *trap_type[] = { + [TRAP_BREAKPOINT] = "breakpoint", + [TRAP_ARITH_ERR] = "arithmetic error", + [TRAP_OVERFLOW] = "overflow", + [TRAP_BOUND_RANGE] = "bound range exceeded", + [TRAP_INVLOP] = "invalid opcode", + [TRAP_DOUBLE_FAULT] = "double fault", + [TRAP_INVLTSS] = "invalid TSS", + [TRAP_SEGNP] = "segment not present", + [TRAP_PROTFLT] = "general protection", + [TRAP_PAGEFLT] = "page fault", + [TRAP_NMI] = "non-maskable interrupt" +}; + +static const int TRAP_COUNT = __ARRAY_COUNT(trap_type); +static ftrap_handler_t ftrap_handler = NULL; +static struct spinlock ftrap_handler_lock = { 0 }; + +static void +trap_print(struct trapframe *tf) +{ + const char *mode; + + if (tf->trapno < TRAP_COUNT) { + kprintf("** Fatal %s", trap_type[tf->trapno]); + } else { + kprintf("** Unknown trap %d", tf->trapno); + } + mode = __TEST(tf->trapno, TRAP_USER) ? "user" : "supervisor"; + kprintf(" in %s mode **\n", mode); +} + +/* + * Registers a handler for + * *fatal* traps. + * + * => Can block + */ +void +register_ftrap_handler(ftrap_handler_t handler) +{ + spinlock_acquire(&ftrap_handler_lock); + ftrap_handler = handler; + spinlock_release(&ftrap_handler_lock); +} + +/* + * Handles traps. + * + * => Can block + */ +void +trap_handler(struct trapframe *tf) +{ + trap_print(tf); + + /* + * XXX: Handle NMIs better. For now we just + * panic. + */ + if (tf->trapno == TRAP_NMI) { + kprintf("Possible hardware failure?\n"); + panic("Caught NMI; bailing out\n"); + } + + panic("Caught pre-sched exception\n"); +} -- cgit v1.2.3