From c5fbfb0a72dd7fb1aa6563347bdc47603df9843f Mon Sep 17 00:00:00 2001 From: Tulio A M Mendes Date: Thu, 12 Feb 2026 00:25:31 -0300 Subject: [PATCH] =?utf8?q?feat:=20alarm()=20syscall=20(83)=20=E2=80=94=20p?= =?utf8?q?er-process=20SIGALRM=20timer=20via=20scheduler=20tick=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- include/process.h | 1 + include/syscall.h | 1 + src/kernel/scheduler.c | 4 ++++ src/kernel/syscall.c | 17 +++++++++++++++++ user/ulibc/include/syscall.h | 1 + user/ulibc/include/unistd.h | 1 + user/ulibc/src/unistd.c | 4 ++++ 7 files changed, 29 insertions(+) diff --git a/include/process.h b/include/process.h index 0be6a690..f2d32b70 100644 --- a/include/process.h +++ b/include/process.h @@ -66,6 +66,7 @@ struct process { int8_t nice; // -20 to +19 (maps to priority) process_state_t state; uint32_t wake_at_tick; + uint32_t alarm_tick; int exit_status; int has_user_regs; diff --git a/include/syscall.h b/include/syscall.h index adee9486..2cdb5a1c 100644 --- a/include/syscall.h +++ b/include/syscall.h @@ -115,6 +115,7 @@ enum { SYSCALL_SIGSUSPEND = 80, SYSCALL_READV = 81, SYSCALL_WRITEV = 82, + SYSCALL_ALARM = 83, }; #endif diff --git a/src/kernel/scheduler.c b/src/kernel/scheduler.c index 9bf571d0..147e7d8d 100644 --- a/src/kernel/scheduler.c +++ b/src/kernel/scheduler.c @@ -853,6 +853,10 @@ void process_wake_check(uint32_t current_tick) { rq_enqueue(rq_active, iter); } } + if (iter->alarm_tick != 0 && current_tick >= iter->alarm_tick) { + iter->alarm_tick = 0; + iter->sig_pending_mask |= (1U << 14); /* SIGALRM */ + } iter = iter->next; } while (iter != start); diff --git a/src/kernel/syscall.c b/src/kernel/syscall.c index 6f647c10..cfc6b941 100644 --- a/src/kernel/syscall.c +++ b/src/kernel/syscall.c @@ -2272,6 +2272,23 @@ void syscall_handler(struct registers* regs) { return; } + if (syscall_no == SYSCALL_ALARM) { + if (!current_process) { regs->eax = 0; return; } + uint32_t seconds = regs->ebx; + uint32_t now = get_tick_count(); + uint32_t old_remaining = 0; + if (current_process->alarm_tick > now) { + old_remaining = (current_process->alarm_tick - now) / 50 + 1; + } + if (seconds == 0) { + current_process->alarm_tick = 0; + } else { + current_process->alarm_tick = now + seconds * 50; + } + regs->eax = old_remaining; + return; + } + if (syscall_no == SYSCALL_SIGSUSPEND) { if (!current_process) { regs->eax = (uint32_t)-EINVAL; return; } uint32_t new_mask = 0; diff --git a/user/ulibc/include/syscall.h b/user/ulibc/include/syscall.h index f43ab027..4598efc3 100644 --- a/user/ulibc/include/syscall.h +++ b/user/ulibc/include/syscall.h @@ -74,6 +74,7 @@ enum { SYS_SIGSUSPEND = 80, SYS_READV = 81, SYS_WRITEV = 82, + SYS_ALARM = 83, }; /* Raw syscall wrappers — up to 5 args via INT 0x80 */ diff --git a/user/ulibc/include/unistd.h b/user/ulibc/include/unistd.h index af7f24df..6b480f84 100644 --- a/user/ulibc/include/unistd.h +++ b/user/ulibc/include/unistd.h @@ -51,6 +51,7 @@ int setuid(int uid); int setgid(int gid); int truncate(const char* path, int length); int ftruncate(int fd, int length); +unsigned int alarm(unsigned int seconds); void* brk(void* addr); void _exit(int status) __attribute__((noreturn)); diff --git a/user/ulibc/src/unistd.c b/user/ulibc/src/unistd.c index f1c04483..7ffeb054 100644 --- a/user/ulibc/src/unistd.c +++ b/user/ulibc/src/unistd.c @@ -131,6 +131,10 @@ int ftruncate(int fd, int length) { return __syscall_ret(_syscall2(SYS_FTRUNCATE, fd, length)); } +unsigned int alarm(unsigned int seconds) { + return (unsigned int)_syscall1(SYS_ALARM, (int)seconds); +} + void* brk(void* addr) { return (void*)_syscall1(SYS_BRK, (int)addr); } -- 2.43.0