diff --git a/include/task.h b/include/task.h index ef4f9d3..08c1d2e 100644 --- a/include/task.h +++ b/include/task.h @@ -70,6 +70,7 @@ typedef struct Process void* brk; uint32_t kstack_top; + size_t wake_up_time;//wake up time in ticks of the processor, gets set by nanosleep syscall } Process; extern Process* current; diff --git a/src/kernel/apic.c b/src/kernel/apic.c index cedbd96..e766498 100644 --- a/src/kernel/apic.c +++ b/src/kernel/apic.c @@ -153,7 +153,7 @@ void init_apic_timer() lapic_write(APIC_REGISTER_TIMER_DIV, 0x3); // Prepare the PIT to sleep for 10ms (10000µs) - init_pit_timer_apic(1000); + init_pit_timer_apic(10000); __asm__ __volatile__ ("sti"); diff --git a/src/tasking/syscalls.c b/src/tasking/syscalls.c index 6db3768..4bb3da1 100644 --- a/src/tasking/syscalls.c +++ b/src/tasking/syscalls.c @@ -7,6 +7,10 @@ #include "../include/screen.h" #include "../include/fat.h" #include "../include/vfs.h" +#include "../include/isr.h" +#include "../include/task.h" + +extern void switchProcess(Process* next); #define STDIN_FILENO 0 #define STDOUT_FILENO 1 @@ -299,6 +303,26 @@ uint32_t sys_brk(TrapFrame *tf) return (uint32_t)current->brk; } +typedef struct timespec { + int32_t tv_sec; // seconds + int32_t tv_nsec; // nanoseconds +} timespec; + +uint32_t sys_nanosleep(TrapFrame *tf) +{ + timespec* duration = (timespec*)tf->ebx; + size_t ms = duration->tv_sec * 1000 + duration->tv_nsec / 1000000; + //debug_log("waiting for 0x%X ms\n", ms); + size_t start_time = timer_ticks; + + current->wake_up_time = timer_ticks + ms; + //debug_log("setting wake up time to %X (current time is %X)\n", current->wake_up_time, timer_ticks); + + //TODO: find a way to call the scheduler from here without fucking shit up + + return 0; +} + void handle_syscall(TrapFrame *tf) { /* @@ -347,6 +371,10 @@ void handle_syscall(TrapFrame *tf) case 0x2d://brk tf->eax = sys_brk(tf); break; + + case 0xa2://nanosleep + tf->eax = sys_nanosleep(tf); + break; default: debug_log("unknown syscall: %X\n", tf->eax); diff --git a/src/tasking/task.c b/src/tasking/task.c index 68297dc..f0c8d68 100644 --- a/src/tasking/task.c +++ b/src/tasking/task.c @@ -4,6 +4,7 @@ #include "../include/string.h" #include "../include/liballoc.h" #include "../include/gdt.h" +#include "../include/isr.h" #include @@ -67,6 +68,8 @@ Process* task_create(uint32_t func, uint32_t user_esp, uint32_t ring, uint32_t* p->context->eip = (uint32_t)trapret; p->kesp = (uint32_t)sp; + p->wake_up_time = 0; + p->next = 0; if (!queue) { @@ -133,6 +136,11 @@ void schedule() { current = queue; // Start search from the beginning } + if(current->wake_up_time > timer_ticks) + { + current = queue; + } + // Find the next ready process to run Process* next = current ? current->next : NULL; if (!next) next = queue;