2020-09-11 11:53:21 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
2020-09-21 13:15:55 +03:00
|
|
|
#include <mm/vmm.h>
|
2020-09-20 13:03:44 +03:00
|
|
|
#include <mm/pmm.h>
|
2020-09-11 11:53:21 +03:00
|
|
|
#include <lib/blib.h>
|
2021-07-15 15:09:12 +03:00
|
|
|
#include <sys/cpu.h>
|
2020-09-11 11:53:21 +03:00
|
|
|
|
|
|
|
#define PT_SIZE ((uint64_t)0x1000)
|
|
|
|
|
|
|
|
typedef uint64_t pt_entry_t;
|
|
|
|
|
2021-07-15 15:09:12 +03:00
|
|
|
void vmm_assert_nx(void) {
|
|
|
|
uint32_t a, b, c, d;
|
|
|
|
if (!cpuid(0x80000001, 0, &a, &b, &c, &d) || !(d & (1 << 20))) {
|
|
|
|
panic("vmm: NX functionality not available on this CPU.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 11:53:21 +03:00
|
|
|
static pt_entry_t *get_next_level(pt_entry_t *current_level, size_t entry) {
|
|
|
|
pt_entry_t *ret;
|
|
|
|
|
|
|
|
if (current_level[entry] & 0x1) {
|
|
|
|
// Present flag set
|
2020-09-21 14:41:31 +03:00
|
|
|
ret = (pt_entry_t *)(size_t)(current_level[entry] & ~((pt_entry_t)0xfff));
|
2020-09-11 11:53:21 +03:00
|
|
|
} else {
|
|
|
|
// Allocate a table for the next level
|
2021-04-15 03:21:38 +03:00
|
|
|
ret = ext_mem_alloc(PT_SIZE);
|
2020-09-11 11:53:21 +03:00
|
|
|
// Present + writable + user (0b111)
|
2020-09-21 14:41:31 +03:00
|
|
|
current_level[entry] = (pt_entry_t)(size_t)ret | 0b111;
|
2020-09-11 11:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-11 15:09:15 +03:00
|
|
|
pagemap_t new_pagemap(int lv) {
|
|
|
|
pagemap_t pagemap;
|
|
|
|
pagemap.levels = lv;
|
2021-04-15 03:21:38 +03:00
|
|
|
pagemap.top_level = ext_mem_alloc(PT_SIZE);
|
2020-09-11 15:09:15 +03:00
|
|
|
return pagemap;
|
2020-09-11 11:53:21 +03:00
|
|
|
}
|
|
|
|
|
2021-04-14 22:57:23 +03:00
|
|
|
void map_page(pagemap_t pagemap, uint64_t virt_addr, uint64_t phys_addr, uint64_t flags, bool hugepages) {
|
2020-09-11 11:53:21 +03:00
|
|
|
// Calculate the indices in the various tables using the virtual address
|
2020-09-11 15:09:15 +03:00
|
|
|
size_t pml5_entry = (virt_addr & ((uint64_t)0x1ff << 48)) >> 48;
|
2020-09-11 11:53:21 +03:00
|
|
|
size_t pml4_entry = (virt_addr & ((uint64_t)0x1ff << 39)) >> 39;
|
|
|
|
size_t pml3_entry = (virt_addr & ((uint64_t)0x1ff << 30)) >> 30;
|
|
|
|
size_t pml2_entry = (virt_addr & ((uint64_t)0x1ff << 21)) >> 21;
|
2021-04-14 22:57:23 +03:00
|
|
|
size_t pml1_entry = (virt_addr & ((uint64_t)0x1ff << 12)) >> 12;
|
2020-09-11 11:53:21 +03:00
|
|
|
|
2021-04-14 22:57:23 +03:00
|
|
|
pt_entry_t *pml5, *pml4, *pml3, *pml2, *pml1;
|
2020-09-11 15:09:15 +03:00
|
|
|
|
|
|
|
// Paging levels
|
|
|
|
switch (pagemap.levels) {
|
|
|
|
case 5:
|
|
|
|
pml5 = pagemap.top_level;
|
2020-11-16 23:23:11 +03:00
|
|
|
goto level5;
|
2020-09-11 15:09:15 +03:00
|
|
|
case 4:
|
|
|
|
pml4 = pagemap.top_level;
|
2020-11-16 23:23:11 +03:00
|
|
|
goto level4;
|
|
|
|
default:
|
|
|
|
panic("");
|
2020-09-11 15:09:15 +03:00
|
|
|
}
|
|
|
|
|
2020-11-16 23:23:11 +03:00
|
|
|
level5:
|
|
|
|
pml4 = get_next_level(pml5, pml5_entry);
|
|
|
|
level4:
|
2020-09-11 15:09:15 +03:00
|
|
|
pml3 = get_next_level(pml4, pml4_entry);
|
|
|
|
pml2 = get_next_level(pml3, pml3_entry);
|
2020-09-11 11:53:21 +03:00
|
|
|
|
2021-04-14 22:57:23 +03:00
|
|
|
if (hugepages) {
|
|
|
|
pml2[pml2_entry] = (pt_entry_t)(phys_addr | flags | (1 << 7));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pml1 = get_next_level(pml2, pml2_entry);
|
|
|
|
|
2021-04-20 07:11:02 +03:00
|
|
|
pml1[pml1_entry] = (pt_entry_t)(phys_addr | flags);
|
2020-09-11 11:53:21 +03:00
|
|
|
}
|