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

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

style

File size: 7.3 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_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/////////////////////////////////////////////
50inline void * ppm_page2vaddr( page_t * page )
51{
52        ppm_t * ppm = &LOCAL_CLUSTER->ppm;
53        return ppm->vaddr_base + ((page - ppm->pages_tbl) << CONFIG_PPM_PAGE_SHIFT);
54}
55
56//////////////////////////////////////////////
57inline page_t * ppm_vaddr2page( void * vaddr )
58{
59        ppm_t * ppm = &LOCAL_CLUSTER->ppm;
60        return ppm->pages_tbl + (vaddr - ppm->vaddr_base);
61}
62
63//////////////////////////////////////////
64inline ppn_t ppm_page2ppn( page_t * page )
65{
66        ppm_t  * ppm = &LOCAL_CLUSTER->ppm;
67        return (ppn_t)( page - ppm->pages_tbl );
68}
69
70/////////////////////////////////////////
71inline page_t * ppm_ppn2page( ppn_t ppn )
72{
73        ppm_t  * ppm = &LOCAL_CLUSTER->ppm;
74        return &ppm->pages_tbl[ppn];
75}
76
77///////////////////////////////////////
78inline void * ppm_ppn2vaddr( ppn_t ppn )
79{
80        ppm_t  * ppm  = &LOCAL_CLUSTER->ppm;
81        return ppm->vaddr_base + (ppn << CONFIG_PPM_PAGE_SHIFT);
82}
83
84//////////////////////////////////////////
85inline ppn_t ppm_vaddr2ppn( void * vaddr )
86{
87        ppm_t  * ppm  = &LOCAL_CLUSTER->ppm;
88        return ( (ppm->vaddr_base - vaddr) >> CONFIG_PPM_PAGE_SHIFT );
89}
90
91
92///////////////////////////////////////////
93void ppm_free_pages_nolock( page_t * page )
94{
95        page_t   * buddy;            // searched buddy page descriptor
96        uint32_t   buddy_index;      // buddy page index
97        page_t   * current;          // current (merged) page descriptor
98        uint32_t   current_index;    // current (merged) page index
99        uint32_t   current_order;    // current (merged) page order
100
101        ppm_t    * ppm         = &LOCAL_CLUSTER->ppm;
102        page_t   * pages_tbl   = ppm->pages_tbl;
103
104        // update released page descriptor flags
105        page_set_flag( page , PG_FREE );
106
107        // search the buddy page descriptor
108        // - merge with current page descriptor if found
109        // - exit to release the current page descriptor if not found
110        current       = page ,
111        current_index = (uint32_t)(page - ppm->pages_tbl);
112        for( current_order = page->order ;
113             current_order < CONFIG_PPM_MAX_ORDER ;
114             current_order++ )
115        {
116                buddy_index = current_index ^ (1 << current_order);
117                buddy       = pages_tbl + buddy_index;
118
119                if( !page_is_flag( buddy , PG_FREE ) || (buddy->order != current_order) ) break;
120
121                // remove buddy from free list
122                list_unlink( &buddy->list );
123                ppm->free_pages_nr[current_order] --;
124
125                // merge buddy with current
126                buddy->order = 0;
127                current_index &= buddy_index;
128        }
129
130        // update merged page descriptor order
131        current        = pages_tbl + current_index;
132        current->order = current_order;
133
134        // insert current in free list
135        list_add_first( &ppm->free_pages_root[current_order] , &current->list );
136        ppm->free_pages_nr[current_order] ++;
137}
138
139////////////////////////////////////////////
140page_t * ppm_alloc_pages( uint32_t   order )
141{
142        uint32_t   current_order;
143        page_t   * remaining_block;
144        uint32_t   current_size;
145
146        ppm_t    * ppm = &LOCAL_CLUSTER->ppm;
147
148        assert( (order < CONFIG_PPM_MAX_ORDER) , __FUNCTION__ , "illegal order argument" );
149
150        page_t * block = NULL;
151
152        ppm_dmsg("\n[INFO] %s : enters / order = %d\n",
153                 __FUNCTION__ , order );
154
155        // take lock protecting free lists
156        spinlock_lock( &ppm->free_lock );
157
158        // find a free block equal or larger to requested size
159        for( current_order = order ; current_order < CONFIG_PPM_MAX_ORDER ; current_order ++ )
160        {
161                if( !list_is_empty( &ppm->free_pages_root[current_order] ) )
162                {
163                        block = LIST_FIRST( &ppm->free_pages_root[current_order] , page_t , list );
164                        list_unlink( &block->list );
165                        break;
166                }
167        }
168
169        if( block == NULL ) // return failure
170        {
171                // release lock protecting free lists
172                spinlock_unlock( &ppm->free_lock );
173
174                return NULL;
175        }
176
177        // update free-lists after removing a block
178        ppm->free_pages_nr[current_order] --;
179        current_size = (1 << current_order);
180
181        // split the removed block in smaller sub-blocks if required
182        // and update the free-lists accordingly
183        while( current_order > order )
184        {
185                current_order --;
186                current_size >>= 1;
187
188                remaining_block = block + current_size;
189                remaining_block->order = current_order;
190
191                list_add_first( &ppm->free_pages_root[current_order] , &remaining_block->list );
192                ppm->free_pages_nr[current_order] ++;
193        }
194
195        // update page descriptor
196        page_clear_flag( block , PG_FREE );
197        page_refcount_up( block );
198        block->order = order;
199
200        // release lock protecting free lists
201        spinlock_unlock( &ppm->free_lock );
202
203        ppm_dmsg("\n[INFO] %s : base = %x / order = %d\n",
204                 __FUNCTION__ , (uint32_t)ppm_page2base( block ) , order );
205
206        return block;
207}
208
209
210////////////////////////////////////
211void ppm_free_pages( page_t * page )
212{
213        ppm_t * ppm = &LOCAL_CLUSTER->ppm;
214
215        // get lock protecting free_pages[] array
216        spinlock_lock( &ppm->free_lock );
217
218        ppm_free_pages_nolock( page );
219
220        // release lock protecting free_pages[] array
221        spinlock_unlock( &ppm->free_lock );
222}
223
224////////////////////////////
225void ppm_print( ppm_t * ppm,
226                char  * string )
227{
228        uint32_t       order;
229        list_entry_t * iter;
230        page_t       * page;
231
232        // get lock protecting free lists
233        spinlock_lock( &ppm->free_lock );
234
235        printk("\n***  PPM in cluster %x : %d pages / &pages_tbl = %x / vaddr_base = %x ***\n",
236               local_cxy , ppm->pages_nr , (intptr_t)ppm->pages_tbl , (intptr_t)ppm->vaddr_base );
237
238        for( order = 0 ; order < CONFIG_PPM_MAX_ORDER ; order++ )
239        {
240                printk("- order = %d / free_pages = %d  [",
241                       order , ppm->free_pages_nr[order] );
242
243                LIST_FOREACH( &ppm->free_pages_root[order] , iter )
244                {
245                        page = LIST_ELEMENT( iter , page_t , list );
246                        printk("%d," , page - ppm->pages_tbl );
247                }
248
249                printk("]\n", NULL );
250        }
251
252        // release lock protecting free lists
253        spinlock_unlock( &ppm->free_lock );
254}
255
256///////////////////////////////////////
257error_t ppm_assert_order( ppm_t * ppm )
258{
259        uint32_t       order;
260        list_entry_t * iter;
261        page_t       * page;
262
263        for(order=0; order < CONFIG_PPM_MAX_ORDER; order++)
264        {
265                if( list_is_empty( &ppm->free_pages_root[order] ) ) continue;
266
267                LIST_FOREACH( &ppm->free_pages_root[order] , iter )
268                {
269                        page = LIST_ELEMENT( iter , page_t , list );
270
271                        if( page->order != order )  return -1;
272                }
273        }
274
275        return 0;
276}
277
Note: See TracBrowser for help on using the repository browser.