source: trunk/kernel/fs/devfs.c @ 590

Last change on this file since 590 was 568, checked in by alain, 6 years ago

omplete restructuration of kernel locks.

File size: 17.9 KB
RevLine 
[23]1/*
2 * devfs.c - DEVFS File system API implementation.
3 *
4 * Author   Mohamed Lamine Karaoui (2014,2015)
5 *          Alain Greiner (2016,2017)
6 *
7 * Copyright (c) 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
[457]25#include <hal_kernel_types.h>
[23]26#include <hal_special.h>
[407]27#include <hal_uspace.h>
[23]28#include <printk.h>
29#include <chdev.h>
[433]30#include <thread.h>
[407]31#include <dev_txt.h>
[188]32#include <cluster.h>
[23]33#include <vfs.h>
[188]34#include <kmem.h>
[23]35#include <devfs.h>
36
[188]37/////////////////////////////////////////////////////////////////////////////////////////
38//     Extern variables
39/////////////////////////////////////////////////////////////////////////////////////////
[23]40
[188]41extern vfs_ctx_t            fs_context[];   // allocated in kernel_init.c
42extern chdev_directory_t    chdev_dir;      // allocated in kernel_init.c
[23]43
[438]44#if (DEBUG_SYS_READ & 1)
[435]45extern uint32_t  enter_devfs_read;
46extern uint32_t  exit_devfs_read;
[407]47#endif
48
[438]49#if (DEBUG_SYS_WRITE & 1)
[435]50extern uint32_t  enter_devfs_write;
51extern uint32_t  exit_devfs_write;
52#endif
53
[568]54/////////////////////////////////////
[484]55devfs_ctx_t * devfs_ctx_alloc( void )
[188]56{
57    kmem_req_t    req;
[23]58
[188]59        req.type    = KMEM_DEVFS_CTX;
60        req.size    = sizeof(devfs_ctx_t);
61    req.flags   = AF_KERNEL | AF_ZERO;
[23]62
[188]63        return (devfs_ctx_t *)kmem_alloc( &req );
64}
[23]65
[188]66/////////////////////////////////////////////
67void devfs_ctx_init( devfs_ctx_t * devfs_ctx,
[204]68                     xptr_t        devfs_dev_inode_xp,
[188]69                     xptr_t        devfs_external_inode_xp )
[23]70{
[204]71    devfs_ctx->dev_inode_xp      = devfs_dev_inode_xp;
[188]72    devfs_ctx->external_inode_xp = devfs_external_inode_xp;
[23]73
[188]74    fs_context[FS_TYPE_DEVFS].extend = devfs_ctx;
75}
[23]76
[188]77/////////////////////////////////////////////////
78void devfs_ctx_destroy( devfs_ctx_t * devfs_ctx )
79{
80    kmem_req_t    req;
[23]81
[188]82    req.type = KMEM_DEVFS_CTX;
83    req.ptr  = devfs_ctx;
84    kmem_free( &req );
85}
[23]86
[437]87/////////////////////////////////////////////////
[188]88void devfs_global_init( xptr_t   parent_inode_xp,
[204]89                        xptr_t * devfs_dev_inode_xp,
[188]90                        xptr_t * devfs_external_inode_xp )
[23]91{
92    error_t  error;
93
[457]94    // creates DEVFS "dev" inode in cluster 0
95    error = vfs_add_child_in_parent( 0,                // cxy
[188]96                                     INODE_TYPE_DIR,
97                                     FS_TYPE_DEVFS,
98                                     parent_inode_xp,
99                                     "dev",
100                                     NULL,
[204]101                                     devfs_dev_inode_xp ); 
[23]102
[492]103    assert( (error == 0) , "cannot create <dev>\n" );
[23]104
[568]105#if DEBUG_DEVFS_INIT
106uint32_t cycle = (uint32_t)hal_get_cycles();
[438]107if( DEBUG_DEVFS_INIT < cycle )
[568]108printk("\n[DBG] %s : thread %x in process %x created <dev> inode / cycle %d\n",
109__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid ,cycle );
[437]110#endif
111
[457]112    // create DEVFS "external" inode in cluster 0
113    error = vfs_add_child_in_parent( 0,               // cxy
[188]114                                     INODE_TYPE_DIR,
115                                     FS_TYPE_DEVFS,
[204]116                                     *devfs_dev_inode_xp,
117                                     "external",
[188]118                                     NULL,
119                                     devfs_external_inode_xp ); 
[23]120
[492]121    assert( (error == 0) , "cannot create <external>\n" );
[279]122
[438]123#if DEBUG_DEVFS_INIT
[433]124cycle = (uint32_t)hal_get_cycles();
[438]125if( DEBUG_DEVFS_INIT < cycle )
[568]126printk("\n[DBG] %s : thread %x in process %x created <external> inode / cycle %d\n",
127__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid , cycle );
[433]128#endif
129
[188]130} 
[23]131
[204]132///////////////////////////////////////////////////
133void devfs_local_init( xptr_t   devfs_dev_inode_xp,
134                       xptr_t   devfs_external_inode_xp,
135                       xptr_t * devfs_internal_inode_xp )
[23]136{
137    char          node_name[16];
[188]138    xptr_t        chdev_xp;
139    cxy_t         chdev_cxy;
[407]140    chdev_t     * chdev_ptr;
[188]141    xptr_t        inode_xp;
[23]142    uint32_t      channel;
143
[568]144    // create "internal" directory
[188]145    snprintf( node_name , 16 , "internal_%x" , local_cxy );
146    vfs_add_child_in_parent( local_cxy,
147                             INODE_TYPE_DIR,
148                             FS_TYPE_DEVFS,
[204]149                             devfs_dev_inode_xp,
[188]150                             node_name,
151                             NULL,
[204]152                             devfs_internal_inode_xp );
[568]153#if DEBUG_DEVFS_INIT
154uint32_t cycle = (uint32_t)hal_get_cycles();
155trdid_t  trdid = CURRENT_THREAD->trdid;
156pid_t    pid   = CURRENT_THREAD->process->pid;
157if( DEBUG_DEVFS_INIT < cycle )
158printk("\n[DBG] %s : thread %x in process %x created <%s> in cluster %x / cycle %d\n",
159__FUNCTION__, trdid, pid, node_name, local_cxy, cycle );
160#endif
[23]161
162    // create MMC chdev inode
[407]163    chdev_xp  = chdev_dir.mmc[local_cxy];
[188]164    if( chdev_xp != XPTR_NULL)
165    {
[407]166        chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]167        vfs_add_child_in_parent( local_cxy,
168                                 INODE_TYPE_DEV,
169                                 FS_TYPE_DEVFS,
[204]170                                 *devfs_internal_inode_xp,
[407]171                                 chdev_ptr->name,
[188]172                                 GET_PTR( chdev_xp ),
173                                 &inode_xp );
[568]174#if DEBUG_DEVFS_INIT
175cycle = (uint32_t)hal_get_cycles();
176if( DEBUG_DEVFS_INIT < cycle )
177printk("\n[DBG] %s : thread %x in process %x created <mmc> inode in cluster %x\n",
178__FUNCTION__, trdid, pid, local_cxy, cycle );
179#endif
180
[188]181    }
[23]182
183    // create DMA chdev inodes (one DMA channel per core)
[188]184    for( channel = 0 ; channel < LOCAL_CLUSTER->cores_nr ; channel++ )
[23]185    {
[188]186        chdev_xp = chdev_dir.dma[channel];
187        if( chdev_xp != XPTR_NULL)
188        {
[407]189            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]190            vfs_add_child_in_parent( local_cxy,
191                                     INODE_TYPE_DEV,
192                                     FS_TYPE_DEVFS,
[204]193                                     *devfs_internal_inode_xp,
[407]194                                     chdev_ptr->name,
[188]195                                     GET_PTR( chdev_xp ),
196                                     &inode_xp );
[568]197#if DEBUG_DEVFS_INIT
198cycle = (uint32_t)hal_get_cycles();
199if( DEBUG_DEVFS_INIT < cycle )
200printk("\n[DBG] %s : thread %x in process %x created <dma[%d]> inode in cluster %x\n",
201__FUNCTION__, trdid, pid, channel, local_cxy, cycle );
202#endif
[188]203        }
[23]204    }
205
[188]206    // create an IOB inode in cluster containing IOB chdev
207    chdev_xp = chdev_dir.iob;
208    if( chdev_xp != XPTR_NULL )
[23]209    {
[188]210        chdev_cxy = GET_CXY( chdev_xp );
[407]211        chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]212        if( chdev_cxy == local_cxy )
213        {
214            vfs_add_child_in_parent( local_cxy,
215                                     INODE_TYPE_DEV,
216                                     FS_TYPE_DEVFS,
217                                     devfs_external_inode_xp,
[407]218                                     chdev_ptr->name,
[188]219                                     GET_PTR( chdev_xp ),
220                                     &inode_xp );
[568]221#if DEBUG_DEVFS_INIT
222cycle = (uint32_t)hal_get_cycles();
223if( DEBUG_DEVFS_INIT < cycle )
224printk("\n[DBG] %s : thread %x in process %x created <iob> inode in cluster %x\n",
225__FUNCTION__, trdid, pid, local_cxy, cycle );
226#endif
[188]227        }
228    }
[23]229       
[188]230    // create a PIC inode in cluster containing PIC chdev
231    chdev_xp = chdev_dir.pic;
232    if( chdev_xp != XPTR_NULL )
233    {
234        chdev_cxy = GET_CXY( chdev_xp );
[407]235        chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]236        if( chdev_cxy == local_cxy )
[23]237        {
[188]238            vfs_add_child_in_parent( local_cxy,
239                                     INODE_TYPE_DEV,
240                                     FS_TYPE_DEVFS,
241                                     devfs_external_inode_xp,
[407]242                                     chdev_ptr->name,
[188]243                                     GET_PTR( chdev_xp ),
244                                     &inode_xp );
[568]245#if DEBUG_DEVFS_INIT
246cycle = (uint32_t)hal_get_cycles();
247if( DEBUG_DEVFS_INIT < cycle )
248printk("\n[DBG] %s : thread %x in process %x created <pic> inode in cluster %x\n",
249__FUNCTION__, trdid, pid, local_cxy, cycle );
250#endif
[23]251        }
[188]252    }
[23]253
[407]254    // create a TXT_RX inode in each cluster containing a TXT_RX chdev
[188]255    for( channel = 0 ; channel < CONFIG_MAX_TXT_CHANNELS ; channel++ )
256    {
[407]257        chdev_xp = chdev_dir.txt_rx[channel];
[188]258        if( chdev_xp != XPTR_NULL )
[23]259        {
[188]260            chdev_cxy = GET_CXY( chdev_xp );
[407]261            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]262            if( chdev_cxy == local_cxy )
263            {
264                vfs_add_child_in_parent( local_cxy,
265                                         INODE_TYPE_DEV,
266                                         FS_TYPE_DEVFS,
267                                         devfs_external_inode_xp,
[407]268                                         chdev_ptr->name,
[188]269                                         GET_PTR( chdev_xp ),
270                                         &inode_xp );
[568]271#if DEBUG_DEVFS_INIT
272cycle = (uint32_t)hal_get_cycles();
273if( DEBUG_DEVFS_INIT < cycle )
274printk("\n[DBG] %s : thread %x in process %x created <txt_rx[%d]> inode in cluster %x\n",
275__FUNCTION__, trdid, pid, channel, local_cxy, cycle );
276#endif
[188]277            }
[23]278        }
[188]279    }
[23]280
[407]281    // create a TXT_TX inode in each cluster containing a TXT_TX chdev
282    for( channel = 0 ; channel < CONFIG_MAX_TXT_CHANNELS ; channel++ )
283    {
284        chdev_xp = chdev_dir.txt_tx[channel];
285        if( chdev_xp != XPTR_NULL )
286        {
287            chdev_cxy = GET_CXY( chdev_xp );
288            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
289            if( chdev_cxy == local_cxy )
290            {
291                vfs_add_child_in_parent( local_cxy,
292                                         INODE_TYPE_DEV,
293                                         FS_TYPE_DEVFS,
294                                         devfs_external_inode_xp,
295                                         chdev_ptr->name,
296                                         GET_PTR( chdev_xp ),
297                                         &inode_xp );
[568]298#if DEBUG_DEVFS_INIT
299cycle = (uint32_t)hal_get_cycles();
300if( DEBUG_DEVFS_INIT < cycle )
301printk("\n[DBG] %s : thread %x in process %x created <txt_tx[%d]> inode in cluster %x\n",
302__FUNCTION__, trdid, pid, channel, local_cxy, cycle );
303#endif
[407]304            }
305        }
306    }
307
[188]308    // create an IOC inode in each cluster containing an IOC chdev
309    for( channel = 0 ; channel < CONFIG_MAX_IOC_CHANNELS ; channel++ )
310    {
311        chdev_xp = chdev_dir.ioc[channel];
312        if( chdev_xp != XPTR_NULL )
[23]313        {
[188]314            chdev_cxy = GET_CXY( chdev_xp );
[407]315            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]316            if( chdev_cxy == local_cxy )
317            {
318                vfs_add_child_in_parent( local_cxy,
319                                         INODE_TYPE_DEV,
320                                         FS_TYPE_DEVFS,
321                                         devfs_external_inode_xp,
[407]322                                         chdev_ptr->name,
[188]323                                         GET_PTR( chdev_xp ),
324                                         &inode_xp );
[568]325#if DEBUG_DEVFS_INIT
326cycle = (uint32_t)hal_get_cycles();
327if( DEBUG_DEVFS_INIT < cycle )
328printk("\n[DBG] %s : thread %x in process %x created <ioc[%d]> inode in cluster %x\n",
329__FUNCTION__, trdid, pid, channel, local_cxy, cycle );
330#endif
[188]331            }
[23]332        }
[188]333    }
[23]334
[188]335    // create a FBF inode in each cluster containing a FBF chdev
[568]336    for( channel = 0 ; channel < CONFIG_MAX_FBF_CHANNELS ; channel++ )
[188]337    {
338        chdev_xp = chdev_dir.fbf[channel];
339        if( chdev_xp != XPTR_NULL )
[23]340        {
[188]341            chdev_cxy = GET_CXY( chdev_xp );
[407]342            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]343            if( chdev_cxy == local_cxy )
344            {
345                vfs_add_child_in_parent( local_cxy,
346                                         INODE_TYPE_DEV,
347                                         FS_TYPE_DEVFS,
348                                         devfs_external_inode_xp,
[407]349                                         chdev_ptr->name,
[188]350                                         GET_PTR( chdev_xp ),
351                                         &inode_xp );
[568]352#if DEBUG_DEVFS_INIT
353cycle = (uint32_t)hal_get_cycles();
354if( DEBUG_DEVFS_INIT < cycle )
355printk("\n[DBG] %s : thread %x in process %x created <fbf[%d]> inode in cluster %x\n",
356__FUNCTION__, trdid, pid, channel, local_cxy, cycle );
357#endif
[188]358            }
[23]359        }
[188]360    }
[23]361
[188]362    // create a NIC_RX inode in each cluster containing a NIC_RX chdev
363    for( channel = 0 ; channel < CONFIG_MAX_NIC_CHANNELS ; channel++ )
364    {
365        chdev_xp = chdev_dir.nic_rx[channel];
366        if( chdev_xp != XPTR_NULL )
[23]367        {
[188]368            chdev_cxy = GET_CXY( chdev_xp );
[407]369            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]370            if( chdev_cxy == local_cxy )
371            {
372                vfs_add_child_in_parent( local_cxy,
373                                         INODE_TYPE_DEV,
374                                         FS_TYPE_DEVFS,
375                                         devfs_external_inode_xp,
[407]376                                         chdev_ptr->name,
[188]377                                         GET_PTR( chdev_xp ),
378                                         &inode_xp );
[568]379#if DEBUG_DEVFS_INIT
380cycle = (uint32_t)hal_get_cycles();
381if( DEBUG_DEVFS_INIT < cycle )
382printk("\n[DBG] %s : thread %x in process %x created <nic_rx[%d]> inode in cluster %x\n",
383__FUNCTION__, trdid, pid, channel, local_cxy, cycle );
384#endif
[188]385            }
[23]386        }
387    }
388
[188]389    // create a NIC_TX inode in each cluster containing a NIC_TX chdev
390    for( channel = 0 ; channel < CONFIG_MAX_NIC_CHANNELS ; channel++ )
[23]391    {
[188]392        chdev_xp = chdev_dir.nic_tx[channel];
393        if( chdev_xp != XPTR_NULL )
394        {
395            chdev_cxy = GET_CXY( chdev_xp );
[407]396            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]397            if( chdev_cxy == local_cxy )
398            {
399                vfs_add_child_in_parent( local_cxy,
400                                         INODE_TYPE_DEV,
401                                         FS_TYPE_DEVFS,
402                                         devfs_external_inode_xp,
[407]403                                         chdev_ptr->name,
[188]404                                         GET_PTR( chdev_xp ),
405                                         &inode_xp );
[438]406#if DEBUG_DEVFS_INIT
[433]407cycle = (uint32_t)hal_get_cycles();
[438]408if( DEBUG_DEVFS_INIT < cycle )
[568]409printk("\n[DBG] %s : thread %x in process %x created <nic_tx[%d]> inode in cluster %x\n",
410__FUNCTION__, trdid, pid, channel, local_cxy, cycle );
[433]411#endif
[568]412            }
413        }
414    }
[188]415}  // end devfs_local_init()
[23]416
[407]417//////////////////////////////////////////
418int devfs_user_move( bool_t     to_buffer,
419                     xptr_t     file_xp,
420                     void     * u_buf,
421                     uint32_t   size )
422{
[492]423    assert( ( file_xp != XPTR_NULL ) , "file_xp == XPTR_NULL" );
[407]424
[492]425    assert( ( size < CONFIG_TXT_KBUF_SIZE ) , "string size too large" );
[407]426
[430]427    xptr_t             chdev_xp;
428    cxy_t              chdev_cxy;
[407]429    chdev_t          * chdev_ptr;    // associated chdev type
430    uint32_t           func;         // chdev functionnal type
431    uint32_t           channel;      // chdev channel index
432    error_t            error;
433
434    char               k_buf[CONFIG_TXT_KBUF_SIZE];  // local kernel buffer
435
[438]436#if (DEBUG_SYS_READ & 1)
[435]437enter_devfs_read = hal_time_stamp();
438#endif
439
[438]440#if (DEBUG_SYS_WRITE & 1)
[435]441enter_devfs_write = hal_time_stamp();
442#endif
443
[438]444#if DEBUG_DEVFS_MOVE
[433]445uint32_t cycle = (uint32_t)hal_get_cycles();
[438]446if( DEBUG_DEVFS_MOVE < cycle )
[433]447printk("\n[DBG] %s : thread %x enter / to_mem %d / cycle %d\n",
448__FUNCTION__ , CURRENT_THREAD , to_buffer , cycle );
449#endif
[407]450
[430]451    // get extended pointer on chdev_xp
452    chdev_xp = chdev_from_file( file_xp );
[407]453
[430]454    // get cluster and local pointer on chdev
455    chdev_cxy  = GET_CXY( chdev_xp );
456    chdev_ptr  = (chdev_t *)GET_PTR( chdev_xp );
[407]457
458    // get chdev functionnal type and channel
[568]459    func    = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->func ) );
460    channel = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->channel ) );
[407]461
462    // action depends on "func" and "to_buffer"
463    if( func == DEV_FUNC_TXT )
464    {
465        if( to_buffer )     // TXT read
466        { 
467            uint32_t i;
468            for( i = 0 ; i < size ; i++ )
469            {
470                error = dev_txt_read( channel , &k_buf[i] );
471
472                if( error ) 
473                {
474                    return -1;
475                }
476                else
477                {
478                    hal_strcpy_to_uspace( u_buf , k_buf , size );
479                }
480             }
481
[438]482#if DEBUG_DEVFS_MOVE
[433]483cycle = (uint32_t)hal_get_cycles();
[438]484if( DEBUG_DEVFS_MOVE < cycle )
[433]485printk("\n[DBG] %s : thread %x exit / to_mem %d / cycle %d\n",
486__FUNCTION__ , CURRENT_THREAD , to_buffer / cycle );
487#endif
488
[438]489#if (DEBUG_SYS_READ & 1)
[435]490exit_devfs_read = hal_time_stamp();
[407]491#endif
492            return size;
493        } 
494        else                // TXT write 
495        {
496            hal_strcpy_from_uspace( k_buf , u_buf , size );
497
498            error = dev_txt_write( channel , k_buf , size );
499            if( error ) 
500            {
501                return -1;
502            }
503            else
504            {
505
[438]506#if DEBUG_DEVFS_MOVE
[433]507cycle = (uint32_t)hal_get_cycles();
[438]508if( DEBUG_DEVFS_MOVE < cycle )
[433]509printk("\n[DBG] %s : thread %x exit / to_mem %d / cycle %d\n",
510__FUNCTION__ , CURRENT_THREAD , to_buffer / cycle );
511#endif
[407]512
[438]513#if (DEBUG_SYS_WRITE & 1)
[435]514exit_devfs_write = hal_time_stamp();
515#endif
[407]516                return size;
517            }
518        }
519    }
520    else
521    {
[494]522        assert( false , "%s does not support direct user access",
523          chdev_func_str(func) );
[407]524
525        return -1;
526    }
[433]527
[407]528}  // end devfs_user_move()
529
530
Note: See TracBrowser for help on using the repository browser.