source: trunk/kernel/mm/ppm.c @ 560

Last change on this file since 560 was 551, checked in by nicolas.van.phan@…, 6 years ago

Make locks before IDLE Init busy

File size: 10.0 KB
Line 
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////////////////////////////////////////////////
41inline 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/////////////////////////////////////////////
51inline 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/////////////////////////////////////////////
66inline 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///////////////////////////////////////////
83inline 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///////////////////////////////////////
97inline 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///////////////////////////////////////
113inline 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///////////////////////////////////////////
127inline 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///////////////////////////////////////////
143void 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 ) ,
155    "page already released : ppn = %x\n" , ppm_page2ppn(XPTR(local_cxy,page)) );
156
157        assert( !page_is_flag( page , PG_RESERVED ) ,
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] , &current->list );
192        ppm->free_pages_nr[current_order] ++;
193
194}  // end ppm_free_pages_nolock()
195
196////////////////////////////////////////////
197page_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
204uint32_t cycle = (uint32_t)hal_get_cycles();
205if( DEBUG_PPM_ALLOC_PAGES < cycle )
206printk("\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)
211if( DEBUG_PPM_ALLOC_PAGES < cycle )
212ppm_print();
213#endif
214
215        ppm_t    * ppm = &LOCAL_CLUSTER->ppm;
216
217        assert( (order < CONFIG_PPM_MAX_ORDER) ,
218    "illegal order argument = %x\n" , order );
219
220        page_t * block = NULL; 
221
222        // take lock protecting free lists
223        uint32_t       irq_state;
224        spinlock_lock_busy( &ppm->free_lock, &irq_state );
225
226        // find a free block equal or larger to requested size
227        for( current_order = order ; current_order < CONFIG_PPM_MAX_ORDER ; current_order ++ )
228        {
229                if( !list_is_empty( &ppm->free_pages_root[current_order] ) )
230                {
231                        block = LIST_FIRST( &ppm->free_pages_root[current_order] , page_t , list );
232                        list_unlink( &block->list );
233                        break;
234                }
235        }
236
237        if( block == NULL ) // return failure
238        {
239                // release lock protecting free lists
240                spinlock_unlock_busy( &ppm->free_lock, irq_state );
241
242#if DEBUG_PPM_ALLOC_PAGES
243cycle = (uint32_t)hal_get_cycles();
244if( DEBUG_PPM_ALLOC_PAGES < cycle )
245printk("\n[DBG] in %s : thread %x cannot allocate %d page(s) at cycle %d\n",
246__FUNCTION__ , CURRENT_THREAD , 1<<order, cycle );
247#endif
248
249                return NULL;
250        }
251
252        // update free-lists after removing a block
253        ppm->free_pages_nr[current_order] --;
254        current_size = (1 << current_order);
255
256        // split the removed block in smaller sub-blocks if required
257        // and update the free-lists accordingly
258        while( current_order > order )
259        {
260                current_order --;
261                current_size >>= 1;
262
263                remaining_block = block + current_size;
264                remaining_block->order = current_order;
265
266                list_add_first( &ppm->free_pages_root[current_order] , &remaining_block->list );
267                ppm->free_pages_nr[current_order] ++;
268        }
269
270        // update page descriptor
271        page_clear_flag( block , PG_FREE );
272        page_refcount_up( block );
273        block->order = order;
274
275        // release lock protecting free lists
276        spinlock_unlock_busy( &ppm->free_lock, irq_state );
277
278#if DEBUG_PPM_ALLOC_PAGES
279cycle = (uint32_t)hal_get_cycles();
280if( DEBUG_PPM_ALLOC_PAGES < cycle )
281printk("\n[DBG] in %s : thread %x exit / %d page(s) allocated / ppn = %x / cycle %d\n",
282__FUNCTION__, CURRENT_THREAD, 1<<order, ppm_page2ppn(XPTR( local_cxy , block )), cycle );
283#endif
284
285        return block;
286
287}  // end ppm_alloc_pages()
288
289
290////////////////////////////////////
291void ppm_free_pages( page_t * page )
292{
293        ppm_t * ppm = &LOCAL_CLUSTER->ppm;
294
295#if DEBUG_PPM_FREE_PAGES
296uint32_t cycle = (uint32_t)hal_get_cycles();
297if( DEBUG_PPM_FREE_PAGES < cycle )
298printk("\n[DBG] in %s : thread %x enter for %d page(s) / cycle %d\n",
299__FUNCTION__ , CURRENT_THREAD , 1<<page->order , cycle );
300#endif
301
302#if(DEBUG_PPM_FREE_PAGES & 0x1)
303if( DEBUG_PPM_FREE_PAGES < cycle )
304ppm_print();
305#endif
306
307        // get lock protecting free_pages[] array
308        spinlock_lock( &ppm->free_lock );
309
310        ppm_free_pages_nolock( page );
311
312        // release lock protecting free_pages[] array
313        spinlock_unlock( &ppm->free_lock );
314
315#if DEBUG_PPM_FREE_PAGES
316cycle = (uint32_t)hal_get_cycles();
317if( DEBUG_PPM_FREE_PAGES < cycle )
318printk("\n[DBG] in %s : thread %x exit / %d page(s) released / ppn = %x / cycle %d\n",
319__FUNCTION__, CURRENT_THREAD, 1<<page->order, ppm_page2ppn(XPTR(local_cxy , page)), cycle );
320#endif
321
322}
323
324////////////////
325void ppm_print( void )
326{
327        uint32_t       order;
328        list_entry_t * iter;
329        page_t       * page;
330
331    ppm_t * ppm = &LOCAL_CLUSTER->ppm;
332
333        // get lock protecting free lists
334        spinlock_lock( &ppm->free_lock );
335
336        printk("\n***  PPM in cluster %x : %d pages ***\n", local_cxy , ppm->pages_nr );
337
338        for( order = 0 ; order < CONFIG_PPM_MAX_ORDER ; order++ )
339        {
340                printk("- order = %d / free_pages = %d\t: ",
341                       order , ppm->free_pages_nr[order] );
342
343                LIST_FOREACH( &ppm->free_pages_root[order] , iter )
344                {
345                        page = LIST_ELEMENT( iter , page_t , list );
346                        printk("%x," , page - ppm->pages_tbl );
347                }
348
349                printk("\n");
350        }
351
352        // release lock protecting free lists
353        spinlock_unlock( &ppm->free_lock );
354}
355
356///////////////////////////////////////
357error_t ppm_assert_order( ppm_t * ppm )
358{
359        uint32_t       order;
360        list_entry_t * iter;
361        page_t       * page;
362
363        for( order=0 ; order < CONFIG_PPM_MAX_ORDER ; order++ )
364        {
365                if( list_is_empty( &ppm->free_pages_root[order] ) ) continue;
366
367                LIST_FOREACH( &ppm->free_pages_root[order] , iter )
368                {
369                        page = LIST_ELEMENT( iter , page_t , list );
370
371                        if( page->order != order )  return -1;
372                }
373        }
374
375        return 0;
376}
377
Note: See TracBrowser for help on using the repository browser.