HelenOS sources
This source file includes following definitions.
- pt_mapping_insert
- pt_mapping_remove
- pt_mapping_find_internal
- pt_mapping_find
- pt_mapping_update
- ptl0_step_get
- pt_mapping_make_global
#include <assert.h>
#include <genarch/mm/page_pt.h>
#include <mm/page.h>
#include <mm/frame.h>
#include <mm/km.h>
#include <mm/as.h>
#include <arch/mm/page.h>
#include <arch/mm/as.h>
#include <barrier.h>
#include <typedefs.h>
#include <arch/asm.h>
#include <memw.h>
#include <align.h>
#include <macros.h>
#include <bitops.h>
static void pt_mapping_insert(as_t *, uintptr_t, uintptr_t, unsigned int);
static void pt_mapping_remove(as_t *, uintptr_t);
static bool pt_mapping_find(as_t *, uintptr_t, bool, pte_t *pte);
static void pt_mapping_update(as_t *, uintptr_t, bool, pte_t *pte);
static void pt_mapping_make_global(uintptr_t, size_t);
const page_mapping_operations_t pt_mapping_operations = {
.mapping_insert = pt_mapping_insert,
.mapping_remove = pt_mapping_remove,
.mapping_find = pt_mapping_find,
.mapping_update = pt_mapping_update,
.mapping_make_global = pt_mapping_make_global
};
void pt_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
unsigned int flags)
{
pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
assert(page_table_locked(as));
if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) {
pte_t *newpt = (pte_t *)
PA2KA(frame_alloc(PTL1_FRAMES, FRAME_LOWMEM, PTL1_SIZE - 1));
memsetb(newpt, PTL1_SIZE, 0);
SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page), KA2PA(newpt));
SET_PTL1_FLAGS(ptl0, PTL0_INDEX(page),
PAGE_NOT_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE |
PAGE_WRITE);
write_barrier();
SET_PTL1_PRESENT(ptl0, PTL0_INDEX(page));
}
pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) {
pte_t *newpt = (pte_t *)
PA2KA(frame_alloc(PTL2_FRAMES, FRAME_LOWMEM, PTL2_SIZE - 1));
memsetb(newpt, PTL2_SIZE, 0);
SET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page), KA2PA(newpt));
SET_PTL2_FLAGS(ptl1, PTL1_INDEX(page),
PAGE_NOT_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE |
PAGE_WRITE);
write_barrier();
SET_PTL2_PRESENT(ptl1, PTL1_INDEX(page));
}
pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) {
pte_t *newpt = (pte_t *)
PA2KA(frame_alloc(PTL3_FRAMES, FRAME_LOWMEM, PTL2_SIZE - 1));
memsetb(newpt, PTL2_SIZE, 0);
SET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page), KA2PA(newpt));
SET_PTL3_FLAGS(ptl2, PTL2_INDEX(page),
PAGE_NOT_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE |
PAGE_WRITE);
write_barrier();
SET_PTL3_PRESENT(ptl2, PTL2_INDEX(page));
}
pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
SET_FRAME_ADDRESS(ptl3, PTL3_INDEX(page), frame);
SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), flags | PAGE_NOT_PRESENT);
write_barrier();
SET_FRAME_PRESENT(ptl3, PTL3_INDEX(page));
}
void pt_mapping_remove(as_t *as, uintptr_t page)
{
assert(page_table_locked(as));
pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
return;
pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
return;
pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
return;
pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), PAGE_NOT_PRESENT);
memsetb(&ptl3[PTL3_INDEX(page)], sizeof(pte_t), 0);
bool empty = true;
unsigned int i;
for (i = 0; i < PTL3_ENTRIES; i++) {
if (PTE_VALID(&ptl3[i])) {
empty = false;
break;
}
}
if (empty) {
#if (PTL2_ENTRIES != 0)
memsetb(&ptl2[PTL2_INDEX(page)], sizeof(pte_t), 0);
#elif (PTL1_ENTRIES != 0)
memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
#else
if (km_is_non_identity(page))
return;
memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
#endif
frame_free(KA2PA((uintptr_t) ptl3), PTL3_FRAMES);
} else {
return;
}
#if (PTL2_ENTRIES != 0)
for (i = 0; i < PTL2_ENTRIES; i++) {
if (PTE_VALID(&ptl2[i])) {
empty = false;
break;
}
}
if (empty) {
#if (PTL1_ENTRIES != 0)
memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
#else
if (km_is_non_identity(page))
return;
memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
#endif
frame_free(KA2PA((uintptr_t) ptl2), PTL2_FRAMES);
} else {
return;
}
#endif
#if (PTL1_ENTRIES != 0)
for (i = 0; i < PTL1_ENTRIES; i++) {
if (PTE_VALID(&ptl1[i])) {
empty = false;
break;
}
}
if (empty) {
if (km_is_non_identity(page))
return;
memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
frame_free(KA2PA((uintptr_t) ptl1), PTL1_FRAMES);
}
#endif
}
static pte_t *pt_mapping_find_internal(as_t *as, uintptr_t page, bool nolock)
{
assert(nolock || page_table_locked(as));
pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
return NULL;
read_barrier();
pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
return NULL;
#if (PTL1_ENTRIES != 0)
read_barrier();
#endif
pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
return NULL;
#if (PTL2_ENTRIES != 0)
read_barrier();
#endif
pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
return &ptl3[PTL3_INDEX(page)];
}
bool pt_mapping_find(as_t *as, uintptr_t page, bool nolock, pte_t *pte)
{
pte_t *t = pt_mapping_find_internal(as, page, nolock);
if (t)
*pte = *t;
return t != NULL;
}
void pt_mapping_update(as_t *as, uintptr_t page, bool nolock, pte_t *pte)
{
pte_t *t = pt_mapping_find_internal(as, page, nolock);
if (!t)
panic("Updating non-existent PTE");
assert(PTE_VALID(t) == PTE_VALID(pte));
assert(PTE_PRESENT(t) == PTE_PRESENT(pte));
assert(PTE_GET_FRAME(t) == PTE_GET_FRAME(pte));
assert(PTE_WRITABLE(t) == PTE_WRITABLE(pte));
assert(PTE_EXECUTABLE(t) == PTE_EXECUTABLE(pte));
*t = *pte;
}
static uintptr_t ptl0_step_get(void)
{
size_t va_bits;
va_bits = fnzb(PTL0_ENTRIES) + fnzb(PTL1_ENTRIES) + fnzb(PTL2_ENTRIES) +
fnzb(PTL3_ENTRIES) + PAGE_WIDTH;
return 1UL << (va_bits - fnzb(PTL0_ENTRIES));
}
void pt_mapping_make_global(uintptr_t base, size_t size)
{
assert(size > 0);
uintptr_t ptl0 = PA2KA((uintptr_t) AS_KERNEL->genarch.page_table);
uintptr_t ptl0_step = ptl0_step_get();
size_t frames;
#if (PTL1_ENTRIES != 0)
frames = PTL1_FRAMES;
#elif (PTL2_ENTRIES != 0)
frames = PTL2_FRAMES;
#else
frames = PTL3_FRAMES;
#endif
for (uintptr_t addr = ALIGN_DOWN(base, ptl0_step);
addr - 1 < base + size - 1;
addr += ptl0_step) {
if (GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(addr)) != 0) {
assert(overlaps(addr, ptl0_step,
config.identity_base, config.identity_size));
continue;
}
uintptr_t l1 = PA2KA(frame_alloc(frames, FRAME_LOWMEM, 0));
memsetb((void *) l1, FRAMES2SIZE(frames), 0);
SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(addr), KA2PA(l1));
SET_PTL1_FLAGS(ptl0, PTL0_INDEX(addr),
PAGE_PRESENT | PAGE_USER | PAGE_CACHEABLE |
PAGE_EXEC | PAGE_WRITE | PAGE_READ);
}
}
HelenOS homepage, sources at GitHub