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

Last change on this file since 670 was 669, checked in by alain, 4 years ago

1) Introduce up to 4 command lines arguments in the KSH "load" command.
These arguments are transfered to the user process through the
argc/argv mechanism, using the user space "args" vseg.

2) Introduce the named and anonymous "pipes", for inter-process communication
through the pipe() and mkfifo() syscalls.

3) Introduce the "chat" application to validate the two above mechanisms.

4) Improve printk() and assert() fonctions in printk.c.

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