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