vbos/init/main.c
Michael Shaw 5a67532500 utest: rename utest to init
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-22 01:13:18 -04:00

141 lines
3 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
/* vbos main entry point:
* VERY BAD OS: a hobby operating system
*
* Copyright 2023-2026 Michael Shaw
*/
#include <arch/proc.h>
#include <kernel/common.h>
#include <kernel/buddy.h>
#include <kernel/mem.h>
#include <kernel/device.h>
#include <kernel/requests.h>
#include <kernel/page.h>
#include <kernel/slab.h>
#include <kernel/proc.h>
#include <arch/init.h>
#include <kernel/tty.h>
#include <drivers/serial.h>
#include <drivers/pci.h>
#include <drivers/apic.h>
#include <drivers/acpi.h>
#include <fs/vfs.h>
#include <fs/inode.h>
#include <fs/cpio.h>
#include <fs/ramfs.h>
#include <fs/devfs.h>
#include <lib/stdio.h>
#include <lib/list.h>
#include <lib/rbtree.h>
#define P_STACK_SIZE 0x10000
typedef void (*initcall_t)(void);
struct tty *fbcon_init();
void arch_ap_startup();
extern initcall_t __initcall_start, __initcall_end;
struct tty *tty_printf = NULL;
bool kernel_init_ready = false;
static void do_initcalls()
{
for (initcall_t *fn = &__initcall_start; fn < &__initcall_end; fn++)
(*fn)();
}
static void kmain()
{
/* data types */
rbt_init();
list_init();
kalloc_init();
vfs_init();
arch_init();
page_init();
ramfs_init();
struct limine_file *file = module_res->modules[0];
fs_t *initramfs;
int res = cpio_read_and_init(file->address, file->size, &initramfs);
if (res < 0)
panic("failed to load initramfs: %s\n", strerror(-res));
/* "layer 2" kernel requirements */
devfs_init();
device_layer_init();
proc_init();
/* init main fallback tty */
struct tty *serial_tty = NULL;
serial_port_tty_init(&serial_tty);
struct tty *visual_tty = fbcon_init();
struct tty *tee = tty_tee_create("broadcast");
tty_tee_add(tee, serial_tty);
if (visual_tty)
tty_tee_add(tee, visual_tty);
tty_printf = tee;
do_initcalls();
arch_ap_startup();
apic_init();
dirent_t *init;
res = vfs_open("/bin/init", &init);
if (res < 0)
panic("failed to open /bin/utest: %s\n", strerror(-res));
char *init_buf = kalloc(init->inode->file_size, ALLOC_KERN);
if (!init_buf)
panic("failed to allocate buffer for /bin/utest: %s\n", strerror(ENOMEM));
res = vfs_read(init->inode, init_buf, init->inode->file_size, 0);
if (res < 0)
panic("failed to read /bin/utest: %s\n", strerror(-res));
struct proc *init_proc = proc_create(NULL);
uaddr_t entry = proc_load_file(init_buf, init_proc);
if ((ssize_t)entry < 0)
panic("failed to load /bin/utest: %s\n", strerror(-(ssize_t)entry));
kernel_init_ready = true;
init_proc->main_thread->state = PROC_STATE_RUNNING;
enter_userspace_init(init_proc->main_thread, entry);
__builtin_unreachable();
}
void _before_stack()
{
/* these first few functions must be called in this order
*
* after calling arch_stack_init, we will lose all of our stack
* variables. only the most minimum things needed to init the stack
* here.
*/
arch_early_init();
requests_init();
mem_init();
buddy_init();
void *bsp_stack = buddy_alloc(buddy_order(P_STACK_SIZE));
arch_stack_init(bsp_stack, P_STACK_SIZE);
/* end previous note */
kmain();
}