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

Last change on this file since 417 was 407, checked in by alain, 7 years ago

First implementation of fork/exec.

File size: 8.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>
[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////////////////////////////////////////////////
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 );
56    page_t * page_ptr = (page_t *)GET_PTR( page_xp );
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 );
71    void   * base_ptr = (void *)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
[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 );
88    page_t * page_ptr = (page_t *)GET_PTR( page_xp );
89
90    paddr_t  paddr    = PADDR( page_cxy , (page_ptr - ppm->pages_tbl)<<CONFIG_PPM_PAGE_SHIFT );
91
92    return 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    = ppn << CONFIG_PPM_PAGE_SHIFT;
102
103    cxy_t    page_cxy = CXY_FROM_PADDR( paddr );
104    lpa_t    page_lpa = LPA_FROM_PADDR( paddr );
105
106    return XPTR( page_cxy , &ppm->pages_tbl[page_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    = ppn << CONFIG_PPM_PAGE_SHIFT;
118
119    cxy_t    page_cxy = CXY_FROM_PADDR( paddr );
120    lpa_t    page_lpa = LPA_FROM_PADDR( paddr );
121
122    void   * base_ptr = (void *)ppm->vaddr_base + (page_lpa & ~CONFIG_PPM_PAGE_SHIFT);
123 
124        return XPTR( page_cxy , base_ptr );
125
126}  // end ppm_ppn2base()
127
128///////////////////////////////////////////
129inline ppn_t ppm_base2ppn( xptr_t base_xp )
130{
131        ppm_t  * ppm      = &LOCAL_CLUSTER->ppm;
132
133    cxy_t    base_cxy = GET_CXY( base_xp );
134    void   * base_ptr = (void *)GET_PTR( base_xp );
135
136    paddr_t  paddr    = PADDR( base_cxy , (base_ptr - ppm->vaddr_base) );
137
138    return paddr >> CONFIG_PPM_PAGE_SHIFT;
139
140}  // end ppm_base2ppn()
141
142
143
144///////////////////////////////////////////
[50]145void ppm_free_pages_nolock( page_t * page )
[1]146{
[7]147        page_t   * buddy;            // searched buddy page descriptor
148        uint32_t   buddy_index;      // buddy page index
149        page_t   * current;          // current (merged) page descriptor
150        uint32_t   current_index;    // current (merged) page index
[50]151        uint32_t   current_order;    // current (merged) page order
[7]152
[160]153        ppm_t    * ppm         = &LOCAL_CLUSTER->ppm;
154        page_t   * pages_tbl   = ppm->pages_tbl;
[1]155
[407]156        assert( !page_is_flag( page , PG_FREE ) , __FUNCTION__ , 
157    "page already released : ppn = %x\n" , ppm_page2ppn(XPTR(local_cxy,page)) );
[177]158
[407]159        assert( !page_is_flag( page , PG_RESERVED ) , __FUNCTION__ , 
160    "reserved page : ppn = %x\n" , ppm_page2ppn(XPTR(local_cxy,page)) );
161
[160]162        // update released page descriptor flags
[1]163        page_set_flag( page , PG_FREE );
164
[160]165        // search the buddy page descriptor
166        // - merge with current page descriptor if found
167        // - exit to release the current page descriptor if not found
168        current       = page ,
169        current_index = (uint32_t)(page - ppm->pages_tbl);
[18]170        for( current_order = page->order ;
[160]171             current_order < CONFIG_PPM_MAX_ORDER ;
172             current_order++ )
173        {
[7]174                buddy_index = current_index ^ (1 << current_order);
175                buddy       = pages_tbl + buddy_index;
[18]176
[7]177                if( !page_is_flag( buddy , PG_FREE ) || (buddy->order != current_order) ) break;
[1]178
[160]179                // remove buddy from free list
[7]180                list_unlink( &buddy->list );
[1]181                ppm->free_pages_nr[current_order] --;
[18]182
[160]183                // merge buddy with current
[7]184                buddy->order = 0;
185                current_index &= buddy_index;
[1]186        }
[18]187
[160]188        // update merged page descriptor order
[7]189        current        = pages_tbl + current_index;
190        current->order = current_order;
[1]191
[160]192        // insert current in free list
[7]193        list_add_first( &ppm->free_pages_root[current_order] , &current->list );
[1]194        ppm->free_pages_nr[current_order] ++;
[160]195}
[1]196
197////////////////////////////////////////////
198page_t * ppm_alloc_pages( uint32_t   order )
199{
[160]200        uint32_t   current_order;
[1]201        page_t   * remaining_block;
202        uint32_t   current_size;
203
[160]204        ppm_t    * ppm = &LOCAL_CLUSTER->ppm;
[1]205
[407]206        assert( (order < CONFIG_PPM_MAX_ORDER) , __FUNCTION__ ,
207    "illegal order argument = %x\n" , order );
[1]208
[406]209        page_t * block = NULL; 
[1]210
[407]211        ppm_dmsg("\n[DBG] %s : enters / order = %d\n",
[160]212                 __FUNCTION__ , order );
[7]213
[160]214        // take lock protecting free lists
[1]215        spinlock_lock( &ppm->free_lock );
216
[160]217        // find a free block equal or larger to requested size
[1]218        for( current_order = order ; current_order < CONFIG_PPM_MAX_ORDER ; current_order ++ )
219        {
220                if( !list_is_empty( &ppm->free_pages_root[current_order] ) )
221                {
222                        block = LIST_FIRST( &ppm->free_pages_root[current_order] , page_t , list );
223                        list_unlink( &block->list );
224                        break;
225                }
226        }
227
228        if( block == NULL ) // return failure
229        {
[160]230                // release lock protecting free lists
231                spinlock_unlock( &ppm->free_lock );
[1]232
[160]233                return NULL;
234        }
[18]235
[160]236        // update free-lists after removing a block
[18]237        ppm->free_pages_nr[current_order] --;
[1]238        current_size = (1 << current_order);
239
[160]240        // split the removed block in smaller sub-blocks if required
241        // and update the free-lists accordingly
[1]242        while( current_order > order )
243        {
244                current_order --;
245                current_size >>= 1;
[18]246
[1]247                remaining_block = block + current_size;
248                remaining_block->order = current_order;
249
250                list_add_first( &ppm->free_pages_root[current_order] , &remaining_block->list );
251                ppm->free_pages_nr[current_order] ++;
252        }
[18]253
[160]254        // update page descriptor
255        page_clear_flag( block , PG_FREE );
[1]256        page_refcount_up( block );
257        block->order = order;
258
[160]259        // release lock protecting free lists
[1]260        spinlock_unlock( &ppm->free_lock );
[18]261
[407]262        ppm_dmsg("\n[DBG] %s : base = %x / order = %d\n",
[160]263                 __FUNCTION__ , (uint32_t)ppm_page2base( block ) , order );
[7]264
[1]265        return block;
[160]266}
[1]267
268
269////////////////////////////////////
270void ppm_free_pages( page_t * page )
271{
272        ppm_t * ppm = &LOCAL_CLUSTER->ppm;
[18]273
[160]274        // get lock protecting free_pages[] array
[1]275        spinlock_lock( &ppm->free_lock );
276
[18]277        ppm_free_pages_nolock( page );
[1]278
[160]279        // release lock protecting free_pages[] array
[1]280        spinlock_unlock( &ppm->free_lock );
281}
282
[7]283////////////////////////////
284void ppm_print( ppm_t * ppm,
285                char  * string )
[1]286{
287        uint32_t       order;
288        list_entry_t * iter;
289        page_t       * page;
290
[160]291        // get lock protecting free lists
[1]292        spinlock_lock( &ppm->free_lock );
293
[50]294        printk("\n***  PPM in cluster %x : %d pages / &pages_tbl = %x / vaddr_base = %x ***\n",
[407]295    local_cxy , ppm->pages_nr , (intptr_t)ppm->pages_tbl , (intptr_t)ppm->vaddr_base );
[18]296
[1]297        for( order = 0 ; order < CONFIG_PPM_MAX_ORDER ; order++ )
298        {
[7]299                printk("- order = %d / free_pages = %d  [",
[160]300                       order , ppm->free_pages_nr[order] );
[18]301
[1]302                LIST_FOREACH( &ppm->free_pages_root[order] , iter )
303                {
304                        page = LIST_ELEMENT( iter , page_t , list );
305                        printk("%d," , page - ppm->pages_tbl );
306                }
[18]307
[1]308                printk("]\n", NULL );
309        }
310
[160]311        // release lock protecting free lists
[1]312        spinlock_unlock( &ppm->free_lock );
[160]313}
[1]314
[53]315///////////////////////////////////////
316error_t ppm_assert_order( ppm_t * ppm )
[1]317{
318        uint32_t       order;
319        list_entry_t * iter;
320        page_t       * page;
[18]321
[407]322        for( order=0 ; order < CONFIG_PPM_MAX_ORDER ; order++ )
[1]323        {
324                if( list_is_empty( &ppm->free_pages_root[order] ) ) continue;
[18]325
[1]326                LIST_FOREACH( &ppm->free_pages_root[order] , iter )
327                {
328                        page = LIST_ELEMENT( iter , page_t , list );
329
[160]330                        if( page->order != order )  return -1;
[1]331                }
332        }
333
[160]334        return 0;
335}
[53]336
Note: See TracBrowser for help on using the repository browser.