From 090bfc3c8e49c005875086173ad3bc5318a66589 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 1 Apr 2019 15:13:45 +0200 Subject: [PATCH 01/25] mem_map bug --- kaleid/kernel/init/init.c | 1 + kaleid/kernel/mm/map.c | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 31e46cb..208efb0 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -154,6 +154,7 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic) //int i = 0; //while(i < 512) { KernLog("%d\n", i++);} + MmPrintMemoryMap(); PsInitSched(); diff --git a/kaleid/kernel/mm/map.c b/kaleid/kernel/mm/map.c index 3d39401..8738e04 100644 --- a/kaleid/kernel/mm/map.c +++ b/kaleid/kernel/mm/map.c @@ -100,10 +100,10 @@ static error_t InitMemoryMap(void) if (memoryMap.freeRamSize < MINIMUM_RAM_SIZE) return ENOMEM; - KernLog("[InitMemoryMap] Available Ram Size : %u Mio, Used Ram Size : %u Kio\n", + DebugLog("[InitMemoryMap] Available Ram Size : %u Mio, Used Ram Size : %u Kio\n\n", memoryMap.freeRamSize / MB, memoryMap.nonfreeRamSize / KB); - KernLog("[InitMemoryMap] Physical Ram Size : %d Mio\n\n", - (memoryMap.freeRamSize + memoryMap.nonfreeRamSize) / MB); + /*DebugLog("[InitMemoryMap] Physical Ram Size : %d Mio\n\n", + (memoryMap.freeRamSize + memoryMap.nonfreeRamSize) / MB);*/ return EOK; } From 4b06043e3f3e66b0fd61cedf795d361dc2c7a0f7 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 1 Apr 2019 17:58:58 +0200 Subject: [PATCH 02/25] Bug resolving --- kaleid/kernel/mm/map.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/kaleid/kernel/mm/map.c b/kaleid/kernel/mm/map.c index 8738e04..96afb64 100644 --- a/kaleid/kernel/mm/map.c +++ b/kaleid/kernel/mm/map.c @@ -68,9 +68,10 @@ static error_t InitMemoryMap(void) // fill the map while (currentEntry < mapEnd) { + // memory zone address - memoryMap.entry[i].addr = (void*)((ulong)currentEntry->addr_low + - (((ulong)currentEntry->addr_high) << 32 )); + memoryMap.entry[i].addr = (void*)((ullong)currentEntry->addr_low + + (((ullong)currentEntry->addr_high) << 32 )); // memory zone size in bytes memoryMap.entry[i].length = (ulong)currentEntry->len_low + (((ulong)currentEntry->len_high) << 32); @@ -82,6 +83,10 @@ static error_t InitMemoryMap(void) currentEntry = (multiboot_memory_map_t*) ((ulong)currentEntry + currentEntry->size + sizeof(currentEntry->size)); i++; + + //DebugLog("addr high : %x, %x\n", currentEntry->addr_high,(ulong)currentEntry->addr_high); + DebugLog("addr shift %x\n", ((currentEntry->addr_high) << 32)); + } DebugLog("[InitMemoryMap] %d entries detected in the memory map\n", From 5f58ffb35a33148910f0518e766db6883dd076f8 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 1 Apr 2019 20:42:06 +0200 Subject: [PATCH 03/25] the bug is in the % modifiers processing --- kaleid/kernel/mm/map.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/kaleid/kernel/mm/map.c b/kaleid/kernel/mm/map.c index 96afb64..061f897 100644 --- a/kaleid/kernel/mm/map.c +++ b/kaleid/kernel/mm/map.c @@ -83,10 +83,6 @@ static error_t InitMemoryMap(void) currentEntry = (multiboot_memory_map_t*) ((ulong)currentEntry + currentEntry->size + sizeof(currentEntry->size)); i++; - - //DebugLog("addr high : %x, %x\n", currentEntry->addr_high,(ulong)currentEntry->addr_high); - DebugLog("addr shift %x\n", ((currentEntry->addr_high) << 32)); - } DebugLog("[InitMemoryMap] %d entries detected in the memory map\n", @@ -197,7 +193,7 @@ void MmPrintMemoryMap(void) { default:; } - KernLog("Mem zone : %p\t%s\twith length: %d Kio\n", + KernLog("Mem zone : %lx\t%s\twith length: %d Kio\n", memoryMap.entry[i].addr, avStr, memoryMap.entry[i].length / KB From 774b1a5f04dcfc1ef63e8437161cf5c6e3dff415 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 1 Apr 2019 22:07:23 +0200 Subject: [PATCH 04/25] test --- kaleid/kernel/mm/map.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kaleid/kernel/mm/map.c b/kaleid/kernel/mm/map.c index 061f897..97a2ae7 100644 --- a/kaleid/kernel/mm/map.c +++ b/kaleid/kernel/mm/map.c @@ -193,7 +193,7 @@ void MmPrintMemoryMap(void) { default:; } - KernLog("Mem zone : %lx\t%s\twith length: %d Kio\n", + KernLog("Mem zone : %lp\t%s\twith length: %d Kio\n", memoryMap.entry[i].addr, avStr, memoryMap.entry[i].length / KB From 0614070cfc2d8906b6deaeea47464c265552b7e2 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 1 Apr 2019 23:51:48 +0200 Subject: [PATCH 05/25] idt stuff --- kaleid/include/kernel/cpu.h | 22 ++++++++++++++++++++++ kaleid/kernel/cpu/idt.c | 20 ++++++++++++-------- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/kaleid/include/kernel/cpu.h b/kaleid/include/kernel/cpu.h index 29ebc8f..b157c3e 100644 --- a/kaleid/include/kernel/cpu.h +++ b/kaleid/include/kernel/cpu.h @@ -31,6 +31,11 @@ // -------------------------------------------------------------------------- // +typedef struct IdtDescriptor_t IdtDescriptor_t; +typedef struct IdtEntry_t IdtEntry_t; + +// -------------------------------------------------------------------------- // +// // CPU features masks enum { FEAT_ECX_SSE3 = 1 << 0, @@ -92,6 +97,23 @@ enum { FEAT_EDX_PBE = 1 << 31 }; +typedef struct IdtDescriptor_t +{ + ushort limit; + ulong base; +} __attribute__((packed)); + +typedef struct IdtEntry_t +{ + ushort baseLow; + ushort selector; + uchar reservedIst; + uchar flags; + ushort baseMiddle; + uint baseHigh; + uint reserved; +} __attribute__((packed)); + // -------------------------------------------------------------------------- // #endif diff --git a/kaleid/kernel/cpu/idt.c b/kaleid/kernel/cpu/idt.c index 469796c..2b76398 100644 --- a/kaleid/kernel/cpu/idt.c +++ b/kaleid/kernel/cpu/idt.c @@ -23,16 +23,20 @@ //----------------------------------------------------------------------------// +idtp + // // Registers the new idt in the idtr register. // -static inline void lidt(void* idtAddr, ushort size) +static inline void lidt(IdtDescriptor_t idtDesc) { - // The IDTR register structure that will be sent - struct { - ushort length; - void* base; - } __attribute__((packed)) IDTR = { size, idtAddr }; - - asm volatile( "lidt %0" : : "m"(IDTR) ); + asm volatile( "lidt %0" : : "m"(idtDesc) ); +} + +// +// Initializes the IDT +// +error_t CpuInitIdt(void) +{ + } From 082a1b82d818857ea679d494d90a6c05021f6ccb Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 1 Apr 2019 23:53:36 +0200 Subject: [PATCH 06/25] idt stuff --- kaleid/kernel/cpu/cpuid.c | 1 + kaleid/kernel/cpu/idt.c | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kaleid/kernel/cpu/cpuid.c b/kaleid/kernel/cpu/cpuid.c index d1f756b..ef89b6e 100644 --- a/kaleid/kernel/cpu/cpuid.c +++ b/kaleid/kernel/cpu/cpuid.c @@ -22,6 +22,7 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// +#include char *KeGetVendorString(void) { return "Null"; diff --git a/kaleid/kernel/cpu/idt.c b/kaleid/kernel/cpu/idt.c index 2b76398..444fef6 100644 --- a/kaleid/kernel/cpu/idt.c +++ b/kaleid/kernel/cpu/idt.c @@ -22,8 +22,7 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// - -idtp +#include // // Registers the new idt in the idtr register. @@ -38,5 +37,5 @@ static inline void lidt(IdtDescriptor_t idtDesc) // error_t CpuInitIdt(void) { - + return error_t stub; } From d5f30ded6695923ac368c9a536d68fe740dc515d Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sat, 13 Apr 2019 09:30:32 +0200 Subject: [PATCH 07/25] modifs --- kaleid/kernel/cpu/idt.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kaleid/kernel/cpu/idt.c b/kaleid/kernel/cpu/idt.c index 444fef6..7404cb8 100644 --- a/kaleid/kernel/cpu/idt.c +++ b/kaleid/kernel/cpu/idt.c @@ -35,7 +35,8 @@ static inline void lidt(IdtDescriptor_t idtDesc) // // Initializes the IDT // -error_t CpuInitIdt(void) +static error_t InitIDT(void) { - return error_t stub; + error_t stub; + return stub; } From 37535f835219fdd27fd098f5ab4a52af382e8526 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sat, 13 Apr 2019 09:30:40 +0200 Subject: [PATCH 08/25] modifs --- kaleid/kernel/cpu/cpu.asm | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 kaleid/kernel/cpu/cpu.asm diff --git a/kaleid/kernel/cpu/cpu.asm b/kaleid/kernel/cpu/cpu.asm new file mode 100644 index 0000000..3a21db0 --- /dev/null +++ b/kaleid/kernel/cpu/cpu.asm @@ -0,0 +1,28 @@ +;=----------------------------------------------------------------------------=; +; 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 . ; +;=----------------------------------------------------------------------------=; + +[BITS 64] +global idtDesc + + From 79f7b736e66a6fb4a2ed646ad6ed85ebdd88c81f Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 22 Apr 2019 20:15:32 +0200 Subject: [PATCH 09/25] 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(); From 3fa203b6b1aced308d9fc61eda1f0a0e9704482c Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 22 Apr 2019 20:15:48 +0200 Subject: [PATCH 10/25] idt in progress --- kaleid/kernel/cpu/isr.asm | 124 ++++++++++++++++++++++++++++++++++++++ kaleid/kernel/cpu/isr.inc | 59 ++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 kaleid/kernel/cpu/isr.asm create mode 100644 kaleid/kernel/cpu/isr.inc diff --git a/kaleid/kernel/cpu/isr.asm b/kaleid/kernel/cpu/isr.asm new file mode 100644 index 0000000..2f779d5 --- /dev/null +++ b/kaleid/kernel/cpu/isr.asm @@ -0,0 +1,124 @@ +;=----------------------------------------------------------------------------=; +; GNU GPL OS/K ; +; ; +; Desc: Interrupt Descriptor Table 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 ; +; (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 . ; +;=----------------------------------------------------------------------------=; + +%include "kaleid/kernel/cpu/isr.inc" + +global idtInit +extern idtPtr +extern isrHandlerPrint + +;; +;; Loads the IDT +;; +idtInit: + lidt [idtPtr] + ret + +;; +;; ISR handler +;; +isrHandler: + pushAll + + mov ax, ds + push rax + + mov ax, 0x10 + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + + call isrHandlerPrint + + pop rbx + mov ds, bx + mov es, bx + mov fs, bx + mov gs, bx + + popAll + add rsp, 8 + ;sti + iretq + +;; Divide Error Fault +IsrWithoutErrCode 0 + +;; Debug Exception Fault/trap +IsrWithoutErrCode 1 + +;; NMI Interrupt +IsrWithoutErrCode 2 + +;; Breakpoint Trap +IsrWithoutErrCode 3 + +;; Overflow Trap +IsrWithoutErrCode 4 + +;; Bound Range Exceeded Fault +IsrWithoutErrCode 5 + +;; Invalid Opcode Fault +IsrWithoutErrCode 6 + +;; Device Not Available or No Math Coprocessor Fault +IsrWithoutErrCode 7 + +;; Coprocessor Segment Overrun Fault +IsrWithoutErrCode 9 + +;; x87 FPU Floating Point or Math Fault +IsrWithoutErrCode 16 + +;; Alignment Check Fault +IsrWithoutErrCode 17 + +;; Machine Check Abort +IsrWithoutErrCode 18 + +;; SIMD Floating Point Fault +IsrWithoutErrCode 19 + +;; Virtualization Exception Fault +IsrWithoutErrCode 20 + +;; Double Fault Abort +IsrWithErrCode 8 + +;; Invalid TSS Fault +IsrWithErrCode 10 + +;; Segment Not Present Fault +IsrWithErrCode 11 + +;; Stack Segment Fault +IsrWithErrCode 12 + +;; General Protection Fault +IsrWithErrCode 13 + +;; Page Fault +IsrWithErrCode 14 diff --git a/kaleid/kernel/cpu/isr.inc b/kaleid/kernel/cpu/isr.inc new file mode 100644 index 0000000..c50e4ab --- /dev/null +++ b/kaleid/kernel/cpu/isr.inc @@ -0,0 +1,59 @@ +;=----------------------------------------------------------------------------=; +; GNU GPL OS/K ; +; ; +; Desc: Interrupt Descriptor Table related macros ; +; ; +; ; +; 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 . ; +;=----------------------------------------------------------------------------=; +%macro pushAll 0 + push rax + push rcx + push rdx + push rbx + push rbp + push rsi + push rdi +%endmacro + +%macro popAll 0 + pop rdi + pop rsi + pop rbp + pop rbx + pop rdx + pop rcx + pop rax +%endmacro + +%macro IsrWithoutErrCode 1 + global isr%1 + isr%1: + cli + push byte 0 + push byte %1 + jmp isrHandler +%endmacro + +%macro IsrWithErrCode 1 + global isr%1 + isr%1: + cli + push byte %1 + jmp isrHandler +%endmacro From 468c97ba1c6bba60d1cec241e849052c3bf3cfc6 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 22 Apr 2019 20:16:50 +0200 Subject: [PATCH 11/25] clean-up --- ProjectTree | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProjectTree b/ProjectTree index df11590..c992fb1 100644 --- a/ProjectTree +++ b/ProjectTree @@ -134,8 +134,8 @@ │   │   ├── cpu │   │   │   ├── cpu.asm │   │   │   ├── cpuid.c -│   │   │   ├── handlers.asm │   │   │   ├── idt.c +│   │   │   ├── isr.asm │   │   │   └── isr.inc │   │   ├── init │   │   │   ├── info.c From ca6c7bbcf993b2e64caec1af2bd1a86adb8fdbbe Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 22 Apr 2019 22:32:21 +0200 Subject: [PATCH 12/25] quasi functionnal idt --- Makefile | 2 +- include/kernel/cpu.h | 3 ++- kaleid/kernel/cpu/idt.c | 48 ++++++++++++++++++++++++++++++--------- kaleid/kernel/cpu/isr.asm | 23 +++++++------------ kaleid/kernel/cpu/isr.inc | 4 ++-- kaleid/kernel/init/init.c | 11 +++++++-- 6 files changed, 59 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index 5116980..8f95abd 100644 --- a/Makefile +++ b/Makefile @@ -169,7 +169,7 @@ debug: all gdb: all @qemu-system-x86_64 -m 64M -hda $(BUILDDIR)/bin/disk.img -no-reboot \ - -no-shutdown -d cpu_reset,guest_errors,pcall,int -s 2> $(BUILDDIR)/qemu.log & + -no-shutdown -d cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log & @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > kaleid64_disasm.asm @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > kaleid32_disasm.asm @gdb \ diff --git a/include/kernel/cpu.h b/include/kernel/cpu.h index df27efa..59a0b9b 100644 --- a/include/kernel/cpu.h +++ b/include/kernel/cpu.h @@ -133,9 +133,10 @@ struct Registers_t // -------------------------------------------------------------------------- // -void idtSetup(void); +void CpuIdtSetup(void); void idtSet(uchar rank, ulong base, ushort selector, uchar flags); void isrHandler(Registers_t reg); +void disablePIC(void); // -------------------------------------------------------------------------- // diff --git a/kaleid/kernel/cpu/idt.c b/kaleid/kernel/cpu/idt.c index 5c3eebb..6e73209 100644 --- a/kaleid/kernel/cpu/idt.c +++ b/kaleid/kernel/cpu/idt.c @@ -25,17 +25,15 @@ #include #include #include +#include IdtEntry_t idt[256] = { 0 }; IdtPtr_t idtPtr; -void isrHandlerPrint(Registers_t regs) +void CpuIdtSetup(void) { - DebugLog("Interrupt %d !!! \n", regs.intNo); -} + disablePIC(); -void idtSetup(void) -{ ushort codeSeg = (ushort)(ulong)BtLoaderInfo.codeSegment; // Set IDT ptr @@ -76,13 +74,9 @@ void idtSetup(void) //idtSet(30, (ulong)isr30, codeSeg, 0x8E); INTEL RESERVED // Load IDT + DebugLog("[IdtSetup] Filled \n"); 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"); + DebugLog("[IdtSetup] Initialized !\n"); } void idtSet(uchar rank, ulong base, ushort selector, uchar flags) @@ -100,3 +94,35 @@ void idtSet(uchar rank, ulong base, ushort selector, uchar flags) idt[rank].reservedIst = 0; idt[rank].reserved = 0; } + +void disablePIC(void) { + // Set ICW1 + IoWriteByteOnPort(0x20, 0x11); + IoWriteByteOnPort(0xa0, 0x11); + + // Set ICW2 (IRQ base offsets) + IoWriteByteOnPort(0x21, 0xe0); + IoWriteByteOnPort(0xa1, 0xe8); + + // Set ICW3 + IoWriteByteOnPort(0x21, 4); + IoWriteByteOnPort(0xa1, 2); + + // Set ICW4 + IoWriteByteOnPort(0x21, 1); + IoWriteByteOnPort(0xa1, 1); + + // Set OCW1 (interrupt masks) + IoWriteByteOnPort(0x21, 0xff); + IoWriteByteOnPort(0xa1, 0xff); + + // ENABLING LOCAL APIC + uint *val = (void*)0xfee000f0; + *val |= (1<<8); +} + +void isrHandler(Registers_t regs) +{ + DebugLog("Interrupt %d !!! \n", regs.intNo); + return; +} diff --git a/kaleid/kernel/cpu/isr.asm b/kaleid/kernel/cpu/isr.asm index 2f779d5..cc9744d 100644 --- a/kaleid/kernel/cpu/isr.asm +++ b/kaleid/kernel/cpu/isr.asm @@ -26,41 +26,34 @@ global idtInit extern idtPtr -extern isrHandlerPrint +extern isrHandler ;; ;; Loads the IDT ;; idtInit: lidt [idtPtr] + sti ret ;; ;; ISR handler ;; -isrHandler: +isrPreHandler: pushAll + xor rax, rax mov ax, ds push rax - mov ax, 0x10 + call isrHandler + + pop rax mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - - call isrHandlerPrint - - pop rbx - mov ds, bx - mov es, bx - mov fs, bx - mov gs, bx popAll add rsp, 8 - ;sti + sti iretq ;; Divide Error Fault diff --git a/kaleid/kernel/cpu/isr.inc b/kaleid/kernel/cpu/isr.inc index c50e4ab..dcb3921 100644 --- a/kaleid/kernel/cpu/isr.inc +++ b/kaleid/kernel/cpu/isr.inc @@ -47,7 +47,7 @@ cli push byte 0 push byte %1 - jmp isrHandler + jmp isrPreHandler %endmacro %macro IsrWithErrCode 1 @@ -55,5 +55,5 @@ isr%1: cli push byte %1 - jmp isrHandler + jmp isrPreHandler %endmacro diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index e3bd6c0..5b0a905 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -40,6 +40,12 @@ extern error_t IoInitVGABuffer(void); // ps/proc.c test function extern void pstest(void); +void bug(void) +{ + asm volatile ("int %0" : : "N" (0) : "cc", "memory"); +} + + // // Entry point of the Kaleid kernel // @@ -67,9 +73,10 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) MmInitHeap(); PsInitSched(); - MmPrintMemoryMap(); + //MmPrintMemoryMap(); + CpuIdtSetup(); - idtSetup(); + bug(); // End this machine's suffering BFlushBuf(BStdOut); From 3dbe85b936a23e0bcc2165f10a6a3b50cadddc25 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 23 Apr 2019 17:06:03 +0200 Subject: [PATCH 13/25] Idt in progress ! --- include/kernel/cpu.h | 43 ++++++++++++- kaleid/kernel/cpu/idt.c | 124 +++++++++++++++++++++----------------- kaleid/kernel/cpu/isr.asm | 21 +++++-- kaleid/kernel/cpu/isr.inc | 5 +- kaleid/kernel/init/init.c | 11 +--- 5 files changed, 131 insertions(+), 73 deletions(-) diff --git a/include/kernel/cpu.h b/include/kernel/cpu.h index 59a0b9b..a1f7fa0 100644 --- a/include/kernel/cpu.h +++ b/include/kernel/cpu.h @@ -37,7 +37,9 @@ typedef struct IdtPtr_t IdtPtr_t; typedef struct Registers_t Registers_t; // -------------------------------------------------------------------------- // -// + +#define interrupt(n) asm volatile ("int %0" : : "N" (n) : "cc", "memory") \ + // CPU features masks enum { FEAT_ECX_SSE3 = 1 << 0, @@ -99,6 +101,41 @@ enum { FEAT_EDX_PBE = 1 << 31 }; +static const char *IsrExceptions[32] = { + "Divide Error Fault", + "Debug Exception Trap", + "Non-maskable Interrupt", + "Breakpoint Trap", + "Overflow Trap", + "Bound Range Exceeded Fault", + "Invalid Opcode Fault", + "Device Not Available or No Math Coprocessor Fault", + "Double Fault Abort", + "Coprocessor Segment Overrun Fault", + "Invalid TSS Fault", + "Segment Not Present Fault", + "Stack Segment fault", + "General Protection Fault", + "Page Fault", + "Reserved", + "x87 FPU Floating Point or Math Fault", + "Alignment Check Fault", + "Machine Check Abort", + "SIMD Floating Point Fault", + "Virtualization Exception Fault", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved" +}; + struct IdtDescriptor_t { ushort limit; ulong base; @@ -134,8 +171,8 @@ struct Registers_t // -------------------------------------------------------------------------- // void CpuIdtSetup(void); -void idtSet(uchar rank, ulong base, ushort selector, uchar flags); -void isrHandler(Registers_t reg); +void IdtSetGate(uchar rank, ulong base, ushort selector, uchar flags); +void IdtHandler(ulong intNo); void disablePIC(void); // -------------------------------------------------------------------------- // diff --git a/kaleid/kernel/cpu/idt.c b/kaleid/kernel/cpu/idt.c index 6e73209..6a9192b 100644 --- a/kaleid/kernel/cpu/idt.c +++ b/kaleid/kernel/cpu/idt.c @@ -1,37 +1,39 @@ //----------------------------------------------------------------------------// -// 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 #include +#include IdtEntry_t idt[256] = { 0 }; IdtPtr_t idtPtr; void CpuIdtSetup(void) { + // XXX detect the APIC with cpuid ! disablePIC(); ushort codeSeg = (ushort)(ulong)BtLoaderInfo.codeSegment; @@ -41,37 +43,37 @@ void CpuIdtSetup(void) 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 + IdtSetGate(0, (ulong)isr0, codeSeg, 0x8E); + IdtSetGate(1, (ulong)isr1, codeSeg, 0x8E); + IdtSetGate(2, (ulong)isr2, codeSeg, 0x8E); + IdtSetGate(3, (ulong)isr3, codeSeg, 0x8E); + IdtSetGate(4, (ulong)isr4, codeSeg, 0x8E); + IdtSetGate(5, (ulong)isr5, codeSeg, 0x8E); + IdtSetGate(6, (ulong)isr6, codeSeg, 0x8E); + IdtSetGate(7, (ulong)isr7, codeSeg, 0x8E); + IdtSetGate(8, (ulong)isr8, codeSeg, 0x8E); + IdtSetGate(9, (ulong)isr9, codeSeg, 0x8E); + IdtSetGate(10, (ulong)isr10, codeSeg, 0x8E); + IdtSetGate(11, (ulong)isr11, codeSeg, 0x8E); + IdtSetGate(12, (ulong)isr12, codeSeg, 0x8E); + IdtSetGate(13, (ulong)isr13, codeSeg, 0x8E); + IdtSetGate(14, (ulong)isr14, codeSeg, 0x8E); + IdtSetGate(15, (ulong)isr15, codeSeg, 0x8E); // INTEL RESERVED + IdtSetGate(16, (ulong)isr16, codeSeg, 0x8E); + IdtSetGate(17, (ulong)isr17, codeSeg, 0x8E); + IdtSetGate(18, (ulong)isr18, codeSeg, 0x8E); + IdtSetGate(19, (ulong)isr19, codeSeg, 0x8E); + IdtSetGate(20, (ulong)isr20, codeSeg, 0x8E); + IdtSetGate(21, (ulong)isr21, codeSeg, 0x8E); // INTEL RESERVED + IdtSetGate(22, (ulong)isr22, codeSeg, 0x8E); // INTEL RESERVED + IdtSetGate(23, (ulong)isr23, codeSeg, 0x8E); // INTEL RESERVED + IdtSetGate(24, (ulong)isr24, codeSeg, 0x8E); // INTEL RESERVED + IdtSetGate(25, (ulong)isr25, codeSeg, 0x8E); // INTEL RESERVED + IdtSetGate(26, (ulong)isr26, codeSeg, 0x8E); // INTEL RESERVED + IdtSetGate(27, (ulong)isr27, codeSeg, 0x8E); // INTEL RESERVED + IdtSetGate(28, (ulong)isr28, codeSeg, 0x8E); // INTEL RESERVED + IdtSetGate(29, (ulong)isr29, codeSeg, 0x8E); // INTEL RESERVED + IdtSetGate(30, (ulong)isr30, codeSeg, 0x8E); // INTEL RESERVED // Load IDT DebugLog("[IdtSetup] Filled \n"); @@ -79,7 +81,7 @@ void CpuIdtSetup(void) DebugLog("[IdtSetup] Initialized !\n"); } -void idtSet(uchar rank, ulong base, ushort selector, uchar flags) +void IdtSetGate(uchar rank, ulong base, ushort selector, uchar flags) { // Set Base Address idt[rank].baseLow = base & 0xFFFF; @@ -121,8 +123,20 @@ void disablePIC(void) { *val |= (1<<8); } -void isrHandler(Registers_t regs) +void IdtHandler(ulong intNo) { - DebugLog("Interrupt %d !!! \n", regs.intNo); + int irrecoverable = 0; + char *exceptionMsg = "Unhandled ISR exception"; + + if (intNo == 6 || intNo == 8 || intNo == 13) irrecoverable++; + + if (intNo < 32) exceptionMsg = IsrExceptions[intNo]; + + if (irrecoverable) { + KeStartPanic("Irrecoverable exception 0x%x : %s\n", intNo, exceptionMsg); + } else { + bprintf(BStdOut, "Exception 0x%x : %s\n", intNo, exceptionMsg); + } + return; } diff --git a/kaleid/kernel/cpu/isr.asm b/kaleid/kernel/cpu/isr.asm index cc9744d..760cd91 100644 --- a/kaleid/kernel/cpu/isr.asm +++ b/kaleid/kernel/cpu/isr.asm @@ -26,14 +26,13 @@ global idtInit extern idtPtr -extern isrHandler +extern IdtHandler ;; ;; Loads the IDT ;; idtInit: lidt [idtPtr] - sti ret ;; @@ -46,13 +45,12 @@ isrPreHandler: mov ax, ds push rax - call isrHandler + call IdtHandler pop rax mov ds, ax popAll - add rsp, 8 sti iretq @@ -115,3 +113,18 @@ IsrWithErrCode 13 ;; Page Fault IsrWithErrCode 14 + +;; Reserved +IsrWithoutErrCode 15 +IsrWithoutErrCode 21 +IsrWithoutErrCode 22 +IsrWithoutErrCode 23 +IsrWithoutErrCode 24 +IsrWithoutErrCode 25 +IsrWithoutErrCode 26 +IsrWithoutErrCode 27 +IsrWithoutErrCode 28 +IsrWithoutErrCode 29 +IsrWithoutErrCode 30 +IsrWithoutErrCode 31 +IsrWithoutErrCode 32 diff --git a/kaleid/kernel/cpu/isr.inc b/kaleid/kernel/cpu/isr.inc index dcb3921..8f65def 100644 --- a/kaleid/kernel/cpu/isr.inc +++ b/kaleid/kernel/cpu/isr.inc @@ -45,8 +45,7 @@ global isr%1 isr%1: cli - push byte 0 - push byte %1 + mov rdi, %1 jmp isrPreHandler %endmacro @@ -54,6 +53,6 @@ global isr%1 isr%1: cli - push byte %1 + mov rdi, %1 jmp isrPreHandler %endmacro diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 5b0a905..2b676ec 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -40,12 +40,6 @@ extern error_t IoInitVGABuffer(void); // ps/proc.c test function extern void pstest(void); -void bug(void) -{ - asm volatile ("int %0" : : "N" (0) : "cc", "memory"); -} - - // // Entry point of the Kaleid kernel // @@ -75,9 +69,10 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) //MmPrintMemoryMap(); CpuIdtSetup(); + KeEnableIRQs(); - bug(); - + long a = -1; + DebugLog("%s", a); // End this machine's suffering BFlushBuf(BStdOut); KeCrashSystem(); From a3f14eb54e79e3aa817151aac90dbbff4687df3c Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 23 Apr 2019 17:07:12 +0200 Subject: [PATCH 14/25] Idt in progress ! --- kaleid/kernel/init/init.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 2b676ec..e669c71 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -71,8 +71,7 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) CpuIdtSetup(); KeEnableIRQs(); - long a = -1; - DebugLog("%s", a); + interrupt(76); // End this machine's suffering BFlushBuf(BStdOut); KeCrashSystem(); From 1cacf1a4d01b0a05fefc166e9d53a3e6ebb229e8 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 23 Apr 2019 17:08:43 +0200 Subject: [PATCH 15/25] Idt in progress ! --- kaleid/kernel/init/init.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index e669c71..6b69541 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -71,7 +71,7 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) CpuIdtSetup(); KeEnableIRQs(); - interrupt(76); + interrupt(30); // End this machine's suffering BFlushBuf(BStdOut); KeCrashSystem(); From 02ded3d5ae6fedb8abe3099082925018cc2444ad Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 23 Apr 2019 17:38:50 +0200 Subject: [PATCH 16/25] Idt in progress ! --- include/kernel/cpu.h | 24 ++++++++++++------------ kaleid/kernel/cpu/idt.c | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/kernel/cpu.h b/include/kernel/cpu.h index a1f7fa0..114f135 100644 --- a/include/kernel/cpu.h +++ b/include/kernel/cpu.h @@ -117,23 +117,23 @@ static const char *IsrExceptions[32] = { "Stack Segment fault", "General Protection Fault", "Page Fault", - "Reserved", + "Intel Reserved", "x87 FPU Floating Point or Math Fault", "Alignment Check Fault", "Machine Check Abort", "SIMD Floating Point Fault", "Virtualization Exception Fault", - "Reserved", - "Reserved", - "Reserved", - "Reserved", - "Reserved", - "Reserved", - "Reserved", - "Reserved", - "Reserved", - "Reserved", - "Reserved" + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved" }; struct IdtDescriptor_t { diff --git a/kaleid/kernel/cpu/idt.c b/kaleid/kernel/cpu/idt.c index 6a9192b..f2a0e1d 100644 --- a/kaleid/kernel/cpu/idt.c +++ b/kaleid/kernel/cpu/idt.c @@ -135,7 +135,7 @@ void IdtHandler(ulong intNo) if (irrecoverable) { KeStartPanic("Irrecoverable exception 0x%x : %s\n", intNo, exceptionMsg); } else { - bprintf(BStdOut, "Exception 0x%x : %s\n", intNo, exceptionMsg); + bprintf(BStdOut, "[ISR 0x%x] %s\n", intNo, exceptionMsg); } return; From 0cf1367e7ea2952f699e5ce01daba55c505c3a41 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 24 Apr 2019 00:05:03 +0200 Subject: [PATCH 17/25] Exception are now fully supported, working on keyboard and rtc --- Makefile | 1 + include/kernel/base.h | 1 + include/kernel/cpu.h | 92 ++++++-------------------- kaleid/kernel/cpu/cpu.asm | 28 -------- kaleid/kernel/cpu/idt.c | 132 ++++++++++++++++++++++++++++++++++--- kaleid/kernel/cpu/isr.asm | 18 ++++- kaleid/kernel/init/init.c | 18 ++++- kaleid/kernel/init/table.c | 2 + kaleid/kernel/mm/gdt.c | 1 + 9 files changed, 182 insertions(+), 111 deletions(-) delete mode 100644 kaleid/kernel/cpu/cpu.asm diff --git a/Makefile b/Makefile index 8f95abd..a9e35c8 100644 --- a/Makefile +++ b/Makefile @@ -94,6 +94,7 @@ KernSources = libbuf/buf.c libbuf/bput.c \ kernel/mm/heap.c kernel/mm/malloc.c \ kernel/mm/gdt.c kernel/ps/sched.c \ kernel/init/info.c kernel/init/ssp.c \ + kernel/cpu/rtc.c LibCObj=$(patsubst %.c,$(KOBJDIR)/%.o,$(LibCSources)) diff --git a/include/kernel/base.h b/include/kernel/base.h index 1b9e585..3f49c89 100644 --- a/include/kernel/base.h +++ b/include/kernel/base.h @@ -43,6 +43,7 @@ typedef struct BootInfo_t BootInfo_t; typedef struct ListHead_t ListHead_t; typedef struct ListNode_t ListNode_t; typedef struct Processor_t Processor_t; +typedef struct IRQList_t IRQList_t; typedef enum ProcState_t ProcState_t; diff --git a/include/kernel/cpu.h b/include/kernel/cpu.h index 114f135..953152b 100644 --- a/include/kernel/cpu.h +++ b/include/kernel/cpu.h @@ -35,6 +35,7 @@ typedef struct IdtDescriptor_t IdtDescriptor_t; typedef struct IdtEntry_t IdtEntry_t; typedef struct IdtPtr_t IdtPtr_t; typedef struct Registers_t Registers_t; +typedef struct IRQList_t IRQList_t; // -------------------------------------------------------------------------- // @@ -101,41 +102,6 @@ enum { FEAT_EDX_PBE = 1 << 31 }; -static const char *IsrExceptions[32] = { - "Divide Error Fault", - "Debug Exception Trap", - "Non-maskable Interrupt", - "Breakpoint Trap", - "Overflow Trap", - "Bound Range Exceeded Fault", - "Invalid Opcode Fault", - "Device Not Available or No Math Coprocessor Fault", - "Double Fault Abort", - "Coprocessor Segment Overrun Fault", - "Invalid TSS Fault", - "Segment Not Present Fault", - "Stack Segment fault", - "General Protection Fault", - "Page Fault", - "Intel Reserved", - "x87 FPU Floating Point or Math Fault", - "Alignment Check Fault", - "Machine Check Abort", - "SIMD Floating Point Fault", - "Virtualization Exception Fault", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved" -}; - struct IdtDescriptor_t { ushort limit; ulong base; @@ -154,9 +120,22 @@ struct IdtEntry_t struct IdtPtr_t { - ushort limit; - void *base; - } __attribute__((packed)); + ushort limit; + void *base; +} __attribute__((packed)); + +struct IRQList_t +{ + uchar n; //number of entries in the list + + struct entry { + void (*isr)(void); + uchar irq; + ulong base; + ushort selector; + uchar flags; + } entry[225]; +}; // -------------------------------------------------------------------------- // @@ -170,46 +149,15 @@ struct Registers_t // -------------------------------------------------------------------------- // +void CpuRegisterIrq(void (*isr)(void), uchar irq, uchar flags); void CpuIdtSetup(void); void IdtSetGate(uchar rank, ulong base, ushort selector, uchar flags); void IdtHandler(ulong intNo); void disablePIC(void); -// -------------------------------------------------------------------------- // +void CpuEnableRtc(void); -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/cpu.asm b/kaleid/kernel/cpu/cpu.asm deleted file mode 100644 index 3a21db0..0000000 --- a/kaleid/kernel/cpu/cpu.asm +++ /dev/null @@ -1,28 +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 . ; -;=----------------------------------------------------------------------------=; - -[BITS 64] -global idtDesc - - diff --git a/kaleid/kernel/cpu/idt.c b/kaleid/kernel/cpu/idt.c index f2a0e1d..eb492f6 100644 --- a/kaleid/kernel/cpu/idt.c +++ b/kaleid/kernel/cpu/idt.c @@ -28,9 +28,97 @@ #include #include +extern void CpuIdtInit(); +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(); + +static char *IsrExceptions[32] = { + "Divide Error Fault", + "Debug Exception Trap", + "Non-maskable Interrupt", + "Breakpoint Trap", + "Overflow Trap", + "Bound Range Exceeded Fault", + "Invalid Opcode Fault", + "Device Not Available or No Math Coprocessor Fault", + "Double Fault Abort", + "Coprocessor Segment Overrun Fault", + "Invalid TSS Fault", + "Segment Not Present Fault", + "Stack Segment fault", + "General Protection Fault", + "Page Fault", + "Intel Reserved", + "x87 FPU Floating Point or Math Fault", + "Alignment Check Fault", + "Machine Check Abort", + "SIMD Floating Point Fault", + "Virtualization Exception Fault", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved" +}; + IdtEntry_t idt[256] = { 0 }; IdtPtr_t idtPtr; +IRQList_t irqList = { 0 }; + + +// +// Registers an isr with his IRQ to handle driver interrupts +// +void CpuRegisterIrq(void (*isr)(void), uchar irq, uchar flags) +{ + KalAssert(idt[0].flags==0); // IDT uninitialized + + irqList.entry[irqList.n].isr = isr; + irqList.entry[irqList.n].irq = irq; + irqList.entry[irqList.n].flags = flags; + irqList.n++; +} + +// +// Installs the IDT in order to activate the interrupts handling +// void CpuIdtSetup(void) { // XXX detect the APIC with cpuid ! @@ -42,7 +130,7 @@ void CpuIdtSetup(void) idtPtr.limit = (sizeof(IdtEntry_t) * 256) - 1; idtPtr.base = &idt; - // Set IDT gates + // Set IDT Exception Gates IdtSetGate(0, (ulong)isr0, codeSeg, 0x8E); IdtSetGate(1, (ulong)isr1, codeSeg, 0x8E); IdtSetGate(2, (ulong)isr2, codeSeg, 0x8E); @@ -75,12 +163,23 @@ void CpuIdtSetup(void) IdtSetGate(29, (ulong)isr29, codeSeg, 0x8E); // INTEL RESERVED IdtSetGate(30, (ulong)isr30, codeSeg, 0x8E); // INTEL RESERVED + // Set the IRQ Driver Gates + for (int i = 0 ; i < irqList.n ; i++) { + IdtSetGate(irqList.entry[irqList.n].irq, + (ulong)irqList.entry[irqList.n].isr, + codeSeg, + irqList.entry[irqList.n].flags + ); + } + // Load IDT - DebugLog("[IdtSetup] Filled \n"); - idtInit(); + CpuIdtInit(); DebugLog("[IdtSetup] Initialized !\n"); } +// +// Set an interrupt gate +// void IdtSetGate(uchar rank, ulong base, ushort selector, uchar flags) { // Set Base Address @@ -97,7 +196,11 @@ void IdtSetGate(uchar rank, ulong base, ushort selector, uchar flags) idt[rank].reserved = 0; } -void disablePIC(void) { +// +// Disables the PIC to activate the APIC +// +void disablePIC(void) +{ // Set ICW1 IoWriteByteOnPort(0x20, 0x11); IoWriteByteOnPort(0xa0, 0x11); @@ -123,20 +226,33 @@ void disablePIC(void) { *val |= (1<<8); } +// +// Ends the current interrupt handling +// +void sendEOItoPIC(uchar isr) +{ + if(isr >= 8) + IoWriteByteOnPort(0xa0,0x20); + + IoWriteByteOnPort(0x20,0x20); +} + +// +// The main exception handler +// void IdtHandler(ulong intNo) { int irrecoverable = 0; char *exceptionMsg = "Unhandled ISR exception"; - if (intNo == 6 || intNo == 8 || intNo == 13) irrecoverable++; + if (intNo == 0 || intNo == 6 || intNo == 8 || intNo == 13) irrecoverable++; if (intNo < 32) exceptionMsg = IsrExceptions[intNo]; if (irrecoverable) { - KeStartPanic("Irrecoverable exception 0x%x : %s\n", intNo, exceptionMsg); + KeStartPanic("[ISR 0x%x] Irrecoverable %s\n", intNo, exceptionMsg); } else { bprintf(BStdOut, "[ISR 0x%x] %s\n", intNo, exceptionMsg); + sendEOItoPIC(intNo); } - - return; } diff --git a/kaleid/kernel/cpu/isr.asm b/kaleid/kernel/cpu/isr.asm index 760cd91..adb6955 100644 --- a/kaleid/kernel/cpu/isr.asm +++ b/kaleid/kernel/cpu/isr.asm @@ -24,17 +24,30 @@ %include "kaleid/kernel/cpu/isr.inc" -global idtInit +global CpuIdtInit +global divideByZero extern idtPtr extern IdtHandler ;; ;; Loads the IDT ;; -idtInit: +CpuIdtInit: lidt [idtPtr] ret +;; +;; Bug test +;; +divideByZero: + pushAll + mov eax, 17 + mov ebx, 0 + xor edx, edx + div ebx + popAll + ret + ;; ;; ISR handler ;; @@ -128,3 +141,4 @@ IsrWithoutErrCode 29 IsrWithoutErrCode 30 IsrWithoutErrCode 31 IsrWithoutErrCode 32 + diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 6b69541..84e0900 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -40,6 +40,14 @@ extern error_t IoInitVGABuffer(void); // ps/proc.c test function extern void pstest(void); +// interrupts tests +extern void divideByZero(void); + +void test(void) +{ + DebugLog("test\n"); +} + // // Entry point of the Kaleid kernel // @@ -67,11 +75,19 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) MmInitHeap(); PsInitSched(); + CpuRegisterIrq(test, 13, 0x8E); + //MmPrintMemoryMap(); CpuIdtSetup(); KeEnableIRQs(); - interrupt(30); + /* // Test Page Fault + long addr = -1; + DebugLog("%s", addr); */ + CpuEnableRtc(); + + KernLog("\nGoodbye!"); + // End this machine's suffering BFlushBuf(BStdOut); KeCrashSystem(); diff --git a/kaleid/kernel/init/table.c b/kaleid/kernel/init/table.c index 0261a15..88a3169 100644 --- a/kaleid/kernel/init/table.c +++ b/kaleid/kernel/init/table.c @@ -31,3 +31,5 @@ volatile BootInfo_t BtBootTab = {0}; volatile bool KeIsPanicking = 0; volatile Processor_t *KeCurCPU = &_KeCPUTable[0]; + + diff --git a/kaleid/kernel/mm/gdt.c b/kaleid/kernel/mm/gdt.c index 06cbc15..e2dbc8f 100644 --- a/kaleid/kernel/mm/gdt.c +++ b/kaleid/kernel/mm/gdt.c @@ -49,6 +49,7 @@ void MmInitGdt(void) gdtPtr.limit = (sizeof(GdtEntry_t) * 5) - 1; gdtPtr.base = (uint)(ullong)&gdtEntries; + SetGdtEntry(0,0,0,0,0); /* XXX set TSS register */ From 6f86a1dc28f39dfe5f6fc59c565840d58fe49022 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 24 Apr 2019 00:05:30 +0200 Subject: [PATCH 18/25] Exception are now fully supported, working on keyboard and rtc --- kaleid/kernel/cpu/rtc.c | 41 ++++++++++++++++++++++++ kaleid/kernel/io/keyb.asm | 67 +++++++++++++++++++++++++++++++++++++++ kaleid/kernel/io/keyb.c | 26 +++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 kaleid/kernel/cpu/rtc.c create mode 100644 kaleid/kernel/io/keyb.asm create mode 100644 kaleid/kernel/io/keyb.c diff --git a/kaleid/kernel/cpu/rtc.c b/kaleid/kernel/cpu/rtc.c new file mode 100644 index 0000000..840cc80 --- /dev/null +++ b/kaleid/kernel/cpu/rtc.c @@ -0,0 +1,41 @@ +//----------------------------------------------------------------------------// +// 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 + +void CpuEnableRtc(void) +{ + ulong flags = KePauseIRQs(); + // Setting up the register control + IoWriteByteOnPort(0x70, 0x8A); //selects status reg A and DISABLE NMI + IoWriteByteOnPort(0x71, 0x20); + //Enabling the IRQ 8 + IoWriteByteOnPort(0x70, 0x8B); + char read = IoReadByteFromPort(0x71); // Because read causes reset + IoWriteByteOnPort(0x70, 0x8B); + IoWriteByteOnPort(0x71, read | 0x40); + KeRestoreIRQs(flags); +} diff --git a/kaleid/kernel/io/keyb.asm b/kaleid/kernel/io/keyb.asm new file mode 100644 index 0000000..a1b4075 --- /dev/null +++ b/kaleid/kernel/io/keyb.asm @@ -0,0 +1,67 @@ +;=----------------------------------------------------------------------------=; +; GNU GPL OS/K ; +; ; +; Desc: Basic Read Only Keyboard Driver ; +; (x86_64 architecture only) ; +; ; +; ; +; 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 . ; +;=----------------------------------------------------------------------------=; + +global KeybIsr +extern KeybHandler + +%macro pushAll 0 + push rax + push rcx + push rdx + push rbx + push rbp + push rsi + push rdi +%endmacro + +%macro popAll 0 + pop rdi + pop rsi + pop rbp + pop rbx + pop rdx + pop rcx + pop rax +%endmacro + +;; +;; Keyboard handler +;; +KeybIsr: + cli + pushAll + + xor rax, rax + mov ax, ds + push rax + + call KeybHandler + + pop rax + mov ds, ax + + popAll + sti + iretq diff --git a/kaleid/kernel/io/keyb.c b/kaleid/kernel/io/keyb.c new file mode 100644 index 0000000..8daf699 --- /dev/null +++ b/kaleid/kernel/io/keyb.c @@ -0,0 +1,26 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Desc: Basic Read Only Keyboard Driver // +// // +// // +// 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 From e739070ca585683bf4fa75a8a185ba21b90b67fd Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 24 Apr 2019 11:40:14 +0200 Subject: [PATCH 19/25] some reorganization --- include/kernel/cpu.h | 163 -------------------------------------- kaleid/kernel/cpu/cpuid.c | 2 +- kaleid/kernel/cpu/idt.c | 83 ++----------------- kaleid/kernel/cpu/rtc.c | 1 - kaleid/kernel/init/init.c | 9 +-- kaleid/kernel/io/keyb.c | 8 ++ 6 files changed, 20 insertions(+), 246 deletions(-) delete mode 100644 include/kernel/cpu.h diff --git a/include/kernel/cpu.h b/include/kernel/cpu.h deleted file mode 100644 index 953152b..0000000 --- a/include/kernel/cpu.h +++ /dev/null @@ -1,163 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Desc: CPU 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 . // -//----------------------------------------------------------------------------// - -#ifndef _KALKERN_BASE_H -#include -#endif - -#ifndef _KALKERN_CPU_H -#define _KALKERN_CPU_H - -// -------------------------------------------------------------------------- // - -typedef struct IdtDescriptor_t IdtDescriptor_t; -typedef struct IdtEntry_t IdtEntry_t; -typedef struct IdtPtr_t IdtPtr_t; -typedef struct Registers_t Registers_t; -typedef struct IRQList_t IRQList_t; - -// -------------------------------------------------------------------------- // - -#define interrupt(n) asm volatile ("int %0" : : "N" (n) : "cc", "memory") \ - -// CPU features masks -enum { - FEAT_ECX_SSE3 = 1 << 0, - FEAT_ECX_PCLMUL = 1 << 1, - FEAT_ECX_DTES64 = 1 << 2, - FEAT_ECX_MONITOR = 1 << 3, - FEAT_ECX_DS_CPL = 1 << 4, - FEAT_ECX_VMX = 1 << 5, - FEAT_ECX_SMX = 1 << 6, - FEAT_ECX_EST = 1 << 7, - FEAT_ECX_TM2 = 1 << 8, - FEAT_ECX_SSSE3 = 1 << 9, - FEAT_ECX_CID = 1 << 10, - FEAT_ECX_FMA = 1 << 12, - FEAT_ECX_CX16 = 1 << 13, - FEAT_ECX_ETPRD = 1 << 14, - FEAT_ECX_PDCM = 1 << 15, - FEAT_ECX_PCIDE = 1 << 17, - FEAT_ECX_DCA = 1 << 18, - FEAT_ECX_SSE4_1 = 1 << 19, - FEAT_ECX_SSE4_2 = 1 << 20, - FEAT_ECX_x2APIC = 1 << 21, - FEAT_ECX_MOVBE = 1 << 22, - FEAT_ECX_POPCNT = 1 << 23, - FEAT_ECX_AES = 1 << 25, - FEAT_ECX_XSAVE = 1 << 26, - FEAT_ECX_OSXSAVE = 1 << 27, - FEAT_ECX_AVX = 1 << 28, - - FEAT_EDX_FPU = 1 << 0, - FEAT_EDX_VME = 1 << 1, - FEAT_EDX_DE = 1 << 2, - FEAT_EDX_PSE = 1 << 3, - FEAT_EDX_TSC = 1 << 4, - FEAT_EDX_MSR = 1 << 5, - FEAT_EDX_PAE = 1 << 6, - FEAT_EDX_MCE = 1 << 7, - FEAT_EDX_CX8 = 1 << 8, - FEAT_EDX_APIC = 1 << 9, - FEAT_EDX_SEP = 1 << 11, - FEAT_EDX_MTRR = 1 << 12, - FEAT_EDX_PGE = 1 << 13, - FEAT_EDX_MCA = 1 << 14, - FEAT_EDX_CMOV = 1 << 15, - FEAT_EDX_PAT = 1 << 16, - FEAT_EDX_PSE36 = 1 << 17, - FEAT_EDX_PSN = 1 << 18, - FEAT_EDX_CLF = 1 << 19, - FEAT_EDX_DTES = 1 << 21, - FEAT_EDX_ACPI = 1 << 22, - FEAT_EDX_MMX = 1 << 23, - FEAT_EDX_FXSR = 1 << 24, - FEAT_EDX_SSE = 1 << 25, - FEAT_EDX_SSE2 = 1 << 26, - FEAT_EDX_SS = 1 << 27, - FEAT_EDX_HTT = 1 << 28, - FEAT_EDX_TM1 = 1 << 29, - FEAT_EDX_IA64 = 1 << 30, - FEAT_EDX_PBE = 1 << 31 -}; - -struct IdtDescriptor_t { - ushort limit; - ulong base; -} __attribute__((packed)) ; - -struct IdtEntry_t -{ - ushort baseLow; - ushort selector; - uchar reservedIst; - uchar flags; - ushort baseMid; - uint baseHigh; - uint reserved; -} __attribute__((packed)); - -struct IdtPtr_t -{ - ushort limit; - void *base; -} __attribute__((packed)); - -struct IRQList_t -{ - uchar n; //number of entries in the list - - struct entry { - void (*isr)(void); - uchar irq; - ulong base; - ushort selector; - uchar flags; - } entry[225]; -}; - -// -------------------------------------------------------------------------- // - -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 CpuRegisterIrq(void (*isr)(void), uchar irq, uchar flags); -void CpuIdtSetup(void); -void IdtSetGate(uchar rank, ulong base, ushort selector, uchar flags); -void IdtHandler(ulong intNo); -void disablePIC(void); - -void CpuEnableRtc(void); - -// -------------------------------------------------------------------------- // - -#endif - diff --git a/kaleid/kernel/cpu/cpuid.c b/kaleid/kernel/cpu/cpuid.c index ef89b6e..c6909d2 100644 --- a/kaleid/kernel/cpu/cpuid.c +++ b/kaleid/kernel/cpu/cpuid.c @@ -22,7 +22,7 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// -#include +#include char *KeGetVendorString(void) { return "Null"; diff --git a/kaleid/kernel/cpu/idt.c b/kaleid/kernel/cpu/idt.c index eb492f6..7c0c86e 100644 --- a/kaleid/kernel/cpu/idt.c +++ b/kaleid/kernel/cpu/idt.c @@ -23,80 +23,11 @@ //----------------------------------------------------------------------------// #include -#include +#include #include #include #include -extern void CpuIdtInit(); -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(); - -static char *IsrExceptions[32] = { - "Divide Error Fault", - "Debug Exception Trap", - "Non-maskable Interrupt", - "Breakpoint Trap", - "Overflow Trap", - "Bound Range Exceeded Fault", - "Invalid Opcode Fault", - "Device Not Available or No Math Coprocessor Fault", - "Double Fault Abort", - "Coprocessor Segment Overrun Fault", - "Invalid TSS Fault", - "Segment Not Present Fault", - "Stack Segment fault", - "General Protection Fault", - "Page Fault", - "Intel Reserved", - "x87 FPU Floating Point or Math Fault", - "Alignment Check Fault", - "Machine Check Abort", - "SIMD Floating Point Fault", - "Virtualization Exception Fault", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved", - "Intel Reserved" -}; - IdtEntry_t idt[256] = { 0 }; IdtPtr_t idtPtr; @@ -106,7 +37,7 @@ IRQList_t irqList = { 0 }; // // Registers an isr with his IRQ to handle driver interrupts // -void CpuRegisterIrq(void (*isr)(void), uchar irq, uchar flags) +void IdtRegisterIrq(void (*isr)(void), uchar irq, uchar flags) { KalAssert(idt[0].flags==0); // IDT uninitialized @@ -119,10 +50,10 @@ void CpuRegisterIrq(void (*isr)(void), uchar irq, uchar flags) // // Installs the IDT in order to activate the interrupts handling // -void CpuIdtSetup(void) +void IdtSetup(void) { // XXX detect the APIC with cpuid ! - disablePIC(); + DisablePIC(); ushort codeSeg = (ushort)(ulong)BtLoaderInfo.codeSegment; @@ -199,7 +130,7 @@ void IdtSetGate(uchar rank, ulong base, ushort selector, uchar flags) // // Disables the PIC to activate the APIC // -void disablePIC(void) +static void DisablePIC(void) { // Set ICW1 IoWriteByteOnPort(0x20, 0x11); @@ -229,7 +160,7 @@ void disablePIC(void) // // Ends the current interrupt handling // -void sendEOItoPIC(uchar isr) +static void SendEOItoPIC(uchar isr) { if(isr >= 8) IoWriteByteOnPort(0xa0,0x20); @@ -253,6 +184,6 @@ void IdtHandler(ulong intNo) KeStartPanic("[ISR 0x%x] Irrecoverable %s\n", intNo, exceptionMsg); } else { bprintf(BStdOut, "[ISR 0x%x] %s\n", intNo, exceptionMsg); - sendEOItoPIC(intNo); + SendEOItoPIC(intNo); } } diff --git a/kaleid/kernel/cpu/rtc.c b/kaleid/kernel/cpu/rtc.c index 840cc80..4be1567 100644 --- a/kaleid/kernel/cpu/rtc.c +++ b/kaleid/kernel/cpu/rtc.c @@ -23,7 +23,6 @@ //----------------------------------------------------------------------------// #include -#include #include void CpuEnableRtc(void) diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 84e0900..7f7452d 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -28,7 +28,6 @@ #include #include #include -#include // info.c extern void BtDoSanityChecks(uint mbMagic); @@ -37,6 +36,9 @@ extern void BtInitBootInfo(multiboot_info_t *mbi, void *codeSeg); // io/vga.c extern error_t IoInitVGABuffer(void); +// cpu/idt.c +extern void IdtSetup(void); + // ps/proc.c test function extern void pstest(void); @@ -75,16 +77,13 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) MmInitHeap(); PsInitSched(); - CpuRegisterIrq(test, 13, 0x8E); - //MmPrintMemoryMap(); - CpuIdtSetup(); + IdtSetup(); KeEnableIRQs(); /* // Test Page Fault long addr = -1; DebugLog("%s", addr); */ - CpuEnableRtc(); KernLog("\nGoodbye!"); diff --git a/kaleid/kernel/io/keyb.c b/kaleid/kernel/io/keyb.c index 8daf699..e676302 100644 --- a/kaleid/kernel/io/keyb.c +++ b/kaleid/kernel/io/keyb.c @@ -24,3 +24,11 @@ #include #include + +extern void KeybIsr(void); + + +void KeybSetup(void) +{ + CpuRegisterIrq(KeybIsr, 0x21, 0x8E); +} From 516a92973bcc34442a1ee6d51c18b0c54df21527 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 24 Apr 2019 11:40:28 +0200 Subject: [PATCH 20/25] some reorganization --- include/kernel/cpuid.h | 112 ++++++++++++++++++++++++++++++++++ include/kernel/idt.h | 132 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 244 insertions(+) create mode 100644 include/kernel/cpuid.h create mode 100644 include/kernel/idt.h diff --git a/include/kernel/cpuid.h b/include/kernel/cpuid.h new file mode 100644 index 0000000..ea63172 --- /dev/null +++ b/include/kernel/cpuid.h @@ -0,0 +1,112 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Desc: CPUID 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 . // +//----------------------------------------------------------------------------// + +#ifndef _KALKERN_BASE_H +#include +#endif + +#ifndef _KALKERN_CPUID_H +#define _KALKERN_CPUID_H + +// -------------------------------------------------------------------------- // + +typedef struct Registers_t Registers_t; + +// -------------------------------------------------------------------------- // + +// CPU features masks +enum { + FEAT_ECX_SSE3 = 1 << 0, + FEAT_ECX_PCLMUL = 1 << 1, + FEAT_ECX_DTES64 = 1 << 2, + FEAT_ECX_MONITOR = 1 << 3, + FEAT_ECX_DS_CPL = 1 << 4, + FEAT_ECX_VMX = 1 << 5, + FEAT_ECX_SMX = 1 << 6, + FEAT_ECX_EST = 1 << 7, + FEAT_ECX_TM2 = 1 << 8, + FEAT_ECX_SSSE3 = 1 << 9, + FEAT_ECX_CID = 1 << 10, + FEAT_ECX_FMA = 1 << 12, + FEAT_ECX_CX16 = 1 << 13, + FEAT_ECX_ETPRD = 1 << 14, + FEAT_ECX_PDCM = 1 << 15, + FEAT_ECX_PCIDE = 1 << 17, + FEAT_ECX_DCA = 1 << 18, + FEAT_ECX_SSE4_1 = 1 << 19, + FEAT_ECX_SSE4_2 = 1 << 20, + FEAT_ECX_x2APIC = 1 << 21, + FEAT_ECX_MOVBE = 1 << 22, + FEAT_ECX_POPCNT = 1 << 23, + FEAT_ECX_AES = 1 << 25, + FEAT_ECX_XSAVE = 1 << 26, + FEAT_ECX_OSXSAVE = 1 << 27, + FEAT_ECX_AVX = 1 << 28, + + FEAT_EDX_FPU = 1 << 0, + FEAT_EDX_VME = 1 << 1, + FEAT_EDX_DE = 1 << 2, + FEAT_EDX_PSE = 1 << 3, + FEAT_EDX_TSC = 1 << 4, + FEAT_EDX_MSR = 1 << 5, + FEAT_EDX_PAE = 1 << 6, + FEAT_EDX_MCE = 1 << 7, + FEAT_EDX_CX8 = 1 << 8, + FEAT_EDX_APIC = 1 << 9, + FEAT_EDX_SEP = 1 << 11, + FEAT_EDX_MTRR = 1 << 12, + FEAT_EDX_PGE = 1 << 13, + FEAT_EDX_MCA = 1 << 14, + FEAT_EDX_CMOV = 1 << 15, + FEAT_EDX_PAT = 1 << 16, + FEAT_EDX_PSE36 = 1 << 17, + FEAT_EDX_PSN = 1 << 18, + FEAT_EDX_CLF = 1 << 19, + FEAT_EDX_DTES = 1 << 21, + FEAT_EDX_ACPI = 1 << 22, + FEAT_EDX_MMX = 1 << 23, + FEAT_EDX_FXSR = 1 << 24, + FEAT_EDX_SSE = 1 << 25, + FEAT_EDX_SSE2 = 1 << 26, + FEAT_EDX_SS = 1 << 27, + FEAT_EDX_HTT = 1 << 28, + FEAT_EDX_TM1 = 1 << 29, + FEAT_EDX_IA64 = 1 << 30, + FEAT_EDX_PBE = 1 << 31 +}; + +// -------------------------------------------------------------------------- // + +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)); + +// -------------------------------------------------------------------------- // + +#endif + diff --git a/include/kernel/idt.h b/include/kernel/idt.h new file mode 100644 index 0000000..d657e34 --- /dev/null +++ b/include/kernel/idt.h @@ -0,0 +1,132 @@ +#ifndef _KALKERN_BASE_H +#include +#endif + +#ifndef _KALKERN_IDT_H +#define _KALKERN_IDT_H + +typedef struct IdtDescriptor_t IdtDescriptor_t; +typedef struct IdtEntry_t IdtEntry_t; +typedef struct IdtPtr_t IdtPtr_t; +typedef struct IRQList_t IRQList_t; + +// -------------------------------------------------------------------------- // + +#define interrupt(n) asm volatile ("int %0" : : "N" (n) : "cc", "memory") \ + +// -------------------------------------------------------------------------- // + +struct IdtDescriptor_t { + ushort limit; + ulong base; +} __attribute__((packed)) ; + +struct IdtEntry_t +{ + ushort baseLow; + ushort selector; + uchar reservedIst; + uchar flags; + ushort baseMid; + uint baseHigh; + uint reserved; +} __attribute__((packed)); + +struct IdtPtr_t +{ + ushort limit; + void *base; +} __attribute__((packed)); + +struct IRQList_t +{ + uchar n; //number of entries in the list + + struct entry { + void (*isr)(void); + uchar irq; + ulong base; + ushort selector; + uchar flags; + } entry[225]; +}; + +static char *IsrExceptions[32] = { + "Divide Error Fault", + "Debug Exception Trap", + "Non-maskable Interrupt", + "Breakpoint Trap", + "Overflow Trap", + "Bound Range Exceeded Fault", + "Invalid Opcode Fault", + "Device Not Available or No Math Coprocessor Fault", + "Double Fault Abort", + "Coprocessor Segment Overrun Fault", + "Invalid TSS Fault", + "Segment Not Present Fault", + "Stack Segment fault", + "General Protection Fault", + "Page Fault", + "Intel Reserved", + "x87 FPU Floating Point or Math Fault", + "Alignment Check Fault", + "Machine Check Abort", + "SIMD Floating Point Fault", + "Virtualization Exception Fault", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved", + "Intel Reserved" +}; + +// -------------------------------------------------------------------------- // + +void IdtRegisterIrq(void (*isr)(void), uchar irq, uchar flags); +void IdtSetup(void); +void IdtSetGate(uchar rank, ulong base, ushort selector, uchar flags); +void IdtHandler(ulong intNo); +static void DisablePIC(void); +static void SendEOItoPIC(uchar isr); + +extern void CpuIdtInit(); +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 From f00589408841a1d5cb66fabf04d8632849968672 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 24 Apr 2019 11:55:15 +0200 Subject: [PATCH 21/25] Keyboard in progress --- kaleid/kernel/io/keyb.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/kaleid/kernel/io/keyb.c b/kaleid/kernel/io/keyb.c index e676302..c0c5f50 100644 --- a/kaleid/kernel/io/keyb.c +++ b/kaleid/kernel/io/keyb.c @@ -26,9 +26,30 @@ #include extern void KeybIsr(void); - +extern void IdtRegisterIrq(void (*isr)(void), uchar irq, uchar flags); void KeybSetup(void) { - CpuRegisterIrq(KeybIsr, 0x21, 0x8E); + IdtRegisterIrq(KeybIsr, 0x21, 0x8E); } + + +uchar KeybHandler(void) +{ + uchar status; + uchar code; + + // write EOI + outb(0x20, 0x20); + + status = IoReadByteFromPort(KeybStatusPort); + + if (status & 0x01) { + code = IoReadByteFromPort(KeybDataPort); + + if(code < 0) return 0; + + return code; + } +} + From dfb65fd19982a40a28a718396242801b3b2093d6 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 24 Apr 2019 18:14:23 +0200 Subject: [PATCH 22/25] Keyboard in progress --- kaleid/kernel/init/init.c | 4 ++-- kaleid/kernel/io/keyb.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 7f7452d..092a3d7 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -81,9 +81,9 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) IdtSetup(); KeEnableIRQs(); - /* // Test Page Fault + // Test Page Fault long addr = -1; - DebugLog("%s", addr); */ + DebugLog("%s", addr); KernLog("\nGoodbye!"); diff --git a/kaleid/kernel/io/keyb.c b/kaleid/kernel/io/keyb.c index c0c5f50..0f38bda 100644 --- a/kaleid/kernel/io/keyb.c +++ b/kaleid/kernel/io/keyb.c @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------// // GNU GPL OS/K // // // -// Desc: Basic Read Only Keyboard Driver // +// Desc: Basic Read Only Keyboard Driver // // // // // // Copyright © 2018-2019 The OS/K Team // @@ -40,7 +40,7 @@ uchar KeybHandler(void) uchar code; // write EOI - outb(0x20, 0x20); + IoWriteByteOnPort(0x20, 0x20); status = IoReadByteFromPort(KeybStatusPort); From 2d610e1ae658e40983b2e95ed4d39f153d1a7d42 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 24 Apr 2019 18:42:38 +0200 Subject: [PATCH 23/25] Keyboard in progress --- Makefile | 20 ++++++++++++++++++- include/kernel/idt.h | 6 +++--- kaleid/kernel/cpu/idt.c | 42 +++++++++++++++++++++------------------ kaleid/kernel/cpu/isr.asm | 4 ++-- kaleid/kernel/cpu/rtc.c | 40 ------------------------------------- kaleid/kernel/init/init.c | 10 ++++------ kaleid/kernel/io/keyb.c | 3 +-- 7 files changed, 52 insertions(+), 73 deletions(-) delete mode 100644 kaleid/kernel/cpu/rtc.c diff --git a/Makefile b/Makefile index a9e35c8..5ee4b89 100644 --- a/Makefile +++ b/Makefile @@ -94,7 +94,7 @@ KernSources = libbuf/buf.c libbuf/bput.c \ kernel/mm/heap.c kernel/mm/malloc.c \ kernel/mm/gdt.c kernel/ps/sched.c \ kernel/init/info.c kernel/init/ssp.c \ - kernel/cpu/rtc.c + kernel/io/rtc.c kernel/io/keyb.c LibCObj=$(patsubst %.c,$(KOBJDIR)/%.o,$(LibCSources)) @@ -148,6 +148,24 @@ $(KOBJDIR)/kernel/cpu/idt.o: $(KALEIDDIR)/kernel/cpu/idt.c \ @rm -f $@.1 $@.2 @echo ${CL2}[$@] ${CL}Compiled.${CL3} +$(KOBJDIR)/kernel/io/keyb.o: $(KALEIDDIR)/kernel/io/keyb.c \ + $(KALEIDDIR)/kernel/io/keyb.asm $(INCLUDEDIR)/*/*.h | $(KOBJDIR) + @mkdir -p $(shell dirname $@) + @$(ASM) $(ASMFLAGS) $(KALEIDDIR)/kernel/io/keyb.asm -o $@.1 + @$(KCC) $< -o $@.2 + @$(LD) $(LDFLAGS) -r $@.1 $@.2 -o $@ + @rm -f $@.1 $@.2 + @echo ${CL2}[$@] ${CL}Compiled.${CL3} + +$(KOBJDIR)/kernel/io/rtc.o: $(KALEIDDIR)/kernel/io/rtc.c \ + $(KALEIDDIR)/kernel/io/rtc.asm $(INCLUDEDIR)/*/*.h | $(KOBJDIR) + @mkdir -p $(shell dirname $@) + @$(ASM) $(ASMFLAGS) $(KALEIDDIR)/kernel/io/rtc.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/include/kernel/idt.h b/include/kernel/idt.h index d657e34..c4eae39 100644 --- a/include/kernel/idt.h +++ b/include/kernel/idt.h @@ -92,10 +92,10 @@ void IdtRegisterIrq(void (*isr)(void), uchar irq, uchar flags); void IdtSetup(void); void IdtSetGate(uchar rank, ulong base, ushort selector, uchar flags); void IdtHandler(ulong intNo); -static void DisablePIC(void); -static void SendEOItoPIC(uchar isr); +static void EnablePIC(void); +void SendEOItoPIC(uchar isr); -extern void CpuIdtInit(); +extern void IdtInit(); extern void isr0(); extern void isr1(); extern void isr2(); diff --git a/kaleid/kernel/cpu/idt.c b/kaleid/kernel/cpu/idt.c index 7c0c86e..960b048 100644 --- a/kaleid/kernel/cpu/idt.c +++ b/kaleid/kernel/cpu/idt.c @@ -39,11 +39,19 @@ IRQList_t irqList = { 0 }; // void IdtRegisterIrq(void (*isr)(void), uchar irq, uchar flags) { + uchar n = irqList.n; + KalAssert(idt[0].flags==0); // IDT uninitialized - irqList.entry[irqList.n].isr = isr; - irqList.entry[irqList.n].irq = irq; - irqList.entry[irqList.n].flags = flags; + if ((n == 225)) // IRQs not filled + KeStartPanic("[IdtRegisterIrq] Cannot register IRQ %c function %p !", + irq, + isr + ); + + irqList.entry[n].isr = isr; + irqList.entry[n].irq = irq; + irqList.entry[n].flags = flags; irqList.n++; } @@ -53,7 +61,7 @@ void IdtRegisterIrq(void (*isr)(void), uchar irq, uchar flags) void IdtSetup(void) { // XXX detect the APIC with cpuid ! - DisablePIC(); + EnablePIC(); ushort codeSeg = (ushort)(ulong)BtLoaderInfo.codeSegment; @@ -104,7 +112,7 @@ void IdtSetup(void) } // Load IDT - CpuIdtInit(); + IdtInit(); DebugLog("[IdtSetup] Initialized !\n"); } @@ -128,39 +136,35 @@ void IdtSetGate(uchar rank, ulong base, ushort selector, uchar flags) } // -// Disables the PIC to activate the APIC +// Enable and initializes the PIC to work correctly // -static void DisablePIC(void) +static void EnablePIC(void) { - // Set ICW1 + // Set ICW1 - begin init of the PIC IoWriteByteOnPort(0x20, 0x11); IoWriteByteOnPort(0xa0, 0x11); // Set ICW2 (IRQ base offsets) - IoWriteByteOnPort(0x21, 0xe0); - IoWriteByteOnPort(0xa1, 0xe8); + IoWriteByteOnPort(0x21, 0x20); //0x20 is the first free interrupt + IoWriteByteOnPort(0xa1, 0x28); // Set ICW3 - IoWriteByteOnPort(0x21, 4); - IoWriteByteOnPort(0xa1, 2); + IoWriteByteOnPort(0x21, 0x0); + IoWriteByteOnPort(0xa1, 0x0); // Set ICW4 - IoWriteByteOnPort(0x21, 1); - IoWriteByteOnPort(0xa1, 1); + IoWriteByteOnPort(0x21, 0x1); + IoWriteByteOnPort(0xa1, 0x1); // Set OCW1 (interrupt masks) IoWriteByteOnPort(0x21, 0xff); IoWriteByteOnPort(0xa1, 0xff); - - // ENABLING LOCAL APIC - uint *val = (void*)0xfee000f0; - *val |= (1<<8); } // // Ends the current interrupt handling // -static void SendEOItoPIC(uchar isr) +void SendEOItoPIC(uchar isr) { if(isr >= 8) IoWriteByteOnPort(0xa0,0x20); diff --git a/kaleid/kernel/cpu/isr.asm b/kaleid/kernel/cpu/isr.asm index adb6955..c8b81fc 100644 --- a/kaleid/kernel/cpu/isr.asm +++ b/kaleid/kernel/cpu/isr.asm @@ -24,7 +24,7 @@ %include "kaleid/kernel/cpu/isr.inc" -global CpuIdtInit +global IdtInit global divideByZero extern idtPtr extern IdtHandler @@ -32,7 +32,7 @@ extern IdtHandler ;; ;; Loads the IDT ;; -CpuIdtInit: +IdtInit: lidt [idtPtr] ret diff --git a/kaleid/kernel/cpu/rtc.c b/kaleid/kernel/cpu/rtc.c deleted file mode 100644 index 4be1567..0000000 --- a/kaleid/kernel/cpu/rtc.c +++ /dev/null @@ -1,40 +0,0 @@ -//----------------------------------------------------------------------------// -// 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 - -void CpuEnableRtc(void) -{ - ulong flags = KePauseIRQs(); - // Setting up the register control - IoWriteByteOnPort(0x70, 0x8A); //selects status reg A and DISABLE NMI - IoWriteByteOnPort(0x71, 0x20); - //Enabling the IRQ 8 - IoWriteByteOnPort(0x70, 0x8B); - char read = IoReadByteFromPort(0x71); // Because read causes reset - IoWriteByteOnPort(0x70, 0x8B); - IoWriteByteOnPort(0x71, read | 0x40); - KeRestoreIRQs(flags); -} diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 092a3d7..8500db5 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -36,6 +36,9 @@ extern void BtInitBootInfo(multiboot_info_t *mbi, void *codeSeg); // io/vga.c extern error_t IoInitVGABuffer(void); +//io/keyb. +extern error_t KeybSetup(void); + // cpu/idt.c extern void IdtSetup(void); @@ -76,15 +79,10 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) // Several inits MmInitHeap(); PsInitSched(); - - //MmPrintMemoryMap(); + KeybSetup(); IdtSetup(); KeEnableIRQs(); - // Test Page Fault - long addr = -1; - DebugLog("%s", addr); - KernLog("\nGoodbye!"); // End this machine's suffering diff --git a/kaleid/kernel/io/keyb.c b/kaleid/kernel/io/keyb.c index 0f38bda..d933fbe 100644 --- a/kaleid/kernel/io/keyb.c +++ b/kaleid/kernel/io/keyb.c @@ -22,11 +22,10 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// -#include #include extern void KeybIsr(void); -extern void IdtRegisterIrq(void (*isr)(void), uchar irq, uchar flags); +extern IdtRegisterIrq(void (*isr)(void), uchar irq, uchar flags); void KeybSetup(void) { From 11792a58f812dfc17475800dcc2432b31a49a603 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 24 Apr 2019 18:43:05 +0200 Subject: [PATCH 24/25] Keyboard in progress --- kaleid/kernel/io/rtc.asm | 24 ++++++++++++++++++++++++ kaleid/kernel/io/rtc.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 kaleid/kernel/io/rtc.asm create mode 100644 kaleid/kernel/io/rtc.c diff --git a/kaleid/kernel/io/rtc.asm b/kaleid/kernel/io/rtc.asm new file mode 100644 index 0000000..93c8d4e --- /dev/null +++ b/kaleid/kernel/io/rtc.asm @@ -0,0 +1,24 @@ +;=----------------------------------------------------------------------------=; +; GNU GPL OS/K ; +; ; +; Desc: Basic Read Only Keyboard Driver ; +; (x86_64 architecture only) ; +; ; +; ; +; 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 . ; +;=----------------------------------------------------------------------------=; diff --git a/kaleid/kernel/io/rtc.c b/kaleid/kernel/io/rtc.c new file mode 100644 index 0000000..4be1567 --- /dev/null +++ b/kaleid/kernel/io/rtc.c @@ -0,0 +1,40 @@ +//----------------------------------------------------------------------------// +// 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 + +void CpuEnableRtc(void) +{ + ulong flags = KePauseIRQs(); + // Setting up the register control + IoWriteByteOnPort(0x70, 0x8A); //selects status reg A and DISABLE NMI + IoWriteByteOnPort(0x71, 0x20); + //Enabling the IRQ 8 + IoWriteByteOnPort(0x70, 0x8B); + char read = IoReadByteFromPort(0x71); // Because read causes reset + IoWriteByteOnPort(0x70, 0x8B); + IoWriteByteOnPort(0x71, read | 0x40); + KeRestoreIRQs(flags); +} From 0118bfb796621f0e3cf14586bd06ae2affcbd7b6 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 24 Apr 2019 21:08:44 +0200 Subject: [PATCH 25/25] RTC finished ! --- Makefile | 7 +++++ include/kernel/idt.h | 4 +-- kaleid/kernel/cpu/idt.c | 16 ++++++----- kaleid/kernel/cpu/isr.asm | 1 - kaleid/kernel/init/init.c | 27 ++++++++++++++---- kaleid/kernel/io/keyb.asm | 2 ++ kaleid/kernel/io/keyb.c | 20 +++++++------ kaleid/kernel/io/rtc.asm | 45 +++++++++++++++++++++++++++++ kaleid/kernel/io/rtc.c | 59 +++++++++++++++++++++++++++++++++------ 9 files changed, 148 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index 5ee4b89..34c5406 100644 --- a/Makefile +++ b/Makefile @@ -197,6 +197,13 @@ gdb: all -ex "symbol-file $(BINDIR)/kaleid" \ -ex "break BtStartKern" \ +ddd: all + @qemu-system-x86_64 -m 64M -hda $(BUILDDIR)/bin/disk.img -no-reboot \ + -no-shutdown -d cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log & + @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > kaleid64_disasm.asm + @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > kaleid32_disasm.asm + @ddd + install_mbr: $(BINDIR)/disk.img $(MBRDIR)/grub.cfg @mkdir -p $(BINDIR)/disk @echo ${CL2}[$@] ${NC}Installing MBR on image...${CL3} diff --git a/include/kernel/idt.h b/include/kernel/idt.h index c4eae39..3083a20 100644 --- a/include/kernel/idt.h +++ b/include/kernel/idt.h @@ -45,10 +45,8 @@ struct IRQList_t struct entry { void (*isr)(void); uchar irq; - ulong base; - ushort selector; uchar flags; - } entry[225]; + } entry[224]; }; static char *IsrExceptions[32] = { diff --git a/kaleid/kernel/cpu/idt.c b/kaleid/kernel/cpu/idt.c index 960b048..082d60a 100644 --- a/kaleid/kernel/cpu/idt.c +++ b/kaleid/kernel/cpu/idt.c @@ -43,7 +43,7 @@ void IdtRegisterIrq(void (*isr)(void), uchar irq, uchar flags) KalAssert(idt[0].flags==0); // IDT uninitialized - if ((n == 225)) // IRQs not filled + if ((n == 224)) // IRQs not filled KeStartPanic("[IdtRegisterIrq] Cannot register IRQ %c function %p !", irq, isr @@ -101,13 +101,15 @@ void IdtSetup(void) IdtSetGate(28, (ulong)isr28, codeSeg, 0x8E); // INTEL RESERVED IdtSetGate(29, (ulong)isr29, codeSeg, 0x8E); // INTEL RESERVED IdtSetGate(30, (ulong)isr30, codeSeg, 0x8E); // INTEL RESERVED + IdtSetGate(31, (ulong)isr31, codeSeg, 0x8E); // INTEL RESERVED // Set the IRQ Driver Gates for (int i = 0 ; i < irqList.n ; i++) { - IdtSetGate(irqList.entry[irqList.n].irq, - (ulong)irqList.entry[irqList.n].isr, + IdtSetGate( + irqList.entry[i].irq, + (ulong)irqList.entry[i].isr, codeSeg, - irqList.entry[irqList.n].flags + irqList.entry[i].flags ); } @@ -166,10 +168,10 @@ static void EnablePIC(void) // void SendEOItoPIC(uchar isr) { - if(isr >= 8) - IoWriteByteOnPort(0xa0,0x20); + if(isr >= 8) + IoWriteByteOnPort(0xa0,0x20); - IoWriteByteOnPort(0x20,0x20); + IoWriteByteOnPort(0x20,0x20); } // diff --git a/kaleid/kernel/cpu/isr.asm b/kaleid/kernel/cpu/isr.asm index c8b81fc..3491e6f 100644 --- a/kaleid/kernel/cpu/isr.asm +++ b/kaleid/kernel/cpu/isr.asm @@ -140,5 +140,4 @@ IsrWithoutErrCode 28 IsrWithoutErrCode 29 IsrWithoutErrCode 30 IsrWithoutErrCode 31 -IsrWithoutErrCode 32 diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 8500db5..4f03219 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -36,8 +36,13 @@ extern void BtInitBootInfo(multiboot_info_t *mbi, void *codeSeg); // io/vga.c extern error_t IoInitVGABuffer(void); -//io/keyb. -extern error_t KeybSetup(void); +//io/keyb.c +extern void IoSetupKeyb(void); + +//io/rtc.c +extern void IoSetupRtc(void); +extern void IoEnableRtc(void); +extern ulong IoRtcTicks; // cpu/idt.c extern void IdtSetup(void); @@ -50,7 +55,7 @@ extern void divideByZero(void); void test(void) { - DebugLog("test\n"); + asm volatile ("hlt"); } // @@ -79,11 +84,23 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) // Several inits MmInitHeap(); PsInitSched(); - KeybSetup(); + + // Drivers ISR inits + IoSetupRtc(); + // Interrupts launching IdtSetup(); KeEnableIRQs(); + // Drivers enabling + IoEnableRtc(); - KernLog("\nGoodbye!"); + while (1) { + asm volatile ("hlt"); + if (IoRtcTicks > 2000) { + KernLog("CC\n"); + break; + } + } + KernLog("\nGoodbye after %d ticks", IoRtcTicks); // End this machine's suffering BFlushBuf(BStdOut); diff --git a/kaleid/kernel/io/keyb.asm b/kaleid/kernel/io/keyb.asm index a1b4075..5128db4 100644 --- a/kaleid/kernel/io/keyb.asm +++ b/kaleid/kernel/io/keyb.asm @@ -23,6 +23,8 @@ ; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; +[BITS 64] + global KeybIsr extern KeybHandler diff --git a/kaleid/kernel/io/keyb.c b/kaleid/kernel/io/keyb.c index d933fbe..6184e16 100644 --- a/kaleid/kernel/io/keyb.c +++ b/kaleid/kernel/io/keyb.c @@ -22,12 +22,14 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// +#include #include extern void KeybIsr(void); -extern IdtRegisterIrq(void (*isr)(void), uchar irq, uchar flags); +extern void IdtRegisterIrq(void (*isr)(void), uchar irq, uchar flags); +extern void SendEOItoPIC(void); -void KeybSetup(void) +void IoSetupKeyb(void) { IdtRegisterIrq(KeybIsr, 0x21, 0x8E); } @@ -35,20 +37,20 @@ void KeybSetup(void) uchar KeybHandler(void) { - uchar status; - uchar code; + char status; + char code = 0; // write EOI IoWriteByteOnPort(0x20, 0x20); - status = IoReadByteFromPort(KeybStatusPort); + status = IoReadByteFromPort(0x64); if (status & 0x01) { - code = IoReadByteFromPort(KeybDataPort); + code = IoReadByteFromPort(0x60); - if(code < 0) return 0; - - return code; + if(code < 0) code = 0; } + + return code; } diff --git a/kaleid/kernel/io/rtc.asm b/kaleid/kernel/io/rtc.asm index 93c8d4e..0457ad4 100644 --- a/kaleid/kernel/io/rtc.asm +++ b/kaleid/kernel/io/rtc.asm @@ -22,3 +22,48 @@ ; You should have received a copy of the GNU General Public License ; ; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; + +[BITS 64] + +global RtcIsr +extern RtcHandler + +%macro pushAll 0 + push rax + push rcx + push rdx + push rbx + push rbp + push rsi + push rdi +%endmacro + +%macro popAll 0 + pop rdi + pop rsi + pop rbp + pop rbx + pop rdx + pop rcx + pop rax +%endmacro + +;; +;; Keyboard handler +;; +RtcIsr: + cli + pushAll + + xor rax, rax + mov ax, ds + push rax + + call RtcHandler + + pop rax + mov ds, ax + + popAll + sti + iretq diff --git a/kaleid/kernel/io/rtc.c b/kaleid/kernel/io/rtc.c index 4be1567..f7d7548 100644 --- a/kaleid/kernel/io/rtc.c +++ b/kaleid/kernel/io/rtc.c @@ -24,17 +24,60 @@ #include #include +#include -void CpuEnableRtc(void) +extern void RtcIsr(void); +extern void SendEOItoPIC(void); +extern void IdtRegisterIrq(void (*isr)(void), uchar irq, uchar flags); + +ulong IoRtcTicks = 0; + +void IoSetupRtc(void) +{ + IdtRegisterIrq(RtcIsr, 0x28, 0x8E); +} + +void IoEnableRtc(void) { ulong flags = KePauseIRQs(); - // Setting up the register control - IoWriteByteOnPort(0x70, 0x8A); //selects status reg A and DISABLE NMI - IoWriteByteOnPort(0x71, 0x20); - //Enabling the IRQ 8 + char readedInterruptConfig; + char readedRegister; + char readedIrqs; + uchar RtcRate; + + // Setting up the register control and interrupt rates + RtcRate = 0x05; + IoWriteByteOnPort(0x70, 0x8B); - char read = IoReadByteFromPort(0x71); // Because read causes reset - IoWriteByteOnPort(0x70, 0x8B); - IoWriteByteOnPort(0x71, read | 0x40); + readedRegister = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x8B); // Because reading flushes + IoWriteByteOnPort(0x71, readedRegister | 0x40); + + IoWriteByteOnPort(0x70, 0x8A); + readedInterruptConfig = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x8A); // Because reading flushes + IoWriteByteOnPort(0x71, (readedInterruptConfig & 0xF0) | RtcRate); + IoWriteByteOnPort(0x70, 0x0C); + IoReadByteFromPort(0x71); // Flush + + // Setting up the IRQs + readedIrqs = IoReadByteFromPort(0xA1); + IoWriteByteOnPort(0xA1, 0xFE & readedIrqs); // Enables IRQ on PIC 2 + readedIrqs = IoReadByteFromPort(0x21); + IoWriteByteOnPort(0x21, 0xFB & readedIrqs); // Enables IRQ on PIC 1 + + // clean-up + IoWriteByteOnPort(0x70, 0x0C); // Select status reg C + IoReadByteFromPort(0x71); // Flush KeRestoreIRQs(flags); } + +void RtcHandler(void) +{ + //bprintf(BStdOut, " *RTC - "); + IoWriteByteOnPort(0x70, 0x0C); // Selects status reg C + IoReadByteFromPort(0x71); // Flush + IoRtcTicks++; + SendEOItoPIC(); + //bprintf(BStdOut, " - EOI* "); +}