HelenOS sources
This source file includes following definitions.
- find_mapping_and_check
- pht_insert
- pht_refill
- pht_invalidate
#include <arch/mm/pht.h>
#include <arch/mm/tlb.h>
#include <assert.h>
#include <interrupt.h>
#include <mm/as.h>
#include <mm/page.h>
#include <macros.h>
#include <typedefs.h>
static unsigned int seed = 42;
static bool find_mapping_and_check(as_t *as, uintptr_t badvaddr, int access,
istate_t *istate, pte_t *pte)
{
bool found = page_mapping_find(as, badvaddr, true, pte);
if (found && pte->present) {
return true;
}
if (as_page_fault(badvaddr, access, istate) == AS_PF_OK) {
found = page_mapping_find(as, badvaddr, true, pte);
assert(found);
assert(pte->present);
return found;
}
return false;
}
static void pht_insert(const uintptr_t vaddr, const pte_t *pte)
{
uint32_t page = (vaddr >> 12) & 0xffff;
uint32_t api = (vaddr >> 22) & 0x3f;
uint32_t vsid = sr_get(vaddr);
uint32_t sdr1 = sdr1_get();
phte_t *phte = (phte_t *) PA2KA(sdr1 & 0xffff0000);
uint32_t h = 0;
uint32_t hash = vsid ^ page;
uint32_t base = (hash & 0x3ff) << 3;
uint32_t i;
bool found = false;
for (i = 0; i < 8; i++) {
if ((phte[base + i].v) &&
(phte[base + i].vsid == vsid) &&
(phte[base + i].api == api) &&
(phte[base + i].h == 0)) {
found = true;
break;
}
}
if (!found) {
for (i = 0; i < 8; i++) {
if (!phte[base + i].v) {
found = true;
break;
}
}
}
if (!found) {
uint32_t base2 = (~hash & 0x3ff) << 3;
for (i = 0; i < 8; i++) {
if ((phte[base2 + i].v) &&
(phte[base2 + i].vsid == vsid) &&
(phte[base2 + i].api == api) &&
(phte[base2 + i].h == 1)) {
found = true;
base = base2;
h = 1;
break;
}
}
if (!found) {
for (i = 0; i < 8; i++) {
if (!phte[base2 + i].v) {
found = true;
base = base2;
h = 1;
break;
}
}
}
if (!found)
i = RANDI(seed) % 8;
}
phte[base + i].v = 1;
phte[base + i].vsid = vsid;
phte[base + i].h = h;
phte[base + i].api = api;
phte[base + i].rpn = pte->pfn;
phte[base + i].r = 0;
phte[base + i].c = 0;
phte[base + i].wimg = (pte->page_cache_disable ? WIMG_NO_CACHE : 0);
phte[base + i].pp = 2;
}
void pht_refill(unsigned int n, istate_t *istate)
{
uintptr_t badvaddr;
if (n == VECTOR_DATA_STORAGE)
badvaddr = istate->dar;
else
badvaddr = istate->pc;
pte_t pte;
bool found = find_mapping_and_check(AS, badvaddr,
PF_ACCESS_READ , istate, &pte);
if (found) {
pte.accessed = 1;
pht_insert(badvaddr, &pte);
}
}
void pht_invalidate(as_t *as, uintptr_t page, size_t pages)
{
uint32_t sdr1 = sdr1_get();
phte_t *phte = (phte_t *) PA2KA(sdr1 & 0xffff0000);
for (size_t i = 0; i < 8192; i++) {
phte[i].v = 0;
}
}
HelenOS homepage, sources at GitHub