From 51d4dd040a291b62c648ff6cc0d7e0058cf4056f Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 17 Aug 2018 17:51:51 +0200 Subject: starting implicit function calls cleanup --- kernel/config.h | 6 +- kernel/interrupts.c | 4 +- kernel/mem.c | 6 +- kernel/syscalls.c | 11 ++- kernel/syscalls.h | 7 -- kernel/timer.c | 204 ++++++++++++++++++++++++++++++++++++++++++++++------ kernel/usermode.c | 9 ++- 7 files changed, 203 insertions(+), 44 deletions(-) (limited to 'kernel') diff --git a/kernel/config.h b/kernel/config.h index 4923a0e..6c9e3d5 100644 --- a/kernel/config.h +++ b/kernel/config.h @@ -1,11 +1,9 @@ /******************************************** - * * - * Fool OS Central Configuration File * - * * + * Fool OS Central Configuration File * ********************************************/ -#include +#include "lib/logger/log.h" #ifndef FOOLOS_CONFIG_H #define FOOLOS_CONFIG_H diff --git a/kernel/interrupts.c b/kernel/interrupts.c index f0b9bbd..e19d816 100644 --- a/kernel/interrupts.c +++ b/kernel/interrupts.c @@ -108,7 +108,7 @@ void exception_handle_18(){ panic(FOOLOS_MODULE_NAME,"Machine Check"); } void int_install_ir(int irq, uint16_t flags, uint16_t sel, void *addr) { - uint64_t base=(uint64_t)&(*addr); + uint64_t base=(uint32_t)&(*(uint32_t*)addr); idt[irq].addrLo = base & 0xffff; idt[irq].addrHi = (base >> 16) & 0xffff; @@ -184,7 +184,7 @@ void int_install() idtd.size=sizeof(struct int_desc)*INT_MAX; - uint32_t addr=&idt[0]; + uint32_t addr=(uint32_t)&idt[0]; idtd.baseHi=addr>>16; idtd.baseLo=0xffff&addr; diff --git a/kernel/mem.c b/kernel/mem.c index dfd59a7..74b3a98 100644 --- a/kernel/mem.c +++ b/kernel/mem.c @@ -129,7 +129,7 @@ void* pmmngr_alloc_block () void pmmngr_free_block (void* p) { - uint32_t addr = (uint32_t*)p; + uint32_t addr = (uint32_t)(uint32_t*)p; int frame = addr / PMMNGR_BLOCK_SIZE; if(mmap_test(frame)) @@ -171,7 +171,7 @@ uint32_t mem_init(multiboot_information *info) // iterate : print memory map, calc blocks, deinit for(uint32_t mmap_addr=memmap;mmap_addrbase_addr; uint64_t mem_end=mmap->base_addr+mmap->length; @@ -200,7 +200,7 @@ uint32_t mem_init(multiboot_information *info) // deinit modules memory if(info->flags&&1<<3) { - multiboot_mod *mod=info->mods_addr; + multiboot_mod *mod=(multiboot_mod *)info->mods_addr; for(int i=0;imods_count;i++) { log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"mod 0x%08X-0x%08X : %s", diff --git a/kernel/syscalls.c b/kernel/syscalls.c index e3db767..c507a4e 100644 --- a/kernel/syscalls.c +++ b/kernel/syscalls.c @@ -10,6 +10,9 @@ #include #include +// TODO: use includes!!! +uint64_t timer_get_ms(); + int syscall_unhandled(int nr) { log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"syscall: %d", nr); @@ -20,13 +23,15 @@ int syscall_gettimeofday(struct timeval *tv, struct timezone *tz) { if(tv!=NULL) { - uint32_t t=timer_get_ms(); - tv->tv_sec=t/1000; + uint64_t t=timer_get_ms(); + tv->tv_sec=t/1000+2*3600; // add gmt+2 tv->tv_usec=0;//t-tv->tv_sec*1000; } + + // tz struct is obsolote if(tz!=NULL) { - tz->tz_minuteswest=0; + tz->tz_minuteswest=0;// -21*60; // warsaw tz->tz_dsttime=DST_NONE; } return 0; diff --git a/kernel/syscalls.h b/kernel/syscalls.h index ff7760e..9a08cba 100644 --- a/kernel/syscalls.h +++ b/kernel/syscalls.h @@ -60,10 +60,3 @@ int syscall_wait(int *status); // int syscall_unhandled(int nr); */ - - - - - - - diff --git a/kernel/timer.c b/kernel/timer.c index 9ca748c..a339d70 100644 --- a/kernel/timer.c +++ b/kernel/timer.c @@ -1,7 +1,8 @@ -/// PIT /// Timer stuff +/// PIT /// Timer stuff // CMOS /* http://www.brokenthorn.com/Resources/OSDevPit.html + https://wiki.osdev.org/CMOS vcc/gnd - voltage/ground @@ -26,20 +27,195 @@ mode1: mode2: rate generator (sys timer) .... - - */ #define FOOLOS_MODULE_NAME "timer" #include "timer.h" +#include "x86.h" +#include "lib/logger/log.h" -#include "lib/logger/log.h" // logger facilities - +// TODO: use mutex? do we need volatile at all!?? +// abstract atomic variable set,get,increment static volatile uint64_t task_system_clock=0; +//called by interrupt +void timer_tick() +{ + task_system_clock++; +} + +// get value +uint64_t timer_get_ticks() +{ + return task_system_clock; +} +/// + +uint64_t timer_get_ms() +{ + uint64_t t=timer_get_ticks(); + uint64_t s=t/25; + uint64_t ms=0; // TODO ms + return s*1000; +} + +// CMOS RTC + + +// read real time clock register +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() { + x86_outb(0x70, 0x0A); + return (x86_inb(0x71) & 0x80); +} + +// get real time clock rom cmos (seconds since jan) +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 + + unsigned char second; + unsigned char minute; + unsigned char hour; + unsigned char day; + unsigned char month; + unsigned int year; + + unsigned char century; + unsigned char last_second; + unsigned char last_minute; + unsigned char last_hour; + unsigned char last_day; + unsigned char last_month; + unsigned char last_year; + unsigned char last_century; + unsigned char registerB; + + // Note: This uses the "read registers until you get the same values twice in a row" technique + // to avoid getting dodgy/inconsistent values due to RTC updates + + while (get_rtc_update_flag()); // Make sure an update isn't in progress + second = get_rtc_reg(0x00); + minute = get_rtc_reg(0x02); + hour = get_rtc_reg(0x04); + day = get_rtc_reg(0x07); + month = get_rtc_reg(0x08); + year = get_rtc_reg(0x09); + + if(century_register != 0) { + century = get_rtc_reg(century_register); + } + + do { + last_second = second; + last_minute = minute; + last_hour = hour; + last_day = day; + last_month = month; + last_year = year; + last_century = century; + + while (get_rtc_update_flag()); // Make sure an update isn't in progress + second = get_rtc_reg(0x00); + minute = get_rtc_reg(0x02); + hour = get_rtc_reg(0x04); + day = get_rtc_reg(0x07); + month = get_rtc_reg(0x08); + year = get_rtc_reg(0x09); + if(century_register != 0) { + century = get_rtc_reg(century_register); + } + } while( (last_second != second) || (last_minute != minute) || (last_hour != hour) || + (last_day != day) || (last_month != month) || (last_year != year) || + (last_century != century) ); + + registerB = get_rtc_reg(0x0B); + + // Convert BCD to binary values if necessary + if (!(registerB & 0x04)) { + second = (second & 0x0F) + ((second / 16) * 10); + minute = (minute & 0x0F) + ((minute / 16) * 10); + hour = ( (hour & 0x0F) + (((hour & 0x70) / 16) * 10) ) | (hour & 0x80); + day = (day & 0x0F) + ((day / 16) * 10); + month = (month & 0x0F) + ((month / 16) * 10); + year = (year & 0x0F) + ((year / 16) * 10); + if(century_register != 0) { + century = (century & 0x0F) + ((century / 16) * 10); + } + } + + // Convert 12 hour clock to 24 hour clock if necessary + if (!(registerB & 0x02) && (hour & 0x80)) { + hour = ((hour & 0x7F) + 12) % 24; + } + + // Calculate the full (4-digit) year + if(century_register != 0) { + year += century * 100; + } else { + year += (CURRENT_YEAR / 100) * 100; + if(year < CURRENT_YEAR) year += 100; + } + + // return ((((uint64_t)year-1970)*356;//+month*30+day)*24+hour*60+minute)*60+second; + + // thank you doug16k @ #osdev + // https://github.com/doug65536/dgos/blob/eab7080e69360493381669e7ce0ff27587d3127a/kernel/lib/time.cc + int days[] = { + 31, + (year-1900) % 4 ? 28 : + (year-1900) % 100 ? 29 : + (year-1900) % 400 ? 28 : + 29, + 31, + 30, + 31, + 30, + 31, + 31, + 30, + 31, + 30, + 31 + }; + + int yday = 0; + for (int m = 1; m < month; ++m) + yday += days[m-1]; + yday += day - 1; + + uint64_t epoch_seconds= ((uint64_t)(second)) + + minute * 60 + + hour * 3600 + + (yday) * 86400 + + (year-1900 - 70) * 365 * 86400 + + ((year-1900 - 69) / 4) * 86400 - + ((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(); + epoch_time*=25; + task_system_clock=epoch_time; + // 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 @@ -61,23 +237,5 @@ void timer_init() __asm__("popa"); log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Configured PIT Channel 0 : Mode 2 : 1/25 s."); - -} - -void timer_tick() -{ - task_system_clock++; -} - -uint64_t timer_get_ticks() -{ - return task_system_clock; } -uint32_t timer_get_ms() -{ - uint32_t t=timer_get_ticks(); - uint32_t s=t/25; - uint32_t ms=t*1000/25-1000*s; - return s*1000;//+ms; -} diff --git a/kernel/usermode.c b/kernel/usermode.c index f0618d7..eb19cdf 100644 --- a/kernel/usermode.c +++ b/kernel/usermode.c @@ -7,6 +7,7 @@ tss_struct sys_tss; //Define the TSS as a global structure // generic syscall interface! +/* int syscall(int call, int p1, int p2, int p3) { int ebx; // will hold return value; @@ -31,16 +32,20 @@ int syscall(int call, int p1, int p2, int p3) return ebx; } +*/ +/* int write(int file, char *ptr, int len) { return syscall(SYSCALL_WRITE,file,ptr,len); } - +*/ +/* int execve(char *name, char **argv, char **env) { return syscall(SYSCALL_EXECVE,name,argv,env); } +*/ void install_tss(int cpu_no){ @@ -65,6 +70,6 @@ char *env_init[]={"var1=dupa","var2=mundl",NULL}; // THIS WILL BE RUN IN RING 3! void userfunc() { - execve("/bin/init",argv_init,env_init); + syscall(SYSCALL_EXECVE,"/bin/init",argv_init,env_init); while(1); // we should never get here. } -- cgit v1.2.3