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

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

blip

File size: 33.5 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
229#if CONFIG_DEBUG_THREAD_USER_CREATE
230uint32_t cycle = (uint32_t)hal_get_cycles();
231if( CONFIG_DEBUG_THREAD_USER_CREATE < cycle )
232printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n",
233__FUNCTION__, CURRENT_THREAD, pid , cycle );
234#endif
235
236    // get process descriptor local copy
237    process = process_get_local_copy( pid );
238    if( process == NULL )
239    {
240                printk("\n[ERROR] in %s : cannot get process descriptor %x\n",
241               __FUNCTION__ , pid );
242        return ENOMEM;
243    }
244
245    // select a target core in local cluster
246    if( attr->attributes & PT_ATTR_CORE_DEFINED )
247    {
248        core_lid = attr->lid;
249        if( core_lid >= LOCAL_CLUSTER->cores_nr )
250        {
251                printk("\n[ERROR] in %s : illegal core index attribute = %d\n",
252            __FUNCTION__ , core_lid );
253            return EINVAL;
254        }
255    }
256    else
257    {
258        core_lid = cluster_select_local_core();
259    }
260
261    // allocate a stack from local VMM
262    vseg = vmm_create_vseg( process,
263                            VSEG_TYPE_STACK,
264                            0,                 // size unused
265                            0,                 // length unused
266                            0,                 // file_offset unused
267                            0,                 // file_size unused
268                            XPTR_NULL,         // mapper_xp unused
269                            local_cxy );
270
271    if( vseg == NULL )
272    {
273            printk("\n[ERROR] in %s : cannot create stack vseg\n", __FUNCTION__ );
274                return ENOMEM;
275    }
276
277    // allocate memory for thread descriptor
278    thread = thread_alloc();
279
280    if( thread == NULL )
281    {
282            printk("\n[ERROR] in %s : cannot create new thread\n", __FUNCTION__ );
283        vmm_remove_vseg( vseg );
284        return ENOMEM;
285    }
286
287    // initialize thread descriptor
288    error = thread_init( thread,
289                         process,
290                         THREAD_USER,
291                         start_func,
292                         start_arg,
293                         core_lid,
294                         vseg->min,
295                         vseg->max - vseg->min );
296    if( error )
297    {
298            printk("\n[ERROR] in %s : cannot initialize new thread\n", __FUNCTION__ );
299        vmm_remove_vseg( vseg );
300        thread_release( thread );
301        return EINVAL;
302    }
303
304    // set DETACHED flag if required
305    if( attr->attributes & PT_ATTR_DETACH ) 
306    {
307        thread->flags |= THREAD_FLAG_DETACHED;
308    }
309
310    // allocate & initialize CPU context
311        if( hal_cpu_context_create( thread ) )
312    {
313            printk("\n[ERROR] in %s : cannot create CPU context\n", __FUNCTION__ );
314        vmm_remove_vseg( vseg );
315        thread_release( thread );
316        return ENOMEM;
317    }
318
319    // allocate  FPU context
320    if( hal_fpu_context_alloc( thread ) )
321    {
322            printk("\n[ERROR] in %s : cannot create FPU context\n", __FUNCTION__ );
323        vmm_remove_vseg( vseg );
324        thread_release( thread );
325        return ENOMEM;
326    }
327
328        // update DQDT for new thread
329    dqdt_local_update_threads( 1 );
330
331#if CONFIG_DEBUG_THREAD_USER_CREATE
332cycle = (uint32_t)hal_get_cycles();
333if( CONFIG_DEBUG_THREAD_USER_CREATE < cycle )
334printk("\n[DBG] %s : thread %x exit / process %x / new_thread %x / core %d / cycle %d\n",
335__FUNCTION__, CURRENT_THREAD, pid, thread, core_lid, cycle );
336#endif
337
338    *new_thread = thread;
339        return 0;
340
341}  // end thread_user_create()
342
343///////////////////////////////////////////////////////
344error_t thread_user_fork( xptr_t      parent_thread_xp,
345                          process_t * child_process,
346                          thread_t ** child_thread )
347{
348    error_t        error;
349        thread_t     * child_ptr;        // local pointer on local child thread
350    lid_t          core_lid;         // selected core local index
351
352    thread_t     * parent_ptr;       // local pointer on remote parent thread
353    cxy_t          parent_cxy;       // parent thread cluster
354    process_t    * parent_process;   // local pointer on parent process
355    xptr_t         parent_gpt_xp;    // extended pointer on parent thread GPT
356
357    void         * func;             // parent thread entry_func
358    void         * args;             // parent thread entry_args
359    intptr_t       base;             // parent thread u_stack_base
360    uint32_t       size;             // parent thread u_stack_size
361    uint32_t       flags;            // parent_thread flags
362    vpn_t          vpn_base;         // parent thread stack vpn_base
363    vpn_t          vpn_size;         // parent thread stack vpn_size
364    reg_t        * uzone;            // parent thread pointer on uzone 
365
366    vseg_t       * vseg;             // child thread STACK vseg
367
368#if CONFIG_DEBUG_THREAD_USER_FORK
369uint32_t cycle = (uint32_t)hal_get_cycles();
370if( CONFIG_DEBUG_THREAD_USER_FORK < cycle )
371printk("\n[DBG] %s : thread %x enter / child_process %x / cycle %d\n",
372__FUNCTION__, CURRENT_THREAD, child_process->pid, cycle );
373#endif
374
375    // select a target core in local cluster
376    core_lid = cluster_select_local_core();
377
378    // get cluster and local pointer on parent thread descriptor
379    parent_cxy = GET_CXY( parent_thread_xp );
380    parent_ptr = (thread_t *)GET_PTR( parent_thread_xp );
381
382    // get relevant fields from parent thread
383    func  = (void *)  hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->entry_func    ));
384    args  = (void *)  hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->entry_args    ));
385    base  = (intptr_t)hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->u_stack_base  ));
386    size  = (uint32_t)hal_remote_lw ( XPTR( parent_cxy , &parent_ptr->u_stack_size  ));
387    flags =           hal_remote_lw ( XPTR( parent_cxy , &parent_ptr->flags         ));
388    uzone = (reg_t *) hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->uzone_current ));
389
390    vpn_base = base >> CONFIG_PPM_PAGE_SHIFT;
391    vpn_size = size >> CONFIG_PPM_PAGE_SHIFT;
392
393    // get pointer on parent process in parent thread cluster
394    parent_process = (process_t *)hal_remote_lpt( XPTR( parent_cxy,
395                                                        &parent_ptr->process ) );
396 
397    // get extended pointer on parent GPT in parent thread cluster
398    parent_gpt_xp = XPTR( parent_cxy , &parent_process->vmm.gpt );
399
400    // allocate memory for child thread descriptor
401    child_ptr = thread_alloc();
402    if( child_ptr == NULL )
403    {
404        printk("\n[ERROR] in %s : cannot allocate new thread\n", __FUNCTION__ );
405        return -1;
406    }
407
408    // initialize thread descriptor
409    error = thread_init( child_ptr,
410                         child_process,
411                         THREAD_USER,
412                         func,
413                         args,
414                         core_lid,
415                         base,
416                         size );
417    if( error )
418    {
419            printk("\n[ERROR] in %s : cannot initialize child thread\n", __FUNCTION__ );
420        thread_release( child_ptr );
421        return EINVAL;
422    }
423
424    // return child pointer
425    *child_thread = child_ptr;
426
427    // set detached flag if required
428    if( flags & THREAD_FLAG_DETACHED ) child_ptr->flags = THREAD_FLAG_DETACHED;
429
430    // update uzone pointer in child thread descriptor
431    child_ptr->uzone_current = (char *)((intptr_t)uzone +
432                                        (intptr_t)child_ptr - 
433                                        (intptr_t)parent_ptr );
434 
435
436    // allocate CPU context for child thread
437        if( hal_cpu_context_alloc( child_ptr ) )
438    {
439            printk("\n[ERROR] in %s : cannot allocate CPU context\n", __FUNCTION__ );
440        thread_release( child_ptr );
441        return -1;
442    }
443
444    // allocate FPU context for child thread
445        if( hal_fpu_context_alloc( child_ptr ) )
446    {
447            printk("\n[ERROR] in %s : cannot allocate FPU context\n", __FUNCTION__ );
448        thread_release( child_ptr );
449        return -1;
450    }
451
452    // create and initialize STACK vseg
453    vseg = vseg_alloc();
454    vseg_init( vseg,
455               VSEG_TYPE_STACK,
456               base,
457               size,
458               vpn_base,
459               vpn_size,
460               0, 0, XPTR_NULL,                         // not a file vseg
461               local_cxy );
462
463    // register STACK vseg in local child VSL
464    vseg_attach( &child_process->vmm , vseg );
465
466    // copy all valid STACK GPT entries   
467    vpn_t          vpn;
468    bool_t         mapped;
469    ppn_t          ppn;
470    for( vpn = vpn_base ; vpn < (vpn_base + vpn_size) ; vpn++ )
471    {
472        error = hal_gpt_pte_copy( &child_process->vmm.gpt,
473                                  parent_gpt_xp,
474                                  vpn,
475                                  true,                 // set cow
476                                  &ppn,
477                                  &mapped );
478        if( error )
479        {
480            vseg_detach( &child_process->vmm , vseg );
481            vseg_free( vseg );
482            thread_release( child_ptr );
483            printk("\n[ERROR] in %s : cannot update child GPT\n", __FUNCTION__ );
484            return -1;
485        }
486
487        // increment pending forks counter for the page if mapped
488        if( mapped )
489        {
490            xptr_t   page_xp  = ppm_ppn2page( ppn );
491            cxy_t    page_cxy = GET_CXY( page_xp );
492            page_t * page_ptr = (page_t *)GET_PTR( page_xp );
493            hal_remote_atomic_add( XPTR( page_cxy , &page_ptr->forks ) , 1 );
494
495#if (CONFIG_DEBUG_THREAD_USER_FORK & 1)
496cycle = (uint32_t)hal_get_cycles();
497if( CONFIG_DEBUG_THREAD_USER_FORK < cycle )
498printk("\n[DBG] %s : thread %x copied stack PTE to child GPT : vpn %x\n",
499__FUNCTION__, CURRENT_THREAD, vpn );
500#endif
501
502        }
503    }
504
505    // set COW flag for all mapped entries of STAK vseg in parent thread GPT
506    hal_gpt_set_cow( parent_gpt_xp,
507                     vpn_base,
508                     vpn_size );
509 
510        // update DQDT for child thread
511    dqdt_local_update_threads( 1 );
512
513#if CONFIG_DEBUG_THREAD_USER_FORK
514cycle = (uint32_t)hal_get_cycles();
515if( CONFIG_DEBUG_THREAD_USER_FORK < cycle )
516printk("\n[DBG] %s : thread %x exit / child_process %x / child_thread %x / cycle %d\n",
517__FUNCTION__, CURRENT_THREAD, child_process->pid, child_ptr, cycle );
518#endif
519
520        return 0;
521
522}  // end thread_user_fork()
523
524/////////////////////////////////////////////////////////
525error_t thread_kernel_create( thread_t     ** new_thread,
526                              thread_type_t   type,
527                              void          * func,
528                              void          * args,
529                                              lid_t           core_lid )
530{
531    error_t        error;
532        thread_t     * thread;       // pointer on new thread descriptor
533
534    assert( ( (type == THREAD_IDLE) || (type == THREAD_RPC) || (type == THREAD_DEV) ) ,
535    __FUNCTION__ , "illegal thread type" );
536
537    assert( (core_lid < LOCAL_CLUSTER->cores_nr) ,
538            __FUNCTION__ , "illegal core_lid" );
539
540#if CONFIG_DEBUG_THREAD_KERNEL_CREATE
541uint32_t cycle = (uint32_t)hal_get_cycles();
542if( CONFIG_DEBUG_THREAD_KERNEL_CREATE < cycle )
543printk("\n[DBG] %s : thread %x enter / requested_type %s / cycle %d\n",
544__FUNCTION__, CURRENT_THREAD, thread, thread_type_str(type), cycle );
545#endif
546
547    // allocate memory for new thread descriptor
548    thread = thread_alloc();
549
550    if( thread == NULL ) return ENOMEM;
551
552    // initialize thread descriptor
553    error = thread_init( thread,
554                         &process_zero,
555                         type,
556                         func,
557                         args,
558                         core_lid,
559                         0 , 0 );  // no user stack for a kernel thread
560
561    if( error ) // release allocated memory for thread descriptor
562    {
563        thread_release( thread );
564        return EINVAL;
565    }
566
567    // allocate & initialize CPU context
568        hal_cpu_context_create( thread );
569
570        // update DQDT for kernel thread
571    dqdt_local_update_threads( 1 );
572
573#if CONFIG_DEBUG_THREAD_KERNEL_CREATE
574cycle = (uint32_t)hal_get_cycles();
575if( CONFIG_DEBUG_THREAD_KERNEL_CREATE < cycle )
576printk("\n[DBG] %s : thread %x exit / new_thread %x / type %s / cycle %d\n",
577__FUNCTION__, CURRENT_THREAD, thread, thread_type_str(type), cycle );
578#endif
579
580    *new_thread = thread;
581        return 0;
582
583} // end thread_kernel_create()
584
585///////////////////////////////////////////////////
586error_t thread_kernel_init( thread_t      * thread,
587                            thread_type_t   type,
588                            void          * func,
589                            void          * args,
590                                            lid_t           core_lid )
591{
592    assert( (type == THREAD_IDLE) , __FUNCTION__ , "illegal thread type" );
593
594    assert( (core_lid < LOCAL_CLUSTER->cores_nr) , __FUNCTION__ , "illegal core index" );
595
596    error_t  error = thread_init( thread,
597                                  &process_zero,
598                                  type,
599                                  func,
600                                  args,
601                                  core_lid,
602                                  0 , 0 );   // no user stack for a kernel thread
603
604    // allocate & initialize CPU context if success
605    if( error == 0 ) hal_cpu_context_create( thread );
606
607    return error;
608
609}  // end thread_kernel_init()
610
611///////////////////////////////////////////////////////////////////////////////////////
612// TODO: check that all memory dynamically allocated during thread execution
613// has been released, using a cache of mmap and malloc requests. [AG]
614///////////////////////////////////////////////////////////////////////////////////////
615void thread_destroy( thread_t * thread )
616{
617    reg_t        save_sr;
618
619    process_t  * process    = thread->process;
620    core_t     * core       = thread->core;
621
622#if CONFIG_DEBUG_THREAD_DESTROY
623uint32_t cycle = (uint32_t)hal_get_cycles();
624if( CONFIG_DEBUG_THREAD_DESTROY < cycle )
625printk("\n[DBG] %s : thread %x enter to destroy thread %x in process %x / cycle %d\n",
626__FUNCTION__, CURRENT_THREAD, thread, process->pid, cycle );
627#endif
628
629    assert( (thread->children_nr == 0) , __FUNCTION__ , "still attached children" );
630
631    assert( (thread->local_locks == 0) , __FUNCTION__ , "all local locks not released" );
632
633    assert( (thread->remote_locks == 0) , __FUNCTION__ , "all remote locks not released" );
634
635    // update intrumentation values
636        process->vmm.pgfault_nr += thread->info.pgfault_nr;
637
638    // release memory allocated for CPU context and FPU context
639        hal_cpu_context_destroy( thread );
640        if ( thread->type == THREAD_USER ) hal_fpu_context_destroy( thread );
641       
642    // release FPU ownership if required
643        hal_disable_irq( &save_sr );
644        if( core->fpu_owner == thread )
645        {
646                core->fpu_owner = NULL;
647                hal_fpu_disable();
648        }
649        hal_restore_irq( save_sr );
650
651    // remove thread from process th_tbl[]
652    process_remove_thread( thread );
653       
654    // update local DQDT
655    dqdt_local_update_threads( -1 );
656
657    // invalidate thread descriptor
658        thread->signature = 0;
659
660    // release memory for thread descriptor
661    thread_release( thread );
662
663#if CONFIG_DEBUG_THREAD_DESTROY
664cycle = (uint32_t)hal_get_cycles();
665if( CONFIG_DEBUG_THREAD_DESTROY < cycle )
666printk("\n[DBG] %s : thread %x exit / destroyed thread %x in process %x / cycle %d\n",
667__FUNCTION__, CURRENT_THREAD, thread, process->pid, cycle );
668#endif
669
670}   // end thread_destroy()
671
672/////////////////////////////////////////////////
673void thread_child_parent_link( xptr_t  xp_parent,
674                               xptr_t  xp_child )
675{
676    // get extended pointers on children list root
677    cxy_t      parent_cxy = GET_CXY( xp_parent );
678    thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent );
679    xptr_t     root       = XPTR( parent_cxy , &parent_ptr->children_root );
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    // set the link
687    xlist_add_first( root , entry );
688    hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr ) , 1 );
689
690}  // end thread_child_parent_link()
691
692///////////////////////////////////////////////////
693void thread_child_parent_unlink( xptr_t  xp_parent,
694                                 xptr_t  xp_child )
695{
696    // get extended pointer on children list lock
697    cxy_t      parent_cxy = GET_CXY( xp_parent );
698    thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent );
699    xptr_t     lock       = XPTR( parent_cxy , &parent_ptr->children_lock );
700
701    // get extended pointer on children list entry
702    cxy_t      child_cxy  = GET_CXY( xp_child );
703    thread_t * child_ptr  = (thread_t *)GET_PTR( xp_child );
704    xptr_t     entry      = XPTR( child_cxy , &child_ptr->brothers_list );
705
706    // get the lock
707    remote_spinlock_lock( lock );
708
709    // remove the link
710    xlist_unlink( entry );
711    hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr ) , -1 );
712
713    // release the lock
714    remote_spinlock_unlock( lock );
715
716}  // thread_child_parent_unlink()
717
718//////////////////////////////////////////////////
719inline void thread_set_req_ack( thread_t * target,
720                                uint32_t * rsp_count )
721{
722    reg_t    save_sr;   // for critical section
723
724    // get pointer on target thread scheduler
725    scheduler_t * sched = &target->core->scheduler;
726
727    // wait scheduler ready to handle a new request
728    while( sched->req_ack_pending ) asm volatile( "nop" );
729   
730    // enter critical section
731    hal_disable_irq( &save_sr );
732     
733    // set request in target thread scheduler
734    sched->req_ack_pending = true;
735
736    // set ack request in target thread "flags"
737    hal_atomic_or( &target->flags , THREAD_FLAG_REQ_ACK );
738
739    // set pointer on responses counter in target thread
740    target->ack_rsp_count = rsp_count;
741   
742    // exit critical section
743    hal_restore_irq( save_sr );
744
745    hal_fence();
746
747}  // thread_set_req_ack()
748
749/////////////////////////////////////////////////////
750inline void thread_reset_req_ack( thread_t * target )
751{
752    reg_t    save_sr;   // for critical section
753
754    // get pointer on target thread scheduler
755    scheduler_t * sched = &target->core->scheduler;
756
757    // check signal pending in scheduler
758    assert( sched->req_ack_pending , __FUNCTION__ , "no pending signal" );
759   
760    // enter critical section
761    hal_disable_irq( &save_sr );
762     
763    // reset signal in scheduler
764    sched->req_ack_pending = false;
765
766    // reset signal in thread "flags"
767    hal_atomic_and( &target->flags , ~THREAD_FLAG_REQ_ACK );
768
769    // reset pointer on responses counter
770    target->ack_rsp_count = NULL;
771   
772    // exit critical section
773    hal_restore_irq( save_sr );
774
775    hal_fence();
776
777}  // thread_reset_req_ack()
778
779////////////////////////////////
780inline bool_t thread_can_yield()
781{
782    thread_t * this = CURRENT_THREAD;
783    return (this->local_locks == 0) && (this->remote_locks == 0);
784}
785
786/////////////////////////
787void thread_check_sched()
788{
789    thread_t * this = CURRENT_THREAD;
790
791        if( (this->local_locks == 0) && 
792        (this->remote_locks == 0) &&
793        (this->flags & THREAD_FLAG_SCHED) ) 
794    {
795        this->flags &= ~THREAD_FLAG_SCHED;
796        sched_yield( "delayed scheduling" );
797    }
798
799}  // end thread_check_sched()
800
801/////////////////////////////////////
802void thread_block( thread_t * thread,
803                   uint32_t   cause )
804{
805    // set blocking cause
806    hal_atomic_or( &thread->blocked , cause );
807    hal_fence();
808
809#if CONFIG_DEBUG_THREAD_BLOCK
810uint32_t cycle = (uint32_t)hal_get_cycles();
811if( CONFIG_DEBUG_THREAD_BLOCK < cycle )
812printk("\n[DBG] %s : thread %x blocked thread %x / cause %x / state %x / cycle %d\n",
813__FUNCTION__ , CURRENT_THREAD , thread , cause , thread->blocked , cycle );
814#endif
815
816} // end thread_block()
817
818////////////////////////////////////////////
819uint32_t thread_unblock( xptr_t   thread_xp,
820                         uint32_t cause )
821{
822    // get thread cluster and local pointer
823    cxy_t      cxy = GET_CXY( thread_xp );
824    thread_t * ptr = GET_PTR( thread_xp );
825
826    // reset blocking cause
827    uint32_t previous = hal_remote_atomic_and( XPTR( cxy , &ptr->blocked ) , ~cause );
828    hal_fence();
829
830#if CONFIG_DEBUG_THREAD_BLOCK
831uint32_t cycle = (uint32_t)hal_get_cycles();
832if( CONFIG_DEBUG_THREAD_BLOCK < cycle )
833printk("\n[DBG] %s : thread %x unblocked thread %x / cause %x / state %x / cycle %d\n",
834__FUNCTION__ , CURRENT_THREAD , ptr , cause , ptr->blocked , cycle );
835#endif
836
837    // return a non zero value if the cause bit is modified
838    return( previous & cause );
839
840}  // end thread_unblock()
841
842/////////////////////////////////////
843void thread_kill( thread_t * target )
844{
845    volatile uint32_t  rsp_count = 1;     // responses counter
846
847    thread_t * killer = CURRENT_THREAD;
848
849#if CONFIG_DEBUG_THREAD_KILL
850uint32_t cycle  = (uint32_t)hal_get_cycles;
851if( CONFIG_DEBUG_THREAD_KILL < cycle )
852printk("\n[DBG] %s : thread %x enter for target thread %x / cycle %d\n",
853__FUNCTION__, killer, target, cycle );
854#endif
855
856    // set the global blocked bit in target thread descriptor.
857    thread_block( target , THREAD_BLOCKED_GLOBAL );
858
859    // request target scheduler to deschedule the target thread
860    // when killer thread is not running on same core as target thread
861    if( killer->core->lid != target->core->lid )
862    {
863        // set signal in target thread descriptor and in target scheduler
864        thread_set_req_ack( target , (void *)(&rsp_count) );
865
866        // send an IPI to the target thread core.
867        dev_pic_send_ipi( local_cxy , target->core->lid );
868
869        // poll the response
870        while( 1 )
871        {
872            // exit when response received from scheduler
873            if( rsp_count == 0 )  break;
874
875            // deschedule without blocking
876            hal_fixed_delay( 1000 );
877        }
878    }
879
880        // set REQ_DELETE flag
881        hal_atomic_or( &target->flags , THREAD_FLAG_REQ_DELETE );
882
883#if CONFIG_DEBUG_THREAD_KILL
884cycle  = (uint32_t)hal_get_cycles;
885if( CONFIG_DEBUG_THREAD_KILL < cycle )
886printk("\n[DBG] %s : thread %x exit for target thread %x / cycle %d\n",
887__FUNCTION__, killer, target, cycle );
888#endif
889
890}  // end thread_kill()
891
892///////////////////////
893void thread_idle_func()
894{
895    while( 1 )
896    {
897        // unmask IRQs
898        hal_enable_irq( NULL );
899
900        if( CONFIG_THREAD_IDLE_MODE_SLEEP ) // force core to low-power mode
901        {
902
903#if CONFIG_DEBUG_THREAD_IDLE
904uint32_t cycle  = (uint32_t)hal_get_cycles;
905thread_t * this = CURRENT_THREAD;
906if( CONFIG_DEBUG_THREAD_IDLE < cycle )
907printk("\n[DBG] %s : idle thread %x on core[%x,%d] goes to sleep / cycle %d\n",
908__FUNCTION__, this, local_cxy, this->core->lid, cycle );
909#endif
910
911            hal_core_sleep();
912
913#if CONFIG_DEBUG_THREAD_IDLE
914cycle  = (uint32_t)hal_get_cycles;
915if( CONFIG_DEBUG_THREAD_IDLE < cycle )
916printk("\n[DBG] %s : idle thread %x on core[%x,%d] wake up / cycle %d\n",
917__FUNCTION__, this, local_cxy, this->core->lid, cycle );
918#endif
919
920        }
921        else                                // search a runable thread
922        {
923            sched_yield( "IDLE" );
924        }
925    }
926}  // end thread_idle()
927
928
929/////////////////////////////////////////////////
930void thread_user_time_update( thread_t * thread )
931{
932    // TODO
933    // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ );
934}
935
936///////////////////////////////////////////////////
937void thread_kernel_time_update( thread_t * thread )
938{
939    // TODO
940    // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ );
941}
942
943/////////////////////////////////////
944xptr_t thread_get_xptr( pid_t    pid,
945                        trdid_t  trdid )
946{
947    cxy_t         target_cxy;          // target thread cluster identifier
948    ltid_t        target_thread_ltid;  // target thread local index
949    thread_t    * target_thread_ptr;   // target thread local pointer
950    xptr_t        target_process_xp;   // extended pointer on target process descriptor
951    process_t   * target_process_ptr;  // local pointer on target process descriptor
952    pid_t         target_process_pid;  // target process identifier
953    xlist_entry_t root;                // root of list of process in target cluster
954    xptr_t        lock_xp;             // extended pointer on lock protecting  this list
955
956    // get target cluster identifier and local thread identifier
957    target_cxy         = CXY_FROM_TRDID( trdid );
958    target_thread_ltid = LTID_FROM_TRDID( trdid );
959
960    // get root of list of process descriptors in target cluster
961    hal_remote_memcpy( XPTR( local_cxy  , &root ),
962                       XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ),
963                       sizeof(xlist_entry_t) );
964
965    // get extended pointer on lock protecting the list of processes
966    lock_xp = XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_lock );
967
968    // take the lock protecting the list of processes in target cluster
969    remote_spinlock_lock( lock_xp );
970
971    // loop on list of process in target cluster to find the PID process
972    xptr_t  iter;
973    bool_t  found = false;
974    XLIST_FOREACH( XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ) , iter )
975    {
976        target_process_xp  = XLIST_ELEMENT( iter , process_t , local_list );
977        target_process_ptr = (process_t *)GET_PTR( target_process_xp );
978        target_process_pid = hal_remote_lw( XPTR( target_cxy , &target_process_ptr->pid ) );
979        if( target_process_pid == pid )
980        {
981            found = true;
982            break;
983        }
984    }
985
986    // release the lock protecting the list of processes in target cluster
987    remote_spinlock_unlock( lock_xp );
988
989    // check target thread found
990    if( found == false )
991    {
992        return XPTR_NULL;
993    }
994
995    // get target thread local pointer
996    xptr_t xp = XPTR( target_cxy , &target_process_ptr->th_tbl[target_thread_ltid] );
997    target_thread_ptr = (thread_t *)hal_remote_lpt( xp );
998
999    if( target_thread_ptr == NULL )
1000    {
1001        return XPTR_NULL;
1002    }
1003
1004    return XPTR( target_cxy , target_thread_ptr );
1005}
1006
Note: See TracBrowser for help on using the repository browser.