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
Line 
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;
49        page->refcount = 0;
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{
58        hal_atomic_or( (uint32_t *)&page->flags , (uint32_t)value );
59}
60
61//////////////////////////////////////////////
62inline void page_clear_flag( page_t   * page,
63                             uint16_t   value )
64{
65        hal_atomic_and( (uint32_t *)&page->flags , ~((uint32_t)value) );
66}
67
68//////////////////////////////////////////////
69inline bool_t page_is_flag( page_t   * page,
70                            uint16_t   value )
71{
72        return (bool_t)(page->flags & value);
73}
74
75//////////////////////////////////////
76bool_t page_do_dirty( page_t * page )
77{
78        bool_t done = false;
79
80        ppm_t * ppm = &LOCAL_CLUSTER->ppm;
81
82        // lock the PPM dirty_list
83        spinlock_lock( &ppm->dirty_lock );
84
85        if( !page_is_flag( page , PG_DIRTY ) )
86        {
87                // set dirty flag in page descriptor
88                page_set_flag( page , PG_DIRTY );
89
90                // register page in PPM dirty list
91                list_add_first( &ppm->dirty_root , &page->list );
92                done = true;
93        }
94
95        // unlock the PPM dirty_list
96        spinlock_unlock( &ppm->dirty_lock );
97
98        return done;
99}
100
101////////////////////////////////////////
102bool_t page_undo_dirty( page_t * page )
103{
104        bool_t done = false;
105
106        ppm_t * ppm = &LOCAL_CLUSTER->ppm;
107
108        // lock the dirty_list
109        spinlock_lock( &ppm->dirty_lock );
110
111        if( page_is_flag( page , PG_DIRTY) )
112        {
113                // clear dirty flag in page descriptor
114                page_clear_flag( page , PG_DIRTY );
115
116                // remove page from PPM dirty list
117                list_unlink( &page->list );
118                done = true;
119        }
120
121        // unlock the dirty_list
122        spinlock_unlock( &ppm->dirty_lock );
123
124        return done;
125}
126
127/////////////////////
128void sync_all_pages()
129{
130        page_t   * page;
131        mapper_t * mapper;
132        uint32_t   index;
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                mapper = page->mapper;
146                index  = page->index;
147
148                // lock the page
149                page_lock( page );
150
151                // sync the page
152                mapper_sync_page( mapper , index , page );
153
154                // unlock the page
155                page_unlock( page );
156
157                // lock the dirty_list
158                spinlock_lock( &ppm->dirty_lock );
159        }
160
161        // unlock the dirty_list
162        spinlock_unlock( &ppm->dirty_lock );
163
164}
165
166///////////////////////////////
167void page_lock( page_t * page )
168{
169        // take the spinlock protecting the PG_LOCKED flag
170        spinlock_lock( &page->lock );
171
172        if( page_is_flag( page , PG_LOCKED ) )  // page is already locked
173        {
174                // get pointer on calling thread
175                thread_t * thread = CURRENT_THREAD;
176
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 ) );
180
181                // release the spinlock
182                spinlock_unlock( &page->lock );
183
184                // deschedule the calling thread
185                thread_block( thread , THREAD_BLOCKED_PAGE );
186                sched_yield();
187        }
188        else                                    // page is not locked
189        {
190                // set the PG_LOCKED flag
191                page_set_flag( page , PG_LOCKED );
192
193                // release the spinlock
194                spinlock_unlock( &page->lock );
195        }
196}
197
198/////////////////////////////////
199void page_unlock( page_t * page )
200{
201        // take the spinlock protecting the PG_LOCKED flag
202        spinlock_lock( &page->lock );
203
204        // check the page waiting list
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
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 );
212
213                // reactivate the first waiting thread
214                thread_unblock( thread_xp , THREAD_BLOCKED_PAGE );
215        }
216        else                      // no waiting thread => clear the PG_LOCKED flag
217        {
218                page_clear_flag( page , PG_LOCKED );
219        }
220
221        // release the spinlock
222        spinlock_unlock( &page->lock );
223}
224
225////////////////////////////////////////////
226inline void page_refcount_up( page_t *page )
227{
228        hal_atomic_inc( &page->refcount );
229}
230
231//////////////////////////////////////////////
232inline void page_refcount_down( page_t *page )
233{
234        hal_atomic_dec( &page->refcount );
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;
244
245        if( dst->order != src->order )
246        {
247                printk("\n[PANIC] in %s : src size != dst size\n", __FUNCTION__ );
248                hal_core_sleep();
249        }
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;
265        base = ppm_page2base( page );
266
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
272        memset( base , 0 , size );
273
274// kmem_print_kcm_table();
275
276}
277
278////////////////////////////////
279void page_print( page_t * page )
280{
281        printk("*** Page %d : base = %x / flags = %x / order = %d / count = %d\n",
282                page->index,
283                ppm_page2base( page ),
284                page->flags,
285                page->order,
286                page->refcount );
287}
288
Note: See TracBrowser for help on using the repository browser.