Index: trunk/kernel/kern/chdev.c
===================================================================
--- trunk/kernel/kern/chdev.c	(revision 317)
+++ trunk/kernel/kern/chdev.c	(revision 407)
@@ -25,8 +25,10 @@
 #include <hal_types.h>
 #include <hal_special.h>
+#include <hal_irqmask.h>
 #include <printk.h>
 #include <boot_info.h>
 #include <xlist.h>
 #include <kmem.h>
+#include <scheduler.h>
 #include <thread.h>
 #include <rpc.h>
@@ -37,4 +39,10 @@
 extern chdev_directory_t    chdev_dir;   // allocated in kernel_init.c
 
+#if CONFIG_READ_DEBUG
+extern uint32_t enter_chdev_cmd;
+extern uint32_t exit_chdev_cmd;
+extern uint32_t enter_chdev_server;
+extern uint32_t exit_chdev_server;
+#endif
 
 ////////////////////////////////////////////
@@ -105,9 +113,18 @@
 }
 
-////////////////////////////////////////////////
-void chdev_register_command( xptr_t     chdev_xp,
-                             thread_t * thread )
-{
-    thread_t * thread_ptr = CURRENT_THREAD;
+//////////////////////////////////////////////////
+void chdev_register_command( xptr_t     chdev_xp )
+{
+    thread_t * server_ptr;    // local pointer on server thread associated to chdev
+    core_t   * core_ptr;      // local pointer on core running the server thread
+    uint32_t   lid;           // core running the server thread local index
+    xptr_t     lock_xp;       // extended pointer on lock protecting the chdev queue
+    uint32_t   modified;      // non zero if the server thread state was modified
+    uint32_t   save_sr;       // for critical section
+
+    thread_t * this = CURRENT_THREAD;
+
+chdev_dmsg("\n[DBG] %s : core[%x,%d] (thread %s) enter / cycle %d\n",
+__FUNCTION__, local_cxy, this->core->lid, thread_type_str(this->type) , hal_time_stamp() );
 
     // get device descriptor cluster and local pointer
@@ -116,22 +133,51 @@
 
     // build extended pointers on client thread xlist and device root
-    xptr_t  xp_list = XPTR( local_cxy , &thread_ptr->wait_list );
-    xptr_t  xp_root = XPTR( chdev_cxy , &chdev_ptr->wait_root );
-
-    // get lock protecting queue
-    remote_spinlock_lock( XPTR( chdev_cxy , &chdev_ptr->wait_lock ) );
+    xptr_t  list_xp = XPTR( local_cxy , &this->wait_list );
+    xptr_t  root_xp = XPTR( chdev_cxy , &chdev_ptr->wait_root );
+
+    // get local pointer on server thread
+    server_ptr = (thread_t *)hal_remote_lpt( XPTR( chdev_cxy , &chdev_ptr->server) );
+
+chdev_dmsg("\n[DBG] %s : core[%x,%d] (thread %s) / server_cxy %x / server_ptr %x / server_type %\n",
+__FUNCTION__, local_cxy, this->core->lid, server_cxy, server_ptr,
+thread_type_str( hal_remote_lw( XPTR( server_cxy , &server_ptr->type) ) ) );
+
+    // build extended pointer on chdev lock protecting queue
+    lock_xp = XPTR( chdev_cxy , &chdev_ptr->wait_lock );
+
+    // get local pointer on core running the server thread
+    core_ptr = (core_t *)hal_remote_lpt( XPTR( chdev_cxy , &server_ptr->core ) );
+
+    // get core local index
+    lid = hal_remote_lw( XPTR( chdev_cxy , &core_ptr->lid ) );
+
+    // enter critical section
+    hal_disable_irq( &save_sr );
 
     // register client thread in waiting queue 
-    xlist_add_last( xp_root , xp_list );
+    remote_spinlock_lock( lock_xp );
+    xlist_add_last( root_xp , list_xp );
+    remote_spinlock_unlock( lock_xp );
 
     // unblock server thread
-    thread_unblock( XPTR( chdev_cxy , &chdev_ptr->server ) , THREAD_BLOCKED_DEV_QUEUE );
-
-    // release lock
-    remote_spinlock_unlock( XPTR( chdev_cxy , &chdev_ptr->wait_lock ) );
-
-    // client thread goes to blocked state and deschedule
-    thread_block( thread_ptr , THREAD_BLOCKED_IO );
-    sched_yield( NULL );
+    modified = thread_unblock( XPTR( chdev_cxy , server_ptr ), THREAD_BLOCKED_DEV_QUEUE );
+
+    // send IPI to core running the server thread
+    if( modified ) dev_pic_send_ipi( chdev_cxy , lid ); 
+    
+    // block client thread 
+    assert( thread_can_yield( this ) , __FUNCTION__ , "illegal sched_yield\n" );
+
+chdev_dmsg("\n[DBG] %s : core[%x,%d] (thread %s) deschedules / cycle %d\n",
+__FUNCTION__, local_cxy, this->core->lid, thread_type_str(this->type) , hal_time_stamp() );
+
+    thread_block( CURRENT_THREAD , THREAD_BLOCKED_IO );
+    sched_yield();
+
+chdev_dmsg("\n[DBG] %s : core[%x,%d] (thread %s) resumes / cycle %d\n",
+__FUNCTION__, local_cxy, this->core->lid, thread_type_str(this->type) , hal_time_stamp() );
+
+    // exit critical section
+    hal_restore_irq( save_sr );
 
 }  // end chdev_register_command()
@@ -143,51 +189,73 @@
     cxy_t           client_cxy;   // cluster of client thread
     thread_t      * client_ptr;   // local pointer on client thread
-    thread_t      * server;       // local pointer on this thread
+    thread_t      * server;       // local pointer on server thread
     xptr_t          root_xp;      // extended pointer on device waiting queue root
+    xptr_t          lock_xp;      // extended pointer on lock ptotecting chdev queue
 
     server = CURRENT_THREAD;
 
+chdev_dmsg("\n[DBG] %s : enter / server = %x / chdev = %x / cycle %d\n",
+__FUNCTION__ , server , chdev , hal_time_stamp() );
+
     root_xp = XPTR( local_cxy , &chdev->wait_root );
-
-    // take the lock protecting the chdev waiting queue, before entering the
-	// infinite loop handling commands registered in this queue.
-    // In the loop, the lock is released during the handling of one command.
-
-    remote_spinlock_lock( XPTR( local_cxy , &chdev->wait_lock ) );
-
+    lock_xp = XPTR( local_cxy , &chdev->wait_lock );
+
+	// This infinite loop is executed by the DEV thread
+    // to handle commands registered in the chdev queue.
     while( 1 )
     {
+        // get the lock protecting the waiting queue
+        remote_spinlock_lock( lock_xp );
+
         // check waiting queue state
-        if( xlist_is_empty( root_xp ) ) // block and deschedule if waiting queue empty 
+        if( xlist_is_empty( root_xp ) ) // waiting queue empty 
         {
             // release lock 
-            remote_spinlock_unlock( XPTR( local_cxy , &chdev->wait_lock ) );
+            remote_spinlock_unlock( lock_xp );
+
+chdev_dmsg("\n[DBG] %s : thread %x deschedule /cycle %d\n",
+__FUNCTION__ , server , hal_time_stamp() );
 
             // block and deschedule
             thread_block( server , THREAD_BLOCKED_DEV_QUEUE );
-            sched_yield( NULL );
+            sched_yield();
+
+chdev_dmsg("\n[DBG] %s : thread %x resume /cycle %d\n",
+__FUNCTION__ , server , hal_time_stamp() );
+
         } 
-        else
+        else                            // waiting queue not empty
         {
+
+#if CONFIG_READ_DEBUG
+enter_chdev_server = hal_time_stamp();
+#endif
             // release lock 
-            remote_spinlock_unlock( XPTR( local_cxy , &chdev->wait_lock ) );
+            remote_spinlock_unlock( lock_xp );
+
+            // get extended pointer on first client thread
+            client_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list );
+
+            // get client thread cluster, local pointer, and identifier 
+            client_cxy = GET_CXY( client_xp );
+            client_ptr = (thread_t *)GET_PTR( client_xp );
+
+            // call driver command function to execute I/O operation
+            chdev->cmd( client_xp );
+        
+            // remove the client thread from waiting queue
+            remote_spinlock_lock( lock_xp );
+            xlist_unlink( XPTR( client_cxy , &client_ptr->wait_list ) );
+            remote_spinlock_unlock( lock_xp );
+
+chdev_dmsg("\n[DBG] %s : thread %x complete operation for client %x / cycle %d\n",
+__FUNCTION__ , server , client_ptr , hal_time_stamp() );
+
+#if CONFIG_READ_DEBUG
+exit_chdev_server = hal_time_stamp();
+#endif
+
         }
-
-        // get extended pointer on first client thread
-        client_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list );
-
-        // call driver command function to execute I/O operation
-        chdev->cmd( client_xp );
-        
-        // get client thread cluster and local pointer
-        client_cxy = GET_CXY( client_xp );
-        client_ptr = (thread_t *)GET_PTR( client_xp );
-
-        // take the lock, and remove the client thread from waiting queue
-        remote_spinlock_lock( XPTR( local_cxy , &chdev->wait_lock ) );
-        xlist_unlink( XPTR( client_cxy , &client_ptr->wait_list ) );
-
     }  // end while
-
 }  // end chdev_sequencial_server()
 
@@ -197,58 +265,76 @@
     cxy_t     iob_cxy  = GET_CXY( chdev_dir.iob );
     chdev_t * iob_ptr  = (chdev_t *)GET_PTR( chdev_dir.iob );
-    xptr_t    iob_base = hal_remote_lwd( XPTR( iob_cxy , &iob_ptr->base ) );
+    uint32_t  iob_base = (uint32_t)hal_remote_lwd( XPTR( iob_cxy , &iob_ptr->base ) );
 
     cxy_t     pic_cxy  = GET_CXY( chdev_dir.pic );
     chdev_t * pic_ptr  = (chdev_t *)GET_PTR( chdev_dir.pic );
-    xptr_t    pic_base = hal_remote_lwd( XPTR( pic_cxy , &pic_ptr->base ) );
-
-    cxy_t     txt0_cxy  = GET_CXY( chdev_dir.txt[0] );
-    chdev_t * txt0_ptr  = (chdev_t *)GET_PTR( chdev_dir.txt[0] );
-    xptr_t    txt0_base = hal_remote_lwd( XPTR( txt0_cxy , &txt0_ptr->base ) );
-
-    cxy_t     txt1_cxy  = GET_CXY( chdev_dir.txt[1] );
-    chdev_t * txt1_ptr  = (chdev_t *)GET_PTR( chdev_dir.txt[1] );
-    xptr_t    txt1_base = hal_remote_lwd( XPTR( txt1_cxy , &txt1_ptr->base ) );
-
-    cxy_t     txt2_cxy  = GET_CXY( chdev_dir.txt[2] );
-    chdev_t * txt2_ptr  = (chdev_t *)GET_PTR( chdev_dir.txt[2] );
-    xptr_t    txt2_base = hal_remote_lwd( XPTR( txt2_cxy , &txt2_ptr->base ) );
+    uint32_t  pic_base = (uint32_t)hal_remote_lwd( XPTR( pic_cxy , &pic_ptr->base ) );
+
+    cxy_t     txt0_tx_cxy  = GET_CXY( chdev_dir.txt_tx[0] );
+    chdev_t * txt0_tx_ptr  = (chdev_t *)GET_PTR( chdev_dir.txt_tx[0] );
+    uint32_t  txt0_tx_base = (uint32_t)hal_remote_lwd( XPTR( txt0_tx_cxy , &txt0_tx_ptr->base ) );
+
+    cxy_t     txt0_rx_cxy  = GET_CXY( chdev_dir.txt_rx[0] );
+    chdev_t * txt0_rx_ptr  = (chdev_t *)GET_PTR( chdev_dir.txt_rx[0] );
+    uint32_t  txt0_rx_base = (uint32_t)hal_remote_lwd( XPTR( txt0_rx_cxy , &txt0_rx_ptr->base ) );
+
+    cxy_t     txt1_tx_cxy  = GET_CXY( chdev_dir.txt_tx[1] );
+    chdev_t * txt1_tx_ptr  = (chdev_t *)GET_PTR( chdev_dir.txt_tx[1] );
+    uint32_t  txt1_tx_base = (uint32_t)hal_remote_lwd( XPTR( txt1_tx_cxy , &txt1_tx_ptr->base ) );
+
+    cxy_t     txt1_rx_cxy  = GET_CXY( chdev_dir.txt_rx[1] );
+    chdev_t * txt1_rx_ptr  = (chdev_t *)GET_PTR( chdev_dir.txt_rx[1] );
+    uint32_t  txt1_rx_base = (uint32_t)hal_remote_lwd( XPTR( txt1_rx_cxy , &txt1_rx_ptr->base ) );
+
+    cxy_t     txt2_tx_cxy  = GET_CXY( chdev_dir.txt_tx[2] );
+    chdev_t * txt2_tx_ptr  = (chdev_t *)GET_PTR( chdev_dir.txt_tx[2] );
+    uint32_t  txt2_tx_base = (uint32_t)hal_remote_lwd( XPTR( txt2_tx_cxy , &txt2_tx_ptr->base ) );
+
+    cxy_t     txt2_rx_cxy  = GET_CXY( chdev_dir.txt_rx[2] );
+    chdev_t * txt2_rx_ptr  = (chdev_t *)GET_PTR( chdev_dir.txt_rx[2] );
+    uint32_t  txt2_rx_base = (uint32_t)hal_remote_lwd( XPTR( txt2_rx_cxy , &txt2_rx_ptr->base ) );
 
     cxy_t     ioc_cxy  = GET_CXY( chdev_dir.ioc[0] );
     chdev_t * ioc_ptr  = (chdev_t *)GET_PTR( chdev_dir.ioc[0] );
-    xptr_t    ioc_base = hal_remote_lwd( XPTR( ioc_cxy , &ioc_ptr->base ) );
+    uint32_t  ioc_base = (uint32_t)hal_remote_lwd( XPTR( ioc_cxy , &ioc_ptr->base ) );
 
     cxy_t     fbf_cxy  = GET_CXY( chdev_dir.fbf[0] );
     chdev_t * fbf_ptr  = (chdev_t *)GET_PTR( chdev_dir.fbf[0] );
-    xptr_t    fbf_base = hal_remote_lwd( XPTR( fbf_cxy , &fbf_ptr->base ) );
-
-    cxy_t     nic_rx_cxy  = GET_CXY( chdev_dir.nic_rx[0] );
-    chdev_t * nic_rx_ptr  = (chdev_t *)GET_PTR( chdev_dir.nic_rx[0] );
-    xptr_t    nic_rx_base = hal_remote_lwd( XPTR( nic_rx_cxy , &nic_rx_ptr->base ) );
-
-    cxy_t     nic_tx_cxy  = GET_CXY( chdev_dir.nic_tx[0] );
-    chdev_t * nic_tx_ptr  = (chdev_t *)GET_PTR( chdev_dir.nic_tx[0] );
-    xptr_t    nic_tx_base = hal_remote_lwd( XPTR( nic_tx_cxy , &nic_tx_ptr->base ) );
+    uint32_t  fbf_base = (uint32_t)hal_remote_lwd( XPTR( fbf_cxy , &fbf_ptr->base ) );
+
+    cxy_t     nic0_rx_cxy  = GET_CXY( chdev_dir.nic_rx[0] );
+    chdev_t * nic0_rx_ptr  = (chdev_t *)GET_PTR( chdev_dir.nic_rx[0] );
+    uint32_t  nic0_rx_base = (uint32_t)hal_remote_lwd( XPTR( nic0_rx_cxy , &nic0_rx_ptr->base ) );
+
+    cxy_t     nic0_tx_cxy  = GET_CXY( chdev_dir.nic_tx[0] );
+    chdev_t * nic0_tx_ptr  = (chdev_t *)GET_PTR( chdev_dir.nic_tx[0] );
+    uint32_t  nic0_tx_base = (uint32_t)hal_remote_lwd( XPTR( nic0_tx_cxy , &nic0_tx_ptr->base ) );
 
     printk("\n***** external chdev directory in cluster %x\n"
-           "  - iob       = %l / base = %l\n"
-           "  - pic       = %l / base = %l\n"
-           "  - txt[0]    = %l / base = %l\n"
-           "  - txt[1]    = %l / base = %l\n"
-           "  - txt[2]    = %l / base = %l\n"
-           "  - ioc[0]    = %l / base = %l\n"
-           "  - fbf[0]    = %l / base = %l\n"
-           "  - nic_rx[0] = %l / base = %l\n"
-           "  - nic_tx[0] = %l / base = %l\n",
+           "  - iob       : cxy = %X / ptr = %X / base = %X\n"
+           "  - pic       : cxy = %X / ptr = %X / base = %X\n"
+           "  - ioc       : cxy = %X / ptr = %X / base = %X\n"
+           "  - fbf       : cxy = %X / ptr = %X / base = %X\n"
+           "  - txt_rx[0] : cxy = %X / ptr = %X / base = %X\n"
+           "  - txt_tx[0] : cxy = %X / ptr = %X / base = %X\n"
+           "  - txt_rx[1] : cxy = %X / ptr = %X / base = %X\n"
+           "  - txt_tx[1] : cxy = %X / ptr = %X / base = %X\n"
+           "  - txt_rx[2] : cxy = %X / ptr = %X / base = %X\n"
+           "  - txt_tx[2] : cxy = %X / ptr = %X / base = %X\n"
+           "  - nic_rx[0] : cxy = %X / ptr = %X / base = %X\n"
+           "  - nic_tx[0] : cxy = %X / ptr = %X / base = %X\n",
            local_cxy,
-           chdev_dir.iob, iob_base,
-           chdev_dir.pic, pic_base,
-           chdev_dir.txt[0], txt0_base,
-           chdev_dir.txt[1], txt1_base,
-           chdev_dir.txt[2], txt2_base,
-           chdev_dir.ioc[0], ioc_base,
-           chdev_dir.fbf[0], fbf_base,
-           chdev_dir.nic_rx[0], nic_rx_base,
-           chdev_dir.nic_tx[0], nic_tx_base );
+           iob_cxy , iob_ptr , iob_base ,
+           pic_cxy , pic_ptr , pic_base ,
+           ioc_cxy , ioc_ptr , ioc_base ,
+           fbf_cxy , fbf_ptr , fbf_base ,
+           txt0_rx_cxy , txt0_rx_ptr , txt0_rx_base ,
+           txt0_tx_cxy , txt0_tx_ptr , txt0_tx_base ,
+           txt1_rx_cxy , txt1_rx_ptr , txt1_rx_base ,
+           txt1_tx_cxy , txt1_tx_ptr , txt1_tx_base ,
+           txt2_rx_cxy , txt2_rx_ptr , txt2_rx_base ,
+           txt2_tx_cxy , txt2_tx_ptr , txt2_tx_base ,
+           nic0_rx_cxy , nic0_rx_ptr , nic0_rx_base ,
+           nic0_tx_cxy , nic0_tx_ptr , nic0_tx_base );
 
 }  // end chdev_dir_display()
