f263dba10e917e509fe0b1946c8a7b7abbe8c77f
[akaros.git] / kern / src / page_alloc.c
1 /* Copyright (c) 2009 The Regents of the University  of California. 
2  * See the COPYRIGHT files at the top of this source tree for full 
3  * license information.
4  * 
5  * Kevin Klues <klueska@cs.berkeley.edu>    
6  */
7
8 #ifdef __SHARC__
9 #pragma nosharc
10 #endif
11
12 #include <sys/queue.h>
13 #include <arch/bitmask.h>
14 #include <page_alloc.h>
15 #include <pmap.h>
16 #include <string.h>
17 #include <kmalloc.h>
18
19 #define l1 (available_caches.l1)
20 #define l2 (available_caches.l2)
21 #define l3 (available_caches.l3)
22
23 static void __page_decref(page_t *CT(1) page);
24 static void __page_incref(page_t *CT(1) page);
25 static error_t __page_alloc_specific(page_t** page, size_t ppn);
26 static error_t __page_free(page_t *CT(1) page);
27
28 // Global list of colors allocated to the general purpose memory allocator
29 uint8_t* global_cache_colors_map;
30 size_t global_next_color = 0;
31
32 void colored_page_alloc_init()
33 {
34         global_cache_colors_map = 
35                kmalloc(BYTES_FOR_BITMASK(llc_cache->num_colors), 0);
36         CLR_BITMASK(global_cache_colors_map, llc_cache->num_colors);
37         for(int i = 0; i < llc_cache->num_colors/8; i++)
38                 cache_color_alloc(llc_cache, global_cache_colors_map);
39 }
40
41 /**
42  * @brief Clear a Page structure.
43  *
44  * The result has null links and 0 refcount.
45  * Note that the corresponding physical page is NOT initialized!
46  */
47 static void __page_clear(page_t *SAFE page)
48 {
49         memset(page, 0, sizeof(page_t));
50 }
51
52 #define __PAGE_ALLOC_FROM_RANGE_GENERIC(page, base_color, range, predicate) \
53         /* Find first available color with pages available */                   \
54     /* in the given range */                                                \
55         int i = base_color;                                                     \
56         for (i; i < (base_color+range); i++) {                                  \
57                 if((predicate))                                                     \
58                         break;                                                          \
59         }                                                                       \
60         /* Allocate a page from that color */                                   \
61         if(i < (base_color+range)) {                                            \
62                 *page = LIST_FIRST(&colored_page_free_list[i]);                     \
63                 LIST_REMOVE(*page, page_link);                                      \
64                 __page_clear(*page);                                                \
65                 return i;                                                           \
66         }                                                                       \
67         return -ENOMEM;
68
69 static ssize_t __page_alloc_from_color_range(page_t** page,  
70                                            uint16_t base_color,
71                                            uint16_t range) 
72 {
73         __PAGE_ALLOC_FROM_RANGE_GENERIC(page, base_color, range, 
74                          !LIST_EMPTY(&colored_page_free_list[i]));
75 }
76
77 static ssize_t __page_alloc_from_color_map_range(page_t** page, uint8_t* map, 
78                                               size_t base_color, size_t range)
79 {  
80         __PAGE_ALLOC_FROM_RANGE_GENERIC(page, base_color, range, 
81                     GET_BITMASK_BIT(map, i) && !LIST_EMPTY(&colored_page_free_list[i]))
82 }
83
84 static ssize_t __colored_page_alloc(uint8_t* map, page_t** page, 
85                                                size_t next_color)
86 {
87         ssize_t ret;
88         if((ret = __page_alloc_from_color_map_range(page, map, 
89                                    next_color, llc_cache->num_colors - next_color)) < 0)
90                 ret = __page_alloc_from_color_map_range(page, map, 0, next_color);
91         return ret;
92 }
93
94 /* Internal version of page_alloc_specific.  Grab the lock first. */
95 static error_t __page_alloc_specific(page_t** page, size_t ppn)
96 {
97         page_t* sp_page = ppn2page(ppn);
98         if( sp_page->page_ref != 0 )
99                 return -ENOMEM;
100         *page = sp_page;
101         LIST_REMOVE(*page, page_link);
102
103         __page_clear(*page);
104         return 0;
105 }
106
107 /**
108  * @brief Allocates a physical page from a pool of unused physical memory.
109  *
110  * Zeroes the page.
111  *
112  * @param[out] page  set to point to the Page struct
113  *                   of the newly allocated page
114  *
115  * @return ESUCCESS on success
116  * @return -ENOMEM  otherwise
117  */
118 #ifdef __CONFIG_PAGE_COLORING__
119 error_t upage_alloc(struct proc* p, page_t** page, int zero)
120 {
121         spin_lock_irqsave(&colored_page_free_list_lock);
122         ssize_t ret = __colored_page_alloc(p->cache_colors_map, 
123                                              page, p->next_cache_color);
124         spin_unlock_irqsave(&colored_page_free_list_lock);
125
126         if(ret >= 0)
127         {
128                 if(zero)
129                         memset(page2kva(*page),0,PGSIZE);
130                 p->next_cache_color = (ret + 1) & (llc_cache->num_colors-1);
131                 return 0;
132         }
133         return ret;
134 }
135 #else 
136 error_t upage_alloc(struct proc* p, page_t** page, int zero)
137 {
138         ssize_t ret;
139         spin_lock_irqsave(&colored_page_free_list_lock);
140         if((ret = __page_alloc_from_color_range(page, global_next_color, 
141                                     llc_cache->num_colors - global_next_color)) < 0)
142                 ret = __page_alloc_from_color_range(page, 0, global_next_color);
143
144         if(ret >= 0) {
145                 global_next_color = ret;        
146                 ret = ESUCCESS;
147         }
148         spin_unlock_irqsave(&colored_page_free_list_lock);
149         
150         return ret;
151 }
152 #endif
153
154 error_t kpage_alloc(page_t** page) 
155 {
156         ssize_t ret;
157         spin_lock_irqsave(&colored_page_free_list_lock);
158         if((ret = __page_alloc_from_color_range(page, global_next_color, 
159                                     llc_cache->num_colors - global_next_color)) < 0)
160                 ret = __page_alloc_from_color_range(page, 0, global_next_color);
161
162         if(ret >= 0) {
163                 global_next_color = ret;        
164                 page_incref(*page);
165                 ret = ESUCCESS;
166         }
167         spin_unlock_irqsave(&colored_page_free_list_lock);
168         
169         return ret;
170 }
171
172 /**
173  * @brief Allocated 2^order contiguous physical pages.  Will increment the
174  * reference count for the pages.
175  *
176  * @param[in] order order of the allocation
177  * @param[in] flags memory allocation flags
178  *
179  * @return The KVA of the first page, NULL otherwise.
180  */
181 void *get_cont_pages(size_t order, int flags)
182 {
183         size_t npages = 1 << order;     
184
185         // Find 'npages' free consecutive pages
186         int first = -1;
187         spin_lock_irqsave(&colored_page_free_list_lock);
188         for(int i=(naddrpages-1); i>=(npages-1); i--) {
189                 int j;
190                 for(j=i; j>=(i-(npages-1)); j--) {
191                         if( !page_is_free(j) ) {
192                                 i = j - 1;
193                                 break;
194                         }
195                 }
196                 if( j == (i-(npages-1)-1)) {
197                         first = j+1;
198                         break;
199                 }
200         }
201         //If we couldn't find them, return NULL
202         if( first == -1 ) {
203                 spin_unlock_irqsave(&colored_page_free_list_lock);
204                 return NULL;
205         }
206
207         for(int i=0; i<npages; i++) {
208                 page_t* page;
209                 __page_alloc_specific(&page, first+i);
210                 page_incref(page); 
211         }
212         spin_unlock_irqsave(&colored_page_free_list_lock);
213         return ppn2kva(first);
214 }
215
216 void free_cont_pages(void *buf, size_t order)
217 {
218         size_t npages = 1 << order;     
219         spin_lock_irqsave(&colored_page_free_list_lock);
220         for (int i = kva2ppn(buf); i < kva2ppn(buf) + npages; i++) {
221                 __page_decref(ppn2page(i));
222                 assert(page_is_free(i));
223         }
224         spin_unlock_irqsave(&colored_page_free_list_lock);
225         return; 
226 }
227
228 /*
229  * Allocates a specific physical page.
230  * Does NOT set the contents of the physical page to zero -
231  * the caller must do that if necessary.
232  *
233  * ppn         -- the page number to allocate
234  * *page       -- is set to point to the Page struct 
235  *                of the newly allocated page
236  *
237  * RETURNS 
238  *   ESUCCESS  -- on success
239  *   -ENOMEM   -- otherwise 
240  */
241 error_t upage_alloc_specific(struct proc* p, page_t** page, size_t ppn)
242 {
243         spin_lock_irqsave(&colored_page_free_list_lock);
244         __page_alloc_specific(page, ppn);
245         spin_unlock_irqsave(&colored_page_free_list_lock);
246         return 0;
247 }
248
249 error_t kpage_alloc_specific(page_t** page, size_t ppn)
250 {
251         spin_lock_irqsave(&colored_page_free_list_lock);
252         __page_alloc_specific(page, ppn);
253         page_incref(*page);
254         spin_unlock_irqsave(&colored_page_free_list_lock);
255         return 0;
256 }
257
258 /*
259  * Return a page to the free list.
260  * (This function should only be called when pp->page_ref reaches 0.)
261  * You must hold the page_free list lock before calling this.
262  */
263 static error_t __page_free(page_t* page) 
264 {
265         __page_clear(page);
266
267         LIST_INSERT_HEAD(
268            &(colored_page_free_list[get_page_color(page2ppn(page), llc_cache)]),
269            page,
270            page_link
271         );
272
273         return ESUCCESS;
274 }
275
276 error_t page_free(page_t *SAFE page)
277 {
278         error_t retval;
279         spin_lock_irqsave(&colored_page_free_list_lock);
280         retval = __page_free(page);
281         spin_unlock_irqsave(&colored_page_free_list_lock);
282         return retval;
283 }
284
285 /*
286  * Check if a page with the given physical page # is free
287  */
288 int page_is_free(size_t ppn) {
289         page_t* page = ppn2page(ppn);
290         if( page->page_ref == 0 )
291                 return TRUE;
292         return FALSE;
293 }
294
295 /*
296  * Increment the reference count on a page
297  */
298 void page_incref(page_t *page)
299 {
300         __page_incref(page);
301 }
302
303 void __page_incref(page_t *page)
304 {
305         page->page_ref++;
306 }
307
308 /*
309  * Decrement the reference count on a page,
310  * freeing it if there are no more refs.
311  */
312 void page_decref(page_t *page)
313 {
314         spin_lock_irqsave(&colored_page_free_list_lock);
315         __page_decref(page);
316         spin_unlock_irqsave(&colored_page_free_list_lock);
317 }
318
319 /*
320  * Decrement the reference count on a page,
321  * freeing it if there are no more refs.
322  */
323 static void __page_decref(page_t *page)
324 {
325         if (page->page_ref == 0) {
326                 panic("Trying to Free already freed page: %d...\n", page2ppn(page));
327                 return;
328         }
329         if (--page->page_ref == 0)
330                 __page_free(page);
331 }
332
333 /*
334  * Set the reference count on a page to a specific value
335  */
336 void page_setref(page_t *page, size_t val)
337 {
338         page->page_ref = val;
339 }
340
341 /*
342  * Get the reference count on a page
343  */
344 size_t page_getref(page_t *page)
345 {
346         return page->page_ref;
347 }
348