libc+kernel: Add alarm() and getpagesize()

This commit is contained in:
apio 2023-08-11 18:09:12 +02:00
parent ec3c1132d2
commit 52064e0317
Signed by: apio
GPG Key ID: B8A7D06E42258954
6 changed files with 36 additions and 0 deletions

View File

@ -44,6 +44,7 @@ set(SOURCES
src/sys/signal.cpp src/sys/signal.cpp
src/sys/socket.cpp src/sys/socket.cpp
src/sys/poll.cpp src/sys/poll.cpp
src/sys/alarm.cpp
src/fs/VFS.cpp src/fs/VFS.cpp
src/fs/Pipe.cpp src/fs/Pipe.cpp
src/fs/Mount.cpp src/fs/Mount.cpp

15
kernel/src/sys/alarm.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
Result<u64> sys_alarm(Registers*, SyscallArgs args)
{
unsigned int seconds = (unsigned int)args[0];
auto* current = Scheduler::current();
u64 time_left = current->alarm_ticks_left;
current->alarm_ticks_left = seconds * 1000;
return time_left * 1000;
}

View File

@ -282,6 +282,8 @@ namespace Scheduler
{ {
if (thread->sleep_ticks_left == 0 || --thread->sleep_ticks_left == 0) thread->wake_up(); if (thread->sleep_ticks_left == 0 || --thread->sleep_ticks_left == 0) thread->wake_up();
} }
if (thread->alarm_ticks_left && --thread->alarm_ticks_left == 0) thread->send_signal(SIGALRM);
} }
if (!g_current->ticks_left) switch_task(regs); if (!g_current->ticks_left) switch_task(regs);

View File

@ -87,6 +87,7 @@ struct Thread : public LinkedListNode<Thread>
u64 ticks_left; u64 ticks_left;
u64 sleep_ticks_left; u64 sleep_ticks_left;
u64 alarm_ticks_left { 0 };
Stack stack; Stack stack;
Stack kernel_stack; Stack kernel_stack;

View File

@ -190,6 +190,12 @@ extern "C"
/* Truncate a file to a specific length. */ /* Truncate a file to a specific length. */
int ftruncate(int fd, size_t size); int ftruncate(int fd, size_t size);
/* Return the system page size. */
int getpagesize(void);
/* Schedule an alarm signal. */
unsigned int alarm(unsigned int seconds);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -8,6 +8,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <sys/utsname.h> #include <sys/utsname.h>
@ -510,4 +511,14 @@ extern "C"
long rc = syscall(SYS_ftruncate, fd, size); long rc = syscall(SYS_ftruncate, fd, size);
__errno_return(rc, int); __errno_return(rc, int);
} }
int getpagesize(void)
{
return PAGE_SIZE;
}
unsigned int alarm(unsigned int seconds)
{
return (unsigned int)syscall(SYS_alarm, seconds);
}
} }