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

Last change on this file since 675 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
Line 
1/*
2 * thread.c -   thread operations implementation (user & kernel)
3 *
4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *         Alain Greiner (2016,2017,2018,2019,2020)
6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <kernel_config.h>
26#include <hal_kernel_types.h>
27#include <hal_context.h>
28#include <hal_irqmask.h>
29#include <hal_special.h>
30#include <hal_remote.h>
31#include <hal_vmm.h>
32#include <hal_switch.h>
33#include <memcpy.h>
34#include <printk.h>
35#include <cluster.h>
36#include <process.h>
37#include <scheduler.h>
38#include <dev_pic.h>
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>
46#include <rpc.h>
47
48//////////////////////////////////////////////////////////////////////////////////////
49// Extern global variables
50//////////////////////////////////////////////////////////////////////////////////////
51
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
55
56//////////////////////////////////////////////////////////////////////////////////////
57// This function returns a printable string for the thread type.
58//////////////////////////////////////////////////////////////////////////////////////
59const char * thread_type_str( thread_type_t type )
60{
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  }
68}
69
70/////////////////////////////////////////////////////////////////////////////////////
71// This static function allocates physical memory for a thread descriptor.
72// It can be called by the three functions:
73// - thread_user_create()
74// - thread_user_fork()
75// - thread_kernel_create()
76/////////////////////////////////////////////////////////////////////////////////////
77// @ return pointer on thread descriptor if success / return NULL if failure.
78/////////////////////////////////////////////////////////////////////////////////////
79static thread_t * thread_alloc( void )
80{
81        kmem_req_t     req;    // kmem request
82
83        // allocates memory for thread descriptor + kernel stack
84        req.type  = KMEM_PPM;
85        req.order = CONFIG_THREAD_DESC_ORDER;
86        req.flags = AF_KERNEL | AF_ZERO;
87
88    return kmem_alloc( &req );
89
90}  // end thread_alloc()
91 
92
93/////////////////////////////////////////////////////////////////////////////////////
94// This static function initializes a thread descriptor (kernel or user).
95// It can be called by the four functions:
96// - thread_user_create()
97// - thread_user_fork()
98// - thread_kernel_create()
99// - thread_idle_init()
100// The "type" and "trdid" fields must have been previously set.
101// It updates the local DQDT.
102/////////////////////////////////////////////////////////////////////////////////////
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)
111/////////////////////////////////////////////////////////////////////////////////////
112static error_t thread_init( thread_t      * thread,
113                            process_t     * process,
114                            thread_type_t   type,
115                            trdid_t         trdid,
116                            void          * func,
117                            void          * args,
118                            lid_t           core_lid,
119                            vseg_t        * user_stack_vseg )
120{
121
122// check type and trdid fields are initialized
123assert( __FUNCTION__, (thread->type == type)   , "bad type argument" );
124assert( __FUNCTION__, (thread->trdid == trdid) , "bad trdid argument" );
125
126#if DEBUG_THREAD_INIT
127uint32_t   cycle = (uint32_t)hal_get_cycles();
128thread_t * this  = CURRENT_THREAD;
129if( DEBUG_THREAD_INIT < cycle )
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 );
132#endif
133
134    // compute thread descriptor size without kernel stack
135    uint32_t desc_size = (intptr_t)(&thread->signature) - (intptr_t)thread + 4; 
136
137        // Initialize new thread descriptor
138    thread->quantum         = 0;            // TODO
139    thread->ticks_nr        = 0;            // TODO
140    thread->time_last_check = 0;            // TODO
141        thread->core            = &LOCAL_CLUSTER->core_tbl[core_lid];
142        thread->process         = process;
143    thread->busylocks       = 0;
144
145#if DEBUG_BUSYLOCK
146    xlist_root_init( XPTR( local_cxy , &thread->busylocks_root ) );
147#endif
148
149    thread->user_stack_vseg = user_stack_vseg;
150    thread->k_stack_base    = (intptr_t)thread + desc_size;
151    thread->k_stack_size    = CONFIG_THREAD_DESC_SIZE - desc_size;
152    thread->entry_func      = func;         // thread entry point
153    thread->entry_args      = args;         // thread function arguments
154    thread->flags           = 0;            // all flags reset
155    thread->errno           = 0;            // no error detected
156    thread->fork_user       = 0;            // no user defined placement for fork
157    thread->fork_cxy        = 0;            // user defined target cluster for fork
158    thread->blocked         = THREAD_BLOCKED_GLOBAL;
159
160    // initialize sched list
161    list_entry_init( &thread->sched_list );
162
163    // initialize the embedded alarm to unlink
164    list_entry_init( &thread->alarm.list );
165
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
171    memset( &thread->info , 0 , sizeof(thread_info_t) );
172
173    // initialize join_lock
174    remote_busylock_init( XPTR( local_cxy , &thread->join_lock ), LOCK_THREAD_JOIN );
175
176    // initialise signature
177        thread->signature = THREAD_SIGNATURE;
178
179    // FIXME define and call an architecture specific hal_thread_init()
180    // function to initialise the save_sr field
181    thread->save_sr = 0xFF13;
182
183    // register new thread in core scheduler
184    sched_register_thread( thread->core , thread );
185
186        // update DQDT
187    dqdt_increment_threads();
188
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
201#if DEBUG_THREAD_INIT
202cycle = (uint32_t)hal_get_cycles();
203if( DEBUG_THREAD_INIT < cycle )
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 );
206#endif
207
208        return 0;
209
210} // end thread_init()
211
212//////////////////////////////////////////////////
213error_t thread_user_create( pid_t             pid,
214                            void            * start_func,
215                            void            * start_arg,
216                            pthread_attr_t  * attr,
217                            thread_t       ** new_thread )
218{
219    error_t        error;
220        thread_t     * thread;       // pointer on created thread descriptor
221    trdid_t        trdid;        // created thred identifier
222    process_t    * process;      // pointer to local process descriptor
223    lid_t          core_lid;     // selected core local index
224    vseg_t       * us_vseg;      // user stack vseg
225
226assert( __FUNCTION__, (attr != NULL) , "pthread attributes must be defined" );
227
228#if DEBUG_THREAD_USER_CREATE
229thread_t * this  = CURRENT_THREAD;
230uint32_t   cycle = (uint32_t)hal_get_cycles();
231if( DEBUG_THREAD_USER_CREATE < cycle )
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 );
234#endif
235
236    // get process descriptor local copy
237    process = process_get_local_copy( pid );
238
239    if( process == NULL )
240    {
241                printk("\n[ERROR] in %s : cannot get process descriptor %x\n",
242        __FUNCTION__ , pid );
243        return -1;
244    }
245
246#if( DEBUG_THREAD_USER_CREATE & 1)
247if( DEBUG_THREAD_USER_CREATE < cycle )
248printk("\n[%s] process descriptor = %x for process %x in cluster %x\n",
249__FUNCTION__, process , pid , local_cxy );
250#endif
251
252    // select a target core in local cluster
253    if( attr->attributes & PT_ATTR_CORE_DEFINED )
254    {
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 );
260            return -1;
261        }
262    }
263    else
264    {
265        core_lid = cluster_select_local_core( local_cxy );
266    }
267
268#if( DEBUG_THREAD_USER_CREATE & 1)
269if( DEBUG_THREAD_USER_CREATE < cycle )
270printk("\n[%s] core[%x,%d] selected\n",
271__FUNCTION__, local_cxy , core_lid );
272#endif
273
274    // allocate memory for thread descriptor
275    thread = thread_alloc();
276
277    if( thread == NULL )
278    {
279            printk("\n[ERROR] in %s : cannot create new thread in cluster %x\n",
280        __FUNCTION__, local_cxy );
281        return -1;
282    }
283
284#if( DEBUG_THREAD_USER_CREATE & 1)
285if( DEBUG_THREAD_USER_CREATE < cycle )
286printk("\n[%s] new thread descriptor %x allocated\n",
287__FUNCTION__, thread );
288#endif
289
290    // set type in thread descriptor
291    thread->type = THREAD_USER;
292
293    // register new thread in process descriptor, and get a TRDID
294    error = process_register_thread( process, thread , &trdid );
295
296    if( error )
297    {
298        printk("\n[ERROR] in %s : cannot register new thread in process %x\n",
299        __FUNCTION__, pid );
300        thread_destroy( thread );
301        return -1;
302    }
303
304    // set trdid in thread descriptor
305    thread->trdid = trdid;
306
307#if( DEBUG_THREAD_USER_CREATE & 1)
308if( DEBUG_THREAD_USER_CREATE < cycle )
309printk("\n[%s] new thread %x registered in process %x\n",
310__FUNCTION__, trdid, pid );
311#endif
312
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
337    // initialize thread descriptor
338    error = thread_init( thread,
339                         process,
340                         THREAD_USER,
341                         trdid,
342                         start_func,
343                         start_arg,
344                         core_lid,
345                         us_vseg );
346    if( error )
347    {
348            printk("\n[ERROR] in %s : cannot initialize new thread\n", __FUNCTION__ );
349        vmm_remove_vseg( process , us_vseg );
350        process_remove_thread( thread );
351        thread_destroy( thread );
352        return -1;
353    }
354
355#if( DEBUG_THREAD_USER_CREATE & 1)
356if( DEBUG_THREAD_USER_CREATE < cycle )
357printk("\n[%s] new thread %x in process %x initialised\n",
358__FUNCTION__, thread->trdid, process->pid );
359#endif
360
361    // set DETACHED flag if required
362    if( attr->attributes & PT_ATTR_DETACH ) 
363    {
364        thread->flags |= THREAD_FLAG_DETACHED;
365    }
366
367    // allocate & initialize CPU context
368        if( hal_cpu_context_alloc( thread ) )
369    {
370            printk("\n[ERROR] in %s : cannot create CPU context\n", __FUNCTION__ );
371        vmm_remove_vseg( process , us_vseg );
372        process_remove_thread( thread );
373        thread_destroy( thread );
374        return -1;
375    }
376    hal_cpu_context_init( thread,
377                          false , 0 , 0 );   // not a main thread
378
379    // allocate & initialize FPU context
380    if( hal_fpu_context_alloc( thread ) )
381    {
382            printk("\n[ERROR] in %s : cannot create FPU context\n", __FUNCTION__ );
383        vmm_remove_vseg( process , us_vseg );
384        process_remove_thread( thread );
385        thread_destroy( thread );
386        return -1;
387    }
388    hal_fpu_context_init( thread );
389
390#if( DEBUG_THREAD_USER_CREATE & 1)
391if( DEBUG_THREAD_USER_CREATE < cycle )
392printk("\n[%s] CPU & FPU contexts created\n",
393__FUNCTION__, thread->trdid );
394hal_vmm_display( XPTR( local_cxy , process ) , true );
395#endif
396
397#if DEBUG_THREAD_USER_CREATE
398cycle = (uint32_t)hal_get_cycles();
399if( DEBUG_THREAD_USER_CREATE < cycle )
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 );
402#endif
403
404    *new_thread = thread;
405        return 0;
406
407}  // end thread_user_create()
408
409///////////////////////////////////////////////////////
410error_t thread_user_fork( xptr_t      parent_thread_xp,
411                          process_t * child_process,
412                          thread_t ** child_thread )
413{
414    error_t        error;
415        thread_t     * child_ptr;        // local pointer on child thread
416    trdid_t        child_trdid;      // child thread identifier
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
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
427
428#if DEBUG_THREAD_USER_FORK
429uint32_t   cycle = (uint32_t)hal_get_cycles();
430thread_t * this  = CURRENT_THREAD;
431if( DEBUG_THREAD_USER_FORK < cycle )
432printk("\n[%s] thread[%x,%x] enter for child_process %x / cycle %d\n",
433__FUNCTION__, this->process->pid, this->trdid, child_process->pid, cycle );
434#endif
435
436    // select a target core in local cluster
437    core_lid = cluster_select_local_core( local_cxy );
438
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
445    // get cluster and local pointer on parent thread descriptor
446    parent_cxy = GET_CXY( parent_thread_xp );
447    parent_ptr = GET_PTR( parent_thread_xp );
448
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 ));
454
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 
459    // build extended pointer on parent GPT in parent thread cluster
460    parent_gpt_xp = XPTR( parent_cxy , &parent_process->vmm.gpt );
461
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
468    // allocate memory for child thread descriptor
469    child_ptr = thread_alloc();
470
471    if( child_ptr == NULL )
472    {
473        printk("\n[ERROR] in %s : cannot allocate new thread\n",
474        __FUNCTION__ );
475        return -1;
476    }
477
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
531    // initialize thread descriptor
532    error = thread_init( child_ptr,
533                         child_process,
534                         THREAD_USER,
535                         child_trdid,
536                         parent_func,
537                         parent_args,
538                         core_lid,
539                         child_us_vseg );
540    if( error )
541    {
542            printk("\n[ERROR] in %s : cannot initialize child thread\n", __FUNCTION__ );
543        vmm_remove_vseg( child_process , child_us_vseg ); 
544        process_remove_thread( child_ptr );
545        thread_destroy( child_ptr );
546        return -1;
547    }
548
549#if (DEBUG_THREAD_USER_FORK & 1)
550if( DEBUG_THREAD_USER_FORK < cycle )
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 );
553#endif
554
555    // set detached flag if required
556    if( parent_flags & THREAD_FLAG_DETACHED ) child_ptr->flags = THREAD_FLAG_DETACHED;
557
558    // allocate a CPU context for child thread
559        if( hal_cpu_context_alloc( child_ptr ) )
560    {
561            printk("\n[ERROR] in %s : cannot allocate CPU context\n", __FUNCTION__ );
562        vmm_remove_vseg( child_process , child_us_vseg );
563        process_remove_thread( child_ptr );
564        thread_destroy( child_ptr );
565        return -1;
566    }
567
568    // allocate a FPU context for child thread
569        if( hal_fpu_context_alloc( child_ptr ) )
570    {
571            printk("\n[ERROR] in %s : cannot allocate FPU context\n", __FUNCTION__ );
572        vmm_remove_vseg( child_process , child_us_vseg );
573        process_remove_thread( child_ptr );
574        thread_destroy( child_ptr );
575        return -1;
576    }
577
578#if (DEBUG_THREAD_USER_FORK & 1)
579if( DEBUG_THREAD_USER_FORK < cycle )
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 );
582#endif
583
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;
593
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++ )
597    {
598        error = hal_gpt_pte_copy( &child_process->vmm.gpt,
599                                  child_vpn,
600                                  parent_gpt_xp,
601                                  parent_vpn,
602                                  true,                 // set cow
603                                  &ppn,
604                                  &mapped );
605        if( error )
606        {
607            printk("\n[ERROR] in %s : cannot update child GPT\n", __FUNCTION__ );
608            vmm_remove_vseg( child_process , child_us_vseg );
609            process_remove_thread( child_ptr );
610            thread_destroy( child_ptr );
611            return -1;
612        }
613
614        // increment pending forks counter for a mapped page
615        if( mapped )
616        {
617            // get pointers on the page descriptor
618            xptr_t   page_xp  = ppm_ppn2page( ppn );
619            cxy_t    page_cxy = GET_CXY( page_xp );
620            page_t * page_ptr = GET_PTR( page_xp );
621
622            // build extended pointers on forks and lock fields
623            xptr_t forks_xp = XPTR( page_cxy , &page_ptr->forks );
624            xptr_t lock_xp  = XPTR( page_cxy , &page_ptr->lock );
625
626            // get lock protecting page
627            remote_busylock_acquire( lock_xp ); 
628
629            // increment the forks counter in page descriptor
630            hal_remote_atomic_add( forks_xp , 1 );
631
632            // release lock protecting page
633            remote_busylock_release( lock_xp ); 
634        }
635    }
636
637#if (DEBUG_THREAD_USER_FORK & 1)
638if( DEBUG_THREAD_USER_FORK < cycle )
639printk("\n[%s] thread[%x,%x] copied STACK vseg PTEs & set COW in child GPT\n",
640__FUNCTION__, this->process->pid, this->trdid );
641#endif
642
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 );
647
648#if (DEBUG_THREAD_USER_FORK & 1)
649if( DEBUG_THREAD_USER_FORK < cycle )
650printk("\n[%s] thread[%x,%x] set COW for STACK vseg in parent GPT\n",
651__FUNCTION__, this->process->pid, this->trdid );
652#endif
653
654    // return child pointer
655    *child_thread = child_ptr;
656
657#if DEBUG_THREAD_USER_FORK
658cycle = (uint32_t)hal_get_cycles();
659if( DEBUG_THREAD_USER_FORK < cycle )
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 );
663#endif
664
665        return 0;
666
667}  // end thread_user_fork()
668
669/////////////////////////////////////
670void thread_user_exec( uint32_t argc,
671                       intptr_t argv )
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 )
679printk("\n[%s] thread[%x,%x] enter / cycle %d\n",
680__FUNCTION__, process->pid, thread->trdid, cycle );
681#endif
682
683// check parent thread attributes
684assert( __FUNCTION__, (thread->type      == THREAD_USER )     , "bad type" );
685assert( __FUNCTION__, (thread->signature == THREAD_SIGNATURE) , "bad signature" );
686assert( __FUNCTION__, (thread->busylocks == 0)                , "bad busylocks" );
687
688        // re-initialize various thread descriptor fields
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
694    thread->blocked         = 0;
695    thread->errno           = 0;
696    thread->fork_user       = 0;
697    thread->fork_cxy        = 0;
698
699    // reset thread info
700    memset( &thread->info , 0 , sizeof(thread_info_t) );
701
702    // re-initialize join_lock
703    remote_busylock_init( XPTR( local_cxy , &thread->join_lock ), LOCK_THREAD_JOIN );
704
705    // release FPU ownership if required
706    if( thread->core->fpu_owner == thread ) thread->core->fpu_owner = NULL;
707
708    // initialize thread FPU context
709    hal_fpu_context_init( thread );
710
711    // initialize thread CPU context
712    hal_cpu_context_init( thread,
713                          true,          // main thread
714                          argc,
715                          argv ); 
716
717#if DEBUG_THREAD_USER_EXEC
718cycle = (uint32_t)hal_get_cycles();
719if( DEBUG_THREAD_USER_EXEC < cycle )
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}
727#endif
728
729    // restore CPU registers ... and jump to user code
730    hal_do_cpu_restore( thread->cpu_context );
731
732}  // end thread_user_exec()
733
734/////////////////////////////////////////////////////////
735error_t thread_kernel_create( thread_t     ** new_thread,
736                              thread_type_t   type,
737                              void          * func,
738                              void          * args,
739                                              lid_t           core_lid )
740{
741    error_t        error;
742        thread_t     * thread;       // pointer on new thread descriptor
743    trdid_t        trdid;        // new thread identifier
744
745    thread_t * this = CURRENT_THREAD; 
746
747assert( __FUNCTION__, ( (type == THREAD_IDLE) || (type == THREAD_RPC) || (type == THREAD_DEV) ) ,
748"illegal thread type" );
749
750assert( __FUNCTION__, (core_lid < LOCAL_CLUSTER->cores_nr) ,
751"illegal core_lid" );
752
753#if DEBUG_THREAD_KERNEL_CREATE
754uint32_t   cycle = (uint32_t)hal_get_cycles();
755if( DEBUG_THREAD_KERNEL_CREATE < cycle )
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 );
758#endif
759
760    // allocate memory for new thread descriptor
761    thread = thread_alloc();
762
763    if( thread == NULL )
764    {
765        printk("\n[ERROR] in %s : thread %x in process %x\n"
766        "   no memory for thread descriptor\n",
767        __FUNCTION__, this->trdid, this->process->pid );
768        return ENOMEM;
769    }
770
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
786    // initialize thread descriptor
787    error = thread_init( thread,
788                         &process_zero,
789                         type,
790                         trdid,
791                         func,
792                         args,
793                         core_lid,
794                         NULL );  // no user stack for a kernel thread
795
796    if( error ) // release allocated memory for thread descriptor
797    {
798        printk("\n[ERROR] in %s : cannot initialize thread descriptor\n", __FUNCTION__ );
799        thread_destroy( thread );
800        return ENOMEM;
801    }
802
803    // allocate & initialize CPU context
804        error = hal_cpu_context_alloc( thread );
805
806    if( error )
807    {
808        printk("\n[ERROR] in %s : thread %x in process %x\n"
809        "    cannot create CPU context\n",
810        __FUNCTION__, this->trdid, this->process->pid );
811        thread_destroy( thread );
812        return EINVAL;
813    }
814
815    hal_cpu_context_init( thread,
816                          false , 0 , 0 );  // not a main thread
817
818#if DEBUG_THREAD_KERNEL_CREATE
819cycle = (uint32_t)hal_get_cycles();
820if( DEBUG_THREAD_KERNEL_CREATE < cycle )
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 );
823#endif
824
825    *new_thread = thread;
826        return 0;
827
828} // end thread_kernel_create()
829
830//////////////////////////////////////////////
831void thread_idle_init( thread_t      * thread,
832                       thread_type_t   type,
833                       void          * func,
834                       void          * args,
835                           lid_t           core_lid )
836{
837    trdid_t trdid;   
838    error_t error;
839
840// check arguments
841assert( __FUNCTION__, (type == THREAD_IDLE) , "illegal thread type" );
842assert( __FUNCTION__, (core_lid < LOCAL_CLUSTER->cores_nr) , "illegal core index" );
843
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
850assert( __FUNCTION__, (error == 0), "cannot register idle_thread in kernel process" );
851
852    // set trdid in thread descriptor
853    thread->trdid = trdid;
854
855    // initialize thread descriptor
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
864
865assert( __FUNCTION__, (error == 0), "cannot initialize idle_thread" );
866
867    // allocate CPU context
868    error = hal_cpu_context_alloc( thread );
869
870assert( __FUNCTION__, (error == 0), "cannot allocate CPU context" );
871
872    // initialize CPU context
873    hal_cpu_context_init( thread,
874                          false , 0 , 0 );   // not a main thread
875
876}  // end thread_idle_init()
877
878////////////////////////////////////////////
879uint32_t thread_destroy( thread_t * thread )
880{
881    reg_t           save_sr;
882    uint32_t        count;
883
884    thread_type_t   type    = thread->type;
885    process_t     * process = thread->process;
886    core_t        * core    = thread->core;
887
888#if DEBUG_THREAD_DESTROY
889uint32_t   cycle;
890thread_t * this  = CURRENT_THREAD;
891#endif
892
893#if (DEBUG_THREAD_DESTROY & 1)
894cycle = (uint32_t)hal_get_cycles();
895if( DEBUG_THREAD_DESTROY < cycle )
896printk("\n[%s] thread[%x,%x] enter to destroy thread[%x,%x] / cycle %d\n",
897__FUNCTION__, this->process->pid, this->trdid, process->pid, thread->trdid, cycle );
898#endif
899
900    // check calling thread busylocks counter
901    thread_assert_can_yield( thread , __FUNCTION__ );
902
903#if CONFIG_INSTRUMENTATION_PGFAULTS
904process->vmm.false_pgfault_nr    += thread->info.false_pgfault_nr;
905process->vmm.false_pgfault_cost  += thread->info.false_pgfault_cost;
906process->vmm.local_pgfault_nr    += thread->info.local_pgfault_nr;
907process->vmm.local_pgfault_cost  += thread->info.local_pgfault_cost;
908process->vmm.global_pgfault_nr   += thread->info.global_pgfault_nr;
909process->vmm.global_pgfault_cost += thread->info.global_pgfault_cost;
910#endif
911
912#if (CONFIG_INSTRUMENTATION_PGFAULTS & 1)
913uint32_t false_nr    = thread->info.false_pgfault_nr;
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
918uint32_t local_nr    = thread->info.local_pgfault_nr;
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
923uint32_t global_nr   = thread->info.global_pgfault_nr;
924uint32_t global_cost = thread->info.global_pgfault_cost;
925uint32_t global_max  = thread->info.global_pgfault_max;
926uint32_t global_one  = global_nr ? (global_cost / global_nr) : 0;
927
928printk("\n***** thread[%x,%x] page faults\n"
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 );
936#endif
937
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
942    // remove thread from process th_tbl[]
943    count = process_remove_thread( thread );
944
945    // release memory allocated for CPU context and FPU context
946        hal_cpu_context_destroy( thread );
947        hal_fpu_context_destroy( thread );
948       
949    // release user stack vseg (for an user thread only)
950    if( type == THREAD_USER )  vmm_remove_vseg( process , thread->user_stack_vseg );
951
952    // release FPU ownership if required
953        hal_disable_irq( &save_sr );
954        if( core->fpu_owner == thread )
955        {
956                core->fpu_owner = NULL;
957                hal_fpu_disable();
958        }
959        hal_restore_irq( save_sr );
960
961    // invalidate thread descriptor
962        thread->signature = 0;
963
964    // release memory for thread descriptor (including kernel stack)
965    kmem_req_t   req;
966    req.type  = KMEM_PPM;
967    req.ptr   = thread;
968    kmem_free( &req );
969
970#if DEBUG_THREAD_DESTROY
971cycle = (uint32_t)hal_get_cycles();
972if( DEBUG_THREAD_DESTROY < cycle )
973printk("\n[%s] thread[%x,%x] exit / destroyed thread[%x,%x] / cycle %d\n",
974__FUNCTION__, this->process->pid, this->trdid, process->pid, thread->trdid, cycle );
975#endif
976
977    return count;
978
979}   // end thread_destroy()
980
981//////////////////////////////////////////////////
982inline void thread_set_req_ack( thread_t * target,
983                                uint32_t * rsp_count )
984{
985    reg_t    save_sr;   // for critical section
986
987    // get pointer on target thread scheduler
988    scheduler_t * sched = &target->core->scheduler;
989
990    // wait scheduler ready to handle a new request
991    while( sched->req_ack_pending ) asm volatile( "nop" );
992   
993    // enter critical section
994    hal_disable_irq( &save_sr );
995     
996    // set request in target thread scheduler
997    sched->req_ack_pending = true;
998
999    // set ack request in target thread "flags"
1000    hal_atomic_or( &target->flags , THREAD_FLAG_REQ_ACK );
1001
1002    // set pointer on responses counter in target thread
1003    target->ack_rsp_count = rsp_count;
1004   
1005    // exit critical section
1006    hal_restore_irq( save_sr );
1007
1008    hal_fence();
1009
1010}  // thread_set_req_ack()
1011
1012/////////////////////////////////////////////////////
1013inline void thread_reset_req_ack( thread_t * target )
1014{
1015    reg_t    save_sr;   // for critical section
1016
1017    // get pointer on target thread scheduler
1018    scheduler_t * sched = &target->core->scheduler;
1019
1020    // check signal pending in scheduler
1021    assert( __FUNCTION__, sched->req_ack_pending , "no pending signal" );
1022   
1023    // enter critical section
1024    hal_disable_irq( &save_sr );
1025     
1026    // reset signal in scheduler
1027    sched->req_ack_pending = false;
1028
1029    // reset signal in thread "flags"
1030    hal_atomic_and( &target->flags , ~THREAD_FLAG_REQ_ACK );
1031
1032    // reset pointer on responses counter
1033    target->ack_rsp_count = NULL;
1034   
1035    // exit critical section
1036    hal_restore_irq( save_sr );
1037
1038    hal_fence();
1039
1040}  // thread_reset_req_ack()
1041
1042//////////////////////////////////////
1043void thread_block( xptr_t   thread_xp,
1044                   uint32_t cause )
1045{
1046    // get thread cluster and local pointer
1047    cxy_t      cxy = GET_CXY( thread_xp );
1048    thread_t * ptr = GET_PTR( thread_xp );
1049
1050    // set blocking cause
1051    hal_remote_atomic_or( XPTR( cxy , &ptr->blocked ) , cause );
1052    hal_fence();
1053
1054#if DEBUG_THREAD_BLOCK
1055uint32_t    cycle   = (uint32_t)hal_get_cycles();
1056process_t * process = hal_remote_lpt( XPTR( cxy , &ptr->process ) );
1057thread_t  * this    = CURRENT_THREAD;
1058if( DEBUG_THREAD_BLOCK < cycle )
1059printk("\n[%s] thread[%x,%x] blocked thread %x in process %x / cause %x\n",
1060__FUNCTION__, this->process->pid, this->trdid,
1061ptr->trdid, hal_remote_l32(XPTR( cxy , &process->pid )), cause );
1062#endif
1063
1064} // end thread_block()
1065
1066////////////////////////////////////////////
1067uint32_t thread_unblock( xptr_t   thread_xp,
1068                         uint32_t cause )
1069{
1070    // get thread cluster and local pointer
1071    cxy_t      cxy = GET_CXY( thread_xp );
1072    thread_t * ptr = GET_PTR( thread_xp );
1073
1074    // reset blocking cause
1075    uint32_t previous = hal_remote_atomic_and( XPTR( cxy , &ptr->blocked ) , ~cause );
1076    hal_fence();
1077
1078#if DEBUG_THREAD_BLOCK
1079uint32_t    cycle   = (uint32_t)hal_get_cycles();
1080process_t * process = hal_remote_lpt( XPTR( cxy , &ptr->process ) );
1081thread_t  * this    = CURRENT_THREAD;
1082if( DEBUG_THREAD_BLOCK < cycle )
1083printk("\n[%s] thread[%x,%x] unblocked thread %x in process %x / cause %x\n",
1084__FUNCTION__, this->process->pid, this->trdid,
1085ptr->trdid, hal_remote_l32(XPTR( cxy , &process->pid )), cause );
1086#endif
1087
1088    // return a non zero value if the cause bit is modified
1089    return( previous & cause );
1090
1091}  // end thread_unblock()
1092
1093//////////////////////////////////////
1094void thread_delete_request( xptr_t  target_xp,
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
1104    process_t * target_process;         // pointer on target process
1105    pid_t       target_pid;             // target process identifier
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
1111    uint32_t    target_flags;           // target thread flags
1112    xptr_t      joining_xp;             // extended pointer on joining thread
1113    thread_t  * joining_ptr;            // local pointer on joining thread
1114    cxy_t       joining_cxy;            // joining thread cluster
1115
1116    // get target thread cluster and local pointer
1117    target_cxy      = GET_CXY( target_xp );
1118    target_ptr      = GET_PTR( target_xp );
1119
1120    // get target thread trdid, ltid, flags, and process PID
1121    target_trdid    = hal_remote_l32( XPTR( target_cxy , &target_ptr->trdid ) );
1122    target_ltid     = LTID_FROM_TRDID( target_trdid );
1123    target_flags_xp = XPTR( target_cxy , &target_ptr->flags );
1124    target_flags    = hal_remote_l32( target_flags_xp );
1125    target_process  = hal_remote_lpt( XPTR( target_cxy , &target_ptr->process ) );
1126    target_pid      = hal_remote_l32( XPTR( target_cxy , &target_process->pid ) );
1127    target_attached = ((target_flags & THREAD_FLAG_DETACHED) == 0); 
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
1134uint32_t cycle  = (uint32_t)hal_get_cycles();
1135if( DEBUG_THREAD_DELETE < cycle )
1136printk("\n[%s] killer[%x,%x] enters / target[%x,%x] / forced %d / flags %x / cycle %d\n",
1137__FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid, 
1138target_pid, target_trdid, is_forced, target_flags, cycle );
1139#endif
1140
1141// check target thread is not the main thread, because the main thread
1142// must be deleted by the parent process sys_wait() function
1143assert( __FUNCTION__, ((CXY_FROM_PID( target_pid ) != target_cxy) || (target_ltid != 0)),
1144"target thread cannot be the main thread" );
1145
1146    // check killer thread can yield
1147    thread_assert_can_yield( killer_ptr , __FUNCTION__ ); 
1148
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
1153    {
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
1162        remote_busylock_acquire( target_join_lock_xp );
1163
1164        // get join_done from target thread descriptor
1165        target_join_done = ((hal_remote_l32( target_flags_xp ) & THREAD_FLAG_JOIN_DONE) != 0);
1166   
1167        if( target_join_done )                     // joining thread arrived first
1168        {
1169            // get extended pointer on joining thread
1170            joining_xp  = (xptr_t)hal_remote_l64( target_join_xp_xp );
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 );
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
1188            remote_busylock_release( target_join_lock_xp );
1189
1190            // block the target thread
1191            thread_block( target_xp , THREAD_BLOCKED_GLOBAL );
1192
1193            // set the REQ_DELETE flag in target thread descriptor
1194            hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
1195
1196            // exit critical section
1197            hal_restore_irq( save_sr );
1198
1199#if DEBUG_THREAD_DELETE
1200cycle  = (uint32_t)hal_get_cycles();
1201if( DEBUG_THREAD_DELETE < cycle )
1202printk("\n[%s] killer[%x,%x] exit / target[%x,%x] marked after join / cycle %d\n",
1203__FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid,
1204target_pid, target_trdid, cycle );
1205#endif
1206
1207        }
1208        else                                      // killer thread arrived first
1209        {
1210            // set the kill_done flag in target thread
1211            hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_KILL_DONE );
1212
1213            // block target thread on BLOCKED_JOIN
1214            thread_block( killer_xp , THREAD_BLOCKED_JOIN );
1215
1216            // set extended pointer on killer thread in target thread
1217            hal_remote_s64( target_join_xp_xp , killer_xp );
1218
1219            // release the join_lock in target thread descriptor
1220            remote_busylock_release( target_join_lock_xp );
1221
1222#if DEBUG_THREAD_DELETE
1223cycle  = (uint32_t)hal_get_cycles();
1224if( DEBUG_THREAD_DELETE < cycle )
1225printk("\n[%s] killer[%x,%x] deschedules / target[%x,%x] not completed / cycle %d\n",
1226__FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid,
1227target_pid, target_trdid, cycle );
1228#endif
1229            // deschedule
1230            sched_yield( "killer thread wait joining thread" );
1231
1232            // block the target thread
1233            thread_block( target_xp , THREAD_BLOCKED_GLOBAL );
1234
1235            // set the REQ_DELETE flag in target thread descriptor
1236            hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
1237
1238            // exit critical section
1239            hal_restore_irq( save_sr );
1240
1241#if DEBUG_THREAD_DELETE
1242cycle  = (uint32_t)hal_get_cycles();
1243if( DEBUG_THREAD_DELETE < cycle )
1244printk("\n[%s] killer[%x,%x] exit / target[%x,%x] marked after join / cycle %d\n",
1245__FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid,
1246target_pid, target_trdid, cycle );
1247#endif
1248
1249        }
1250    }
1251    else                     // no synchronization with joining thread required
1252    {
1253        // block the target thread
1254        thread_block( target_xp , THREAD_BLOCKED_GLOBAL );
1255
1256        // set the REQ_DELETE flag in target thread descriptor
1257        hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
1258
1259#if DEBUG_THREAD_DELETE
1260cycle  = (uint32_t)hal_get_cycles();
1261if( DEBUG_THREAD_DELETE < cycle )
1262printk("\n[%s] killer[%x,%x] exit / target [%x,%x] marked / no join / cycle %d\n",
1263__FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid,
1264target_pid, target_trdid, cycle );
1265#endif
1266
1267    }
1268}  // end thread_delete_request()
1269
1270
1271
1272/////////////////////////////
1273void thread_idle_func( void )
1274{
1275
1276#if DEBUG_THREAD_IDLE
1277uint32_t cycle;
1278#endif
1279
1280    while( 1 )
1281    {
1282        // unmask IRQs
1283        hal_enable_irq( NULL );
1284
1285        // force core to low-power mode (optional)
1286        if( CONFIG_SCHED_IDLE_MODE_SLEEP ) 
1287        {
1288
1289#if DEBUG_THREAD_IDLE
1290cycle = (uint32_t)hal_get_cycles();
1291if( DEBUG_THREAD_IDLE < cycle )
1292printk("\n[%s] idle thread on core[%x,%d] goes to sleep / cycle %d\n",
1293__FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, cycle );
1294#endif
1295
1296            hal_core_sleep();
1297
1298#if DEBUG_THREAD_IDLE
1299cycle = (uint32_t)hal_get_cycles();
1300if( DEBUG_THREAD_IDLE < cycle )
1301printk("\n[%s] idle thread on core[%x,%d] wake up / cycle %d\n",
1302__FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, cycle );
1303#endif
1304
1305        }
1306
1307#if DEBUG_THREAD_IDLE
1308cycle = (uint32_t)hal_get_cycles();
1309if( DEBUG_THREAD_IDLE < cycle )
1310sched_remote_display( local_cxy , CURRENT_THREAD->core->lid );
1311#endif     
1312        // search a runable thread
1313        sched_yield( "running idle thread" );
1314
1315    } // end while
1316
1317}  // end thread_idle()
1318
1319
1320///////////////////////////////////////////
1321void thread_time_update( thread_t * thread,
1322                         bool_t     is_user )
1323{
1324    cycle_t current_cycle;   // current cycle counter value
1325    cycle_t last_cycle;      // last cycle counter value
1326
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);
1342
1343}  // end thread_time_update()
1344
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
1351    thread_t    * target_thread_ptr;   // target thread local pointer
1352    xptr_t        target_process_xp;   // extended pointer on target process descriptor
1353    process_t   * target_process_ptr;  // local pointer on target process descriptor
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
1357
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 )
1362printk("\n[%s] thread %x in process %x enters / pid %x / trdid %x / cycle %d\n",
1363__FUNCTION__, this->trdid, this->process->pid, pid, trdid, cycle );
1364#endif
1365
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
1370    // check trdid argument
1371        if( (target_thread_ltid >= CONFIG_THREADS_MAX_PER_CLUSTER) || 
1372        cluster_is_active( target_cxy ) == false )                return XPTR_NULL;
1373
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
1379    // get extended pointer on lock protecting the list of local processes
1380    lock_xp = XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_lock );
1381
1382    // take the lock protecting the list of processes in target cluster
1383    remote_queuelock_acquire( lock_xp );
1384
1385#if( DEBUG_THREAD_GET_XPTR & 1 )
1386if( DEBUG_THREAD_GET_XPTR < cycle )
1387printk("\n[%s] scan processes in cluster %x :\n", __FUNCTION__, target_cxy );
1388#endif
1389
1390    // scan the list of local processes in target cluster
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 );
1396        target_process_ptr = GET_PTR( target_process_xp );
1397        target_process_pid = hal_remote_l32( XPTR( target_cxy , &target_process_ptr->pid ) );
1398
1399#if( DEBUG_THREAD_GET_XPTR & 1 )
1400if( DEBUG_THREAD_GET_XPTR < cycle )
1401printk(" - process %x\n", target_process_pid );
1402#endif
1403
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
1412    remote_queuelock_release( lock_xp );
1413
1414    // check PID found
1415    if( found == false ) 
1416    {
1417
1418#if( DEBUG_THREAD_GET_XPTR & 1 )
1419if( DEBUG_THREAD_GET_XPTR < cycle )
1420printk("\n[%s] pid %x not found in cluster %x\n",
1421__FUNCTION__, pid, target_cxy );
1422#endif
1423        return XPTR_NULL;
1424    }
1425
1426    // get target thread local pointer
1427    xptr_t xp = XPTR( target_cxy , &target_process_ptr->th_tbl[target_thread_ltid] );
1428    target_thread_ptr = (thread_t *)hal_remote_lpt( xp );
1429
1430    if( target_thread_ptr == NULL )
1431    {
1432
1433#if( DEBUG_THREAD_GET_XPTR & 1 )
1434if( DEBUG_THREAD_GET_XPTR < cycle )
1435printk("\n[%s] thread %x not registered in process %x in cluster %x\n",
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 )
1444printk("\n[%s] thread %x in process %x exit / pid %x / trdid %x / cycle %d\n",
1445__FUNCTION__, this->trdid, this->process->pid, pid, trdid, cycle );
1446#endif
1447
1448    return XPTR( target_cxy , target_thread_ptr );
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
1472        nolock_printk("\n[PANIC] in %s / thread[%x,%x] cannot yield : "
1473        "hold %d busylock(s) / cycle %d\n",
1474        func_str, thread->process->pid, thread->trdid,
1475        thread->busylocks - 1, (uint32_t)hal_get_cycles() );
1476
1477#if DEBUG_BUSYLOCK
1478
1479// scan list of busylocks
1480xptr_t    iter_xp;
1481xptr_t    root_xp  = XPTR( local_cxy , &thread->busylocks_root );
1482XLIST_FOREACH( root_xp , iter_xp )
1483{
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}
1490
1491#endif
1492
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
1501//////////////////////////////////////////////////////
1502void thread_display_busylocks( xptr_t       thread_xp,
1503                               const char * string )
1504{
1505
1506    cxy_t      thread_cxy = GET_CXY( thread_xp );
1507    thread_t * thread_ptr = GET_PTR( thread_xp );
1508
1509#if DEBUG_BUSYLOCK
1510
1511    xptr_t     iter_xp;
1512
1513    // get relevant info from target thread descriptor
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 ) );
1518
1519    // get extended pointer on root of busylocks
1520    xptr_t root_xp = XPTR( thread_cxy , &thread_ptr->busylocks_root );
1521
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 );
1526
1527    // get extended pointer on remote TXT0 lock
1528    xptr_t  txt0_lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
1529
1530    // get TXT0 lock
1531    remote_busylock_acquire( txt0_lock_xp );
1532
1533    // display header
1534    nolock_printk("\n***** thread[%x,%x] in <%s> : %d busylocks *****\n",
1535    pid, trdid, string, locks );
1536
1537    // scan the xlist of busylocks when required
1538    if( locks )
1539    {
1540        XLIST_FOREACH( root_xp , iter_xp )
1541        {
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 );
1547        }
1548    }
1549
1550    // release TXT0 lock
1551    remote_busylock_release( txt0_lock_xp );
1552
1553#else
1554
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
1558#endif
1559
1560    return;
1561
1562}  // end thread_display_busylock()
1563
Note: See TracBrowser for help on using the repository browser.