summaryrefslogtreecommitdiff
path: root/website-kernel
diff options
context:
space:
mode:
Diffstat (limited to 'website-kernel')
-rw-r--r--website-kernel/arch/kernel.c33
-rw-r--r--website-kernel/arch/mem.c15
-rw-r--r--website-kernel/arch/start.S8
-rw-r--r--website-kernel/fs/binfmt_elf.c12
-rw-r--r--website-kernel/fs/exec.S1
-rw-r--r--website-kernel/fs/fs_cpio.c76
-rw-r--r--website-kernel/include/fs/cpio.h24
-rw-r--r--website-kernel/include/fs/elf.h1
-rw-r--r--website-kernel/include/sys.h1
9 files changed, 164 insertions, 7 deletions
diff --git a/website-kernel/arch/kernel.c b/website-kernel/arch/kernel.c
index 32d9dbd..8b8db6f 100644
--- a/website-kernel/arch/kernel.c
+++ b/website-kernel/arch/kernel.c
@@ -1,3 +1,5 @@
+#include "fs/cpio.h"
+#include "fs/elf.h"
#include <int.h>
#include <log.h>
#include <sys.h>
@@ -6,6 +8,10 @@
#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;
@@ -22,6 +28,10 @@ void kernel_main(int hart, struct boot_args *args) {
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 ",
@@ -31,18 +41,41 @@ void kernel_main(int hart, struct boot_args *args) {
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");
+}
diff --git a/website-kernel/arch/mem.c b/website-kernel/arch/mem.c
index 301eb74..4b9a0b0 100644
--- a/website-kernel/arch/mem.c
+++ b/website-kernel/arch/mem.c
@@ -1,5 +1,6 @@
#include <int.h>
#include <sys.h>
+#include <log.h>
static struct mempage_t *root;
@@ -24,7 +25,6 @@ void set_root(struct mempage_t *incoming_root) {
}
void *kmalloc(size_t size) {
-
size = sizeof(size_t) + round_size(size, sizeof(struct mempage_t));
struct mempage_t *parent = NULL;
struct mempage_t *node = root;
@@ -92,3 +92,16 @@ void memzero(void *ptr, size_t size) {
((uint8_t *)ptr)[i] = 0;
}
}
+
+int memcmp(const void *s1, const void *s2, size_t n) {
+ const unsigned char *p1 = (const unsigned char *)s1;
+ const unsigned char *p2 = (const unsigned char *)s2;
+
+ for (size_t i = 0; i < n; i++) {
+ if (p1[i] != p2[i]) {
+ return p1[i] - p2[i];
+ }
+ }
+
+ return 0;
+}
diff --git a/website-kernel/arch/start.S b/website-kernel/arch/start.S
index 20ddeb1..b5c7a4b 100644
--- a/website-kernel/arch/start.S
+++ b/website-kernel/arch/start.S
@@ -2,12 +2,14 @@
.global _start
.type _start, @function
-.extern user_regs
_start:
csrw mstatus, zero
csrw mie, zero
+ la t0, _syscall
+ csrw mtvec, t0
+
la sp, __stack_top
la t0, __data_load
@@ -33,3 +35,7 @@ _start:
call kernel_main
wfi
+
+_syscall:
+ call handle_syscall
+ wfi
diff --git a/website-kernel/fs/binfmt_elf.c b/website-kernel/fs/binfmt_elf.c
index edf1781..f8073ae 100644
--- a/website-kernel/fs/binfmt_elf.c
+++ b/website-kernel/fs/binfmt_elf.c
@@ -1,5 +1,6 @@
#include <fs/elf.h>
#include <sys.h>
+#include <log.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
@@ -34,10 +35,6 @@ int elf_chk_header(struct elf_header *header) {
return 0;
}
- if (header->e_phoff != 0) {
- return 0; // Not PIE
- }
-
if (header->e_phentsize != sizeof(struct elf_program_header)) {
return 0;
}
@@ -66,6 +63,7 @@ void elf_exec(void *executable) {
struct elf_header *hdr = executable;
if (!elf_chk_header(hdr)) {
+ printk("Magic bytes invalid, returning...\n");
return;
}
@@ -88,11 +86,15 @@ void elf_exec(void *executable) {
continue;
}
- void *dst = (void *)(bias + (uintptr_t)base);
+ void *dst = (void *)(bias + phdrs[i].p_vaddr);
+ printk("section at file 0x%x will be loaded at 0x%x (v_addr requested: 0x%x)\n",
+ (uint8_t *)executable + phdrs[i].p_offset, dst, phdrs[i].p_vaddr);
memcpy((uint8_t *)executable + phdrs[i].p_offset, dst, phdrs[i].p_filesz);
memzero((uint8_t *)dst + phdrs[i].p_filesz, phdrs[i].p_memsz - phdrs[i].p_filesz);
}
+ printk("Mounted program and will jump to entrypoint 0x%x (e_entry: 0x%x)\n", (uintptr_t)(hdr->e_entry + bias), (uintptr_t)hdr->e_entry);
+
__userspace_invoke((elf_entry)(hdr->e_entry + bias));
kfree(base);
diff --git a/website-kernel/fs/exec.S b/website-kernel/fs/exec.S
index 745761d..c16e73f 100644
--- a/website-kernel/fs/exec.S
+++ b/website-kernel/fs/exec.S
@@ -4,6 +4,7 @@
# __userspace_invoke(fn exec *entry)
__userspace_invoke:
+ csrw mscratch, sp
csrw mepc, a0
csrw mie, zero
diff --git a/website-kernel/fs/fs_cpio.c b/website-kernel/fs/fs_cpio.c
new file mode 100644
index 0000000..bbab632
--- /dev/null
+++ b/website-kernel/fs/fs_cpio.c
@@ -0,0 +1,76 @@
+#include <fs/cpio.h>
+#include <sys.h>
+#include <int.h>
+#include <log.h>
+
+static inline int strcmp(const char *str1, const char* str2) {
+ while (*str1 && (*str1 == *str2)) {
+ ++str1;
+ ++str2;
+ }
+ return *(const unsigned char *)str1 - *(const unsigned char *)str2;
+}
+
+static uint32_t parse_hex(const char *p)
+{
+ uint32_t value = 0;
+
+ for (int i = 0; i < 8; i++) {
+ char c = p[i];
+ value <<= 4;
+
+ if (c >= '0' && c <= '9')
+ value |= c - '0';
+ else if (c >= 'A' && c <= 'F')
+ value |= c - 'A' + 10;
+ else if (c >= 'a' && c <= 'f')
+ value |= c - 'a' + 10;
+ }
+ return value;
+}
+
+void *cpio_get_file(void *archive, size_t size, const char *filename) {
+ const uint8_t *p = (uint8_t *)archive;
+ const uint8_t *end = archive + size;
+
+ while (p + CPIO_HEADER_LEN <= end) {
+ const struct cpio_newc_hdr *hdr =
+ (const struct cpio_newc_hdr *)p;
+
+ if (memcmp(hdr->magic, "070701", 6) &&
+ memcmp(hdr->magic, "070702", 6)) {
+ printk("Error parsing cpio: bad magic number!\n");
+ return NULL;
+ }
+
+ uint32_t namesize = parse_hex(hdr->namesize);
+ uint32_t filesize = parse_hex(hdr->filesize);
+
+ p += CPIO_HEADER_LEN;
+
+ if (p + namesize > end) {
+ printk("Error parsing cpio: truncated filename\n");
+ return NULL;
+ }
+
+ const char *name = (const char *)p;
+
+ if (strcmp(name, "TRAILER!!!") == 0)
+ return NULL;
+
+ p += namesize;
+
+ if (p + filesize > end) {
+ printk("Error parsing cpio: truncated file\n");
+ return NULL;
+ }
+
+ if (strcmp(name, filename) == 0) {
+ return (void*)p;
+ }
+
+ p += filesize;
+ }
+ return NULL;
+}
+
diff --git a/website-kernel/include/fs/cpio.h b/website-kernel/include/fs/cpio.h
new file mode 100644
index 0000000..43440d2
--- /dev/null
+++ b/website-kernel/include/fs/cpio.h
@@ -0,0 +1,24 @@
+#pragma once
+#include <int.h>
+#define CPIO_HEADER_LEN 110
+
+struct cpio_newc_hdr {
+ char magic[6];
+ char ino[8];
+ char mode[8];
+ char uid[8];
+ char gid[8];
+ char nlink[8];
+ char mtime[8];
+ char filesize[8];
+ char devmajor[8];
+ char devminor[8];
+ char rdevmajor[8];
+ char rdevminor[8];
+ char namesize[8];
+ char check[8];
+};
+
+void *cpio_get_file(void *archive, size_t size, const char *filename);
+
+
diff --git a/website-kernel/include/fs/elf.h b/website-kernel/include/fs/elf.h
index 8157384..65d2817 100644
--- a/website-kernel/include/fs/elf.h
+++ b/website-kernel/include/fs/elf.h
@@ -77,3 +77,4 @@ struct elf_program_header {
int elf_chk_header(struct elf_header *header);
struct elf_section_header *elf_get_section(struct elf_header *hdr, void *data, uint32_t index);
char *elf_get_section_name(struct elf_header *hdr, struct elf_section_header *section, void *data);
+void elf_exec(void *executable);
diff --git a/website-kernel/include/sys.h b/website-kernel/include/sys.h
index 00ab4b7..bc0279d 100644
--- a/website-kernel/include/sys.h
+++ b/website-kernel/include/sys.h
@@ -25,3 +25,4 @@ void kfree(void *ptr);
void memcpy(void *src, void *dst, size_t size);
void memzero(void *ptr, size_t size);
+int memcmp(const void *s1, const void *s2, size_t n);