Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 3035 → Rev 3036

/branches/tracing/kernel/generic/src/mm/as.c
986,7 → 986,17
mutex_lock(&AS->lock);
ipl = interrupts_disable();
area = find_area_and_lock(AS, page);
if (area->backend != &anon_backend || area->sh_info != NULL) {
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
 
rc = as_area_make_writeable(area->base);
if (rc != 0) return rc;
 
goto restart;
}
 
pte = page_mapping_find(AS, page);
if (! (pte && PTE_VALID(pte) && PTE_PRESENT(pte)) ) {
mutex_unlock(&area->lock);
1033,6 → 1043,123
return EOK;
}
 
/** Make sure area is private and anonymous.
*
* Not atomic atm.
* @param address Virtual address in AS.
*/
int as_area_make_writeable(uintptr_t address)
{
ipl_t ipl;
as_area_t *area;
uintptr_t base, page;
uintptr_t old_frame, frame;
size_t size;
int flags;
int page_flags;
pte_t *pte;
int rc;
uintptr_t *pagemap;
 
ipl = interrupts_disable();
mutex_lock(&AS->lock);
area = find_area_and_lock(AS, address);
if (!area) {
/*
* Could not find the address space area.
*/
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
return ENOENT;
}
 
if (area->backend == &anon_backend && !area->sh_info) {
/* Nothing to do */
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
return EOK;
}
 
base = area->base;
size = area->pages * PAGE_SIZE;
flags = area->flags;
page_flags = as_area_get_flags(area);
 
pagemap = malloc(area->pages * sizeof(uintptr_t), 0);
page_table_lock(AS, false);
 
for (page = base; page < base + size; page += PAGE_SIZE) {
pte = page_mapping_find(AS, page);
if (!pte || !PTE_PRESENT(pte) || !PTE_READABLE(pte)) {
/* Fetch the missing page */
if (!area->backend || !area->backend->page_fault) {
page_table_unlock(AS, false);
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
return EINVAL;
}
if (area->backend->page_fault(area, page, PF_ACCESS_READ) != AS_PF_OK) {
page_table_unlock(AS, false);
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
return EINVAL;
}
}
ASSERT(PTE_VALID(pte));
 
old_frame = PTE_GET_FRAME(pte);
 
frame = (uintptr_t)frame_alloc(ONE_FRAME, 0);
memcpy((void *) PA2KA(frame), (void *)PA2KA(old_frame),
FRAME_SIZE);
 
pagemap[(page - base) / PAGE_SIZE] = frame;
}
 
page_table_unlock(AS, false);
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
 
rc = as_area_destroy(AS, address);
if (rc < 0) {
free(pagemap);
return rc;
}
 
area = as_area_create(AS, flags, size, base, AS_AREA_ATTR_PARTIAL,
&anon_backend, NULL);
if (area == NULL) {
free(pagemap);
return rc;
}
 
mutex_lock(&AS->lock);
mutex_lock(&area->lock);
page_table_lock(AS, false);
for (page = base; page < base + size; page += PAGE_SIZE) {
frame = pagemap[(page - base) / PAGE_SIZE];
 
page_mapping_insert(AS, page, frame, page_flags);
if (!used_space_insert(area, page, 1))
panic("Could not insert used space.\n");
}
 
page_table_unlock(AS, false);
 
area->attributes &= ~AS_AREA_ATTR_PARTIAL;
 
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
 
free(pagemap);
 
return EOK;
}
 
/** Convert address space area flags to page flags.
*
* @param aflags Flags of some address space area.