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

Last change on this file since 605 was 585, checked in by alain, 6 years ago

Introduce two separate vmm_handle_page_fault() & vmm_handle_cow() functions in vmm.c

File size: 12.7 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,2018)
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 <dqdt.h>
33#include <busylock.h>
34#include <queuelock.h>
35#include <thread.h>
36#include <cluster.h>
37#include <kmem.h>
38#include <process.h>
39#include <mapper.h>
40#include <ppm.h>
41
42////////////////////////////////////////////////////////////////////////////////////////
43//     functions to  translate [ page <-> base <-> ppn ]
44////////////////////////////////////////////////////////////////////////////////////////
45
46////////////////////////////////////////////////
47inline bool_t ppm_page_is_valid( page_t * page )
48{
49        ppm_t    * ppm  = &LOCAL_CLUSTER->ppm;
50        uint32_t   pgnr = (uint32_t)( page - ppm->pages_tbl );
51        return (pgnr <= ppm->pages_nr);
52}
53
54/////////////////////////////////////////////
55inline xptr_t ppm_page2base( xptr_t page_xp )
56{
57        ppm_t  * ppm      = &LOCAL_CLUSTER->ppm;
58
59    cxy_t    page_cxy = GET_CXY( page_xp );
60    page_t * page_ptr = GET_PTR( page_xp );
61
62   void   * base_ptr = ppm->vaddr_base + 
63                       ((page_ptr - ppm->pages_tbl)<<CONFIG_PPM_PAGE_SHIFT);
64
65        return XPTR( page_cxy , base_ptr );
66
67} // end ppm_page2base()
68
69/////////////////////////////////////////////
70inline xptr_t ppm_base2page( xptr_t base_xp )
71{
72        ppm_t  * ppm = &LOCAL_CLUSTER->ppm;
73
74    cxy_t    base_cxy = GET_CXY( base_xp );
75    void   * base_ptr = GET_PTR( base_xp );
76
77        page_t * page_ptr = ppm->pages_tbl + 
78                        ((base_ptr - ppm->vaddr_base)>>CONFIG_PPM_PAGE_SHIFT);
79
80        return XPTR( base_cxy , page_ptr );
81
82}  // end ppm_base2page()
83
84
85
86///////////////////////////////////////////
87inline ppn_t ppm_page2ppn( xptr_t page_xp )
88{
89        ppm_t  * ppm      = &LOCAL_CLUSTER->ppm;
90
91    cxy_t    page_cxy = GET_CXY( page_xp );
92    page_t * page_ptr = GET_PTR( page_xp );
93
94    paddr_t  paddr    = PADDR( page_cxy , (page_ptr - ppm->pages_tbl)<<CONFIG_PPM_PAGE_SHIFT );
95
96    return (ppn_t)(paddr >> CONFIG_PPM_PAGE_SHIFT);
97
98}  // end hal_page2ppn()
99
100///////////////////////////////////////
101inline xptr_t ppm_ppn2page( ppn_t ppn )
102{
103        ppm_t   * ppm  = &LOCAL_CLUSTER->ppm;
104
105    paddr_t  paddr = ((paddr_t)ppn) << CONFIG_PPM_PAGE_SHIFT;
106
107    cxy_t    cxy   = CXY_FROM_PADDR( paddr );
108    lpa_t    lpa   = LPA_FROM_PADDR( paddr );
109
110    return XPTR( cxy , &ppm->pages_tbl[lpa>>CONFIG_PPM_PAGE_SHIFT] );
111
112}  // end hal_ppn2page
113
114
115
116///////////////////////////////////////
117inline xptr_t ppm_ppn2base( ppn_t ppn )
118{
119        ppm_t  * ppm   = &LOCAL_CLUSTER->ppm;
120   
121    paddr_t  paddr = ((paddr_t)ppn) << CONFIG_PPM_PAGE_SHIFT;
122
123    cxy_t    cxy   = CXY_FROM_PADDR( paddr );
124    lpa_t    lpa   = LPA_FROM_PADDR( paddr );
125
126        return XPTR( cxy , (void *)ppm->vaddr_base + lpa );
127
128}  // end ppm_ppn2base()
129
130///////////////////////////////////////////
131inline ppn_t ppm_base2ppn( xptr_t base_xp )
132{
133        ppm_t  * ppm      = &LOCAL_CLUSTER->ppm;
134
135    cxy_t    base_cxy = GET_CXY( base_xp );
136    void   * base_ptr = GET_PTR( base_xp );
137
138    paddr_t  paddr    = PADDR( base_cxy , (base_ptr - ppm->vaddr_base) );
139
140    return (ppn_t)(paddr >> CONFIG_PPM_PAGE_SHIFT);
141
142}  // end ppm_base2ppn()
143
144
145////////////////////////////////////////////////////////////////////////////////////////
146//     functions to  allocate / release  physical pages
147////////////////////////////////////////////////////////////////////////////////////////
148
149///////////////////////////////////////////
150void ppm_free_pages_nolock( page_t * page )
151{
152        page_t   * buddy;            // searched buddy page descriptor
153        uint32_t   buddy_index;      // buddy page index
154        page_t   * current;          // current (merged) page descriptor
155        uint32_t   current_index;    // current (merged) page index
156        uint32_t   current_order;    // current (merged) page order
157
158        ppm_t    * ppm         = &LOCAL_CLUSTER->ppm;
159        page_t   * pages_tbl   = ppm->pages_tbl;
160
161        assert( !page_is_flag( page , PG_FREE ) ,
162    "page already released : ppn = %x\n" , ppm_page2ppn(XPTR(local_cxy,page)) );
163
164        assert( !page_is_flag( page , PG_RESERVED ) ,
165    "reserved page : ppn = %x\n" , ppm_page2ppn(XPTR(local_cxy,page)) );
166
167        // update released page descriptor flags
168        page_set_flag( page , PG_FREE );
169
170        // search the buddy page descriptor
171        // - merge with current page descriptor if found
172        // - exit to release the current page descriptor if not found
173        current       = page ,
174        current_index = (uint32_t)(page - ppm->pages_tbl);
175        for( current_order = page->order ;
176             current_order < CONFIG_PPM_MAX_ORDER ;
177             current_order++ )
178        {
179                buddy_index = current_index ^ (1 << current_order);
180                buddy       = pages_tbl + buddy_index;
181
182                if( !page_is_flag( buddy , PG_FREE ) || (buddy->order != current_order) ) break;
183
184                // remove buddy from free list
185                list_unlink( &buddy->list );
186                ppm->free_pages_nr[current_order] --;
187
188                // merge buddy with current
189                buddy->order = 0;
190                current_index &= buddy_index;
191        }
192
193        // update merged page descriptor order
194        current        = pages_tbl + current_index;
195        current->order = current_order;
196
197        // insert current in free list
198        list_add_first( &ppm->free_pages_root[current_order] , &current->list );
199        ppm->free_pages_nr[current_order] ++;
200
201}  // end ppm_free_pages_nolock()
202
203////////////////////////////////////////////
204page_t * ppm_alloc_pages( uint32_t   order )
205{
206        uint32_t   current_order;
207        page_t   * remaining_block;
208        uint32_t   current_size;
209
210#if DEBUG_PPM_ALLOC_PAGES
211uint32_t cycle = (uint32_t)hal_get_cycles();
212if( DEBUG_PPM_ALLOC_PAGES < cycle )
213printk("\n[DBG] in %s : thread %x in process %x enter for %d page(s) / cycle %d\n",
214__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, 1<<order, cycle );
215#endif
216
217#if(DEBUG_PPM_ALLOC_PAGES & 0x1)
218if( DEBUG_PPM_ALLOC_PAGES < cycle )
219ppm_print();
220#endif
221
222        ppm_t    * ppm = &LOCAL_CLUSTER->ppm;
223
224        assert( (order < CONFIG_PPM_MAX_ORDER) ,
225    "illegal order argument = %x\n" , order );
226
227        page_t * block = NULL; 
228
229        // take lock protecting free lists
230        busylock_acquire( &ppm->free_lock );
231
232        // find a free block equal or larger to requested size
233        for( current_order = order ; current_order < CONFIG_PPM_MAX_ORDER ; current_order ++ )
234        {
235                if( !list_is_empty( &ppm->free_pages_root[current_order] ) )
236                {
237                        block = LIST_FIRST( &ppm->free_pages_root[current_order] , page_t , list );
238                        list_unlink( &block->list );
239                        break;
240                }
241        }
242
243        if( block == NULL ) // return failure
244        {
245                // release lock protecting free lists
246                busylock_release( &ppm->free_lock );
247
248#if DEBUG_PPM_ALLOC_PAGES
249cycle = (uint32_t)hal_get_cycles();
250if( DEBUG_PPM_ALLOC_PAGES < cycle )
251printk("\n[DBG] in %s : thread %x in process %x cannot allocate %d page(s) / cycle %d\n",
252__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, 1<<order, cycle );
253#endif
254
255                return NULL;
256        }
257
258        // update free-lists after removing a block
259        ppm->free_pages_nr[current_order] --;
260        current_size = (1 << current_order);
261
262        // split the removed block in smaller sub-blocks if required
263        // and update the free-lists accordingly
264        while( current_order > order )
265        {
266                current_order --;
267                current_size >>= 1;
268
269                remaining_block = block + current_size;
270                remaining_block->order = current_order;
271
272                list_add_first( &ppm->free_pages_root[current_order] , &remaining_block->list );
273                ppm->free_pages_nr[current_order] ++;
274        }
275
276        // update page descriptor
277        page_clear_flag( block , PG_FREE );
278        page_refcount_up( block );
279        block->order = order;
280
281        // release lock protecting free lists
282        busylock_release( &ppm->free_lock );
283
284    // update DQDT
285    dqdt_increment_pages( order );
286
287#if DEBUG_PPM_ALLOC_PAGES
288cycle = (uint32_t)hal_get_cycles();
289if( DEBUG_PPM_ALLOC_PAGES < cycle )
290printk("\n[DBG] in %s : thread %x in process %x exit for %d page(s) / ppn = %x / cycle %d\n",
291__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, 
2921<<order, ppm_page2ppn(XPTR( local_cxy , block )), cycle );
293#endif
294
295        return block;
296
297}  // end ppm_alloc_pages()
298
299
300////////////////////////////////////
301void ppm_free_pages( page_t * page )
302{
303        ppm_t * ppm = &LOCAL_CLUSTER->ppm;
304
305#if DEBUG_PPM_FREE_PAGES
306uint32_t cycle = (uint32_t)hal_get_cycles();
307if( DEBUG_PPM_FREE_PAGES < cycle )
308printk("\n[DBG] in %s : thread %x in process %x enter for %d page(s) / ppn %x / cycle %d\n",
309__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, 
3101<<page->order, ppm_page2ppn(XPTR(local_cxy , page)), cycle );
311#endif
312
313#if(DEBUG_PPM_FREE_PAGES & 0x1)
314if( DEBUG_PPM_FREE_PAGES < cycle )
315ppm_print();
316#endif
317
318        // get lock protecting free_pages[] array
319        busylock_acquire( &ppm->free_lock );
320
321        ppm_free_pages_nolock( page );
322
323        // release lock protecting free_pages[] array
324        busylock_release( &ppm->free_lock );
325
326    // update DQDT
327    dqdt_decrement_pages( page->order );
328
329#if DEBUG_PPM_FREE_PAGES
330cycle = (uint32_t)hal_get_cycles();
331if( DEBUG_PPM_FREE_PAGES < cycle )
332printk("\n[DBG] in %s : thread %x in process %x exit for %d page(s) / ppn %x / cycle %d\n",
333__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, 
3341<<page->order, ppm_page2ppn(XPTR(local_cxy , page)), cycle );
335#endif
336
337}  // end ppm_free_pages()
338
339//////////////////////
340void ppm_print( void )
341{
342        uint32_t       order;
343        list_entry_t * iter;
344        page_t       * page;
345
346    ppm_t * ppm = &LOCAL_CLUSTER->ppm;
347
348        // get lock protecting free lists
349        busylock_acquire( &ppm->free_lock );
350
351        printk("\n***  PPM in cluster %x : %d pages ***\n", local_cxy , ppm->pages_nr );
352
353        for( order = 0 ; order < CONFIG_PPM_MAX_ORDER ; order++ )
354        {
355                printk("- order = %d / free_pages = %d\t: ",
356                       order , ppm->free_pages_nr[order] );
357
358                LIST_FOREACH( &ppm->free_pages_root[order] , iter )
359                {
360                        page = LIST_ELEMENT( iter , page_t , list );
361                        printk("%x," , page - ppm->pages_tbl );
362                }
363
364                printk("\n");
365        }
366
367        // release lock protecting free lists
368        busylock_release( &ppm->free_lock );
369}
370
371///////////////////////////////////////
372error_t ppm_assert_order( ppm_t * ppm )
373{
374        uint32_t       order;
375        list_entry_t * iter;
376        page_t       * page;
377
378        for( order=0 ; order < CONFIG_PPM_MAX_ORDER ; order++ )
379        {
380                if( list_is_empty( &ppm->free_pages_root[order] ) ) continue;
381
382                LIST_FOREACH( &ppm->free_pages_root[order] , iter )
383                {
384                        page = LIST_ELEMENT( iter , page_t , list );
385                        if( page->order != order )  return -1;
386                }
387        }
388
389        return 0;
390}
391
392
393//////////////////////////////////////////////////////////////////////////////////////
394//     functions to handle  dirty physical pages
395//////////////////////////////////////////////////////////////////////////////////////
396
397/////////////////////////////////////////
398bool_t ppm_page_do_dirty( page_t * page )
399{
400        bool_t done = false;
401
402        ppm_t * ppm = &LOCAL_CLUSTER->ppm;
403
404        // lock the PPM dirty_list
405        queuelock_acquire( &ppm->dirty_lock );
406
407        if( !page_is_flag( page , PG_DIRTY ) )
408        {
409                // set dirty flag in page descriptor
410                page_set_flag( page , PG_DIRTY );
411
412                // register page in PPM dirty list
413                list_add_first( &ppm->dirty_root , &page->list );
414                done = true;
415        }
416
417        // unlock the PPM dirty_list
418        queuelock_release( &ppm->dirty_lock );
419
420        return done;
421}
422
423///////////////////////////////////////////
424bool_t ppm_page_undo_dirty( page_t * page )
425{
426        bool_t done = false;
427
428        ppm_t * ppm = &LOCAL_CLUSTER->ppm;
429
430        // lock the dirty_list
431        queuelock_acquire( &ppm->dirty_lock );
432
433        if( page_is_flag( page , PG_DIRTY) )
434        {
435                // clear dirty flag in page descriptor
436                page_clear_flag( page , PG_DIRTY );
437
438                // remove page from PPM dirty list
439                list_unlink( &page->list );
440                done = true;
441        }
442
443        // unlock the dirty_list
444        queuelock_release( &ppm->dirty_lock );
445
446        return done;
447}
448
449///////////////////////////////
450void ppm_sync_all_pages( void )
451{
452        page_t   * page;
453        ppm_t    * ppm = &LOCAL_CLUSTER->ppm;
454
455        // get the PPM dirty_list lock
456        queuelock_acquire( &ppm->dirty_lock );
457
458        while( !list_is_empty( &ppm->dirty_root ) )
459        {
460                page = LIST_FIRST( &ppm->dirty_root ,  page_t , list );
461
462                // get the page lock
463                remote_busylock_acquire( XPTR( local_cxy, &page->lock ) );
464
465                // sync the page
466                vfs_mapper_move_page( page , false );  // from mapper
467
468                // release the page lock
469                remote_busylock_release( XPTR( local_cxy , &page->lock ) );
470        }
471
472        // release the PPM dirty_list lock
473        queuelock_release( &ppm->dirty_lock );
474}
475
Note: See TracBrowser for help on using the repository browser.