Index: soft/giet_vm/sys/ctx_handler.c
===================================================================
--- soft/giet_vm/sys/ctx_handler.c	(revision 232)
+++ soft/giet_vm/sys/ctx_handler.c	(revision 238)
@@ -25,15 +25,17 @@
 // A task context is an array of 64 words = 256 bytes. 
 // It contains copies of processor registers (when the task is preempted):
-// - GPR[i], generally stored in slot (i). $0, *26 & $27 are not saved.
+// - GPR[i], generally stored in slot (i). $0, $26 & $27 are not saved.
 // - HI & LO registers
 // - CP0 registers: EPC, SR, CR, BVAR 
 // - CP2 registers : PTPR 
 // It contains some general informations associated to the task:
-// - TTY    : terminal global index
-// - FBDMA    : DMA channel global index
+// - TTY    : TTY channel global index
 // - NIC    : NIC channel global index
-// - TIMER  : Timer global index
+// - CMA    : CMA channel global index
+// - IOC    : IOC channel global index
+// - DMA    : DMA channel local index
+// - TIM    : TIM channel local index
 // - PTAB   : page table virtual base address 
-// - LTID    : Task local index (in scheduler)
+// - LTID   : Task local index (in scheduler)
 // - VSID   : Virtual space index
 // - RUN    : Task state (0 => sleeping / 1 => runable ) 
@@ -42,8 +44,8 @@
 // ctx[1]<- $1 |ctx[9] <- $9 |ctx[17]<- $17|ctx[25]<- $25|ctx[33]<- CR   |ctx[41]<- DMA
 // ctx[2]<- $2 |ctx[10]<- $10|ctx[18]<- $18|ctx[26]<- LO |ctx[34]<- SR   |ctx[42]<- NIC
-// ctx[3]<- $3 |ctx[11]<- $11|ctx[19]<- $19|ctx[27]<- HI |ctx[35]<- BVAR |ctx[43]<- TIMER
-// ctx[4]<- $4 |ctx[12]<- $12|ctx[20]<- $20|ctx[28]<- $28|ctx[36]<- ***  |ctx[44]<- PTAB
-// ctx[5]<- $5 |ctx[13]<- $13|ctx[21]<- $21|ctx[29]<- SP |ctx[37]<- ***  |ctx[45]<- LTID
-// ctx[6]<- $6 |ctx[14]<- $14|ctx[22]<- $22|ctx[30]<- $30|ctx[38]<- ***  |ctx[46]<- VSID
+// ctx[3]<- $3 |ctx[11]<- $11|ctx[19]<- $19|ctx[27]<- HI |ctx[35]<- BVAR |ctx[43]<- TIM
+// ctx[4]<- $4 |ctx[12]<- $12|ctx[20]<- $20|ctx[28]<- $28|ctx[36]<- PTAB |ctx[44]<- IOC
+// ctx[5]<- $5 |ctx[13]<- $13|ctx[21]<- $21|ctx[29]<- SP |ctx[37]<- LTID |ctx[45]<- CMA 
+// ctx[6]<- $6 |ctx[14]<- $14|ctx[22]<- $22|ctx[30]<- $30|ctx[38]<- VSID |ctx[46]<- GTID
 // ctx[7]<- $7 |ctx[15]<- $15|ctx[23]<- $23|ctx[31]<- RA |ctx[39]<- PTPR |ctx[47]<- RUN
 //////////////////////////////////////////////////////////////////////////////////////////
@@ -60,21 +62,19 @@
 // If there is no runable task, the scheduler switch to the default "idle" task.
 //
-// Implementation notes:
-// - As we only have the scheduler physical address (in CP0_SCHED register),
-//   this function must use specific assess functions to access the scheduler.
-// - All the context switch procedure is executed with interrupts masked.
-// - The return address contained in $31 is saved in the current task context
-//   (in the ctx[31] slot), and the function actually returns to the address
-//   contained in the ctx[31] slot of the next task context.
+// Implementation note
+// The return address contained in $31 is saved in the current task context
+// (in the ctx[31] slot), and the function actually returns to the address
+// contained in the ctx[31] slot of the next task context.
 /////////////////////////////////////////////////////////////////////////////////
-void _ctx_switch() {
-    // get scheduler physical address
-    static_scheduler_t * psched = (static_scheduler_t *) _get_sched();
+void _ctx_switch() 
+{
+    // get scheduler address
+    static_scheduler_t* psched = _get_sched();
 
     // get number of tasks allocated to scheduler
-    unsigned int tasks = _get_tasks_number();
+    unsigned int tasks = psched->tasks;
 
     // get current task index
-    unsigned int curr_task_id = _get_proc_task_id();
+    unsigned int curr_task_id = psched->current;
 
     // select the next task using a round-robin policy
@@ -83,9 +83,10 @@
     unsigned int found = 0;
 
-    for (tid = curr_task_id + 1; tid < curr_task_id + 1 + tasks; tid++) {
+    for (tid = curr_task_id + 1; tid < curr_task_id + 1 + tasks; tid++) 
+    {
         next_task_id = tid % tasks;
-
         // test if the task is runable
-        if (_get_context_slot(next_task_id, CTX_RUN_ID)) {
+        if ( psched->context[next_task_id][CTX_RUN_ID] ) 
+        {
             found = 1;
             break;
@@ -94,35 +95,41 @@
 
     // launch "idle" task if no runable task
-    if (found == 0) {
+    if (found == 0) 
+    {
         next_task_id = IDLE_TASK_INDEX;
     }
 
     // no switch if no change
-    if (curr_task_id != next_task_id) {
-        unsigned int * curr_ctx_paddr = &(psched->context[curr_task_id][0]);
-        unsigned int * next_ctx_paddr = &(psched->context[next_task_id][0]);
+    if (curr_task_id != next_task_id) 
+    {
+        unsigned int* curr_ctx_vaddr = &(psched->context[curr_task_id][0]);
+        unsigned int* next_ctx_vaddr = &(psched->context[next_task_id][0]);
 
-        _set_proc_task_id(next_task_id);
-        //_timer_reset_irq_cpt(cluster_id, local_id); // commented until not properly supported in soclib
+        // set current task index 
+        psched->current = next_task_id;
+
+        //_timer_reset_irq_cpt(cluster_id, local_id); 
+        // commented until not properly supported in soclib
         // (the function is not yet present in drivers.c)
-        _task_switch(curr_ctx_paddr, next_ctx_paddr);
+
+        _task_switch(curr_ctx_vaddr, next_ctx_vaddr);
 
 #if GIET_DEBUG_SWITCH
-        _get_lock(&_tty_put_lock);
-        _puts("\n[GIET DEBUG] Context switch for processor ");
-        _putd(_procid());
-        _puts(" at cycle ");
-        _putd(_proctime());
-        _puts("\n");
-        _puts(" - tasks        = ");
-        _putd(tasks);
-        _puts("\n");
-        _puts(" - curr_task_id = ");
-        _putd( curr_task_id );
-        _puts("\n");
-        _puts(" - next_task_id = ");
-        _putd(next_task_id);
-        _puts("\n");
-        _release_lock( &_tty_put_lock);
+_get_lock(&_tty_put_lock);
+_puts("\n[GIET DEBUG] Context switch for processor ");
+_putd(_procid());
+_puts(" at cycle ");
+_putd(_proctime());
+_puts("\n");
+_puts(" - tasks        = ");
+_putd(tasks);
+_puts("\n");
+_puts(" - curr_task_id = ");
+_putd( curr_task_id );
+_puts("\n");
+_puts(" - next_task_id = ");
+_putd(next_task_id);
+_puts("\n");
+_release_lock( &_tty_put_lock);
 #endif
 
@@ -133,5 +140,6 @@
 // This function is executed as the"idle" task when no other task can be executed 
 /////////////////////////////////////////////////////////////////////////////////////
-void _ctx_idle() {
+void _ctx_idle() 
+{
     unsigned int delay = 1000000;
 
@@ -164,5 +172,6 @@
 // in the "idle" task context.
 /////////////////////////////////////////////////////////////////////////////////
-void _ctx_eret() {
+void _ctx_eret() 
+{
     asm volatile("eret");
 }
