source: trunk/kernel/kern/thread.c @ 639

Last change on this file since 639 was 637, checked in by alain, 5 years ago

Introduce the non-standard pthread_parallel_create() system call
and re-write the <fft> and <sort> applications to improve the
intrinsic paralelism in applications.

File size: 52.1 KB
Line 
1/*
2 * thread.c -   thread operations implementation (user & kernel)
3 *
4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *         Alain Greiner (2016,2017,2018,2019)
6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <kernel_config.h>
26#include <hal_kernel_types.h>
27#include <hal_context.h>
28#include <hal_irqmask.h>
29#include <hal_special.h>
30#include <hal_remote.h>
31#include <hal_vmm.h>
32#include <memcpy.h>
33#include <printk.h>
34#include <cluster.h>
35#include <process.h>
36#include <scheduler.h>
37#include <dev_pic.h>
38#include <core.h>
39#include <list.h>
40#include <xlist.h>
41#include <page.h>
42#include <kmem.h>
43#include <ppm.h>
44#include <thread.h>
45#include <rpc.h>
46
47//////////////////////////////////////////////////////////////////////////////////////
48// Extern global variables
49//////////////////////////////////////////////////////////////////////////////////////
50
51extern process_t            process_zero;       // allocated in kernel_init.c
52extern char               * lock_type_str[];    // allocated in kernel_init.c
53extern chdev_directory_t    chdev_dir;          // allocated in kernel_init.c
54
55//////////////////////////////////////////////////////////////////////////////////////
56// This function returns a printable string for the thread type.
57//////////////////////////////////////////////////////////////////////////////////////
58const char * thread_type_str( thread_type_t type )
59{
60  switch ( type ) {
61  case THREAD_USER:   return "USR";
62  case THREAD_RPC:    return "RPC";
63  case THREAD_DEV:    return "DEV";
64  case THREAD_IDLE:   return "IDL";
65  default:            return "undefined";
66  }
67}
68
69/////////////////////////////////////////////////////////////////////////////////////
70// This static function allocates physical memory for a thread descriptor.
71// It can be called by the three functions:
72// - thread_user_create()
73// - thread_user_fork()
74// - thread_kernel_create()
75/////////////////////////////////////////////////////////////////////////////////////
76// @ return pointer on thread descriptor if success / return NULL if failure.
77/////////////////////////////////////////////////////////////////////////////////////
78static thread_t * thread_alloc( void )
79{
80        kmem_req_t     req;    // kmem request
81
82        // allocates memory for thread descriptor + kernel stack
83        req.type  = KMEM_PPM;
84        req.order = CONFIG_THREAD_DESC_ORDER;
85        req.flags = AF_KERNEL | AF_ZERO;
86
87    return kmem_alloc( &req );
88
89}  // end thread_alloc()
90 
91
92/////////////////////////////////////////////////////////////////////////////////////
93// This static function initializes a thread descriptor (kernel or user).
94// It can be called by the four functions:
95// - thread_user_create()
96// - thread_user_fork()
97// - thread_kernel_create()
98// - thread_idle_init()
99// The "type" and "trdid" fields must have been previously set.
100// It updates the local DQDT.
101/////////////////////////////////////////////////////////////////////////////////////
102// @ thread          : pointer on local thread descriptor
103// @ process         : pointer on local process descriptor.
104// @ type            : thread type.
105// @ trdid           : thread identifier
106// @ func            : pointer on thread entry function.
107// @ args            : pointer on thread entry function arguments.
108// @ core_lid        : target core local index.
109// @ user_stack_vseg : local pointer on user stack vseg (user thread only)
110/////////////////////////////////////////////////////////////////////////////////////
111static error_t thread_init( thread_t      * thread,
112                            process_t     * process,
113                            thread_type_t   type,
114                            trdid_t         trdid,
115                            void          * func,
116                            void          * args,
117                            lid_t           core_lid,
118                            vseg_t        * user_stack_vseg )
119{
120
121// check type and trdid fields are initialized
122assert( (thread->type == type)   , "bad type argument" );
123assert( (thread->trdid == trdid) , "bad trdid argument" );
124
125#if DEBUG_THREAD_INIT
126uint32_t   cycle = (uint32_t)hal_get_cycles();
127thread_t * this  = CURRENT_THREAD;
128if( DEBUG_THREAD_INIT < cycle )
129printk("\n[%s] thread[%x,%x] enter for thread[%x,%x] / cycle %d\n",
130__FUNCTION__, this->process->pid, this->trdid, process->pid, thread->trdid, cycle );
131#endif
132
133    // compute thread descriptor size without kernel stack
134    uint32_t desc_size = (intptr_t)(&thread->signature) - (intptr_t)thread + 4; 
135
136        // Initialize new thread descriptor
137    thread->quantum         = 0;            // TODO
138    thread->ticks_nr        = 0;            // TODO
139    thread->time_last_check = 0;            // TODO
140        thread->core            = &LOCAL_CLUSTER->core_tbl[core_lid];
141        thread->process         = process;
142    thread->busylocks       = 0;
143
144#if DEBUG_BUSYLOCK
145    xlist_root_init( XPTR( local_cxy , &thread->busylocks_root ) );
146#endif
147
148    thread->user_stack_vseg = user_stack_vseg;
149    thread->k_stack_base    = (intptr_t)thread + desc_size;
150    thread->k_stack_size    = CONFIG_THREAD_DESC_SIZE - desc_size;
151    thread->entry_func      = func;         // thread entry point
152    thread->entry_args      = args;         // thread function arguments
153    thread->flags           = 0;            // all flags reset
154    thread->errno           = 0;            // no error detected
155    thread->fork_user       = 0;            // no user defined placement for fork
156    thread->fork_cxy        = 0;            // user defined target cluster for fork
157    thread->blocked         = THREAD_BLOCKED_GLOBAL;
158
159    // initialize sched list
160    list_entry_init( &thread->sched_list );
161
162    // initialize waiting queue entries
163    list_entry_init( &thread->wait_list );
164    xlist_entry_init( XPTR( local_cxy , &thread->wait_xlist ) );
165
166    // initialize thread info
167    memset( &thread->info , 0 , sizeof(thread_info_t) );
168
169    // initialize join_lock
170    remote_busylock_init( XPTR( local_cxy , &thread->join_lock ), LOCK_THREAD_JOIN );
171
172    // initialise signature
173        thread->signature = THREAD_SIGNATURE;
174
175    // FIXME define and call an architecture specific hal_thread_init()
176    // function to initialise the save_sr field
177    thread->save_sr = 0xFF13;
178
179    // register new thread in core scheduler
180    sched_register_thread( thread->core , thread );
181
182        // update DQDT
183    dqdt_increment_threads();
184
185#if DEBUG_THREAD_INIT
186cycle = (uint32_t)hal_get_cycles();
187if( DEBUG_THREAD_INIT < cycle )
188printk("\n[%s] thread[%x,%x] exit for thread[%x,%x] / cycle %d\n",
189__FUNCTION__, this->process->pid, this->trdid, process->pid, thread->trdid, cycle );
190#endif
191
192        return 0;
193
194} // end thread_init()
195
196//////////////////////////////////////////////////
197error_t thread_user_create( pid_t             pid,
198                            void            * start_func,
199                            void            * start_arg,
200                            pthread_attr_t  * attr,
201                            thread_t       ** new_thread )
202{
203    error_t        error;
204        thread_t     * thread;       // pointer on created thread descriptor
205    trdid_t        trdid;        // created thred identifier
206    process_t    * process;      // pointer to local process descriptor
207    lid_t          core_lid;     // selected core local index
208    vseg_t       * us_vseg;      // user stack vseg
209
210assert( (attr != NULL) , "pthread attributes must be defined" );
211
212#if DEBUG_THREAD_USER_CREATE
213thread_t * this  = CURRENT_THREAD;
214uint32_t   cycle = (uint32_t)hal_get_cycles();
215if( DEBUG_THREAD_USER_CREATE < cycle )
216printk("\n[%s] thread[%x,%x] enter in cluster %x for process %x / cycle %d\n",
217__FUNCTION__, this->process->pid , this->trdid , local_cxy , pid , cycle );
218#endif
219
220    // get process descriptor local copy
221    process = process_get_local_copy( pid );
222
223    if( process == NULL )
224    {
225                printk("\n[ERROR] in %s : cannot get process descriptor %x\n",
226        __FUNCTION__ , pid );
227        return -1;
228    }
229
230#if( DEBUG_THREAD_USER_CREATE & 1)
231if( DEBUG_THREAD_USER_CREATE < cycle )
232printk("\n[%s] process descriptor = %x for process %x in cluster %x\n",
233__FUNCTION__, process , pid , local_cxy );
234#endif
235
236    // select a target core in local cluster
237    if( attr->attributes & PT_ATTR_CORE_DEFINED )
238    {
239        core_lid = attr->lid;
240        if( core_lid >= LOCAL_CLUSTER->cores_nr )
241        {
242                printk("\n[ERROR] in %s : illegal core index attribute = %d\n",
243            __FUNCTION__ , core_lid );
244            return -1;
245        }
246    }
247    else
248    {
249        core_lid = cluster_select_local_core( local_cxy );
250    }
251
252#if( DEBUG_THREAD_USER_CREATE & 1)
253if( DEBUG_THREAD_USER_CREATE < cycle )
254printk("\n[%s] core[%x,%d] selected\n",
255__FUNCTION__, local_cxy , core_lid );
256#endif
257
258    // allocate memory for thread descriptor
259    thread = thread_alloc();
260
261    if( thread == NULL )
262    {
263            printk("\n[ERROR] in %s : cannot create new thread in cluster %x\n",
264        __FUNCTION__, local_cxy );
265        return -1;
266    }
267
268#if( DEBUG_THREAD_USER_CREATE & 1)
269if( DEBUG_THREAD_USER_CREATE < cycle )
270printk("\n[%s] new thread descriptor %x allocated\n",
271__FUNCTION__, thread );
272#endif
273
274    // set type in thread descriptor
275    thread->type = THREAD_USER;
276
277    // register new thread in process descriptor, and get a TRDID
278    error = process_register_thread( process, thread , &trdid );
279
280    if( error )
281    {
282        printk("\n[ERROR] in %s : cannot register new thread in process %x\n",
283        __FUNCTION__, pid );
284        thread_destroy( thread );
285        return -1;
286    }
287
288    // set trdid in thread descriptor
289    thread->trdid = trdid;
290
291#if( DEBUG_THREAD_USER_CREATE & 1)
292if( DEBUG_THREAD_USER_CREATE < cycle )
293printk("\n[%s] new thread %x registered in process %x\n",
294__FUNCTION__, trdid, pid );
295#endif
296
297    // allocate a stack from local VMM
298    us_vseg = vmm_create_vseg( process,
299                               VSEG_TYPE_STACK,
300                               LTID_FROM_TRDID( trdid ),
301                               0,                         // size unused
302                               0,                         // file_offset unused
303                               0,                         // file_size unused
304                               XPTR_NULL,                 // mapper_xp unused
305                               local_cxy );
306
307    if( us_vseg == NULL )
308    {
309            printk("\n[ERROR] in %s : cannot create stack vseg\n", __FUNCTION__ );
310        process_remove_thread( thread );
311        thread_destroy( thread );
312                return -1;
313    }
314
315#if( DEBUG_THREAD_USER_CREATE & 1)
316if( DEBUG_THREAD_USER_CREATE < cycle )
317printk("\n[%s] stack vseg created / vpn_base %x / %d pages\n",
318__FUNCTION__, us_vseg->vpn_base, us_vseg->vpn_size );
319#endif
320
321    // initialize thread descriptor
322    error = thread_init( thread,
323                         process,
324                         THREAD_USER,
325                         trdid,
326                         start_func,
327                         start_arg,
328                         core_lid,
329                         us_vseg );
330    if( error )
331    {
332            printk("\n[ERROR] in %s : cannot initialize new thread\n", __FUNCTION__ );
333        vmm_remove_vseg( process , us_vseg );
334        process_remove_thread( thread );
335        thread_destroy( thread );
336        return -1;
337    }
338
339#if( DEBUG_THREAD_USER_CREATE & 1)
340if( DEBUG_THREAD_USER_CREATE < cycle )
341printk("\n[%s] new thread %x in process %x initialised\n",
342__FUNCTION__, thread->trdid, process->pid );
343#endif
344
345    // set DETACHED flag if required
346    if( attr->attributes & PT_ATTR_DETACH ) 
347    {
348        thread->flags |= THREAD_FLAG_DETACHED;
349    }
350
351    // allocate & initialize CPU context
352        if( hal_cpu_context_alloc( thread ) )
353    {
354            printk("\n[ERROR] in %s : cannot create CPU context\n", __FUNCTION__ );
355        vmm_remove_vseg( process , us_vseg );
356        process_remove_thread( thread );
357        thread_destroy( thread );
358        return -1;
359    }
360    hal_cpu_context_init( thread );
361
362    // allocate & initialize FPU context
363    if( hal_fpu_context_alloc( thread ) )
364    {
365            printk("\n[ERROR] in %s : cannot create FPU context\n", __FUNCTION__ );
366        vmm_remove_vseg( process , us_vseg );
367        process_remove_thread( thread );
368        thread_destroy( thread );
369        return -1;
370    }
371    hal_fpu_context_init( thread );
372
373#if( DEBUG_THREAD_USER_CREATE & 1)
374if( DEBUG_THREAD_USER_CREATE < cycle )
375printk("\n[%s] CPU & FPU contexts created\n",
376__FUNCTION__, thread->trdid );
377hal_vmm_display( XPTR( local_cxy , process ) , true );
378#endif
379
380#if DEBUG_THREAD_USER_CREATE
381cycle = (uint32_t)hal_get_cycles();
382if( DEBUG_THREAD_USER_CREATE < cycle )
383printk("\n[%s] thread[%x,%x] exit / new_thread %x / core %d / cycle %d\n",
384__FUNCTION__, this->process->pid , this->trdid , thread->trdid, core_lid, cycle );
385#endif
386
387    *new_thread = thread;
388        return 0;
389
390}  // end thread_user_create()
391
392///////////////////////////////////////////////////////
393error_t thread_user_fork( xptr_t      parent_thread_xp,
394                          process_t * child_process,
395                          thread_t ** child_thread )
396{
397    error_t        error;
398        thread_t     * child_ptr;        // local pointer on child thread
399    trdid_t        child_trdid;      // child thread identifier
400    lid_t          core_lid;         // selected core local index
401    thread_t     * parent_ptr;       // local pointer on remote parent thread
402    cxy_t          parent_cxy;       // parent thread cluster
403    process_t    * parent_process;   // local pointer on parent process
404    xptr_t         parent_gpt_xp;    // extended pointer on parent thread GPT
405    void         * parent_func;      // parent thread entry_func
406    void         * parent_args;      // parent thread entry_args
407    uint32_t       parent_flags;     // parent_thread flags
408    vseg_t       * parent_us_vseg;   // parent thread user stack vseg
409    vseg_t       * child_us_vseg;    // child thread user stack vseg
410
411#if DEBUG_THREAD_USER_FORK
412uint32_t   cycle = (uint32_t)hal_get_cycles();
413thread_t * this  = CURRENT_THREAD;
414if( DEBUG_THREAD_USER_FORK < cycle )
415printk("\n[%s] thread[%x,%x] enter for child_process %x / cycle %d\n",
416__FUNCTION__, this->process->pid, this->trdid, child_process->pid, cycle );
417#endif
418
419    // select a target core in local cluster
420    core_lid = cluster_select_local_core( local_cxy );
421
422#if (DEBUG_THREAD_USER_FORK & 1)
423if( DEBUG_THREAD_USER_FORK < cycle )
424printk("\n[%s] thread[%x,%x] selected core [%x,%d]\n",
425__FUNCTION__, this->process->pid, this->trdid, local_cxy, core_lid );
426#endif
427
428    // get cluster and local pointer on parent thread descriptor
429    parent_cxy = GET_CXY( parent_thread_xp );
430    parent_ptr = GET_PTR( parent_thread_xp );
431
432    // get relevant infos from parent thread
433    parent_func    = (void *)  hal_remote_lpt( XPTR(parent_cxy,&parent_ptr->entry_func ));
434    parent_args    = (void *)  hal_remote_lpt( XPTR(parent_cxy,&parent_ptr->entry_args ));
435    parent_flags   = (uint32_t)hal_remote_l32( XPTR(parent_cxy,&parent_ptr->flags ));
436    parent_us_vseg = (vseg_t *)hal_remote_lpt( XPTR(parent_cxy,&parent_ptr->user_stack_vseg ));
437
438    // get pointer on parent process in parent thread cluster
439    parent_process = (process_t *)hal_remote_lpt( XPTR( parent_cxy,
440                                                        &parent_ptr->process ) );
441 
442    // build extended pointer on parent GPT in parent thread cluster
443    parent_gpt_xp = XPTR( parent_cxy , &parent_process->vmm.gpt );
444
445#if (DEBUG_THREAD_USER_FORK & 1)
446if( DEBUG_THREAD_USER_FORK < cycle )
447printk("\n[%s] thread[%x,%x] get parent GPT\n",
448__FUNCTION__, this->process->pid, this->trdid );
449#endif
450
451    // allocate memory for child thread descriptor
452    child_ptr = thread_alloc();
453
454    if( child_ptr == NULL )
455    {
456        printk("\n[ERROR] in %s : cannot allocate new thread\n",
457        __FUNCTION__ );
458        return -1;
459    }
460
461#if (DEBUG_THREAD_USER_FORK & 1)
462if( DEBUG_THREAD_USER_FORK < cycle )
463printk("\n[%s] thread[%x,%x] allocated new thread descriptor %x\n",
464__FUNCTION__, this->process->pid, this->trdid, child_ptr );
465#endif
466
467    // set type in thread descriptor
468    child_ptr->type = THREAD_USER;
469
470    // register new thread in process descriptor, and get a TRDID
471    error = process_register_thread( child_process, child_ptr , &child_trdid );
472
473    if( error )
474    {
475        printk("\n[ERROR] in %s : cannot register new thread in process %x\n",
476        __FUNCTION__, child_process->pid );
477        thread_destroy( child_ptr );
478        return -1;
479    }
480
481    // set trdid in thread descriptor
482    child_ptr->trdid = child_trdid;
483
484#if (DEBUG_THREAD_USER_FORK & 1)
485if( DEBUG_THREAD_USER_FORK < cycle )
486printk("\n[%s] thread[%x,%x] registered child thread %x in child process %x\n",
487__FUNCTION__, this->process->pid, this->trdid, child_trdid, child_process->pid );
488#endif
489
490    // get an user stack vseg from local VMM allocator
491    child_us_vseg = vmm_create_vseg( child_process,
492                                     VSEG_TYPE_STACK,
493                                     LTID_FROM_TRDID( child_trdid ), 
494                                     0,                               // size unused
495                                     0,                               // file_offset unused
496                                     0,                               // file_size unused
497                                     XPTR_NULL,                       // mapper_xp unused
498                                     local_cxy );
499    if( child_us_vseg == NULL )
500    {
501            printk("\n[ERROR] in %s : cannot create stack vseg\n", __FUNCTION__ );
502        process_remove_thread( child_ptr );
503        thread_destroy( child_ptr );
504        return -1;
505    }
506
507#if (DEBUG_THREAD_USER_FORK & 1)
508if( DEBUG_THREAD_USER_FORK < cycle )
509printk("\n[%s] thread[%x,%x] created an user stack vseg / vpn_base %x / %d pages\n",
510__FUNCTION__, this->process->pid, this->trdid,
511child_us_vseg->vpn_base, child_us_vseg->vpn_size );
512#endif
513
514    // initialize thread descriptor
515    error = thread_init( child_ptr,
516                         child_process,
517                         THREAD_USER,
518                         child_trdid,
519                         parent_func,
520                         parent_args,
521                         core_lid,
522                         child_us_vseg );
523    if( error )
524    {
525            printk("\n[ERROR] in %s : cannot initialize child thread\n", __FUNCTION__ );
526        vmm_remove_vseg( child_process , child_us_vseg ); 
527        process_remove_thread( child_ptr );
528        thread_destroy( child_ptr );
529        return -1;
530    }
531
532#if (DEBUG_THREAD_USER_FORK & 1)
533if( DEBUG_THREAD_USER_FORK < cycle )
534printk("\n[%s] thread[%x,%x] initialised thread %x in process %x\n",
535__FUNCTION__, this->process->pid, this->trdid, child_ptr->trdid, child_process->pid );
536#endif
537
538    // set detached flag if required
539    if( parent_flags & THREAD_FLAG_DETACHED ) child_ptr->flags = THREAD_FLAG_DETACHED;
540
541    // allocate a CPU context for child thread
542        if( hal_cpu_context_alloc( child_ptr ) )
543    {
544            printk("\n[ERROR] in %s : cannot allocate CPU context\n", __FUNCTION__ );
545        vmm_remove_vseg( child_process , child_us_vseg );
546        process_remove_thread( child_ptr );
547        thread_destroy( child_ptr );
548        return -1;
549    }
550
551    // allocate a FPU context for child thread
552        if( hal_fpu_context_alloc( child_ptr ) )
553    {
554            printk("\n[ERROR] in %s : cannot allocate FPU context\n", __FUNCTION__ );
555        vmm_remove_vseg( child_process , child_us_vseg );
556        process_remove_thread( child_ptr );
557        thread_destroy( child_ptr );
558        return -1;
559    }
560
561#if (DEBUG_THREAD_USER_FORK & 1)
562if( DEBUG_THREAD_USER_FORK < cycle )
563printk("\n[%s] thread[%x,%x] created CPU & FPU contexts for thread %x in process %x\n",
564__FUNCTION__, this->process->pid, this->trdid, child_ptr->trdid, child_process->pid );
565#endif
566
567    // scan parent GPT, and copy all valid entries
568    // associated to user stack vseg into child GPT
569    vpn_t  parent_vpn;
570    vpn_t  child_vpn;
571    bool_t mapped;
572    ppn_t  ppn;
573    vpn_t  parent_vpn_base = hal_remote_l32( XPTR( parent_cxy, &parent_us_vseg->vpn_base ) );
574    vpn_t  parent_vpn_size = hal_remote_l32( XPTR( parent_cxy, &parent_us_vseg->vpn_size ) );
575    vpn_t  child_vpn_base  = child_us_vseg->vpn_base;
576
577    for( parent_vpn = parent_vpn_base , child_vpn = child_vpn_base ; 
578         parent_vpn < (parent_vpn_base + parent_vpn_size) ;
579         parent_vpn++ , child_vpn++ )
580    {
581        error = hal_gpt_pte_copy( &child_process->vmm.gpt,
582                                  child_vpn,
583                                  parent_gpt_xp,
584                                  parent_vpn,
585                                  true,                 // set cow
586                                  &ppn,
587                                  &mapped );
588        if( error )
589        {
590            printk("\n[ERROR] in %s : cannot update child GPT\n", __FUNCTION__ );
591            vmm_remove_vseg( child_process , child_us_vseg );
592            process_remove_thread( child_ptr );
593            thread_destroy( child_ptr );
594            return -1;
595        }
596
597        // increment pending forks counter for a mapped page
598        if( mapped )
599        {
600            // get pointers on the page descriptor
601            xptr_t   page_xp  = ppm_ppn2page( ppn );
602            cxy_t    page_cxy = GET_CXY( page_xp );
603            page_t * page_ptr = GET_PTR( page_xp );
604
605            // build extended pointers on forks and lock fields
606            xptr_t forks_xp = XPTR( page_cxy , &page_ptr->forks );
607            xptr_t lock_xp  = XPTR( page_cxy , &page_ptr->lock );
608
609            // get lock protecting page
610            remote_busylock_acquire( lock_xp ); 
611
612            // increment the forks counter in page descriptor
613            hal_remote_atomic_add( forks_xp , 1 );
614
615            // release lock protecting page
616            remote_busylock_release( lock_xp ); 
617        }
618    }
619
620#if (DEBUG_THREAD_USER_FORK & 1)
621if( DEBUG_THREAD_USER_FORK < cycle )
622printk("\n[%s] thread[%x,%x] copied STACK vseg PTEs & set COW in child GPT\n",
623__FUNCTION__, this->process->pid, this->trdid );
624#endif
625
626    // set COW flag for all mapped entries of user stack vseg in parent GPT
627    hal_gpt_set_cow( parent_gpt_xp,
628                     parent_vpn_base,
629                     parent_vpn_size );
630
631#if (DEBUG_THREAD_USER_FORK & 1)
632if( DEBUG_THREAD_USER_FORK < cycle )
633printk("\n[%s] thread[%x,%x] set COW for STACK vseg in parent GPT\n",
634__FUNCTION__, this->process->pid, this->trdid );
635#endif
636
637    // return child pointer
638    *child_thread = child_ptr;
639
640#if DEBUG_THREAD_USER_FORK
641cycle = (uint32_t)hal_get_cycles();
642if( DEBUG_THREAD_USER_FORK < cycle )
643printk("\n[%s] thread[%x,%x] exit / created thread[%x,%x] / cycle %d\n",
644__FUNCTION__, this->process->pid, this->trdid,
645child_ptr->process->pid, child_ptr->trdid, cycle );
646#endif
647
648        return 0;
649
650}  // end thread_user_fork()
651
652////////////////////////////////////////////////
653error_t thread_user_exec( void     * entry_func,
654                          uint32_t   argc,
655                          char    ** argv )
656{
657    thread_t  * thread  = CURRENT_THREAD;
658    process_t * process = thread->process;
659
660#if DEBUG_THREAD_USER_EXEC
661uint32_t cycle = (uint32_t)hal_get_cycles();
662if( DEBUG_THREAD_USER_EXEC < cycle )
663printk("\n[%s] thread[%x,%x] enter / cycle %d\n",
664__FUNCTION__, process->pid, thread->trdid, cycle );
665#endif
666
667// check parent thread attributes
668assert( (thread->type == THREAD_USER )          , "bad type" );
669assert( (thread->signature == THREAD_SIGNATURE) , "bad signature" );
670assert( (thread->busylocks == 0)                , "bad busylocks" );
671
672        // re-initialize various thread descriptor fields
673    thread->quantum         = 0;            // TODO
674    thread->ticks_nr        = 0;            // TODO
675    thread->time_last_check = 0;            // TODO
676
677    thread->entry_func      = entry_func;
678    thread->main_argc       = argc; 
679    thread->main_argv       = argv;
680
681    // the main thread is always detached
682    thread->flags           = THREAD_FLAG_DETACHED;
683    thread->blocked         = 0;
684    thread->errno           = 0;
685    thread->fork_user       = 0;    // not inherited
686    thread->fork_cxy        = 0;    // not inherited
687
688    // re-initialize busylocks counters
689    thread->busylocks       = 0;
690
691    // reset thread info
692    memset( &thread->info , 0 , sizeof(thread_info_t) );
693
694    // re-initialize join_lock
695    remote_busylock_init( XPTR( local_cxy , &thread->join_lock ), LOCK_THREAD_JOIN );
696
697    // allocate an user stack vseg for main thread
698    vseg_t * us_vseg = vmm_create_vseg( process,
699                                        VSEG_TYPE_STACK,
700                                        LTID_FROM_TRDID( thread->trdid ),
701                                        0,                 // length unused
702                                        0,                 // file_offset unused
703                                        0,                 // file_size unused
704                                        XPTR_NULL,         // mapper_xp unused
705                                        local_cxy );
706    if( us_vseg == NULL )
707    {
708            printk("\n[ERROR] in %s : cannot create stack vseg for main thread\n", __FUNCTION__ );
709                return -1;
710    }
711
712    // update user stack in thread descriptor
713    thread->user_stack_vseg = us_vseg;
714   
715    // release FPU ownership if required
716    if( thread->core->fpu_owner == thread ) thread->core->fpu_owner = NULL;
717
718    // re-initialize  FPU context
719    hal_fpu_context_init( thread );
720
721#if DEBUG_THREAD_USER_EXEC
722cycle = (uint32_t)hal_get_cycles();
723if( DEBUG_THREAD_USER_EXEC < cycle )
724printk("\n[%s] thread[%x,%x] set CPU context & jump to user code / cycle %d\n",
725__FUNCTION__, process->pid, thread->trdid, cycle );
726hal_vmm_display( XPTR( local_cxy , process ) , true );
727#endif
728
729    // re-initialize CPU context... and jump to user code
730        hal_cpu_context_exec( thread );
731
732    assert( false, "we should not execute this code");
733 
734    return 0;
735
736}  // end thread_user_exec()
737
738/////////////////////////////////////////////////////////
739error_t thread_kernel_create( thread_t     ** new_thread,
740                              thread_type_t   type,
741                              void          * func,
742                              void          * args,
743                                              lid_t           core_lid )
744{
745    error_t        error;
746        thread_t     * thread;       // pointer on new thread descriptor
747    trdid_t        trdid;        // new thread identifier
748
749    thread_t * this = CURRENT_THREAD; 
750
751assert( ( (type == THREAD_IDLE) || (type == THREAD_RPC) || (type == THREAD_DEV) ) ,
752"illegal thread type" );
753
754assert( (core_lid < LOCAL_CLUSTER->cores_nr) ,
755"illegal core_lid" );
756
757#if DEBUG_THREAD_KERNEL_CREATE
758uint32_t   cycle = (uint32_t)hal_get_cycles();
759if( DEBUG_THREAD_KERNEL_CREATE < cycle )
760printk("\n[%s] thread[%x,%x] enter / requested_type %s / cycle %d\n",
761__FUNCTION__, this->process->pid, this->trdid, thread_type_str(type), cycle );
762#endif
763
764    // allocate memory for new thread descriptor
765    thread = thread_alloc();
766
767    if( thread == NULL )
768    {
769        printk("\n[ERROR] in %s : thread %x in process %x\n"
770        "   no memory for thread descriptor\n",
771        __FUNCTION__, this->trdid, this->process->pid );
772        return ENOMEM;
773    }
774
775    // set type in thread descriptor
776    thread->type = type;
777
778    // register new thread in local kernel process descriptor, and get a TRDID
779    error = process_register_thread( &process_zero , thread , &trdid );
780
781    if( error )
782    {
783        printk("\n[ERROR] in %s : cannot register thread in kernel process\n", __FUNCTION__ );
784        return -1;
785    }
786
787    // set trdid in thread descriptor
788    thread->trdid = trdid;
789
790    // initialize thread descriptor
791    error = thread_init( thread,
792                         &process_zero,
793                         type,
794                         trdid,
795                         func,
796                         args,
797                         core_lid,
798                         NULL );  // no user stack for a kernel thread
799
800    if( error ) // release allocated memory for thread descriptor
801    {
802        printk("\n[ERROR] in %s : cannot initialize thread descriptor\n", __FUNCTION__ );
803        thread_destroy( thread );
804        return ENOMEM;
805    }
806
807    // allocate & initialize CPU context
808        error = hal_cpu_context_alloc( thread );
809
810    if( error )
811    {
812        printk("\n[ERROR] in %s : thread %x in process %x\n"
813        "    cannot create CPU context\n",
814        __FUNCTION__, this->trdid, this->process->pid );
815        thread_destroy( thread );
816        return EINVAL;
817    }
818
819    hal_cpu_context_init( thread );
820
821    // set THREAD_BLOCKED_IDLE for DEV threads
822    if( type == THREAD_DEV ) thread->blocked |= THREAD_BLOCKED_IDLE;
823
824#if DEBUG_THREAD_KERNEL_CREATE
825cycle = (uint32_t)hal_get_cycles();
826if( DEBUG_THREAD_KERNEL_CREATE < cycle )
827printk("\n[%s] thread[%x,%x] exit / new_thread %x / type %s / cycle %d\n",
828__FUNCTION__, this->process->pid, this->trdid, thread, thread_type_str(type), cycle );
829#endif
830
831    *new_thread = thread;
832        return 0;
833
834} // end thread_kernel_create()
835
836//////////////////////////////////////////////
837void thread_idle_init( thread_t      * thread,
838                       thread_type_t   type,
839                       void          * func,
840                       void          * args,
841                           lid_t           core_lid )
842{
843    trdid_t trdid;   
844    error_t error;
845
846// check arguments
847assert( (type == THREAD_IDLE) , "illegal thread type" );
848assert( (core_lid < LOCAL_CLUSTER->cores_nr) , "illegal core index" );
849
850    // set type in thread descriptor
851    thread->type = THREAD_IDLE;
852
853    // register idle thread in local kernel process descriptor, and get a TRDID
854    error = process_register_thread( &process_zero , thread , &trdid );
855
856assert( (error == 0), "cannot register idle_thread in kernel process" );
857
858    // set trdid in thread descriptor
859    thread->trdid = trdid;
860
861    // initialize thread descriptor
862    error = thread_init( thread,
863                         &process_zero,
864                         THREAD_IDLE,
865                         trdid,
866                         func,
867                         args,
868                         core_lid,
869                         NULL );   // no user stack for a kernel thread
870
871assert( (error == 0), "cannot initialize idle_thread" );
872
873    // allocate & initialize CPU context if success
874    error = hal_cpu_context_alloc( thread );
875
876assert( (error == 0), "cannot allocate CPU context" );
877
878    hal_cpu_context_init( thread );
879
880}  // end thread_idle_init()
881
882////////////////////////////////////////////
883uint32_t thread_destroy( thread_t * thread )
884{
885    reg_t           save_sr;
886    uint32_t        count;
887
888    thread_type_t   type    = thread->type;
889    process_t     * process = thread->process;
890    core_t        * core    = thread->core;
891
892#if DEBUG_THREAD_DESTROY
893uint32_t   cycle = (uint32_t)hal_get_cycles();
894thread_t * this  = CURRENT_THREAD;
895if( DEBUG_THREAD_DESTROY < cycle )
896printk("\n[%s] thread[%x,%x] enter to destroy thread[%x,%x] / cycle %d\n",
897__FUNCTION__, this->process->pid, this->trdid, process->pid, thread->trdid, cycle );
898#endif
899
900    // check calling thread busylocks counter
901    thread_assert_can_yield( thread , __FUNCTION__ );
902
903#if CONFIG_INSTRUMENTATION_PGFAULTS
904        process->vmm.false_pgfault_nr    += thread->info.false_pgfault_nr;
905        process->vmm.local_pgfault_nr    += thread->info.local_pgfault_nr;
906        process->vmm.global_pgfault_nr   += thread->info.global_pgfault_nr;
907        process->vmm.false_pgfault_cost  += thread->info.false_pgfault_cost;
908        process->vmm.local_pgfault_cost  += thread->info.local_pgfault_cost;
909        process->vmm.global_pgfault_cost += thread->info.global_pgfault_cost;
910#endif
911
912    // remove thread from process th_tbl[]
913    count = process_remove_thread( thread );
914
915    // release memory allocated for CPU context and FPU context
916        hal_cpu_context_destroy( thread );
917        hal_fpu_context_destroy( thread );
918       
919    // release user stack vseg (for an user thread only)
920    if( type == THREAD_USER )  vmm_remove_vseg( process , thread->user_stack_vseg );
921
922    // release FPU ownership if required
923        hal_disable_irq( &save_sr );
924        if( core->fpu_owner == thread )
925        {
926                core->fpu_owner = NULL;
927                hal_fpu_disable();
928        }
929        hal_restore_irq( save_sr );
930
931    // invalidate thread descriptor
932        thread->signature = 0;
933
934    // release memory for thread descriptor (including kernel stack)
935    kmem_req_t   req;
936    req.type  = KMEM_PPM;
937    req.ptr   = thread;
938    kmem_free( &req );
939
940#if DEBUG_THREAD_DESTROY
941cycle = (uint32_t)hal_get_cycles();
942if( DEBUG_THREAD_DESTROY < cycle )
943printk("\n[%s] thread[%x,%x] exit / destroyed thread[%x,%x] / cycle %d\n",
944__FUNCTION__, this->process->pid, this->trdid, process->pid, thread->trdid, cycle );
945#endif
946
947    return count;
948
949}   // end thread_destroy()
950
951//////////////////////////////////////////////////
952inline void thread_set_req_ack( thread_t * target,
953                                uint32_t * rsp_count )
954{
955    reg_t    save_sr;   // for critical section
956
957    // get pointer on target thread scheduler
958    scheduler_t * sched = &target->core->scheduler;
959
960    // wait scheduler ready to handle a new request
961    while( sched->req_ack_pending ) asm volatile( "nop" );
962   
963    // enter critical section
964    hal_disable_irq( &save_sr );
965     
966    // set request in target thread scheduler
967    sched->req_ack_pending = true;
968
969    // set ack request in target thread "flags"
970    hal_atomic_or( &target->flags , THREAD_FLAG_REQ_ACK );
971
972    // set pointer on responses counter in target thread
973    target->ack_rsp_count = rsp_count;
974   
975    // exit critical section
976    hal_restore_irq( save_sr );
977
978    hal_fence();
979
980}  // thread_set_req_ack()
981
982/////////////////////////////////////////////////////
983inline void thread_reset_req_ack( thread_t * target )
984{
985    reg_t    save_sr;   // for critical section
986
987    // get pointer on target thread scheduler
988    scheduler_t * sched = &target->core->scheduler;
989
990    // check signal pending in scheduler
991    assert( sched->req_ack_pending , "no pending signal" );
992   
993    // enter critical section
994    hal_disable_irq( &save_sr );
995     
996    // reset signal in scheduler
997    sched->req_ack_pending = false;
998
999    // reset signal in thread "flags"
1000    hal_atomic_and( &target->flags , ~THREAD_FLAG_REQ_ACK );
1001
1002    // reset pointer on responses counter
1003    target->ack_rsp_count = NULL;
1004   
1005    // exit critical section
1006    hal_restore_irq( save_sr );
1007
1008    hal_fence();
1009
1010}  // thread_reset_req_ack()
1011
1012//////////////////////////////////////
1013void thread_block( xptr_t   thread_xp,
1014                   uint32_t cause )
1015{
1016    // get thread cluster and local pointer
1017    cxy_t      cxy = GET_CXY( thread_xp );
1018    thread_t * ptr = GET_PTR( thread_xp );
1019
1020    // set blocking cause
1021    hal_remote_atomic_or( XPTR( cxy , &ptr->blocked ) , cause );
1022    hal_fence();
1023
1024#if DEBUG_THREAD_BLOCK
1025uint32_t    cycle   = (uint32_t)hal_get_cycles();
1026process_t * process = hal_remote_lpt( XPTR( cxy , &ptr->process ) );
1027thread_t  * this    = CURRENT_THREAD;
1028if( DEBUG_THREAD_BLOCK < cycle )
1029printk("\n[%s] thread[%x,%x] blocked thread %x in process %x / cause %x\n",
1030__FUNCTION__, this->process->pid, this->trdid,
1031ptr->trdid, hal_remote_l32(XPTR( cxy , &process->pid )), cause );
1032#endif
1033
1034} // end thread_block()
1035
1036////////////////////////////////////////////
1037uint32_t thread_unblock( xptr_t   thread_xp,
1038                         uint32_t cause )
1039{
1040    // get thread cluster and local pointer
1041    cxy_t      cxy = GET_CXY( thread_xp );
1042    thread_t * ptr = GET_PTR( thread_xp );
1043
1044    // reset blocking cause
1045    uint32_t previous = hal_remote_atomic_and( XPTR( cxy , &ptr->blocked ) , ~cause );
1046    hal_fence();
1047
1048#if DEBUG_THREAD_BLOCK
1049uint32_t    cycle   = (uint32_t)hal_get_cycles();
1050process_t * process = hal_remote_lpt( XPTR( cxy , &ptr->process ) );
1051thread_t  * this    = CURRENT_THREAD;
1052if( DEBUG_THREAD_BLOCK < cycle )
1053printk("\n[%s] thread[%x,%x] unblocked thread %x in process %x / cause %x\n",
1054__FUNCTION__, this->process->pid, this->trdid,
1055ptr->trdid, hal_remote_l32(XPTR( cxy , &process->pid )), cause );
1056#endif
1057
1058    // return a non zero value if the cause bit is modified
1059    return( previous & cause );
1060
1061}  // end thread_unblock()
1062
1063//////////////////////////////////////
1064void thread_delete( xptr_t  target_xp,
1065                    pid_t   pid,
1066                    bool_t  is_forced )
1067{
1068    reg_t       save_sr;                // for critical section
1069    bool_t      target_join_done;       // joining thread arrived first
1070    bool_t      target_attached;        // target thread attached
1071    xptr_t      killer_xp;              // extended pointer on killer thread (this)
1072    thread_t  * killer_ptr;             // pointer on killer thread (this)
1073    cxy_t       target_cxy;             // target thread cluster     
1074    thread_t  * target_ptr;             // pointer on target thread
1075    process_t * target_process;         // pointer on arget process
1076    pid_t       target_pid;             // target process identifier
1077    xptr_t      target_flags_xp;        // extended pointer on target thread <flags>
1078    xptr_t      target_join_lock_xp;    // extended pointer on target thread <join_lock>
1079    xptr_t      target_join_xp_xp;      // extended pointer on target thread <join_xp>
1080    trdid_t     target_trdid;           // target thread identifier
1081    ltid_t      target_ltid;            // target thread local index
1082    xptr_t      joining_xp;             // extended pointer on joining thread
1083
1084    // get target thread cluster and local pointer
1085    target_cxy      = GET_CXY( target_xp );
1086    target_ptr      = GET_PTR( target_xp );
1087
1088    // get target thread identifier, attached flag, and process PID
1089    target_trdid    = hal_remote_l32( XPTR( target_cxy , &target_ptr->trdid ) );
1090    target_ltid     = LTID_FROM_TRDID( target_trdid );
1091    target_flags_xp = XPTR( target_cxy , &target_ptr->flags ); 
1092    target_attached = ( (hal_remote_l32( target_flags_xp ) & THREAD_FLAG_DETACHED) == 0 );
1093    target_process  = hal_remote_lpt( XPTR( target_cxy , &target_ptr->process ) );
1094    target_pid      = hal_remote_l32( XPTR( target_cxy , &target_process->pid ) );
1095
1096// check target PID
1097assert( (pid == target_pid),
1098"unconsistent pid and target_xp arguments" );
1099
1100    // get killer thread pointers
1101    killer_ptr = CURRENT_THREAD;
1102    killer_xp  = XPTR( local_cxy , killer_ptr );
1103
1104#if DEBUG_THREAD_DELETE
1105uint32_t cycle  = (uint32_t)hal_get_cycles();
1106if( DEBUG_THREAD_DELETE < cycle )
1107printk("\n[%s] killer[%x,%x] enters / target[%x,%x] / cycle %d\n",
1108__FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid, 
1109target_ptr->process->pid, target_ptr->trdid, cycle );
1110#endif
1111
1112// check target thread is not the main thread, because the main thread
1113// must be deleted by the parent process sys_wait() function
1114assert( ((CXY_FROM_PID( pid ) != target_cxy) || (target_ltid != 0)),
1115"target thread cannot be the main thread" );
1116
1117    // check killer thread can yield
1118    thread_assert_can_yield( killer_ptr , __FUNCTION__ ); 
1119
1120    // if the target thread is attached, we must synchonize with the joining thread
1121    // before blocking and marking the target thead for delete.
1122
1123    if( target_attached && (is_forced == false) ) // synchronize with joining thread
1124    {
1125        // build extended pointers on target thread join fields
1126        target_join_lock_xp  = XPTR( target_cxy , &target_ptr->join_lock );
1127        target_join_xp_xp    = XPTR( target_cxy , &target_ptr->join_xp );
1128
1129        // enter critical section
1130        hal_disable_irq( &save_sr );
1131
1132        // take the join_lock in target thread descriptor
1133        remote_busylock_acquire( target_join_lock_xp );
1134
1135        // get join_done from target thread descriptor
1136        target_join_done = ((hal_remote_l32( target_flags_xp ) & THREAD_FLAG_JOIN_DONE) != 0);
1137   
1138        if( target_join_done )                     // joining thread arrived first
1139        {
1140            // get extended pointer on joining thread
1141            joining_xp  = (xptr_t)hal_remote_l64( target_join_xp_xp );
1142           
1143            // reset the join_done flag in target thread
1144            hal_remote_atomic_and( target_flags_xp , ~THREAD_FLAG_JOIN_DONE );
1145
1146            // unblock the joining thread
1147            thread_unblock( joining_xp , THREAD_BLOCKED_JOIN );
1148
1149            // release the join_lock in target thread descriptor
1150            remote_busylock_release( target_join_lock_xp );
1151
1152            // block the target thread
1153            thread_block( target_xp , THREAD_BLOCKED_GLOBAL );
1154
1155            // set the REQ_DELETE flag in target thread descriptor
1156            hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
1157
1158            // exit critical section
1159            hal_restore_irq( save_sr );
1160
1161#if DEBUG_THREAD_DELETE
1162cycle  = (uint32_t)hal_get_cycles;
1163if( DEBUG_THREAD_DELETE < cycle )
1164printk("\n[%s] killer[%x,%x] exit / target[%x,%x] marked after join / cycle %d\n",
1165__FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid,
1166target_ptr->process->pid, target_ptr->trdid, cycle );
1167#endif
1168
1169        }
1170        else                                      // killer thread arrived first
1171        {
1172            // set the kill_done flag in target thread
1173            hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_KILL_DONE );
1174
1175            // block this thread on BLOCKED_JOIN
1176            thread_block( killer_xp , THREAD_BLOCKED_JOIN );
1177
1178            // set extended pointer on killer thread in target thread
1179            hal_remote_s64( target_join_xp_xp , killer_xp );
1180
1181            // release the join_lock in target thread descriptor
1182            remote_busylock_release( target_join_lock_xp );
1183
1184#if DEBUG_THREAD_DELETE
1185cycle  = (uint32_t)hal_get_cycles;
1186if( DEBUG_THREAD_DELETE < cycle )
1187printk("\n[%s] killer[%x,%x] deschedules / target[%x,%x] not completed / cycle %d\n",
1188__FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid,
1189target_ptr->process->pid, target_ptr->trdid, cycle );
1190#endif
1191            // deschedule
1192            sched_yield( "killer thread wait joining thread" );
1193
1194            // block the target thread
1195            thread_block( target_xp , THREAD_BLOCKED_GLOBAL );
1196
1197            // set the REQ_DELETE flag in target thread descriptor
1198            hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
1199
1200            // exit critical section
1201            hal_restore_irq( save_sr );
1202
1203#if DEBUG_THREAD_DELETE
1204cycle  = (uint32_t)hal_get_cycles;
1205if( DEBUG_THREAD_DELETE < cycle )
1206printk("\n[%s] killer[%x,%x] exit / target[%x,%x] marked after join / cycle %d\n",
1207__FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid,
1208target_ptr->process->pid, target_ptr->trdid, cycle );
1209#endif
1210
1211        }
1212    }
1213    else                     // no synchronization with joining thread required
1214    {
1215        // block the target thread
1216        thread_block( target_xp , THREAD_BLOCKED_GLOBAL );
1217
1218        // set the REQ_DELETE flag in target thread descriptor
1219        hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
1220
1221#if DEBUG_THREAD_DELETE
1222cycle  = (uint32_t)hal_get_cycles;
1223if( DEBUG_THREAD_DELETE < cycle )
1224printk("\n[%s] killer[%x,%x] exit / target [%x,%x] marked / no join / cycle %d\n",
1225__FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid,
1226target_ptr->process->pid, target_ptr->trdid, cycle );
1227#endif
1228
1229    }
1230}  // end thread_delete()
1231
1232
1233
1234/////////////////////////////
1235void thread_idle_func( void )
1236{
1237
1238#if DEBUG_THREAD_IDLE
1239uint32_t cycle;
1240#endif
1241
1242    while( 1 )
1243    {
1244        // unmask IRQs
1245        hal_enable_irq( NULL );
1246
1247        // force core to low-power mode (optional)
1248        if( CONFIG_SCHED_IDLE_MODE_SLEEP ) 
1249        {
1250
1251#if DEBUG_THREAD_IDLE
1252cycle = (uint32_t)hal_get_cycles();
1253if( DEBUG_THREAD_IDLE < cycle )
1254printk("\n[%s] idle thread on core[%x,%d] goes to sleep / cycle %d\n",
1255__FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, cycle );
1256#endif
1257
1258            hal_core_sleep();
1259
1260#if DEBUG_THREAD_IDLE
1261cycle = (uint32_t)hal_get_cycles();
1262if( DEBUG_THREAD_IDLE < cycle )
1263printk("\n[%s] idle thread on core[%x,%d] wake up / cycle %d\n",
1264__FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, cycle );
1265#endif
1266
1267        }
1268
1269#if DEBUG_THREAD_IDLE
1270cycle = (uint32_t)hal_get_cycles();
1271if( DEBUG_THREAD_IDLE < cycle )
1272sched_display( CURRENT_THREAD->core->lid );
1273#endif     
1274        // search a runable thread
1275        sched_yield( "running idle thread" );
1276
1277    } // end while
1278
1279}  // end thread_idle()
1280
1281
1282///////////////////////////////////////////
1283void thread_time_update( thread_t * thread,
1284                         bool_t     is_user )
1285{
1286    cycle_t current_cycle;   // current cycle counter value
1287    cycle_t last_cycle;      // last cycle counter value
1288
1289    // get pointer on thread_info structure
1290    thread_info_t * info = &thread->info;
1291
1292    // get last cycle counter value
1293    last_cycle = info->last_cycle;
1294
1295    // get current cycle counter value
1296    current_cycle = hal_get_cycles();
1297
1298    // update thread_info structure
1299    info->last_cycle = current_cycle;
1300
1301    // update time in thread_info
1302    if( is_user ) info->usr_cycles += (current_cycle - last_cycle);
1303    else          info->sys_cycles += (current_cycle - last_cycle);
1304
1305}  // end thread_time_update()
1306
1307/////////////////////////////////////
1308xptr_t thread_get_xptr( pid_t    pid,
1309                        trdid_t  trdid )
1310{
1311    cxy_t         target_cxy;          // target thread cluster identifier
1312    ltid_t        target_thread_ltid;  // target thread local index
1313    thread_t    * target_thread_ptr;   // target thread local pointer
1314    xptr_t        target_process_xp;   // extended pointer on target process descriptor
1315    process_t   * target_process_ptr;  // local pointer on target process descriptor
1316    pid_t         target_process_pid;  // target process identifier
1317    xlist_entry_t root;                // root of list of process in target cluster
1318    xptr_t        lock_xp;             // extended pointer on lock protecting  this list
1319
1320#if DEBUG_THREAD_GET_XPTR
1321uint32_t cycle  = (uint32_t)hal_get_cycles();
1322thread_t * this = CURRENT_THREAD;
1323if( DEBUG_THREAD_GET_XPTR < cycle )
1324printk("\n[%s] thread %x in process %x enters / pid %x / trdid %x / cycle %d\n",
1325__FUNCTION__, this->trdid, this->process->pid, pid, trdid, cycle );
1326#endif
1327
1328    // get target cluster identifier and local thread identifier
1329    target_cxy         = CXY_FROM_TRDID( trdid );
1330    target_thread_ltid = LTID_FROM_TRDID( trdid );
1331
1332    // check trdid argument
1333        if( (target_thread_ltid >= CONFIG_THREADS_MAX_PER_CLUSTER) || 
1334        cluster_is_active( target_cxy ) == false )                return XPTR_NULL;
1335
1336    // get root of list of process descriptors in target cluster
1337    hal_remote_memcpy( XPTR( local_cxy  , &root ),
1338                       XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ),
1339                       sizeof(xlist_entry_t) );
1340
1341    // get extended pointer on lock protecting the list of local processes
1342    lock_xp = XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_lock );
1343
1344    // take the lock protecting the list of processes in target cluster
1345    remote_queuelock_acquire( lock_xp );
1346
1347#if( DEBUG_THREAD_GET_XPTR & 1 )
1348if( DEBUG_THREAD_GET_XPTR < cycle )
1349printk("\n[%s] scan processes in cluster %x :\n", __FUNCTION__, target_cxy );
1350#endif
1351
1352    // scan the list of local processes in target cluster
1353    xptr_t  iter;
1354    bool_t  found = false;
1355    XLIST_FOREACH( XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ) , iter )
1356    {
1357        target_process_xp  = XLIST_ELEMENT( iter , process_t , local_list );
1358        target_process_ptr = GET_PTR( target_process_xp );
1359        target_process_pid = hal_remote_l32( XPTR( target_cxy , &target_process_ptr->pid ) );
1360
1361#if( DEBUG_THREAD_GET_XPTR & 1 )
1362if( DEBUG_THREAD_GET_XPTR < cycle )
1363printk(" - process %x\n", target_process_pid );
1364#endif
1365
1366        if( target_process_pid == pid )
1367        {
1368            found = true;
1369            break;
1370        }
1371    }
1372
1373    // release the lock protecting the list of processes in target cluster
1374    remote_queuelock_release( lock_xp );
1375
1376    // check PID found
1377    if( found == false ) 
1378    {
1379
1380#if( DEBUG_THREAD_GET_XPTR & 1 )
1381if( DEBUG_THREAD_GET_XPTR < cycle )
1382printk("\n[%s] pid %x not found in cluster %x\n",
1383__FUNCTION__, pid, target_cxy );
1384#endif
1385        return XPTR_NULL;
1386    }
1387
1388    // get target thread local pointer
1389    xptr_t xp = XPTR( target_cxy , &target_process_ptr->th_tbl[target_thread_ltid] );
1390    target_thread_ptr = (thread_t *)hal_remote_lpt( xp );
1391
1392    if( target_thread_ptr == NULL )
1393    {
1394
1395#if( DEBUG_THREAD_GET_XPTR & 1 )
1396if( DEBUG_THREAD_GET_XPTR < cycle )
1397printk("\n[%s] thread %x not registered in process %x in cluster %x\n",
1398__FUNCTION__, trdid, pid, target_cxy );
1399#endif
1400        return XPTR_NULL;
1401    }
1402
1403#if DEBUG_THREAD_GET_XPTR
1404cycle  = (uint32_t)hal_get_cycles();
1405if( DEBUG_THREAD_GET_XPTR < cycle )
1406printk("\n[%s] thread %x in process %x exit / pid %x / trdid %x / cycle %d\n",
1407__FUNCTION__, this->trdid, this->process->pid, pid, trdid, cycle );
1408#endif
1409
1410    return XPTR( target_cxy , target_thread_ptr );
1411
1412}  // end thread_get_xptr()
1413
1414///////////////////////////////////////////////////
1415void thread_assert_can_yield( thread_t    * thread,
1416                              const char  * func_str )
1417{
1418    // does nothing if thread does not hold any busylock
1419
1420    if( thread->busylocks )
1421    {
1422        // get pointers on TXT0 chdev
1423        xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
1424        cxy_t     txt0_cxy = GET_CXY( txt0_xp );
1425        chdev_t * txt0_ptr = GET_PTR( txt0_xp );
1426
1427        // get extended pointer on TXT0 lock
1428        xptr_t  txt0_lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
1429
1430        // get TXT0 lock
1431        remote_busylock_acquire( txt0_lock_xp );
1432
1433        // display error message on TXT0
1434        nolock_printk("\n[PANIC] in %s / thread[%x,%x] cannot yield : "
1435        "hold %d busylock(s) / cycle %d\n",
1436        func_str, thread->process->pid, thread->trdid,
1437        thread->busylocks - 1, (uint32_t)hal_get_cycles() );
1438
1439#if DEBUG_BUSYLOCK
1440
1441// scan list of busylocks
1442xptr_t    iter_xp;
1443xptr_t    root_xp  = XPTR( local_cxy , &thread->busylocks_root );
1444XLIST_FOREACH( root_xp , iter_xp )
1445{
1446    xptr_t       lock_xp   = XLIST_ELEMENT( iter_xp , busylock_t , xlist );
1447    cxy_t        lock_cxy  = GET_CXY( lock_xp );
1448    busylock_t * lock_ptr  = GET_PTR( lock_xp );
1449    uint32_t     lock_type = hal_remote_l32( XPTR( lock_cxy , &lock_ptr->type ) );
1450    nolock_printk(" - %s in cluster %x\n", lock_type_str[lock_type] , lock_cxy );
1451}
1452
1453#endif
1454
1455        // release TXT0 lock
1456        remote_busylock_release( txt0_lock_xp );
1457
1458        // suicide
1459        hal_core_sleep();
1460    }
1461}  // end thread_assert_can yield()
1462
1463//////////////////////////////////////////////////////
1464void thread_display_busylocks( xptr_t       thread_xp,
1465                               const char * string )
1466{
1467
1468    cxy_t      thread_cxy = GET_CXY( thread_xp );
1469    thread_t * thread_ptr = GET_PTR( thread_xp );
1470
1471#if DEBUG_BUSYLOCK
1472
1473    xptr_t     iter_xp;
1474
1475    // get relevant info from target thread descriptor
1476    uint32_t    locks   = hal_remote_l32( XPTR( thread_cxy , &thread_ptr->busylocks ) );
1477    trdid_t     trdid   = hal_remote_l32( XPTR( thread_cxy , &thread_ptr->trdid ) );
1478    process_t * process = hal_remote_lpt( XPTR( thread_cxy , &thread_ptr->process ) );
1479    pid_t       pid     = hal_remote_l32( XPTR( thread_cxy , &process->pid ) );
1480
1481    // get extended pointer on root of busylocks
1482    xptr_t root_xp = XPTR( thread_cxy , &thread_ptr->busylocks_root );
1483
1484    // get pointers on TXT0 chdev
1485    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
1486    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
1487    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
1488
1489    // get extended pointer on remote TXT0 lock
1490    xptr_t  txt0_lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
1491
1492    // get TXT0 lock
1493    remote_busylock_acquire( txt0_lock_xp );
1494
1495    // display header
1496    nolock_printk("\n***** thread[%x,%x] in <%s> : %d busylocks *****\n",
1497    pid, trdid, string, locks );
1498
1499    // scan the xlist of busylocks when required
1500    if( locks )
1501    {
1502        XLIST_FOREACH( root_xp , iter_xp )
1503        {
1504            xptr_t       lock_xp   = XLIST_ELEMENT( iter_xp , busylock_t , xlist );
1505            cxy_t        lock_cxy  = GET_CXY( lock_xp );
1506            busylock_t * lock_ptr  = GET_PTR( lock_xp );
1507            uint32_t     lock_type = hal_remote_l32(XPTR( lock_cxy , &lock_ptr->type ));
1508            nolock_printk(" - %s in cluster %x\n", lock_type_str[lock_type] , lock_cxy );
1509        }
1510    }
1511
1512    // release TXT0 lock
1513    remote_busylock_release( txt0_lock_xp );
1514
1515#else
1516
1517printk("\n[ERROR] in %s : set DEBUG_BUSYLOCK in kernel_config.h for %s / thread(%x,%x)\n",
1518__FUNCTION__, string, thread_cxy, thread_ptr );
1519
1520#endif
1521
1522    return;
1523
1524}  // end thread_display_busylock()
1525
Note: See TracBrowser for help on using the repository browser.