From c65c7bb7b03d1b8af36a1749e91855cdbf6a22a5 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Wed, 29 Apr 2020 23:07:01 +0200 Subject: [PATCH] More more stuff in arch subdir --- Makefile | 2 +- boot.asm => arch/x86/boot/boot.asm | 0 boot.s => arch/x86/boot/boot.s | 0 arch/x86/exception.c | 47 ++++++++++++++++++++++++++++ arch/x86/exception.h | 1 + arch/x86/processor.h | 14 +++++++++ core/alloc.c | 2 +- core/alloc.h | 2 +- core/main.c | 49 ++---------------------------- 9 files changed, 67 insertions(+), 50 deletions(-) rename boot.asm => arch/x86/boot/boot.asm (100%) rename boot.s => arch/x86/boot/boot.s (100%) create mode 100644 arch/x86/processor.h diff --git a/Makefile b/Makefile index b75931b..8344b55 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ SUBDIRS := core drivers tests arch/$(ARCH) CPPFLAGS += $(foreach dir, $(SUBDIRS), -I$(dir)) -asmsrc=$(wildcard *.asm) +asmsrc=$(wildcard arch/$(ARCH)/boot/*.asm) asmobj=$(asmsrc:%.asm=%.o) csrc=$(shell find $(SUBDIRS) -type f -name "*.c")# $(wildcard *.c) cobj=$(csrc:%.c=%.o) arch/$(ARCH)/cpu_context_switch.o arch/$(ARCH)/irq_pit.o diff --git a/boot.asm b/arch/x86/boot/boot.asm similarity index 100% rename from boot.asm rename to arch/x86/boot/boot.asm diff --git a/boot.s b/arch/x86/boot/boot.s similarity index 100% rename from boot.s rename to arch/x86/boot/boot.s diff --git a/arch/x86/exception.c b/arch/x86/exception.c index fe452d3..51fe56d 100644 --- a/arch/x86/exception.c +++ b/arch/x86/exception.c @@ -21,3 +21,50 @@ int exceptionSetRoutine(int exception, exception_handler handler) restore_IRQs(flags); return 0; } + +int exceptionSetup() +{ + exceptionSetRoutine(EXCEPTION_DOUBLE_FAULT, ACCESS_INTERRUPT(EXCEPTION_DOUBLE_FAULT)); + exceptionSetRoutine(EXCEPTION_DIVIDE_ZERO, ACCESS_INTERRUPT(EXCEPTION_DIVIDE_ZERO)); + // Used by the DBG + // exceptionSetRoutine(EXCEPTION_DEBUG, ACCESS_INTERRUPT(EXCEPTION_DEBUG)); + exceptionSetRoutine(EXCEPTION_NMI, ACCESS_INTERRUPT(EXCEPTION_NMI)); + exceptionSetRoutine(EXCEPTION_BREAKPOINT, ACCESS_INTERRUPT(EXCEPTION_BREAKPOINT)); + exceptionSetRoutine(EXCEPTION_OVERFLOW, ACCESS_INTERRUPT(EXCEPTION_OVERFLOW)); + exceptionSetRoutine(EXCEPTION_BOUND_RANGE_EXCEEDED, + ACCESS_INTERRUPT(EXCEPTION_BOUND_RANGE_EXCEEDED)); + exceptionSetRoutine(EXCEPTION_INVALID_OPCODE, ACCESS_INTERRUPT(EXCEPTION_INVALID_OPCODE)); + exceptionSetRoutine(EXCEPTION_DEVICE_NOT_AVAILABLE, + ACCESS_INTERRUPT(EXCEPTION_DEVICE_NOT_AVAILABLE)); + exceptionSetRoutine(EXCEPTION_COPRO_OVERRUN, ACCESS_INTERRUPT(EXCEPTION_COPRO_OVERRUN)); + exceptionSetRoutine(EXCEPTION_INVALID_TSS, ACCESS_INTERRUPT(EXCEPTION_INVALID_TSS)); + exceptionSetRoutine(EXCEPTION_SEGMENT_NOT_PRESENT, + ACCESS_INTERRUPT(EXCEPTION_SEGMENT_NOT_PRESENT)); + exceptionSetRoutine(EXCEPTION_STACK_SEGMENT_FAULT, + ACCESS_INTERRUPT(EXCEPTION_STACK_SEGMENT_FAULT)); + exceptionSetRoutine(EXCEPTION_GENERAL_PROTECTION_FAULT, + ACCESS_INTERRUPT(EXCEPTION_GENERAL_PROTECTION_FAULT)); + exceptionSetRoutine(EXCEPTION_PAGE_FAULT, ACCESS_INTERRUPT(EXCEPTION_PAGE_FAULT)); + exceptionSetRoutine(EXCEPTION_RESERVED_1, ACCESS_INTERRUPT(EXCEPTION_RESERVED_1)); + exceptionSetRoutine(EXCEPTION_X87_FP_EXCEPTION, + ACCESS_INTERRUPT(EXCEPTION_X87_FP_EXCEPTION)); + exceptionSetRoutine(EXCEPTION_ALIGNMENT_CHECK, + ACCESS_INTERRUPT(EXCEPTION_ALIGNMENT_CHECK)); + exceptionSetRoutine(EXCEPTION_MACHINE_CHECK, ACCESS_INTERRUPT(EXCEPTION_MACHINE_CHECK)); + exceptionSetRoutine(EXCEPTION_SIMD_FP, ACCESS_INTERRUPT(EXCEPTION_SIMD_FP)); + exceptionSetRoutine(EXCEPTION_VIRTUALIZATION, ACCESS_INTERRUPT(EXCEPTION_VIRTUALIZATION)); + exceptionSetRoutine(EXCEPTION_RESERVED_2, ACCESS_INTERRUPT(EXCEPTION_RESERVED_2)); + exceptionSetRoutine(EXCEPTION_RESERVED_3, ACCESS_INTERRUPT(EXCEPTION_RESERVED_3)); + exceptionSetRoutine(EXCEPTION_RESERVED_4, ACCESS_INTERRUPT(EXCEPTION_RESERVED_4)); + exceptionSetRoutine(EXCEPTION_RESERVED_5, ACCESS_INTERRUPT(EXCEPTION_RESERVED_5)); + exceptionSetRoutine(EXCEPTION_RESERVED_6, ACCESS_INTERRUPT(EXCEPTION_RESERVED_6)); + exceptionSetRoutine(EXCEPTION_RESERVED_7, ACCESS_INTERRUPT(EXCEPTION_RESERVED_7)); + exceptionSetRoutine(EXCEPTION_RESERVED_8, ACCESS_INTERRUPT(EXCEPTION_RESERVED_8)); + exceptionSetRoutine(EXCEPTION_RESERVED_9, ACCESS_INTERRUPT(EXCEPTION_RESERVED_9)); + exceptionSetRoutine(EXCEPTION_RESERVED_10, ACCESS_INTERRUPT(EXCEPTION_RESERVED_10)); + exceptionSetRoutine(EXCEPTION_SECURITY, ACCESS_INTERRUPT(EXCEPTION_SECURITY)); + exceptionSetRoutine(EXCEPTION_RESERVED_11, ACCESS_INTERRUPT(EXCEPTION_RESERVED_11)); + + exceptionSetRoutine(EXCEPTION_PAGE_FAULT, pagefault_handler); + return 0; +} diff --git a/arch/x86/exception.h b/arch/x86/exception.h index 36bee46..9b7e2be 100644 --- a/arch/x86/exception.h +++ b/arch/x86/exception.h @@ -43,3 +43,4 @@ typedef void (*exception_handler)(struct interrupt_frame *frame, ulong error_code); int exceptionSetRoutine(int exception, exception_handler handler); +int exceptionSetup(); diff --git a/arch/x86/processor.h b/arch/x86/processor.h new file mode 100644 index 0000000..0605dd6 --- /dev/null +++ b/arch/x86/processor.h @@ -0,0 +1,14 @@ +#pragma once + +static void cpuid(unsigned int *eax, unsigned int *ebx, + unsigned int *ecx, unsigned int *edx) +{ + /* ecx is often an input as well as an output. */ + asm volatile("cpuid" + : "=a" (*eax), + "=b" (*ebx), + "=c" (*ecx), + "=d" (*edx) + : "0" (*eax), "2" (*ecx) + : "memory"); +} diff --git a/core/alloc.c b/core/alloc.c index 4b4cb44..09202b7 100644 --- a/core/alloc.c +++ b/core/alloc.c @@ -16,7 +16,7 @@ int allocSlab(struct slabDesc **desc, size_t size, int self_containing); int allocSlabEntry(struct slabEntry **desc, size_t size, int selfContained); static int formatPage(struct slabEntry *desc, size_t size, int selfContained); -int allocInit(void) +int allocSetup(void) { uint start = log2(sizeof(void *)); list_init(slub); diff --git a/core/alloc.h b/core/alloc.h index f1e41a2..139b46b 100644 --- a/core/alloc.h +++ b/core/alloc.h @@ -16,7 +16,7 @@ struct slabDesc { struct slabDesc *next; struct slabDesc *prev; }; -int allocInit(void); +int allocSetup(void); int allocBookSlab(size_t size, int selfContained); void *malloc(size_t size); diff --git a/core/main.c b/core/main.c index d29fbe2..5305d8e 100644 --- a/core/main.c +++ b/core/main.c @@ -22,10 +22,6 @@ #include "vga.h" #define CHECK_FLAG(flags, bit) ((flags) & (1 << (bit))) -void cpuid(int code, uint32_t *a, uint32_t *d) -{ - asm volatile("cpuid" : "=a"(*a), "=d"(*d) : "0"(code) : "ebx", "ecx"); -} void idleThread(void *arg) { @@ -110,54 +106,13 @@ void kmain(unsigned long magic, unsigned long addr) irqSetRoutine(IRQ_KEYBOARD, keyboard_handler); printf("Enabling HW interrupts\n"); - exceptionSetRoutine(EXCEPTION_DOUBLE_FAULT, ACCESS_INTERRUPT(EXCEPTION_DOUBLE_FAULT)); - exceptionSetRoutine(EXCEPTION_DIVIDE_ZERO, ACCESS_INTERRUPT(EXCEPTION_DIVIDE_ZERO)); - // Used by the DBG - // exceptionSetRoutine(EXCEPTION_DEBUG, ACCESS_INTERRUPT(EXCEPTION_DEBUG)); - exceptionSetRoutine(EXCEPTION_NMI, ACCESS_INTERRUPT(EXCEPTION_NMI)); - exceptionSetRoutine(EXCEPTION_BREAKPOINT, ACCESS_INTERRUPT(EXCEPTION_BREAKPOINT)); - exceptionSetRoutine(EXCEPTION_OVERFLOW, ACCESS_INTERRUPT(EXCEPTION_OVERFLOW)); - exceptionSetRoutine(EXCEPTION_BOUND_RANGE_EXCEEDED, - ACCESS_INTERRUPT(EXCEPTION_BOUND_RANGE_EXCEEDED)); - exceptionSetRoutine(EXCEPTION_INVALID_OPCODE, ACCESS_INTERRUPT(EXCEPTION_INVALID_OPCODE)); - exceptionSetRoutine(EXCEPTION_DEVICE_NOT_AVAILABLE, - ACCESS_INTERRUPT(EXCEPTION_DEVICE_NOT_AVAILABLE)); - exceptionSetRoutine(EXCEPTION_COPRO_OVERRUN, ACCESS_INTERRUPT(EXCEPTION_COPRO_OVERRUN)); - exceptionSetRoutine(EXCEPTION_INVALID_TSS, ACCESS_INTERRUPT(EXCEPTION_INVALID_TSS)); - exceptionSetRoutine(EXCEPTION_SEGMENT_NOT_PRESENT, - ACCESS_INTERRUPT(EXCEPTION_SEGMENT_NOT_PRESENT)); - exceptionSetRoutine(EXCEPTION_STACK_SEGMENT_FAULT, - ACCESS_INTERRUPT(EXCEPTION_STACK_SEGMENT_FAULT)); - exceptionSetRoutine(EXCEPTION_GENERAL_PROTECTION_FAULT, - ACCESS_INTERRUPT(EXCEPTION_GENERAL_PROTECTION_FAULT)); - exceptionSetRoutine(EXCEPTION_PAGE_FAULT, ACCESS_INTERRUPT(EXCEPTION_PAGE_FAULT)); - exceptionSetRoutine(EXCEPTION_RESERVED_1, ACCESS_INTERRUPT(EXCEPTION_RESERVED_1)); - exceptionSetRoutine(EXCEPTION_X87_FP_EXCEPTION, - ACCESS_INTERRUPT(EXCEPTION_X87_FP_EXCEPTION)); - exceptionSetRoutine(EXCEPTION_ALIGNMENT_CHECK, - ACCESS_INTERRUPT(EXCEPTION_ALIGNMENT_CHECK)); - exceptionSetRoutine(EXCEPTION_MACHINE_CHECK, ACCESS_INTERRUPT(EXCEPTION_MACHINE_CHECK)); - exceptionSetRoutine(EXCEPTION_SIMD_FP, ACCESS_INTERRUPT(EXCEPTION_SIMD_FP)); - exceptionSetRoutine(EXCEPTION_VIRTUALIZATION, ACCESS_INTERRUPT(EXCEPTION_VIRTUALIZATION)); - exceptionSetRoutine(EXCEPTION_RESERVED_2, ACCESS_INTERRUPT(EXCEPTION_RESERVED_2)); - exceptionSetRoutine(EXCEPTION_RESERVED_3, ACCESS_INTERRUPT(EXCEPTION_RESERVED_3)); - exceptionSetRoutine(EXCEPTION_RESERVED_4, ACCESS_INTERRUPT(EXCEPTION_RESERVED_4)); - exceptionSetRoutine(EXCEPTION_RESERVED_5, ACCESS_INTERRUPT(EXCEPTION_RESERVED_5)); - exceptionSetRoutine(EXCEPTION_RESERVED_6, ACCESS_INTERRUPT(EXCEPTION_RESERVED_6)); - exceptionSetRoutine(EXCEPTION_RESERVED_7, ACCESS_INTERRUPT(EXCEPTION_RESERVED_7)); - exceptionSetRoutine(EXCEPTION_RESERVED_8, ACCESS_INTERRUPT(EXCEPTION_RESERVED_8)); - exceptionSetRoutine(EXCEPTION_RESERVED_9, ACCESS_INTERRUPT(EXCEPTION_RESERVED_9)); - exceptionSetRoutine(EXCEPTION_RESERVED_10, ACCESS_INTERRUPT(EXCEPTION_RESERVED_10)); - exceptionSetRoutine(EXCEPTION_SECURITY, ACCESS_INTERRUPT(EXCEPTION_SECURITY)); - exceptionSetRoutine(EXCEPTION_RESERVED_11, ACCESS_INTERRUPT(EXCEPTION_RESERVED_11)); - - exceptionSetRoutine(EXCEPTION_PAGE_FAULT, pagefault_handler); // Enabling the HW interrupts + exceptionSetup(); asm volatile("sti\n"); printf("Setting up Serial link (115200)\n"); serialSetup(115200); - allocInit(); + allocSetup(); kthreadSetup(_stack_bottom, (_stack_top - _stack_bottom + 1)); kthreadCreate("idle", idleThread, NULL); irqSetRoutine(IRQ_TIMER, pit_handler);