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

Last change on this file since 513 was 492, checked in by viala@…, 6 years ago

Refactoring assert calling to conform with new assert macro.

Made with this command for the general case.
find ./kernel/ hal/ -name "*.c" | xargs sed -i -e '/assert(/ s/,[ ]*FUNCTION[ ]*,/,/'

And some done by hand.

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