2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#define _SYSTEM 1
|
|
|
|
|
|
|
|
#include <minix/com.h>
|
|
|
|
#include <minix/callnr.h>
|
|
|
|
#include <minix/type.h>
|
|
|
|
#include <minix/config.h>
|
|
|
|
#include <minix/const.h>
|
|
|
|
#include <minix/sysutil.h>
|
|
|
|
#include <minix/syslib.h>
|
2009-09-21 16:49:49 +02:00
|
|
|
#include <minix/debug.h>
|
|
|
|
#include <minix/bitmap.h>
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <memory.h>
|
|
|
|
|
|
|
|
#include "vm.h"
|
|
|
|
#include "proto.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "glo.h"
|
|
|
|
#include "region.h"
|
|
|
|
#include "sanitycheck.h"
|
2009-09-21 16:49:49 +02:00
|
|
|
#include "physravl.h"
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
/* Should a physblock be mapped writable? */
|
|
|
|
#define WRITABLE(r, pb) \
|
|
|
|
(((r)->flags & (VR_DIRECT | VR_SHARED)) || \
|
|
|
|
(((r)->flags & VR_WRITABLE) && (pb)->refcount == 1))
|
|
|
|
|
|
|
|
FORWARD _PROTOTYPE(struct phys_region *map_new_physblock, (struct vmproc *vmp,
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vir_region *region, vir_bytes offset, vir_bytes length,
|
2009-09-21 16:49:49 +02:00
|
|
|
phys_bytes what));
|
|
|
|
|
|
|
|
FORWARD _PROTOTYPE(int map_ph_writept, (struct vmproc *vmp, struct vir_region *vr,
|
|
|
|
struct phys_region *pr));
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
FORWARD _PROTOTYPE(int map_copy_ph_block, (struct vmproc *vmp, struct vir_region *region, struct phys_region *ph));
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
FORWARD _PROTOTYPE(struct vir_region *map_copy_region, (struct vmproc *vmp, struct vir_region *vr));
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
PRIVATE char *map_name(struct vir_region *vr)
|
|
|
|
{
|
|
|
|
int type = vr->flags & (VR_ANON|VR_DIRECT);
|
|
|
|
switch(type) {
|
|
|
|
case VR_ANON:
|
|
|
|
return "anonymous";
|
|
|
|
case VR_DIRECT:
|
|
|
|
return "direct";
|
|
|
|
default:
|
|
|
|
vm_panic("unknown mapping type", type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return "NOTREACHED";
|
|
|
|
}
|
|
|
|
|
2009-09-27 14:44:36 +02:00
|
|
|
PUBLIC void map_printregion(struct vmproc *vmp, struct vir_region *vr)
|
|
|
|
{
|
|
|
|
physr_iter iter;
|
|
|
|
struct phys_region *ph;
|
|
|
|
printf("map_printmap: map_name: %s\n", map_name(vr));
|
|
|
|
printf("\t%s (len 0x%lx), %s\n",
|
|
|
|
arch_map2str(vmp, vr->vaddr), vr->length,
|
|
|
|
map_name(vr));
|
|
|
|
printf("\t\tphysblocks:\n");
|
|
|
|
physr_start_iter_least(vr->phys, &iter);
|
|
|
|
while((ph = physr_get_iter(&iter))) {
|
|
|
|
printf("\t\t@ %s (refs %d): phys 0x%lx len 0x%lx\n",
|
|
|
|
arch_map2str(vmp, vr->vaddr + ph->offset),
|
|
|
|
ph->ph->refcount, ph->ph->phys, ph->ph->length);
|
|
|
|
physr_incr_iter(&iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*===========================================================================*
|
|
|
|
* map_printmap *
|
|
|
|
*===========================================================================*/
|
2009-09-21 16:49:49 +02:00
|
|
|
PUBLIC void map_printmap(vmp)
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vmproc *vmp;
|
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2008-12-08 17:43:20 +01:00
|
|
|
printf("memory regions in process %d:\n", vmp->vm_endpoint);
|
2008-11-19 13:26:10 +01:00
|
|
|
for(vr = vmp->vm_regions; vr; vr = vr->next) {
|
2009-09-27 14:44:36 +02:00
|
|
|
map_printregion(vmp, vr);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-08 17:43:20 +01:00
|
|
|
|
|
|
|
#if SANITYCHECKS
|
|
|
|
|
|
|
|
|
2009-09-23 15:33:01 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* map_sanitycheck_pt *
|
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE int map_sanitycheck_pt(struct vmproc *vmp,
|
|
|
|
struct vir_region *vr, struct phys_region *pr)
|
|
|
|
{
|
|
|
|
struct phys_block *pb = pr->ph;
|
|
|
|
int rw;
|
|
|
|
|
|
|
|
if(!(vmp->vm_flags & VMF_HASPT))
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
if(WRITABLE(vr, pb))
|
|
|
|
rw = PTF_WRITE;
|
|
|
|
else
|
|
|
|
rw = 0;
|
|
|
|
|
|
|
|
return pt_writemap(&vmp->vm_pt, vr->vaddr + pr->offset,
|
|
|
|
pb->phys, pb->length, PTF_PRESENT | PTF_USER | rw, WMF_VERIFY);
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*===========================================================================*
|
|
|
|
* map_sanitycheck *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void map_sanitycheck(char *file, int line)
|
|
|
|
{
|
|
|
|
struct vmproc *vmp;
|
|
|
|
|
|
|
|
/* Macro for looping over all physical blocks of all regions of
|
|
|
|
* all processes.
|
|
|
|
*/
|
|
|
|
#define ALLREGIONS(regioncode, physcode) \
|
2009-09-21 16:49:49 +02:00
|
|
|
for(vmp = vmproc; vmp < &vmproc[VMP_NR]; vmp++) { \
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vir_region *vr; \
|
|
|
|
if(!(vmp->vm_flags & VMF_INUSE)) \
|
|
|
|
continue; \
|
|
|
|
for(vr = vmp->vm_regions; vr; vr = vr->next) { \
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_iter iter; \
|
2008-11-19 13:26:10 +01:00
|
|
|
struct phys_region *pr; \
|
|
|
|
regioncode; \
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_start_iter_least(vr->phys, &iter); \
|
|
|
|
while((pr = physr_get_iter(&iter))) { \
|
2008-11-19 13:26:10 +01:00
|
|
|
physcode; \
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_incr_iter(&iter); \
|
2008-11-19 13:26:10 +01:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
#define MYSLABSANE(s) MYASSERT(slabsane_f(__FILE__, __LINE__, s, sizeof(*(s))))
|
2009-04-22 14:39:29 +02:00
|
|
|
/* Basic pointers check. */
|
|
|
|
ALLREGIONS(MYSLABSANE(vr),MYSLABSANE(pr); MYSLABSANE(pr->ph);MYSLABSANE(pr->parent));
|
2009-09-21 16:49:49 +02:00
|
|
|
ALLREGIONS(/* MYASSERT(vr->parent == vmp) */,MYASSERT(pr->parent == vr););
|
2009-04-22 14:39:29 +02:00
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/* Do counting for consistency check. */
|
2009-09-21 16:49:49 +02:00
|
|
|
ALLREGIONS(;,USE(pr->ph, pr->ph->seencount = 0;););
|
|
|
|
ALLREGIONS(;,USE(pr->ph, pr->ph->seencount++;);
|
|
|
|
if(pr->ph->seencount == 1) {
|
|
|
|
MYASSERT(usedpages_add(pr->ph->phys,
|
|
|
|
pr->ph->length) == OK);
|
|
|
|
}
|
|
|
|
);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* Do consistency check. */
|
|
|
|
ALLREGIONS(if(vr->next) {
|
|
|
|
MYASSERT(vr->vaddr < vr->next->vaddr);
|
|
|
|
MYASSERT(vr->vaddr + vr->length <= vr->next->vaddr);
|
|
|
|
}
|
|
|
|
MYASSERT(!(vr->vaddr % VM_PAGE_SIZE));,
|
|
|
|
if(pr->ph->refcount != pr->ph->seencount) {
|
|
|
|
map_printmap(vmp);
|
|
|
|
printf("ph in vr 0x%lx: 0x%lx-0x%lx refcount %d "
|
|
|
|
"but seencount %lu\n",
|
2009-09-21 16:49:49 +02:00
|
|
|
vr, pr->offset,
|
|
|
|
pr->offset + pr->ph->length,
|
2008-11-19 13:26:10 +01:00
|
|
|
pr->ph->refcount, pr->ph->seencount);
|
|
|
|
}
|
2009-04-22 14:39:29 +02:00
|
|
|
{
|
|
|
|
int n_others = 0;
|
|
|
|
struct phys_region *others;
|
|
|
|
if(pr->ph->refcount > 0) {
|
|
|
|
MYASSERT(pr->ph->firstregion);
|
|
|
|
if(pr->ph->refcount == 1) {
|
|
|
|
MYASSERT(pr->ph->firstregion == pr);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MYASSERT(!pr->ph->firstregion);
|
|
|
|
}
|
|
|
|
for(others = pr->ph->firstregion; others;
|
|
|
|
others = others->next_ph_list) {
|
|
|
|
MYSLABSANE(others);
|
|
|
|
MYASSERT(others->ph == pr->ph);
|
|
|
|
n_others++;
|
|
|
|
}
|
|
|
|
MYASSERT(pr->ph->refcount == n_others);
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
MYASSERT(pr->ph->refcount == pr->ph->seencount);
|
2009-09-21 16:49:49 +02:00
|
|
|
MYASSERT(!(pr->offset % VM_PAGE_SIZE));
|
2008-11-19 13:26:10 +01:00
|
|
|
MYASSERT(!(pr->ph->length % VM_PAGE_SIZE)););
|
2009-09-23 15:33:01 +02:00
|
|
|
ALLREGIONS(,MYASSERT(map_sanitycheck_pt(vmp, vr, pr) == OK));
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*=========================================================================*
|
|
|
|
* map_ph_writept *
|
|
|
|
*=========================================================================*/
|
2009-09-21 16:49:49 +02:00
|
|
|
PRIVATE int map_ph_writept(struct vmproc *vmp, struct vir_region *vr,
|
|
|
|
struct phys_region *pr)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
|
|
|
int rw;
|
2009-09-21 16:49:49 +02:00
|
|
|
struct phys_block *pb = pr->ph;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
vm_assert(!(vr->vaddr % VM_PAGE_SIZE));
|
|
|
|
vm_assert(!(pb->length % VM_PAGE_SIZE));
|
2009-09-21 16:49:49 +02:00
|
|
|
vm_assert(!(pr->offset % VM_PAGE_SIZE));
|
2008-11-19 13:26:10 +01:00
|
|
|
vm_assert(pb->refcount > 0);
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
if(WRITABLE(vr, pb))
|
2008-11-19 13:26:10 +01:00
|
|
|
rw = PTF_WRITE;
|
|
|
|
else
|
|
|
|
rw = 0;
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
if(pt_writemap(&vmp->vm_pt, vr->vaddr + pr->offset,
|
2008-11-19 13:26:10 +01:00
|
|
|
pb->phys, pb->length, PTF_PRESENT | PTF_USER | rw,
|
2009-09-27 14:44:36 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
!pr->written ? 0 :
|
|
|
|
#endif
|
|
|
|
WMF_OVERWRITE) != OK) {
|
2008-11-19 13:26:10 +01:00
|
|
|
printf("VM: map_writept: pt_writemap failed\n");
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
2009-09-27 14:44:36 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
USE(pr, pr->written = 1;);
|
|
|
|
#endif
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2009-09-21 16:49:49 +02:00
|
|
|
* region_find_slot *
|
2008-11-19 13:26:10 +01:00
|
|
|
*===========================================================================*/
|
2009-09-21 16:49:49 +02:00
|
|
|
PRIVATE vir_bytes region_find_slot(struct vmproc *vmp,
|
|
|
|
vir_bytes minv, vir_bytes maxv, vir_bytes length,
|
|
|
|
struct vir_region **prev)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
2009-09-21 16:49:49 +02:00
|
|
|
struct vir_region *firstregion = vmp->vm_regions, *prevregion = NULL;
|
2008-11-19 13:26:10 +01:00
|
|
|
vir_bytes startv;
|
|
|
|
int foundflag = 0;
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
/* We must be in paged mode to be able to do this. */
|
|
|
|
vm_assert(vm_paged);
|
|
|
|
|
|
|
|
/* Length must be reasonable. */
|
|
|
|
vm_assert(length > 0);
|
|
|
|
|
|
|
|
/* Special case: allow caller to set maxv to 0 meaning 'I want
|
|
|
|
* it to be mapped in right here.'
|
|
|
|
*/
|
|
|
|
if(maxv == 0) {
|
|
|
|
maxv = minv + length;
|
|
|
|
|
|
|
|
/* Sanity check. */
|
|
|
|
if(maxv <= minv) {
|
2009-09-21 16:49:49 +02:00
|
|
|
printf("region_find_slot: minv 0x%lx and bytes 0x%lx\n",
|
2008-11-19 13:26:10 +01:00
|
|
|
minv, length);
|
2008-12-08 17:43:20 +01:00
|
|
|
map_printmap(vmp);
|
2009-09-21 16:49:49 +02:00
|
|
|
return (vir_bytes) -1;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Basic input sanity checks. */
|
|
|
|
vm_assert(!(length % VM_PAGE_SIZE));
|
|
|
|
if(minv >= maxv) {
|
|
|
|
printf("VM: 1 minv: 0x%lx maxv: 0x%lx length: 0x%lx\n",
|
|
|
|
minv, maxv, length);
|
|
|
|
}
|
|
|
|
vm_assert(minv < maxv);
|
|
|
|
vm_assert(minv + length <= maxv);
|
|
|
|
|
|
|
|
#define FREEVRANGE(rangestart, rangeend, foundcode) { \
|
|
|
|
vir_bytes frstart = (rangestart), frend = (rangeend); \
|
|
|
|
frstart = MAX(frstart, minv); \
|
|
|
|
frend = MIN(frend, maxv); \
|
|
|
|
if(frend > frstart && (frend - frstart) >= length) { \
|
|
|
|
startv = frstart; \
|
|
|
|
foundflag = 1; \
|
|
|
|
foundcode; \
|
|
|
|
} }
|
|
|
|
|
|
|
|
/* This is the free virtual address space before the first region. */
|
|
|
|
FREEVRANGE(0, firstregion ? firstregion->vaddr : VM_DATATOP, ;);
|
|
|
|
|
|
|
|
if(!foundflag) {
|
2009-09-21 16:49:49 +02:00
|
|
|
struct vir_region *vr;
|
2008-11-19 13:26:10 +01:00
|
|
|
for(vr = vmp->vm_regions; vr && !foundflag; vr = vr->next) {
|
|
|
|
FREEVRANGE(vr->vaddr + vr->length,
|
|
|
|
vr->next ? vr->next->vaddr : VM_DATATOP,
|
|
|
|
prevregion = vr;);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!foundflag) {
|
2009-09-21 16:49:49 +02:00
|
|
|
printf("VM: region_find_slot: no 0x%lx bytes found for %d between 0x%lx and 0x%lx\n",
|
2008-11-19 13:26:10 +01:00
|
|
|
length, vmp->vm_endpoint, minv, maxv);
|
2008-12-08 17:43:20 +01:00
|
|
|
map_printmap(vmp);
|
2009-09-21 16:49:49 +02:00
|
|
|
return (vir_bytes) -1;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if SANITYCHECKS
|
|
|
|
if(prevregion) vm_assert(prevregion->vaddr < startv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* However we got it, startv must be in the requested range. */
|
|
|
|
vm_assert(startv >= minv);
|
|
|
|
vm_assert(startv < maxv);
|
|
|
|
vm_assert(startv + length <= maxv);
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
if (prev)
|
|
|
|
*prev = prevregion;
|
|
|
|
return startv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* map_page_region *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC struct vir_region *map_page_region(vmp, minv, maxv, length,
|
|
|
|
what, flags, mapflags)
|
|
|
|
struct vmproc *vmp;
|
|
|
|
vir_bytes minv;
|
|
|
|
vir_bytes maxv;
|
|
|
|
vir_bytes length;
|
|
|
|
vir_bytes what;
|
|
|
|
u32_t flags;
|
|
|
|
int mapflags;
|
|
|
|
{
|
|
|
|
struct vir_region *prevregion = NULL, *newregion;
|
|
|
|
vir_bytes startv;
|
|
|
|
struct phys_region *ph;
|
|
|
|
physr_avl *phavl;
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
startv = region_find_slot(vmp, minv, maxv, length, &prevregion);
|
|
|
|
if (startv == (vir_bytes) -1)
|
|
|
|
return NULL;
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/* Now we want a new region. */
|
|
|
|
if(!SLABALLOC(newregion)) {
|
|
|
|
printf("VM: map_page_region: allocating region failed\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill in node details. */
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(newregion,
|
2008-11-19 13:26:10 +01:00
|
|
|
newregion->vaddr = startv;
|
|
|
|
newregion->length = length;
|
|
|
|
newregion->flags = flags;
|
|
|
|
newregion->tag = VRT_NONE;
|
2009-09-21 16:49:49 +02:00
|
|
|
newregion->parent = vmp;);
|
|
|
|
|
|
|
|
SLABALLOC(phavl);
|
|
|
|
if(!phavl) {
|
|
|
|
printf("VM: map_page_region: allocating phys avl failed\n");
|
|
|
|
SLABFREE(newregion);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
USE(newregion, newregion->phys = phavl;);
|
|
|
|
|
|
|
|
physr_init(newregion->phys);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2008-12-08 17:43:20 +01:00
|
|
|
/* If we know what we're going to map to, map it right away. */
|
|
|
|
if(what != MAP_NONE) {
|
2009-09-21 16:49:49 +02:00
|
|
|
struct phys_region *pr;
|
2008-12-08 17:43:20 +01:00
|
|
|
vm_assert(!(what % VM_PAGE_SIZE));
|
2008-11-19 13:26:10 +01:00
|
|
|
vm_assert(!(length % VM_PAGE_SIZE));
|
|
|
|
vm_assert(!(startv % VM_PAGE_SIZE));
|
|
|
|
vm_assert(!(mapflags & MF_PREALLOC));
|
2009-09-21 16:49:49 +02:00
|
|
|
if(!(pr=map_new_physblock(vmp, newregion, 0, length, what))) {
|
2008-11-19 13:26:10 +01:00
|
|
|
printf("VM: map_new_physblock failed\n");
|
2009-09-21 16:49:49 +02:00
|
|
|
SLABFREE(newregion->phys);
|
2008-11-19 13:26:10 +01:00
|
|
|
SLABFREE(newregion);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
if(map_ph_writept(vmp, newregion, pr) != OK) {
|
2008-11-19 13:26:10 +01:00
|
|
|
printf("VM: map_region_writept failed\n");
|
|
|
|
SLABFREE(newregion);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if((flags & VR_ANON) && (mapflags & MF_PREALLOC)) {
|
|
|
|
if(map_handle_memory(vmp, newregion, 0, length, 1) != OK) {
|
2009-09-21 16:49:49 +02:00
|
|
|
printf("VM: map_page_region: prealloc failed\n");
|
|
|
|
SLABFREE(newregion->phys);
|
2008-11-19 13:26:10 +01:00
|
|
|
SLABFREE(newregion);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Link it. */
|
|
|
|
if(prevregion) {
|
|
|
|
vm_assert(prevregion->vaddr < newregion->vaddr);
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(newregion, newregion->next = prevregion->next;);
|
|
|
|
USE(prevregion, prevregion->next = newregion;);
|
2008-11-19 13:26:10 +01:00
|
|
|
} else {
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(newregion, newregion->next = vmp->vm_regions;);
|
2008-11-19 13:26:10 +01:00
|
|
|
vmp->vm_regions = newregion;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if SANITYCHECKS
|
|
|
|
vm_assert(startv == newregion->vaddr);
|
|
|
|
if(newregion->next) {
|
|
|
|
vm_assert(newregion->vaddr < newregion->next->vaddr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
return newregion;
|
|
|
|
}
|
|
|
|
|
2009-04-22 14:39:29 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* pb_unreferenced *
|
|
|
|
*===========================================================================*/
|
|
|
|
void pb_unreferenced(struct vir_region *region, struct phys_region *pr)
|
|
|
|
{
|
|
|
|
struct phys_block *pb;
|
|
|
|
int remap = 0;
|
|
|
|
|
|
|
|
pb = pr->ph;
|
|
|
|
vm_assert(pb->refcount > 0);
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(pb, pb->refcount--;);
|
2009-04-22 14:39:29 +02:00
|
|
|
vm_assert(pb->refcount >= 0);
|
|
|
|
|
|
|
|
if(pb->firstregion == pr) {
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(pb, pb->firstregion = pr->next_ph_list;);
|
2009-04-22 14:39:29 +02:00
|
|
|
} else {
|
|
|
|
struct phys_region *others;
|
|
|
|
|
|
|
|
for(others = pb->firstregion; others;
|
|
|
|
others = others->next_ph_list) {
|
|
|
|
vm_assert(others->ph == pb);
|
|
|
|
if(others->next_ph_list == pr) {
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(others, others->next_ph_list = pr->next_ph_list;);
|
2009-04-22 14:39:29 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vm_assert(others); /* Otherwise, wasn't on the list. */
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pb->refcount == 0) {
|
|
|
|
vm_assert(!pb->firstregion);
|
|
|
|
if(region->flags & VR_ANON) {
|
|
|
|
FREE_MEM(ABS2CLICK(pb->phys),
|
|
|
|
ABS2CLICK(pb->length));
|
|
|
|
} else if(region->flags & VR_DIRECT) {
|
|
|
|
; /* No action required. */
|
|
|
|
} else {
|
|
|
|
vm_panic("strange phys flags", NO_NUM);
|
|
|
|
}
|
|
|
|
SLABFREE(pb);
|
2009-09-21 16:49:49 +02:00
|
|
|
} else if(WRITABLE(region, pb)) {
|
2009-04-22 14:39:29 +02:00
|
|
|
/* If a writable piece of physical memory is now only
|
|
|
|
* referenced once, map it writable right away instead of
|
|
|
|
* waiting for a page fault.
|
|
|
|
*/
|
|
|
|
vm_assert(pb);
|
|
|
|
vm_assert(pb->firstregion);
|
|
|
|
vm_assert(!pb->firstregion->next_ph_list);
|
|
|
|
vm_assert(pb->firstregion->ph == pb);
|
|
|
|
vm_assert(pb->firstregion->ph == pb);
|
|
|
|
if(map_ph_writept(pb->firstregion->parent->parent,
|
2009-09-21 16:49:49 +02:00
|
|
|
pb->firstregion->parent, pb->firstregion) != OK) {
|
2009-04-22 14:39:29 +02:00
|
|
|
vm_panic("pb_unreferenced: writept", NO_NUM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/*===========================================================================*
|
2009-09-21 16:49:49 +02:00
|
|
|
* map_subfree *
|
2008-11-19 13:26:10 +01:00
|
|
|
*===========================================================================*/
|
2009-09-21 16:49:49 +02:00
|
|
|
PRIVATE int map_subfree(struct vmproc *vmp,
|
|
|
|
struct vir_region *region, vir_bytes len)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
|
|
|
struct phys_region *pr, *nextpr;
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_iter iter;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-04-22 14:39:29 +02:00
|
|
|
#if SANITYCHECKS
|
2009-09-21 16:49:49 +02:00
|
|
|
{
|
|
|
|
physr_start_iter_least(region->phys, &iter);
|
|
|
|
while((pr = physr_get_iter(&iter))) {
|
2009-04-22 14:39:29 +02:00
|
|
|
struct phys_region *others;
|
|
|
|
struct phys_block *pb;
|
|
|
|
|
|
|
|
pb = pr->ph;
|
|
|
|
|
|
|
|
for(others = pb->firstregion; others;
|
|
|
|
others = others->next_ph_list) {
|
|
|
|
vm_assert(others->ph == pb);
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_incr_iter(&iter);
|
|
|
|
}
|
2009-04-22 14:39:29 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_start_iter_least(region->phys, &iter);
|
|
|
|
while((pr = physr_get_iter(&iter))) {
|
|
|
|
physr_incr_iter(&iter);
|
|
|
|
if(pr->offset >= len)
|
|
|
|
break;
|
|
|
|
if(pr->offset + pr->ph->length <= len) {
|
|
|
|
pb_unreferenced(region, pr);
|
|
|
|
physr_remove(region->phys, pr->offset);
|
|
|
|
physr_start_iter_least(region->phys, &iter);
|
|
|
|
SLABFREE(pr);
|
|
|
|
} else {
|
|
|
|
vir_bytes sublen;
|
|
|
|
vm_assert(len > pr->offset);
|
|
|
|
vm_assert(len < pr->offset + pr->ph->length);
|
|
|
|
vm_assert(pr->ph->refcount > 0);
|
|
|
|
sublen = len - pr->offset;
|
2009-09-23 15:33:01 +02:00
|
|
|
vm_assert(!(sublen % VM_PAGE_SIZE));
|
|
|
|
vm_assert(sublen < pr->ph->length);
|
2009-09-21 16:49:49 +02:00
|
|
|
if(pr->ph->refcount > 1) {
|
|
|
|
int r;
|
|
|
|
r = map_copy_ph_block(vmp, region, pr);
|
|
|
|
if(r != OK)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
vm_assert(pr->ph->refcount == 1);
|
2009-09-22 13:51:17 +02:00
|
|
|
if(!(region->flags & VR_DIRECT)) {
|
|
|
|
FREE_MEM(ABS2CLICK(pr->ph->phys), ABS2CLICK(sublen));
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(pr, pr->offset += sublen;);
|
|
|
|
USE(pr->ph,
|
|
|
|
pr->ph->phys += sublen;
|
|
|
|
pr->ph->length -= sublen;);
|
2009-09-23 15:33:01 +02:00
|
|
|
vm_assert(!(pr->offset % VM_PAGE_SIZE));
|
|
|
|
vm_assert(!(pr->ph->phys % VM_PAGE_SIZE));
|
|
|
|
vm_assert(!(pr->ph->length % VM_PAGE_SIZE));
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* map_free *
|
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE int map_free(struct vmproc *vmp, struct vir_region *region)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if((r=map_subfree(vmp, region, region->length)) != OK)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
SLABFREE(region->phys);
|
2008-11-19 13:26:10 +01:00
|
|
|
SLABFREE(region);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* map_free_proc *
|
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC int map_free_proc(vmp)
|
|
|
|
struct vmproc *vmp;
|
|
|
|
{
|
|
|
|
struct vir_region *r, *nextr;
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
for(r = vmp->vm_regions; r; r = nextr) {
|
|
|
|
nextr = r->next;
|
2009-04-22 14:39:29 +02:00
|
|
|
SANITYCHECK(SCL_DETAIL);
|
|
|
|
#if SANITYCHECKS
|
|
|
|
nocheck++;
|
|
|
|
#endif
|
2009-09-21 16:49:49 +02:00
|
|
|
map_free(vmp, r);
|
2008-11-19 13:26:10 +01:00
|
|
|
vmp->vm_regions = nextr; /* For sanity checks. */
|
2009-04-22 14:39:29 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
nocheck--;
|
|
|
|
#endif
|
|
|
|
SANITYCHECK(SCL_DETAIL);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
vmp->vm_regions = NULL;
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* map_lookup *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC struct vir_region *map_lookup(vmp, offset)
|
|
|
|
struct vmproc *vmp;
|
|
|
|
vir_bytes offset;
|
|
|
|
{
|
|
|
|
struct vir_region *r;
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
if(!vmp->vm_regions)
|
|
|
|
vm_panic("process has no regions", vmp->vm_endpoint);
|
|
|
|
|
|
|
|
for(r = vmp->vm_regions; r; r = r->next) {
|
|
|
|
if(offset >= r->vaddr && offset < r->vaddr + r->length)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* map_new_physblock *
|
|
|
|
*===========================================================================*/
|
2009-09-21 16:49:49 +02:00
|
|
|
PRIVATE struct phys_region *map_new_physblock(vmp, region, offset, length, what_mem)
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vmproc *vmp;
|
|
|
|
struct vir_region *region;
|
|
|
|
vir_bytes offset;
|
|
|
|
vir_bytes length;
|
|
|
|
phys_bytes what_mem;
|
|
|
|
{
|
2009-04-22 14:39:29 +02:00
|
|
|
struct phys_region *newphysr;
|
2008-11-19 13:26:10 +01:00
|
|
|
struct phys_block *newpb;
|
|
|
|
phys_bytes mem_clicks, clicks;
|
|
|
|
vir_bytes mem;
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
vm_assert(!(length % VM_PAGE_SIZE));
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
NOTRUNNABLE(vmp->vm_endpoint);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* Allocate things necessary for this chunk of memory. */
|
|
|
|
if(!SLABALLOC(newphysr))
|
2009-09-21 16:49:49 +02:00
|
|
|
return NULL;
|
2008-11-19 13:26:10 +01:00
|
|
|
if(!SLABALLOC(newpb)) {
|
|
|
|
SLABFREE(newphysr);
|
2009-09-21 16:49:49 +02:00
|
|
|
return NULL;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Memory for new physical block. */
|
|
|
|
clicks = CLICKSPERPAGE * length / VM_PAGE_SIZE;
|
2008-12-08 17:43:20 +01:00
|
|
|
if(what_mem == MAP_NONE) {
|
2009-02-12 13:26:08 +01:00
|
|
|
u32_t af = PAF_CLEAR;
|
|
|
|
if(region->flags & VR_PHYS64K)
|
|
|
|
af |= PAF_ALIGN64K;
|
2009-09-21 16:49:49 +02:00
|
|
|
if(region->flags & VR_LOWER16MB)
|
|
|
|
af |= PAF_LOWER16MB;
|
|
|
|
if(region->flags & VR_LOWER1MB)
|
|
|
|
af |= PAF_LOWER1MB;
|
2009-02-12 13:26:08 +01:00
|
|
|
if((mem_clicks = ALLOC_MEM(clicks, af)) == NO_MEM) {
|
2008-11-19 13:26:10 +01:00
|
|
|
SLABFREE(newpb);
|
|
|
|
SLABFREE(newphysr);
|
2009-09-21 16:49:49 +02:00
|
|
|
printf("map_new_physblock: couldn't allocate\n");
|
|
|
|
return NULL;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
mem = CLICK2ABS(mem_clicks);
|
|
|
|
} else {
|
|
|
|
mem = what_mem;
|
|
|
|
}
|
2009-04-22 14:39:29 +02:00
|
|
|
SANITYCHECK(SCL_DETAIL);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* New physical block. */
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(newpb,
|
2008-11-19 13:26:10 +01:00
|
|
|
newpb->phys = mem;
|
|
|
|
newpb->refcount = 1;
|
|
|
|
newpb->length = length;
|
2009-09-21 16:49:49 +02:00
|
|
|
newpb->firstregion = newphysr;);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* New physical region. */
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(newphysr,
|
|
|
|
newphysr->offset = offset;
|
2008-11-19 13:26:10 +01:00
|
|
|
newphysr->ph = newpb;
|
2009-04-22 14:39:29 +02:00
|
|
|
newphysr->parent = region;
|
2009-09-21 16:49:49 +02:00
|
|
|
newphysr->next_ph_list = NULL; /* No other references to this block. */);
|
2009-09-27 14:44:36 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
USE(newphysr, newphysr->written = 0;);
|
|
|
|
#endif
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* Update pagetable. */
|
|
|
|
vm_assert(!(length % VM_PAGE_SIZE));
|
|
|
|
vm_assert(!(newpb->length % VM_PAGE_SIZE));
|
2009-04-22 14:39:29 +02:00
|
|
|
SANITYCHECK(SCL_DETAIL);
|
2009-09-21 16:49:49 +02:00
|
|
|
if(map_ph_writept(vmp, region, newphysr) != OK) {
|
2008-12-08 17:43:20 +01:00
|
|
|
if(what_mem == MAP_NONE)
|
2008-11-19 13:26:10 +01:00
|
|
|
FREE_MEM(mem_clicks, clicks);
|
|
|
|
SLABFREE(newpb);
|
|
|
|
SLABFREE(newphysr);
|
2009-09-21 16:49:49 +02:00
|
|
|
printf("map_new_physblock: map_ph_writept failed\n");
|
|
|
|
return NULL;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_insert(region->phys, newphysr);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
return newphysr;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* map_copy_ph_block *
|
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE int map_copy_ph_block(vmp, region, ph)
|
|
|
|
struct vmproc *vmp;
|
|
|
|
struct vir_region *region;
|
|
|
|
struct phys_region *ph;
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
phys_bytes newmem, newmem_cl, clicks;
|
|
|
|
struct phys_block *newpb;
|
2009-02-12 13:26:08 +01:00
|
|
|
u32_t af = 0;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* This is only to be done if there is more than one copy. */
|
|
|
|
vm_assert(ph->ph->refcount > 1);
|
|
|
|
|
2009-04-22 14:39:29 +02:00
|
|
|
/* Do actual copy on write; allocate new physblock. */
|
2008-11-19 13:26:10 +01:00
|
|
|
if(!SLABALLOC(newpb)) {
|
|
|
|
printf("VM: map_copy_ph_block: couldn't allocate newpb\n");
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
clicks = CLICKSPERPAGE * ph->ph->length / VM_PAGE_SIZE;
|
|
|
|
vm_assert(CLICK2ABS(clicks) == ph->ph->length);
|
2009-02-12 13:26:08 +01:00
|
|
|
if(region->flags & VR_PHYS64K)
|
|
|
|
af |= PAF_ALIGN64K;
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
NOTRUNNABLE(vmp->vm_endpoint);
|
|
|
|
|
2009-02-12 13:26:08 +01:00
|
|
|
if((newmem_cl = ALLOC_MEM(clicks, af)) == NO_MEM) {
|
2009-09-21 16:49:49 +02:00
|
|
|
printf("VM: map_copy_ph_block: couldn't allocate new block\n");
|
2008-11-19 13:26:10 +01:00
|
|
|
SLABFREE(newpb);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
newmem = CLICK2ABS(newmem_cl);
|
2009-04-22 14:39:29 +02:00
|
|
|
vm_assert(ABS2CLICK(newmem) == newmem_cl);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-04-22 14:39:29 +02:00
|
|
|
pb_unreferenced(region, ph);
|
2008-11-19 13:26:10 +01:00
|
|
|
vm_assert(ph->ph->refcount > 0);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
USE(newpb,
|
2008-11-19 13:26:10 +01:00
|
|
|
newpb->length = ph->ph->length;
|
|
|
|
newpb->refcount = 1;
|
|
|
|
newpb->phys = newmem;
|
2009-09-21 16:49:49 +02:00
|
|
|
newpb->firstregion = ph;);
|
|
|
|
|
|
|
|
USE(ph, ph->next_ph_list = NULL;);
|
|
|
|
|
|
|
|
NOTRUNNABLE(vmp->vm_endpoint);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* Copy old memory to new memory. */
|
|
|
|
if((r=sys_abscopy(ph->ph->phys, newpb->phys, newpb->length)) != OK) {
|
|
|
|
printf("VM: map_copy_ph_block: sys_abscopy failed\n");
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if VMSTATS
|
|
|
|
vmp->vm_bytecopies += newpb->length;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Reference new block. */
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(ph, ph->ph = newpb;);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* Update pagetable with new address.
|
|
|
|
* This will also make it writable.
|
|
|
|
*/
|
2009-09-21 16:49:49 +02:00
|
|
|
r = map_ph_writept(vmp, region, ph);
|
2008-11-19 13:26:10 +01:00
|
|
|
if(r != OK)
|
|
|
|
vm_panic("map_copy_ph_block: map_ph_writept failed", r);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2009-04-22 14:39:29 +02:00
|
|
|
* map_pf *
|
2008-11-19 13:26:10 +01:00
|
|
|
*===========================================================================*/
|
2009-04-22 14:39:29 +02:00
|
|
|
PUBLIC int map_pf(vmp, region, offset, write)
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vmproc *vmp;
|
|
|
|
struct vir_region *region;
|
|
|
|
vir_bytes offset;
|
|
|
|
int write;
|
|
|
|
{
|
|
|
|
vir_bytes virpage;
|
|
|
|
struct phys_region *ph;
|
2009-09-21 16:49:49 +02:00
|
|
|
int r = OK;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
vm_assert(offset >= 0);
|
|
|
|
vm_assert(offset < region->length);
|
|
|
|
|
|
|
|
vm_assert(region->flags & VR_ANON);
|
|
|
|
vm_assert(!(region->vaddr % VM_PAGE_SIZE));
|
|
|
|
|
|
|
|
virpage = offset - offset % VM_PAGE_SIZE;
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
NOTRUNNABLE(vmp->vm_endpoint);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
if((ph = physr_search(region->phys, offset, AVL_LESS_EQUAL)) &&
|
|
|
|
(ph->offset <= offset && offset < ph->offset + ph->ph->length)) {
|
2008-11-19 13:26:10 +01:00
|
|
|
/* Pagefault in existing block. Do copy-on-write. */
|
|
|
|
vm_assert(write);
|
|
|
|
vm_assert(region->flags & VR_WRITABLE);
|
|
|
|
vm_assert(ph->ph->refcount > 0);
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
if(WRITABLE(region, ph->ph)) {
|
|
|
|
r = map_ph_writept(vmp, region, ph);
|
|
|
|
if(r != OK)
|
|
|
|
printf("map_ph_writept failed\n");
|
|
|
|
} else {
|
2008-11-19 13:26:10 +01:00
|
|
|
r = map_copy_ph_block(vmp, region, ph);
|
2009-09-21 16:49:49 +02:00
|
|
|
if(r != OK)
|
|
|
|
printf("map_copy_ph_block failed\n");
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
} else {
|
|
|
|
/* Pagefault in non-existing block. Map in new block. */
|
2009-09-21 16:49:49 +02:00
|
|
|
if(!map_new_physblock(vmp, region, virpage, VM_PAGE_SIZE, MAP_NONE)) {
|
|
|
|
printf("map_new_physblock failed\n");
|
|
|
|
r = ENOMEM;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-27 14:44:36 +02:00
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
if(r != OK) {
|
2009-04-22 14:39:29 +02:00
|
|
|
printf("VM: map_pf: failed (%d)\n", r);
|
2009-09-27 14:44:36 +02:00
|
|
|
return r;
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-27 14:44:36 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
if(OK != pt_checkrange(&vmp->vm_pt, region->vaddr+offset, VM_PAGE_SIZE, write)) {
|
|
|
|
vm_panic("map_pf: pt_checkrange failed", r);
|
|
|
|
}
|
|
|
|
#endif
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* map_handle_memory *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int map_handle_memory(vmp, region, offset, length, write)
|
|
|
|
struct vmproc *vmp;
|
|
|
|
struct vir_region *region;
|
|
|
|
vir_bytes offset, length;
|
|
|
|
int write;
|
|
|
|
{
|
2009-09-21 16:49:49 +02:00
|
|
|
struct phys_region *physr, *nextphysr;
|
2008-11-19 13:26:10 +01:00
|
|
|
int changes = 0;
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_iter iter;
|
|
|
|
|
|
|
|
NOTRUNNABLE(vmp->vm_endpoint);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#define FREE_RANGE_HERE(er1, er2) { \
|
|
|
|
struct phys_region *r1 = (er1), *r2 = (er2); \
|
|
|
|
vir_bytes start = offset, end = offset + length; \
|
2009-09-21 16:49:49 +02:00
|
|
|
if(r1) { \
|
|
|
|
start = MAX(start, r1->offset + r1->ph->length); } \
|
|
|
|
if(r2) { \
|
|
|
|
end = MIN(end, r2->offset); } \
|
2008-11-19 13:26:10 +01:00
|
|
|
if(start < end) { \
|
|
|
|
int r; \
|
|
|
|
SANITYCHECK(SCL_DETAIL); \
|
2009-09-21 16:49:49 +02:00
|
|
|
if(!map_new_physblock(vmp, region, start, \
|
|
|
|
end-start, MAP_NONE) != OK) { \
|
2008-11-19 13:26:10 +01:00
|
|
|
SANITYCHECK(SCL_DETAIL); \
|
2009-09-21 16:49:49 +02:00
|
|
|
return ENOMEM; \
|
2008-11-19 13:26:10 +01:00
|
|
|
} \
|
|
|
|
changes++; \
|
|
|
|
} }
|
|
|
|
|
2009-09-27 14:44:36 +02:00
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
vm_assert(region->flags & VR_ANON);
|
|
|
|
vm_assert(!(region->vaddr % VM_PAGE_SIZE));
|
|
|
|
vm_assert(!(offset % VM_PAGE_SIZE));
|
|
|
|
vm_assert(!(length % VM_PAGE_SIZE));
|
|
|
|
vm_assert(!write || (region->flags & VR_WRITABLE));
|
|
|
|
|
2009-09-27 14:44:36 +02:00
|
|
|
physr_start_iter(region->phys, &iter, offset, AVL_LESS_EQUAL);
|
2009-09-21 16:49:49 +02:00
|
|
|
physr = physr_get_iter(&iter);
|
|
|
|
|
2009-09-27 14:44:36 +02:00
|
|
|
if(!physr) {
|
|
|
|
physr_start_iter(region->phys, &iter, offset, AVL_GREATER_EQUAL);
|
|
|
|
physr = physr_get_iter(&iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define RESET_ITER(it, where, what) { \
|
|
|
|
physr_start_iter(region->phys, &it, where, AVL_EQUAL); \
|
|
|
|
what = physr_get_iter(&it); \
|
|
|
|
if(!what) vm_panic("thing missing", NO_NUM); \
|
|
|
|
}
|
|
|
|
|
|
|
|
FREE_RANGE_HERE(NULL, physr);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2009-09-27 14:44:36 +02:00
|
|
|
if(physr) {
|
|
|
|
RESET_ITER(iter, physr->offset, physr);
|
|
|
|
if(physr->offset + physr->ph->length <= offset) {
|
|
|
|
physr_incr_iter(&iter);
|
|
|
|
physr = physr_get_iter(&iter);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2009-09-27 14:44:36 +02:00
|
|
|
if(physr) {
|
|
|
|
FREE_RANGE_HERE(NULL, physr);
|
|
|
|
RESET_ITER(iter, physr->offset, physr);
|
|
|
|
}
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
while(physr) {
|
2008-11-19 13:26:10 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_DETAIL);
|
|
|
|
|
|
|
|
if(write) {
|
|
|
|
vm_assert(physr->ph->refcount > 0);
|
2009-09-21 16:49:49 +02:00
|
|
|
if(!WRITABLE(region, physr->ph)) {
|
2008-11-19 13:26:10 +01:00
|
|
|
SANITYCHECK(SCL_DETAIL);
|
|
|
|
r = map_copy_ph_block(vmp, region, physr);
|
|
|
|
if(r != OK) {
|
|
|
|
printf("VM: map_handle_memory: no copy\n");
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
changes++;
|
|
|
|
SANITYCHECK(SCL_DETAIL);
|
|
|
|
} else {
|
|
|
|
SANITYCHECK(SCL_DETAIL);
|
2009-09-21 16:49:49 +02:00
|
|
|
if((r=map_ph_writept(vmp, region, physr)) != OK) {
|
2008-11-19 13:26:10 +01:00
|
|
|
printf("VM: map_ph_writept failed\n");
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
changes++;
|
|
|
|
SANITYCHECK(SCL_DETAIL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_DETAIL);
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_incr_iter(&iter);
|
|
|
|
nextphysr = physr_get_iter(&iter);
|
|
|
|
FREE_RANGE_HERE(physr, nextphysr);
|
2008-11-19 13:26:10 +01:00
|
|
|
SANITYCHECK(SCL_DETAIL);
|
2009-09-21 16:49:49 +02:00
|
|
|
if(nextphysr) {
|
|
|
|
if(nextphysr->offset >= offset + length)
|
|
|
|
break;
|
2009-09-27 14:44:36 +02:00
|
|
|
RESET_ITER(iter, nextphysr->offset, nextphysr);
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
|
|
|
physr = nextphysr;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
vm_assert(changes > 0);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-27 14:44:36 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
if(OK != pt_checkrange(&vmp->vm_pt, region->vaddr+offset, length, write)) {
|
|
|
|
printf("handle mem %s-", arch_map2str(vmp, region->vaddr+offset));
|
|
|
|
printf("%s failed\n", arch_map2str(vmp, region->vaddr+offset+length));
|
|
|
|
map_printregion(vmp, region);
|
|
|
|
vm_panic("checkrange failed", NO_NUM);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if SANITYCHECKS
|
|
|
|
static int countregions(struct vir_region *vr)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
struct phys_region *ph;
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_iter iter;
|
|
|
|
physr_start_iter_least(vr->phys, &iter);
|
|
|
|
while((ph = physr_get_iter(&iter))) {
|
2008-11-19 13:26:10 +01:00
|
|
|
n++;
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_incr_iter(&iter);
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* map_copy_region *
|
|
|
|
*===========================================================================*/
|
2009-09-21 16:49:49 +02:00
|
|
|
PRIVATE struct vir_region *map_copy_region(struct vmproc *vmp, struct vir_region *vr)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
2009-04-22 14:39:29 +02:00
|
|
|
/* map_copy_region creates a complete copy of the vir_region
|
|
|
|
* data structure, linking in the same phys_blocks directly,
|
|
|
|
* but all in limbo, i.e., the caller has to link the vir_region
|
|
|
|
* to a process. Therefore it doesn't increase the refcount in
|
|
|
|
* the phys_block; the caller has to do this once it's linked.
|
|
|
|
* The reason for this is to keep the sanity checks working
|
|
|
|
* within this function.
|
|
|
|
*/
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vir_region *newvr;
|
2009-09-21 16:49:49 +02:00
|
|
|
struct phys_region *ph;
|
|
|
|
physr_iter iter;
|
|
|
|
physr_avl *phavl;
|
2008-11-19 13:26:10 +01:00
|
|
|
#if SANITYCHECKS
|
|
|
|
int cr;
|
|
|
|
cr = countregions(vr);
|
|
|
|
#endif
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
if(!SLABALLOC(newvr))
|
|
|
|
return NULL;
|
2009-09-21 16:49:49 +02:00
|
|
|
SLABALLOC(phavl);
|
|
|
|
if(!phavl) {
|
|
|
|
SLABFREE(newvr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
USE(newvr,
|
|
|
|
*newvr = *vr;
|
|
|
|
newvr->next = NULL;
|
|
|
|
newvr->phys = phavl;
|
|
|
|
);
|
|
|
|
physr_init(newvr->phys);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_start_iter_least(vr->phys, &iter);
|
|
|
|
while((ph = physr_get_iter(&iter))) {
|
2008-11-19 13:26:10 +01:00
|
|
|
struct phys_region *newph;
|
|
|
|
if(!SLABALLOC(newph)) {
|
2009-09-21 16:49:49 +02:00
|
|
|
map_free(vmp, newvr);
|
2008-11-19 13:26:10 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(newph,
|
2008-11-19 13:26:10 +01:00
|
|
|
newph->ph = ph->ph;
|
2009-04-22 14:39:29 +02:00
|
|
|
newph->next_ph_list = NULL;
|
|
|
|
newph->parent = newvr;
|
2009-09-21 16:49:49 +02:00
|
|
|
newph->offset = ph->offset;);
|
2009-09-27 14:44:36 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
USE(newph, newph->written = 0;);
|
|
|
|
#endif
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_insert(newvr->phys, newph);
|
2008-11-19 13:26:10 +01:00
|
|
|
vm_assert(countregions(vr) == cr);
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_incr_iter(&iter);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
vm_assert(countregions(vr) == countregions(newvr));
|
|
|
|
|
|
|
|
return newvr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*=========================================================================*
|
|
|
|
* map_writept *
|
|
|
|
*=========================================================================*/
|
|
|
|
PUBLIC int map_writept(struct vmproc *vmp)
|
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
|
|
|
struct phys_region *ph;
|
2009-09-21 16:49:49 +02:00
|
|
|
int r;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
for(vr = vmp->vm_regions; vr; vr = vr->next) {
|
|
|
|
physr_iter iter;
|
|
|
|
physr_start_iter_least(vr->phys, &iter);
|
|
|
|
while((ph = physr_get_iter(&iter))) {
|
|
|
|
if((r=map_ph_writept(vmp, vr, ph)) != OK) {
|
|
|
|
printf("VM: map_writept: failed\n");
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
physr_incr_iter(&iter);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* map_proc_copy *
|
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC int map_proc_copy(dst, src)
|
|
|
|
struct vmproc *dst;
|
|
|
|
struct vmproc *src;
|
|
|
|
{
|
|
|
|
struct vir_region *vr, *prevvr = NULL;
|
|
|
|
dst->vm_regions = NULL;
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
PT_SANE(&src->vm_pt);
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
for(vr = src->vm_regions; vr; vr = vr->next) {
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_iter iter_orig, iter_new;
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vir_region *newvr;
|
2009-04-22 14:39:29 +02:00
|
|
|
struct phys_region *orig_ph, *new_ph;
|
2009-09-21 16:49:49 +02:00
|
|
|
if(!(newvr = map_copy_region(dst, vr))) {
|
2008-11-19 13:26:10 +01:00
|
|
|
map_free_proc(dst);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(newvr, newvr->parent = dst;);
|
|
|
|
if(prevvr) { USE(prevvr, prevvr->next = newvr;); }
|
2008-11-19 13:26:10 +01:00
|
|
|
else { dst->vm_regions = newvr; }
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_start_iter_least(vr->phys, &iter_orig);
|
|
|
|
physr_start_iter_least(newvr->phys, &iter_new);
|
|
|
|
while((orig_ph = physr_get_iter(&iter_orig))) {
|
2009-04-22 14:39:29 +02:00
|
|
|
struct phys_block *pb;
|
2009-09-21 16:49:49 +02:00
|
|
|
new_ph = physr_get_iter(&iter_new);
|
2009-04-22 14:39:29 +02:00
|
|
|
/* Check two physregions both are nonnull,
|
|
|
|
* are different, and match physblocks.
|
|
|
|
*/
|
2009-09-21 16:49:49 +02:00
|
|
|
vm_assert(new_ph);
|
|
|
|
vm_assert(orig_ph);
|
2009-04-22 14:39:29 +02:00
|
|
|
vm_assert(orig_ph != new_ph);
|
|
|
|
pb = orig_ph->ph;
|
|
|
|
vm_assert(pb == new_ph->ph);
|
|
|
|
|
|
|
|
/* Link in new physregion. */
|
|
|
|
vm_assert(!new_ph->next_ph_list);
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(new_ph, new_ph->next_ph_list = pb->firstregion;);
|
|
|
|
USE(pb, pb->firstregion = new_ph;);
|
2009-04-22 14:39:29 +02:00
|
|
|
|
|
|
|
/* Increase phys block refcount */
|
|
|
|
vm_assert(pb->refcount > 0);
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(pb, pb->refcount++;);
|
2009-04-22 14:39:29 +02:00
|
|
|
vm_assert(pb->refcount > 1);
|
|
|
|
|
|
|
|
/* Get next new physregion */
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_incr_iter(&iter_orig);
|
|
|
|
physr_incr_iter(&iter_new);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
vm_assert(!physr_get_iter(&iter_new));
|
2008-11-19 13:26:10 +01:00
|
|
|
prevvr = newvr;
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
map_writept(src);
|
|
|
|
map_writept(dst);
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* map_proc_kernel *
|
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC struct vir_region *map_proc_kernel(struct vmproc *vmp)
|
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
|
|
|
|
|
|
|
/* We assume these are the first regions to be mapped to
|
|
|
|
* make the function a bit simpler (free all regions on error).
|
|
|
|
*/
|
|
|
|
vm_assert(!vmp->vm_regions);
|
|
|
|
vm_assert(vmproc[VMP_SYSTEM].vm_flags & VMF_INUSE);
|
|
|
|
vm_assert(!(KERNEL_TEXT % VM_PAGE_SIZE));
|
|
|
|
vm_assert(!(KERNEL_TEXT_LEN % VM_PAGE_SIZE));
|
|
|
|
vm_assert(!(KERNEL_DATA % VM_PAGE_SIZE));
|
|
|
|
vm_assert(!(KERNEL_DATA_LEN % VM_PAGE_SIZE));
|
|
|
|
|
|
|
|
if(!(vr = map_page_region(vmp, KERNEL_TEXT, 0, KERNEL_TEXT_LEN,
|
|
|
|
KERNEL_TEXT, VR_DIRECT | VR_WRITABLE | VR_NOPF, 0)) ||
|
|
|
|
!(vr = map_page_region(vmp, KERNEL_DATA, 0, KERNEL_DATA_LEN,
|
|
|
|
KERNEL_DATA, VR_DIRECT | VR_WRITABLE | VR_NOPF, 0))) {
|
|
|
|
map_free_proc(vmp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vr; /* Return pointer not useful, just non-NULL. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* map_region_extend *
|
|
|
|
*========================================================================*/
|
2008-12-08 17:43:20 +01:00
|
|
|
PUBLIC int map_region_extend(struct vmproc *vmp, struct vir_region *vr,
|
|
|
|
vir_bytes delta)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
|
|
|
vir_bytes end;
|
|
|
|
|
|
|
|
vm_assert(vr);
|
|
|
|
vm_assert(vr->flags & VR_ANON);
|
|
|
|
vm_assert(!(delta % VM_PAGE_SIZE));
|
|
|
|
|
|
|
|
if(!delta) return OK;
|
|
|
|
end = vr->vaddr + vr->length;
|
|
|
|
vm_assert(end >= vr->vaddr);
|
|
|
|
|
|
|
|
if(end + delta <= end) {
|
|
|
|
printf("VM: strange delta 0x%lx\n", delta);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!vr->next || end + delta <= vr->next->vaddr) {
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(vr, vr->length += delta;);
|
2008-11-19 13:26:10 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2008-12-08 17:43:20 +01:00
|
|
|
map_printmap(vmp);
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* map_region_shrink *
|
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC int map_region_shrink(struct vir_region *vr, vir_bytes delta)
|
|
|
|
{
|
|
|
|
vm_assert(vr);
|
|
|
|
vm_assert(vr->flags & VR_ANON);
|
|
|
|
vm_assert(!(delta % VM_PAGE_SIZE));
|
|
|
|
|
2008-12-08 17:43:20 +01:00
|
|
|
#if 0
|
2008-11-19 13:26:10 +01:00
|
|
|
printf("VM: ignoring region shrink\n");
|
2008-12-08 17:43:20 +01:00
|
|
|
#endif
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PUBLIC struct vir_region *map_region_lookup_tag(vmp, tag)
|
|
|
|
struct vmproc *vmp;
|
|
|
|
u32_t tag;
|
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
|
|
|
|
|
|
|
for(vr = vmp->vm_regions; vr; vr = vr->next)
|
|
|
|
if(vr->tag == tag)
|
|
|
|
return vr;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PUBLIC void map_region_set_tag(struct vir_region *vr, u32_t tag)
|
|
|
|
{
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(vr, vr->tag = tag;);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PUBLIC u32_t map_region_get_tag(struct vir_region *vr)
|
|
|
|
{
|
|
|
|
return vr->tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* map_unmap_region *
|
|
|
|
*========================================================================*/
|
2009-09-21 16:49:49 +02:00
|
|
|
PUBLIC int map_unmap_region(struct vmproc *vmp, struct vir_region *region,
|
|
|
|
vir_bytes len)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
2009-09-21 16:49:49 +02:00
|
|
|
/* Shrink the region by 'len' bytes, from the start. Unreference
|
|
|
|
* memory it used to reference if any.
|
|
|
|
*/
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vir_region *r, *nextr, *prev = NULL;
|
2009-09-21 16:49:49 +02:00
|
|
|
vir_bytes regionstart;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
for(r = vmp->vm_regions; r; r = r->next) {
|
|
|
|
if(r == region)
|
|
|
|
break;
|
|
|
|
|
|
|
|
prev = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_DETAIL);
|
|
|
|
|
|
|
|
if(r == NULL)
|
|
|
|
vm_panic("map_unmap_region: region not found\n", NO_NUM);
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
if(len > r->length || (len % VM_PAGE_SIZE)) {
|
|
|
|
printf("VM: bogus length 0x%lx\n", len);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2009-09-22 13:51:17 +02:00
|
|
|
if(!(r->flags & (VR_ANON|VR_DIRECT))) {
|
|
|
|
printf("VM: only unmap anonymous or direct memory\n");
|
2009-09-21 16:49:49 +02:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
regionstart = r->vaddr;
|
|
|
|
|
|
|
|
if(len == r->length) {
|
|
|
|
/* Whole region disappears. Unlink and free it. */
|
|
|
|
if(!prev) {
|
|
|
|
vmp->vm_regions = r->next;
|
|
|
|
} else {
|
|
|
|
USE(prev, prev->next = r->next;);
|
|
|
|
}
|
|
|
|
map_free(vmp, r);
|
|
|
|
} else {
|
|
|
|
struct phys_region *pr;
|
|
|
|
physr_iter iter;
|
|
|
|
/* Region shrinks. First unreference its memory
|
|
|
|
* and then shrink the region.
|
|
|
|
*/
|
|
|
|
map_subfree(vmp, r, len);
|
|
|
|
USE(r,
|
|
|
|
r->vaddr += len;
|
|
|
|
r->length -= len;);
|
|
|
|
physr_start_iter_least(r->phys, &iter);
|
|
|
|
|
|
|
|
/* vaddr has increased; to make all the phys_regions
|
|
|
|
* point to the same addresses, make them shrink by the
|
|
|
|
* same amount.
|
|
|
|
*/
|
|
|
|
while((pr = physr_get_iter(&iter))) {
|
|
|
|
vm_assert(pr->offset >= len);
|
|
|
|
USE(pr, pr->offset -= len;);
|
|
|
|
physr_incr_iter(&iter);
|
|
|
|
}
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
SANITYCHECK(SCL_DETAIL);
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
if(pt_writemap(&vmp->vm_pt, regionstart,
|
|
|
|
MAP_NONE, len, 0, WMF_OVERWRITE) != OK) {
|
2008-11-19 13:26:10 +01:00
|
|
|
printf("VM: map_unmap_region: pt_writemap failed\n");
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* map_remap *
|
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC int map_remap(struct vmproc *dvmp, vir_bytes da, size_t size,
|
|
|
|
struct vir_region *region, vir_bytes *r)
|
|
|
|
{
|
|
|
|
struct vir_region *vr, *prev;
|
|
|
|
struct phys_region *ph;
|
|
|
|
vir_bytes startv, dst_addr;
|
|
|
|
physr_iter iter;
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
/* da is handled differently */
|
|
|
|
if (!da)
|
|
|
|
dst_addr = dvmp->vm_stacktop;
|
|
|
|
else
|
|
|
|
dst_addr = da;
|
|
|
|
dst_addr = arch_vir2map(dvmp, dst_addr);
|
|
|
|
|
|
|
|
prev = NULL;
|
|
|
|
/* round up to page size */
|
|
|
|
if (size % I386_PAGE_SIZE)
|
|
|
|
size += I386_PAGE_SIZE - size % I386_PAGE_SIZE;
|
|
|
|
startv = region_find_slot(dvmp, dst_addr, VM_DATATOP, size, &prev);
|
|
|
|
if (startv == (vir_bytes) -1) {
|
|
|
|
printf("map_remap: search %x...\n", dst_addr);
|
|
|
|
map_printmap(dvmp);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
/* when the user specifies the address, we cannot change it */
|
|
|
|
if (da && (startv != dst_addr))
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
vr = map_copy_region(dvmp, region);
|
|
|
|
if(!vr)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
USE(vr,
|
|
|
|
vr->vaddr = startv;
|
|
|
|
vr->length = size;
|
|
|
|
vr->flags = region->flags;
|
|
|
|
vr->tag = VRT_NONE;
|
|
|
|
vr->parent = dvmp;);
|
|
|
|
vm_assert(vr->flags & VR_SHARED);
|
|
|
|
|
|
|
|
if (prev) {
|
|
|
|
USE(vr,
|
|
|
|
vr->next = prev->next;);
|
|
|
|
USE(prev, prev->next = vr;);
|
|
|
|
} else {
|
|
|
|
USE(vr,
|
|
|
|
vr->next = dvmp->vm_regions;);
|
|
|
|
dvmp->vm_regions = vr;
|
|
|
|
}
|
|
|
|
|
|
|
|
physr_start_iter_least(vr->phys, &iter);
|
|
|
|
while((ph = physr_get_iter(&iter))) {
|
|
|
|
struct phys_block *pb = ph->ph;
|
|
|
|
USE(pb, pb->refcount++;);
|
|
|
|
if(map_ph_writept(dvmp, vr, ph) != OK) {
|
|
|
|
vm_panic("map_remap: map_ph_writept failed", NO_NUM);
|
|
|
|
}
|
|
|
|
|
|
|
|
physr_incr_iter(&iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
*r = startv;
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* map_get_phys *
|
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC int map_get_phys(struct vmproc *vmp, vir_bytes addr, phys_bytes *r)
|
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
|
|
|
struct phys_region *ph;
|
|
|
|
physr_iter iter;
|
|
|
|
|
|
|
|
if (!(vr = map_lookup(vmp, addr)) ||
|
|
|
|
(vr->vaddr != addr))
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (!(vr->flags & VR_SHARED))
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
physr_start_iter_least(vr->phys, &iter);
|
|
|
|
ph = physr_get_iter(&iter);
|
|
|
|
|
|
|
|
vm_assert(ph);
|
|
|
|
vm_assert(ph->ph);
|
|
|
|
if (r)
|
|
|
|
*r = ph->ph->phys;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* map_get_ref *
|
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC int map_get_ref(struct vmproc *vmp, vir_bytes addr, u8_t *cnt)
|
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
|
|
|
struct phys_region *ph;
|
|
|
|
physr_iter iter;
|
|
|
|
|
|
|
|
if (!(vr = map_lookup(vmp, addr)) ||
|
|
|
|
(vr->vaddr != addr))
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (!(vr->flags & VR_SHARED))
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
physr_start_iter_least(vr->phys, &iter);
|
|
|
|
ph = physr_get_iter(&iter);
|
|
|
|
|
|
|
|
vm_assert(ph);
|
|
|
|
vm_assert(ph->ph);
|
|
|
|
if (cnt)
|
|
|
|
*cnt = ph->ph->refcount;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* regionprintstats *
|
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC void printregionstats(struct vmproc *vmp)
|
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
|
|
|
struct phys_region *pr;
|
|
|
|
physr_iter iter;
|
|
|
|
vir_bytes used = 0, weighted = 0;
|
|
|
|
|
|
|
|
for(vr = vmp->vm_regions; vr; vr = vr->next) {
|
|
|
|
if(vr->flags & VR_DIRECT)
|
|
|
|
continue;
|
|
|
|
physr_start_iter_least(vr->phys, &iter);
|
|
|
|
while((pr = physr_get_iter(&iter))) {
|
|
|
|
physr_incr_iter(&iter);
|
|
|
|
used += pr->ph->length;
|
|
|
|
weighted += pr->ph->length / pr->ph->refcount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%6dkB %6dkB\n", used/1024, weighted/1024);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|