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

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

blip

File size: 31.8 KB
Line 
1/*
2 * thread.c -  implementation of thread operations (user & kernel)
3 *
4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *         Alain Greiner (2016,2017)
6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <kernel_config.h>
26#include <hal_types.h>
27#include <hal_context.h>
28#include <hal_irqmask.h>
29#include <hal_special.h>
30#include <hal_remote.h>
31#include <memcpy.h>
32#include <printk.h>
33#include <cluster.h>
34#include <process.h>
35#include <scheduler.h>
36#include <dev_pic.h>
37#include <core.h>
38#include <list.h>
39#include <xlist.h>
40#include <page.h>
41#include <kmem.h>
42#include <ppm.h>
43#include <thread.h>
44
45//////////////////////////////////////////////////////////////////////////////////////
46// Extern global variables
47//////////////////////////////////////////////////////////////////////////////////////
48
49extern process_t      process_zero;
50
51//////////////////////////////////////////////////////////////////////////////////////
52// This function returns a printable string for the thread type.
53//////////////////////////////////////////////////////////////////////////////////////
54char * thread_type_str( uint32_t type )
55{
56    if     ( type == THREAD_USER   ) return "USR";
57    else if( type == THREAD_RPC    ) return "RPC";
58    else if( type == THREAD_DEV    ) return "DEV";
59    else if( type == THREAD_IDLE   ) return "IDL";
60    else                             return "undefined";
61}
62
63/////////////////////////////////////////////////////////////////////////////////////
64// This static function allocates physical memory for a thread descriptor.
65// It can be called by the three functions:
66// - thread_user_create()
67// - thread_user_fork()
68// - thread_kernel_create()
69/////////////////////////////////////////////////////////////////////////////////////
70// @ return pointer on thread descriptor if success / return NULL if failure.
71/////////////////////////////////////////////////////////////////////////////////////
72static thread_t * thread_alloc()
73{
74        page_t       * page;   // pointer on page descriptor containing thread descriptor
75        kmem_req_t     req;    // kmem request
76
77        // allocates memory for thread descriptor + kernel stack
78        req.type  = KMEM_PAGE;
79        req.size  = CONFIG_THREAD_DESC_ORDER;
80        req.flags = AF_KERNEL | AF_ZERO;
81        page      = kmem_alloc( &req );
82
83        if( page == NULL ) return NULL;
84
85    // return pointer on new thread descriptor
86    xptr_t base_xp = ppm_page2base( XPTR(local_cxy , page ) );
87    return (thread_t *)GET_PTR( base_xp );
88
89}  // end thread_alloc()
90 
91
92/////////////////////////////////////////////////////////////////////////////////////
93// This static function releases the physical memory for a thread descriptor.
94// It is called by the three functions:
95// - thread_user_create()
96// - thread_user_fork()
97// - thread_kernel_create()
98/////////////////////////////////////////////////////////////////////////////////////
99// @ thread  : pointer on thread descriptor.
100/////////////////////////////////////////////////////////////////////////////////////
101static void thread_release( thread_t * thread )
102{
103    kmem_req_t   req;
104
105    xptr_t base_xp = ppm_base2page( XPTR(local_cxy , thread ) );
106
107    req.type  = KMEM_PAGE;
108    req.ptr   = GET_PTR( base_xp );
109    kmem_free( &req );
110}
111
112/////////////////////////////////////////////////////////////////////////////////////
113// This static function initializes a thread descriptor (kernel or user).
114// It can be called by the three 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    error = process_register_thread( process, thread , &trdid );
144
145    if( error )
146    {
147        printk("\n[ERROR] in %s : cannot get TRDID\n", __FUNCTION__ );
148        return EINVAL;
149    }
150
151    // compute thread descriptor size without kernel stack
152    uint32_t desc_size = (intptr_t)(&thread->signature) - (intptr_t)thread + 4; 
153
154        // Initialize new thread descriptor
155    thread->trdid           = trdid;
156        thread->type            = type;
157    thread->quantum         = 0;            // TODO
158    thread->ticks_nr        = 0;            // TODO
159    thread->time_last_check = 0;
160        thread->core            = &local_cluster->core_tbl[core_lid];
161        thread->process         = process;
162
163    thread->local_locks     = 0;
164    thread->remote_locks    = 0;
165
166#if CONFIG_LOCKS_DEBUG
167    list_root_init( &thread->locks_root ); 
168    xlist_root_init( XPTR( local_cxy , &thread->xlocks_root ) );
169#endif
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->errno           = 0;            // no error detected
180    thread->fork_user       = 0;            // no user defined placement for fork
181    thread->fork_cxy        = 0;            // user defined target cluster for fork
182    thread->blocked         = THREAD_BLOCKED_GLOBAL;
183
184    // reset children list
185    xlist_root_init( XPTR( local_cxy , &thread->children_root ) );
186    thread->children_nr = 0;
187
188    // reset sched list and brothers list
189    list_entry_init( &thread->sched_list );
190    xlist_entry_init( XPTR( local_cxy , &thread->brothers_list ) );
191
192    // reset thread info
193    memset( &thread->info , 0 , sizeof(thread_info_t) );
194
195    // initializes join_lock
196    remote_spinlock_init( XPTR( local_cxy , &thread->join_lock ) );
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
229thread_dmsg("\n[DBG] %s : core[%x,%d] enter for process %x\n",
230__FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, process->pid );
231
232    // get process descriptor local copy
233    process = process_get_local_copy( pid );
234
235    if( process == NULL )
236    {
237                printk("\n[ERROR] in %s : cannot get process descriptor %x\n",
238               __FUNCTION__ , pid );
239        return ENOMEM;
240    }
241
242    // select a target core in local cluster
243    if( attr->attributes & PT_ATTR_CORE_DEFINED )
244    {
245        core_lid = attr->lid;
246        if( core_lid >= LOCAL_CLUSTER->cores_nr )
247        {
248                printk("\n[ERROR] in %s : illegal core index attribute = %d\n",
249            __FUNCTION__ , core_lid );
250            return EINVAL;
251        }
252    }
253    else
254    {
255        core_lid = cluster_select_local_core();
256    }
257
258    // allocate a stack from local VMM
259    vseg = vmm_create_vseg( process,
260                            VSEG_TYPE_STACK,
261                            0,                 // size unused
262                            0,                 // length unused
263                            0,                 // file_offset unused
264                            0,                 // file_size unused
265                            XPTR_NULL,         // mapper_xp unused
266                            local_cxy );
267
268    if( vseg == NULL )
269    {
270            printk("\n[ERROR] in %s : cannot create stack vseg\n", __FUNCTION__ );
271                return ENOMEM;
272    }
273
274    // allocate memory for thread descriptor
275    thread = thread_alloc();
276
277    if( thread == NULL )
278    {
279            printk("\n[ERROR] in %s : cannot create new thread\n", __FUNCTION__ );
280        vmm_remove_vseg( vseg );
281        return ENOMEM;
282    }
283
284    // initialize thread descriptor
285    error = thread_init( thread,
286                         process,
287                         THREAD_USER,
288                         start_func,
289                         start_arg,
290                         core_lid,
291                         vseg->min,
292                         vseg->max - vseg->min );
293    if( error )
294    {
295            printk("\n[ERROR] in %s : cannot initialize new thread\n", __FUNCTION__ );
296        vmm_remove_vseg( vseg );
297        thread_release( thread );
298        return EINVAL;
299    }
300
301    // set DETACHED flag if required
302    if( attr->attributes & PT_ATTR_DETACH ) 
303    {
304        thread->flags |= THREAD_FLAG_DETACHED;
305    }
306
307    // allocate & initialize CPU context
308        if( hal_cpu_context_create( thread ) )
309    {
310            printk("\n[ERROR] in %s : cannot create CPU context\n", __FUNCTION__ );
311        vmm_remove_vseg( vseg );
312        thread_release( thread );
313        return ENOMEM;
314    }
315
316    // allocate  FPU context
317    if( hal_fpu_context_alloc( thread ) )
318    {
319            printk("\n[ERROR] in %s : cannot create FPU context\n", __FUNCTION__ );
320        vmm_remove_vseg( vseg );
321        thread_release( thread );
322        return ENOMEM;
323    }
324
325        // update DQDT for new thread
326    dqdt_local_update_threads( 1 );
327
328thread_dmsg("\n[DBG] %s : core[%x,%d] exit for process %x / trdid = %x / core = %d\n",
329__FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, process->pid, thread->trdid, core_lid );
330
331    *new_thread = thread;
332        return 0;
333
334}  // end thread_user_create()
335
336///////////////////////////////////////////////////////
337error_t thread_user_fork( xptr_t      parent_thread_xp,
338                          process_t * child_process,
339                          thread_t ** child_thread )
340{
341    error_t        error;
342        thread_t     * child_ptr;        // local pointer on local child thread
343    lid_t          core_lid;         // selected core local index
344
345    thread_t     * parent_ptr;       // local pointer on remote parent thread
346    cxy_t          parent_cxy;       // parent thread cluster
347    process_t    * parent_process;   // local pointer on parent process
348    xptr_t         parent_gpt_xp;    // extended pointer on parent thread GPT
349
350    void         * func;             // parent thread entry_func
351    void         * args;             // parent thread entry_args
352    intptr_t       base;             // parent thread u_stack_base
353    uint32_t       size;             // parent thread u_stack_size
354    uint32_t       flags;            // parent_thread flags
355    vpn_t          vpn_base;         // parent thread stack vpn_base
356    vpn_t          vpn_size;         // parent thread stack vpn_size
357    reg_t        * uzone;            // parent thread pointer on uzone 
358
359    vseg_t       * vseg;             // child thread STACK vseg
360
361thread_dmsg("\n[DBG] %s : core[%x,%d] enters at cycle %d\n",
362__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , hal_get_cycles() );
363
364    // select a target core in local cluster
365    core_lid = cluster_select_local_core();
366
367    // get cluster and local pointer on parent thread descriptor
368    parent_cxy = GET_CXY( parent_thread_xp );
369    parent_ptr = (thread_t *)GET_PTR( parent_thread_xp );
370
371    // get relevant fields from parent thread
372    func  = (void *)  hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->entry_func    ));
373    args  = (void *)  hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->entry_args    ));
374    base  = (intptr_t)hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->u_stack_base  ));
375    size  = (uint32_t)hal_remote_lw ( XPTR( parent_cxy , &parent_ptr->u_stack_size  ));
376    flags =           hal_remote_lw ( XPTR( parent_cxy , &parent_ptr->flags         ));
377    uzone = (reg_t *) hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->uzone_current ));
378
379    vpn_base = base >> CONFIG_PPM_PAGE_SHIFT;
380    vpn_size = size >> CONFIG_PPM_PAGE_SHIFT;
381
382    // get pointer on parent process in parent thread cluster
383    parent_process = (process_t *)hal_remote_lpt( XPTR( parent_cxy,
384                                                        &parent_ptr->process ) );
385 
386    // get extended pointer on parent GPT in parent thread cluster
387    parent_gpt_xp = XPTR( parent_cxy , &parent_process->vmm.gpt );
388
389    // allocate memory for child thread descriptor
390    child_ptr = thread_alloc();
391    if( child_ptr == NULL )
392    {
393        printk("\n[ERROR] in %s : cannot allocate new thread\n", __FUNCTION__ );
394        return -1;
395    }
396
397    // initialize thread descriptor
398    error = thread_init( child_ptr,
399                         child_process,
400                         THREAD_USER,
401                         func,
402                         args,
403                         core_lid,
404                         base,
405                         size );
406    if( error )
407    {
408            printk("\n[ERROR] in %s : cannot initialize child thread\n", __FUNCTION__ );
409        thread_release( child_ptr );
410        return EINVAL;
411    }
412
413    // return child pointer
414    *child_thread = child_ptr;
415
416    // set detached flag if required
417    if( flags & THREAD_FLAG_DETACHED ) child_ptr->flags = THREAD_FLAG_DETACHED;
418
419    // update uzone pointer in child thread descriptor
420    child_ptr->uzone_current = (char *)((intptr_t)uzone +
421                                        (intptr_t)child_ptr - 
422                                        (intptr_t)parent_ptr );
423 
424
425    // allocate CPU context for child thread
426        if( hal_cpu_context_alloc( child_ptr ) )
427    {
428            printk("\n[ERROR] in %s : cannot allocate CPU context\n", __FUNCTION__ );
429        thread_release( child_ptr );
430        return -1;
431    }
432
433    // allocate FPU context for child thread
434        if( hal_fpu_context_alloc( child_ptr ) )
435    {
436            printk("\n[ERROR] in %s : cannot allocate FPU context\n", __FUNCTION__ );
437        thread_release( child_ptr );
438        return -1;
439    }
440
441    // create and initialize STACK vseg
442    vseg = vseg_alloc();
443    vseg_init( vseg,
444               VSEG_TYPE_STACK,
445               base,
446               size,
447               vpn_base,
448               vpn_size,
449               0, 0, XPTR_NULL,                         // not a file vseg
450               local_cxy );
451
452    // register STACK vseg in local child VSL
453    vseg_attach( &child_process->vmm , vseg );
454
455    // copy all valid STACK GPT entries   
456    vpn_t          vpn;
457    bool_t         mapped;
458    ppn_t          ppn;
459    for( vpn = vpn_base ; vpn < (vpn_base + vpn_size) ; vpn++ )
460    {
461        error = hal_gpt_pte_copy( &child_process->vmm.gpt,
462                                  parent_gpt_xp,
463                                  vpn,
464                                  true,                 // set cow
465                                  &ppn,
466                                  &mapped );
467        if( error )
468        {
469            vseg_detach( &child_process->vmm , vseg );
470            vseg_free( vseg );
471            thread_release( child_ptr );
472            printk("\n[ERROR] in %s : cannot update child GPT\n", __FUNCTION__ );
473            return -1;
474        }
475
476        // increment page descriptor fork_nr for the referenced page if mapped
477        if( mapped )
478        {
479            xptr_t   page_xp  = ppm_ppn2page( ppn );
480            cxy_t    page_cxy = GET_CXY( page_xp );
481            page_t * page_ptr = (page_t *)GET_PTR( page_xp );
482            hal_remote_atomic_add( XPTR( page_cxy , &page_ptr->fork_nr ) , 1 );
483
484thread_dmsg("\n[DBG] %s : core[%x,%d] copied PTE to child GPT : vpn %x\n",
485__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn );
486
487        }
488    }
489
490    // set COW flag for STAK vseg in parent thread GPT
491    hal_gpt_flip_cow( true,                               // set cow
492                      parent_gpt_xp,
493                      vpn_base,
494                      vpn_size );
495 
496        // update DQDT for child thread
497    dqdt_local_update_threads( 1 );
498
499thread_dmsg("\n[DBG] %s : core[%x,%d] exit / created main thread %x for process %x\n",
500__FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, child_ptr->trdid, child_process->pid );
501
502        return 0;
503
504}  // end thread_user_fork()
505
506/////////////////////////////////////////////////////////
507error_t thread_kernel_create( thread_t     ** new_thread,
508                              thread_type_t   type,
509                              void          * func,
510                              void          * args,
511                                              lid_t           core_lid )
512{
513    error_t        error;
514        thread_t     * thread;       // pointer on new thread descriptor
515
516thread_dmsg("\n[DBG] %s : core[%x,%d] enters / type %s / cycle %d\n",
517__FUNCTION__ , local_cxy , core_lid , thread_type_str( type ) , hal_time_stamp() );
518
519    assert( ( (type == THREAD_IDLE) || (type == THREAD_RPC) || (type == THREAD_DEV) ) ,
520    __FUNCTION__ , "illegal thread type" );
521
522    assert( (core_lid < LOCAL_CLUSTER->cores_nr) ,
523            __FUNCTION__ , "illegal core_lid" );
524
525    // allocate memory for new thread descriptor
526    thread = thread_alloc();
527
528    if( thread == NULL ) return ENOMEM;
529
530    // initialize thread descriptor
531    error = thread_init( thread,
532                         &process_zero,
533                         type,
534                         func,
535                         args,
536                         core_lid,
537                         0 , 0 );  // no user stack for a kernel thread
538
539    if( error ) // release allocated memory for thread descriptor
540    {
541        thread_release( thread );
542        return EINVAL;
543    }
544
545    // allocate & initialize CPU context
546        hal_cpu_context_create( thread );
547
548        // update DQDT for kernel thread
549    dqdt_local_update_threads( 1 );
550
551thread_dmsg("\n[DBG] %s : core = [%x,%d] exit / trdid = %x / type %s / cycle %d\n",
552__FUNCTION__, local_cxy, core_lid, thread->trdid, thread_type_str(type), hal_time_stamp() );
553
554    *new_thread = thread;
555        return 0;
556
557} // end thread_kernel_create()
558
559///////////////////////////////////////////////////
560error_t thread_kernel_init( thread_t      * thread,
561                            thread_type_t   type,
562                            void          * func,
563                            void          * args,
564                                            lid_t           core_lid )
565{
566    assert( (type == THREAD_IDLE) , __FUNCTION__ , "illegal thread type" );
567
568    assert( (core_lid < LOCAL_CLUSTER->cores_nr) , __FUNCTION__ , "illegal core index" );
569
570    error_t  error = thread_init( thread,
571                                  &process_zero,
572                                  type,
573                                  func,
574                                  args,
575                                  core_lid,
576                                  0 , 0 );   // no user stack for a kernel thread
577
578    // allocate & initialize CPU context if success
579    if( error == 0 ) hal_cpu_context_create( thread );
580
581    return error;
582
583}  // end thread_kernel_init()
584
585///////////////////////////////////////////////////////////////////////////////////////
586// TODO: check that all memory dynamically allocated during thread execution
587// has been released, using a cache of mmap and malloc requests. [AG]
588///////////////////////////////////////////////////////////////////////////////////////
589void thread_destroy( thread_t * thread )
590{
591        uint32_t     tm_start;
592        uint32_t     tm_end;
593    reg_t        save_sr;
594
595    process_t  * process    = thread->process;
596    core_t     * core       = thread->core;
597
598    thread_dmsg("\n[DBG] %s : enters for thread %x in process %x / type = %s\n",
599                __FUNCTION__ , thread->trdid , process->pid , thread_type_str( thread->type ) );
600
601    assert( (thread->children_nr == 0) , __FUNCTION__ , "still attached children" );
602
603    assert( (thread->local_locks == 0) , __FUNCTION__ , "all local locks not released" );
604
605    assert( (thread->remote_locks == 0) , __FUNCTION__ , "all remote locks not released" );
606
607        tm_start = hal_get_cycles();
608
609    // update intrumentation values
610        process->vmm.pgfault_nr += thread->info.pgfault_nr;
611
612    // release memory allocated for CPU context and FPU context
613        hal_cpu_context_destroy( thread );
614        if ( thread->type == THREAD_USER ) hal_fpu_context_destroy( thread );
615       
616    // release FPU ownership if required
617        hal_disable_irq( &save_sr );
618        if( core->fpu_owner == thread )
619        {
620                core->fpu_owner = NULL;
621                hal_fpu_disable();
622        }
623        hal_restore_irq( save_sr );
624
625    // remove thread from process th_tbl[]
626    process_remove_thread( thread );
627       
628    // update local DQDT
629    dqdt_local_update_threads( -1 );
630
631    // invalidate thread descriptor
632        thread->signature = 0;
633
634    // release memory for thread descriptor
635    thread_release( thread );
636
637        tm_end = hal_get_cycles();
638
639        thread_dmsg("\n[DBG] %s : exit for thread %x in process %x / duration = %d\n",
640                       __FUNCTION__, thread->trdid , process->pid , tm_end - tm_start );
641
642}   // end thread_destroy()
643
644/////////////////////////////////////////////////
645void thread_child_parent_link( xptr_t  xp_parent,
646                               xptr_t  xp_child )
647{
648    // get extended pointers on children list root
649    cxy_t      parent_cxy = GET_CXY( xp_parent );
650    thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent );
651    xptr_t     root       = XPTR( parent_cxy , &parent_ptr->children_root );
652
653    // get extended pointer on children list entry
654    cxy_t      child_cxy  = GET_CXY( xp_child );
655    thread_t * child_ptr  = (thread_t *)GET_PTR( xp_child );
656    xptr_t     entry      = XPTR( child_cxy , &child_ptr->brothers_list );
657
658    // set the link
659    xlist_add_first( root , entry );
660    hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr ) , 1 );
661
662}  // end thread_child_parent_link()
663
664///////////////////////////////////////////////////
665void thread_child_parent_unlink( xptr_t  xp_parent,
666                                 xptr_t  xp_child )
667{
668    // get extended pointer on children list lock
669    cxy_t      parent_cxy = GET_CXY( xp_parent );
670    thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent );
671    xptr_t     lock       = XPTR( parent_cxy , &parent_ptr->children_lock );
672
673    // get extended pointer on children list entry
674    cxy_t      child_cxy  = GET_CXY( xp_child );
675    thread_t * child_ptr  = (thread_t *)GET_PTR( xp_child );
676    xptr_t     entry      = XPTR( child_cxy , &child_ptr->brothers_list );
677
678    // get the lock
679    remote_spinlock_lock( lock );
680
681    // remove the link
682    xlist_unlink( entry );
683    hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr ) , -1 );
684
685    // release the lock
686    remote_spinlock_unlock( lock );
687
688}  // thread_child_parent_unlink()
689
690//////////////////////////////////////////////////
691inline void thread_set_req_ack( thread_t * target,
692                                uint32_t * rsp_count )
693{
694    reg_t    save_sr;   // for critical section
695
696    // get pointer on target thread scheduler
697    scheduler_t * sched = &target->core->scheduler;
698
699    // wait scheduler ready to handle a new request
700    while( sched->req_ack_pending ) asm volatile( "nop" );
701   
702    // enter critical section
703    hal_disable_irq( &save_sr );
704     
705    // set request in target thread scheduler
706    sched->req_ack_pending = true;
707
708    // set ack request in target thread "flags"
709    hal_atomic_or( &target->flags , THREAD_FLAG_REQ_ACK );
710
711    // set pointer on responses counter in target thread
712    target->ack_rsp_count = rsp_count;
713   
714    // exit critical section
715    hal_restore_irq( save_sr );
716
717    hal_fence();
718
719}  // thread_set_req_ack()
720
721/////////////////////////////////////////////////////
722inline void thread_reset_req_ack( thread_t * target )
723{
724    reg_t    save_sr;   // for critical section
725
726    // get pointer on target thread scheduler
727    scheduler_t * sched = &target->core->scheduler;
728
729    // check signal pending in scheduler
730    assert( sched->req_ack_pending , __FUNCTION__ , "no pending signal" );
731   
732    // enter critical section
733    hal_disable_irq( &save_sr );
734     
735    // reset signal in scheduler
736    sched->req_ack_pending = false;
737
738    // reset signal in thread "flags"
739    hal_atomic_and( &target->flags , ~THREAD_FLAG_REQ_ACK );
740
741    // reset pointer on responses counter
742    target->ack_rsp_count = NULL;
743   
744    // exit critical section
745    hal_restore_irq( save_sr );
746
747    hal_fence();
748
749}  // thread_reset_req_ack()
750
751////////////////////////////////
752inline bool_t thread_can_yield()
753{
754    thread_t * this = CURRENT_THREAD;
755    return (this->local_locks == 0) && (this->remote_locks == 0);
756}
757
758/////////////////////////
759void thread_check_sched()
760{
761    thread_t * this = CURRENT_THREAD;
762
763        if( (this->local_locks == 0) && 
764        (this->remote_locks == 0) &&
765        (this->flags & THREAD_FLAG_SCHED) ) 
766    {
767        this->flags &= ~THREAD_FLAG_SCHED;
768        sched_yield( "delayed scheduling" );
769    }
770
771}  // end thread_check_sched()
772
773/////////////////////////////////////
774void thread_block( thread_t * thread,
775                   uint32_t   cause )
776{
777    // set blocking cause
778    hal_atomic_or( &thread->blocked , cause );
779    hal_fence();
780
781} // end thread_block()
782
783/////////////////////////////////////////
784uint32_t thread_unblock( xptr_t   thread,
785                         uint32_t cause )
786{
787    // get thread cluster and local pointer
788    cxy_t      cxy = GET_CXY( thread );
789    thread_t * ptr = (thread_t *)GET_PTR( thread );
790
791    // reset blocking cause
792    uint32_t previous = hal_remote_atomic_and( XPTR( cxy , &ptr->blocked ) , ~cause );
793    hal_fence();
794
795    // return a non zero value if the cause bit is modified
796    return( previous & cause );
797
798}  // end thread_unblock()
799
800/////////////////////////////////////
801void thread_kill( thread_t * target )
802{
803    volatile uint32_t  rsp_count = 1;     // responses counter
804
805    thread_t * killer = CURRENT_THREAD;
806
807thread_dmsg("\n[DBG] %s : killer thread %x enter for target thread %x\n",
808__FUNCTION__, local_cxy, killer->trdid , target->trdid );
809
810    // set the global blocked bit in target thread descriptor.
811    thread_block( target , THREAD_BLOCKED_GLOBAL );
812
813    // request target scheduler to deschedule the target thread
814    // when killer thread is not running on same core as target thread
815    if( killer->core->lid != target->core->lid )
816    {
817        // set signal in target thread descriptor and in target scheduler
818        thread_set_req_ack( target , (void *)(&rsp_count) );
819
820        // send an IPI to the target thread core.
821        dev_pic_send_ipi( local_cxy , target->core->lid );
822
823        // poll the response
824        while( 1 )
825        {
826            // exit when response received from scheduler
827            if( rsp_count == 0 )  break;
828
829            // deschedule without blocking
830            hal_fixed_delay( 1000 );
831        }
832    }
833
834        // set REQ_DELETE flag
835        hal_atomic_or( &target->flags , THREAD_FLAG_REQ_DELETE );
836
837thread_dmsg("\n[DBG] %s : killer thread %x exit for target thread %x\n",
838__FUNCTION__, local_cxy, killer->trdid , target->trdid );
839
840}  // end thread_kill()
841
842///////////////////////
843void thread_idle_func()
844{
845    while( 1 )
846    {
847        // unmask IRQs
848        hal_enable_irq( NULL );
849
850        if( CONFIG_THREAD_IDLE_MODE_SLEEP ) // force core to low-power mode
851        {
852
853idle_dmsg("\n[DBG] %s : core[%x][%d] goes to sleep at cycle %d\n",
854__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , hal_get_cycles() );
855
856            hal_core_sleep();
857
858idle_dmsg("\n[DBG] %s : core[%x][%d] wake up at cycle %d\n",
859__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , hal_get_cycles() );
860
861        }
862        else                                // search a runable thread
863        {
864            sched_yield( "IDLE" );
865        }
866    }
867}  // end thread_idle()
868
869
870/////////////////////////////////////////////////
871void thread_user_time_update( thread_t * thread )
872{
873    // TODO
874    // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ );
875}
876
877///////////////////////////////////////////////////
878void thread_kernel_time_update( thread_t * thread )
879{
880    // TODO
881    // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ );
882}
883
884/////////////////////////////////////
885xptr_t thread_get_xptr( pid_t    pid,
886                        trdid_t  trdid )
887{
888    cxy_t         target_cxy;          // target thread cluster identifier
889    ltid_t        target_thread_ltid;  // target thread local index
890    thread_t    * target_thread_ptr;   // target thread local pointer
891    xptr_t        target_process_xp;   // extended pointer on target process descriptor
892    process_t   * target_process_ptr;  // local pointer on target process descriptor
893    pid_t         target_process_pid;  // target process identifier
894    xlist_entry_t root;                // root of list of process in target cluster
895    xptr_t        lock_xp;             // extended pointer on lock protecting  this list
896
897    // get target cluster identifier and local thread identifier
898    target_cxy         = CXY_FROM_TRDID( trdid );
899    target_thread_ltid = LTID_FROM_TRDID( trdid );
900
901    // get root of list of process descriptors in target cluster
902    hal_remote_memcpy( XPTR( local_cxy  , &root ),
903                       XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ),
904                       sizeof(xlist_entry_t) );
905
906    // get extended pointer on lock protecting the list of processes
907    lock_xp = XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_lock );
908
909    // take the lock protecting the list of processes in target cluster
910    remote_spinlock_lock( lock_xp );
911
912    // loop on list of process in target cluster to find the PID process
913    xptr_t  iter;
914    bool_t  found = false;
915    XLIST_FOREACH( XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ) , iter )
916    {
917        target_process_xp  = XLIST_ELEMENT( iter , process_t , local_list );
918        target_process_ptr = (process_t *)GET_PTR( target_process_xp );
919        target_process_pid = hal_remote_lw( XPTR( target_cxy , &target_process_ptr->pid ) );
920        if( target_process_pid == pid )
921        {
922            found = true;
923            break;
924        }
925    }
926
927    // release the lock protecting the list of processes in target cluster
928    remote_spinlock_unlock( lock_xp );
929
930    // check target thread found
931    if( found == false )
932    {
933        return XPTR_NULL;
934    }
935
936    // get target thread local pointer
937    xptr_t xp = XPTR( target_cxy , &target_process_ptr->th_tbl[target_thread_ltid] );
938    target_thread_ptr = (thread_t *)hal_remote_lpt( xp );
939
940    if( target_thread_ptr == NULL )
941    {
942        return XPTR_NULL;
943    }
944
945    return XPTR( target_cxy , target_thread_ptr );
946}
947
Note: See TracBrowser for help on using the repository browser.