kern

annotate src/mem.c @ 20:369adbbd4bdd

added a few comments in mem.c
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 30 Mar 2011 23:14:29 +0300
parents 8be069e6bb05
children 3ba93d8f586c
rev   line source
nuclear@17 1 #include <stdio.h>
nuclear@19 2 #include <string.h>
nuclear@17 3 #include "mem.h"
nuclear@17 4 #include "panic.h"
nuclear@17 5 #include "vm.h"
nuclear@17 6
nuclear@19 7 #define FREE 0
nuclear@19 8 #define USED 1
nuclear@19 9
nuclear@19 10 #define BM_IDX(pg) ((pg) / 32)
nuclear@19 11 #define BM_BIT(pg) ((pg) & 0x1f)
nuclear@19 12
nuclear@19 13 #define IS_FREE(pg) ((bitmap[BM_IDX(pg)] & (1 << BM_BIT(pg))) == 0)
nuclear@19 14
nuclear@19 15 static void mark_page(int pg, int free);
nuclear@19 16 static void add_memory(uint32_t start, size_t size);
nuclear@19 17
nuclear@17 18 /* end of kernel image */
nuclear@17 19 extern int _end;
nuclear@17 20
nuclear@20 21 /* A bitmap is used to track which physical memory pages are used or available
nuclear@20 22 * for allocation by alloc_phys_page.
nuclear@20 23 *
nuclear@20 24 * last_alloc_idx keeps track of the last 32bit element in the bitmap array
nuclear@20 25 * where a free page was found. It's guaranteed that all the elements before
nuclear@20 26 * this have no free pages, but it doesn't imply that there will be another
nuclear@20 27 * free page there. So it's used as a starting point for the search.
nuclear@20 28 */
nuclear@19 29 static uint32_t *bitmap;
nuclear@19 30 static int bmsize, last_alloc_idx;
nuclear@17 31
nuclear@20 32
nuclear@17 33 void init_mem(struct mboot_info *mb)
nuclear@17 34 {
nuclear@19 35 int i, num_pages, max_pg = 0;
nuclear@19 36 uint32_t used_end;
nuclear@19 37
nuclear@19 38 num_pages = 0;
nuclear@19 39 last_alloc_idx = 0;
nuclear@19 40
nuclear@20 41 /* the allocation bitmap starts right at the end of the ELF image */
nuclear@19 42 bitmap = (uint32_t*)&_end;
nuclear@19 43
nuclear@20 44 /* start by marking all posible pages (2**20) as used. We do not "reserve"
nuclear@20 45 * all this space. Pages beyond the end of the useful bitmap area
nuclear@20 46 * ((char*)bitmap + bmsize), which will be determined after we traverse the
nuclear@20 47 * memory map, are going to be marked as available for allocation.
nuclear@20 48 */
nuclear@19 49 memset(bitmap, 0xff, 1024 * 1024 / 8);
nuclear@19 50
nuclear@20 51 /* if the bootloader gave us an available memory map, traverse it and mark
nuclear@20 52 * all the corresponding pages as free.
nuclear@20 53 */
nuclear@19 54 if(mb->flags & MB_MMAP) {
nuclear@19 55 struct mboot_mmap *mem, *mmap_end;
nuclear@19 56
nuclear@19 57 mem = mb->mmap;
nuclear@19 58 mmap_end = (struct mboot_mmap*)((char*)mb->mmap + mb->mmap_len);
nuclear@19 59
nuclear@19 60 printf("memory map:\n");
nuclear@19 61 while(mem < mmap_end) {
nuclear@19 62 char *type;
nuclear@19 63 unsigned int end = mem->base_low + mem->length_low;
nuclear@19 64
nuclear@19 65 if(mem->type == MB_MEM_VALID) {
nuclear@19 66 type = "free:";
nuclear@19 67 add_memory(mem->base_low, mem->length_low);
nuclear@19 68
nuclear@19 69 num_pages = ADDR_TO_PAGE(mem->base_low + mem->length_low);
nuclear@19 70 if(max_pg < num_pages) {
nuclear@19 71 max_pg = num_pages;
nuclear@19 72 }
nuclear@19 73 } else {
nuclear@19 74 type = "hole:";
nuclear@19 75 }
nuclear@19 76
nuclear@19 77 printf(" %s %x - %x (%u bytes)\n", type, mem->base_low, end, mem->length_low);
nuclear@19 78 mem = (struct mboot_mmap*)((char*)mem + mem->skip + sizeof mem->skip);
nuclear@19 79 }
nuclear@19 80 } else if(mb->flags & MB_MEM) {
nuclear@20 81 /* if we don't have a detailed memory map, just use the lower and upper
nuclear@20 82 * memory block sizes to determine which pages should be available.
nuclear@20 83 */
nuclear@19 84 add_memory(0, mb->mem_lower);
nuclear@19 85 add_memory(0x100000, mb->mem_upper * 1024);
nuclear@19 86 max_pg = mb->mem_upper / 4;
nuclear@19 87
nuclear@19 88 printf("lower memory: %ukb, upper mem: %ukb\n", mb->mem_lower, mb->mem_upper);
nuclear@19 89 } else {
nuclear@20 90 /* I don't think this should ever happen with a multiboot-compliant boot loader */
nuclear@19 91 panic("didn't get any memory info from the boot loader, I give up\n");
nuclear@19 92 }
nuclear@19 93
nuclear@20 94 bmsize = max_pg / 8; /* size of the useful bitmap in bytes */
nuclear@19 95
nuclear@19 96 /* mark all the used pages as ... well ... used */
nuclear@19 97 used_end = ((uint32_t)bitmap + bmsize - 1);
nuclear@19 98
nuclear@19 99 printf("marking pages up to %x ", used_end);
nuclear@19 100 used_end = ADDR_TO_PAGE(used_end);
nuclear@19 101 printf("(page: %d) inclusive as used\n", used_end);
nuclear@19 102
nuclear@19 103 for(i=0; i<=used_end; i++) {
nuclear@19 104 mark_page(i, USED);
nuclear@19 105 }
nuclear@17 106 }
nuclear@17 107
nuclear@20 108 /* alloc_phys_page finds the first available page of physical memory,
nuclear@20 109 * marks it as used in the bitmap, and returns its address. If there's
nuclear@20 110 * no unused physical page, 0 is returned.
nuclear@20 111 */
nuclear@17 112 uint32_t alloc_phys_page(void)
nuclear@17 113 {
nuclear@19 114 int i, idx, max;
nuclear@17 115
nuclear@19 116 idx = last_alloc_idx;
nuclear@19 117 max = bmsize / 4;
nuclear@19 118
nuclear@19 119 while(idx <= max) {
nuclear@19 120 /* if at least one bit is 0 then we have at least
nuclear@19 121 * one free page. find it and allocate it.
nuclear@19 122 */
nuclear@19 123 if(bitmap[idx] != 0xffffffff) {
nuclear@19 124 for(i=0; i<32; i++) {
nuclear@19 125 int pg = idx * 32 + i;
nuclear@19 126
nuclear@19 127 if(IS_FREE(pg)) {
nuclear@19 128 mark_page(pg, USED);
nuclear@19 129
nuclear@19 130 last_alloc_idx = idx;
nuclear@19 131
nuclear@19 132 printf("alloc_phys_page() -> %x (page: %d)\n", PAGE_TO_ADDR(pg), pg);
nuclear@19 133 return PAGE_TO_ADDR(pg);
nuclear@19 134 }
nuclear@19 135 }
nuclear@19 136 panic("can't happen: alloc_phys_page (mem.c)\n");
nuclear@19 137 }
nuclear@19 138 idx++;
nuclear@17 139 }
nuclear@17 140
nuclear@19 141 return 0;
nuclear@19 142 }
nuclear@19 143
nuclear@20 144 /* free_phys_page marks the physical page which corresponds to the specified
nuclear@20 145 * address as free in the allocation bitmap.
nuclear@20 146 *
nuclear@20 147 * CAUTION: no checks are done that this page should actually be freed or not.
nuclear@20 148 * If you call free_phys_page with the address of some part of memory that was
nuclear@20 149 * originally reserved due to it being in a memory hole or part of the kernel
nuclear@20 150 * image or whatever, it will be subsequently allocatable by alloc_phys_page.
nuclear@20 151 */
nuclear@19 152 void free_phys_page(uint32_t addr)
nuclear@19 153 {
nuclear@19 154 int pg = ADDR_TO_PAGE(addr);
nuclear@19 155 int bmidx = BM_IDX(pg);
nuclear@19 156
nuclear@19 157 if(!IS_FREE(pg)) {
nuclear@19 158 panic("free_phys_page(%d): I thought that was already free!\n", pg);
nuclear@17 159 }
nuclear@17 160
nuclear@19 161 mark_page(pg, FREE);
nuclear@19 162 if(bmidx < last_alloc_idx) {
nuclear@19 163 last_alloc_idx = bmidx;
nuclear@19 164 }
nuclear@19 165 }
nuclear@17 166
nuclear@20 167 /* this is only ever used by the VM init code to find out what the extends of
nuclear@20 168 * the kernel image are, in order to map them 1-1 before enabling paging.
nuclear@20 169 */
nuclear@19 170 void get_kernel_mem_range(uint32_t *start, uint32_t *end)
nuclear@19 171 {
nuclear@19 172 if(start) {
nuclear@19 173 *start = 0x100000;
nuclear@19 174 }
nuclear@19 175 if(end) {
nuclear@19 176 uint32_t e = (uint32_t)bitmap + bmsize;
nuclear@19 177
nuclear@19 178 if(e & PGOFFS_MASK) {
nuclear@19 179 *end = (e + 4096) & PGOFFS_MASK;
nuclear@19 180 } else {
nuclear@19 181 *end = e;
nuclear@19 182 }
nuclear@19 183 }
nuclear@17 184 }
nuclear@19 185
nuclear@20 186 /* adds a range of physical memory to the available pool. used during init_mem
nuclear@20 187 * when traversing the memory map.
nuclear@20 188 */
nuclear@19 189 static void add_memory(uint32_t start, size_t sz)
nuclear@19 190 {
nuclear@19 191 int i, szpg, pg;
nuclear@19 192
nuclear@19 193 szpg = ADDR_TO_PAGE(sz);
nuclear@19 194 pg = ADDR_TO_PAGE(start);
nuclear@19 195
nuclear@19 196 for(i=0; i<szpg; i++) {
nuclear@19 197 mark_page(pg++, FREE);
nuclear@19 198 }
nuclear@19 199 }
nuclear@19 200
nuclear@20 201 /* maps a page as used or free in the allocation bitmap */
nuclear@19 202 static void mark_page(int pg, int used)
nuclear@19 203 {
nuclear@19 204 int idx = BM_IDX(pg);
nuclear@19 205 int bit = BM_BIT(pg);
nuclear@19 206
nuclear@19 207 if(used) {
nuclear@19 208 bitmap[idx] |= 1 << bit;
nuclear@19 209 } else {
nuclear@19 210 bitmap[idx] &= ~(1 << bit);
nuclear@19 211 }
nuclear@19 212 }
nuclear@19 213