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

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

Fix several bugs in the fork() syscall.

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