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
Line 
1/*
2 * thread.c -  implementation of thread operations (user & kernel)
3 *
4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *         Alain Greiner (2016,2017)
6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <kernel_config.h>
26#include <hal_types.h>
27#include <hal_context.h>
28#include <hal_irqmask.h>
29#include <hal_special.h>
30#include <hal_remote.h>
31#include <memcpy.h>
32#include <printk.h>
33#include <cluster.h>
34#include <process.h>
35#include <scheduler.h>
36#include <dev_pic.h>
37#include <core.h>
38#include <list.h>
39#include <xlist.h>
40#include <page.h>
41#include <kmem.h>
42#include <ppm.h>
43#include <thread.h>
44
45//////////////////////////////////////////////////////////////////////////////////////
46// Extern global variables
47//////////////////////////////////////////////////////////////////////////////////////
48
49extern process_t      process_zero;
50
51//////////////////////////////////////////////////////////////////////////////////////
52// This function returns a printable string for the thread type.
53//////////////////////////////////////////////////////////////////////////////////////
54char * thread_type_str( uint32_t type )
55{
56    if     ( type == THREAD_USER   ) return "USR";
57    else if( type == THREAD_RPC    ) return "RPC";
58    else if( type == THREAD_DEV    ) return "DEV";
59    else if( type == THREAD_IDLE   ) return "IDL";
60    else                             return "undefined";
61}
62
63/////////////////////////////////////////////////////////////////////////////////////
64// This static function allocates physical memory for a thread descriptor.
65// It can be called by the three functions:
66// - thread_user_create()
67// - thread_user_fork()
68// - thread_kernel_create()
69/////////////////////////////////////////////////////////////////////////////////////
70// @ return pointer on thread descriptor if success / return NULL if failure.
71/////////////////////////////////////////////////////////////////////////////////////
72static thread_t * thread_alloc()
73{
74        page_t       * page;   // pointer on page descriptor containing thread descriptor
75        kmem_req_t     req;    // kmem request
76
77        // allocates memory for thread descriptor + kernel stack
78        req.type  = KMEM_PAGE;
79        req.size  = CONFIG_THREAD_DESC_ORDER;
80        req.flags = AF_KERNEL | AF_ZERO;
81        page      = kmem_alloc( &req );
82
83        if( page == NULL ) return NULL;
84
85    // return pointer on new thread descriptor
86    xptr_t base_xp = ppm_page2base( XPTR(local_cxy , page ) );
87    return (thread_t *)GET_PTR( base_xp );
88
89}  // end thread_alloc()
90 
91
92/////////////////////////////////////////////////////////////////////////////////////
93// This static function releases the physical memory for a thread descriptor.
94// It is called by the three functions:
95// - thread_user_create()
96// - thread_user_fork()
97// - thread_kernel_create()
98/////////////////////////////////////////////////////////////////////////////////////
99// @ thread  : pointer on thread descriptor.
100/////////////////////////////////////////////////////////////////////////////////////
101static void thread_release( thread_t * thread )
102{
103    kmem_req_t   req;
104
105    xptr_t base_xp = ppm_base2page( XPTR(local_cxy , thread ) );
106
107    req.type  = KMEM_PAGE;
108    req.ptr   = GET_PTR( base_xp );
109    kmem_free( &req );
110}
111
112/////////////////////////////////////////////////////////////////////////////////////
113// This static function initializes a thread descriptor (kernel or user).
114// It can be called by the four functions:
115// - thread_user_create()
116// - thread_user_fork()
117// - thread_kernel_create()
118/////////////////////////////////////////////////////////////////////////////////////
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
143    spinlock_lock( &process->th_lock );
144    error = process_register_thread( process, thread , &trdid );
145    spinlock_unlock( &process->th_lock );
146
147    if( error )
148    {
149        printk("\n[ERROR] in %s : cannot get TRDID\n", __FUNCTION__ );
150        return EINVAL;
151    }
152
153    // compute thread descriptor size without kernel stack
154    uint32_t desc_size = (intptr_t)(&thread->signature) - (intptr_t)thread + 4; 
155
156        // Initialize new thread descriptor
157    thread->trdid           = trdid;
158        thread->type            = type;
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    thread->u_stack_base    = u_stack_base;
172    thread->u_stack_size    = u_stack_size;
173    thread->k_stack_base    = (intptr_t)thread + desc_size;
174    thread->k_stack_size    = CONFIG_THREAD_DESC_SIZE - desc_size;
175
176    thread->entry_func      = func;         // thread entry point
177    thread->entry_args      = args;         // thread function arguments
178    thread->flags           = 0;            // all flags reset
179    thread->signals         = 0;            // no pending signal
180    thread->errno           = 0;            // no error detected
181    thread->fork_user       = 0;            // no user defined placement for fork
182    thread->fork_cxy        = 0;            // user defined target cluster for fork
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
201    // FIXME call hal_thread_init() function to initialise the save_sr field
202    thread->save_sr = 0xFF13;
203
204    // update local DQDT
205    dqdt_local_update_threads( 1 );
206
207    // register new thread in core scheduler
208    sched_register_thread( thread->core , thread );
209
210        return 0;
211
212} // end thread_init()
213
214/////////////////////////////////////////////////////////
215error_t thread_user_create( pid_t             pid,
216                            void            * start_func,
217                            void            * start_arg,
218                            pthread_attr_t  * attr,
219                            thread_t       ** new_thread )
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
225    vseg_t       * vseg;         // stack vseg
226
227    assert( (attr != NULL) , __FUNCTION__, "pthread attributes must be defined" );
228
229    // get process descriptor local copy
230    process = process_get_local_copy( pid );
231
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
239    // select a target core in local cluster
240    if( attr->attributes & PT_ATTR_CORE_DEFINED )
241    {
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        }
249    }
250    else
251    {
252        core_lid = cluster_select_local_core();
253    }
254
255    // allocate a stack from local VMM
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 );
264
265    if( vseg == NULL )
266    {
267            printk("\n[ERROR] in %s : cannot create stack vseg\n", __FUNCTION__ );
268                return ENOMEM;
269    }
270
271    // allocate memory for thread descriptor
272    thread = thread_alloc();
273
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    }
280
281    // initialize thread descriptor
282    error = thread_init( thread,
283                         process,
284                         THREAD_USER,
285                         start_func,
286                         start_arg,
287                         core_lid,
288                         vseg->min,
289                         vseg->max - vseg->min );
290
291    if( error )
292    {
293            printk("\n[ERROR] in %s : cannot initialize new thread\n", __FUNCTION__ );
294        vmm_remove_vseg( vseg );
295        thread_release( thread );
296        return EINVAL;
297    }
298
299    // set LOADABLE flag
300    thread->flags = THREAD_FLAG_LOADABLE;
301
302    // set DETACHED flag if required
303    if( attr->attributes & PT_ATTR_DETACH ) 
304    {
305        thread->flags |= THREAD_FLAG_DETACHED;
306    }
307
308    // allocate & initialize CPU context
309        if( hal_cpu_context_create( thread ) )
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
317    // allocate  FPU context
318    if( hal_fpu_context_alloc( thread ) )
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
326        // update DQDT for new thread
327    dqdt_local_update_threads( 1 );
328
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 );
332
333    *new_thread = thread;
334        return 0;
335
336}  // end thread_user_create()
337
338///////////////////////////////////////////////////////
339error_t thread_user_fork( xptr_t      parent_thread_xp,
340                          process_t * child_process,
341                          thread_t ** child_thread )
342{
343    error_t        error;
344        thread_t     * child_ptr;        // local pointer on local child thread
345    lid_t          core_lid;         // selected core local index
346
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
351
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
366    // select a target core in local cluster
367    core_lid = cluster_select_local_core();
368
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 );
372
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        ) );
380
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 )
394    {
395        printk("\n[ERROR] in %s : cannot allocate new thread\n", __FUNCTION__ );
396        return -1;
397    }
398
399    // initialize thread descriptor
400    error = thread_init( child_ptr,
401                         child_process,
402                         THREAD_USER,
403                         func,
404                         args,
405                         core_lid,
406                         base,
407                         size );
408    if( error )
409    {
410            printk("\n[ERROR] in %s : cannot initialize child thread\n", __FUNCTION__ );
411        thread_release( child_ptr );
412        return EINVAL;
413    }
414
415    // return child pointer
416    *child_thread = child_ptr;
417
418    // set detached flag if required
419    if( flags & THREAD_FLAG_DETACHED ) child_ptr->flags = THREAD_FLAG_DETACHED;
420
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
427    // allocate CPU context for child thread
428        if( hal_cpu_context_alloc( child_ptr ) )
429    {
430            printk("\n[ERROR] in %s : cannot allocate CPU context\n", __FUNCTION__ );
431        thread_release( child_ptr );
432        return -1;
433    }
434
435    // allocate FPU context for child thread
436        if( hal_fpu_context_alloc( child_ptr ) )
437    {
438            printk("\n[ERROR] in %s : cannot allocate FPU context\n", __FUNCTION__ );
439        thread_release( child_ptr );
440        return -1;
441    }
442
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 );
453
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
501thread_dmsg("\n[DBG] %s : core[%x,%d] exit / created main thread %x for process %x\n",
502__FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, child_ptr->trdid, child_process->pid );
503
504        return 0;
505
506}  // end thread_user_fork()
507
508/////////////////////////////////////////////////////////
509error_t thread_kernel_create( thread_t     ** new_thread,
510                              thread_type_t   type,
511                              void          * func,
512                              void          * args,
513                                              lid_t           core_lid )
514{
515    error_t        error;
516        thread_t     * thread;       // pointer on new thread descriptor
517
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() );
520
521    assert( ( (type == THREAD_IDLE) || (type == THREAD_RPC) || (type == THREAD_DEV) ) ,
522    __FUNCTION__ , "illegal thread type" );
523
524    assert( (core_lid < LOCAL_CLUSTER->cores_nr) ,
525            __FUNCTION__ , "illegal core_lid" );
526
527    // allocate memory for new thread descriptor
528    thread = thread_alloc();
529
530    if( thread == NULL ) return ENOMEM;
531
532    // initialize thread descriptor
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
541    if( error ) // release allocated memory for thread descriptor
542    {
543        thread_release( thread );
544        return EINVAL;
545    }
546
547    // allocate & initialize CPU context
548        hal_cpu_context_create( thread );
549
550        // update DQDT for kernel thread
551    dqdt_local_update_threads( 1 );
552
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() );
555
556    *new_thread = thread;
557        return 0;
558
559} // end thread_kernel_create()
560
561///////////////////////////////////////////////////
562error_t thread_kernel_init( thread_t      * thread,
563                            thread_type_t   type,
564                            void          * func,
565                            void          * args,
566                                            lid_t           core_lid )
567{
568    assert( (type == THREAD_IDLE) , __FUNCTION__ , "illegal thread type" );
569
570    assert( (core_lid < LOCAL_CLUSTER->cores_nr) , __FUNCTION__ , "illegal core index" );
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 );
582
583    return error;
584
585}  // end thread_kernel_init()
586
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;
595    reg_t        state;
596
597    process_t  * process    = thread->process;
598    core_t     * core       = thread->core;
599
600    thread_dmsg("\n[DBG] %s : enters for thread %x in process %x / type = %s\n",
601                __FUNCTION__ , thread->trdid , process->pid , thread_type_str( thread->type ) );
602
603    assert( (thread->children_nr == 0) , __FUNCTION__ , "still attached children" );
604
605    assert( (thread->local_locks == 0) , __FUNCTION__ , "all local locks not released" );
606
607    assert( (thread->remote_locks == 0) , __FUNCTION__ , "all remote locks not released" );
608
609        tm_start = hal_get_cycles();
610
611    // update intrumentation values
612        process->vmm.pgfault_nr += thread->info.pgfault_nr;
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
628    // remove thread from process th_tbl[]
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       
637    // update local DQDT
638    dqdt_local_update_threads( -1 );
639
640    // invalidate thread descriptor
641        thread->signature = 0;
642
643    // release memory for thread descriptor
644    thread_release( thread );
645
646        tm_end = hal_get_cycles();
647
648        thread_dmsg("\n[DBG] %s : exit for thread %x in process %x / duration = %d\n",
649                       __FUNCTION__, thread->trdid , process->pid , tm_end - tm_start );
650
651}   // end thread_destroy()
652
653/////////////////////////////////////////////////
654void thread_child_parent_link( xptr_t  xp_parent,
655                               xptr_t  xp_child )
656{
657    // get extended pointers on children list root
658    cxy_t      parent_cxy = GET_CXY( xp_parent );
659    thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent );
660    xptr_t     root       = XPTR( parent_cxy , &parent_ptr->children_root );
661
662    // get extended pointer on children list entry
663    cxy_t      child_cxy  = GET_CXY( xp_child );
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 );
670}
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
677    cxy_t      parent_cxy = GET_CXY( xp_parent );
678    thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent );
679    xptr_t     lock       = XPTR( parent_cxy , &parent_ptr->children_lock );
680
681    // get extended pointer on children list entry
682    cxy_t      child_cxy  = GET_CXY( xp_child );
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 );
692
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 );
702    hal_fence();
703}
704
705///////////////////////////////////////////////////
706inline void thread_reset_signal( thread_t * thread,
707                                 uint32_t   mask )
708{
709    hal_atomic_and( &thread->signals , ~mask );
710    hal_fence();
711}
712
713////////////////////////////////
714inline bool_t thread_can_yield()
715{
716    thread_t * this = CURRENT_THREAD;
717    return (this->local_locks == 0) && (this->remote_locks == 0);
718}
719
720/////////////////////////
721void thread_check_sched()
722{
723    thread_t * this = CURRENT_THREAD;
724
725        if( (this->local_locks == 0) && 
726        (this->remote_locks == 0) &&
727        (this->flags & THREAD_FLAG_SCHED) ) 
728    {
729        this->flags &= ~THREAD_FLAG_SCHED;
730        sched_yield( "delayed scheduling" );
731    }
732
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
762/////////////////////
763error_t thread_exit()
764{
765    reg_t      sr_save;
766
767        thread_t * this = CURRENT_THREAD;
768
769    // test if this thread can be descheduled
770        if( !thread_can_yield() )
771        {
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 );
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    }
785    else
786    {
787        // if attached, set blocking cause
788        thread_block( this , THREAD_BLOCKED_EXIT );
789    }
790
791    // deschedule
792    sched_yield( "exit" );
793    return 0;
794
795}  // end thread_exit()
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
806    // send an IPI to schedule the target thread core.
807    dev_pic_send_ipi( local_cxy , target->core->lid );
808
809}  // end thread_kill()
810
811///////////////////////
812void thread_idle_func()
813{
814    while( 1 )
815    {
816        // unmask IRQs
817        hal_enable_irq( NULL );
818
819        if( CONFIG_THREAD_IDLE_MODE_SLEEP ) // force core to low-power mode
820        {
821
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() );
824
825            hal_core_sleep();
826
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
838        sched_yield( "idle" );
839   }
840}  // end thread_idle()
841
842
843/////////////////////////////////////////////////
844void thread_user_time_update( thread_t * thread )
845{
846    // TODO
847    // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ );
848}
849
850///////////////////////////////////////////////////
851void thread_kernel_time_update( thread_t * thread )
852{
853    // TODO
854    // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ );
855}
856
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
863    thread_t    * target_thread_ptr;   // target thread local pointer
864    xptr_t        target_process_xp;   // extended pointer on target process descriptor
865    process_t   * target_process_ptr;  // local pointer on target process descriptor
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
869
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
879    // get extended pointer on lock protecting the list of processes
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] );
911    target_thread_ptr = (thread_t *)hal_remote_lpt( xp );
912
913    if( target_thread_ptr == NULL )
914    {
915        return XPTR_NULL;
916    }
917
918    return XPTR( target_cxy , target_thread_ptr );
919}
920
Note: See TracBrowser for help on using the repository browser.