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

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

cosmetic

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