syscalls: start trying to add nanosleep syscall

This commit is contained in:
2025-09-02 00:15:29 +03:00
parent a459751453
commit 5b922ea773
4 changed files with 38 additions and 1 deletions
+1 -1
View File
@@ -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");
+28
View File
@@ -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);
+8
View File
@@ -4,6 +4,7 @@
#include "../include/string.h"
#include "../include/liballoc.h"
#include "../include/gdt.h"
#include "../include/isr.h"
#include <stddef.h>
@@ -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;