From 90436a4f46dec5ce3cc341e981b9aaf9f4e79605 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Wed, 14 Feb 2024 18:43:47 +0100 Subject: [PATCH] Add usleep syscall --- core/syscall.c | 12 +++++++++++- core/syscall.h | 1 + core/thread.c | 11 ++++++++--- core/thread.h | 1 + userspace/kernel/syscall.h | 1 + userspace/libc.c | 5 +++++ userspace/main_user.c | 2 ++ userspace/unistd.h | 4 ++++ 8 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 userspace/unistd.h diff --git a/core/syscall.c b/core/syscall.c index 288d5a7..5a5151f 100644 --- a/core/syscall.c +++ b/core/syscall.c @@ -11,7 +11,6 @@ #include "uaddrspace.h" #include "zero.h" - int syscallExecute(int syscallId, const struct cpu_state *userCtx) { @@ -146,6 +145,17 @@ int syscallExecute(int syscallId, const struct cpu_state *userCtx) break; } + case SYSCALL_ID_USLEEP: { + unsigned int sleep; + + ret = syscallGet1arg(userCtx, &sleep); + if (ret) + break; + + ret = threadUsleep(sleep); + + break; + } default: printf("Unknon syscall id %d\n", syscallId); ret = -ENOENT; diff --git a/core/syscall.h b/core/syscall.h index 72ccf9e..53cfa05 100644 --- a/core/syscall.h +++ b/core/syscall.h @@ -12,6 +12,7 @@ #define SYSCALL_ID_MMAP 7 #define SYSCALL_ID_MUNMAP 8 #define SYSCALL_ID_NEW_THREAD 9 +#define SYSCALL_ID_USLEEP 10 #ifdef __KERNEL__ int syscallExecute(int syscallId, const struct cpu_state *user_ctx); diff --git a/core/thread.c b/core/thread.c index ae0f293..cbb55af 100644 --- a/core/thread.c +++ b/core/thread.c @@ -292,6 +292,11 @@ int threadYield() } int threadMsleep(unsigned long msec) +{ + return threadUsleep(msec*1000); +} + +int threadUsleep(unsigned long usec) { uint32_t flags; struct thread *next, *current; @@ -299,12 +304,12 @@ int threadMsleep(unsigned long msec) disable_IRQs(flags); current = currentThread; - assertmsg(current->state == RUNNING, "thread %s is in state %d for %lu\n", current->name, - current->state, msec); + assertmsg(current->state == RUNNING, "thread %s is in state %d for %lu us\n", current->name, + current->state, usec); current->state = SLEEPING; current->sleepHaveTimeouted = 0; - current->jiffiesSleeping = msecs_to_jiffies(msec); + current->jiffiesSleeping = usecs_to_jiffies(usec); next = threadSelectNext(); assert(next != current); diff --git a/core/thread.h b/core/thread.h index 0997676..6da8cd7 100644 --- a/core/thread.h +++ b/core/thread.h @@ -83,6 +83,7 @@ int threadYield(); int threadWait(struct thread *current, struct thread *next, unsigned long msec); int threadUnsched(struct thread *th); int threadMsleep(unsigned long msec); +int threadUsleep(unsigned long usec); int threadOnJieffiesTick(); struct thread *getCurrentThread(); int threadAddThread(struct thread *th); diff --git a/userspace/kernel/syscall.h b/userspace/kernel/syscall.h index 72ccf9e..53cfa05 100644 --- a/userspace/kernel/syscall.h +++ b/userspace/kernel/syscall.h @@ -12,6 +12,7 @@ #define SYSCALL_ID_MMAP 7 #define SYSCALL_ID_MUNMAP 8 #define SYSCALL_ID_NEW_THREAD 9 +#define SYSCALL_ID_USLEEP 10 #ifdef __KERNEL__ int syscallExecute(int syscallId, const struct cpu_state *user_ctx); diff --git a/userspace/libc.c b/userspace/libc.c index ff91b25..fa793c2 100644 --- a/userspace/libc.c +++ b/userspace/libc.c @@ -5,6 +5,7 @@ #include "swintr.h" #include "sys/mman.h" #include "syscall.h" +#include "unistd.h" int errno = 0; int memcmp(const void *aptr, const void *bptr, size_t size) @@ -765,3 +766,7 @@ int new_thread(start_routine *func, void *arg, size_t stackSize) return syscall4(SYSCALL_ID_NEW_THREAD, (unsigned int)thread_runner, (unsigned int)func, (unsigned int)arg, stackSize); } + +int usleep(useconds_t usec) { + return syscall1(SYSCALL_ID_USLEEP, (unsigned int)usec); +} diff --git a/userspace/main_user.c b/userspace/main_user.c index 7354fdc..88128fd 100644 --- a/userspace/main_user.c +++ b/userspace/main_user.c @@ -4,6 +4,7 @@ #include "stddef.h" #include "sys/mman.h" #include "tiny.h" +#include "unistd.h" int func_help() { @@ -122,6 +123,7 @@ int func_munmap() static void *print_hello(void *arg) { (void)arg; printf("Hello World from thread\n"); + usleep(100); return NULL; } diff --git a/userspace/unistd.h b/userspace/unistd.h new file mode 100644 index 0000000..7d7da70 --- /dev/null +++ b/userspace/unistd.h @@ -0,0 +1,4 @@ +#pragma once + +typedef unsigned int useconds_t; +int usleep(useconds_t usec);