/* This file is concerned with allocating and freeing arbitrary-size blocks of * physical memory. */ #define _SYSTEM 1 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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. */ static phys_bytes mem_low, mem_high; static void free_pages(phys_bytes addr, int pages); static phys_bytes alloc_pages(int pages, int flags, phys_bytes *ret); #if SANITYCHECKS #define PAGESPERGB (1024*1024*1024/VM_PAGE_SIZE) /* 1GB of memory */ #define MAXPAGES (2*PAGESPERGB) #define CHUNKS BITMAP_CHUNKS(MAXPAGES) static bitchunk_t pagemap[CHUNKS]; #endif #define page_isfree(i) GET_BIT(free_pages_bitmap, i) /*===========================================================================* * alloc_mem * *===========================================================================*/ 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; } else if(memflags & PAF_ALIGN16K) { align_clicks = (16 * 1024) / CLICK_SIZE; clicks += align_clicks; } mem = alloc_pages(clicks, memflags, NULL); if(mem == NO_MEM) { free_yielded(clicks * CLICK_SIZE); mem = alloc_pages(clicks, memflags, NULL); } 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 * *===========================================================================*/ 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 * *===========================================================================*/ 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; 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); total_pages += chunks[i].size; first = 0; } } } #if SANITYCHECKS void mem_sanitycheck(char *file, int line) { int i; for(i = 0; i < NUMBER_PHYSICAL_PAGES; i++) { if(!page_isfree(i)) continue; usedpages_add(i * VM_PAGE_SIZE, VM_PAGE_SIZE); } } #endif void memstats(int *nodes, int *pages, int *largest) { int i; *nodes = 0; *pages = 0; *largest = 0; 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 || (memflags & PAF_FIRSTBLOCK)) { /* 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 *len) { 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 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 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 || (memflags & PAF_FIRSTBLOCK))) { 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) { if(len) *len = 0; return NO_MEM; } /* remember for next time */ lastscan = mem; if(memflags & PAF_FIRSTBLOCK) { assert(len); /* block doesn't have to as big as requested; * return its size though. */ if(run_length < pages) { pages = run_length; } } if(len) *len = pages; 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 * *===========================================================================*/ 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); printf("%d blocks, %d pages (%lukB) free, largest %d pages (%lukB)\n", 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 < MAXPAGES); 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 /*===========================================================================* * alloc_mem_in_list * *===========================================================================*/ struct memlist *alloc_mem_in_list(phys_bytes bytes, u32_t flags) { phys_bytes rempages; struct memlist *head = NULL, *tail = NULL; assert(bytes > 0); assert(!(bytes % VM_PAGE_SIZE)); 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; 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); if(mem == NO_MEM) { printf("alloc_mem_in_list: giving up, %lukB missing\n", rempages * VM_PAGE_SIZE/1024); printmemstats(); free_mem_list(head, 1); return NULL; } assert(gotpages <= rempages); assert(gotpages > 0); if(!(SLABALLOC(ml))) { free_mem_list(head, 1); free_pages(mem, gotpages); return NULL; } USE(ml, ml->phys = CLICK2ABS(mem); ml->length = CLICK2ABS(gotpages); ml->next = NULL;); if(tail) { USE(tail, tail->next = ml;); } tail = ml; if(!head) head = ml; rempages -= gotpages; } while(rempages > 0); { struct memlist *ml; for(ml = head; ml; ml = ml->next) { assert(ml->phys); assert(ml->length); #if NONCONTIGUOUS if(!(flags & PAF_CONTIG)) { assert(ml->length == VM_PAGE_SIZE); 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)); assert(!(list->length % VM_PAGE_SIZE)); 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) { assert(list->length > 0); printf("0x%lx-0x%lx", list->phys, list->phys+list->length-1); printf(" "); list = list->next; } printf("\n"); }