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

Last change on this file since 436 was 436, checked in by alain, 4 years ago

1) improve the threads and process destruction mechanism.
2) introduce FIFOs in the soclib_tty driver.

File size: 38.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
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( xptr_t   thread_xp,
803                   uint32_t cause )
804{
805    // get thread cluster and local pointer
806    cxy_t      cxy = GET_CXY( thread_xp );
807    thread_t * ptr = GET_PTR( thread_xp );
808
809    // set blocking cause
810    hal_remote_atomic_or( XPTR( cxy , &ptr->blocked ) , cause );
811    hal_fence();
812
813#if CONFIG_DEBUG_THREAD_BLOCK
814uint32_t cycle = (uint32_t)hal_get_cycles();
815if( CONFIG_DEBUG_THREAD_BLOCK < cycle )
816printk("\n[DBG] %s : thread %x blocked thread %x / cause %x / cycle %d\n",
817__FUNCTION__ , CURRENT_THREAD , ptr , cause , cycle );
818#endif
819
820#if (CONFIG_DEBUG_THREAD_BLOCK & 1)
821if( CONFIG_DEBUG_THREAD_BLOCK < cycle )
822sched_display( ptr->core->lid );
823#endif
824
825} // end thread_block()
826
827////////////////////////////////////////////
828uint32_t thread_unblock( xptr_t   thread_xp,
829                         uint32_t cause )
830{
831    // get thread cluster and local pointer
832    cxy_t      cxy = GET_CXY( thread_xp );
833    thread_t * ptr = GET_PTR( thread_xp );
834
835    // reset blocking cause
836    uint32_t previous = hal_remote_atomic_and( XPTR( cxy , &ptr->blocked ) , ~cause );
837    hal_fence();
838
839#if CONFIG_DEBUG_THREAD_BLOCK
840uint32_t cycle = (uint32_t)hal_get_cycles();
841if( CONFIG_DEBUG_THREAD_BLOCK < cycle )
842printk("\n[DBG] %s : thread %x unblocked thread %x / cause %x / cycle %d\n",
843__FUNCTION__ , CURRENT_THREAD , ptr , cause , cycle );
844#endif
845
846#if (CONFIG_DEBUG_THREAD_BLOCK & 1)
847if( CONFIG_DEBUG_THREAD_BLOCK < cycle )
848sched_display( ptr->core->lid );
849#endif
850
851    // return a non zero value if the cause bit is modified
852    return( previous & cause );
853
854}  // end thread_unblock()
855
856////////////////////////////////////
857void thread_kill( xptr_t  target_xp,
858                  bool_t  is_exit,
859                  bool_t  is_forced )
860{
861    reg_t       save_sr;                // for critical section
862    bool_t      attached;               // target thread in attached mode
863    bool_t      join_done;              // joining thread arrived first
864    xptr_t      killer_xp;              // extended pointer on killer thread (this)
865    thread_t  * killer_ptr;             // pointer on killer thread (this)
866    cxy_t       target_cxy;             // target thread cluster     
867    thread_t  * target_ptr;             // pointer on target thread
868    xptr_t      joining_xp;             // extended pointer on joining thread
869    thread_t  * joining_ptr;            // pointer on joining thread
870    cxy_t       joining_cxy;            // joining thread cluster
871    pid_t       target_pid;             // target process PID
872    cxy_t       owner_cxy;              // target process owner cluster
873    trdid_t     target_trdid;           // target thread identifier
874    ltid_t      target_ltid;            // target thread local index
875    xptr_t      process_state_xp;       // extended pointer on <term_state> in process
876
877    xptr_t      target_flags_xp;        // extended pointer on target thread <flags>
878    xptr_t      target_join_lock_xp;    // extended pointer on target thread <join_lock>
879    xptr_t      target_join_xp_xp;      // extended pointer on target thread <join_xp>
880    xptr_t      target_process_xp;      // extended pointer on target thread <process>
881
882    process_t * target_process;         // pointer on target thread process
883
884    // get target thread cluster and pointer
885    target_cxy = GET_CXY( target_xp );
886    target_ptr = GET_PTR( target_xp );
887
888    // get killer thread pointers
889    killer_ptr = CURRENT_THREAD;
890    killer_xp  = XPTR( local_cxy , killer_ptr );
891
892#if CONFIG_DEBUG_THREAD_KILL
893uint32_t cycle  = (uint32_t)hal_get_cycles;
894if( CONFIG_DEBUG_THREAD_KILL < cycle )
895printk("\n[DBG] %s : thread %x enter for target thread %x / cycle %d\n",
896__FUNCTION__, killer_ptr, target_ptr, cycle );
897#endif
898
899    // block the target thread
900    thread_block( target_xp , THREAD_BLOCKED_GLOBAL );
901
902    // get target thread attached mode
903    target_flags_xp = XPTR( target_cxy , &target_ptr->flags );
904    attached = ((hal_remote_lw( target_flags_xp ) & THREAD_FLAG_DETACHED) == 0);
905
906    // synchronize with the joining thread
907    // if the target thread is attached && not forced
908
909    if( attached  && (is_forced == false) )
910    {
911        // build extended pointers on target thread join fields
912        target_join_lock_xp  = XPTR( target_cxy , &target_ptr->join_lock );
913        target_join_xp_xp    = XPTR( target_cxy , &target_ptr->join_xp );
914
915        // enter critical section
916        hal_disable_irq( &save_sr );
917
918        // take the join_lock in target thread descriptor
919        remote_spinlock_lock( target_join_lock_xp );
920
921        // get join_done from target thread descriptor
922        join_done = ((hal_remote_lw( target_flags_xp ) & THREAD_FLAG_JOIN_DONE) != 0);
923   
924        if( join_done )     // joining thread arrived first
925        {
926            // get extended pointer on joining thread
927            joining_xp  = (xptr_t)hal_remote_lwd( target_join_xp_xp );
928            joining_ptr = GET_PTR( joining_xp );
929            joining_cxy = GET_CXY( joining_xp );
930           
931            // reset the join_done flag in target thread
932            hal_remote_atomic_and( target_flags_xp , ~THREAD_FLAG_JOIN_DONE );
933
934            // unblock the joining thread
935            thread_unblock( joining_xp , THREAD_BLOCKED_JOIN );
936
937            // release the join_lock in target thread descriptor
938            remote_spinlock_unlock( target_join_lock_xp );
939
940            // restore IRQs
941            hal_restore_irq( save_sr );
942        }
943        else                // this thread arrived first
944        {
945            // set the kill_done flag in target thread
946            hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_KILL_DONE );
947
948            // block this thread on BLOCKED_JOIN
949            thread_block( killer_xp , THREAD_BLOCKED_JOIN );
950
951            // set extended pointer on killer thread in target thread
952            hal_remote_swd( target_join_xp_xp , killer_xp );
953
954            // release the join_lock in target thread descriptor
955            remote_spinlock_unlock( target_join_lock_xp );
956
957            // deschedule
958            sched_yield( "killer thread wait joining thread" );
959
960            // restore IRQs
961            hal_restore_irq( save_sr );
962        }
963    }  // end if attached
964
965    // - if the target thread is the main thread
966    //   => synchronize with the parent process main thread
967    // - if the target thread is not the main thread
968    //   => simply mark the target thread for delete
969
970    // get pointer on target thread process
971    target_process_xp  = XPTR( target_cxy , &target_ptr->process );
972    target_process     = (process_t *)hal_remote_lpt( target_process_xp ); 
973
974        // get target process owner cluster
975        target_pid = hal_remote_lw( XPTR( target_cxy , &target_process->pid ) );
976    owner_cxy = CXY_FROM_PID( target_pid );
977
978    // get target thread local index
979    target_trdid = hal_remote_lw( XPTR( target_cxy , &target_ptr->trdid ) );
980    target_ltid  = LTID_FROM_TRDID( target_trdid );
981
982    if( (owner_cxy == target_cxy) && (target_ltid == 0) )     // main thread
983    {
984        // get extended pointer on term_state in target process owner cluster
985        process_state_xp = XPTR( owner_cxy , &target_process->term_state );
986
987        // set termination info in target process owner 
988        if( is_exit ) hal_remote_atomic_or( process_state_xp , PROCESS_TERM_EXIT );
989        else          hal_remote_atomic_or( process_state_xp , PROCESS_TERM_KILL );
990
991#if CONFIG_DEBUG_THREAD_KILL
992cycle  = (uint32_t)hal_get_cycles;
993if( CONFIG_DEBUG_THREAD_KILL < cycle )
994printk("\n[DBG] %s : thread %x exit for thread %x / main thread / cycle %d\n",
995__FUNCTION__, killer_ptr, target_ptr, cycle );
996#endif
997
998    }
999    else                                                      // main thread
1000    {
1001        // set the REQ_DELETE flag in target thread descriptor
1002        hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
1003
1004#if CONFIG_DEBUG_THREAD_KILL
1005cycle  = (uint32_t)hal_get_cycles;
1006if( CONFIG_DEBUG_THREAD_KILL < cycle )
1007printk("\n[DBG] %s : thread %x exit for thread %x / not the main thread / cycle %d\n",
1008__FUNCTION__, killer_ptr, target_ptr, cycle );
1009#endif
1010
1011    }
1012
1013}  // end thread_kill()
1014
1015///////////////////////
1016void thread_idle_func()
1017{
1018    while( 1 )
1019    {
1020        // unmask IRQs
1021        hal_enable_irq( NULL );
1022
1023        if( CONFIG_THREAD_IDLE_MODE_SLEEP ) // force core to low-power mode
1024        {
1025
1026#if CONFIG_DEBUG_THREAD_IDLE
1027uint32_t cycle  = (uint32_t)hal_get_cycles;
1028thread_t * this = CURRENT_THREAD;
1029if( CONFIG_DEBUG_THREAD_IDLE < cycle )
1030printk("\n[DBG] %s : idle thread %x on core[%x,%d] goes to sleep / cycle %d\n",
1031__FUNCTION__, this, local_cxy, this->core->lid, cycle );
1032#endif
1033
1034            hal_core_sleep();
1035
1036#if CONFIG_DEBUG_THREAD_IDLE
1037cycle  = (uint32_t)hal_get_cycles;
1038if( CONFIG_DEBUG_THREAD_IDLE < cycle )
1039printk("\n[DBG] %s : idle thread %x on core[%x,%d] wake up / cycle %d\n",
1040__FUNCTION__, this, local_cxy, this->core->lid, cycle );
1041#endif
1042
1043        }
1044        else                                // search a runable thread
1045        {
1046            sched_yield( "IDLE" );
1047        }
1048    }
1049}  // end thread_idle()
1050
1051
1052/////////////////////////////////////////////////
1053void thread_user_time_update( thread_t * thread )
1054{
1055    // TODO
1056    // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ );
1057}
1058
1059///////////////////////////////////////////////////
1060void thread_kernel_time_update( thread_t * thread )
1061{
1062    // TODO
1063    // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ );
1064}
1065
1066/////////////////////////////////////
1067xptr_t thread_get_xptr( pid_t    pid,
1068                        trdid_t  trdid )
1069{
1070    cxy_t         target_cxy;          // target thread cluster identifier
1071    ltid_t        target_thread_ltid;  // target thread local index
1072    thread_t    * target_thread_ptr;   // target thread local pointer
1073    xptr_t        target_process_xp;   // extended pointer on target process descriptor
1074    process_t   * target_process_ptr;  // local pointer on target process descriptor
1075    pid_t         target_process_pid;  // target process identifier
1076    xlist_entry_t root;                // root of list of process in target cluster
1077    xptr_t        lock_xp;             // extended pointer on lock protecting  this list
1078
1079    // get target cluster identifier and local thread identifier
1080    target_cxy         = CXY_FROM_TRDID( trdid );
1081    target_thread_ltid = LTID_FROM_TRDID( trdid );
1082
1083    // check trdid argument
1084        if( (target_thread_ltid >= CONFIG_THREAD_MAX_PER_CLUSTER) || 
1085        cluster_is_undefined( target_cxy ) )         return XPTR_NULL;
1086
1087    // get root of list of process descriptors in target cluster
1088    hal_remote_memcpy( XPTR( local_cxy  , &root ),
1089                       XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ),
1090                       sizeof(xlist_entry_t) );
1091
1092    // get extended pointer on lock protecting the list of processes
1093    lock_xp = XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_lock );
1094
1095    // take the lock protecting the list of processes in target cluster
1096    remote_spinlock_lock( lock_xp );
1097
1098    // loop on list of process in target cluster to find the PID process
1099    xptr_t  iter;
1100    bool_t  found = false;
1101    XLIST_FOREACH( XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ) , iter )
1102    {
1103        target_process_xp  = XLIST_ELEMENT( iter , process_t , local_list );
1104        target_process_ptr = (process_t *)GET_PTR( target_process_xp );
1105        target_process_pid = hal_remote_lw( XPTR( target_cxy , &target_process_ptr->pid ) );
1106        if( target_process_pid == pid )
1107        {
1108            found = true;
1109            break;
1110        }
1111    }
1112
1113    // release the lock protecting the list of processes in target cluster
1114    remote_spinlock_unlock( lock_xp );
1115
1116    // check PID found
1117    if( found == false ) return XPTR_NULL;
1118
1119    // get target thread local pointer
1120    xptr_t xp = XPTR( target_cxy , &target_process_ptr->th_tbl[target_thread_ltid] );
1121    target_thread_ptr = (thread_t *)hal_remote_lpt( xp );
1122
1123    if( target_thread_ptr == NULL )  return XPTR_NULL;
1124
1125    return XPTR( target_cxy , target_thread_ptr );
1126}
1127
Note: See TracBrowser for help on using the repository browser.