9ns: Add fs_files and tree_files
[akaros.git] / kern / src / init.c
index be24026..1304f28 100644 (file)
@@ -5,6 +5,7 @@
 #else
 
 #include <arch/arch.h>
+#include <arch/topology.h>
 #include <arch/console.h>
 #include <multiboot.h>
 #include <stab.h>
 #include <process.h>
 #include <trap.h>
 #include <syscall.h>
-#include <kclock.h>
 #include <manager.h>
 #include <testing.h>
 #include <kmalloc.h>
 #include <hashtable.h>
 #include <radix.h>
 #include <mm.h>
-#include <frontend.h>
+#include <ex_table.h>
+#include <percpu.h>
 
 #include <arch/init.h>
 #include <bitmask.h>
 #include <blockdev.h>
 #include <ext2fs.h>
 #include <kthread.h>
-#include <console.h>
 #include <linker_func.h>
-#include <ip.h>
+#include <net/ip.h>
 #include <acpi.h>
 #include <coreboot_tables.h>
 
-int booting = 1;
+#define MAX_BOOT_CMDLINE_SIZE 4096
+
+#define ASSIGN_PTRVAL(prm, top, val)                   \
+       do {                                                                            \
+               if (prm && (prm < top)) {                               \
+                       *prm = val;                                                     \
+                       prm++;                                                          \
+               }                                                                               \
+       } while (0)
+
+bool booting = TRUE;
+struct proc_global_info __proc_global_info;
 struct sysinfo_t sysinfo;
+static char boot_cmdline[MAX_BOOT_CMDLINE_SIZE];
+
 static void run_linker_funcs(void);
 static int run_init_script(void);
 
+const char *get_boot_option(const char *base, const char *option, char *param,
+                                                       size_t max_param)
+{
+       size_t optlen = strlen(option);
+       char *ptop = param + max_param - 1;
+       const char *opt, *arg;
+
+       if (!base)
+               base = boot_cmdline;
+       for (;;) {
+               opt = strstr(base, option);
+               if (!opt)
+                       return NULL;
+               if (((opt == base) || (opt[-1] == ' ')) &&
+                       ((opt[optlen] == 0) || (opt[optlen] == '=') ||
+                        (opt[optlen] == ' ')))
+                       break;
+               base = opt + optlen;
+       }
+       arg = opt + optlen;
+       if (*arg == '=') {
+               arg++;
+               if (*arg == '\'') {
+                       arg++;
+                       for (; *arg; arg++) {
+                               if (*arg == '\\')
+                                       arg++;
+                               else if (*arg == '\'')
+                                       break;
+                               ASSIGN_PTRVAL(param, ptop, *arg);
+                       }
+               } else {
+                       for (; *arg && (*arg != ' '); arg++)
+                               ASSIGN_PTRVAL(param, ptop, *arg);
+               }
+       }
+       ASSIGN_PTRVAL(param, ptop, 0);
+
+       return arg;
+}
+
+static void extract_multiboot_cmdline(struct multiboot_info *mbi)
+{
+       if (mbi && (mbi->flags & MULTIBOOT_INFO_CMDLINE) && mbi->cmdline) {
+               const char *cmdln = (const char *) KADDR(mbi->cmdline);
+
+               /* We need to copy the command line in a permanent buffer, since the
+                * multiboot memory where it is currently residing will be part of the
+                * free boot memory later on in the boot process.
+                */
+               strlcpy(boot_cmdline, cmdln, sizeof(boot_cmdline));
+       }
+}
+
+static void __kernel_init_part_deux(void *arg);
+
 void kernel_init(multiboot_info_t *mboot_info)
 {
-       extern char edata[], end[];
+       extern char __start_bss[], __stop_bss[];
 
-       memset(edata, 0, end - edata);
+       memset(__start_bss, 0, __stop_bss - __start_bss);
        /* mboot_info is a physical address.  while some arches currently have the
         * lower memory mapped, everyone should have it mapped at kernbase by now.
         * also, it might be in 'free' memory, so once we start dynamically using
         * memory, we may clobber it. */
        multiboot_kaddr = (struct multiboot_info*)((physaddr_t)mboot_info
                                                + KERNBASE);
+       extract_multiboot_cmdline(multiboot_kaddr);
+
        cons_init();
        print_cpuinfo();
 
-       cache_init();                                   // Determine systems's cache properties
+       printk("Boot Command Line: '%s'\n", boot_cmdline);
+
+       exception_table_init();
+       num_cores = get_early_num_cores();
        pmem_init(multiboot_kaddr);
-       kmem_cache_init();              // Sets up slab allocator
        kmalloc_init();
+       vmap_init();
        hashtable_init();
        radix_init();
-       cache_color_alloc_init();       // Inits data structs
-       colored_page_alloc_init();      // Allocates colors for agnostic processes
        acpiinit();
+       topology_init();
+       percpu_init();
        kthread_init();                                 /* might need to tweak when this happens */
        vmr_init();
-       file_init();
        page_check();
        idt_init();
+       /* After kthread_init and idt_init, we can use a real kstack. */
+       __use_real_kstack(__kernel_init_part_deux);
+}
+
+static void __kernel_init_part_deux(void *arg)
+{
        kernel_msg_init();
        timer_init();
        vfs_init();
        devfs_init();
-       train_timing();
-       kb_buf_init(&cons_buf);
+       time_init();
        arch_init();
        block_init();
        enable_irq();
@@ -93,13 +171,13 @@ void kernel_init(multiboot_info_t *mboot_info)
        devtabinit();
 
 #ifdef CONFIG_EXT2FS
-       mount_fs(&ext2_fs_type, "/dev/ramdisk", "/mnt", 0);
+       mount_fs(&ext2_fs_type, "/dev_vfs/ramdisk", "/mnt", 0);
 #endif /* CONFIG_EXT2FS */
 #ifdef CONFIG_ETH_AUDIO
        eth_audio_init();
 #endif /* CONFIG_ETH_AUDIO */
        get_coreboot_info(&sysinfo);
-       booting = 0;
+       booting = FALSE;
 
 #ifdef CONFIG_RUN_INIT_SCRIPT
        if (run_init_script()) {
@@ -131,12 +209,11 @@ static int run_init_script(void)
 
                /* Initialize l_argv with its first three arguments, but allocate space
                 * for all arguments as calculated above */
-               int static_args = 3;
+               int static_args = 2;
                int total_args = vargs + static_args;
                char *l_argv[total_args];
-               l_argv[0] = "";
-               l_argv[1] = "busybox";
-               l_argv[2] = "ash";
+               l_argv[0] = "/bin/bash";
+               l_argv[1] = "bash";
 
                /* Initialize l_argv with the rest of the arguments */
                int i = static_args;
@@ -178,12 +255,19 @@ void _panic(const char *file, int line, const char *fmt,...)
        cprintf("\n");
        va_end(ap);
 
-dead:
        monitor(NULL);
-       /* We could consider turning the lock checker back on here, but things are
-        * probably a mess anyways, and with it on we would probably lock up right
-        * away when we idle. */
-       //pcpui->__lock_checking_enabled++;
+       if (pcpui->cur_proc) {
+               printk("panic killing proc %d\n", pcpui->cur_proc->pid);
+               proc_destroy(pcpui->cur_proc);
+       }
+       /* Yikes!  We're claiming to be not in IRQ/trap ctx and not holding any
+        * locks.  Obviously we could be wrong, and could easily deadlock.  We could
+        * be in an IRQ handler, an unhandled kernel fault, or just a 'normal' panic
+        * in a syscall - any of which can involve unrestore invariants. */
+       pcpui->__ctx_depth = 0;
+       pcpui->lock_depth = 0;
+       if (pcpui->cur_kthread)
+               kth_panic_sysc(pcpui->cur_kthread);
        smp_idle();
 }