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

Last change on this file since 581 was 581, checked in by alain, 3 years ago

1) Improve the busylock debug infrastructure.
2) introduce a non-distributed, but portable implementation for the pthread_barrier.

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