aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2021-10-10 23:03:20 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2021-10-10 23:03:20 +0300
commitd2ff961a2a08229e1f2245e8dba9b2275016133c (patch)
treecffab8f0586b05a059715b2fa9676b8310bd297e
parentb30ee14270a44e48167934b9b3923b4280bdc855 (diff)
downloadkmi-d2ff961a2a08229e1f2245e8dba9b2275016133c.tar.gz
kmi-d2ff961a2a08229e1f2245e8dba9b2275016133c.zip
Prepare proper jump to kernel
-rw-r--r--arch/riscv/common/vmem.c4
-rw-r--r--arch/riscv/config.h2
-rw-r--r--arch/riscv/init/init.c44
-rw-r--r--arch/riscv/kernel/main.c14
-rw-r--r--common/pmem.c4
-rw-r--r--include/apos/attrs.h2
-rw-r--r--include/apos/init.h25
-rw-r--r--include/apos/main.h8
-rw-r--r--include/apos/mem.h5
9 files changed, 73 insertions, 35 deletions
diff --git a/arch/riscv/common/vmem.c b/arch/riscv/common/vmem.c
index 17db777..d0f3ebe 100644
--- a/arch/riscv/common/vmem.c
+++ b/arch/riscv/common/vmem.c
@@ -3,10 +3,6 @@
#include <pages.h>
#include <vmem.h>
-extern const size_t mm_shifts[10];
-extern const size_t mm_widths[10];
-extern const size_t mm_sizes[10];
-
#define pte_ppn(pte) (((paddr_t)(pte)) >> 10)
#define pte_flags(pte) (((paddr_t)(pte)) & 0xff)
#define to_pte(p, f) ((paddr_to_pnum(p) << 10) + (f))
diff --git a/arch/riscv/config.h b/arch/riscv/config.h
index 13e166b..f223ee7 100644
--- a/arch/riscv/config.h
+++ b/arch/riscv/config.h
@@ -2,7 +2,7 @@
#define PM_KERN 0x83800000
#define PM_STACK_BASE (PM_KERN + SZ_2M)
#define PM_STACK_TOP (PM_STACK_BASE + SZ_2M - 2)
-#define VM_KERN ((-1) - SZ_1G + 1)
+#define VM_KERN (-SZ_1G)
#define MAX_ORDER 3
#define PAGE_SHIFT 12
diff --git a/arch/riscv/init/init.c b/arch/riscv/init/init.c
index 1e04d34..f1a4697 100644
--- a/arch/riscv/init/init.c
+++ b/arch/riscv/init/init.c
@@ -1,3 +1,5 @@
+/* TODO: cleanup :P */
+
#include <apos/init.h>
#include <apos/sizes.h>
#include <apos/types.h>
@@ -79,7 +81,6 @@ static enum serial_dev_t serial_dev_enum(const char *dev_name)
return -1;
}
-static void* uart_ptr_glbl = 0;
static void init_debug(void *fdt)
{
int chosen_offset = fdt_path_offset(fdt, "/chosen");
@@ -98,9 +99,7 @@ static void init_debug(void *fdt)
struct cell_info ci = get_reginfo(fdt, stdout);
void *reg_ptr = (void *)fdt_getprop(fdt, stdout_offset, "reg", NULL);
- void *uart_ptr = 0;
- uart_ptr = (void *)(paddr_t)fdt_load_int_ptr(ci.addr_cells, reg_ptr);
- uart_ptr_glbl = uart_ptr;
+ void *uart_ptr = (void *)(paddr_t)fdt_load_int_ptr(ci.addr_cells, reg_ptr);
dbg_init(uart_ptr, dev);
}
@@ -258,30 +257,53 @@ struct vm_branch_t *prepare_vmem()
/* map stack */
map_vmem(branch, PM_STACK_BASE, PM_STACK_BASE, VM_R | VM_W | VM_V, MM_MPAGE);
- /* map debug VERY UGLY GLOBALS BAH */
- map_vmem(branch, (paddr_t)uart_ptr_glbl, (paddr_t)uart_ptr_glbl, VM_R | VM_W | VM_V, MM_KPAGE);
-
- /* TODO: map more stuff */
-
+ /* TODO: map more stuff? */
return branch;
}
void start_vmem(struct vm_branch_t *branch)
{
/* assume Sv48 and ASID 0*/
+ /* TODO: get ASID from CPU id
+ * TODO: more cores lol
+ */
csr_write(CSR_SATP, SATP_MODE_48 | (((paddr_t)(branch)) >> 12));
}
+struct init_data_t populate_initdata(void *fdt, struct vm_branch_t *branch)
+{
+ extern char __init_start, __init_end;
+
+ struct init_data_t d = {0};
+ d.init_base = (paddr_t)&__init_start;
+ d.init_top = (paddr_t)&__init_end;
+
+ d.initrd_base = get_initrdbase(fdt);
+ d.initrd_top = get_initrdtop(fdt);
+
+ d.fdt_base = get_fdtbase(fdt);
+ d.fdt_top = get_fdttop(fdt);
+
+ d.stack_base = PM_STACK_BASE;
+ d.stack_top = PM_STACK_TOP;
+
+ d.kernel_vm_base = branch;
+
+ return d;
+}
+
void init(void *fdt)
{
init_debug(fdt);
dbg_fdt(fdt);
setup_pmem(fdt);
+
struct vm_branch_t *branch = prepare_vmem();
+ struct init_data_t d = populate_initdata(fdt, branch);
start_vmem(branch);
/* update_pmap(TODO: figure out where to place pmap in vmem); */
- void (*main)(void *uart) = (void (*)(void *))VM_KERN;
- main(uart_ptr_glbl);
+ void (*main)(struct init_data_t) = (void (*)(struct init_data_t))VM_KERN;
+ main(d);
}
diff --git a/arch/riscv/kernel/main.c b/arch/riscv/kernel/main.c
index 510ea42..3575ceb 100644
--- a/arch/riscv/kernel/main.c
+++ b/arch/riscv/kernel/main.c
@@ -1,8 +1,14 @@
-#include <apos/main.h>
#include <apos/debug.h>
+#include <apos/init.h>
-void __main main(void *uart)
+void __main main(struct init_data_t d)
{
- dbg_init(uart, NS16550A);
- dbg("Hello from (hopefully) virtual memory!\n");
+ /* TODO: approximate order of business:
+ * setup debugging in vmem
+ * free unnecessary init
+ * setup interrupts (should this be done in init?)
+ * load init from initrd
+ * setup init environment (thread control blocks etc.)
+ * jumpstart init (free stack init setup)
+ */
}
diff --git a/common/pmem.c b/common/pmem.c
index 68632ae..dcbe921 100644
--- a/common/pmem.c
+++ b/common/pmem.c
@@ -24,10 +24,6 @@
#define foreach_not_used_page(var, start, order)\
__foreach_page(var, start, var->entries, used, NEG)
-extern const size_t mm_shifts[10];
-extern const size_t mm_widths[10];
-extern const size_t mm_sizes[10];
-
typedef uint32_t mm_info_t;
typedef void mm_node_t;
diff --git a/include/apos/attrs.h b/include/apos/attrs.h
index ae936d9..469b7bd 100644
--- a/include/apos/attrs.h
+++ b/include/apos/attrs.h
@@ -7,4 +7,6 @@
#define __packed __attribute__((packed))
#define __weak __attribute__((weak))
+#define __main __section(".kernel.start") __noinline
+
#endif /* APOS_COMPILER_ATTRIBUTES_H */
diff --git a/include/apos/init.h b/include/apos/init.h
index 1bf6a9e..1d687cb 100644
--- a/include/apos/init.h
+++ b/include/apos/init.h
@@ -1,9 +1,28 @@
#ifndef APOS_INIT_H
#define APOS_INIT_H
-#include <apos/attrs.h>
+#include <apos/pmem.h>
-#define __init
-#define __initdata
+struct init_data_t {
+ paddr_t init_base;
+ paddr_t init_top;
+
+ paddr_t initrd_base;
+ paddr_t initrd_top;
+
+ paddr_t fdt_base;
+ paddr_t fdt_top;
+
+ paddr_t stack_base;
+ paddr_t stack_top;
+
+ struct vm_branch_t *kernel_vm_base;
+
+ /*
+ TODO: is this necessary?
+ paddr_t pmap_base;
+ paddr_t pmap_top;
+ */
+};
#endif /* APOS_INIT_H */
diff --git a/include/apos/main.h b/include/apos/main.h
deleted file mode 100644
index 15fff73..0000000
--- a/include/apos/main.h
+++ /dev/null
@@ -1,8 +0,0 @@
-#ifndef APOS_MAIN_H
-#define APOS_MAIN_H
-
-#include <apos/attrs.h>
-
-#define __main __section(".kernel.start") __noinline
-
-#endif /* APOS_MAIN_H */
diff --git a/include/apos/mem.h b/include/apos/mem.h
index 158ac5e..4846f5e 100644
--- a/include/apos/mem.h
+++ b/include/apos/mem.h
@@ -127,4 +127,9 @@
#define __o_container(idx) ((idx) / MM_OINFO_WIDTH)
#define __o_bit(idx) ((idx) & (MM_OINFO_WIDTH - 1))
+
+extern const size_t mm_shifts[10];
+extern const size_t mm_widths[10];
+extern const size_t mm_sizes[10];
+
#endif /* APOS_MEM_H */