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

Last change on this file since 604 was 602, checked in by alain, 5 years ago

Improve the FAT32 file system to support cat, rm, cp commands.

File size: 20.8 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   root_inode_xp,
89                        xptr_t * devfs_dev_inode_xp,
90                        xptr_t * devfs_external_inode_xp )
91{
92    error_t  error;
93    xptr_t   unused_xp;   // required by vfs_add_child_in_parent()
94
95    // creates DEVFS "dev" inode in cluster 0
96    error = vfs_add_child_in_parent( 0,                // cxy
97                                     INODE_TYPE_DIR,
98                                     FS_TYPE_DEVFS,
99                                     root_inode_xp,
100                                     "dev",
101                                     &unused_xp,
102                                     devfs_dev_inode_xp );
103
104    assert( (error == 0) , "cannot create <dev>\n" );
105
106#if DEBUG_DEVFS_INIT
107uint32_t   cycle = (uint32_t)hal_get_cycles();
108thread_t * this  = CURRENT_THREAD;
109if( DEBUG_DEVFS_INIT < cycle )
110printk("\n[%s] thread[%x,%x] created <dev> inode / cycle %d\n",
111__FUNCTION__, this->process->pid, this->trdid, cycle );
112#endif
113
114    // create DEVFS "external" inode in cluster 0
115    error = vfs_add_child_in_parent( 0,               // cxy
116                                     INODE_TYPE_DIR,
117                                     FS_TYPE_DEVFS,
118                                     *devfs_dev_inode_xp,
119                                     "external",
120                                     &unused_xp,
121                                     devfs_external_inode_xp );
122
123    assert( (error == 0) , "cannot create <external>\n" );
124
125#if DEBUG_DEVFS_INIT
126cycle = (uint32_t)hal_get_cycles();
127if( DEBUG_DEVFS_INIT < cycle )
128printk("\n[%s] thread[%x,%x] created <external> inode / cycle %d\n",
129__FUNCTION__, this->process->pid, this->trdid, cycle );
130#endif
131
132}  // end devfs_global_init()
133
134///////////////////////////////////////////////////
135void devfs_local_init( xptr_t   devfs_dev_inode_xp,
136                       xptr_t   devfs_external_inode_xp,
137                       xptr_t * devfs_internal_inode_xp )
138{
139    char          node_name[16];
140    xptr_t        chdev_xp;
141    cxy_t         chdev_cxy;
142    chdev_t     * chdev_ptr;
143    xptr_t        inode_xp;
144    cxy_t         inode_cxy;
145    vfs_inode_t * inode_ptr;
146    uint32_t      channel;
147    xptr_t        unused_xp;    // required by add_child_in_parent()
148
149    // create "internal" directory
150    snprintf( node_name , 16 , "internal_%x" , local_cxy );
151    vfs_add_child_in_parent( local_cxy,
152                             INODE_TYPE_DIR,
153                             FS_TYPE_DEVFS,
154                             devfs_dev_inode_xp,
155                             node_name,
156                             &unused_xp,
157                             devfs_internal_inode_xp );
158#if DEBUG_DEVFS_INIT
159uint32_t   cycle = (uint32_t)hal_get_cycles();
160thread_t * this  = CURRENT_THREAD;
161if( DEBUG_DEVFS_INIT < cycle )
162printk("\n[%s] thread[%x,%x] created <%s> inode in cluster %x / cycle %d\n",
163__FUNCTION__, this->process->pid, this->trdid, node_name, local_cxy, cycle );
164#endif
165
166    // create MMC chdev inode
167    chdev_xp  = chdev_dir.mmc[local_cxy];
168    if( chdev_xp != XPTR_NULL)
169    {
170        chdev_ptr = GET_PTR( chdev_xp );
171        vfs_add_child_in_parent( local_cxy,
172                                 INODE_TYPE_DEV,
173                                 FS_TYPE_DEVFS,
174                                 *devfs_internal_inode_xp,
175                                 chdev_ptr->name,
176                                 &unused_xp,
177                                 &inode_xp );
178
179        // update child inode "extend" field
180        inode_cxy = GET_CXY( inode_xp );
181        inode_ptr = GET_PTR( inode_xp );
182        hal_remote_spt( XPTR( inode_cxy , &inode_ptr->extend ) , chdev_ptr );
183       
184#if DEBUG_DEVFS_INIT
185cycle = (uint32_t)hal_get_cycles();
186if( DEBUG_DEVFS_INIT < cycle )
187printk("\n[%s] thread[%x,%x] created <mmc> inode in cluster %x\n",
188__FUNCTION__, this->process->pid, this->trdid, local_cxy, cycle );
189#endif
190
191    }
192
193    // create DMA chdev inodes (one DMA channel per core)
194    for( channel = 0 ; channel < LOCAL_CLUSTER->cores_nr ; channel++ )
195    {
196        chdev_xp  = chdev_dir.dma[channel];
197        if( chdev_xp != XPTR_NULL)
198        {
199            chdev_ptr = GET_PTR( chdev_xp );
200            vfs_add_child_in_parent( local_cxy,
201                                     INODE_TYPE_DEV,
202                                     FS_TYPE_DEVFS,
203                                     *devfs_internal_inode_xp,
204                                     chdev_ptr->name,
205                                     &unused_xp,
206                                     &inode_xp );
207
208            // update child inode "extend" field
209            inode_cxy = GET_CXY( inode_xp );
210            inode_ptr = GET_PTR( inode_xp );
211            hal_remote_spt( XPTR( inode_cxy , &inode_ptr->extend ) , chdev_ptr );
212       
213#if DEBUG_DEVFS_INIT
214cycle = (uint32_t)hal_get_cycles();
215if( DEBUG_DEVFS_INIT < cycle )
216printk("\n[%s] thread [%x,%x] created <dma[%d]> inode in cluster %x\n",
217__FUNCTION__, this->process->pid, this->trdid, channel, local_cxy, cycle );
218#endif
219        }
220    }
221
222    // create an IOB inode in cluster containing IOB chdev
223    chdev_xp = chdev_dir.iob;
224    if( chdev_xp != XPTR_NULL )
225    {
226        chdev_cxy = GET_CXY( chdev_xp );
227        chdev_ptr = GET_PTR( chdev_xp );
228        if( chdev_cxy == local_cxy )
229        {
230            vfs_add_child_in_parent( local_cxy,
231                                     INODE_TYPE_DEV,
232                                     FS_TYPE_DEVFS,
233                                     devfs_external_inode_xp,
234                                     chdev_ptr->name,
235                                     &unused_xp,
236                                     &inode_xp );
237
238            // update child inode "extend" field
239            inode_cxy = GET_CXY( inode_xp );
240            inode_ptr = GET_PTR( inode_xp );
241            hal_remote_spt( XPTR( inode_cxy , &inode_ptr->extend ) , chdev_ptr );
242       
243#if DEBUG_DEVFS_INIT
244cycle = (uint32_t)hal_get_cycles();
245if( DEBUG_DEVFS_INIT < cycle )
246printk("\n[%s] thread[%x,%x] created <iob> inode in cluster %x\n",
247__FUNCTION__, this->process->pid, this->trdid, local_cxy, cycle );
248#endif
249        }
250    }
251       
252    // create a PIC inode in cluster containing PIC chdev
253    chdev_xp = chdev_dir.pic;
254    if( chdev_xp != XPTR_NULL )
255    {
256        chdev_cxy = GET_CXY( chdev_xp );
257        chdev_ptr = GET_PTR( chdev_xp );
258        if( chdev_cxy == local_cxy )
259        {
260            vfs_add_child_in_parent( local_cxy,
261                                     INODE_TYPE_DEV,
262                                     FS_TYPE_DEVFS,
263                                     devfs_external_inode_xp,
264                                     chdev_ptr->name,
265                                     &unused_xp,
266                                     &inode_xp );
267
268            // update child inode "extend" field
269            inode_cxy = GET_CXY( inode_xp );
270            inode_ptr = GET_PTR( inode_xp );
271            hal_remote_spt( XPTR( inode_cxy , &inode_ptr->extend ) , chdev_ptr );
272       
273#if DEBUG_DEVFS_INIT
274cycle = (uint32_t)hal_get_cycles();
275if( DEBUG_DEVFS_INIT < cycle )
276printk("\n[%s] thread[%x,%x] created <pic> inode in cluster %x\n",
277__FUNCTION__, this->process->pid, this->trdid, local_cxy, cycle );
278#endif
279        }
280    }
281
282    // create a TXT_RX inode in each cluster containing a TXT_RX chdev
283    for( channel = 0 ; channel < CONFIG_MAX_TXT_CHANNELS ; channel++ )
284    {
285        chdev_xp = chdev_dir.txt_rx[channel];
286        if( chdev_xp != XPTR_NULL )
287        {
288            chdev_cxy = GET_CXY( chdev_xp );
289            chdev_ptr = GET_PTR( chdev_xp );
290            if( chdev_cxy == local_cxy )
291            {
292                vfs_add_child_in_parent( local_cxy,
293                                         INODE_TYPE_DEV,
294                                         FS_TYPE_DEVFS,
295                                         devfs_external_inode_xp,
296                                         chdev_ptr->name,
297                                         &unused_xp,
298                                         &inode_xp );
299
300                // update child inode "extend" field
301                inode_cxy = GET_CXY( inode_xp );
302                inode_ptr = GET_PTR( inode_xp );
303                hal_remote_spt( XPTR( inode_cxy , &inode_ptr->extend ) , chdev_ptr );
304       
305#if DEBUG_DEVFS_INIT
306cycle = (uint32_t)hal_get_cycles();
307if( DEBUG_DEVFS_INIT < cycle )
308printk("\n[%s] thread[%x,%x] created <txt_rx[%d]> inode in cluster %x\n",
309__FUNCTION__, this->process->pid, this->trdid, channel, local_cxy, cycle );
310#endif
311            }
312        }
313    }
314
315    // create a TXT_TX inode in each cluster containing a TXT_TX chdev
316    for( channel = 0 ; channel < CONFIG_MAX_TXT_CHANNELS ; channel++ )
317    {
318        chdev_xp = chdev_dir.txt_tx[channel];
319        if( chdev_xp != XPTR_NULL )
320        {
321            chdev_cxy = GET_CXY( chdev_xp );
322            chdev_ptr = GET_PTR( chdev_xp );
323            if( chdev_cxy == local_cxy )
324            {
325                vfs_add_child_in_parent( local_cxy,
326                                         INODE_TYPE_DEV,
327                                         FS_TYPE_DEVFS,
328                                         devfs_external_inode_xp,
329                                         chdev_ptr->name,
330                                         &unused_xp,
331                                         &inode_xp );
332
333                // update child inode "extend" field
334                inode_cxy = GET_CXY( inode_xp );
335                inode_ptr = GET_PTR( inode_xp );
336                hal_remote_spt( XPTR( inode_cxy , &inode_ptr->extend ) , chdev_ptr );
337       
338#if DEBUG_DEVFS_INIT
339cycle = (uint32_t)hal_get_cycles();
340if( DEBUG_DEVFS_INIT < cycle )
341printk("\n[%s] thread[%x,%x] created <txt_tx[%d]> inode in cluster %x\n",
342__FUNCTION__, this->process->pid, this->trdid, channel, local_cxy, cycle );
343#endif
344            }
345        }
346    }
347
348    // create an IOC inode in each cluster containing an IOC chdev
349    for( channel = 0 ; channel < CONFIG_MAX_IOC_CHANNELS ; channel++ )
350    {
351        chdev_xp = chdev_dir.ioc[channel];
352        if( chdev_xp != XPTR_NULL )
353        {
354            chdev_cxy = GET_CXY( chdev_xp );
355            chdev_ptr = GET_PTR( chdev_xp );
356            if( chdev_cxy == local_cxy )
357            {
358                vfs_add_child_in_parent( local_cxy,
359                                         INODE_TYPE_DEV,
360                                         FS_TYPE_DEVFS,
361                                         devfs_external_inode_xp,
362                                         chdev_ptr->name,
363                                         &unused_xp,
364                                         &inode_xp );
365
366                // update child inode "extend" field
367                inode_cxy = GET_CXY( inode_xp );
368                inode_ptr = GET_PTR( inode_xp );
369                hal_remote_spt( XPTR( inode_cxy , &inode_ptr->extend ) , chdev_ptr );
370       
371#if DEBUG_DEVFS_INIT
372cycle = (uint32_t)hal_get_cycles();
373if( DEBUG_DEVFS_INIT < cycle )
374printk("\n[%s] thread[%x,%x] created <ioc[%d]> inode in cluster %x\n",
375__FUNCTION__, this->process->pid, this->trdid, channel, local_cxy, cycle );
376#endif
377            }
378        }
379    }
380
381    // create a FBF inode in each cluster containing a FBF chdev
382    for( channel = 0 ; channel < CONFIG_MAX_FBF_CHANNELS ; channel++ )
383    {
384        chdev_xp = chdev_dir.fbf[channel];
385        if( chdev_xp != XPTR_NULL )
386        {
387            chdev_cxy = GET_CXY( chdev_xp );
388            chdev_ptr = GET_PTR( chdev_xp );
389            if( chdev_cxy == local_cxy )
390            {
391                vfs_add_child_in_parent( local_cxy,
392                                         INODE_TYPE_DEV,
393                                         FS_TYPE_DEVFS,
394                                         devfs_external_inode_xp,
395                                         chdev_ptr->name,
396                                         &unused_xp,
397                                         &inode_xp );
398
399                // update child inode "extend" field
400                inode_cxy = GET_CXY( inode_xp );
401                inode_ptr = GET_PTR( inode_xp );
402                hal_remote_spt( XPTR( inode_cxy , &inode_ptr->extend ) , chdev_ptr );
403       
404#if DEBUG_DEVFS_INIT
405cycle = (uint32_t)hal_get_cycles();
406if( DEBUG_DEVFS_INIT < cycle )
407printk("\n[%s] thread[%x,%x] created <fbf[%d]> inode in cluster %x\n",
408__FUNCTION__, this->process->pid, this->trdid, channel, local_cxy, cycle );
409#endif
410            }
411        }
412    }
413
414    // create a NIC_RX inode in each cluster containing a NIC_RX chdev
415    for( channel = 0 ; channel < CONFIG_MAX_NIC_CHANNELS ; channel++ )
416    {
417        chdev_xp = chdev_dir.nic_rx[channel];
418        if( chdev_xp != XPTR_NULL )
419        {
420            chdev_cxy = GET_CXY( chdev_xp );
421            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
422            if( chdev_cxy == local_cxy )
423            {
424                vfs_add_child_in_parent( local_cxy,
425                                         INODE_TYPE_DEV,
426                                         FS_TYPE_DEVFS,
427                                         devfs_external_inode_xp,
428                                         chdev_ptr->name,
429                                         &unused_xp,
430                                         &inode_xp );
431#if DEBUG_DEVFS_INIT
432cycle = (uint32_t)hal_get_cycles();
433if( DEBUG_DEVFS_INIT < cycle )
434printk("\n[%s] thread[%x,%x] created <nic_rx[%d]> inode in cluster %x\n",
435__FUNCTION__, this->process->pid, this->trdid, channel, local_cxy, cycle );
436#endif
437            }
438        }
439    }
440
441    // create a NIC_TX inode in each cluster containing a NIC_TX chdev
442    for( channel = 0 ; channel < CONFIG_MAX_NIC_CHANNELS ; channel++ )
443    {
444        chdev_xp = chdev_dir.nic_tx[channel];
445        if( chdev_xp != XPTR_NULL )
446        {
447            chdev_cxy = GET_CXY( chdev_xp );
448            chdev_ptr = GET_PTR( chdev_xp );
449            if( chdev_cxy == local_cxy )
450            {
451                vfs_add_child_in_parent( local_cxy,
452                                         INODE_TYPE_DEV,
453                                         FS_TYPE_DEVFS,
454                                         devfs_external_inode_xp,
455                                         chdev_ptr->name,
456                                         &unused_xp,
457                                         &inode_xp );
458
459                // update child inode "extend" field
460                inode_cxy = GET_CXY( inode_xp );
461                inode_ptr = GET_PTR( inode_xp );
462                hal_remote_spt( XPTR( inode_cxy , &inode_ptr->extend ) , chdev_ptr );
463       
464#if DEBUG_DEVFS_INIT
465cycle = (uint32_t)hal_get_cycles();
466if( DEBUG_DEVFS_INIT < cycle )
467printk("\n[%s] thread[%x,%x] created <nic_tx[%d]> inode in cluster %x\n",
468__FUNCTION__, this->process->pid, this->trdid, channel, local_cxy, cycle );
469#endif
470            }
471        }
472    }
473}  // end devfs_local_init()
474
475//////////////////////////////////////////
476int devfs_user_move( bool_t     to_buffer,
477                     xptr_t     file_xp,
478                     char     * u_buf,
479                     uint32_t   size )
480{
481    xptr_t           chdev_xp;
482    cxy_t            chdev_cxy;
483    chdev_t        * chdev_ptr;                    // associated chdev type
484    uint32_t         func;                         // chdev functionnal type
485    uint32_t         channel;                      // chdev channel index
486    uint32_t         burst;                        // number of bytes in a burst
487    uint32_t         todo;                         // number of bytes not yet moved
488    error_t          error;
489    uint32_t         i;
490
491    char             k_buf[CONFIG_TXT_KBUF_SIZE];  // local kernel buffer
492
493assert( ( file_xp != XPTR_NULL ) , "file_xp == XPTR_NULL" );
494
495#if (DEBUG_SYS_READ & 1)
496enter_devfs_read = hal_time_stamp();
497#endif
498
499#if (DEBUG_SYS_WRITE & 1)
500enter_devfs_write = hal_time_stamp();
501#endif
502
503#if DEBUG_DEVFS_MOVE
504uint32_t   cycle = (uint32_t)hal_get_cycles();
505thread_t * this  = CURRENT_THREAD;
506if( DEBUG_DEVFS_MOVE < cycle )
507printk("\n[%s] thread[%x,%x] enter / to_mem %d / cycle %d\n",
508__FUNCTION__, this->process->pid, this->trdid, to_buffer, cycle );
509#endif
510
511    // get extended pointer on chdev_xp
512    chdev_xp = chdev_from_file( file_xp );
513
514    // get cluster and local pointer on chdev
515    chdev_cxy  = GET_CXY( chdev_xp );
516    chdev_ptr  = (chdev_t *)GET_PTR( chdev_xp );
517
518    // get chdev functionnal type and channel
519    func    = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->func ) );
520    channel = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->channel ) );
521
522// Only TXT devices are associated to a pseudo-file
523assert( ( func == DEV_FUNC_TXT ) , __FUNCTION__, "illegal device func_type");
524
525    // initialise number of bytes to move
526    todo = size;
527
528    /////////////// TXT read
529    if( to_buffer )
530    { 
531        while( todo )
532        {
533            // set burst size
534            if( todo > CONFIG_TXT_KBUF_SIZE ) burst = CONFIG_TXT_KBUF_SIZE;
535            else                              burst = todo;
536
537            // read burst bytes from TXT device to kernel buffer
538            for( i = 0 ; i < burst ; i++ )
539            {
540                error = dev_txt_read( channel , &k_buf[i] );
541
542                if( error )  return -1;
543            }
544
545            // move burst bytes from k_buf to u_buf                   
546            hal_strcpy_to_uspace( u_buf , k_buf , burst );
547
548            // update loop variables
549            todo  -= burst;
550            u_buf += burst;
551        }
552
553#if DEBUG_DEVFS_MOVE
554cycle = (uint32_t)hal_get_cycles();
555if( DEBUG_DEVFS_MOVE < cycle )
556printk("\n[%s] thread[%x,%x] exit / to_mem %d / cycle %d\n",
557__FUNCTION__, this->process->pid, this->trdid, to_buffer, cycle );
558#endif
559
560#if (DEBUG_SYS_READ & 1)
561exit_devfs_read = hal_time_stamp();
562#endif
563            return size;
564    } 
565    ///////////// TXT write 
566    else               
567    {
568        while( todo )
569        {
570            // set burst size
571            if( todo > CONFIG_TXT_KBUF_SIZE ) burst = CONFIG_TXT_KBUF_SIZE;
572            else                              burst = todo;
573
574            // move burst bytes from u_buf to k_buf
575            hal_strcpy_from_uspace( k_buf , u_buf , burst );
576
577            // write burst bytes from kernel buffer to TXT device
578            error = dev_txt_write( channel , k_buf , burst );
579
580            if( error ) return -1;
581
582            // update loop variables
583            todo  -= burst;
584            u_buf += burst;
585        } 
586
587#if DEBUG_DEVFS_MOVE
588cycle = (uint32_t)hal_get_cycles();
589if( DEBUG_DEVFS_MOVE < cycle )
590printk("\n[%s] thread[%x,%x] exit / to_mem %d / cycle %d\n",
591__FUNCTION__, this->process->pid, this->trdid, to_buffer, cycle );
592#endif
593
594#if (DEBUG_SYS_WRITE & 1)
595exit_devfs_write = hal_time_stamp();
596#endif
597            return size;
598    }
599
600}  // end devfs_user_move()
601
602
Note: See TracBrowser for help on using the repository browser.