amaps
This commit is contained in:
+112
@@ -0,0 +1,112 @@
|
||||
// SPDX-License-Identifier: ISC
|
||||
|
||||
#include <zinnia/alloc.h>
|
||||
#include <zinnia/amap.h>
|
||||
#include <zinnia/utils.h>
|
||||
#include <string.h>
|
||||
|
||||
errno_t anon_new(struct page* src, struct vm_anon** out) {
|
||||
struct vm_anon* anon = mem_alloc(sizeof(struct vm_anon), 0);
|
||||
if (!anon)
|
||||
return ENOMEM;
|
||||
|
||||
struct page* page;
|
||||
errno_t status = page_alloc(src ? ALLOC_NOZERO : 0, &page);
|
||||
if (status) {
|
||||
mem_free(anon);
|
||||
return status;
|
||||
}
|
||||
|
||||
if (src)
|
||||
memcpy(HHDM_PTR(vm_page_addr(page)), HHDM_PTR(vm_page_addr(src)), arch_mem_page_size());
|
||||
|
||||
atomic_fetch_or(&page->flags, PAGE_ANON, ATOMIC_RELAXED);
|
||||
page->obj.owner = anon;
|
||||
atomic_store(&anon->rc, 1, ATOMIC_RELAXED);
|
||||
anon->page = page;
|
||||
*out = anon;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void anon_ref(struct vm_anon* anon) {
|
||||
atomic_fetch_add(&anon->rc, 1, ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
void anon_unref(struct vm_anon* anon) {
|
||||
if (atomic_fetch_sub(&anon->rc, 1, ATOMIC_RELEASE) != 1)
|
||||
return;
|
||||
atomic_fence(ATOMIC_ACQUIRE);
|
||||
page_unhold(anon->page);
|
||||
mem_free(anon);
|
||||
}
|
||||
|
||||
errno_t amap_new(struct amap** out) {
|
||||
struct amap* amap = mem_alloc(sizeof(struct amap), 0);
|
||||
if (!amap)
|
||||
return ENOMEM;
|
||||
refcount_init(&amap->rc);
|
||||
mutex_init(&amap->lock);
|
||||
radix_init(&amap->anons);
|
||||
*out = amap;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void amap_ref(struct amap* amap) {
|
||||
refcount_inc(&amap->rc);
|
||||
}
|
||||
|
||||
void amap_unref(struct amap* amap) {
|
||||
if (!refcount_dec(&amap->rc))
|
||||
return;
|
||||
|
||||
struct vm_anon* batch[32];
|
||||
uint64_t slots[32];
|
||||
size_t found;
|
||||
while ((found = radix_gang_lookup(&amap->anons, 0, ARRAY_SIZE(batch), (void**)batch, slots)) != 0) {
|
||||
for (size_t i = 0; i < found; i++) {
|
||||
radix_remove(&amap->anons, slots[i]);
|
||||
anon_unref(batch[i]);
|
||||
}
|
||||
}
|
||||
radix_destroy(&amap->anons);
|
||||
mem_free(amap);
|
||||
}
|
||||
|
||||
bool amap_is_shared(struct amap* amap) {
|
||||
return atomic_load(&amap->rc.count, ATOMIC_ACQUIRE) != 1;
|
||||
}
|
||||
|
||||
errno_t amap_copy(struct amap* src, size_t offset, size_t num_slots, struct amap** out) {
|
||||
struct amap* copy;
|
||||
errno_t status = amap_new(©);
|
||||
if (status)
|
||||
return status;
|
||||
|
||||
struct vm_anon* batch[32];
|
||||
uint64_t slots[32];
|
||||
uint64_t next = offset;
|
||||
|
||||
mutex_lock(&src->lock);
|
||||
while (status == 0) {
|
||||
const size_t found = radix_gang_lookup(&src->anons, next, ARRAY_SIZE(batch), (void**)batch, slots);
|
||||
if (found == 0)
|
||||
break;
|
||||
for (size_t i = 0; i < found && status == 0; i++) {
|
||||
if (slots[i] >= offset + num_slots)
|
||||
goto done;
|
||||
status = radix_insert(©->anons, slots[i] - offset, batch[i]);
|
||||
if (!status)
|
||||
anon_ref(batch[i]);
|
||||
}
|
||||
next = slots[found - 1] + 1;
|
||||
}
|
||||
done:
|
||||
mutex_unlock(&src->lock);
|
||||
|
||||
if (status) {
|
||||
amap_unref(copy);
|
||||
return status;
|
||||
}
|
||||
*out = copy;
|
||||
return 0;
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
kernel_sources += files(
|
||||
'amap.c',
|
||||
'fault.c',
|
||||
'init.c',
|
||||
'page.c',
|
||||
'pmm.c',
|
||||
'slab.c',
|
||||
'stack.c',
|
||||
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
// SPDX-License-Identifier: ISC
|
||||
|
||||
#include <zinnia/alloc.h>
|
||||
#include <zinnia/atomic.h>
|
||||
#include <zinnia/park.h>
|
||||
#include <zinnia/virt.h>
|
||||
|
||||
struct page* vm_page_from_addr(phys_t addr) {
|
||||
return &vm_pfndb[addr / arch_mem_page_size()];
|
||||
}
|
||||
|
||||
errno_t page_alloc(enum alloc_flags flags, struct page** out) {
|
||||
phys_t addr;
|
||||
errno_t status = mem_phys_alloc(1, flags, &addr);
|
||||
if (status)
|
||||
return status;
|
||||
|
||||
struct page* page = vm_page_from_addr(addr);
|
||||
page->type = PAGE_PHYS;
|
||||
page->obj.owner = nullptr;
|
||||
page->obj.index = 0;
|
||||
atomic_store(&page->flags, 0, ATOMIC_RELAXED);
|
||||
atomic_store(&page->holds, 1, ATOMIC_RELEASE);
|
||||
*out = page;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void page_hold(struct page* page) {
|
||||
atomic_fetch_add(&page->holds, 1, ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
void page_unhold(struct page* page) {
|
||||
if (atomic_fetch_sub(&page->holds, 1, ATOMIC_RELEASE) != 1)
|
||||
return;
|
||||
atomic_fence(ATOMIC_ACQUIRE);
|
||||
atomic_store(&page->flags, 0, ATOMIC_RELAXED);
|
||||
page->obj.owner = nullptr;
|
||||
mem_phys_free(vm_page_addr(page), 1);
|
||||
}
|
||||
|
||||
bool page_try_busy(struct page* page) {
|
||||
return !(atomic_fetch_or(&page->flags, PAGE_BUSY, ATOMIC_ACQUIRE) & PAGE_BUSY);
|
||||
}
|
||||
|
||||
void page_unbusy(struct page* page) {
|
||||
const uint32_t old = atomic_fetch_and(&page->flags, ~(PAGE_BUSY | PAGE_WANTED), ATOMIC_RELEASE);
|
||||
if (old & PAGE_WANTED)
|
||||
unpark_all(&page->flags, 0);
|
||||
}
|
||||
|
||||
static bool still_busy(void* ctx) {
|
||||
struct page* page = ctx;
|
||||
uint32_t flags = atomic_load(&page->flags, ATOMIC_RELAXED);
|
||||
while (flags & PAGE_BUSY) {
|
||||
if (atomic_compare_exchange_weak(&page->flags, &flags, flags | PAGE_WANTED, ATOMIC_RELAXED, ATOMIC_RELAXED))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void page_wait(struct page* page) {
|
||||
park(&page->flags, still_busy, page, 0);
|
||||
}
|
||||
Reference in New Issue
Block a user