aboutsummaryrefslogtreecommitdiff
path: root/arch/riscv
diff options
context:
space:
mode:
Diffstat (limited to 'arch/riscv')
-rw-r--r--arch/riscv/conf/init-link.S4
-rw-r--r--arch/riscv/config.h23
-rw-r--r--arch/riscv/include/vmem.h6
-rw-r--r--arch/riscv/init/init.c33
-rw-r--r--arch/riscv/init/start.S5
-rw-r--r--arch/riscv/kernel/irq.c6
-rw-r--r--arch/riscv/kernel/main.c295
-rw-r--r--arch/riscv/kernel/pmem.c15
-rw-r--r--arch/riscv/kernel/proc.c4
-rw-r--r--arch/riscv/kernel/vmem.c92
-rw-r--r--arch/riscv/source.mk2
11 files changed, 148 insertions, 337 deletions
diff --git a/arch/riscv/conf/init-link.S b/arch/riscv/conf/init-link.S
index 53d24ab..ce53e5e 100644
--- a/arch/riscv/conf/init-link.S
+++ b/arch/riscv/conf/init-link.S
@@ -1,8 +1,8 @@
OUTPUT_ARCH(riscv)
-ENTRY(init)
+ENTRY(_start)
SECTIONS {
- . = ABSOLUTE(PM_KERN);
+ . = ABSOLUTE(PM_KERN_BASE);
__init_start = .;
/* objcopy only copies these three sections (as far as I'm aware) into the
diff --git a/arch/riscv/config.h b/arch/riscv/config.h
index fa6785e..0ef22fa 100644
--- a/arch/riscv/config.h
+++ b/arch/riscv/config.h
@@ -3,15 +3,32 @@
/* --- START ARCH USER CONFIG VALUES --- */
/* physical address to which the kernel will be loaded */
#define RAM_BASE 0x80000000
-#define PM_KERN (RAM_BASE + SZ_512K)
+#define PM_KERN_BASE (RAM_BASE + SZ_512K)
+#define PM_KERN_SIZE (SZ_256K)
+#define PM_KERN_TOP (PM_KERN_BASE + PM_KERN_SIZE)
/* --- END ARCH USER CONFIG VALUES --- */
/* don't touch >:( */
-#define PM_STACK_BASE (PM_KERN)
-#define PM_STACK_TOP (RAM_BASE + SZ_1M)
+#define PM_STACK_BASE (PM_KERN_BASE + SZ_256K)
+#define PM_STACK_SIZE (SZ_256K)
+#define PM_STACK_TOP (PM_STACK_BASE + PM_STACK_SIZE)
+
#define VM_DMAP (0xffffffc000000000) /* testing for now */
#define VM_KERN (VM_DMAP + SZ_256K)
#define TMP_PTE (-SZ_2G)
#define ROOT_PTE (0)
#define ROOT_REGION (SZ_4K)
+
+#define IO_PAGE 511
+#define KSTART_PAGE 256
+#define CSTACK_PAGE 255
+
+/* assume Sv39, probably wouldn't be too difficult to use runtime parameters
+ * instead. First 4K is reserved for NULL, but I suppose it could be mapped
+ * later if *absolutely* necessary. */
+#define UVMEM_START (SZ_4K)
+#define UVMEM_END (SZ_256G - SZ_1G)
+
+#define PROC_STACK_TOP (SZ_256G)
+#define PROC_STACK_BASE (SZ_256G - SZ_1G)
diff --git a/arch/riscv/include/vmem.h b/arch/riscv/include/vmem.h
index d17aaf1..228b24b 100644
--- a/arch/riscv/include/vmem.h
+++ b/arch/riscv/include/vmem.h
@@ -13,15 +13,15 @@
#define VM_A (1 << 6)
#define VM_D (1 << 7)
-enum mm_mode_t {
+enum mm_mode {
Sv48,
Sv39,
Sv32,
};
typedef size_t vm_t;
-struct __packed vm_branch_t {
- struct vm_branch_t *leaf[512];
+struct __packed vm_branch {
+ struct vm_branch *leaf[512];
};
#endif /* APOS_RISCV_VMAP_H */
diff --git a/arch/riscv/init/init.c b/arch/riscv/init/init.c
index dc9d7e9..23dcadd 100644
--- a/arch/riscv/init/init.c
+++ b/arch/riscv/init/init.c
@@ -1,6 +1,7 @@
#include <apos/types.h>
#include <apos/attrs.h>
#include <apos/utils.h>
+#include <apos/vmem.h>
#include <vmem.h>
#include <csr.h>
@@ -14,29 +15,24 @@ struct init_vmem *root_branch;
#define to_pte(a, f) (((a) >> 12) << 10 | (f))
/* assume Sv39 for now */
-void init_vmem()
+void init_bootmem()
{
size_t flags = VM_V | VM_X | VM_R | VM_W;
extern char *__init_start;
root_branch = (struct init_vmem *)align_down((size_t)&__init_start - SZ_4K, SZ_4K);
- for(size_t i = 0; i < 512; ++i)
- root_branch->leaf[i] = 0;
/* direct mapping (temp) */
- for(size_t i = 0; i < 256; ++i)
+ for(size_t i = 0; i < CSTACK_PAGE; ++i)
root_branch->leaf[i] = (int*)to_pte(SZ_1G * i, flags);
/* kernel (also sort of direct mapping) */
- /* FIXME set up actually correct addressing retard */
flags |= VM_G;
- for(size_t i = 256; i < 511; ++i)
+ for(size_t i = KSTART_PAGE; i < IO_PAGE; ++i)
root_branch->leaf[i] = (int *)to_pte(RAM_BASE + SZ_1G * (i - 256), flags);
- /* kernel IO, map to 0 for now, should be made more robust in the future */
- /* same goes for the equivalent piece of code over in kernel/main.c,
- * which btw should really get refactored, it's a mess */
- root_branch->leaf[511] = (int *)to_pte(0, flags);
+ /* kernel IO, map to 0 for now, will be updated in the future */
+ root_branch->leaf[IO_PAGE] = (int *)to_pte(0, flags);
csr_write(CSR_SATP, SATP_MODE_Sv39 | ((size_t)root_branch >> 12));
}
@@ -53,11 +49,22 @@ void move_kernel()
dst[i] = src[i];
}
-void __init init(void *fdt)
+#define __va_reg(reg)\
+{\
+ vm_t reg = 0;\
+ __asm__("mv %0, " QUOTE(reg) : "=r" (reg) :: );\
+ reg = (vm_t)__va(reg);\
+ __asm__("mv " QUOTE(reg) ", %0" :: "rK" (reg) : );\
+}
+
+void init(void *fdt)
{
extern char *__init_end;
void (*kernel_main)(void *fdt) = (void (*)(void *))(VM_KERN);
- init_vmem();
+ init_bootmem();
move_kernel();
- kernel_main(fdt);
+ __va_reg(sp);
+ __va_reg(fp);
+ __va_reg(gp);
+ kernel_main(__va(fdt));
}
diff --git a/arch/riscv/init/start.S b/arch/riscv/init/start.S
new file mode 100644
index 0000000..f9bfb92
--- /dev/null
+++ b/arch/riscv/init/start.S
@@ -0,0 +1,5 @@
+.section .init.start
+.global _start
+_start:
+li sp, PM_STACK_TOP
+jal init
diff --git a/arch/riscv/kernel/irq.c b/arch/riscv/kernel/irq.c
index 113773a..ed97c00 100644
--- a/arch/riscv/kernel/irq.c
+++ b/arch/riscv/kernel/irq.c
@@ -1,9 +1,13 @@
#include <apos/irq.h>
#include <csr.h>
-void irq_handler()
+void init_irq(void *fdt)
{
+ UNUSED(fdt);
+}
+void handle_irq()
+{
}
/* very simple for now */
diff --git a/arch/riscv/kernel/main.c b/arch/riscv/kernel/main.c
index 83270eb..9ce256e 100644
--- a/arch/riscv/kernel/main.c
+++ b/arch/riscv/kernel/main.c
@@ -1,298 +1,11 @@
-#include <apos/debug.h>
-#include <apos/vmem.h>
-#include <apos/pmem.h>
#include <apos/utils.h>
-#include <apos/types.h>
-#include <apos/mem.h>
-#include <apos/lock.h>
-#include <apos/tcb.h>
-#include <apos/string.h>
-#include <apos/proc.h>
-#include <apos/mem_nodes.h>
-#include <apos/elf.h>
-#include <apos/initrd.h>
-#include <libfdt.h>
#include <csr.h>
-struct pm_layout_t {
- pm_t base;
- pm_t top;
-};
-
-struct pm_orders_t {
- size_t max_order;
- size_t bits[10];
- size_t page_shift;
-};
-
-#ifdef DEBUG
-
-static void init_dbg(void *fdt)
-{
- struct dbg_info_t dbg = dbg_from_fdt(fdt);
- dbg_init(dbg.dbg_ptr + (-SZ_1G), dbg.dev);
-}
-
-#else
-#define init_dbg(...)
-#endif
-
-static struct pm_layout_t get_memlayout(void *fdt)
-{
- struct cell_info_t ci = get_reginfo(fdt, "/memory");
- int mem_offset = fdt_path_offset(fdt, "/memory");
- uint8_t *mem_reg =
- (uint8_t *) fdt_getprop(fdt, mem_offset, "reg", NULL);
-
- /* if riscv128 comes around we will probably see addr_cells == 4, but
- * I'm not too concerned about it at the moment */
- pm_t base = (pm_t)fdt_load_int_ptr(ci.addr_cells, mem_reg);
-
- if(ci.addr_cells == 2)
- mem_reg += sizeof(fdt64_t);
- else
- mem_reg += sizeof(fdt32_t);
-
- /* -1 because base is a legitimate memory address */
- pm_t top = (pm_t)fdt_load_int_ptr(ci.size_cells, mem_reg) + base - 1;
- return (struct pm_layout_t){(size_t)__va(base), (size_t)__va(top)};
-}
-
-static pm_t get_kerneltop()
-{
- /* interesting, for some reason if I define these to be just char
- * pointers I get some wacky values. Not sure why that would be, but
- * this works. */
- extern char __kernel_end;
- return (pm_t)&__kernel_end;
-}
-
-static pm_t get_fdttop(void *fdt)
-{
- const char *b = (const char *)fdt;
- return (pm_t)(b + fdt_totalsize(fdt));
-}
-
-static pm_t get_fdtbase(void *fdt)
-{
- /* lol */
- return (pm_t)fdt;
-}
-
-
-static void mark_area_used(pm_t base, pm_t top)
-{
- size_t area_left = top - base;
- /* TODO: add in a method to make sure that we use as large mappings as
- * possible. */
- while(area_left >= MM_KPAGE_SIZE){
- mark_used(base, MM_KPAGE);
- area_left -= MM_KPAGE_SIZE;
- base += MM_KPAGE_SIZE;
- }
-
- if(area_left != 0)
- mark_used(base, MM_KPAGE_SIZE);
-}
-
-static void mark_reserved_mem(void *fdt)
-{
- int rmem_offset = fdt_path_offset(fdt, "/reserved-memory/mmode_resv0");
- struct cell_info_t ci = get_reginfo(fdt, "/reserved-memory/mmode_resv0");
- uint8_t *rmem_reg = (uint8_t *)fdt_getprop(fdt, rmem_offset, "reg", NULL);
-
- pm_t base = (pm_t)fdt_load_int_ptr(ci.addr_cells, rmem_reg);
-
- if(ci.addr_cells == 2)
- rmem_reg += sizeof(fdt64_t);
- else
- rmem_reg += sizeof(fdt32_t);
-
- pm_t top = (pm_t)fdt_load_int_ptr(ci.size_cells, rmem_reg) + base - 1;
- mark_area_used(base, top);
-}
-
-static struct pm_layout_t setup_pmem(void *fdt)
-{
- struct pm_layout_t pmem = get_memlayout(fdt);
-
- pm_t initrd_top = get_initrdtop(fdt);
- pm_t kernel_top = get_kerneltop();
- pm_t fdt_top = get_fdttop(fdt);
-
- pm_t top = MAX3(kernel_top, initrd_top, fdt_top);
- dbg("initrd_top:\t%#lx\n", initrd_top);
- dbg("kernel_top:\t%#lx\n", kernel_top);
- dbg("fdt_top:\t%#lx\n", fdt_top);
-
- /* TODO: check that pmap placement doesn't overwrite anything, such as
- * stack or go over top address of memory */
- size_t probe_size = probe_pmap(pmem.base, pmem.top - pmem.base);
- /* riscv handles two byte boundaries better than one byte, so align
- * upwards */
- pm_t pmap_base = align_up(top + 1, 2);
- size_t actual_size = populate_pmap(pmem.base, pmem.top - pmem.base,
- pmap_base);
-
- /* TODO: not entirely sure what to do about this, probably give up trying to
- * boot? */
- if(probe_size != actual_size){
- dbg("BUG! probe_size (%#lx) != actual_size (%#lx)\n",
- probe_size, actual_size);
- }
-
- /* mark init stack, at the moment always mapped to 2M */
- mark_used(PM_STACK_BASE, MM_MPAGE);
-
- /* mark kernel, at the moment with a 2M partition */
- mark_used(PM_KERN, MM_MPAGE);
-
- /* mark fdt and initrd */
- mark_area_used(get_initrdbase(fdt), initrd_top);
- mark_area_used(get_fdtbase(fdt), fdt_top);
-
- /* mark pmap */
- mark_area_used(pmap_base, pmap_base + actual_size);
-
- /* mark reserved mem */
- mark_reserved_mem(fdt);
-
- return (struct pm_layout_t){.base = pmap_base, .top = actual_size + pmap_base};
-}
-
-static struct pm_orders_t init_pmem(void *fdt)
-{
- /* bad idea for now */
- /* enum mm_mode_t mmode = get_mmode(fdt); */
-
- size_t max_order = 0;
- size_t order_bits = 9;
- enum mm_mode_t mmode = Sv39;
- switch(mmode){
- case Sv32:
- max_order = 1;
- order_bits = 10;
- break;
-
- case Sv39:
- max_order = 2;
- break;
-
- case Sv48:
- max_order = 3;
- break;
- };
-
- size_t bits[10] = {0};
- for(size_t i = 0; i <= max_order; ++i)
- bits[i] = order_bits;
-
- init_mem(max_order, bits, 12);
-
- struct pm_orders_t ret = {max_order, {0}, 12};
- for(size_t i = 0; i <= __mm_max_order; ++i)
- ret.bits[i] = bits[i];
-
- return ret;
-}
-
-/* assume Sv39 for now */
-#define to_pte(a, f) (((a) >> 12) << 10 | (f))
-static void populate_root_branch(struct vm_branch_t *b)
-{
- size_t flags = VM_V | VM_R | VM_W | VM_X | VM_G;
- for(size_t i = 256; i < 511; ++i)
- b->leaf[i] = (struct vm_branch_t *)to_pte(RAM_BASE + SZ_1G * (i - 256), flags);
-
- /* very lazy, should be ashamed, yes */
- b->leaf[511] = (struct vm_branch_t *)to_pte(0, flags);
-}
-
-static void start_vmem(struct vm_branch_t *branch, enum mm_mode_t m)
-{
- /* TODO: get ASID from CPU id */
-
- branch = (struct vm_branch_t *)__pa(branch);
-
- if(m == Sv32)
- csr_write(CSR_SATP, SATP_MODE_Sv32 | pm_to_pnum((pm_t)(branch)));
- else if (m == Sv39)
- csr_write(CSR_SATP, SATP_MODE_Sv39 | pm_to_pnum((pm_t)(branch)));
- else
- csr_write(CSR_SATP, SATP_MODE_Sv48 | pm_to_pnum((pm_t)(branch)));
-
- __asm__ ("sfence.vma" : : : "memory");
- /* Sv57 && Sv64 in the future? */
-}
-
-static struct vm_branch_t *init_vmem(void *fdt)
-{
- init_pmem(fdt);
- setup_pmem(fdt);
-
- struct vm_branch_t *b = (struct vm_branch_t *)alloc_page(MM_KPAGE, 0);
- memset(b, 0, sizeof(struct vm_branch_t));
-
- populate_root_branch(b);
- /* update which memory branch to use */
- start_vmem(b, Sv39);
- return b;
-}
-
-static void init_irq(void *fdt)
-{
-}
-
-static void init_proc(void *fdt, struct vm_branch_t *b)
+void arch_setup(void *fdt)
{
- struct tcb *t = (struct tcb *)alloc_page(MM_KPAGE, 0);
- t->b_r = b;
- t->pid = 0;
- t->tid = 0;
- threads_insert(t);
- /* first page reserved to avoid issues with null pointers being legal */
- /* last gig page (assuming Sv39) reserved for call stack */
- sp_mem_init(&t->sp_r, SZ_4K, SZ_256G - SZ_1G);
-
- /* stack (should probably be set up to allow for configuration
- * parameters to be passed) */
- t->proc_stack = alloc_uvmem(t, SZ_2M, VM_V | VM_R | VM_W | VM_U);
- /* if need be, the call stack can later be expanded, but this init
- * program uses it like this. */
- t->call_stack = setup_call_stack(t, SZ_256G - SZ_1G + SZ_2M, SZ_2M);
-
- /* should probably wrap this in like tlb_flush_all() or something */
- __asm__ ("sfence.vma" : : : "memory");
-
- jump_to_userspace(t, get_init_base(fdt), 0, 0);
-}
-
-#define __va_reg(reg)\
-{\
- vm_t reg = 0;\
- __asm__("mv %0, " QUOTE(reg) : "=r" (reg) :: );\
- reg = (vm_t)__va(reg);\
- __asm__("mv " QUOTE(reg) ", %0" :: "rK" (reg) : );\
-}
-
-void __main arch_main(void *fdt)
-{
- __va_reg(sp);
- __va_reg(fp);
- __va_reg(gp);
-
- /* correct fdt pointer */
- fdt = (void *)__va(fdt);
- init_dbg(fdt);
- dbg_fdt(fdt);
-
- /* allow supervisor code to touch user data */
+ UNUSED(fdt);
+ /* allow supervisor code to touch user pages */
csr_set(CSR_SSTATUS, SSTATUS_SUM);
- /* maybe this is too early but mark that we want to jump to user mode
- * eventually*/
+ /* mark that we want to eventually jump to userspace */
csr_clear(CSR_SSTATUS, SSTATUS_SPP);
- struct vm_branch_t *b = init_vmem(fdt);
- init_mem_blocks();
- init_irq(fdt);
- init_proc(fdt, b);
}
diff --git a/arch/riscv/kernel/pmem.c b/arch/riscv/kernel/pmem.c
new file mode 100644
index 0000000..45b8eeb
--- /dev/null
+++ b/arch/riscv/kernel/pmem.c
@@ -0,0 +1,15 @@
+#include <apos/pmem.h>
+
+int arch_pmem_conf(void *fdt, size_t *max_order, size_t *base_bits, size_t bits[ORDERS_NUM])
+{
+ UNUSED(fdt);
+
+ /* Sv39 for now */
+ *max_order = 2;
+ *base_bits = 12;
+ /* we can assume bits[] is zeroed out beforehand */
+ for(size_t i = 0; i <= *max_order; ++i)
+ bits[i] = 9;
+
+ return 0;
+}
diff --git a/arch/riscv/kernel/proc.c b/arch/riscv/kernel/proc.c
index 57ddc5b..6120c2d 100644
--- a/arch/riscv/kernel/proc.c
+++ b/arch/riscv/kernel/proc.c
@@ -2,9 +2,9 @@
#include <apos/elf.h>
#include <csr.h>
-void jump_to_userspace(struct tcb *t, vm_t bin, int argc, char **argv)
+void jump_to_userspace(struct tcb *t, int argc, char **argv)
{
- csr_write(CSR_SEPC, prepare_proc(t, bin));
+ csr_write(CSR_SEPC, t->entry);
__asm__("mv sp, %0\n" : "=r" (t->proc_stack) :: "memory");
__asm__("sret\n" ::: "memory");
}
diff --git a/arch/riscv/kernel/vmem.c b/arch/riscv/kernel/vmem.c
index 45f6336..c862cea 100644
--- a/arch/riscv/kernel/vmem.c
+++ b/arch/riscv/kernel/vmem.c
@@ -3,12 +3,14 @@
#include <apos/vmem.h>
#include <apos/mem.h>
#include <apos/debug.h>
+#include <apos/cpu.h>
#include <pages.h>
#include <vmem.h>
+#include <csr.h>
#define pte_ppn(pte) (((pm_t)(pte)) >> 10)
#define pte_flags(pte) (((pm_t)(pte)) & 0xff)
-#define to_pte(p, f) ((pm_to_pnum((pm_t)__pa(p)) << 10) | (f))
+#define to_pte(p, f) ((pm_to_pnum(p) << 10) | (f))
#define pte_addr(pte) __va(pnum_to_paddr(pte_ppn(pte)))
#define pte_paddr(pte) (pnum_to_paddr(pte_ppn(pte)))
#define vm_to_index(a, o) (pm_to_index(a, o))
@@ -16,9 +18,9 @@
#define is_leaf(pte) (is_active(pte) && (pte_flags(pte) & ~VM_V))
#define is_branch(pte) (is_active(pte) && !(pte_flags(pte) & ~VM_V))
-static pm_t *__find_vmem(struct vm_branch_t *b, vm_t v, enum mm_order_t *o)
+static pm_t *__find_vmem(struct vm_branch *b, vm_t v, enum mm_order *o)
{
- enum mm_order_t top = __mm_max_order;
+ enum mm_order top = __mm_max_order;
if(o)
*o = MM_O0;
do {
@@ -35,17 +37,17 @@ static pm_t *__find_vmem(struct vm_branch_t *b, vm_t v, enum mm_order_t *o)
return (pm_t *)&b->leaf[idx];
}
- b = (struct vm_branch_t *)pte_addr(pte);
+ b = (struct vm_branch *)pte_addr(pte);
} while (top--);
return 0;
}
-int mod_vmem(struct vm_branch_t *branch, vm_t vaddr, pm_t paddr, uint8_t flags)
+int mod_vmem(struct vm_branch *branch, vm_t vaddr, pm_t paddr, uint8_t flags)
{
pm_t *pte = __find_vmem(branch, vaddr, 0);
if(pte){
- *pte = to_pte(paddr, flags);
+ *pte = to_pte((pm_t)__pa(paddr), flags);
return 0;
}
@@ -54,8 +56,8 @@ int mod_vmem(struct vm_branch_t *branch, vm_t vaddr, pm_t paddr, uint8_t flags)
/* huh, should probably add status flags etc. to all my API functions. Damn, I'm
* lazy. */
-int stat_vmem(struct vm_branch_t *branch, vm_t vaddr, pm_t *paddr,
- enum mm_order_t *order, uint8_t *flags)
+int stat_vmem(struct vm_branch *branch, vm_t vaddr, pm_t *paddr,
+ enum mm_order *order, uint8_t *flags)
{
pm_t *pte = __find_vmem(branch, vaddr, order);
if(pte){
@@ -71,30 +73,30 @@ int stat_vmem(struct vm_branch_t *branch, vm_t vaddr, pm_t *paddr,
return -1;
}
-static struct vm_branch_t *__create_leaf()
+static struct vm_branch *__create_leaf()
{
pm_t new_leaf = alloc_page(MM_KPAGE, 0);
- memset((void *)new_leaf, 0, sizeof(struct vm_branch_t));
- return (struct vm_branch_t *)to_pte(new_leaf, VM_V);
+ memset((void *)new_leaf, 0, sizeof(struct vm_branch));
+ return (struct vm_branch *)to_pte((pm_t)__pa(new_leaf), VM_V);
}
-static void __destroy_branch(struct vm_branch_t *b)
+static void __destroy_branch(struct vm_branch *b)
{
if(!b)
return;
for(size_t i = 0; i < BASE_PAGE_SIZE / sizeof(pm_t); ++i){
if(is_branch(b->leaf[i]))
- __destroy_branch((struct vm_branch_t *)pte_addr(b->leaf[i]));
+ __destroy_branch((struct vm_branch *)pte_addr(b->leaf[i]));
free_page(MM_KPAGE, (pm_t)pte_addr(b->leaf[i]));
}
}
-void map_vmem(struct vm_branch_t *branch,
- pm_t paddr, vm_t vaddr, uint8_t flags, enum mm_order_t order)
+void map_vmem(struct vm_branch *branch,
+ pm_t paddr, vm_t vaddr, uint8_t flags, enum mm_order order)
{
- enum mm_order_t top = __mm_max_order;
+ enum mm_order top = __mm_max_order;
while (top != order) {
size_t idx = vm_to_index(vaddr, top);
@@ -102,7 +104,7 @@ void map_vmem(struct vm_branch_t *branch,
branch->leaf[idx] = __create_leaf();
- branch = (struct vm_branch_t *)pte_addr(branch->leaf[idx]);
+ branch = (struct vm_branch *)pte_addr(branch->leaf[idx]);
top--;
}
@@ -110,10 +112,10 @@ void map_vmem(struct vm_branch_t *branch,
if (is_branch(branch->leaf[idx])) /* something has gone terribly wrong */
__destroy_branch(branch->leaf[idx]);
- branch->leaf[idx] = (struct vm_branch_t *)to_pte(paddr, flags);
+ branch->leaf[idx] = (struct vm_branch *)to_pte((pm_t)__pa(paddr), flags);
}
-void unmap_vmem(struct vm_branch_t *branch, vm_t vaddr)
+void unmap_vmem(struct vm_branch *branch, vm_t vaddr)
{
pm_t *pte = __find_vmem(branch, vaddr, 0);
@@ -121,12 +123,60 @@ void unmap_vmem(struct vm_branch_t *branch, vm_t vaddr)
*pte = 0;
}
-void flush_tlb(id_t cpu_id)
+void flush_tlb()
{
- __asm__("sfence.vma %0\n" :: "rk" (cpu_id) : "memory");
+ __asm__("sfence.vma %0\n" :: "rk" (cpu_id()) : "memory");
}
void flush_tlb_all()
{
__asm__("sfence.vma\n" ::: "memory");
}
+
+static void start_vmem(struct vm_branch *branch, enum mm_mode m)
+{
+ branch = (struct vm_branch *)__pa(branch);
+
+ if(m == Sv32)
+ csr_write(CSR_SATP, SATP_MODE_Sv32 | pm_to_pnum((pm_t)(branch)));
+ else if (m == Sv39)
+ csr_write(CSR_SATP, SATP_MODE_Sv39 | pm_to_pnum((pm_t)(branch)));
+ else
+ csr_write(CSR_SATP, SATP_MODE_Sv48 | pm_to_pnum((pm_t)(branch)));
+
+ flush_tlb_all();
+ /* Sv57 && Sv64 in the future? */
+}
+
+struct vm_branch *init_vmem(void *fdt)
+{
+ UNUSED(fdt);
+ struct vm_branch *b = (struct vm_branch *)alloc_page(MM_KPAGE, 0);
+ memset(b, 0, MM_KPAGE_SIZE);
+
+ populate_root_branch(b);
+ /* update which memory branch to use */
+ start_vmem(b, Sv39);
+ return b;
+}
+
+void populate_root_branch(struct vm_branch *b)
+{
+ size_t flags = VM_V | VM_R | VM_W | VM_X | VM_G;
+ for(size_t i = KSTART_PAGE; i < IO_PAGE; ++i)
+ b->leaf[i] = (struct vm_branch *)to_pte(RAM_BASE + SZ_1G * (i - KSTART_PAGE), flags);
+
+ /* map kernel IO to zero for now, this will be overridden later
+ * (if at all) */
+ b->leaf[IO_PAGE] = (struct vm_branch *)to_pte(0, flags);
+}
+
+#ifdef DEBUG
+int setup_kernel_io(struct vm_branch *b, vm_t paddr)
+{
+ /* assume Sv39 for now */
+ pm_t gigapage = paddr / MM_GPAGE_SIZE;
+ b->leaf[IO_PAGE] = (struct vm_branch *)to_pte(gigapage, VM_V | VM_R | VM_W);
+ return 0;
+}
+#endif
diff --git a/arch/riscv/source.mk b/arch/riscv/source.mk
index 0e9a5c3..e50880c 100644
--- a/arch/riscv/source.mk
+++ b/arch/riscv/source.mk
@@ -1,6 +1,6 @@
KERNEL_LOCAL != echo arch/riscv/kernel/*.[cS]
KERNEL_SOURCES += $(KERNEL_LOCAL)
-INIT_SOURCES += arch/riscv/init/init.c
+INIT_SOURCES += arch/riscv/init/*.[cS]
CLEANUP_CMD := ./arch/riscv/conf/rmimage.sh