kern

view src/mem.c @ 19:8be069e6bb05

I think I'm done with the physical memory page allocator
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 30 Mar 2011 22:42:16 +0300
parents 098b1cb5eeaa
children 369adbbd4bdd
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 static uint32_t *bitmap;
22 static int bmsize, last_alloc_idx;
24 void init_mem(struct mboot_info *mb)
25 {
26 int i, num_pages, max_pg = 0;
27 uint32_t used_end;
29 num_pages = 0;
30 last_alloc_idx = 0;
32 bitmap = (uint32_t*)&_end;
34 /* start by marking all posible pages as used */
35 memset(bitmap, 0xff, 1024 * 1024 / 8);
37 /* build the memory map */
38 if(mb->flags & MB_MMAP) {
39 struct mboot_mmap *mem, *mmap_end;
41 mem = mb->mmap;
42 mmap_end = (struct mboot_mmap*)((char*)mb->mmap + mb->mmap_len);
44 printf("memory map:\n");
45 while(mem < mmap_end) {
46 char *type;
47 unsigned int end = mem->base_low + mem->length_low;
49 if(mem->type == MB_MEM_VALID) {
50 type = "free:";
51 add_memory(mem->base_low, mem->length_low);
53 num_pages = ADDR_TO_PAGE(mem->base_low + mem->length_low);
54 if(max_pg < num_pages) {
55 max_pg = num_pages;
56 }
57 } else {
58 type = "hole:";
59 }
61 printf(" %s %x - %x (%u bytes)\n", type, mem->base_low, end, mem->length_low);
62 mem = (struct mboot_mmap*)((char*)mem + mem->skip + sizeof mem->skip);
63 }
64 } else if(mb->flags & MB_MEM) {
65 add_memory(0, mb->mem_lower);
66 add_memory(0x100000, mb->mem_upper * 1024);
67 max_pg = mb->mem_upper / 4;
69 printf("lower memory: %ukb, upper mem: %ukb\n", mb->mem_lower, mb->mem_upper);
70 } else {
71 panic("didn't get any memory info from the boot loader, I give up\n");
72 }
74 bmsize = max_pg / 8; /* size of the bitmap in bytes */
76 /* mark all the used pages as ... well ... used */
77 used_end = ((uint32_t)bitmap + bmsize - 1);
79 printf("marking pages up to %x ", used_end);
80 used_end = ADDR_TO_PAGE(used_end);
81 printf("(page: %d) inclusive as used\n", used_end);
83 for(i=0; i<=used_end; i++) {
84 mark_page(i, USED);
85 }
87 /*for(i=0; i<bmsize / 4; i++) {
88 printf("%3d [%x]\n", i, bitmap[i]);
89 asm("hlt");
90 }
91 putchar('\n');*/
92 }
94 uint32_t alloc_phys_page(void)
95 {
96 int i, idx, max;
98 idx = last_alloc_idx;
99 max = bmsize / 4;
101 while(idx <= max) {
102 /* if at least one bit is 0 then we have at least
103 * one free page. find it and allocate it.
104 */
105 if(bitmap[idx] != 0xffffffff) {
106 for(i=0; i<32; i++) {
107 int pg = idx * 32 + i;
109 if(IS_FREE(pg)) {
110 mark_page(pg, USED);
112 last_alloc_idx = idx;
114 printf("alloc_phys_page() -> %x (page: %d)\n", PAGE_TO_ADDR(pg), pg);
115 return PAGE_TO_ADDR(pg);
116 }
117 }
118 panic("can't happen: alloc_phys_page (mem.c)\n");
119 }
120 idx++;
121 }
123 panic("alloc_phys_page(): out of memory\n");
124 return 0;
125 }
127 void free_phys_page(uint32_t addr)
128 {
129 int pg = ADDR_TO_PAGE(addr);
130 int bmidx = BM_IDX(pg);
132 if(!IS_FREE(pg)) {
133 panic("free_phys_page(%d): I thought that was already free!\n", pg);
134 }
136 mark_page(pg, FREE);
137 if(bmidx < last_alloc_idx) {
138 last_alloc_idx = bmidx;
139 }
140 }
142 void get_kernel_mem_range(uint32_t *start, uint32_t *end)
143 {
144 if(start) {
145 *start = 0x100000;
146 }
147 if(end) {
148 uint32_t e = (uint32_t)bitmap + bmsize;
150 if(e & PGOFFS_MASK) {
151 *end = (e + 4096) & PGOFFS_MASK;
152 } else {
153 *end = e;
154 }
155 }
156 }
158 static void add_memory(uint32_t start, size_t sz)
159 {
160 int i, szpg, pg;
162 szpg = ADDR_TO_PAGE(sz);
163 pg = ADDR_TO_PAGE(start);
165 for(i=0; i<szpg; i++) {
166 mark_page(pg++, FREE);
167 }
168 }
170 static void mark_page(int pg, int used)
171 {
172 int idx = BM_IDX(pg);
173 int bit = BM_BIT(pg);
175 if(used) {
176 bitmap[idx] |= 1 << bit;
177 } else {
178 bitmap[idx] &= ~(1 << bit);
179 }
180 }