From 79f7b736e66a6fb4a2ed646ad6ed85ebdd88c81f Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 22 Apr 2019 20:15:32 +0200 Subject: [PATCH] idt in progress --- Makefile | 13 +++- ProjectTree | 7 +- boot/loader/loader.asm | 1 + include/kernel/boot.h | 1 + include/kernel/cpu.h | 73 ++++++++++++++++--- kaleid/kernel/cpu/handlers.asm | 39 ----------- kaleid/kernel/cpu/idt.c | 123 ++++++++++++++++++++++++--------- kaleid/kernel/init/info.c | 3 +- kaleid/kernel/init/init.c | 9 ++- 9 files changed, 180 insertions(+), 89 deletions(-) delete mode 100644 kaleid/kernel/cpu/handlers.asm diff --git a/Makefile b/Makefile index 2484a8f..5116980 100644 --- a/Makefile +++ b/Makefile @@ -119,7 +119,7 @@ $(KOBJDIR)/libc/atoi.o: $(KALEIDDIR)/libc/atoi.c $(INCLUDEDIR)/*/*.h | $(KOBJDIR @$(KCC) -D_NEED_ATOL $< -o $@.2 @$(KCC) -D_NEED_ATOU $< -o $@.3 @$(KCC) -D_NEED_ATOUL $< -o $@.4 - @$(LD) -r $@.1 $@.2 $@.3 $@.4 -o $@ + @$(LD) $(LDFLAGS) -r $@.1 $@.2 $@.3 $@.4 -o $@ @rm -f $@.1 $@.2 $@.3 $@.4 @echo ${CL2}[$@] ${CL}Compiled.${CL3} @@ -129,7 +129,7 @@ $(KOBJDIR)/libc/itoa.o: $(KALEIDDIR)/libc/itoa.c $(INCLUDEDIR)/*/*.h | $(KOBJDIR @$(KCC) -D_NEED_LTOA $< -o $@.2 @$(KCC) -D_NEED_UTOA $< -o $@.3 @$(KCC) -D_NEED_ULTOA $< -o $@.4 - @$(LD) -r $@.1 $@.2 $@.3 $@.4 -o $@ + @$(LD) $(LDFLAGS) -r $@.1 $@.2 $@.3 $@.4 -o $@ @rm -f $@.1 $@.2 $@.3 $@.4 @echo ${CL2}[$@] ${CL}Compiled.${CL3} @@ -138,6 +138,15 @@ $(KOBJDIR)/libc/mem.o: $(KALEIDDIR)/libc/mem.c $(INCLUDEDIR)/*/*.h | $(KOBJDIR) @$(KCC) -fno-strict-aliasing $< -o $@ @echo ${CL2}[$@] ${CL}Compiled.${CL3} +$(KOBJDIR)/kernel/cpu/idt.o: $(KALEIDDIR)/kernel/cpu/idt.c \ + $(KALEIDDIR)/kernel/cpu/isr.asm $(INCLUDEDIR)/*/*.h | $(KOBJDIR) + @mkdir -p $(shell dirname $@) + @$(ASM) $(ASMFLAGS) $(KALEIDDIR)/kernel/cpu/isr.asm -o $@.1 + @$(KCC) $< -o $@.2 + @$(LD) $(LDFLAGS) -r $@.1 $@.2 -o $@ + @rm -f $@.1 $@.2 + @echo ${CL2}[$@] ${CL}Compiled.${CL3} + ## MAIN MAKEFILE ------------------------------------------------------------- # $(KOBJDIR)/%.o: %.c $(INCLUDEDIR)/*/*.h | $(KOBJDIR) diff --git a/ProjectTree b/ProjectTree index f8fa114..df11590 100644 --- a/ProjectTree +++ b/ProjectTree @@ -132,8 +132,11 @@ │   │   └── prog.c │   ├── kernel │   │   ├── cpu +│   │   │   ├── cpu.asm │   │   │   ├── cpuid.c -│   │   │   └── idt.c +│   │   │   ├── handlers.asm +│   │   │   ├── idt.c +│   │   │   └── isr.inc │   │   ├── init │   │   │   ├── info.c │   │   │   ├── init.c @@ -174,4 +177,4 @@ ├── ProjectTree └── README.md -37 directories, 112 files +37 directories, 115 files diff --git a/boot/loader/loader.asm b/boot/loader/loader.asm index 2b87b3f..001d524 100644 --- a/boot/loader/loader.asm +++ b/boot/loader/loader.asm @@ -172,6 +172,7 @@ _loader64: mov qword [newKernelEnd], KERNEL_STACK mov rdi, [mbInfo] mov rsi, [mbMagic] + mov rdx, GDT64.code call BtStartKern ;; We must never reach this point ------------------------------------------- ;; diff --git a/include/kernel/boot.h b/include/kernel/boot.h index f59f332..6be8bef 100644 --- a/include/kernel/boot.h +++ b/include/kernel/boot.h @@ -46,6 +46,7 @@ struct BootInfo_t void *modulesAddr; //mods_addr char *grubName; //boot_loader_name void *kernelAddr; + void *codeSegment; void *kernelEndAddr; } btldr; diff --git a/include/kernel/cpu.h b/include/kernel/cpu.h index b80e23d..df27efa 100644 --- a/include/kernel/cpu.h +++ b/include/kernel/cpu.h @@ -33,6 +33,8 @@ typedef struct IdtDescriptor_t IdtDescriptor_t; typedef struct IdtEntry_t IdtEntry_t; +typedef struct IdtPtr_t IdtPtr_t; +typedef struct Registers_t Registers_t; // -------------------------------------------------------------------------- // // @@ -97,30 +99,79 @@ enum { FEAT_EDX_PBE = 1 << 31 }; -typedef struct IdtDescriptor_t -{ +struct IdtDescriptor_t { ushort limit; ulong base; -} __attribute__((packed)); +} __attribute__((packed)) ; -typedef struct IdtEntry_t +struct IdtEntry_t { ushort baseLow; ushort selector; uchar reservedIst; uchar flags; - ushort baseMiddle; + ushort baseMid; uint baseHigh; uint reserved; } __attribute__((packed)); -// -------------------------------------------------------------------------- // - -typedef struct { - ushort length; - void* base; - } __attribute__((packed)) Idtr_t; +struct IdtPtr_t +{ + ushort limit; + void *base; + } __attribute__((packed)); // -------------------------------------------------------------------------- // + +struct Registers_t +{ + ulong ds; + ulong rdi, rsi, rbp, rsp, rbx, rdx, rcx, rax; + ulong intNo, errCode; + ulong rip, cs, eflags, useresp, ss; +} __attribute__((packed)); + +// -------------------------------------------------------------------------- // + +void idtSetup(void); +void idtSet(uchar rank, ulong base, ushort selector, uchar flags); +void isrHandler(Registers_t reg); + +// -------------------------------------------------------------------------- // + +extern void idtInit(); +extern void isr0(); +extern void isr1(); +extern void isr2(); +extern void isr3(); +extern void isr4(); +extern void isr5(); +extern void isr6(); +extern void isr7(); +extern void isr8(); +extern void isr9(); +extern void isr10(); +extern void isr11(); +extern void isr12(); +extern void isr13(); +extern void isr14(); +extern void isr15(); +extern void isr16(); +extern void isr17(); +extern void isr18(); +extern void isr19(); +extern void isr20(); +extern void isr21(); +extern void isr22(); +extern void isr23(); +extern void isr24(); +extern void isr25(); +extern void isr26(); +extern void isr27(); +extern void isr28(); +extern void isr29(); +extern void isr30(); +extern void isr31(); + #endif diff --git a/kaleid/kernel/cpu/handlers.asm b/kaleid/kernel/cpu/handlers.asm deleted file mode 100644 index 8ef69dd..0000000 --- a/kaleid/kernel/cpu/handlers.asm +++ /dev/null @@ -1,39 +0,0 @@ -;=----------------------------------------------------------------------------=; -; GNU GPL OS/K ; -; ; -; Desc: ; -; ; -; ; -; Copyright © 2018-2019 The OS/K Team ; -; ; -; This file is part of OS/K. ; -; ; -; OS/K is free software: you can redistribute it and/or modify ; -; it under the terms of the GNU General Public License as published by ; -; the Free Software Foundation, either version 3 of the License, or ; -; (at your option) any later version. ; -; ; -; OS/K is distributed in the hope that it will be useful, ; -; but WITHOUT ANY WARRANTY; without even the implied warranty of ; -; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; -; GNU General Public License for more details. ; -; ; -; You should have received a copy of the GNU General Public License ; -; along with OS/K. If not, see . ; -;=----------------------------------------------------------------------------=; - -;; Divide Error Fault - -;; Debug Exception Fault/trap - -;; NMI Interrupt - -;; Breakpoint Trap - -;; Overflow Trap - -;; Bound Range Exceeded Fault - -;; - - diff --git a/kaleid/kernel/cpu/idt.c b/kaleid/kernel/cpu/idt.c index 1e3549d..5c3eebb 100644 --- a/kaleid/kernel/cpu/idt.c +++ b/kaleid/kernel/cpu/idt.c @@ -1,41 +1,102 @@ //----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Desc: Interrupt related functions // -// // -// // -// Copyright © 2018-2019 The OS/K Team // -// // -// This file is part of OS/K. // -// // -// OS/K is free software: you can redistribute it and/or modify // -// it under the terms of the GNU General Public License as published by // -// the Free Software Foundation, either version 3 of the License, or // -// any later version. // -// // -// OS/K is distributed in the hope that it will be useful, // -// but WITHOUT ANY WARRANTY//without even the implied warranty of // -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // -// GNU General Public License for more details. // -// // -// You should have received a copy of the GNU General Public License // -// along with OS/K. If not, see . // +// GNU GPL OS/K // +// // +// Desc: Interrupt related functions // +// // +// // +// Copyright © 2018-2019 The OS/K Team // +// // +// This file is part of OS/K. // +// // +// OS/K is free software: you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation, either version 3 of the License, or // +// any later version. // +// // +// OS/K is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY//without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with OS/K. If not, see . // //----------------------------------------------------------------------------// #include #include +#include -extern void lidt(Idtr_t reg); +IdtEntry_t idt[256] = { 0 }; +IdtPtr_t idtPtr; -// -// Registers the new idt in the idtr register. -// - -static inline void loadIdt(void* idtAddr, ushort size) +void isrHandlerPrint(Registers_t regs) { - // The IDTR register structure that will be sent - Idtr_t IDTR = { size, idtAddr }; - - lidt(IDTR); + DebugLog("Interrupt %d !!! \n", regs.intNo); } +void idtSetup(void) +{ + ushort codeSeg = (ushort)(ulong)BtLoaderInfo.codeSegment; + + // Set IDT ptr + idtPtr.limit = (sizeof(IdtEntry_t) * 256) - 1; + idtPtr.base = &idt; + + // Set IDT gates + idtSet(0, (ulong)isr0, codeSeg, 0x8E); + idtSet(1, (ulong)isr1, codeSeg, 0x8E); + idtSet(2, (ulong)isr2, codeSeg, 0x8E); + idtSet(3, (ulong)isr3, codeSeg, 0x8E); + idtSet(4, (ulong)isr4, codeSeg, 0x8E); + idtSet(5, (ulong)isr5, codeSeg, 0x8E); + idtSet(6, (ulong)isr6, codeSeg, 0x8E); + idtSet(7, (ulong)isr7, codeSeg, 0x8E); + idtSet(8, (ulong)isr8, codeSeg, 0x8E); + idtSet(9, (ulong)isr9, codeSeg, 0x8E); + idtSet(10, (ulong)isr10, codeSeg, 0x8E); + idtSet(11, (ulong)isr11, codeSeg, 0x8E); + idtSet(12, (ulong)isr12, codeSeg, 0x8E); + idtSet(13, (ulong)isr13, codeSeg, 0x8E); + idtSet(14, (ulong)isr14, codeSeg, 0x8E); + //idtSet(15, (ulong)isr15, codeSeg, 0x8E); INTEL RESERVED + idtSet(16, (ulong)isr16, codeSeg, 0x8E); + idtSet(17, (ulong)isr17, codeSeg, 0x8E); + idtSet(18, (ulong)isr18, codeSeg, 0x8E); + idtSet(19, (ulong)isr19, codeSeg, 0x8E); + idtSet(20, (ulong)isr20, codeSeg, 0x8E); + //idtSet(21, (ulong)isr21, codeSeg, 0x8E); INTEL RESERVED + //idtSet(22, (ulong)isr22, codeSeg, 0x8E); INTEL RESERVED + //idtSet(23, (ulong)isr23, codeSeg, 0x8E); INTEL RESERVED + //idtSet(24, (ulong)isr24, codeSeg, 0x8E); INTEL RESERVED + //idtSet(25, (ulong)isr25, codeSeg, 0x8E); INTEL RESERVED + //idtSet(26, (ulong)isr26, codeSeg, 0x8E); INTEL RESERVED + //idtSet(27, (ulong)isr27, codeSeg, 0x8E); INTEL RESERVED + //idtSet(28, (ulong)isr28, codeSeg, 0x8E); INTEL RESERVED + //idtSet(29, (ulong)isr29, codeSeg, 0x8E); INTEL RESERVED + //idtSet(30, (ulong)isr30, codeSeg, 0x8E); INTEL RESERVED + + // Load IDT + idtInit(); + DebugLog("[IdtSetup] Filled at %d with %d bytes\n", sizeof(idtPtr), sizeof(IdtEntry_t)); + DebugLog("[IdtSetup] offset %p with %p bytes\n", (ulong)(&idtPtr), (ulong)(idt)); + //asm volatile ("sti":::"memory"); + DebugLog(" And initialized !\n"); + + asm volatile ("int %0" : : "N" (0) : "cc", "memory"); +} + +void idtSet(uchar rank, ulong base, ushort selector, uchar flags) +{ + // Set Base Address + idt[rank].baseLow = base & 0xFFFF; + idt[rank].baseMid = (base >> 16) & 0xFFFF; + idt[rank].baseHigh = (base >> 32) & 0xFFFFFFFF; + + // Set Selector + idt[rank].selector = selector; + idt[rank].flags = flags; + + // Set Reserved Areas to Zero + idt[rank].reservedIst = 0; + idt[rank].reserved = 0; +} diff --git a/kaleid/kernel/init/info.c b/kaleid/kernel/init/info.c index d12fc5e..861d90f 100644 --- a/kaleid/kernel/init/info.c +++ b/kaleid/kernel/init/info.c @@ -29,7 +29,7 @@ // BootInfo_t initialization. It is necessary because grub will potentially be // wiped since it is below 1MB.... And we must reorganize all that stuff. // -void BtInitBootInfo(multiboot_info_t *mbi) +void BtInitBootInfo(multiboot_info_t *mbi, void *codeSeg) { extern ulong MB_header; extern ulong newKernelEnd; @@ -44,6 +44,7 @@ void BtInitBootInfo(multiboot_info_t *mbi) BtLoaderInfo.grubName = (char*)(ulong)(mbi->boot_loader_name); BtLoaderInfo.kernelAddr = (void*)&MB_header; BtLoaderInfo.kernelEndAddr = (void*)newKernelEnd; + BtLoaderInfo.codeSegment = codeSeg; BtLoaderInfo.valid = 1; } if (BtLoaderInfo.grubFlags & MULTIBOOT_INFO_MODS) { diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 6b07c02..e3bd6c0 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -28,10 +28,11 @@ #include #include #include +#include // info.c extern void BtDoSanityChecks(uint mbMagic); -extern void BtInitBootInfo(multiboot_info_t *mbi); +extern void BtInitBootInfo(multiboot_info_t *mbi, void *codeSeg); // io/vga.c extern error_t IoInitVGABuffer(void); @@ -42,12 +43,12 @@ extern void pstest(void); // // Entry point of the Kaleid kernel // -noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic) +noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) { KeDisableIRQs(); // Initialize the BootInfo_t structure - BtInitBootInfo(mbInfo); + BtInitBootInfo(mbInfo, codeSeg); // Screen I/O available from this point on IoInitVGABuffer(); @@ -68,6 +69,8 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic) MmPrintMemoryMap(); + idtSetup(); + // End this machine's suffering BFlushBuf(BStdOut); KeCrashSystem();