From bf9e5005b66c3e1219340837d3c29443dd0fb4e1 Mon Sep 17 00:00:00 2001
From: Ian Moffett <ian@osmora.org>
Date: Thu, 4 Jul 2024 23:44:14 -0400
Subject: kernel: init_main: Startup PID 1

Signed-off-by: Ian Moffett <ian@osmora.org>
---
 sys/kern/init_main.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c
index a5bdf1c..02e8705 100644
--- a/sys/kern/init_main.c
+++ b/sys/kern/init_main.c
@@ -32,13 +32,31 @@
 #include <sys/sched.h>
 #include <sys/mount.h>
 #include <sys/proc.h>
+#include <sys/exec.h>
+#include <sys/panic.h>
 #include <dev/cons/cons.h>
 #include <dev/acpi/acpi.h>
 #include <machine/cpu.h>
 #include <vm/vm.h>
+#include <string.h>
 
 static struct proc proc0;
 
+static void
+start_init(void)
+{
+    struct proc *td = this_td();
+    struct execve_args execve_args;
+    char *argv[] = { "/usr/sbin/init", NULL };
+    char *envp[] = { NULL };
+
+    execve_args.pathname = argv[0];
+    execve_args.argv = argv;
+    execve_args.envp = envp;
+    if (execve(td, &execve_args) != 0)
+        panic("Failed to load init\n");
+}
+
 int
 main(void)
 {
@@ -63,6 +81,10 @@ main(void)
     sched_init();
     mp_bootstrap_aps(&g_bsp_ci);
 
+    /* Startup init */
+    memset(&proc0, 0, sizeof(proc0.tf));
+    fork1(&proc0, 0, start_init, NULL);
+
     /* Nothing left to do... halt */
     cpu_reboot(REBOOT_HALT);
     __builtin_unreachable();
-- 
cgit v1.2.3