2008-11-19 13:26:10 +01:00
|
|
|
/* This file is concerned with allocating and freeing arbitrary-size blocks of
|
2012-09-18 13:17:46 +02:00
|
|
|
* physical memory.
|
2008-11-19 13:26:10 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define _SYSTEM 1
|
|
|
|
|
|
|
|
#include <minix/com.h>
|
|
|
|
#include <minix/callnr.h>
|
|
|
|
#include <minix/type.h>
|
|
|
|
#include <minix/config.h>
|
|
|
|
#include <minix/const.h>
|
|
|
|
#include <minix/sysutil.h>
|
|
|
|
#include <minix/syslib.h>
|
2009-09-21 16:49:49 +02:00
|
|
|
#include <minix/debug.h>
|
|
|
|
#include <minix/bitmap.h>
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <memory.h>
|
|
|
|
|
|
|
|
#include "vm.h"
|
|
|
|
#include "proto.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "glo.h"
|
2009-09-21 16:49:49 +02:00
|
|
|
#include "sanitycheck.h"
|
2010-04-12 13:25:24 +02:00
|
|
|
#include "memlist.h"
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2012-09-18 13:17:48 +02:00
|
|
|
/* Number of physical pages in a 32-bit address space */
|
2013-08-20 14:02:33 +02:00
|
|
|
#define NUMBER_PHYSICAL_PAGES (int)(0x100000000ULL/VM_PAGE_SIZE)
|
2012-09-18 13:17:48 +02:00
|
|
|
#define PAGE_BITMAP_CHUNKS BITMAP_CHUNKS(NUMBER_PHYSICAL_PAGES)
|
|
|
|
static bitchunk_t free_pages_bitmap[PAGE_BITMAP_CHUNKS];
|
|
|
|
#define PAGE_CACHE_MAX 10000
|
|
|
|
static int free_page_cache[PAGE_CACHE_MAX];
|
|
|
|
static int free_page_cache_size = 0;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* Used for sanity check. */
|
2012-03-25 20:25:53 +02:00
|
|
|
static phys_bytes mem_low, mem_high;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static void free_pages(phys_bytes addr, int pages);
|
2012-09-18 13:17:49 +02:00
|
|
|
static phys_bytes alloc_pages(int pages, int flags);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#if SANITYCHECKS
|
2012-09-18 13:17:49 +02:00
|
|
|
struct {
|
|
|
|
int used;
|
make vfs & filesystems use failable copying
Change the kernel to add features to vircopy and safecopies so that
transparent copy fixing won't happen to avoid deadlocks, and such copies
fail with EFAULT.
Transparently making copying work from filesystems (as normally done by
the kernel & VM when copying fails because of missing/readonly memory)
is problematic as it can happen that, for file-mapped ranges, that that
same filesystem that is blocked on the copy request is needed to satisfy
the memory range, leading to deadlock. Dito for VFS itself, if done with
a blocking call.
This change makes the copying done from a filesystem fail in such cases
with EFAULT by VFS adding the CPF_TRY flag to the grants. If a FS call
fails with EFAULT, VFS will then request the range to be made available
to VM after the FS is unblocked, allowing it to be used to satisfy the
range if need be in another VFS thread.
Similarly, for datacopies that VFS itself does, it uses the failable
vircopy variant and callers use a wrapper that talk to VM if necessary
to get the copy to work.
. kernel: add CPF_TRY flag to safecopies
. kernel: only request writable ranges to VM for the
target buffer when copying fails
. do copying in VFS TRY-first
. some fixes in VM to build SANITYCHECK mode
. add regression test for the cases where
- a FS system call needs memory mapped in a process that the
FS itself must map.
- such a range covers more than one file-mapped region.
. add 'try' mode to vircopy, physcopy
. add flags field to copy kernel call messages
. if CP_FLAG_TRY is set, do not transparently try
to fix memory ranges
. for use by VFS when accessing user buffers to avoid
deadlock
. remove some obsolete backwards compatability assignments
. VFS: let thread scheduling work for VM requests too
Allows VFS to make calls to VM while suspending and resuming
the currently running thread. Does currently not work for the
main thread.
. VM: add fix memory range call for use by VFS
Change-Id: I295794269cea51a3163519a9cfe5901301d90b32
2014-01-16 14:22:13 +01:00
|
|
|
const char *file;
|
2012-09-18 13:17:49 +02:00
|
|
|
int line;
|
|
|
|
} pagemap[NUMBER_PHYSICAL_PAGES];
|
2008-11-19 13:26:10 +01:00
|
|
|
#endif
|
|
|
|
|
2012-09-18 13:17:48 +02:00
|
|
|
#define page_isfree(i) GET_BIT(free_pages_bitmap, i)
|
|
|
|
|
2012-12-29 01:16:33 +01:00
|
|
|
#define RESERVEDMAGIC 0x6e4c74d5
|
2013-02-24 22:00:52 +01:00
|
|
|
#define MAXRESERVEDPAGES 300
|
2012-12-29 01:16:33 +01:00
|
|
|
#define MAXRESERVEDQUEUES 15
|
|
|
|
|
|
|
|
static struct reserved_pages {
|
|
|
|
struct reserved_pages *next; /* next in use */
|
|
|
|
int max_available; /* queue depth use, 0 if not in use at all */
|
|
|
|
int npages; /* number of consecutive pages */
|
|
|
|
int mappedin; /* must reserved pages also be mapped? */
|
|
|
|
int n_available; /* number of queue entries */
|
|
|
|
int allocflags; /* allocflags for alloc_mem */
|
|
|
|
struct reserved_pageslot {
|
|
|
|
phys_bytes phys;
|
|
|
|
void *vir;
|
|
|
|
} slots[MAXRESERVEDPAGES];
|
|
|
|
u32_t magic;
|
|
|
|
} reservedqueues[MAXRESERVEDQUEUES], *first_reserved_inuse = NULL;
|
|
|
|
|
|
|
|
int missing_spares = 0;
|
|
|
|
|
|
|
|
static void sanitycheck_queues(void)
|
|
|
|
{
|
|
|
|
struct reserved_pages *mrq;
|
|
|
|
int m = 0;
|
|
|
|
|
2013-08-20 14:02:33 +02:00
|
|
|
for(mrq = first_reserved_inuse; mrq; mrq = mrq->next) {
|
2012-12-29 01:16:33 +01:00
|
|
|
assert(mrq->max_available > 0);
|
|
|
|
assert(mrq->max_available >= mrq->n_available);
|
|
|
|
m += mrq->max_available - mrq->n_available;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(m == missing_spares);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sanitycheck_rq(struct reserved_pages *rq)
|
|
|
|
{
|
|
|
|
assert(rq->magic == RESERVEDMAGIC);
|
|
|
|
assert(rq->n_available >= 0);
|
|
|
|
assert(rq->n_available <= MAXRESERVEDPAGES);
|
|
|
|
assert(rq->n_available <= rq->max_available);
|
|
|
|
|
|
|
|
sanitycheck_queues();
|
|
|
|
}
|
|
|
|
|
|
|
|
void *reservedqueue_new(int max_available, int npages, int mapped, int allocflags)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
struct reserved_pages *rq;
|
|
|
|
|
|
|
|
assert(max_available > 0);
|
|
|
|
assert(max_available < MAXRESERVEDPAGES);
|
|
|
|
assert(npages > 0);
|
|
|
|
assert(npages < 10);
|
|
|
|
|
|
|
|
for(r = 0; r < MAXRESERVEDQUEUES; r++)
|
|
|
|
if(!reservedqueues[r].max_available)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(r >= MAXRESERVEDQUEUES) {
|
|
|
|
printf("VM: %d reserved queues in use\n", MAXRESERVEDQUEUES);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rq = &reservedqueues[r];
|
|
|
|
|
|
|
|
memset(rq, 0, sizeof(*rq));
|
|
|
|
rq->next = first_reserved_inuse;
|
|
|
|
first_reserved_inuse = rq;
|
|
|
|
|
|
|
|
rq->max_available = max_available;
|
|
|
|
rq->npages = npages;
|
|
|
|
rq->mappedin = mapped;
|
|
|
|
rq->allocflags = allocflags;
|
|
|
|
rq->magic = RESERVEDMAGIC;
|
|
|
|
|
|
|
|
missing_spares += max_available;
|
|
|
|
|
|
|
|
return rq;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
reservedqueue_fillslot(struct reserved_pages *rq,
|
|
|
|
struct reserved_pageslot *rps, phys_bytes ph, void *vir)
|
|
|
|
{
|
|
|
|
rps->phys = ph;
|
|
|
|
rps->vir = vir;
|
|
|
|
assert(missing_spares > 0);
|
|
|
|
if(rq->mappedin) assert(vir);
|
|
|
|
missing_spares--;
|
|
|
|
rq->n_available++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
reservedqueue_addslot(struct reserved_pages *rq)
|
|
|
|
{
|
|
|
|
phys_bytes cl, cl_addr;
|
|
|
|
void *vir;
|
|
|
|
struct reserved_pageslot *rps;
|
|
|
|
|
|
|
|
sanitycheck_rq(rq);
|
|
|
|
|
|
|
|
if((cl = alloc_mem(rq->npages, rq->allocflags)) == NO_MEM)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
cl_addr = CLICK2ABS(cl);
|
|
|
|
|
|
|
|
vir = NULL;
|
|
|
|
|
|
|
|
if(rq->mappedin) {
|
|
|
|
if(!(vir = vm_mappages(cl_addr, rq->npages))) {
|
|
|
|
free_mem(cl, rq->npages);
|
|
|
|
printf("reservedqueue_addslot: vm_mappages failed\n");
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rps = &rq->slots[rq->n_available];
|
|
|
|
|
|
|
|
reservedqueue_fillslot(rq, rps, cl_addr, vir);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reservedqueue_add(void *rq_v, void *vir, phys_bytes ph)
|
|
|
|
{
|
|
|
|
struct reserved_pages *rq = rq_v;
|
|
|
|
struct reserved_pageslot *rps;
|
|
|
|
|
|
|
|
sanitycheck_rq(rq);
|
|
|
|
|
|
|
|
rps = &rq->slots[rq->n_available];
|
|
|
|
|
|
|
|
reservedqueue_fillslot(rq, rps, ph, vir);
|
|
|
|
}
|
|
|
|
|
2013-08-20 14:02:33 +02:00
|
|
|
static int reservedqueue_fill(void *rq_v)
|
2012-12-29 01:16:33 +01:00
|
|
|
{
|
|
|
|
struct reserved_pages *rq = rq_v;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
sanitycheck_rq(rq);
|
|
|
|
|
|
|
|
while(rq->n_available < rq->max_available)
|
|
|
|
if((r=reservedqueue_addslot(rq)) != OK)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
reservedqueue_alloc(void *rq_v, phys_bytes *ph, void **vir)
|
|
|
|
{
|
|
|
|
struct reserved_pages *rq = rq_v;
|
|
|
|
struct reserved_pageslot *rps;
|
|
|
|
|
|
|
|
sanitycheck_rq(rq);
|
|
|
|
|
|
|
|
if(rq->n_available < 1) return ENOMEM;
|
|
|
|
|
|
|
|
rq->n_available--;
|
|
|
|
missing_spares++;
|
|
|
|
rps = &rq->slots[rq->n_available];
|
|
|
|
|
|
|
|
*ph = rps->phys;
|
|
|
|
*vir = rps->vir;
|
|
|
|
|
|
|
|
sanitycheck_rq(rq);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void alloc_cycle(void)
|
|
|
|
{
|
|
|
|
struct reserved_pages *rq;
|
|
|
|
sanitycheck_queues();
|
|
|
|
for(rq = first_reserved_inuse; rq && missing_spares > 0; rq = rq->next) {
|
|
|
|
sanitycheck_rq(rq);
|
|
|
|
reservedqueue_fill(rq);
|
|
|
|
sanitycheck_rq(rq);
|
|
|
|
}
|
|
|
|
sanitycheck_queues();
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*===========================================================================*
|
2010-04-12 13:25:24 +02:00
|
|
|
* alloc_mem *
|
2008-11-19 13:26:10 +01:00
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
phys_clicks alloc_mem(phys_clicks clicks, u32_t memflags)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
|
|
|
/* Allocate a block of memory from the free list using first fit. The block
|
|
|
|
* consists of a sequence of contiguous bytes, whose length in clicks is
|
|
|
|
* given by 'clicks'. A pointer to the block is returned. The block is
|
|
|
|
* always on a click boundary. This procedure is called when memory is
|
|
|
|
* needed for FORK or EXEC.
|
|
|
|
*/
|
2011-06-01 11:30:58 +02:00
|
|
|
phys_clicks mem = NO_MEM, align_clicks = 0;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-02-12 13:26:08 +01:00
|
|
|
if(memflags & PAF_ALIGN64K) {
|
|
|
|
align_clicks = (64 * 1024) / CLICK_SIZE;
|
|
|
|
clicks += align_clicks;
|
2012-08-16 23:33:27 +02:00
|
|
|
} else if(memflags & PAF_ALIGN16K) {
|
|
|
|
align_clicks = (16 * 1024) / CLICK_SIZE;
|
|
|
|
clicks += align_clicks;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2013-03-20 20:18:52 +01:00
|
|
|
do {
|
|
|
|
mem = alloc_pages(clicks, memflags);
|
|
|
|
} while(mem == NO_MEM && cache_freepages(clicks) > 0);
|
2009-02-12 13:26:08 +01:00
|
|
|
|
|
|
|
if(mem == NO_MEM)
|
|
|
|
return mem;
|
|
|
|
|
|
|
|
if(align_clicks) {
|
|
|
|
phys_clicks o;
|
|
|
|
o = mem % align_clicks;
|
|
|
|
if(o > 0) {
|
|
|
|
phys_clicks e;
|
|
|
|
e = align_clicks - o;
|
2010-04-12 13:25:24 +02:00
|
|
|
free_mem(mem, e);
|
2009-02-12 13:26:08 +01:00
|
|
|
mem += e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mem;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 19:11:42 +01:00
|
|
|
void mem_add_total_pages(int pages)
|
|
|
|
{
|
|
|
|
total_pages += pages;
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*===========================================================================*
|
2010-04-12 13:25:24 +02:00
|
|
|
* free_mem *
|
2008-11-19 13:26:10 +01:00
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void free_mem(phys_clicks base, phys_clicks clicks)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
|
|
|
/* Return a block of free memory to the hole list. The parameters tell where
|
|
|
|
* the block starts in physical memory and how big it is. The block is added
|
|
|
|
* to the hole list. If it is contiguous with an existing hole on either end,
|
|
|
|
* it is merged with the hole or holes.
|
|
|
|
*/
|
|
|
|
if (clicks == 0) return;
|
|
|
|
|
2010-06-05 16:39:40 +02:00
|
|
|
assert(CLICK_SIZE == VM_PAGE_SIZE);
|
|
|
|
free_pages(base, clicks);
|
|
|
|
return;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mem_init *
|
|
|
|
*===========================================================================*/
|
2013-08-20 14:02:33 +02:00
|
|
|
void mem_init(struct memory *chunks)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
|
|
|
/* Initialize hole lists. There are two lists: 'hole_head' points to a linked
|
|
|
|
* list of all the holes (unused memory) in the system; 'free_slots' points to
|
|
|
|
* a linked list of table entries that are not in use. Initially, the former
|
|
|
|
* list has one entry for each chunk of physical memory, and the second
|
|
|
|
* list links together the remaining table slots. As memory becomes more
|
|
|
|
* fragmented in the course of time (i.e., the initial big holes break up into
|
|
|
|
* smaller holes), new table slots are needed to represent them. These slots
|
|
|
|
* are taken from the list headed by 'free_slots'.
|
|
|
|
*/
|
|
|
|
int i, first = 0;
|
|
|
|
|
2010-01-19 22:00:20 +01:00
|
|
|
total_pages = 0;
|
|
|
|
|
2012-09-18 13:17:48 +02:00
|
|
|
memset(free_pages_bitmap, 0, sizeof(free_pages_bitmap));
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/* Use the chunks of physical memory to allocate holes. */
|
|
|
|
for (i=NR_MEMS-1; i>=0; i--) {
|
|
|
|
if (chunks[i].size > 0) {
|
|
|
|
phys_bytes from = CLICK2ABS(chunks[i].base),
|
|
|
|
to = CLICK2ABS(chunks[i].base+chunks[i].size)-1;
|
|
|
|
if(first || from < mem_low) mem_low = from;
|
|
|
|
if(first || to > mem_high) mem_high = to;
|
2010-04-12 13:25:24 +02:00
|
|
|
free_mem(chunks[i].base, chunks[i].size);
|
2010-01-19 22:00:20 +01:00
|
|
|
total_pages += chunks[i].size;
|
2008-11-19 13:26:10 +01:00
|
|
|
first = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
#if SANITYCHECKS
|
make vfs & filesystems use failable copying
Change the kernel to add features to vircopy and safecopies so that
transparent copy fixing won't happen to avoid deadlocks, and such copies
fail with EFAULT.
Transparently making copying work from filesystems (as normally done by
the kernel & VM when copying fails because of missing/readonly memory)
is problematic as it can happen that, for file-mapped ranges, that that
same filesystem that is blocked on the copy request is needed to satisfy
the memory range, leading to deadlock. Dito for VFS itself, if done with
a blocking call.
This change makes the copying done from a filesystem fail in such cases
with EFAULT by VFS adding the CPF_TRY flag to the grants. If a FS call
fails with EFAULT, VFS will then request the range to be made available
to VM after the FS is unblocked, allowing it to be used to satisfy the
range if need be in another VFS thread.
Similarly, for datacopies that VFS itself does, it uses the failable
vircopy variant and callers use a wrapper that talk to VM if necessary
to get the copy to work.
. kernel: add CPF_TRY flag to safecopies
. kernel: only request writable ranges to VM for the
target buffer when copying fails
. do copying in VFS TRY-first
. some fixes in VM to build SANITYCHECK mode
. add regression test for the cases where
- a FS system call needs memory mapped in a process that the
FS itself must map.
- such a range covers more than one file-mapped region.
. add 'try' mode to vircopy, physcopy
. add flags field to copy kernel call messages
. if CP_FLAG_TRY is set, do not transparently try
to fix memory ranges
. for use by VFS when accessing user buffers to avoid
deadlock
. remove some obsolete backwards compatability assignments
. VFS: let thread scheduling work for VM requests too
Allows VFS to make calls to VM while suspending and resuming
the currently running thread. Does currently not work for the
main thread.
. VM: add fix memory range call for use by VFS
Change-Id: I295794269cea51a3163519a9cfe5901301d90b32
2014-01-16 14:22:13 +01:00
|
|
|
void mem_sanitycheck(const char *file, int line)
|
2009-09-21 16:49:49 +02:00
|
|
|
{
|
2012-09-18 13:17:48 +02:00
|
|
|
int i;
|
|
|
|
for(i = 0; i < NUMBER_PHYSICAL_PAGES; i++) {
|
|
|
|
if(!page_isfree(i)) continue;
|
2012-09-18 13:17:49 +02:00
|
|
|
MYASSERT(usedpages_add(i * VM_PAGE_SIZE, VM_PAGE_SIZE) == OK);
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
void memstats(int *nodes, int *pages, int *largest)
|
2009-09-21 16:49:49 +02:00
|
|
|
{
|
2012-09-18 13:17:48 +02:00
|
|
|
int i;
|
2009-09-21 16:49:49 +02:00
|
|
|
*nodes = 0;
|
|
|
|
*pages = 0;
|
|
|
|
*largest = 0;
|
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
|
|
|
|
2012-09-18 13:17:48 +02:00
|
|
|
for(i = 0; i < NUMBER_PHYSICAL_PAGES; i++) {
|
|
|
|
int size = 0;
|
|
|
|
while(i < NUMBER_PHYSICAL_PAGES && page_isfree(i)) {
|
|
|
|
size++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if(size == 0) continue;
|
2009-09-21 16:49:49 +02:00
|
|
|
(*nodes)++;
|
2012-09-18 13:17:48 +02:00
|
|
|
(*pages)+= size;
|
|
|
|
if(size > *largest)
|
|
|
|
*largest = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int findbit(int low, int startscan, int pages, int memflags, int *len)
|
|
|
|
{
|
2012-12-11 18:42:21 +01:00
|
|
|
int run_length = 0, i;
|
|
|
|
int freerange_start = startscan;
|
2012-09-18 13:17:48 +02:00
|
|
|
|
|
|
|
for(i = startscan; i >= low; i--) {
|
|
|
|
if(!page_isfree(i)) {
|
|
|
|
int pi;
|
|
|
|
int chunk = i/BITCHUNK_BITS, moved = 0;
|
|
|
|
run_length = 0;
|
|
|
|
pi = i;
|
|
|
|
while(chunk > 0 &&
|
|
|
|
!MAP_CHUNK(free_pages_bitmap, chunk*BITCHUNK_BITS)) {
|
|
|
|
chunk--;
|
|
|
|
moved = 1;
|
|
|
|
}
|
|
|
|
if(moved) { i = chunk * BITCHUNK_BITS + BITCHUNK_BITS; }
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(!run_length) { freerange_start = i; run_length = 1; }
|
|
|
|
else { freerange_start--; run_length++; }
|
|
|
|
assert(run_length <= pages);
|
2012-09-18 13:17:49 +02:00
|
|
|
if(run_length == pages) {
|
2012-09-18 13:17:48 +02:00
|
|
|
/* good block found! */
|
|
|
|
*len = run_length;
|
|
|
|
return freerange_start;
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
2012-09-18 13:17:48 +02:00
|
|
|
|
|
|
|
return NO_MEM;
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*===========================================================================*
|
|
|
|
* alloc_pages *
|
|
|
|
*===========================================================================*/
|
2012-09-18 13:17:49 +02:00
|
|
|
static phys_bytes alloc_pages(int pages, int memflags)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
2009-09-21 16:49:49 +02:00
|
|
|
phys_bytes boundary16 = 16 * 1024 * 1024 / VM_PAGE_SIZE;
|
|
|
|
phys_bytes boundary1 = 1 * 1024 * 1024 / VM_PAGE_SIZE;
|
2013-08-20 14:02:33 +02:00
|
|
|
phys_bytes mem = NO_MEM, i; /* page number */
|
|
|
|
int maxpage = NUMBER_PHYSICAL_PAGES - 1;
|
2012-09-18 13:17:48 +02:00
|
|
|
static int lastscan = -1;
|
|
|
|
int startscan, run_length;
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2012-09-18 13:17:48 +02:00
|
|
|
if(memflags & PAF_LOWER16MB)
|
|
|
|
maxpage = boundary16 - 1;
|
|
|
|
else if(memflags & PAF_LOWER1MB)
|
|
|
|
maxpage = boundary1 - 1;
|
|
|
|
else {
|
|
|
|
/* no position restrictions: check page cache */
|
2012-09-18 13:17:49 +02:00
|
|
|
if(pages == 1) {
|
2012-09-18 13:17:48 +02:00
|
|
|
while(free_page_cache_size > 0) {
|
|
|
|
i = free_page_cache[free_page_cache_size-1];
|
|
|
|
if(page_isfree(i)) {
|
|
|
|
free_page_cache_size--;
|
|
|
|
mem = i;
|
|
|
|
assert(mem != NO_MEM);
|
|
|
|
run_length = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free_page_cache_size--;
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2012-09-18 13:17:48 +02:00
|
|
|
if(lastscan < maxpage && lastscan >= 0)
|
|
|
|
startscan = lastscan;
|
|
|
|
else startscan = maxpage;
|
|
|
|
|
|
|
|
if(mem == NO_MEM)
|
|
|
|
mem = findbit(0, startscan, pages, memflags, &run_length);
|
|
|
|
if(mem == NO_MEM)
|
|
|
|
mem = findbit(0, maxpage, pages, memflags, &run_length);
|
2012-09-18 13:17:49 +02:00
|
|
|
if(mem == NO_MEM)
|
2012-09-18 13:17:48 +02:00
|
|
|
return NO_MEM;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2012-09-18 13:17:48 +02:00
|
|
|
/* remember for next time */
|
|
|
|
lastscan = mem;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2012-09-18 13:17:48 +02:00
|
|
|
for(i = mem; i < mem + pages; i++) {
|
|
|
|
UNSET_BIT(free_pages_bitmap, i);
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
if(memflags & PAF_CLEAR) {
|
|
|
|
int s;
|
2012-06-06 19:05:28 +02:00
|
|
|
if ((s= sys_memset(NONE, 0, CLICK_SIZE*mem,
|
2009-09-21 16:49:49 +02:00
|
|
|
VM_PAGE_SIZE*pages)) != OK)
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("alloc_mem: sys_memset failed: %d", s);
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
return mem;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* free_pages *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static void free_pages(phys_bytes pageno, int npages)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
2012-09-18 13:17:48 +02:00
|
|
|
int i, lim = pageno + npages - 1;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-06-08 02:59:48 +02:00
|
|
|
#if JUNKFREE
|
2012-06-06 19:05:28 +02:00
|
|
|
if(sys_memset(NONE, 0xa5a5a5a5, VM_PAGE_SIZE * pageno,
|
2010-06-08 02:59:48 +02:00
|
|
|
VM_PAGE_SIZE * npages) != OK)
|
|
|
|
panic("free_pages: sys_memset failed");
|
|
|
|
#endif
|
|
|
|
|
2012-09-18 13:17:48 +02:00
|
|
|
for(i = pageno; i <= lim; i++) {
|
|
|
|
SET_BIT(free_pages_bitmap, i);
|
|
|
|
if(free_page_cache_size < PAGE_CACHE_MAX) {
|
|
|
|
free_page_cache[free_page_cache_size++] = i;
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2010-01-05 20:39:27 +01:00
|
|
|
* printmemstats *
|
2009-09-21 16:49:49 +02:00
|
|
|
*===========================================================================*/
|
|
|
|
void printmemstats(void)
|
|
|
|
{
|
|
|
|
int nodes, pages, largest;
|
|
|
|
memstats(&nodes, &pages, &largest);
|
2010-07-05 15:58:57 +02:00
|
|
|
printf("%d blocks, %d pages (%lukB) free, largest %d pages (%lukB)\n",
|
2011-04-27 15:00:52 +02:00
|
|
|
nodes, pages, (unsigned long) pages * (VM_PAGE_SIZE/1024),
|
|
|
|
largest, (unsigned long) largest * (VM_PAGE_SIZE/1024));
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if SANITYCHECKS
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* usedpages_reset *
|
|
|
|
*===========================================================================*/
|
|
|
|
void usedpages_reset(void)
|
|
|
|
{
|
|
|
|
memset(pagemap, 0, sizeof(pagemap));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* usedpages_add *
|
|
|
|
*===========================================================================*/
|
make vfs & filesystems use failable copying
Change the kernel to add features to vircopy and safecopies so that
transparent copy fixing won't happen to avoid deadlocks, and such copies
fail with EFAULT.
Transparently making copying work from filesystems (as normally done by
the kernel & VM when copying fails because of missing/readonly memory)
is problematic as it can happen that, for file-mapped ranges, that that
same filesystem that is blocked on the copy request is needed to satisfy
the memory range, leading to deadlock. Dito for VFS itself, if done with
a blocking call.
This change makes the copying done from a filesystem fail in such cases
with EFAULT by VFS adding the CPF_TRY flag to the grants. If a FS call
fails with EFAULT, VFS will then request the range to be made available
to VM after the FS is unblocked, allowing it to be used to satisfy the
range if need be in another VFS thread.
Similarly, for datacopies that VFS itself does, it uses the failable
vircopy variant and callers use a wrapper that talk to VM if necessary
to get the copy to work.
. kernel: add CPF_TRY flag to safecopies
. kernel: only request writable ranges to VM for the
target buffer when copying fails
. do copying in VFS TRY-first
. some fixes in VM to build SANITYCHECK mode
. add regression test for the cases where
- a FS system call needs memory mapped in a process that the
FS itself must map.
- such a range covers more than one file-mapped region.
. add 'try' mode to vircopy, physcopy
. add flags field to copy kernel call messages
. if CP_FLAG_TRY is set, do not transparently try
to fix memory ranges
. for use by VFS when accessing user buffers to avoid
deadlock
. remove some obsolete backwards compatability assignments
. VFS: let thread scheduling work for VM requests too
Allows VFS to make calls to VM while suspending and resuming
the currently running thread. Does currently not work for the
main thread.
. VM: add fix memory range call for use by VFS
Change-Id: I295794269cea51a3163519a9cfe5901301d90b32
2014-01-16 14:22:13 +01:00
|
|
|
int usedpages_add_f(phys_bytes addr, phys_bytes len, const char *file, int line)
|
2009-09-21 16:49:49 +02:00
|
|
|
{
|
|
|
|
u32_t pagestart, pages;
|
|
|
|
|
|
|
|
if(!incheck)
|
|
|
|
return OK;
|
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!(addr % VM_PAGE_SIZE));
|
|
|
|
assert(!(len % VM_PAGE_SIZE));
|
|
|
|
assert(len > 0);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
pagestart = addr / VM_PAGE_SIZE;
|
|
|
|
pages = len / VM_PAGE_SIZE;
|
|
|
|
|
|
|
|
while(pages > 0) {
|
|
|
|
phys_bytes thisaddr;
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(pagestart > 0);
|
2012-09-18 13:17:49 +02:00
|
|
|
assert(pagestart < NUMBER_PHYSICAL_PAGES);
|
2009-09-21 16:49:49 +02:00
|
|
|
thisaddr = pagestart * VM_PAGE_SIZE;
|
2012-09-18 13:17:49 +02:00
|
|
|
assert(pagestart < NUMBER_PHYSICAL_PAGES);
|
|
|
|
if(pagemap[pagestart].used) {
|
|
|
|
static int warnings = 0;
|
|
|
|
if(warnings++ < 100)
|
|
|
|
printf("%s:%d: usedpages_add: addr 0x%lx reused, first %s:%d\n",
|
|
|
|
file, line, thisaddr, pagemap[pagestart].file, pagemap[pagestart].line);
|
|
|
|
util_stacktrace();
|
2009-09-21 16:49:49 +02:00
|
|
|
return EFAULT;
|
|
|
|
}
|
2012-09-18 13:17:49 +02:00
|
|
|
pagemap[pagestart].used = 1;
|
|
|
|
pagemap[pagestart].file = file;
|
|
|
|
pagemap[pagestart].line = line;
|
2009-09-21 16:49:49 +02:00
|
|
|
pages--;
|
|
|
|
pagestart++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2010-04-12 13:25:24 +02:00
|
|
|
|