Ignore:
Timestamp:
Nov 10, 2018, 2:33:26 PM (5 years ago)
Author:
alain
Message:

Fix various bugs in sys_stat() and sys_mmap() functions.
Improve debug in other functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/syscalls/sys_mmap.c

    r566 r594  
    5151        process_t   * process = this->process;
    5252
     53#if (DEBUG_SYS_MMAP || CONFIG_INSTRUMENTATION_SYSCALLS)
     54uint64_t     tm_start = hal_get_cycles();
     55#endif
     56
    5357#if DEBUG_SYS_MMAP
    54 uint64_t      tm_start;
    55 uint64_t      tm_end;
    5658tm_start = hal_get_cycles();
    5759if ( DEBUG_SYS_MMAP < tm_start )
    58 printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n",
    59 __FUNCTION__, this, process->pid, (uint32_t)tm_start );
    60 #endif
    61 
    62     // check arguments in user space
     60printk("\n[%s] thread[%x,%x] enter / cycle %d\n",
     61__FUNCTION__, process->pid, this->trdid, (uint32_t)tm_start );
     62#endif
     63
     64    // check user buffer (containing attributes) is mapped
    6365    error = vmm_get_vseg( process , (intptr_t)attr , &vseg );
    6466
    65     if ( error )
    66     {
    67 
    68 #if DEBUG_SYSCALLS_ERROR
    69 printk("\n[ERROR] in %s : user buffer unmapped %x / thread %x / process %x\n",
    70 __FUNCTION__ , (intptr_t)attr , this->trdid , process->pid );
     67    if( error )
     68    {
     69
     70#if DEBUG_SYSCALLS_ERROR
     71printk("\n[ERROR] in %s : thread[%x,%x] / mmap attributes unmapped %x\n",
     72__FUNCTION__ , process->pid, this->trdid, (intptr_t)attr );
    7173vmm_display( process , false );
    7274#endif
     
    7577    }
    7678
    77     // copy arguments from uspace
     79    // copy attributes from user space to kernel space
    7880    hal_copy_from_uspace( &k_attr , attr , sizeof(mmap_attr_t) );
    7981
    80     // get fdid, offset, and length arguments
    81     uint32_t fdid   = k_attr.fdid;
    82     uint32_t offset = k_attr.offset;
    83     uint32_t length = k_attr.length;
     82    // get addr, fdid, offset, and length attributes
     83    uint32_t  fdid   = k_attr.fdid;
     84    uint32_t  offset = k_attr.offset;
     85    uint32_t  length = k_attr.length;
    8486
    8587    // get flags
     
    9597
    9698#if DEBUG_SYSCALLS_ERROR
    97 printk("\n[ERROR] in %s : MAP_FIXED not supported / thread %x / process %x\n",
    98 __FUNCTION__ , this->trdid , process->pid );
     99printk("\n[ERROR] in %s : thread[%x,%x] / MAP_FIXED not supported\n",
     100__FUNCTION__ , process->pid, this->trdid );
    99101#endif
    100102        this->errno = EINVAL;
     
    106108
    107109#if DEBUG_SYSCALLS_ERROR
    108 printk("\n[ERROR] in %s : MAP_SHARED == MAP_PRIVATE / thread %x / process %x\n",
    109 __FUNCTION__ , this->trdid , process->pid );
     110printk("\n[ERROR] in %s : thread[%x,%x] / MAP_SHARED == MAP_PRIVATE\n",
     111__FUNCTION__ , process->pid, this->trdid );
    110112#endif
    111113        this->errno = EINVAL;
     
    115117    // FIXME handle Copy_On_Write for MAP_PRIVATE...
    116118
    117     // get access rigths
    118     bool_t     prot_read   = ( (k_attr.prot & PROT_READ )   != 0 );
    119     bool_t     prot_write  = ( (k_attr.prot & PROT_WRITE)   != 0 );
    120 
    121119    // test mmap type : can be FILE / ANON / REMOTE
    122120
    123121    if( (map_anon == false) && (map_remote == false) )   // FILE
    124122    {
     123
     124#if (DEBUG_SYS_MMAP & 1)
     125if ( DEBUG_SYS_MMAP < tm_start )
     126printk("\n[%s] thread[%x,%x] map file : fdid %d / offset %d / %d bytes\n",
     127__FUNCTION__, process->pid, this->trdid, fdid, offset, length );
     128#endif
     129
    125130            // FIXME: handle concurent delete of file by another thread closing it
    126131
     
    129134
    130135#if DEBUG_SYSCALLS_ERROR
    131 printk("\n[ERROR] in %s: bad file descriptor %d / thread %x / process %x\n",
    132 __FUNCTION__ , fdid , this->trdid , process->pid );
     136printk("\n[ERROR] in %s : thread[%x,%x] / bad file descriptor %d\n",
     137__FUNCTION__ , process->pid , this->trdid , fdid );
    133138#endif
    134139            this->errno = EBADFD;
     
    143148
    144149#if DEBUG_SYSCALLS_ERROR
    145 printk("\n[ERROR] in %s: file %d not found / thread %x / process %x\n",
    146 __FUNCTION__ , fdid , this->trdid , process->pid );
     150printk("\n[ERROR] in %s : thread[%x,%x] / file descriptor %d not found\n",
     151__FUNCTION__  , this->trdid , process->pid , fdid );
    147152#endif
    148153            this->errno = EBADFD;
     
    154159        vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
    155160
    156         // get inode pointer, mapper pointer and file attributes
     161#if (DEBUG_SYS_MMAP & 1)
     162if ( DEBUG_SYS_MMAP < tm_start )
     163printk("\n[%s] thread[%x,%x] get file pointer %x in cluster %x\n",
     164__FUNCTION__, process->pid, this->trdid, file_ptr, file_cxy );
     165#endif
     166
     167        // get inode pointer & mapper pointer
    157168        vfs_inode_t * inode_ptr  = hal_remote_lpt(XPTR(file_cxy , &file_ptr->inode ));
    158         uint32_t      file_attr  = hal_remote_l32 (XPTR(file_cxy , &file_ptr->attr  ));
    159169        mapper_t    * mapper_ptr = hal_remote_lpt(XPTR(file_cxy , &file_ptr->mapper));
    160170
    161171        // get file size
    162172                uint32_t size = hal_remote_l32( XPTR( file_cxy , &inode_ptr->size ) );
     173
     174#if (DEBUG_SYS_MMAP & 1)
     175if ( DEBUG_SYS_MMAP < tm_start )
     176printk("\n[%s] thread[%x,%x] get file size : %d bytes\n",
     177__FUNCTION__, process->pid, this->trdid, size );
     178#endif
    163179
    164180        // chek offset and length arguments
     
    167183
    168184#if DEBUG_SYSCALLS_ERROR
    169 printk("\n[ERROR] in %s: offset(%d) + len(%d) >= file's size(%d) / thread %x / process %x\n",
    170 __FUNCTION__, k_attr.offset, k_attr.length, size, this->trdid, process->pid );
     185printk("\n[ERROR] in %s: thread[%x,%x] / offset(%d) + len(%d) >= file's size(%d)\n",
     186__FUNCTION__, process->pid, this->trdid, k_attr.offset, k_attr.length, size );
    171187#endif
    172188            this->errno = ERANGE;
    173189            return -1;
    174190                }
     191
     192/* TODO
     193        // chek access rigths
     194        uint32_t   file_attr  = hal_remote_l32(XPTR(file_cxy , &file_ptr->attr  ));
     195        bool_t     prot_read  = ( (k_attr.prot & PROT_READ )   != 0 );
     196        bool_t     prot_write = ( (k_attr.prot & PROT_WRITE)   != 0 );
    175197
    176198        // check access rights
     
    186208                        return -1;
    187209                }
     210*/
    188211
    189212                // increment file refcount
     
    194217        vseg_cxy  = file_cxy;
    195218    }
    196     else                                                // ANON or REMOTE
    197     {
    198         // no mapper for ANON or REMOTE
     219    else if ( map_anon )                                 // MAP_ANON
     220    {
    199221        mapper_xp = XPTR_NULL;
    200 
    201         if( map_anon )
     222        vseg_type = VSEG_TYPE_ANON;
     223        vseg_cxy  = local_cxy;
     224
     225#if (DEBUG_SYS_MMAP & 1)
     226if ( DEBUG_SYS_MMAP < tm_start )
     227printk("\n[%s] thread[%x,%x] map anon / %d bytes / cluster %x\n",
     228__FUNCTION__, process->pid, this->trdid, length, vseg_cxy );
     229#endif
     230
     231    }
     232    else                                                 // MAP_REMOTE
     233    {
     234        mapper_xp = XPTR_NULL;
     235        vseg_type = VSEG_TYPE_REMOTE;
     236        vseg_cxy  = k_attr.fdid;
     237
     238#if (DEBUG_SYS_MMAP & 1)
     239if ( DEBUG_SYS_MMAP < tm_start )
     240printk("\n[%s] thread[%x,%x] map remote / %d bytes / cluster %x\n",
     241__FUNCTION__, process->pid, this->trdid, length, vseg_cxy );
     242#endif
     243 
     244        if( cluster_is_undefined( vseg_cxy ) )
    202245        {
    203             vseg_type = VSEG_TYPE_ANON;
    204             vseg_cxy  = local_cxy;
    205         }
    206         else
    207         {
    208             vseg_type = VSEG_TYPE_REMOTE;
    209             vseg_cxy  = k_attr.fdid;
    210  
    211             if( cluster_is_undefined( vseg_cxy ) )
    212             {
    213 
    214 #if DEBUG_SYSCALLS_ERROR
    215 printk("\n[ERROR] in %s : illegal cxy for MAP_REMOTE / thread %x / process %x\n",
    216 __FUNCTION__, this->trdid , process->pid );
    217 #endif
    218                 this->errno = EINVAL;
    219                 return -1;
    220             }
     246
     247#if DEBUG_SYSCALLS_ERROR
     248printk("\n[ERROR] in %s : thread[%x,%x] / illegal cxy %x for REMOTE\n",
     249__FUNCTION__, this->trdid , process->pid, vseg_cxy );
     250#endif
     251            this->errno = EINVAL;
     252            return -1;
    221253        }
    222254    }
     
    228260    xptr_t      ref_xp  = process->ref_xp;
    229261    cxy_t       ref_cxy = GET_CXY( ref_xp );
    230     process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
     262    process_t * ref_ptr = GET_PTR( ref_xp );
    231263
    232264    // create the vseg in reference cluster
     
    235267        vseg = vmm_create_vseg( process,
    236268                                vseg_type,
    237                                 0,               // base address unused for mmap()
    238                                 length,
    239                                 offset,
    240                                 0,               // file_size unused for mmap()
     269                                0,               // vseg base (unused for mmap)
     270                                length,          // vseg size
     271                                offset,          // file offset
     272                                0,               // file_size (unused for mmap)
    241273                                mapper_xp,
    242274                                vseg_cxy );
     
    247279                                    ref_ptr,
    248280                                    vseg_type,
    249                                     0,            // base address unused for mmap()
    250                                     length,
    251                                     offset,
    252                                     0,            // file size unused for mmap()
     281                                    0,            // vseg base (unused for mmap)
     282                                    length,       // vseg size
     283                                    offset,       // file offset
     284                                    0,            // file size (unused for mmap)
    253285                                    mapper_xp,
    254286                                    vseg_cxy,
     
    263295
    264296#if DEBUG_SYSCALLS_ERROR
    265 printk("\n[ERROR] in %s : cannot create vseg / thread %x / process %x\n",
    266 __FUNCTION__, this->trdid , process->pid );
     297printk("\n[ERROR] in %s : thread[%x,%x] / cannot create vseg\n",
     298__FUNCTION__, process->pid, this->trdid );
    267299#endif
    268300        this->errno = ENOMEM;
     
    275307    hal_fence();
    276308
     309#if (DEBUG_SYS_MMAP || CONFIG_INSTRUMENTATION_SYSCALLS)
     310uint64_t     tm_end = hal_get_cycles();
     311#endif
     312
    277313#if DEBUG_SYS_MMAP
    278 tm_end = hal_get_cycles();
    279 if ( DEBUG_SYS_MMAP < tm_start )
    280 printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n"
    281 "vseg %s / cluster %x / base %x / size %x / cost %d\n",
    282 __FUNCTION__, this, process->pid, (uint32_t)tm_end,
    283 vseg_type_str(vseg->type), vseg->cxy, vseg->min, length, (uint32_t)(tm_end - tm_start) );
     314if ( DEBUG_SYS_MMAP < tm_start )
     315printk("\n[%s] thread[%x,%x] exit / %s / cxy %x / base %x / size %d / cycle %d\n",
     316__FUNCTION__, process->pid, this->trdid,
     317vseg_type_str(vseg->type), vseg->cxy, vseg->min, length, (uint32_t)tm_end );
    284318#endif
    285319
Note: See TracChangeset for help on using the changeset viewer.