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

Last change on this file since 389 was 374, checked in by max@…, 7 years ago

Use panic().

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