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 "pagerange.h"
|
|
|
|
#include "addravl.h"
|
|
|
|
#include "sanitycheck.h"
|
2010-04-12 13:25:24 +02:00
|
|
|
#include "memlist.h"
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
/* AVL tree of free pages. */
|
|
|
|
addr_avl addravl;
|
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);
|
|
|
|
static phys_bytes alloc_pages(int pages, int flags, phys_bytes *ret);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#if SANITYCHECKS
|
2010-04-12 13:25:24 +02:00
|
|
|
#define PAGESPERGB (1024*1024*1024/VM_PAGE_SIZE) /* 1GB of memory */
|
|
|
|
#define MAXPAGES (2*PAGESPERGB)
|
2009-09-21 16:49:49 +02:00
|
|
|
#define CHUNKS BITMAP_CHUNKS(MAXPAGES)
|
2012-03-25 20:25:53 +02:00
|
|
|
static bitchunk_t pagemap[CHUNKS];
|
2008-11-19 13:26:10 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*===========================================================================*
|
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
|
|
|
}
|
|
|
|
|
2010-06-05 16:39:40 +02:00
|
|
|
mem = alloc_pages(clicks, memflags, NULL);
|
|
|
|
if(mem == NO_MEM) {
|
|
|
|
free_yielded(clicks * CLICK_SIZE);
|
|
|
|
mem = alloc_pages(clicks, memflags, NULL);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
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 *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void mem_init(chunks)
|
2008-11-19 13:26:10 +01:00
|
|
|
struct memory *chunks; /* list of free memory chunks */
|
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
addr_init(&addravl);
|
|
|
|
|
2010-01-19 22:00:20 +01:00
|
|
|
total_pages = 0;
|
|
|
|
|
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
|
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
|
|
|
void mem_sanitycheck(char *file, int line)
|
2009-09-21 16:49:49 +02:00
|
|
|
{
|
|
|
|
pagerange_t *p, *prevp = NULL;
|
|
|
|
addr_iter iter;
|
|
|
|
addr_start_iter_least(&addravl, &iter);
|
|
|
|
while((p=addr_get_iter(&iter))) {
|
|
|
|
SLABSANE(p);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(p->size > 0);
|
2009-09-21 16:49:49 +02:00
|
|
|
if(prevp) {
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(prevp->addr < p->addr);
|
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(prevp->addr + prevp->size < p->addr);
|
2009-09-21 16:49:49 +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
|
|
|
usedpages_add(p->addr * VM_PAGE_SIZE, p->size * VM_PAGE_SIZE);
|
|
|
|
prevp = p;
|
2009-09-21 16:49:49 +02:00
|
|
|
addr_incr_iter(&iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#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
|
|
|
{
|
2011-06-01 11:30:58 +02:00
|
|
|
pagerange_t *p;
|
2009-09-21 16:49:49 +02:00
|
|
|
addr_iter iter;
|
|
|
|
addr_start_iter_least(&addravl, &iter);
|
|
|
|
*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
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
while((p=addr_get_iter(&iter))) {
|
|
|
|
SLABSANE(p);
|
|
|
|
(*nodes)++;
|
|
|
|
(*pages)+= p->size;
|
|
|
|
if(p->size > *largest)
|
|
|
|
*largest = p->size;
|
|
|
|
addr_incr_iter(&iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*===========================================================================*
|
|
|
|
* alloc_pages *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static phys_bytes alloc_pages(int pages, int memflags, phys_bytes *len)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
2009-09-21 16:49:49 +02:00
|
|
|
addr_iter iter;
|
|
|
|
pagerange_t *pr;
|
|
|
|
int incr;
|
|
|
|
phys_bytes boundary16 = 16 * 1024 * 1024 / VM_PAGE_SIZE;
|
|
|
|
phys_bytes boundary1 = 1 * 1024 * 1024 / VM_PAGE_SIZE;
|
|
|
|
phys_bytes mem;
|
2008-11-19 13:26:10 +01:00
|
|
|
#if SANITYCHECKS
|
2009-09-21 16:49:49 +02:00
|
|
|
int firstnodes, firstpages, wantnodes, wantpages;
|
|
|
|
int finalnodes, finalpages;
|
|
|
|
int largest;
|
|
|
|
|
2010-07-19 20:20:14 +02:00
|
|
|
#if NONCONTIGUOUS
|
|
|
|
/* If NONCONTIGUOUS is on, allocate physical pages single
|
|
|
|
* pages at a time, accomplished by returning single pages
|
|
|
|
* if the caller can handle that (indicated by PAF_FIRSTBLOCK).
|
|
|
|
*/
|
|
|
|
if(memflags & PAF_FIRSTBLOCK) {
|
|
|
|
assert(!(memflags & PAF_CONTIG));
|
|
|
|
pages = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
memstats(&firstnodes, &firstpages, &largest);
|
|
|
|
wantnodes = firstnodes;
|
|
|
|
wantpages = firstpages - pages;
|
2008-11-19 13:26:10 +01:00
|
|
|
#endif
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
if(memflags & (PAF_LOWER16MB|PAF_LOWER1MB)) {
|
|
|
|
addr_start_iter_least(&addravl, &iter);
|
|
|
|
incr = 1;
|
|
|
|
} else {
|
|
|
|
addr_start_iter_greatest(&addravl, &iter);
|
|
|
|
incr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while((pr = addr_get_iter(&iter))) {
|
|
|
|
SLABSANE(pr);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(pr->size > 0);
|
2010-04-12 13:25:24 +02:00
|
|
|
if(pr->size >= pages || (memflags & PAF_FIRSTBLOCK)) {
|
2009-09-21 16:49:49 +02:00
|
|
|
if(memflags & PAF_LOWER16MB) {
|
|
|
|
if(pr->addr + pages > boundary16)
|
|
|
|
return NO_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(memflags & PAF_LOWER1MB) {
|
|
|
|
if(pr->addr + pages > boundary1)
|
|
|
|
return NO_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* good block found! */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(incr)
|
|
|
|
addr_incr_iter(&iter);
|
|
|
|
else
|
|
|
|
addr_decr_iter(&iter);
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
if(!pr) {
|
2010-04-12 13:25:24 +02:00
|
|
|
if(len)
|
|
|
|
*len = 0;
|
2008-11-19 13:26:10 +01:00
|
|
|
#if SANITYCHECKS
|
2010-05-05 13:35:04 +02:00
|
|
|
assert(largest < pages);
|
2008-11-19 13:26:10 +01:00
|
|
|
#endif
|
2010-05-05 13:35:04 +02:00
|
|
|
return NO_MEM;
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
SLABSANE(pr);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
if(memflags & PAF_FIRSTBLOCK) {
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(len);
|
2010-04-12 13:25:24 +02:00
|
|
|
/* block doesn't have to as big as requested;
|
|
|
|
* return its size though.
|
|
|
|
*/
|
|
|
|
if(pr->size < pages) {
|
|
|
|
pages = pr->size;
|
|
|
|
#if SANITYCHECKS
|
|
|
|
wantpages = firstpages - pages;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(len)
|
|
|
|
*len = pages;
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
/* Allocated chunk is off the end. */
|
|
|
|
mem = pr->addr + pr->size - pages;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(pr->size >= pages);
|
2009-09-21 16:49:49 +02:00
|
|
|
if(pr->size == pages) {
|
|
|
|
pagerange_t *prr;
|
|
|
|
prr = addr_remove(&addravl, pr->addr);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(prr);
|
|
|
|
assert(prr == pr);
|
2009-09-21 16:49:49 +02:00
|
|
|
SLABFREE(pr);
|
|
|
|
#if SANITYCHECKS
|
|
|
|
wantnodes--;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
USE(pr, pr->size -= pages;);
|
|
|
|
}
|
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
|
|
|
#if SANITYCHECKS
|
|
|
|
memstats(&finalnodes, &finalpages, &largest);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(finalnodes == wantnodes);
|
|
|
|
assert(finalpages == wantpages);
|
2009-09-21 16:49:49 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2009-09-21 16:49:49 +02:00
|
|
|
pagerange_t *pr, *p;
|
|
|
|
addr_iter iter;
|
2008-11-19 13:26:10 +01:00
|
|
|
#if SANITYCHECKS
|
2009-09-21 16:49:49 +02:00
|
|
|
int firstnodes, firstpages, wantnodes, wantpages;
|
|
|
|
int finalnodes, finalpages, largest;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
memstats(&firstnodes, &firstpages, &largest);
|
|
|
|
|
|
|
|
wantnodes = firstnodes;
|
|
|
|
wantpages = firstpages + npages;
|
2008-11-19 13:26:10 +01:00
|
|
|
#endif
|
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!addr_search(&addravl, pageno, AVL_EQUAL));
|
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
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
/* try to merge with higher neighbour */
|
|
|
|
if((pr=addr_search(&addravl, pageno+npages, AVL_EQUAL))) {
|
|
|
|
USE(pr, pr->addr -= npages;
|
|
|
|
pr->size += npages;);
|
|
|
|
} else {
|
|
|
|
if(!SLABALLOC(pr))
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("alloc_pages: can't alloc");
|
2009-09-21 16:49:49 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
memstats(&firstnodes, &firstpages, &largest);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
wantnodes = firstnodes;
|
|
|
|
wantpages = firstpages + npages;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
#endif
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(npages > 0);
|
2009-09-21 16:49:49 +02:00
|
|
|
USE(pr, pr->addr = pageno;
|
|
|
|
pr->size = npages;);
|
|
|
|
addr_insert(&addravl, pr);
|
|
|
|
#if SANITYCHECKS
|
|
|
|
wantnodes++;
|
|
|
|
#endif
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
addr_start_iter(&addravl, &iter, pr->addr, AVL_EQUAL);
|
|
|
|
p = addr_get_iter(&iter);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(p);
|
|
|
|
assert(p == pr);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
addr_decr_iter(&iter);
|
|
|
|
if((p = addr_get_iter(&iter))) {
|
|
|
|
SLABSANE(p);
|
|
|
|
if(p->addr + p->size == pr->addr) {
|
|
|
|
USE(p, p->size += pr->size;);
|
|
|
|
addr_remove(&addravl, pr->addr);
|
|
|
|
SLABFREE(pr);
|
2008-11-19 13:26:10 +01:00
|
|
|
#if SANITYCHECKS
|
2009-09-21 16:49:49 +02:00
|
|
|
wantnodes--;
|
2008-11-19 13:26:10 +01:00
|
|
|
#endif
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
memstats(&finalnodes, &finalpages, &largest);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(finalnodes == wantnodes);
|
|
|
|
assert(finalpages == wantpages);
|
2009-09-21 16:49:49 +02:00
|
|
|
#endif
|
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 *
|
|
|
|
*===========================================================================*/
|
|
|
|
int usedpages_add_f(phys_bytes addr, phys_bytes len, char *file, int line)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
assert(pagestart < MAXPAGES);
|
2009-09-21 16:49:49 +02:00
|
|
|
thisaddr = pagestart * VM_PAGE_SIZE;
|
|
|
|
if(GET_BIT(pagemap, pagestart)) {
|
|
|
|
printf("%s:%d: usedpages_add: addr 0x%lx reused.\n",
|
|
|
|
file, line, thisaddr);
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
SET_BIT(pagemap, pagestart);
|
|
|
|
pages--;
|
|
|
|
pagestart++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2010-04-12 13:25:24 +02:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* alloc_mem_in_list *
|
|
|
|
*===========================================================================*/
|
|
|
|
struct memlist *alloc_mem_in_list(phys_bytes bytes, u32_t flags)
|
|
|
|
{
|
|
|
|
phys_bytes rempages;
|
2010-07-09 14:22:33 +02:00
|
|
|
struct memlist *head = NULL, *tail = NULL;
|
2010-04-12 13:25:24 +02:00
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(bytes > 0);
|
|
|
|
assert(!(bytes % VM_PAGE_SIZE));
|
2010-04-12 13:25:24 +02:00
|
|
|
|
|
|
|
rempages = bytes / VM_PAGE_SIZE;
|
|
|
|
|
|
|
|
/* unless we are told to allocate all memory
|
|
|
|
* contiguously, tell alloc function to grab whatever
|
|
|
|
* block it can find.
|
|
|
|
*/
|
|
|
|
if(!(flags & PAF_CONTIG))
|
|
|
|
flags |= PAF_FIRSTBLOCK;
|
|
|
|
|
|
|
|
do {
|
|
|
|
struct memlist *ml;
|
|
|
|
phys_bytes mem, gotpages;
|
2010-05-05 13:35:04 +02:00
|
|
|
vir_bytes freed = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
mem = alloc_pages(rempages, flags, &gotpages);
|
|
|
|
|
|
|
|
if(mem == NO_MEM) {
|
|
|
|
freed = free_yielded(rempages * VM_PAGE_SIZE);
|
|
|
|
}
|
|
|
|
} while(mem == NO_MEM && freed > 0);
|
2010-04-12 13:25:24 +02:00
|
|
|
|
|
|
|
if(mem == NO_MEM) {
|
2010-07-05 15:58:57 +02:00
|
|
|
printf("alloc_mem_in_list: giving up, %lukB missing\n",
|
2010-05-05 13:35:04 +02:00
|
|
|
rempages * VM_PAGE_SIZE/1024);
|
|
|
|
printmemstats();
|
2010-04-12 13:25:24 +02:00
|
|
|
free_mem_list(head, 1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(gotpages <= rempages);
|
|
|
|
assert(gotpages > 0);
|
2010-04-12 13:25:24 +02:00
|
|
|
|
|
|
|
if(!(SLABALLOC(ml))) {
|
|
|
|
free_mem_list(head, 1);
|
|
|
|
free_pages(mem, gotpages);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
USE(ml,
|
|
|
|
ml->phys = CLICK2ABS(mem);
|
|
|
|
ml->length = CLICK2ABS(gotpages);
|
2010-07-09 14:22:33 +02:00
|
|
|
ml->next = NULL;);
|
2010-07-19 20:20:14 +02:00
|
|
|
if(tail) {
|
|
|
|
USE(tail,
|
|
|
|
tail->next = ml;);
|
|
|
|
}
|
2010-07-09 14:22:33 +02:00
|
|
|
tail = ml;
|
|
|
|
if(!head)
|
|
|
|
head = ml;
|
2010-04-12 13:25:24 +02:00
|
|
|
rempages -= gotpages;
|
|
|
|
} while(rempages > 0);
|
|
|
|
|
2010-07-09 14:22:33 +02:00
|
|
|
{
|
|
|
|
struct memlist *ml;
|
2010-04-12 13:25:24 +02:00
|
|
|
for(ml = head; ml; ml = ml->next) {
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(ml->phys);
|
|
|
|
assert(ml->length);
|
2010-07-09 14:22:33 +02:00
|
|
|
#if NONCONTIGUOUS
|
|
|
|
if(!(flags & PAF_CONTIG)) {
|
|
|
|
assert(ml->length == VM_PAGE_SIZE);
|
|
|
|
if(ml->next)
|
|
|
|
assert(ml->phys + ml->length != ml->next->phys);
|
|
|
|
}
|
|
|
|
#endif
|
2010-04-12 13:25:24 +02:00
|
|
|
}
|
2010-07-09 14:22:33 +02:00
|
|
|
}
|
2010-04-12 13:25:24 +02:00
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* free_mem_list *
|
|
|
|
*===========================================================================*/
|
|
|
|
void free_mem_list(struct memlist *list, int all)
|
|
|
|
{
|
|
|
|
while(list) {
|
|
|
|
struct memlist *next;
|
|
|
|
next = list->next;
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!(list->phys % VM_PAGE_SIZE));
|
|
|
|
assert(!(list->length % VM_PAGE_SIZE));
|
2010-04-12 13:25:24 +02:00
|
|
|
if(all)
|
|
|
|
free_pages(list->phys / VM_PAGE_SIZE,
|
|
|
|
list->length / VM_PAGE_SIZE);
|
|
|
|
SLABFREE(list);
|
|
|
|
list = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* print_mem_list *
|
|
|
|
*===========================================================================*/
|
|
|
|
void print_mem_list(struct memlist *list)
|
|
|
|
{
|
|
|
|
while(list) {
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(list->length > 0);
|
2010-04-12 13:25:24 +02:00
|
|
|
printf("0x%lx-0x%lx", list->phys, list->phys+list->length-1);
|
|
|
|
printf(" ");
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|