aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bbl/bbl.c4
-rw-r--r--machine/encoding.h8
-rw-r--r--machine/unprivileged_memory.h4
-rw-r--r--machine/vm.h2
-rw-r--r--pk/mmap.c38
5 files changed, 17 insertions, 39 deletions
diff --git a/bbl/bbl.c b/bbl/bbl.c
index af2483f..efee22b 100644
--- a/bbl/bbl.c
+++ b/bbl/bbl.c
@@ -34,7 +34,7 @@ static void supervisor_vm_init()
int l2_shift = RISCV_PGLEVEL_BITS + RISCV_PGSHIFT;
size_t l2_idx = (info.first_user_vaddr >> l2_shift) & ((1 << RISCV_PGLEVEL_BITS)-1);
l2_idx += ((vaddr - info.first_user_vaddr) >> l2_shift);
- middle_pt[l2_idx] = pte_create(paddr >> RISCV_PGSHIFT, PTE_TYPE_SRWX_GLOBAL);
+ middle_pt[l2_idx] = pte_create(paddr >> RISCV_PGSHIFT, PTE_G | PTE_R | PTE_W | PTE_X);
}
// map SBI at top of vaddr space
@@ -43,7 +43,7 @@ static void supervisor_vm_init()
assert(num_sbi_pages <= (1 << RISCV_PGLEVEL_BITS));
for (uintptr_t i = 0; i < num_sbi_pages; i++) {
uintptr_t idx = (1 << RISCV_PGLEVEL_BITS) - num_sbi_pages + i;
- sbi_pt[idx] = pte_create((DRAM_BASE / RISCV_PGSIZE) + i, PTE_TYPE_SRX_GLOBAL);
+ sbi_pt[idx] = pte_create((DRAM_BASE / RISCV_PGSIZE) + i, PTE_G | PTE_R | PTE_X);
}
pte_t* sbi_pte = middle_pt + ((num_middle_pts << RISCV_PGLEVEL_BITS)-1);
assert(!*sbi_pte);
diff --git a/machine/encoding.h b/machine/encoding.h
index 2f52bdb..641954a 100644
--- a/machine/encoding.h
+++ b/machine/encoding.h
@@ -18,6 +18,7 @@
#define MSTATUS_XS 0x00018000
#define MSTATUS_MPRV 0x00020000
#define MSTATUS_PUM 0x00040000
+#define MSTATUS_MXR 0x00080000
#define MSTATUS_VM 0x1F000000
#define MSTATUS32_SD 0x80000000
#define MSTATUS64_SD 0x8000000000000000
@@ -128,13 +129,6 @@
#define PTE_D 0x080 // Dirty
#define PTE_SOFT 0x300 // Reserved for Software
-#define PTE_TYPE_TABLE 0x00
-#define PTE_TYPE_R 0x02
-#define PTE_TYPE_RW 0x06
-#define PTE_TYPE_X 0x08
-#define PTE_TYPE_RX 0x0A
-#define PTE_TYPE_RWX 0x0E
-
#define PTE_PPN_SHIFT 10
#define PTE_TABLE(PTE) (((PTE) & (PTE_V | PTE_R | PTE_W | PTE_X)) == PTE_V)
diff --git a/machine/unprivileged_memory.h b/machine/unprivileged_memory.h
index d03cc5e..7ed9e7c 100644
--- a/machine/unprivileged_memory.h
+++ b/machine/unprivileged_memory.h
@@ -57,7 +57,7 @@ static uint32_t __attribute__((always_inline)) get_insn(uintptr_t mepc, uintptr_
"lw %[insn], (%[addr])\n"
"csrw mstatus, %[mstatus]"
: [mstatus] "+&r" (__mstatus), [insn] "=&r" (val)
- : [mprv] "r" (MSTATUS_MPRV), [addr] "r" (__mepc));
+ : [mprv] "r" (MSTATUS_MPRV | MSTATUS_MXR), [addr] "r" (__mepc));
#else
uintptr_t rvc_mask = 3, tmp;
asm ("csrrs %[mstatus], mstatus, %[mprv]\n"
@@ -69,7 +69,7 @@ static uint32_t __attribute__((always_inline)) get_insn(uintptr_t mepc, uintptr_
"add %[insn], %[insn], %[tmp]\n"
"1: csrw mstatus, %[mstatus]"
: [mstatus] "+&r" (__mstatus), [insn] "=&r" (val), [tmp] "=&r" (tmp)
- : [mprv] "r" (MSTATUS_MPRV), [addr] "r" (__mepc),
+ : [mprv] "r" (MSTATUS_MPRV | MSTATUS_MXR), [addr] "r" (__mepc),
[rvc_mask] "r" (rvc_mask));
#endif
*mstatus = __mstatus;
diff --git a/machine/vm.h b/machine/vm.h
index f51c639..dd9bf59 100644
--- a/machine/vm.h
+++ b/machine/vm.h
@@ -29,7 +29,7 @@ static inline pte_t pte_create(uintptr_t ppn, int type)
static inline pte_t ptd_create(uintptr_t ppn)
{
- return pte_create(ppn, PTE_TYPE_TABLE);
+ return pte_create(ppn, PTE_V);
}
#endif
diff --git a/pk/mmap.c b/pk/mmap.c
index 9f886ed..db491d7 100644
--- a/pk/mmap.c
+++ b/pk/mmap.c
@@ -140,31 +140,13 @@ static uintptr_t __vm_alloc(size_t npage)
static inline pte_t prot_to_type(int prot, int user)
{
- prot &= PROT_READ|PROT_WRITE|PROT_EXEC;
- if (user) {
- switch (prot) {
- case PROT_NONE: return PTE_TYPE_SR;
- case PROT_READ: return PTE_TYPE_UR_SR;
- case PROT_WRITE: return PTE_TYPE_URW_SRW;
- case PROT_EXEC: return PTE_TYPE_URX_SRX;
- case PROT_READ|PROT_WRITE: return PTE_TYPE_URW_SRW;
- case PROT_READ|PROT_EXEC: return PTE_TYPE_URX_SRX;
- case PROT_WRITE|PROT_EXEC: return PTE_TYPE_URWX_SRWX;
- case PROT_READ|PROT_WRITE|PROT_EXEC: return PTE_TYPE_URWX_SRWX;
- }
- } else {
- switch (prot) {
- case PROT_NONE:
- case PROT_READ: return PTE_TYPE_SR;
- case PROT_WRITE: return PTE_TYPE_SRW;
- case PROT_EXEC: return PTE_TYPE_SRX;
- case PROT_READ|PROT_WRITE: return PTE_TYPE_SRW;
- case PROT_READ|PROT_EXEC: return PTE_TYPE_SRX;
- case PROT_WRITE|PROT_EXEC: return PTE_TYPE_SRWX;
- case PROT_READ|PROT_WRITE|PROT_EXEC: return PTE_TYPE_SRWX;
- }
- }
- __builtin_unreachable();
+ pte_t pte = 0;
+ if (prot & PROT_READ) pte |= PTE_R;
+ if (prot & PROT_WRITE) pte |= PTE_W;
+ if (prot & PROT_EXEC) pte |= PTE_X;
+ if (pte == 0) pte = PTE_R;
+ if (user) pte |= PTE_U;
+ return pte;
}
int __valid_user_range(uintptr_t vaddr, size_t len)
@@ -360,8 +342,10 @@ uintptr_t do_mprotect(uintptr_t addr, size_t length, int prot)
}
v->prot = prot;
} else {
- if (((prot & PROT_WRITE) && !PTE_UW(*pte))
- || ((prot & PROT_EXEC) && !PTE_UX(*pte))) {
+ if (!(*pte & PTE_U) ||
+ ((prot & PROT_READ) && !(*pte & PTE_R)) ||
+ ((prot & PROT_WRITE) && !(*pte & PTE_W)) ||
+ ((prot & PROT_EXEC) && !(*pte & PTE_X))) {
//TODO:look at file to find perms
res = -EACCES;
break;