summaryrefslogtreecommitdiff
path: root/sys/include
diff options
context:
space:
mode:
authorstefan <stefan@s00.xyz>2023-04-29 17:37:05 -0400
committerstefan <stefan@s00.xyz>2023-04-29 17:37:05 -0400
commit2a20e3596a446df55cfe4b34b45b4cd8eae594db (patch)
treed64632a8d804a9af9b1cbda49bb9f8d763d93719 /sys/include
parenta3c174ee4c08d1d5e7a89ce187f52e3c0807a7eb (diff)
downloadsv-2a20e3596a446df55cfe4b34b45b4cd8eae594db.tar.gz
some work on paging
Diffstat (limited to 'sys/include')
-rw-r--r--sys/include/kalloc.h4
-rw-r--r--sys/include/sbi.h6
-rw-r--r--sys/include/vm.h66
3 files changed, 74 insertions, 2 deletions
diff --git a/sys/include/kalloc.h b/sys/include/kalloc.h
index 5553d94..d77eae5 100644
--- a/sys/include/kalloc.h
+++ b/sys/include/kalloc.h
@@ -1,13 +1,15 @@
#ifndef _KALLOC_H
#define _KALLOC_H
+#include <stdint.h>
+
void *kalloc(void);
void *kzalloc(void);
void kfree(void *);
-void kalloc_init(size_t);
+void kalloc_init(uintptr_t end);
void walkfree(void);
diff --git a/sys/include/sbi.h b/sys/include/sbi.h
index 86ca295..7430bc6 100644
--- a/sys/include/sbi.h
+++ b/sys/include/sbi.h
@@ -67,6 +67,12 @@ sbi_debug_console_write_byte(uint8_t byte)
}
static inline void
+sbi_remote_sfence_vma(unsigned long hart_mask, unsigned long hart_mask_base, unsigned long start_addr, unsigned long size)
+{
+ sbi_ecall(0x52464E43, 1, hart_mask, hart_mask_base, start_addr, size, 0, 0);
+}
+
+static inline void
sbi_shutdown(void)
{
sbi_ecall(8, 0, 0, 0, 0, 0, 0, 0);
diff --git a/sys/include/vm.h b/sys/include/vm.h
index 1883ea7..4378552 100644
--- a/sys/include/vm.h
+++ b/sys/include/vm.h
@@ -1,6 +1,70 @@
#ifndef _VM_H
#define _VM_H
-void vminit(void);
+#include <stdint.h>
+
+#define PAGEBITS_VALID (1 << 0)
+#define PAGEBITS_R (1 << 1)
+#define PAGEBITS_W (1 << 2)
+#define PAGEBITS_X (1 << 3)
+#define PAGEBITS_USER (1 << 4)
+#define PAGEBITS_GLOBAL (1 << 5)
+#define PAGEBITS_ACCESSED (1 << 6)
+#define PAGEBITS_DIRTY (1 << 7)
+
+#define PAGEBITS_RO PAGEBITS_R
+#define PAGEBITS_XO PAGEBITS_X
+#define PAGEBITS_RW (PAGEBITS_R | PAGEBITS_W)
+#define PAGEPITS_RX (PAGEBITS_R | PAGEBITS_X)
+#define PAGEBITS_RWX (PAGEBITS_R | PAGEBITS_W | PAGEBITS_X)
+
+#define MODE_SV39 (8L << 60)
+
+typedef uint64_t pt_entry_t;
+typedef struct _pagetable {
+ pt_entry_t entries[512];
+} pagetable_t;
+
+/* allocate kernel pagetable */
+void kptinit(void);
+void kptinithart(void);
+//void kptinitharts(uintptr_t mask);
+
+/* maps virtual address to physical address for @pt. */
+void vmap(pagetable_t *root, uintptr_t vaddr, uintptr_t paddr, int bits, int level);
+
+/* returns address of page table entry for @vaddr. if @alloc, next level page tables will be allocated */
+pt_entry_t *walkpt(pagetable_t *root, uintptr_t vaddr, int alloc);
+
+/* helper functions */
+static inline void write_satp(uintptr_t mode, uintptr_t ppn);
+static inline int pte_is_branch(pt_entry_t);
+static inline int pte_is_valid(pt_entry_t);
+
+#define PA2PTE(pa) ((uintptr_t)pa >> 2)
+#define PTE2PA(pa) ((uintptr_t)pa << 2)
+#define FLUSH_TLB() asm volatile("sfence.vma zero, zero")
+
+static inline void
+write_satp(uintptr_t mode, uintptr_t ppn)
+{
+ FLUSH_TLB();
+ asm volatile("csrw satp, %0" : : "r"((ppn >> 12) | mode));
+ FLUSH_TLB();
+}
+
+static inline int
+pte_is_branch(pt_entry_t e)
+{
+ return ((e & PAGEBITS_RWX) == 0);
+}
+#define pte_is_leaf(e) (!pte_is_branch(e))
+
+static inline int
+pte_is_valid(pt_entry_t e)
+{
+ return (e & PAGEBITS_VALID);
+}
+
#endif /* _VM_H */