2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#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>
|
2012-09-18 22:19:22 +02:00
|
|
|
#include <machine/multiboot.h>
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
2012-12-17 19:26:52 +01:00
|
|
|
#include <stdlib.h>
|
2008-11-19 13:26:10 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdint.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"
|
2012-12-17 19:26:52 +01:00
|
|
|
#include "yieldedavl.h"
|
2010-04-12 13:25:24 +02:00
|
|
|
#include "memlist.h"
|
2012-10-11 15:15:49 +02:00
|
|
|
#include "memtype.h"
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
/* LRU list. */
|
2012-03-25 20:25:53 +02:00
|
|
|
static yielded_t *lru_youngest = NULL, *lru_oldest = NULL;
|
2010-05-05 13:35:04 +02:00
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static int map_ph_writept(struct vmproc *vmp, struct vir_region *vr,
|
2012-03-24 16:16:34 +01:00
|
|
|
struct phys_region *pr);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static phys_bytes freeyieldednode(yielded_t *node, int freemem);
|
2010-05-05 13:35:04 +02:00
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static struct vir_region *map_copy_region(struct vmproc *vmp, struct
|
2012-03-24 16:16:34 +01:00
|
|
|
vir_region *vr);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
#if SANITYCHECKS
|
2012-03-25 20:25:53 +02:00
|
|
|
static void lrucheck(void);
|
2010-05-05 13:35:04 +02:00
|
|
|
#endif
|
|
|
|
|
2010-10-15 11:10:14 +02:00
|
|
|
/* hash table of yielded blocks */
|
|
|
|
#define YIELD_HASHSIZE 65536
|
2012-03-25 20:25:53 +02:00
|
|
|
static yielded_avl vm_yielded_blocks[YIELD_HASHSIZE];
|
2010-10-15 11:10:14 +02:00
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static int avl_inited = 0;
|
2010-10-15 11:10:14 +02:00
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
void map_region_init(void)
|
2010-10-15 11:10:14 +02:00
|
|
|
{
|
|
|
|
int h;
|
|
|
|
assert(!avl_inited);
|
|
|
|
for(h = 0; h < YIELD_HASHSIZE; h++)
|
|
|
|
yielded_init(&vm_yielded_blocks[h]);
|
|
|
|
avl_inited = 1;
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static yielded_avl *get_yielded_avl(block_id_t id)
|
2010-10-15 11:10:14 +02:00
|
|
|
{
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2012-12-17 19:26:52 +01:00
|
|
|
void map_printregion(struct vir_region *vr)
|
2009-09-27 14:44:36 +02:00
|
|
|
{
|
2012-12-17 19:26:52 +01:00
|
|
|
int i;
|
2009-09-27 14:44:36 +02:00
|
|
|
struct phys_region *ph;
|
2012-10-11 15:15:49 +02:00
|
|
|
printf("map_printmap: map_name: %s\n", vr->memtype->name);
|
No more intel/minix segments.
This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.
There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.
No static pre-allocated memory sizes exist any more.
Changes to booting:
. The pre_init.c leaves the kernel and modules exactly as
they were left by the bootloader in physical memory
. The kernel starts running using physical addressing,
loaded at a fixed location given in its linker script by the
bootloader. All code and data in this phase are linked to
this fixed low location.
. It makes a bootstrap pagetable to map itself to a
fixed high location (also in linker script) and jumps to
the high address. All code and data then use this high addressing.
. All code/data symbols linked at the low addresses is prefixed by
an objcopy step with __k_unpaged_*, so that that code cannot
reference highly-linked symbols (which aren't valid yet) or vice
versa (symbols that aren't valid any more).
. The two addressing modes are separated in the linker script by
collecting the unpaged_*.o objects and linking them with low
addresses, and linking the rest high. Some objects are linked
twice, once low and once high.
. The bootstrap phase passes a lot of information (e.g. free memory
list, physical location of the modules, etc.) using the kinfo
struct.
. After this bootstrap the low-linked part is freed.
. The kernel maps in VM into the bootstrap page table so that VM can
begin executing. Its first job is to make page tables for all other
boot processes. So VM runs before RS, and RS gets a fully dynamic,
VM-managed address space. VM gets its privilege info from RS as usual
but that happens after RS starts running.
. Both the kernel loading VM and VM organizing boot processes happen
using the libexec logic. This removes the last reason for VM to
still know much about exec() and vm/exec.c is gone.
Further Implementation:
. All segments are based at 0 and have a 4 GB limit.
. The kernel is mapped in at the top of the virtual address
space so as not to constrain the user processes.
. Processes do not use segments from the LDT at all; there are
no segments in the LDT any more, so no LLDT is needed.
. The Minix segments T/D/S are gone and so none of the
user-space or in-kernel copy functions use them. The copy
functions use a process endpoint of NONE to realize it's
a physical address, virtual otherwise.
. The umap call only makes sense to translate a virtual address
to a physical address now.
. Segments-related calls like newmap and alloc_segments are gone.
. All segments-related translation in VM is gone (vir2map etc).
. Initialization in VM is simpler as no moving around is necessary.
. VM and all other boot processes can be linked wherever they wish
and will be mapped in at the right location by the kernel and VM
respectively.
Other changes:
. The multiboot code is less special: it does not use mb_print
for its diagnostics any more but uses printf() as normal, saving
the output into the diagnostics buffer, only printing to the
screen using the direct print functions if a panic() occurs.
. The multiboot code uses the flexible 'free memory map list'
style to receive the list of free memory if available.
. The kernel determines the memory layout of the processes to
a degree: it tells VM where the kernel starts and ends and
where the kernel wants the top of the process to be. VM then
uses this entire range, i.e. the stack is right at the top,
and mmap()ped bits of memory are placed below that downwards,
and the break grows upwards.
Other Consequences:
. Every process gets its own page table as address spaces
can't be separated any more by segments.
. As all segments are 0-based, there is no distinction between
virtual and linear addresses, nor between userspace and
kernel addresses.
. Less work is done when context switching, leading to a net
performance increase. (8% faster on my machine for 'make servers'.)
. The layout and configuration of the GDT makes sysenter and syscall
possible.
2012-05-07 16:03:35 +02:00
|
|
|
printf("\t%lx (len 0x%lx, %lukB), %p\n",
|
2012-10-11 15:15:49 +02:00
|
|
|
vr->vaddr, vr->length, vr->length/1024, vr->memtype->name);
|
2009-09-27 14:44:36 +02:00
|
|
|
printf("\t\tphysblocks:\n");
|
2012-12-17 19:26:52 +01:00
|
|
|
for(i = 0; i < vr->length/VM_PAGE_SIZE; i++) {
|
|
|
|
if(!(ph=vr->physblocks[i])) continue;
|
2012-09-18 13:17:49 +02:00
|
|
|
printf("\t\t@ %lx (refs %d): phys 0x%lx\n",
|
No more intel/minix segments.
This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.
There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.
No static pre-allocated memory sizes exist any more.
Changes to booting:
. The pre_init.c leaves the kernel and modules exactly as
they were left by the bootloader in physical memory
. The kernel starts running using physical addressing,
loaded at a fixed location given in its linker script by the
bootloader. All code and data in this phase are linked to
this fixed low location.
. It makes a bootstrap pagetable to map itself to a
fixed high location (also in linker script) and jumps to
the high address. All code and data then use this high addressing.
. All code/data symbols linked at the low addresses is prefixed by
an objcopy step with __k_unpaged_*, so that that code cannot
reference highly-linked symbols (which aren't valid yet) or vice
versa (symbols that aren't valid any more).
. The two addressing modes are separated in the linker script by
collecting the unpaged_*.o objects and linking them with low
addresses, and linking the rest high. Some objects are linked
twice, once low and once high.
. The bootstrap phase passes a lot of information (e.g. free memory
list, physical location of the modules, etc.) using the kinfo
struct.
. After this bootstrap the low-linked part is freed.
. The kernel maps in VM into the bootstrap page table so that VM can
begin executing. Its first job is to make page tables for all other
boot processes. So VM runs before RS, and RS gets a fully dynamic,
VM-managed address space. VM gets its privilege info from RS as usual
but that happens after RS starts running.
. Both the kernel loading VM and VM organizing boot processes happen
using the libexec logic. This removes the last reason for VM to
still know much about exec() and vm/exec.c is gone.
Further Implementation:
. All segments are based at 0 and have a 4 GB limit.
. The kernel is mapped in at the top of the virtual address
space so as not to constrain the user processes.
. Processes do not use segments from the LDT at all; there are
no segments in the LDT any more, so no LLDT is needed.
. The Minix segments T/D/S are gone and so none of the
user-space or in-kernel copy functions use them. The copy
functions use a process endpoint of NONE to realize it's
a physical address, virtual otherwise.
. The umap call only makes sense to translate a virtual address
to a physical address now.
. Segments-related calls like newmap and alloc_segments are gone.
. All segments-related translation in VM is gone (vir2map etc).
. Initialization in VM is simpler as no moving around is necessary.
. VM and all other boot processes can be linked wherever they wish
and will be mapped in at the right location by the kernel and VM
respectively.
Other changes:
. The multiboot code is less special: it does not use mb_print
for its diagnostics any more but uses printf() as normal, saving
the output into the diagnostics buffer, only printing to the
screen using the direct print functions if a panic() occurs.
. The multiboot code uses the flexible 'free memory map list'
style to receive the list of free memory if available.
. The kernel determines the memory layout of the processes to
a degree: it tells VM where the kernel starts and ends and
where the kernel wants the top of the process to be. VM then
uses this entire range, i.e. the stack is right at the top,
and mmap()ped bits of memory are placed below that downwards,
and the break grows upwards.
Other Consequences:
. Every process gets its own page table as address spaces
can't be separated any more by segments.
. As all segments are 0-based, there is no distinction between
virtual and linear addresses, nor between userspace and
kernel addresses.
. Less work is done when context switching, leading to a net
performance increase. (8% faster on my machine for 'make servers'.)
. The layout and configuration of the GDT makes sysenter and syscall
possible.
2012-05-07 16:03:35 +02:00
|
|
|
(vr->vaddr + ph->offset),
|
2012-09-18 13:17:49 +02:00
|
|
|
ph->ph->refcount, ph->ph->phys);
|
2009-09-27 14:44:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-17 19:26:52 +01:00
|
|
|
struct phys_region *physblock_get(struct vir_region *region, vir_bytes offset)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct phys_region *foundregion;
|
|
|
|
assert(!(offset % VM_PAGE_SIZE));
|
|
|
|
assert(offset >= 0 && offset < region->length);
|
|
|
|
i = offset/VM_PAGE_SIZE;
|
|
|
|
if((foundregion = region->physblocks[i]))
|
|
|
|
assert(foundregion->offset == offset);
|
|
|
|
return foundregion;
|
|
|
|
}
|
|
|
|
|
|
|
|
void physblock_set(struct vir_region *region, vir_bytes offset,
|
|
|
|
struct phys_region *newphysr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
assert(!(offset % VM_PAGE_SIZE));
|
|
|
|
assert(offset >= 0 && offset < region->length);
|
|
|
|
i = offset/VM_PAGE_SIZE;
|
|
|
|
if(newphysr) {
|
|
|
|
assert(!region->physblocks[i]);
|
|
|
|
assert(newphysr->offset == offset);
|
|
|
|
} else {
|
|
|
|
assert(region->physblocks[i]);
|
|
|
|
}
|
|
|
|
region->physblocks[i] = newphysr;
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*===========================================================================*
|
|
|
|
* map_printmap *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
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))) {
|
2012-12-17 19:26:52 +01:00
|
|
|
map_printregion(vr);
|
2010-10-04 13:41:10 +02:00
|
|
|
region_incr_iter(&iter);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static struct vir_region *getnextvr(struct vir_region *vr)
|
2010-10-04 13:41:10 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
int pr_writable(struct vir_region *vr, struct phys_region *pr)
|
|
|
|
{
|
|
|
|
assert(vr->memtype->writable);
|
|
|
|
return ((vr->flags & VR_WRITABLE) && vr->memtype->writable(pr));
|
|
|
|
}
|
|
|
|
|
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 *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static int map_sanitycheck_pt(struct vmproc *vmp,
|
2009-09-23 15:33:01 +02:00
|
|
|
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
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(pr_writable(vr, pr))
|
2009-09-23 15:33:01 +02:00
|
|
|
rw = PTF_WRITE;
|
|
|
|
else
|
2012-08-16 23:34:15 +02:00
|
|
|
rw = PTF_READ;
|
2009-09-23 15:33:01 +02:00
|
|
|
|
2010-09-15 16:11:12 +02:00
|
|
|
r = pt_writemap(vmp, &vmp->vm_pt, vr->vaddr + pr->offset,
|
2012-09-18 13:17:49 +02:00
|
|
|
pb->phys, VM_PAGE_SIZE, 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);
|
2012-12-17 19:26:52 +01:00
|
|
|
map_printregion(vr);
|
2010-04-12 13:25:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
2009-09-23 15:33:01 +02:00
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*===========================================================================*
|
|
|
|
* map_sanitycheck *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void map_sanitycheck(char *file, int line)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
|
|
|
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++) { \
|
2012-12-17 19:26:52 +01:00
|
|
|
vir_bytes voffset; \
|
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))) { \
|
2008-11-19 13:26:10 +01:00
|
|
|
struct phys_region *pr; \
|
|
|
|
regioncode; \
|
2012-12-17 19:26:52 +01:00
|
|
|
for(voffset = 0; voffset < vr->length; \
|
|
|
|
voffset += VM_PAGE_SIZE) { \
|
|
|
|
if(!(pr = physblock_get(vr, voffset))) \
|
|
|
|
continue; \
|
2008-11-19 13:26:10 +01:00
|
|
|
physcode; \
|
|
|
|
} \
|
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;););
|
2012-12-17 19:26:52 +01:00
|
|
|
ALLREGIONS(;,MYASSERT(pr->offset == voffset););
|
2009-09-21 16:49:49 +02:00
|
|
|
ALLREGIONS(;,USE(pr->ph, pr->ph->seencount++;);
|
|
|
|
if(pr->ph->seencount == 1) {
|
2012-10-11 15:15:49 +02:00
|
|
|
if(pr->parent->memtype->ev_sanitycheck)
|
|
|
|
pr->parent->memtype->ev_sanitycheck(pr, file, line);
|
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);
|
2012-09-18 13:17:49 +02:00
|
|
|
printf("ph in vr %p: 0x%lx refcount %u "
|
2012-09-18 13:17:45 +02:00
|
|
|
"but seencount %u\n",
|
2009-09-21 16:49:49 +02:00
|
|
|
vr, pr->offset,
|
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);
|
2012-09-18 13:17:49 +02:00
|
|
|
MYASSERT(!(pr->offset % 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()
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static void lrucheck(void)
|
2010-05-05 13:35:04 +02:00
|
|
|
{
|
|
|
|
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) {
|
2012-09-18 13:17:51 +02:00
|
|
|
mem += VM_PAGE_SIZE;
|
2010-05-05 13:35:04 +02:00
|
|
|
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 *
|
|
|
|
*=========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static int map_ph_writept(struct vmproc *vmp, struct vir_region *vr,
|
2009-09-21 16:49:49 +02:00
|
|
|
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
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
assert(vr);
|
|
|
|
assert(pr);
|
|
|
|
assert(pb);
|
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!(vr->vaddr % VM_PAGE_SIZE));
|
|
|
|
assert(!(pr->offset % VM_PAGE_SIZE));
|
|
|
|
assert(pb->refcount > 0);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(pr_writable(vr, pr))
|
2008-11-19 13:26:10 +01:00
|
|
|
rw = PTF_WRITE;
|
|
|
|
else
|
2012-08-16 23:34:15 +02:00
|
|
|
rw = PTF_READ;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-09-15 16:11:12 +02:00
|
|
|
if(pt_writemap(vmp, &vmp->vm_pt, vr->vaddr + pr->offset,
|
2012-09-18 13:17:49 +02:00
|
|
|
pb->phys, VM_PAGE_SIZE, 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
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static vir_bytes region_find_slot_range(struct vmproc *vmp,
|
2010-10-07 12:04:05 +02:00
|
|
|
vir_bytes minv, vir_bytes maxv, vir_bytes length)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
No more intel/minix segments.
This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.
There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.
No static pre-allocated memory sizes exist any more.
Changes to booting:
. The pre_init.c leaves the kernel and modules exactly as
they were left by the bootloader in physical memory
. The kernel starts running using physical addressing,
loaded at a fixed location given in its linker script by the
bootloader. All code and data in this phase are linked to
this fixed low location.
. It makes a bootstrap pagetable to map itself to a
fixed high location (also in linker script) and jumps to
the high address. All code and data then use this high addressing.
. All code/data symbols linked at the low addresses is prefixed by
an objcopy step with __k_unpaged_*, so that that code cannot
reference highly-linked symbols (which aren't valid yet) or vice
versa (symbols that aren't valid any more).
. The two addressing modes are separated in the linker script by
collecting the unpaged_*.o objects and linking them with low
addresses, and linking the rest high. Some objects are linked
twice, once low and once high.
. The bootstrap phase passes a lot of information (e.g. free memory
list, physical location of the modules, etc.) using the kinfo
struct.
. After this bootstrap the low-linked part is freed.
. The kernel maps in VM into the bootstrap page table so that VM can
begin executing. Its first job is to make page tables for all other
boot processes. So VM runs before RS, and RS gets a fully dynamic,
VM-managed address space. VM gets its privilege info from RS as usual
but that happens after RS starts running.
. Both the kernel loading VM and VM organizing boot processes happen
using the libexec logic. This removes the last reason for VM to
still know much about exec() and vm/exec.c is gone.
Further Implementation:
. All segments are based at 0 and have a 4 GB limit.
. The kernel is mapped in at the top of the virtual address
space so as not to constrain the user processes.
. Processes do not use segments from the LDT at all; there are
no segments in the LDT any more, so no LLDT is needed.
. The Minix segments T/D/S are gone and so none of the
user-space or in-kernel copy functions use them. The copy
functions use a process endpoint of NONE to realize it's
a physical address, virtual otherwise.
. The umap call only makes sense to translate a virtual address
to a physical address now.
. Segments-related calls like newmap and alloc_segments are gone.
. All segments-related translation in VM is gone (vir2map etc).
. Initialization in VM is simpler as no moving around is necessary.
. VM and all other boot processes can be linked wherever they wish
and will be mapped in at the right location by the kernel and VM
respectively.
Other changes:
. The multiboot code is less special: it does not use mb_print
for its diagnostics any more but uses printf() as normal, saving
the output into the diagnostics buffer, only printing to the
screen using the direct print functions if a panic() occurs.
. The multiboot code uses the flexible 'free memory map list'
style to receive the list of free memory if available.
. The kernel determines the memory layout of the processes to
a degree: it tells VM where the kernel starts and ends and
where the kernel wants the top of the process to be. VM then
uses this entire range, i.e. the stack is right at the top,
and mmap()ped bits of memory are placed below that downwards,
and the break grows upwards.
Other Consequences:
. Every process gets its own page table as address spaces
can't be separated any more by segments.
. As all segments are 0-based, there is no distinction between
virtual and linear addresses, nor between userspace and
kernel addresses.
. Less work is done when context switching, leading to a net
performance increase. (8% faster on my machine for 'make servers'.)
. The layout and configuration of the GDT makes sysenter and syscall
possible.
2012-05-07 16:03:35 +02:00
|
|
|
struct vir_region *lastregion;
|
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);
|
|
|
|
}
|
2012-09-18 13:17:52 +02:00
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(minv < maxv);
|
2012-09-18 13:17:52 +02:00
|
|
|
|
|
|
|
if(minv + length > maxv)
|
|
|
|
return SLOT_FAIL;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
No more intel/minix segments.
This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.
There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.
No static pre-allocated memory sizes exist any more.
Changes to booting:
. The pre_init.c leaves the kernel and modules exactly as
they were left by the bootloader in physical memory
. The kernel starts running using physical addressing,
loaded at a fixed location given in its linker script by the
bootloader. All code and data in this phase are linked to
this fixed low location.
. It makes a bootstrap pagetable to map itself to a
fixed high location (also in linker script) and jumps to
the high address. All code and data then use this high addressing.
. All code/data symbols linked at the low addresses is prefixed by
an objcopy step with __k_unpaged_*, so that that code cannot
reference highly-linked symbols (which aren't valid yet) or vice
versa (symbols that aren't valid any more).
. The two addressing modes are separated in the linker script by
collecting the unpaged_*.o objects and linking them with low
addresses, and linking the rest high. Some objects are linked
twice, once low and once high.
. The bootstrap phase passes a lot of information (e.g. free memory
list, physical location of the modules, etc.) using the kinfo
struct.
. After this bootstrap the low-linked part is freed.
. The kernel maps in VM into the bootstrap page table so that VM can
begin executing. Its first job is to make page tables for all other
boot processes. So VM runs before RS, and RS gets a fully dynamic,
VM-managed address space. VM gets its privilege info from RS as usual
but that happens after RS starts running.
. Both the kernel loading VM and VM organizing boot processes happen
using the libexec logic. This removes the last reason for VM to
still know much about exec() and vm/exec.c is gone.
Further Implementation:
. All segments are based at 0 and have a 4 GB limit.
. The kernel is mapped in at the top of the virtual address
space so as not to constrain the user processes.
. Processes do not use segments from the LDT at all; there are
no segments in the LDT any more, so no LLDT is needed.
. The Minix segments T/D/S are gone and so none of the
user-space or in-kernel copy functions use them. The copy
functions use a process endpoint of NONE to realize it's
a physical address, virtual otherwise.
. The umap call only makes sense to translate a virtual address
to a physical address now.
. Segments-related calls like newmap and alloc_segments are gone.
. All segments-related translation in VM is gone (vir2map etc).
. Initialization in VM is simpler as no moving around is necessary.
. VM and all other boot processes can be linked wherever they wish
and will be mapped in at the right location by the kernel and VM
respectively.
Other changes:
. The multiboot code is less special: it does not use mb_print
for its diagnostics any more but uses printf() as normal, saving
the output into the diagnostics buffer, only printing to the
screen using the direct print functions if a panic() occurs.
. The multiboot code uses the flexible 'free memory map list'
style to receive the list of free memory if available.
. The kernel determines the memory layout of the processes to
a degree: it tells VM where the kernel starts and ends and
where the kernel wants the top of the process to be. VM then
uses this entire range, i.e. the stack is right at the top,
and mmap()ped bits of memory are placed below that downwards,
and the break grows upwards.
Other Consequences:
. Every process gets its own page table as address spaces
can't be separated any more by segments.
. As all segments are 0-based, there is no distinction between
virtual and linear addresses, nor between userspace and
kernel addresses.
. Less work is done when context switching, leading to a net
performance increase. (8% faster on my machine for 'make servers'.)
. The layout and configuration of the GDT makes sysenter and syscall
possible.
2012-05-07 16:03:35 +02:00
|
|
|
#define FREEVRANGE_TRY(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) { \
|
No more intel/minix segments.
This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.
There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.
No static pre-allocated memory sizes exist any more.
Changes to booting:
. The pre_init.c leaves the kernel and modules exactly as
they were left by the bootloader in physical memory
. The kernel starts running using physical addressing,
loaded at a fixed location given in its linker script by the
bootloader. All code and data in this phase are linked to
this fixed low location.
. It makes a bootstrap pagetable to map itself to a
fixed high location (also in linker script) and jumps to
the high address. All code and data then use this high addressing.
. All code/data symbols linked at the low addresses is prefixed by
an objcopy step with __k_unpaged_*, so that that code cannot
reference highly-linked symbols (which aren't valid yet) or vice
versa (symbols that aren't valid any more).
. The two addressing modes are separated in the linker script by
collecting the unpaged_*.o objects and linking them with low
addresses, and linking the rest high. Some objects are linked
twice, once low and once high.
. The bootstrap phase passes a lot of information (e.g. free memory
list, physical location of the modules, etc.) using the kinfo
struct.
. After this bootstrap the low-linked part is freed.
. The kernel maps in VM into the bootstrap page table so that VM can
begin executing. Its first job is to make page tables for all other
boot processes. So VM runs before RS, and RS gets a fully dynamic,
VM-managed address space. VM gets its privilege info from RS as usual
but that happens after RS starts running.
. Both the kernel loading VM and VM organizing boot processes happen
using the libexec logic. This removes the last reason for VM to
still know much about exec() and vm/exec.c is gone.
Further Implementation:
. All segments are based at 0 and have a 4 GB limit.
. The kernel is mapped in at the top of the virtual address
space so as not to constrain the user processes.
. Processes do not use segments from the LDT at all; there are
no segments in the LDT any more, so no LLDT is needed.
. The Minix segments T/D/S are gone and so none of the
user-space or in-kernel copy functions use them. The copy
functions use a process endpoint of NONE to realize it's
a physical address, virtual otherwise.
. The umap call only makes sense to translate a virtual address
to a physical address now.
. Segments-related calls like newmap and alloc_segments are gone.
. All segments-related translation in VM is gone (vir2map etc).
. Initialization in VM is simpler as no moving around is necessary.
. VM and all other boot processes can be linked wherever they wish
and will be mapped in at the right location by the kernel and VM
respectively.
Other changes:
. The multiboot code is less special: it does not use mb_print
for its diagnostics any more but uses printf() as normal, saving
the output into the diagnostics buffer, only printing to the
screen using the direct print functions if a panic() occurs.
. The multiboot code uses the flexible 'free memory map list'
style to receive the list of free memory if available.
. The kernel determines the memory layout of the processes to
a degree: it tells VM where the kernel starts and ends and
where the kernel wants the top of the process to be. VM then
uses this entire range, i.e. the stack is right at the top,
and mmap()ped bits of memory are placed below that downwards,
and the break grows upwards.
Other Consequences:
. Every process gets its own page table as address spaces
can't be separated any more by segments.
. As all segments are 0-based, there is no distinction between
virtual and linear addresses, nor between userspace and
kernel addresses.
. Less work is done when context switching, leading to a net
performance increase. (8% faster on my machine for 'make servers'.)
. The layout and configuration of the GDT makes sysenter and syscall
possible.
2012-05-07 16:03:35 +02:00
|
|
|
startv = frend-length; \
|
2008-11-19 13:26:10 +01:00
|
|
|
foundflag = 1; \
|
|
|
|
} }
|
|
|
|
|
No more intel/minix segments.
This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.
There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.
No static pre-allocated memory sizes exist any more.
Changes to booting:
. The pre_init.c leaves the kernel and modules exactly as
they were left by the bootloader in physical memory
. The kernel starts running using physical addressing,
loaded at a fixed location given in its linker script by the
bootloader. All code and data in this phase are linked to
this fixed low location.
. It makes a bootstrap pagetable to map itself to a
fixed high location (also in linker script) and jumps to
the high address. All code and data then use this high addressing.
. All code/data symbols linked at the low addresses is prefixed by
an objcopy step with __k_unpaged_*, so that that code cannot
reference highly-linked symbols (which aren't valid yet) or vice
versa (symbols that aren't valid any more).
. The two addressing modes are separated in the linker script by
collecting the unpaged_*.o objects and linking them with low
addresses, and linking the rest high. Some objects are linked
twice, once low and once high.
. The bootstrap phase passes a lot of information (e.g. free memory
list, physical location of the modules, etc.) using the kinfo
struct.
. After this bootstrap the low-linked part is freed.
. The kernel maps in VM into the bootstrap page table so that VM can
begin executing. Its first job is to make page tables for all other
boot processes. So VM runs before RS, and RS gets a fully dynamic,
VM-managed address space. VM gets its privilege info from RS as usual
but that happens after RS starts running.
. Both the kernel loading VM and VM organizing boot processes happen
using the libexec logic. This removes the last reason for VM to
still know much about exec() and vm/exec.c is gone.
Further Implementation:
. All segments are based at 0 and have a 4 GB limit.
. The kernel is mapped in at the top of the virtual address
space so as not to constrain the user processes.
. Processes do not use segments from the LDT at all; there are
no segments in the LDT any more, so no LLDT is needed.
. The Minix segments T/D/S are gone and so none of the
user-space or in-kernel copy functions use them. The copy
functions use a process endpoint of NONE to realize it's
a physical address, virtual otherwise.
. The umap call only makes sense to translate a virtual address
to a physical address now.
. Segments-related calls like newmap and alloc_segments are gone.
. All segments-related translation in VM is gone (vir2map etc).
. Initialization in VM is simpler as no moving around is necessary.
. VM and all other boot processes can be linked wherever they wish
and will be mapped in at the right location by the kernel and VM
respectively.
Other changes:
. The multiboot code is less special: it does not use mb_print
for its diagnostics any more but uses printf() as normal, saving
the output into the diagnostics buffer, only printing to the
screen using the direct print functions if a panic() occurs.
. The multiboot code uses the flexible 'free memory map list'
style to receive the list of free memory if available.
. The kernel determines the memory layout of the processes to
a degree: it tells VM where the kernel starts and ends and
where the kernel wants the top of the process to be. VM then
uses this entire range, i.e. the stack is right at the top,
and mmap()ped bits of memory are placed below that downwards,
and the break grows upwards.
Other Consequences:
. Every process gets its own page table as address spaces
can't be separated any more by segments.
. As all segments are 0-based, there is no distinction between
virtual and linear addresses, nor between userspace and
kernel addresses.
. Less work is done when context switching, leading to a net
performance increase. (8% faster on my machine for 'make servers'.)
. The layout and configuration of the GDT makes sysenter and syscall
possible.
2012-05-07 16:03:35 +02:00
|
|
|
#define FREEVRANGE(start, end) { \
|
|
|
|
assert(!foundflag); \
|
|
|
|
FREEVRANGE_TRY(((start)+VM_PAGE_SIZE), ((end)-VM_PAGE_SIZE)); \
|
|
|
|
if(!foundflag) { \
|
|
|
|
FREEVRANGE_TRY((start), (end)); \
|
|
|
|
} \
|
|
|
|
}
|
2010-10-07 12:04:05 +02:00
|
|
|
|
No more intel/minix segments.
This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.
There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.
No static pre-allocated memory sizes exist any more.
Changes to booting:
. The pre_init.c leaves the kernel and modules exactly as
they were left by the bootloader in physical memory
. The kernel starts running using physical addressing,
loaded at a fixed location given in its linker script by the
bootloader. All code and data in this phase are linked to
this fixed low location.
. It makes a bootstrap pagetable to map itself to a
fixed high location (also in linker script) and jumps to
the high address. All code and data then use this high addressing.
. All code/data symbols linked at the low addresses is prefixed by
an objcopy step with __k_unpaged_*, so that that code cannot
reference highly-linked symbols (which aren't valid yet) or vice
versa (symbols that aren't valid any more).
. The two addressing modes are separated in the linker script by
collecting the unpaged_*.o objects and linking them with low
addresses, and linking the rest high. Some objects are linked
twice, once low and once high.
. The bootstrap phase passes a lot of information (e.g. free memory
list, physical location of the modules, etc.) using the kinfo
struct.
. After this bootstrap the low-linked part is freed.
. The kernel maps in VM into the bootstrap page table so that VM can
begin executing. Its first job is to make page tables for all other
boot processes. So VM runs before RS, and RS gets a fully dynamic,
VM-managed address space. VM gets its privilege info from RS as usual
but that happens after RS starts running.
. Both the kernel loading VM and VM organizing boot processes happen
using the libexec logic. This removes the last reason for VM to
still know much about exec() and vm/exec.c is gone.
Further Implementation:
. All segments are based at 0 and have a 4 GB limit.
. The kernel is mapped in at the top of the virtual address
space so as not to constrain the user processes.
. Processes do not use segments from the LDT at all; there are
no segments in the LDT any more, so no LLDT is needed.
. The Minix segments T/D/S are gone and so none of the
user-space or in-kernel copy functions use them. The copy
functions use a process endpoint of NONE to realize it's
a physical address, virtual otherwise.
. The umap call only makes sense to translate a virtual address
to a physical address now.
. Segments-related calls like newmap and alloc_segments are gone.
. All segments-related translation in VM is gone (vir2map etc).
. Initialization in VM is simpler as no moving around is necessary.
. VM and all other boot processes can be linked wherever they wish
and will be mapped in at the right location by the kernel and VM
respectively.
Other changes:
. The multiboot code is less special: it does not use mb_print
for its diagnostics any more but uses printf() as normal, saving
the output into the diagnostics buffer, only printing to the
screen using the direct print functions if a panic() occurs.
. The multiboot code uses the flexible 'free memory map list'
style to receive the list of free memory if available.
. The kernel determines the memory layout of the processes to
a degree: it tells VM where the kernel starts and ends and
where the kernel wants the top of the process to be. VM then
uses this entire range, i.e. the stack is right at the top,
and mmap()ped bits of memory are placed below that downwards,
and the break grows upwards.
Other Consequences:
. Every process gets its own page table as address spaces
can't be separated any more by segments.
. As all segments are 0-based, there is no distinction between
virtual and linear addresses, nor between userspace and
kernel addresses.
. Less work is done when context switching, leading to a net
performance increase. (8% faster on my machine for 'make servers'.)
. The layout and configuration of the GDT makes sysenter and syscall
possible.
2012-05-07 16:03:35 +02:00
|
|
|
/* find region after maxv. */
|
|
|
|
region_start_iter(&vmp->vm_regions_avl, &iter, maxv, AVL_GREATER_EQUAL);
|
|
|
|
lastregion = region_get_iter(&iter);
|
|
|
|
|
|
|
|
if(!lastregion) {
|
|
|
|
/* This is the free virtual address space after the last region. */
|
|
|
|
region_start_iter(&vmp->vm_regions_avl, &iter, maxv, AVL_LESS);
|
|
|
|
lastregion = region_get_iter(&iter);
|
|
|
|
FREEVRANGE(lastregion ?
|
|
|
|
lastregion->vaddr+lastregion->length : 0, VM_DATATOP);
|
2010-10-07 12:04:05 +02:00
|
|
|
}
|
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;
|
No more intel/minix segments.
This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.
There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.
No static pre-allocated memory sizes exist any more.
Changes to booting:
. The pre_init.c leaves the kernel and modules exactly as
they were left by the bootloader in physical memory
. The kernel starts running using physical addressing,
loaded at a fixed location given in its linker script by the
bootloader. All code and data in this phase are linked to
this fixed low location.
. It makes a bootstrap pagetable to map itself to a
fixed high location (also in linker script) and jumps to
the high address. All code and data then use this high addressing.
. All code/data symbols linked at the low addresses is prefixed by
an objcopy step with __k_unpaged_*, so that that code cannot
reference highly-linked symbols (which aren't valid yet) or vice
versa (symbols that aren't valid any more).
. The two addressing modes are separated in the linker script by
collecting the unpaged_*.o objects and linking them with low
addresses, and linking the rest high. Some objects are linked
twice, once low and once high.
. The bootstrap phase passes a lot of information (e.g. free memory
list, physical location of the modules, etc.) using the kinfo
struct.
. After this bootstrap the low-linked part is freed.
. The kernel maps in VM into the bootstrap page table so that VM can
begin executing. Its first job is to make page tables for all other
boot processes. So VM runs before RS, and RS gets a fully dynamic,
VM-managed address space. VM gets its privilege info from RS as usual
but that happens after RS starts running.
. Both the kernel loading VM and VM organizing boot processes happen
using the libexec logic. This removes the last reason for VM to
still know much about exec() and vm/exec.c is gone.
Further Implementation:
. All segments are based at 0 and have a 4 GB limit.
. The kernel is mapped in at the top of the virtual address
space so as not to constrain the user processes.
. Processes do not use segments from the LDT at all; there are
no segments in the LDT any more, so no LLDT is needed.
. The Minix segments T/D/S are gone and so none of the
user-space or in-kernel copy functions use them. The copy
functions use a process endpoint of NONE to realize it's
a physical address, virtual otherwise.
. The umap call only makes sense to translate a virtual address
to a physical address now.
. Segments-related calls like newmap and alloc_segments are gone.
. All segments-related translation in VM is gone (vir2map etc).
. Initialization in VM is simpler as no moving around is necessary.
. VM and all other boot processes can be linked wherever they wish
and will be mapped in at the right location by the kernel and VM
respectively.
Other changes:
. The multiboot code is less special: it does not use mb_print
for its diagnostics any more but uses printf() as normal, saving
the output into the diagnostics buffer, only printing to the
screen using the direct print functions if a panic() occurs.
. The multiboot code uses the flexible 'free memory map list'
style to receive the list of free memory if available.
. The kernel determines the memory layout of the processes to
a degree: it tells VM where the kernel starts and ends and
where the kernel wants the top of the process to be. VM then
uses this entire range, i.e. the stack is right at the top,
and mmap()ped bits of memory are placed below that downwards,
and the break grows upwards.
Other Consequences:
. Every process gets its own page table as address spaces
can't be separated any more by segments.
. As all segments are 0-based, there is no distinction between
virtual and linear addresses, nor between userspace and
kernel addresses.
. Less work is done when context switching, leading to a net
performance increase. (8% faster on my machine for 'make servers'.)
. The layout and configuration of the GDT makes sysenter and syscall
possible.
2012-05-07 16:03:35 +02:00
|
|
|
region_decr_iter(&iter);
|
2010-10-04 13:41:10 +02:00
|
|
|
nextvr = region_get_iter(&iter);
|
No more intel/minix segments.
This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.
There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.
No static pre-allocated memory sizes exist any more.
Changes to booting:
. The pre_init.c leaves the kernel and modules exactly as
they were left by the bootloader in physical memory
. The kernel starts running using physical addressing,
loaded at a fixed location given in its linker script by the
bootloader. All code and data in this phase are linked to
this fixed low location.
. It makes a bootstrap pagetable to map itself to a
fixed high location (also in linker script) and jumps to
the high address. All code and data then use this high addressing.
. All code/data symbols linked at the low addresses is prefixed by
an objcopy step with __k_unpaged_*, so that that code cannot
reference highly-linked symbols (which aren't valid yet) or vice
versa (symbols that aren't valid any more).
. The two addressing modes are separated in the linker script by
collecting the unpaged_*.o objects and linking them with low
addresses, and linking the rest high. Some objects are linked
twice, once low and once high.
. The bootstrap phase passes a lot of information (e.g. free memory
list, physical location of the modules, etc.) using the kinfo
struct.
. After this bootstrap the low-linked part is freed.
. The kernel maps in VM into the bootstrap page table so that VM can
begin executing. Its first job is to make page tables for all other
boot processes. So VM runs before RS, and RS gets a fully dynamic,
VM-managed address space. VM gets its privilege info from RS as usual
but that happens after RS starts running.
. Both the kernel loading VM and VM organizing boot processes happen
using the libexec logic. This removes the last reason for VM to
still know much about exec() and vm/exec.c is gone.
Further Implementation:
. All segments are based at 0 and have a 4 GB limit.
. The kernel is mapped in at the top of the virtual address
space so as not to constrain the user processes.
. Processes do not use segments from the LDT at all; there are
no segments in the LDT any more, so no LLDT is needed.
. The Minix segments T/D/S are gone and so none of the
user-space or in-kernel copy functions use them. The copy
functions use a process endpoint of NONE to realize it's
a physical address, virtual otherwise.
. The umap call only makes sense to translate a virtual address
to a physical address now.
. Segments-related calls like newmap and alloc_segments are gone.
. All segments-related translation in VM is gone (vir2map etc).
. Initialization in VM is simpler as no moving around is necessary.
. VM and all other boot processes can be linked wherever they wish
and will be mapped in at the right location by the kernel and VM
respectively.
Other changes:
. The multiboot code is less special: it does not use mb_print
for its diagnostics any more but uses printf() as normal, saving
the output into the diagnostics buffer, only printing to the
screen using the direct print functions if a panic() occurs.
. The multiboot code uses the flexible 'free memory map list'
style to receive the list of free memory if available.
. The kernel determines the memory layout of the processes to
a degree: it tells VM where the kernel starts and ends and
where the kernel wants the top of the process to be. VM then
uses this entire range, i.e. the stack is right at the top,
and mmap()ped bits of memory are placed below that downwards,
and the break grows upwards.
Other Consequences:
. Every process gets its own page table as address spaces
can't be separated any more by segments.
. As all segments are 0-based, there is no distinction between
virtual and linear addresses, nor between userspace and
kernel addresses.
. Less work is done when context switching, leading to a net
performance increase. (8% faster on my machine for 'make servers'.)
. The layout and configuration of the GDT makes sysenter and syscall
possible.
2012-05-07 16:03:35 +02:00
|
|
|
FREEVRANGE(nextvr ? nextvr->vaddr+nextvr->length : 0,
|
|
|
|
vr->vaddr);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!foundflag) {
|
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 *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static vir_bytes region_find_slot(struct vmproc *vmp,
|
2010-10-07 12:04:05 +02:00
|
|
|
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) {
|
No more intel/minix segments.
This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.
There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.
No static pre-allocated memory sizes exist any more.
Changes to booting:
. The pre_init.c leaves the kernel and modules exactly as
they were left by the bootloader in physical memory
. The kernel starts running using physical addressing,
loaded at a fixed location given in its linker script by the
bootloader. All code and data in this phase are linked to
this fixed low location.
. It makes a bootstrap pagetable to map itself to a
fixed high location (also in linker script) and jumps to
the high address. All code and data then use this high addressing.
. All code/data symbols linked at the low addresses is prefixed by
an objcopy step with __k_unpaged_*, so that that code cannot
reference highly-linked symbols (which aren't valid yet) or vice
versa (symbols that aren't valid any more).
. The two addressing modes are separated in the linker script by
collecting the unpaged_*.o objects and linking them with low
addresses, and linking the rest high. Some objects are linked
twice, once low and once high.
. The bootstrap phase passes a lot of information (e.g. free memory
list, physical location of the modules, etc.) using the kinfo
struct.
. After this bootstrap the low-linked part is freed.
. The kernel maps in VM into the bootstrap page table so that VM can
begin executing. Its first job is to make page tables for all other
boot processes. So VM runs before RS, and RS gets a fully dynamic,
VM-managed address space. VM gets its privilege info from RS as usual
but that happens after RS starts running.
. Both the kernel loading VM and VM organizing boot processes happen
using the libexec logic. This removes the last reason for VM to
still know much about exec() and vm/exec.c is gone.
Further Implementation:
. All segments are based at 0 and have a 4 GB limit.
. The kernel is mapped in at the top of the virtual address
space so as not to constrain the user processes.
. Processes do not use segments from the LDT at all; there are
no segments in the LDT any more, so no LLDT is needed.
. The Minix segments T/D/S are gone and so none of the
user-space or in-kernel copy functions use them. The copy
functions use a process endpoint of NONE to realize it's
a physical address, virtual otherwise.
. The umap call only makes sense to translate a virtual address
to a physical address now.
. Segments-related calls like newmap and alloc_segments are gone.
. All segments-related translation in VM is gone (vir2map etc).
. Initialization in VM is simpler as no moving around is necessary.
. VM and all other boot processes can be linked wherever they wish
and will be mapped in at the right location by the kernel and VM
respectively.
Other changes:
. The multiboot code is less special: it does not use mb_print
for its diagnostics any more but uses printf() as normal, saving
the output into the diagnostics buffer, only printing to the
screen using the direct print functions if a panic() occurs.
. The multiboot code uses the flexible 'free memory map list'
style to receive the list of free memory if available.
. The kernel determines the memory layout of the processes to
a degree: it tells VM where the kernel starts and ends and
where the kernel wants the top of the process to be. VM then
uses this entire range, i.e. the stack is right at the top,
and mmap()ped bits of memory are placed below that downwards,
and the break grows upwards.
Other Consequences:
. Every process gets its own page table as address spaces
can't be separated any more by segments.
. As all segments are 0-based, there is no distinction between
virtual and linear addresses, nor between userspace and
kernel addresses.
. Less work is done when context switching, leading to a net
performance increase. (8% faster on my machine for 'make servers'.)
. The layout and configuration of the GDT makes sysenter and syscall
possible.
2012-05-07 16:03:35 +02:00
|
|
|
v = region_find_slot_range(vmp, minv, hint, length);
|
2010-10-07 12:04:05 +02:00
|
|
|
|
|
|
|
if(v != SLOT_FAIL)
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return region_find_slot_range(vmp, minv, maxv, length);
|
|
|
|
}
|
|
|
|
|
2012-12-17 19:26:52 +01:00
|
|
|
static int phys_slot(vir_bytes len)
|
|
|
|
{
|
|
|
|
assert(!(len % VM_PAGE_SIZE));
|
|
|
|
return len / VM_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
struct vir_region *region_new(struct vmproc *vmp, vir_bytes startv, vir_bytes length,
|
|
|
|
int flags, mem_type_t *memtype)
|
2012-09-18 13:17:51 +02:00
|
|
|
{
|
|
|
|
struct vir_region *newregion;
|
2012-12-17 19:26:52 +01:00
|
|
|
struct phys_region **physregions;
|
2012-10-11 15:15:49 +02:00
|
|
|
static u32_t id;
|
2012-12-17 19:26:52 +01:00
|
|
|
int slots = phys_slot(length);
|
2012-09-18 13:17:51 +02:00
|
|
|
|
|
|
|
if(!(SLABALLOC(newregion))) {
|
|
|
|
printf("vm: region_new: could not allocate\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill in node details. */
|
|
|
|
USE(newregion,
|
2012-10-11 15:15:49 +02:00
|
|
|
memset(newregion, 0, sizeof(*newregion));
|
2012-09-18 13:17:51 +02:00
|
|
|
newregion->vaddr = startv;
|
|
|
|
newregion->length = length;
|
|
|
|
newregion->flags = flags;
|
2012-10-11 15:15:49 +02:00
|
|
|
newregion->memtype = memtype;
|
|
|
|
newregion->remaps = 0;
|
|
|
|
newregion->id = id++;
|
2012-09-18 13:17:51 +02:00
|
|
|
newregion->lower = newregion->higher = NULL;
|
|
|
|
newregion->parent = vmp;);
|
|
|
|
|
2012-12-17 19:26:52 +01:00
|
|
|
if(!(physregions = calloc(slots, sizeof(struct phys_region *)))) {
|
|
|
|
printf("VM: region_new: allocating phys blocks failed\n");
|
2012-09-18 13:17:51 +02:00
|
|
|
SLABFREE(newregion);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-12-17 19:26:52 +01:00
|
|
|
|
|
|
|
USE(newregion, newregion->physblocks = physregions;);
|
2012-09-18 13:17:51 +02:00
|
|
|
|
|
|
|
return newregion;
|
|
|
|
}
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* map_page_region *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
struct vir_region *map_page_region(vmp, minv, maxv, length,
|
2012-10-11 15:15:49 +02:00
|
|
|
flags, mapflags, memtype)
|
2009-09-21 16:49:49 +02:00
|
|
|
struct vmproc *vmp;
|
|
|
|
vir_bytes minv;
|
|
|
|
vir_bytes maxv;
|
|
|
|
vir_bytes length;
|
|
|
|
u32_t flags;
|
|
|
|
int mapflags;
|
2012-10-11 15:15:49 +02:00
|
|
|
mem_type_t *memtype;
|
2009-09-21 16:49:49 +02:00
|
|
|
{
|
2010-10-07 12:04:05 +02:00
|
|
|
struct vir_region *newregion;
|
2009-09-21 16:49:49 +02:00
|
|
|
vir_bytes startv;
|
|
|
|
|
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. */
|
2012-10-11 15:15:49 +02:00
|
|
|
if(!(newregion = region_new(vmp, startv, length, flags, memtype))) {
|
2008-11-19 13:26:10 +01:00
|
|
|
printf("VM: map_page_region: allocating region failed\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
/* If a new event is specified, invoke it. */
|
2012-11-09 16:50:31 +01:00
|
|
|
if(newregion->memtype->ev_new) {
|
|
|
|
if(newregion->memtype->ev_new(newregion) != OK) {
|
|
|
|
/* ev_new will have freed and removed the region */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(mapflags & MF_PREALLOC) {
|
2008-11-19 13:26:10 +01:00
|
|
|
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");
|
2012-12-17 19:26:52 +01:00
|
|
|
free(newregion->physblocks);
|
2010-04-12 13:25:24 +02:00
|
|
|
USE(newregion,
|
2012-12-17 19:26:52 +01:00
|
|
|
newregion->physblocks = NULL;);
|
2008-11-19 13:26:10 +01:00
|
|
|
SLABFREE(newregion);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-06 19:05:28 +02:00
|
|
|
/* Pre-allocations should be uninitialized, but after that it's a
|
|
|
|
* different story.
|
|
|
|
*/
|
2012-09-18 13:17:49 +02:00
|
|
|
USE(newregion, newregion->flags &= ~VR_UNINITIALIZED;);
|
2012-06-06 19:05:28 +02:00
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/* 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-09-21 16:49:49 +02:00
|
|
|
* map_subfree *
|
2008-11-19 13:26:10 +01:00
|
|
|
*===========================================================================*/
|
2012-09-18 13:17:52 +02:00
|
|
|
static int map_subfree(struct vir_region *region,
|
|
|
|
vir_bytes start, vir_bytes len)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
2011-06-01 11:30:58 +02:00
|
|
|
struct phys_region *pr;
|
2012-09-18 13:17:52 +02:00
|
|
|
vir_bytes end = start+len;
|
2012-12-17 19:26:52 +01:00
|
|
|
vir_bytes voffset;
|
2010-04-12 13:25:24 +02:00
|
|
|
|
2009-04-22 14:39:29 +02:00
|
|
|
#if SANITYCHECKS
|
2010-10-04 13:41:10 +02:00
|
|
|
SLABSANE(region);
|
2012-12-17 19:26:52 +01:00
|
|
|
for(voffset = 0; voffset < phys_slot(region->length);
|
|
|
|
voffset += VM_PAGE_SIZE) {
|
2009-04-22 14:39:29 +02:00
|
|
|
struct phys_region *others;
|
|
|
|
struct phys_block *pb;
|
|
|
|
|
2012-12-17 19:26:52 +01:00
|
|
|
if(!(pr = physblock_get(region, voffset)))
|
|
|
|
continue;
|
|
|
|
|
2009-04-22 14:39:29 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-12-17 19:26:52 +01:00
|
|
|
for(voffset = start; voffset < end; voffset+=VM_PAGE_SIZE) {
|
|
|
|
if(!(pr = physblock_get(region, voffset)))
|
|
|
|
continue;
|
|
|
|
assert(pr->offset >= start);
|
|
|
|
assert(pr->offset < end);
|
|
|
|
pb_unreferenced(region, pr, 1);
|
2012-09-18 13:17:52 +02:00
|
|
|
SLABFREE(pr);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* map_free *
|
|
|
|
*===========================================================================*/
|
2012-10-11 15:15:49 +02:00
|
|
|
int map_free(struct vir_region *region)
|
2009-09-21 16:49:49 +02:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
if((r=map_subfree(region, 0, region->length)) != OK) {
|
2010-04-12 13:25:24 +02:00
|
|
|
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
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(region->memtype->ev_delete)
|
|
|
|
region->memtype->ev_delete(region);
|
2012-12-17 19:26:52 +01:00
|
|
|
free(region->physblocks);
|
|
|
|
region->physblocks = NULL;
|
2008-11-19 13:26:10 +01:00
|
|
|
SLABFREE(region);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2010-10-15 11:10:14 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* yielded_block_cmp *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int yielded_block_cmp(struct block_id *id1, struct block_id *id2)
|
2010-10-15 11:10:14 +02:00
|
|
|
{
|
|
|
|
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 *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static vir_bytes free_yielded_proc(struct vmproc *vmp)
|
2010-05-05 13:35:04 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static phys_bytes freeyieldednode(yielded_t *node, int freemem)
|
2010-05-05 13:35:04 +02:00
|
|
|
{
|
|
|
|
yielded_t *older, *younger, *removed;
|
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) {
|
2012-09-18 13:17:52 +02:00
|
|
|
free_mem(ABS2CLICK(node->physaddr), node->pages);
|
2010-05-05 13:35:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free node. */
|
|
|
|
SLABFREE(node);
|
|
|
|
|
2012-09-18 13:17:49 +02:00
|
|
|
return VM_PAGE_SIZE;
|
2010-05-05 13:35:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* free_yielded *
|
|
|
|
*========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
vir_bytes free_yielded(vir_bytes max_bytes)
|
2010-05-05 13:35:04 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
/* 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 *
|
|
|
|
*========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int map_free_proc(vmp)
|
2008-11-19 13:26:10 +01:00
|
|
|
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. */
|
2012-09-18 13:17:51 +02:00
|
|
|
map_free(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 *
|
|
|
|
*===========================================================================*/
|
2012-09-18 13:17:52 +02:00
|
|
|
struct vir_region *map_lookup(vmp, offset, physr)
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vmproc *vmp;
|
|
|
|
vir_bytes offset;
|
2012-09-18 13:17:52 +02:00
|
|
|
struct phys_region **physr;
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
|
|
|
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))) {
|
2012-09-18 13:17:52 +02:00
|
|
|
vir_bytes ph;
|
|
|
|
if(offset >= r->vaddr && offset < r->vaddr + r->length) {
|
|
|
|
ph = offset - r->vaddr;
|
|
|
|
if(physr) {
|
2012-12-17 19:26:52 +01:00
|
|
|
*physr = physblock_get(r, ph);
|
2012-10-24 19:47:47 +02:00
|
|
|
if(*physr) assert((*physr)->offset == ph);
|
2012-09-18 13:17:52 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
return r;
|
2012-09-18 13:17:52 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
u32_t vrallocflags(u32_t flags)
|
2010-04-12 13:25:24 +02:00
|
|
|
{
|
|
|
|
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;
|
2012-10-11 15:15:49 +02:00
|
|
|
if(!(flags & VR_UNINITIALIZED))
|
|
|
|
allocflags |= PAF_CLEAR;
|
2010-04-12 13:25:24 +02:00
|
|
|
|
|
|
|
return allocflags;
|
|
|
|
}
|
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
|
|
|
*===========================================================================*/
|
2012-12-17 19:26:52 +01:00
|
|
|
struct phys_region *map_clone_ph_block(vmp, region, ph)
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vmproc *vmp;
|
|
|
|
struct vir_region *region;
|
|
|
|
struct phys_region *ph;
|
|
|
|
{
|
2012-09-18 13:17:49 +02:00
|
|
|
vir_bytes offset;
|
2010-04-12 13:25:24 +02:00
|
|
|
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
|
|
|
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)
|
2012-09-18 13:17:51 +02:00
|
|
|
* with another! So both the pointer to it
|
2010-04-12 13:25:24 +02:00
|
|
|
* 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;
|
|
|
|
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);
|
2012-09-18 13:17:52 +02:00
|
|
|
pb_unreferenced(region, ph, 1);
|
2010-04-12 13:25:24 +02:00
|
|
|
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. */
|
2012-10-11 15:15:49 +02:00
|
|
|
allocflags = vrallocflags(region->flags | VR_UNINITIALIZED);
|
2012-09-18 13:17:49 +02:00
|
|
|
region_has_single_block = (offset == 0 && region->length == VM_PAGE_SIZE);
|
2010-07-20 20:57:25 +02:00
|
|
|
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
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(map_pf(vmp, region, offset, 1) != OK) {
|
2010-04-12 13:25:24 +02:00
|
|
|
/* XXX original range now gone. */
|
2012-10-11 15:15:49 +02:00
|
|
|
printf("VM: map_clone_ph_block: map_pf failed.\n");
|
2010-04-12 13:25:24 +02:00
|
|
|
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.
|
|
|
|
*/
|
2012-09-18 13:17:49 +02:00
|
|
|
if(copy_abs2region(physaddr, region, offset, VM_PAGE_SIZE) != OK)
|
2010-04-12 13:25:24 +02:00
|
|
|
panic("copy_abs2region failed, no good reason for that");
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2012-12-17 19:26:52 +01:00
|
|
|
newpr = physblock_get(region, offset);
|
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
|
|
|
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
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
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;
|
|
|
|
{
|
|
|
|
struct phys_region *ph;
|
2009-09-21 16:49:49 +02:00
|
|
|
int r = OK;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
offset -= offset % VM_PAGE_SIZE;
|
|
|
|
|
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->vaddr % VM_PAGE_SIZE));
|
2012-10-11 15:15:49 +02:00
|
|
|
assert(!(write && !(region->flags & VR_WRITABLE)));
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
2012-12-17 19:26:52 +01:00
|
|
|
if(!(ph = physblock_get(region, offset))) {
|
2012-10-11 15:15:49 +02:00
|
|
|
struct phys_block *pb;
|
|
|
|
|
|
|
|
/* New block. */
|
|
|
|
|
|
|
|
if(!(pb = pb_new(MAP_NONE))) {
|
|
|
|
printf("map_pf: pb_new failed\n");
|
|
|
|
return ENOMEM;
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
2012-10-11 15:15:49 +02:00
|
|
|
|
|
|
|
if(!(ph = pb_reference(pb, offset, region))) {
|
|
|
|
printf("map_pf: pb_reference failed\n");
|
|
|
|
pb_free(pb);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ph);
|
|
|
|
assert(ph->ph);
|
|
|
|
|
|
|
|
/* If we're writing and the block is already
|
|
|
|
* writable, nothing to do.
|
|
|
|
*/
|
|
|
|
|
|
|
|
assert(region->memtype->writable);
|
|
|
|
|
|
|
|
if(!write || !region->memtype->writable(ph)) {
|
|
|
|
assert(region->memtype->ev_pagefault);
|
|
|
|
assert(ph->ph);
|
2012-12-17 19:26:52 +01:00
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if((r = region->memtype->ev_pagefault(vmp,
|
|
|
|
region, ph, write)) == SUSPEND) {
|
|
|
|
panic("map_pf: memtype->ev_pagefault returned SUSPEND\n");
|
|
|
|
return SUSPEND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(r != OK) {
|
|
|
|
printf("map_pf: memtype->ev_pagefault failed\n");
|
|
|
|
if(ph)
|
|
|
|
pb_unreferenced(region, ph, 1);
|
|
|
|
return r;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
2012-10-11 15:15:49 +02:00
|
|
|
|
|
|
|
assert(ph);
|
|
|
|
assert(ph->ph);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
assert(ph->ph);
|
2009-09-27 14:44:36 +02:00
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if((r = map_ph_writept(vmp, region, ph)) != OK) {
|
|
|
|
printf("map_pf: writept failed\n");
|
2009-09-27 14:44:36 +02:00
|
|
|
return r;
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
2009-09-27 14:44:36 +02:00
|
|
|
#if SANITYCHECKS
|
2012-10-11 15:15:49 +02:00
|
|
|
if(OK != pt_checkrange(&vmp->vm_pt, region->vaddr+offset,
|
2010-04-12 13:25:24 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
int map_handle_memory(vmp, region, start_offset, length, write)
|
|
|
|
struct vmproc *vmp;
|
|
|
|
struct vir_region *region;
|
|
|
|
vir_bytes start_offset;
|
|
|
|
vir_bytes length;
|
|
|
|
int write;
|
|
|
|
{
|
|
|
|
vir_bytes offset, lim;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(length > 0);
|
|
|
|
lim = start_offset + length;
|
|
|
|
assert(lim > start_offset);
|
|
|
|
|
|
|
|
for(offset = start_offset; offset < lim; offset += VM_PAGE_SIZE)
|
|
|
|
if((r = map_pf(vmp, region, offset, write)) != OK)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2010-06-28 23:53:37 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* map_pin_memory *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int map_pin_memory(struct vmproc *vmp)
|
2010-06-28 23:53:37 +02:00
|
|
|
{
|
|
|
|
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_copy_region *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static 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;
|
2012-10-11 15:15:49 +02:00
|
|
|
int r;
|
2008-11-19 13:26:10 +01:00
|
|
|
#if SANITYCHECKS
|
|
|
|
int cr;
|
2012-12-17 19:26:52 +01:00
|
|
|
cr = physregions(vr);
|
2008-11-19 13:26:10 +01:00
|
|
|
#endif
|
2012-12-17 19:26:52 +01:00
|
|
|
vir_bytes p;
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(!(newvr = region_new(vr->parent, vr->vaddr, vr->length, vr->flags, vr->memtype)))
|
2009-09-21 16:49:49 +02:00
|
|
|
return NULL;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(vr->memtype->ev_copy && (r=vr->memtype->ev_copy(vr, newvr)) != OK) {
|
|
|
|
map_free(newvr);
|
|
|
|
printf("VM: memtype-specific copy failed (%d)\n", r);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-17 19:26:52 +01:00
|
|
|
for(p = 0; p < phys_slot(vr->length); p++) {
|
|
|
|
if(!(ph = physblock_get(vr, p*VM_PAGE_SIZE))) continue;
|
2012-09-18 13:17:51 +02:00
|
|
|
struct phys_region *newph = pb_reference(ph->ph, ph->offset, newvr);
|
|
|
|
|
|
|
|
if(!newph) { map_free(newvr); return NULL; }
|
|
|
|
|
2009-09-27 14:44:36 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
USE(newph, newph->written = 0;);
|
2012-12-17 19:26:52 +01:00
|
|
|
assert(physregions(vr) == cr);
|
2010-04-12 13:25:24 +02:00
|
|
|
#endif
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
#if SANITYCHECKS
|
2012-12-17 19:26:52 +01:00
|
|
|
assert(physregions(vr) == physregions(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 *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int copy_abs2region(phys_bytes abs, struct vir_region *destregion,
|
2010-04-12 13:25:24 +02:00
|
|
|
phys_bytes offset, phys_bytes len)
|
|
|
|
|
|
|
|
{
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(destregion);
|
2012-12-17 19:26:52 +01:00
|
|
|
assert(destregion->physblocks);
|
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);
|
2012-12-17 19:26:52 +01:00
|
|
|
assert(destregion->physblocks);
|
|
|
|
if(!(ph = physblock_get(destregion, offset))) {
|
2010-04-12 13:25:24 +02:00
|
|
|
printf("VM: copy_abs2region: no phys region found (1).\n");
|
|
|
|
return EFAULT;
|
|
|
|
}
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(ph->offset <= offset);
|
2012-09-18 13:17:49 +02:00
|
|
|
if(ph->offset+VM_PAGE_SIZE <= offset) {
|
2010-04-12 13:25:24 +02:00
|
|
|
printf("VM: copy_abs2region: no phys region found (2).\n");
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
suboffset = offset - ph->offset;
|
2012-09-18 13:17:49 +02:00
|
|
|
assert(suboffset < VM_PAGE_SIZE);
|
2010-04-12 13:25:24 +02:00
|
|
|
sublen = len;
|
2012-09-18 13:17:49 +02:00
|
|
|
if(sublen > VM_PAGE_SIZE - suboffset)
|
|
|
|
sublen = VM_PAGE_SIZE - suboffset;
|
|
|
|
assert(suboffset + sublen <= VM_PAGE_SIZE);
|
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 *
|
|
|
|
*=========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int map_writept(struct vmproc *vmp)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
|
|
|
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))) {
|
2012-12-17 19:26:52 +01:00
|
|
|
vir_bytes p;
|
|
|
|
for(p = 0; p < vr->length; p += VM_PAGE_SIZE) {
|
|
|
|
if(!(ph = physblock_get(vr, p))) continue;
|
2010-01-14 16:24:16 +01:00
|
|
|
|
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
|
|
|
*========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int map_proc_copy(dst, src)
|
2008-11-19 13:26:10 +01:00
|
|
|
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 *
|
|
|
|
*========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int map_proc_copy_from(dst, src, start_src_vr)
|
2010-07-20 04:08:28 +02:00
|
|
|
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))) {
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vir_region *newvr;
|
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);
|
2012-12-17 19:26:52 +01:00
|
|
|
assert(vr->length == newvr->length);
|
|
|
|
|
|
|
|
#if SANITYCHECKS
|
|
|
|
{
|
|
|
|
vir_bytes vaddr;
|
|
|
|
struct phys_region *orig_ph, *new_ph;
|
|
|
|
assert(vr->physblocks != newvr->physblocks);
|
|
|
|
for(vaddr = 0; vaddr < vr->length; vaddr += VM_PAGE_SIZE) {
|
|
|
|
orig_ph = physblock_get(vr, vaddr);
|
|
|
|
new_ph = physblock_get(newvr, vaddr);
|
|
|
|
if(!orig_ph) { assert(!new_ph); continue;}
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(new_ph);
|
|
|
|
assert(orig_ph != new_ph);
|
2012-09-18 13:17:51 +02:00
|
|
|
assert(orig_ph->ph == new_ph->ph);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
2012-12-17 19:26:52 +01:00
|
|
|
}
|
|
|
|
#endif
|
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;
|
|
|
|
}
|
|
|
|
|
2012-04-07 01:19:28 +02:00
|
|
|
int map_region_extend_upto_v(struct vmproc *vmp, vir_bytes v)
|
|
|
|
{
|
2012-10-11 15:15:49 +02:00
|
|
|
vir_bytes offset = v;
|
2012-04-07 01:19:28 +02:00
|
|
|
struct vir_region *vr, *nextvr;
|
2012-12-17 19:26:52 +01:00
|
|
|
struct phys_region **newpr;
|
|
|
|
int newslots, prevslots, addedslots;
|
2012-10-11 15:15:49 +02:00
|
|
|
|
|
|
|
offset = roundup(offset, VM_PAGE_SIZE);
|
2012-04-07 01:19:28 +02:00
|
|
|
|
No more intel/minix segments.
This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.
There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.
No static pre-allocated memory sizes exist any more.
Changes to booting:
. The pre_init.c leaves the kernel and modules exactly as
they were left by the bootloader in physical memory
. The kernel starts running using physical addressing,
loaded at a fixed location given in its linker script by the
bootloader. All code and data in this phase are linked to
this fixed low location.
. It makes a bootstrap pagetable to map itself to a
fixed high location (also in linker script) and jumps to
the high address. All code and data then use this high addressing.
. All code/data symbols linked at the low addresses is prefixed by
an objcopy step with __k_unpaged_*, so that that code cannot
reference highly-linked symbols (which aren't valid yet) or vice
versa (symbols that aren't valid any more).
. The two addressing modes are separated in the linker script by
collecting the unpaged_*.o objects and linking them with low
addresses, and linking the rest high. Some objects are linked
twice, once low and once high.
. The bootstrap phase passes a lot of information (e.g. free memory
list, physical location of the modules, etc.) using the kinfo
struct.
. After this bootstrap the low-linked part is freed.
. The kernel maps in VM into the bootstrap page table so that VM can
begin executing. Its first job is to make page tables for all other
boot processes. So VM runs before RS, and RS gets a fully dynamic,
VM-managed address space. VM gets its privilege info from RS as usual
but that happens after RS starts running.
. Both the kernel loading VM and VM organizing boot processes happen
using the libexec logic. This removes the last reason for VM to
still know much about exec() and vm/exec.c is gone.
Further Implementation:
. All segments are based at 0 and have a 4 GB limit.
. The kernel is mapped in at the top of the virtual address
space so as not to constrain the user processes.
. Processes do not use segments from the LDT at all; there are
no segments in the LDT any more, so no LLDT is needed.
. The Minix segments T/D/S are gone and so none of the
user-space or in-kernel copy functions use them. The copy
functions use a process endpoint of NONE to realize it's
a physical address, virtual otherwise.
. The umap call only makes sense to translate a virtual address
to a physical address now.
. Segments-related calls like newmap and alloc_segments are gone.
. All segments-related translation in VM is gone (vir2map etc).
. Initialization in VM is simpler as no moving around is necessary.
. VM and all other boot processes can be linked wherever they wish
and will be mapped in at the right location by the kernel and VM
respectively.
Other changes:
. The multiboot code is less special: it does not use mb_print
for its diagnostics any more but uses printf() as normal, saving
the output into the diagnostics buffer, only printing to the
screen using the direct print functions if a panic() occurs.
. The multiboot code uses the flexible 'free memory map list'
style to receive the list of free memory if available.
. The kernel determines the memory layout of the processes to
a degree: it tells VM where the kernel starts and ends and
where the kernel wants the top of the process to be. VM then
uses this entire range, i.e. the stack is right at the top,
and mmap()ped bits of memory are placed below that downwards,
and the break grows upwards.
Other Consequences:
. Every process gets its own page table as address spaces
can't be separated any more by segments.
. As all segments are 0-based, there is no distinction between
virtual and linear addresses, nor between userspace and
kernel addresses.
. Less work is done when context switching, leading to a net
performance increase. (8% faster on my machine for 'make servers'.)
. The layout and configuration of the GDT makes sysenter and syscall
possible.
2012-05-07 16:03:35 +02:00
|
|
|
if(!(vr = region_search(&vmp->vm_regions_avl, offset, AVL_LESS))) {
|
2012-04-07 01:19:28 +02:00
|
|
|
printf("VM: nothing to extend\n");
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
2012-12-17 19:26:52 +01:00
|
|
|
if(vr->vaddr + vr->length >= v) return OK;
|
|
|
|
|
2012-04-07 01:19:28 +02:00
|
|
|
assert(vr->vaddr <= offset);
|
2012-12-17 19:26:52 +01:00
|
|
|
newslots = phys_slot(offset - vr->vaddr);
|
|
|
|
prevslots = phys_slot(vr->length);
|
|
|
|
assert(newslots >= prevslots);
|
|
|
|
addedslots = newslots - prevslots;
|
|
|
|
|
|
|
|
if(!(newpr = realloc(vr->physblocks,
|
|
|
|
newslots * sizeof(struct phys_region *)))) {
|
|
|
|
printf("VM: map_region_extend_upto_v: realloc failed\n");
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
vr->physblocks = newpr;
|
|
|
|
memset(vr->physblocks + prevslots, 0,
|
|
|
|
addedslots * sizeof(struct phys_region *));
|
|
|
|
|
2012-04-07 01:19:28 +02:00
|
|
|
if((nextvr = getnextvr(vr))) {
|
No more intel/minix segments.
This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.
There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.
No static pre-allocated memory sizes exist any more.
Changes to booting:
. The pre_init.c leaves the kernel and modules exactly as
they were left by the bootloader in physical memory
. The kernel starts running using physical addressing,
loaded at a fixed location given in its linker script by the
bootloader. All code and data in this phase are linked to
this fixed low location.
. It makes a bootstrap pagetable to map itself to a
fixed high location (also in linker script) and jumps to
the high address. All code and data then use this high addressing.
. All code/data symbols linked at the low addresses is prefixed by
an objcopy step with __k_unpaged_*, so that that code cannot
reference highly-linked symbols (which aren't valid yet) or vice
versa (symbols that aren't valid any more).
. The two addressing modes are separated in the linker script by
collecting the unpaged_*.o objects and linking them with low
addresses, and linking the rest high. Some objects are linked
twice, once low and once high.
. The bootstrap phase passes a lot of information (e.g. free memory
list, physical location of the modules, etc.) using the kinfo
struct.
. After this bootstrap the low-linked part is freed.
. The kernel maps in VM into the bootstrap page table so that VM can
begin executing. Its first job is to make page tables for all other
boot processes. So VM runs before RS, and RS gets a fully dynamic,
VM-managed address space. VM gets its privilege info from RS as usual
but that happens after RS starts running.
. Both the kernel loading VM and VM organizing boot processes happen
using the libexec logic. This removes the last reason for VM to
still know much about exec() and vm/exec.c is gone.
Further Implementation:
. All segments are based at 0 and have a 4 GB limit.
. The kernel is mapped in at the top of the virtual address
space so as not to constrain the user processes.
. Processes do not use segments from the LDT at all; there are
no segments in the LDT any more, so no LLDT is needed.
. The Minix segments T/D/S are gone and so none of the
user-space or in-kernel copy functions use them. The copy
functions use a process endpoint of NONE to realize it's
a physical address, virtual otherwise.
. The umap call only makes sense to translate a virtual address
to a physical address now.
. Segments-related calls like newmap and alloc_segments are gone.
. All segments-related translation in VM is gone (vir2map etc).
. Initialization in VM is simpler as no moving around is necessary.
. VM and all other boot processes can be linked wherever they wish
and will be mapped in at the right location by the kernel and VM
respectively.
Other changes:
. The multiboot code is less special: it does not use mb_print
for its diagnostics any more but uses printf() as normal, saving
the output into the diagnostics buffer, only printing to the
screen using the direct print functions if a panic() occurs.
. The multiboot code uses the flexible 'free memory map list'
style to receive the list of free memory if available.
. The kernel determines the memory layout of the processes to
a degree: it tells VM where the kernel starts and ends and
where the kernel wants the top of the process to be. VM then
uses this entire range, i.e. the stack is right at the top,
and mmap()ped bits of memory are placed below that downwards,
and the break grows upwards.
Other Consequences:
. Every process gets its own page table as address spaces
can't be separated any more by segments.
. As all segments are 0-based, there is no distinction between
virtual and linear addresses, nor between userspace and
kernel addresses.
. Less work is done when context switching, leading to a net
performance increase. (8% faster on my machine for 'make servers'.)
. The layout and configuration of the GDT makes sysenter and syscall
possible.
2012-05-07 16:03:35 +02:00
|
|
|
assert(offset <= nextvr->vaddr);
|
2012-04-07 01:19:28 +02:00
|
|
|
}
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(nextvr && nextvr->vaddr < offset) {
|
|
|
|
printf("VM: can't grow into next region\n");
|
2008-11-19 13:26:10 +01:00
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(!vr->memtype->ev_resize) {
|
|
|
|
printf("VM: can't resize this type of memory\n");
|
|
|
|
return ENOMEM;
|
2010-10-04 13:41:10 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
return vr->memtype->ev_resize(vmp, vr, offset - vr->vaddr);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* map_unmap_region *
|
|
|
|
*========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int map_unmap_region(struct vmproc *vmp, struct vir_region *r,
|
2012-09-18 13:17:52 +02:00
|
|
|
vir_bytes offset, 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;
|
2012-12-17 19:26:52 +01:00
|
|
|
int freeslots = phys_slot(len);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
SANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
if(offset+len > r->length || (len % VM_PAGE_SIZE)) {
|
2009-09-21 16:49:49 +02:00
|
|
|
printf("VM: bogus length 0x%lx\n", len);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
regionstart = r->vaddr + offset;
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
/* unreference its memory */
|
|
|
|
map_subfree(r, offset, len);
|
|
|
|
|
|
|
|
/* if unmap was at start/end of this region, it actually shrinks */
|
|
|
|
if(offset == 0) {
|
2009-09-21 16:49:49 +02:00
|
|
|
struct phys_region *pr;
|
2012-12-17 19:26:52 +01:00
|
|
|
vir_bytes voffset;
|
|
|
|
int remslots;
|
2012-09-18 13:17:52 +02:00
|
|
|
|
|
|
|
region_remove(&vmp->vm_regions_avl, r->vaddr);
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(r,
|
|
|
|
r->vaddr += len;
|
|
|
|
r->length -= len;);
|
2012-09-18 13:17:52 +02:00
|
|
|
|
2012-12-17 19:26:52 +01:00
|
|
|
remslots = phys_slot(r->length);
|
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
region_insert(&vmp->vm_regions_avl, r);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
/* vaddr has increased; to make all the phys_regions
|
|
|
|
* point to the same addresses, make them shrink by the
|
|
|
|
* same amount.
|
|
|
|
*/
|
2012-12-17 19:26:52 +01:00
|
|
|
for(voffset = offset; voffset < r->length;
|
|
|
|
voffset += VM_PAGE_SIZE) {
|
|
|
|
if(!(pr = physblock_get(r, voffset))) continue;
|
2012-09-18 13:17:52 +02:00
|
|
|
assert(pr->offset >= offset);
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(pr, pr->offset -= len;);
|
|
|
|
}
|
2012-12-17 19:26:52 +01:00
|
|
|
if(remslots)
|
|
|
|
memmove(r->physblocks, r->physblocks + freeslots,
|
|
|
|
remslots * sizeof(struct phys_region *));
|
2012-09-18 13:17:52 +02:00
|
|
|
} else if(offset + len == r->length) {
|
|
|
|
assert(len <= r->length);
|
|
|
|
r->length -= len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(r->length == 0) {
|
|
|
|
/* Whole region disappears. Unlink and free it. */
|
|
|
|
region_remove(&vmp->vm_regions_avl, r->vaddr);
|
|
|
|
map_free(r);
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
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_get_phys *
|
|
|
|
*========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int map_get_phys(struct vmproc *vmp, vir_bytes addr, phys_bytes *r)
|
2009-09-21 16:49:49 +02:00
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
if (!(vr = map_lookup(vmp, addr, NULL)) ||
|
2009-09-21 16:49:49 +02:00
|
|
|
(vr->vaddr != addr))
|
|
|
|
return EINVAL;
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if (!vr->memtype->regionid)
|
2009-09-21 16:49:49 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(r)
|
|
|
|
*r = vr->memtype->regionid(vr);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*========================================================================*
|
|
|
|
* map_get_ref *
|
|
|
|
*========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int map_get_ref(struct vmproc *vmp, vir_bytes addr, u8_t *cnt)
|
2009-09-21 16:49:49 +02:00
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
if (!(vr = map_lookup(vmp, addr, NULL)) ||
|
2012-10-11 15:15:49 +02:00
|
|
|
(vr->vaddr != addr) || !vr->memtype->refcount)
|
2009-09-21 16:49:49 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (cnt)
|
2012-10-11 15:15:49 +02:00
|
|
|
*cnt = vr->memtype->refcount(vr);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2010-09-14 23:22:56 +02:00
|
|
|
/*========================================================================*
|
|
|
|
* get_stats_info *
|
|
|
|
*========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void get_stats_info(struct vm_stats_info *vsi)
|
2010-09-14 23:22:56 +02:00
|
|
|
{
|
|
|
|
yielded_t *yb;
|
|
|
|
|
|
|
|
vsi->vsi_cached = 0L;
|
|
|
|
|
|
|
|
for(yb = lru_youngest; yb; yb = yb->older)
|
2012-09-18 13:17:49 +02:00
|
|
|
vsi->vsi_cached++;
|
2010-09-14 23:22:56 +02:00
|
|
|
}
|
|
|
|
|
2012-09-18 22:19:22 +02:00
|
|
|
void get_usage_info_kernel(struct vm_usage_info *vui)
|
|
|
|
{
|
|
|
|
memset(vui, 0, sizeof(*vui));
|
|
|
|
vui->vui_total = kernel_boot_info.kernel_allocated_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_usage_info_vm(struct vm_usage_info *vui)
|
|
|
|
{
|
|
|
|
memset(vui, 0, sizeof(*vui));
|
|
|
|
vui->vui_total = kernel_boot_info.vm_allocated_bytes +
|
|
|
|
get_vm_self_pages() * VM_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
2010-01-19 22:00:20 +01:00
|
|
|
/*========================================================================*
|
|
|
|
* get_usage_info *
|
|
|
|
*========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void get_usage_info(struct vmproc *vmp, struct vm_usage_info *vui)
|
2010-01-19 22:00:20 +01:00
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
|
|
|
struct phys_region *ph;
|
2010-10-04 13:41:10 +02:00
|
|
|
region_iter v_iter;
|
|
|
|
region_start_iter_least(&vmp->vm_regions_avl, &v_iter);
|
2012-12-17 19:26:52 +01:00
|
|
|
vir_bytes voffset;
|
2010-01-19 22:00:20 +01:00
|
|
|
|
|
|
|
memset(vui, 0, sizeof(*vui));
|
|
|
|
|
2012-09-18 22:19:22 +02:00
|
|
|
if(vmp->vm_endpoint == VM_PROC_NR) {
|
|
|
|
get_usage_info_vm(vui);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(vmp->vm_endpoint < 0) {
|
|
|
|
get_usage_info_kernel(vui);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-04 13:41:10 +02:00
|
|
|
while((vr = region_get_iter(&v_iter))) {
|
2012-12-17 19:26:52 +01:00
|
|
|
for(voffset = 0; voffset < vr->length; voffset += VM_PAGE_SIZE) {
|
|
|
|
if(!(ph = physblock_get(vr, voffset))) continue;
|
2010-01-19 22:00:20 +01:00
|
|
|
/* All present pages are counted towards the total. */
|
2012-09-18 13:17:49 +02:00
|
|
|
vui->vui_total += VM_PAGE_SIZE;
|
2010-01-19 22:00:20 +01:00
|
|
|
|
|
|
|
if (ph->ph->refcount > 1) {
|
|
|
|
/* Any page with a refcount > 1 is common. */
|
2012-09-18 13:17:49 +02:00
|
|
|
vui->vui_common += VM_PAGE_SIZE;
|
2010-01-19 22:00:20 +01:00
|
|
|
|
|
|
|
/* Any common, non-COW page is shared. */
|
2012-10-25 16:38:38 +02:00
|
|
|
if (vr->flags & VR_SHARED)
|
2012-09-18 13:17:49 +02:00
|
|
|
vui->vui_shared += VM_PAGE_SIZE;
|
2010-01-19 22:00:20 +01:00
|
|
|
}
|
|
|
|
}
|
2010-10-04 13:41:10 +02:00
|
|
|
region_incr_iter(&v_iter);
|
2010-01-19 22:00:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* get_region_info *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int get_region_info(struct vmproc *vmp, struct vm_region_info *vri,
|
2010-01-19 22:00:20 +01:00
|
|
|
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++) {
|
2012-12-17 19:26:52 +01:00
|
|
|
struct phys_region *ph1 = NULL, *ph2 = NULL;
|
|
|
|
vir_bytes voffset;
|
2011-11-26 16:12:17 +01:00
|
|
|
|
2013-01-09 20:45:37 +01:00
|
|
|
/* where to start on next iteration, regardless of what we find now */
|
|
|
|
next = vr->vaddr + vr->length;
|
|
|
|
|
2011-11-26 16:12:17 +01:00
|
|
|
/* Report part of the region that's actually in use. */
|
|
|
|
|
|
|
|
/* Get first and last phys_regions, if any */
|
2013-01-09 20:45:37 +01:00
|
|
|
for(voffset = 0; voffset < vr->length; voffset += VM_PAGE_SIZE) {
|
2012-12-17 19:26:52 +01:00
|
|
|
struct phys_region *ph;
|
|
|
|
if(!(ph = physblock_get(vr, voffset))) continue;
|
|
|
|
if(!ph1) ph1 = ph;
|
|
|
|
ph2 = ph;
|
|
|
|
}
|
2011-11-26 16:12:17 +01:00
|
|
|
if(!ph1 || !ph2) { assert(!ph1 && !ph2); continue; }
|
|
|
|
|
|
|
|
/* Report start+length of region starting from lowest use. */
|
No more intel/minix segments.
This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.
There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.
No static pre-allocated memory sizes exist any more.
Changes to booting:
. The pre_init.c leaves the kernel and modules exactly as
they were left by the bootloader in physical memory
. The kernel starts running using physical addressing,
loaded at a fixed location given in its linker script by the
bootloader. All code and data in this phase are linked to
this fixed low location.
. It makes a bootstrap pagetable to map itself to a
fixed high location (also in linker script) and jumps to
the high address. All code and data then use this high addressing.
. All code/data symbols linked at the low addresses is prefixed by
an objcopy step with __k_unpaged_*, so that that code cannot
reference highly-linked symbols (which aren't valid yet) or vice
versa (symbols that aren't valid any more).
. The two addressing modes are separated in the linker script by
collecting the unpaged_*.o objects and linking them with low
addresses, and linking the rest high. Some objects are linked
twice, once low and once high.
. The bootstrap phase passes a lot of information (e.g. free memory
list, physical location of the modules, etc.) using the kinfo
struct.
. After this bootstrap the low-linked part is freed.
. The kernel maps in VM into the bootstrap page table so that VM can
begin executing. Its first job is to make page tables for all other
boot processes. So VM runs before RS, and RS gets a fully dynamic,
VM-managed address space. VM gets its privilege info from RS as usual
but that happens after RS starts running.
. Both the kernel loading VM and VM organizing boot processes happen
using the libexec logic. This removes the last reason for VM to
still know much about exec() and vm/exec.c is gone.
Further Implementation:
. All segments are based at 0 and have a 4 GB limit.
. The kernel is mapped in at the top of the virtual address
space so as not to constrain the user processes.
. Processes do not use segments from the LDT at all; there are
no segments in the LDT any more, so no LLDT is needed.
. The Minix segments T/D/S are gone and so none of the
user-space or in-kernel copy functions use them. The copy
functions use a process endpoint of NONE to realize it's
a physical address, virtual otherwise.
. The umap call only makes sense to translate a virtual address
to a physical address now.
. Segments-related calls like newmap and alloc_segments are gone.
. All segments-related translation in VM is gone (vir2map etc).
. Initialization in VM is simpler as no moving around is necessary.
. VM and all other boot processes can be linked wherever they wish
and will be mapped in at the right location by the kernel and VM
respectively.
Other changes:
. The multiboot code is less special: it does not use mb_print
for its diagnostics any more but uses printf() as normal, saving
the output into the diagnostics buffer, only printing to the
screen using the direct print functions if a panic() occurs.
. The multiboot code uses the flexible 'free memory map list'
style to receive the list of free memory if available.
. The kernel determines the memory layout of the processes to
a degree: it tells VM where the kernel starts and ends and
where the kernel wants the top of the process to be. VM then
uses this entire range, i.e. the stack is right at the top,
and mmap()ped bits of memory are placed below that downwards,
and the break grows upwards.
Other Consequences:
. Every process gets its own page table as address spaces
can't be separated any more by segments.
. As all segments are 0-based, there is no distinction between
virtual and linear addresses, nor between userspace and
kernel addresses.
. Less work is done when context switching, leading to a net
performance increase. (8% faster on my machine for 'make servers'.)
. The layout and configuration of the GDT makes sysenter and syscall
possible.
2012-05-07 16:03:35 +02:00
|
|
|
vri->vri_addr = vr->vaddr + ph1->offset;
|
|
|
|
vri->vri_prot = 0;
|
2012-09-18 13:17:49 +02:00
|
|
|
vri->vri_length = ph2->offset + VM_PAGE_SIZE - ph1->offset;
|
2010-01-19 22:00:20 +01:00
|
|
|
|
|
|
|
/* "AND" the provided protection with per-page protection. */
|
|
|
|
if (!(vr->flags & VR_WRITABLE))
|
|
|
|
vri->vri_prot &= ~PROT_WRITE;
|
|
|
|
|
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 *
|
|
|
|
*========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void printregionstats(struct vmproc *vmp)
|
2009-09-21 16:49:49 +02:00
|
|
|
{
|
|
|
|
struct vir_region *vr;
|
|
|
|
struct phys_region *pr;
|
|
|
|
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))) {
|
2012-12-17 19:26:52 +01:00
|
|
|
vir_bytes voffset;
|
2010-10-04 13:41:10 +02:00
|
|
|
region_incr_iter(&v_iter);
|
2009-09-21 16:49:49 +02:00
|
|
|
if(vr->flags & VR_DIRECT)
|
|
|
|
continue;
|
2012-12-17 19:26:52 +01:00
|
|
|
for(voffset = 0; voffset < vr->length; voffset+=VM_PAGE_SIZE) {
|
|
|
|
if(!(pr = physblock_get(vr, voffset))) continue;
|
2012-09-18 13:17:49 +02:00
|
|
|
used += VM_PAGE_SIZE;
|
|
|
|
weighted += VM_PAGE_SIZE / pr->ph->refcount;
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-05 13:35:04 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* get_clean_phys_region *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static struct phys_region *
|
2012-09-18 13:17:49 +02:00
|
|
|
get_clean_phys_region(struct vmproc *vmp, vir_bytes vaddr, struct vir_region **ret_region)
|
2010-05-05 13:35:04 +02:00
|
|
|
{
|
|
|
|
struct vir_region *region;
|
2012-09-18 13:17:52 +02:00
|
|
|
vir_bytes mapaddr;
|
2010-05-05 13:35:04 +02:00
|
|
|
struct phys_region *ph;
|
|
|
|
|
No more intel/minix segments.
This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.
There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.
No static pre-allocated memory sizes exist any more.
Changes to booting:
. The pre_init.c leaves the kernel and modules exactly as
they were left by the bootloader in physical memory
. The kernel starts running using physical addressing,
loaded at a fixed location given in its linker script by the
bootloader. All code and data in this phase are linked to
this fixed low location.
. It makes a bootstrap pagetable to map itself to a
fixed high location (also in linker script) and jumps to
the high address. All code and data then use this high addressing.
. All code/data symbols linked at the low addresses is prefixed by
an objcopy step with __k_unpaged_*, so that that code cannot
reference highly-linked symbols (which aren't valid yet) or vice
versa (symbols that aren't valid any more).
. The two addressing modes are separated in the linker script by
collecting the unpaged_*.o objects and linking them with low
addresses, and linking the rest high. Some objects are linked
twice, once low and once high.
. The bootstrap phase passes a lot of information (e.g. free memory
list, physical location of the modules, etc.) using the kinfo
struct.
. After this bootstrap the low-linked part is freed.
. The kernel maps in VM into the bootstrap page table so that VM can
begin executing. Its first job is to make page tables for all other
boot processes. So VM runs before RS, and RS gets a fully dynamic,
VM-managed address space. VM gets its privilege info from RS as usual
but that happens after RS starts running.
. Both the kernel loading VM and VM organizing boot processes happen
using the libexec logic. This removes the last reason for VM to
still know much about exec() and vm/exec.c is gone.
Further Implementation:
. All segments are based at 0 and have a 4 GB limit.
. The kernel is mapped in at the top of the virtual address
space so as not to constrain the user processes.
. Processes do not use segments from the LDT at all; there are
no segments in the LDT any more, so no LLDT is needed.
. The Minix segments T/D/S are gone and so none of the
user-space or in-kernel copy functions use them. The copy
functions use a process endpoint of NONE to realize it's
a physical address, virtual otherwise.
. The umap call only makes sense to translate a virtual address
to a physical address now.
. Segments-related calls like newmap and alloc_segments are gone.
. All segments-related translation in VM is gone (vir2map etc).
. Initialization in VM is simpler as no moving around is necessary.
. VM and all other boot processes can be linked wherever they wish
and will be mapped in at the right location by the kernel and VM
respectively.
Other changes:
. The multiboot code is less special: it does not use mb_print
for its diagnostics any more but uses printf() as normal, saving
the output into the diagnostics buffer, only printing to the
screen using the direct print functions if a panic() occurs.
. The multiboot code uses the flexible 'free memory map list'
style to receive the list of free memory if available.
. The kernel determines the memory layout of the processes to
a degree: it tells VM where the kernel starts and ends and
where the kernel wants the top of the process to be. VM then
uses this entire range, i.e. the stack is right at the top,
and mmap()ped bits of memory are placed below that downwards,
and the break grows upwards.
Other Consequences:
. Every process gets its own page table as address spaces
can't be separated any more by segments.
. As all segments are 0-based, there is no distinction between
virtual and linear addresses, nor between userspace and
kernel addresses.
. Less work is done when context switching, leading to a net
performance increase. (8% faster on my machine for 'make servers'.)
. The layout and configuration of the GDT makes sysenter and syscall
possible.
2012-05-07 16:03:35 +02:00
|
|
|
mapaddr = vaddr;
|
2010-05-05 13:35:04 +02:00
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
if(!(region = map_lookup(vmp, mapaddr, &ph)) || !ph) {
|
2010-05-05 13:35:04 +02:00
|
|
|
printf("VM: get_clean_phys_region: 0x%lx not found\n", vaddr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(mapaddr >= region->vaddr);
|
|
|
|
assert(mapaddr < region->vaddr + region->length);
|
|
|
|
|
|
|
|
/* 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,
|
2012-12-17 19:26:52 +01:00
|
|
|
ph))) {
|
2010-05-05 13:35:04 +02:00
|
|
|
printf("VM: get_clean_phys_region: ph copy failed\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ph->ph->refcount == 1);
|
|
|
|
|
|
|
|
*ret_region = region;
|
|
|
|
|
|
|
|
return ph;
|
|
|
|
}
|
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
static int getblock(struct vmproc *vmp, u64_t id, vir_bytes vaddr, int pages)
|
2010-05-05 13:35:04 +02:00
|
|
|
{
|
|
|
|
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;
|
2012-09-18 13:17:52 +02:00
|
|
|
phys_bytes phaddr;
|
|
|
|
int p;
|
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;
|
|
|
|
}
|
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
if(yb->pages != pages) {
|
|
|
|
printf("VM: getblock: length mismatch (%d != %d)\n",
|
|
|
|
pages, yb->pages);
|
|
|
|
return EFAULT;
|
2010-05-05 13:35:04 +02:00
|
|
|
}
|
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
phaddr = yb->physaddr;
|
2010-05-05 13:35:04 +02:00
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
for(p = 0; p < pages; p++) {
|
|
|
|
/* Get the intended phys region, make sure refcount is 1. */
|
|
|
|
if(!(ph = get_clean_phys_region(vmp, vaddr, ®ion))) {
|
|
|
|
printf("VM: getblock: not found for %d\n", vmp->vm_endpoint);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2010-05-05 13:35:04 +02:00
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
assert(ph->ph->refcount == 1);
|
|
|
|
|
|
|
|
/* Free the block that is currently there. */
|
|
|
|
free_mem(ABS2CLICK(ph->ph->phys), 1);
|
2010-05-05 13:35:04 +02:00
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
/* Set the phys block to new addr and update pagetable. */
|
|
|
|
USE(ph->ph, ph->ph->phys = phaddr;);
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
vaddr += VM_PAGE_SIZE;
|
|
|
|
phaddr += VM_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
/* Forget about the yielded block and free the struct. */
|
|
|
|
freeyieldednode(yb, 0);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static int yieldblock(struct vmproc *vmp, u64_t id,
|
2012-09-18 13:17:52 +02:00
|
|
|
vir_bytes vaddr, yielded_t **retyb, int pages)
|
2010-05-05 13:35:04 +02:00
|
|
|
{
|
|
|
|
yielded_t *newyb;
|
2012-09-18 13:17:52 +02:00
|
|
|
vir_bytes mem_clicks, v, p, new_phaddr;
|
2010-05-05 13:35:04 +02:00
|
|
|
struct vir_region *region;
|
2012-09-18 13:17:52 +02:00
|
|
|
struct phys_region *ph = NULL, *prev_ph = NULL, *first_ph = NULL;
|
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;
|
|
|
|
}
|
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
if((vaddr % VM_PAGE_SIZE) || pages < 1) return EFAULT;
|
2012-09-18 13:17:49 +02:00
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
v = vaddr;
|
|
|
|
for(p = 0; p < pages; p++) {
|
|
|
|
if(!(region = map_lookup(vmp, v, &ph)) || !ph) {
|
|
|
|
printf("VM: do_yield_block: not found for %d\n",
|
|
|
|
vmp->vm_endpoint);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
if(!(region->flags & VR_ANON)) {
|
|
|
|
printf("VM: yieldblock: non-anon 0x%lx\n", v);
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
if(ph->ph->refcount != 1) {
|
|
|
|
printf("VM: do_yield_block: mapped not once for %d\n",
|
|
|
|
vmp->vm_endpoint);
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
if(prev_ph) {
|
|
|
|
if(ph->ph->phys != prev_ph->ph->phys + VM_PAGE_SIZE) {
|
|
|
|
printf("VM: physically discontiguous yield\n");
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prev_ph = ph;
|
|
|
|
if(!first_ph) first_ph = ph;
|
|
|
|
v += VM_PAGE_SIZE;
|
2010-05-05 13:35:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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));
|
2010-04-12 13:25:24 +02:00
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
if((mem_clicks = alloc_mem(pages, PAF_CLEAR)) == NO_MEM) {
|
2010-05-05 13:35:04 +02:00
|
|
|
SLABFREE(newyb);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update yielded block info. */
|
|
|
|
USE(newyb,
|
2010-10-15 11:10:14 +02:00
|
|
|
newyb->id = blockid;
|
2012-09-18 13:17:52 +02:00
|
|
|
newyb->physaddr = first_ph->ph->phys;
|
|
|
|
newyb->pages = pages;
|
2010-05-05 13:35:04 +02:00
|
|
|
newyb->younger = NULL;);
|
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
new_phaddr = CLICK2ABS(mem_clicks);
|
|
|
|
|
2010-05-05 13:35:04 +02:00
|
|
|
/* Set new phys block to new addr and update pagetable. */
|
2012-09-18 13:17:52 +02:00
|
|
|
v = vaddr;
|
|
|
|
for(p = 0; p < pages; p++) {
|
|
|
|
region = map_lookup(vmp, v, &ph);
|
|
|
|
assert(region && ph);
|
|
|
|
assert(ph->ph->refcount == 1);
|
|
|
|
USE(ph->ph,
|
|
|
|
ph->ph->phys = new_phaddr;);
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
v += VM_PAGE_SIZE;
|
|
|
|
new_phaddr += VM_PAGE_SIZE;
|
2010-05-05 13:35:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int do_forgetblocks(message *m)
|
2010-05-05 13:35:04 +02:00
|
|
|
{
|
|
|
|
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];
|
|
|
|
|
|
|
|
free_yielded_proc(vmp);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_forgetblock *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int do_forgetblock(message *m)
|
2010-05-05 13:35:04 +02:00
|
|
|
{
|
|
|
|
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];
|
|
|
|
|
|
|
|
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 *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int do_yieldblockgetblock(message *m)
|
2010-05-05 13:35:04 +02:00
|
|
|
{
|
|
|
|
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;
|
2012-09-18 13:17:52 +02:00
|
|
|
int pages;
|
2010-05-05 13:35:04 +02:00
|
|
|
|
|
|
|
if(vm_isokendpt(caller, &n) != OK)
|
|
|
|
panic("do_yieldblockgetblock: message from strange source: %d",
|
|
|
|
m->m_source);
|
|
|
|
|
|
|
|
vmp = &vmproc[n];
|
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
pages = m->VMYBGB_LEN / VM_PAGE_SIZE;
|
|
|
|
|
|
|
|
if((m->VMYBGB_LEN % VM_PAGE_SIZE) || pages < 1) {
|
|
|
|
static int printed;
|
2012-09-18 13:17:49 +02:00
|
|
|
if(!printed) {
|
|
|
|
printed = 1;
|
2012-09-18 13:17:52 +02:00
|
|
|
printf("vm: non-page-aligned or short block length\n");
|
2012-09-18 13:17:49 +02:00
|
|
|
}
|
2012-09-18 13:17:52 +02:00
|
|
|
return EFAULT;
|
2010-05-05 13:35:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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. */
|
2012-09-18 13:17:52 +02:00
|
|
|
yieldblock(vmp, yieldid, (vir_bytes) m->VMYBGB_VADDR, &yb,
|
|
|
|
pages);
|
2010-05-05 13:35:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(cmp64(getid, VM_BLOCKID_NONE) != 0) {
|
|
|
|
/* A block was given to get. */
|
2012-09-18 13:17:52 +02:00
|
|
|
r = getblock(vmp, getid, (vir_bytes) m->VMYBGB_VADDR, pages);
|
2010-05-05 13:35:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2012-10-11 15:15:49 +02:00
|
|
|
|
|
|
|
int physregions(struct vir_region *vr)
|
|
|
|
{
|
|
|
|
int n = 0;
|
2012-12-17 19:26:52 +01:00
|
|
|
vir_bytes voffset;
|
|
|
|
for(voffset = 0; voffset < vr->length; voffset += VM_PAGE_SIZE) {
|
|
|
|
if(physblock_get(vr, voffset))
|
|
|
|
n++;
|
2012-10-11 15:15:49 +02:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|