minix/servers/vm/alloc.c

474 lines
12 KiB
C
Raw Normal View History

/* This file is concerned with allocating and freeing arbitrary-size blocks of
* physical memory.
*/
#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>
#include <minix/debug.h>
#include <minix/bitmap.h>
#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"
#include "sanitycheck.h"
#include "memlist.h"
/* Number of physical pages in a 32-bit address space */
#define NUMBER_PHYSICAL_PAGES (0x100000000ULL/VM_PAGE_SIZE)
#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;
/* Used for sanity check. */
2012-03-25 20:25:53 +02:00
static phys_bytes mem_low, mem_high;
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);
#if SANITYCHECKS
struct {
int used;
char *file;
int line;
} pagemap[NUMBER_PHYSICAL_PAGES];
#endif
#define page_isfree(i) GET_BIT(free_pages_bitmap, i)
/*===========================================================================*
* alloc_mem *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
phys_clicks alloc_mem(phys_clicks clicks, u32_t memflags)
{
/* 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.
*/
phys_clicks mem = NO_MEM, align_clicks = 0;
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;
}
mem = alloc_pages(clicks, memflags);
if(mem == NO_MEM) {
free_yielded(clicks * CLICK_SIZE);
mem = alloc_pages(clicks, memflags);
}
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;
free_mem(mem, e);
mem += e;
}
}
return mem;
}
/*===========================================================================*
* free_mem *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
void free_mem(phys_clicks base, phys_clicks clicks)
{
/* 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;
assert(CLICK_SIZE == VM_PAGE_SIZE);
free_pages(base, clicks);
return;
}
/*===========================================================================*
* mem_init *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
void mem_init(chunks)
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;
2010-01-19 22:00:20 +01:00
total_pages = 0;
memset(free_pages_bitmap, 0, sizeof(free_pages_bitmap));
/* 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;
free_mem(chunks[i].base, chunks[i].size);
2010-01-19 22:00:20 +01:00
total_pages += chunks[i].size;
first = 0;
}
}
}
#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)
{
int i;
for(i = 0; i < NUMBER_PHYSICAL_PAGES; i++) {
if(!page_isfree(i)) continue;
MYASSERT(usedpages_add(i * VM_PAGE_SIZE, VM_PAGE_SIZE) == OK);
}
}
#endif
2012-03-25 20:25:53 +02:00
void memstats(int *nodes, int *pages, int *largest)
{
int i;
*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
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;
(*nodes)++;
(*pages)+= size;
if(size > *largest)
*largest = size;
}
}
static int findbit(int low, int startscan, int pages, int memflags, int *len)
{
int run_length = 0, i, freerange_start;
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);
if(run_length == pages) {
/* good block found! */
*len = run_length;
return freerange_start;
}
}
return NO_MEM;
}
/*===========================================================================*
* alloc_pages *
*===========================================================================*/
static phys_bytes alloc_pages(int pages, int memflags)
{
phys_bytes boundary16 = 16 * 1024 * 1024 / VM_PAGE_SIZE;
phys_bytes boundary1 = 1 * 1024 * 1024 / VM_PAGE_SIZE;
phys_bytes mem = NO_MEM;
int maxpage = NUMBER_PHYSICAL_PAGES - 1, i;
static int lastscan = -1;
int startscan, run_length;
if(memflags & PAF_LOWER16MB)
maxpage = boundary16 - 1;
else if(memflags & PAF_LOWER1MB)
maxpage = boundary1 - 1;
else {
/* no position restrictions: check page cache */
if(pages == 1) {
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--;
}
}
}
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);
if(mem == NO_MEM)
return NO_MEM;
/* remember for next time */
lastscan = mem;
for(i = mem; i < mem + pages; i++) {
UNSET_BIT(free_pages_bitmap, i);
}
if(memflags & PAF_CLEAR) {
int s;
if ((s= sys_memset(NONE, 0, CLICK_SIZE*mem,
VM_PAGE_SIZE*pages)) != OK)
panic("alloc_mem: sys_memset failed: %d", s);
}
return mem;
}
/*===========================================================================*
* free_pages *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
static void free_pages(phys_bytes pageno, int npages)
{
int i, lim = pageno + npages - 1;
#if JUNKFREE
if(sys_memset(NONE, 0xa5a5a5a5, VM_PAGE_SIZE * pageno,
VM_PAGE_SIZE * npages) != OK)
panic("free_pages: sys_memset failed");
#endif
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;
}
}
}
/*===========================================================================*
* printmemstats *
*===========================================================================*/
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",
Build NetBSD libc library in world in ELF mode. 3 sets of libraries are built now: . ack: all libraries that ack can compile (/usr/lib/i386/) . clang+elf: all libraries with minix headers (/usr/lib/) . clang+elf: all libraries with netbsd headers (/usr/netbsd/) Once everything can be compiled with netbsd libraries and headers, the /usr/netbsd hierarchy will be obsolete and its libraries compiled with netbsd headers will be installed in /usr/lib, and its headers in /usr/include. (i.e. minix libc and current minix headers set will be gone.) To use the NetBSD libc system (libraries + headers) before it is the default libc, see: http://wiki.minix3.org/en/DevelopersGuide/UsingNetBSDCode This wiki page also documents the maintenance of the patch files of minix-specific changes to imported NetBSD code. Changes in this commit: . libsys: Add NBSD compilation and create a safe NBSD-based libc. . Port rest of libraries (except libddekit) to new header system. . Enable compilation of libddekit with new headers. . Enable kernel compilation with new headers. . Enable drivers compilation with new headers. . Port legacy commands to new headers and libc. . Port servers to new headers. . Add <sys/sigcontext.h> in compat library. . Remove dependency file in tree. . Enable compilation of common/lib/libc/atomic in libsys . Do not generate RCSID strings in libc. . Temporarily disable zoneinfo as they are incompatible with NetBSD format . obj-nbsd for .gitignore . Procfs: use only integer arithmetic. (Antoine Leca) . Increase ramdisk size to create NBSD-based images. . Remove INCSYMLINKS handling hack. . Add nbsd_include/sys/exec_elf.h . Enable ELF compilation with NBSD libc. . Add 'make nbsdsrc' in tools to download reference NetBSD sources. . Automate minix-port.patch creation. . Avoid using fstavfs() as it is *extremely* slow and unneeded. . Set err() as PRIVATE to avoid name clash with libc. . [NBSD] servers/vm: remove compilation warnings. . u32 is not a long in NBSD headers. . UPDATING info on netbsd hierarchy . commands fixes for netbsd libc
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));
}
#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;
assert(!(addr % VM_PAGE_SIZE));
assert(!(len % VM_PAGE_SIZE));
assert(len > 0);
pagestart = addr / VM_PAGE_SIZE;
pages = len / VM_PAGE_SIZE;
while(pages > 0) {
phys_bytes thisaddr;
assert(pagestart > 0);
assert(pagestart < NUMBER_PHYSICAL_PAGES);
thisaddr = pagestart * VM_PAGE_SIZE;
assert(pagestart >= 0);
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();
return EFAULT;
}
pagemap[pagestart].used = 1;
pagemap[pagestart].file = file;
pagemap[pagestart].line = line;
pages--;
pagestart++;
}
return OK;
}
#endif
/*===========================================================================*
* alloc_mem_in_list *
*===========================================================================*/
struct memlist *alloc_mem_in_list(phys_bytes bytes, u32_t flags, phys_bytes known)
{
phys_bytes rempages, phys_count;
struct memlist *head = NULL, *tail = NULL;
assert(bytes > 0);
assert(!(bytes % VM_PAGE_SIZE));
rempages = bytes / VM_PAGE_SIZE;
assert(!(flags & PAF_CONTIG));
if(known != MAP_NONE)
phys_count = known;
do {
struct memlist *ml;
phys_bytes mem;
vir_bytes freed = 0;
do {
if(known == MAP_NONE) {
mem = alloc_pages(1, flags);
if(mem == NO_MEM) {
freed = free_yielded(rempages * VM_PAGE_SIZE);
}
assert(mem != MAP_NONE);
} else {
mem = ABS2CLICK(phys_count);
phys_count += VM_PAGE_SIZE;
assert(mem != MAP_NONE);
assert(mem != NO_MEM);
}
assert(mem != MAP_NONE);
} while(mem == NO_MEM && freed > 0);
if(mem == NO_MEM) {
2010-07-05 15:58:57 +02:00
printf("alloc_mem_in_list: giving up, %lukB missing\n",
rempages * VM_PAGE_SIZE/1024);
printmemstats();
free_mem_list(head, 1);
return NULL;
}
if(!(SLABALLOC(ml))) {
free_mem_list(head, 1);
free_pages(mem, VM_PAGE_SIZE);
return NULL;
}
USE(ml, ml->phys = CLICK2ABS(mem); ml->next = NULL;);
if(tail) {
USE(tail,
tail->next = ml;);
}
tail = ml;
if(!head)
head = ml;
rempages--;
} while(rempages > 0);
{
struct memlist *ml;
for(ml = head; ml; ml = ml->next) {
assert(ml->phys);
#if NONCONTIGUOUS
if(!(flags & PAF_CONTIG)) {
if(ml->next)
assert(ml->phys + ml->length != ml->next->phys);
}
#endif
}
}
return head;
}
/*===========================================================================*
* free_mem_list *
*===========================================================================*/
void free_mem_list(struct memlist *list, int all)
{
while(list) {
struct memlist *next;
next = list->next;
assert(!(list->phys % VM_PAGE_SIZE));
if(all)
free_pages(list->phys / VM_PAGE_SIZE, 1);
SLABFREE(list);
list = next;
}
}
/*===========================================================================*
* print_mem_list *
*===========================================================================*/
void print_mem_list(struct memlist *list)
{
while(list) {
printf("0x%lx-0x%lx", list->phys, list->phys+VM_PAGE_SIZE-1);
printf(" ");
list = list->next;
}
printf("\n");
}