kern

view src/mem.c @ 22:7ece008f09c5

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