From 72c6e9763ca61bc9d7de5f7080ee1c8a1c7c1562 Mon Sep 17 00:00:00 2001 From: Miguel Date: Tue, 21 Aug 2018 19:42:33 +0200 Subject: cleaning up a bit --- driver/keyboard.c | 4 +- driver/mouse.c | 2 - driver/pci.h | 1 + driver/timer.c | 167 +++++++++++++++++++++++++++++++++++++++ driver/timer.h | 50 ++++++++++++ kernel/GDT.c | 136 -------------------------------- kernel/gdt.c | 136 ++++++++++++++++++++++++++++++++ kernel/gdt.h | 1 + kernel/interrupts.c | 4 +- kernel/interrupts.h | 5 +- kernel/kernel.c | 25 +++--- kernel/scheduler.c | 222 +++++++++++++++++++++++++++++++++++++++++++++++++++ kernel/scheduler.h | 1 + kernel/task.c | 223 ---------------------------------------------------- kernel/task.h | 1 - kernel/timer.c | 167 --------------------------------------- kernel/timer.h | 50 ------------ kernel/vmem.h | 1 - lib/logger/log.c | 1 - 19 files changed, 598 insertions(+), 599 deletions(-) create mode 100644 driver/pci.h create mode 100644 driver/timer.c create mode 100644 driver/timer.h delete mode 100644 kernel/GDT.c create mode 100644 kernel/gdt.c create mode 100644 kernel/gdt.h create mode 100644 kernel/scheduler.c create mode 100644 kernel/scheduler.h delete mode 100644 kernel/task.c delete mode 100644 kernel/task.h delete mode 100644 kernel/timer.c delete mode 100644 kernel/timer.h diff --git a/driver/keyboard.c b/driver/keyboard.c index 52e6454..9377515 100644 --- a/driver/keyboard.c +++ b/driver/keyboard.c @@ -2,9 +2,9 @@ // http://www.computer-engineering.org/ps2keyboard/scancodes1.html #define FOOLOS_MODULE_NAME "keyboard" -#include #include "asm/x86.h" -#include "lib/logger/log.h" + +#include static bool ctrl_l=false; static bool shift_l=false; diff --git a/driver/mouse.c b/driver/mouse.c index f585b99..28acf53 100644 --- a/driver/mouse.c +++ b/driver/mouse.c @@ -4,7 +4,6 @@ //based on Mouse.inc by SANiK //License: Use as you wish, except to cause damage -#include "lib/logger/log.h" #include #include "asm/x86.h" @@ -94,7 +93,6 @@ void mouse_init() mouse_write(0xF4); mouse_read(); //Acknowledge - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Mouse Initialized"); } void mouse_log() diff --git a/driver/pci.h b/driver/pci.h new file mode 100644 index 0000000..f02c5c7 --- /dev/null +++ b/driver/pci.h @@ -0,0 +1 @@ +//stub diff --git a/driver/timer.c b/driver/timer.c new file mode 100644 index 0000000..2d4c7ff --- /dev/null +++ b/driver/timer.c @@ -0,0 +1,167 @@ +#define FOOLOS_MODULE_NAME "timer" +#include "timer.h" + +#include "asm/x86.h" + +static volatile uint64_t task_system_clock_start=0; + +// CMOS RTC + +// read real time clock register +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 +static int get_rtc_update_flag() { + x86_outb(0x70, 0x0A); + return (x86_inb(0x71) & 0x80); +} + +// get real time clock rom cmos (seconds since jan) +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 + + 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; + } + + // 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; + + return epoch_seconds; +} + + +// PIT +uint64_t timer_init() +{ + uint64_t epoch_time=get_rtc_time(); + task_system_clock_start=epoch_time*25; // since pit ticks 25times a second + pit_init(); + return epoch_time; +} + +uint64_t timer_get_ms() +{ + return (pit_get_ticks()+task_system_clock_start)*40; +} + +uint64_t timer_get_uptime_ms() +{ + return pit_get_ticks()*40; +} diff --git a/driver/timer.h b/driver/timer.h new file mode 100644 index 0000000..1e3d066 --- /dev/null +++ b/driver/timer.h @@ -0,0 +1,50 @@ + +/** + * @file + * References + * ---------- + * * 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 + +/** + * 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(); diff --git a/kernel/GDT.c b/kernel/GDT.c deleted file mode 100644 index 4bc7ca2..0000000 --- a/kernel/GDT.c +++ /dev/null @@ -1,136 +0,0 @@ -// http://wiki.osdev.org/GDT_Tutorial -#include "lib/logger/log.h" -#include "usermode.h" -#define FOOLOS_MODULE_NAME "GDT" - -#include -#define GDT_SIZE 6 - -extern sys_tss; - -typedef struct GDT_struct -{ - uint32_t base; - uint32_t limit; - uint32_t type; - -}GDT; - -//alternative -struct gdt_entry_bits -{ - unsigned int limit_low:16; - unsigned int base_low : 24; - //attribute byte split into bitfields - unsigned int accessed :1; - unsigned int read_write :1; //readable for code, writable for data - unsigned int conforming_expand_down :1; //conforming for code, expand down for data - unsigned int code :1; //1 for code, 0 for data - unsigned int always_1 :1; //should be 1 for everything but TSS and LDT - unsigned int DPL :2; //priveledge level - unsigned int present :1; - //and now into granularity - unsigned int limit_high :4; - unsigned int available :1; - unsigned int always_0 :1; //should always be 0 - unsigned int big :1; //32bit opcodes for code, uint32_t stack for data - unsigned int gran :1; //1 to use 4k page addressing, 0 for byte addressing - unsigned int base_high :8; -} __packed; //or __attribute__((packed)) - - -static GDT myGDT[GDT_SIZE]; -static uint8_t gdt_struct[GDT_SIZE*8]; - -/** - * \param target A pointer to the 8-byte GDT entry - * \param source An arbitrary structure describing the GDT entry - */ -void encodeGdtEntry(uint8_t *target, GDT source) -{ - // Check the limit to make sure that it can be encoded - if ((source.limit > 65536) && (source.limit & 0xFFF) != 0xFFF) - { - panic(FOOLOS_MODULE_NAME,"trying to set an invalid GDT source.limit!"); - } - if (source.limit > 65536) { - // Adjust granularity if required - source.limit = source.limit >> 12; - target[6] = 0xC0; - } else { - target[6] = 0x40; - } - - // Encode the limit - target[0] = source.limit & 0xFF; - target[1] = (source.limit >> 8) & 0xFF; - - // Encode the base - target[2] = source.base & 0xFF; - target[3] = (source.base >> 8) & 0xFF; - target[4] = (source.base >> 16) & 0xFF; - target[7] = (source.base >> 24) & 0xFF; - - target[5] = source.type; - target[6] |= (source.limit >> 16) & 0xF; - - // And... Type - - /* - -0 1 dw 0xffff ;limit -2 3 dw 0x0 ;base -4 db 0x0 ;base -5 db 10011010b ;flags -6 db 11001111b ;flags & seg.limit -7 db 0x0 ;base - */ - - -} - -void gdt_setup() -{ - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"setting up Global Descriptor Table"); - //selector 0x0 - myGDT[0].base=0; - myGDT[0].limit=0; - myGDT[0].type=0; - - //selector 0x8 code - myGDT[1].base=0; - myGDT[1].limit=0xffffffff; - myGDT[1].type=0x9a; - - //selector 0x10 data - myGDT[2].base=0; - myGDT[2].limit=0xffffffff; - myGDT[2].type=0x92; - - //selector 0x18 code user - myGDT[3].base=0; - myGDT[3].limit=0xffffffff; - myGDT[3].type=0xFa; - - //selector 0x20 data user - myGDT[4].base=0; - myGDT[4].limit=0xffffffff; - myGDT[4].type=0xF2; - - //TSS 0x28 - myGDT[5].base=&sys_tss; //tss start? - myGDT[5].limit=sizeof(tss_struct); //tss end? - myGDT[5].type=0x89; - - - // transcript to format the processor wants - for(int i=0;i +#define GDT_SIZE 6 + +extern sys_tss; + +typedef struct GDT_struct +{ + uint32_t base; + uint32_t limit; + uint32_t type; + +}GDT; + +//alternative +struct gdt_entry_bits +{ + unsigned int limit_low:16; + unsigned int base_low : 24; + //attribute byte split into bitfields + unsigned int accessed :1; + unsigned int read_write :1; //readable for code, writable for data + unsigned int conforming_expand_down :1; //conforming for code, expand down for data + unsigned int code :1; //1 for code, 0 for data + unsigned int always_1 :1; //should be 1 for everything but TSS and LDT + unsigned int DPL :2; //priveledge level + unsigned int present :1; + //and now into granularity + unsigned int limit_high :4; + unsigned int available :1; + unsigned int always_0 :1; //should always be 0 + unsigned int big :1; //32bit opcodes for code, uint32_t stack for data + unsigned int gran :1; //1 to use 4k page addressing, 0 for byte addressing + unsigned int base_high :8; +} __packed; //or __attribute__((packed)) + + +static GDT myGDT[GDT_SIZE]; +static uint8_t gdt_struct[GDT_SIZE*8]; + +/** + * \param target A pointer to the 8-byte GDT entry + * \param source An arbitrary structure describing the GDT entry + */ +void encodeGdtEntry(uint8_t *target, GDT source) +{ + // Check the limit to make sure that it can be encoded + if ((source.limit > 65536) && (source.limit & 0xFFF) != 0xFFF) + { + panic(FOOLOS_MODULE_NAME,"trying to set an invalid GDT source.limit!"); + } + if (source.limit > 65536) { + // Adjust granularity if required + source.limit = source.limit >> 12; + target[6] = 0xC0; + } else { + target[6] = 0x40; + } + + // Encode the limit + target[0] = source.limit & 0xFF; + target[1] = (source.limit >> 8) & 0xFF; + + // Encode the base + target[2] = source.base & 0xFF; + target[3] = (source.base >> 8) & 0xFF; + target[4] = (source.base >> 16) & 0xFF; + target[7] = (source.base >> 24) & 0xFF; + + target[5] = source.type; + target[6] |= (source.limit >> 16) & 0xF; + + // And... Type + + /* + +0 1 dw 0xffff ;limit +2 3 dw 0x0 ;base +4 db 0x0 ;base +5 db 10011010b ;flags +6 db 11001111b ;flags & seg.limit +7 db 0x0 ;base + */ + + +} + +void gdt_init() +{ + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"setting up Global Descriptor Table"); + //selector 0x0 + myGDT[0].base=0; + myGDT[0].limit=0; + myGDT[0].type=0; + + //selector 0x8 code + myGDT[1].base=0; + myGDT[1].limit=0xffffffff; + myGDT[1].type=0x9a; + + //selector 0x10 data + myGDT[2].base=0; + myGDT[2].limit=0xffffffff; + myGDT[2].type=0x92; + + //selector 0x18 code user + myGDT[3].base=0; + myGDT[3].limit=0xffffffff; + myGDT[3].type=0xFa; + + //selector 0x20 data user + myGDT[4].base=0; + myGDT[4].limit=0xffffffff; + myGDT[4].type=0xF2; + + //TSS 0x28 + myGDT[5].base=&sys_tss; //tss start? + myGDT[5].limit=sizeof(tss_struct); //tss end? + myGDT[5].type=0x89; + + + // transcript to format the processor wants + for(int i=0;i -#define INT_MAX 255 // size of our interrupts table - -void int_install(); -void int_init(uint16_t sel); +void interrupts_init(uint16_t sel); #endif diff --git a/kernel/kernel.c b/kernel/kernel.c index e1076a9..67f1355 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -2,20 +2,25 @@ #include "kernel.h" #include -#include + +#include "driver/serial.h" +#include "driver/timer.h" +#include "driver/keyboard.h" +#include "driver/mouse.h" + +#include "kernel/gdt.h" +#include "kernel/scheduler.h" #include "syscalls.h" #include "types.h" #include "lib/logger/log.h" #include "fifo.h" -#include "timer.h" #include "mem.h" #include "vmem.h" #include "mp.h" #include "interrupts.h" #include "multiboot.h" #include "ringbuffer.h" -#include "task.h" #include "multiboot.h" #include "terminal/terminal.h" #include "driver/screen.h" @@ -33,15 +38,14 @@ void kernel_main(uint32_t eax,uint32_t ebx) uint64_t epoch_time=timer_init(); log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"PIT - initialized. %u seconds passed since 1970.",epoch_time); - // KEYBOARD DRIVER keyboard_init(0); //sstdin log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Keyboard Initialized"); - // MOUSE DRIVER mouse_init(); + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Mouse Initialized"); - // GDT - gdt_setup(); + gdt_init(); + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"GDT Initialized"); // MULTIBOOT HEADER multiboot_information *info=get_multiboot(eax, ebx); @@ -74,6 +78,7 @@ void kernel_main(uint32_t eax,uint32_t ebx) // 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); // STREAMS @@ -81,10 +86,8 @@ void kernel_main(uint32_t eax,uint32_t ebx) uint32_t sstdout = syscall_open("term",0,0); // stdout 1 uint32_t sstderr = syscall_open("stderr",0,0); // stderr 2 - // INSTALL INTERRUPTS (code segment: 0x08) - int_init(0x08); + interrupts_init(0x08); - // INIT MULTITASKING (and start scheduler) - task_init(dir); + scheduler_init(dir); } diff --git a/kernel/scheduler.c b/kernel/scheduler.c new file mode 100644 index 0000000..4823276 --- /dev/null +++ b/kernel/scheduler.c @@ -0,0 +1,222 @@ +// http://hosted.cjmovie.net/TutMultitask.htm +// +// +#define FOOLOS_MODULE_NAME "task" + +#include "kernel.h" +#include "lib/logger/log.h" // logger facilities +#include "mem.h" +#include "asm/x86.h" + +#include "vmem.h" +#include "syscalls.h" +#include "fs/fs.h" +#include "fs/ext2.h" + +#define MAX_TASKS 10 + +static volatile int volatile current_task=-1; + +static volatile struct task_list_struct +{ + volatile int parent; + volatile bool active; + volatile uint32_t esp; // stack pointer of the task; + volatile pdirectory *vmem; // number of virtual memory table to switch to + volatile bool waiting; + volatile bool skipwait; + volatile uint32_t brk; + +}volatile task_list[MAX_TASKS]; + +volatile int add_task(uint32_t esp, uint32_t vmem) +{ + + for(int i=0;i [%d] (free blocks remaining: %d )", current_task, pid,mem_get_free_blocks_count()); + return pid; +} + +// init task (root of all other tasks / processes) // +volatile void scheduler_init(pdirectory *dir) +{ + current_task=0; + + // this is our main task on slot 0 + task_list[0].parent=0; + task_list[0].active=true; + task_list[0].waiting=false; + task_list[0].vmem=dir; + task_list[0].esp = kballoc(4); // 0; // will be set by next task_switch_next() call. + + task_list[1].parent=0; + task_list[1].active=true; + task_list[1].waiting=false; + task_list[1].vmem=dir; + task_list[1].esp = kballoc(4); // fresh 16kb stack from here. + + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"fresh esp on: 0x%08X",1,task_list[1].esp); + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"fresh esp on: 0x%08X",0,task_list[0].esp); + + task_pusha(task_list[1].esp); // pusha but to alternative location + task_pusha(task_list[0].esp); // pusha but to alternative location + + /* + current_task=0; + unsigned esp, ebp, eax, ebx, ecx, edx; + + asm( + "movl %%esp, %0;" + "movl %%ebp, %1;" + "movl %%eax, %2;" + "movl %%ebx, %3;" + "movl %%ecx, %4;" + "movl %%edx, %5;" + :"=r"(esp), "=r"(ebp), "=r"(eax), "=r"(ebx), "=r"(ecx), "=r"(edx) + ); + + // TODO: prepare stack so popa get's what it wants! + int i=task_fork(esp); + */ + + // finally enable interrrupts so the scheduler is called (by timer) + x86_sti(); + + // loop until scheduler kicks in and reschedules us... + while(1); + +} + +volatile int task_get_current_pid() +{ + return current_task; +} + +volatile uint32_t task_get_brk() +{ + return task_list[current_task].brk; +} + +volatile void task_set_brk(uint32_t brk) +{ + task_list[current_task].brk=brk; +} diff --git a/kernel/scheduler.h b/kernel/scheduler.h new file mode 100644 index 0000000..db0e3ae --- /dev/null +++ b/kernel/scheduler.h @@ -0,0 +1 @@ +void scheduler_init(void *pdirectory_dir); diff --git a/kernel/task.c b/kernel/task.c deleted file mode 100644 index 395c196..0000000 --- a/kernel/task.c +++ /dev/null @@ -1,223 +0,0 @@ -// http://hosted.cjmovie.net/TutMultitask.htm -// -// -#define FOOLOS_MODULE_NAME "task" - -#include "kernel.h" -#include "lib/logger/log.h" // logger facilities -#include "mem.h" -#include "timer.h" -#include "asm/x86.h" - -#include "vmem.h" -#include "syscalls.h" -#include "fs/fs.h" -#include "fs/ext2.h" - -#define MAX_TASKS 10 - -static volatile int volatile current_task=-1; - -static volatile struct task_list_struct -{ - volatile int parent; - volatile bool active; - volatile uint32_t esp; // stack pointer of the task; - volatile pdirectory *vmem; // number of virtual memory table to switch to - volatile bool waiting; - volatile bool skipwait; - volatile uint32_t brk; - -}volatile task_list[MAX_TASKS]; - -volatile int add_task(uint32_t esp, uint32_t vmem) -{ - - for(int i=0;i [%d] (free blocks remaining: %d )", current_task, pid,mem_get_free_blocks_count()); - return pid; -} - -// init task (root of all other tasks / processes) // -volatile void task_init(pdirectory *dir) -{ - current_task=0; - - // this is our main task on slot 0 - task_list[0].parent=0; - task_list[0].active=true; - task_list[0].waiting=false; - task_list[0].vmem=dir; - task_list[0].esp = kballoc(4); // 0; // will be set by next task_switch_next() call. - - task_list[1].parent=0; - task_list[1].active=true; - task_list[1].waiting=false; - task_list[1].vmem=dir; - task_list[1].esp = kballoc(4); // fresh 16kb stack from here. - - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"fresh esp on: 0x%08X",1,task_list[1].esp); - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"fresh esp on: 0x%08X",0,task_list[0].esp); - - task_pusha(task_list[1].esp); // pusha but to alternative location - task_pusha(task_list[0].esp); // pusha but to alternative location - - /* - current_task=0; - unsigned esp, ebp, eax, ebx, ecx, edx; - - asm( - "movl %%esp, %0;" - "movl %%ebp, %1;" - "movl %%eax, %2;" - "movl %%ebx, %3;" - "movl %%ecx, %4;" - "movl %%edx, %5;" - :"=r"(esp), "=r"(ebp), "=r"(eax), "=r"(ebx), "=r"(ecx), "=r"(edx) - ); - - // TODO: prepare stack so popa get's what it wants! - int i=task_fork(esp); - */ - - // finally enable interrrupts so the scheduler is called (by timer) - x86_sti(); - - // loop until scheduler kicks in and reschedules us... - while(1); - -} - -volatile int task_get_current_pid() -{ - return current_task; -} - -volatile uint32_t task_get_brk() -{ - return task_list[current_task].brk; -} - -volatile void task_set_brk(uint32_t brk) -{ - task_list[current_task].brk=brk; -} diff --git a/kernel/task.h b/kernel/task.h deleted file mode 100644 index c934d6d..0000000 --- a/kernel/task.h +++ /dev/null @@ -1 +0,0 @@ -void task_init(pdirectory *dir); diff --git a/kernel/timer.c b/kernel/timer.c deleted file mode 100644 index 2d4c7ff..0000000 --- a/kernel/timer.c +++ /dev/null @@ -1,167 +0,0 @@ -#define FOOLOS_MODULE_NAME "timer" -#include "timer.h" - -#include "asm/x86.h" - -static volatile uint64_t task_system_clock_start=0; - -// CMOS RTC - -// read real time clock register -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 -static int get_rtc_update_flag() { - x86_outb(0x70, 0x0A); - return (x86_inb(0x71) & 0x80); -} - -// get real time clock rom cmos (seconds since jan) -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 - - 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; - } - - // 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; - - return epoch_seconds; -} - - -// PIT -uint64_t timer_init() -{ - uint64_t epoch_time=get_rtc_time(); - task_system_clock_start=epoch_time*25; // since pit ticks 25times a second - pit_init(); - return epoch_time; -} - -uint64_t timer_get_ms() -{ - return (pit_get_ticks()+task_system_clock_start)*40; -} - -uint64_t timer_get_uptime_ms() -{ - return pit_get_ticks()*40; -} diff --git a/kernel/timer.h b/kernel/timer.h deleted file mode 100644 index 1e3d066..0000000 --- a/kernel/timer.h +++ /dev/null @@ -1,50 +0,0 @@ - -/** - * @file - * References - * ---------- - * * 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 - -/** - * 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(); diff --git a/kernel/vmem.h b/kernel/vmem.h index 104e5ab..ae1e134 100644 --- a/kernel/vmem.h +++ b/kernel/vmem.h @@ -30,7 +30,6 @@ typedef struct ptable_struct { //! page directory typedef struct pdirectory_struct { - pd_entry m_entries[PAGES_PER_DIR]; }pdirectory; diff --git a/lib/logger/log.c b/lib/logger/log.c index 968d978..ff1b8bf 100644 --- a/lib/logger/log.c +++ b/lib/logger/log.c @@ -5,7 +5,6 @@ #include #include "kernel/kernel.h" -#include "kernel/timer.h" #include "kernel/fifo.h" #include "lib/string/string.h" -- cgit v1.2.3