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

Last change on this file since 407 was 407, checked in by alain, 6 years ago

First implementation of fork/exec.

File size: 26.8 KB
Line 
1/*
2 * thread.c -  implementation of thread operations (user & kernel)
3 *
4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *         Alain Greiner (2016,2017)
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_types.h>
27#include <hal_context.h>
28#include <hal_irqmask.h>
29#include <hal_special.h>
30#include <hal_remote.h>
31#include <memcpy.h>
32#include <printk.h>
33#include <cluster.h>
34#include <process.h>
35#include <scheduler.h>
36#include <dev_pic.h>
37#include <core.h>
38#include <list.h>
39#include <xlist.h>
40#include <page.h>
41#include <kmem.h>
42#include <ppm.h>
43#include <thread.h>
44
45//////////////////////////////////////////////////////////////////////////////////////
46// Extern global variables
47//////////////////////////////////////////////////////////////////////////////////////
48
49extern process_t      process_zero;
50
51//////////////////////////////////////////////////////////////////////////////////////
52// This function returns a printable string for the thread type.
53//////////////////////////////////////////////////////////////////////////////////////
54char * thread_type_str( uint32_t type )
55{
56    if     ( type == THREAD_USER   ) return "USR";
57    else if( type == THREAD_RPC    ) return "RPC";
58    else if( type == THREAD_DEV    ) return "DEV";
59    else if( type == THREAD_IDLE   ) return "IDL";
60    else                             return "undefined";
61}
62
63/////////////////////////////////////////////////////////////////////////////////////
64// This static function allocates physical memory for a thread descriptor.
65// It can be called by the three functions:
66// - thread_user_create()
67// - thread_user_fork()
68// - thread_kernel_create()
69/////////////////////////////////////////////////////////////////////////////////////
70// @ return pointer on thread descriptor if success / return NULL if failure.
71/////////////////////////////////////////////////////////////////////////////////////
72static thread_t * thread_alloc()
73{
74        page_t       * page;   // pointer on page descriptor containing thread descriptor
75        kmem_req_t     req;    // kmem request
76
77        // allocates memory for thread descriptor + kernel stack
78        req.type  = KMEM_PAGE;
79        req.size  = CONFIG_THREAD_DESC_ORDER;
80        req.flags = AF_KERNEL | AF_ZERO;
81        page      = kmem_alloc( &req );
82
83        if( page == NULL ) return NULL;
84
85    // return pointer on new thread descriptor
86    xptr_t base_xp = ppm_page2base( XPTR(local_cxy , page ) );
87    return (thread_t *)GET_PTR( base_xp );
88
89}  // end thread_alloc()
90 
91
92/////////////////////////////////////////////////////////////////////////////////////
93// This static function releases the physical memory for a thread descriptor.
94// It is called by the three functions:
95// - thread_user_create()
96// - thread_user_fork()
97// - thread_kernel_create()
98/////////////////////////////////////////////////////////////////////////////////////
99// @ thread  : pointer on thread descriptor.
100/////////////////////////////////////////////////////////////////////////////////////
101static void thread_release( thread_t * thread )
102{
103    kmem_req_t   req;
104
105    xptr_t base_xp = ppm_base2page( XPTR(local_cxy , thread ) );
106
107    req.type  = KMEM_PAGE;
108    req.ptr   = GET_PTR( base_xp );
109    kmem_free( &req );
110}
111
112/////////////////////////////////////////////////////////////////////////////////////
113// This static function initializes a thread descriptor (kernel or user).
114// It can be called by the four functions:
115// - thread_user_create()
116// - thread_user_fork()
117// - thread_kernel_create()
118// - thread_user_init()
119/////////////////////////////////////////////////////////////////////////////////////
120// @ thread       : pointer on thread descriptor
121// @ process      : pointer on process descriptor.
122// @ type         : thread type.
123// @ func         : pointer on thread entry function.
124// @ args         : pointer on thread entry function arguments.
125// @ core_lid     : target core local index.
126// @ u_stack_base : stack base (user thread only)
127// @ u_stack_size : stack base (user thread only)
128/////////////////////////////////////////////////////////////////////////////////////
129static error_t thread_init( thread_t      * thread,
130                            process_t     * process,
131                            thread_type_t   type,
132                            void          * func,
133                            void          * args,
134                            lid_t           core_lid,
135                            intptr_t        u_stack_base,
136                            uint32_t        u_stack_size )
137{
138    error_t        error;
139    trdid_t        trdid;      // allocated thread identifier
140
141        cluster_t    * local_cluster = LOCAL_CLUSTER;
142
143    // register new thread in process descriptor, and get a TRDID
144    spinlock_lock( &process->th_lock );
145    error = process_register_thread( process, thread , &trdid );
146    spinlock_unlock( &process->th_lock );
147
148    if( error )
149    {
150        printk("\n[ERROR] in %s : cannot get TRDID\n", __FUNCTION__ );
151        return EINVAL;
152    }
153
154    // compute thread descriptor size without kernel stack
155    uint32_t desc_size = (intptr_t)(&thread->signature) - (intptr_t)thread + 4; 
156
157        // Initialize new thread descriptor
158    thread->trdid           = trdid;
159        thread->type            = type;
160    thread->quantum         = 0;            // TODO
161    thread->ticks_nr        = 0;            // TODO
162    thread->time_last_check = 0;
163        thread->core            = &local_cluster->core_tbl[core_lid];
164        thread->process         = process;
165
166    thread->local_locks     = 0;
167    list_root_init( &thread->locks_root );
168
169    thread->remote_locks    = 0;
170    xlist_root_init( XPTR( local_cxy , &thread->xlocks_root ) );
171
172    thread->u_stack_base    = u_stack_base;
173    thread->u_stack_size    = u_stack_size;
174    thread->k_stack_base    = (intptr_t)thread + desc_size;
175    thread->k_stack_size    = CONFIG_THREAD_DESC_SIZE - desc_size;
176
177    thread->entry_func      = func;         // thread entry point
178    thread->entry_args      = args;         // thread function arguments
179    thread->flags           = 0;            // all flags reset
180    thread->signals         = 0;            // no pending signal
181    thread->errno           = 0;            // no error detected
182    thread->fork_user       = 0;            // no user defined placement for fork
183    thread->fork_cxy        = 0;            // user defined target cluster for fork
184
185    // thread blocked
186    thread->blocked = THREAD_BLOCKED_GLOBAL;
187
188    // reset children list
189    xlist_root_init( XPTR( local_cxy , &thread->children_root ) );
190    thread->children_nr = 0;
191
192    // reset sched list and brothers list
193    list_entry_init( &thread->sched_list );
194    xlist_entry_init( XPTR( local_cxy , &thread->brothers_list ) );
195
196    // reset thread info
197    memset( &thread->info , 0 , sizeof(thread_info_t) );
198
199    // initialise signature
200        thread->signature = THREAD_SIGNATURE;
201
202    // update local DQDT
203    dqdt_local_update_threads( 1 );
204
205    // register new thread in core scheduler
206    sched_register_thread( thread->core , thread );
207
208        return 0;
209
210} // end thread_init()
211
212/////////////////////////////////////////////////////////
213error_t thread_user_create( pid_t             pid,
214                            void            * start_func,
215                            void            * start_arg,
216                            pthread_attr_t  * attr,
217                            thread_t       ** new_thread )
218{
219    error_t        error;
220        thread_t     * thread;       // pointer on created thread descriptor
221    process_t    * process;      // pointer to local process descriptor
222    lid_t          core_lid;     // selected core local index
223    vseg_t       * vseg;         // stack vseg
224
225    assert( (attr != NULL) , __FUNCTION__, "pthread attributes must be defined" );
226
227    // get process descriptor local copy
228    process = process_get_local_copy( pid );
229
230    if( process == NULL )
231    {
232                printk("\n[ERROR] in %s : cannot get process descriptor %x\n",
233               __FUNCTION__ , pid );
234        return ENOMEM;
235    }
236
237    // select a target core in local cluster
238    if( attr->attributes & PT_ATTR_CORE_DEFINED )
239    {
240        core_lid = attr->lid;
241        if( core_lid >= LOCAL_CLUSTER->cores_nr )
242        {
243                printk("\n[ERROR] in %s : illegal core index attribute = %d\n",
244            __FUNCTION__ , core_lid );
245            return EINVAL;
246        }
247    }
248    else
249    {
250        core_lid = cluster_select_local_core();
251    }
252
253    // allocate a stack from local VMM
254    vseg = vmm_create_vseg( process,
255                            VSEG_TYPE_STACK,
256                            0,                 // size unused
257                            0,                 // length unused
258                            0,                 // file_offset unused
259                            0,                 // file_size unused
260                            XPTR_NULL,         // mapper_xp unused
261                            local_cxy );
262
263    if( vseg == NULL )
264    {
265            printk("\n[ERROR] in %s : cannot create stack vseg\n", __FUNCTION__ );
266                return ENOMEM;
267    }
268
269    // allocate memory for thread descriptor
270    thread = thread_alloc();
271
272    if( thread == NULL )
273    {
274            printk("\n[ERROR] in %s : cannot create new thread\n", __FUNCTION__ );
275        vmm_remove_vseg( vseg );
276        return ENOMEM;
277    }
278
279    // initialize thread descriptor
280    error = thread_init( thread,
281                         process,
282                         THREAD_USER,
283                         start_func,
284                         start_arg,
285                         core_lid,
286                         vseg->min,
287                         vseg->max - vseg->min );
288
289    if( error )
290    {
291            printk("\n[ERROR] in %s : cannot initialize new thread\n", __FUNCTION__ );
292        vmm_remove_vseg( vseg );
293        thread_release( thread );
294        return EINVAL;
295    }
296
297    // set LOADABLE flag
298    thread->flags = THREAD_FLAG_LOADABLE;
299
300    // set DETACHED flag if required
301    if( attr->attributes & PT_ATTR_DETACH ) 
302    {
303        thread->flags |= THREAD_FLAG_DETACHED;
304    }
305
306    // allocate & initialize CPU context
307        if( hal_cpu_context_create( thread ) )
308    {
309            printk("\n[ERROR] in %s : cannot create CPU context\n", __FUNCTION__ );
310        vmm_remove_vseg( vseg );
311        thread_release( thread );
312        return ENOMEM;
313    }
314
315    // allocate  FPU context
316    if( hal_fpu_context_alloc( thread ) )
317    {
318            printk("\n[ERROR] in %s : cannot create FPU context\n", __FUNCTION__ );
319        vmm_remove_vseg( vseg );
320        thread_release( thread );
321        return ENOMEM;
322    }
323
324thread_dmsg("\n[DBG] %s : core[%x,%d] exit / trdid = %x / process %x / core = %d\n",
325__FUNCTION__, local_cxy, CURRENT_THREAD->core->lid,
326thread->trdid , process->pid , core_lid );
327
328    *new_thread = thread;
329        return 0;
330
331}  // end thread_user_create()
332
333////////////////////////////////////////////////////
334error_t thread_user_fork( process_t * process,
335                          intptr_t    stack_base,
336                          uint32_t    stack_size,
337                          thread_t ** new_thread )
338{
339    error_t        error;
340        thread_t     * child;       // pointer on new thread descriptor
341    lid_t          core_lid;     // selected core local index
342
343thread_dmsg("\n[DBG] %s : core[%x,%d] enters\n",
344__FUNCTION__ , local_cxy , core_lid );
345
346    // select a target core in local cluster
347    core_lid = cluster_select_local_core();
348
349    // get pointer on parent thread descriptor
350    thread_t * parent = CURRENT_THREAD;
351
352    // allocate memory for new thread descriptor
353    child = thread_alloc();
354
355    if( child == NULL )
356    {
357        printk("\n[ERROR] in %s : cannot allocate new thread\n", __FUNCTION__ );
358        return ENOMEM;
359    }
360
361    // initialize thread descriptor
362    error = thread_init( child,
363                         process,
364                         THREAD_USER,
365                         parent->entry_func,
366                         parent->entry_args,
367                         core_lid,
368                         stack_base,
369                         stack_size );
370
371    if( error )
372    {
373            printk("\n[ERROR] in %s : cannot initialize new thread\n", __FUNCTION__ );
374        thread_release( child );
375        return EINVAL;
376    }
377
378    // return child pointer
379    *new_thread = child;
380
381    // set DETACHED flag if required
382    if( parent->flags & THREAD_FLAG_DETACHED ) child->flags = THREAD_FLAG_DETACHED;
383
384    // allocate CPU context for child thread
385        if( hal_cpu_context_alloc( child ) )
386    {
387            printk("\n[ERROR] in %s : cannot allocate CPU context\n", __FUNCTION__ );
388        thread_release( child );
389        return ENOMEM;
390    }
391
392    // allocate FPU context for child thread
393        if( hal_fpu_context_alloc( child ) )
394    {
395            printk("\n[ERROR] in %s : cannot allocate FPU context\n", __FUNCTION__ );
396        thread_release( child );
397        return ENOMEM;
398    }
399
400    // copy kernel stack content from parent to child thread descriptor
401    void * dst = (void *)(&child->signature) + 4;
402    void * src = (void *)(&parent->signature) + 4;
403    memcpy( dst , src , parent->k_stack_size );
404
405thread_dmsg("\n[DBG] %s : core[%x,%d] exit / created main thread %x for process %x\n",
406__FUNCTION__, local_cxy , core_lid , child->trdid , process->pid );
407
408        return 0;
409
410}  // end thread_user_fork()
411
412/////////////////////////////////////////////////////////
413error_t thread_kernel_create( thread_t     ** new_thread,
414                              thread_type_t   type,
415                              void          * func,
416                              void          * args,
417                                              lid_t           core_lid )
418{
419    error_t        error;
420        thread_t     * thread;       // pointer on new thread descriptor
421
422thread_dmsg("\n[DBG] %s : core[%x,%d] enters / type % / cycle %d\n",
423__FUNCTION__ , local_cxy , core_lid , thread_type_str( type ) , hal_time_stamp() );
424
425    assert( ( (type == THREAD_IDLE) || (type == THREAD_RPC) || (type == THREAD_DEV) ) ,
426    __FUNCTION__ , "illegal thread type" );
427
428    assert( (core_lid < LOCAL_CLUSTER->cores_nr) ,
429            __FUNCTION__ , "illegal core_lid" );
430
431    // allocate memory for new thread descriptor
432    thread = thread_alloc();
433
434    if( thread == NULL ) return ENOMEM;
435
436    // initialize thread descriptor
437    error = thread_init( thread,
438                         &process_zero,
439                         type,
440                         func,
441                         args,
442                         core_lid,
443                         0 , 0 );  // no user stack for a kernel thread
444
445    if( error ) // release allocated memory for thread descriptor
446    {
447        thread_release( thread );
448        return EINVAL;
449    }
450
451    // allocate & initialize CPU context
452        hal_cpu_context_create( thread );
453
454thread_dmsg("\n[DBG] %s : core = [%x,%d] exit / trdid = %x / type %s / cycle %d\n",
455__FUNCTION__, local_cxy, core_lid, thread->trdid, thread_type_str(type), hal_time_stamp() );
456
457    *new_thread = thread;
458        return 0;
459
460} // end thread_kernel_create()
461
462///////////////////////////////////////////////////
463error_t thread_kernel_init( thread_t      * thread,
464                            thread_type_t   type,
465                            void          * func,
466                            void          * args,
467                                            lid_t           core_lid )
468{
469    assert( (type == THREAD_IDLE) , __FUNCTION__ , "illegal thread type" );
470
471    assert( (core_lid < LOCAL_CLUSTER->cores_nr) , __FUNCTION__ , "illegal core index" );
472
473    error_t  error = thread_init( thread,
474                                  &process_zero,
475                                  type,
476                                  func,
477                                  args,
478                                  core_lid,
479                                  0 , 0 );   // no user stack for a kernel thread
480
481    // allocate & initialize CPU context if success
482    if( error == 0 ) hal_cpu_context_create( thread );
483
484    return error;
485
486}  // end thread_kernel_init()
487
488///////////////////////////////////////////////////////////////////////////////////////
489// TODO: check that all memory dynamically allocated during thread execution
490// has been released, using a cache of mmap and malloc requests. [AG]
491///////////////////////////////////////////////////////////////////////////////////////
492void thread_destroy( thread_t * thread )
493{
494        uint32_t     tm_start;
495        uint32_t     tm_end;
496    reg_t        state;
497
498    process_t  * process    = thread->process;
499    core_t     * core       = thread->core;
500
501    thread_dmsg("\n[DBG] %s : enters for thread %x in process %x / type = %s\n",
502                __FUNCTION__ , thread->trdid , process->pid , thread_type_str( thread->type ) );
503
504    assert( (thread->children_nr == 0) , __FUNCTION__ , "still attached children" );
505
506    assert( (thread->local_locks == 0) , __FUNCTION__ , "all local locks not released" );
507
508    assert( (thread->remote_locks == 0) , __FUNCTION__ , "all remote locks not released" );
509
510        tm_start = hal_get_cycles();
511
512    // update intrumentation values
513    uint32_t pgfaults = thread->info.pgfault_nr;
514    uint32_t u_errors = thread->info.u_err_nr;
515    uint32_t m_errors = thread->info.m_err_nr;
516
517        process->vmm.pgfault_nr += pgfaults;
518        process->vmm.u_err_nr   += u_errors;
519        process->vmm.m_err_nr   += m_errors;
520
521    // release memory allocated for CPU context and FPU context
522        hal_cpu_context_destroy( thread );
523        hal_fpu_context_destroy( thread );
524       
525    // release FPU if required
526    // TODO This should be done before calling thread_destroy()
527        hal_disable_irq( &state );
528        if( core->fpu_owner == thread )
529        {
530                core->fpu_owner = NULL;
531                hal_fpu_disable();
532        }
533        hal_restore_irq( state );
534
535    // remove thread from process th_tbl[]
536    // TODO This should be done before calling thread_destroy()
537    ltid_t ltid = LTID_FROM_TRDID( thread->trdid );
538
539        spinlock_lock( &process->th_lock );
540        process->th_tbl[ltid] = XPTR_NULL;
541        process->th_nr--;
542        spinlock_unlock( &process->th_lock );
543       
544    // update local DQDT
545    dqdt_local_update_threads( -1 );
546
547    // invalidate thread descriptor
548        thread->signature = 0;
549
550    // release memory for thread descriptor
551    thread_release( thread );
552
553        tm_end = hal_get_cycles();
554
555        thread_dmsg("\n[DBG] %s : exit for thread %x in process %x / duration = %d\n",
556                       __FUNCTION__, thread->trdid , process->pid , tm_end - tm_start );
557
558}   // end thread_destroy()
559
560/////////////////////////////////////////////////
561void thread_child_parent_link( xptr_t  xp_parent,
562                               xptr_t  xp_child )
563{
564    // get extended pointers on children list root
565    cxy_t      parent_cxy = GET_CXY( xp_parent );
566    thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent );
567    xptr_t     root       = XPTR( parent_cxy , &parent_ptr->children_root );
568
569    // get extended pointer on children list entry
570    cxy_t      child_cxy  = GET_CXY( xp_child );
571    thread_t * child_ptr  = (thread_t *)GET_PTR( xp_child );
572    xptr_t     entry      = XPTR( child_cxy , &child_ptr->brothers_list );
573
574    // set the link
575    xlist_add_first( root , entry );
576    hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr ) , 1 );
577}
578
579///////////////////////////////////////////////////
580void thread_child_parent_unlink( xptr_t  xp_parent,
581                                 xptr_t  xp_child )
582{
583    // get extended pointer on children list lock
584    cxy_t      parent_cxy = GET_CXY( xp_parent );
585    thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent );
586    xptr_t     lock       = XPTR( parent_cxy , &parent_ptr->children_lock );
587
588    // get extended pointer on children list entry
589    cxy_t      child_cxy  = GET_CXY( xp_child );
590    thread_t * child_ptr  = (thread_t *)GET_PTR( xp_child );
591    xptr_t     entry      = XPTR( child_cxy , &child_ptr->brothers_list );
592
593    // get the lock
594    remote_spinlock_lock( lock );
595
596    // remove the link
597    xlist_unlink( entry );
598    hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr ) , -1 );
599
600    // release the lock
601    remote_spinlock_unlock( lock );
602}
603
604/////////////////////////////////////////////////
605inline void thread_set_signal( thread_t * thread,
606                               uint32_t   mask )
607{
608    hal_atomic_or( &thread->signals , mask );
609    hal_fence();
610}
611
612///////////////////////////////////////////////////
613inline void thread_reset_signal( thread_t * thread,
614                                 uint32_t   mask )
615{
616    hal_atomic_and( &thread->signals , ~mask );
617    hal_fence();
618}
619
620////////////////////////////////
621inline bool_t thread_can_yield()
622{
623    thread_t * this = CURRENT_THREAD;
624    return (this->local_locks == 0) && (this->remote_locks == 0);
625}
626
627/////////////////////////
628void thread_check_sched()
629{
630    thread_t * this = CURRENT_THREAD;
631
632        if( (this->local_locks == 0) && 
633        (this->remote_locks == 0) &&
634        (this->flags & THREAD_FLAG_SCHED) ) 
635    {
636        this->flags &= ~THREAD_FLAG_SCHED;
637        sched_yield();
638    }
639
640}  // end thread_check_sched()
641
642/////////////////////////////////////
643void thread_block( thread_t * thread,
644                   uint32_t   cause )
645{
646    // set blocking cause
647    hal_atomic_or( &thread->blocked , cause );
648    hal_fence();
649
650} // end thread_block()
651
652/////////////////////////////////////////
653uint32_t thread_unblock( xptr_t   thread,
654                         uint32_t cause )
655{
656    // get thread cluster and local pointer
657    cxy_t      cxy = GET_CXY( thread );
658    thread_t * ptr = (thread_t *)GET_PTR( thread );
659
660    // reset blocking cause
661    uint32_t previous = hal_remote_atomic_and( XPTR( cxy , &ptr->blocked ) , ~cause );
662    hal_fence();
663
664    // return a non zero value if the cause bit is modified
665    return( previous & cause );
666
667}  // end thread_unblock()
668
669/////////////////////
670error_t thread_exit()
671{
672    reg_t      sr_save;
673
674        thread_t * this = CURRENT_THREAD;
675
676    // test if this thread can be descheduled
677        if( !thread_can_yield() )
678        {
679        printk("ERROR in %s : locks not released for thread %x in process %x on core[%x,%d]\n",
680        __FUNCTION__, this->trdid, this->process->pid, local_cxy, this->core->lid );
681        return EINVAL;
682    }
683
684    if( this->flags & THREAD_FLAG_DETACHED )
685    {
686        // if detached set signal and set blocking cause atomically
687        hal_disable_irq( &sr_save );
688        thread_set_signal( this , THREAD_SIG_KILL );
689        thread_block( this , THREAD_BLOCKED_EXIT );
690        hal_restore_irq( sr_save );
691    }
692    else
693    {
694        // if attached, set blocking cause
695        thread_block( this , THREAD_BLOCKED_EXIT );
696    }
697
698    // deschedule
699    sched_yield();
700    return 0;
701
702}  // end thread_exit()
703
704/////////////////////////////////////
705void thread_kill( thread_t * target )
706{
707    // set SIG_KILL signal in target thread descriptor
708    thread_set_signal( target , THREAD_SIG_KILL );
709
710    // set the global blocked bit in target thread descriptor.
711    thread_block( target , THREAD_BLOCKED_GLOBAL );
712
713    // send an IPI to schedule the target thread core.
714    dev_pic_send_ipi( local_cxy , target->core->lid );
715
716}  // end thread_kill()
717
718///////////////////////
719void thread_idle_func()
720{
721    while( 1 )
722    {
723        if( CONFIG_THREAD_IDLE_MODE_SLEEP ) // force core to low-power mode
724        {
725
726idle_dmsg("\n[DBG] %s : core[%x][%d] goes to sleep at cycle %d\n",
727__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , hal_get_cycles() );
728
729            hal_core_sleep();
730
731idle_dmsg("\n[DBG] %s : core[%x][%d] wake up at cycle %d\n",
732__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , hal_get_cycles() );
733
734        }
735        else                                // yield each ~ 100000 cycles
736
737        {
738             hal_fixed_delay( 500000 );
739        }
740
741        // force scheduling at each iteration
742        sched_yield();
743   }
744}  // end thread_idle()
745
746
747/////////////////////////////////////////////////
748void thread_user_time_update( thread_t * thread )
749{
750    // TODO
751    // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ );
752}
753
754///////////////////////////////////////////////////
755void thread_kernel_time_update( thread_t * thread )
756{
757    // TODO
758    // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ );
759}
760
761////////////////////////////////////////////////
762void thread_signals_handle( thread_t * thread )
763{
764    // TODO
765    // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ );
766}
767
768/////////////////////////////////////
769xptr_t thread_get_xptr( pid_t    pid,
770                        trdid_t  trdid )
771{
772    cxy_t         target_cxy;          // target thread cluster identifier
773    ltid_t        target_thread_ltid;  // target thread local index
774    thread_t    * target_thread_ptr;   // target thread local pointer
775    xptr_t        target_process_xp;   // extended pointer on target process descriptor
776    process_t   * target_process_ptr;  // local pointer on target process descriptor
777    pid_t         target_process_pid;  // target process identifier
778    xlist_entry_t root;                // root of list of process in target cluster
779    xptr_t        lock_xp;             // extended pointer on lock protecting  this list
780
781    // get target cluster identifier and local thread identifier
782    target_cxy         = CXY_FROM_TRDID( trdid );
783    target_thread_ltid = LTID_FROM_TRDID( trdid );
784
785    // get root of list of process descriptors in target cluster
786    hal_remote_memcpy( XPTR( local_cxy  , &root ),
787                       XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ),
788                       sizeof(xlist_entry_t) );
789
790    // get extended pointer on lock protecting the list of processes
791    lock_xp = XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_lock );
792
793    // take the lock protecting the list of processes in target cluster
794    remote_spinlock_lock( lock_xp );
795
796    // loop on list of process in target cluster to find the PID process
797    xptr_t  iter;
798    bool_t  found = false;
799    XLIST_FOREACH( XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ) , iter )
800    {
801        target_process_xp  = XLIST_ELEMENT( iter , process_t , local_list );
802        target_process_ptr = (process_t *)GET_PTR( target_process_xp );
803        target_process_pid = hal_remote_lw( XPTR( target_cxy , &target_process_ptr->pid ) );
804        if( target_process_pid == pid )
805        {
806            found = true;
807            break;
808        }
809    }
810
811    // release the lock protecting the list of processes in target cluster
812    remote_spinlock_unlock( lock_xp );
813
814    // check target thread found
815    if( found == false )
816    {
817        return XPTR_NULL;
818    }
819
820    // get target thread local pointer
821    xptr_t xp = XPTR( target_cxy , &target_process_ptr->th_tbl[target_thread_ltid] );
822    target_thread_ptr = (thread_t *)hal_remote_lpt( xp );
823
824    if( target_thread_ptr == NULL )
825    {
826        return XPTR_NULL;
827    }
828
829    return XPTR( target_cxy , target_thread_ptr );
830}
831
Note: See TracBrowser for help on using the repository browser.