Index: trunk/kernel/kern/thread.c
===================================================================
--- trunk/kernel/kern/thread.c	(revision 428)
+++ trunk/kernel/kern/thread.c	(revision 433)
@@ -227,10 +227,13 @@
     assert( (attr != NULL) , __FUNCTION__, "pthread attributes must be defined" );
 
-thread_dmsg("\n[DBG] %s : core[%x,%d] enter for process %x\n",
-__FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, process->pid );
+#if CONFIG_DEBUG_THREAD_USER_CREATE
+uint32_t cycle = (uint32_t)hal_get_cycles();
+if( CONFIG_DEBUG_THREAD_USER_CREATE < cycle )
+printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n",
+__FUNCTION__, CURRENT_THREAD, pid , cycle );
+#endif
 
     // get process descriptor local copy
     process = process_get_local_copy( pid );
-
     if( process == NULL )
     {
@@ -326,6 +329,10 @@
     dqdt_local_update_threads( 1 );
 
-thread_dmsg("\n[DBG] %s : core[%x,%d] exit for process %x / trdid = %x / core = %d\n",
-__FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, process->pid, thread->trdid, core_lid );
+#if CONFIG_DEBUG_THREAD_USER_CREATE
+cycle = (uint32_t)hal_get_cycles();
+if( CONFIG_DEBUG_THREAD_USER_CREATE < cycle )
+printk("\n[DBG] %s : thread %x exit / process %x / new_thread %x / core %d / cycle %d\n",
+__FUNCTION__, CURRENT_THREAD, pid, thread, core_lid, cycle );
+#endif
 
     *new_thread = thread;
@@ -359,6 +366,10 @@
     vseg_t       * vseg;             // child thread STACK vseg
 
-thread_dmsg("\n[DBG] %s : core[%x,%d] enters at cycle %d\n",
-__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , hal_get_cycles() );
+#if CONFIG_DEBUG_THREAD_USER_FORK
+uint32_t cycle = (uint32_t)hal_get_cycles();
+if( CONFIG_DEBUG_THREAD_USER_FORK < cycle )
+printk("\n[DBG] %s : thread %x enter / child_process %x / cycle %d\n",
+__FUNCTION__, CURRENT_THREAD, child_process->pid, cycle );
+#endif
 
     // select a target core in local cluster
@@ -474,5 +485,5 @@
         }
 
-        // increment page descriptor fork_nr for the referenced page if mapped
+        // increment pending forks counter for the page if mapped
         if( mapped )
         {
@@ -480,23 +491,30 @@
             cxy_t    page_cxy = GET_CXY( page_xp );
             page_t * page_ptr = (page_t *)GET_PTR( page_xp );
-            hal_remote_atomic_add( XPTR( page_cxy , &page_ptr->fork_nr ) , 1 );
-
-thread_dmsg("\n[DBG] %s : core[%x,%d] copied PTE to child GPT : vpn %x\n",
-__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn );
+            hal_remote_atomic_add( XPTR( page_cxy , &page_ptr->forks ) , 1 );
+
+#if (CONFIG_DEBUG_THREAD_USER_FORK & 1)
+cycle = (uint32_t)hal_get_cycles();
+if( CONFIG_DEBUG_THREAD_USER_FORK < cycle )
+printk("\n[DBG] %s : thread %x copied stack PTE to child GPT : vpn %x\n",
+__FUNCTION__, CURRENT_THREAD, vpn );
+#endif
 
         }
     }
 
-    // set COW flag for STAK vseg in parent thread GPT 
-    hal_gpt_flip_cow( true,                               // set cow
-                      parent_gpt_xp,
-                      vpn_base,
-                      vpn_size );
+    // set COW flag for all mapped entries of STAK vseg in parent thread GPT 
+    hal_gpt_set_cow( parent_gpt_xp,
+                     vpn_base,
+                     vpn_size );
  
 	// update DQDT for child thread 
     dqdt_local_update_threads( 1 );
 
-thread_dmsg("\n[DBG] %s : core[%x,%d] exit / created main thread %x for process %x\n",
-__FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, child_ptr->trdid, child_process->pid );
+#if CONFIG_DEBUG_THREAD_USER_FORK
+cycle = (uint32_t)hal_get_cycles();
+if( CONFIG_DEBUG_THREAD_USER_FORK < cycle )
+printk("\n[DBG] %s : thread %x exit / child_process %x / child_thread %x / cycle %d\n",
+__FUNCTION__, CURRENT_THREAD, child_process->pid, child_ptr, cycle );
+#endif
 
 	return 0;
@@ -514,7 +532,4 @@
 	thread_t     * thread;       // pointer on new thread descriptor
 
-thread_dmsg("\n[DBG] %s : core[%x,%d] enters / type %s / cycle %d\n",
-__FUNCTION__ , local_cxy , core_lid , thread_type_str( type ) , hal_time_stamp() );
-
     assert( ( (type == THREAD_IDLE) || (type == THREAD_RPC) || (type == THREAD_DEV) ) ,
     __FUNCTION__ , "illegal thread type" );
@@ -522,4 +537,11 @@
     assert( (core_lid < LOCAL_CLUSTER->cores_nr) ,
             __FUNCTION__ , "illegal core_lid" );
+
+#if CONFIG_DEBUG_THREAD_KERNEL_CREATE
+uint32_t cycle = (uint32_t)hal_get_cycles();
+if( CONFIG_DEBUG_THREAD_KERNEL_CREATE < cycle )
+printk("\n[DBG] %s : thread %x enter / requested_type %s / cycle %d\n",
+__FUNCTION__, CURRENT_THREAD, thread, thread_type_str(type), cycle );
+#endif
 
     // allocate memory for new thread descriptor
@@ -549,6 +571,10 @@
     dqdt_local_update_threads( 1 );
 
-thread_dmsg("\n[DBG] %s : core = [%x,%d] exit / trdid = %x / type %s / cycle %d\n",
-__FUNCTION__, local_cxy, core_lid, thread->trdid, thread_type_str(type), hal_time_stamp() );
+#if CONFIG_DEBUG_THREAD_KERNEL_CREATE
+cycle = (uint32_t)hal_get_cycles();
+if( CONFIG_DEBUG_THREAD_KERNEL_CREATE < cycle )
+printk("\n[DBG] %s : thread %x exit / new_thread %x / type %s / cycle %d\n",
+__FUNCTION__, CURRENT_THREAD, thread, thread_type_str(type), cycle );
+#endif
 
     *new_thread = thread;
@@ -589,6 +615,4 @@
 void thread_destroy( thread_t * thread )
 {
-	uint32_t     tm_start;
-	uint32_t     tm_end;
     reg_t        save_sr;
 
@@ -596,6 +620,10 @@
     core_t     * core       = thread->core;
 
-    thread_dmsg("\n[DBG] %s : enters for thread %x in process %x / type = %s\n",
-                __FUNCTION__ , thread->trdid , process->pid , thread_type_str( thread->type ) );
+#if CONFIG_DEBUG_THREAD_DESTROY
+uint32_t cycle = (uint32_t)hal_get_cycles();
+if( CONFIG_DEBUG_THREAD_DESTROY < cycle )
+printk("\n[DBG] %s : thread %x enter to destroy thread %x in process %x / cycle %d\n",
+__FUNCTION__, CURRENT_THREAD, thread, process->pid, cycle );
+#endif
 
     assert( (thread->children_nr == 0) , __FUNCTION__ , "still attached children" );
@@ -604,6 +632,4 @@
 
     assert( (thread->remote_locks == 0) , __FUNCTION__ , "all remote locks not released" );
-
-	tm_start = hal_get_cycles();
 
     // update intrumentation values
@@ -635,8 +661,10 @@
     thread_release( thread );
 
-	tm_end = hal_get_cycles();
-
-	thread_dmsg("\n[DBG] %s : exit for thread %x in process %x / duration = %d\n",
-	               __FUNCTION__, thread->trdid , process->pid , tm_end - tm_start );
+#if CONFIG_DEBUG_THREAD_DESTROY
+cycle = (uint32_t)hal_get_cycles();
+if( CONFIG_DEBUG_THREAD_DESTROY < cycle )
+printk("\n[DBG] %s : thread %x exit / destroyed thread %x in process %x / cycle %d\n",
+__FUNCTION__, CURRENT_THREAD, thread, process->pid, cycle );
+#endif
 
 }   // end thread_destroy()
@@ -779,13 +807,20 @@
     hal_fence();
 
+#if CONFIG_DEBUG_THREAD_BLOCK
+uint32_t cycle = (uint32_t)hal_get_cycles();
+if( CONFIG_DEBUG_THREAD_BLOCK < cycle )
+printk("\n[DBG] %s : thread %x blocked thread %x / cause %x / state %x / cycle %d\n",
+__FUNCTION__ , CURRENT_THREAD , thread , cause , thread->blocked , cycle );
+#endif
+
 } // end thread_block()
 
-/////////////////////////////////////////
-uint32_t thread_unblock( xptr_t   thread,
+////////////////////////////////////////////
+uint32_t thread_unblock( xptr_t   thread_xp,
                          uint32_t cause )
 {
     // get thread cluster and local pointer
-    cxy_t      cxy = GET_CXY( thread );
-    thread_t * ptr = (thread_t *)GET_PTR( thread );
+    cxy_t      cxy = GET_CXY( thread_xp );
+    thread_t * ptr = GET_PTR( thread_xp );
 
     // reset blocking cause
@@ -793,4 +828,11 @@
     hal_fence();
 
+#if CONFIG_DEBUG_THREAD_BLOCK
+uint32_t cycle = (uint32_t)hal_get_cycles();
+if( CONFIG_DEBUG_THREAD_BLOCK < cycle )
+printk("\n[DBG] %s : thread %x unblocked thread %x / cause %x / state %x / cycle %d\n",
+__FUNCTION__ , CURRENT_THREAD , ptr , cause , ptr->blocked , cycle );
+#endif
+
     // return a non zero value if the cause bit is modified 
     return( previous & cause );
@@ -805,6 +847,10 @@
     thread_t * killer = CURRENT_THREAD;
 
-thread_dmsg("\n[DBG] %s : killer thread %x enter for target thread %x\n",
-__FUNCTION__, local_cxy, killer->trdid , target->trdid );
+#if CONFIG_DEBUG_THREAD_KILL
+uint32_t cycle  = (uint32_t)hal_get_cycles;
+if( CONFIG_DEBUG_THREAD_KILL < cycle )
+printk("\n[DBG] %s : thread %x enter for target thread %x / cycle %d\n",
+__FUNCTION__, killer, target, cycle );
+#endif
 
     // set the global blocked bit in target thread descriptor.
@@ -835,6 +881,10 @@
 	hal_atomic_or( &target->flags , THREAD_FLAG_REQ_DELETE );
 
-thread_dmsg("\n[DBG] %s : killer thread %x exit for target thread %x\n",
-__FUNCTION__, local_cxy, killer->trdid , target->trdid );
+#if CONFIG_DEBUG_THREAD_KILL
+cycle  = (uint32_t)hal_get_cycles;
+if( CONFIG_DEBUG_THREAD_KILL < cycle )
+printk("\n[DBG] %s : thread %x exit for target thread %x / cycle %d\n",
+__FUNCTION__, killer, target, cycle );
+#endif
 
 }  // end thread_kill()
@@ -851,11 +901,20 @@
         {
 
-idle_dmsg("\n[DBG] %s : core[%x][%d] goes to sleep at cycle %d\n",
-__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , hal_get_cycles() );
+#if CONFIG_DEBUG_THREAD_IDLE
+uint32_t cycle  = (uint32_t)hal_get_cycles;
+thread_t * this = CURRENT_THREAD;
+if( CONFIG_DEBUG_THREAD_IDLE < cycle )
+printk("\n[DBG] %s : idle thread %x on core[%x,%d] goes to sleep / cycle %d\n",
+__FUNCTION__, this, local_cxy, this->core->lid, cycle );
+#endif
 
             hal_core_sleep();
 
-idle_dmsg("\n[DBG] %s : core[%x][%d] wake up at cycle %d\n",
-__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , hal_get_cycles() );
+#if CONFIG_DEBUG_THREAD_IDLE
+cycle  = (uint32_t)hal_get_cycles;
+if( CONFIG_DEBUG_THREAD_IDLE < cycle )
+printk("\n[DBG] %s : idle thread %x on core[%x,%d] wake up / cycle %d\n",
+__FUNCTION__, this, local_cxy, this->core->lid, cycle );
+#endif
 
         }
