-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement user-space virtual memory
- Loading branch information
1 parent
c2ffe3a
commit a73bc0d
Showing
14 changed files
with
25,073 additions
and
24,937 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#pragma once | ||
|
||
extern void *elf_entry; | ||
extern void *init_elf_entry; | ||
extern void *init_page_table; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#pragma once | ||
|
||
#define max(a, b) ((a) > (b) ? (a) : (b)) | ||
#define min(a, b) ((a) < (b) ? (a) : (b)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,7 @@ | |
|
||
#define PAGE_SIZE 4096 | ||
|
||
#ifndef __ASSEMBLER__ | ||
void *page_alloc(void); | ||
void page_free(void *ptr); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#include <types.h> | ||
|
||
#define VM_FLAG_READABLE 0x1 | ||
#define VM_FLAG_WRITABLE 0x2 | ||
#define VM_FLAG_EXECUTABLE 0x4 | ||
#define VM_FLAG_USER 0x8 | ||
|
||
void *vm_create_page_table(void); | ||
i32 vm_map_page(void *page_table, usize virt_addr, void *phys_addr, u32 flags); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include <mm/vm.h> | ||
|
||
#include <errno.h> | ||
#include <mm/page_alloc.h> | ||
#include <utils/mem.h> | ||
|
||
typedef struct { | ||
u8 flags; | ||
u8 reserved : 2; | ||
u64 ppn : 44; | ||
u16 padding : 10; | ||
} pte_t; | ||
|
||
#define PTE_FLAG_V 0x01 | ||
#define PTE_FLAG_R 0x02 | ||
#define PTE_FLAG_W 0x04 | ||
#define PTE_FLAG_X 0x08 | ||
#define PTE_FLAG_U 0x10 | ||
#define PTE_FLAG_G 0x20 | ||
#define PTE_FLAG_A 0x40 | ||
#define PTE_FLAG_D 0x80 | ||
|
||
static void init_pte(pte_t *pte, void *page, u32 flags) { | ||
pte->flags |= PTE_FLAG_V; | ||
|
||
if (flags & VM_FLAG_READABLE) | ||
pte->flags |= PTE_FLAG_R; | ||
if (flags & VM_FLAG_WRITABLE) | ||
pte->flags |= PTE_FLAG_W; | ||
if (flags & VM_FLAG_EXECUTABLE) | ||
pte->flags |= PTE_FLAG_X; | ||
if (flags & VM_FLAG_USER) | ||
pte->flags |= PTE_FLAG_U; | ||
|
||
pte->ppn = (usize) page >> 12; | ||
} | ||
|
||
void *vm_create_page_table(void) { | ||
pte_t *page_table = (pte_t *) page_alloc(); | ||
if (!page_table) | ||
return nullptr; | ||
mem_set(page_table, 0, PAGE_SIZE); | ||
return page_table; | ||
} | ||
|
||
i32 vm_map_page(void *page_table, usize virt_addr, void *phys_addr, u32 flags) { | ||
pte_t *page_table0 = (pte_t *) page_table; | ||
u16 idx0 = virt_addr >> 30; | ||
pte_t *pte0 = page_table0 + idx0; | ||
|
||
pte_t *page_table1 = (pte_t *) (usize) (pte0->ppn << 12); | ||
if (!(pte0->flags & PTE_FLAG_V)) { | ||
page_table1 = (pte_t *) vm_create_page_table(); | ||
if (!page_table1) | ||
return -ENOMEM; | ||
init_pte(pte0, page_table1, 0); | ||
} | ||
u16 idx1 = (virt_addr >> 21) & 0x1FF; | ||
pte_t *pte1 = page_table1 + idx1; | ||
|
||
pte_t *page_table2 = (pte_t *) (usize) (pte1->ppn << 12); | ||
if (!(pte1->flags & PTE_FLAG_V)) { | ||
page_table2 = (pte_t *) vm_create_page_table(); | ||
if (!page_table2) | ||
return -ENOMEM; | ||
init_pte(pte1, page_table2, 0); | ||
} | ||
u16 idx2 = (virt_addr >> 12) & 0x1FF; | ||
pte_t *pte2 = page_table2 + idx2; | ||
|
||
init_pte(pte2, phys_addr, flags); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.