1 | /* |
---|
2 | * ppm.c - Per-cluster Physical Pages Manager implementation |
---|
3 | * |
---|
4 | * Authors Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
5 | * Alain Greiner (2016,2017) |
---|
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_kernel_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 | inline bool_t ppm_page_is_valid( page_t * page ) |
---|
42 | { |
---|
43 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
44 | uint32_t pgnr = (uint32_t)( page - ppm->pages_tbl ); |
---|
45 | return (pgnr <= ppm->pages_nr); |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | |
---|
50 | ///////////////////////////////////////////// |
---|
51 | inline xptr_t ppm_page2base( xptr_t page_xp ) |
---|
52 | { |
---|
53 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
54 | |
---|
55 | cxy_t page_cxy = GET_CXY( page_xp ); |
---|
56 | page_t * page_ptr = GET_PTR( page_xp ); |
---|
57 | |
---|
58 | void * base_ptr = ppm->vaddr_base + |
---|
59 | ((page_ptr - ppm->pages_tbl)<<CONFIG_PPM_PAGE_SHIFT); |
---|
60 | |
---|
61 | return XPTR( page_cxy , base_ptr ); |
---|
62 | |
---|
63 | } // end ppm_page2base() |
---|
64 | |
---|
65 | ///////////////////////////////////////////// |
---|
66 | inline xptr_t ppm_base2page( xptr_t base_xp ) |
---|
67 | { |
---|
68 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
69 | |
---|
70 | cxy_t base_cxy = GET_CXY( base_xp ); |
---|
71 | void * base_ptr = GET_PTR( base_xp ); |
---|
72 | |
---|
73 | page_t * page_ptr = ppm->pages_tbl + |
---|
74 | ((base_ptr - ppm->vaddr_base)>>CONFIG_PPM_PAGE_SHIFT); |
---|
75 | |
---|
76 | return XPTR( base_cxy , page_ptr ); |
---|
77 | |
---|
78 | } // end ppm_base2page() |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | /////////////////////////////////////////// |
---|
83 | inline ppn_t ppm_page2ppn( xptr_t page_xp ) |
---|
84 | { |
---|
85 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
86 | |
---|
87 | cxy_t page_cxy = GET_CXY( page_xp ); |
---|
88 | page_t * page_ptr = GET_PTR( page_xp ); |
---|
89 | |
---|
90 | paddr_t paddr = PADDR( page_cxy , (page_ptr - ppm->pages_tbl)<<CONFIG_PPM_PAGE_SHIFT ); |
---|
91 | |
---|
92 | return (ppn_t)(paddr >> CONFIG_PPM_PAGE_SHIFT); |
---|
93 | |
---|
94 | } // end hal_page2ppn() |
---|
95 | |
---|
96 | /////////////////////////////////////// |
---|
97 | inline xptr_t ppm_ppn2page( ppn_t ppn ) |
---|
98 | { |
---|
99 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
100 | |
---|
101 | paddr_t paddr = ((paddr_t)ppn) << CONFIG_PPM_PAGE_SHIFT; |
---|
102 | |
---|
103 | cxy_t cxy = CXY_FROM_PADDR( paddr ); |
---|
104 | lpa_t lpa = LPA_FROM_PADDR( paddr ); |
---|
105 | |
---|
106 | return XPTR( cxy , &ppm->pages_tbl[lpa>>CONFIG_PPM_PAGE_SHIFT] ); |
---|
107 | |
---|
108 | } // end hal_ppn2page |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | /////////////////////////////////////// |
---|
113 | inline xptr_t ppm_ppn2base( ppn_t ppn ) |
---|
114 | { |
---|
115 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
116 | |
---|
117 | paddr_t paddr = ((paddr_t)ppn) << CONFIG_PPM_PAGE_SHIFT; |
---|
118 | |
---|
119 | cxy_t cxy = CXY_FROM_PADDR( paddr ); |
---|
120 | lpa_t lpa = LPA_FROM_PADDR( paddr ); |
---|
121 | |
---|
122 | return XPTR( cxy , (void *)ppm->vaddr_base + lpa ); |
---|
123 | |
---|
124 | } // end ppm_ppn2base() |
---|
125 | |
---|
126 | /////////////////////////////////////////// |
---|
127 | inline ppn_t ppm_base2ppn( xptr_t base_xp ) |
---|
128 | { |
---|
129 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
130 | |
---|
131 | cxy_t base_cxy = GET_CXY( base_xp ); |
---|
132 | void * base_ptr = GET_PTR( base_xp ); |
---|
133 | |
---|
134 | paddr_t paddr = PADDR( base_cxy , (base_ptr - ppm->vaddr_base) ); |
---|
135 | |
---|
136 | return (ppn_t)(paddr >> CONFIG_PPM_PAGE_SHIFT); |
---|
137 | |
---|
138 | } // end ppm_base2ppn() |
---|
139 | |
---|
140 | |
---|
141 | |
---|
142 | /////////////////////////////////////////// |
---|
143 | void ppm_free_pages_nolock( page_t * page ) |
---|
144 | { |
---|
145 | page_t * buddy; // searched buddy page descriptor |
---|
146 | uint32_t buddy_index; // buddy page index |
---|
147 | page_t * current; // current (merged) page descriptor |
---|
148 | uint32_t current_index; // current (merged) page index |
---|
149 | uint32_t current_order; // current (merged) page order |
---|
150 | |
---|
151 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
152 | page_t * pages_tbl = ppm->pages_tbl; |
---|
153 | |
---|
154 | assert( !page_is_flag( page , PG_FREE ) , __FUNCTION__ , |
---|
155 | "page already released : ppn = %x\n" , ppm_page2ppn(XPTR(local_cxy,page)) ); |
---|
156 | |
---|
157 | assert( !page_is_flag( page , PG_RESERVED ) , __FUNCTION__ , |
---|
158 | "reserved page : ppn = %x\n" , ppm_page2ppn(XPTR(local_cxy,page)) ); |
---|
159 | |
---|
160 | // update released page descriptor flags |
---|
161 | page_set_flag( page , PG_FREE ); |
---|
162 | |
---|
163 | // search the buddy page descriptor |
---|
164 | // - merge with current page descriptor if found |
---|
165 | // - exit to release the current page descriptor if not found |
---|
166 | current = page , |
---|
167 | current_index = (uint32_t)(page - ppm->pages_tbl); |
---|
168 | for( current_order = page->order ; |
---|
169 | current_order < CONFIG_PPM_MAX_ORDER ; |
---|
170 | current_order++ ) |
---|
171 | { |
---|
172 | buddy_index = current_index ^ (1 << current_order); |
---|
173 | buddy = pages_tbl + buddy_index; |
---|
174 | |
---|
175 | if( !page_is_flag( buddy , PG_FREE ) || (buddy->order != current_order) ) break; |
---|
176 | |
---|
177 | // remove buddy from free list |
---|
178 | list_unlink( &buddy->list ); |
---|
179 | ppm->free_pages_nr[current_order] --; |
---|
180 | |
---|
181 | // merge buddy with current |
---|
182 | buddy->order = 0; |
---|
183 | current_index &= buddy_index; |
---|
184 | } |
---|
185 | |
---|
186 | // update merged page descriptor order |
---|
187 | current = pages_tbl + current_index; |
---|
188 | current->order = current_order; |
---|
189 | |
---|
190 | // insert current in free list |
---|
191 | list_add_first( &ppm->free_pages_root[current_order] , ¤t->list ); |
---|
192 | ppm->free_pages_nr[current_order] ++; |
---|
193 | |
---|
194 | } // end ppm_free_pages_nolock() |
---|
195 | |
---|
196 | //////////////////////////////////////////// |
---|
197 | page_t * ppm_alloc_pages( uint32_t order ) |
---|
198 | { |
---|
199 | uint32_t current_order; |
---|
200 | page_t * remaining_block; |
---|
201 | uint32_t current_size; |
---|
202 | |
---|
203 | #if DEBUG_PPM_ALLOC_PAGES |
---|
204 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
205 | if( DEBUG_PPM_ALLOC_PAGES < cycle ) |
---|
206 | printk("\n[DBG] in %s : thread %x enter for %d page(s) / cycle %d\n", |
---|
207 | __FUNCTION__ , CURRENT_THREAD , 1<<order, cycle ); |
---|
208 | #endif |
---|
209 | |
---|
210 | #if(DEBUG_PPM_ALLOC_PAGES & 0x1) |
---|
211 | if( DEBUG_PPM_ALLOC_PAGES < cycle ) |
---|
212 | ppm_print(); |
---|
213 | #endif |
---|
214 | |
---|
215 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
216 | |
---|
217 | assert( (order < CONFIG_PPM_MAX_ORDER) , __FUNCTION__ , |
---|
218 | "illegal order argument = %x\n" , order ); |
---|
219 | |
---|
220 | page_t * block = NULL; |
---|
221 | |
---|
222 | // take lock protecting free lists |
---|
223 | spinlock_lock( &ppm->free_lock ); |
---|
224 | |
---|
225 | // find a free block equal or larger to requested size |
---|
226 | for( current_order = order ; current_order < CONFIG_PPM_MAX_ORDER ; current_order ++ ) |
---|
227 | { |
---|
228 | if( !list_is_empty( &ppm->free_pages_root[current_order] ) ) |
---|
229 | { |
---|
230 | block = LIST_FIRST( &ppm->free_pages_root[current_order] , page_t , list ); |
---|
231 | list_unlink( &block->list ); |
---|
232 | break; |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | if( block == NULL ) // return failure |
---|
237 | { |
---|
238 | // release lock protecting free lists |
---|
239 | spinlock_unlock( &ppm->free_lock ); |
---|
240 | |
---|
241 | #if DEBUG_PPM_ALLOC_PAGES |
---|
242 | cycle = (uint32_t)hal_get_cycles(); |
---|
243 | if( DEBUG_PPM_ALLOC_PAGES < cycle ) |
---|
244 | printk("\n[DBG] in %s : thread %x cannot allocate %d page(s) at cycle %d\n", |
---|
245 | __FUNCTION__ , CURRENT_THREAD , 1<<order, cycle ); |
---|
246 | #endif |
---|
247 | |
---|
248 | return NULL; |
---|
249 | } |
---|
250 | |
---|
251 | // update free-lists after removing a block |
---|
252 | ppm->free_pages_nr[current_order] --; |
---|
253 | current_size = (1 << current_order); |
---|
254 | |
---|
255 | // split the removed block in smaller sub-blocks if required |
---|
256 | // and update the free-lists accordingly |
---|
257 | while( current_order > order ) |
---|
258 | { |
---|
259 | current_order --; |
---|
260 | current_size >>= 1; |
---|
261 | |
---|
262 | remaining_block = block + current_size; |
---|
263 | remaining_block->order = current_order; |
---|
264 | |
---|
265 | list_add_first( &ppm->free_pages_root[current_order] , &remaining_block->list ); |
---|
266 | ppm->free_pages_nr[current_order] ++; |
---|
267 | } |
---|
268 | |
---|
269 | // update page descriptor |
---|
270 | page_clear_flag( block , PG_FREE ); |
---|
271 | page_refcount_up( block ); |
---|
272 | block->order = order; |
---|
273 | |
---|
274 | // release lock protecting free lists |
---|
275 | spinlock_unlock( &ppm->free_lock ); |
---|
276 | |
---|
277 | #if DEBUG_PPM_ALLOC_PAGES |
---|
278 | cycle = (uint32_t)hal_get_cycles(); |
---|
279 | if( DEBUG_PPM_ALLOC_PAGES < cycle ) |
---|
280 | printk("\n[DBG] in %s : thread %x exit / %d page(s) allocated / ppn = %x / cycle %d\n", |
---|
281 | __FUNCTION__, CURRENT_THREAD, 1<<order, ppm_page2ppn(XPTR( local_cxy , block )), cycle ); |
---|
282 | #endif |
---|
283 | |
---|
284 | return block; |
---|
285 | |
---|
286 | } // end ppm_alloc_pages() |
---|
287 | |
---|
288 | |
---|
289 | //////////////////////////////////// |
---|
290 | void ppm_free_pages( page_t * page ) |
---|
291 | { |
---|
292 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
293 | |
---|
294 | #if DEBUG_PPM_FREE_PAGES |
---|
295 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
296 | if( DEBUG_PPM_FREE_PAGES < cycle ) |
---|
297 | printk("\n[DBG] in %s : thread %x enter for %d page(s) / cycle %d\n", |
---|
298 | __FUNCTION__ , CURRENT_THREAD , 1<<page->order , cycle ); |
---|
299 | #endif |
---|
300 | |
---|
301 | #if(DEBUG_PPM_FREE_PAGES & 0x1) |
---|
302 | if( DEBUG_PPM_FREE_PAGES < cycle ) |
---|
303 | ppm_print(); |
---|
304 | #endif |
---|
305 | |
---|
306 | // get lock protecting free_pages[] array |
---|
307 | spinlock_lock( &ppm->free_lock ); |
---|
308 | |
---|
309 | ppm_free_pages_nolock( page ); |
---|
310 | |
---|
311 | // release lock protecting free_pages[] array |
---|
312 | spinlock_unlock( &ppm->free_lock ); |
---|
313 | |
---|
314 | #if DEBUG_PPM_FREE_PAGES |
---|
315 | cycle = (uint32_t)hal_get_cycles(); |
---|
316 | if( DEBUG_PPM_FREE_PAGES < cycle ) |
---|
317 | printk("\n[DBG] in %s : thread %x exit / %d page(s) released / ppn = %x / cycle %d\n", |
---|
318 | __FUNCTION__, CURRENT_THREAD, 1<<page->order, ppm_page2ppn(XPTR(local_cxy , page)), cycle ); |
---|
319 | #endif |
---|
320 | |
---|
321 | } |
---|
322 | |
---|
323 | //////////////// |
---|
324 | void ppm_print( void ) |
---|
325 | { |
---|
326 | uint32_t order; |
---|
327 | list_entry_t * iter; |
---|
328 | page_t * page; |
---|
329 | |
---|
330 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
331 | |
---|
332 | // get lock protecting free lists |
---|
333 | spinlock_lock( &ppm->free_lock ); |
---|
334 | |
---|
335 | printk("\n*** PPM in cluster %x : %d pages ***\n", local_cxy , ppm->pages_nr ); |
---|
336 | |
---|
337 | for( order = 0 ; order < CONFIG_PPM_MAX_ORDER ; order++ ) |
---|
338 | { |
---|
339 | printk("- order = %d / free_pages = %d\t: ", |
---|
340 | order , ppm->free_pages_nr[order] ); |
---|
341 | |
---|
342 | LIST_FOREACH( &ppm->free_pages_root[order] , iter ) |
---|
343 | { |
---|
344 | page = LIST_ELEMENT( iter , page_t , list ); |
---|
345 | printk("%x," , page - ppm->pages_tbl ); |
---|
346 | } |
---|
347 | |
---|
348 | printk("\n"); |
---|
349 | } |
---|
350 | |
---|
351 | // release lock protecting free lists |
---|
352 | spinlock_unlock( &ppm->free_lock ); |
---|
353 | } |
---|
354 | |
---|
355 | /////////////////////////////////////// |
---|
356 | error_t ppm_assert_order( ppm_t * ppm ) |
---|
357 | { |
---|
358 | uint32_t order; |
---|
359 | list_entry_t * iter; |
---|
360 | page_t * page; |
---|
361 | |
---|
362 | for( order=0 ; order < CONFIG_PPM_MAX_ORDER ; order++ ) |
---|
363 | { |
---|
364 | if( list_is_empty( &ppm->free_pages_root[order] ) ) continue; |
---|
365 | |
---|
366 | LIST_FOREACH( &ppm->free_pages_root[order] , iter ) |
---|
367 | { |
---|
368 | page = LIST_ELEMENT( iter , page_t , list ); |
---|
369 | |
---|
370 | if( page->order != order ) return -1; |
---|
371 | } |
---|
372 | } |
---|
373 | |
---|
374 | return 0; |
---|
375 | } |
---|
376 | |
---|