From 7648b5a7b22ae3b3e3a3fa39fd83bce06554385d Mon Sep 17 00:00:00 2001 From: turret Date: Mon, 18 Dec 2023 18:40:19 -0600 Subject: init: minor optimisations move spin locks to dedicated function so we can minimise repetition of code. now uses more C and less assembly. init.c uses 8192 * 512 as a maximum even if the stack rlimit is larger --- init/init.c | 3 ++- init/log.c | 34 +++++++++++++++++++++------------- 2 files changed, 23 insertions(+), 14 deletions(-) (limited to 'init') diff --git a/init/init.c b/init/init.c index e2ece8a..806792f 100644 --- a/init/init.c +++ b/init/init.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -71,7 +72,7 @@ int main(void) struct rlimit *stack_rlimit = malloc(sizeof(struct rlimit)); getrlimit(RLIMIT_STACK, stack_rlimit); if(stack_rlimit->rlim_cur != RLIM_INFINITY) { - stack_size = stack_rlimit->rlim_cur; + stack_size = MIN(stack_rlimit->rlim_cur, stack_size); } free(stack_rlimit); diff --git a/init/log.c b/init/log.c index 75840a2..2df6d82 100644 --- a/init/log.c +++ b/init/log.c @@ -32,7 +32,23 @@ static const char *mode_to_string[] = { [PANICMODE_DIE] = "catastrophic failure", }; -int console_lock = 0; +static int console_lock = 0; + +static void obtain_console_lock() +{ + register int rax asm("rax"); +retry: + while(console_lock) + ; + + asm("mov %0, 1\n\t" + "xchg %0, %1" : "=r" (rax) : "m" (console_lock)); + + if(rax) + goto retry; + + return; +} static int vaprint(const char *fmt, va_list ap) { @@ -80,13 +96,9 @@ static int vaprint(const char *fmt, va_list ap) at the same time. I considered simply writing all at once, but ended up just not caring enough to the point where spinlocks prevail. */ - if(dolocks) { - __asm__(".spin_lock:"); - __asm__("mov rax, 1"); - __asm__("xchg rax, [console_lock]"); - __asm__("test rax, rax"); - __asm__("jnz .spin_lock"); - } + if(dolocks) + obtain_console_lock(); + /* we want to support stuff without colons, but frankly I havent tested this at time of writing. will find out later */ @@ -137,11 +149,7 @@ void _panic(const char *fileorigin, char **backtrace_symbolnames = backtrace_symbols(backtrace_addresses, backtrace_count); - __asm__("_panic.spin_lock:"); - __asm__("mov rax, 1"); - __asm__("xchg rax, [console_lock]"); - __asm__("test rax, rax"); - __asm__("jnz .spin_lock"); + obtain_console_lock(); print(NOLOCK("5") "------------[ cut here ]------------"); print(LOG_SOH "\7""0" "%s at %s:%d", mode_to_string[(int)mode], -- cgit v1.2.3