From: Michalis Pappas Date: Tue, 25 Mar 2025 08:09:50 +0000 (+0100) Subject: lib/ukboot: Pass tmain to init context X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=0dc7050fd16b3c0c3ef5ed44540ff5a273d54bea;p=unikraft%2Funikraft.git lib/ukboot: Pass tmain to init context Populate the init context with the thread that executes main(). When LIBUKBOOT_MAINTHREAD is selected, this allows libposix-process to create INIT_PID from the main thread while excluding Unikraft's init thread from the process. Move the initialization of the main() thread before executing inittab. With that change, update the parameters main_thread() function to pass a pointer to ictx, so that it's possible for args to be updated by inittab. Signed-off-by: Michalis Pappas Approved-by: Andrei Tatar Approved-by: Sergiu Moga Reviewed-by: Andrei Tatar Reviewed-by: Sergiu Moga GitHub-Closes: #1620 --- diff --git a/lib/ukboot/boot.c b/lib/ukboot/boot.c index 65effd315..b0c14d851 100644 --- a/lib/ukboot/boot.c +++ b/lib/ukboot/boot.c @@ -114,8 +114,9 @@ int main(int argc, char *argv[]) __weak; static inline int do_main(int argc, char *argv[]); #if CONFIG_LIBUKBOOT_MAINTHREAD -static __noreturn void main_thread(void *, void *); +static __noreturn void main_thread(void *); static void main_thread_dtor(struct uk_thread *m); +struct uk_semaphore main_sema; #endif /* CONFIG_LIBUKBOOT_MAINTHREAD */ #if defined(CONFIG_LIBUKBOOT_HEAP_BASE) && defined(CONFIG_LIBUKVMEM) @@ -269,6 +270,8 @@ void uk_boot_entry(void) UK_ASSERT(boot_argc); #if CONFIG_LIBUKBOOT_MAINTHREAD + /* Initialize main thread semaphore */ + uk_semaphore_init(&main_sema, 0); /* Initialize shutdown control structure */ uk_boot_shutdown_ctl_init(); #endif /* CONFIG_LIBUKBOOT_MAINTHREAD */ @@ -364,6 +367,21 @@ void uk_boot_entry(void) ictx.cmdline.argc = boot_argc; ictx.cmdline.argv = boot_argv; +#if CONFIG_LIBUKBOOT_MAINTHREAD + /* Start main thread (will block on semaphore) */ + m = uk_sched_thread_create_fn1(s, main_thread, + &ictx, + 0x0 /* default stack size */, + 0x0 /* default auxiliary stack size */, + false, false, + "main", NULL, + main_thread_dtor); + if (unlikely(!m || PTRISERR(m))) + UK_CRASH("Failed to launch application's main()\n"); + + ictx.tmain = m; +#endif /* CONFIG_LIBUKBOOT_MAINTHREAD */ + /* Enable interrupts before starting the application */ ukplat_lcpu_enable_irq(); @@ -400,18 +418,8 @@ void uk_boot_entry(void) tctx.target = UKPLAT_HALT; #else /* CONFIG_LIBUKBOOT_MAINTHREAD */ - m = uk_sched_thread_create_fn2(s, main_thread, - (void *)((long)ictx.cmdline.argc), - (void *)ictx.cmdline.argv, - 0x0 /* default stack size */, - 0x0 /* default auxiliary stack size */, - false, false, - "main", NULL, - main_thread_dtor); - if (unlikely(!m || PTRISERR(m))) { - uk_pr_err("Failed to launch application's main()\n"); - goto exit; - } + /* Unblock main thread (will execute main()) */ + uk_semaphore_up(&main_sema); /* Block execution of "init" until we receive the first request */ tctx.target = uk_boot_shutdown_barrier(); @@ -505,12 +513,17 @@ static inline int do_main(int argc, char *argv[]) } #if CONFIG_LIBUKBOOT_MAINTHREAD -static __noreturn void main_thread(void *a_argc, void *a_argv) +/* Pass ictx so that inittab handlers can update args */ +static __noreturn void main_thread(void *ictx) { - int argc = (int)((__uptr)a_argc); - char **argv = (char **)a_argv; + UK_ASSERT(ictx); + + /* block until we are allowed to execute main() */ + uk_semaphore_down(&main_sema); + + do_main(((struct uk_init_ctx *)ictx)->cmdline.argc, + ((struct uk_init_ctx *)ictx)->cmdline.argv); - do_main(argc, argv); #if !CONFIG_LIBUKBOOT_MAINTHREAD_NOHALT /* NOTE: The scheduler's garbage collector would also initiate a * shutdown request via `main_thread_dtor()`.