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

Last change on this file since 569 was 564, checked in by alain, 6 years ago

Complete restructuration of kernel locks.

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