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

Last change on this file since 656 was 656, checked in by alain, 5 years ago

Fix several bugs in the FATFS and in the VFS,
related to the creation of big files requiring
more than 4 Kbytes (one cluster) on device.

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