ia64/xen-unstable

view extras/mini-os/mm.c @ 3017:d0006e7baa9a

bitkeeper revision 1.1159.170.41 (419b5697bEIXHXiSbTxwpjfvjHraWw)

Reassert 2.1-devel after push to 2.0 tree.
author kaf24@freefall.cl.cam.ac.uk
date Wed Nov 17 13:48:07 2004 +0000 (2004-11-17)
parents 71f9c171157e
children 7561a06348cf
line source
1 /* -*- Mode:C; c-basic-offset:4; tab-width:4 -*-
2 ****************************************************************************
3 * (C) 2003 - Rolf Neugebauer - Intel Research Cambridge
4 ****************************************************************************
5 *
6 * File: mm.c
7 * Author: Rolf Neugebauer (neugebar@dcs.gla.ac.uk)
8 * Changes:
9 *
10 * Date: Aug 2003
11 *
12 * Environment: Xen Minimal OS
13 * Description: memory management related functions
14 * contains buddy page allocator from Xen.
15 *
16 ****************************************************************************
17 * $Id: c-insert.c,v 1.7 2002/11/08 16:04:34 rn Exp $
18 ****************************************************************************
19 * Permission is hereby granted, free of charge, to any person obtaining a copy
20 * of this software and associated documentation files (the "Software"), to
21 * deal in the Software without restriction, including without limitation the
22 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
23 * sell copies of the Software, and to permit persons to whom the Software is
24 * furnished to do so, subject to the following conditions:
25 *
26 * The above copyright notice and this permission notice shall be included in
27 * all copies or substantial portions of the Software.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35 * DEALINGS IN THE SOFTWARE.
36 */
38 #include <os.h>
39 #include <hypervisor.h>
40 #include <mm.h>
41 #include <types.h>
42 #include <lib.h>
44 unsigned long *phys_to_machine_mapping;
45 extern char *stack;
46 extern char _text, _etext, _edata, _end;
48 static void init_page_allocator(unsigned long min, unsigned long max);
50 void init_mm(void)
51 {
53 unsigned long start_pfn, max_pfn, max_free_pfn;
55 unsigned long *pgd = (unsigned long *)start_info.pt_base;
57 printk("MM: Init\n");
59 printk(" _text: %p\n", &_text);
60 printk(" _etext: %p\n", &_etext);
61 printk(" _edata: %p\n", &_edata);
62 printk(" stack start: %p\n", &stack);
63 printk(" _end: %p\n", &_end);
65 /* set up minimal memory infos */
66 start_pfn = PFN_UP(to_phys(&_end));
67 max_pfn = start_info.nr_pages;
69 printk(" start_pfn: %lx\n", start_pfn);
70 printk(" max_pfn: %lx\n", max_pfn);
72 /*
73 * we know where free tables start (start_pfn) and how many we
74 * have (max_pfn).
75 *
76 * Currently the hypervisor stores page tables it providesin the
77 * high region of the this memory range.
78 *
79 * next we work out how far down this goes (max_free_pfn)
80 *
81 * XXX this assumes the hypervisor provided page tables to be in
82 * the upper region of our initial memory. I don't know if this
83 * is always true.
84 */
86 max_free_pfn = PFN_DOWN(to_phys(pgd));
87 {
88 unsigned long *pgd = (unsigned long *)start_info.pt_base;
89 unsigned long pte;
90 int i;
91 printk(" pgd(pa(pgd)): %lx(%lx)", (u_long)pgd, to_phys(pgd));
93 for ( i = 0; i < (HYPERVISOR_VIRT_START>>22); i++ )
94 {
95 unsigned long pgde = *pgd++;
96 if ( !(pgde & 1) ) continue;
97 pte = machine_to_phys(pgde & PAGE_MASK);
98 printk(" PT(%x): %lx(%lx)", i, (u_long)to_virt(pte), pte);
99 if (PFN_DOWN(pte) <= max_free_pfn)
100 max_free_pfn = PFN_DOWN(pte);
101 }
102 }
103 max_free_pfn--;
104 printk(" max_free_pfn: %lx\n", max_free_pfn);
106 /*
107 * now we can initialise the page allocator
108 */
109 printk("MM: Initialise page allocator for %lx(%lx)-%lx(%lx)\n",
110 (u_long)to_virt(PFN_PHYS(start_pfn)), PFN_PHYS(start_pfn),
111 (u_long)to_virt(PFN_PHYS(max_free_pfn)), PFN_PHYS(max_free_pfn));
112 init_page_allocator(PFN_PHYS(start_pfn), PFN_PHYS(max_free_pfn));
115 /* Now initialise the physical->machine mapping table. */
118 printk("MM: done\n");
121 }
123 /*********************
124 * ALLOCATION BITMAP
125 * One bit per page of memory. Bit set => page is allocated.
126 */
128 static unsigned long *alloc_bitmap;
129 #define PAGES_PER_MAPWORD (sizeof(unsigned long) * 8)
131 #define allocated_in_map(_pn) \
132 (alloc_bitmap[(_pn)/PAGES_PER_MAPWORD] & (1<<((_pn)&(PAGES_PER_MAPWORD-1))))
135 /*
136 * Hint regarding bitwise arithmetic in map_{alloc,free}:
137 * -(1<<n) sets all bits >= n.
138 * (1<<n)-1 sets all bits < n.
139 * Variable names in map_{alloc,free}:
140 * *_idx == Index into `alloc_bitmap' array.
141 * *_off == Bit offset within an element of the `alloc_bitmap' array.
142 */
144 static void map_alloc(unsigned long first_page, unsigned long nr_pages)
145 {
146 unsigned long start_off, end_off, curr_idx, end_idx;
148 curr_idx = first_page / PAGES_PER_MAPWORD;
149 start_off = first_page & (PAGES_PER_MAPWORD-1);
150 end_idx = (first_page + nr_pages) / PAGES_PER_MAPWORD;
151 end_off = (first_page + nr_pages) & (PAGES_PER_MAPWORD-1);
153 if ( curr_idx == end_idx )
154 {
155 alloc_bitmap[curr_idx] |= ((1<<end_off)-1) & -(1<<start_off);
156 }
157 else
158 {
159 alloc_bitmap[curr_idx] |= -(1<<start_off);
160 while ( ++curr_idx < end_idx ) alloc_bitmap[curr_idx] = ~0L;
161 alloc_bitmap[curr_idx] |= (1<<end_off)-1;
162 }
163 }
166 static void map_free(unsigned long first_page, unsigned long nr_pages)
167 {
168 unsigned long start_off, end_off, curr_idx, end_idx;
170 curr_idx = first_page / PAGES_PER_MAPWORD;
171 start_off = first_page & (PAGES_PER_MAPWORD-1);
172 end_idx = (first_page + nr_pages) / PAGES_PER_MAPWORD;
173 end_off = (first_page + nr_pages) & (PAGES_PER_MAPWORD-1);
175 if ( curr_idx == end_idx )
176 {
177 alloc_bitmap[curr_idx] &= -(1<<end_off) | ((1<<start_off)-1);
178 }
179 else
180 {
181 alloc_bitmap[curr_idx] &= (1<<start_off)-1;
182 while ( ++curr_idx != end_idx ) alloc_bitmap[curr_idx] = 0;
183 alloc_bitmap[curr_idx] &= -(1<<end_off);
184 }
185 }
189 /*************************
190 * BINARY BUDDY ALLOCATOR
191 */
193 typedef struct chunk_head_st chunk_head_t;
194 typedef struct chunk_tail_st chunk_tail_t;
196 struct chunk_head_st {
197 chunk_head_t *next;
198 chunk_head_t **pprev;
199 int level;
200 };
202 struct chunk_tail_st {
203 int level;
204 };
206 /* Linked lists of free chunks of different powers-of-two in size. */
207 #define FREELIST_SIZE ((sizeof(void*)<<3)-PAGE_SHIFT)
208 static chunk_head_t *free_head[FREELIST_SIZE];
209 static chunk_head_t free_tail[FREELIST_SIZE];
210 #define FREELIST_EMPTY(_l) ((_l)->next == NULL)
212 #define round_pgdown(_p) ((_p)&PAGE_MASK)
213 #define round_pgup(_p) (((_p)+(PAGE_SIZE-1))&PAGE_MASK)
216 /*
217 * Initialise allocator, placing addresses [@min,@max] in free pool.
218 * @min and @max are PHYSICAL addresses.
219 */
220 static void init_page_allocator(unsigned long min, unsigned long max)
221 {
222 int i;
223 unsigned long range, bitmap_size;
224 chunk_head_t *ch;
225 chunk_tail_t *ct;
227 for ( i = 0; i < FREELIST_SIZE; i++ )
228 {
229 free_head[i] = &free_tail[i];
230 free_tail[i].pprev = &free_head[i];
231 free_tail[i].next = NULL;
232 }
234 min = round_pgup (min);
235 max = round_pgdown(max);
237 /* Allocate space for the allocation bitmap. */
238 bitmap_size = (max+1) >> (PAGE_SHIFT+3);
239 bitmap_size = round_pgup(bitmap_size);
240 alloc_bitmap = (unsigned long *)to_virt(min);
241 min += bitmap_size;
242 range = max - min;
244 /* All allocated by default. */
245 memset(alloc_bitmap, ~0, bitmap_size);
246 /* Free up the memory we've been given to play with. */
247 map_free(min>>PAGE_SHIFT, range>>PAGE_SHIFT);
249 /* The buddy lists are addressed in high memory. */
250 min += VIRT_START;
251 max += VIRT_START;
253 while ( range != 0 )
254 {
255 /*
256 * Next chunk is limited by alignment of min, but also
257 * must not be bigger than remaining range.
258 */
259 for ( i = PAGE_SHIFT; (1<<(i+1)) <= range; i++ )
260 if ( min & (1<<i) ) break;
263 ch = (chunk_head_t *)min;
264 min += (1<<i);
265 range -= (1<<i);
266 ct = (chunk_tail_t *)min-1;
267 i -= PAGE_SHIFT;
268 ch->level = i;
269 ch->next = free_head[i];
270 ch->pprev = &free_head[i];
271 ch->next->pprev = &ch->next;
272 free_head[i] = ch;
273 ct->level = i;
274 }
275 }
278 /* Allocate 2^@order contiguous pages. Returns a VIRTUAL address. */
279 unsigned long alloc_pages(int order)
280 {
281 int i;
282 chunk_head_t *alloc_ch, *spare_ch;
283 chunk_tail_t *spare_ct;
286 /* Find smallest order which can satisfy the request. */
287 for ( i = order; i < FREELIST_SIZE; i++ ) {
288 if ( !FREELIST_EMPTY(free_head[i]) )
289 break;
290 }
292 if ( i == FREELIST_SIZE ) goto no_memory;
294 /* Unlink a chunk. */
295 alloc_ch = free_head[i];
296 free_head[i] = alloc_ch->next;
297 alloc_ch->next->pprev = alloc_ch->pprev;
299 /* We may have to break the chunk a number of times. */
300 while ( i != order )
301 {
302 /* Split into two equal parts. */
303 i--;
304 spare_ch = (chunk_head_t *)((char *)alloc_ch + (1<<(i+PAGE_SHIFT)));
305 spare_ct = (chunk_tail_t *)((char *)spare_ch + (1<<(i+PAGE_SHIFT)))-1;
307 /* Create new header for spare chunk. */
308 spare_ch->level = i;
309 spare_ch->next = free_head[i];
310 spare_ch->pprev = &free_head[i];
311 spare_ct->level = i;
313 /* Link in the spare chunk. */
314 spare_ch->next->pprev = &spare_ch->next;
315 free_head[i] = spare_ch;
316 }
318 map_alloc(to_phys(alloc_ch)>>PAGE_SHIFT, 1<<order);
320 return((unsigned long)alloc_ch);
322 no_memory:
324 printk("Cannot handle page request order %d!\n", order);
326 return 0;
327 }