source: trunk/kernel/kern/chdev.c @ 443

Last change on this file since 443 was 440, checked in by alain, 6 years ago

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

File size: 15.7 KB
Line 
1/*
2 * chdev.c - channel device descriptor operations implementation.
3 *
4 * Authors  Alain Greiner   (2016)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH.is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH.is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <kernel_config.h>
25#include <hal_types.h>
26#include <hal_special.h>
27#include <hal_irqmask.h>
28#include <printk.h>
29#include <boot_info.h>
30#include <xlist.h>
31#include <kmem.h>
32#include <scheduler.h>
33#include <thread.h>
34#include <rpc.h>
35#include <chdev.h>
36#include <devfs.h>
37
38
39extern chdev_directory_t    chdev_dir;   // allocated in kernel_init.c
40
41#if (DEBUG_SYS_READ & 1)
42extern uint32_t enter_chdev_cmd_read;
43extern uint32_t exit_chdev_cmd_read;
44extern uint32_t enter_chdev_server_read;
45extern uint32_t exit_chdev_server_read;
46#endif
47
48#if (DEBUG_SYS_WRITE & 1)
49extern uint32_t enter_chdev_cmd_write;
50extern uint32_t exit_chdev_cmd_write;
51extern uint32_t enter_chdev_server_write;
52extern uint32_t exit_chdev_server_write;
53#endif
54
55////////////////////////////////////////////
56char * chdev_func_str( uint32_t func_type ) 
57{
58        if     ( func_type == DEV_FUNC_RAM ) return "RAM";
59        else if( func_type == DEV_FUNC_ROM ) return "ROM";
60        else if( func_type == DEV_FUNC_FBF ) return "FBF";
61        else if( func_type == DEV_FUNC_IOB ) return "IOB";
62        else if( func_type == DEV_FUNC_IOC ) return "IOC";
63        else if( func_type == DEV_FUNC_MMC ) return "MMC";
64        else if( func_type == DEV_FUNC_DMA ) return "DMA";
65        else if( func_type == DEV_FUNC_NIC ) return "NIC";
66        else if( func_type == DEV_FUNC_TIM ) return "TIM";
67        else if( func_type == DEV_FUNC_TXT ) return "TXT";
68        else if( func_type == DEV_FUNC_ICU ) return "ICU";
69        else if( func_type == DEV_FUNC_PIC ) return "PIC";
70    else                                 return "undefined";
71}
72
73/////////////////////////////////////////
74chdev_t * chdev_create( uint32_t    func,
75                        uint32_t    impl,
76                        uint32_t    channel,
77                        uint32_t    is_rx,
78                        xptr_t      base )
79{
80    chdev_t    * chdev;
81    kmem_req_t   req;
82
83    // allocate memory for chdev
84    req.type   = KMEM_DEVICE;
85    req.flags  = AF_ZERO;
86    chdev      = (chdev_t *)kmem_alloc( &req );
87
88    if( chdev == NULL ) return NULL;
89
90    // initialize waiting threads queue and associated lock
91    remote_spinlock_init( XPTR( local_cxy , &chdev->wait_lock ) );
92    xlist_root_init( XPTR( local_cxy , &chdev->wait_root ) );
93
94    // initialize attributes
95    chdev->func    =  func;
96    chdev->impl    =  impl;
97    chdev->channel =  channel;
98    chdev->is_rx   =  is_rx;
99    chdev->base    =  base; 
100
101    return chdev;
102
103}  // end chdev_create()
104
105///////////////////////////////////
106void chdev_print( chdev_t * chdev )
107{
108    printk("\n - func      = %s"
109           "\n - channel   = %d"
110           "\n - base      = %l"
111           "\n - cmd       = %x"
112           "\n - isr       = %x"
113           "\n - chdev     = %x\n",
114           chdev_func_str(chdev->func),
115           chdev->channel,
116           chdev->base,
117           chdev->cmd,
118           chdev->isr,
119           chdev );
120}
121
122//////////////////////////////////////////////////
123void chdev_register_command( xptr_t     chdev_xp )
124{
125    thread_t * server_ptr;    // local pointer on server thread associated to chdev
126    xptr_t     server_xp;     // extended pointer on server thread
127    core_t   * core_ptr;      // local pointer on core running the server thread
128    uint32_t   lid;           // core running the server thread local index
129    xptr_t     lock_xp;       // extended pointer on lock protecting the chdev queue
130    uint32_t   different;     // non zero if server thread core != client thread core
131    uint32_t   save_sr;       // for critical section
132
133#if (DEBUG_SYS_READ & 1)
134enter_chdev_cmd_read = (uint32_t)hal_get_cycles();
135#endif
136
137#if (DEBUG_SYS_WRITE & 1)
138enter_chdev_cmd_write = (uint32_t)hal_get_cycles();
139#endif
140
141    thread_t * this = CURRENT_THREAD;
142
143    // get chdev cluster and local pointer
144    cxy_t     chdev_cxy = GET_CXY( chdev_xp );
145    chdev_t * chdev_ptr = GET_PTR( chdev_xp );
146
147    // get local and extended pointers on server thread
148    server_ptr = (thread_t *)hal_remote_lpt( XPTR( chdev_cxy , &chdev_ptr->server) );
149    server_xp  = XPTR( chdev_cxy , server_ptr );
150
151    // get local pointer on core running the server thread
152    core_ptr   = (core_t *)hal_remote_lpt( XPTR( chdev_cxy , &server_ptr->core ) );
153
154    // get server core local index
155    lid = hal_remote_lw( XPTR( chdev_cxy , &core_ptr->lid ) );
156
157#if (DEBUG_CHDEV_CMD_RX || DEBUG_CHDEV_CMD_TX)
158bool_t is_rx = hal_remote_lw( XPTR( chdev_cxy , &chdev_ptr->is_rx ) );
159#endif
160   
161#if DEBUG_CHDEV_CMD_RX
162uint32_t rx_cycle = (uint32_t)hal_get_cycles();
163if( (is_rx) && (DEBUG_CHDEV_CMD_RX < rx_cycle) )
164printk("\n[DBG] %s : client_thread %x (%s) enter for RX / cycle %d\n",
165__FUNCTION__, this, thread_type_str(this->type) , rx_cycle );
166#endif
167
168#if DEBUG_CHDEV_CMD_TX
169uint32_t tx_cycle = (uint32_t)hal_get_cycles();
170if( (is_rx == 0) && (DEBUG_CHDEV_CMD_TX < tx_cycle) )
171printk("\n[DBG] %s : client_thread %x (%s) enter for TX / cycle %d\n",
172__FUNCTION__, this, thread_type_str(this->type) , tx_cycle );
173#endif
174
175    // build extended pointer on client thread xlist
176    xptr_t  list_xp    = XPTR( local_cxy , &this->wait_list );
177
178    // build extended pointer on chdev waiting queue root
179    xptr_t  root_xp    = XPTR( chdev_cxy , &chdev_ptr->wait_root );
180
181    // build extended pointer on server thread blocked state
182    xptr_t  blocked_xp = XPTR( chdev_cxy , &server_ptr->blocked );
183
184    // build extended pointer on lock protecting chdev waiting queue
185    lock_xp = XPTR( chdev_cxy , &chdev_ptr->wait_lock );
186
187    // critical section for the following sequence:
188    // (1) take the lock protecting waiting queue
189    // (2) block the client thread
190    // (3) unblock the server thread if required
191    // (4) register client thread in server queue
192    // (5) send IPI to force server scheduling
193    // (6) release the lock protecting waiting queue
194    // (7) deschedule
195    // ... in this order
196
197    // enter critical section
198    hal_disable_irq( &save_sr );
199
200    // take the lock
201    remote_spinlock_lock( lock_xp );
202
203    // block current thread
204    thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_IO );
205
206    if( hal_remote_lw( blocked_xp ) & THREAD_BLOCKED_IDLE )
207    thread_unblock( server_xp , THREAD_BLOCKED_IDLE );
208
209    // register client thread in waiting queue
210    xlist_add_last( root_xp , list_xp );
211
212    // send IPI to core running the server thread when server != client
213    different = (lid != this->core->lid) || (local_cxy != chdev_cxy);
214    if( different ) dev_pic_send_ipi( chdev_cxy , lid ); 
215   
216    // release lock
217    remote_spinlock_unlock( lock_xp );
218
219    // deschedule
220    assert( thread_can_yield( this ) , __FUNCTION__ , "illegal sched_yield\n" );
221    sched_yield("blocked on I/O");
222
223    // exit critical section
224    hal_restore_irq( save_sr );
225
226#if DEBUG_CHDEV_CMD_RX
227rx_cycle = (uint32_t)hal_get_cycles();
228if( (is_rx) && (DEBUG_CHDEV_CMD_RX < rx_cycle) )
229printk("\n[DBG] %s : client_thread %x (%s) exit for RX / cycle %d\n",
230__FUNCTION__, this, thread_type_str(this->type) , rx_cycle );
231#endif
232
233#if DEBUG_CHDEV_CMD_TX
234tx_cycle = (uint32_t)hal_get_cycles();
235if( (is_rx == 0) && (DEBUG_CHDEV_CMD_TX < tx_cycle) )
236printk("\n[DBG] %s : client_thread %x (%s) exit for TX / cycle %d\n",
237__FUNCTION__, this, thread_type_str(this->type) , tx_cycle );
238#endif
239
240#if (DEBUG_SYS_READ & 1)
241exit_chdev_cmd_read = (uint32_t)hal_get_cycles();
242#endif
243
244#if (DEBUG_SYS_WRITE & 1)
245exit_chdev_cmd_write = (uint32_t)hal_get_cycles();
246#endif
247
248}  // end chdev_register_command()
249
250///////////////////////////////////////////////
251void chdev_sequencial_server( chdev_t * chdev )
252{
253    xptr_t          client_xp;    // extended pointer on waiting thread
254    cxy_t           client_cxy;   // cluster of client thread
255    thread_t      * client_ptr;   // local pointer on client thread
256    thread_t      * server;       // local pointer on server thread
257    xptr_t          root_xp;      // extended pointer on device waiting queue root
258    xptr_t          lock_xp;      // extended pointer on lock ptotecting chdev queue
259
260    server = CURRENT_THREAD;
261
262    // get root and lock on command queue
263    root_xp = XPTR( local_cxy , &chdev->wait_root );
264    lock_xp = XPTR( local_cxy , &chdev->wait_lock );
265
266        // This infinite loop is executed by the DEV thread
267    // to handle commands registered in the chdev queue.
268    while( 1 )
269    {
270        // get the lock protecting the waiting queue
271        remote_spinlock_lock( lock_xp );
272
273        // check waiting queue state
274        if( xlist_is_empty( root_xp ) ) // waiting queue empty
275        {
276            // release lock
277            remote_spinlock_unlock( lock_xp );
278
279            // block
280            thread_block( XPTR( local_cxy , server ) , THREAD_BLOCKED_IDLE ); 
281
282            // deschedule
283            assert( thread_can_yield( server ) , __FUNCTION__ , "illegal sched_yield\n" );
284            sched_yield("I/O queue empty");
285        } 
286        else                            // waiting queue not empty
287        {
288            // get extended pointer on first client thread
289            client_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list );
290
291            // get client thread cluster and local pointer
292            client_cxy = GET_CXY( client_xp );
293            client_ptr = GET_PTR( client_xp );
294
295            // remove this first client thread from waiting queue
296            xlist_unlink( XPTR( client_cxy , &client_ptr->wait_list ) );
297
298            // release lock
299            remote_spinlock_unlock( lock_xp );
300
301#if DEBUG_CHDEV_SERVER_RX
302uint32_t rx_cycle = (uint32_t)hal_get_cycles();
303if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
304printk("\n[DBG] %s : server_thread %x start RX / client %x / cycle %d\n",
305__FUNCTION__ , server , client_ptr , rx_cycle );
306#endif
307
308#if DEBUG_CHDEV_SERVER_TX
309uint32_t tx_cycle = (uint32_t)hal_get_cycles();
310if( (chdev->is_rx == 0) && (DEBUG_CHDEV_SERVER_TX < tx_cycle) )
311printk("\n[DBG] %s : server_thread %x start TX / client %x / cycle %d\n",
312__FUNCTION__ , server , client_ptr , tx_cycle );
313#endif
314
315#if (DEBUG_SYS_READ & 1)
316enter_chdev_server_read = (uint32_t)hal_get_cycles();
317#endif
318
319#if (DEBUG_SYS_WRITE & 1)
320enter_chdev_server_write = (uint32_t)hal_get_cycles();
321#endif
322
323            // call driver command function to execute I/O operation
324            chdev->cmd( client_xp );
325       
326            // unblock client thread
327            thread_unblock( client_xp , THREAD_BLOCKED_IO );
328
329#if DEBUG_CHDEV_SERVER_RX
330rx_cycle = (uint32_t)hal_get_cycles();
331if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
332printk("\n[DBG] %s : server_thread %x completes RX / client %x / cycle %d\n",
333__FUNCTION__ , server , client_ptr , rx_cycle );
334#endif
335
336#if DEBUG_CHDEV_SERVER_TX
337tx_cycle = (uint32_t)hal_get_cycles();
338if( (chdev->is_rx == 0) && (DEBUG_CHDEV_SERVER_TX < tx_cycle) )
339printk("\n[DBG] %s : server_thread %x completes TX / client %x / cycle %d\n",
340__FUNCTION__ , server , client_ptr , tx_cycle );
341#endif
342
343#if (DEBUG_SYS_READ & 1)
344exit_chdev_server_read = (uint32_t)hal_get_cycles();
345#endif
346
347#if (DEBUG_SYS_WRITE & 1)
348exit_chdev_server_write = (uint32_t)hal_get_cycles();
349#endif
350
351        }
352    }  // end while
353}  // end chdev_sequencial_server()
354
355////////////////////////////////////////
356xptr_t chdev_from_file( xptr_t file_xp )
357{
358    cxy_t         file_cxy;
359    vfs_file_t  * file_ptr;
360    uint32_t      inode_type;
361    vfs_inode_t * inode_ptr;
362    chdev_t     * chdev_ptr;
363
364    assert( (file_xp != XPTR_NULL) , __FUNCTION__,
365    "file_xp == XPTR_NULL\n" );
366
367    // get cluster and local pointer on remote file descriptor
368    // associated inode and chdev are stored in same cluster as the file desc.
369    file_cxy  = GET_CXY( file_xp );
370    file_ptr  = (vfs_file_t *)GET_PTR( file_xp );
371
372    // get inode type from file descriptor
373    inode_type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
374    inode_ptr  = (vfs_inode_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
375
376    assert( (inode_type == INODE_TYPE_DEV) , __FUNCTION__ ,
377    "inode type %d is not INODE_TYPE_DEV\n", inode_type );
378
379    // get chdev local pointer from inode extension
380    chdev_ptr = (chdev_t *)hal_remote_lpt( XPTR( file_cxy , &inode_ptr->extend ) );
381
382    return XPTR( file_cxy , chdev_ptr );
383
384}  // end chdev_from_file()
385
386////////////////////////
387void chdev_dir_display()
388{
389    uint32_t  i;
390    cxy_t     cxy;
391    chdev_t * ptr;
392    uint32_t  base;
393    reg_t     save_sr;
394
395    // get pointers on TXT0 chdev
396    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
397    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
398    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
399
400    // get extended pointer on remote TXT0 chdev lock
401    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
402
403    // get TXT0 lock in busy waiting mode
404    remote_spinlock_lock_busy( lock_xp , &save_sr );
405
406    // header
407    nolock_printk("\n***** external chdevs directory *****\n");
408
409    // IOB
410    cxy  = GET_CXY( chdev_dir.iob );
411    ptr  = GET_PTR( chdev_dir.iob );
412    base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
413    nolock_printk("  - iob       : cxy = %X / ptr = %X / base = %X\n", cxy, ptr, base);
414
415    // PIC
416    cxy  = GET_CXY( chdev_dir.pic );
417    ptr  = GET_PTR( chdev_dir.pic );
418    base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
419    nolock_printk("  - pic       : cxy = %X / ptr = %X / base = %X\n", cxy, ptr, base);
420
421    // TXT
422    for( i = 0 ; i < LOCAL_CLUSTER->nb_txt_channels ; i++ )
423    {
424        cxy = GET_CXY( chdev_dir.txt_rx[i] );
425        ptr = GET_PTR( chdev_dir.txt_rx[i] );
426        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
427        nolock_printk("  - txt_rx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
428
429        cxy = GET_CXY( chdev_dir.txt_tx[i] );
430        ptr = GET_PTR( chdev_dir.txt_tx[i] );
431        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
432        nolock_printk("  - txt_tx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
433    }
434
435    // IOC
436    for( i = 0 ; i < LOCAL_CLUSTER->nb_ioc_channels ; i++ )
437    {
438        cxy = GET_CXY( chdev_dir.ioc[i] );
439        ptr = GET_PTR( chdev_dir.ioc[i] );
440        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
441        nolock_printk("  - ioc[%d]    : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
442    }
443
444    // FBF
445    for( i = 0 ; i < LOCAL_CLUSTER->nb_fbf_channels ; i++ )
446    {
447        cxy  = GET_CXY( chdev_dir.fbf[i] );
448        ptr  = GET_PTR( chdev_dir.fbf[i] );
449        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
450        nolock_printk("  - fbf[%d]    : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
451    }
452
453    // NIC
454    for( i = 0 ; i < LOCAL_CLUSTER->nb_nic_channels ; i++ )
455    {
456        cxy = GET_CXY( chdev_dir.nic_rx[i] );
457        ptr = GET_PTR( chdev_dir.nic_rx[i] );
458        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
459        nolock_printk("  - nic_rx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
460
461        cxy = GET_CXY( chdev_dir.nic_tx[i] );
462        ptr = GET_PTR( chdev_dir.nic_tx[i] );
463        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
464        nolock_printk("  - nic_tx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
465    }
466
467    // release lock
468    remote_spinlock_unlock_busy( lock_xp , save_sr );
469
470}  // end chdev_dir_display()
471
Note: See TracBrowser for help on using the repository browser.