aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xarch/riscv64/conf/mkimage.sh14
-rw-r--r--arch/riscv64/config.h31
-rw-r--r--arch/riscv64/init/init.c24
-rw-r--r--arch/riscv64/kernel/pmem.c14
-rw-r--r--arch/riscv64/kernel/vmem.c17
-rw-r--r--arch/riscv64/source.mk2
-rw-r--r--common/debug.c7
-rw-r--r--common/tcb.c5
-rw-r--r--include/apos/debug.h11
9 files changed, 81 insertions, 44 deletions
diff --git a/arch/riscv64/conf/mkimage.sh b/arch/riscv64/conf/mkimage.sh
index d791396..9f745a1 100755
--- a/arch/riscv64/conf/mkimage.sh
+++ b/arch/riscv64/conf/mkimage.sh
@@ -1,5 +1,7 @@
#!/bin/sh
+XLEN=$(echo "$1" | cut -c '6-7')
+
if [ ! -e fs ]; then
mkdir fs
fi
@@ -8,7 +10,6 @@ if [ ! -e rootfs.img ]; then
virt-make-fs --partition=mbr --type=vfat --size=10M fs rootfs.img
fi
-
SSIZE=$(fdisk -l rootfs.img | awk '$1=="Units:" {print $8}')
OFFSET=$(fdisk -l rootfs.img | awk '$1=="rootfs.img1" {print $2}')
mount -o loop,offset=$((${SSIZE}*${OFFSET})) rootfs.img fs
@@ -16,15 +17,16 @@ mount -o loop,offset=$((${SSIZE}*${OFFSET})) rootfs.img fs
# not entirely pleased with this solution, although eventually I should probably
# move `run` out of the kernel repo and into some `aposos` repo with a runtime
# and proper initrd etc. so this is good enough for now
-../rv64/u-boot-apos/tools/mkimage -f arch/riscv64/conf/apos.its fs/apos.itb
-../rv64/u-boot-apos/tools/mkimage -A riscv -O apos -T script \
+../rv${XLEN}/u-boot-apos/tools/mkimage -f arch/riscv${XLEN}/conf/apos.its fs/apos.itb
+../rv${XLEN}/u-boot-apos/tools/mkimage -A riscv -O apos -T script \
-d arch/riscv64/conf/boot.cmd fs/boot.scr
umount -l fs
-qemu-system-riscv64 -machine virt \
- -kernel ../rv64/u-boot-apos/u-boot \
- -bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf \
+qemu-system-riscv${XLEN} \
+ -machine virt \
+ -kernel ../rv${XLEN}/u-boot-apos/u-boot \
+ -bios ../rv${XLEN}/opensbi/build/platform/generic/firmware/fw_jump.elf \
-device virtio-blk-device,drive=hd0 \
-drive file=rootfs.img,format=raw,id=hd0 \
-S -s -m 2G \
diff --git a/arch/riscv64/config.h b/arch/riscv64/config.h
index 7606e38..2c3dce6 100644
--- a/arch/riscv64/config.h
+++ b/arch/riscv64/config.h
@@ -77,28 +77,41 @@
#define UVMEM_END (SZ_256G - SZ_1G)
/** RPC stack top. */
-#define RPC_STACK_TOP (SZ_256G)
+#define RPC_STACK_TOP (UVMEM_END)
/** RPC stack base. */
-#define RPC_STACK_BASE (SZ_256G - SZ_1G)
+#define RPC_STACK_BASE (RPC_STACK_TOP - SZ_1G)
+
+/**
+ * Size of the default top page.
+ * For now we're only targeting Sv39, so this can easily be a macro.
+ * Eventually it might have to be turned into a function call to support Sv48
+ * etc.
+ */
+#define TOP_PAGE_SIZE SZ_1G
+
+/** Default Sv mode on rv64 in apos. */
+#define DEFAULT_Sv_MODE Sv39
#else
/* 32bit */
/** \todo figure this stuff out */
-#define VM_DMAP (0x000000000)
-#define VM_KERN (VM_DMAP + SZ_256K)
-#define ROOT_PTE (0UL)
-#define ROOT_REGION (SZ_4K)
+#define VM_DMAP 0x80000000 /* works on qemu at least */
+#define VM_KERN (VM_DMAP + FW_MAX_SIZE)
#define IO_PAGE 1023UL
#define KSTART_PAGE 512UL
#define CSTACK_PAGE 511UL
#define UVMEM_START (SZ_4K)
-#define UVMEM_END (SZ_4G - SZ_8M)
+#define UVMEM_END (SZ_2G - SZ_8M)
+
+#define RPC_STACK_TOP (UVMEM_END)
+#define RPC_STACK_BASE (RPC_STACK_TOP - SZ_8M)
+
+#define TOP_PAGE_SIZE SZ_4M
+#define DEFAULT_Sv_MODE Sv32
-#define RPC_STACK_TOP (SZ_4G)
-#define RPC_STACK_BASE (SZ_4G - SZ_8M)
#endif
#endif /* APOS_RISCV_CONFIG_H */
diff --git a/arch/riscv64/init/init.c b/arch/riscv64/init/init.c
index 0ea1f37..0680296 100644
--- a/arch/riscv64/init/init.c
+++ b/arch/riscv64/init/init.c
@@ -42,31 +42,27 @@ static void init_bootmem()
* @todo Allocate root_branch statically? */
root_branch = (struct vmem *)align_up(top, SZ_4K);
-#if defined(riscv64)
- const size_t top_size = SZ_1G;
-#else
- const size_t top_size = SZ_4M;
-#endif
-
/* direct mapping (temp) */
for (size_t i = 0; i < CSTACK_PAGE; ++i)
- root_branch->leaf[i] = (struct vmem *)to_pte(top_size * i, flags);
+ root_branch->leaf[i] = (struct vmem *)to_pte(TOP_PAGE_SIZE * i, flags);
/* kernel (also sort of direct mapping) */
flags |= VM_G;
for (size_t i = KSTART_PAGE; i < IO_PAGE; ++i)
root_branch->leaf[i] = (struct vmem *)to_pte(
- RAM_BASE + top_size * (i - KSTART_PAGE), flags);
+ RAM_BASE + TOP_PAGE_SIZE * (i - KSTART_PAGE), flags);
/* kernel IO, map to 0 for now, will be updated in the future */
root_branch->leaf[IO_PAGE] = (struct vmem *)to_pte(0, flags);
-#if defined(riscv64)
- const uintmax_t mode = SATP_MODE_Sv39;
-#else
- const uintmax_t mode = SATP_MODE_Sv32;
-#endif
- csr_write(CSR_SATP, SATP_MODE_Sv39 | ((uintptr_t)root_branch >> 12));
+ uintmax_t mode = Sv39;
+ switch (DEFAULT_Sv_MODE) {
+ case Sv32: mode = SATP_MODE_Sv32; break;
+ case Sv39: mode = SATP_MODE_Sv39; break;
+ case Sv48: mode = SATP_MODE_Sv48; break;
+ default: break;
+ };
+ csr_write(CSR_SATP, mode | ((uintptr_t)root_branch >> 12));
}
/** Relocate kernel proper. */
diff --git a/arch/riscv64/kernel/pmem.c b/arch/riscv64/kernel/pmem.c
index e438131..3076dc5 100644
--- a/arch/riscv64/kernel/pmem.c
+++ b/arch/riscv64/kernel/pmem.c
@@ -12,13 +12,21 @@ stat_t stat_pmem_conf(void *fdt, size_t *max_order, size_t *base_bits,
size_t bits[NUM_ORDERS])
{
UNUSED(fdt);
+ *base_bits = 12;
+
+ size_t nbits = 9;
- /* Sv39 for now */
+#if defined(riscv64)
*max_order = 2;
- *base_bits = 12;
+ nbits = 9;
+#else
+ *max_order = 1;
+ nbits = 10;
+#endif
+
/* we can assume bits[] is zeroed out beforehand */
for (size_t i = 0; i <= *max_order; ++i)
- bits[i] = 9;
+ bits[i] = nbits;
return OK;
}
diff --git a/arch/riscv64/kernel/vmem.c b/arch/riscv64/kernel/vmem.c
index 381ef5c..1abc58d 100644
--- a/arch/riscv64/kernel/vmem.c
+++ b/arch/riscv64/kernel/vmem.c
@@ -279,7 +279,7 @@ struct vmem *create_vmem()
stat_t use_vmem(struct vmem *b)
{
- __use_vmem(b, Sv39);
+ __use_vmem(b, DEFAULT_Sv_MODE);
return OK;
}
@@ -294,21 +294,20 @@ stat_t populate_kvmem(struct vmem *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 vmem *)to_pte(
- RAM_BASE + SZ_1G * (i - KSTART_PAGE), flags);
+ RAM_BASE + TOP_PAGE_SIZE * (i - KSTART_PAGE), flags);
- /* map kernel IO to zero for now, this will be overridden later
- * (if at all) */
- b->leaf[IO_PAGE] = (struct vmem *)to_pte(0, flags);
+ /* map in IO region */
+ map_io_dbg(b);
return OK;
}
#if defined(DEBUG)
vm_t setup_kernel_io(struct vmem *b, vm_t paddr)
{
- /* assume Sv39 for now */
- pm_t gigapage = paddr / MM_GPAGE_SIZE;
- b->leaf[IO_PAGE] = (struct vmem *)to_pte(gigapage, VM_V | VM_R | VM_W);
- return -SZ_1G + paddr - (gigapage * MM_GPAGE_SIZE);
+ pm_t top_page = paddr / TOP_PAGE_SIZE;
+ b->leaf[IO_PAGE] = (struct vmem *)to_pte(top_page * TOP_PAGE_SIZE,
+ VM_V | VM_R | VM_W);
+ return -TOP_PAGE_SIZE + paddr - (top_page * TOP_PAGE_SIZE);
}
#endif
diff --git a/arch/riscv64/source.mk b/arch/riscv64/source.mk
index f469e40..f050468 100644
--- a/arch/riscv64/source.mk
+++ b/arch/riscv64/source.mk
@@ -16,7 +16,7 @@ DEBUGFLAGS != [ $(LLVM) ] && echo $(DEBUGFLAGS:-flto=) || echo $(DEBUGFLAGS)
OBJCOPY := $(CROSS_COMPILE)-objcopy
run:
- $(ARCH_SOURCE)/conf/mkimage.sh
+ $(ARCH_SOURCE)/conf/mkimage.sh $(ARCH)
include $(ARCH_SOURCE)/asm/source.mk
diff --git a/common/debug.c b/common/debug.c
index 1f5e872..36fed87 100644
--- a/common/debug.c
+++ b/common/debug.c
@@ -46,10 +46,15 @@ void setup_dmap_dbg()
void setup_io_dbg(struct vmem *b)
{
- vm_t io_ptr = setup_kernel_io(b, dbg_info.dbg_ptr);
+ vm_t io_ptr = map_io_dbg(b);
__setup_dbg(io_ptr, dbg_info.dev);
}
+vm_t map_io_dbg(struct vmem *b)
+{
+ return setup_kernel_io(b, dbg_info.dbg_ptr);
+}
+
/* if there arises a need for more supported serial drivers, I should probably
* try to implement some kind of basic driver subsystem, but this is good enough
* for now. */
diff --git a/common/tcb.c b/common/tcb.c
index 119898f..1e25f47 100644
--- a/common/tcb.c
+++ b/common/tcb.c
@@ -76,7 +76,10 @@ static id_t __alloc_tid(struct tcb *t)
* Setup RPC stack.
*
* RPC stack is local to each thread, and should not be visible to other threads
- * in the same process.
+ * in the same process. Currently maps the RPC stack in BASE_PAGE increments, to
+ * hopefully allow us to later quickly disallow access to programs lower down in
+ * the RPC call chain by turning off all stack pages lower than the current
+ * stack pointer. We shall see if this actually works or not.
*
* @param t Thread to setup RPC stack for.
* @param bytes Minimum size of RPC stack.
diff --git a/include/apos/debug.h b/include/apos/debug.h
index e7172f5..0d8ec26 100644
--- a/include/apos/debug.h
+++ b/include/apos/debug.h
@@ -345,11 +345,21 @@ void setup_dmap_dbg();
/**
* Setup debugging in virtual memory context.
+ * Both maps the debugging region and sets the UART subsystem to use
+ * the mapped region.
*
* @param vmem Virtual memory space to use.
*/
void setup_io_dbg(struct vmem *vmem);
+/**
+ * Map debugging into virtual memory context.
+ *
+ * @param vmem Virtual memory space to use.
+ * @return Virtual address of mapped region.
+ */
+vm_t map_io_dbg(struct vmem *vmem);
+
/** @name Internal. */
/** @{ */
@@ -407,6 +417,7 @@ void setup_io_dbg(struct vmem *vmem);
#define init_dbg(...)
#define setup_dmap_dbg(...)
#define setup_io_dbg(...)
+#define map_io_dbg(...)
#define bug(...)
#define warn(...)