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>
|
2010-10-15 11:10:14 +02:00
|
|
|
#include <minix/hash.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>
|
2010-08-21 15:10:41 +02:00
|
|
|
#include <sys/param.h>
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#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"
|
2010-04-12 13:25:24 +02:00
|
|
|
#include "memlist.h"
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
/* LRU list. */
|
|
|
|
PRIVATE yielded_t *lru_youngest = NULL, *lru_oldest = NULL;
|
|
|
|
|
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))
|
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
FORWARD _PROTOTYPE(int map_new_physblock, (struct vmproc *vmp,
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vir_region *region, vir_bytes offset, vir_bytes length,
|
2010-04-12 13:25:24 +02:00
|
|
|
phys_bytes what, u32_t allocflags, int written));
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
FORWARD _PROTOTYPE(phys_bytes freeyieldednode, (yielded_t *node, int freemem));
|
|
|
|
|
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
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
FORWARD _PROTOTYPE(struct phys_region *map_clone_ph_block, (struct vmproc *vmp,
|
|
|
|
struct vir_region *region, struct phys_region *ph, physr_iter *iter));
|
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
FORWARD _PROTOTYPE(void lrucheck, (void));
|
|
|
|
#endif
|
|
|
|
|
2010-10-15 11:10:14 +02:00
|
|
|
/* hash table of yielded blocks */
|
|
|
|
#define YIELD_HASHSIZE 65536
|
|
|
|
PRIVATE yielded_avl vm_yielded_blocks[YIELD_HASHSIZE];
|
|
|
|
|
|
|
|
PRIVATE int avl_inited = 0;
|
|
|
|
|
|
|
|
PUBLIC void map_region_init(void)
|
|
|
|
{
|
|
|
|
int h;
|
|
|
|
assert(!avl_inited);
|
|
|
|
for(h = 0; h < YIELD_HASHSIZE; h++)
|
|
|
|
yielded_init(&vm_yielded_blocks[h]);
|
|
|
|
avl_inited = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIVATE yielded_avl *get_yielded_avl(block_id_t id)
|
|
|
|
{
|
|
|
|
u32_t h;
|
|
|
|
|
|
|
|
assert(avl_inited);
|
|
|
|
|
|
|
|
hash_i_64(id.owner, id.id, h);
|
|
|
|
h = h % YIELD_HASHSIZE;
|
|
|
|
|
|
|
|
assert(h >= 0);
|
|
|
|
assert(h < YIELD_HASHSIZE);
|
|
|
|
|
|
|
|
return &vm_yielded_blocks[h];
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
PRIVATE char *map_name(struct vir_region *vr)
|
|
|
|
{
|
2010-03-18 18:17:31 +01:00
|
|
|
static char name[100];
|
|
|
|
char *typename, *tag;
|
2008-11-19 13:26:10 +01:00
|
|
|
int type = vr->flags & (VR_ANON|VR_DIRECT);
|
|
|
|
switch(type) {
|
|
|
|
case VR_ANON:
|
2010-03-18 18:17:31 +01:00
|
|
|
typename = "anonymous";
|
|
|
|
break;
|
2008-11-19 13:26:10 +01:00
|
|
|
case VR_DIRECT:
|
2010-03-18 18:17:31 +01:00
|
|
|
typename = "direct";
|
|
|
|
break;
|
2008-11-19 13:26:10 +01:00
|
|
|
default:
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("unknown mapping type: %d", type);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2010-03-18 18:17:31 +01:00
|
|
|
switch(vr->tag) {
|
|
|
|
case VRT_TEXT:
|
|
|
|
tag = "text";
|
|
|
|
break;
|
|
|
|
case VRT_STACK:
|
|
|
|
tag = "stack";
|
|
|
|
break;
|
|
|
|
case VRT_HEAP:
|
|
|
|
tag = "heap";
|
|
|
|
break;
|
|
|
|
case VRT_NONE:
|
|
|
|
tag = "untagged";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tag = "unknown tag value";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(name, "%s, %s", typename, tag);
|
|
|
|
|
|
|
|
return name;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
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));
|
2010-07-05 15:58:57 +02:00
|
|
|
printf("\t%s (len 0x%lx, %lukB), %s\n",
|
2009-09-27 14:44:36 +02:00
|
|
|
arch_map2str(vmp, vr->vaddr), vr->length,
|
2010-03-18 18:17:31 +01:00
|
|
|
vr->length/1024, map_name(vr));
|
2009-09-27 14:44:36 +02:00
|
|
|
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;
|
2010-10-04 13:41:10 +02:00
|
|
|
region_iter iter;
|
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);
|
2010-10-04 13:41:10 +02:00
|
|
|
|
|
|
|
region_start_iter_least(&vmp->vm_regions_avl, &iter);
|
|
|
|
while((vr = region_get_iter(&iter))) {
|
2009-09-27 14:44:36 +02:00
|
|
|
map_printregion(vmp, vr);
|
2010-10-04 13:41:10 +02:00
|
|
|
region_incr_iter(&iter);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
PRIVATE struct vir_region *getnextvr(struct vir_region *vr)
|
|
|
|
{
|
|
|
|
struct vir_region *nextvr;
|
|
|
|
region_iter v_iter;
|
|
|
|
SLABSANE(vr);
|
|
|
|
region_start_iter(&vr->parent->vm_regions_avl, &v_iter, vr->vaddr, AVL_EQUAL);
|
|
|
|
assert(region_get_iter(&v_iter));
|
|
|
|
assert(region_get_iter(&v_iter) == vr);
|
|
|
|
region_incr_iter(&v_iter);
|
|
|
|
nextvr = region_get_iter(&v_iter);
|
|
|
|
if(!nextvr) return NULL;
|
|
|
|
SLABSANE(nextvr);
|
|
|
|
assert(vr->parent == nextvr->parent);
|
|
|
|
assert(vr->vaddr < nextvr->vaddr);
|
|
|
|
assert(vr->vaddr + vr->length <= nextvr->vaddr);
|
|
|
|
return nextvr;
|
|
|
|
}
|
2008-12-08 17:43:20 +01:00
|
|
|
|
|
|
|
#if SANITYCHECKS
|
2010-10-04 13:41:10 +02:00
|
|
|
|
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;
|
2010-04-12 13:25:24 +02:00
|
|
|
int r;
|
2009-09-23 15:33:01 +02:00
|
|
|
|
|
|
|
if(!(vmp->vm_flags & VMF_HASPT))
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
if(WRITABLE(vr, pb))
|
|
|
|
rw = PTF_WRITE;
|
|
|
|
else
|
|
|
|
rw = 0;
|
|
|
|
|
2010-09-15 16:11:12 +02:00
|
|
|
r = pt_writemap(vmp, &vmp->vm_pt, vr->vaddr + pr->offset,
|
2009-09-23 15:33:01 +02:00
|
|
|
pb->phys, pb->length, PTF_PRESENT | PTF_USER | rw, WMF_VERIFY);
|
2010-04-12 13:25:24 +02:00
|
|
|
|
|
|
|
if(r != OK) {
|
|
|
|
printf("proc %d phys_region 0x%lx sanity check failed\n",
|
|
|
|
vmp->vm_endpoint, pr->offset);
|
|
|
|
map_printregion(vmp, vr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
2009-09-23 15:33:01 +02:00
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*===========================================================================*
|
|
|
|
* map_sanitycheck *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void map_sanitycheck(char *file, int line)
|
|
|
|
{
|
|
|
|
struct vmproc *vmp;
|
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
lrucheck();
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/* 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++) { \
|
2010-10-04 13:41:10 +02:00
|
|
|
region_iter v_iter; \
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vir_region *vr; \
|
|
|
|
if(!(vmp->vm_flags & VMF_INUSE)) \
|
|
|
|
continue; \
|
2010-10-04 13:41:10 +02:00
|
|
|
region_start_iter_least(&vmp->vm_regions_avl, &v_iter); \
|
|
|
|
while((vr = region_get_iter(&v_iter))) { \
|
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
|
|
|
} \
|
2010-10-04 13:41:10 +02:00
|
|
|
region_incr_iter(&v_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) {
|
2010-10-04 13:41:10 +02:00
|
|
|
if(!(pr->parent->flags & VR_DIRECT)) {
|
|
|
|
MYASSERT(usedpages_add(pr->ph->phys,
|
|
|
|
pr->ph->length) == OK);
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
|
|
|
);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* Do consistency check. */
|
2010-10-04 13:41:10 +02:00
|
|
|
ALLREGIONS({ struct vir_region *nextvr = getnextvr(vr);
|
|
|
|
if(nextvr) {
|
|
|
|
MYASSERT(vr->vaddr < nextvr->vaddr);
|
|
|
|
MYASSERT(vr->vaddr + vr->length <= nextvr->vaddr);
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
MYASSERT(!(vr->vaddr % VM_PAGE_SIZE));,
|
|
|
|
if(pr->ph->refcount != pr->ph->seencount) {
|
|
|
|
map_printmap(vmp);
|
2010-07-05 15:58:57 +02:00
|
|
|
printf("ph in vr %p: 0x%lx-0x%lx refcount %d "
|
2008-11-19 13:26:10 +01:00
|
|
|
"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
|
|
|
}
|
2010-05-05 13:35:04 +02:00
|
|
|
|
|
|
|
#define LRUCHECK lrucheck()
|
|
|
|
|
|
|
|
PRIVATE void lrucheck(void)
|
|
|
|
{
|
|
|
|
yielded_t *list;
|
|
|
|
|
|
|
|
/* list is empty and ok if both ends point to null. */
|
|
|
|
if(!lru_youngest && !lru_oldest)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* if not, both should point to something. */
|
|
|
|
SLABSANE(lru_youngest);
|
|
|
|
SLABSANE(lru_oldest);
|
|
|
|
|
|
|
|
assert(!lru_youngest->younger);
|
|
|
|
assert(!lru_oldest->older);
|
|
|
|
|
|
|
|
for(list = lru_youngest; list; list = list->older) {
|
|
|
|
SLABSANE(list);
|
|
|
|
if(list->younger) {
|
|
|
|
SLABSANE(list->younger);
|
|
|
|
assert(list->younger->older == list);
|
|
|
|
} else assert(list == lru_youngest);
|
|
|
|
if(list->older) {
|
|
|
|
SLABSANE(list->older);
|
|
|
|
assert(list->older->younger == list);
|
|
|
|
} else assert(list == lru_oldest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void blockstats(void)
|
|
|
|
{
|
|
|
|
yielded_t *list;
|
|
|
|
int blocks = 0;
|
|
|
|
phys_bytes mem = 0;
|
|
|
|
clock_t ticks;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = getuptime(&ticks);
|
|
|
|
|
|
|
|
assert(s == OK);
|
|
|
|
|
|
|
|
LRUCHECK;
|
|
|
|
|
|
|
|
for(list = lru_youngest; list; list = list->older) {
|
|
|
|
mem += list->len;
|
|
|
|
blocks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(blocks > 0)
|
2010-07-05 15:58:57 +02:00
|
|
|
printf("%d blocks, %lukB; ", blocks, mem/1024);
|
2010-05-05 13:35:04 +02:00
|
|
|
|
|
|
|
printmemstats();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define LRUCHECK
|
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
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!(vr->vaddr % VM_PAGE_SIZE));
|
|
|
|
assert(!(pb->length % VM_PAGE_SIZE));
|
|
|
|
assert(!(pr->offset % VM_PAGE_SIZE));
|
|
|
|
assert(pb->refcount > 0);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
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;
|
|
|
|
|
2010-09-15 16:11:12 +02:00
|
|
|
if(pt_writemap(vmp, &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;
|
|
|
|
}
|
|
|
|
|
2010-10-07 12:04:05 +02:00
|
|
|
#define SLOT_FAIL ((vir_bytes) -1)
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*===========================================================================*
|
2010-10-07 12:04:05 +02:00
|
|
|
* region_find_slot_range *
|
2008-11-19 13:26:10 +01:00
|
|
|
*===========================================================================*/
|
2010-10-07 12:04:05 +02:00
|
|
|
PRIVATE vir_bytes region_find_slot_range(struct vmproc *vmp,
|
|
|
|
vir_bytes minv, vir_bytes maxv, vir_bytes length)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
2010-10-07 12:04:05 +02:00
|
|
|
struct vir_region *firstregion;
|
2011-06-01 11:30:58 +02:00
|
|
|
vir_bytes startv = 0;
|
2008-11-19 13:26:10 +01:00
|
|
|
int foundflag = 0;
|
2010-10-07 12:04:05 +02:00
|
|
|
region_iter iter;
|
2010-10-04 13:41:10 +02:00
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
/* Length must be reasonable. */
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(length > 0);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* 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);
|
2010-10-07 12:04:05 +02:00
|
|
|
return SLOT_FAIL;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Basic input sanity checks. */
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!(length % VM_PAGE_SIZE));
|
2008-11-19 13:26:10 +01:00
|
|
|
if(minv >= maxv) {
|
|
|
|
printf("VM: 1 minv: 0x%lx maxv: 0x%lx length: 0x%lx\n",
|
|
|
|
minv, maxv, length);
|
|
|
|
}
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(minv < maxv);
|
|
|
|
assert(minv + length <= maxv);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-10-07 12:04:05 +02:00
|
|
|
#define FREEVRANGE(rangestart, rangeend) { \
|
2008-11-19 13:26:10 +01:00
|
|
|
vir_bytes frstart = (rangestart), frend = (rangeend); \
|
|
|
|
frstart = MAX(frstart, minv); \
|
|
|
|
frend = MIN(frend, maxv); \
|
|
|
|
if(frend > frstart && (frend - frstart) >= length) { \
|
|
|
|
startv = frstart; \
|
|
|
|
foundflag = 1; \
|
|
|
|
} }
|
|
|
|
|
2010-10-07 12:04:05 +02:00
|
|
|
/* find region before minv. */
|
|
|
|
region_start_iter(&vmp->vm_regions_avl, &iter, minv, AVL_LESS_EQUAL);
|
|
|
|
firstregion = region_get_iter(&iter);
|
|
|
|
|
|
|
|
if(!firstregion) {
|
|
|
|
/* This is the free virtual address space before the first region. */
|
|
|
|
region_start_iter(&vmp->vm_regions_avl, &iter, minv, AVL_GREATER_EQUAL);
|
|
|
|
firstregion = region_get_iter(&iter);
|
|
|
|
FREEVRANGE(0, firstregion ? firstregion->vaddr : VM_DATATOP);
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
if(!foundflag) {
|
2009-09-21 16:49:49 +02:00
|
|
|
struct vir_region *vr;
|
2010-10-04 13:41:10 +02:00
|
|
|
while((vr = region_get_iter(&iter)) && !foundflag) {
|
|
|
|
struct vir_region *nextvr;
|
|
|
|
region_incr_iter(&iter);
|
|
|
|
nextvr = region_get_iter(&iter);
|
2008-11-19 13:26:10 +01:00
|
|
|
FREEVRANGE(vr->vaddr + vr->length,
|
2010-10-07 12:04:05 +02:00
|
|
|
nextvr ? nextvr->vaddr : VM_DATATOP);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2010-10-07 12:04:05 +02:00
|
|
|
return SLOT_FAIL;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* However we got it, startv must be in the requested range. */
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(startv >= minv);
|
|
|
|
assert(startv < maxv);
|
|
|
|
assert(startv + length <= maxv);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-10-07 12:04:05 +02:00
|
|
|
/* remember this position as a hint for next time. */
|
|
|
|
vmp->vm_region_top = startv + length;
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
return startv;
|
|
|
|
}
|
|
|
|
|
2010-10-07 12:04:05 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* region_find_slot *
|
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE vir_bytes region_find_slot(struct vmproc *vmp,
|
|
|
|
vir_bytes minv, vir_bytes maxv, vir_bytes length)
|
|
|
|
{
|
|
|
|
vir_bytes v, hint = vmp->vm_region_top;
|
|
|
|
|
|
|
|
/* use the top of the last inserted region as a minv hint if
|
|
|
|
* possible. remember that a zero maxv is a special case.
|
|
|
|
*/
|
|
|
|
|
2010-10-15 11:09:29 +02:00
|
|
|
if(maxv && hint < maxv && hint >= minv) {
|
2010-10-07 12:04:05 +02:00
|
|
|
v = region_find_slot_range(vmp, hint, maxv, length);
|
|
|
|
|
|
|
|
if(v != SLOT_FAIL)
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return region_find_slot_range(vmp, minv, maxv, length);
|
|
|
|
}
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* 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;
|
|
|
|
{
|
2010-10-07 12:04:05 +02:00
|
|
|
struct vir_region *newregion;
|
2009-09-21 16:49:49 +02:00
|
|
|
vir_bytes startv;
|
|
|
|
physr_avl *phavl;
|
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!(length % VM_PAGE_SIZE));
|
2010-04-12 13:25:24 +02:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
2010-10-07 12:04:05 +02:00
|
|
|
startv = region_find_slot(vmp, minv, maxv, length);
|
|
|
|
if (startv == SLOT_FAIL)
|
2009-09-21 16:49:49 +02:00
|
|
|
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) {
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!(what % VM_PAGE_SIZE));
|
|
|
|
assert(!(startv % VM_PAGE_SIZE));
|
|
|
|
assert(!(mapflags & MF_PREALLOC));
|
2010-04-12 13:25:24 +02:00
|
|
|
if(map_new_physblock(vmp, newregion, 0, length,
|
|
|
|
what, PAF_CLEAR, 0) != OK) {
|
2008-11-19 13:26:10 +01:00
|
|
|
printf("VM: map_new_physblock failed\n");
|
2010-04-12 13:25:24 +02:00
|
|
|
USE(newregion,
|
|
|
|
SLABFREE(newregion->phys););
|
2008-11-19 13:26:10 +01:00
|
|
|
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");
|
2010-04-12 13:25:24 +02:00
|
|
|
USE(newregion,
|
|
|
|
SLABFREE(newregion->phys););
|
2008-11-19 13:26:10 +01:00
|
|
|
SLABFREE(newregion);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Link it. */
|
2010-10-04 13:41:10 +02:00
|
|
|
region_insert(&vmp->vm_regions_avl, newregion);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#if SANITYCHECKS
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(startv == newregion->vaddr);
|
2010-10-04 13:41:10 +02:00
|
|
|
{
|
|
|
|
struct vir_region *nextvr;
|
|
|
|
if((nextvr = getnextvr(newregion))) {
|
|
|
|
assert(newregion->vaddr < nextvr->vaddr);
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
return newregion;
|
|
|
|
}
|
|
|
|
|
2009-04-22 14:39:29 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* pb_unreferenced *
|
|
|
|
*===========================================================================*/
|
2010-01-14 16:24:16 +01:00
|
|
|
PUBLIC void pb_unreferenced(struct vir_region *region, struct phys_region *pr)
|
2009-04-22 14:39:29 +02:00
|
|
|
{
|
|
|
|
struct phys_block *pb;
|
|
|
|
|
|
|
|
pb = pr->ph;
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(pb->refcount > 0);
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(pb, pb->refcount--;);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(pb->refcount >= 0);
|
2009-04-22 14:39:29 +02:00
|
|
|
|
|
|
|
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) {
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(others->ph == pb);
|
2009-04-22 14:39:29 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(others); /* Otherwise, wasn't on the list. */
|
2009-04-22 14:39:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(pb->refcount == 0) {
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!pb->firstregion);
|
2009-04-22 14:39:29 +02:00
|
|
|
if(region->flags & VR_ANON) {
|
2010-04-12 13:25:24 +02:00
|
|
|
free_mem(ABS2CLICK(pb->phys),
|
2009-04-22 14:39:29 +02:00
|
|
|
ABS2CLICK(pb->length));
|
|
|
|
} else if(region->flags & VR_DIRECT) {
|
|
|
|
; /* No action required. */
|
|
|
|
} else {
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("strange phys flags");
|
2009-04-22 14:39:29 +02:00
|
|
|
}
|
|
|
|
SLABFREE(pb);
|
|
|
|
}
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
PRIVATE struct phys_region *reset_physr_iter(struct vir_region *region,
|
|
|
|
physr_iter *iter, vir_bytes offset)
|
|
|
|
{
|
|
|
|
struct phys_region *ph;
|
|
|
|
|
|
|
|
physr_start_iter(region->phys, iter, offset, AVL_EQUAL);
|
|
|
|
ph = physr_get_iter(iter);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(ph);
|
|
|
|
assert(ph->offset == offset);
|
2010-04-12 13:25:24 +02:00
|
|
|
|
|
|
|
return ph;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2011-06-01 11:30:58 +02:00
|
|
|
struct phys_region *pr;
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_iter iter;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
|
2009-04-22 14:39:29 +02:00
|
|
|
#if SANITYCHECKS
|
2009-09-21 16:49:49 +02:00
|
|
|
{
|
2010-10-04 13:41:10 +02:00
|
|
|
SLABSANE(region);
|
|
|
|
SLABSANE(region->phys);
|
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) {
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(others->ph == pb);
|
2009-04-22 14:39:29 +02:00
|
|
|
}
|
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;
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(len > pr->offset);
|
|
|
|
assert(len < pr->offset + pr->ph->length);
|
|
|
|
assert(pr->ph->refcount > 0);
|
2009-09-21 16:49:49 +02:00
|
|
|
sublen = len - pr->offset;
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!(sublen % VM_PAGE_SIZE));
|
|
|
|
assert(sublen < pr->ph->length);
|
2009-09-21 16:49:49 +02:00
|
|
|
if(pr->ph->refcount > 1) {
|
2010-04-12 13:25:24 +02:00
|
|
|
if(!(pr = map_clone_ph_block(vmp, region,
|
|
|
|
pr, &iter)))
|
|
|
|
return ENOMEM;
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(pr->ph->refcount == 1);
|
2009-09-22 13:51:17 +02:00
|
|
|
if(!(region->flags & VR_DIRECT)) {
|
2010-04-12 13:25:24 +02:00
|
|
|
free_mem(ABS2CLICK(pr->ph->phys), ABS2CLICK(sublen));
|
2009-09-22 13:51:17 +02:00
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(pr, pr->offset += sublen;);
|
|
|
|
USE(pr->ph,
|
|
|
|
pr->ph->phys += sublen;
|
|
|
|
pr->ph->length -= sublen;);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!(pr->offset % VM_PAGE_SIZE));
|
|
|
|
assert(!(pr->ph->phys % VM_PAGE_SIZE));
|
|
|
|
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;
|
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
if((r=map_subfree(vmp, region, region->length)) != OK) {
|
|
|
|
printf("%d\n", __LINE__);
|
2009-09-21 16:49:49 +02:00
|
|
|
return r;
|
2010-04-12 13:25:24 +02:00
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
USE(region,
|
|
|
|
SLABFREE(region->phys););
|
2008-11-19 13:26:10 +01:00
|
|
|
SLABFREE(region);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2010-10-15 11:10:14 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* yielded_block_cmp *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int yielded_block_cmp(struct block_id *id1, struct block_id *id2)
|
|
|
|
{
|
|
|
|
if(id1->owner < id2->owner)
|
|
|
|
return -1;
|
|
|
|
if(id1->owner > id2->owner)
|
|
|
|
return 1;
|
|
|
|
return cmp64(id1->id, id2->id);
|
|
|
|
}
|
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* free_yielded_proc *
|
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE vir_bytes free_yielded_proc(struct vmproc *vmp)
|
|
|
|
{
|
|
|
|
vir_bytes total = 0;
|
2010-10-15 11:10:14 +02:00
|
|
|
int h;
|
2010-05-05 13:35:04 +02:00
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
/* Free associated regions. */
|
2010-10-15 11:10:14 +02:00
|
|
|
for(h = 0; h < YIELD_HASHSIZE && vmp->vm_yielded > 0; h++) {
|
|
|
|
yielded_t *yb;
|
|
|
|
yielded_iter iter;
|
|
|
|
yielded_avl *avl = &vm_yielded_blocks[h];
|
|
|
|
yielded_start_iter_least(avl, &iter);
|
|
|
|
while((yb = yielded_get_iter(&iter))) {
|
|
|
|
yielded_t *next_yb;
|
|
|
|
SLABSANE(yb);
|
|
|
|
yielded_incr_iter(&iter);
|
|
|
|
if(yb->id.owner != vmp->vm_endpoint)
|
|
|
|
continue;
|
|
|
|
next_yb = yielded_get_iter(&iter);
|
|
|
|
total += freeyieldednode(yb, 1);
|
|
|
|
/* the above removal invalidated our iter; restart it
|
|
|
|
* for the node we want to start at.
|
|
|
|
*/
|
|
|
|
if(!next_yb) break;
|
|
|
|
yielded_start_iter(avl, &iter, next_yb->id, AVL_EQUAL);
|
|
|
|
assert(yielded_get_iter(&iter) == next_yb);
|
|
|
|
}
|
2010-05-05 13:35:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PRIVATE phys_bytes freeyieldednode(yielded_t *node, int freemem)
|
|
|
|
{
|
|
|
|
yielded_t *older, *younger, *removed;
|
|
|
|
vir_bytes len;
|
2010-10-15 11:10:14 +02:00
|
|
|
yielded_avl *avl;
|
|
|
|
int p;
|
2010-05-05 13:35:04 +02:00
|
|
|
|
|
|
|
SLABSANE(node);
|
|
|
|
|
|
|
|
LRUCHECK;
|
|
|
|
|
|
|
|
/* Update LRU. */
|
|
|
|
|
|
|
|
younger = node->younger;
|
|
|
|
older = node->older;
|
|
|
|
|
|
|
|
if(younger) {
|
|
|
|
SLABSANE(younger);
|
|
|
|
assert(younger->older == node);
|
|
|
|
USE(younger, younger->older = node->older;);
|
|
|
|
} else {
|
|
|
|
assert(node == lru_youngest);
|
|
|
|
lru_youngest = node->older;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(older) {
|
|
|
|
SLABSANE(older);
|
|
|
|
assert(older->younger == node);
|
|
|
|
USE(older, older->younger = node->younger;);
|
|
|
|
} else {
|
|
|
|
assert(node == lru_oldest);
|
|
|
|
lru_oldest = node->younger;
|
|
|
|
}
|
|
|
|
|
|
|
|
LRUCHECK;
|
|
|
|
|
|
|
|
/* Update AVL. */
|
|
|
|
|
2010-10-15 11:10:14 +02:00
|
|
|
if(vm_isokendpt(node->id.owner, &p) != OK)
|
|
|
|
panic("out of date owner of yielded block %d", node->id.owner);
|
|
|
|
avl = get_yielded_avl(node->id);
|
|
|
|
removed = yielded_remove(avl, node->id);
|
2010-05-05 13:35:04 +02:00
|
|
|
assert(removed == node);
|
2010-10-15 11:10:14 +02:00
|
|
|
assert(vmproc[p].vm_yielded > 0);
|
|
|
|
vmproc[p].vm_yielded--;
|
2010-05-05 13:35:04 +02:00
|
|
|
|
|
|
|
/* Free associated memory if requested. */
|
|
|
|
|
|
|
|
if(freemem) {
|
|
|
|
free_mem(ABS2CLICK(node->addr), ABS2CLICK(node->len));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free node. */
|
|
|
|
|
|
|
|
len = node->len;
|
|
|
|
SLABFREE(node);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* free_yielded *
|
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC vir_bytes free_yielded(vir_bytes max_bytes)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* PRIVATE yielded_t *lru_youngest = NULL, *lru_oldest = NULL; */
|
|
|
|
vir_bytes freed = 0;
|
|
|
|
int blocks = 0;
|
|
|
|
|
|
|
|
while(freed < max_bytes && lru_oldest) {
|
|
|
|
SLABSANE(lru_oldest);
|
|
|
|
freed += freeyieldednode(lru_oldest, 1);
|
|
|
|
blocks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return freed;
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*========================================================================*
|
|
|
|
* map_free_proc *
|
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC int map_free_proc(vmp)
|
|
|
|
struct vmproc *vmp;
|
|
|
|
{
|
2010-10-04 13:41:10 +02:00
|
|
|
struct vir_region *r;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
while((r = region_search_root(&vmp->vm_regions_avl))) {
|
2009-04-22 14:39:29 +02:00
|
|
|
SANITYCHECK(SCL_DETAIL);
|
|
|
|
#if SANITYCHECKS
|
|
|
|
nocheck++;
|
|
|
|
#endif
|
2010-10-04 13:41:10 +02:00
|
|
|
region_remove(&vmp->vm_regions_avl, r->vaddr); /* For sanity checks. */
|
2009-09-21 16:49:49 +02:00
|
|
|
map_free(vmp, r);
|
2009-04-22 14:39:29 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
nocheck--;
|
|
|
|
#endif
|
|
|
|
SANITYCHECK(SCL_DETAIL);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
2010-10-04 13:41:10 +02:00
|
|
|
|
|
|
|
region_init(&vmp->vm_regions_avl);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
/* Free associated yielded blocks. */
|
|
|
|
free_yielded_proc(vmp);
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
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);
|
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
if(!region_search_root(&vmp->vm_regions_avl))
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("process has no regions: %d", vmp->vm_endpoint);
|
2010-10-04 13:41:10 +02:00
|
|
|
#endif
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
if((r = region_search(&vmp->vm_regions_avl, offset, AVL_LESS_EQUAL))) {
|
2008-11-19 13:26:10 +01:00
|
|
|
if(offset >= r->vaddr && offset < r->vaddr + r->length)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
PRIVATE u32_t vrallocflags(u32_t flags)
|
|
|
|
{
|
|
|
|
u32_t allocflags = 0;
|
|
|
|
|
|
|
|
if(flags & VR_PHYS64K)
|
|
|
|
allocflags |= PAF_ALIGN64K;
|
|
|
|
if(flags & VR_LOWER16MB)
|
|
|
|
allocflags |= PAF_LOWER16MB;
|
|
|
|
if(flags & VR_LOWER1MB)
|
|
|
|
allocflags |= PAF_LOWER1MB;
|
|
|
|
if(flags & VR_CONTIG)
|
|
|
|
allocflags |= PAF_CONTIG;
|
|
|
|
|
|
|
|
return allocflags;
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* map_new_physblock *
|
|
|
|
*===========================================================================*/
|
2010-04-12 13:25:24 +02:00
|
|
|
PRIVATE int map_new_physblock(vmp, region, start_offset, length,
|
|
|
|
what_mem, allocflags, written)
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vmproc *vmp;
|
|
|
|
struct vir_region *region;
|
2010-04-12 13:25:24 +02:00
|
|
|
vir_bytes start_offset;
|
2008-11-19 13:26:10 +01:00
|
|
|
vir_bytes length;
|
|
|
|
phys_bytes what_mem;
|
2010-04-12 13:25:24 +02:00
|
|
|
u32_t allocflags;
|
|
|
|
int written;
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
2010-04-12 13:25:24 +02:00
|
|
|
struct memlist *memlist, given, *ml;
|
|
|
|
int used_memlist, r;
|
|
|
|
vir_bytes mapped = 0;
|
|
|
|
vir_bytes offset = start_offset;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!(length % VM_PAGE_SIZE));
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
if((region->flags & VR_CONTIG) &&
|
|
|
|
(start_offset > 0 || length < region->length)) {
|
|
|
|
printf("VM: map_new_physblock: non-full allocation requested\n");
|
|
|
|
return EFAULT;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Memory for new physical block. */
|
2008-12-08 17:43:20 +01:00
|
|
|
if(what_mem == MAP_NONE) {
|
2010-04-12 13:25:24 +02:00
|
|
|
allocflags |= vrallocflags(region->flags);
|
|
|
|
|
|
|
|
if(!(memlist = alloc_mem_in_list(length, allocflags))) {
|
2009-09-21 16:49:49 +02:00
|
|
|
printf("map_new_physblock: couldn't allocate\n");
|
2010-04-12 13:25:24 +02:00
|
|
|
return ENOMEM;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
2010-04-12 13:25:24 +02:00
|
|
|
used_memlist = 1;
|
2008-11-19 13:26:10 +01:00
|
|
|
} else {
|
2010-04-12 13:25:24 +02:00
|
|
|
given.phys = what_mem;
|
|
|
|
given.length = length;
|
|
|
|
given.next = NULL;
|
|
|
|
memlist = &given;
|
|
|
|
used_memlist = 0;
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(given.length);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
r = OK;
|
|
|
|
|
|
|
|
for(ml = memlist; ml; ml = ml->next) {
|
|
|
|
struct phys_region *newphysr = NULL;
|
|
|
|
struct phys_block *newpb = NULL;
|
|
|
|
|
|
|
|
/* Allocate things necessary for this chunk of memory. */
|
|
|
|
if(!SLABALLOC(newphysr) || !SLABALLOC(newpb)) {
|
|
|
|
printf("map_new_physblock: no memory for the ph slabs\n");
|
|
|
|
if(newphysr) SLABFREE(newphysr);
|
|
|
|
if(newpb) SLABFREE(newpb);
|
|
|
|
r = ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* New physical block. */
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!(ml->phys % VM_PAGE_SIZE));
|
2010-04-12 13:25:24 +02:00
|
|
|
|
|
|
|
USE(newpb,
|
|
|
|
newpb->phys = ml->phys;
|
|
|
|
newpb->refcount = 1;
|
|
|
|
newpb->length = ml->length;
|
|
|
|
newpb->firstregion = newphysr;);
|
|
|
|
|
|
|
|
/* New physical region. */
|
|
|
|
USE(newphysr,
|
|
|
|
newphysr->offset = offset;
|
|
|
|
newphysr->ph = newpb;
|
|
|
|
newphysr->parent = region;
|
|
|
|
/* No other references to this block. */
|
|
|
|
newphysr->next_ph_list = NULL;);
|
2010-05-05 13:35:04 +02:00
|
|
|
|
2009-09-27 14:44:36 +02:00
|
|
|
#if SANITYCHECKS
|
2010-04-12 13:25:24 +02:00
|
|
|
USE(newphysr, newphysr->written = written;);
|
2009-09-27 14:44:36 +02:00
|
|
|
#endif
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
/* Update pagetable. */
|
|
|
|
if(map_ph_writept(vmp, region, newphysr) != OK) {
|
|
|
|
printf("map_new_physblock: map_ph_writept failed\n");
|
|
|
|
r = ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
physr_insert(region->phys, newphysr);
|
|
|
|
|
|
|
|
offset += ml->length;
|
|
|
|
mapped += ml->length;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
if(used_memlist) {
|
|
|
|
if(r != OK) {
|
|
|
|
offset = start_offset;
|
|
|
|
/* Things did not go well. Undo everything. */
|
|
|
|
for(ml = memlist; ml; ml = ml->next) {
|
|
|
|
struct phys_region *physr;
|
|
|
|
if((physr = physr_search(region->phys, offset,
|
|
|
|
AVL_EQUAL))) {
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(physr->ph->refcount == 1);
|
2010-04-12 13:25:24 +02:00
|
|
|
pb_unreferenced(region, physr);
|
|
|
|
physr_remove(region->phys, physr->offset);
|
|
|
|
SLABFREE(physr);
|
|
|
|
}
|
2010-05-05 13:35:04 +02:00
|
|
|
offset += ml->length;
|
2010-04-12 13:25:24 +02:00
|
|
|
}
|
2010-04-12 14:37:28 +02:00
|
|
|
} else assert(mapped == length);
|
2010-04-12 13:25:24 +02:00
|
|
|
|
|
|
|
/* Always clean up the memlist itself, even if everything
|
|
|
|
* worked we're not using the memlist nodes any more. And
|
|
|
|
* the memory they reference is either freed above or in use.
|
|
|
|
*/
|
|
|
|
free_mem_list(memlist, 0);
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
return r;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2010-04-12 13:25:24 +02:00
|
|
|
* map_clone_ph_block *
|
2008-11-19 13:26:10 +01:00
|
|
|
*===========================================================================*/
|
2010-04-12 13:25:24 +02:00
|
|
|
PRIVATE struct phys_region *map_clone_ph_block(vmp, region, ph, iter)
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vmproc *vmp;
|
|
|
|
struct vir_region *region;
|
|
|
|
struct phys_region *ph;
|
2010-04-12 13:25:24 +02:00
|
|
|
physr_iter *iter;
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
2010-04-12 13:25:24 +02:00
|
|
|
vir_bytes offset, length;
|
|
|
|
u32_t allocflags;
|
|
|
|
phys_bytes physaddr;
|
|
|
|
struct phys_region *newpr;
|
2010-07-20 20:57:25 +02:00
|
|
|
int region_has_single_block;
|
2010-04-12 13:25:24 +02:00
|
|
|
int written = 0;
|
|
|
|
#if SANITYCHECKS
|
|
|
|
written = ph->written;
|
|
|
|
#endif
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
/* Warning: this function will free the passed
|
|
|
|
* phys_region *ph and replace it (in the same offset)
|
|
|
|
* with one or more others! So both the pointer to it
|
|
|
|
* and any iterators over the phys_regions in the vir_region
|
|
|
|
* will be invalid on successful return. (Iterators over
|
|
|
|
* the vir_region could be invalid on unsuccessful return too.)
|
|
|
|
*/
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
/* This is only to be done if there is more than one copy. */
|
|
|
|
assert(ph->ph->refcount > 1);
|
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
/* This function takes a physical block, copies its contents
|
|
|
|
* into newly allocated memory, and replaces the single physical
|
|
|
|
* block by one or more physical blocks with refcount 1 with the
|
|
|
|
* same contents as the original. In other words, a fragmentable
|
|
|
|
* version of map_copy_ph_block().
|
|
|
|
*/
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
/* Remember where and how much. */
|
|
|
|
offset = ph->offset;
|
|
|
|
length = ph->ph->length;
|
|
|
|
physaddr = ph->ph->phys;
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
/* Now unlink the original physical block so we can replace
|
|
|
|
* it with new ones.
|
|
|
|
*/
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
SLABSANE(ph);
|
|
|
|
SLABSANE(ph->ph);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(ph->ph->refcount > 1);
|
2009-04-22 14:39:29 +02:00
|
|
|
pb_unreferenced(region, ph);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(ph->ph->refcount >= 1);
|
2010-04-12 13:25:24 +02:00
|
|
|
physr_remove(region->phys, offset);
|
|
|
|
SLABFREE(ph);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
SANITYCHECK(SCL_DETAIL);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
/* Put new free memory in. */
|
|
|
|
allocflags = vrallocflags(region->flags);
|
2010-07-20 20:57:25 +02:00
|
|
|
region_has_single_block = (offset == 0 && length == region->length);
|
|
|
|
assert(region_has_single_block || !(allocflags & PAF_CONTIG));
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!(allocflags & PAF_CLEAR));
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
if(map_new_physblock(vmp, region, offset, length,
|
|
|
|
MAP_NONE, allocflags, written) != OK) {
|
|
|
|
/* XXX original range now gone. */
|
|
|
|
printf("VM: map_clone_ph_block: map_new_physblock failed.\n");
|
|
|
|
return NULL;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
/* Copy the block to the new memory.
|
|
|
|
* Can only fail if map_new_physblock didn't do what we asked.
|
|
|
|
*/
|
|
|
|
if(copy_abs2region(physaddr, region, offset, length) != OK)
|
|
|
|
panic("copy_abs2region failed, no good reason for that");
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
newpr = physr_search(region->phys, offset, AVL_EQUAL);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(newpr);
|
|
|
|
assert(newpr->offset == offset);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
if(iter) {
|
|
|
|
physr_start_iter(region->phys, iter, offset, AVL_EQUAL);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(physr_get_iter(iter) == newpr);
|
2010-04-12 13:25:24 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
return newpr;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*===========================================================================*
|
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
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(offset >= 0);
|
|
|
|
assert(offset < region->length);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(region->flags & VR_ANON);
|
|
|
|
assert(!(region->vaddr % VM_PAGE_SIZE));
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
virpage = offset - offset % VM_PAGE_SIZE;
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
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. */
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(write);
|
|
|
|
assert(region->flags & VR_WRITABLE);
|
|
|
|
assert(ph->ph->refcount > 0);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
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 {
|
2010-01-14 16:24:16 +01:00
|
|
|
if(ph->ph->refcount > 0
|
|
|
|
&& ph->ph->share_flag != PBSH_COW) {
|
|
|
|
printf("VM: write RO mapped pages.\n");
|
|
|
|
return EFAULT;
|
|
|
|
} else {
|
2010-04-12 13:25:24 +02:00
|
|
|
if(!map_clone_ph_block(vmp, region, ph, NULL))
|
|
|
|
r = ENOMEM;
|
2010-01-14 16:24:16 +01:00
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
} else {
|
|
|
|
/* Pagefault in non-existing block. Map in new block. */
|
2010-04-12 13:25:24 +02:00
|
|
|
if(map_new_physblock(vmp, region, virpage,
|
|
|
|
VM_PAGE_SIZE, MAP_NONE, PAF_CLEAR, 0) != OK) {
|
2009-09-21 16:49:49 +02:00
|
|
|
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
|
2010-04-12 13:25:24 +02:00
|
|
|
if(OK != pt_checkrange(&vmp->vm_pt, region->vaddr+virpage,
|
|
|
|
VM_PAGE_SIZE, write)) {
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("map_pf: pt_checkrange failed: %d", r);
|
2009-09-27 14:44:36 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2010-06-28 23:53:37 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* map_pin_memory *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int map_pin_memory(struct vmproc *vmp)
|
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
2010-07-01 10:54:25 +02:00
|
|
|
int r;
|
2010-10-04 13:41:10 +02:00
|
|
|
region_iter iter;
|
|
|
|
region_start_iter_least(&vmp->vm_regions_avl, &iter);
|
2010-06-28 23:53:37 +02:00
|
|
|
/* Scan all memory regions. */
|
2010-10-04 13:41:10 +02:00
|
|
|
while((vr = region_get_iter(&iter))) {
|
2010-07-01 10:54:25 +02:00
|
|
|
/* Make sure region is mapped to physical memory and writable.*/
|
|
|
|
r = map_handle_memory(vmp, vr, 0, vr->length, 1);
|
|
|
|
if(r != OK) {
|
|
|
|
panic("map_pin_memory: map_handle_memory failed: %d", r);
|
2010-06-28 23:53:37 +02:00
|
|
|
}
|
2010-10-04 13:41:10 +02:00
|
|
|
region_incr_iter(&iter);
|
2010-06-28 23:53:37 +02:00
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*===========================================================================*
|
|
|
|
* 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;
|
|
|
|
|
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) { \
|
2010-05-05 13:35:04 +02:00
|
|
|
start = MAX(start, r1->offset + r1->ph->length); } \
|
2009-09-21 16:49:49 +02:00
|
|
|
if(r2) { \
|
2010-05-05 13:35:04 +02:00
|
|
|
end = MIN(end, r2->offset); } \
|
2008-11-19 13:26:10 +01:00
|
|
|
if(start < end) { \
|
|
|
|
SANITYCHECK(SCL_DETAIL); \
|
2010-04-12 13:25:24 +02:00
|
|
|
if(map_new_physblock(vmp, region, start, \
|
|
|
|
end-start, MAP_NONE, PAF_CLEAR, 0) != 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);
|
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(region->flags & VR_ANON);
|
|
|
|
assert(!(region->vaddr % VM_PAGE_SIZE));
|
|
|
|
assert(!(offset % VM_PAGE_SIZE));
|
|
|
|
assert(!(length % VM_PAGE_SIZE));
|
|
|
|
assert(!write || (region->flags & VR_WRITABLE));
|
2008-11-19 13:26:10 +01:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
FREE_RANGE_HERE(NULL, physr);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2009-09-27 14:44:36 +02:00
|
|
|
if(physr) {
|
2010-04-12 13:25:24 +02:00
|
|
|
physr = reset_physr_iter(region, &iter, physr->offset);
|
2009-09-27 14:44:36 +02:00
|
|
|
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-12-08 14:35:52 +01:00
|
|
|
FREE_RANGE_HERE(NULL, physr);
|
2009-09-27 14:44:36 +02:00
|
|
|
if(physr) {
|
2010-04-12 13:25:24 +02:00
|
|
|
physr = reset_physr_iter(region, &iter,
|
|
|
|
physr->offset);
|
2009-09-27 14:44:36 +02:00
|
|
|
}
|
|
|
|
}
|
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) {
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(physr->ph->refcount > 0);
|
2009-09-21 16:49:49 +02:00
|
|
|
if(!WRITABLE(region, physr->ph)) {
|
2010-04-12 13:25:24 +02:00
|
|
|
if(!(physr = map_clone_ph_block(vmp, region,
|
|
|
|
physr, &iter))) {
|
2008-11-19 13:26:10 +01:00
|
|
|
printf("VM: map_handle_memory: no copy\n");
|
2010-04-12 13:25:24 +02:00
|
|
|
return ENOMEM;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
changes++;
|
|
|
|
} 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;
|
2010-04-12 13:25:24 +02:00
|
|
|
nextphysr = reset_physr_iter(region, &iter,
|
|
|
|
nextphysr->offset);
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
|
|
|
physr = nextphysr;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
2009-12-08 14:35:52 +01:00
|
|
|
if(changes < 1) {
|
2010-01-14 16:24:16 +01:00
|
|
|
#if VERBOSE
|
2009-12-08 14:35:52 +01:00
|
|
|
printf("region start at 0x%lx offset 0x%lx len 0x%lx write %d\n",
|
|
|
|
region->vaddr, offset, length, write);
|
2010-01-14 16:24:16 +01:00
|
|
|
printf("no changes in map_handle_memory\n");
|
|
|
|
#endif
|
|
|
|
return EFAULT;
|
2009-12-08 14:35:52 +01:00
|
|
|
}
|
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);
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("checkrange failed");
|
2009-09-27 14:44:36 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if SANITYCHECKS
|
2010-10-07 12:04:05 +02:00
|
|
|
static int count_phys_regions(struct vir_region *vr)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
|
|
|
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;
|
2010-10-07 12:04:05 +02:00
|
|
|
cr = count_phys_regions(vr);
|
2008-11-19 13:26:10 +01:00
|
|
|
#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;
|
2010-10-04 13:41:10 +02:00
|
|
|
newvr->lower = newvr->higher = NULL;
|
2009-09-21 16:49:49 +02:00
|
|
|
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);
|
2010-04-12 13:25:24 +02:00
|
|
|
#if SANITYCHECKS
|
2010-10-07 12:04:05 +02:00
|
|
|
assert(count_phys_regions(vr) == cr);
|
2010-04-12 13:25:24 +02:00
|
|
|
#endif
|
2009-09-21 16:49:49 +02:00
|
|
|
physr_incr_iter(&iter);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
#if SANITYCHECKS
|
2010-10-07 12:04:05 +02:00
|
|
|
assert(count_phys_regions(vr) == count_phys_regions(newvr));
|
2010-04-12 13:25:24 +02:00
|
|
|
#endif
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
return newvr;
|
|
|
|
}
|
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* copy_abs2region *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int copy_abs2region(phys_bytes abs, struct vir_region *destregion,
|
|
|
|
phys_bytes offset, phys_bytes len)
|
|
|
|
|
|
|
|
{
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(destregion);
|
|
|
|
assert(destregion->phys);
|
2010-04-12 13:25:24 +02:00
|
|
|
while(len > 0) {
|
|
|
|
phys_bytes sublen, suboffset;
|
|
|
|
struct phys_region *ph;
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(destregion);
|
|
|
|
assert(destregion->phys);
|
2010-04-12 13:25:24 +02:00
|
|
|
if(!(ph = physr_search(destregion->phys, offset, AVL_LESS_EQUAL))) {
|
|
|
|
printf("VM: copy_abs2region: no phys region found (1).\n");
|
|
|
|
return EFAULT;
|
|
|
|
}
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(ph->offset <= offset);
|
2010-04-12 13:25:24 +02:00
|
|
|
if(ph->offset+ph->ph->length <= offset) {
|
|
|
|
printf("VM: copy_abs2region: no phys region found (2).\n");
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
suboffset = offset - ph->offset;
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(suboffset < ph->ph->length);
|
2010-04-12 13:25:24 +02:00
|
|
|
sublen = len;
|
|
|
|
if(sublen > ph->ph->length - suboffset)
|
|
|
|
sublen = ph->ph->length - suboffset;
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(suboffset + sublen <= ph->ph->length);
|
2010-04-12 13:25:24 +02:00
|
|
|
if(ph->ph->refcount != 1) {
|
2010-05-05 13:35:04 +02:00
|
|
|
printf("VM: copy_abs2region: refcount not 1.\n");
|
2010-04-12 13:25:24 +02:00
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sys_abscopy(abs, ph->ph->phys + suboffset, sublen) != OK) {
|
|
|
|
printf("VM: copy_abs2region: abscopy failed.\n");
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
abs += sublen;
|
|
|
|
offset += sublen;
|
|
|
|
len -= sublen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*=========================================================================*
|
|
|
|
* 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;
|
2010-10-04 13:41:10 +02:00
|
|
|
region_iter v_iter;
|
|
|
|
region_start_iter_least(&vmp->vm_regions_avl, &v_iter);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
while((vr = region_get_iter(&v_iter))) {
|
|
|
|
physr_iter ph_iter;
|
|
|
|
physr_start_iter_least(vr->phys, &ph_iter);
|
|
|
|
|
|
|
|
while((ph = physr_get_iter(&ph_iter))) {
|
|
|
|
physr_incr_iter(&ph_iter);
|
2010-01-14 16:24:16 +01:00
|
|
|
|
|
|
|
/* If this phys block is shared as SMAP, then do
|
|
|
|
* not update the page table. */
|
|
|
|
if(ph->ph->refcount > 1
|
|
|
|
&& ph->ph->share_flag == PBSH_SMAP) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
if((r=map_ph_writept(vmp, vr, ph)) != OK) {
|
|
|
|
printf("VM: map_writept: failed\n");
|
|
|
|
return r;
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
2010-10-04 13:41:10 +02:00
|
|
|
region_incr_iter(&v_iter);
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*========================================================================*
|
2010-07-20 04:08:28 +02:00
|
|
|
* map_proc_copy *
|
2008-11-19 13:26:10 +01:00
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC int map_proc_copy(dst, src)
|
|
|
|
struct vmproc *dst;
|
|
|
|
struct vmproc *src;
|
|
|
|
{
|
2010-07-20 04:08:28 +02:00
|
|
|
/* Copy all the memory regions from the src process to the dst process. */
|
2010-10-04 13:41:10 +02:00
|
|
|
region_init(&dst->vm_regions_avl);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
return map_proc_copy_from(dst, src, NULL);
|
2010-07-20 04:08:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* map_proc_copy_from *
|
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC int map_proc_copy_from(dst, src, start_src_vr)
|
|
|
|
struct vmproc *dst;
|
|
|
|
struct vmproc *src;
|
|
|
|
struct vir_region *start_src_vr;
|
|
|
|
{
|
2010-10-04 13:41:10 +02:00
|
|
|
struct vir_region *vr;
|
|
|
|
region_iter v_iter;
|
|
|
|
|
|
|
|
if(!start_src_vr)
|
|
|
|
start_src_vr = region_search_least(&src->vm_regions_avl);
|
2010-07-20 04:08:28 +02:00
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
assert(start_src_vr);
|
2010-07-20 04:08:28 +02:00
|
|
|
assert(start_src_vr->parent == src);
|
2010-10-04 13:41:10 +02:00
|
|
|
region_start_iter(&src->vm_regions_avl, &v_iter,
|
|
|
|
start_src_vr->vaddr, AVL_EQUAL);
|
|
|
|
assert(region_get_iter(&v_iter) == start_src_vr);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2010-07-20 04:08:28 +02:00
|
|
|
/* Copy source regions after the destination's last region (if any). */
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
while((vr = region_get_iter(&v_iter))) {
|
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;);
|
2010-10-04 13:41:10 +02:00
|
|
|
region_insert(&dst->vm_regions_avl, 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.
|
|
|
|
*/
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(new_ph);
|
|
|
|
assert(orig_ph);
|
|
|
|
assert(orig_ph != new_ph);
|
2009-04-22 14:39:29 +02:00
|
|
|
pb = orig_ph->ph;
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(pb == new_ph->ph);
|
2009-04-22 14:39:29 +02:00
|
|
|
|
|
|
|
/* Link in new physregion. */
|
2010-04-12 14:37:28 +02:00
|
|
|
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 */
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(pb->refcount > 0);
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(pb, pb->refcount++;);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(pb->refcount > 1);
|
2009-04-22 14:39:29 +02:00
|
|
|
|
2010-01-14 16:24:16 +01:00
|
|
|
/* If the phys block has been shared as SMAP,
|
|
|
|
* do the regular copy. */
|
|
|
|
if(pb->refcount > 2 && pb->share_flag == PBSH_SMAP) {
|
2010-04-12 13:25:24 +02:00
|
|
|
map_clone_ph_block(dst, newvr,new_ph,
|
|
|
|
&iter_new);
|
2010-01-14 16:24:16 +01:00
|
|
|
} else {
|
2010-04-12 13:25:24 +02:00
|
|
|
USE(pb, pb->share_flag = PBSH_COW;);
|
2010-01-14 16:24:16 +01:00
|
|
|
}
|
|
|
|
|
2009-04-22 14:39:29 +02:00
|
|
|
/* 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
|
|
|
}
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!physr_get_iter(&iter_new));
|
2010-10-04 13:41:10 +02:00
|
|
|
region_incr_iter(&v_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
|
|
|
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).
|
|
|
|
*/
|
2010-10-04 13:41:10 +02:00
|
|
|
assert(!region_search_root(&vmp->vm_regions_avl));
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(vmproc[VMP_SYSTEM].vm_flags & VMF_INUSE);
|
|
|
|
assert(!(KERNEL_TEXT % VM_PAGE_SIZE));
|
|
|
|
assert(!(KERNEL_TEXT_LEN % VM_PAGE_SIZE));
|
|
|
|
assert(!(KERNEL_DATA % VM_PAGE_SIZE));
|
|
|
|
assert(!(KERNEL_DATA_LEN % VM_PAGE_SIZE));
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
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;
|
2010-10-04 13:41:10 +02:00
|
|
|
struct vir_region *nextvr;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(vr);
|
|
|
|
assert(vr->flags & VR_ANON);
|
|
|
|
assert(!(delta % VM_PAGE_SIZE));
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
if(!delta) return OK;
|
|
|
|
end = vr->vaddr + vr->length;
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(end >= vr->vaddr);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
if(end + delta <= end) {
|
|
|
|
printf("VM: strange delta 0x%lx\n", delta);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
nextvr = getnextvr(vr);
|
|
|
|
|
|
|
|
if(!nextvr || end + delta <= nextvr->vaddr) {
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(vr, vr->length += delta;);
|
2008-11-19 13:26:10 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* map_region_shrink *
|
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC int map_region_shrink(struct vir_region *vr, vir_bytes delta)
|
|
|
|
{
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(vr);
|
|
|
|
assert(vr->flags & VR_ANON);
|
|
|
|
assert(!(delta % VM_PAGE_SIZE));
|
2008-11-19 13:26:10 +01:00
|
|
|
|
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;
|
2010-10-04 13:41:10 +02:00
|
|
|
region_iter v_iter;
|
|
|
|
region_start_iter_least(&vmp->vm_regions_avl, &v_iter);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
while((vr = region_get_iter(&v_iter))) {
|
2008-11-19 13:26:10 +01:00
|
|
|
if(vr->tag == tag)
|
|
|
|
return vr;
|
2010-10-04 13:41:10 +02:00
|
|
|
region_incr_iter(&v_iter);
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
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 *
|
|
|
|
*========================================================================*/
|
2010-10-04 13:41:10 +02:00
|
|
|
PUBLIC int map_unmap_region(struct vmproc *vmp, struct vir_region *r,
|
2009-09-21 16:49:49 +02:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
vir_bytes regionstart;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
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. */
|
2010-10-04 13:41:10 +02:00
|
|
|
region_remove(&vmp->vm_regions_avl, r->vaddr);
|
2009-09-21 16:49:49 +02:00
|
|
|
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))) {
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(pr->offset >= len);
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(pr, pr->offset -= len;);
|
|
|
|
physr_incr_iter(&iter);
|
|
|
|
}
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
SANITYCHECK(SCL_DETAIL);
|
|
|
|
|
2010-09-15 16:11:12 +02:00
|
|
|
if(pt_writemap(vmp, &vmp->vm_pt, regionstart,
|
2009-09-21 16:49:49 +02:00
|
|
|
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)
|
|
|
|
{
|
2010-10-07 12:04:05 +02:00
|
|
|
struct vir_region *vr;
|
2009-09-21 16:49:49 +02:00
|
|
|
struct phys_region *ph;
|
|
|
|
vir_bytes startv, dst_addr;
|
|
|
|
physr_iter iter;
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(region->flags & VR_SHARED);
|
2010-04-12 13:25:24 +02:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
/* da is handled differently */
|
|
|
|
if (!da)
|
|
|
|
dst_addr = dvmp->vm_stacktop;
|
|
|
|
else
|
|
|
|
dst_addr = da;
|
|
|
|
dst_addr = arch_vir2map(dvmp, dst_addr);
|
|
|
|
|
|
|
|
/* round up to page size */
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!(size % VM_PAGE_SIZE));
|
2010-10-07 12:04:05 +02:00
|
|
|
startv = region_find_slot(dvmp, dst_addr, VM_DATATOP, size);
|
|
|
|
if (startv == SLOT_FAIL) {
|
2009-09-21 16:49:49 +02:00
|
|
|
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;);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(vr->flags & VR_SHARED);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
region_insert(&dvmp->vm_regions_avl, vr);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
physr_start_iter_least(vr->phys, &iter);
|
|
|
|
while((ph = physr_get_iter(&iter))) {
|
|
|
|
struct phys_block *pb = ph->ph;
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!ph->next_ph_list);
|
2010-04-12 13:25:24 +02:00
|
|
|
USE(ph, ph->next_ph_list = pb->firstregion;);
|
|
|
|
USE(pb, pb->firstregion = ph;);
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(pb, pb->refcount++;);
|
|
|
|
if(map_ph_writept(dvmp, vr, ph) != OK) {
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("map_remap: map_ph_writept failed");
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(ph);
|
|
|
|
assert(ph->ph);
|
2009-09-21 16:49:49 +02:00
|
|
|
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);
|
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(ph);
|
|
|
|
assert(ph->ph);
|
2009-09-21 16:49:49 +02:00
|
|
|
if (cnt)
|
|
|
|
*cnt = ph->ph->refcount;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2010-09-14 23:22:56 +02:00
|
|
|
/*========================================================================*
|
|
|
|
* get_stats_info *
|
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC void get_stats_info(struct vm_stats_info *vsi)
|
|
|
|
{
|
|
|
|
yielded_t *yb;
|
|
|
|
|
|
|
|
vsi->vsi_cached = 0L;
|
|
|
|
|
|
|
|
for(yb = lru_youngest; yb; yb = yb->older)
|
|
|
|
vsi->vsi_cached += yb->len / VM_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
2010-01-19 22:00:20 +01:00
|
|
|
/*========================================================================*
|
|
|
|
* get_usage_info *
|
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC void get_usage_info(struct vmproc *vmp, struct vm_usage_info *vui)
|
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
|
|
|
physr_iter iter;
|
|
|
|
struct phys_region *ph;
|
|
|
|
vir_bytes len;
|
2010-10-04 13:41:10 +02:00
|
|
|
region_iter v_iter;
|
|
|
|
region_start_iter_least(&vmp->vm_regions_avl, &v_iter);
|
2010-01-19 22:00:20 +01:00
|
|
|
|
|
|
|
memset(vui, 0, sizeof(*vui));
|
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
while((vr = region_get_iter(&v_iter))) {
|
2010-01-19 22:00:20 +01:00
|
|
|
physr_start_iter_least(vr->phys, &iter);
|
|
|
|
while((ph = physr_get_iter(&iter))) {
|
|
|
|
len = ph->ph->length;
|
|
|
|
|
|
|
|
/* All present pages are counted towards the total. */
|
|
|
|
vui->vui_total += len;
|
|
|
|
|
|
|
|
if (ph->ph->refcount > 1) {
|
|
|
|
/* Any page with a refcount > 1 is common. */
|
|
|
|
vui->vui_common += len;
|
|
|
|
|
|
|
|
/* Any common, non-COW page is shared. */
|
|
|
|
if (vr->flags & VR_SHARED ||
|
|
|
|
ph->ph->share_flag == PBSH_SMAP)
|
|
|
|
vui->vui_shared += len;
|
|
|
|
}
|
|
|
|
physr_incr_iter(&iter);
|
|
|
|
}
|
2010-10-04 13:41:10 +02:00
|
|
|
region_incr_iter(&v_iter);
|
2010-01-19 22:00:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* get_region_info *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int get_region_info(struct vmproc *vmp, struct vm_region_info *vri,
|
|
|
|
int max, vir_bytes *nextp)
|
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
|
|
|
vir_bytes next;
|
|
|
|
int count;
|
2010-10-04 13:41:10 +02:00
|
|
|
region_iter v_iter;
|
2010-01-19 22:00:20 +01:00
|
|
|
|
|
|
|
next = *nextp;
|
|
|
|
|
|
|
|
if (!max) return 0;
|
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
region_start_iter(&vmp->vm_regions_avl, &v_iter, next, AVL_GREATER_EQUAL);
|
|
|
|
if(!(vr = region_get_iter(&v_iter))) return 0;
|
2010-01-19 22:00:20 +01:00
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
for(count = 0; (vr = region_get_iter(&v_iter)) && count < max; count++, vri++) {
|
2010-01-19 22:00:20 +01:00
|
|
|
vri->vri_addr = arch_map2info(vmp, vr->vaddr, &vri->vri_seg,
|
|
|
|
&vri->vri_prot);
|
|
|
|
vri->vri_length = vr->length;
|
|
|
|
|
|
|
|
/* "AND" the provided protection with per-page protection. */
|
|
|
|
if (!(vr->flags & VR_WRITABLE))
|
|
|
|
vri->vri_prot &= ~PROT_WRITE;
|
|
|
|
|
2011-07-15 18:10:50 +02:00
|
|
|
vri->vri_flags = (vr->flags & VR_SHARED) ? MAP_IPC_SHARED : 0;
|
2010-01-19 22:00:20 +01:00
|
|
|
|
|
|
|
next = vr->vaddr + vr->length;
|
2010-10-04 13:41:10 +02:00
|
|
|
region_incr_iter(&v_iter);
|
2010-01-19 22:00:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*nextp = next;
|
|
|
|
return count;
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* regionprintstats *
|
|
|
|
*========================================================================*/
|
|
|
|
PUBLIC void printregionstats(struct vmproc *vmp)
|
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
|
|
|
struct phys_region *pr;
|
|
|
|
physr_iter iter;
|
|
|
|
vir_bytes used = 0, weighted = 0;
|
2010-10-04 13:41:10 +02:00
|
|
|
region_iter v_iter;
|
|
|
|
region_start_iter_least(&vmp->vm_regions_avl, &v_iter);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
while((vr = region_get_iter(&v_iter))) {
|
|
|
|
region_incr_iter(&v_iter);
|
2009-09-21 16:49:49 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 15:58:57 +02:00
|
|
|
printf("%6lukB %6lukB\n", used/1024, weighted/1024);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
/*===========================================================================*
|
2010-05-05 13:35:04 +02:00
|
|
|
* do_map_memory *
|
2010-04-12 13:25:24 +02:00
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE int do_map_memory(struct vmproc *vms, struct vmproc *vmd,
|
2010-05-05 13:35:04 +02:00
|
|
|
struct vir_region *vrs, struct vir_region *vrd,
|
|
|
|
vir_bytes offset_s, vir_bytes offset_d,
|
|
|
|
vir_bytes length, int flag)
|
2010-04-12 13:25:24 +02:00
|
|
|
{
|
2010-05-05 13:35:04 +02:00
|
|
|
struct phys_region *prs;
|
|
|
|
struct phys_region *newphysr;
|
|
|
|
struct phys_block *pb;
|
|
|
|
physr_iter iter;
|
|
|
|
u32_t pt_flag = PTF_PRESENT | PTF_USER;
|
|
|
|
vir_bytes end;
|
|
|
|
|
|
|
|
/* Search for the first phys region in the source process. */
|
|
|
|
physr_start_iter(vrs->phys, &iter, offset_s, AVL_EQUAL);
|
|
|
|
prs = physr_get_iter(&iter);
|
|
|
|
if(!prs)
|
|
|
|
panic("do_map_memory: no aligned phys region: %d", 0);
|
|
|
|
|
|
|
|
/* flag: 0 -> read-only
|
|
|
|
* 1 -> writable
|
|
|
|
* -1 -> share as COW, so read-only
|
|
|
|
*/
|
|
|
|
if(flag > 0)
|
|
|
|
pt_flag |= PTF_WRITE;
|
|
|
|
|
|
|
|
/* Map phys blocks in the source process to the destination process. */
|
|
|
|
end = offset_d + length;
|
|
|
|
while((prs = physr_get_iter(&iter)) && offset_d < end) {
|
|
|
|
/* If a SMAP share was requested but the phys block has already
|
|
|
|
* been shared as COW, copy the block for the source phys region
|
|
|
|
* first.
|
|
|
|
*/
|
|
|
|
pb = prs->ph;
|
|
|
|
if(flag >= 0 && pb->refcount > 1
|
|
|
|
&& pb->share_flag == PBSH_COW) {
|
2010-04-12 13:25:24 +02:00
|
|
|
if(!(prs = map_clone_ph_block(vms, vrs, prs, &iter)))
|
|
|
|
return ENOMEM;
|
2010-05-05 13:35:04 +02:00
|
|
|
pb = prs->ph;
|
|
|
|
}
|
2010-04-12 13:25:24 +02:00
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
/* Allocate a new phys region. */
|
|
|
|
if(!SLABALLOC(newphysr))
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
/* Set and link the new phys region to the block. */
|
|
|
|
newphysr->ph = pb;
|
|
|
|
newphysr->offset = offset_d;
|
|
|
|
newphysr->parent = vrd;
|
|
|
|
newphysr->next_ph_list = pb->firstregion;
|
|
|
|
pb->firstregion = newphysr;
|
|
|
|
physr_insert(newphysr->parent->phys, newphysr);
|
|
|
|
pb->refcount++;
|
|
|
|
|
|
|
|
/* If a COW share was requested but the phys block has already
|
|
|
|
* been shared as SMAP, give up on COW and copy the block for
|
|
|
|
* the destination phys region now.
|
|
|
|
*/
|
|
|
|
if(flag < 0 && pb->refcount > 1
|
|
|
|
&& pb->share_flag == PBSH_SMAP) {
|
2010-04-12 13:25:24 +02:00
|
|
|
if(!(newphysr = map_clone_ph_block(vmd, vrd,
|
|
|
|
newphysr, NULL))) {
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
2010-05-05 13:35:04 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* See if this is a COW share or SMAP share. */
|
|
|
|
if(flag < 0) { /* COW share */
|
|
|
|
pb->share_flag = PBSH_COW;
|
|
|
|
/* Update the page table for the src process. */
|
2010-09-15 16:11:12 +02:00
|
|
|
pt_writemap(vms, &vms->vm_pt, offset_s + vrs->vaddr,
|
2010-05-05 13:35:04 +02:00
|
|
|
pb->phys, pb->length,
|
|
|
|
pt_flag, WMF_OVERWRITE);
|
|
|
|
}
|
|
|
|
else { /* SMAP share */
|
|
|
|
pb->share_flag = PBSH_SMAP;
|
|
|
|
}
|
|
|
|
/* Update the page table for the destination process. */
|
2010-09-15 16:11:12 +02:00
|
|
|
pt_writemap(vmd, &vmd->vm_pt, offset_d + vrd->vaddr,
|
2010-05-05 13:35:04 +02:00
|
|
|
pb->phys, pb->length, pt_flag, WMF_OVERWRITE);
|
|
|
|
}
|
2010-04-12 13:25:24 +02:00
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
physr_incr_iter(&iter);
|
|
|
|
offset_d += pb->length;
|
|
|
|
offset_s += pb->length;
|
|
|
|
}
|
|
|
|
return OK;
|
2010-04-12 13:25:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* unmap_memory *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int unmap_memory(endpoint_t sour, endpoint_t dest,
|
|
|
|
vir_bytes virt_s, vir_bytes virt_d, vir_bytes length, int flag)
|
|
|
|
{
|
|
|
|
struct vmproc *vmd;
|
|
|
|
struct vir_region *vrd;
|
|
|
|
struct phys_region *pr;
|
|
|
|
struct phys_block *pb;
|
|
|
|
physr_iter iter;
|
|
|
|
vir_bytes off, end;
|
|
|
|
int p;
|
|
|
|
|
|
|
|
/* Use information on the destination process to unmap. */
|
|
|
|
if(vm_isokendpt(dest, &p) != OK)
|
|
|
|
panic("unmap_memory: bad endpoint: %d", dest);
|
|
|
|
vmd = &vmproc[p];
|
|
|
|
|
|
|
|
vrd = map_lookup(vmd, virt_d);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(vrd);
|
2010-04-12 13:25:24 +02:00
|
|
|
|
|
|
|
/* Search for the first phys region in the destination process. */
|
|
|
|
off = virt_d - vrd->vaddr;
|
|
|
|
physr_start_iter(vrd->phys, &iter, off, AVL_EQUAL);
|
|
|
|
pr = physr_get_iter(&iter);
|
|
|
|
if(!pr)
|
|
|
|
panic("unmap_memory: no aligned phys region: %d", 0);
|
|
|
|
|
|
|
|
/* Copy the phys block now rather than doing COW. */
|
|
|
|
end = off + length;
|
|
|
|
while((pr = physr_get_iter(&iter)) && off < end) {
|
|
|
|
pb = pr->ph;
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(pb->refcount > 1);
|
|
|
|
assert(pb->share_flag == PBSH_SMAP);
|
2010-04-12 13:25:24 +02:00
|
|
|
|
|
|
|
if(!(pr = map_clone_ph_block(vmd, vrd, pr, &iter)))
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
physr_incr_iter(&iter);
|
|
|
|
off += pb->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
/*===========================================================================*
|
2010-05-05 13:35:04 +02:00
|
|
|
* split_phys *
|
2010-04-12 13:25:24 +02:00
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE int split_phys(struct phys_region *pr, vir_bytes point)
|
|
|
|
{
|
2011-06-01 11:30:58 +02:00
|
|
|
struct phys_region *newpr, *q, *prev = NULL;
|
2010-05-05 13:35:04 +02:00
|
|
|
struct phys_block *newpb;
|
|
|
|
struct phys_block *pb = pr->ph;
|
2010-04-12 13:25:24 +02:00
|
|
|
/* Split the phys region into 2 parts by @point. */
|
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
if(pr->offset >= point || pr->offset + pb->length <= point)
|
|
|
|
return OK;
|
|
|
|
if(!SLABALLOC(newpb))
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
/* Split phys block. */
|
|
|
|
*newpb = *pb;
|
|
|
|
pb->length = point - pr->offset;
|
|
|
|
newpb->length -= pb->length;
|
|
|
|
newpb->phys += pb->length;
|
|
|
|
|
|
|
|
/* Split phys regions in a list. */
|
|
|
|
for(q = pb->firstregion; q; q = q->next_ph_list) {
|
|
|
|
if(!SLABALLOC(newpr))
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
*newpr = *q;
|
|
|
|
newpr->ph = newpb;
|
|
|
|
newpr->offset += pb->length;
|
|
|
|
|
|
|
|
/* Link to the vir region's phys region list. */
|
|
|
|
physr_insert(newpr->parent->phys, newpr);
|
|
|
|
|
|
|
|
/* Link to the next_ph_list. */
|
|
|
|
if(q == pb->firstregion) {
|
|
|
|
newpb->firstregion = newpr;
|
|
|
|
prev = newpr;
|
|
|
|
} else {
|
|
|
|
prev->next_ph_list = newpr;
|
|
|
|
prev = newpr;
|
|
|
|
}
|
|
|
|
}
|
2011-06-01 11:30:58 +02:00
|
|
|
assert(prev);
|
2010-05-05 13:35:04 +02:00
|
|
|
prev->next_ph_list = NULL;
|
2010-04-12 13:25:24 +02:00
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
return OK;
|
2010-04-12 13:25:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2010-05-05 13:35:04 +02:00
|
|
|
* clean_phys_regions *
|
2010-04-12 13:25:24 +02:00
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE void clean_phys_regions(struct vir_region *region,
|
2010-05-05 13:35:04 +02:00
|
|
|
vir_bytes offset, vir_bytes length)
|
2010-04-12 13:25:24 +02:00
|
|
|
{
|
|
|
|
/* Consider @offset as the start address and @offset+length as the end address.
|
|
|
|
* If there are phys regions crossing the start address or the end address,
|
|
|
|
* split them into 2 parts.
|
|
|
|
*
|
|
|
|
* We assume that the phys regions are listed in order and don't overlap.
|
|
|
|
*/
|
2010-05-05 13:35:04 +02:00
|
|
|
struct phys_region *pr;
|
|
|
|
physr_iter iter;
|
|
|
|
|
|
|
|
physr_start_iter_least(region->phys, &iter);
|
|
|
|
while((pr = physr_get_iter(&iter))) {
|
|
|
|
/* If this phys region crosses the start address, split it. */
|
|
|
|
if(pr->offset < offset
|
|
|
|
&& pr->offset + pr->ph->length > offset) {
|
|
|
|
split_phys(pr, offset);
|
|
|
|
physr_start_iter_least(region->phys, &iter);
|
|
|
|
}
|
|
|
|
/* If this phys region crosses the end address, split it. */
|
|
|
|
else if(pr->offset < offset + length
|
|
|
|
&& pr->offset + pr->ph->length > offset + length) {
|
|
|
|
split_phys(pr, offset + length);
|
|
|
|
physr_start_iter_least(region->phys, &iter);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
physr_incr_iter(&iter);
|
|
|
|
}
|
|
|
|
}
|
2010-04-12 13:25:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2010-05-05 13:35:04 +02:00
|
|
|
* rm_phys_regions *
|
2010-04-12 13:25:24 +02:00
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE void rm_phys_regions(struct vir_region *region,
|
2010-05-05 13:35:04 +02:00
|
|
|
vir_bytes begin, vir_bytes length)
|
2010-04-12 13:25:24 +02:00
|
|
|
{
|
|
|
|
/* Remove all phys regions between @begin and @begin+length.
|
|
|
|
*
|
|
|
|
* Don't update the page table, because we will update it at map_memory()
|
|
|
|
* later.
|
|
|
|
*/
|
2010-05-05 13:35:04 +02:00
|
|
|
struct phys_region *pr;
|
|
|
|
physr_iter iter;
|
|
|
|
|
|
|
|
physr_start_iter(region->phys, &iter, begin, AVL_GREATER_EQUAL);
|
|
|
|
while((pr = physr_get_iter(&iter)) && pr->offset < begin + length) {
|
|
|
|
pb_unreferenced(region, pr);
|
|
|
|
physr_remove(region->phys, pr->offset);
|
|
|
|
physr_start_iter(region->phys, &iter, begin,
|
|
|
|
AVL_GREATER_EQUAL);
|
|
|
|
SLABFREE(pr);
|
|
|
|
}
|
2010-04-12 13:25:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* map_memory *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int map_memory(endpoint_t sour, endpoint_t dest,
|
|
|
|
vir_bytes virt_s, vir_bytes virt_d, vir_bytes length, int flag)
|
|
|
|
{
|
|
|
|
/* This is the entry point. This function will be called by handle_memory() when
|
|
|
|
* VM recieves a map-memory request.
|
|
|
|
*/
|
|
|
|
struct vmproc *vms, *vmd;
|
|
|
|
struct vir_region *vrs, *vrd;
|
|
|
|
vir_bytes offset_s, offset_d;
|
|
|
|
int p;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if(vm_isokendpt(sour, &p) != OK)
|
|
|
|
panic("map_memory: bad endpoint: %d", sour);
|
|
|
|
vms = &vmproc[p];
|
|
|
|
if(vm_isokendpt(dest, &p) != OK)
|
|
|
|
panic("map_memory: bad endpoint: %d", dest);
|
|
|
|
vmd = &vmproc[p];
|
|
|
|
|
|
|
|
vrs = map_lookup(vms, virt_s);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(vrs);
|
2010-04-12 13:25:24 +02:00
|
|
|
vrd = map_lookup(vmd, virt_d);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(vrd);
|
2010-04-12 13:25:24 +02:00
|
|
|
|
|
|
|
/* Linear address -> offset from start of vir region. */
|
|
|
|
offset_s = virt_s - vrs->vaddr;
|
|
|
|
offset_d = virt_d - vrd->vaddr;
|
|
|
|
|
|
|
|
/* Make sure that the range in the source process has been mapped
|
|
|
|
* to physical memory.
|
|
|
|
*/
|
|
|
|
map_handle_memory(vms, vrs, offset_s, length, 0);
|
|
|
|
|
|
|
|
/* Prepare work. */
|
|
|
|
clean_phys_regions(vrs, offset_s, length);
|
|
|
|
clean_phys_regions(vrd, offset_d, length);
|
|
|
|
rm_phys_regions(vrd, offset_d, length);
|
|
|
|
|
|
|
|
/* Map memory. */
|
|
|
|
r = do_map_memory(vms, vmd, vrs, vrd, offset_s, offset_d, length, flag);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2010-03-18 18:17:31 +01:00
|
|
|
/*========================================================================*
|
|
|
|
* map_lookup_phys *
|
|
|
|
*========================================================================*/
|
|
|
|
phys_bytes
|
|
|
|
map_lookup_phys(struct vmproc *vmp, u32_t tag)
|
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
|
|
|
struct phys_region *pr;
|
|
|
|
physr_iter iter;
|
|
|
|
|
|
|
|
if(!(vr = map_region_lookup_tag(vmp, tag))) {
|
|
|
|
printf("VM: request for phys of missing region\n");
|
|
|
|
return MAP_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
physr_start_iter_least(vr->phys, &iter);
|
|
|
|
|
|
|
|
if(!(pr = physr_get_iter(&iter))) {
|
|
|
|
printf("VM: request for phys of unmapped region\n");
|
|
|
|
return MAP_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pr->offset != 0 || pr->ph->length != vr->length) {
|
|
|
|
printf("VM: request for phys of partially mapped region\n");
|
|
|
|
return MAP_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pr->ph->phys;
|
|
|
|
}
|
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* get_clean_phys_region *
|
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE struct phys_region *
|
|
|
|
get_clean_phys_region(struct vmproc *vmp, vir_bytes vaddr, vir_bytes length,
|
|
|
|
struct vir_region **ret_region)
|
|
|
|
{
|
|
|
|
struct vir_region *region;
|
|
|
|
vir_bytes regionoffset, mapaddr;
|
|
|
|
struct phys_region *ph;
|
|
|
|
|
|
|
|
mapaddr = arch_vir2map(vmp, vaddr);
|
|
|
|
|
|
|
|
if(!(region = map_lookup(vmp, mapaddr))) {
|
|
|
|
printf("VM: get_clean_phys_region: 0x%lx not found\n", vaddr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!(region->flags & VR_ANON)) {
|
|
|
|
printf("VM: get_clean_phys_region: non-anon 0x%lx\n", vaddr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(mapaddr >= region->vaddr);
|
|
|
|
assert(mapaddr < region->vaddr + region->length);
|
|
|
|
|
|
|
|
regionoffset = mapaddr-region->vaddr;
|
|
|
|
|
|
|
|
/* For now, only support the yielding of blocks that are
|
|
|
|
* exactly a mapped phys_region. Go get that phys_region.
|
|
|
|
* (This can be improved without changing the interface.)
|
|
|
|
*/
|
|
|
|
if(!(ph = physr_search(region->phys, regionoffset,
|
|
|
|
AVL_EQUAL))) {
|
|
|
|
printf("VM: get_clean_phys_region: exact block not found\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure this is what we asked for. */
|
|
|
|
assert(ph->offset == regionoffset);
|
|
|
|
|
|
|
|
if(ph->ph->length != length) {
|
2010-07-05 15:58:57 +02:00
|
|
|
printf("VM: get_clean_phys_region: len mismatch (%lu, %lu)\n",
|
2010-05-05 13:35:04 +02:00
|
|
|
ph->ph->length, length);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it's mapped more than once, make a copy. */
|
|
|
|
assert(ph->ph->refcount > 0);
|
|
|
|
if(ph->ph->refcount > 1) {
|
|
|
|
if(!(ph = map_clone_ph_block(vmp, region,
|
|
|
|
ph, NULL))) {
|
|
|
|
printf("VM: get_clean_phys_region: ph copy failed\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ph->ph->refcount == 1);
|
|
|
|
|
|
|
|
*ret_region = region;
|
|
|
|
|
|
|
|
return ph;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIVATE int getblock(struct vmproc *vmp, u64_t id,
|
|
|
|
vir_bytes vaddr, vir_bytes len)
|
|
|
|
{
|
|
|
|
yielded_t *yb;
|
|
|
|
struct phys_region *ph;
|
|
|
|
struct vir_region *region;
|
2010-10-15 11:10:14 +02:00
|
|
|
yielded_avl *avl;
|
|
|
|
block_id_t blockid;
|
2010-05-05 13:35:04 +02:00
|
|
|
|
|
|
|
/* Try to get the yielded block */
|
2010-10-15 11:10:14 +02:00
|
|
|
blockid.owner = vmp->vm_endpoint;
|
|
|
|
blockid.id = id;
|
|
|
|
avl = get_yielded_avl(blockid);
|
|
|
|
if(!(yb = yielded_search(avl, blockid, AVL_EQUAL))) {
|
2010-05-05 13:35:04 +02:00
|
|
|
return ESRCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the size as a sanity check. */
|
|
|
|
if(yb->len != len) {
|
2010-07-05 15:58:57 +02:00
|
|
|
printf("VM: id 0x%lx%08lx mismatched size (%lu, %lu) for %d\n",
|
2010-05-05 13:35:04 +02:00
|
|
|
ex64hi(id), ex64lo(id), yb->len, len, vmp->vm_endpoint);
|
|
|
|
return ESRCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the intended phys region, make sure refcount is 1. */
|
|
|
|
if(!(ph = get_clean_phys_region(vmp, vaddr, len, ®ion))) {
|
|
|
|
printf("VM: getblock: not found for %d\n", vmp->vm_endpoint);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ph->ph->refcount == 1);
|
|
|
|
|
|
|
|
/* Free the block that is currently there. */
|
|
|
|
free_mem(ABS2CLICK(ph->ph->phys), ABS2CLICK(ph->ph->length));
|
|
|
|
|
|
|
|
/* Set the phys block to new addr and update pagetable. */
|
|
|
|
USE(ph->ph, ph->ph->phys = yb->addr;);
|
|
|
|
if(map_ph_writept(vmp, region, ph) != OK) {
|
|
|
|
/* Presumably it was mapped, so there is no reason
|
|
|
|
* updating should fail.
|
|
|
|
*/
|
|
|
|
panic("do_get_block: couldn't write pt");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Forget about the yielded block and free the struct. */
|
|
|
|
freeyieldednode(yb, 0);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIVATE int yieldblock(struct vmproc *vmp, u64_t id,
|
|
|
|
vir_bytes vaddr, vir_bytes len, yielded_t **retyb)
|
|
|
|
{
|
|
|
|
yielded_t *newyb;
|
2011-06-01 11:30:58 +02:00
|
|
|
vir_bytes mem_clicks, clicks;
|
2010-05-05 13:35:04 +02:00
|
|
|
struct vir_region *region;
|
|
|
|
struct phys_region *ph;
|
2010-10-15 11:10:14 +02:00
|
|
|
yielded_avl *avl;
|
|
|
|
block_id_t blockid;
|
2010-05-05 13:35:04 +02:00
|
|
|
|
|
|
|
/* Makes no sense if yielded block ID already exists, and
|
|
|
|
* is likely a serious bug in the caller.
|
|
|
|
*/
|
2010-10-15 11:10:14 +02:00
|
|
|
blockid.id = id;
|
|
|
|
blockid.owner = vmp->vm_endpoint;
|
|
|
|
avl = get_yielded_avl(blockid);
|
|
|
|
if(yielded_search(avl, blockid, AVL_EQUAL)) {
|
2010-05-05 13:35:04 +02:00
|
|
|
printf("!");
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!(ph = get_clean_phys_region(vmp, vaddr, len, ®ion))) {
|
|
|
|
printf("VM: do_yield_block: not found for %d\n",
|
|
|
|
vmp->vm_endpoint);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make a new block to record the yielding in. */
|
|
|
|
if(!SLABALLOC(newyb)) {
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
2010-04-12 13:25:24 +02:00
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
assert(!(ph->ph->phys % VM_PAGE_SIZE));
|
|
|
|
assert(!(ph->ph->length % VM_PAGE_SIZE));
|
2010-04-12 13:25:24 +02:00
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
clicks = CLICKSPERPAGE * ph->ph->length / VM_PAGE_SIZE;
|
|
|
|
if((mem_clicks = alloc_mem(clicks, PAF_CLEAR)) == NO_MEM) {
|
|
|
|
SLABFREE(newyb);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update yielded block info. */
|
|
|
|
USE(newyb,
|
2010-10-15 11:10:14 +02:00
|
|
|
newyb->id = blockid;
|
2010-05-05 13:35:04 +02:00
|
|
|
newyb->addr = ph->ph->phys;
|
|
|
|
newyb->len = ph->ph->length;
|
|
|
|
newyb->younger = NULL;);
|
|
|
|
|
|
|
|
/* Set new phys block to new addr and update pagetable. */
|
|
|
|
USE(ph->ph,
|
|
|
|
ph->ph->phys = CLICK2ABS(mem_clicks););
|
|
|
|
if(map_ph_writept(vmp, region, ph) != OK) {
|
|
|
|
/* Presumably it was mapped, so there is no reason
|
|
|
|
* updating should fail.
|
|
|
|
*/
|
|
|
|
panic("yield_block: couldn't write pt");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remember yielded block. */
|
2010-10-15 11:10:14 +02:00
|
|
|
|
|
|
|
yielded_insert(avl, newyb);
|
|
|
|
vmp->vm_yielded++;
|
2010-05-05 13:35:04 +02:00
|
|
|
|
|
|
|
/* Add to LRU list too. It's the youngest block. */
|
|
|
|
LRUCHECK;
|
|
|
|
|
|
|
|
if(lru_youngest) {
|
|
|
|
USE(lru_youngest,
|
|
|
|
lru_youngest->younger = newyb;);
|
|
|
|
} else {
|
|
|
|
lru_oldest = newyb;
|
|
|
|
}
|
|
|
|
|
|
|
|
USE(newyb,
|
|
|
|
newyb->older = lru_youngest;);
|
|
|
|
|
|
|
|
lru_youngest = newyb;
|
|
|
|
|
|
|
|
LRUCHECK;
|
|
|
|
|
|
|
|
if(retyb)
|
|
|
|
*retyb = newyb;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_forgetblocks *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int do_forgetblocks(message *m)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
struct vmproc *vmp;
|
|
|
|
endpoint_t caller = m->m_source;
|
|
|
|
|
|
|
|
if(vm_isokendpt(caller, &n) != OK)
|
|
|
|
panic("do_yield_block: message from strange source: %d",
|
|
|
|
m->m_source);
|
|
|
|
|
|
|
|
vmp = &vmproc[n];
|
|
|
|
|
|
|
|
if(!(vmp->vm_flags & VMF_HASPT)) {
|
|
|
|
printf("do_forgetblocks: no pt\n");
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
free_yielded_proc(vmp);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_forgetblock *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int do_forgetblock(message *m)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
struct vmproc *vmp;
|
|
|
|
endpoint_t caller = m->m_source;
|
|
|
|
yielded_t *yb;
|
|
|
|
u64_t id;
|
2010-10-15 11:10:14 +02:00
|
|
|
block_id_t blockid;
|
|
|
|
yielded_avl *avl;
|
2010-05-05 13:35:04 +02:00
|
|
|
|
|
|
|
if(vm_isokendpt(caller, &n) != OK)
|
|
|
|
panic("do_yield_block: message from strange source: %d",
|
|
|
|
m->m_source);
|
|
|
|
|
|
|
|
vmp = &vmproc[n];
|
|
|
|
|
|
|
|
if(!(vmp->vm_flags & VMF_HASPT)) {
|
|
|
|
printf("do_forgetblock: no pt\n");
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = make64(m->VMFB_IDLO, m->VMFB_IDHI);
|
|
|
|
|
2010-10-15 11:10:14 +02:00
|
|
|
blockid.id = id;
|
|
|
|
blockid.owner = vmp->vm_endpoint;
|
|
|
|
avl = get_yielded_avl(blockid);
|
|
|
|
if((yb = yielded_search(avl, blockid, AVL_EQUAL))) {
|
2010-05-05 13:35:04 +02:00
|
|
|
freeyieldednode(yb, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_yieldblockgetblock *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int do_yieldblockgetblock(message *m)
|
|
|
|
{
|
|
|
|
u64_t yieldid, getid;
|
2011-06-01 11:30:58 +02:00
|
|
|
int n;
|
2010-05-05 13:35:04 +02:00
|
|
|
endpoint_t caller = m->m_source;
|
|
|
|
struct vmproc *vmp;
|
|
|
|
yielded_t *yb = NULL;
|
|
|
|
int r = ESRCH;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if(vm_isokendpt(caller, &n) != OK)
|
|
|
|
panic("do_yieldblockgetblock: message from strange source: %d",
|
|
|
|
m->m_source);
|
|
|
|
|
|
|
|
vmp = &vmproc[n];
|
|
|
|
|
|
|
|
if(!(vmp->vm_flags & VMF_HASPT)) {
|
|
|
|
printf("do_yieldblockgetblock: no pt\n");
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = m->VMYBGB_LEN;
|
|
|
|
|
|
|
|
if((len % VM_PAGE_SIZE)) {
|
|
|
|
len += VM_PAGE_SIZE - len % VM_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
yieldid = make64(m->VMYBGB_YIELDIDLO, m->VMYBGB_YIELDIDHI);
|
|
|
|
getid = make64(m->VMYBGB_GETIDLO, m->VMYBGB_GETIDHI);
|
|
|
|
|
|
|
|
if(cmp64(yieldid, VM_BLOCKID_NONE) != 0) {
|
|
|
|
/* A block was given to yield. */
|
|
|
|
yieldblock(vmp, yieldid, (vir_bytes) m->VMYBGB_VADDR, len, &yb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cmp64(getid, VM_BLOCKID_NONE) != 0) {
|
|
|
|
/* A block was given to get. */
|
|
|
|
r = getblock(vmp, getid, (vir_bytes) m->VMYBGB_VADDR, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2010-04-12 13:25:24 +02:00
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
void map_setparent(struct vmproc *vmp)
|
|
|
|
{
|
|
|
|
region_iter iter;
|
|
|
|
struct vir_region *vr;
|
|
|
|
region_start_iter_least(&vmp->vm_regions_avl, &iter);
|
|
|
|
while((vr = region_get_iter(&iter))) {
|
|
|
|
USE(vr, vr->parent = vmp;);
|
|
|
|
region_incr_iter(&iter);
|
|
|
|
}
|
|
|
|
}
|