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

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

cosmetic

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