Index: trunk/kernel/kern/kernel_init.c
===================================================================
--- trunk/kernel/kern/kernel_init.c	(revision 406)
+++ trunk/kernel/kern/kernel_init.c	(revision 407)
@@ -32,4 +32,5 @@
 #include <barrier.h>
 #include <remote_barrier.h>
+#include <remote_fifo.h>
 #include <core.h>
 #include <list.h>
@@ -85,5 +86,5 @@
 cluster_t            cluster_manager                         CONFIG_CACHE_LINE_ALIGNED;
 
-// This variable defines the TXT0 kernel terminal 
+// This variable defines the TXT0 kernel terminal (TX only)
 __attribute__((section(".kdata")))
 chdev_t              txt0_chdev                              CONFIG_CACHE_LINE_ALIGNED;
@@ -121,4 +122,30 @@
 vfs_ctx_t            fs_context[FS_TYPES_NR]                 CONFIG_CACHE_LINE_ALIGNED;
 
+// These variables are used by the sched_yield function to save SR value
+__attribute__((section(".kdata")))
+uint32_t             switch_save_sr[CONFIG_MAX_LOCAL_CORES]  CONFIG_CACHE_LINE_ALIGNED;
+
+#if CONFIG_READ_DEBUG
+uint32_t   enter_sys_read;
+uint32_t   exit_sys_read;
+
+uint32_t   enter_devfs_move;
+uint32_t   exit_devfs_move;
+
+uint32_t   enter_txt_read;
+uint32_t   exit_txt_read;
+
+uint32_t   enter_chdev_cmd;
+uint32_t   exit_chdev_cmd;
+
+uint32_t   enter_chdev_server;
+uint32_t   exit_chdev_server;
+
+uint32_t   enter_tty_cmd;
+uint32_t   exit_tty_cmd;
+
+uint32_t   enter_tty_isr;
+uint32_t   exit_tty_isr;
+#endif
 
 ///////////////////////////////////////////////////////////////////////////////////////////
@@ -137,5 +164,6 @@
            "    /_/        \\_\\ |______| |_|    |_|   \\_____/  |______/        |_|    |_|  |_|  \\_\\ |_|   |_|  \n"
            "\n\n\t\t Advanced Locality Management Operating System / Multi Kernel Hybrid\n"
-           "\n\n\t\t\t Version 0.0 / %d cluster(s) / %d core(s) per cluster\n\n", nclusters , ncores );
+           "\n\n\t\t Version 0.0 / %d cluster(s) / %d core(s) per cluster / cycle %d\n\n",
+           nclusters , ncores , hal_time_stamp() );
 }
 
@@ -201,5 +229,5 @@
                 {
                     cxy_t  cxy = (x<<info->y_width) + y;
-                    hal_remote_swd( XPTR( cxy , &chdev_dir.txt[0] ) ,
+                    hal_remote_swd( XPTR( cxy , &chdev_dir.txt_tx[0] ) ,
                                     XPTR( local_cxy , &txt0_chdev ) );
                 }
@@ -273,7 +301,8 @@
             }
 
-#if( CONFIG_KINIT_DEBUG > 1 )
-printk("\n[DMSG] %s : created MMC in cluster %x / chdev = %x\n",
-__FUNCTION__ , channel , local_cxy , chdev_ptr );
+#if( CONFIG_KINIT_DEBUG & 0x1 )
+if( hal_time_stamp() > CONFIG_KINIT_DEBUG )
+printk("\n[DBG] %s : created MMC in cluster %x / chdev = %x\n",
+__FUNCTION__ , local_cxy , chdev_ptr );
 #endif
         }
@@ -301,6 +330,7 @@
                 chdev_dir.dma[channel] = XPTR( local_cxy , chdev_ptr );
 
-#if( CONFIG_KINIT_DEBUG > 1 )
-printk("\n[DMSG] %s : created DMA[%d] in cluster %x / chdev = %x\n",
+#if( CONFIG_KINIT_DEBUG & 0x1 )
+if( hal_time_stamp() > CONFIG_KINIT_DEBUG )
+printk("\n[DBG] %s : created DMA[%d] in cluster %x / chdev = %x\n",
 __FUNCTION__ , channel , local_cxy , chdev_ptr );
 #endif
@@ -355,7 +385,7 @@
         impl     = IMPL_FROM_TYPE( dev_tbl[i].type );
 
-        // There is one chdev per direction for NIC
-        if (func == DEV_FUNC_NIC) directions = 2;
-        else                      directions = 1;
+        // There is one chdev per direction for NIC and for TXT
+        if((func == DEV_FUNC_NIC) || (func == DEV_FUNC_TXT)) directions = 2;
+        else                                                 directions = 1;
 
         // The TXT0 chdev has already been created
@@ -363,5 +393,5 @@
         else                      first_channel = 0;
 
-        // do nothing for RO, that does not require a device descriptor.
+        // do nothing for ROM, that does not require a device descriptor.
         if( func == DEV_FUNC_ROM ) continue;
 
@@ -394,5 +424,5 @@
 
                 // allocate and initialize a local chdev
-                // if local cluster matches target cluster
+                // when local cluster matches target cluster
                 if( target_cxy == local_cxy )
                 {
@@ -420,6 +450,7 @@
                     if(func==DEV_FUNC_IOB             ) entry  = &chdev_dir.iob;
                     if(func==DEV_FUNC_IOC             ) entry  = &chdev_dir.ioc[channel];
-                    if(func==DEV_FUNC_TXT             ) entry  = &chdev_dir.txt[channel];
                     if(func==DEV_FUNC_FBF             ) entry  = &chdev_dir.fbf[channel];
+                    if((func==DEV_FUNC_TXT) && (rx==0)) entry  = &chdev_dir.txt_tx[channel];
+                    if((func==DEV_FUNC_TXT) && (rx==1)) entry  = &chdev_dir.txt_rx[channel];
                     if((func==DEV_FUNC_NIC) && (rx==0)) entry  = &chdev_dir.nic_tx[channel];
                     if((func==DEV_FUNC_NIC) && (rx==1)) entry  = &chdev_dir.nic_rx[channel];
@@ -435,7 +466,8 @@
                     }
 
-#if( CONFIG_KINIT_DEBUG > 1 )
-printk("\n[DMSG] %s : create chdev %s[%d] in cluster %x / chdev = %x\n",
-__FUNCTION__ , chdev_func_str( func ), channel , local_cxy , chdev );
+#if( CONFIG_KINIT_DEBUG & 0x1 )
+if( hal_time_stamp() > CONFIG_KINIT_DEBUG )
+printk("\n[DBG] %s : create chdev %s / channel = %d / rx = %d / cluster %x / chdev = %x\n",
+__FUNCTION__ , chdev_func_str( func ), channel , rx , local_cxy , chdev );
 #endif
                 }  // end if match
@@ -451,7 +483,8 @@
 ///////////////////////////////////////////////////////////////////////////////////////////
 // This function is called by CP0 in cluster 0 to allocate memory and initialize the PIC
-// device, namely the informations attached to the external IOPIC controller.
+// device, namely the informations attached to the external IOPIC controller, that
+// must be replicated in all clusters (struct iopic_input).
 // This initialisation must be done before other devices initialisation because the IRQ
-// routing infrastructure is required for internal and external devices initialisation.
+// routing infrastructure is required for both internal and external devices init.
 ///////////////////////////////////////////////////////////////////////////////////////////
 // @ info    : pointer on the local boot-info structure.
@@ -490,6 +523,6 @@
     assert( found , __FUNCTION__ , "PIC device not found\n" );
 
-    // allocate and initialize the PIC chdev in local cluster
-    chdev = chdev_create( func,
+    // allocate and initialize the PIC chdev in cluster 0
+    chdev = chdev_create( DEV_FUNC_PIC,
                           impl,
                           0,      // channel
@@ -502,5 +535,6 @@
     dev_pic_init( chdev );
 
-    // register extended pointer on PIC chdev in "chdev_dir" array in all clusters
+    // register, in all clusters, the extended pointer
+    // on PIC chdev in "chdev_dir" array
     xptr_t * entry = &chdev_dir.pic;    
                 
@@ -515,11 +549,24 @@
     }
 
-    // initialize the "iopic_input" structure
+    // initialize, in all clusters, the "iopic_input" structure
     // defining how external IRQs are connected to IOPIC 
-    uint32_t   id;
-    uint8_t    valid;
-    uint32_t   type;
-    uint8_t    channel;
-    uint8_t    is_rx;
+
+    // register default value for unused inputs 
+    for( x = 0 ; x < info->x_size ; x++ )
+    {
+        for( y = 0 ; y < info->y_size ; y++ )
+        {
+            cxy_t  cxy = (x<<info->y_width) + y;
+            hal_remote_memset( XPTR( cxy , &iopic_input ) , 0xFF , sizeof(iopic_input_t) );
+        }
+    }
+
+    // register input IRQ index for valid inputs 
+    uint32_t   id;         // input IRQ index
+    uint8_t    valid;      // input IRQ is connected
+    uint32_t   type;       // source device type
+    uint8_t    channel;    // source device channel
+    uint8_t    is_rx;      // source device direction
+    uint32_t * ptr;        // local pointer on one field in iopic_input stucture
 
     for( id = 0 ; id < CONFIG_MAX_EXTERNAL_IRQS ; id++ )
@@ -529,31 +576,36 @@
         channel = dev_tbl[i].irq[id].channel;
         is_rx   = dev_tbl[i].irq[id].is_rx;
-
-        if( valid )  // only valid inputs are registered
-        {
-            uint32_t * index;  // local pointer on one entry
-            uint16_t func = FUNC_FROM_TYPE( type );
-
-            if     ( func == DEV_FUNC_TXT ) 
-            index = &iopic_input.txt[channel];
-            else if( func == DEV_FUNC_IOC ) 
-            index = &iopic_input.ioc[channel]; 
-            else if( (func == DEV_FUNC_NIC) && (is_rx == 0) )
-            index = &iopic_input.nic_tx[channel]; 
-            else if( (func == DEV_FUNC_NIC) && (is_rx != 0) )
-            index = &iopic_input.nic_rx[channel]; 
-            else if( func == DEV_FUNC_IOB )
-            index = &iopic_input.iob; 
-            else
-            assert( false , __FUNCTION__ , "illegal source device for IOPIC input" );
-
-            // set entry in local structure
-            *index = id; 
+        func    = FUNC_FROM_TYPE( type );
+
+        // get pointer on relevant field in iopic_input
+        if( valid )
+        {
+            if     ( func == DEV_FUNC_IOC )                 ptr = &iopic_input.ioc[channel]; 
+            else if((func == DEV_FUNC_TXT) && (is_rx == 0)) ptr = &iopic_input.txt_tx[channel];
+            else if((func == DEV_FUNC_TXT) && (is_rx != 0)) ptr = &iopic_input.txt_rx[channel];
+            else if((func == DEV_FUNC_NIC) && (is_rx == 0)) ptr = &iopic_input.nic_tx[channel]; 
+            else if((func == DEV_FUNC_NIC) && (is_rx != 0)) ptr = &iopic_input.nic_rx[channel]; 
+            else if( func == DEV_FUNC_IOB )                 ptr = &iopic_input.iob; 
+            else     panic( "illegal source device for IOPIC input" );
+
+            // set one entry in all "iopic_input" structures
+            for( x = 0 ; x < info->x_size ; x++ )
+            {
+                for( y = 0 ; y < info->y_size ; y++ )
+                {
+                    cxy_t  cxy = (x<<info->y_width) + y;
+                    hal_remote_swd( XPTR( cxy , ptr ) , id ); 
+                }
+            }
         }
     } 
 
-#if( CONFIG_KINIT_DEBUG > 1 )
-printk("\n[DMSG] %s created PIC chdev in cluster %x at cycle %d\n",
-__FUNCTION__ , local_cxy , (uint32_t)hal_time_stamp() );
+#if( CONFIG_KINIT_DEBUG & 0x1 )
+if( hal_time_stamp() > CONFIG_KINIT_DEBUG )
+{
+    printk("\n[DBG] %s created PIC chdev in cluster %x at cycle %d\n",
+    __FUNCTION__ , local_cxy , (uint32_t)hal_time_stamp() );
+    dev_pic_inputs_display();
+}
 #endif
     
@@ -715,5 +767,8 @@
     hal_set_current_thread( thread );
 
-    // each core initializes the idle thread "locks_root" and "xlocks_root" fields
+    // each core register core descriptor pointer in idle thread descriptor
+    thread->core = &LOCAL_CLUSTER->core_tbl[core_lid];
+
+    // each core initializes locks_root" and "xlocks_root" in idle thread descriptor
     list_root_init( &thread->locks_root );
     xlist_root_init( XPTR( local_cxy , &thread->xlocks_root ) );
@@ -728,9 +783,9 @@
 
     if( (core_lid ==  0) && (local_cxy == 0) ) 
-    printk("\n[KINIT] %s : exit barrier 0 : TXT0 initialized / cycle %d\n",
+    kinit_dmsg("\n[DBG] %s : exit barrier 0 : TXT0 initialized / cycle %d\n",
     __FUNCTION__, hal_time_stamp() );
 
     /////////////////////////////////////////////////////////////////////////////
-    // STEP 1 : all cores check its core identifier.
+    // STEP 1 : all cores check core identifier.
     //          CP0 initializes the local cluster manager.
     //          This includes the memory allocators.
@@ -762,9 +817,9 @@
 
     if( (core_lid ==  0) && (local_cxy == 0) ) 
-    printk("\n[KINIT] %s : exit barrier 1 : clusters initialised / cycle %d\n",
+    kinit_dmsg("\n[DBG] %s : exit barrier 1 : clusters initialised / cycle %d\n",
     __FUNCTION__, hal_time_stamp() );
 
     /////////////////////////////////////////////////////////////////////////////////
-    // STEP 2 : all CP0s initialize the process_zero descriptor.
+    // STEP 2 : CP0 initializes the process_zero descriptor.
     //          CP0 in cluster 0 initializes the IOPIC device.
     /////////////////////////////////////////////////////////////////////////////////
@@ -787,11 +842,11 @@
 
     if( (core_lid ==  0) && (local_cxy == 0) ) 
-    printk("\n[KINIT] %s : exit barrier 2 : PIC initialised / cycle %d\n",
+    kinit_dmsg("\n[DBG] %s : exit barrier 2 : PIC initialised / cycle %d\n",
     __FUNCTION__, hal_time_stamp() );
 
     ////////////////////////////////////////////////////////////////////////////////
-    // STEP 3 : all CP0s initialize the distibuted LAPIC descriptor.
-    //          all CP0s initialize the internal chdev descriptors
-    //          all CP0s initialize the local external chdev descriptors
+    // STEP 3 : CP0 initializes the distibuted LAPIC descriptor.
+    //          CP0 initializes the internal chdev descriptors
+    //          CP0 initialize the local external chdev descriptors
     ////////////////////////////////////////////////////////////////////////////////
 
@@ -818,10 +873,9 @@
 
     if( (core_lid ==  0) && (local_cxy == 0) ) 
-    printk("\n[KINIT] %s : exit barrier 3 : all chdev initialised / cycle %d\n",
+    kinit_dmsg("\n[DBG] %s : exit barrier 3 : all chdev initialised / cycle %d\n",
                __FUNCTION__, hal_time_stamp());
 
     /////////////////////////////////////////////////////////////////////////////////
     // STEP 4 : All cores enable IPI (Inter Procesor Interrupt),
-    //          All cores initialise specific core registers
     //          Alh cores initialize IDLE thread.
     //          Only CP0 in cluster 0 creates the VFS root inode.
@@ -837,7 +891,4 @@
     hal_enable_irq( &status );
 
-    // All cores initialize specific core registers
-    hal_core_init( info );
-
     // all cores initialize the idle thread descriptor
     error = thread_kernel_init( thread,
@@ -857,5 +908,5 @@
 
 #if CONFIG_KINIT_DEBUG
-sched_display();
+sched_display( core_lid );
 #endif
 
@@ -928,5 +979,5 @@
 
     if( (core_lid ==  0) && (local_cxy == 0) ) 
-    printk("\n[KINIT] %s : exit barrier 4 : VFS_root = %l in cluster 0 / cycle %d\n",
+    kinit_dmsg("\n[DBG] %s : exit barrier 4 : VFS_root = %l in cluster 0 / cycle %d\n",
                __FUNCTION__, vfs_root_inode_xp , hal_time_stamp());
 
@@ -987,5 +1038,5 @@
 
     if( (core_lid ==  0) && (local_cxy == 0) ) 
-    printk("\n[KINIT] %s : exit barrier 5 : VFS_root = %l in cluster IO / cycle %d\n",
+    kinit_dmsg("\n[DBG] %s : exit barrier 5 : VFS_root = %l in cluster IO / cycle %d\n",
     __FUNCTION__, vfs_root_inode_xp , hal_time_stamp() );
 
@@ -1020,5 +1071,5 @@
 
     if( (core_lid ==  0) && (local_cxy == 0) ) 
-    printk("\n[KINIT] %s : exit barrier 6 : dev_root = %l in cluster IO / cycle %d\n",
+    kinit_dmsg("\n[DBG] %s : exit barrier 6 : dev_root = %l in cluster IO / cycle %d\n",
     __FUNCTION__, devfs_dev_inode_xp , hal_time_stamp() );
 
@@ -1057,5 +1108,5 @@
 
     if( (core_lid ==  0) && (local_cxy == 0) ) 
-    printk("\n[KINIT] %s : exit barrier 7 : dev_root = %l in cluster 0 / cycle %d\n",
+    kinit_dmsg("\n[DBG] %s : exit barrier 7 : dev_root = %l in cluster 0 / cycle %d\n",
     __FUNCTION__, devfs_dev_inode_xp , hal_time_stamp() );
 
@@ -1075,6 +1126,10 @@
     /////////////////////////////////////////////////////////////////////////////////
 
+#if CONFIG_KINIT_DEBUG
+sched_display( core_lid );
+#endif
+
     if( (core_lid ==  0) && (local_cxy == 0) ) 
-    printk("\n[KINIT] %s : exit barrier 8 : process init created / cycle %d\n", 
+    kinit_dmsg("\n[DBG] %s : exit barrier 8 : process init created / cycle %d\n", 
     __FUNCTION__ , hal_time_stamp() );
 
@@ -1118,5 +1173,5 @@
                    sizeof( core_t            ),
                    sizeof( scheduler_t       ),
-                   sizeof( rpc_fifo_t        ),
+                   sizeof( remote_fifo_t     ),
                    sizeof( page_t            ),
                    sizeof( mapper_t          ),
@@ -1139,5 +1194,5 @@
     dev_pic_enable_timer( CONFIG_SCHED_TICK_MS_PERIOD );
 
-    // each core jump to idle thread
+    // each core jump to thread_idle_func
     thread_idle_func();
 }
