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

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

Remove all RPCs in page-fault handling.

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