HelenOS sources
This source file includes following definitions.
- anon_create
- anon_resize
- anon_share
- anon_destroy
- anon_is_resizable
- anon_is_shareable
- anon_page_fault
- anon_frame_free
#include <assert.h>
#include <mm/as.h>
#include <mm/page.h>
#include <mm/reserve.h>
#include <genarch/mm/page_pt.h>
#include <genarch/mm/page_ht.h>
#include <mm/frame.h>
#include <mm/slab.h>
#include <mm/km.h>
#include <synch/mutex.h>
#include <adt/list.h>
#include <errno.h>
#include <typedefs.h>
#include <align.h>
#include <memw.h>
#include <arch.h>
static bool anon_create(as_area_t *);
static bool anon_resize(as_area_t *, size_t);
static void anon_share(as_area_t *);
static void anon_destroy(as_area_t *);
static bool anon_is_resizable(as_area_t *);
static bool anon_is_shareable(as_area_t *);
static int anon_page_fault(as_area_t *, uintptr_t, pf_access_t);
static void anon_frame_free(as_area_t *, uintptr_t, uintptr_t);
mem_backend_t anon_backend = {
.create = anon_create,
.resize = anon_resize,
.share = anon_share,
.destroy = anon_destroy,
.is_resizable = anon_is_resizable,
.is_shareable = anon_is_shareable,
.page_fault = anon_page_fault,
.frame_free = anon_frame_free,
.create_shared_data = NULL,
.destroy_shared_data = NULL
};
bool anon_create(as_area_t *area)
{
if (area->flags & AS_AREA_LATE_RESERVE)
return true;
return reserve_try_alloc(area->pages);
}
bool anon_resize(as_area_t *area, size_t new_pages)
{
if (area->flags & AS_AREA_LATE_RESERVE)
return true;
if (new_pages > area->pages)
return reserve_try_alloc(new_pages - area->pages);
else if (new_pages < area->pages)
reserve_free(area->pages - new_pages);
return true;
}
void anon_share(as_area_t *area)
{
assert(mutex_locked(&area->as->lock));
assert(mutex_locked(&area->lock));
assert(!(area->flags & AS_AREA_LATE_RESERVE));
mutex_lock(&area->sh_info->lock);
used_space_ival_t *ival = used_space_first(&area->used_space);
while (ival != NULL) {
uintptr_t base = ival->page;
size_t count = ival->count;
unsigned int j;
for (j = 0; j < count; j++) {
pte_t pte;
bool found;
page_table_lock(area->as, false);
found = page_mapping_find(area->as, base + P2SZ(j),
false, &pte);
(void)found;
assert(found);
assert(PTE_VALID(&pte));
assert(PTE_PRESENT(&pte));
as_pagemap_insert(&area->sh_info->pagemap,
(base + P2SZ(j)) - area->base, PTE_GET_FRAME(&pte));
page_table_unlock(area->as, false);
pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(&pte));
frame_reference_add(pfn);
}
ival = used_space_next(ival);
}
mutex_unlock(&area->sh_info->lock);
}
void anon_destroy(as_area_t *area)
{
if (area->flags & AS_AREA_LATE_RESERVE)
return;
reserve_free(area->pages);
}
bool anon_is_resizable(as_area_t *area)
{
return true;
}
bool anon_is_shareable(as_area_t *area)
{
return !(area->flags & AS_AREA_LATE_RESERVE);
}
int anon_page_fault(as_area_t *area, uintptr_t upage, pf_access_t access)
{
uintptr_t kpage;
uintptr_t frame;
assert(page_table_locked(AS));
assert(mutex_locked(&area->lock));
assert(IS_ALIGNED(upage, PAGE_SIZE));
if (!as_area_check_access(area, access))
return AS_PF_FAULT;
mutex_lock(&area->sh_info->lock);
if (area->sh_info->shared) {
errno_t rc = as_pagemap_find(&area->sh_info->pagemap,
upage - area->base, &frame);
if (rc != EOK) {
kpage = km_temporary_page_get(&frame,
FRAME_NO_RESERVE);
memsetb((void *) kpage, PAGE_SIZE, 0);
km_temporary_page_put(kpage);
as_pagemap_insert(&area->sh_info->pagemap,
upage - area->base, frame);
}
frame_reference_add(ADDR2PFN(frame));
} else {
if (area->flags & AS_AREA_LATE_RESERVE) {
if (!reserve_try_alloc(1)) {
mutex_unlock(&area->sh_info->lock);
return AS_PF_SILENT;
}
}
kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
memsetb((void *) kpage, PAGE_SIZE, 0);
km_temporary_page_put(kpage);
}
mutex_unlock(&area->sh_info->lock);
page_mapping_insert(AS, upage, frame, as_area_get_flags(area));
if (!used_space_insert(&area->used_space, upage, 1))
panic("Cannot insert used space.");
return AS_PF_OK;
}
void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
{
assert(page_table_locked(area->as));
assert(mutex_locked(&area->lock));
if (area->flags & AS_AREA_LATE_RESERVE) {
frame_free(frame, 1);
} else {
frame_free_noreserve(frame, 1);
}
}
HelenOS homepage, sources at GitHub