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