| 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 |  | 
|---|
| 25 | #include <kernel_config.h> | 
|---|
| 26 | #include <hal_types.h> | 
|---|
| 27 | #include <hal_special.h> | 
|---|
| 28 | #include <hal_uspace.h> | 
|---|
| 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 | 
|---|
| 50 | req.type  = KMEM_MAPPER; | 
|---|
| 51 | req.size  = sizeof(mapper_t); | 
|---|
| 52 | req.flags = AF_KERNEL | AF_ZERO; | 
|---|
| 53 | mapper    = (mapper_t *)kmem_alloc( &req ); | 
|---|
| 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 | 
|---|
| 62 | mapper->refcount = 0; | 
|---|
| 63 | mapper->inode    = NULL; | 
|---|
| 64 |  | 
|---|
| 65 | // initialize radix tree | 
|---|
| 66 | error = grdxt_init( &mapper->radix, | 
|---|
| 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__ ); | 
|---|
| 74 | req.type  = KMEM_MAPPER; | 
|---|
| 75 | req.ptr   = mapper; | 
|---|
| 76 | kmem_free( &req ); | 
|---|
| 77 | return NULL; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | // initialize mapper lock | 
|---|
| 81 | rwlock_init(  &mapper->lock ); | 
|---|
| 82 |  | 
|---|
| 83 | // initialize waiting threads xlist (empty) | 
|---|
| 84 | xlist_root_init( XPTR( local_cxy , &mapper->wait_root ) ); | 
|---|
| 85 |  | 
|---|
| 86 | // initialize vsegs xlist (empty) | 
|---|
| 87 | xlist_root_init( XPTR( local_cxy , &mapper->vsegs_root ) ); | 
|---|
| 88 |  | 
|---|
| 89 | return mapper; | 
|---|
| 90 |  | 
|---|
| 91 | }  // end mapper_create() | 
|---|
| 92 |  | 
|---|
| 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 |  | 
|---|
| 108 | if( page != NULL ) | 
|---|
| 109 | { | 
|---|
| 110 | // remove page from mapper and release to PPM | 
|---|
| 111 | error = mapper_release_page( mapper , page ); | 
|---|
| 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; | 
|---|
| 130 |  | 
|---|
| 131 | }  // end mapper_destroy() | 
|---|
| 132 |  | 
|---|
| 133 | //////////////////////////////////////////// | 
|---|
| 134 | page_t * mapper_get_page( mapper_t * mapper, | 
|---|
| 135 | uint32_t   index ) | 
|---|
| 136 | { | 
|---|
| 137 | kmem_req_t    req; | 
|---|
| 138 | page_t      * page; | 
|---|
| 139 | error_t       error; | 
|---|
| 140 |  | 
|---|
| 141 | mapper_dmsg("\n[INFO] %s : enter for page %d / mapper = %x\n", | 
|---|
| 142 | __FUNCTION__ , index , mapper ); | 
|---|
| 143 |  | 
|---|
| 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 |  | 
|---|
| 152 | // test if page available in mapper | 
|---|
| 153 | if( ( page == NULL) || page_is_flag( page , PG_INLOAD ) )  // page not available | 
|---|
| 154 | { | 
|---|
| 155 |  | 
|---|
| 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 => create it and load it from file system | 
|---|
| 167 | { | 
|---|
| 168 | mapper_dmsg("\n[INFO] %s : missing page => load from FS\n", __FUNCTION__ ); | 
|---|
| 169 |  | 
|---|
| 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 ); | 
|---|
| 175 |  | 
|---|
| 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 |  | 
|---|
| 198 | if( error ) | 
|---|
| 199 | { | 
|---|
| 200 | printk("\n[ERROR] in %s : thread %x cannot insert page in mapper\n", | 
|---|
| 201 | __FUNCTION__ , this->trdid ); | 
|---|
| 202 | mapper_release_page( mapper , page ); | 
|---|
| 203 | page_clear_flag( page , PG_ALL ); | 
|---|
| 204 | req.ptr  = page; | 
|---|
| 205 | req.type = KMEM_PAGE; | 
|---|
| 206 | kmem_free(&req); | 
|---|
| 207 | return NULL; | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | // update the mapper and index fields in page descriptor | 
|---|
| 211 | // required by the vfs_move_page_to_mapper() | 
|---|
| 212 | page->mapper = mapper; | 
|---|
| 213 | page->index  = index; | 
|---|
| 214 |  | 
|---|
| 215 | // launch I/O operation to load page from file system | 
|---|
| 216 | error = vfs_mapper_move_page( page , true );   // to mapper | 
|---|
| 217 |  | 
|---|
| 218 | if( error ) | 
|---|
| 219 | { | 
|---|
| 220 | printk("\n[ERROR] in %s : thread %x cannot load page from device\n", | 
|---|
| 221 | __FUNCTION__ , this->trdid ); | 
|---|
| 222 | mapper_release_page( mapper , page ); | 
|---|
| 223 | page_clear_flag( page , PG_ALL ); | 
|---|
| 224 | req.ptr  = page; | 
|---|
| 225 | req.type = KMEM_PAGE; | 
|---|
| 226 | kmem_free( &req ); | 
|---|
| 227 | return NULL; | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | // reset the page INLOAD flag to make the page available to all readers | 
|---|
| 231 | page_clear_flag( page , PG_INLOAD ); | 
|---|
| 232 |  | 
|---|
| 233 | } | 
|---|
| 234 | else if( page_is_flag( page , PG_INLOAD ) )   // page is loaded by another thread | 
|---|
| 235 | { | 
|---|
| 236 | // release mapper lock from WRITE_MODE | 
|---|
| 237 | rwlock_wr_unlock( &mapper->lock ); | 
|---|
| 238 |  | 
|---|
| 239 | // deschedule to wait load completion | 
|---|
| 240 | while( 1 ) | 
|---|
| 241 | { | 
|---|
| 242 | // exit waiting loop when loaded | 
|---|
| 243 | if(  page_is_flag( page , PG_INLOAD ) ) break; | 
|---|
| 244 |  | 
|---|
| 245 | // deschedule | 
|---|
| 246 | sched_yield(); | 
|---|
| 247 | } | 
|---|
| 248 | } | 
|---|
| 249 | } | 
|---|
| 250 | else                          // page available in mapper | 
|---|
| 251 | { | 
|---|
| 252 |  | 
|---|
| 253 | rwlock_rd_unlock( &mapper->lock ); | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | mapper_dmsg("\n[INFO] %s : exit for page %d / page desc = %x\n", | 
|---|
| 257 | __FUNCTION__ , index , page ); | 
|---|
| 258 |  | 
|---|
| 259 | return page; | 
|---|
| 260 |  | 
|---|
| 261 | }  // end mapper_get_page() | 
|---|
| 262 |  | 
|---|
| 263 | /////////////////////////////////////////////// | 
|---|
| 264 | error_t mapper_release_page( mapper_t * mapper, | 
|---|
| 265 | page_t   * page ) | 
|---|
| 266 | { | 
|---|
| 267 | error_t error; | 
|---|
| 268 |  | 
|---|
| 269 | // lauch IO operation to update page to file system | 
|---|
| 270 | error = vfs_mapper_move_page( page , false );    // from mapper | 
|---|
| 271 |  | 
|---|
| 272 | if( error ) | 
|---|
| 273 | { | 
|---|
| 274 | printk("\n[ERROR] in %s : cannot update file system\n", __FUNCTION__ ); | 
|---|
| 275 | return EIO; | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | // take mapper lock in WRITE_MODE | 
|---|
| 279 | rwlock_wr_lock( &mapper->lock ); | 
|---|
| 280 |  | 
|---|
| 281 | // remove physical page from radix tree | 
|---|
| 282 | grdxt_remove( &mapper->radix , page->index ); | 
|---|
| 283 |  | 
|---|
| 284 | // release mapper lock from WRITE_MODE | 
|---|
| 285 | rwlock_wr_unlock( &mapper->lock ); | 
|---|
| 286 |  | 
|---|
| 287 | // release page to PPM | 
|---|
| 288 | kmem_req_t   req; | 
|---|
| 289 | req.type  = KMEM_PAGE; | 
|---|
| 290 | req.ptr   = page; | 
|---|
| 291 | kmem_free( &req ); | 
|---|
| 292 |  | 
|---|
| 293 | return 0; | 
|---|
| 294 |  | 
|---|
| 295 | }  // end mapper_release_page() | 
|---|
| 296 |  | 
|---|
| 297 | ///////////////////////////////////////// | 
|---|
| 298 | error_t mapper_move( mapper_t  *  mapper, | 
|---|
| 299 | bool_t       to_buffer, | 
|---|
| 300 | uint32_t     file_offset, | 
|---|
| 301 | void      *  buffer, | 
|---|
| 302 | uint32_t     size ) | 
|---|
| 303 | { | 
|---|
| 304 | uint32_t   page_offset;    // first byte to move to/from a mapper page | 
|---|
| 305 | uint32_t   page_count;     // number of bytes to move to/from a mapper page | 
|---|
| 306 | uint32_t   index;          // current mapper page index | 
|---|
| 307 | uint32_t   done;           // number of moved bytes | 
|---|
| 308 | page_t   * page;           // current mapper page descriptor | 
|---|
| 309 | uint8_t  * map_ptr;        // current mapper  address | 
|---|
| 310 | uint8_t  * buf_ptr;        // current buffer  address | 
|---|
| 311 |  | 
|---|
| 312 | mapper_dmsg("\n[INFO] %s : enter / to_buf = %d / buffer = %x\n", | 
|---|
| 313 | __FUNCTION__ , to_buffer , buffer ); | 
|---|
| 314 |  | 
|---|
| 315 | // compute offsets of first and last bytes in file | 
|---|
| 316 | uint32_t min_byte = file_offset; | 
|---|
| 317 | uint32_t max_byte = file_offset + size -1; | 
|---|
| 318 |  | 
|---|
| 319 | // compute indexes of pages for first and last byte in mapper | 
|---|
| 320 | uint32_t first = min_byte >> CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 321 | uint32_t last  = max_byte >> CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 322 |  | 
|---|
| 323 | done = 0; | 
|---|
| 324 |  | 
|---|
| 325 | // loop on pages in mapper | 
|---|
| 326 | for( index = first ; index <= last ; index++ ) | 
|---|
| 327 | { | 
|---|
| 328 | // compute page_offset | 
|---|
| 329 | if( index == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK; | 
|---|
| 330 | else                 page_offset = 0; | 
|---|
| 331 |  | 
|---|
| 332 | // compute page_count | 
|---|
| 333 | if      ( first == last  ) page_count = size; | 
|---|
| 334 | else if ( index == first ) page_count = CONFIG_PPM_PAGE_SIZE - page_offset; | 
|---|
| 335 | else if ( index == last  ) page_count = (max_byte & CONFIG_PPM_PAGE_MASK) + 1; | 
|---|
| 336 | else                       page_count = CONFIG_PPM_PAGE_SIZE; | 
|---|
| 337 |  | 
|---|
| 338 | // get page descriptor | 
|---|
| 339 | page = mapper_get_page( mapper , index ); | 
|---|
| 340 |  | 
|---|
| 341 | if ( page == NULL ) return EINVAL; | 
|---|
| 342 |  | 
|---|
| 343 | // compute pointer in mapper | 
|---|
| 344 | map_ptr = (uint8_t *)ppm_page2vaddr( page ) + page_offset; | 
|---|
| 345 |  | 
|---|
| 346 | // compute pointer in buffer | 
|---|
| 347 | buf_ptr = (uint8_t *)buffer + done; | 
|---|
| 348 |  | 
|---|
| 349 | // move fragment | 
|---|
| 350 | if( to_buffer ) | 
|---|
| 351 | { | 
|---|
| 352 | hal_copy_to_uspace( buf_ptr , map_ptr , page_count ); | 
|---|
| 353 | } | 
|---|
| 354 | else | 
|---|
| 355 | { | 
|---|
| 356 | page_do_dirty( page ); | 
|---|
| 357 | hal_copy_from_uspace( map_ptr , buf_ptr , page_count ); | 
|---|
| 358 | } | 
|---|
| 359 |  | 
|---|
| 360 | done += page_count; | 
|---|
| 361 | } | 
|---|
| 362 |  | 
|---|
| 363 | mapper_dmsg("\n[INFO] %s : exit for buffer %x\n", | 
|---|
| 364 | __FUNCTION__, buffer ); | 
|---|
| 365 |  | 
|---|
| 366 | return 0; | 
|---|
| 367 |  | 
|---|
| 368 | }  // end mapper_move() | 
|---|
| 369 |  | 
|---|