From 0189cc9d771e5911b07fbf91ca18da3a338f8d30 Mon Sep 17 00:00:00 2001 From: Nathan Lee Date: Sat, 3 Oct 2026 21:37:57 -0500 Subject: add first userspace invocation --- website-kernel/fs/binfmt_elf.c | 12 ++++--- website-kernel/fs/exec.S | 1 + website-kernel/fs/fs_cpio.c | 76 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 website-kernel/fs/fs_cpio.c (limited to 'website-kernel/fs') 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 #include +#include #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 +#include +#include +#include + +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; +} + -- cgit v1.2.3