[1] | 1 | /* |
---|
[606] | 2 | * mapper.c - Kernel cache for FS files or directories implementation. |
---|
[1] | 3 | * |
---|
| 4 | * Authors Mohamed Lamine Karaoui (2015) |
---|
[440] | 5 | * Alain Greiner (2016,2017,2018) |
---|
[1] | 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> |
---|
[457] | 26 | #include <hal_kernel_types.h> |
---|
[1] | 27 | #include <hal_special.h> |
---|
[23] | 28 | #include <hal_uspace.h> |
---|
[1] | 29 | #include <grdxt.h> |
---|
| 30 | #include <rwlock.h> |
---|
| 31 | #include <printk.h> |
---|
[279] | 32 | #include <memcpy.h> |
---|
[1] | 33 | #include <thread.h> |
---|
| 34 | #include <core.h> |
---|
| 35 | #include <process.h> |
---|
| 36 | #include <kmem.h> |
---|
| 37 | #include <kcm.h> |
---|
[567] | 38 | #include <ppm.h> |
---|
[1] | 39 | #include <page.h> |
---|
| 40 | #include <cluster.h> |
---|
| 41 | #include <vfs.h> |
---|
| 42 | #include <mapper.h> |
---|
| 43 | |
---|
[567] | 44 | |
---|
[246] | 45 | ////////////////////////////////////////////// |
---|
| 46 | mapper_t * mapper_create( vfs_fs_type_t type ) |
---|
[1] | 47 | { |
---|
| 48 | mapper_t * mapper; |
---|
| 49 | kmem_req_t req; |
---|
| 50 | error_t error; |
---|
| 51 | |
---|
[606] | 52 | // allocate memory for mapper |
---|
[183] | 53 | req.type = KMEM_MAPPER; |
---|
| 54 | req.size = sizeof(mapper_t); |
---|
[1] | 55 | req.flags = AF_KERNEL | AF_ZERO; |
---|
[183] | 56 | mapper = (mapper_t *)kmem_alloc( &req ); |
---|
[1] | 57 | |
---|
| 58 | if( mapper == NULL ) |
---|
| 59 | { |
---|
| 60 | printk("\n[ERROR] in %s : no memory for mapper descriptor\n", __FUNCTION__ ); |
---|
| 61 | return NULL; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | // initialize refcount & inode |
---|
[183] | 65 | mapper->refcount = 0; |
---|
[1] | 66 | mapper->inode = NULL; |
---|
| 67 | |
---|
| 68 | // initialize radix tree |
---|
[606] | 69 | error = grdxt_init( &mapper->rt, |
---|
| 70 | CONFIG_MAPPER_GRDXT_W1, |
---|
| 71 | CONFIG_MAPPER_GRDXT_W2, |
---|
| 72 | CONFIG_MAPPER_GRDXT_W3 ); |
---|
[1] | 73 | |
---|
| 74 | if( error ) |
---|
| 75 | { |
---|
| 76 | printk("\n[ERROR] in %s : cannot initialize radix tree\n", __FUNCTION__ ); |
---|
[183] | 77 | req.type = KMEM_MAPPER; |
---|
[1] | 78 | req.ptr = mapper; |
---|
| 79 | kmem_free( &req ); |
---|
| 80 | return NULL; |
---|
| 81 | } |
---|
| 82 | |
---|
[246] | 83 | // initialize mapper type |
---|
| 84 | mapper->type = type; |
---|
| 85 | |
---|
[1] | 86 | // initialize mapper lock |
---|
[606] | 87 | remote_rwlock_init( XPTR( local_cxy , &mapper->lock ) , LOCK_MAPPER_STATE ); |
---|
[1] | 88 | |
---|
| 89 | // initialize waiting threads xlist (empty) |
---|
[183] | 90 | xlist_root_init( XPTR( local_cxy , &mapper->wait_root ) ); |
---|
[1] | 91 | |
---|
| 92 | // initialize vsegs xlist (empty) |
---|
[183] | 93 | xlist_root_init( XPTR( local_cxy , &mapper->vsegs_root ) ); |
---|
[1] | 94 | |
---|
| 95 | return mapper; |
---|
| 96 | |
---|
[204] | 97 | } // end mapper_create() |
---|
| 98 | |
---|
[606] | 99 | //////////////////////////////////////// |
---|
| 100 | void mapper_destroy( mapper_t * mapper ) |
---|
[1] | 101 | { |
---|
| 102 | page_t * page; |
---|
| 103 | uint32_t found_index = 0; |
---|
| 104 | uint32_t start_index = 0; |
---|
| 105 | kmem_req_t req; |
---|
| 106 | |
---|
[606] | 107 | // scan radix tree |
---|
[1] | 108 | do |
---|
| 109 | { |
---|
| 110 | // get page from radix tree |
---|
[606] | 111 | page = (page_t *)grdxt_get_first( &mapper->rt , start_index , &found_index ); |
---|
[1] | 112 | |
---|
[606] | 113 | // release registered pages to PPM |
---|
[18] | 114 | if( page != NULL ) |
---|
[1] | 115 | { |
---|
| 116 | // remove page from mapper and release to PPM |
---|
[606] | 117 | mapper_release_page( mapper , page ); |
---|
[1] | 118 | |
---|
| 119 | // update start_key value for next page |
---|
| 120 | start_index = found_index; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | while( page != NULL ); |
---|
| 124 | |
---|
[606] | 125 | // release the memory allocated to radix tree itself |
---|
| 126 | grdxt_destroy( &mapper->rt ); |
---|
[1] | 127 | |
---|
| 128 | // release memory for mapper descriptor |
---|
| 129 | req.type = KMEM_MAPPER; |
---|
| 130 | req.ptr = mapper; |
---|
| 131 | kmem_free( &req ); |
---|
| 132 | |
---|
[204] | 133 | } // end mapper_destroy() |
---|
| 134 | |
---|
[606] | 135 | //////////////////////////////////////////////////// |
---|
| 136 | xptr_t mapper_remote_get_page( xptr_t mapper_xp, |
---|
| 137 | uint32_t page_id ) |
---|
[1] | 138 | { |
---|
[183] | 139 | error_t error; |
---|
[606] | 140 | mapper_t * mapper_ptr; |
---|
| 141 | cxy_t mapper_cxy; |
---|
| 142 | xptr_t lock_xp; // extended pointer on mapper lock |
---|
| 143 | xptr_t page_xp; // extended pointer on searched page descriptor |
---|
| 144 | xptr_t rt_xp; // extended pointer on radix tree in mapper |
---|
[1] | 145 | |
---|
[606] | 146 | thread_t * this = CURRENT_THREAD; |
---|
| 147 | |
---|
| 148 | // get mapper cluster and local pointer |
---|
| 149 | mapper_ptr = GET_PTR( mapper_xp ); |
---|
| 150 | mapper_cxy = GET_CXY( mapper_xp ); |
---|
| 151 | |
---|
[438] | 152 | #if DEBUG_MAPPER_GET_PAGE |
---|
[435] | 153 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[606] | 154 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 155 | vfs_inode_t * inode = hal_remote_lpt( XPTR( mapper_cxy , &mapper_ptr->inode ) ); |
---|
| 156 | vfs_inode_get_name( XPTR( mapper_cxy , inode ) , name ); |
---|
[438] | 157 | if( DEBUG_MAPPER_GET_PAGE < cycle ) |
---|
[606] | 158 | printk("\n[%s] thread [%x,%x] enter for page %d of <%s> / cycle %d\n", |
---|
| 159 | __FUNCTION__, this->process->pid, this->trdid, page_id, name, cycle ); |
---|
[435] | 160 | #endif |
---|
[204] | 161 | |
---|
[581] | 162 | // check thread can yield |
---|
| 163 | thread_assert_can_yield( this , __FUNCTION__ ); |
---|
| 164 | |
---|
[606] | 165 | // build extended pointer on mapper lock and mapper rt |
---|
| 166 | lock_xp = XPTR( mapper_cxy , &mapper_ptr->lock ); |
---|
| 167 | rt_xp = XPTR( mapper_cxy , &mapper_ptr->rt ); |
---|
| 168 | |
---|
[1] | 169 | // take mapper lock in READ_MODE |
---|
[606] | 170 | remote_rwlock_rd_acquire( lock_xp ); |
---|
[1] | 171 | |
---|
| 172 | // search page in radix tree |
---|
[606] | 173 | page_xp = grdxt_remote_lookup( rt_xp , page_id ); |
---|
[1] | 174 | |
---|
[606] | 175 | // test mapper miss |
---|
| 176 | if( page_xp == XPTR_NULL ) // miss => try to handle it |
---|
[1] | 177 | { |
---|
| 178 | // release the lock in READ_MODE and take it in WRITE_MODE |
---|
[606] | 179 | remote_rwlock_rd_release( lock_xp ); |
---|
| 180 | remote_rwlock_wr_acquire( lock_xp ); |
---|
[1] | 181 | |
---|
[606] | 182 | // second test on missing page because the page status can be modified |
---|
[1] | 183 | // by another thread, when passing from READ_MODE to WRITE_MODE. |
---|
| 184 | // from this point there is no concurrent accesses to mapper. |
---|
[606] | 185 | page_xp = grdxt_remote_lookup( rt_xp , page_id ); |
---|
[1] | 186 | |
---|
[606] | 187 | if ( page_xp == XPTR_NULL ) // miss confirmed => handle it |
---|
[1] | 188 | { |
---|
[204] | 189 | |
---|
[438] | 190 | #if (DEBUG_MAPPER_GET_PAGE & 1) |
---|
| 191 | if( DEBUG_MAPPER_GET_PAGE < cycle ) |
---|
[606] | 192 | printk("\n[%s] missing page => load it from IOC device\n", __FUNCTION__ ); |
---|
[435] | 193 | #endif |
---|
[606] | 194 | if( mapper_cxy == local_cxy ) // mapper is local |
---|
[1] | 195 | { |
---|
[606] | 196 | error = mapper_handle_miss( mapper_ptr, |
---|
| 197 | page_id, |
---|
| 198 | &page_xp ); |
---|
| 199 | } |
---|
| 200 | else |
---|
[1] | 201 | { |
---|
[606] | 202 | rpc_mapper_handle_miss_client( mapper_cxy, |
---|
| 203 | mapper_ptr, |
---|
| 204 | page_id, |
---|
| 205 | &page_xp, |
---|
| 206 | &error ); |
---|
[1] | 207 | } |
---|
[18] | 208 | |
---|
[606] | 209 | if ( error ) |
---|
[1] | 210 | { |
---|
[606] | 211 | printk("\n[ERROR] in %s : thread[%x,%x] cannot handle mapper miss\n", |
---|
| 212 | __FUNCTION__ , this->process->pid, this->trdid ); |
---|
| 213 | remote_rwlock_wr_release( lock_xp ); |
---|
| 214 | return XPTR_NULL; |
---|
[1] | 215 | } |
---|
| 216 | } |
---|
[606] | 217 | |
---|
| 218 | // release mapper lock from WRITE_MODE |
---|
| 219 | remote_rwlock_wr_release( lock_xp ); |
---|
[1] | 220 | } |
---|
[606] | 221 | else // hit |
---|
[1] | 222 | { |
---|
[606] | 223 | // release mapper lock from READ_MODE |
---|
| 224 | remote_rwlock_rd_release( lock_xp ); |
---|
[1] | 225 | } |
---|
| 226 | |
---|
[438] | 227 | #if DEBUG_MAPPER_GET_PAGE |
---|
[435] | 228 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 229 | if( DEBUG_MAPPER_GET_PAGE < cycle ) |
---|
[606] | 230 | printk("\n[%s] thread[%x,%x] exit for page %d of <%s> / ppn %x / cycle %d\n", |
---|
| 231 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 232 | page_id, name, ppm_page2ppn( page_xp ), cycle ); |
---|
[435] | 233 | #endif |
---|
[204] | 234 | |
---|
[606] | 235 | return page_xp; |
---|
[204] | 236 | |
---|
[606] | 237 | } // end mapper_remote_get_page() |
---|
[204] | 238 | |
---|
[606] | 239 | ////////////////////////////////////////////// |
---|
| 240 | error_t mapper_handle_miss( mapper_t * mapper, |
---|
| 241 | uint32_t page_id, |
---|
| 242 | xptr_t * page_xp ) |
---|
[1] | 243 | { |
---|
[606] | 244 | kmem_req_t req; |
---|
| 245 | page_t * page; |
---|
| 246 | error_t error; |
---|
[1] | 247 | |
---|
[606] | 248 | thread_t * this = CURRENT_THREAD; |
---|
[1] | 249 | |
---|
[606] | 250 | #if DEBUG_MAPPER_HANDLE_MISS |
---|
| 251 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 252 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 253 | vfs_inode_t * inode = mapper->inode; |
---|
| 254 | vfs_inode_get_name( XPTR( local_cxy , inode ) , name ); |
---|
| 255 | if( DEBUG_MAPPER_HANDLE_MISS < cycle ) |
---|
| 256 | printk("\n[%s] enter for page %d in <%s> / cycle %d\n", |
---|
| 257 | __FUNCTION__, page_id, name, cycle ); |
---|
| 258 | if( DEBUG_MAPPER_HANDLE_MISS & 1 ) |
---|
| 259 | grdxt_display( &mapper->rt , name ); |
---|
| 260 | #endif |
---|
| 261 | |
---|
| 262 | // allocate one page from the mapper cluster |
---|
| 263 | req.type = KMEM_PAGE; |
---|
| 264 | req.size = 0; |
---|
| 265 | req.flags = AF_NONE; |
---|
| 266 | page = kmem_alloc( &req ); |
---|
| 267 | |
---|
| 268 | if( page == NULL ) |
---|
| 269 | { |
---|
| 270 | printk("\n[ERROR] in %s : thread [%x,%x] cannot allocate page in cluster %x\n", |
---|
| 271 | __FUNCTION__ , this->process->pid, this->trdid , local_cxy ); |
---|
| 272 | return -1; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | // initialize the page descriptor |
---|
| 276 | page_init( page ); |
---|
| 277 | page_set_flag( page , PG_INIT ); |
---|
| 278 | page_refcount_up( page ); |
---|
| 279 | page->mapper = mapper; |
---|
| 280 | page->index = page_id; |
---|
| 281 | |
---|
| 282 | // insert page in mapper radix tree |
---|
| 283 | error = grdxt_insert( &mapper->rt , page_id , page ); |
---|
| 284 | |
---|
[1] | 285 | if( error ) |
---|
| 286 | { |
---|
[606] | 287 | printk("\n[ERROR] in %s : thread[%x,%x] cannot insert page in mapper\n", |
---|
| 288 | __FUNCTION__ , this->process->pid, this->trdid ); |
---|
| 289 | mapper_release_page( mapper , page ); |
---|
| 290 | req.ptr = page; |
---|
| 291 | req.type = KMEM_PAGE; |
---|
| 292 | kmem_free(&req); |
---|
| 293 | return -1; |
---|
[1] | 294 | } |
---|
[18] | 295 | |
---|
[606] | 296 | // launch I/O operation to load page from device to mapper |
---|
| 297 | error = vfs_fs_move_page( XPTR( local_cxy , page ) , true ); |
---|
| 298 | |
---|
| 299 | if( error ) |
---|
| 300 | { |
---|
| 301 | printk("\n[ERROR] in %s : thread[%x,%x] cannot load page from device\n", |
---|
| 302 | __FUNCTION__ , this->process->pid, this->trdid ); |
---|
| 303 | mapper_release_page( mapper , page ); |
---|
| 304 | req.ptr = page; |
---|
| 305 | req.type = KMEM_PAGE; |
---|
| 306 | kmem_free( &req ); |
---|
| 307 | return -1; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | // set extended pointer on allocated page |
---|
| 311 | *page_xp = XPTR( local_cxy , page ); |
---|
| 312 | |
---|
| 313 | #if DEBUG_MAPPER_HANDLE_MISS |
---|
| 314 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 315 | if( DEBUG_MAPPER_HANDLE_MISS < cycle ) |
---|
| 316 | printk("\n[%s] exit for page %d in <%s> / ppn %x / cycle %d\n", |
---|
| 317 | __FUNCTION__, page_id, name, ppm_page2ppn( *page_xp ), cycle ); |
---|
| 318 | if( DEBUG_MAPPER_HANDLE_MISS & 1 ) |
---|
| 319 | grdxt_display( &mapper->rt , name ); |
---|
| 320 | #endif |
---|
| 321 | |
---|
| 322 | return 0; |
---|
| 323 | |
---|
| 324 | } // end mapper_handle_miss() |
---|
| 325 | |
---|
| 326 | //////////////////////////////////////////// |
---|
| 327 | void mapper_release_page( mapper_t * mapper, |
---|
| 328 | page_t * page ) |
---|
| 329 | { |
---|
| 330 | // build extended pointer on mapper lock |
---|
| 331 | xptr_t mapper_lock_xp = XPTR( local_cxy , &mapper->lock ); |
---|
| 332 | |
---|
[1] | 333 | // take mapper lock in WRITE_MODE |
---|
[606] | 334 | remote_rwlock_wr_acquire( mapper_lock_xp ); |
---|
[1] | 335 | |
---|
| 336 | // remove physical page from radix tree |
---|
[606] | 337 | grdxt_remove( &mapper->rt , page->index ); |
---|
[1] | 338 | |
---|
| 339 | // release mapper lock from WRITE_MODE |
---|
[606] | 340 | remote_rwlock_wr_release( mapper_lock_xp ); |
---|
[1] | 341 | |
---|
| 342 | // release page to PPM |
---|
[183] | 343 | kmem_req_t req; |
---|
| 344 | req.type = KMEM_PAGE; |
---|
[1] | 345 | req.ptr = page; |
---|
| 346 | kmem_free( &req ); |
---|
| 347 | |
---|
[204] | 348 | } // end mapper_release_page() |
---|
| 349 | |
---|
[606] | 350 | //////////////////////////////////////////// |
---|
[313] | 351 | error_t mapper_move_user( mapper_t * mapper, |
---|
| 352 | bool_t to_buffer, |
---|
| 353 | uint32_t file_offset, |
---|
| 354 | void * buffer, |
---|
| 355 | uint32_t size ) |
---|
[1] | 356 | { |
---|
[606] | 357 | xptr_t mapper_xp; // extended pointer on local mapper |
---|
[23] | 358 | uint32_t page_offset; // first byte to move to/from a mapper page |
---|
| 359 | uint32_t page_count; // number of bytes to move to/from a mapper page |
---|
[606] | 360 | uint32_t page_id; // current mapper page index |
---|
[23] | 361 | uint32_t done; // number of moved bytes |
---|
[606] | 362 | xptr_t page_xp; // extended pointer on current mapper page descriptor |
---|
[330] | 363 | |
---|
[438] | 364 | #if DEBUG_MAPPER_MOVE_USER |
---|
[606] | 365 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 366 | thread_t * this = CURRENT_THREAD; |
---|
[438] | 367 | if( DEBUG_MAPPER_MOVE_USER < cycle ) |
---|
[606] | 368 | printk("\n[%s] thread[%x,%x] : to_buf %d / buffer %x / size %d / offset %d / cycle %d\n", |
---|
| 369 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 370 | to_buffer, buffer, size, file_offset, cycle ); |
---|
[435] | 371 | #endif |
---|
[1] | 372 | |
---|
[606] | 373 | // build extended pointer on mapper |
---|
| 374 | mapper_xp = XPTR( local_cxy , mapper ); |
---|
| 375 | |
---|
[23] | 376 | // compute offsets of first and last bytes in file |
---|
| 377 | uint32_t min_byte = file_offset; |
---|
[606] | 378 | uint32_t max_byte = file_offset + size - 1; |
---|
[1] | 379 | |
---|
[23] | 380 | // compute indexes of pages for first and last byte in mapper |
---|
| 381 | uint32_t first = min_byte >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 382 | uint32_t last = max_byte >> CONFIG_PPM_PAGE_SHIFT; |
---|
[1] | 383 | |
---|
[606] | 384 | #if (DEBUG_MAPPER_MOVE_USER & 1) |
---|
| 385 | if( DEBUG_MAPPER_MOVE_USER < cycle ) |
---|
| 386 | printk("\n[%s] first_page %d / last_page %d\n", __FUNCTION__, first, last ); |
---|
| 387 | #endif |
---|
| 388 | |
---|
[23] | 389 | done = 0; |
---|
[1] | 390 | |
---|
[23] | 391 | // loop on pages in mapper |
---|
[606] | 392 | for( page_id = first ; page_id <= last ; page_id++ ) |
---|
[1] | 393 | { |
---|
[183] | 394 | // compute page_offset |
---|
[606] | 395 | if( page_id == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK; |
---|
| 396 | else page_offset = 0; |
---|
[1] | 397 | |
---|
[313] | 398 | // compute number of bytes in page |
---|
[606] | 399 | if ( first == last ) page_count = size; |
---|
| 400 | else if ( page_id == first ) page_count = CONFIG_PPM_PAGE_SIZE - page_offset; |
---|
| 401 | else if ( page_id == last ) page_count = (max_byte & CONFIG_PPM_PAGE_MASK) + 1; |
---|
| 402 | else page_count = CONFIG_PPM_PAGE_SIZE; |
---|
[1] | 403 | |
---|
[438] | 404 | #if (DEBUG_MAPPER_MOVE_USER & 1) |
---|
| 405 | if( DEBUG_MAPPER_MOVE_USER < cycle ) |
---|
[606] | 406 | printk("\n[%s] page_id = %d / page_offset = %d / page_count = %d\n", |
---|
| 407 | __FUNCTION__ , page_id , page_offset , page_count ); |
---|
[435] | 408 | #endif |
---|
[265] | 409 | |
---|
[606] | 410 | // get extended pointer on page descriptor |
---|
| 411 | page_xp = mapper_remote_get_page( mapper_xp , page_id ); |
---|
[1] | 412 | |
---|
[606] | 413 | if ( page_xp == XPTR_NULL ) return -1; |
---|
[1] | 414 | |
---|
[23] | 415 | // compute pointer in mapper |
---|
[606] | 416 | xptr_t base_xp = ppm_page2base( page_xp ); |
---|
| 417 | uint8_t * map_ptr = (uint8_t *)GET_PTR( base_xp ) + page_offset; |
---|
[1] | 418 | |
---|
[23] | 419 | // compute pointer in buffer |
---|
[606] | 420 | uint8_t * buf_ptr = (uint8_t *)buffer + done; |
---|
[1] | 421 | |
---|
| 422 | // move fragment |
---|
[330] | 423 | if( to_buffer ) |
---|
[1] | 424 | { |
---|
[606] | 425 | hal_copy_to_uspace( buf_ptr , map_ptr , page_count ); |
---|
[1] | 426 | } |
---|
[330] | 427 | else |
---|
[1] | 428 | { |
---|
[606] | 429 | ppm_page_do_dirty( page_xp ); |
---|
| 430 | hal_copy_from_uspace( map_ptr , buf_ptr , page_count ); |
---|
[1] | 431 | } |
---|
| 432 | |
---|
[23] | 433 | done += page_count; |
---|
[1] | 434 | } |
---|
| 435 | |
---|
[438] | 436 | #if DEBUG_MAPPER_MOVE_USER |
---|
[435] | 437 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 438 | if( DEBUG_MAPPER_MOVE_USER < cycle ) |
---|
[606] | 439 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
---|
| 440 | __FUNCTION__, this->process->pid, this->trdid, cycle ); |
---|
[435] | 441 | #endif |
---|
[204] | 442 | |
---|
[1] | 443 | return 0; |
---|
| 444 | |
---|
[313] | 445 | } // end mapper_move_user() |
---|
[204] | 446 | |
---|
[313] | 447 | //////////////////////////////////////////////// |
---|
[606] | 448 | error_t mapper_move_kernel( xptr_t mapper_xp, |
---|
| 449 | bool_t to_buffer, |
---|
| 450 | uint32_t file_offset, |
---|
| 451 | xptr_t buffer_xp, |
---|
| 452 | uint32_t size ) |
---|
[313] | 453 | { |
---|
| 454 | uint32_t page_offset; // first byte to move to/from a mapper page |
---|
| 455 | uint32_t page_count; // number of bytes to move to/from a mapper page |
---|
[606] | 456 | uint32_t page_id; // current mapper page index |
---|
[313] | 457 | uint32_t done; // number of moved bytes |
---|
[606] | 458 | xptr_t page_xp; // extended pointer on current mapper page descriptor |
---|
[313] | 459 | |
---|
| 460 | uint8_t * src_ptr; // source buffer local pointer |
---|
| 461 | cxy_t src_cxy; // source cluster |
---|
| 462 | uint8_t * dst_ptr; // destination buffer local pointer |
---|
| 463 | cxy_t dst_cxy; // destination cluster |
---|
[330] | 464 | |
---|
[406] | 465 | // get buffer cluster and local pointer |
---|
| 466 | cxy_t buffer_cxy = GET_CXY( buffer_xp ); |
---|
[606] | 467 | uint8_t * buffer_ptr = GET_PTR( buffer_xp ); |
---|
[313] | 468 | |
---|
[606] | 469 | // get mapper cluster |
---|
| 470 | cxy_t mapper_cxy = GET_CXY( mapper_xp ); |
---|
| 471 | |
---|
[438] | 472 | #if DEBUG_MAPPER_MOVE_KERNEL |
---|
[606] | 473 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 474 | thread_t * this = CURRENT_THREAD; |
---|
[438] | 475 | if( DEBUG_MAPPER_MOVE_KERNEL < cycle ) |
---|
[606] | 476 | printk("\n[%s] thread[%x,%x] enter / to_buf %d / buf_cxy %x / buf_ptr %x / cycle %d\n", |
---|
| 477 | __FUNCTION__, this->process->pid, this->trdid, to_buffer, buffer_cxy, buffer_ptr, cycle ); |
---|
[435] | 478 | #endif |
---|
[406] | 479 | |
---|
[313] | 480 | // compute offsets of first and last bytes in file |
---|
| 481 | uint32_t min_byte = file_offset; |
---|
| 482 | uint32_t max_byte = file_offset + size -1; |
---|
| 483 | |
---|
| 484 | // compute indexes for first and last pages in mapper |
---|
| 485 | uint32_t first = min_byte >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 486 | uint32_t last = max_byte >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 487 | |
---|
[438] | 488 | #if (DEBUG_MAPPER_MOVE_KERNEL & 1) |
---|
| 489 | if( DEBUG_MAPPER_MOVE_KERNEL < cycle ) |
---|
[606] | 490 | printk("\n[%s] first_page %d / last_page %d\n", __FUNCTION__, first, last ); |
---|
[435] | 491 | #endif |
---|
[313] | 492 | |
---|
| 493 | // compute source and destination clusters |
---|
| 494 | if( to_buffer ) |
---|
| 495 | { |
---|
| 496 | dst_cxy = buffer_cxy; |
---|
[606] | 497 | src_cxy = mapper_cxy; |
---|
[313] | 498 | } |
---|
| 499 | else |
---|
| 500 | { |
---|
| 501 | src_cxy = buffer_cxy; |
---|
[606] | 502 | dst_cxy = mapper_cxy; |
---|
[313] | 503 | } |
---|
| 504 | |
---|
| 505 | done = 0; |
---|
| 506 | |
---|
| 507 | // loop on pages in mapper |
---|
[606] | 508 | for( page_id = first ; page_id <= last ; page_id++ ) |
---|
[313] | 509 | { |
---|
| 510 | // compute page_offset |
---|
[606] | 511 | if( page_id == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK; |
---|
| 512 | else page_offset = 0; |
---|
[313] | 513 | |
---|
| 514 | // compute number of bytes to move in page |
---|
[606] | 515 | if ( first == last ) page_count = size; |
---|
| 516 | else if ( page_id == first ) page_count = CONFIG_PPM_PAGE_SIZE - page_offset; |
---|
| 517 | else if ( page_id == last ) page_count = (max_byte & CONFIG_PPM_PAGE_MASK) + 1; |
---|
| 518 | else page_count = CONFIG_PPM_PAGE_SIZE; |
---|
[313] | 519 | |
---|
[438] | 520 | #if (DEBUG_MAPPER_MOVE_KERNEL & 1) |
---|
| 521 | if( DEBUG_MAPPER_MOVE_KERNEL < cycle ) |
---|
[606] | 522 | printk("\n[%s] page_id = %d / offset = %d / bytes = %d\n", |
---|
| 523 | __FUNCTION__ , page_id , page_offset , page_count ); |
---|
[435] | 524 | #endif |
---|
[313] | 525 | |
---|
[606] | 526 | // get extended pointer on page descriptor |
---|
| 527 | page_xp = mapper_remote_get_page( mapper_xp , page_id ); |
---|
[313] | 528 | |
---|
[606] | 529 | if ( page_xp == XPTR_NULL ) return -1; |
---|
[313] | 530 | |
---|
[315] | 531 | // get page base address |
---|
[606] | 532 | xptr_t base_xp = ppm_page2base( page_xp ); |
---|
[367] | 533 | uint8_t * base_ptr = (uint8_t *)GET_PTR( base_xp ); |
---|
[330] | 534 | |
---|
[313] | 535 | // compute source and destination pointers |
---|
| 536 | if( to_buffer ) |
---|
| 537 | { |
---|
[315] | 538 | dst_ptr = buffer_ptr + done; |
---|
[367] | 539 | src_ptr = base_ptr + page_offset; |
---|
[313] | 540 | } |
---|
| 541 | else |
---|
| 542 | { |
---|
[315] | 543 | src_ptr = buffer_ptr + done; |
---|
[367] | 544 | dst_ptr = base_ptr + page_offset; |
---|
[313] | 545 | |
---|
[606] | 546 | ppm_page_do_dirty( page_xp ); |
---|
[313] | 547 | } |
---|
| 548 | |
---|
| 549 | // move fragment |
---|
| 550 | hal_remote_memcpy( XPTR( dst_cxy , dst_ptr ), XPTR( src_cxy , src_ptr ), page_count ); |
---|
[330] | 551 | |
---|
[313] | 552 | done += page_count; |
---|
| 553 | } |
---|
| 554 | |
---|
[438] | 555 | #if DEBUG_MAPPER_MOVE_KERNEL |
---|
[435] | 556 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 557 | if( DEBUG_MAPPER_MOVE_KERNEL < cycle ) |
---|
[606] | 558 | printk("\n[%s] thread[%x,%x] exit / to_buf %d / buf_cxy %x / buf_ptr %x / cycle %d\n", |
---|
| 559 | __FUNCTION__, this->process->pid, this->trdid, to_buffer, buffer_cxy, buffer_ptr, cycle ); |
---|
[435] | 560 | #endif |
---|
[313] | 561 | |
---|
| 562 | return 0; |
---|
| 563 | |
---|
[406] | 564 | } // end mapper_move_kernel() |
---|
[313] | 565 | |
---|
[606] | 566 | /////////////////////////////////////////////////// |
---|
| 567 | error_t mapper_remote_get_32( xptr_t mapper_xp, |
---|
| 568 | uint32_t word_id, |
---|
| 569 | uint32_t * p_value ) |
---|
| 570 | { |
---|
| 571 | uint32_t page_id; // page index in file |
---|
| 572 | uint32_t local_id; // word index in page |
---|
| 573 | xptr_t page_xp; // extended pointer on searched page descriptor |
---|
| 574 | xptr_t base_xp; // extended pointer on searched page base |
---|
| 575 | |
---|
| 576 | |
---|
| 577 | // get page index and local word index |
---|
| 578 | page_id = word_id >> 10; |
---|
| 579 | local_id = word_id & 0x3FF; |
---|
| 580 | |
---|
| 581 | // get page containing the searched word |
---|
| 582 | page_xp = mapper_remote_get_page( mapper_xp , page_id ); |
---|
| 583 | |
---|
| 584 | if( page_xp == XPTR_NULL ) return -1; |
---|
| 585 | |
---|
| 586 | // get page base |
---|
| 587 | base_xp = ppm_page2base( page_xp ); |
---|
| 588 | |
---|
| 589 | // get the value from mapper |
---|
| 590 | *p_value = hal_remote_l32( base_xp + (local_id<<2) ); |
---|
| 591 | |
---|
| 592 | return 0; |
---|
| 593 | |
---|
| 594 | } // end mapper_remote_get_32() |
---|
| 595 | |
---|
| 596 | /////////////////////////////////////////////////// |
---|
| 597 | error_t mapper_remote_set_32( xptr_t mapper_xp, |
---|
| 598 | uint32_t word_id, |
---|
| 599 | uint32_t value ) |
---|
| 600 | { |
---|
| 601 | |
---|
| 602 | uint32_t page_id; // page index in file |
---|
| 603 | uint32_t local_id; // word index in page |
---|
| 604 | xptr_t page_xp; // extended pointer on searched page descriptor |
---|
| 605 | xptr_t base_xp; // extended pointer on searched page base |
---|
| 606 | |
---|
| 607 | // get page index and local vord index |
---|
| 608 | page_id = word_id >> 10; |
---|
| 609 | local_id = word_id & 0x3FF; |
---|
| 610 | |
---|
| 611 | // get page containing the searched word |
---|
| 612 | page_xp = mapper_remote_get_page( mapper_xp , page_id ); |
---|
| 613 | |
---|
| 614 | if( page_xp == XPTR_NULL ) return -1; |
---|
| 615 | |
---|
| 616 | // get page base |
---|
| 617 | base_xp = ppm_page2base( page_xp ); |
---|
| 618 | |
---|
| 619 | // set value to mapper |
---|
| 620 | hal_remote_s32( (base_xp + (local_id << 2)) , value ); |
---|
| 621 | |
---|
| 622 | // set the dirty flag |
---|
| 623 | ppm_page_do_dirty( page_xp ); |
---|
| 624 | |
---|
| 625 | return 0; |
---|
| 626 | |
---|
| 627 | } // end mapper_remote_set_32() |
---|
| 628 | |
---|
| 629 | |
---|