summaryrefslogtreecommitdiff
path: root/website-kernel/arch/kernel.c
blob: 8b8db6f0ca28d8dee6e63f8c8f3b7c0c3a32059c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "fs/cpio.h"
#include "fs/elf.h"
#include <int.h>
#include <log.h>
#include <sys.h>

#define SERIAL_PORT 0x70000000

#define DRAM_DEVICE 0
#define UART_DEVICE 1
#define DRAM_RESERVED_INITRD 10
#define DRAM_RESERVED_OTHER 11

extern void set_root(struct mempage_t *incoming_root);

void serial_write(char value) {
    *(char *)SERIAL_PORT = value;
}

void kernel_main(int hart, struct boot_args *args) {
    if (hart != 0) {
        return;
    }
    register_console_callback(serial_write);

    printk("webkernel version %s (Compiler %s, Linker %s) on %s\n", VERSION, COMPILER_NAME, LINKER_NAME, ARCH_NAME);
    printk("Command line: %s\n", args->cmd_line);

    printk("BIOS provided device map:\n");
    struct device *root = args->devices;
    struct mempage_t *mem_root;

    void *initrd_base;
    size_t initrd_size;

    while (root != NULL) {
        printk("....Device at 0x%x to 0x%x is ",
               (uintptr_t)root->start,
               (uintptr_t)root->start + root->size
        );
        switch(root->device_type) {
            case DRAM_DEVICE:
                printk("generic DRAM device, usable\n");
                mem_root = (struct mempage_t *)root->start;
                mem_root->next = NULL;
                mem_root->size = root->size;
                break;
            case UART_DEVICE:
                printk("Web OS UART character device, not ns16550a compatible, MMIO device\n");
                break;
            case DRAM_RESERVED_INITRD:

                printk("generic DRAM device, not usable, reserved, initial ramdisk\n");
                initrd_base = (void *)root->start;
                initrd_size = root->size;
                break;
            case DRAM_RESERVED_OTHER:
                printk("generic DRAM device, not usable, reserved, other (likely kernel or firmware)\n");
                break;
            default:
                printk("unknown\n");

        }
        root = root->next;
    }

    set_root(mem_root);

    printk("Using initramfs mounted at 0x%x, size %d\n", (uintptr_t)initrd_base, initrd_size);
    printk("Skipping initramfs mount, invoking /sbin/init\n");
    void *file = cpio_get_file(initrd_base, initrd_size, "sbin/init");
    elf_exec(file);
}

void panic(char *cause) {
    printk("PANIC: %s\n", cause);
}

void handle_syscall() {
    panic("syscall handler unimplemented");
}