[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; |
---|
| 267 | base = ppm_page2base(page); |
---|
| 268 | |
---|
| 269 | memset( base , 0 , size ); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | //////////////////////////////// |
---|
| 273 | void page_print( page_t * page ) |
---|
| 274 | { |
---|
| 275 | printk("*** Page %d : base = %x / flags = %x / order = %d / count = %d\n", |
---|
| 276 | page->index, |
---|
| 277 | ppm_page2base( page ), |
---|
| 278 | page->flags, |
---|
| 279 | page->order, |
---|
| 280 | page->refcount ); |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | |
---|
| 284 | |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | /* |
---|
| 288 | ///////////////////////////////////////// |
---|
| 289 | static void page_to_free( page_t * page ) |
---|
| 290 | { |
---|
| 291 | assert((page->state == PGINVALID) || |
---|
| 292 | (page->state == PGVALID) || |
---|
| 293 | (page->state == PGINIT)); |
---|
| 294 | |
---|
| 295 | if(page_refcount_get(page) != 0) |
---|
| 296 | { |
---|
| 297 | printk(ERROR, "ERROR: %s: cpu %d, pid %d, tid %x, page %x, state %x, count %d [%d]\n", |
---|
| 298 | __FUNCTION__, |
---|
| 299 | cpu_get_id(), |
---|
| 300 | current_task->pid, |
---|
| 301 | current_thread, |
---|
| 302 | page, |
---|
| 303 | page->state, |
---|
| 304 | page_refcount_get(page), |
---|
| 305 | cpu_time_stamp()); |
---|
| 306 | } |
---|
| 307 | page->state = PGFREE; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | //////////////////////////////////////////// |
---|
| 311 | static void page_to_invalid( page_t * page ) |
---|
| 312 | { |
---|
| 313 | assert((page->state == PGFREE) || |
---|
| 314 | (page->state == PGVALID) || |
---|
| 315 | (page->state == PGLOCKEDIO)); |
---|
| 316 | |
---|
| 317 | if(page->state == PGLOCKEDIO) |
---|
| 318 | { |
---|
| 319 | page->state = PGINVALID; |
---|
| 320 | page_unlock(page); |
---|
| 321 | return; |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | page->state = PGINVALID; |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | ////////////////////////////////////////// |
---|
| 328 | static void page_to_valid( page_t * page ) |
---|
| 329 | { |
---|
| 330 | assert((page->state == PGINVALID) || |
---|
| 331 | (page->state == PGLOCKED) || |
---|
| 332 | (page->state == PGLOCKEDIO)); |
---|
| 333 | |
---|
| 334 | if(page->state == PGINVALID) |
---|
| 335 | { |
---|
| 336 | page->state = PGVALID; |
---|
| 337 | return; |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | page->state = PGVALID; |
---|
| 341 | page_unlock(page); |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | ////////////////////////////////////////////// |
---|
| 345 | static void page_to_locked_io( page_t * page ) |
---|
| 346 | { |
---|
| 347 | assert((page->state == PGINVALID) || |
---|
| 348 | (page->state == PGVALID)); |
---|
| 349 | |
---|
| 350 | page_lock(page); |
---|
| 351 | page->state = PGLOCKEDIO; |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | /////////////////////////////////////////// |
---|
| 355 | static void page_to_locked( page_t * page ) |
---|
| 356 | { |
---|
| 357 | assert(page->state == PGVALID); |
---|
| 358 | page_lock(page); |
---|
| 359 | page->state = PGLOCKED; |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | ///////////////////////////////////////// |
---|
| 363 | void page_state_set( page_t * page, |
---|
| 364 | page_state_t state ) |
---|
| 365 | { |
---|
| 366 | switch( state ) |
---|
| 367 | { |
---|
| 368 | case PGFREE: |
---|
| 369 | page_to_free(page); |
---|
| 370 | return; |
---|
| 371 | |
---|
| 372 | case PGINVALID: |
---|
| 373 | page_to_invalid(page); |
---|
| 374 | return; |
---|
| 375 | |
---|
| 376 | case PGVALID: |
---|
| 377 | page_to_valid(page); |
---|
| 378 | return; |
---|
| 379 | |
---|
| 380 | case PGLOCKEDIO: |
---|
| 381 | page_to_locked_io(page); |
---|
| 382 | return; |
---|
| 383 | |
---|
| 384 | case PGLOCKED: |
---|
| 385 | page_to_locked(page); |
---|
| 386 | return; |
---|
| 387 | |
---|
| 388 | case PGRESERVED: |
---|
| 389 | refcount_set(&page->count,1); |
---|
| 390 | |
---|
| 391 | case PGINIT: |
---|
| 392 | page->state = new_state; |
---|
| 393 | return; |
---|
| 394 | |
---|
| 395 | default: |
---|
| 396 | printk(ERROR, "ERROR: %s: Unknown Asked State %d\n", new_state); |
---|
| 397 | return; |
---|
| 398 | } |
---|
| 399 | } |
---|
| 400 | */ |
---|
| 401 | |
---|
| 402 | |
---|