From ed469e02c0a8937062d02b29c885a374085e1cac 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 ddc7e74..70b0570 100644 --- a/include/process.h +++ b/include/process.h @@ -57,6 +57,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 a82cb3b..ff6680c 100644 --- a/include/syscall.h +++ b/include/syscall.h @@ -106,6 +106,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 1597f87..f7cb152 100644 --- a/src/kernel/scheduler.c +++ b/src/kernel/scheduler.c @@ -844,6 +844,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 0bb88fe..41b3dc2 100644 --- a/src/kernel/syscall.c +++ b/src/kernel/syscall.c @@ -2263,6 +2263,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 333867a..7501627 100644 --- a/user/ulibc/include/syscall.h +++ b/user/ulibc/include/syscall.h @@ -65,6 +65,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 841053b..e2bb132 100644 --- a/user/ulibc/include/unistd.h +++ b/user/ulibc/include/unistd.h @@ -42,6 +42,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 3023195..609de9f 100644 --- a/user/ulibc/src/unistd.c +++ b/user/ulibc/src/unistd.c @@ -122,6 +122,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