Index: soft/giet_vm/giet_kernel/sys_handler.c
===================================================================
--- soft/giet_vm/giet_kernel/sys_handler.c	(revision 519)
+++ soft/giet_vm/giet_kernel/sys_handler.c	(revision 528)
@@ -61,14 +61,18 @@
 #endif
 
-
 ////////////////////////////////////////////////////////////////////////////
-//     Coprocessors loks and synchronisation variables
+//        Extern variables
 ////////////////////////////////////////////////////////////////////////////
 
-__attribute__((section(".kdata")))
-simple_lock_t  _coproc_lock[X_SIZE*Y_SIZE];
-
-__attribute__((section(".kdata")))
-unsigned int   _coproc_done[X_SIZE*Y_SIZE];
+// allocated in tty0.c file.
+extern sqt_lock_t _tty0_sqt_lock;
+
+// allocated in mwr_driver.c file.
+extern simple_lock_t  _coproc_lock[X_SIZE*Y_SIZE];
+extern unsigned int   _coproc_done[X_SIZE*Y_SIZE];
+
+// allocated in tty_driver.c file.
+extern unsigned int _tty_rx_full[NB_TTY_CHANNELS];
+extern unsigned int _tty_rx_buf[NB_TTY_CHANNELS];
 
 ////////////////////////////////////////////////////////////////////////////
@@ -91,14 +95,4 @@
 __attribute__((section(".kdata")))
 unsigned int _nic_tx_channel_allocator = 0;
-
-////////////////////////////////////////////////////////////////////////////
-// These global variables are allocated in tty0.c and tty_driver.c files.
-////////////////////////////////////////////////////////////////////////////
-
-extern sqt_lock_t _tty0_sqt_lock;
-
-extern unsigned int _tty_rx_full[NB_TTY_CHANNELS];
-
-extern unsigned int _tty_rx_buf[NB_TTY_CHANNELS];
 
 ////////////////////////////////////////////////////////////////////////////
@@ -157,5 +151,5 @@
     &_sys_ukn,                  /* 0x17 */
     &_sys_ukn,                  /* 0x18 */   
-    &_context_switch,           /* 0x19 */
+    &_sys_context_switch,       /* 0x19 */
     &_sys_vseg_get_vbase,       /* 0x1A */
     &_sys_vseg_get_length,      /* 0x1B */
@@ -355,8 +349,5 @@
     unsigned int size = desc->buffer_size;
  
-    // these variables are used for the v2p translations
-    unsigned int       ptab  = _get_context_slot(CTX_PTAB_ID);
-    unsigned int       ppn;
-    unsigned int       flags;
+    // physical addresses
     unsigned long long buffer_paddr;
     unsigned int       buffer_lsb;
@@ -369,13 +360,10 @@
     unsigned int       lock_msb;
 
+    unsigned int       flags;     // unused
+
     // compute memory buffer physical address
-    _v2p_translate( (page_table_t*)ptab,
-                    desc->buffer_vaddr>>12,
-                    &ppn,
-                    &flags );
-    buffer_paddr = (((unsigned long long)ppn) << 12) | 
-                   (desc->buffer_vaddr & 0x00000FFF);
-    buffer_lsb = (unsigned int)buffer_paddr;
-    buffer_msb = (unsigned int)(buffer_paddr>>32); 
+    buffer_paddr = _v2p_translate( desc->buffer_vaddr , &flags );
+    buffer_lsb   = (unsigned int)buffer_paddr;
+    buffer_msb   = (unsigned int)(buffer_paddr>>32); 
 
     // call MWMR_DMA driver
@@ -385,28 +373,21 @@
     _mwr_set_channel_register( cluster_xy, channel, CHANNEL_BUFFER_MSB, buffer_msb ); 
                        
-    // compute MWMR descriptor and lock physical addresses (if required)
     if ( mode == MODE_MWMR )
     {
-        _v2p_translate( (page_table_t*)ptab,
-                        desc->mwmr_vaddr>>12,
-                        &ppn,
-                        &flags );
-        mwmr_paddr = (((unsigned long long)ppn) << 12) | 
-                     (desc->mwmr_vaddr & 0x00000FFF);
+        // compute MWMR descriptor physical address
+        mwmr_paddr = _v2p_translate( desc->mwmr_vaddr , &flags );
         mwmr_lsb = (unsigned int)mwmr_paddr;
         mwmr_msb = (unsigned int)(mwmr_paddr>>32); 
-
-        _v2p_translate( (page_table_t*)ptab,
-                        desc->lock_vaddr>>12,
-                        &ppn,
-                        &flags );
-        lock_paddr = (((unsigned long long)ppn) << 12) | 
-                     (desc->lock_vaddr & 0x00000FFF);
-        lock_lsb = (unsigned int)lock_paddr;
-        lock_msb = (unsigned int)(lock_paddr>>32); 
 
         // call MWMR_DMA driver
         _mwr_set_channel_register( cluster_xy, channel, CHANNEL_MWMR_LSB, mwmr_lsb ); 
         _mwr_set_channel_register( cluster_xy, channel, CHANNEL_MWMR_MSB, mwmr_msb ); 
+
+        // compute lock physical address
+        lock_paddr = _v2p_translate( desc->lock_vaddr , &flags );
+        lock_lsb = (unsigned int)lock_paddr;
+        lock_msb = (unsigned int)(lock_paddr>>32); 
+
+        // call MWMR_DMA driver
         _mwr_set_channel_register( cluster_xy, channel, CHANNEL_LOCK_LSB, lock_lsb ); 
         _mwr_set_channel_register( cluster_xy, channel, CHANNEL_LOCK_MSB, lock_msb ); 
@@ -740,15 +721,12 @@
     unsigned long long ker_chbuf_pbase;     // kernel chbuf physical address
 
-    // These variables are used for the various V2P translation
-    unsigned int       ptab  = _get_context_slot(CTX_PTAB_ID);
-    unsigned int       ppn;
-    unsigned int       flags;
-    unsigned int       vaddr;
-
     // allocate one kernel container per cluster in the (xmax / ymax) mesh
-    unsigned int        cx;              // cluster X coordinate
-    unsigned int        cy;              // cluster Y coordinate
-    unsigned int        index;           // container index in chbuf
-    unsigned long long  cont_paddr;      // container physical address
+    unsigned int        cx;                 // cluster X coordinate
+    unsigned int        cy;                 // cluster Y coordinate
+    unsigned int        index;              // container index in chbuf
+    unsigned int        vaddr;              // virtual address
+    unsigned long long  cont_paddr;         // container physical address
+
+    unsigned int        flags;              // for _v2p_translate()
 
     for ( cx = 0 ; cx < xmax ; cx++ )
@@ -770,9 +748,5 @@
 
             // compute container physical address
-            _v2p_translate( (page_table_t*)ptab,
-                            vaddr>>12,
-                            &ppn,
-                            &flags );
-            cont_paddr = (((unsigned long long)ppn) << 12) | (vaddr & 0x00000FFF);
+            cont_paddr = _v2p_translate( vaddr , &flags );
 
             // initialize chbuf entry
@@ -816,9 +790,6 @@
     if ( is_rx ) vaddr = (unsigned int)( &_nic_rx_chbuf[nic_channel] );
     else         vaddr = (unsigned int)( &_nic_tx_chbuf[nic_channel] );
-    _v2p_translate( (page_table_t*)ptab,
-                     vaddr>>12,
-                     &ppn,
-                     &flags );
-    ker_chbuf_pbase = (((unsigned long long)ppn) << 12) | (vaddr & 0x00000FFF);
+
+    ker_chbuf_pbase = _v2p_translate( vaddr , &flags );
 
 #if GIET_DEBUG_NIC
@@ -965,20 +936,11 @@
     unsigned long long kernel_buffer_paddr;  // kernel buffer physical address
     unsigned long long kernel_chbuf_paddr;   // kernel chbuf physical address
-    unsigned long long buffer_desc;          // kernel buffer descriptor
-    unsigned long long buffer_desc_paddr;    // kernel buffer descriptor physical address
+    unsigned long long buffer_descriptor;    // kernel buffer descriptor
+    unsigned long long buffer_desc_paddr;    // kernel buffer descriptor paddr
     unsigned int       index;                // kernel buffer index in chbuf
-
-    // The following variables are used for V2P translation
-    unsigned int ptab = _get_context_slot( CTX_PTAB_ID );
-    unsigned int ppn;
-    unsigned int flags;
-    unsigned int vaddr;
+    unsigned int       flags;                // for _v2P_translate
 
     // Compute user buffer physical address and check access rights
-    vaddr = (unsigned int)buffer;
-    _v2p_translate( (page_table_t*)ptab,
-                     vaddr>>12,
-                     &ppn,
-                     &flags );
+    user_buffer_paddr = _v2p_translate( (unsigned int)buffer , &flags );
 
     if ( (flags & PTE_U) == 0 )
@@ -987,5 +949,4 @@
         return -1;
     }
-    user_buffer_paddr = ((unsigned long long)ppn << 12) | (vaddr & 0x00000FFF);
 
 #if GIET_DEBUG_NIC
@@ -995,10 +956,5 @@
 
     // compute kernel chbuf physical address (required for sync)
-    vaddr = (unsigned int)chbuf;
-    _v2p_translate( (page_table_t*)ptab,
-                     vaddr>>12,
-                     &ppn,
-                     &flags );
-    kernel_chbuf_paddr = ((unsigned long long)ppn << 12) | (vaddr & 0x00000FFF);
+    kernel_chbuf_paddr = _v2p_translate( (unsigned int)chbuf , &flags );
 
     // poll local kernel container status until success
@@ -1011,19 +967,19 @@
         // inval buffer descriptor in L2 before read in L2
         _mmc_inval( buffer_desc_paddr , 8 );
-        buffer_desc = chbuf->buffer[index].desc;
+        buffer_descriptor = chbuf->buffer[index].desc;
 
 #if GIET_DEBUG_NIC
 _printf("\n[GIET DEBUG NIC] Task %d in _sys_nic_move() read buffer descriptor %d\n"
         " at cycle = %d / paddr = %l / buffer descriptor = %l\n",
-        thread, index, _get_proctime(), buffer_desc_paddr, buffer_desc );
+        thread, index, _get_proctime(), buffer_desc_paddr, buffer_descriptor );
 #endif
 
         // test buffer status and break if found
-        if ( ( is_rx != 0 ) && (buffer_desc >> 63) == 1 )  break;
-        if ( ( is_rx == 0 ) && (buffer_desc >> 63) == 0 )  break;
+        if ( ( is_rx != 0 ) && (buffer_descriptor >> 63) == 1 )  break;
+        if ( ( is_rx == 0 ) && (buffer_descriptor >> 63) == 0 )  break;
     }
 
     // compute kernel buffer physical address
-    kernel_buffer_paddr = buffer_desc & 0x0000FFFFFFFFFFFFULL;
+    kernel_buffer_paddr = buffer_descriptor & 0x0000FFFFFFFFFFFFULL;
     
     // move one container
@@ -1281,8 +1237,6 @@
 #if NB_CMA_CHANNELS > 0
 
-    unsigned int       ptab;            // page table virtual address
     unsigned int       vaddr;           // virtual address
-    unsigned int       flags;           // protection flags
-    unsigned int       ppn;             // physical page number
+    unsigned int       flags;           // for _v2p_translate()
 
     // get channel index
@@ -1308,49 +1262,35 @@
 
     // checking user buffers virtual addresses and length alignment
-    if ( ((unsigned int)vbase0 & 0x3) || ((unsigned int)vbase1 & 0x3) || (length & 0x3) ) 
-    {
-        _printf("\n[GIET ERROR] in _fbf_cma_start() : user buffer not word aligned\n");
-        return -1;
-    }
-
-    // get page table virtual address
-    ptab = _get_context_slot(CTX_PTAB_ID);
+    if ( ((unsigned int)vbase0 & 0x3) || 
+         ((unsigned int)vbase1 & 0x3) || 
+         (length & 0x3) ) 
+    {
+        _printf("\n[GIET ERROR] in _fbf_cma_start() : user buffer not aligned\n");
+        return -1;
+    }
 
     // compute frame buffer physical address and initialize _fbf_chbuf[channel]
-    vaddr = ((unsigned int)SEG_FBF_BASE);
-    _v2p_translate( (page_table_t*) ptab, 
-                    (vaddr >> 12),
-                    &ppn, 
-                    &flags );
-
-    _fbf_chbuf[channel].fbf.desc = ((paddr_t)ppn << 12) | (vaddr & 0x00000FFF);
+    vaddr = (unsigned int)SEG_FBF_BASE;
+    _fbf_chbuf[channel].fbf.desc = _v2p_translate( vaddr , &flags );
 
     // Compute user buffer 0 physical addresses and intialize _fbf_chbuf[channel]
-    vaddr = (unsigned int)vbase0; 
-    _v2p_translate( (page_table_t*) ptab, 
-                    (vaddr >> 12),
-                    &ppn, 
-                    &flags );
+    vaddr = (unsigned int)vbase0;
+    _fbf_chbuf[channel].buf0.desc = _v2p_translate( vaddr , &flags );
+
     if ((flags & PTE_U) == 0) 
     {
-        _printf("\n[GIET ERROR] in _fbf_cma_start() : user buffer 0 not in user space\n");
-        return -1;
-    }
-
-    _fbf_chbuf[channel].buf0.desc = ((paddr_t)ppn << 12) | (vaddr & 0x00000FFF);
+        _printf("\n[GIET ERROR] in _fbf_cma_start() : buf0 not in user space\n");
+        return -1;
+    }
 
     // Compute user buffer 1 physical addresses and intialize _fbf_chbuf[channel]
-    vaddr = (unsigned int)vbase1; 
-    _v2p_translate( (page_table_t*) ptab, 
-                    (vaddr >> 12),
-                    &ppn, 
-                    &flags );
+    vaddr = (unsigned int)vbase1;
+    _fbf_chbuf[channel].buf1.desc = _v2p_translate( vaddr , &flags );
+
     if ((flags & PTE_U) == 0) 
     {
-        _printf("\n[GIET ERROR] in _fbf_cma_start() : user buffer 1 not in user space\n");
-        return -1;
-    }
-
-    _fbf_chbuf[channel].buf1.desc = ((paddr_t)ppn << 12) | (vaddr & 0x00000FFF);
+        _printf("\n[GIET ERROR] in _fbf_cma_start() : buf1 not in user space\n");
+        return -1;
+    }
 
     // initializes buffer length
@@ -1358,12 +1298,7 @@
 
     // Compute and register physical adress of the fbf_chbuf descriptor
-    vaddr = (unsigned int)(&_fbf_chbuf[channel]);
-    _v2p_translate( (page_table_t*) ptab, 
-                    (vaddr >> 12),
-                    &ppn, 
-                    &flags );
+    vaddr = (unsigned int)&_fbf_chbuf[channel];
+    _fbf_chbuf_paddr[channel] = _v2p_translate( vaddr , &flags );
  
-    _fbf_chbuf_paddr[channel] = (((paddr_t)ppn) << 12) | (vaddr & 0x00000FFF);
-
     if ( USE_IOB )
     {
@@ -1594,11 +1529,11 @@
 
     // deschedule
-    _context_switch();
+    _sys_context_switch();
 
     return 0;
 } 
 
-//////////////////////
-int _context_switch() 
+/////////////////////////
+int _sys_context_switch() 
 {
     unsigned int save_sr;
@@ -1755,16 +1690,10 @@
                       unsigned int* y )
 {
-    unsigned int ppn;
     unsigned int flags;
-    unsigned int vpn  = (((unsigned int)ptr)>>12);
+    unsigned long long paddr = _v2p_translate( (unsigned int)ptr , &flags );
     
-    // get the page table pointer
-    page_table_t* pt = (page_table_t*)_get_context_slot( CTX_PTAB_ID ); 
-
-    // compute the physical address
-    _v2p_translate( pt, vpn, &ppn, &flags );
-
-    *x = (ppn>>24) & 0xF;
-    *y = (ppn>>20) & 0xF;
+    *x = (paddr>>36) & 0xF;
+    *y = (paddr>>32) & 0xF;
+
     return 0;
 }
