HelenOS sources
This source file includes following definitions.
- as_constructor
- as_destructor
- as_init
- as_create
- as_destroy
- as_hold
- as_release
- as_area_first
- as_area_next
- area_is_conflicting
- check_area_conflicts
- as_get_unmapped_area
- as_pagemap_getkey
- as_pagemap_cmp
- as_pagemap_initialize
- as_pagemap_finalize
- as_pagemap_first
- as_pagemap_next
- as_pagemap_find
- as_pagemap_insert
- as_pagemap_remove
- sh_info_remove_reference
- as_area_create
- find_area_and_lock
- as_area_resize
- as_area_destroy
- as_area_share
- as_area_check_access
- area_flags_to_page_flags
- as_area_change_flags
- as_page_fault
- as_switch
- as_area_get_flags
- as_areas_getkey
- as_areas_cmp
- page_table_create
- page_table_destroy
- page_table_lock
- page_table_unlock
- page_table_locked
- as_area_get_size
- used_space_initialize
- used_space_finalize
- used_space_first
- used_space_next
- used_space_last
- used_space_find_gteq
- used_space_getkey
- used_space_cmp
- used_space_remove_ival
- used_space_shorten_ival
- used_space_insert
- sys_as_area_create
- sys_as_area_resize
- sys_as_area_change_flags
- sys_as_area_get_info
- sys_as_area_destroy
- as_get_area_info
- as_print
#include <mm/as.h>
#include <arch/mm/as.h>
#include <mm/page.h>
#include <mm/frame.h>
#include <mm/slab.h>
#include <mm/tlb.h>
#include <arch/mm/page.h>
#include <genarch/mm/page_pt.h>
#include <genarch/mm/page_ht.h>
#include <mm/asid.h>
#include <arch/mm/asid.h>
#include <preemption.h>
#include <synch/spinlock.h>
#include <synch/mutex.h>
#include <adt/list.h>
#include <proc/task.h>
#include <proc/thread.h>
#include <arch/asm.h>
#include <panic.h>
#include <assert.h>
#include <stdio.h>
#include <memw.h>
#include <macros.h>
#include <bitops.h>
#include <arch.h>
#include <errno.h>
#include <config.h>
#include <align.h>
#include <typedefs.h>
#include <syscall/copy.h>
#include <arch/interrupt.h>
#include <interrupt.h>
#include <stdlib.h>
const as_operations_t *as_operations = NULL;
static slab_cache_t *as_cache;
static slab_cache_t *as_page_mapping_cache;
static slab_cache_t *used_space_ival_cache;
SPINLOCK_INITIALIZE(asidlock);
LIST_INITIALIZE(inactive_as_with_asid_list);
as_t *AS_KERNEL = NULL;
static void *as_areas_getkey(odlink_t *);
static int as_areas_cmp(void *, void *);
static void used_space_initialize(used_space_t *);
static void used_space_finalize(used_space_t *);
static void *used_space_getkey(odlink_t *);
static int used_space_cmp(void *, void *);
static used_space_ival_t *used_space_last(used_space_t *);
static void used_space_remove_ival(used_space_ival_t *);
static void used_space_shorten_ival(used_space_ival_t *, size_t);
_NO_TRACE static errno_t as_constructor(void *obj, unsigned int flags)
{
as_t *as = (as_t *) obj;
link_initialize(&as->inactive_as_with_asid_link);
mutex_initialize(&as->lock, MUTEX_PASSIVE);
return as_constructor_arch(as, flags);
}
_NO_TRACE static size_t as_destructor(void *obj)
{
return as_destructor_arch((as_t *) obj);
}
void as_init(void)
{
as_arch_init();
as_cache = slab_cache_create("as_t", sizeof(as_t), 0,
as_constructor, as_destructor, SLAB_CACHE_MAGDEFERRED);
as_page_mapping_cache = slab_cache_create("as_page_mapping_t",
sizeof(as_page_mapping_t), 0, NULL, NULL, SLAB_CACHE_MAGDEFERRED);
used_space_ival_cache = slab_cache_create("used_space_ival_t",
sizeof(used_space_ival_t), 0, NULL, NULL, SLAB_CACHE_MAGDEFERRED);
AS_KERNEL = as_create(FLAG_AS_KERNEL);
if (!AS_KERNEL)
panic("Cannot create kernel address space.");
}
as_t *as_create(unsigned int flags)
{
as_t *as = (as_t *) slab_alloc(as_cache, FRAME_ATOMIC);
if (!as)
return NULL;
(void) as_create_arch(as, 0);
odict_initialize(&as->as_areas, as_areas_getkey, as_areas_cmp);
if (flags & FLAG_AS_KERNEL)
as->asid = ASID_KERNEL;
else
as->asid = ASID_INVALID;
refcount_init(&as->refcount);
as->cpu_refcount = 0;
#ifdef AS_PAGE_TABLE
as->genarch.page_table = page_table_create(flags);
#else
page_table_create(flags);
#endif
return as;
}
static void as_destroy(as_t *as)
{
DEADLOCK_PROBE_INIT(p_asidlock);
assert(as != AS);
assert(refcount_unique(&as->refcount));
preemption_disable();
ipl_t ipl = interrupts_read();
retry:
interrupts_disable();
if (!spinlock_trylock(&asidlock)) {
interrupts_enable();
DEADLOCK_PROBE(p_asidlock, DEADLOCK_THRESHOLD);
goto retry;
}
preemption_enable();
if ((as->asid != ASID_INVALID) && (as != AS_KERNEL)) {
if (as->cpu_refcount == 0)
list_remove(&as->inactive_as_with_asid_link);
asid_put(as->asid);
}
spinlock_unlock(&asidlock);
interrupts_restore(ipl);
as_area_t *area = as_area_first(as);
while (area != NULL) {
as_area_destroy(as, area->base);
area = as_area_first(as);
}
odict_finalize(&as->as_areas);
#ifdef AS_PAGE_TABLE
page_table_destroy(as->genarch.page_table);
#else
page_table_destroy(NULL);
#endif
slab_free(as_cache, as);
}
_NO_TRACE void as_hold(as_t *as)
{
refcount_up(&as->refcount);
}
_NO_TRACE void as_release(as_t *as)
{
if (refcount_down(&as->refcount))
as_destroy(as);
}
as_area_t *as_area_first(as_t *as)
{
odlink_t *odlink = odict_first(&as->as_areas);
if (odlink == NULL)
return NULL;
return odict_get_instance(odlink, as_area_t, las_areas);
}
as_area_t *as_area_next(as_area_t *cur)
{
odlink_t *odlink = odict_next(&cur->las_areas, &cur->as->as_areas);
if (odlink == NULL)
return NULL;
return odict_get_instance(odlink, as_area_t, las_areas);
}
_NO_TRACE static bool area_is_conflicting(uintptr_t addr,
size_t count, bool guarded, as_area_t *area)
{
assert((addr % PAGE_SIZE) == 0);
size_t gsize = P2SZ(count);
size_t agsize = P2SZ(area->pages);
if (guarded || (area->flags & AS_AREA_GUARD) != 0) {
if (!overflows(addr, P2SZ(count)))
gsize += PAGE_SIZE;
if (!overflows(area->base, P2SZ(area->pages)))
agsize += PAGE_SIZE;
}
return overlaps(addr, gsize, area->base, agsize);
}
_NO_TRACE static bool check_area_conflicts(as_t *as, uintptr_t addr,
size_t count, bool guarded, as_area_t *avoid)
{
assert((addr % PAGE_SIZE) == 0);
assert(mutex_locked(&as->lock));
if (overflows_into_positive(addr, P2SZ(count)))
return false;
if (overlaps(addr, P2SZ(count), (uintptr_t) NULL, PAGE_SIZE))
return false;
odlink_t *odlink = odict_find_leq(&as->as_areas, &addr, NULL);
if (odlink != NULL) {
as_area_t *area = odict_get_instance(odlink, as_area_t,
las_areas);
if (area != avoid) {
mutex_lock(&area->lock);
if (area_is_conflicting(addr, count, guarded, area)) {
mutex_unlock(&area->lock);
return false;
}
mutex_unlock(&area->lock);
}
odlink = odict_next(odlink, &as->as_areas);
}
if (odlink == NULL)
odlink = odict_first(&as->as_areas);
if (odlink != NULL) {
as_area_t *area = odict_get_instance(odlink, as_area_t,
las_areas);
if (area != avoid) {
mutex_lock(&area->lock);
if (area_is_conflicting(addr, count, guarded, area)) {
mutex_unlock(&area->lock);
return false;
}
mutex_unlock(&area->lock);
}
}
if (!KERNEL_ADDRESS_SPACE_SHADOWED) {
return iswithin(USER_ADDRESS_SPACE_START,
(USER_ADDRESS_SPACE_END - USER_ADDRESS_SPACE_START) + 1,
addr, P2SZ(count));
}
return true;
}
_NO_TRACE static uintptr_t as_get_unmapped_area(as_t *as, uintptr_t bound,
size_t size, bool guarded)
{
assert(mutex_locked(&as->lock));
if (size == 0)
return (uintptr_t) -1;
size_t pages = SIZE2FRAMES(size);
uintptr_t addr = ALIGN_UP(bound, PAGE_SIZE);
if (addr >= bound) {
if (guarded) {
addr += P2SZ(1);
}
if (check_area_conflicts(as, addr, pages, guarded, NULL))
return addr;
}
as_area_t *area = as_area_first(as);
while (area != NULL) {
mutex_lock(&area->lock);
addr = area->base + P2SZ(area->pages);
if (guarded || area->flags & AS_AREA_GUARD) {
addr += P2SZ(1);
}
bool avail =
((addr >= bound) && (addr >= area->base) &&
(check_area_conflicts(as, addr, pages, guarded, area)));
mutex_unlock(&area->lock);
if (avail)
return addr;
area = as_area_next(area);
}
return (uintptr_t) -1;
}
static void *as_pagemap_getkey(odlink_t *odlink)
{
as_page_mapping_t *mapping;
mapping = odict_get_instance(odlink, as_page_mapping_t, lpagemap);
return (void *) &mapping->vaddr;
}
static int as_pagemap_cmp(void *a, void *b)
{
uintptr_t va = *(uintptr_t *)a;
uintptr_t vb = *(uintptr_t *)b;
if (va < vb)
return -1;
else if (va == vb)
return 0;
else
return +1;
}
_NO_TRACE void as_pagemap_initialize(as_pagemap_t *pagemap)
{
odict_initialize(&pagemap->map, as_pagemap_getkey, as_pagemap_cmp);
}
_NO_TRACE void as_pagemap_finalize(as_pagemap_t *pagemap)
{
as_page_mapping_t *mapping = as_pagemap_first(pagemap);
while (mapping != NULL) {
as_pagemap_remove(mapping);
mapping = as_pagemap_first(pagemap);
}
odict_finalize(&pagemap->map);
}
_NO_TRACE as_page_mapping_t *as_pagemap_first(as_pagemap_t *pagemap)
{
odlink_t *odlink;
odlink = odict_first(&pagemap->map);
if (odlink == NULL)
return NULL;
return odict_get_instance(odlink, as_page_mapping_t, lpagemap);
}
_NO_TRACE as_page_mapping_t *as_pagemap_next(as_page_mapping_t *cur)
{
odlink_t *odlink;
odlink = odict_next(&cur->lpagemap, &cur->pagemap->map);
if (odlink == NULL)
return NULL;
return odict_get_instance(odlink, as_page_mapping_t, lpagemap);
}
_NO_TRACE errno_t as_pagemap_find(as_pagemap_t *pagemap, uintptr_t vaddr,
uintptr_t *rframe)
{
odlink_t *odlink;
as_page_mapping_t *mapping;
odlink = odict_find_eq(&pagemap->map, &vaddr, NULL);
if (odlink == NULL)
return ENOENT;
mapping = odict_get_instance(odlink, as_page_mapping_t, lpagemap);
*rframe = mapping->frame;
return EOK;
}
_NO_TRACE void as_pagemap_insert(as_pagemap_t *pagemap, uintptr_t vaddr,
uintptr_t frame)
{
as_page_mapping_t *mapping;
mapping = slab_alloc(as_page_mapping_cache, 0);
mapping->pagemap = pagemap;
odlink_initialize(&mapping->lpagemap);
mapping->vaddr = vaddr;
mapping->frame = frame;
odict_insert(&mapping->lpagemap, &pagemap->map, NULL);
}
_NO_TRACE void as_pagemap_remove(as_page_mapping_t *mapping)
{
odict_remove(&mapping->lpagemap);
slab_free(as_page_mapping_cache, mapping);
}
_NO_TRACE static void sh_info_remove_reference(share_info_t *sh_info)
{
bool dealloc = false;
mutex_lock(&sh_info->lock);
assert(sh_info->refcount);
if (--sh_info->refcount == 0) {
dealloc = true;
as_page_mapping_t *mapping = as_pagemap_first(&sh_info->pagemap);
while (mapping != NULL) {
frame_free(mapping->frame, 1);
mapping = as_pagemap_next(mapping);
}
}
mutex_unlock(&sh_info->lock);
if (dealloc) {
if (sh_info->backend && sh_info->backend->destroy_shared_data) {
sh_info->backend->destroy_shared_data(
sh_info->backend_shared_data);
}
as_pagemap_finalize(&sh_info->pagemap);
free(sh_info);
}
}
as_area_t *as_area_create(as_t *as, unsigned int flags, size_t size,
unsigned int attrs, mem_backend_t *backend,
mem_backend_data_t *backend_data, uintptr_t *base, uintptr_t bound)
{
if ((*base != (uintptr_t) AS_AREA_ANY) && !IS_ALIGNED(*base, PAGE_SIZE))
return NULL;
if (size == 0)
return NULL;
size_t pages = SIZE2FRAMES(size);
if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE))
return NULL;
bool const guarded = flags & AS_AREA_GUARD;
mutex_lock(&as->lock);
if (*base == (uintptr_t) AS_AREA_ANY) {
*base = as_get_unmapped_area(as, bound, size, guarded);
if (*base == (uintptr_t) -1) {
mutex_unlock(&as->lock);
return NULL;
}
}
if (overflows_into_positive(*base, size)) {
mutex_unlock(&as->lock);
return NULL;
}
if (!check_area_conflicts(as, *base, pages, guarded, NULL)) {
mutex_unlock(&as->lock);
return NULL;
}
as_area_t *area = (as_area_t *) malloc(sizeof(as_area_t));
if (!area) {
mutex_unlock(&as->lock);
return NULL;
}
mutex_initialize(&area->lock, MUTEX_PASSIVE);
area->as = as;
odlink_initialize(&area->las_areas);
area->flags = flags;
area->attributes = attrs;
area->pages = pages;
area->base = *base;
area->backend = backend;
area->sh_info = NULL;
if (backend_data)
area->backend_data = *backend_data;
else
memsetb(&area->backend_data, sizeof(area->backend_data), 0);
share_info_t *si = NULL;
if (!(attrs & AS_AREA_ATTR_PARTIAL)) {
si = (share_info_t *) malloc(sizeof(share_info_t));
if (!si) {
free(area);
mutex_unlock(&as->lock);
return NULL;
}
mutex_initialize(&si->lock, MUTEX_PASSIVE);
si->refcount = 1;
si->shared = false;
si->backend_shared_data = NULL;
si->backend = backend;
as_pagemap_initialize(&si->pagemap);
area->sh_info = si;
if (area->backend && area->backend->create_shared_data) {
if (!area->backend->create_shared_data(area)) {
free(area);
mutex_unlock(&as->lock);
sh_info_remove_reference(si);
return NULL;
}
}
}
if (area->backend && area->backend->create) {
if (!area->backend->create(area)) {
free(area);
mutex_unlock(&as->lock);
if (!(attrs & AS_AREA_ATTR_PARTIAL))
sh_info_remove_reference(si);
return NULL;
}
}
used_space_initialize(&area->used_space);
odict_insert(&area->las_areas, &as->as_areas, NULL);
mutex_unlock(&as->lock);
return area;
}
_NO_TRACE static as_area_t *find_area_and_lock(as_t *as, uintptr_t va)
{
assert(mutex_locked(&as->lock));
odlink_t *odlink = odict_find_leq(&as->as_areas, &va, NULL);
if (odlink == NULL)
return NULL;
as_area_t *area = odict_get_instance(odlink, as_area_t, las_areas);
mutex_lock(&area->lock);
assert(area->base <= va);
if (va <= area->base + (P2SZ(area->pages) - 1))
return area;
mutex_unlock(&area->lock);
return NULL;
}
errno_t as_area_resize(as_t *as, uintptr_t address, size_t size, unsigned int flags)
{
if (!IS_ALIGNED(address, PAGE_SIZE))
return EINVAL;
mutex_lock(&as->lock);
as_area_t *area = find_area_and_lock(as, address);
if (!area) {
mutex_unlock(&as->lock);
return ENOENT;
}
if (!area->backend->is_resizable(area)) {
mutex_unlock(&area->lock);
mutex_unlock(&as->lock);
return ENOTSUP;
}
mutex_lock(&area->sh_info->lock);
if (area->sh_info->shared) {
mutex_unlock(&area->sh_info->lock);
mutex_unlock(&area->lock);
mutex_unlock(&as->lock);
return ENOTSUP;
}
mutex_unlock(&area->sh_info->lock);
size_t pages = SIZE2FRAMES((address - area->base) + size);
if (!pages) {
mutex_unlock(&area->lock);
mutex_unlock(&as->lock);
return EPERM;
}
if (pages < area->pages) {
uintptr_t start_free = area->base + P2SZ(pages);
page_table_lock(as, false);
ipl_t ipl = tlb_shootdown_start(TLB_INVL_PAGES,
as->asid, area->base + P2SZ(pages),
area->pages - pages);
bool cond = true;
while (cond) {
used_space_ival_t *ival =
used_space_last(&area->used_space);
assert(ival != NULL);
uintptr_t ptr = ival->page;
size_t pcount = ival->count;
size_t i = 0;
if (overlaps(ptr, P2SZ(pcount), area->base,
P2SZ(pages))) {
if (ptr + P2SZ(pcount) <= start_free) {
break;
}
cond = false;
i = (start_free - ptr) >> PAGE_WIDTH;
used_space_shorten_ival(ival, i);
} else {
used_space_remove_ival(ival);
}
for (; i < pcount; i++) {
pte_t pte;
bool found = page_mapping_find(as,
ptr + P2SZ(i), false, &pte);
(void) found;
assert(found);
assert(PTE_VALID(&pte));
assert(PTE_PRESENT(&pte));
if ((area->backend) &&
(area->backend->frame_free)) {
area->backend->frame_free(area,
ptr + P2SZ(i),
PTE_GET_FRAME(&pte));
}
page_mapping_remove(as, ptr + P2SZ(i));
}
}
tlb_invalidate_pages(as->asid,
area->base + P2SZ(pages),
area->pages - pages);
as_invalidate_translation_cache(as,
area->base + P2SZ(pages),
area->pages - pages);
tlb_shootdown_finalize(ipl);
page_table_unlock(as, false);
} else {
if (overflows_into_positive(address, P2SZ(pages)))
return EINVAL;
bool const guarded = area->flags & AS_AREA_GUARD;
if (!check_area_conflicts(as, address, pages, guarded, area)) {
mutex_unlock(&area->lock);
mutex_unlock(&as->lock);
return EADDRNOTAVAIL;
}
}
if (area->backend && area->backend->resize) {
if (!area->backend->resize(area, pages)) {
mutex_unlock(&area->lock);
mutex_unlock(&as->lock);
return ENOMEM;
}
}
area->pages = pages;
mutex_unlock(&area->lock);
mutex_unlock(&as->lock);
return 0;
}
errno_t as_area_destroy(as_t *as, uintptr_t address)
{
mutex_lock(&as->lock);
as_area_t *area = find_area_and_lock(as, address);
if (!area) {
mutex_unlock(&as->lock);
return ENOENT;
}
if (area->backend && area->backend->destroy)
area->backend->destroy(area);
page_table_lock(as, false);
ipl_t ipl = tlb_shootdown_start(TLB_INVL_PAGES, as->asid, area->base,
area->pages);
used_space_ival_t *ival = used_space_first(&area->used_space);
while (ival != NULL) {
uintptr_t ptr = ival->page;
for (size_t size = 0; size < ival->count; size++) {
pte_t pte;
bool found = page_mapping_find(as,
ptr + P2SZ(size), false, &pte);
(void) found;
assert(found);
assert(PTE_VALID(&pte));
assert(PTE_PRESENT(&pte));
if ((area->backend) &&
(area->backend->frame_free)) {
area->backend->frame_free(area,
ptr + P2SZ(size),
PTE_GET_FRAME(&pte));
}
page_mapping_remove(as, ptr + P2SZ(size));
}
used_space_remove_ival(ival);
ival = used_space_first(&area->used_space);
}
tlb_invalidate_pages(as->asid, area->base, area->pages);
as_invalidate_translation_cache(as, area->base, area->pages);
tlb_shootdown_finalize(ipl);
page_table_unlock(as, false);
used_space_finalize(&area->used_space);
area->attributes |= AS_AREA_ATTR_PARTIAL;
sh_info_remove_reference(area->sh_info);
mutex_unlock(&area->lock);
odict_remove(&area->las_areas);
free(area);
mutex_unlock(&as->lock);
return 0;
}
errno_t as_area_share(as_t *src_as, uintptr_t src_base, size_t acc_size,
as_t *dst_as, unsigned int dst_flags_mask, uintptr_t *dst_base,
uintptr_t bound)
{
mutex_lock(&src_as->lock);
as_area_t *src_area = find_area_and_lock(src_as, src_base);
if (!src_area) {
mutex_unlock(&src_as->lock);
return ENOENT;
}
if (!src_area->backend->is_shareable(src_area)) {
mutex_unlock(&src_area->lock);
mutex_unlock(&src_as->lock);
return ENOTSUP;
}
size_t src_size = P2SZ(src_area->pages);
unsigned int src_flags = src_area->flags;
mem_backend_t *src_backend = src_area->backend;
mem_backend_data_t src_backend_data = src_area->backend_data;
if (src_flags & AS_AREA_CACHEABLE)
dst_flags_mask |= AS_AREA_CACHEABLE;
if ((src_size != acc_size) ||
((src_flags & dst_flags_mask) != dst_flags_mask)) {
mutex_unlock(&src_area->lock);
mutex_unlock(&src_as->lock);
return EPERM;
}
share_info_t *sh_info = src_area->sh_info;
mutex_lock(&sh_info->lock);
sh_info->refcount++;
bool shared = sh_info->shared;
sh_info->shared = true;
mutex_unlock(&sh_info->lock);
if (!shared) {
src_area->backend->share(src_area);
}
mutex_unlock(&src_area->lock);
mutex_unlock(&src_as->lock);
as_area_t *dst_area = as_area_create(dst_as, dst_flags_mask,
src_size, AS_AREA_ATTR_PARTIAL, src_backend,
&src_backend_data, dst_base, bound);
if (!dst_area) {
sh_info_remove_reference(sh_info);
return ENOMEM;
}
mutex_lock(&dst_as->lock);
mutex_lock(&dst_area->lock);
dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL;
dst_area->sh_info = sh_info;
mutex_unlock(&dst_area->lock);
mutex_unlock(&dst_as->lock);
return 0;
}
_NO_TRACE bool as_area_check_access(as_area_t *area, pf_access_t access)
{
assert(mutex_locked(&area->lock));
int flagmap[] = {
[PF_ACCESS_READ] = AS_AREA_READ,
[PF_ACCESS_WRITE] = AS_AREA_WRITE,
[PF_ACCESS_EXEC] = AS_AREA_EXEC
};
if (!(area->flags & flagmap[access]))
return false;
return true;
}
_NO_TRACE static unsigned int area_flags_to_page_flags(unsigned int aflags)
{
unsigned int flags = PAGE_USER | PAGE_PRESENT;
if (aflags & AS_AREA_READ)
flags |= PAGE_READ;
if (aflags & AS_AREA_WRITE)
flags |= PAGE_WRITE;
if (aflags & AS_AREA_EXEC)
flags |= PAGE_EXEC;
if (aflags & AS_AREA_CACHEABLE)
flags |= PAGE_CACHEABLE;
return flags;
}
errno_t as_area_change_flags(as_t *as, unsigned int flags, uintptr_t address)
{
unsigned int page_flags = area_flags_to_page_flags(flags);
mutex_lock(&as->lock);
as_area_t *area = find_area_and_lock(as, address);
if (!area) {
mutex_unlock(&as->lock);
return ENOENT;
}
if (area->backend != &anon_backend) {
mutex_unlock(&area->lock);
mutex_unlock(&as->lock);
return ENOTSUP;
}
mutex_lock(&area->sh_info->lock);
if (area->sh_info->shared) {
mutex_unlock(&area->sh_info->lock);
mutex_unlock(&area->lock);
mutex_unlock(&as->lock);
return ENOTSUP;
}
mutex_unlock(&area->sh_info->lock);
uintptr_t *old_frame = malloc(area->used_space.pages *
sizeof(uintptr_t));
if (!old_frame) {
mutex_unlock(&area->lock);
mutex_unlock(&as->lock);
return ENOMEM;
}
page_table_lock(as, false);
ipl_t ipl = tlb_shootdown_start(TLB_INVL_PAGES, as->asid, area->base,
area->pages);
size_t frame_idx = 0;
used_space_ival_t *ival = used_space_first(&area->used_space);
while (ival != NULL) {
uintptr_t ptr = ival->page;
size_t size;
for (size = 0; size < ival->count; size++) {
pte_t pte;
bool found = page_mapping_find(as, ptr + P2SZ(size),
false, &pte);
(void) found;
assert(found);
assert(PTE_VALID(&pte));
assert(PTE_PRESENT(&pte));
old_frame[frame_idx++] = PTE_GET_FRAME(&pte);
page_mapping_remove(as, ptr + P2SZ(size));
}
ival = used_space_next(ival);
}
tlb_invalidate_pages(as->asid, area->base, area->pages);
as_invalidate_translation_cache(as, area->base, area->pages);
tlb_shootdown_finalize(ipl);
page_table_unlock(as, false);
area->flags = flags;
frame_idx = 0;
ival = used_space_first(&area->used_space);
while (ival != NULL) {
uintptr_t ptr = ival->page;
size_t size;
for (size = 0; size < ival->count; size++) {
page_table_lock(as, false);
page_mapping_insert(as, ptr + P2SZ(size),
old_frame[frame_idx++], page_flags);
page_table_unlock(as, false);
}
ival = used_space_next(ival);
}
free(old_frame);
mutex_unlock(&area->lock);
mutex_unlock(&as->lock);
return 0;
}
int as_page_fault(uintptr_t address, pf_access_t access, istate_t *istate)
{
uintptr_t page = ALIGN_DOWN(address, PAGE_SIZE);
int rc = AS_PF_FAULT;
if (!THREAD)
goto page_fault;
if (!AS)
goto page_fault;
mutex_lock(&AS->lock);
as_area_t *area = find_area_and_lock(AS, page);
if (!area) {
mutex_unlock(&AS->lock);
goto page_fault;
}
if (area->attributes & AS_AREA_ATTR_PARTIAL) {
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
goto page_fault;
}
if ((!area->backend) || (!area->backend->page_fault)) {
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
goto page_fault;
}
page_table_lock(AS, false);
pte_t pte;
bool found = page_mapping_find(AS, page, false, &pte);
if (found && PTE_PRESENT(&pte)) {
if (((access == PF_ACCESS_READ) && PTE_READABLE(&pte)) ||
(access == PF_ACCESS_WRITE && PTE_WRITABLE(&pte)) ||
(access == PF_ACCESS_EXEC && PTE_EXECUTABLE(&pte))) {
page_table_unlock(AS, false);
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
return AS_PF_OK;
}
}
rc = area->backend->page_fault(area, page, access);
if (rc != AS_PF_OK) {
page_table_unlock(AS, false);
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
goto page_fault;
}
page_table_unlock(AS, false);
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
return AS_PF_OK;
page_fault:
if (THREAD && THREAD->in_copy_from_uspace) {
THREAD->in_copy_from_uspace = false;
istate_set_retaddr(istate,
(uintptr_t) &memcpy_from_uspace_failover_address);
} else if (THREAD && THREAD->in_copy_to_uspace) {
THREAD->in_copy_to_uspace = false;
istate_set_retaddr(istate,
(uintptr_t) &memcpy_to_uspace_failover_address);
} else if (rc == AS_PF_SILENT) {
printf("Killing task %" PRIu64 " due to a "
"failed late reservation request.\n", TASK->taskid);
task_kill_self(true);
} else {
fault_if_from_uspace(istate, "Page fault: %p.", (void *) address);
panic_memtrap(istate, access, address, NULL);
}
return AS_PF_DEFER;
}
void as_switch(as_t *old_as, as_t *new_as)
{
DEADLOCK_PROBE_INIT(p_asidlock);
preemption_disable();
retry:
(void) interrupts_disable();
if (!spinlock_trylock(&asidlock)) {
(void) interrupts_enable();
DEADLOCK_PROBE(p_asidlock, DEADLOCK_THRESHOLD);
goto retry;
}
preemption_enable();
if (old_as) {
assert(old_as->cpu_refcount);
if ((--old_as->cpu_refcount == 0) && (old_as != AS_KERNEL)) {
assert(old_as->asid != ASID_INVALID);
list_append(&old_as->inactive_as_with_asid_link,
&inactive_as_with_asid_list);
}
as_deinstall_arch(old_as);
}
if ((new_as->cpu_refcount++ == 0) && (new_as != AS_KERNEL)) {
if (new_as->asid != ASID_INVALID)
list_remove(&new_as->inactive_as_with_asid_link);
else
new_as->asid = asid_get();
}
#ifdef AS_PAGE_TABLE
SET_PTL0_ADDRESS(new_as->genarch.page_table);
#endif
as_install_arch(new_as);
spinlock_unlock(&asidlock);
if (AS)
as_release(AS);
AS = new_as;
as_hold(AS);
}
_NO_TRACE unsigned int as_area_get_flags(as_area_t *area)
{
assert(mutex_locked(&area->lock));
return area_flags_to_page_flags(area->flags);
}
static void *as_areas_getkey(odlink_t *odlink)
{
as_area_t *area = odict_get_instance(odlink, as_area_t, las_areas);
return (void *) &area->base;
}
static int as_areas_cmp(void *a, void *b)
{
uintptr_t base_a = *(uintptr_t *)a;
uintptr_t base_b = *(uintptr_t *)b;
if (base_a < base_b)
return -1;
else if (base_a == base_b)
return 0;
else
return +1;
}
_NO_TRACE pte_t *page_table_create(unsigned int flags)
{
assert(as_operations);
assert(as_operations->page_table_create);
return as_operations->page_table_create(flags);
}
_NO_TRACE void page_table_destroy(pte_t *page_table)
{
assert(as_operations);
assert(as_operations->page_table_destroy);
as_operations->page_table_destroy(page_table);
}
_NO_TRACE void page_table_lock(as_t *as, bool lock)
{
assert(as_operations);
assert(as_operations->page_table_lock);
as_operations->page_table_lock(as, lock);
}
_NO_TRACE void page_table_unlock(as_t *as, bool unlock)
{
assert(as_operations);
assert(as_operations->page_table_unlock);
as_operations->page_table_unlock(as, unlock);
}
_NO_TRACE bool page_table_locked(as_t *as)
{
assert(as_operations);
assert(as_operations->page_table_locked);
return as_operations->page_table_locked(as);
}
size_t as_area_get_size(uintptr_t base)
{
size_t size;
page_table_lock(AS, true);
as_area_t *src_area = find_area_and_lock(AS, base);
if (src_area) {
size = P2SZ(src_area->pages);
mutex_unlock(&src_area->lock);
} else
size = 0;
page_table_unlock(AS, true);
return size;
}
static void used_space_initialize(used_space_t *used_space)
{
odict_initialize(&used_space->ivals, used_space_getkey, used_space_cmp);
used_space->pages = 0;
}
static void used_space_finalize(used_space_t *used_space)
{
assert(odict_empty(&used_space->ivals));
odict_finalize(&used_space->ivals);
}
used_space_ival_t *used_space_first(used_space_t *used_space)
{
odlink_t *odlink = odict_first(&used_space->ivals);
if (odlink == NULL)
return NULL;
return odict_get_instance(odlink, used_space_ival_t, lused_space);
}
used_space_ival_t *used_space_next(used_space_ival_t *cur)
{
odlink_t *odlink = odict_next(&cur->lused_space,
&cur->used_space->ivals);
if (odlink == NULL)
return NULL;
return odict_get_instance(odlink, used_space_ival_t, lused_space);
}
static used_space_ival_t *used_space_last(used_space_t *used_space)
{
odlink_t *odlink = odict_last(&used_space->ivals);
if (odlink == NULL)
return NULL;
return odict_get_instance(odlink, used_space_ival_t, lused_space);
}
used_space_ival_t *used_space_find_gteq(used_space_t *used_space, uintptr_t ptr)
{
odlink_t *odlink;
used_space_ival_t *ival;
odlink = odict_find_lt(&used_space->ivals, &ptr, NULL);
if (odlink != NULL) {
ival = odict_get_instance(odlink, used_space_ival_t,
lused_space);
if (ival->page + P2SZ(ival->count) > ptr)
return ival;
odlink = odict_next(&ival->lused_space, &used_space->ivals);
} else {
odlink = odict_first(&used_space->ivals);
}
if (odlink != NULL) {
ival = odict_get_instance(odlink, used_space_ival_t,
lused_space);
return ival;
}
return NULL;
}
static void *used_space_getkey(odlink_t *odlink)
{
used_space_ival_t *ival = odict_get_instance(odlink, used_space_ival_t,
lused_space);
return (void *) &ival->page;
}
static int used_space_cmp(void *a, void *b)
{
uintptr_t va = *(uintptr_t *) a;
uintptr_t vb = *(uintptr_t *) b;
if (va < vb)
return -1;
else if (va == vb)
return 0;
else
return +1;
}
static void used_space_remove_ival(used_space_ival_t *ival)
{
ival->used_space->pages -= ival->count;
odict_remove(&ival->lused_space);
slab_free(used_space_ival_cache, ival);
}
static void used_space_shorten_ival(used_space_ival_t *ival, size_t count)
{
assert(count > 0);
assert(count < ival->count);
ival->used_space->pages -= ival->count - count;
ival->count = count;
}
bool used_space_insert(used_space_t *used_space, uintptr_t page, size_t count)
{
used_space_ival_t *a;
used_space_ival_t *b;
bool adj_a;
bool adj_b;
odlink_t *odlink;
used_space_ival_t *ival;
assert(IS_ALIGNED(page, PAGE_SIZE));
assert(count);
odlink = odict_find_lt(&used_space->ivals, &page, NULL);
a = (odlink != NULL) ?
odict_get_instance(odlink, used_space_ival_t, lused_space) :
NULL;
b = (a != NULL) ? used_space_next(a) :
used_space_first(used_space);
if (a != NULL && overlaps(a->page, P2SZ(a->count), page, P2SZ(count)))
return false;
if (b != NULL && overlaps(page, P2SZ(count), b->page, P2SZ(b->count)))
return false;
adj_a = (a != NULL) && (a->page + P2SZ(a->count) == page);
adj_b = (b != NULL) && page + P2SZ(count) == b->page;
if (adj_a && adj_b) {
a->count += count + b->count;
used_space_remove_ival(b);
} else if (adj_a) {
a->count += count;
} else if (adj_b) {
b->page = page;
b->count += count;
} else {
ival = slab_alloc(used_space_ival_cache, 0);
ival->used_space = used_space;
odlink_initialize(&ival->lused_space);
ival->page = page;
ival->count = count;
odict_insert(&ival->lused_space, &used_space->ivals,
NULL);
}
used_space->pages += count;
return true;
}
sysarg_t sys_as_area_create(uintptr_t base, size_t size, unsigned int flags,
uintptr_t bound, uspace_ptr_as_area_pager_info_t pager_info)
{
uintptr_t virt = base;
mem_backend_t *backend;
mem_backend_data_t backend_data;
if (!pager_info)
backend = &anon_backend;
else {
backend = &user_backend;
if (copy_from_uspace(&backend_data.pager_info, pager_info,
sizeof(as_area_pager_info_t)) != EOK) {
return (sysarg_t) AS_MAP_FAILED;
}
}
as_area_t *area = as_area_create(AS, flags, size,
AS_AREA_ATTR_NONE, backend, &backend_data, &virt, bound);
if (area == NULL)
return (sysarg_t) AS_MAP_FAILED;
return (sysarg_t) virt;
}
sys_errno_t sys_as_area_resize(uintptr_t address, size_t size, unsigned int flags)
{
return (sys_errno_t) as_area_resize(AS, address, size, 0);
}
sys_errno_t sys_as_area_change_flags(uintptr_t address, unsigned int flags)
{
return (sys_errno_t) as_area_change_flags(AS, flags, address);
}
sys_errno_t sys_as_area_get_info(uintptr_t address, uspace_ptr_as_area_info_t dest)
{
as_area_t *area;
as_area_info_t info;
memset(&info, 0, sizeof(info));
mutex_lock(&AS->lock);
area = find_area_and_lock(AS, address);
if (area == NULL) {
mutex_unlock(&AS->lock);
return ENOENT;
}
info.start_addr = area->base;
info.size = P2SZ(area->pages);
info.flags = area->flags;
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
copy_to_uspace(dest, &info, sizeof(info));
return EOK;
}
sys_errno_t sys_as_area_destroy(uintptr_t address)
{
return (sys_errno_t) as_area_destroy(AS, address);
}
as_area_info_t *as_get_area_info(as_t *as, size_t *osize)
{
mutex_lock(&as->lock);
size_t area_cnt = odict_count(&as->as_areas);
size_t isize = area_cnt * sizeof(as_area_info_t);
as_area_info_t *info = malloc(isize);
if (!info) {
mutex_unlock(&as->lock);
return NULL;
}
size_t area_idx = 0;
as_area_t *area = as_area_first(as);
while (area != NULL) {
assert(area_idx < area_cnt);
mutex_lock(&area->lock);
info[area_idx].start_addr = area->base;
info[area_idx].size = P2SZ(area->pages);
info[area_idx].flags = area->flags;
++area_idx;
mutex_unlock(&area->lock);
area = as_area_next(area);
}
mutex_unlock(&as->lock);
*osize = isize;
return info;
}
void as_print(as_t *as)
{
mutex_lock(&as->lock);
as_area_t *area = as_area_first(as);
while (area != NULL) {
mutex_lock(&area->lock);
printf("as_area: %p, base=%p, pages=%zu"
" (%p - %p)\n", area, (void *) area->base,
area->pages, (void *) area->base,
(void *) (area->base + P2SZ(area->pages)));
mutex_unlock(&area->lock);
area = as_area_next(area);
}
mutex_unlock(&as->lock);
}
HelenOS homepage, sources at GitHub