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

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

style

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