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

Last change on this file since 41 was 18, checked in by max@…, 7 years ago

cosmetic, and a few typos

File size: 10.0 KB
RevLine 
[1]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
[14]25#include <kernel_config.h>
[1]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////////////////////////////////////////////////
43inline 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////////////////////////////////////////////
51inline 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////////////////////////////////////////////
[18]58inline page_t * ppm_base2page( void * base )
[1]59{
60        ppm_t * ppm = &LOCAL_CLUSTER->ppm;
61        return (ppm->pages_tbl + (((uint32_t)base ) >> CONFIG_PPM_PAGE_SHIFT));
62}
63
64//////////////////////////////////////////
65inline 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/////////////////////////////////////////
72inline page_t * ppm_ppn2page( ppn_t ppn )
73{
74        ppm_t  * ppm  = &LOCAL_CLUSTER->ppm;
75        return &ppm->pages_tbl[ppn];
76}
77
78///////////////////////////////////////
79inline void * ppm_ppn2base( ppn_t ppn )
80{
81        return (void*)( ppn << CONFIG_PPM_PAGE_SHIFT );
82}
83
84////////////////////////////////////////
85inline ppn_t ppm_base2ppn( void * base )
86{
87        return (ppn_t)( (uint32_t)base >> CONFIG_PPM_PAGE_SHIFT );
88}
89
90//////////////////////////////////////////////////
91static void ppm_free_pages_nolock( page_t * page )
92{
[7]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
[1]99    ppm_t    * ppm         = &LOCAL_CLUSTER->ppm;
100    page_t   * pages_tbl   = ppm->pages_tbl;
101
[7]102    // update released page descriptor flags
[1]103        page_set_flag( page , PG_FREE );
104
[7]105    // search the buddy page descriptor
106    // - merge with current page descriptor if found
[18]107    // - exit to release the current page descriptor if not found
108    current       = page ,
[7]109    current_index = (uint32_t)(page - ppm->pages_tbl);
[18]110        for( current_order = page->order ;
[7]111         current_order < CONFIG_PPM_MAX_ORDER ;
112         current_order++ )
[1]113    {
[7]114                buddy_index = current_index ^ (1 << current_order);
115                buddy       = pages_tbl + buddy_index;
[18]116
[7]117                if( !page_is_flag( buddy , PG_FREE ) || (buddy->order != current_order) ) break;
[1]118
[18]119        // remove buddy from free list
[7]120                list_unlink( &buddy->list );
[1]121                ppm->free_pages_nr[current_order] --;
122        ppm->total_free_pages -= (1 << current_order);
[18]123
[7]124        // merge buddy with current
125                buddy->order = 0;
126                current_index &= buddy_index;
[1]127        }
[18]128
[7]129    // update merged page descriptor order
130        current        = pages_tbl + current_index;
131        current->order = current_order;
[1]132
[7]133    // insert current in free list
134        list_add_first( &ppm->free_pages_root[current_order] , &current->list );
[1]135        ppm->free_pages_nr[current_order] ++;
136    ppm->total_free_pages += (1 << current_order);
137
138}  // end ppm_free_pages_nolock()
139
[7]140//////////////////////////////
[1]141void 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        }
[7]160
[1]161    // initialize dirty_list as empty
162    list_root_init( &ppm->dirty_root );
163
[18]164    // initialize pointer on page descriptors array
[1]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
[18]176        // set pages numbers
[1]177        ppm->pages_nr      = pages_nr;
[7]178    ppm->pages_offset  = reserved_pages;
[1]179
180    // initialises all page descriptors in pages_tbl[]
181        for( i = 0 ; i < pages_nr ; i++ )
[18]182    {
[1]183        page_init( &ppm->pages_tbl[i] );
[7]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;
[18]188    }
[1]189
[18]190    // - set PG_RESERVED flag for reserved pages (kernel code & pages_tbl[])
[7]191    // - release all other pages to populate the free lists
[18]192        for( i = 0 ; i < reserved_pages ; i++)
[1]193    {
194        page_set_flag( &ppm->pages_tbl[i] , PG_RESERVED );
195    }
[7]196        for( i = reserved_pages ; i < pages_nr ; i++ )
197        {
198            ppm_free_pages_nolock( &ppm->pages_tbl[i] );
[1]199
[7]200        // TODO optimisation : decompose this enormous set of small pages
201        // to a set big pages with various order values
[1]202        }
[7]203
204    // check consistency
205    ppm_assert_order( ppm );
206
[1]207} // end ppm_init()
208
209////////////////////////////////////////////
210page_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
[7]218        assert( (ppm->signature == PPM_SIGNATURE) , __FUNCTION__ , "PPM non initialised" );
[1]219
[7]220        assert( (order < CONFIG_PPM_MAX_ORDER) , __FUNCTION__ , "illegal order argument" );
[1]221
222        page_t * block = NULL;
223
[7]224    ppm_dmsg("\n[INFO] %s : enters / order = %d\n",
225             __FUNCTION__ , order );
226
227#if( CONFIG_PPM_DEBUG )
228ppm_print( ppm , "before allocation" );
229#endif
230
[1]231    // take lock protecting free lists
232        spinlock_lock( &ppm->free_lock );
233
[7]234    // find a free block equal or larger to requested size
[1]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    }
[18]252
253    // update free-lists after removing a block
[1]254        ppm->total_free_pages -= (1 << current_order);
[18]255        ppm->free_pages_nr[current_order] --;
[1]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;
[18]264
[1]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        }
[18]272
[1]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 );
[18]280
[7]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
285ppm_print( ppm , "after allocation" );
286#endif
287
[1]288        return block;
289}  // end pmm_alloc-pages()
290
291
292////////////////////////////////////
293void ppm_free_pages( page_t * page )
294{
295        ppm_t * ppm = &LOCAL_CLUSTER->ppm;
[18]296
[1]297    // get lock protecting free_pages[] array
298        spinlock_lock( &ppm->free_lock );
299
[18]300        ppm_free_pages_nolock( page );
[1]301
302    // release lock protecting free_pages[] array
303        spinlock_unlock( &ppm->free_lock );
304}
305
[7]306////////////////////////////
307void ppm_print( ppm_t * ppm,
308                char  * string )
[1]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
[7]317        printk("\n***  PPM state in cluster %x %s : pages = %d / offset = %d / free = %d ***\n",
[18]318               local_cxy , string , ppm->pages_nr , ppm->pages_offset , ppm->total_free_pages );
319
[1]320        for( order = 0 ; order < CONFIG_PPM_MAX_ORDER ; order++ )
321        {
[7]322                printk("- order = %d / free_pages = %d  [",
[1]323               order , ppm->free_pages_nr[order] );
[18]324
[1]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                }
[18]330
[1]331                printk("]\n", NULL );
332        }
333
334    // release lock protecting free lists
335        spinlock_unlock( &ppm->free_lock );
336
[7]337}  // end ppm_print()
[18]338
[7]339//////////////////////////u/////////
340void ppm_assert_order( ppm_t * ppm )
[1]341{
342        uint32_t       order;
343        list_entry_t * iter;
344        page_t       * page;
[18]345
[1]346        for(order=0; order < CONFIG_PPM_MAX_ORDER; order++)
347        {
348                if( list_is_empty( &ppm->free_pages_root[order] ) ) continue;
[18]349
[1]350                LIST_FOREACH( &ppm->free_pages_root[order] , iter )
351                {
352                        page = LIST_ELEMENT( iter , page_t , list );
353
[7]354                        assert( (page->order == order) , __FUNCTION__ , "PPM inconsistency" );
[1]355                }
356        }
357
[7]358}  // end ppm_assert_order()
359
Note: See TracBrowser for help on using the repository browser.