From 1e200eea25da30ea46394b4ba0bcaa115635b526 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Tue, 14 May 2019 11:48:07 +0200 Subject: [PATCH] Style update --- Makefile | 2 +- include/asm.h | 6 +- include/ke/idt.h | 18 +-- include/ke/time.h | 26 ++-- include/kernel.h | 3 +- kaleid/kernel/cpu/idt.c | 138 +++++++++-------- kaleid/kernel/cpu/isr.asm | 18 +-- kaleid/kernel/init/init.c | 7 +- kaleid/kernel/init/init.h | 18 +-- kaleid/kernel/io/keyb.c | 9 +- kaleid/kernel/io/spkr.c | 12 +- kaleid/kernel/{io => ke}/rtc.c | 265 ++++++++++++++++++--------------- kaleid/kernel/po/shtdwn.c | 12 +- kaleid/kernel/sh/shcmds.c | 32 ++-- kaleid/libbuf/bgetc.c | 2 +- 15 files changed, 286 insertions(+), 282 deletions(-) rename kaleid/kernel/{io => ke}/rtc.c (50%) diff --git a/Makefile b/Makefile index 627c730..bf3045d 100644 --- a/Makefile +++ b/Makefile @@ -99,7 +99,7 @@ KernSources = kernel/cpu/cpuid.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/io/rtc.c kernel/io/keyb.c \ + kernel/ke/rtc.c kernel/io/keyb.c \ kernel/io/spkr.c kernel/po/shtdwn.c \ kernel/sh/shell.c kernel/sh/shcmds.c diff --git a/include/asm.h b/include/asm.h index 9e6c4d1..f69585c 100644 --- a/include/asm.h +++ b/include/asm.h @@ -61,9 +61,9 @@ static inline ulong KePauseIRQs(void) { return flags; } -extern void IoSendEOItoPIC(uchar isr); -extern void IoEnableNMI(void); -extern void IoDisableNMI(void); +extern void KeSendEOItoPIC(uchar isr); +extern void KeEnableNMI(void); +extern void KeDisableNMI(void); // // Restore IRQ flag to its state before KePauseIRQs diff --git a/include/ke/idt.h b/include/ke/idt.h index 80d13ad..975004c 100644 --- a/include/ke/idt.h +++ b/include/ke/idt.h @@ -65,22 +65,14 @@ struct ISRList_t } entry[255]; }; -typedef struct -{ - -} __attribute__((__packed__)) cpu_state_t; - -extern char *IsrExceptions[32]; - //----------------------------------------------------------------------------// -void IdtInit(void); -void IdtSetup(void); +void KeLoadIDT(void); +void KeSetupIDT(void); -void IoSendEOItoPIC(uchar isr); -void IdtEarlyExceptionHandler(ISRFrame_t *regs); -void IdtSetGate(uchar rank, ulong base, ushort selector, uchar flags); -error_t IdtRegisterIsr(void (*isr)(ISRFrame_t *regs), uchar isrNo); +void KeSendEOItoPIC(uchar isr); +void KeSetIDTGate(uchar rank, ulong base, ushort selector, uchar flags); +error_t KeRegisterISR(void (*isr)(ISRFrame_t *regs), uchar isrNo); //----------------------------------------------------------------------------// diff --git a/include/ke/time.h b/include/ke/time.h index cc3e312..8e87ddd 100644 --- a/include/ke/time.h +++ b/include/ke/time.h @@ -26,12 +26,12 @@ #include #endif -#ifndef _KALKERN_TIME_H -#define _KALKERN_TIME_H +#ifndef _KE_TIME_H +#define _KE_TIME_H //----------------------------------------------------------------------------// -typedef struct +struct Time_t { uchar sec; uchar min; @@ -41,16 +41,18 @@ typedef struct uchar month; uchar year; uchar century; -} __attribute__((packed)) Time_t; +} __attribute__((packed)); + +void KeSetupRTC(void); +void KeEnableRTC(void); + +ulong KeGetTimeStamp(void); +ulong KeGetClockTicks(void); +void KeDelayExecution(uint); + +Time_t *KeGetCurTime(void); +char *KeFormatCurTime(void); -extern void IoSetupRtc(void); -extern void IoEnableRtc(void); -extern void IoPrintRtcTime(void); -extern ulong IoGetRtcTicks(void); -extern Time_t* IoGetRtcTime(void); -extern char* IoGetRtcTimeChar(void); -extern void IoRtcWait(uint time); // time in ms -//static char* WeekDays[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; //----------------------------------------------------------------------------// diff --git a/include/kernel.h b/include/kernel.h index dd2d43b..8e5c271 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -36,6 +36,7 @@ typedef volatile ulong Spinlock_t; typedef Spinlock_t Lock_t; //typedef struct Lock_t Lock_t; +typedef struct Time_t Time_t; typedef struct Buffer_t Buffer_t; typedef struct ListHead_t ListHead_t; typedef struct ListNode_t ListNode_t; @@ -124,8 +125,6 @@ struct ISRFrame_t { ulong ss; } __attribute__((__packed__)); -error_t IdtRegisterIsr(void (*isr)(ISRFrame_t *regs), uchar isrNo); - //----------------------------------------------------------------------------// #ifndef NCPUS diff --git a/kaleid/kernel/cpu/idt.c b/kaleid/kernel/cpu/idt.c index ce34d50..dbfe933 100644 --- a/kaleid/kernel/cpu/idt.c +++ b/kaleid/kernel/cpu/idt.c @@ -27,11 +27,11 @@ #include IdtEntry_t idt[256] = { 0 }; -IdtPtr_t idtPtr; +IdtPtr_t _KeIdtPtr; -ISRList_t isrList = { 0 }; +static ISRList_t isrList = { 0 }; -char *ExceptionsChar[32] = { +static char *ExceptionsChar[32] = { "Divide Error Fault", "Debug Exception Trap", "Non-maskable Interrupt", @@ -67,13 +67,14 @@ char *ExceptionsChar[32] = { }; static void EnablePIC(void); +static void EarlyExceptionHandler(ISRFrame_t *regs); #define interrupt(n) asm volatile ("int %0" : : "N" (n) : "cc", "memory") // // Registers an isr with his IRQ to handle driver interrupts // -error_t IdtRegisterIsr(void (*isr)(ISRFrame_t *regs), uchar isrNo) +error_t KeRegisterISR(void (*isr)(ISRFrame_t *regs), uchar isrNo) { uchar n = isrList.n; int OverWriting = 0; @@ -104,7 +105,7 @@ settingUp: // // Installs the IDT in order to activate the interrupts handling // -void IdtSetup(void) +void KeSetupIDT(void) { // XXX detect the APIC with cpuid ! EnablePIC(); @@ -112,75 +113,75 @@ void IdtSetup(void) ushort codeSeg = (ushort)(ulong)BtLoaderInfo.codeSegment; // Set IDT ptr - idtPtr.limit = (sizeof(IdtEntry_t) * 256) - 1; - idtPtr.base = &idt; + _KeIdtPtr.limit = (sizeof(IdtEntry_t) * 256) - 1; + _KeIdtPtr.base = &idt; // Set IDT Exception Gates - IdtSetGate(0x00, (ulong)isr0, codeSeg, 0x8E); - IdtSetGate(0x01, (ulong)isr1, codeSeg, 0x8E); - IdtSetGate(0x02, (ulong)isr2, codeSeg, 0x8E); - IdtSetGate(0x03, (ulong)isr3, codeSeg, 0x8E); - IdtSetGate(0x04, (ulong)isr4, codeSeg, 0x8E); - IdtSetGate(0x05, (ulong)isr5, codeSeg, 0x8E); - IdtSetGate(0x06, (ulong)isr6, codeSeg, 0x8E); - IdtSetGate(0x07, (ulong)isr7, codeSeg, 0x8E); - IdtSetGate(0x08, (ulong)isr8, codeSeg, 0x8E); - IdtSetGate(0x09, (ulong)isr9, codeSeg, 0x8E); - IdtSetGate(0x0A, (ulong)isr10, codeSeg, 0x8E); - IdtSetGate(0x0B, (ulong)isr11, codeSeg, 0x8E); - IdtSetGate(0x0C, (ulong)isr12, codeSeg, 0x8E); - IdtSetGate(0x0D, (ulong)isr13, codeSeg, 0x8E); - IdtSetGate(0x0E, (ulong)isr14, codeSeg, 0x8E); - IdtSetGate(0x0F, (ulong)isr15, codeSeg, 0x8E); // INTEL RESERVED - IdtSetGate(0x10, (ulong)isr16, codeSeg, 0x8E); - IdtSetGate(0x11, (ulong)isr17, codeSeg, 0x8E); - IdtSetGate(0x12, (ulong)isr18, codeSeg, 0x8E); - IdtSetGate(0x13, (ulong)isr19, codeSeg, 0x8E); - IdtSetGate(0x14, (ulong)isr20, codeSeg, 0x8E); - IdtSetGate(0x15, (ulong)isr21, codeSeg, 0x8E); // INTEL RESERVED - IdtSetGate(0x16, (ulong)isr22, codeSeg, 0x8E); // INTEL RESERVED - IdtSetGate(0x17, (ulong)isr23, codeSeg, 0x8E); // INTEL RESERVED - IdtSetGate(0x18, (ulong)isr24, codeSeg, 0x8E); // INTEL RESERVED - IdtSetGate(0x19, (ulong)isr25, codeSeg, 0x8E); // INTEL RESERVED - IdtSetGate(0x1A, (ulong)isr26, codeSeg, 0x8E); // INTEL RESERVED - IdtSetGate(0x1B, (ulong)isr27, codeSeg, 0x8E); // INTEL RESERVED - IdtSetGate(0x1C, (ulong)isr28, codeSeg, 0x8E); // INTEL RESERVED - IdtSetGate(0x1D, (ulong)isr29, codeSeg, 0x8E); // INTEL RESERVED - IdtSetGate(0x1E, (ulong)isr30, codeSeg, 0x8E); // INTEL RESERVED - IdtSetGate(0x1F, (ulong)isr31, codeSeg, 0x8E); // INTEL RESERVED + KeSetIDTGate(0x00, (ulong)isr0, codeSeg, 0x8E); + KeSetIDTGate(0x01, (ulong)isr1, codeSeg, 0x8E); + KeSetIDTGate(0x02, (ulong)isr2, codeSeg, 0x8E); + KeSetIDTGate(0x03, (ulong)isr3, codeSeg, 0x8E); + KeSetIDTGate(0x04, (ulong)isr4, codeSeg, 0x8E); + KeSetIDTGate(0x05, (ulong)isr5, codeSeg, 0x8E); + KeSetIDTGate(0x06, (ulong)isr6, codeSeg, 0x8E); + KeSetIDTGate(0x07, (ulong)isr7, codeSeg, 0x8E); + KeSetIDTGate(0x08, (ulong)isr8, codeSeg, 0x8E); + KeSetIDTGate(0x09, (ulong)isr9, codeSeg, 0x8E); + KeSetIDTGate(0x0A, (ulong)isr10, codeSeg, 0x8E); + KeSetIDTGate(0x0B, (ulong)isr11, codeSeg, 0x8E); + KeSetIDTGate(0x0C, (ulong)isr12, codeSeg, 0x8E); + KeSetIDTGate(0x0D, (ulong)isr13, codeSeg, 0x8E); + KeSetIDTGate(0x0E, (ulong)isr14, codeSeg, 0x8E); + KeSetIDTGate(0x0F, (ulong)isr15, codeSeg, 0x8E); // INTEL RESERVED + KeSetIDTGate(0x10, (ulong)isr16, codeSeg, 0x8E); + KeSetIDTGate(0x11, (ulong)isr17, codeSeg, 0x8E); + KeSetIDTGate(0x12, (ulong)isr18, codeSeg, 0x8E); + KeSetIDTGate(0x13, (ulong)isr19, codeSeg, 0x8E); + KeSetIDTGate(0x14, (ulong)isr20, codeSeg, 0x8E); + KeSetIDTGate(0x15, (ulong)isr21, codeSeg, 0x8E); // INTEL RESERVED + KeSetIDTGate(0x16, (ulong)isr22, codeSeg, 0x8E); // INTEL RESERVED + KeSetIDTGate(0x17, (ulong)isr23, codeSeg, 0x8E); // INTEL RESERVED + KeSetIDTGate(0x18, (ulong)isr24, codeSeg, 0x8E); // INTEL RESERVED + KeSetIDTGate(0x19, (ulong)isr25, codeSeg, 0x8E); // INTEL RESERVED + KeSetIDTGate(0x1A, (ulong)isr26, codeSeg, 0x8E); // INTEL RESERVED + KeSetIDTGate(0x1B, (ulong)isr27, codeSeg, 0x8E); // INTEL RESERVED + KeSetIDTGate(0x1C, (ulong)isr28, codeSeg, 0x8E); // INTEL RESERVED + KeSetIDTGate(0x1D, (ulong)isr29, codeSeg, 0x8E); // INTEL RESERVED + KeSetIDTGate(0x1E, (ulong)isr30, codeSeg, 0x8E); // INTEL RESERVED + KeSetIDTGate(0x1F, (ulong)isr31, codeSeg, 0x8E); // INTEL RESERVED // Set IDT IRQs Gates - IdtSetGate(0x20, (ulong)isr32, codeSeg, 0x8E); - IdtSetGate(0x21, (ulong)isr33, codeSeg, 0x8E); - IdtSetGate(0x22, (ulong)isr34, codeSeg, 0x8E); - IdtSetGate(0x23, (ulong)isr35, codeSeg, 0x8E); - IdtSetGate(0x24, (ulong)isr36, codeSeg, 0x8E); - IdtSetGate(0x25, (ulong)isr37, codeSeg, 0x8E); - IdtSetGate(0x26, (ulong)isr38, codeSeg, 0x8E); - IdtSetGate(0x27, (ulong)isr39, codeSeg, 0x8E); - IdtSetGate(0x28, (ulong)isr40, codeSeg, 0x8E); - IdtSetGate(0x29, (ulong)isr41, codeSeg, 0x8E); - IdtSetGate(0x2A, (ulong)isr42, codeSeg, 0x8E); - IdtSetGate(0x2B, (ulong)isr43, codeSeg, 0x8E); - IdtSetGate(0x2C, (ulong)isr44, codeSeg, 0x8E); - IdtSetGate(0x2D, (ulong)isr45, codeSeg, 0x8E); - IdtSetGate(0x2E, (ulong)isr46, codeSeg, 0x8E); - IdtSetGate(0x2F, (ulong)isr47, codeSeg, 0x8E); + KeSetIDTGate(0x20, (ulong)isr32, codeSeg, 0x8E); + KeSetIDTGate(0x21, (ulong)isr33, codeSeg, 0x8E); + KeSetIDTGate(0x22, (ulong)isr34, codeSeg, 0x8E); + KeSetIDTGate(0x23, (ulong)isr35, codeSeg, 0x8E); + KeSetIDTGate(0x24, (ulong)isr36, codeSeg, 0x8E); + KeSetIDTGate(0x25, (ulong)isr37, codeSeg, 0x8E); + KeSetIDTGate(0x26, (ulong)isr38, codeSeg, 0x8E); + KeSetIDTGate(0x27, (ulong)isr39, codeSeg, 0x8E); + KeSetIDTGate(0x28, (ulong)isr40, codeSeg, 0x8E); + KeSetIDTGate(0x29, (ulong)isr41, codeSeg, 0x8E); + KeSetIDTGate(0x2A, (ulong)isr42, codeSeg, 0x8E); + KeSetIDTGate(0x2B, (ulong)isr43, codeSeg, 0x8E); + KeSetIDTGate(0x2C, (ulong)isr44, codeSeg, 0x8E); + KeSetIDTGate(0x2D, (ulong)isr45, codeSeg, 0x8E); + KeSetIDTGate(0x2E, (ulong)isr46, codeSeg, 0x8E); + KeSetIDTGate(0x2F, (ulong)isr47, codeSeg, 0x8E); //Setup Early Exception handler for (uchar i = 0 ; i < 0x20 ; i++) { - IdtRegisterIsr(IdtEarlyExceptionHandler, i); + KeRegisterISR(EarlyExceptionHandler, i); } // Load IDT - IdtInit(); + KeLoadIDT(); DebugLog("[IdtSetup] Initialized !\n"); } // // Set an interrupt gate // -void IdtSetGate(uchar rank, ulong base, ushort selector, uchar flags) +void KeSetIDTGate(uchar rank, ulong base, ushort selector, uchar flags) { // Set Base Address idt[rank].baseLow = base & 0xFFFF; @@ -204,15 +205,19 @@ static void EnablePIC(void) // Set ICW1 - begin init of the PIC IoWriteByteOnPort(0x20, 0x11); IoWriteByteOnPort(0xa0, 0x11); + // Set ICW2 (IRQ base offsets) IoWriteByteOnPort(0x21, 0x20); //0x20 is the first free interrupt for IRQ0 IoWriteByteOnPort(0xa1, 0x28); // PIC2 is offseted to 0x28 + // Set ICW3 IoWriteByteOnPort(0x21, 0x4); IoWriteByteOnPort(0xa1, 0x2); + // Set ICW4 IoWriteByteOnPort(0x21, 0x1); IoWriteByteOnPort(0xa1, 0x1); + // Set OCW1 (interrupt masks) IoWriteByteOnPort(0x21, 0xff); IoWriteByteOnPort(0xa1, 0xff); @@ -221,7 +226,7 @@ static void EnablePIC(void) // // Ends the current interrupt handling // -void IoSendEOItoPIC(uchar isr) +void KeSendEOItoPIC(uchar isr) { if(isr >= 8) IoWriteByteOnPort(0xa0,0x20); @@ -229,12 +234,12 @@ void IoSendEOItoPIC(uchar isr) IoWriteByteOnPort(0x20,0x20); } -void IoEnableNMI(void) +void KeEnableNMI(void) { IoWriteByteOnPort(0x70, IoReadByteFromPort(0x70) & 0x7F); } -void IoDisableNMI(void) +void KeDisableNMI(void) { IoWriteByteOnPort(0x70, IoReadByteFromPort(0x70) | 0x80); } @@ -242,13 +247,14 @@ void IoDisableNMI(void) // // The main ISR handler // -void IsrHandler(ISRFrame_t *regs) +void _KeHandleISR(ISRFrame_t *regs) { if ((!regs) || (!regs->rip)) KeStartPanic("[ISR ?] Unknown ISR Exception Abort\n"); if ((regs->intNo >= 0x15) && (regs->intNo <= 0x1F)) return; // INTEL RESERVED + if (regs->intNo == 0x0F) return; // INTEL RESERVED @@ -261,14 +267,13 @@ void IsrHandler(ISRFrame_t *regs) bprintf(BStdOut, "[ISR 0x%x] %s\n", regs->intNo, "Unknown ISR Exception"); BStdOut->flusher(BStdOut); - IoSendEOItoPIC(regs->intNo); + KeSendEOItoPIC(regs->intNo); } - // // Early CPU Exception handler // -void IdtEarlyExceptionHandler(ISRFrame_t *regs) +static void EarlyExceptionHandler(ISRFrame_t *regs) { int recoverable = 0; @@ -309,7 +314,8 @@ void IdtEarlyExceptionHandler(ISRFrame_t *regs) ); } else { - bprintf(BStdOut, "[ISR 0x%x] %s\n", regs->intNo, ExceptionsChar[regs->intNo]); + bprintf(BStdOut, "[ISR %#x] %s\n", + regs->intNo, ExceptionsChar[regs->intNo]); BStdOut->flusher(BStdOut); } } diff --git a/kaleid/kernel/cpu/isr.asm b/kaleid/kernel/cpu/isr.asm index 14b0523..d565eb1 100644 --- a/kaleid/kernel/cpu/isr.asm +++ b/kaleid/kernel/cpu/isr.asm @@ -26,27 +26,23 @@ %include "kaleid/kernel/cpu/cpuf.inc" -global IdtInit +global KeLoadIDT global divideByZero -extern idtPtr -extern IsrHandler -extern label0 -extern KernLog + +extern _KeIdtPtr +extern _KeHandleISR ;; ;; Loads the IDT ;; -IdtInit: - lidt [idtPtr] +KeLoadIDT: + lidt [_KeIdtPtr] ret ;; ;; Bug test ;; -chain db "Salut", 0x0A, 0 divideByZero: - mov rdi, chain - call KernLog ret ;; @@ -71,7 +67,7 @@ isrPreHandler: mov rdi, rsp ; First argument points to the processor state mov rbp, 0 ; Terminate stack traces here - call IsrHandler + call _KeHandleISR ; decrement mask count dec qword [gs:8] diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index beb1af1..3d775d2 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -48,19 +48,16 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) MmInitMemoryMap(); // Interrupts launching - IdtSetup(); + KeSetupIDT(); KeEnableIRQs(); // Several inits MmInitHeap(); // Start drivers - IoEnableRtc(); + KeEnableRTC(); IoEnableKeyb(); - IoGetRtcTimeChar(); - IoPrintRtcTime(); - KeStartShell(); PoShutdown(); diff --git a/kaleid/kernel/init/init.h b/kaleid/kernel/init/init.h index 3781ecd..5aa43f1 100644 --- a/kaleid/kernel/init/init.h +++ b/kaleid/kernel/init/init.h @@ -22,14 +22,16 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// -#include -#include #include +#include +#include +#include +#include +#include +#include +#include #include #include -#include -#include -#include // info.c extern void BtDoSanityChecks(uint mbMagic); @@ -41,12 +43,6 @@ extern error_t IoInitVGABuffer(void); // ke/shell.c extern void KeStartShell(void); -// io/keyb.c -extern void IoEnableKeyb(void); - -// cpu/idt.c -extern void IdtSetup(void); - // ps/proc.c test function extern void pstest(void); diff --git a/kaleid/kernel/io/keyb.c b/kaleid/kernel/io/keyb.c index 190edfd..9504c06 100644 --- a/kaleid/kernel/io/keyb.c +++ b/kaleid/kernel/io/keyb.c @@ -24,6 +24,7 @@ #include #include +#include static char EarlyScanCodes[100] = { 0 }; static char Invisible[100] = { 0 }; @@ -119,7 +120,7 @@ void ScanCodesInit(void) Invisible[0x4D] = 1; } -void KeybHandler(ISRFrame_t *regs) +static void KeybHandler(ISRFrame_t *regs) { char status; char code = 0; @@ -135,7 +136,7 @@ void KeybHandler(ISRFrame_t *regs) if(code < 0) code = 0; } KeybPrint((int)code); - IoSendEOItoPIC(0x21); + KeSendEOItoPIC(0x21); } void IoCreateInputBuffer(void) @@ -154,13 +155,13 @@ void IoEnableKeyb(void) { ulong flags = KePauseIRQs(); - IdtRegisterIsr(KeybHandler, 0x21); + KeRegisterISR(KeybHandler, 0x21); char readedInterruptConfig = IoReadByteFromPort(0x21); IoWriteByteOnPort(0x21, 0xFD & readedInterruptConfig); KeRestoreIRQs(flags); - IoEnableNMI(); + KeEnableNMI(); ScanCodesInit(); IoCreateInputBuffer(); diff --git a/kaleid/kernel/io/spkr.c b/kaleid/kernel/io/spkr.c index adbc24c..7221b66 100644 --- a/kaleid/kernel/io/spkr.c +++ b/kaleid/kernel/io/spkr.c @@ -49,24 +49,16 @@ void IoQuietSpeaker(void) void IoDoBeep(void) { - IoStartSpeaker(1000); - - IoRtcWait(100); - + KeDelayExecution(100); IoQuietSpeaker(); - } void IoDoTone(uint tone, uint time) { - IoStartSpeaker(tone); - - IoRtcWait(time); - + KeDelayExecution(time); IoQuietSpeaker(); - } void IoDoStarWars(void) diff --git a/kaleid/kernel/io/rtc.c b/kaleid/kernel/ke/rtc.c similarity index 50% rename from kaleid/kernel/io/rtc.c rename to kaleid/kernel/ke/rtc.c index 1bc000f..4805200 100644 --- a/kaleid/kernel/io/rtc.c +++ b/kaleid/kernel/ke/rtc.c @@ -24,145 +24,162 @@ #include #include +#include -static ulong IoRtcTicks = 0; -static char IoTimeChar[22] = { 0 }; -static uchar RtcRate = 0x05; //2048Hz -static Time_t IoRtcOriginTime; -static Time_t IoRtcTime; +static ulong Ticks = 0; +static Time_t OriginTime; +static Time_t CurTime; + +// TODO asnprintf() +static char TimeFmtBuf[22] = { 0 }; + +static uchar RTC_RATE = 0x05; //2048Hz static char time24or12Mode; -static void GetTimeFromRtc(void) +static void GetTimeFromRTC(void) { Time_t lastTime; char updateInProgress = 1; - while(updateInProgress) { // wait while the RTC updates its value + // Wait while the RTC updates its value + while (updateInProgress) { IoWriteByteOnPort(0x70, 0x0A); updateInProgress = (IoReadByteFromPort(0x71) & 0x80); } IoWriteByteOnPort(0x70, 0x0); - IoRtcOriginTime.sec = IoReadByteFromPort(0x71); + OriginTime.sec = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x02); - IoRtcOriginTime.min = IoReadByteFromPort(0x71); + OriginTime.min = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x04); - IoRtcOriginTime.hour = IoReadByteFromPort(0x71); + OriginTime.hour = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x06); - IoRtcOriginTime.weekday = IoReadByteFromPort(0x71); + OriginTime.weekday = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x07); - IoRtcOriginTime.day = IoReadByteFromPort(0x71); + OriginTime.day = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x08); - IoRtcOriginTime.month = IoReadByteFromPort(0x71); + OriginTime.month = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x09); - IoRtcOriginTime.year = IoReadByteFromPort(0x71); + OriginTime.year = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x32); - IoRtcOriginTime.century = IoReadByteFromPort(0x71); + OriginTime.century = IoReadByteFromPort(0x71); // Now while we don't get the same value, read the registers (ensure data are valid) do { - lastTime.sec = IoRtcOriginTime.sec; - lastTime.min = IoRtcOriginTime.min; - lastTime.hour = IoRtcOriginTime.hour; - lastTime.weekday = IoRtcOriginTime.weekday; - lastTime.day = IoRtcOriginTime.day; - lastTime.month = IoRtcOriginTime.month; - lastTime.year = IoRtcOriginTime.year; - lastTime.century = IoRtcOriginTime.century; + lastTime.sec = OriginTime.sec; + lastTime.min = OriginTime.min; + lastTime.hour = OriginTime.hour; + lastTime.weekday = OriginTime.weekday; + lastTime.day = OriginTime.day; + lastTime.month = OriginTime.month; + lastTime.year = OriginTime.year; + lastTime.century = OriginTime.century; - while(updateInProgress) { // wait while the RTC updates its value + while (updateInProgress) { IoWriteByteOnPort(0x70, 0x0A); updateInProgress = (IoReadByteFromPort(0x71) & 0x80); } IoWriteByteOnPort(0x70, 0x0); - IoRtcOriginTime.sec = IoReadByteFromPort(0x71); - IoWriteByteOnPort(0x70, 0x02); - IoRtcOriginTime.min = IoReadByteFromPort(0x71); - IoWriteByteOnPort(0x70, 0x04); - IoRtcOriginTime.hour = IoReadByteFromPort(0x71); - IoWriteByteOnPort(0x70, 0x06); - IoRtcOriginTime.weekday = IoReadByteFromPort(0x71); - IoWriteByteOnPort(0x70, 0x07); - IoRtcOriginTime.day = IoReadByteFromPort(0x71); - IoWriteByteOnPort(0x70, 0x08); - IoRtcOriginTime.month = IoReadByteFromPort(0x71); - IoWriteByteOnPort(0x70, 0x09); - IoRtcOriginTime.year = IoReadByteFromPort(0x71); - IoWriteByteOnPort(0x70, 0x32); - IoRtcOriginTime.century = IoReadByteFromPort(0x71); + OriginTime.sec = IoReadByteFromPort(0x71); - } while ( (lastTime.sec != IoRtcOriginTime.sec) || (lastTime.min != IoRtcOriginTime.min) || - (lastTime.hour != IoRtcOriginTime.hour) || (lastTime.weekday != IoRtcOriginTime.weekday) || - (lastTime.day != IoRtcOriginTime.day) || (lastTime.month != IoRtcOriginTime.month) || - (lastTime.year != IoRtcOriginTime.year) || (lastTime.century != IoRtcOriginTime.century) + IoWriteByteOnPort(0x70, 0x02); + OriginTime.min = IoReadByteFromPort(0x71); + + IoWriteByteOnPort(0x70, 0x04); + OriginTime.hour = IoReadByteFromPort(0x71); + + IoWriteByteOnPort(0x70, 0x06); + OriginTime.weekday = IoReadByteFromPort(0x71); + + IoWriteByteOnPort(0x70, 0x07); + OriginTime.day = IoReadByteFromPort(0x71); + + IoWriteByteOnPort(0x70, 0x08); + OriginTime.month = IoReadByteFromPort(0x71); + + IoWriteByteOnPort(0x70, 0x09); + OriginTime.year = IoReadByteFromPort(0x71); + + IoWriteByteOnPort(0x70, 0x32); + OriginTime.century = IoReadByteFromPort(0x71); + + } while ((lastTime.sec != OriginTime.sec) + || (lastTime.min != OriginTime.min) + || (lastTime.hour != OriginTime.hour) + || (lastTime.weekday != OriginTime.weekday) + || (lastTime.day != OriginTime.day) + || (lastTime.month != OriginTime.month) + || (lastTime.year != OriginTime.year) + || (lastTime.century != OriginTime.century) ); IoWriteByteOnPort(0x70, 0x0B); time24or12Mode = IoReadByteFromPort(0x71); + // Convert to binary if it is necessary if (!(time24or12Mode & 0x04)) { - IoRtcOriginTime.sec = (IoRtcOriginTime.sec & 0x0F) - + ((IoRtcOriginTime.sec / 16) * 10); - IoRtcOriginTime.min = (IoRtcOriginTime.min & 0x0F) - + ((IoRtcOriginTime.min / 16) * 10); - IoRtcOriginTime.hour = ( (IoRtcOriginTime.hour & 0x0F) - + (((IoRtcOriginTime.hour & 0x70) / 16) * 10) ) - | (IoRtcOriginTime.hour & 0x80); - IoRtcOriginTime.day = (IoRtcOriginTime.day & 0x0F) + ((IoRtcOriginTime.day / 16) * 10); - IoRtcOriginTime.month = (IoRtcOriginTime.month & 0x0F) - + ((IoRtcOriginTime.month / 16) * 10); - IoRtcOriginTime.year = (IoRtcOriginTime.year & 0x0F) - + ((IoRtcOriginTime.year / 16) * 10); - IoRtcOriginTime.century = (IoRtcOriginTime.century & 0x0F) - + ((IoRtcOriginTime.century / 16) * 10); - IoRtcOriginTime.weekday = (IoRtcOriginTime.weekday & 0x0F) - + ((IoRtcOriginTime.weekday / 16) * 10); + OriginTime.sec = (OriginTime.sec & 0x0F) + + ((OriginTime.sec / 16) * 10); + + OriginTime.min = (OriginTime.min & 0x0F) + + ((OriginTime.min / 16) * 10); + + OriginTime.hour = ( (OriginTime.hour & 0x0F) + + (((OriginTime.hour & 0x70) / 16) * 10) ) + | (OriginTime.hour & 0x80); + + OriginTime.day = (OriginTime.day & 0x0F) + + ((OriginTime.day / 16) * 10); + + OriginTime.month = (OriginTime.month & 0x0F) + + ((OriginTime.month / 16) * 10); + + OriginTime.year = (OriginTime.year & 0x0F) + + ((OriginTime.year / 16) * 10); + + OriginTime.century = (OriginTime.century & 0x0F) + + ((OriginTime.century / 16) * 10); + + OriginTime.weekday = (OriginTime.weekday & 0x0F) + + ((OriginTime.weekday / 16) * 10); } // Convert 12 to 24 hour if necessary - if (!(time24or12Mode & 0x02) && (IoRtcOriginTime.hour & 0x80)) { - IoRtcOriginTime.hour = ((IoRtcOriginTime.hour & 0x7)+ 10) % 24; + if (!(time24or12Mode & 0x02) && (OriginTime.hour & 0x80)) { + OriginTime.hour = ((OriginTime.hour & 0x7)+ 10) % 24; } - IoRtcTime.sec = IoRtcOriginTime.sec; - IoRtcTime.min = IoRtcOriginTime.min; - IoRtcTime.hour = IoRtcOriginTime.hour; - IoRtcTime.weekday = IoRtcOriginTime.weekday; - IoRtcTime.day = IoRtcOriginTime.day; - IoRtcTime.month = IoRtcOriginTime.month; - IoRtcTime.year = IoRtcOriginTime.year; - IoRtcTime.century = IoRtcOriginTime.century; + CurTime.sec = OriginTime.sec; + CurTime.min = OriginTime.min; + CurTime.hour = OriginTime.hour; + CurTime.weekday = OriginTime.weekday; + CurTime.day = OriginTime.day; + CurTime.month = OriginTime.month; + CurTime.year = OriginTime.year; + CurTime.century = OriginTime.century; } -void RtcHandler(ISRFrame_t *regs) +static void HandleRTC(ISRFrame_t *regs) { - //bprintf(BStdOut, " *RTC - "); IoWriteByteOnPort(0x70, 0x0C); // Selects status reg C IoReadByteFromPort(0x71); // Flush - IoRtcTicks++; - IoSendEOItoPIC(0x28); - //bprintf(BStdOut, " - EOI* "); + Ticks++; + KeSendEOItoPIC(0x28); } -void IoPrintRtcTime(void) +char *KeFormatCurTime(void) { - Time_t* RtcTime = IoGetRtcTime(); - KernLog("[RTC Time] %02d/%02d/%04d ; %02d:%02d:%02d \n", - RtcTime->day, - RtcTime->month, - RtcTime->year + RtcTime->century*100, - RtcTime->hour, - RtcTime->min, - RtcTime->sec - ); -} - -char* IoGetRtcTimeChar(void) -{ - Time_t *RtcTime = IoGetRtcTime(); - snprintf(IoTimeChar, sizeof(IoTimeChar), + Time_t *RtcTime = KeGetCurTime(); + snprintf(TimeFmtBuf, sizeof(TimeFmtBuf), "%02d/%02d/%02d ; %02d:%02d:%02d", RtcTime->day, RtcTime->month, @@ -171,39 +188,42 @@ char* IoGetRtcTimeChar(void) RtcTime->min, RtcTime->sec ); - return IoTimeChar; + return TimeFmtBuf; } -static void UpdateRtcTime(void) +static void UpdateCurTime(void) { - ulong frequency = 32768 >> (RtcRate-1); + ulong frequency = 32768 >> (RTC_RATE - 1); uchar minRemain, hourRemain, dayRemain; - IoRtcTime.sec = - (uchar)(((ulong)IoRtcOriginTime.sec + (IoRtcTicks / frequency)) % 60); + CurTime.sec = + (uchar)(((ulong)OriginTime.sec + (Ticks / frequency)) % 60); + minRemain = - (uchar)(((ulong)IoRtcOriginTime.sec + (IoRtcTicks / frequency)) / 60); + (uchar)(((ulong)OriginTime.sec + (Ticks / frequency)) / 60); + + CurTime.min = + (uchar)(((ulong)OriginTime.min + minRemain) % 60); - IoRtcTime.min = - (uchar)(((ulong)IoRtcOriginTime.min + minRemain) % 60); hourRemain = - (uchar)(((ulong)IoRtcOriginTime.min + minRemain) / 60); + (uchar)(((ulong)OriginTime.min + minRemain) / 60); + + CurTime.hour = + (uchar)(((ulong)OriginTime.hour + hourRemain) % 24); - IoRtcTime.hour = - (uchar)(((ulong)IoRtcOriginTime.hour + hourRemain) % 24); dayRemain = - (uchar)(((ulong)IoRtcOriginTime.hour + hourRemain) / 24); + (uchar)(((ulong)OriginTime.hour + hourRemain) / 24); if (dayRemain) { - KeStartPanic("[RTC Time] We must shutdown this computer for your safety.\n"); + KeStartPanic("[RTC] We must shutdown this computer for your safety.\n"); } } -Time_t* IoGetRtcTime(void) +Time_t* KeGetCurTime(void) { - UpdateRtcTime(); - return &IoRtcTime; + UpdateCurTime(); + return &CurTime; } static uint IsLeapYear(uint year) @@ -224,9 +244,9 @@ static uint DaysInMonth(uint month, uint year) : 31 - (month - 1) % 7 % 2; } -ulong IoGetTimeStamp(void) +ulong KeGetTimeStamp(void) { - Time_t *time = IoGetRtcTime(); + Time_t *time = KeGetCurTime(); uint dpy = 365 + IsLeapYear(time->year); uint dim = DaysInMonth(time->month, time->year + time->century * 100); @@ -240,23 +260,23 @@ ulong IoGetTimeStamp(void) * dpy * 24 * 60 * 60; } -ulong IoGetRtcTicks(void) +ulong KeGetClockTicks(void) { - return IoRtcTicks; + return Ticks; } -void IoEnableRtc(void) +void KeEnableRTC(void) { ulong flags = KePauseIRQs(); char readInterruptConfig; char readRegister; char readIrqs; - IdtRegisterIsr(RtcHandler, 0x28); + KeRegisterISR(HandleRTC, 0x28); // Setting up the register control and interrupt rates DebugLog("[RTC Time] Interrupt frequency set to %d Hz\n", - 32768 >> (RtcRate-1)); + 32768 >> (RTC_RATE - 1)); IoWriteByteOnPort(0x70, 0x8B); readRegister = IoReadByteFromPort(0x71); @@ -266,7 +286,7 @@ void IoEnableRtc(void) IoWriteByteOnPort(0x70, 0x8A); readInterruptConfig = IoReadByteFromPort(0x71); IoWriteByteOnPort(0x70, 0x8A); // Because reading flushes - IoWriteByteOnPort(0x71, (readInterruptConfig & 0xF0) | RtcRate); + IoWriteByteOnPort(0x71, (readInterruptConfig & 0xF0) | RTC_RATE); IoWriteByteOnPort(0x70, 0x0C); IoReadByteFromPort(0x71); // Flush @@ -280,19 +300,20 @@ void IoEnableRtc(void) IoWriteByteOnPort(0x70, 0x0C); // Select status reg C IoReadByteFromPort(0x71); // Flush - GetTimeFromRtc(); + GetTimeFromRTC(); KeRestoreIRQs(flags); - IoEnableNMI(); + KeEnableNMI(); - srand(IoGetTimeStamp()); + srand(KeGetTimeStamp()); } -void IoRtcWait(uint time) // time in ms +void KeDelayExecution(uint time) { - ulong frequency = 32768 >> (RtcRate-1); - ulong beginTick = IoGetRtcTicks(); - while(IoGetRtcTicks() < beginTick + (frequency/1000) * time) { - KePauseCPU(); + ulong frequency = 32768 >> (RTC_RATE - 1); + ulong beginTick = KeGetClockTicks(); + + while (KeGetClockTicks() < beginTick + (frequency/1000) * time) { + KeRelaxCPU(); } } diff --git a/kaleid/kernel/po/shtdwn.c b/kaleid/kernel/po/shtdwn.c index 34354e6..5bcc4cd 100644 --- a/kaleid/kernel/po/shtdwn.c +++ b/kaleid/kernel/po/shtdwn.c @@ -27,9 +27,9 @@ noreturn void PoShutdownQemu(void) { - KernLog("\nShutdown QEMU at %s...\n", IoGetRtcTimeChar()); + KernLog("\nShutdown QEMU at %s...\n", KeFormatCurTime()); - IoRtcWait(1000); + KeDelayExecution(1000); IoWriteWordOnPort(0x604, 0x2000); @@ -39,9 +39,9 @@ noreturn void PoShutdownQemu(void) noreturn void PoShutdownVirtualbox(void) { - KernLog("\nShutdown VirtualBox at %s...\n", IoGetRtcTimeChar()); + KernLog("\nShutdown VirtualBox at %s...\n", KeFormatCurTime()); - IoRtcWait(1000); + KeDelayExecution(1000); IoWriteWordOnPort(0x4004, 0x3400); @@ -51,9 +51,9 @@ noreturn void PoShutdownVirtualbox(void) noreturn void PoShutdownBochs(void) { - KernLog("\nShutdown Bochs at %s...\n", IoGetRtcTimeChar()); + KernLog("\nShutdown Bochs at %s...\n", KeFormatCurTime()); - IoRtcWait(1000); + KeDelayExecution(1000); IoWriteWordOnPort(0xB004, 0x2000); diff --git a/kaleid/kernel/sh/shcmds.c b/kaleid/kernel/sh/shcmds.c index 5763f79..2d9da7c 100644 --- a/kaleid/kernel/sh/shcmds.c +++ b/kaleid/kernel/sh/shcmds.c @@ -70,7 +70,7 @@ error_t CmdHelp(int argc, char **argv, char *cmdline) error_t CmdDate(int argc, char **argv, char *cmdline) { - IoPrintRtcTime(); + KernLog("%s\n", KeFormatCurTime()); return EOK; } @@ -213,9 +213,9 @@ error_t CmdPF(int argc, char **argv, char *cmdline) return EOK; } +extern void KeStartShell(void); error_t CmdShell(int argc, char **argv, char *cmdline) -{ - extern void KeStartShell(void); +{ KeStartShell(); return EOK; } @@ -224,18 +224,20 @@ error_t CmdShell(int argc, char **argv, char *cmdline) Command_t cmdtable[] = { - { "beep", CmdBeep, "Make a beep" }, - { "date", CmdDate, "Print date" }, - { "die", CmdDie, "Die painfully" }, - { "exit", CmdQuit, "Initiate shutdown" }, - { "help", CmdHelp, "Show this message" }, - { "march", CmdStarWars, "Play the Imperial March"}, - { "mmap", CmdMemMap, "Show memory map" }, - { "musage", CmdMemUsage, "Show memory statistics" }, - { "pfault", CmdPF, "Provokes a PF" }, - { "pstest", CmdPsTest, "Scheduler test routine" }, - { "shell", CmdShell, "New shell instance" }, - { "quit", CmdQuit, "Alias for 'exit'" }, + { "beep", CmdBeep, "Make a beep" }, + { "cls", CmdClear, "Clears standard output" }, + { "date", CmdDate, "Print date" }, + { "die", CmdDie, "Die painfully" }, + { "exit", CmdQuit, "Initiate shutdown" }, + { "help", CmdHelp, "Show this message" }, + { "march", CmdStarWars, "Play the Imperial March"}, + { "mmap", CmdMemMap, "Show memory map" }, + { "musage", CmdMemUsage, "Show memory statistics" }, + { "pfault", CmdPF, "Provokes a PF" }, + { "pstest", CmdPsTest, "Scheduler test routine" }, + { "quit", CmdQuit, "Alias for 'exit'" }, + { "shell", CmdShell, "New shell instance" }, + { "time", CmdTime, "Print time" }, { NULL, NULL, NULL } }; diff --git a/kaleid/libbuf/bgetc.c b/kaleid/libbuf/bgetc.c index 7a3542d..2da4e7b 100644 --- a/kaleid/libbuf/bgetc.c +++ b/kaleid/libbuf/bgetc.c @@ -72,7 +72,7 @@ error_t bgetc(Buffer_t *buf, uchar *ch) // (so that BStdIn works; don't make anything else) while (buf->rp >= buf->wp) { #ifdef _KALEID_KERNEL - IoRtcWait(1); + KeDelayExecution(1); #endif }