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

Last change on this file since 656 was 656, checked in by alain, 4 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
Line 
1/*
2 * mapper.c - Kernel cache for FS files or directories implementation.
3 *
4 * Authors   Mohamed Lamine Karaoui (2015)
5 *           Alain Greiner (2016,2017,2018,2019)
6 *
7 * Copyright (c)  UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <kernel_config.h>
26#include <hal_kernel_types.h>
27#include <hal_special.h>
28#include <hal_uspace.h>
29#include <hal_vmm.h>
30#include <grdxt.h>
31#include <string.h>
32#include <rwlock.h>
33#include <printk.h>
34#include <memcpy.h>
35#include <thread.h>
36#include <core.h>
37#include <process.h>
38#include <kmem.h>
39#include <kcm.h>
40#include <ppm.h>
41#include <page.h>
42#include <cluster.h>
43#include <vfs.h>
44#include <mapper.h>
45#include <dev_ioc.h>
46
47
48//////////////////////////////////////////////
49mapper_t * mapper_create( vfs_fs_type_t type )
50{
51    mapper_t * mapper;
52    kmem_req_t req;
53    error_t    error;
54
55    // allocate memory for mapper descriptor
56    req.type  = KMEM_KCM;
57    req.order = bits_log2( sizeof(mapper_t) );
58    req.flags = AF_KERNEL | AF_ZERO;
59    mapper    = kmem_alloc( &req );
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
68    mapper->refcount = 0;
69    mapper->inode    = NULL;
70
71    // initialize radix tree
72    error = grdxt_init( &mapper->rt,
73                        CONFIG_MAPPER_GRDXT_W1,
74                        CONFIG_MAPPER_GRDXT_W2,
75                        CONFIG_MAPPER_GRDXT_W3 );
76    if( error )
77    {
78        printk("\n[ERROR] in %s : cannot initialize radix tree\n", __FUNCTION__ );
79        req.type  = KMEM_KCM;
80        req.ptr   = mapper;
81        kmem_free( &req );
82        return NULL;
83    }
84
85    // initialize mapper type
86    mapper->type = type;
87
88    // initialize mapper lock
89    remote_rwlock_init( XPTR( local_cxy , &mapper->lock ) , LOCK_MAPPER_STATE );
90
91    // initialize waiting threads xlist (empty)
92    xlist_root_init( XPTR( local_cxy , &mapper->wait_root ) );
93
94    // initialize vsegs xlist (empty)
95    xlist_root_init( XPTR( local_cxy , &mapper->vsegs_root ) );
96
97    return mapper;
98
99}  // end mapper_create()
100
101////////////////////////////////////////
102void mapper_destroy( mapper_t * mapper )
103{
104    page_t   * page;
105    uint32_t   found_index = 0;
106    uint32_t   start_index = 0;
107    kmem_req_t req;
108
109    // scan radix tree
110    do
111    {
112        // get page from radix tree
113        page = (page_t *)grdxt_get_first( &mapper->rt , start_index , &found_index );
114
115        // release registered pages to PPM
116        if( page != NULL )
117        {
118            // remove page from mapper and release to PPM
119            mapper_remote_release_page( XPTR( local_cxy , mapper ) , page );
120
121            // update start_key value for next page
122            start_index = found_index;
123        }
124    }
125    while( page != NULL );
126
127    // release the memory allocated to radix tree itself
128    grdxt_destroy( &mapper->rt );
129
130    // release memory for mapper descriptor
131    req.type = KMEM_KCM;
132    req.ptr  = mapper;
133    kmem_free( &req );
134
135}  // end mapper_destroy()
136
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
144    uint32_t   inode_size;   
145    uint32_t   inode_type;
146
147    thread_t * this = CURRENT_THREAD;
148
149    // get target mapper cluster and local pointer
150    cxy_t         mapper_cxy = GET_CXY( mapper_xp );
151    mapper_t    * mapper_ptr = GET_PTR( mapper_xp );
152
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
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{
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",
175    __FUNCTION__, this->process->pid, this->trdid, page_id, name, mapper_cxy, cycle );
176}
177if( (DEBUG_MAPPER_HANDLE_MISS < cycle) && (inode == NULL) )
178{
179    printk("\n[%s] thread[%x,%x] enter for page %d in FAT / cxy %x / cycle %d\n",
180    __FUNCTION__, this->process->pid, this->trdid, page_id, mapper_cxy, cycle );
181}
182#endif
183
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
192    // allocate one 4 Kbytes page from the remote mapper cluster
193    xptr_t page_xp = ppm_remote_alloc_pages( mapper_cxy , 0 );
194    page_t * page_ptr = GET_PTR( page_xp );
195                           
196    if( page_xp == XPTR_NULL )
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
206    // initialize specific page descriptor fields
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
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 );
232
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         }
240    }
241
242    // return extended pointer on allocated page
243    *page_xp_ptr = page_xp;
244
245#if DEBUG_MAPPER_HANDLE_MISS
246ppn_t ppn = ppm_page2ppn( page_xp );
247if( (DEBUG_MAPPER_HANDLE_MISS < cycle) && (inode != NULL) )
248{
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 );
251}
252if( (DEBUG_MAPPER_HANDLE_MISS < cycle) && (inode == NULL) )
253{
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 );
256}
257#endif
258
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
267    return 0;
268
269}  // end mapper_remote_handle_miss()
270
271////////////////////////////////////////////////////
272xptr_t  mapper_remote_get_page( xptr_t    mapper_xp,
273                                uint32_t  page_id )
274{
275    error_t       error;
276
277    thread_t * this = CURRENT_THREAD;
278
279    // get mapper cluster and local pointer
280    mapper_t * mapper_ptr = GET_PTR( mapper_xp );
281    cxy_t      mapper_cxy = GET_CXY( mapper_xp );
282
283#if DEBUG_MAPPER_GET_PAGE
284vfs_inode_t * inode = hal_remote_lpt( XPTR( mapper_cxy , &mapper_ptr->inode ) );
285uint32_t      cycle = (uint32_t)hal_get_cycles();
286char          name[CONFIG_VFS_MAX_NAME_LENGTH];
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}
298#endif
299
300#if( DEBUG_MAPPER_GET_PAGE & 2 )
301if( DEBUG_MAPPER_GET_PAGE < cycle ) 
302ppm_remote_display( local_cxy );
303#endif
304
305    // check thread can yield
306    thread_assert_can_yield( this , __FUNCTION__ );
307
308    // build extended pointer on mapper lock and mapper rt
309    xptr_t lock_xp  = XPTR( mapper_cxy , &mapper_ptr->lock );
310    xptr_t rt_xp    = XPTR( mapper_cxy , &mapper_ptr->rt );
311
312    // take mapper lock in READ_MODE
313    remote_rwlock_rd_acquire( lock_xp );
314
315    // search page in radix tree
316    xptr_t page_xp  = grdxt_remote_lookup( rt_xp , page_id );
317
318    // test mapper miss
319    if( page_xp == XPTR_NULL )                  // miss => handle it
320    {
321        // release the lock in READ_MODE and take it in WRITE_MODE
322        remote_rwlock_rd_release( lock_xp );
323        remote_rwlock_wr_acquire( lock_xp );
324
325        // second test on missing page because the page status can be modified
326        // by another thread, when passing from READ_MODE to WRITE_MODE.
327        // from this point there is no concurrent accesses to mapper.
328        page_xp = grdxt_remote_lookup( rt_xp , page_id );
329
330        if ( page_xp == XPTR_NULL )  // miss confirmed => handle it
331        {
332            error = mapper_remote_handle_miss( mapper_xp,
333                                               page_id,
334                                               &page_xp );
335            if( error )
336            {
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;
341            }
342        }
343
344#if (DEBUG_MAPPER_GET_PAGE & 1)
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}
355#endif
356       
357        // release mapper lock from WRITE_MODE
358        remote_rwlock_wr_release( lock_xp );
359    }
360    else                                              // hit
361    {
362        // release mapper lock from READ_MODE
363        remote_rwlock_rd_release( lock_xp );
364    }
365
366#if DEBUG_MAPPER_GET_PAGE
367cycle = (uint32_t)hal_get_cycles();
368if( (DEBUG_MAPPER_GET_PAGE < cycle) && (inode != NULL) )
369{
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) );
372}
373if( (DEBUG_MAPPER_GET_PAGE < cycle) && (inode == NULL) )
374{
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) );
377}
378#endif
379
380#if( DEBUG_MAPPER_GET_PAGE & 2)
381if( DEBUG_MAPPER_GET_PAGE < cycle ) 
382ppm_remote_display( local_cxy );
383#endif
384
385    return page_xp;
386
387}  // end mapper_remote_get_page()
388
389////////////////////////////////////////////////////
390void mapper_remote_release_page( xptr_t   mapper_xp,
391                                 page_t * page )
392{
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 );
396
397    // build extended pointer on mapper lock
398    xptr_t lock_xp = XPTR( mapper_cxy , &mapper_ptr->lock );
399
400    // take mapper lock in WRITE_MODE
401    remote_rwlock_wr_acquire( lock_xp );
402
403    // remove physical page from radix tree
404    grdxt_remote_remove( XPTR( mapper_cxy , &mapper_ptr->rt ) , page->index );
405
406    // release mapper lock from WRITE_MODE
407    remote_rwlock_wr_release( lock_xp );
408
409    // release page to PPM
410    ppm_remote_free_pages( mapper_cxy , page );
411                           
412}  // end mapper_release_page()
413
414///////////////////////////////////////////////
415error_t mapper_move_user( xptr_t     mapper_xp,
416                          bool_t     to_buffer,
417                          uint32_t   file_offset,
418                          void     * buffer,
419                          uint32_t   size )
420{
421    uint32_t   page_offset;    // first byte to move to/from a mapper page
422    uint32_t   page_bytes;     // number of bytes to move to/from a mapper page
423    uint32_t   page_id;        // current mapper page index
424    uint32_t   done;           // number of moved bytes
425    xptr_t     page_xp;        // extended pointer on current mapper page descriptor
426
427#if DEBUG_MAPPER_MOVE_USER
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 );
436if( DEBUG_MAPPER_MOVE_USER < cycle )
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}
445#endif
446
447    // compute indexes of first and last bytes in file
448    uint32_t min_byte = file_offset;
449    uint32_t max_byte = file_offset + size - 1;
450
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;
454
455#if (DEBUG_MAPPER_MOVE_USER & 1)
456if( DEBUG_MAPPER_MOVE_USER < cycle )
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 );
459#endif
460
461    done = 0;
462
463    // loop on pages in mapper
464    for( page_id = first ; page_id <= last ; page_id++ )
465    {
466        // compute page_offset
467        if( page_id == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK;
468        else                   page_offset = 0;
469
470        // compute number of bytes in page
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;
475
476#if (DEBUG_MAPPER_MOVE_USER & 1)
477if( DEBUG_MAPPER_MOVE_USER < cycle )
478printk("\n[%s] thread[%x,%x] : page_id %d / page_offset %d / bytes %d\n",
479__FUNCTION__, this->process->pid, this->trdid, page_id , page_offset , page_bytes );
480#endif
481
482        // get extended pointer on page descriptor in mapper
483        page_xp = mapper_remote_get_page( mapper_xp , page_id ); 
484
485        if ( page_xp == XPTR_NULL ) return -1;
486
487        // compute extended pointer on kernel mapper
488        xptr_t     map_xp  = ppm_page2base( page_xp ) + page_offset;
489
490#if (DEBUG_MAPPER_MOVE_USER & 1)
491if( DEBUG_MAPPER_MOVE_USER < cycle )
492printk("\n[%s] thread[%x,%x] : get buffer(%x,%x) in mapper\n",
493__FUNCTION__, this->process->pid, this->trdid, GET_CXY(map_xp), GET_PTR(map_xp) );
494#endif
495        // compute pointer in user buffer
496        uint8_t * buf_ptr = (uint8_t *)buffer + done;
497
498        // move fragment
499        if( to_buffer )
500        {
501            hal_copy_to_uspace( buf_ptr , map_xp , page_bytes ); 
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",
506__FUNCTION__, this->process->pid, this->trdid, page_bytes,
507name, GET_CXY(map_xp), GET_PTR(map_xp), local_cxy, buf_ptr );
508#endif
509
510        }
511        else
512        {
513            ppm_page_do_dirty( page_xp ); 
514            hal_copy_from_uspace( map_xp , buf_ptr , page_bytes ); 
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",
519__FUNCTION__, this->process->pid, this->trdid, page_bytes,
520local_cxy, buf_ptr, name, GET_CXY(map_xp), GET_PTR(map_xp) );
521mapper_display_page(  mapper_xp , page_xp , 128 );
522#endif
523
524        }
525
526        done += page_bytes;
527    }
528
529#if DEBUG_MAPPER_MOVE_USER
530cycle      = (uint32_t)hal_get_cycles();
531if( DEBUG_MAPPER_MOVE_USER < cycle )
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}
540#endif
541
542    return 0;
543
544}  // end mapper_move_user()
545
546////////////////////////////////////////////////
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 )
552{
553    uint32_t   page_offset;    // first byte to move to/from a mapper page
554    uint32_t   page_bytes;     // number of bytes to move to/from a mapper page
555    uint32_t   page_id;        // current mapper page index
556    uint32_t   done;           // number of moved bytes
557    xptr_t     page_xp;        // extended pointer on current mapper page descriptor
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
563
564    // get buffer cluster and local pointer
565    cxy_t     buffer_cxy = GET_CXY( buffer_xp );
566    uint8_t * buffer_ptr = GET_PTR( buffer_xp );
567
568    // get mapper cluster
569    cxy_t     mapper_cxy = GET_CXY( mapper_xp );
570
571#if DEBUG_MAPPER_MOVE_KERNEL
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 );
578if( DEBUG_MAPPER_MOVE_KERNEL < cycle )
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 );
581#endif
582
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;
595        src_cxy = mapper_cxy;
596    }
597    else
598    {
599        src_cxy = buffer_cxy;
600        dst_cxy = mapper_cxy;
601    }
602
603    done = 0;
604
605    // loop on pages in mapper
606    for( page_id = first ; page_id <= last ; page_id++ )
607    {
608        // compute page_offset
609        if( page_id == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK;
610        else                   page_offset = 0;
611
612        // compute number of bytes to move in page
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;
617
618        // get extended pointer on page descriptor
619        page_xp = mapper_remote_get_page( mapper_xp , page_id );
620
621        if ( page_xp == XPTR_NULL ) return -1;
622
623        // get page base address
624        xptr_t    base_xp  = ppm_page2base( page_xp );
625        uint8_t * base_ptr = (uint8_t *)GET_PTR( base_xp );
626
627        // compute source and destination pointers
628        if( to_buffer )
629        {
630            dst_ptr = buffer_ptr + done;
631            src_ptr = base_ptr + page_offset;
632        }
633        else
634        {
635            src_ptr = buffer_ptr + done;
636            dst_ptr = base_ptr + page_offset;
637
638            ppm_page_do_dirty( page_xp );
639        }
640
641#if (DEBUG_MAPPER_MOVE_KERNEL & 1)
642if( DEBUG_MAPPER_MOVE_KERNEL < cycle )
643{
644    if( to_buffer )
645    printk("\n[%s] mapper <%s> page %d => buffer (%x,%x) / %d bytes\n",
646    __FUNCTION__, name, page_id, dst_cxy, dst_ptr, page_bytes );
647    else
648    printk("\n[%s] buffer (%x,%x) => mapper <%s> page %d / %d bytes\n",
649    __FUNCTION__, src_cxy, src_ptr, name, page_id, page_bytes );
650}
651#endif
652
653        // move fragment
654        hal_remote_memcpy( XPTR( dst_cxy , dst_ptr ), XPTR( src_cxy , src_ptr ), page_bytes );
655
656        done += page_bytes;
657    }
658
659#if DEBUG_MAPPER_MOVE_KERNEL
660cycle  = (uint32_t)hal_get_cycles();
661if( DEBUG_MAPPER_MOVE_KERNEL < cycle )
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 );
664#endif
665
666    return 0;
667
668}  // end mapper_move_kernel()
669
670///////////////////////////////////////////////////
671error_t mapper_remote_get_32( xptr_t     mapper_xp,
672                              uint32_t   page_id,
673                              uint32_t   word_id,
674                              uint32_t * value )
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
688    *value = hal_remote_l32( base_xp + (word_id<<2) ); 
689
690    return 0;
691
692}  // end mapper_remote_get_32()
693
694///////////////////////////////////////////////////
695error_t mapper_remote_set_32( xptr_t     mapper_xp,
696                              uint32_t   page_id,
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
712    hal_remote_s32( (base_xp + (word_id << 2)) , value );
713
714    // set the dirty flag in page descriptor
715    ppm_page_do_dirty( page_xp );
716
717    return 0;
718
719}  // end mapper_remote_set_32()
720
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
739    rt = &mapper->rt;
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
752assert( (page->index == found_key ), "page_index (%d) != key (%d)", page->index, found_key );
753assert( (page->order == 0), "page_order (%d] != 0", page->order );
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 )
764printk("\n[%s] thread[%x,%x] synchonise page %d of <%s> to IOC device\n",
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
798///////////////////////////////////////////////
799void mapper_display_page( xptr_t     mapper_xp,
800                          xptr_t     page_xp,
801                          uint32_t   nbytes )
802{
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];
808
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");
812
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 );
816
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 );
820
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
828    // get inode
829    vfs_inode_t * inode_ptr = hal_remote_lpt( XPTR( mapper_cxy , &mapper_ptr->inode ) );
830
831    // get inode name
832    if( inode_ptr == NULL ) strcpy( name , "FAT" );
833    else  vfs_inode_get_name( XPTR( mapper_cxy , inode_ptr ) , name );
834   
835    // get extended pointer on page base
836    xptr_t base_xp = ppm_page2base( page_xp );
837   
838    // copy remote page to local buffer
839    hal_remote_memcpy( XPTR( local_cxy , buffer ) , base_xp , nbytes );
840
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
846    // display 8 words per line
847    for( line = 0 ; line < (nbytes >> 5) ; line++ )
848    {
849        printk("%X : ", line << 5 );
850        for( word = 0 ; word < 8 ; word++ ) printk("%X ", tabi[(line<<3) + word] );
851        printk("\n");
852    }
853
854}  // end mapper_display_page()
855
856
Note: See TracBrowser for help on using the repository browser.