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

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

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

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