From 0118bfb796621f0e3cf14586bd06ae2affcbd7b6 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 24 Apr 2019 21:08:44 +0200 Subject: [PATCH] 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* "); +}