From 39100c30b7a16103e75187c9840a79c7df54f3da Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 20 Aug 2018 00:47:53 +0200 Subject: schicophrenic cleanup after realizing many fundamental desgin problems! --- kernel/interrupts.c | 2 +- kernel/kernel.c | 21 ++++++------ kernel/ringbuffer.c | 16 ++++----- kernel/syscalls.c | 8 ++--- kernel/timer.c | 95 +++++++++++++++++++---------------------------------- kernel/timer.h | 46 ++++++++++++++++++++++++-- 6 files changed, 102 insertions(+), 86 deletions(-) (limited to 'kernel') diff --git a/kernel/interrupts.c b/kernel/interrupts.c index 41d0a9c..46883d6 100644 --- a/kernel/interrupts.c +++ b/kernel/interrupts.c @@ -167,7 +167,7 @@ void int_init(uint16_t sel) // now we can enable interrupts back again // x86_sti(); - x86_cli(); + //x86_cli(); } void int_install() diff --git a/kernel/kernel.c b/kernel/kernel.c index ba6bc5d..62556cb 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -24,13 +24,14 @@ void kernel_main(uint32_t eax,uint32_t ebx) { serial_init(); - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"COM 1 - initialized"); log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"%s - BUILD: git-%s (%s %s)", KERNEL_NAME,GIT_REVISION,__DATE__,__TIME__); - // PIT TIMER - timer_init(); + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"COM 1 - initialized"); + + uint64_t epoch_time=timer_init(); + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"PIT - initialized. %u seconds passed since 1970.",epoch_time); // STREAMS uint32_t sstdin = syscall_open("stdin",0,0); // stdin 0 @@ -53,11 +54,11 @@ void kernel_main(uint32_t eax,uint32_t ebx) multiboot_information *info=get_multiboot(eax, ebx); // Gather Info about other processors. (APs = application processors) // ACPI or MP - smp_processors procdata; - - if(!acpi_find(&procdata)) - if(!mp_find(&procdata)) - panic(FOOLOS_MODULE_NAME,"ACPI and MP search failed! I do not want to continue!"); + //smp_processors procdata; + // + //if(!acpi_find(&procdata)) + // if(!mp_find(&procdata)) + // panic(FOOLOS_MODULE_NAME,"ACPI and MP search failed! I do not want to continue!"); // MEMORY INIT (allows allocating and deaclloating physical memory) uint32_t kernel_blocks=mem_init(info); @@ -75,9 +76,9 @@ void kernel_main(uint32_t eax,uint32_t ebx) pdirectory *dir=vmem_init(kernel_blocks,(uint32_t)info->framebuffer_addr); // PCI Bus - pci_init(); + //pci_init(); - // INIT VESA + // INIT VESA: TODO: stop and say if not 32bit colormode! uint32_t addr=kballoc(1); fs_content("/binfont.bin",addr,0x100); // copy 0x100 bytes to 0x7000 vesa_init(info->vbe_control_info,info->vbe_mode_info,addr); diff --git a/kernel/ringbuffer.c b/kernel/ringbuffer.c index 099e96a..6085aaf 100644 --- a/kernel/ringbuffer.c +++ b/kernel/ringbuffer.c @@ -20,11 +20,11 @@ ringbuffer ringbuffer_init(uint32_t size) bool ringbuffer_put(ringbuffer* f,uint8_t c) { - x86_cli(); +// x86_cli(); if((f->back-1+f->size)%f->size==f->front) { - x86_sti(); +// x86_sti(); return false; } @@ -33,30 +33,30 @@ bool ringbuffer_put(ringbuffer* f,uint8_t c) f->back+=f->size; f->back%=f->size; - x86_sti(); +// x86_sti(); return true; } bool ringbuffer_has(ringbuffer* f) { - x86_cli(); +// x86_cli(); bool res=true; if(f->front==f->back) res=false; - x86_sti(); +// x86_sti(); return res; } uint8_t ringbuffer_get(ringbuffer* f) // non blocking . please check first { - x86_cli(); +// x86_cli(); char c; if(f->front==f->back) { - x86_sti(); +// x86_sti(); return ' '; } @@ -66,6 +66,6 @@ uint8_t ringbuffer_get(ringbuffer* f) // non blocking . please check first f->front+=f->size; f->front%=f->size; - x86_sti(); + // x86_sti(); return c; } diff --git a/kernel/syscalls.c b/kernel/syscalls.c index 3f2a811..0c334bc 100644 --- a/kernel/syscalls.c +++ b/kernel/syscalls.c @@ -149,15 +149,15 @@ int syscall_tune(int v1,int v2, int v3) int copy_args(char **in, char **out) { - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"copy_args(0x%08x, 0x%08X)",in,out); + //log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"copy_args(0x%08x, 0x%08X)",in,out); int count=0; while(in[count]!=NULL) { - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"args(0x%08x: %s)",in[count],out); +// log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"args(0x%08x: %s)",in[count],out); count++; }; - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"copy_args : count: %d",count); + // log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"copy_args : count: %d",count); char **pos=out; pos+=sizeof(char **)*(count+1); @@ -218,7 +218,7 @@ int syscall_execve(char *name, char **argv, char **env) /* try to move this to asm */ // asm volatile("jmp ."); // loop forever - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"returning to jump addy (0x%08X)", entry_global); + //log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"returning to jump addy (0x%08X)", entry_global); asm volatile("mov $0x8fff000,%esp"); // set stack at high end of process image diff --git a/kernel/timer.c b/kernel/timer.c index e5e26b4..2bf1592 100644 --- a/kernel/timer.c +++ b/kernel/timer.c @@ -1,58 +1,29 @@ -/// PIT /// Timer stuff // CMOS - -/* - http://www.brokenthorn.com/Resources/OSDevPit.html - https://wiki.osdev.org/CMOS - - vcc/gnd - voltage/ground - - D0-D7 - data lines (data bus) - wr/rd - writing / reading (system control bus) - cs - ignore wr/rd or not (address bus) - a0-a1 (address bus) - - // the three 16bit down counters/timers/channels - clk 0-2 (in) - gate 0-2 (in) - out 0-2 (out) - - //typical - out1 -> pic interrupt on every tick (system timer) - out2 - was used for genearting dram memory refresh (Do not use) - out3 -> pc speaker - - gate pins : depend on mode of operation - we do have modes 0-5. - mode0: counts down to zero , triggers interrupt and waits - mode1: - mode2: rate generator (sys timer) - .... - */ - #define FOOLOS_MODULE_NAME "timer" - #include "timer.h" + #include "asm/x86.h" -#include "lib/logger/log.h" -// TODO: use mutex? do we need volatile at all!?? -// abstract atomic variable set,get,increment +//TODO: volatile? spinlock? static volatile uint64_t task_system_clock=0; static volatile uint64_t task_system_clock_start=0; +static volatile uint8_t interrupts_on = 0; -//called by interrupt +// interrupt handler (do NOT call yourself!) void timer_tick() { task_system_clock++; + interrupts_on=1; } // get value uint64_t timer_get_ticks() { - return task_system_clock; + uint64_t ret; +// x86_cli(); // do not disturb by timer_tick! + ret=task_system_clock; + // if(interrupts_on!=0)x86_sti(); // reenable (if clock was running already) + return ret; } -/// - uint64_t timer_get_ms() { @@ -68,24 +39,22 @@ uint64_t timer_get_uptime_ms() } // CMOS RTC - // read real time clock register -unsigned char get_rtc_reg(int reg) { +static unsigned char get_rtc_reg(int reg) { x86_outb(0x70, reg); //cmos at addr 0x70 return x86_inb(0x71); //cmos data at addr 0x71 } // check if cmos rtc update in progress -int get_rtc_update_flag() { +static int get_rtc_update_flag() { x86_outb(0x70, 0x0A); return (x86_inb(0x71) & 0x80); } // get real time clock rom cmos (seconds since jan) -uint64_t get_rtc_time() +static uint64_t get_rtc_time() { - int CURRENT_YEAR = 2018; // Change this each year! int century_register = 0x00; // Set by ACPI table parsing code if possible @@ -209,26 +178,13 @@ uint64_t get_rtc_time() ((year-1900 - 1) / 100) * 86400 + ((year-1900 + 299) / 400) * 86400; - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"CMOS Hardware Clock: %u-%u-%u %u:%u:%u",day,month,year,hour,minute,second); - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"CMOS Hardware Clock: %u seconds since Epoch",epoch_seconds); - return epoch_seconds; } -// PIT -// TODO: move to asm file -void timer_init() -{ - uint64_t epoch_time=get_rtc_time(); - - task_system_clock_start=epoch_time; - task_system_clock=epoch_time*25; - // config out timer on channel 0 : mode 2 (sys timer) - // http://en.wikipedia.org/wiki/Intel_8253#Control_Word_Register - // http://www.brokenthorn.com/Resources/OSDevPit.html - // int0 will be triggered ~25 times a second. - +// TODO: put in asm file! +static void timer_config() +{ __asm__("pusha"); __asm__("mov %0, %%dx"::"X" (1193180 / 25)); @@ -244,6 +200,23 @@ void timer_init() __asm__("popa"); - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Configured PIT Channel 0 : Mode 2 : 1/25 s."); } +// PIT +uint64_t timer_init() +{ + uint64_t epoch_time=get_rtc_time(); + + task_system_clock_start=epoch_time; + task_system_clock=epoch_time*25; + + timer_config(); + + // config out timer on channel 0 : mode 2 (sys timer) + // http://en.wikipedia.org/wiki/Intel_8253#Control_Word_Register + // http://www.brokenthorn.com/Resources/OSDevPit.html + // int0 will be triggered ~25 times a second. + + //log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Configured PIT Channel 0 : Mode 2 : 1/25 s."); + return epoch_time; +} diff --git a/kernel/timer.h b/kernel/timer.h index a39d433..15488ff 100644 --- a/kernel/timer.h +++ b/kernel/timer.h @@ -1,6 +1,48 @@ + +/** + * @file +/// PIT /// Timer stuff // CMOS + http://www.brokenthorn.com/Resources/OSDevPit.html + https://wiki.osdev.org/CMOS + + vcc/gnd - voltage/ground + + D0-D7 - data lines (data bus) + wr/rd - writing / reading (system control bus) + cs - ignore wr/rd or not (address bus) + a0-a1 (address bus) + + // the three 16bit down counters/timers/channels + clk 0-2 (in) + gate 0-2 (in) + out 0-2 (out) + + //typical + out1 -> pic interrupt on every tick (system timer) + out2 - was used for genearting dram memory refresh (Do not use) + out3 -> pc speaker + + gate pins : depend on mode of operation + we do have modes 0-5. + mode0: counts down to zero , triggers interrupt and waits + mode1: + mode2: rate generator (sys timer) + .... + */ + #include -void timer_init(); -void timer_tick(); +/** + * Initilize time and PIT (trigger 25 times a second). + * Returns the number of seconds passed since 1970. + */ +uint64_t timer_init(); + +/** get number of ticks since boot */ uint64_t timer_get_ticks(); + +/** get number of milliseconds since boot */ uint64_t timer_get_uptime_ms(); + +/** get number of milliseconds since 1970 */ +uint64_t timer_get_ms(); -- cgit v1.2.3