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

Last change on this file since 459 was 457, checked in by alain, 6 years ago

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

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