1 | /* |
---|
2 | * ppm.c - Per-cluster Physical Pages Manager implementation |
---|
3 | * |
---|
4 | * Authors Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
5 | * Alain Greiner (2016) |
---|
6 | * |
---|
7 | * Copyright (c) UPMC Sorbonne Universites |
---|
8 | * |
---|
9 | * This file is part of ALMOS-MKH. |
---|
10 | * |
---|
11 | * ALMOS-MKH.is free software; you can redistribute it and/or modify it |
---|
12 | * under the terms of the GNU General Public License as published by |
---|
13 | * the Free Software Foundation; version 2.0 of the License. |
---|
14 | * |
---|
15 | * ALMOS-MKH.is distributed in the hope that it will be useful, but |
---|
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | * General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License |
---|
21 | * along with ALMOS-MKH.; if not, write to the Free Software Foundation, |
---|
22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
23 | */ |
---|
24 | |
---|
25 | #include <kernel_config.h> |
---|
26 | #include <hal_types.h> |
---|
27 | #include <hal_special.h> |
---|
28 | #include <printk.h> |
---|
29 | #include <list.h> |
---|
30 | #include <bits.h> |
---|
31 | #include <page.h> |
---|
32 | #include <spinlock.h> |
---|
33 | #include <thread.h> |
---|
34 | #include <cluster.h> |
---|
35 | #include <kmem.h> |
---|
36 | #include <process.h> |
---|
37 | #include <dqdt.h> |
---|
38 | #include <ppm.h> |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | //////////////////////////////////////////////// |
---|
43 | inline bool_t ppm_page_is_valid( page_t * page ) |
---|
44 | { |
---|
45 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
46 | uint32_t pgnr = (uint32_t)( page - ppm->pages_tbl ); |
---|
47 | return (pgnr <= ppm->pages_nr); |
---|
48 | } |
---|
49 | |
---|
50 | //////////////////////////////////////////// |
---|
51 | inline void * ppm_page2base( page_t * page ) |
---|
52 | { |
---|
53 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
54 | return (void*)((page - ppm->pages_tbl) << CONFIG_PPM_PAGE_SHIFT); |
---|
55 | } |
---|
56 | |
---|
57 | //////////////////////////////////////////// |
---|
58 | inline page_t * ppm_base2page( void * base ) |
---|
59 | { |
---|
60 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
61 | return (ppm->pages_tbl + (((uint32_t)base ) >> CONFIG_PPM_PAGE_SHIFT)); |
---|
62 | } |
---|
63 | |
---|
64 | ////////////////////////////////////////// |
---|
65 | inline ppn_t ppm_page2ppn( page_t * page ) |
---|
66 | { |
---|
67 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
68 | return (ppn_t)( page - ppm->pages_tbl ); |
---|
69 | } |
---|
70 | |
---|
71 | ///////////////////////////////////////// |
---|
72 | inline page_t * ppm_ppn2page( ppn_t ppn ) |
---|
73 | { |
---|
74 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
75 | return &ppm->pages_tbl[ppn]; |
---|
76 | } |
---|
77 | |
---|
78 | /////////////////////////////////////// |
---|
79 | inline void * ppm_ppn2base( ppn_t ppn ) |
---|
80 | { |
---|
81 | return (void*)( ppn << CONFIG_PPM_PAGE_SHIFT ); |
---|
82 | } |
---|
83 | |
---|
84 | //////////////////////////////////////// |
---|
85 | inline ppn_t ppm_base2ppn( void * base ) |
---|
86 | { |
---|
87 | return (ppn_t)( (uint32_t)base >> CONFIG_PPM_PAGE_SHIFT ); |
---|
88 | } |
---|
89 | |
---|
90 | ////////////////////////////////////////////////// |
---|
91 | static void ppm_free_pages_nolock( page_t * page ) |
---|
92 | { |
---|
93 | page_t * buddy; // searched buddy page descriptor |
---|
94 | uint32_t buddy_index; // buddy page index |
---|
95 | page_t * current; // current (merged) page descriptor |
---|
96 | uint32_t current_index; // current (merged) page index |
---|
97 | uint32_t current_order; // current (merget) page order |
---|
98 | |
---|
99 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
100 | page_t * pages_tbl = ppm->pages_tbl; |
---|
101 | |
---|
102 | // update released page descriptor flags |
---|
103 | page_set_flag( page , PG_FREE ); |
---|
104 | |
---|
105 | // search the buddy page descriptor |
---|
106 | // - merge with current page descriptor if found |
---|
107 | // - exit to release the current page descriptor if not found |
---|
108 | current = page , |
---|
109 | current_index = (uint32_t)(page - ppm->pages_tbl); |
---|
110 | for( current_order = page->order ; |
---|
111 | current_order < CONFIG_PPM_MAX_ORDER ; |
---|
112 | current_order++ ) |
---|
113 | { |
---|
114 | buddy_index = current_index ^ (1 << current_order); |
---|
115 | buddy = pages_tbl + buddy_index; |
---|
116 | |
---|
117 | if( !page_is_flag( buddy , PG_FREE ) || (buddy->order != current_order) ) break; |
---|
118 | |
---|
119 | // remove buddy from free list |
---|
120 | list_unlink( &buddy->list ); |
---|
121 | ppm->free_pages_nr[current_order] --; |
---|
122 | ppm->total_free_pages -= (1 << current_order); |
---|
123 | |
---|
124 | // merge buddy with current |
---|
125 | buddy->order = 0; |
---|
126 | current_index &= buddy_index; |
---|
127 | } |
---|
128 | |
---|
129 | // update merged page descriptor order |
---|
130 | current = pages_tbl + current_index; |
---|
131 | current->order = current_order; |
---|
132 | |
---|
133 | // insert current in free list |
---|
134 | list_add_first( &ppm->free_pages_root[current_order] , ¤t->list ); |
---|
135 | ppm->free_pages_nr[current_order] ++; |
---|
136 | ppm->total_free_pages += (1 << current_order); |
---|
137 | |
---|
138 | } // end ppm_free_pages_nolock() |
---|
139 | |
---|
140 | ////////////////////////////// |
---|
141 | void ppm_init( ppm_t * ppm, |
---|
142 | uint32_t pages_nr, // total pages number |
---|
143 | uint32_t pages_offset ) // occupied pages |
---|
144 | { |
---|
145 | uint32_t i; |
---|
146 | |
---|
147 | // set signature |
---|
148 | ppm->signature = PPM_SIGNATURE; |
---|
149 | |
---|
150 | // initialize lock protecting the free_pages[] array |
---|
151 | spinlock_init( &ppm->free_lock ); |
---|
152 | |
---|
153 | // initialize free_pages[] array as empty |
---|
154 | ppm->total_free_pages = 0; |
---|
155 | for( i = 0 ; i < CONFIG_PPM_MAX_ORDER ; i++ ) |
---|
156 | { |
---|
157 | list_root_init( &ppm->free_pages_root[i] ); |
---|
158 | ppm->free_pages_nr[i] = 0; |
---|
159 | } |
---|
160 | |
---|
161 | // initialize dirty_list as empty |
---|
162 | list_root_init( &ppm->dirty_root ); |
---|
163 | |
---|
164 | // initialize pointer on page descriptors array |
---|
165 | ppm->pages_tbl = (page_t*)( pages_offset << CONFIG_PPM_PAGE_SHIFT ); |
---|
166 | |
---|
167 | // compute size of pages descriptor array rounded to an integer number of pages |
---|
168 | uint32_t bytes = ARROUND_UP( pages_nr * sizeof(page_t), CONFIG_PPM_PAGE_SIZE ); |
---|
169 | |
---|
170 | // compute number of pages required to store page descriptor array |
---|
171 | uint32_t pages_array = bytes >> CONFIG_PPM_PAGE_SHIFT; |
---|
172 | |
---|
173 | // compute total number of reserved pages (kernel code & pages_tbl[]) |
---|
174 | uint32_t reserved_pages = pages_offset + pages_array; |
---|
175 | |
---|
176 | // set pages numbers |
---|
177 | ppm->pages_nr = pages_nr; |
---|
178 | ppm->pages_offset = reserved_pages; |
---|
179 | |
---|
180 | // initialises all page descriptors in pages_tbl[] |
---|
181 | for( i = 0 ; i < pages_nr ; i++ ) |
---|
182 | { |
---|
183 | page_init( &ppm->pages_tbl[i] ); |
---|
184 | |
---|
185 | // TODO optimisation : make only a partial init [AG] |
---|
186 | // complete the initialisation when page is allocated [AG] |
---|
187 | // ppm->pages_tbl[i].flags = 0; |
---|
188 | } |
---|
189 | |
---|
190 | // - set PG_RESERVED flag for reserved pages (kernel code & pages_tbl[]) |
---|
191 | // - release all other pages to populate the free lists |
---|
192 | for( i = 0 ; i < reserved_pages ; i++) |
---|
193 | { |
---|
194 | page_set_flag( &ppm->pages_tbl[i] , PG_RESERVED ); |
---|
195 | } |
---|
196 | for( i = reserved_pages ; i < pages_nr ; i++ ) |
---|
197 | { |
---|
198 | ppm_free_pages_nolock( &ppm->pages_tbl[i] ); |
---|
199 | |
---|
200 | // TODO optimisation : decompose this enormous set of small pages |
---|
201 | // to a set big pages with various order values |
---|
202 | } |
---|
203 | |
---|
204 | // check consistency |
---|
205 | ppm_assert_order( ppm ); |
---|
206 | |
---|
207 | } // end ppm_init() |
---|
208 | |
---|
209 | //////////////////////////////////////////// |
---|
210 | page_t * ppm_alloc_pages( uint32_t order ) |
---|
211 | { |
---|
212 | uint32_t current_order; |
---|
213 | page_t * remaining_block; |
---|
214 | uint32_t current_size; |
---|
215 | |
---|
216 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
217 | |
---|
218 | assert( (ppm->signature == PPM_SIGNATURE) , __FUNCTION__ , "PPM non initialised" ); |
---|
219 | |
---|
220 | assert( (order < CONFIG_PPM_MAX_ORDER) , __FUNCTION__ , "illegal order argument" ); |
---|
221 | |
---|
222 | page_t * block = NULL; |
---|
223 | |
---|
224 | ppm_dmsg("\n[INFO] %s : enters / order = %d\n", |
---|
225 | __FUNCTION__ , order ); |
---|
226 | |
---|
227 | #if( CONFIG_PPM_DEBUG ) |
---|
228 | ppm_print( ppm , "before allocation" ); |
---|
229 | #endif |
---|
230 | |
---|
231 | // take lock protecting free lists |
---|
232 | spinlock_lock( &ppm->free_lock ); |
---|
233 | |
---|
234 | // find a free block equal or larger to requested size |
---|
235 | for( current_order = order ; current_order < CONFIG_PPM_MAX_ORDER ; current_order ++ ) |
---|
236 | { |
---|
237 | if( !list_is_empty( &ppm->free_pages_root[current_order] ) ) |
---|
238 | { |
---|
239 | block = LIST_FIRST( &ppm->free_pages_root[current_order] , page_t , list ); |
---|
240 | list_unlink( &block->list ); |
---|
241 | break; |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | if( block == NULL ) // return failure |
---|
246 | { |
---|
247 | // release lock protecting free lists |
---|
248 | spinlock_unlock( &ppm->free_lock ); |
---|
249 | |
---|
250 | return NULL; |
---|
251 | } |
---|
252 | |
---|
253 | // update free-lists after removing a block |
---|
254 | ppm->total_free_pages -= (1 << current_order); |
---|
255 | ppm->free_pages_nr[current_order] --; |
---|
256 | current_size = (1 << current_order); |
---|
257 | |
---|
258 | // split the removed block in smaller sub-blocks if required |
---|
259 | // and update the free-lists accordingly |
---|
260 | while( current_order > order ) |
---|
261 | { |
---|
262 | current_order --; |
---|
263 | current_size >>= 1; |
---|
264 | |
---|
265 | remaining_block = block + current_size; |
---|
266 | remaining_block->order = current_order; |
---|
267 | |
---|
268 | list_add_first( &ppm->free_pages_root[current_order] , &remaining_block->list ); |
---|
269 | ppm->free_pages_nr[current_order] ++; |
---|
270 | ppm->total_free_pages += (1 << current_order); |
---|
271 | } |
---|
272 | |
---|
273 | // update page descriptor |
---|
274 | page_clear_flag( block , PG_FREE ); |
---|
275 | page_refcount_up( block ); |
---|
276 | block->order = order; |
---|
277 | |
---|
278 | // release lock protecting free lists |
---|
279 | spinlock_unlock( &ppm->free_lock ); |
---|
280 | |
---|
281 | ppm_dmsg("\n[INFO] %s : base = %x / order = %d\n", |
---|
282 | __FUNCTION__ , (uint32_t)ppm_page2base( block ) , order ); |
---|
283 | |
---|
284 | #if CONFIG_PPM_DEBUG |
---|
285 | ppm_print( ppm , "after allocation" ); |
---|
286 | #endif |
---|
287 | |
---|
288 | return block; |
---|
289 | } // end pmm_alloc-pages() |
---|
290 | |
---|
291 | |
---|
292 | //////////////////////////////////// |
---|
293 | void ppm_free_pages( page_t * page ) |
---|
294 | { |
---|
295 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
296 | |
---|
297 | // get lock protecting free_pages[] array |
---|
298 | spinlock_lock( &ppm->free_lock ); |
---|
299 | |
---|
300 | ppm_free_pages_nolock( page ); |
---|
301 | |
---|
302 | // release lock protecting free_pages[] array |
---|
303 | spinlock_unlock( &ppm->free_lock ); |
---|
304 | } |
---|
305 | |
---|
306 | //////////////////////////// |
---|
307 | void ppm_print( ppm_t * ppm, |
---|
308 | char * string ) |
---|
309 | { |
---|
310 | uint32_t order; |
---|
311 | list_entry_t * iter; |
---|
312 | page_t * page; |
---|
313 | |
---|
314 | // get lock protecting free lists |
---|
315 | spinlock_lock( &ppm->free_lock ); |
---|
316 | |
---|
317 | printk("\n*** PPM state in cluster %x %s : pages = %d / offset = %d / free = %d ***\n", |
---|
318 | local_cxy , string , ppm->pages_nr , ppm->pages_offset , ppm->total_free_pages ); |
---|
319 | |
---|
320 | for( order = 0 ; order < CONFIG_PPM_MAX_ORDER ; order++ ) |
---|
321 | { |
---|
322 | printk("- order = %d / free_pages = %d [", |
---|
323 | order , ppm->free_pages_nr[order] ); |
---|
324 | |
---|
325 | LIST_FOREACH( &ppm->free_pages_root[order] , iter ) |
---|
326 | { |
---|
327 | page = LIST_ELEMENT( iter , page_t , list ); |
---|
328 | printk("%d," , page - ppm->pages_tbl ); |
---|
329 | } |
---|
330 | |
---|
331 | printk("]\n", NULL ); |
---|
332 | } |
---|
333 | |
---|
334 | // release lock protecting free lists |
---|
335 | spinlock_unlock( &ppm->free_lock ); |
---|
336 | |
---|
337 | } // end ppm_print() |
---|
338 | |
---|
339 | //////////////////////////u///////// |
---|
340 | void ppm_assert_order( ppm_t * ppm ) |
---|
341 | { |
---|
342 | uint32_t order; |
---|
343 | list_entry_t * iter; |
---|
344 | page_t * page; |
---|
345 | |
---|
346 | for(order=0; order < CONFIG_PPM_MAX_ORDER; order++) |
---|
347 | { |
---|
348 | if( list_is_empty( &ppm->free_pages_root[order] ) ) continue; |
---|
349 | |
---|
350 | LIST_FOREACH( &ppm->free_pages_root[order] , iter ) |
---|
351 | { |
---|
352 | page = LIST_ELEMENT( iter , page_t , list ); |
---|
353 | |
---|
354 | assert( (page->order == order) , __FUNCTION__ , "PPM inconsistency" ); |
---|
355 | } |
---|
356 | } |
---|
357 | |
---|
358 | } // end ppm_assert_order() |
---|
359 | |
---|