From 2c0efd93530c472e96ff3529aee983b53f3b78d9 Mon Sep 17 00:00:00 2001 From: Nathan Lee Date: Wed, 12 Aug 2026 17:44:17 -0500 Subject: rework project structure --- BuildConfig | 4 +- console/module_main.c | 4 +- include/bump.h | 10 ----- include/common.h | 39 ----------------- include/cpio.h | 23 ---------- include/dtb.h | 46 -------------------- include/early/bump.h | 9 ++++ include/early/dtb.h | 46 ++++++++++++++++++++ include/elf.h | 81 ---------------------------------- include/log.h | 10 ----- include/mem.h | 24 ---------- include/mmio.h | 8 ---- include/sys/cpio.h | 23 ++++++++++ include/sys/elf.h | 81 ++++++++++++++++++++++++++++++++++ include/sys/log.h | 10 +++++ include/sys/mem.h | 30 +++++++++++++ include/sys/stddef.h | 77 ++++++++++++++++++++++++++++++++ include/types.h | 41 ------------------ kernel/bump.c | 54 ----------------------- kernel/common/bump.c | 54 +++++++++++++++++++++++ kernel/common/cpio.c | 71 ++++++++++++++++++++++++++++++ kernel/common/dtb.c | 84 +++++++++++++++++++++++++++++++++++ kernel/common/elf.c | 40 +++++++++++++++++ kernel/common/log.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++ kernel/common/mem.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++ kernel/cpio.c | 73 ------------------------------- kernel/dtb.c | 86 ------------------------------------ kernel/elf.c | 41 ------------------ kernel/kernel.c | 46 -------------------- kernel/log.c | 109 ---------------------------------------------- kernel/mem.c | 118 -------------------------------------------------- kernel/riscv/kernel.c | 45 +++++++++++++++++++ kernel/riscv/start.S | 106 +++++++++++++++++++++++++++++++++++++++++++++ kernel/start.S | 106 --------------------------------------------- linker.ld | 1 + 35 files changed, 906 insertions(+), 919 deletions(-) delete mode 100644 include/bump.h delete mode 100644 include/common.h delete mode 100644 include/cpio.h delete mode 100644 include/dtb.h create mode 100644 include/early/bump.h create mode 100644 include/early/dtb.h delete mode 100644 include/elf.h delete mode 100644 include/log.h delete mode 100644 include/mem.h delete mode 100644 include/mmio.h create mode 100644 include/sys/cpio.h create mode 100644 include/sys/elf.h create mode 100644 include/sys/log.h create mode 100644 include/sys/mem.h create mode 100644 include/sys/stddef.h delete mode 100644 include/types.h delete mode 100644 kernel/bump.c create mode 100644 kernel/common/bump.c create mode 100644 kernel/common/cpio.c create mode 100644 kernel/common/dtb.c create mode 100644 kernel/common/elf.c create mode 100644 kernel/common/log.c create mode 100644 kernel/common/mem.c delete mode 100644 kernel/cpio.c delete mode 100644 kernel/dtb.c delete mode 100644 kernel/elf.c delete mode 100644 kernel/kernel.c delete mode 100644 kernel/log.c delete mode 100644 kernel/mem.c create mode 100644 kernel/riscv/kernel.c create mode 100644 kernel/riscv/start.S delete mode 100644 kernel/start.S diff --git a/BuildConfig b/BuildConfig index 6f160fe..000d4ee 100644 --- a/BuildConfig +++ b/BuildConfig @@ -1,4 +1,4 @@ -kernel: kernel/*.c kernel/*.S -kernel_target: kernel +kernel: kernel/riscv/*.c kernel/riscv/*.S kernel/common/*.c +kernel_target: kernel/riscv modules: console console: console/*.c diff --git a/console/module_main.c b/console/module_main.c index 643afc1..a3a71e8 100644 --- a/console/module_main.c +++ b/console/module_main.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include #ifndef __KERNEL_MODULE__ #define __KERNEL_MODULE__ diff --git a/include/bump.h b/include/bump.h deleted file mode 100644 index 81cd571..0000000 --- a/include/bump.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once -#include -#include -#include -#define EHEAP_SIZE (512 * 1024) - - -void* kemalloc(size_t size); -struct mem_info *km_find_memory(struct dt_node *head); - diff --git a/include/common.h b/include/common.h deleted file mode 100644 index d26b8ae..0000000 --- a/include/common.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -#include - -static inline size_t strlen(const char *s) { - size_t len = 0; - while (*s++) { - ++len; - } - return len; -} -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 inline 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; -} - -static inline void *memcpy(void *restrict dest, const void* restrict src, size_t size) { - unsigned char *d = (unsigned char *)dest; - unsigned char *s = (unsigned char *)src; - for (size_t i = 0; i < size; i++) { - d[i] = s[i]; - } - return d; -} diff --git a/include/cpio.h b/include/cpio.h deleted file mode 100644 index 3bb40c0..0000000 --- a/include/cpio.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once -#include -#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/include/dtb.h b/include/dtb.h deleted file mode 100644 index 1f8f14f..0000000 --- a/include/dtb.h +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once -#include - -#define FDT_MAGIC 0xEDFE0DD0 -#define FDT_BEGIN_NODE 1 -#define FDT_END_NODE 2 -#define FDT_PROP 3 -#define FDT_NOP 4 -#define FDT_END 9 - -#define DTB_MAX_NODES 16 - -struct dt_node { - char *name; - struct dt_prop *props; - struct dt_node *children; - struct dt_node *next; -}; - -struct dt_prop { - char *name; - char *value; - size_t len; - struct dt_prop *next; -}; - -struct fdt_header { - uint32_t magic; - uint32_t totalsize; - uint32_t off_dt_struct; - uint32_t off_dt_strings; - uint32_t off_mem_rsvmap; - uint32_t version; - uint32_t last_comp_version; - uint32_t boot_cpuid_phys; - uint32_t size_dt_strings; - uint32_t size_dt_struct; -}; - -struct dt_node *dt_parse(void *fdt); -void *dt_get_prop(struct dt_node *node, char *name, size_t *len); -uint32_t dt_read_u32(struct dt_node *node, char *name, uint32_t *out); - - -struct dt_node *dt_node_alloc(char *name); -struct dt_prop *dt_prop_alloc(char *name, void *value, size_t len); diff --git a/include/early/bump.h b/include/early/bump.h new file mode 100644 index 0000000..bf3e6f9 --- /dev/null +++ b/include/early/bump.h @@ -0,0 +1,9 @@ +#pragma once +#include +#include +#define EHEAP_SIZE (512 * 1024) + + +void* kemalloc(size_t size); +struct mem_info *km_find_memory(struct dt_node *head); + diff --git a/include/early/dtb.h b/include/early/dtb.h new file mode 100644 index 0000000..41a7bcc --- /dev/null +++ b/include/early/dtb.h @@ -0,0 +1,46 @@ +#pragma once +#include + +#define FDT_MAGIC 0xEDFE0DD0 +#define FDT_BEGIN_NODE 1 +#define FDT_END_NODE 2 +#define FDT_PROP 3 +#define FDT_NOP 4 +#define FDT_END 9 + +#define DTB_MAX_NODES 16 + +struct dt_node { + char *name; + struct dt_prop *props; + struct dt_node *children; + struct dt_node *next; +}; + +struct dt_prop { + char *name; + char *value; + size_t len; + struct dt_prop *next; +}; + +struct fdt_header { + uint32_t magic; + uint32_t totalsize; + uint32_t off_dt_struct; + uint32_t off_dt_strings; + uint32_t off_mem_rsvmap; + uint32_t version; + uint32_t last_comp_version; + uint32_t boot_cpuid_phys; + uint32_t size_dt_strings; + uint32_t size_dt_struct; +}; + +struct dt_node *dt_parse(void *fdt); +void *dt_get_prop(struct dt_node *node, char *name, size_t *len); +uint32_t dt_read_u32(struct dt_node *node, char *name, uint32_t *out); + + +struct dt_node *dt_node_alloc(char *name); +struct dt_prop *dt_prop_alloc(char *name, void *value, size_t len); diff --git a/include/elf.h b/include/elf.h deleted file mode 100644 index 76c384d..0000000 --- a/include/elf.h +++ /dev/null @@ -1,81 +0,0 @@ -#pragma once -#include - -#define ELF_MAGIC_LE 0x464C457F -#define ELF_PAD 0 - -#define EI_CLASS 4 -#define EI_DATA 5 -#define EI_VERSION 6 -#define EI_OSABI 7 -#define EI_ABI_VERSION 8 - -#define ELFDATA2LSB 1 -#define ELFDATA2MSB 2 - -#define ELFCLASS32 1 -#define ELFCLASS64 2 - -#define EV_CURRENT 1 - -#ifdef __SYS_LITTLE_ENDIAN__ - #define ELFHOSTDATA ELFDATA2LSB -#else - #define ELFHOSTDATA ELFDATA2MSB -#endif - -#ifdef __SYS_64_BIT__ - #define ELFHOSTCLASS ELFCLASS64 -#else - #define ELFHOSTCLASS ELFCLASS32 -#endif - -struct elf_header { - uint8_t e_ident[16]; - - uint16_t e_type; - uint16_t e_machine; - uint32_t e_version; - - uint32_t e_entry; - uint32_t e_phoff; - uint32_t e_shoff; - - uint32_t e_flags; - - uint16_t e_ehsize; - uint16_t e_phentsize; - uint16_t e_phnum; - - uint16_t e_shentsize; - uint16_t e_shnum; - uint16_t e_shstrndx; -}; - -struct elf_section_header { - uint32_t sh_name; - uint32_t sh_type; - uint32_t sh_flags; - uint32_t sh_addr; - uint32_t sh_offset; - uint32_t sh_size; - uint32_t sh_link; - uint32_t sh_info; - uint32_t sh_addralign; - uint32_t sh_entsize; -}; - -struct elf_program_header { - uint32_t p_type; - uint32_t p_offset; - uint32_t p_vaddr; - uint32_t p_paddr; - uint32_t p_filesz; - uint32_t p_memsz; - uint32_t p_flags; - uint32_t p_align; -}; - -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); diff --git a/include/log.h b/include/log.h deleted file mode 100644 index da9274b..0000000 --- a/include/log.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#define RINGBUF_SIZE 64 * 1024 - -typedef void (*console_callback_t)(char); - -void vprintk(const char *message, __builtin_va_list args); -void printk(const char *message, ...); - -void register_console_callback(console_callback_t callback); diff --git a/include/mem.h b/include/mem.h deleted file mode 100644 index bd6c911..0000000 --- a/include/mem.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include - -struct mem_info { - void* start; - size_t size; -}; - -enum PageStatus { - MEMPAGE_FREE, MEMPAGE_USED -}; - -struct page { - struct page* next; - size_t size; - enum PageStatus status; -}; - -void* kmalloc(size_t size); -void* kzalloc(size_t size); -void kfree(void* ptr); - -void km_init(struct mem_info *info); - diff --git a/include/mmio.h b/include/mmio.h deleted file mode 100644 index b6531bf..0000000 --- a/include/mmio.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -#include - -struct mmio_device_meta { - const char *name; - void *start; - size_t size; -}; diff --git a/include/sys/cpio.h b/include/sys/cpio.h new file mode 100644 index 0000000..c501e96 --- /dev/null +++ b/include/sys/cpio.h @@ -0,0 +1,23 @@ +#pragma once +#include +#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/include/sys/elf.h b/include/sys/elf.h new file mode 100644 index 0000000..95ed592 --- /dev/null +++ b/include/sys/elf.h @@ -0,0 +1,81 @@ +#pragma once +#include + +#define ELF_MAGIC_LE 0x464C457F +#define ELF_PAD 0 + +#define EI_CLASS 4 +#define EI_DATA 5 +#define EI_VERSION 6 +#define EI_OSABI 7 +#define EI_ABI_VERSION 8 + +#define ELFDATA2LSB 1 +#define ELFDATA2MSB 2 + +#define ELFCLASS32 1 +#define ELFCLASS64 2 + +#define EV_CURRENT 1 + +#ifdef __SYS_LITTLE_ENDIAN__ + #define ELFHOSTDATA ELFDATA2LSB +#else + #define ELFHOSTDATA ELFDATA2MSB +#endif + +#ifdef __SYS_64_BIT__ + #define ELFHOSTCLASS ELFCLASS64 +#else + #define ELFHOSTCLASS ELFCLASS32 +#endif + +struct elf_header { + uint8_t e_ident[16]; + + uint16_t e_type; + uint16_t e_machine; + uint32_t e_version; + + uint32_t e_entry; + uint32_t e_phoff; + uint32_t e_shoff; + + uint32_t e_flags; + + uint16_t e_ehsize; + uint16_t e_phentsize; + uint16_t e_phnum; + + uint16_t e_shentsize; + uint16_t e_shnum; + uint16_t e_shstrndx; +}; + +struct elf_section_header { + uint32_t sh_name; + uint32_t sh_type; + uint32_t sh_flags; + uint32_t sh_addr; + uint32_t sh_offset; + uint32_t sh_size; + uint32_t sh_link; + uint32_t sh_info; + uint32_t sh_addralign; + uint32_t sh_entsize; +}; + +struct elf_program_header { + uint32_t p_type; + uint32_t p_offset; + uint32_t p_vaddr; + uint32_t p_paddr; + uint32_t p_filesz; + uint32_t p_memsz; + uint32_t p_flags; + uint32_t p_align; +}; + +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); diff --git a/include/sys/log.h b/include/sys/log.h new file mode 100644 index 0000000..da9274b --- /dev/null +++ b/include/sys/log.h @@ -0,0 +1,10 @@ +#pragma once + +#define RINGBUF_SIZE 64 * 1024 + +typedef void (*console_callback_t)(char); + +void vprintk(const char *message, __builtin_va_list args); +void printk(const char *message, ...); + +void register_console_callback(console_callback_t callback); diff --git a/include/sys/mem.h b/include/sys/mem.h new file mode 100644 index 0000000..769a162 --- /dev/null +++ b/include/sys/mem.h @@ -0,0 +1,30 @@ +#pragma once +#include + +struct mem_info { + void* start; + size_t size; +}; + +enum PageStatus { + MEMPAGE_FREE, MEMPAGE_USED +}; + +struct page { + struct page* next; + size_t size; + enum PageStatus status; +}; + +struct mmio_device_meta { + const char *name; + void *start; + size_t size; +}; + +void* kmalloc(size_t size); +void* kzalloc(size_t size); +void kfree(void* ptr); + +void km_init(struct mem_info *info); + diff --git a/include/sys/stddef.h b/include/sys/stddef.h new file mode 100644 index 0000000..27aea74 --- /dev/null +++ b/include/sys/stddef.h @@ -0,0 +1,77 @@ +#pragma once +#define NULL (void*)0 + +#define uintptr_t __wk_size_t +#define size_t __wk_size_t +#define uint8_t __wk_uint8_t +#define uint16_t __wk_uint16_t +#define uint32_t __wk_uint32_t +#define uint64_t __wk_uint64_t + +#define htole + +#ifdef __SYS_LITTLE_ENDIAN__ + #define ltohs(x) x + #define ltohi(x) x + #define htols(x) x + #define htoli(x) x + + #define btohs(x) __builtin_bswap16(x) + #define btohi(x) __builtin_bswap32(x) + #define htobs(x) __builtin_bswap16(x) + #define htobi(x) __builtin_bswap32(x) +#else + #define ltohs(x) __builtin_bswap16(x) + #define ltohi(x) __builtin_bswap32(x) + #define htols(x) __builtin_bswap16(x) + #define htoli(x) __builtin_bswap32(x) + + #define btohs(x) x + #define btohi(x) x + #define htobs(x) x + #define htobi(x) x +#endif + +typedef __UINTPTR_TYPE__ __wk_size_t; +typedef __UINT8_TYPE__ __wk_uint8_t; +typedef __UINT16_TYPE__ __wk_uint16_t; +typedef __UINT32_TYPE__ __wk_uint32_t; +typedef __UINT64_TYPE__ __wk_uint64_t; + + +static inline size_t strlen(const char *s) { + size_t len = 0; + while (*s++) { + ++len; + } + return len; +} +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 inline 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; +} + +static inline void *memcpy(void *restrict dest, const void* restrict src, size_t size) { + unsigned char *d = (unsigned char *)dest; + unsigned char *s = (unsigned char *)src; + for (size_t i = 0; i < size; i++) { + d[i] = s[i]; + } + return d; +} diff --git a/include/types.h b/include/types.h deleted file mode 100644 index 5be3023..0000000 --- a/include/types.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#define NULL (void*)0 - -#define uintptr_t __wk_size_t -#define size_t __wk_size_t -#define uint8_t __wk_uint8_t -#define uint16_t __wk_uint16_t -#define uint32_t __wk_uint32_t -#define uint64_t __wk_uint64_t - -#define htole - -#ifdef __SYS_LITTLE_ENDIAN__ - #define ltohs(x) x - #define ltohi(x) x - #define htols(x) x - #define htoli(x) x - - #define btohs(x) __builtin_bswap16(x) - #define btohi(x) __builtin_bswap32(x) - #define htobs(x) __builtin_bswap16(x) - #define htobi(x) __builtin_bswap32(x) -#else - #define ltohs(x) __builtin_bswap16(x) - #define ltohi(x) __builtin_bswap32(x) - #define htols(x) __builtin_bswap16(x) - #define htoli(x) __builtin_bswap32(x) - - #define btohs(x) x - #define btohi(x) x - #define htobs(x) x - #define htobi(x) x -#endif - -typedef __UINTPTR_TYPE__ __wk_size_t; -typedef __UINT8_TYPE__ __wk_uint8_t; -typedef __UINT16_TYPE__ __wk_uint16_t; -typedef __UINT32_TYPE__ __wk_uint32_t; -typedef __UINT64_TYPE__ __wk_uint64_t; - diff --git a/kernel/bump.c b/kernel/bump.c deleted file mode 100644 index 8b7bd53..0000000 --- a/kernel/bump.c +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include - -static char eheap[EHEAP_SIZE] = {0}; -static size_t eheap_off = 0; - -struct mem_info *km_find_memory(struct dt_node *head) { - struct dt_node *child = head->children; - while (child) { - struct mem_info *location = km_find_memory(child); - if (location) { - return location; - } else { - child = child->next; - } - } - - if (strlen(head->name) != 0) { - struct dt_prop *prop_head = head->props; - while (prop_head) { - if (strcmp(prop_head->name, "device_type") == 0 - && strcmp(prop_head->value, "memory") == 0) { - size_t len = 0; - uint32_t *reg = (uint32_t *)dt_get_prop(head, "reg", &len); - if (reg && len >= 8) { - uint32_t base = btohi(reg[0]); - uint32_t size = btohi(reg[1]); - struct mem_info *ret = kemalloc(sizeof(struct mem_info)); - ret->start = (void*)base; - ret->size = (size_t)size; - - return ret; - } - } - prop_head = prop_head->next; - } - } - return NULL; -} - - -void* kemalloc(size_t size) { - size = (size + 7) & ~7; - - if (eheap_off + size > EHEAP_SIZE) { - return NULL; - } - - void *ptr = &eheap[eheap_off]; - eheap_off += size; - return ptr; -} - diff --git a/kernel/common/bump.c b/kernel/common/bump.c new file mode 100644 index 0000000..1e07837 --- /dev/null +++ b/kernel/common/bump.c @@ -0,0 +1,54 @@ +#include +#include +#include + +static char eheap[EHEAP_SIZE] = {0}; +static size_t eheap_off = 0; + +struct mem_info *km_find_memory(struct dt_node *head) { + struct dt_node *child = head->children; + while (child) { + struct mem_info *location = km_find_memory(child); + if (location) { + return location; + } else { + child = child->next; + } + } + + if (strlen(head->name) != 0) { + struct dt_prop *prop_head = head->props; + while (prop_head) { + if (strcmp(prop_head->name, "device_type") == 0 + && strcmp(prop_head->value, "memory") == 0) { + size_t len = 0; + uint32_t *reg = (uint32_t *)dt_get_prop(head, "reg", &len); + if (reg && len >= 8) { + uint32_t base = btohi(reg[0]); + uint32_t size = btohi(reg[1]); + struct mem_info *ret = kemalloc(sizeof(struct mem_info)); + ret->start = (void*)base; + ret->size = (size_t)size; + + return ret; + } + } + prop_head = prop_head->next; + } + } + return NULL; +} + + +void* kemalloc(size_t size) { + size = (size + 7) & ~7; + + if (eheap_off + size > EHEAP_SIZE) { + return NULL; + } + + void *ptr = &eheap[eheap_off]; + eheap_off += size; + return ptr; +} + diff --git a/kernel/common/cpio.c b/kernel/common/cpio.c new file mode 100644 index 0000000..c8f7850 --- /dev/null +++ b/kernel/common/cpio.c @@ -0,0 +1,71 @@ +#include +#include + +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; +} + +static size_t align4(size_t x) +{ + return (x + 3) & ~3; +} + +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 0; + + p += align4(namesize); + + if (p + filesize > end) { + printk("Error parsing cpio: truncated file\n"); + return NULL; + } + + if (strcmp(name, filename) == 0) { + return (void*)p; + } + + p += align4(filesize); + } + return NULL; +} + diff --git a/kernel/common/dtb.c b/kernel/common/dtb.c new file mode 100644 index 0000000..b345db0 --- /dev/null +++ b/kernel/common/dtb.c @@ -0,0 +1,84 @@ +#include +#include + +struct dt_node *dt_parse(void *fdt) { + struct fdt_header *hdr = fdt; + + if (hdr->magic != FDT_MAGIC) { + return NULL; + } + + uint32_t *p = (uint32_t *)(fdt + btohi(hdr->off_dt_struct)); + char *strings = fdt + btohi(hdr->off_dt_strings); + + struct dt_node *root = NULL; + struct dt_node *stack[DTB_MAX_NODES]; + int sp = 0; + + while (1) { + uint32_t token = btohi(*p++); + switch (token) { + case FDT_BEGIN_NODE: { + char *name = (char *)p; + p += (strlen(name) + 3) / 4; + + struct dt_node *node = dt_node_alloc(name); + if (sp == 0) { + root = node; + } else { + node->next = stack[sp-1]->children; + stack[sp-1]->children = node; + } + stack[sp++] = node; + break; + } + case FDT_PROP: { + uint32_t len = btohi(*p++); + uint32_t nameoff = btohi(*p++); + void *value = p; + p += (len + 3) / 4; + + struct dt_prop *prop = dt_prop_alloc(strings + nameoff, value, len); + prop->next = stack[sp-1]->props; + stack[sp-1]->props = prop; + break; + } + case FDT_END_NODE: + sp--; + break; + case FDT_END: + return root; + } + } +} + +void *dt_get_prop(struct dt_node *node, char *name, size_t *len) { + for (struct dt_prop *p = node->props; p; p = p->next) { + if (strcmp(p->name, name) == 0) { + if (len) *len = p->len; + return p->value; + } + } + return NULL; +} + +struct dt_node *dt_node_alloc(char *name) { + struct dt_node *node = kemalloc(sizeof(struct dt_node)); + node->name = name; + node->props = NULL; + node->children = NULL; + node->next = NULL; + return node; +} + +struct dt_prop *dt_prop_alloc(char *name, void *value, size_t len) { + struct dt_prop *prop = kemalloc(sizeof(struct dt_prop)); + prop->name = name; + prop->value = value; + prop->len = len; + prop->next = NULL; + + return prop; +} + + diff --git a/kernel/common/elf.c b/kernel/common/elf.c new file mode 100644 index 0000000..871eee7 --- /dev/null +++ b/kernel/common/elf.c @@ -0,0 +1,40 @@ +#include + +int elf_chk_header(struct elf_header *header) { + uint32_t magic_chk; + memcpy(&magic_chk, header->e_ident, sizeof(uint32_t)); + if (magic_chk != ltohi(ELF_MAGIC_LE)) { + return -1; + } + + if (header->e_ident[EI_CLASS] != ELFHOSTCLASS) { + return -1; + } + + if (header->e_ident[EI_DATA] != ELFHOSTDATA) { + return -1; + } + + if (header->e_ident[EI_VERSION] != EV_CURRENT) { + return -1; + } + + return 0; +} + +struct elf_section_header *elf_get_section(struct elf_header *hdr, void *data, uint32_t index) { + uint8_t *base = (uint8_t *)data; + + if (index >= hdr->e_shnum) { + return 0; + } + + return (struct elf_section_header *)(base + + hdr->e_shoff + + index * hdr->e_shentsize); +} + +char *elf_get_section_name(struct elf_header *hdr, struct elf_section_header *section, void *data) { + struct elf_section_header *shstrtab = elf_get_section(hdr, data, hdr->e_shstrndx); + return (char *)((uintptr_t)section->sh_name + (uintptr_t)shstrtab->sh_offset + (uintptr_t)data); +} diff --git a/kernel/common/log.c b/kernel/common/log.c new file mode 100644 index 0000000..88136f2 --- /dev/null +++ b/kernel/common/log.c @@ -0,0 +1,109 @@ +#include + +__attribute__((section(".ringbuf"), aligned(64))) +volatile char log_ring[RINGBUF_SIZE]; +static int pos = 0; + +void stub(char c) { + (void)c; + return; +} + +console_callback_t callback = stub; + +void put_char(char c) { + log_ring[pos++] = c; + if (pos >= RINGBUF_SIZE) + pos = 0; + + callback(c); +} + +void put_string(const char *s) { + while (*s) + put_char(*s++); +} + +void put_uint(unsigned int value, unsigned int radix) { + char tmp[16]; + const char digits[] = "0123456789abcdef"; + int i = 0; + + if (value == 0) { + put_char('0'); + return; + } + + while (value) { + tmp[i++] = digits[value % radix]; + value /= radix; + } + + while (i--) + put_char(tmp[i]); +} + +void put_int(int value) { + if (value < 0) { + put_char('-'); + put_uint((unsigned int)(-value), 10); + } else { + put_uint((unsigned int)value, 10); + } +} + +void vprintk(const char *fmt, __builtin_va_list args) { + while (*fmt) { + if (*fmt != '%') { + put_char(*fmt++); + continue; + } + + fmt++; + + switch (*fmt) { + case '%': + put_char('%'); + break; + + case 'c': + put_char((char)__builtin_va_arg(args, int)); + break; + + case 's': + put_string(__builtin_va_arg(args, const char *)); + break; + + case 'd': + case 'i': + put_int(__builtin_va_arg(args, int)); + break; + + case 'u': + put_uint(__builtin_va_arg(args, unsigned int), 10); + break; + + case 'x': + put_uint(__builtin_va_arg(args, unsigned int), 16); + break; + + default: + put_char('%'); + put_char(*fmt); + break; + } + + fmt++; + } +} + +void printk(const char *fmt, ...) { + __builtin_va_list args; + __builtin_va_start(args, fmt); + vprintk(fmt, args); + __builtin_va_end(args); +} + +void register_console_callback(console_callback_t cb) { + callback = cb; +} diff --git a/kernel/common/mem.c b/kernel/common/mem.c new file mode 100644 index 0000000..fee0761 --- /dev/null +++ b/kernel/common/mem.c @@ -0,0 +1,116 @@ +#include +#include + +static struct page *free_list; + +static void merge_freelist() { + struct page *list = free_list; + + while (list && list->next) { + if ((void*)list + list->size == list->next) { + list->size += list->next->size; + list->next = list->next->next; + continue; + } + list = list->next; + } +} + +void* kmalloc(size_t size) { + struct page *page = free_list; + struct page *last = NULL; + + size_t combined_size = size + sizeof(struct page); + + while (page && page->size < combined_size) { + last = page; + page = page->next; + } + + if (page == NULL) + return NULL; + + + if (page->size - combined_size < sizeof(struct page)) { + // Return the entire page + if (last) + last->next = page->next; + else + free_list = page->next; + + } else { + // break page and create a new one + struct page *broken = (void *)page + combined_size; + broken->size = page->size - combined_size; + broken->next = page->next; + broken->status = MEMPAGE_FREE; + + page->size = combined_size; + page->next = broken; + + if (last) + last->next = broken; + else + free_list = broken; + } + + page->status = MEMPAGE_USED; + + return (void *)page + sizeof(struct page); +} + +void* kzalloc(size_t size) { + char *ptr = kmalloc(size); + + if (!ptr) + return NULL; + + for (size_t i = 0; i < size; i++) { + *(ptr + i) = 0; + } + + return ptr; +} + +void kfree(void *ptr) { + if (!ptr) + return; + + struct page *page_info = (struct page *)(ptr - sizeof(struct page)); + struct page *iter = free_list; + + page_info->status = MEMPAGE_FREE; + + if (!iter) { + free_list = page_info; + page_info->next = NULL; + goto merge; + } + if (page_info < free_list) { + page_info->next = iter; + free_list = page_info; + goto merge; + } + + while (iter->next && iter->next < page_info) { + iter = iter->next; + } + + page_info->next = iter->next; + iter->next = page_info; + + merge: + merge_freelist(); +} + + +void km_init(struct mem_info *info) { + struct page *base = (struct page *)info->start; + base->next = NULL; + base->size = info->size - sizeof(struct page); + base->status = MEMPAGE_FREE; + + free_list = base; +} + + diff --git a/kernel/cpio.c b/kernel/cpio.c deleted file mode 100644 index 565b073..0000000 --- a/kernel/cpio.c +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include -#include -#include - -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; -} - -static size_t align4(size_t x) -{ - return (x + 3) & ~3; -} - -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 0; - - p += align4(namesize); - - if (p + filesize > end) { - printk("Error parsing cpio: truncated file\n"); - return NULL; - } - - if (strcmp(name, filename) == 0) { - return (void*)p; - } - - p += align4(filesize); - } - return NULL; -} - diff --git a/kernel/dtb.c b/kernel/dtb.c deleted file mode 100644 index 7647767..0000000 --- a/kernel/dtb.c +++ /dev/null @@ -1,86 +0,0 @@ - -#include -#include -#include - -struct dt_node *dt_parse(void *fdt) { - struct fdt_header *hdr = fdt; - - if (hdr->magic != FDT_MAGIC) { - return NULL; - } - - uint32_t *p = (uint32_t *)(fdt + btohi(hdr->off_dt_struct)); - char *strings = fdt + btohi(hdr->off_dt_strings); - - struct dt_node *root = NULL; - struct dt_node *stack[DTB_MAX_NODES]; - int sp = 0; - - while (1) { - uint32_t token = btohi(*p++); - switch (token) { - case FDT_BEGIN_NODE: { - char *name = (char *)p; - p += (strlen(name) + 3) / 4; - - struct dt_node *node = dt_node_alloc(name); - if (sp == 0) { - root = node; - } else { - node->next = stack[sp-1]->children; - stack[sp-1]->children = node; - } - stack[sp++] = node; - break; - } - case FDT_PROP: { - uint32_t len = btohi(*p++); - uint32_t nameoff = btohi(*p++); - void *value = p; - p += (len + 3) / 4; - - struct dt_prop *prop = dt_prop_alloc(strings + nameoff, value, len); - prop->next = stack[sp-1]->props; - stack[sp-1]->props = prop; - break; - } - case FDT_END_NODE: - sp--; - break; - case FDT_END: - return root; - } - } -} - -void *dt_get_prop(struct dt_node *node, char *name, size_t *len) { - for (struct dt_prop *p = node->props; p; p = p->next) { - if (strcmp(p->name, name) == 0) { - if (len) *len = p->len; - return p->value; - } - } - return NULL; -} - -struct dt_node *dt_node_alloc(char *name) { - struct dt_node *node = kemalloc(sizeof(struct dt_node)); - node->name = name; - node->props = NULL; - node->children = NULL; - node->next = NULL; - return node; -} - -struct dt_prop *dt_prop_alloc(char *name, void *value, size_t len) { - struct dt_prop *prop = kemalloc(sizeof(struct dt_prop)); - prop->name = name; - prop->value = value; - prop->len = len; - prop->next = NULL; - - return prop; -} - - diff --git a/kernel/elf.c b/kernel/elf.c deleted file mode 100644 index 69c4128..0000000 --- a/kernel/elf.c +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include - -int elf_chk_header(struct elf_header *header) { - uint32_t magic_chk; - memcpy(&magic_chk, header->e_ident, sizeof(uint32_t)); - if (magic_chk != ltohi(ELF_MAGIC_LE)) { - return -1; - } - - if (header->e_ident[EI_CLASS] != ELFHOSTCLASS) { - return -1; - } - - if (header->e_ident[EI_DATA] != ELFHOSTDATA) { - return -1; - } - - if (header->e_ident[EI_VERSION] != EV_CURRENT) { - return -1; - } - - return 0; -} - -struct elf_section_header *elf_get_section(struct elf_header *hdr, void *data, uint32_t index) { - uint8_t *base = (uint8_t *)data; - - if (index >= hdr->e_shnum) { - return 0; - } - - return (struct elf_section_header *)(base - + hdr->e_shoff - + index * hdr->e_shentsize); -} - -char *elf_get_section_name(struct elf_header *hdr, struct elf_section_header *section, void *data) { - struct elf_section_header *shstrtab = elf_get_section(hdr, data, hdr->e_shstrndx); - return (char *)((uintptr_t)section->sh_name + (uintptr_t)shstrtab->sh_offset + (uintptr_t)data); -} diff --git a/kernel/kernel.c b/kernel/kernel.c deleted file mode 100644 index de556e7..0000000 --- a/kernel/kernel.c +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include -#include -#include -#include - -#define KERNEL_SIZE 3 * 1024 * 1024 - -uint32_t user_regs[31]; - -void kernel_main(size_t hart, void *fdt) { - printk("Doing kernel init with hard ID %d\n", hart); - - struct dt_node *head = dt_parse(fdt); - struct mem_info *info = km_find_memory(head); - - - if (!info) { - printk("failed to find memory info!\n"); - return; - } - - info->start = (void *)((uint8_t *)info->start + KERNEL_SIZE); - info->size -= KERNEL_SIZE; - - km_init(info); - - printk("memory blocks initialized\n"); - - size_t len = 0; - uint32_t *initrd = dt_get_prop(head, "initrd", &len); - if (!initrd || len < 8) { - printk("failed to find initrd!\n"); - return; - } - uint32_t base = btohi(initrd[0]); - uint32_t size = btohi(initrd[1]); - - char *file = cpio_get_file((void*)base, (size_t)size, "/sbin/init"); - (void)file; -} - -void kernel_trap() { - - -} diff --git a/kernel/log.c b/kernel/log.c deleted file mode 100644 index 84b52d3..0000000 --- a/kernel/log.c +++ /dev/null @@ -1,109 +0,0 @@ -#include -#include -__attribute__((section(".ringbuf"), aligned(64))) -volatile char log_ring[RINGBUF_SIZE]; -static int pos = 0; - -void stub(char c) { - (void)c; - return; -} - -console_callback_t callback = stub; - -void put_char(char c) { - log_ring[pos++] = c; - if (pos >= RINGBUF_SIZE) - pos = 0; - - callback(c); -} - -void put_string(const char *s) { - while (*s) - put_char(*s++); -} - -void put_uint(unsigned int value, unsigned int radix) { - char tmp[16]; - const char digits[] = "0123456789abcdef"; - int i = 0; - - if (value == 0) { - put_char('0'); - return; - } - - while (value) { - tmp[i++] = digits[value % radix]; - value /= radix; - } - - while (i--) - put_char(tmp[i]); -} - -void put_int(int value) { - if (value < 0) { - put_char('-'); - put_uint((unsigned int)(-value), 10); - } else { - put_uint((unsigned int)value, 10); - } -} - -void vprintk(const char *fmt, __builtin_va_list args) { - while (*fmt) { - if (*fmt != '%') { - put_char(*fmt++); - continue; - } - - fmt++; - - switch (*fmt) { - case '%': - put_char('%'); - break; - - case 'c': - put_char((char)__builtin_va_arg(args, int)); - break; - - case 's': - put_string(__builtin_va_arg(args, const char *)); - break; - - case 'd': - case 'i': - put_int(__builtin_va_arg(args, int)); - break; - - case 'u': - put_uint(__builtin_va_arg(args, unsigned int), 10); - break; - - case 'x': - put_uint(__builtin_va_arg(args, unsigned int), 16); - break; - - default: - put_char('%'); - put_char(*fmt); - break; - } - - fmt++; - } -} - -void printk(const char *fmt, ...) { - __builtin_va_list args; - __builtin_va_start(args, fmt); - vprintk(fmt, args); - __builtin_va_end(args); -} - -void register_console_callback(console_callback_t cb) { - callback = cb; -} diff --git a/kernel/mem.c b/kernel/mem.c deleted file mode 100644 index 2a6bd35..0000000 --- a/kernel/mem.c +++ /dev/null @@ -1,118 +0,0 @@ -#include -#include -#include -#include - -static struct page *free_list; - -static void merge_freelist() { - struct page *list = free_list; - - while (list && list->next) { - if ((void*)list + list->size == list->next) { - list->size += list->next->size; - list->next = list->next->next; - continue; - } - list = list->next; - } -} - -void* kmalloc(size_t size) { - struct page *page = free_list; - struct page *last = NULL; - - size_t combined_size = size + sizeof(struct page); - - while (page && page->size < combined_size) { - last = page; - page = page->next; - } - - if (page == NULL) - return NULL; - - - if (page->size - combined_size < sizeof(struct page)) { - // Return the entire page - if (last) - last->next = page->next; - else - free_list = page->next; - - } else { - // break page and create a new one - struct page *broken = (void *)page + combined_size; - broken->size = page->size - combined_size; - broken->next = page->next; - broken->status = MEMPAGE_FREE; - - page->size = combined_size; - page->next = broken; - - if (last) - last->next = broken; - else - free_list = broken; - } - - page->status = MEMPAGE_USED; - - return (void *)page + sizeof(struct page); -} - -void* kzalloc(size_t size) { - char *ptr = kmalloc(size); - - if (!ptr) - return NULL; - - for (size_t i = 0; i < size; i++) { - *(ptr + i) = 0; - } - - return ptr; -} - -void kfree(void *ptr) { - if (!ptr) - return; - - struct page *page_info = (struct page *)(ptr - sizeof(struct page)); - struct page *iter = free_list; - - page_info->status = MEMPAGE_FREE; - - if (!iter) { - free_list = page_info; - page_info->next = NULL; - goto merge; - } - if (page_info < free_list) { - page_info->next = iter; - free_list = page_info; - goto merge; - } - - while (iter->next && iter->next < page_info) { - iter = iter->next; - } - - page_info->next = iter->next; - iter->next = page_info; - - merge: - merge_freelist(); -} - - -void km_init(struct mem_info *info) { - struct page *base = (struct page *)info->start; - base->next = NULL; - base->size = info->size - sizeof(struct page); - base->status = MEMPAGE_FREE; - - free_list = base; -} - - diff --git a/kernel/riscv/kernel.c b/kernel/riscv/kernel.c new file mode 100644 index 0000000..4e72e2a --- /dev/null +++ b/kernel/riscv/kernel.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include + +#define KERNEL_SIZE 3 * 1024 * 1024 + +uint32_t user_regs[31]; + +void kernel_main(size_t hart, void *fdt) { + printk("Doing kernel init with hard ID %d\n", hart); + + struct dt_node *head = dt_parse(fdt); + struct mem_info *info = km_find_memory(head); + + + if (!info) { + printk("failed to find memory info!\n"); + return; + } + + info->start = (void *)((uint8_t *)info->start + KERNEL_SIZE); + info->size -= KERNEL_SIZE; + + km_init(info); + + printk("memory blocks initialized\n"); + + size_t len = 0; + uint32_t *initrd = dt_get_prop(head, "initrd", &len); + if (!initrd || len < 8) { + printk("failed to find initrd!\n"); + return; + } + uint32_t base = btohi(initrd[0]); + uint32_t size = btohi(initrd[1]); + + char *file = cpio_get_file((void*)base, (size_t)size, "/sbin/init"); + (void)file; +} + +void kernel_trap() { + + +} diff --git a/kernel/riscv/start.S b/kernel/riscv/start.S new file mode 100644 index 0000000..ecec47b --- /dev/null +++ b/kernel/riscv/start.S @@ -0,0 +1,106 @@ +.section .text._start +.global _start +.type _start, @function +.type _syscall_start, @function +.type _syscall_exit, @function + +.extern user_regs + +_start: + csrw sstatus, zero + csrw sie, zero + + la t0, _syscall_start + csrw stvec, t0 + + la sp, __stack_top + + la t0, __bss_start + la t1, __bss_end + beq t0, t1, 2f +1: + sw zero, 0(t0) + addi t0, t0, 4 + bltu t0, t1, 1b +2: + call kernel_main + wfi + +_syscall_start: + csrw sscratch, x1 + la x1, user_regs + + sw x2, 4(x1) + sw x3, 8(x1) + sw x4, 12(x1) + sw x5, 16(x1) + sw x6, 20(x1) + sw x7, 24(x1) + sw x8, 28(x1) + sw x9, 32(x1) + sw x10, 36(x1) + sw x11, 40(x1) + sw x12, 44(x1) + sw x13, 48(x1) + sw x14, 52(x1) + sw x15, 56(x1) + sw x16, 60(x1) + sw x17, 64(x1) + sw x18, 68(x1) + sw x19, 72(x1) + sw x20, 76(x1) + sw x21, 80(x1) + sw x22, 84(x1) + sw x23, 88(x1) + sw x24, 92(x1) + sw x25, 96(x1) + sw x26, 100(x1) + sw x27, 104(x1) + sw x28, 108(x1) + sw x29, 112(x1) + sw x30, 116(x1) + sw x31, 120(x1) + + csrr t0, sscratch + sw t0, 0(x1) + + jal kernel_trap + +_syscall_exit: + csrw sscratch, x1 + la x1, user_regs + + lw x2, 4(x1) + lw x3, 8(x1) + lw x4, 12(x1) + lw x5, 16(x1) + lw x6, 20(x1) + lw x7, 24(x1) + lw x8, 28(x1) + lw x9, 32(x1) + lw x10, 36(x1) + lw x11, 40(x1) + lw x12, 44(x1) + lw x13, 48(x1) + lw x14, 52(x1) + lw x15, 56(x1) + lw x16, 60(x1) + lw x17, 64(x1) + lw x18, 68(x1) + lw x19, 72(x1) + lw x20, 76(x1) + lw x21, 80(x1) + lw x22, 84(x1) + lw x23, 88(x1) + lw x24, 92(x1) + lw x25, 96(x1) + lw x26, 100(x1) + lw x27, 104(x1) + lw x28, 108(x1) + lw x29, 112(x1) + lw x30, 116(x1) + lw x31, 120(x1) + + csrr x1, sscratch + + sret diff --git a/kernel/start.S b/kernel/start.S deleted file mode 100644 index ecec47b..0000000 --- a/kernel/start.S +++ /dev/null @@ -1,106 +0,0 @@ -.section .text._start -.global _start -.type _start, @function -.type _syscall_start, @function -.type _syscall_exit, @function - -.extern user_regs - -_start: - csrw sstatus, zero - csrw sie, zero - - la t0, _syscall_start - csrw stvec, t0 - - la sp, __stack_top - - la t0, __bss_start - la t1, __bss_end - beq t0, t1, 2f -1: - sw zero, 0(t0) - addi t0, t0, 4 - bltu t0, t1, 1b -2: - call kernel_main - wfi - -_syscall_start: - csrw sscratch, x1 - la x1, user_regs - - sw x2, 4(x1) - sw x3, 8(x1) - sw x4, 12(x1) - sw x5, 16(x1) - sw x6, 20(x1) - sw x7, 24(x1) - sw x8, 28(x1) - sw x9, 32(x1) - sw x10, 36(x1) - sw x11, 40(x1) - sw x12, 44(x1) - sw x13, 48(x1) - sw x14, 52(x1) - sw x15, 56(x1) - sw x16, 60(x1) - sw x17, 64(x1) - sw x18, 68(x1) - sw x19, 72(x1) - sw x20, 76(x1) - sw x21, 80(x1) - sw x22, 84(x1) - sw x23, 88(x1) - sw x24, 92(x1) - sw x25, 96(x1) - sw x26, 100(x1) - sw x27, 104(x1) - sw x28, 108(x1) - sw x29, 112(x1) - sw x30, 116(x1) - sw x31, 120(x1) - - csrr t0, sscratch - sw t0, 0(x1) - - jal kernel_trap - -_syscall_exit: - csrw sscratch, x1 - la x1, user_regs - - lw x2, 4(x1) - lw x3, 8(x1) - lw x4, 12(x1) - lw x5, 16(x1) - lw x6, 20(x1) - lw x7, 24(x1) - lw x8, 28(x1) - lw x9, 32(x1) - lw x10, 36(x1) - lw x11, 40(x1) - lw x12, 44(x1) - lw x13, 48(x1) - lw x14, 52(x1) - lw x15, 56(x1) - lw x16, 60(x1) - lw x17, 64(x1) - lw x18, 68(x1) - lw x19, 72(x1) - lw x20, 76(x1) - lw x21, 80(x1) - lw x22, 84(x1) - lw x23, 88(x1) - lw x24, 92(x1) - lw x25, 96(x1) - lw x26, 100(x1) - lw x27, 104(x1) - lw x28, 108(x1) - lw x29, 112(x1) - lw x30, 116(x1) - lw x31, 120(x1) - - csrr x1, sscratch - - sret diff --git a/linker.ld b/linker.ld index 5bf6586..4fec2bf 100644 --- a/linker.ld +++ b/linker.ld @@ -71,4 +71,5 @@ SECTIONS *(.llvm_*) *(.rela.*) } + _end = .; } -- cgit v1.2.3