[1] | 1 | /* |
---|
| 2 | * mapper.c - Map memory, file or device in process virtual address space. |
---|
| 3 | * |
---|
| 4 | * Authors Mohamed Lamine Karaoui (2015) |
---|
| 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 | |
---|
[14] | 25 | #include <kernel_config.h> |
---|
[1] | 26 | #include <hal_types.h> |
---|
| 27 | #include <hal_special.h> |
---|
[23] | 28 | #include <hal_uspace.h> |
---|
[1] | 29 | #include <grdxt.h> |
---|
| 30 | #include <rwlock.h> |
---|
| 31 | #include <printk.h> |
---|
| 32 | #include <thread.h> |
---|
| 33 | #include <core.h> |
---|
| 34 | #include <process.h> |
---|
| 35 | #include <kmem.h> |
---|
| 36 | #include <kcm.h> |
---|
| 37 | #include <page.h> |
---|
| 38 | #include <cluster.h> |
---|
| 39 | #include <vfs.h> |
---|
| 40 | #include <mapper.h> |
---|
| 41 | |
---|
| 42 | ////////////////////////// |
---|
| 43 | mapper_t * mapper_create() |
---|
| 44 | { |
---|
| 45 | mapper_t * mapper; |
---|
| 46 | kmem_req_t req; |
---|
| 47 | error_t error; |
---|
| 48 | |
---|
| 49 | // allocate memory for associated mapper |
---|
[183] | 50 | req.type = KMEM_MAPPER; |
---|
| 51 | req.size = sizeof(mapper_t); |
---|
[1] | 52 | req.flags = AF_KERNEL | AF_ZERO; |
---|
[183] | 53 | mapper = (mapper_t *)kmem_alloc( &req ); |
---|
[1] | 54 | |
---|
| 55 | if( mapper == NULL ) |
---|
| 56 | { |
---|
| 57 | printk("\n[ERROR] in %s : no memory for mapper descriptor\n", __FUNCTION__ ); |
---|
| 58 | return NULL; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | // initialize refcount & inode |
---|
[183] | 62 | mapper->refcount = 0; |
---|
[1] | 63 | mapper->inode = NULL; |
---|
| 64 | |
---|
| 65 | // initialize radix tree |
---|
[183] | 66 | error = grdxt_init( &mapper->radix, |
---|
[1] | 67 | CONFIG_VMM_GRDXT_W1, |
---|
| 68 | CONFIG_VMM_GRDXT_W2, |
---|
| 69 | CONFIG_VMM_GRDXT_W3 ); |
---|
| 70 | |
---|
| 71 | if( error ) |
---|
| 72 | { |
---|
| 73 | printk("\n[ERROR] in %s : cannot initialize radix tree\n", __FUNCTION__ ); |
---|
[183] | 74 | req.type = KMEM_MAPPER; |
---|
[1] | 75 | req.ptr = mapper; |
---|
| 76 | kmem_free( &req ); |
---|
| 77 | return NULL; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | // initialize mapper lock |
---|
[183] | 81 | rwlock_init( &mapper->lock ); |
---|
[1] | 82 | |
---|
| 83 | // initialize waiting threads xlist (empty) |
---|
[183] | 84 | xlist_root_init( XPTR( local_cxy , &mapper->wait_root ) ); |
---|
[1] | 85 | |
---|
| 86 | // initialize vsegs xlist (empty) |
---|
[183] | 87 | xlist_root_init( XPTR( local_cxy , &mapper->vsegs_root ) ); |
---|
[1] | 88 | |
---|
| 89 | return mapper; |
---|
| 90 | |
---|
[204] | 91 | } // end mapper_create() |
---|
| 92 | |
---|
[1] | 93 | /////////////////////////////////////////// |
---|
| 94 | error_t mapper_destroy( mapper_t * mapper ) |
---|
| 95 | { |
---|
| 96 | page_t * page; |
---|
| 97 | uint32_t found_index = 0; |
---|
| 98 | uint32_t start_index = 0; |
---|
| 99 | kmem_req_t req; |
---|
| 100 | error_t error; |
---|
| 101 | |
---|
| 102 | // scan radix three and release all registered pages to PPM |
---|
| 103 | do |
---|
| 104 | { |
---|
| 105 | // get page from radix tree |
---|
| 106 | page = (page_t *)grdxt_get_first( &mapper->radix , start_index , &found_index ); |
---|
| 107 | |
---|
[18] | 108 | if( page != NULL ) |
---|
[1] | 109 | { |
---|
| 110 | // remove page from mapper and release to PPM |
---|
[183] | 111 | error = mapper_release_page( mapper , page ); |
---|
[1] | 112 | |
---|
| 113 | if ( error ) return error; |
---|
| 114 | |
---|
| 115 | // update start_key value for next page |
---|
| 116 | start_index = found_index; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | while( page != NULL ); |
---|
| 120 | |
---|
| 121 | // release the memory allocated to radix-tree itself |
---|
| 122 | grdxt_destroy( &mapper->radix ); |
---|
| 123 | |
---|
| 124 | // release memory for mapper descriptor |
---|
| 125 | req.type = KMEM_MAPPER; |
---|
| 126 | req.ptr = mapper; |
---|
| 127 | kmem_free( &req ); |
---|
| 128 | |
---|
| 129 | return 0; |
---|
[18] | 130 | |
---|
[204] | 131 | } // end mapper_destroy() |
---|
| 132 | |
---|
[1] | 133 | //////////////////////////////////////////// |
---|
| 134 | page_t * mapper_get_page( mapper_t * mapper, |
---|
| 135 | uint32_t index ) |
---|
| 136 | { |
---|
[183] | 137 | kmem_req_t req; |
---|
| 138 | page_t * page; |
---|
| 139 | error_t error; |
---|
[1] | 140 | |
---|
[204] | 141 | mapper_dmsg("\n[INFO] %s : enter for page %d / mapper = %x\n", |
---|
| 142 | __FUNCTION__ , index , mapper ); |
---|
| 143 | |
---|
[1] | 144 | thread_t * this = CURRENT_THREAD; |
---|
| 145 | |
---|
| 146 | // take mapper lock in READ_MODE |
---|
| 147 | rwlock_rd_lock( &mapper->lock ); |
---|
| 148 | |
---|
| 149 | // search page in radix tree |
---|
| 150 | page = (page_t *)grdxt_lookup( &mapper->radix , index ); |
---|
| 151 | |
---|
[18] | 152 | // test if page available in mapper |
---|
[183] | 153 | if( ( page == NULL) || page_is_flag( page , PG_INLOAD ) ) // page not available |
---|
[1] | 154 | { |
---|
[204] | 155 | |
---|
[1] | 156 | // release the lock in READ_MODE and take it in WRITE_MODE |
---|
| 157 | rwlock_rd_unlock( &mapper->lock ); |
---|
| 158 | rwlock_wr_lock( &mapper->lock ); |
---|
| 159 | |
---|
| 160 | // second test on missing page because the page status can have been modified |
---|
| 161 | // by another thread, when passing from READ_MODE to WRITE_MODE. |
---|
| 162 | // from this point there is no concurrent accesses to mapper. |
---|
| 163 | |
---|
| 164 | page = grdxt_lookup( &mapper->radix , index ); |
---|
| 165 | |
---|
| 166 | if ( page == NULL ) // missing page => load it from file system |
---|
| 167 | { |
---|
[204] | 168 | mapper_dmsg("\n[INFO] %s : missing page => load from FS\n", __FUNCTION__ ); |
---|
| 169 | |
---|
[1] | 170 | // allocate one page from PPM |
---|
| 171 | req.type = KMEM_PAGE; |
---|
| 172 | req.size = 0; |
---|
| 173 | req.flags = AF_NONE; |
---|
| 174 | page = kmem_alloc( &req ); |
---|
[18] | 175 | |
---|
[1] | 176 | if( page == NULL ) |
---|
| 177 | { |
---|
| 178 | printk("\n[ERROR] in %s : thread %x cannot allocate a page in cluster %x\n", |
---|
| 179 | __FUNCTION__ , this->trdid , local_cxy ); |
---|
| 180 | rwlock_wr_unlock( &mapper->lock ); |
---|
| 181 | return NULL; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | // initialize the page descriptor |
---|
| 185 | page_init( page ); |
---|
| 186 | page_set_flag( page , PG_INIT ); |
---|
| 187 | page_set_flag( page , PG_INLOAD ); |
---|
| 188 | page_refcount_up( page ); |
---|
| 189 | page->mapper = mapper; |
---|
| 190 | page->index = index; |
---|
| 191 | |
---|
| 192 | // insert page in mapper radix tree |
---|
| 193 | error = grdxt_insert( &mapper->radix, index , page ); |
---|
| 194 | |
---|
| 195 | // release mapper lock from WRITE_MODE |
---|
| 196 | rwlock_wr_unlock( &mapper->lock ); |
---|
| 197 | |
---|
[18] | 198 | if( error ) |
---|
[1] | 199 | { |
---|
| 200 | printk("\n[ERROR] in %s : thread %x cannot insert page in mapper\n", |
---|
| 201 | __FUNCTION__ , this->trdid ); |
---|
[23] | 202 | mapper_release_page( mapper , page ); |
---|
[1] | 203 | page_clear_flag( page , PG_ALL ); |
---|
| 204 | req.ptr = page; |
---|
| 205 | req.type = KMEM_PAGE; |
---|
| 206 | kmem_free(&req); |
---|
| 207 | return NULL; |
---|
| 208 | } |
---|
[18] | 209 | |
---|
[1] | 210 | // launch I/O operation to load page from file system |
---|
[23] | 211 | error = vfs_move_page_to_mapper( page ); |
---|
[1] | 212 | |
---|
| 213 | if( error ) |
---|
| 214 | { |
---|
| 215 | printk("\n[ERROR] in %s : thread %x cannot load page from device\n", |
---|
| 216 | __FUNCTION__ , this->trdid ); |
---|
[23] | 217 | mapper_release_page( mapper , page ); |
---|
[1] | 218 | page_clear_flag( page , PG_ALL ); |
---|
| 219 | req.ptr = page; |
---|
| 220 | req.type = KMEM_PAGE; |
---|
| 221 | kmem_free( &req ); |
---|
| 222 | return NULL; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | // update the mapper and index fields in page descriptor |
---|
| 226 | page->mapper = mapper; |
---|
| 227 | page->index = index; |
---|
| 228 | |
---|
| 229 | // reset the page INLOAD flag to make the page available to all readers |
---|
| 230 | page_clear_flag( page , PG_INLOAD ); |
---|
| 231 | |
---|
| 232 | } |
---|
| 233 | else if( page_is_flag( page , PG_INLOAD ) ) // page is loaded by another thread |
---|
| 234 | { |
---|
| 235 | // release mapper lock from WRITE_MODE |
---|
| 236 | rwlock_wr_unlock( &mapper->lock ); |
---|
| 237 | |
---|
| 238 | // deschedule to wait load completion |
---|
| 239 | while( 1 ) |
---|
| 240 | { |
---|
| 241 | // exit waiting loop when loaded |
---|
| 242 | if( page_is_flag( page , PG_INLOAD ) ) break; |
---|
| 243 | |
---|
[18] | 244 | // deschedule |
---|
[1] | 245 | sched_yield(); |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | } |
---|
[204] | 249 | else // page available in mapper |
---|
[1] | 250 | { |
---|
| 251 | |
---|
[204] | 252 | rwlock_rd_unlock( &mapper->lock ); |
---|
[1] | 253 | } |
---|
| 254 | |
---|
[204] | 255 | mapper_dmsg("\n[INFO] %s : exit for page %d / page desc = %x\n", |
---|
| 256 | __FUNCTION__ , index , page ); |
---|
| 257 | |
---|
| 258 | return page; |
---|
| 259 | |
---|
| 260 | } // end mapper_get_page() |
---|
| 261 | |
---|
[1] | 262 | /////////////////////////////////////////////// |
---|
| 263 | error_t mapper_release_page( mapper_t * mapper, |
---|
| 264 | page_t * page ) |
---|
| 265 | { |
---|
| 266 | error_t error; |
---|
| 267 | |
---|
| 268 | // lauch IO operation to update page to file system |
---|
[23] | 269 | error = vfs_move_page_from_mapper( page ); |
---|
[1] | 270 | |
---|
| 271 | if( error ) |
---|
| 272 | { |
---|
| 273 | printk("\n[ERROR] in %s : cannot update file system\n", __FUNCTION__ ); |
---|
| 274 | return EIO; |
---|
| 275 | } |
---|
[18] | 276 | |
---|
[1] | 277 | // take mapper lock in WRITE_MODE |
---|
| 278 | rwlock_wr_lock( &mapper->lock ); |
---|
| 279 | |
---|
| 280 | // remove physical page from radix tree |
---|
[183] | 281 | grdxt_remove( &mapper->radix , page->index ); |
---|
[1] | 282 | |
---|
| 283 | // release mapper lock from WRITE_MODE |
---|
| 284 | rwlock_wr_unlock( &mapper->lock ); |
---|
| 285 | |
---|
| 286 | // release page to PPM |
---|
[183] | 287 | kmem_req_t req; |
---|
| 288 | req.type = KMEM_PAGE; |
---|
[1] | 289 | req.ptr = page; |
---|
| 290 | kmem_free( &req ); |
---|
| 291 | |
---|
| 292 | return 0; |
---|
| 293 | |
---|
[204] | 294 | } // end mapper_release_page() |
---|
| 295 | |
---|
[23] | 296 | ///////////////////////////////////////// |
---|
| 297 | error_t mapper_move( mapper_t * mapper, |
---|
| 298 | bool_t to_buffer, |
---|
| 299 | uint32_t file_offset, |
---|
| 300 | void * buffer, |
---|
| 301 | uint32_t size ) |
---|
[1] | 302 | { |
---|
[23] | 303 | uint32_t page_offset; // first byte to move to/from a mapper page |
---|
| 304 | uint32_t page_count; // number of bytes to move to/from a mapper page |
---|
| 305 | uint32_t index; // current mapper page index |
---|
| 306 | uint32_t done; // number of moved bytes |
---|
| 307 | page_t * page; // current mapper page descriptor |
---|
| 308 | uint8_t * map_ptr; // current mapper address |
---|
| 309 | uint8_t * buf_ptr; // current buffer address |
---|
[204] | 310 | |
---|
| 311 | mapper_dmsg("\n[INFO] %s : enter / to_buf = %d / buffer = %x\n", |
---|
| 312 | __FUNCTION__ , to_buffer , buffer ); |
---|
[1] | 313 | |
---|
[23] | 314 | // compute offsets of first and last bytes in file |
---|
| 315 | uint32_t min_byte = file_offset; |
---|
| 316 | uint32_t max_byte = file_offset + size -1; |
---|
[1] | 317 | |
---|
[23] | 318 | // compute indexes of pages for first and last byte in mapper |
---|
| 319 | uint32_t first = min_byte >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 320 | uint32_t last = max_byte >> CONFIG_PPM_PAGE_SHIFT; |
---|
[1] | 321 | |
---|
[23] | 322 | done = 0; |
---|
[1] | 323 | |
---|
[23] | 324 | // loop on pages in mapper |
---|
| 325 | for( index = first ; index <= last ; index++ ) |
---|
[1] | 326 | { |
---|
[183] | 327 | // compute page_offset |
---|
[23] | 328 | if( index == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK; |
---|
| 329 | else page_offset = 0; |
---|
[1] | 330 | |
---|
[183] | 331 | // compute page_count |
---|
[23] | 332 | if ( first == last ) page_count = size; |
---|
| 333 | else if ( index == first ) page_count = CONFIG_PPM_PAGE_SIZE - page_offset; |
---|
| 334 | else if ( index == last ) page_count = (max_byte & CONFIG_PPM_PAGE_MASK) + 1; |
---|
| 335 | else page_count = CONFIG_PPM_PAGE_SIZE; |
---|
[1] | 336 | |
---|
[23] | 337 | // get page descriptor |
---|
| 338 | page = mapper_get_page( mapper , index ); |
---|
[1] | 339 | |
---|
| 340 | if ( page == NULL ) return EINVAL; |
---|
| 341 | |
---|
[23] | 342 | // compute pointer in mapper |
---|
[53] | 343 | map_ptr = (uint8_t *)ppm_page2vaddr( page ) + page_offset; |
---|
[1] | 344 | |
---|
[23] | 345 | // compute pointer in buffer |
---|
| 346 | buf_ptr = (uint8_t *)buffer + done; |
---|
[1] | 347 | |
---|
| 348 | // move fragment |
---|
| 349 | if( to_buffer ) |
---|
| 350 | { |
---|
[23] | 351 | hal_copy_to_uspace( buf_ptr , map_ptr , page_count ); |
---|
[1] | 352 | } |
---|
| 353 | else |
---|
| 354 | { |
---|
| 355 | page_do_dirty( page ); |
---|
[23] | 356 | hal_copy_from_uspace( map_ptr , buf_ptr , page_count ); |
---|
[1] | 357 | } |
---|
| 358 | |
---|
[23] | 359 | done += page_count; |
---|
[1] | 360 | } |
---|
| 361 | |
---|
[204] | 362 | mapper_dmsg("\n[INFO] %s : exit for buffer %x\n", |
---|
| 363 | __FUNCTION__, buffer ); |
---|
| 364 | |
---|
[1] | 365 | return 0; |
---|
| 366 | |
---|
[204] | 367 | } // end mapper_move() |
---|
| 368 | |
---|