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

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

Introduce the ksocket.h & ksocket.c files in kernel/kern.

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