[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 | |
---|
| 42 | |
---|
| 43 | //////////////////////////////////////// |
---|
| 44 | inline void page_init( page_t * page ) |
---|
| 45 | { |
---|
| 46 | page->flags = 0; |
---|
| 47 | page->order = 0; |
---|
| 48 | page->index = 0; |
---|
| 49 | page->mapper = NULL; |
---|
| 50 | page->private = 0; |
---|
| 51 | page->refcount = 0; |
---|
| 52 | spinlock_init( &page->lock ); |
---|
| 53 | list_entry_init( &page->list ); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | //////////////////////////////////////////// |
---|
| 57 | inline void page_set_flag( page_t * page, |
---|
| 58 | uint16_t value ) |
---|
| 59 | { |
---|
| 60 | hal_atomic_or( (uint32_t *)&page->flags , (uint32_t)value ); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | ////////////////////////////////////////////// |
---|
| 64 | inline void page_clear_flag( page_t * page, |
---|
| 65 | uint16_t value ) |
---|
| 66 | { |
---|
| 67 | hal_atomic_and( (uint32_t *)&page->flags , ~((uint32_t)value) ); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | ////////////////////////////////////////////// |
---|
| 71 | inline bool_t page_is_flag( page_t * page, |
---|
| 72 | uint16_t value ) |
---|
| 73 | { |
---|
| 74 | return (bool_t)(page->flags & value); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | ////////////////////////////////////// |
---|
| 78 | bool_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 | //////////////////////////////////////// |
---|
| 104 | bool_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 | ///////////////////// |
---|
| 130 | void sync_all_pages() |
---|
| 131 | { |
---|
| 132 | page_t * page; |
---|
| 133 | mapper_t * mapper; |
---|
| 134 | uint32_t index; |
---|
| 135 | ppm_t * ppm = &LOCAL_CLUSTER->ppm; |
---|
| 136 | |
---|
| 137 | // lock the dirty_list |
---|
| 138 | spinlock_lock( &ppm->dirty_lock ); |
---|
| 139 | |
---|
| 140 | while( !list_is_empty( &ppm->dirty_root ) ) |
---|
| 141 | { |
---|
| 142 | page = LIST_FIRST( &ppm->dirty_root , page_t , list ); |
---|
| 143 | |
---|
| 144 | // unlock the dirty_list |
---|
| 145 | spinlock_unlock( &ppm->dirty_lock ); |
---|
| 146 | |
---|
| 147 | mapper = page->mapper; |
---|
| 148 | index = page->index; |
---|
| 149 | |
---|
| 150 | // lock the page |
---|
| 151 | page_lock( page ); |
---|
| 152 | |
---|
| 153 | // sync the page |
---|
| 154 | mapper_sync_page( mapper , index , page ); |
---|
| 155 | |
---|
| 156 | // unlock the page |
---|
| 157 | page_unlock( page ); |
---|
| 158 | |
---|
| 159 | // lock the dirty_list |
---|
| 160 | spinlock_lock( &ppm->dirty_lock ); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | // unlock the dirty_list |
---|
| 164 | spinlock_unlock( &ppm->dirty_lock ); |
---|
| 165 | |
---|
| 166 | } // end sync_all_pages() |
---|
| 167 | |
---|
| 168 | /////////////////////////////// |
---|
| 169 | void page_lock( page_t * page ) |
---|
| 170 | { |
---|
| 171 | // take the spinlock protecting the PG_LOCKED flag |
---|
| 172 | spinlock_lock( &page->lock ); |
---|
| 173 | |
---|
| 174 | if( page_is_flag( page , PG_LOCKED ) ) // page is already locked |
---|
| 175 | { |
---|
| 176 | // get pointer on calling thread |
---|
| 177 | thread_t * thread = CURRENT_THREAD; |
---|
| 178 | |
---|
| 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 ) ); |
---|
| 182 | |
---|
| 183 | // release the spinlock |
---|
| 184 | spinlock_unlock( &page->lock ); |
---|
| 185 | |
---|
| 186 | // deschedule the calling thread |
---|
| 187 | thread_block( thread , THREAD_BLOCKED_PAGE ); |
---|
| 188 | sched_yield(); |
---|
| 189 | } |
---|
| 190 | else // page is not locked |
---|
| 191 | { |
---|
| 192 | // set the PG_LOCKED flag |
---|
| 193 | page_set_flag( page , PG_LOCKED ); |
---|
| 194 | |
---|
| 195 | // release the spinlock |
---|
| 196 | spinlock_unlock( &page->lock ); |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | ///////////////////////////////// |
---|
| 201 | void page_unlock( page_t * page ) |
---|
| 202 | { |
---|
| 203 | // take the spinlock protecting the PG_LOCKED flag |
---|
| 204 | spinlock_lock( &page->lock ); |
---|
| 205 | |
---|
| 206 | // check the page waiting list |
---|
| 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 |
---|
| 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 ); |
---|
| 214 | |
---|
| 215 | // reactivate the first waiting thread |
---|
| 216 | thread_unblock( thread_xp , THREAD_BLOCKED_PAGE ); |
---|
| 217 | } |
---|
| 218 | else // no waiting thread => clear the PG_LOCKED flag |
---|
| 219 | { |
---|
| 220 | page_clear_flag( page , PG_LOCKED ); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | // release the spinlock |
---|
| 224 | spinlock_unlock( &page->lock ); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | //////////////////////////////////////////// |
---|
| 228 | inline void page_refcount_up( page_t *page ) |
---|
| 229 | { |
---|
| 230 | hal_atomic_inc( &page->refcount ); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | ////////////////////////////////////////////// |
---|
| 234 | inline void page_refcount_down( page_t *page ) |
---|
| 235 | { |
---|
| 236 | hal_atomic_dec( &page->refcount ); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | ////////////////////////////// |
---|
| 240 | void page_copy( page_t * dst, |
---|
| 241 | page_t * src ) |
---|
| 242 | { |
---|
| 243 | uint32_t size; |
---|
| 244 | void * src_base; |
---|
| 245 | void * dst_base; |
---|
| 246 | |
---|
| 247 | if( dst->order != src->order ) |
---|
| 248 | { |
---|
| 249 | printk("\n[PANIC] in %s : src size != dst size\n", __FUNCTION__ ); |
---|
| 250 | hal_core_sleep(); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | size = (1 << dst->order) * CONFIG_PPM_PAGE_SIZE; |
---|
| 254 | src_base = ppm_page2base( src ); |
---|
| 255 | dst_base = ppm_page2base( dst ); |
---|
| 256 | |
---|
| 257 | memcpy( dst_base , src_base , size ); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | /////////////////////////////// |
---|
| 261 | void page_zero( page_t * page ) |
---|
| 262 | { |
---|
| 263 | uint32_t size; |
---|
| 264 | void * base; |
---|
| 265 | |
---|
| 266 | size = (1 << page->order) * CONFIG_PPM_PAGE_SIZE; |
---|
[7] | 267 | base = ppm_page2base( page ); |
---|
[1] | 268 | |
---|
[7] | 269 | // kmem_print_kcm_table(); |
---|
| 270 | |
---|
| 271 | // printk("\n@@@ in page_zero : size = %x / base = %x / kcm_tbl = %x\n", |
---|
| 272 | // size , (uint32_t)base , (uint32_t)(&LOCAL_CLUSTER->kcm_tbl[0] ) ); |
---|
| 273 | |
---|
[1] | 274 | memset( base , 0 , size ); |
---|
[7] | 275 | |
---|
| 276 | // kmem_print_kcm_table(); |
---|
| 277 | |
---|
[1] | 278 | } |
---|
| 279 | |
---|
| 280 | //////////////////////////////// |
---|
| 281 | void page_print( page_t * page ) |
---|
| 282 | { |
---|
| 283 | printk("*** Page %d : base = %x / flags = %x / order = %d / count = %d\n", |
---|
| 284 | page->index, |
---|
| 285 | ppm_page2base( page ), |
---|
| 286 | page->flags, |
---|
| 287 | page->order, |
---|
| 288 | page->refcount ); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | |
---|
| 292 | |
---|
| 293 | |
---|
| 294 | |
---|
| 295 | |
---|