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

Last change on this file since 73 was 53, checked in by alain, 7 years ago

Compilation OK pout TSAR

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