source: trunk/kernel/mm/mapper.c

Last change on this file was 683, checked in by alain, 13 months ago

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

File size: 31.5 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,2020)
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/////////////////////////////////////
49xptr_t  mapper_create( cxy_t     cxy,
50                       uint32_t  type )
51{
52    mapper_t * mapper_ptr;
53    error_t    error;
54
55    // allocate memory for mapper descriptor
56    mapper_ptr  = kmem_remote_alloc( cxy , bits_log2(sizeof(mapper_t)) , AF_ZERO );
57
58    if( mapper_ptr == NULL )
59    {
60
61#if DEBUG_MAPPER_ERROR
62printk("\n[ERROR] in %s : no memory for mapper descriptor\n", __FUNCTION__ );
63#endif
64        return XPTR_NULL;
65    }
66
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 );
70
71    // initialize radix tree
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 );
76    if( error )
77    {
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
83        return XPTR_NULL;
84    }
85
86    // initialize mapper lock
87    remote_rwlock_init( XPTR( cxy , &mapper_ptr->lock ) , LOCK_MAPPER_STATE );
88
89    // initialize waiting threads xlist (empty)
90    xlist_root_init( XPTR( cxy , &mapper_ptr->wait_root ) );
91
92    // initialize vsegs xlist (empty)
93    xlist_root_init( XPTR( cxy , &mapper_ptr->vsegs_root ) );
94
95    return XPTR( cxy , mapper_ptr );
96
97}  // end mapper_create()
98
99////////////////////////////////////////
100void mapper_destroy( xptr_t  mapper_xp )
101{
102    xptr_t     page_xp;
103    page_t   * page;
104    uint32_t   found_index = 0;
105    uint32_t   start_index = 0;
106
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
113    // scan radix tree
114    do
115    {
116        // get page from radix tree
117        page_xp = grdxt_remote_get_first( rt_xp,
118                                          start_index , 
119                                          &found_index );
120        page = GET_PTR( page_xp );
121       
122        // release registered pages to PPM
123        if( page != NULL )
124        {
125            // remove page from mapper and release to PPM
126            mapper_remote_release_page( mapper_xp , page );
127
128            // update start_key value for next page
129            start_index = found_index;
130        }
131    }
132    while( page != NULL );
133
134    // release the memory allocated to radix tree itself
135    grdxt_remote_destroy( rt_xp );
136
137    // release memory for mapper descriptor
138    kmem_remote_free( mapper_cxy , mapper_ptr , bits_log2(sizeof(mapper_t)) );
139
140}  // end mapper_destroy()
141
142/////////////////////////////////////////////////
143error_t mapper_handle_miss( xptr_t     mapper_xp,
144                            uint32_t   page_id,
145                            xptr_t   * page_xp_ptr )
146{
147    error_t    error;
148
149    uint32_t   inode_size = 0;   
150    uint32_t   inode_type = 0;
151
152#if DEBUG_MAPPER_HANDLE_MISS || DEBUG_MAPPER_ERROR
153thread_t * this  = CURRENT_THREAD;
154uint32_t   cycle = (uint32_t)hal_get_cycles();
155#endif
156
157    // get target mapper cluster and local pointer
158    cxy_t         mapper_cxy = GET_CXY( mapper_xp );
159    mapper_t    * mapper_ptr = GET_PTR( mapper_xp );
160
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
171#if DEBUG_MAPPER_HANDLE_MISS
172char          name[CONFIG_VFS_MAX_NAME_LENGTH];
173if( (DEBUG_MAPPER_HANDLE_MISS < cycle) && (inode != NULL) )
174{
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",
177    __FUNCTION__, this->process->pid, this->trdid, page_id, name, mapper_cxy, cycle );
178}
179if( (DEBUG_MAPPER_HANDLE_MISS < cycle) && (inode == NULL) )
180{
181    printk("\n[%s] thread[%x,%x] enter for page %d in FAT / cxy %x / cycle %d\n",
182    __FUNCTION__, this->process->pid, this->trdid, page_id, mapper_cxy, cycle );
183}
184#endif
185
186#if( DEBUG_MAPPER_HANDLE_MISS & 1 )
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
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 )
198    {
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
204        return -1;
205    }
206
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
211    // initialize the page descriptor
212    page_remote_init( page_xp );
213
214    // initialize specific page descriptor fields
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    {
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
232        return -1;
233    }
234
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
239    if( (inode == NULL) || (inode_type == FILE_TYPE_DIR) || (inode_size > (page_id << 10) ) )
240    {
241        error = vfs_fs_move_page( page_xp , IOC_SYNC_READ );
242
243        if( error )
244        {
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
251            return -1;
252         }
253    }
254
255    // return extended pointer on allocated page
256    *page_xp_ptr = page_xp;
257
258#if DEBUG_MAPPER_HANDLE_MISS
259ppn_t ppn = ppm_page2ppn( page_xp );
260if( (DEBUG_MAPPER_HANDLE_MISS < cycle) && (inode != NULL) )
261{
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 );
264}
265if( (DEBUG_MAPPER_HANDLE_MISS < cycle) && (inode == NULL) )
266{
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 );
269}
270#endif
271
272#if( DEBUG_MAPPER_HANDLE_MISS & 1 )
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
280    return 0;
281
282}  // end mapper_handle_miss()
283
284/////////////////////////////////////////////
285xptr_t  mapper_get_page( xptr_t    mapper_xp,
286                         uint32_t  page_id )
287{
288    error_t       error;
289
290    thread_t * this = CURRENT_THREAD;
291
292    // get mapper cluster and local pointer
293    mapper_t * mapper_ptr = GET_PTR( mapper_xp );
294    cxy_t      mapper_cxy = GET_CXY( mapper_xp );
295
296assert( __FUNCTION__, (hal_remote_lpt( XPTR( mapper_cxy , &mapper_ptr->inode ) ) != NULL ),
297"should not be used for the FAT mapper");
298
299#if DEBUG_MAPPER_GET_PAGE
300uint32_t      cycle = (uint32_t)hal_get_cycles();
301char          name[CONFIG_VFS_MAX_NAME_LENGTH];
302if( DEBUG_MAPPER_GET_PAGE < cycle ) 
303{
304    vfs_inode_t * inode = hal_remote_lpt( XPTR( mapper_cxy , &mapper_ptr->inode ) );
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}
309#endif
310
311#if( DEBUG_MAPPER_GET_PAGE & 1 )
312if( DEBUG_MAPPER_GET_PAGE < cycle ) 
313ppm_remote_display( local_cxy );
314#endif
315
316    // check thread can yield
317    thread_assert_can_yield( this , __FUNCTION__ );
318
319    // build extended pointer on mapper lock and mapper rt
320    xptr_t lock_xp  = XPTR( mapper_cxy , &mapper_ptr->lock );
321    xptr_t rt_xp    = XPTR( mapper_cxy , &mapper_ptr->rt );
322
323    // take mapper lock in READ_MODE
324    remote_rwlock_rd_acquire( lock_xp );
325
326    // search page in radix tree
327    xptr_t page_xp  = grdxt_remote_lookup( rt_xp , page_id );
328
329    // test mapper miss
330    if( page_xp == XPTR_NULL )                  // miss => handle it
331    {
332        // release the lock in READ_MODE and take it in WRITE_MODE
333        remote_rwlock_rd_release( lock_xp );
334        remote_rwlock_wr_acquire( lock_xp );
335
336        // second test on missing page because the page status can be modified
337        // by another thread, when passing from READ_MODE to WRITE_MODE.
338        // from this point there is no concurrent accesses to mapper.
339        page_xp = grdxt_remote_lookup( rt_xp , page_id );
340
341        if ( page_xp == XPTR_NULL )  // miss confirmed => handle it
342        {
343            error = mapper_handle_miss( mapper_xp,
344                                        page_id,
345                                        &page_xp );
346            if( error )
347            {
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
354                return XPTR_NULL;
355            }
356        }
357
358#if (DEBUG_MAPPER_GET_PAGE & 1)
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) );
362#endif
363       
364        // release mapper lock from WRITE_MODE
365        remote_rwlock_wr_release( lock_xp );
366    }
367    else                                              // hit
368    {
369        // release mapper lock from READ_MODE
370        remote_rwlock_rd_release( lock_xp );
371    }
372
373#if DEBUG_MAPPER_GET_PAGE
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) );
377#endif
378
379#if( DEBUG_MAPPER_GET_PAGE & 1)
380if( DEBUG_MAPPER_GET_PAGE < cycle ) 
381ppm_remote_display( local_cxy );
382#endif
383
384    return page_xp;
385
386}  // end mapper_get_page()
387
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
400assert( __FUNCTION__, (hal_remote_lpt( XPTR( mapper_cxy , &mapper_ptr->inode ) ) == NULL ),
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            {
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
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
478#if( DEBUG_MAPPER_GET_FAT_PAGE & 1)
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
487////////////////////////////////////////////////////
488void mapper_remote_release_page( xptr_t   mapper_xp,
489                                 page_t * page )
490{
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 );
494
495    // build extended pointer on mapper lock
496    xptr_t lock_xp = XPTR( mapper_cxy , &mapper_ptr->lock );
497
498    // take mapper lock in WRITE_MODE
499    remote_rwlock_wr_acquire( lock_xp );
500
501    // remove physical page from radix tree
502    grdxt_remote_remove( XPTR( mapper_cxy , &mapper_ptr->rt ) , page->index );
503
504    // release mapper lock from WRITE_MODE
505    remote_rwlock_wr_release( lock_xp );
506
507    // release page to PPM
508    ppm_remote_free_pages( mapper_cxy , page );
509                           
510}  // end mapper_release_page()
511
512///////////////////////////////////////////////
513error_t mapper_move_user( xptr_t     mapper_xp,
514                          bool_t     to_buffer,
515                          uint32_t   file_offset,
516                          void     * buffer,
517                          uint32_t   size )
518{
519    uint32_t   page_offset;    // first byte to move to/from a mapper page
520    uint32_t   page_bytes;     // number of bytes to move to/from a mapper page
521    uint32_t   page_id;        // current mapper page index
522    uint32_t   done;           // number of moved bytes
523    xptr_t     page_xp;        // extended pointer on current mapper page descriptor
524
525#if DEBUG_MAPPER_MOVE_USER
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 );
534if( DEBUG_MAPPER_MOVE_USER < cycle )
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}
543#endif
544
545    // compute indexes of first and last bytes in file
546    uint32_t min_byte = file_offset;
547    uint32_t max_byte = file_offset + size - 1;
548
549    // compute indexes of pages for first and last byte in mapper
550    uint32_t first = min_byte >> CONFIG_PPM_PAGE_ORDER;
551    uint32_t last  = max_byte >> CONFIG_PPM_PAGE_ORDER;
552
553#if (DEBUG_MAPPER_MOVE_USER & 1)
554if( DEBUG_MAPPER_MOVE_USER < cycle )
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 );
557#endif
558
559    done = 0;
560
561    // loop on pages in mapper
562    for( page_id = first ; page_id <= last ; page_id++ )
563    {
564        // compute page_offset
565        if( page_id == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK;
566        else                   page_offset = 0;
567
568        // compute number of bytes in page
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;
573
574#if (DEBUG_MAPPER_MOVE_USER & 1)
575if( DEBUG_MAPPER_MOVE_USER < cycle )
576printk("\n[%s] thread[%x,%x] : page_id %d / page_offset %d / bytes %d\n",
577__FUNCTION__, this->process->pid, this->trdid, page_id , page_offset , page_bytes );
578#endif
579
580        // get extended pointer on page descriptor in mapper
581        page_xp = mapper_get_page( mapper_xp , page_id ); 
582
583        if ( page_xp == XPTR_NULL ) return -1;
584
585        // compute extended pointer on kernel mapper
586        xptr_t     map_xp  = ppm_page2base( page_xp ) + page_offset;
587
588#if (DEBUG_MAPPER_MOVE_USER & 1)
589if( DEBUG_MAPPER_MOVE_USER < cycle )
590printk("\n[%s] thread[%x,%x] : get buffer(%x,%x) in mapper\n",
591__FUNCTION__, this->process->pid, this->trdid, GET_CXY(map_xp), GET_PTR(map_xp) );
592#endif
593        // compute pointer in user buffer
594        uint8_t * buf_ptr = (uint8_t *)buffer + done;
595
596        // move fragment
597        if( to_buffer )
598        {
599            hal_copy_to_uspace( buf_ptr , map_xp , page_bytes ); 
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",
604__FUNCTION__, this->process->pid, this->trdid, page_bytes,
605name, GET_CXY(map_xp), GET_PTR(map_xp), local_cxy, buf_ptr );
606#endif
607
608        }
609        else
610        {
611            ppm_page_do_dirty( page_xp ); 
612            hal_copy_from_uspace( map_xp , buf_ptr , page_bytes ); 
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",
617__FUNCTION__, this->process->pid, this->trdid, page_bytes,
618local_cxy, buf_ptr, name, GET_CXY(map_xp), GET_PTR(map_xp) );
619mapper_display_page(  mapper_xp , page_id , 128 );
620#endif
621
622        }
623
624        done += page_bytes;
625    }
626
627#if DEBUG_MAPPER_MOVE_USER
628cycle      = (uint32_t)hal_get_cycles();
629if( DEBUG_MAPPER_MOVE_USER < cycle )
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}
638#endif
639
640    return 0;
641
642}  // end mapper_move_user()
643
644////////////////////////////////////////////////
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 )
650{
651    uint32_t   page_offset;    // first byte to move to/from a mapper page
652    uint32_t   page_bytes;     // number of bytes to move to/from a mapper page
653    uint32_t   page_id;        // current mapper page index
654    uint32_t   done;           // number of moved bytes
655    xptr_t     page_xp;        // extended pointer on current mapper page descriptor
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
661
662    // get buffer cluster and local pointer
663    cxy_t     buffer_cxy = GET_CXY( buffer_xp );
664    uint8_t * buffer_ptr = GET_PTR( buffer_xp );
665
666    // get mapper cluster
667    cxy_t     mapper_cxy = GET_CXY( mapper_xp );
668
669#if DEBUG_MAPPER_MOVE_KERNEL
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 );
676if( DEBUG_MAPPER_MOVE_KERNEL < cycle )
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 );
679#endif
680
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
686    uint32_t first = min_byte >> CONFIG_PPM_PAGE_ORDER;
687    uint32_t last  = max_byte >> CONFIG_PPM_PAGE_ORDER;
688
689    // compute source and destination clusters
690    if( to_buffer )
691    {
692        dst_cxy = buffer_cxy;
693        src_cxy = mapper_cxy;
694    }
695    else
696    {
697        src_cxy = buffer_cxy;
698        dst_cxy = mapper_cxy;
699    }
700
701    done = 0;
702
703    // loop on pages in mapper
704    for( page_id = first ; page_id <= last ; page_id++ )
705    {
706        // compute page_offset
707        if( page_id == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK;
708        else                   page_offset = 0;
709
710        // compute number of bytes to move in page
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;
715
716        // get extended pointer on page descriptor
717        page_xp = mapper_get_page( mapper_xp , page_id );
718
719        if ( page_xp == XPTR_NULL ) return -1;
720
721        // get page base address
722        xptr_t    base_xp  = ppm_page2base( page_xp );
723        uint8_t * base_ptr = (uint8_t *)GET_PTR( base_xp );
724
725        // compute source and destination pointers
726        if( to_buffer )
727        {
728            dst_ptr = buffer_ptr + done;
729            src_ptr = base_ptr + page_offset;
730        }
731        else
732        {
733            src_ptr = buffer_ptr + done;
734            dst_ptr = base_ptr + page_offset;
735
736            ppm_page_do_dirty( page_xp );
737        }
738
739#if (DEBUG_MAPPER_MOVE_KERNEL & 1)
740if( DEBUG_MAPPER_MOVE_KERNEL < cycle )
741{
742    if( to_buffer )
743    printk("\n[%s] mapper <%s> page %d => buffer (%x,%x) / %d bytes\n",
744    __FUNCTION__, name, page_id, dst_cxy, dst_ptr, page_bytes );
745    else
746    printk("\n[%s] buffer (%x,%x) => mapper <%s> page %d / %d bytes\n",
747    __FUNCTION__, src_cxy, src_ptr, name, page_id, page_bytes );
748}
749#endif
750
751        // move fragment
752        hal_remote_memcpy( XPTR( dst_cxy , dst_ptr ), XPTR( src_cxy , src_ptr ), page_bytes );
753
754        done += page_bytes;
755    }
756
757#if DEBUG_MAPPER_MOVE_KERNEL
758cycle  = (uint32_t)hal_get_cycles();
759if( DEBUG_MAPPER_MOVE_KERNEL < cycle )
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 );
762#endif
763
764    return 0;
765
766}  // end mapper_move_kernel()
767
768///////////////////////////////////////////////////
769error_t mapper_remote_get_32( xptr_t     mapper_xp,
770                              uint32_t   page_id,
771                              uint32_t   word_id,
772                              uint32_t * value )
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
778    page_xp  = mapper_get_page( mapper_xp , page_id );
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
786    *value = hal_remote_l32( base_xp + (word_id<<2) ); 
787
788    return 0;
789
790}  // end mapper_remote_get_32()
791
792///////////////////////////////////////////////////
793error_t mapper_remote_set_32( xptr_t     mapper_xp,
794                              uint32_t   page_id,
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
802    page_xp  = mapper_get_page( mapper_xp , page_id );
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
810    hal_remote_s32( (base_xp + (word_id << 2)) , value );
811
812    // set the dirty flag in page descriptor
813    ppm_page_do_dirty( page_xp );
814
815    return 0;
816
817}  // end mapper_remote_set_32()
818
819////////////////////////////////////////
820error_t mapper_sync( xptr_t  mapper_xp )
821{
822    uint32_t   found_key;           // unused, required by grdxt_remote_get_first()
823    error_t    error;
824
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
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];
833vfs_inode_get_name( XPTR( mapper_cxy , &mapper_ptr->inode ) , name );
834#endif
835
836    // build extended pointer on radix tree
837    xptr_t   rt_xp = XPTR( mapper_cxy , &mapper_ptr->rt );
838
839    // initialise loop variable
840    uint32_t start_key = 0;
841
842    // scan radix-tree until last page found
843    while( 1 )
844    {
845        // get page descriptor from radix tree
846        xptr_t page_xp = grdxt_remote_get_first( rt_xp , start_key , &found_key );
847         
848        page_t * page_ptr = GET_PTR( page_xp );
849
850        // exit loop when last page found
851        if( page_ptr == NULL ) break;
852
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 ) );
856
857        // synchronize page if dirty
858        if( flags & PG_DIRTY )
859        {
860
861#if DEBUG_MAPPER_SYNC
862if( cycle > DEBUG_MAPPER_SYNC )
863printk("\n[%s] thread[%x,%x] synchonise page %d of <%s> to IOC device\n",
864__FUNCTION__, this->process->pid, this->trdid, page_ptr->index, name );
865#endif
866            // copy page to file system
867            error = vfs_fs_move_page( page_xp , IOC_WRITE );
868
869            if( error )
870            {
871
872#if DEBUG_MAPPER_SYNC
873printk("\n[ERROR] in %s : cannot synchonize dirty page %d\n", 
874__FUNCTION__, page_ptr->index );
875#endif
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",
888__FUNCTION__, this->process->pid, this->trdid, page_ptr->index, name );
889#endif
890        }
891
892        // update loop variable
893        start_key = index + 1;
894    }  // end while
895
896    return 0;
897
898}  // end mapper_sync()
899
900///////////////////////////////////////////////
901void mapper_display_page( xptr_t     mapper_xp,
902                          uint32_t   page_id,
903                          uint32_t   nbytes )
904{
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];
910
911assert( __FUNCTION__, (nbytes <= 4096)         , "nbytes cannot be larger than 4096");
912assert( __FUNCTION__, (mapper_xp != XPTR_NULL) , "mapper_xp argument cannot be null");
913
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 );
917
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
922    cxy_t    page_cxy = GET_CXY( page_xp );
923    page_t * page_ptr = GET_PTR( page_xp );
924
925    // get page_id and mapper from page descriptor
926    uint32_t   index   = hal_remote_l32( XPTR( page_cxy , &page_ptr->index ) );
927    mapper_t * mapper  = hal_remote_lpt( XPTR( page_cxy , &page_ptr->mapper ) );
928
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");
932
933    // get inode
934    vfs_inode_t * inode_ptr = hal_remote_lpt( XPTR( mapper_cxy , &mapper_ptr->inode ) );
935
936    // get inode name
937    if( inode_ptr == NULL ) strcpy( name , "FAT" );
938    else  vfs_inode_get_name( XPTR( mapper_cxy , inode_ptr ) , name );
939   
940    // get extended pointer on page base
941    xptr_t base_xp = ppm_page2base( page_xp );
942   
943    // copy remote page to local buffer
944    hal_remote_memcpy( XPTR( local_cxy , buffer ) , base_xp , nbytes );
945
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
951    // display 8 words per line
952    for( line = 0 ; line < (nbytes >> 5) ; line++ )
953    {
954        printk("%X : ", line << 5 );
955        for( word = 0 ; word < 8 ; word++ ) printk("%X ", tabi[(line<<3) + word] );
956        printk("\n");
957    }
958
959}  // end mapper_display_page()
960
961
Note: See TracBrowser for help on using the repository browser.