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

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

omplete restructuration of kernel locks.

File size: 17.9 KB
Line 
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
25#include <hal_kernel_types.h>
26#include <hal_special.h>
27#include <hal_uspace.h>
28#include <printk.h>
29#include <chdev.h>
30#include <thread.h>
31#include <dev_txt.h>
32#include <cluster.h>
33#include <vfs.h>
34#include <kmem.h>
35#include <devfs.h>
36
37/////////////////////////////////////////////////////////////////////////////////////////
38//     Extern variables
39/////////////////////////////////////////////////////////////////////////////////////////
40
41extern vfs_ctx_t            fs_context[];   // allocated in kernel_init.c
42extern chdev_directory_t    chdev_dir;      // allocated in kernel_init.c
43
44#if (DEBUG_SYS_READ & 1)
45extern uint32_t  enter_devfs_read;
46extern uint32_t  exit_devfs_read;
47#endif
48
49#if (DEBUG_SYS_WRITE & 1)
50extern uint32_t  enter_devfs_write;
51extern uint32_t  exit_devfs_write;
52#endif
53
54/////////////////////////////////////
55devfs_ctx_t * devfs_ctx_alloc( void )
56{
57    kmem_req_t    req;
58
59        req.type    = KMEM_DEVFS_CTX;
60        req.size    = sizeof(devfs_ctx_t);
61    req.flags   = AF_KERNEL | AF_ZERO;
62
63        return (devfs_ctx_t *)kmem_alloc( &req );
64}
65
66/////////////////////////////////////////////
67void devfs_ctx_init( devfs_ctx_t * devfs_ctx,
68                     xptr_t        devfs_dev_inode_xp,
69                     xptr_t        devfs_external_inode_xp )
70{
71    devfs_ctx->dev_inode_xp      = devfs_dev_inode_xp;
72    devfs_ctx->external_inode_xp = devfs_external_inode_xp;
73
74    fs_context[FS_TYPE_DEVFS].extend = devfs_ctx;
75}
76
77/////////////////////////////////////////////////
78void devfs_ctx_destroy( devfs_ctx_t * devfs_ctx )
79{
80    kmem_req_t    req;
81
82    req.type = KMEM_DEVFS_CTX;
83    req.ptr  = devfs_ctx;
84    kmem_free( &req );
85}
86
87/////////////////////////////////////////////////
88void devfs_global_init( xptr_t   parent_inode_xp,
89                        xptr_t * devfs_dev_inode_xp,
90                        xptr_t * devfs_external_inode_xp )
91{
92    error_t  error;
93
94    // creates DEVFS "dev" inode in cluster 0
95    error = vfs_add_child_in_parent( 0,                // cxy
96                                     INODE_TYPE_DIR,
97                                     FS_TYPE_DEVFS,
98                                     parent_inode_xp,
99                                     "dev",
100                                     NULL,
101                                     devfs_dev_inode_xp ); 
102
103    assert( (error == 0) , "cannot create <dev>\n" );
104
105#if DEBUG_DEVFS_INIT
106uint32_t cycle = (uint32_t)hal_get_cycles();
107if( DEBUG_DEVFS_INIT < cycle )
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 );
110#endif
111
112    // create DEVFS "external" inode in cluster 0
113    error = vfs_add_child_in_parent( 0,               // cxy
114                                     INODE_TYPE_DIR,
115                                     FS_TYPE_DEVFS,
116                                     *devfs_dev_inode_xp,
117                                     "external",
118                                     NULL,
119                                     devfs_external_inode_xp ); 
120
121    assert( (error == 0) , "cannot create <external>\n" );
122
123#if DEBUG_DEVFS_INIT
124cycle = (uint32_t)hal_get_cycles();
125if( DEBUG_DEVFS_INIT < cycle )
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 );
128#endif
129
130} 
131
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 )
136{
137    char          node_name[16];
138    xptr_t        chdev_xp;
139    cxy_t         chdev_cxy;
140    chdev_t     * chdev_ptr;
141    xptr_t        inode_xp;
142    uint32_t      channel;
143
144    // create "internal" directory
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,
149                             devfs_dev_inode_xp,
150                             node_name,
151                             NULL,
152                             devfs_internal_inode_xp );
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
161
162    // create MMC chdev inode
163    chdev_xp  = chdev_dir.mmc[local_cxy];
164    if( chdev_xp != XPTR_NULL)
165    {
166        chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
167        vfs_add_child_in_parent( local_cxy,
168                                 INODE_TYPE_DEV,
169                                 FS_TYPE_DEVFS,
170                                 *devfs_internal_inode_xp,
171                                 chdev_ptr->name,
172                                 GET_PTR( chdev_xp ),
173                                 &inode_xp );
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
181    }
182
183    // create DMA chdev inodes (one DMA channel per core)
184    for( channel = 0 ; channel < LOCAL_CLUSTER->cores_nr ; channel++ )
185    {
186        chdev_xp = chdev_dir.dma[channel];
187        if( chdev_xp != XPTR_NULL)
188        {
189            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
190            vfs_add_child_in_parent( local_cxy,
191                                     INODE_TYPE_DEV,
192                                     FS_TYPE_DEVFS,
193                                     *devfs_internal_inode_xp,
194                                     chdev_ptr->name,
195                                     GET_PTR( chdev_xp ),
196                                     &inode_xp );
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
203        }
204    }
205
206    // create an IOB inode in cluster containing IOB chdev
207    chdev_xp = chdev_dir.iob;
208    if( chdev_xp != XPTR_NULL )
209    {
210        chdev_cxy = GET_CXY( chdev_xp );
211        chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
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,
218                                     chdev_ptr->name,
219                                     GET_PTR( chdev_xp ),
220                                     &inode_xp );
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
227        }
228    }
229       
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 );
235        chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
236        if( chdev_cxy == local_cxy )
237        {
238            vfs_add_child_in_parent( local_cxy,
239                                     INODE_TYPE_DEV,
240                                     FS_TYPE_DEVFS,
241                                     devfs_external_inode_xp,
242                                     chdev_ptr->name,
243                                     GET_PTR( chdev_xp ),
244                                     &inode_xp );
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
251        }
252    }
253
254    // create a TXT_RX inode in each cluster containing a TXT_RX chdev
255    for( channel = 0 ; channel < CONFIG_MAX_TXT_CHANNELS ; channel++ )
256    {
257        chdev_xp = chdev_dir.txt_rx[channel];
258        if( chdev_xp != XPTR_NULL )
259        {
260            chdev_cxy = GET_CXY( chdev_xp );
261            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
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,
268                                         chdev_ptr->name,
269                                         GET_PTR( chdev_xp ),
270                                         &inode_xp );
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
277            }
278        }
279    }
280
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 );
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
304            }
305        }
306    }
307
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 )
313        {
314            chdev_cxy = GET_CXY( chdev_xp );
315            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
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,
322                                         chdev_ptr->name,
323                                         GET_PTR( chdev_xp ),
324                                         &inode_xp );
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
331            }
332        }
333    }
334
335    // create a FBF inode in each cluster containing a FBF chdev
336    for( channel = 0 ; channel < CONFIG_MAX_FBF_CHANNELS ; channel++ )
337    {
338        chdev_xp = chdev_dir.fbf[channel];
339        if( chdev_xp != XPTR_NULL )
340        {
341            chdev_cxy = GET_CXY( chdev_xp );
342            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
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,
349                                         chdev_ptr->name,
350                                         GET_PTR( chdev_xp ),
351                                         &inode_xp );
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
358            }
359        }
360    }
361
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 )
367        {
368            chdev_cxy = GET_CXY( chdev_xp );
369            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
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,
376                                         chdev_ptr->name,
377                                         GET_PTR( chdev_xp ),
378                                         &inode_xp );
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
385            }
386        }
387    }
388
389    // create a NIC_TX inode in each cluster containing a NIC_TX chdev
390    for( channel = 0 ; channel < CONFIG_MAX_NIC_CHANNELS ; channel++ )
391    {
392        chdev_xp = chdev_dir.nic_tx[channel];
393        if( chdev_xp != XPTR_NULL )
394        {
395            chdev_cxy = GET_CXY( chdev_xp );
396            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
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,
403                                         chdev_ptr->name,
404                                         GET_PTR( chdev_xp ),
405                                         &inode_xp );
406#if DEBUG_DEVFS_INIT
407cycle = (uint32_t)hal_get_cycles();
408if( DEBUG_DEVFS_INIT < cycle )
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 );
411#endif
412            }
413        }
414    }
415}  // end devfs_local_init()
416
417//////////////////////////////////////////
418int devfs_user_move( bool_t     to_buffer,
419                     xptr_t     file_xp,
420                     void     * u_buf,
421                     uint32_t   size )
422{
423    assert( ( file_xp != XPTR_NULL ) , "file_xp == XPTR_NULL" );
424
425    assert( ( size < CONFIG_TXT_KBUF_SIZE ) , "string size too large" );
426
427    xptr_t             chdev_xp;
428    cxy_t              chdev_cxy;
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
436#if (DEBUG_SYS_READ & 1)
437enter_devfs_read = hal_time_stamp();
438#endif
439
440#if (DEBUG_SYS_WRITE & 1)
441enter_devfs_write = hal_time_stamp();
442#endif
443
444#if DEBUG_DEVFS_MOVE
445uint32_t cycle = (uint32_t)hal_get_cycles();
446if( DEBUG_DEVFS_MOVE < cycle )
447printk("\n[DBG] %s : thread %x enter / to_mem %d / cycle %d\n",
448__FUNCTION__ , CURRENT_THREAD , to_buffer , cycle );
449#endif
450
451    // get extended pointer on chdev_xp
452    chdev_xp = chdev_from_file( file_xp );
453
454    // get cluster and local pointer on chdev
455    chdev_cxy  = GET_CXY( chdev_xp );
456    chdev_ptr  = (chdev_t *)GET_PTR( chdev_xp );
457
458    // get chdev functionnal type and channel
459    func    = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->func ) );
460    channel = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->channel ) );
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
482#if DEBUG_DEVFS_MOVE
483cycle = (uint32_t)hal_get_cycles();
484if( DEBUG_DEVFS_MOVE < cycle )
485printk("\n[DBG] %s : thread %x exit / to_mem %d / cycle %d\n",
486__FUNCTION__ , CURRENT_THREAD , to_buffer / cycle );
487#endif
488
489#if (DEBUG_SYS_READ & 1)
490exit_devfs_read = hal_time_stamp();
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
506#if DEBUG_DEVFS_MOVE
507cycle = (uint32_t)hal_get_cycles();
508if( DEBUG_DEVFS_MOVE < cycle )
509printk("\n[DBG] %s : thread %x exit / to_mem %d / cycle %d\n",
510__FUNCTION__ , CURRENT_THREAD , to_buffer / cycle );
511#endif
512
513#if (DEBUG_SYS_WRITE & 1)
514exit_devfs_write = hal_time_stamp();
515#endif
516                return size;
517            }
518        }
519    }
520    else
521    {
522        assert( false , "%s does not support direct user access",
523          chdev_func_str(func) );
524
525        return -1;
526    }
527
528}  // end devfs_user_move()
529
530
Note: See TracBrowser for help on using the repository browser.