source: trunk/kernel/mm/mapper.c @ 690

Last change on this file since 690 was 683, checked in by alain, 3 years ago

All modifications required to support the <tcp_chat> application
including error recovery in case of packet loss.A

File size: 31.5 KB
RevLine 
[1]1/*
[606]2 * mapper.c - Kernel cache for FS files or directories implementation.
[1]3 *
4 * Authors   Mohamed Lamine Karaoui (2015)
[683]5 *           Alain Greiner          (2016,2017,2018,2019,2020)
[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>
[656]29#include <hal_vmm.h>
[1]30#include <grdxt.h>
[614]31#include <string.h>
[1]32#include <rwlock.h>
33#include <printk.h>
[279]34#include <memcpy.h>
[1]35#include <thread.h>
36#include <core.h>
37#include <process.h>
38#include <kmem.h>
39#include <kcm.h>
[567]40#include <ppm.h>
[1]41#include <page.h>
42#include <cluster.h>
43#include <vfs.h>
44#include <mapper.h>
[614]45#include <dev_ioc.h>
[1]46
[567]47
[657]48/////////////////////////////////////
49xptr_t  mapper_create( cxy_t     cxy,
50                       uint32_t  type )
[1]51{
[657]52    mapper_t * mapper_ptr;
[1]53    error_t    error;
54
[635]55    // allocate memory for mapper descriptor
[683]56    mapper_ptr  = kmem_remote_alloc( cxy , bits_log2(sizeof(mapper_t)) , AF_ZERO );
[1]57
[657]58    if( mapper_ptr == NULL )
[1]59    {
[683]60
61#if DEBUG_MAPPER_ERROR
62printk("\n[ERROR] in %s : no memory for mapper descriptor\n", __FUNCTION__ );
63#endif
[657]64        return XPTR_NULL;
[1]65    }
66
[657]67    // initialize refcount and type
68    hal_remote_s32( XPTR( cxy , &mapper_ptr->refcount ) , 0 );
69    hal_remote_s32( XPTR( cxy , &mapper_ptr->fs_type )  , type );
[1]70
71    // initialize radix tree
[657]72    error = grdxt_remote_init( XPTR( cxy , &mapper_ptr->rt ),
73                               CONFIG_MAPPER_GRDXT_W1,
74                               CONFIG_MAPPER_GRDXT_W2,
75                               CONFIG_MAPPER_GRDXT_W3 );
[1]76    if( error )
77    {
[683]78
79#if DEBUG_MAPPER_ERROR
80printk("\n[ERROR] in %s : cannot initialize radix tree\n", __FUNCTION__ );
81kmem_remote_free( cxy , mapper_ptr , bits_log2(sizeof(mapper_t)) );
82#endif
[657]83        return XPTR_NULL;
[1]84    }
85
86    // initialize mapper lock
[657]87    remote_rwlock_init( XPTR( cxy , &mapper_ptr->lock ) , LOCK_MAPPER_STATE );
[1]88
89    // initialize waiting threads xlist (empty)
[657]90    xlist_root_init( XPTR( cxy , &mapper_ptr->wait_root ) );
[1]91
92    // initialize vsegs xlist (empty)
[657]93    xlist_root_init( XPTR( cxy , &mapper_ptr->vsegs_root ) );
[1]94
[657]95    return XPTR( cxy , mapper_ptr );
[1]96
[204]97}  // end mapper_create()
98
[606]99////////////////////////////////////////
[657]100void mapper_destroy( xptr_t  mapper_xp )
[1]101{
[657]102    xptr_t     page_xp;
[1]103    page_t   * page;
104    uint32_t   found_index = 0;
105    uint32_t   start_index = 0;
106
[657]107    cxy_t      mapper_cxy = GET_CXY( mapper_xp ); 
108    mapper_t * mapper_ptr = GET_PTR( mapper_xp );
109
110    // build extended pointer on radix tree
111    xptr_t rt_xp = XPTR( mapper_cxy , &mapper_ptr->rt );
112
[606]113    // scan radix tree
[1]114    do
115    {
116        // get page from radix tree
[657]117        page_xp = grdxt_remote_get_first( rt_xp,
118                                          start_index , 
119                                          &found_index );
120        page = GET_PTR( page_xp );
121       
[606]122        // release registered pages to PPM
[18]123        if( page != NULL )
[1]124        {
125            // remove page from mapper and release to PPM
[657]126            mapper_remote_release_page( mapper_xp , page );
[1]127
128            // update start_key value for next page
129            start_index = found_index;
130        }
131    }
132    while( page != NULL );
133
[606]134    // release the memory allocated to radix tree itself
[657]135    grdxt_remote_destroy( rt_xp );
[1]136
137    // release memory for mapper descriptor
[683]138    kmem_remote_free( mapper_cxy , mapper_ptr , bits_log2(sizeof(mapper_t)) );
[1]139
[204]140}  // end mapper_destroy()
141
[657]142/////////////////////////////////////////////////
143error_t mapper_handle_miss( xptr_t     mapper_xp,
144                            uint32_t   page_id,
145                            xptr_t   * page_xp_ptr )
[635]146{
147    error_t    error;
148
[657]149    uint32_t   inode_size = 0;   
150    uint32_t   inode_type = 0;
[656]151
[683]152#if DEBUG_MAPPER_HANDLE_MISS || DEBUG_MAPPER_ERROR
153thread_t * this  = CURRENT_THREAD;
154uint32_t   cycle = (uint32_t)hal_get_cycles();
155#endif
[635]156
157    // get target mapper cluster and local pointer
[656]158    cxy_t         mapper_cxy = GET_CXY( mapper_xp );
159    mapper_t    * mapper_ptr = GET_PTR( mapper_xp );
[635]160
[656]161    // get inode pointer
162    vfs_inode_t * inode = hal_remote_lpt( XPTR( mapper_cxy , &mapper_ptr->inode ) );
163
164    // get inode size and type if relevant
165    if( inode != NULL )
166    {
167        inode_size = hal_remote_l32( XPTR( mapper_cxy , &inode->size ) );
168        inode_type = hal_remote_l32( XPTR( mapper_cxy , &inode->type ) );
169    }
170
[635]171#if DEBUG_MAPPER_HANDLE_MISS
172char          name[CONFIG_VFS_MAX_NAME_LENGTH];
173if( (DEBUG_MAPPER_HANDLE_MISS < cycle) && (inode != NULL) )
174{
[656]175    vfs_inode_get_name( XPTR( mapper_cxy , inode ) , name );
176    printk("\n[%s] thread[%x,%x] enter for page %d in <%s> / cxy %x / cycle %d\n",
[635]177    __FUNCTION__, this->process->pid, this->trdid, page_id, name, mapper_cxy, cycle );
178}
179if( (DEBUG_MAPPER_HANDLE_MISS < cycle) && (inode == NULL) )
180{
[656]181    printk("\n[%s] thread[%x,%x] enter for page %d in FAT / cxy %x / cycle %d\n",
[635]182    __FUNCTION__, this->process->pid, this->trdid, page_id, mapper_cxy, cycle );
183}
184#endif
185
[683]186#if( DEBUG_MAPPER_HANDLE_MISS & 1 )
[656]187if( DEBUG_MAPPER_HANDLE_MISS < cycle ) 
188{
189    if (inode != NULL) grdxt_remote_display( XPTR( mapper_cxy , &mapper_ptr->rt ) , name );
190    else               grdxt_remote_display( XPTR( mapper_cxy , &mapper_ptr->rt ) , "FAT" );
191}
192#endif
193
[683]194    // allocate one 4 Kbytes page in the remote mapper cluster
195    void * base_ptr = kmem_remote_alloc( mapper_cxy , 12 , AF_NONE );
196
197    if( base_ptr == NULL )
[635]198    {
[683]199
200#if DEBUG_MAPPER_ERROR
201printk("\n[ERROR] in %s : thread [%x,%x] cannot allocate page in cluster %x / cycle %d\n",
202__FUNCTION__ , this->process->pid, this->trdid , mapper_cxy , cycle );
203#endif
[635]204        return -1;
205    }
206
[683]207    // get pointers on allocated page descrptor
208    xptr_t   page_xp  = ppm_base2page( XPTR( mapper_cxy , base_ptr ) );
209    page_t * page_ptr = GET_PTR( page_xp );
210
[635]211    // initialize the page descriptor
212    page_remote_init( page_xp );
213
[656]214    // initialize specific page descriptor fields
[635]215    hal_remote_s32( XPTR( mapper_cxy , &page_ptr->refcount ) , 1          );
216    hal_remote_s32( XPTR( mapper_cxy , &page_ptr->index )    , page_id    );
217    hal_remote_spt( XPTR( mapper_cxy , &page_ptr->mapper )   , mapper_ptr );
218    hal_remote_s32( XPTR( mapper_cxy , &page_ptr->flags )    , PG_INIT    );
219
220    // insert page in mapper radix tree
221    error = grdxt_remote_insert( XPTR( mapper_cxy , &mapper_ptr->rt),
222                                 page_id,
223                                 page_ptr );
224    if( error )
225    {
[683]226
227#if DEBUG_MAPPER_ERROR
228printk("\n[ERROR] in %s : thread[%x,%x] cannot insert page in mapper / cycle %d\n",
229__FUNCTION__ , this->process->pid, this->trdid , cycle );
230ppm_remote_free_pages( mapper_cxy , page_ptr );
231#endif
[635]232        return -1;
233    }
234
[656]235    // launch I/O operation to load page from IOC device when required:
236    // - it is the FAT mapper
237    // - it is a directory mapper
238    // - it is a file mapper, and it exist data on IOC device for this page
[672]239    if( (inode == NULL) || (inode_type == FILE_TYPE_DIR) || (inode_size > (page_id << 10) ) )
[656]240    {
241        error = vfs_fs_move_page( page_xp , IOC_SYNC_READ );
[635]242
[656]243        if( error )
244        {
[683]245
246#if DEBUG_MAPPER_ERROR
247printk("\n[ERROR] in %s : thread[%x,%x] cannot load page from device / cycle %d\n",
248__FUNCTION__ , this->process->pid, this->trdid , cycle );
249mapper_remote_release_page( mapper_xp , page_ptr );
250#endif
[656]251            return -1;
252         }
[635]253    }
254
255    // return extended pointer on allocated page
256    *page_xp_ptr = page_xp;
257
258#if DEBUG_MAPPER_HANDLE_MISS
[656]259ppn_t ppn = ppm_page2ppn( page_xp );
[635]260if( (DEBUG_MAPPER_HANDLE_MISS < cycle) && (inode != NULL) )
261{
[656]262    printk("\n[%s] thread[%x,%x] exit for page %d in <%s> / page %x / ppn %x\n",
263    __FUNCTION__, this->process->pid, this->trdid, page_id, name, page_ptr, ppn );
[635]264}
265if( (DEBUG_MAPPER_HANDLE_MISS < cycle) && (inode == NULL) )
266{
[656]267    printk("\n[%s] thread[%x,%x] exit for page %d in FAT / page %x / ppn %x\n",
268    __FUNCTION__, this->process->pid, this->trdid, page_id, page_ptr, ppn );
[635]269}
270#endif
271
[683]272#if( DEBUG_MAPPER_HANDLE_MISS & 1 )
[656]273if( DEBUG_MAPPER_HANDLE_MISS < cycle ) 
274{
275    if (inode != NULL) grdxt_remote_display( XPTR( mapper_cxy , &mapper_ptr->rt ) , name );
276    else               grdxt_remote_display( XPTR( mapper_cxy , &mapper_ptr->rt ) , "FAT" );
277}
278#endif
279
[635]280    return 0;
281
[657]282}  // end mapper_handle_miss()
[635]283
[657]284/////////////////////////////////////////////
285xptr_t  mapper_get_page( xptr_t    mapper_xp,
286                         uint32_t  page_id )
[1]287{
[183]288    error_t       error;
[1]289
[606]290    thread_t * this = CURRENT_THREAD;
291
292    // get mapper cluster and local pointer
[656]293    mapper_t * mapper_ptr = GET_PTR( mapper_xp );
294    cxy_t      mapper_cxy = GET_CXY( mapper_xp );
[606]295
[672]296assert( __FUNCTION__, (hal_remote_lpt( XPTR( mapper_cxy , &mapper_ptr->inode ) ) != NULL ),
[657]297"should not be used for the FAT mapper");
298
[438]299#if DEBUG_MAPPER_GET_PAGE
[625]300uint32_t      cycle = (uint32_t)hal_get_cycles();
[606]301char          name[CONFIG_VFS_MAX_NAME_LENGTH];
[657]302if( DEBUG_MAPPER_GET_PAGE < cycle ) 
[625]303{
[657]304    vfs_inode_t * inode = hal_remote_lpt( XPTR( mapper_cxy , &mapper_ptr->inode ) );
[625]305    vfs_inode_get_name( XPTR( mapper_cxy , inode ) , name );
306    printk("\n[%s] thread[%x,%x] enter for page %d of <%s> mapper / cycle %d\n",
307    __FUNCTION__, this->process->pid, this->trdid, page_id, name, cycle );
308}
[435]309#endif
[204]310
[683]311#if( DEBUG_MAPPER_GET_PAGE & 1 )
[656]312if( DEBUG_MAPPER_GET_PAGE < cycle ) 
313ppm_remote_display( local_cxy );
314#endif
315
[581]316    // check thread can yield
317    thread_assert_can_yield( this , __FUNCTION__ );
318
[606]319    // build extended pointer on mapper lock and mapper rt
[656]320    xptr_t lock_xp  = XPTR( mapper_cxy , &mapper_ptr->lock );
321    xptr_t rt_xp    = XPTR( mapper_cxy , &mapper_ptr->rt );
[606]322
[1]323    // take mapper lock in READ_MODE
[606]324    remote_rwlock_rd_acquire( lock_xp );
[1]325
326    // search page in radix tree
[656]327    xptr_t page_xp  = grdxt_remote_lookup( rt_xp , page_id );
[1]328
[606]329    // test mapper miss
[635]330    if( page_xp == XPTR_NULL )                  // miss => handle it
[1]331    {
332        // release the lock in READ_MODE and take it in WRITE_MODE
[606]333        remote_rwlock_rd_release( lock_xp );
334        remote_rwlock_wr_acquire( lock_xp );
[1]335
[606]336        // second test on missing page because the page status can be modified
[1]337        // by another thread, when passing from READ_MODE to WRITE_MODE.
338        // from this point there is no concurrent accesses to mapper.
[606]339        page_xp = grdxt_remote_lookup( rt_xp , page_id );
[1]340
[606]341        if ( page_xp == XPTR_NULL )  // miss confirmed => handle it
[1]342        {
[657]343            error = mapper_handle_miss( mapper_xp,
344                                        page_id,
345                                        &page_xp );
[635]346            if( error )
[610]347            {
[683]348
349#if DEBUG_MAPPER_ERROR
350printk("\n[ERROR] in %s : thread[%x,%x] cannot handle mapper miss\n",
351__FUNCTION__ , this->process->pid, this->trdid );
352remote_rwlock_wr_release( lock_xp );
353#endif
[606]354                return XPTR_NULL;
[1]355            }
356        }
[635]357
358#if (DEBUG_MAPPER_GET_PAGE & 1)
[657]359if( DEBUG_MAPPER_GET_PAGE < cycle )
360printk("\n[%s] thread[%x,%x] introduced missing page %d in <%s> mapper / ppn %x\n",
361__FUNCTION__, this->process->pid, this->trdid, page_id, name, ppm_page2ppn(page_xp) );
[635]362#endif
[606]363       
364        // release mapper lock from WRITE_MODE
365        remote_rwlock_wr_release( lock_xp );
[1]366    }
[606]367    else                                              // hit
[1]368    {
[606]369        // release mapper lock from READ_MODE
370        remote_rwlock_rd_release( lock_xp );
[1]371    }
372
[438]373#if DEBUG_MAPPER_GET_PAGE
[657]374if( DEBUG_MAPPER_GET_PAGE < cycle )
375printk("\n[%s] thread[%x,%x] exit for page %d of <%s> mapper / ppn %x\n",
376__FUNCTION__, this->process->pid, this->trdid, page_id, name, ppm_page2ppn(page_xp) );
[435]377#endif
[204]378
[683]379#if( DEBUG_MAPPER_GET_PAGE & 1)
[656]380if( DEBUG_MAPPER_GET_PAGE < cycle ) 
381ppm_remote_display( local_cxy );
382#endif
383
[606]384    return page_xp;
[204]385
[657]386}  // end mapper_get_page()
[204]387
[657]388/////////////////////////////////////////////////
389xptr_t  mapper_get_fat_page( xptr_t    mapper_xp,
390                             uint32_t  page_id )
391{
392    error_t       error;
393
394    thread_t * this = CURRENT_THREAD;
395
396    // get mapper cluster and local pointer
397    mapper_t * mapper_ptr = GET_PTR( mapper_xp );
398    cxy_t      mapper_cxy = GET_CXY( mapper_xp );
399
[672]400assert( __FUNCTION__, (hal_remote_lpt( XPTR( mapper_cxy , &mapper_ptr->inode ) ) == NULL ),
[657]401"should be used for the FAT mapper");
402
403#if DEBUG_MAPPER_GET_FAT_PAGE
404uint32_t      cycle = (uint32_t)hal_get_cycles();
405if( DEBUG_MAPPER_GET_FAT_PAGE < cycle ) 
406printk("\n[%s] thread[%x,%x] enter for page %d of FAT mapper / cycle %d\n",
407__FUNCTION__, this->process->pid, this->trdid, page_id, cycle );
408#endif
409
410#if( DEBUG_MAPPER_GET_FAT_PAGE & 2 )
411if( DEBUG_MAPPER_GET_FAT_PAGE < cycle ) 
412ppm_remote_display( local_cxy );
413#endif
414
415    // check thread can yield
416    thread_assert_can_yield( this , __FUNCTION__ );
417
418    // build extended pointer on mapper lock and mapper rt
419    xptr_t lock_xp  = XPTR( mapper_cxy , &mapper_ptr->lock );
420    xptr_t rt_xp    = XPTR( mapper_cxy , &mapper_ptr->rt );
421
422    // take mapper lock in READ_MODE
423    remote_rwlock_rd_acquire( lock_xp );
424
425    // search page in radix tree
426    xptr_t page_xp  = grdxt_remote_lookup( rt_xp , page_id );
427
428    // test mapper miss
429    if( page_xp == XPTR_NULL )                  // miss => handle it
430    {
431        // release the lock in READ_MODE and take it in WRITE_MODE
432        remote_rwlock_rd_release( lock_xp );
433        remote_rwlock_wr_acquire( lock_xp );
434
435        // second test on missing page because the page status can be modified
436        // by another thread, when passing from READ_MODE to WRITE_MODE.
437        // from this point there is no concurrent accesses to mapper.
438        page_xp = grdxt_remote_lookup( rt_xp , page_id );
439
440        if ( page_xp == XPTR_NULL )  // miss confirmed => handle it
441        {
442            error = mapper_handle_miss( mapper_xp,
443                                        page_id,
444                                        &page_xp );
445            if( error )
446            {
[683]447
448#if DEBUG_MAPPER_ERROR
449printk("\n[ERROR] in %s : thread[%x,%x] cannot handle mapper miss\n",
450__FUNCTION__ , this->process->pid, this->trdid );
451remote_rwlock_wr_release( lock_xp );
452#endif
[657]453                return XPTR_NULL;
454            }
455        }
456
457#if (DEBUG_MAPPER_GET_FAT_PAGE & 1)
458if( DEBUG_MAPPER_GET_FAT_PAGE < cycle )
459printk("\n[%s] thread[%x,%x] introduced missing page %d in FAT mapper / ppn %x\n",
460__FUNCTION__, this->process->pid, this->trdid, page_id, ppm_page2ppn(page_xp) );
461#endif
462       
463        // release mapper lock from WRITE_MODE
464        remote_rwlock_wr_release( lock_xp );
465    }
466    else                                              // hit
467    {
468        // release mapper lock from READ_MODE
469        remote_rwlock_rd_release( lock_xp );
470    }
471
472#if DEBUG_MAPPER_GET_FAT_PAGE
473if( DEBUG_MAPPER_GET_FAT_PAGE < cycle )
474printk("\n[%s] thread[%x,%x] exit for page %d of FAT mapper  / ppn %x\n",
475__FUNCTION__, this->process->pid, this->trdid, page_id, ppm_page2ppn(page_xp) );
476#endif
477
[683]478#if( DEBUG_MAPPER_GET_FAT_PAGE & 1)
[657]479if( DEBUG_MAPPER_GET_FAT_PAGE < cycle ) 
480ppm_remote_display( local_cxy );
481#endif
482
483    return page_xp;
484
485}  // end mapper_get_fat_page()
486
[635]487////////////////////////////////////////////////////
488void mapper_remote_release_page( xptr_t   mapper_xp,
489                                 page_t * page )
[1]490{
[635]491    // get mapper cluster an local pointer
492    cxy_t      mapper_cxy = GET_CXY( mapper_xp );
493    mapper_t * mapper_ptr = GET_PTR( mapper_xp );
[1]494
[606]495    // build extended pointer on mapper lock
[635]496    xptr_t lock_xp = XPTR( mapper_cxy , &mapper_ptr->lock );
[606]497
[1]498    // take mapper lock in WRITE_MODE
[635]499    remote_rwlock_wr_acquire( lock_xp );
[1]500
501    // remove physical page from radix tree
[635]502    grdxt_remote_remove( XPTR( mapper_cxy , &mapper_ptr->rt ) , page->index );
[1]503
504    // release mapper lock from WRITE_MODE
[635]505    remote_rwlock_wr_release( lock_xp );
[1]506
507    // release page to PPM
[635]508    ppm_remote_free_pages( mapper_cxy , page );
509                           
[204]510}  // end mapper_release_page()
511
[610]512///////////////////////////////////////////////
513error_t mapper_move_user( xptr_t     mapper_xp,
[313]514                          bool_t     to_buffer,
515                          uint32_t   file_offset,
516                          void     * buffer,
517                          uint32_t   size )
[1]518{
[23]519    uint32_t   page_offset;    // first byte to move to/from a mapper page
[628]520    uint32_t   page_bytes;     // number of bytes to move to/from a mapper page
[606]521    uint32_t   page_id;        // current mapper page index
[23]522    uint32_t   done;           // number of moved bytes
[606]523    xptr_t     page_xp;        // extended pointer on current mapper page descriptor
[330]524
[438]525#if DEBUG_MAPPER_MOVE_USER
[626]526uint32_t      cycle      = (uint32_t)hal_get_cycles();
527thread_t    * this       = CURRENT_THREAD;
528cxy_t         mapper_cxy = GET_CXY( mapper_xp );
529mapper_t    * mapper_ptr = GET_PTR( mapper_xp );
530vfs_inode_t * inode_ptr  = hal_remote_lpt( XPTR( mapper_cxy , &mapper_ptr->inode ) );
531xptr_t        inode_xp   = XPTR( mapper_cxy , inode_ptr );
532char          name[CONFIG_VFS_MAX_NAME_LENGTH];
533vfs_inode_get_name( inode_xp , name );
[438]534if( DEBUG_MAPPER_MOVE_USER < cycle )
[626]535{
536    if( to_buffer )
537    printk("\n[%s] thread[%x,%x] : mapper(%s) -> buffer(%x) / bytes %d / cycle %d\n",
538    __FUNCTION__, this->process->pid, this->trdid, name, buffer, size, cycle );
539    else
540    printk("\n[%s] thread[%x,%x] : buffer(%x) -> mapper(%s) / bytes %d / cycle %d\n",
541    __FUNCTION__, this->process->pid, this->trdid, buffer, name, size, cycle );
542}
[435]543#endif
[1]544
[628]545    // compute indexes of first and last bytes in file
[23]546    uint32_t min_byte = file_offset;
[606]547    uint32_t max_byte = file_offset + size - 1;
[1]548
[23]549    // compute indexes of pages for first and last byte in mapper
[683]550    uint32_t first = min_byte >> CONFIG_PPM_PAGE_ORDER;
551    uint32_t last  = max_byte >> CONFIG_PPM_PAGE_ORDER;
[1]552
[606]553#if (DEBUG_MAPPER_MOVE_USER & 1)
554if( DEBUG_MAPPER_MOVE_USER < cycle )
[626]555printk("\n[%s] thread[%x,%x] : mapper(%x,%x) / first_page %d / last_page %d\n",
556__FUNCTION__, this->process->pid, this->trdid, mapper_cxy, mapper_ptr, first, last );
[606]557#endif
558
[23]559    done = 0;
[1]560
[23]561    // loop on pages in mapper
[606]562    for( page_id = first ; page_id <= last ; page_id++ )
[1]563    {
[183]564        // compute page_offset
[606]565        if( page_id == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK;
566        else                   page_offset = 0;
[1]567
[313]568        // compute number of bytes in page
[628]569        if      ( first   == last  ) page_bytes = size;
570        else if ( page_id == first ) page_bytes = CONFIG_PPM_PAGE_SIZE - page_offset;
571        else if ( page_id == last  ) page_bytes = (max_byte & CONFIG_PPM_PAGE_MASK) + 1;
572        else                         page_bytes = CONFIG_PPM_PAGE_SIZE;
[1]573
[438]574#if (DEBUG_MAPPER_MOVE_USER & 1)
575if( DEBUG_MAPPER_MOVE_USER < cycle )
[626]576printk("\n[%s] thread[%x,%x] : page_id %d / page_offset %d / bytes %d\n",
[628]577__FUNCTION__, this->process->pid, this->trdid, page_id , page_offset , page_bytes );
[435]578#endif
[265]579
[628]580        // get extended pointer on page descriptor in mapper
[657]581        page_xp = mapper_get_page( mapper_xp , page_id ); 
[1]582
[606]583        if ( page_xp == XPTR_NULL ) return -1;
[1]584
[651]585        // compute extended pointer on kernel mapper
[637]586        xptr_t     map_xp  = ppm_page2base( page_xp ) + page_offset;
[626]587
[610]588#if (DEBUG_MAPPER_MOVE_USER & 1)
589if( DEBUG_MAPPER_MOVE_USER < cycle )
[626]590printk("\n[%s] thread[%x,%x] : get buffer(%x,%x) in mapper\n",
[651]591__FUNCTION__, this->process->pid, this->trdid, GET_CXY(map_xp), GET_PTR(map_xp) );
[610]592#endif
[626]593        // compute pointer in user buffer
[606]594        uint8_t * buf_ptr = (uint8_t *)buffer + done;
[1]595
596        // move fragment
[330]597        if( to_buffer )
[1]598        {
[637]599            hal_copy_to_uspace( buf_ptr , map_xp , page_bytes ); 
[626]600
601#if DEBUG_MAPPER_MOVE_USER & 1
602if( DEBUG_MAPPER_MOVE_USER < cycle )
603printk("\n[%s] thread[%x,%x] moved %d bytes / mapper %s (%x,%x) -> user buffer(%x,%x)\n",
[628]604__FUNCTION__, this->process->pid, this->trdid, page_bytes,
[637]605name, GET_CXY(map_xp), GET_PTR(map_xp), local_cxy, buf_ptr );
[626]606#endif
607
[1]608        }
[330]609        else
[1]610        {
[606]611            ppm_page_do_dirty( page_xp ); 
[637]612            hal_copy_from_uspace( map_xp , buf_ptr , page_bytes ); 
[626]613
614#if DEBUG_MAPPER_MOVE_USER & 1
615if( DEBUG_MAPPER_MOVE_USER < cycle )
616printk("\n[%s] thread[%x,%x] moved %d bytes / user buffer(%x,%x) -> mapper %s (%x,%x)\n",
[628]617__FUNCTION__, this->process->pid, this->trdid, page_bytes,
[637]618local_cxy, buf_ptr, name, GET_CXY(map_xp), GET_PTR(map_xp) );
[657]619mapper_display_page(  mapper_xp , page_id , 128 );
[626]620#endif
621
[1]622        }
623
[628]624        done += page_bytes;
[1]625    }
626
[438]627#if DEBUG_MAPPER_MOVE_USER
[626]628cycle      = (uint32_t)hal_get_cycles();
[438]629if( DEBUG_MAPPER_MOVE_USER < cycle )
[626]630{
631    if( to_buffer )
632    printk("\n[%s] thread[%x,%x] completed mapper(%s) -> buffer(%x) / cycle %d\n",
633    __FUNCTION__, this->process->pid, this->trdid, name, buffer, cycle );
634    else
635    printk("\n[%s] thread[%x,%x] completed buffer(%x) -> mapper(%s) / cycle %d\n",
636    __FUNCTION__, this->process->pid, this->trdid, buffer, name, cycle );
637}
[435]638#endif
[204]639
[1]640    return 0;
641
[313]642}  // end mapper_move_user()
[204]643
[313]644////////////////////////////////////////////////
[606]645error_t mapper_move_kernel( xptr_t    mapper_xp,
646                            bool_t    to_buffer,
647                            uint32_t  file_offset,
648                            xptr_t    buffer_xp,
649                            uint32_t  size )
[313]650{
651    uint32_t   page_offset;    // first byte to move to/from a mapper page
[628]652    uint32_t   page_bytes;     // number of bytes to move to/from a mapper page
[606]653    uint32_t   page_id;        // current mapper page index
[313]654    uint32_t   done;           // number of moved bytes
[606]655    xptr_t     page_xp;        // extended pointer on current mapper page descriptor
[313]656
657    uint8_t  * src_ptr;        // source buffer local pointer
658    cxy_t      src_cxy;        // source cluster
659    uint8_t  * dst_ptr;        // destination buffer local pointer
660    cxy_t      dst_cxy;        // destination cluster
[330]661
[406]662    // get buffer cluster and local pointer
663    cxy_t     buffer_cxy = GET_CXY( buffer_xp );
[606]664    uint8_t * buffer_ptr = GET_PTR( buffer_xp );
[313]665
[606]666    // get mapper cluster
667    cxy_t     mapper_cxy = GET_CXY( mapper_xp );
668
[438]669#if DEBUG_MAPPER_MOVE_KERNEL
[625]670char          name[CONFIG_VFS_MAX_NAME_LENGTH];
671uint32_t      cycle  = (uint32_t)hal_get_cycles();
672thread_t    * this   = CURRENT_THREAD;
673mapper_t    * mapper = GET_PTR( mapper_xp );
674vfs_inode_t * inode  = hal_remote_lpt( XPTR( mapper_cxy , &mapper->inode ) );
675vfs_inode_get_name( XPTR( mapper_cxy , inode ) , name );
[438]676if( DEBUG_MAPPER_MOVE_KERNEL < cycle )
[625]677printk("\n[%s] thread[%x,%x] enter / %d bytes / offset %d / mapper <%s> / cycle %d\n",
678__FUNCTION__, this->process->pid, this->trdid, size, file_offset, name, cycle );
[435]679#endif
[406]680
[313]681    // compute offsets of first and last bytes in file
682    uint32_t min_byte = file_offset;
683    uint32_t max_byte = file_offset + size -1;
684
685    // compute indexes for first and last pages in mapper
[683]686    uint32_t first = min_byte >> CONFIG_PPM_PAGE_ORDER;
687    uint32_t last  = max_byte >> CONFIG_PPM_PAGE_ORDER;
[313]688
689    // compute source and destination clusters
690    if( to_buffer )
691    {
692        dst_cxy = buffer_cxy;
[606]693        src_cxy = mapper_cxy;
[313]694    }
695    else
696    {
697        src_cxy = buffer_cxy;
[606]698        dst_cxy = mapper_cxy;
[313]699    }
700
701    done = 0;
702
703    // loop on pages in mapper
[606]704    for( page_id = first ; page_id <= last ; page_id++ )
[313]705    {
706        // compute page_offset
[606]707        if( page_id == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK;
708        else                   page_offset = 0;
[313]709
710        // compute number of bytes to move in page
[628]711        if      ( first == last  )   page_bytes = size;
712        else if ( page_id == first ) page_bytes = CONFIG_PPM_PAGE_SIZE - page_offset;
713        else if ( page_id == last  ) page_bytes = (max_byte & CONFIG_PPM_PAGE_MASK) + 1;
714        else                         page_bytes = CONFIG_PPM_PAGE_SIZE;
[313]715
[606]716        // get extended pointer on page descriptor
[657]717        page_xp = mapper_get_page( mapper_xp , page_id );
[313]718
[606]719        if ( page_xp == XPTR_NULL ) return -1;
[313]720
[315]721        // get page base address
[606]722        xptr_t    base_xp  = ppm_page2base( page_xp );
[367]723        uint8_t * base_ptr = (uint8_t *)GET_PTR( base_xp );
[330]724
[313]725        // compute source and destination pointers
726        if( to_buffer )
727        {
[315]728            dst_ptr = buffer_ptr + done;
[367]729            src_ptr = base_ptr + page_offset;
[313]730        }
731        else
732        {
[315]733            src_ptr = buffer_ptr + done;
[367]734            dst_ptr = base_ptr + page_offset;
[313]735
[606]736            ppm_page_do_dirty( page_xp );
[313]737        }
738
[610]739#if (DEBUG_MAPPER_MOVE_KERNEL & 1)
740if( DEBUG_MAPPER_MOVE_KERNEL < cycle )
[625]741{
742    if( to_buffer )
[656]743    printk("\n[%s] mapper <%s> page %d => buffer (%x,%x) / %d bytes\n",
[628]744    __FUNCTION__, name, page_id, dst_cxy, dst_ptr, page_bytes );
[625]745    else
[656]746    printk("\n[%s] buffer (%x,%x) => mapper <%s> page %d / %d bytes\n",
[628]747    __FUNCTION__, src_cxy, src_ptr, name, page_id, page_bytes );
[625]748}
[610]749#endif
750
[313]751        // move fragment
[628]752        hal_remote_memcpy( XPTR( dst_cxy , dst_ptr ), XPTR( src_cxy , src_ptr ), page_bytes );
[330]753
[628]754        done += page_bytes;
[313]755    }
756
[438]757#if DEBUG_MAPPER_MOVE_KERNEL
[625]758cycle  = (uint32_t)hal_get_cycles();
[438]759if( DEBUG_MAPPER_MOVE_KERNEL < cycle )
[656]760printk("\n[%s] thread[%x,%x] exit / mapper <%s> / buffer (%x,%x) / cycle %d\n",
761__FUNCTION__, this->process->pid, this->trdid, name, buffer_cxy, buffer_ptr, cycle );
[435]762#endif
[313]763
764    return 0;
765
[406]766}  // end mapper_move_kernel()
[313]767
[606]768///////////////////////////////////////////////////
769error_t mapper_remote_get_32( xptr_t     mapper_xp,
[628]770                              uint32_t   page_id,
[606]771                              uint32_t   word_id,
[628]772                              uint32_t * value )
[606]773{
774    xptr_t     page_xp;      // extended pointer on searched page descriptor
775    xptr_t     base_xp;      // extended pointer on searched page base
776   
777    // get page containing the searched word
[657]778    page_xp  = mapper_get_page( mapper_xp , page_id );
[606]779
780    if( page_xp == XPTR_NULL )  return -1;
781   
782    // get page base
783    base_xp = ppm_page2base( page_xp );
784
785    // get the value from mapper
[628]786    *value = hal_remote_l32( base_xp + (word_id<<2) ); 
[606]787
788    return 0;
789
790}  // end mapper_remote_get_32()
791
792///////////////////////////////////////////////////
793error_t mapper_remote_set_32( xptr_t     mapper_xp,
[628]794                              uint32_t   page_id,
[606]795                              uint32_t   word_id,
796                              uint32_t   value )
797{
798    xptr_t     page_xp;      // extended pointer on searched page descriptor
799    xptr_t     base_xp;      // extended pointer on searched page base
800
801    // get page containing the searched word
[657]802    page_xp  = mapper_get_page( mapper_xp , page_id );
[606]803
804    if( page_xp == XPTR_NULL ) return -1;
805
806    // get page base
807    base_xp = ppm_page2base( page_xp );
808
809    // set value to mapper
[628]810    hal_remote_s32( (base_xp + (word_id << 2)) , value );
[606]811
[628]812    // set the dirty flag in page descriptor
[606]813    ppm_page_do_dirty( page_xp );
814
815    return 0;
816
817}  // end mapper_remote_set_32()
818
[657]819////////////////////////////////////////
820error_t mapper_sync( xptr_t  mapper_xp )
[623]821{
[657]822    uint32_t   found_key;           // unused, required by grdxt_remote_get_first()
[623]823    error_t    error;
824
[657]825    // get mapper cluster and local pointer
826    mapper_t * mapper_ptr = GET_PTR( mapper_xp );
827    cxy_t      mapper_cxy = GET_CXY( mapper_xp );
828
[623]829#if DEBUG_MAPPER_SYNC
830thread_t * this  = CURRENT_THREAD;
831uint32_t   cycle = (uint32_t)hal_get_cycles();
832char       name[CONFIG_VFS_MAX_NAME_LENGTH];
[657]833vfs_inode_get_name( XPTR( mapper_cxy , &mapper_ptr->inode ) , name );
[623]834#endif
835
[657]836    // build extended pointer on radix tree
837    xptr_t   rt_xp = XPTR( mapper_cxy , &mapper_ptr->rt );
[623]838
839    // initialise loop variable
[657]840    uint32_t start_key = 0;
[623]841
842    // scan radix-tree until last page found
843    while( 1 )
844    {
845        // get page descriptor from radix tree
[657]846        xptr_t page_xp = grdxt_remote_get_first( rt_xp , start_key , &found_key );
[623]847         
[657]848        page_t * page_ptr = GET_PTR( page_xp );
[623]849
[657]850        // exit loop when last page found
851        if( page_ptr == NULL ) break;
[623]852
[657]853        // get page flags & index fields
854        uint32_t flags = hal_remote_l32( XPTR( mapper_cxy , &page_ptr->flags ) );
855        uint32_t index = hal_remote_l32( XPTR( mapper_cxy , &page_ptr->index ) );
[623]856
857        // synchronize page if dirty
[657]858        if( flags & PG_DIRTY )
[623]859        {
860
861#if DEBUG_MAPPER_SYNC
862if( cycle > DEBUG_MAPPER_SYNC )
[626]863printk("\n[%s] thread[%x,%x] synchonise page %d of <%s> to IOC device\n",
[657]864__FUNCTION__, this->process->pid, this->trdid, page_ptr->index, name );
[623]865#endif
866            // copy page to file system
867            error = vfs_fs_move_page( page_xp , IOC_WRITE );
868
869            if( error )
870            {
[683]871
872#if DEBUG_MAPPER_SYNC
873printk("\n[ERROR] in %s : cannot synchonize dirty page %d\n", 
874__FUNCTION__, page_ptr->index );
875#endif
[623]876                return -1;
877            }
878
879            // remove page from PPM dirty list
880            ppm_page_undo_dirty( page_xp ); 
881        } 
882        else
883        {
884
885#if DEBUG_MAPPER_SYNC
886if( cycle > DEBUG_MAPPER_SYNC )
887printk("\n[%s] thread[%x,%x] skip page %d for <%s>\n",
[657]888__FUNCTION__, this->process->pid, this->trdid, page_ptr->index, name );
[623]889#endif
890        }
891
892        // update loop variable
[657]893        start_key = index + 1;
[623]894    }  // end while
895
896    return 0;
897
898}  // end mapper_sync()
899
[656]900///////////////////////////////////////////////
901void mapper_display_page( xptr_t     mapper_xp,
[657]902                          uint32_t   page_id,
[656]903                          uint32_t   nbytes )
[611]904{
[614]905    char          buffer[4096];   // local buffer
906    uint32_t      line;           // line index
907    uint32_t      word;           // word index
908 
909    char       name[CONFIG_VFS_MAX_NAME_LENGTH];
[606]910
[672]911assert( __FUNCTION__, (nbytes <= 4096)         , "nbytes cannot be larger than 4096");
912assert( __FUNCTION__, (mapper_xp != XPTR_NULL) , "mapper_xp argument cannot be null");
[611]913
[656]914    // get mapper cluster and local pointer
915    cxy_t      mapper_cxy = GET_CXY( mapper_xp );
916    mapper_t * mapper_ptr = GET_PTR( mapper_xp );
[611]917
[657]918    // get extended pointer on page descriptor
919    xptr_t page_xp = mapper_get_page( mapper_xp , page_id );
920
921    // get page cluster and local pointer
[656]922    cxy_t    page_cxy = GET_CXY( page_xp );
923    page_t * page_ptr = GET_PTR( page_xp );
[614]924
[656]925    // get page_id and mapper from page descriptor
[657]926    uint32_t   index   = hal_remote_l32( XPTR( page_cxy , &page_ptr->index ) );
[656]927    mapper_t * mapper  = hal_remote_lpt( XPTR( page_cxy , &page_ptr->mapper ) );
928
[672]929assert( __FUNCTION__, (mapper_cxy == page_cxy ) , "mapper and page must be in same cluster");
930assert( __FUNCTION__, (mapper_ptr == mapper   ) , "unconsistent mapper field in page descriptor");
931assert( __FUNCTION__, (page_id    == index    ) , "unconsistent index  field in page descriptor");
[656]932
[614]933    // get inode
[656]934    vfs_inode_t * inode_ptr = hal_remote_lpt( XPTR( mapper_cxy , &mapper_ptr->inode ) );
[614]935
936    // get inode name
[656]937    if( inode_ptr == NULL ) strcpy( name , "FAT" );
[614]938    else  vfs_inode_get_name( XPTR( mapper_cxy , inode_ptr ) , name );
939   
[611]940    // get extended pointer on page base
[656]941    xptr_t base_xp = ppm_page2base( page_xp );
[611]942   
943    // copy remote page to local buffer
944    hal_remote_memcpy( XPTR( local_cxy , buffer ) , base_xp , nbytes );
945
[656]946    // display header
947    uint32_t * tabi = (uint32_t *)buffer;
948    printk("\n***** mapper <%s> / page_id %d / cxy %x / mapper %x / buffer %x\n",
949    name, page_id, mapper_cxy, mapper_ptr, GET_PTR( base_xp ) );
950
[611]951    // display 8 words per line
952    for( line = 0 ; line < (nbytes >> 5) ; line++ )
953    {
[625]954        printk("%X : ", line << 5 );
[614]955        for( word = 0 ; word < 8 ; word++ ) printk("%X ", tabi[(line<<3) + word] );
[611]956        printk("\n");
957    }
958
[656]959}  // end mapper_display_page()
[611]960
961
Note: See TracBrowser for help on using the repository browser.