source: trunk/kernel/mm/page.c @ 22

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

cosmetic & typos again

File size: 6.7 KB
RevLine 
[1]1/*
2 * page.c - physical page related operations 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
25#include <hal_types.h>
26#include <hal_special.h>
27#include <hal_atomic.h>
28#include <list.h>
29#include <xlist.h>
30#include <memcpy.h>
31#include <thread.h>
32#include <scheduler.h>
33#include <cluster.h>
34#include <ppm.h>
35#include <mapper.h>
36#include <printk.h>
37#include <vfs.h>
38#include <process.h>
39#include <page.h>
40
41////////////////////////////////////////
42inline void page_init( page_t   * page )
43{
44        page->flags    = 0;
45        page->order    = 0;
46        page->index    = 0;
47        page->mapper   = NULL;
48        page->private  = 0;
[22]49        page->refcount = 0;
[1]50        spinlock_init( &page->lock );
51        list_entry_init( &page->list );
52}
53
54////////////////////////////////////////////
55inline void page_set_flag( page_t   * page,
56                           uint16_t   value )
57{
[22]58        hal_atomic_or( (uint32_t *)&page->flags , (uint32_t)value );
[1]59}
60
61//////////////////////////////////////////////
62inline void page_clear_flag( page_t   * page,
63                             uint16_t   value )
64{
[22]65        hal_atomic_and( (uint32_t *)&page->flags , ~((uint32_t)value) );
[1]66}
67
68//////////////////////////////////////////////
69inline bool_t page_is_flag( page_t   * page,
70                            uint16_t   value )
71{
[22]72        return (bool_t)(page->flags & value);
[1]73}
74
75//////////////////////////////////////
76bool_t page_do_dirty( page_t * page )
77{
78        bool_t done = false;
79
[22]80        ppm_t * ppm = &LOCAL_CLUSTER->ppm;
[1]81
[22]82        // lock the PPM dirty_list
[1]83        spinlock_lock( &ppm->dirty_lock );
84
85        if( !page_is_flag( page , PG_DIRTY ) )
86        {
[22]87                // set dirty flag in page descriptor
[1]88                page_set_flag( page , PG_DIRTY );
[18]89
[22]90                // register page in PPM dirty list
[1]91                list_add_first( &ppm->dirty_root , &page->list );
92                done = true;
93        }
94
[22]95        // unlock the PPM dirty_list
[1]96        spinlock_unlock( &ppm->dirty_lock );
[18]97
[1]98        return done;
99}
100
101////////////////////////////////////////
102bool_t page_undo_dirty( page_t * page )
103{
104        bool_t done = false;
105
[22]106        ppm_t * ppm = &LOCAL_CLUSTER->ppm;
[1]107
[22]108        // lock the dirty_list
[1]109        spinlock_lock( &ppm->dirty_lock );
110
111        if( page_is_flag( page , PG_DIRTY) )
112        {
[22]113                // clear dirty flag in page descriptor
[1]114                page_clear_flag( page , PG_DIRTY );
115
[22]116                // remove page from PPM dirty list
[1]117                list_unlink( &page->list );
118                done = true;
119        }
120
[22]121        // unlock the dirty_list
[1]122        spinlock_unlock( &ppm->dirty_lock );
123
124        return done;
125}
126
127/////////////////////
[18]128void sync_all_pages()
[1]129{
130        page_t   * page;
131        mapper_t * mapper;
[22]132        uint32_t   index;
133        ppm_t    * ppm = &LOCAL_CLUSTER->ppm;
[1]134
[22]135        // lock the dirty_list
[1]136        spinlock_lock( &ppm->dirty_lock );
137
[18]138        while( !list_is_empty( &ppm->dirty_root ) )
[1]139        {
140                page = LIST_FIRST( &ppm->dirty_root ,  page_t , list );
141
[22]142                // unlock the dirty_list
143                spinlock_unlock( &ppm->dirty_lock );
[1]144
145                mapper = page->mapper;
[22]146                index  = page->index;
[18]147
[22]148                // lock the page
[1]149                page_lock( page );
150
[22]151                // sync the page
[1]152                mapper_sync_page( mapper , index , page );
153
[22]154                // unlock the page
[1]155                page_unlock( page );
156
[22]157                // lock the dirty_list
158                spinlock_lock( &ppm->dirty_lock );
[1]159        }
160
[22]161        // unlock the dirty_list
[1]162        spinlock_unlock( &ppm->dirty_lock );
163
[22]164}
[1]165
166///////////////////////////////
167void page_lock( page_t * page )
168{
[22]169        // take the spinlock protecting the PG_LOCKED flag
[1]170        spinlock_lock( &page->lock );
171
172        if( page_is_flag( page , PG_LOCKED ) )  // page is already locked
173        {
[22]174                // get pointer on calling thread
175                thread_t * thread = CURRENT_THREAD;
[1]176
[22]177                // register thread in the page waiting queue
178                xlist_add_last( XPTR( local_cxy , &page->wait_root ),
179                                XPTR( local_cxy , &thread->wait_list ) );
[1]180
[22]181                // release the spinlock
[1]182                spinlock_unlock( &page->lock );
183
[22]184                // deschedule the calling thread
185                thread_block( thread , THREAD_BLOCKED_PAGE );
[1]186                sched_yield();
187        }
188        else                                    // page is not locked
189        {
[22]190                // set the PG_LOCKED flag
[1]191                page_set_flag( page , PG_LOCKED );
192
[22]193                // release the spinlock
[1]194                spinlock_unlock( &page->lock );
195        }
196}
197
198/////////////////////////////////
199void page_unlock( page_t * page )
200{
[22]201        // take the spinlock protecting the PG_LOCKED flag
[1]202        spinlock_lock( &page->lock );
[18]203
[22]204        // check the page waiting list
[1]205        bool_t is_empty = xlist_is_empty( XPTR( local_cxy , &page->wait_root ) );
206
207        if( is_empty == false )    // at least one waiting thread => resume it
[22]208        {
209                // get an extended pointer on the first waiting thread
210                xptr_t root_xp   = XPTR( local_cxy , &page->wait_root );
211                xptr_t thread_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list );
[1]212
[22]213                // reactivate the first waiting thread
214                thread_unblock( thread_xp , THREAD_BLOCKED_PAGE );
215        }
[1]216        else                      // no waiting thread => clear the PG_LOCKED flag
217        {
[22]218                page_clear_flag( page , PG_LOCKED );
219        }
[1]220
[22]221        // release the spinlock
[1]222        spinlock_unlock( &page->lock );
223}
224
225////////////////////////////////////////////
226inline void page_refcount_up( page_t *page )
227{
[22]228        hal_atomic_inc( &page->refcount );
[1]229}
230
231//////////////////////////////////////////////
232inline void page_refcount_down( page_t *page )
233{
[22]234        hal_atomic_dec( &page->refcount );
[1]235}
236
237//////////////////////////////
238void page_copy(  page_t * dst,
239                 page_t * src )
240{
241        uint32_t  size;
242        void    * src_base;
243        void    * dst_base;
[18]244
[1]245        if( dst->order != src->order )
[22]246        {
247                printk("\n[PANIC] in %s : src size != dst size\n", __FUNCTION__ );
248                hal_core_sleep();
249        }
[1]250
251        size = (1 << dst->order) * CONFIG_PPM_PAGE_SIZE;
252        src_base = ppm_page2base( src );
253        dst_base = ppm_page2base( dst );
254
255        memcpy( dst_base , src_base , size );
256}
257
258///////////////////////////////
259void page_zero( page_t * page )
260{
261        uint32_t   size;
262        void     * base;
263
264        size = (1 << page->order) * CONFIG_PPM_PAGE_SIZE;
[7]265        base = ppm_page2base( page );
[1]266
[7]267// kmem_print_kcm_table();
268
269// printk("\n@@@ in page_zero : size = %x / base = %x / kcm_tbl = %x\n",
270//        size , (uint32_t)base , (uint32_t)(&LOCAL_CLUSTER->kcm_tbl[0] ) );
271
[18]272        memset( base , 0 , size );
[7]273
274// kmem_print_kcm_table();
275
[1]276}
277
278////////////////////////////////
279void page_print( page_t * page )
280{
281        printk("*** Page %d : base = %x / flags = %x / order = %d / count = %d\n",
[18]282                page->index,
[1]283                ppm_page2base( page ),
[18]284                page->flags,
285                page->order,
[1]286                page->refcount );
287}
288
Note: See TracBrowser for help on using the repository browser.