diff options
| -rw-r--r-- | arch/riscv64/conf/kmi.its | 4 | ||||
| -rw-r--r-- | arch/riscv64/include/mem.h | 15 | ||||
| -rw-r--r-- | arch/riscv64/include/pmem.h | 4 | ||||
| -rw-r--r-- | arch/riscv64/kernel/arch.c | 7 | ||||
| -rw-r--r-- | arch/riscv64/kernel/proc.c | 8 | ||||
| -rw-r--r-- | arch/riscv64/kernel/vmem.c | 13 | ||||
| -rw-r--r-- | include/arch/mem.h | 21 | ||||
| -rw-r--r-- | include/arch/pmem.h | 3 | ||||
| -rw-r--r-- | include/kmi/assert.h | 14 | ||||
| -rw-r--r-- | include/kmi/mem.h | 8 | ||||
| -rw-r--r-- | include/kmi/utils.h | 245 | ||||
| -rw-r--r-- | src/debug.c | 11 | ||||
| -rw-r--r-- | src/elf.c | 2 | ||||
| -rw-r--r-- | src/main.c | 11 | ||||
| -rw-r--r-- | src/proc.c | 5 | ||||
| -rw-r--r-- | src/regions.c | 2 |
16 files changed, 222 insertions, 151 deletions
diff --git a/arch/riscv64/conf/kmi.its b/arch/riscv64/conf/kmi.its index 768f98b..be49033 100644 --- a/arch/riscv64/conf/kmi.its +++ b/arch/riscv64/conf/kmi.its @@ -12,8 +12,8 @@ arch = "riscv"; os = "kmi"; compression = "none"; - load = <0x80260000>; - entry = <0x80260000>; + load = <0x80200000>; + entry = <0x80200000>; hash-1 { algo = "sha1"; }; diff --git a/arch/riscv64/include/mem.h b/arch/riscv64/include/mem.h new file mode 100644 index 0000000..410a428 --- /dev/null +++ b/arch/riscv64/include/mem.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ +/* Copyright 2021 - 2022, Kim Kuparinen < kimi.h.kuparinen@gmail.com > */ + +#ifndef KMI_RISCV_MEM_H +#define KMI_RISCV_MEM_H +/** + * @file pmem.h + * riscv64-specific general memory stuff. + */ + +/** Base page size is always 4K, use a constant for better optimizations here + * and there */ +#define BASE_PAGE_SIZE 4096 + +#endif /* KMI_RISCV_MEM_H */ diff --git a/arch/riscv64/include/pmem.h b/arch/riscv64/include/pmem.h index 7101757..071c3b5 100644 --- a/arch/riscv64/include/pmem.h +++ b/arch/riscv64/include/pmem.h @@ -5,7 +5,7 @@ #define KMI_RISCV_PMEM_H /** * @file pmem.h - * riscv64-specific physical memory, currently empty but kept around for - * forwards compatibility. + * riscv64-specific physical memory stuff. */ + #endif /* KMI_RISCV_PMEM_H */ diff --git a/arch/riscv64/kernel/arch.c b/arch/riscv64/kernel/arch.c index ec4cf43..4561d8b 100644 --- a/arch/riscv64/kernel/arch.c +++ b/arch/riscv64/kernel/arch.c @@ -8,6 +8,8 @@ #include <kmi/assert.h> #include <kmi/debug.h> +#include <kmi/power.h> +#include <kmi/bkl.h> #include "csr.h" #include "arch.h" @@ -19,8 +21,11 @@ id_t hartid_to_cpuid(id_t hart) if (cpuid_to_hartid(i) == hart) return i; + /* put extra cores to sleep so they don't do anything bad */ error("failed to match hart id %ld to cpu id\n", (long)hart); - /* default to zero, though this should maybe be a panic? */ + bkl_unlock(); + sleep(); + /* should never really be reached but eh */ return 0; } diff --git a/arch/riscv64/kernel/proc.c b/arch/riscv64/kernel/proc.c index af4b2f3..4310378 100644 --- a/arch/riscv64/kernel/proc.c +++ b/arch/riscv64/kernel/proc.c @@ -33,13 +33,13 @@ void run_init(struct tcb *t, vm_t fdt, vm_t initrd) "li a0, %1\n" "li a1, %2\n" "mv a2, %3\n" - "mv a3, %4\n" - "mv a4, %5\n" + "mv a3, %4\n" + "mv a4, %5\n" "sret\n" : : "r" (stack_top), - "K"(0), "K"(SYS_USER_BOOTED), - "r" (t->tid), "r" (fdt), "r" (initrd) + "K" (0), "K" (SYS_USER_BOOTED), + "r" (t->tid), "r" (fdt), "r" (initrd) : "memory"); /* we should never reach this */ unreachable(); diff --git a/arch/riscv64/kernel/vmem.c b/arch/riscv64/kernel/vmem.c index 0aa35cc..cb1eba0 100644 --- a/arch/riscv64/kernel/vmem.c +++ b/arch/riscv64/kernel/vmem.c @@ -420,11 +420,15 @@ __aligned(4096) struct vmem bootvmem; * it. */ __aligned(4096) struct vmem kvmem; +/* adding a third page entry would let us map the kernel at any 4KiB boundary + * but eh, Linux seems fine with 2MiB so I guess I shall be as well. */ + struct vmem *init_mapping() { rpc_pages = order_size(MM_O1) / BASE_PAGE_SIZE; kvmem.leaf[0] = (struct vmem *)to_pte(get_load_addr(), - VM_A | VM_G | VM_D | VM_R | VM_W | VM_X | VM_V); + VM_A | VM_G | VM_D | VM_R | VM_W | + VM_X | VM_V); __populate_dmap(&bootvmem); populate_kvmem(&bootvmem); @@ -462,6 +466,13 @@ void destroy_vmem(struct vmem *b) __destroy_branch(b); } +/** + * Helper for mapping in the kernel virtual page. + * Remember that the kernel lives on its own in a 2MiB (riscv64) region at + * VM_KERNEL, and \ref __va() and \ref __pa() won't directly work on it. + * + * @param b Branch to map kernel into. + */ static void map_kernel(struct vmem *b) { /* slightly worried about this not being guaranteed to be pc relative, diff --git a/include/arch/mem.h b/include/arch/mem.h new file mode 100644 index 0000000..a70637c --- /dev/null +++ b/include/arch/mem.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ +/* Copyright 2024, Kim Kuparinen < kimi.h.kuparinen@gmail.com > */ + +#ifndef KMI_ARCH_MEM_H +#define KMI_ARCH_MEM_H + +/** + * @file mem.h + * + * Arch-specific stuff generic to all memory. + */ + +#if defined(__riscv) +# if __riscv_xlen == 64 +#include "../../arch/riscv64/include/pmem.h" +# else +#include "../../arch/riscv32/include/pmem.h" +# endif +#endif + +#endif /* KMI_ARCH_MEM_H */ diff --git a/include/arch/pmem.h b/include/arch/pmem.h index 6a17449..9abbe8b 100644 --- a/include/arch/pmem.h +++ b/include/arch/pmem.h @@ -10,8 +10,8 @@ * arch/whatever/kernel/pmem.c */ -#include <kmi/mem.h> /* NUM_ORDERS */ #include <kmi/types.h> +#include <kmi/mem.h> /* NUM_ORDERS */ #if defined(__riscv) # if __riscv_xlen == 64 @@ -21,6 +21,7 @@ # endif #endif + /** * Get physical memory parameters. * diff --git a/include/kmi/assert.h b/include/kmi/assert.h index 3120edc..ecf5272 100644 --- a/include/kmi/assert.h +++ b/include/kmi/assert.h @@ -30,13 +30,13 @@ * * @param x Condition to check for. */ -#define assert(x) \ - do { \ - if (unlikely(!(x))) { \ - error("assertion failed: " QUOTE(x) "\n"); \ - while (1) { \ - } \ - } \ +#define assert(x) \ + do { \ + if (unlikely(!(x))) { \ + error("assertion failed: " #x "\n"); \ + while (1) { \ + } \ + } \ } while (0); #else diff --git a/include/kmi/mem.h b/include/kmi/mem.h index bf55c61..e840683 100644 --- a/include/kmi/mem.h +++ b/include/kmi/mem.h @@ -11,6 +11,7 @@ #include <kmi/utils.h> #include <kmi/types.h> +#include <arch/mem.h> /** * Convert physical memory address \c paddr to index of page order \c order. @@ -224,6 +225,7 @@ void set_ram_base(pm_t base); /** @param size Set RAM size. */ void set_ram_size(size_t size); +/** @param addr Set load address. */ void set_load_addr(pm_t addr); /** @@ -243,10 +245,14 @@ pm_t get_ram_base(); */ size_t get_ram_size(); +/** @return Load address. */ pm_t get_load_addr(); -/** Base page size. */ +/** Base page size. Can be overridden by arch if we know it to be some constant + * value. */ +#ifndef BASE_PAGE_SIZE #define BASE_PAGE_SIZE (order_size(BASE_PAGE)) +#endif /** Base page order. */ #define BASE_PAGE (MM_O0) diff --git a/include/kmi/utils.h b/include/kmi/utils.h index 914d456..a37f4bb 100644 --- a/include/kmi/utils.h +++ b/include/kmi/utils.h @@ -4,6 +4,8 @@ #ifndef KMI_UTILS_H #define KMI_UTILS_H +#include <kmi/bits.h> + /** * @file utils.h * Misc utils and helpers. @@ -238,269 +240,271 @@ /* clang-format doesn't like _Generic, but I guess that's fine. * Uncrustify just ignores it, as far as I can tell. */ + /** - * Align value upwards. + * Align value downwards. * Type is deduced from \c x. * - * @param x Value to align up. + * @param x Value to align. * @param y Value to align to. - * @return \c x aligned to \c y. + * @return \c x aligned to down \c y. */ -#define align_up(x, y) \ - _Generic((x), signed char \ - : align_up_c, signed short \ - : align_up_s, signed int \ - : align_up_i, signed long \ - : align_up_l, signed long long \ - : align_up_ll, \ - \ - unsigned char \ - : align_up_uc, unsigned short \ - : align_up_us, unsigned int \ - : align_up_ui, unsigned long \ - : align_up_ul, unsigned long long \ - : align_up_ull)((x), (y)) +#define align_down(x, y) \ + _Generic((x), signed char \ + : align_down_c, signed short \ + : align_down_s, signed int \ + : align_down_i, signed long \ + : align_down_l, signed long long \ + : align_down_ll, \ + \ + unsigned char \ + : align_down_uc, unsigned short \ + : align_down_us, unsigned int \ + : align_down_ui, unsigned long \ + : align_down_ul, unsigned long long \ + : align_down_ull)((x), (y)) /** * Helper macro for defining type specific aligning. + * Treats 0 as if it was 1, i.e. returns the value as it is. * * @param name Name of type in function name. * @param type Actual type. */ -#define DEFINE_ALIGN_UP(name, type) \ - static inline type align_up_##name(type val, type a) \ - { \ - if (!a) { \ - return val; \ - } \ - \ - type rem = val % a; \ - \ - if (rem == 0) { \ - return val; \ - } \ - \ - return val + a - rem; \ +#define DEFINE_ALIGN_DOWN(name, type) \ + static inline type align_down_##name(type val, type a) \ + { \ + if (a == 0) { \ + return val; \ + } \ + \ + /* a branch is likely a bit faster than a rem */ \ + if (likely(is_powerof2(a))) { \ + return val & ~(a - 1); \ + } \ + \ + return val - (val % a); \ } /** - * Align signed char up. + * Align signed char down. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned up to nearest multiple of \c a. + * @return \c val aligned down to nearest multiple of \c a. */ -DEFINE_ALIGN_UP(c, signed char); +DEFINE_ALIGN_DOWN(c, signed char); /** - * Align signed short up. + * Align signed short down. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned up to nearest multiple of \c a. + * @return \c val aligned down to nearest multiple of \c a. */ -DEFINE_ALIGN_UP(s, signed short); +DEFINE_ALIGN_DOWN(s, signed short); /** - * Align signed int up. + * Align signed int down. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned up to nearest multiple of \c a. + * @return \c val aligned down to nearest multiple of \c a. */ -DEFINE_ALIGN_UP(i, signed int); +DEFINE_ALIGN_DOWN(i, signed int); /** - * Align signed long up. + * Align signed long down. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned up to nearest multiple of \c a. + * @return \c val aligned down to nearest multiple of \c a. */ -DEFINE_ALIGN_UP(l, signed long); +DEFINE_ALIGN_DOWN(l, signed long); /** - * Align signed long long up. + * Align signed long long down. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned up to nearest multiple of \c a. + * @return \c val aligned down to nearest multiple of \c a. */ -DEFINE_ALIGN_UP(ll, signed long long); +DEFINE_ALIGN_DOWN(ll, signed long long); /** - * Align unsigned char up. + * Align unsigned char down. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned up to nearest multiple of \c a. + * @return \c val aligned down to nearest multiple of \c a. */ -DEFINE_ALIGN_UP(uc, unsigned char); +DEFINE_ALIGN_DOWN(uc, unsigned char); /** - * Align unsigned short up. + * Align unsigned short down. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned up to nearest multiple of \c a. + * @return \c val aligned down to nearest multiple of \c a. */ -DEFINE_ALIGN_UP(us, unsigned short); +DEFINE_ALIGN_DOWN(us, unsigned short); /** - * Align unsigned int up. + * Align unsigned int down. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned up to nearest multiple of \c a. + * @return \c val aligned down to nearest multiple of \c a. */ -DEFINE_ALIGN_UP(ui, unsigned int); +DEFINE_ALIGN_DOWN(ui, unsigned int); /** - * Align unsigned long up. + * Align unsigned long down. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned up to nearest multiple of \c a. + * @return \c val aligned down to nearest multiple of \c a. */ -DEFINE_ALIGN_UP(ul, unsigned long); +DEFINE_ALIGN_DOWN(ul, unsigned long); /** - * Align unsigned long long up. + * Align unsigned long long down. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned up to nearest multiple of \c a. + * @return \c val aligned down to nearest multiple of \c a. */ -DEFINE_ALIGN_UP(ull, unsigned long long); - +DEFINE_ALIGN_DOWN(ull, unsigned long long); /** - * Align value downwards. + * Align value upwards. * Type is deduced from \c x. * - * @param x Value to align. + * @param x Value to align up. * @param y Value to align to. - * @return \c x aligned to down \c y. + * @return \c x aligned to \c y. */ -#define align_down(x, y) \ - _Generic((x), signed char \ - : align_down_c, signed short \ - : align_down_s, signed int \ - : align_down_i, signed long \ - : align_down_l, signed long long \ - : align_down_ll, \ - \ - unsigned char \ - : align_down_uc, unsigned short \ - : align_down_us, unsigned int \ - : align_down_ui, unsigned long \ - : align_down_ul, unsigned long long \ - : align_down_ull)((x), (y)) +#define align_up(x, y) \ + _Generic((x), signed char \ + : align_up_c, signed short \ + : align_up_s, signed int \ + : align_up_i, signed long \ + : align_up_l, signed long long \ + : align_up_ll, \ + \ + unsigned char \ + : align_up_uc, unsigned short \ + : align_up_us, unsigned int \ + : align_up_ui, unsigned long \ + : align_up_ul, unsigned long long \ + : align_up_ull)((x), (y)) /** * Helper macro for defining type specific aligning. + * Treats 0 as 1, i.e. lowest possible alignment. * * @param name Name of type in function name. * @param type Actual type. */ -#define DEFINE_ALIGN_DOWN(name, type) \ - static inline type align_down_##name(type val, type a) \ - { \ - if (!a) { \ - return val; \ - } \ - \ - return val - (val % a); \ +#define DEFINE_ALIGN_UP(name, type) \ + static inline type align_up_##name(type val, type a) \ + { \ + type new = align_down(val, a); \ + if (new == val) { \ + return val; \ + } \ + \ + return new + a; \ } /** - * Align signed char down. + * Align signed char up. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned down to nearest multiple of \c a. + * @return \c val aligned up to nearest multiple of \c a. */ -DEFINE_ALIGN_DOWN(c, signed char); +DEFINE_ALIGN_UP(c, signed char); /** - * Align signed short down. + * Align signed short up. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned down to nearest multiple of \c a. + * @return \c val aligned up to nearest multiple of \c a. */ -DEFINE_ALIGN_DOWN(s, signed short); +DEFINE_ALIGN_UP(s, signed short); /** - * Align signed int down. + * Align signed int up. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned down to nearest multiple of \c a. + * @return \c val aligned up to nearest multiple of \c a. */ -DEFINE_ALIGN_DOWN(i, signed int); +DEFINE_ALIGN_UP(i, signed int); /** - * Align signed long down. + * Align signed long up. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned down to nearest multiple of \c a. + * @return \c val aligned up to nearest multiple of \c a. */ -DEFINE_ALIGN_DOWN(l, signed long); +DEFINE_ALIGN_UP(l, signed long); /** - * Align signed long long down. + * Align signed long long up. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned down to nearest multiple of \c a. + * @return \c val aligned up to nearest multiple of \c a. */ -DEFINE_ALIGN_DOWN(ll, signed long long); +DEFINE_ALIGN_UP(ll, signed long long); /** - * Align unsigned char down. + * Align unsigned char up. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned down to nearest multiple of \c a. + * @return \c val aligned up to nearest multiple of \c a. */ -DEFINE_ALIGN_DOWN(uc, unsigned char); +DEFINE_ALIGN_UP(uc, unsigned char); /** - * Align unsigned short down. + * Align unsigned short up. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned down to nearest multiple of \c a. + * @return \c val aligned up to nearest multiple of \c a. */ -DEFINE_ALIGN_DOWN(us, unsigned short); +DEFINE_ALIGN_UP(us, unsigned short); /** - * Align unsigned int down. + * Align unsigned int up. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned down to nearest multiple of \c a. + * @return \c val aligned up to nearest multiple of \c a. */ -DEFINE_ALIGN_DOWN(ui, unsigned int); +DEFINE_ALIGN_UP(ui, unsigned int); /** - * Align unsigned long down. + * Align unsigned long up. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned down to nearest multiple of \c a. + * @return \c val aligned up to nearest multiple of \c a. */ -DEFINE_ALIGN_DOWN(ul, unsigned long); +DEFINE_ALIGN_UP(ul, unsigned long); /** - * Align unsigned long long down. + * Align unsigned long long up. * * @param val Value to align. * @param a Value to align to. - * @return \c val aligned down to nearest multiple of \c a. + * @return \c val aligned up to nearest multiple of \c a. */ -DEFINE_ALIGN_DOWN(ull, unsigned long long); +DEFINE_ALIGN_UP(ull, unsigned long long); /** * Check if value is aligned. @@ -533,11 +537,8 @@ DEFINE_ALIGN_DOWN(ull, unsigned long long); #define DEFINE_ALIGNED(name, type) \ static inline bool is_aligned_##name(type val, type a) \ { \ - if (!a) { \ - return true; \ - } \ - \ - return val % a == 0; \ + type new = align_down(val, a); \ + return new == val; \ } /** diff --git a/src/debug.c b/src/debug.c index 15640b4..9573bbc 100644 --- a/src/debug.c +++ b/src/debug.c @@ -364,6 +364,12 @@ static size_t __integral_val(ssize_t value, size_t base, size_t flags, return ret + 1; } +/** + * Print out a string. + * + * @param s String to print out. + * @return Bytes printed. + */ static size_t __puts(const char *s) { size_t i = 0; @@ -747,10 +753,7 @@ void dbg(const char *fmt, ...) if (is_set(flags, PRECS_FLAG)) i = precision; - for (; *s && i--;) { - __putchar(*s++); - chars_written++; - } + chars_written += __puts(s); fmt++; break; @@ -69,7 +69,7 @@ static void __map_exec(struct tcb *t, vm_t bin, uint8_t ei_c, vm_t phstart, if (!start) return; /* out of memory or something */ - info("mapped ELF section to %x\n", start); + info("mapped ELF section to %lx\n", (long)start); uint8_t elf_flags = program_header_prop(ei_c, runner, p_flags); uint8_t uvflags = __elf_to_uvflags(elf_flags); @@ -61,8 +61,6 @@ __noreturn void kernel(void *fdt, uintptr_t load_addr, struct vmem *d) /* we should be in kernelspace, so use the virtual address of our FDT. */ fdt = __va(fdt); - /* dbg uses direct mapping at this point */ - init_dbg(fdt); /* start up debugging in kernel IO */ setup_io_dbg(d); @@ -105,6 +103,10 @@ __noreturn void main(unsigned long hart, void *fdt, uintptr_t load_addr) * have to get the function signature right */ (void)hart; + /* dbg uses direct mapping at this point, useful for early init asserts + * and so on */ + init_dbg(fdt); + /** @todo some kind of lottery? */ pm_t ram_base = __fdt_ram_base(fdt); pm_t ram_size = __fdt_ram_size(fdt); @@ -114,6 +116,11 @@ __noreturn void main(unsigned long hart, void *fdt, uintptr_t load_addr) init_mem(fdt); + /* we don't have any debug output just yet but still */ + /* also this is I guess more of an architecture limitation, should the + * whole of main() just be moved to arch? */ + assert(is_aligned(load_addr, order_size(MM_O1))); + struct vmem *d = init_mapping(); to_kernelspace(fdt, load_addr, d, ram_base); unreachable(); @@ -18,8 +18,6 @@ #include <libfdt.h> -static vm_t entry; - stat_t prepare_proc(struct tcb *t, vm_t bin, vm_t interp) { vm_t entry = load_elf(t, bin, interp); @@ -60,7 +58,8 @@ stat_t init_proc(void *fdt, vm_t *proc_fdt, vm_t *proc_initrd) /* allocate stacks etc after ELF file to make sure nothing of importance * clashes */ - prepare_proc(t, get_init_base(fdt), 0); + stat_t ret = prepare_proc(t, get_init_base(fdt), 0); + assert(ret == OK); /** In the init process, can the entry be the callback? Is that too * unergonomic? */ diff --git a/src/regions.c b/src/regions.c index 60cab23..b57b548 100644 --- a/src/regions.c +++ b/src/regions.c @@ -448,6 +448,8 @@ vm_t alloc_fixed_region(struct mem_region_root *r, vm_t start, size_t size, m = m->next; else m = m->prev; + + assert(m); } /* if region is already in use, forget it */ |
