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

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

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

File size: 41.6 KB
Line 
1/*
2 * thread.c -  implementation of thread operations (user & kernel)
3 *
4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *         Alain Greiner (2016,2017)
6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <kernel_config.h>
26#include <hal_types.h>
27#include <hal_context.h>
28#include <hal_irqmask.h>
29#include <hal_special.h>
30#include <hal_remote.h>
31#include <memcpy.h>
32#include <printk.h>
33#include <cluster.h>
34#include <process.h>
35#include <scheduler.h>
36#include <dev_pic.h>
37#include <core.h>
38#include <list.h>
39#include <xlist.h>
40#include <page.h>
41#include <kmem.h>
42#include <ppm.h>
43#include <thread.h>
44
45//////////////////////////////////////////////////////////////////////////////////////
46// Extern global variables
47//////////////////////////////////////////////////////////////////////////////////////
48
49extern process_t      process_zero;
50
51//////////////////////////////////////////////////////////////////////////////////////
52// This function returns a printable string for the thread type.
53//////////////////////////////////////////////////////////////////////////////////////
54char * thread_type_str( uint32_t type )
55{
56    if     ( type == THREAD_USER   ) return "USR";
57    else if( type == THREAD_RPC    ) return "RPC";
58    else if( type == THREAD_DEV    ) return "DEV";
59    else if( type == THREAD_IDLE   ) return "IDL";
60    else                             return "undefined";
61}
62
63/////////////////////////////////////////////////////////////////////////////////////
64// This static function allocates physical memory for a thread descriptor.
65// It can be called by the three functions:
66// - thread_user_create()
67// - thread_user_fork()
68// - thread_kernel_create()
69/////////////////////////////////////////////////////////////////////////////////////
70// @ return pointer on thread descriptor if success / return NULL if failure.
71/////////////////////////////////////////////////////////////////////////////////////
72static thread_t * thread_alloc()
73{
74        page_t       * page;   // pointer on page descriptor containing thread descriptor
75        kmem_req_t     req;    // kmem request
76
77        // allocates memory for thread descriptor + kernel stack
78        req.type  = KMEM_PAGE;
79        req.size  = CONFIG_THREAD_DESC_ORDER;
80        req.flags = AF_KERNEL | AF_ZERO;
81        page      = kmem_alloc( &req );
82
83        if( page == NULL ) return NULL;
84
85    // return pointer on new thread descriptor
86    xptr_t base_xp = ppm_page2base( XPTR(local_cxy , page ) );
87    return (thread_t *)GET_PTR( base_xp );
88
89}  // end thread_alloc()
90 
91
92/////////////////////////////////////////////////////////////////////////////////////
93// This static function releases the physical memory for a thread descriptor.
94// It is called by the three functions:
95// - thread_user_create()
96// - thread_user_fork()
97// - thread_kernel_create()
98/////////////////////////////////////////////////////////////////////////////////////
99// @ thread  : pointer on thread descriptor.
100/////////////////////////////////////////////////////////////////////////////////////
101static void thread_release( thread_t * thread )
102{
103    kmem_req_t   req;
104
105    xptr_t base_xp = ppm_base2page( XPTR(local_cxy , thread ) );
106
107    req.type  = KMEM_PAGE;
108    req.ptr   = GET_PTR( base_xp );
109    kmem_free( &req );
110}
111
112/////////////////////////////////////////////////////////////////////////////////////
113// This static function initializes a thread descriptor (kernel or user).
114// It can be called by the four functions:
115// - thread_user_create()
116// - thread_user_fork()
117// - thread_kernel_create()
118// - thread_idle_init()
119// It updates the local DQDT.
120/////////////////////////////////////////////////////////////////////////////////////
121// @ thread       : pointer on thread descriptor
122// @ process      : pointer on process descriptor.
123// @ type         : thread type.
124// @ func         : pointer on thread entry function.
125// @ args         : pointer on thread entry function arguments.
126// @ core_lid     : target core local index.
127// @ u_stack_base : stack base (user thread only)
128// @ u_stack_size : stack base (user thread only)
129/////////////////////////////////////////////////////////////////////////////////////
130static error_t thread_init( thread_t      * thread,
131                            process_t     * process,
132                            thread_type_t   type,
133                            void          * func,
134                            void          * args,
135                            lid_t           core_lid,
136                            intptr_t        u_stack_base,
137                            uint32_t        u_stack_size )
138{
139    error_t        error;
140    trdid_t        trdid;      // allocated thread identifier
141
142        cluster_t    * local_cluster = LOCAL_CLUSTER;
143
144    // register new thread in process descriptor, and get a TRDID
145    error = process_register_thread( process, thread , &trdid );
146
147    if( error )
148    {
149        printk("\n[ERROR] in %s : cannot get TRDID\n", __FUNCTION__ );
150        return EINVAL;
151    }
152
153    // compute thread descriptor size without kernel stack
154    uint32_t desc_size = (intptr_t)(&thread->signature) - (intptr_t)thread + 4; 
155
156        // Initialize new thread descriptor
157    thread->trdid           = trdid;
158        thread->type            = type;
159    thread->quantum         = 0;            // TODO
160    thread->ticks_nr        = 0;            // TODO
161    thread->time_last_check = 0;
162        thread->core            = &local_cluster->core_tbl[core_lid];
163        thread->process         = process;
164
165    thread->local_locks     = 0;
166    thread->remote_locks    = 0;
167
168#if CONFIG_LOCKS_DEBUG
169    list_root_init( &thread->locks_root ); 
170    xlist_root_init( XPTR( local_cxy , &thread->xlocks_root ) );
171#endif
172
173    thread->u_stack_base    = u_stack_base;
174    thread->u_stack_size    = u_stack_size;
175    thread->k_stack_base    = (intptr_t)thread + desc_size;
176    thread->k_stack_size    = CONFIG_THREAD_DESC_SIZE - desc_size;
177
178    thread->entry_func      = func;         // thread entry point
179    thread->entry_args      = args;         // thread function arguments
180    thread->flags           = 0;            // all flags reset
181    thread->errno           = 0;            // no error detected
182    thread->fork_user       = 0;            // no user defined placement for fork
183    thread->fork_cxy        = 0;            // user defined target cluster for fork
184    thread->blocked         = THREAD_BLOCKED_GLOBAL;
185
186    // reset sched list
187    list_entry_init( &thread->sched_list );
188
189    // reset thread info
190    memset( &thread->info , 0 , sizeof(thread_info_t) );
191
192    // initializes join_lock
193    remote_spinlock_init( XPTR( local_cxy , &thread->join_lock ) );
194
195    // initialise signature
196        thread->signature = THREAD_SIGNATURE;
197
198    // FIXME call hal_thread_init() function to initialise the save_sr field
199    thread->save_sr = 0xFF13;
200
201    // register new thread in core scheduler
202    sched_register_thread( thread->core , thread );
203
204        // update DQDT
205    dqdt_update_threads( 1 );
206
207        return 0;
208
209} // end thread_init()
210
211/////////////////////////////////////////////////////////
212error_t thread_user_create( pid_t             pid,
213                            void            * start_func,
214                            void            * start_arg,
215                            pthread_attr_t  * attr,
216                            thread_t       ** new_thread )
217{
218    error_t        error;
219        thread_t     * thread;       // pointer on created thread descriptor
220    process_t    * process;      // pointer to local process descriptor
221    lid_t          core_lid;     // selected core local index
222    vseg_t       * vseg;         // stack vseg
223
224    assert( (attr != NULL) , __FUNCTION__, "pthread attributes must be defined" );
225
226#if DEBUG_THREAD_USER_CREATE
227uint32_t cycle = (uint32_t)hal_get_cycles();
228if( DEBUG_THREAD_USER_CREATE < cycle )
229printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n",
230__FUNCTION__, CURRENT_THREAD, pid , cycle );
231#endif
232
233    // get process descriptor local copy
234    process = process_get_local_copy( pid );
235
236    if( process == NULL )
237    {
238                printk("\n[ERROR] in %s : cannot get process descriptor %x\n",
239               __FUNCTION__ , pid );
240        return ENOMEM;
241    }
242
243    // select a target core in local cluster
244    if( attr->attributes & PT_ATTR_CORE_DEFINED )
245    {
246        core_lid = attr->lid;
247        if( core_lid >= LOCAL_CLUSTER->cores_nr )
248        {
249                printk("\n[ERROR] in %s : illegal core index attribute = %d\n",
250            __FUNCTION__ , core_lid );
251            return EINVAL;
252        }
253    }
254    else
255    {
256        core_lid = cluster_select_local_core();
257    }
258
259    // allocate a stack from local VMM
260    vseg = vmm_create_vseg( process,
261                            VSEG_TYPE_STACK,
262                            0,                 // size unused
263                            0,                 // length unused
264                            0,                 // file_offset unused
265                            0,                 // file_size unused
266                            XPTR_NULL,         // mapper_xp unused
267                            local_cxy );
268
269    if( vseg == NULL )
270    {
271            printk("\n[ERROR] in %s : cannot create stack vseg\n", __FUNCTION__ );
272                return ENOMEM;
273    }
274
275    // allocate memory for thread descriptor
276    thread = thread_alloc();
277
278    if( thread == NULL )
279    {
280            printk("\n[ERROR] in %s : cannot create new thread\n", __FUNCTION__ );
281        vmm_remove_vseg( vseg );
282        return ENOMEM;
283    }
284
285    // initialize thread descriptor
286    error = thread_init( thread,
287                         process,
288                         THREAD_USER,
289                         start_func,
290                         start_arg,
291                         core_lid,
292                         vseg->min,
293                         vseg->max - vseg->min );
294    if( error )
295    {
296            printk("\n[ERROR] in %s : cannot initialize new thread\n", __FUNCTION__ );
297        vmm_remove_vseg( vseg );
298        thread_release( thread );
299        return EINVAL;
300    }
301
302    // set DETACHED flag if required
303    if( attr->attributes & PT_ATTR_DETACH ) 
304    {
305        thread->flags |= THREAD_FLAG_DETACHED;
306    }
307
308    // allocate & initialize CPU context
309        if( hal_cpu_context_create( thread ) )
310    {
311            printk("\n[ERROR] in %s : cannot create CPU context\n", __FUNCTION__ );
312        vmm_remove_vseg( vseg );
313        thread_release( thread );
314        return ENOMEM;
315    }
316
317    // allocate  FPU context
318    if( hal_fpu_context_alloc( thread ) )
319    {
320            printk("\n[ERROR] in %s : cannot create FPU context\n", __FUNCTION__ );
321        vmm_remove_vseg( vseg );
322        thread_release( thread );
323        return ENOMEM;
324    }
325
326#if DEBUG_THREAD_USER_CREATE
327cycle = (uint32_t)hal_get_cycles();
328if( DEBUG_THREAD_USER_CREATE < cycle )
329printk("\n[DBG] %s : thread %x exit / process %x / new_thread %x / core %d / cycle %d\n",
330__FUNCTION__, CURRENT_THREAD, pid, thread, core_lid, cycle );
331#endif
332
333    *new_thread = thread;
334        return 0;
335
336}  // end thread_user_create()
337
338///////////////////////////////////////////////////////
339error_t thread_user_fork( xptr_t      parent_thread_xp,
340                          process_t * child_process,
341                          thread_t ** child_thread )
342{
343    error_t        error;
344        thread_t     * child_ptr;        // local pointer on local child thread
345    lid_t          core_lid;         // selected core local index
346
347    thread_t     * parent_ptr;       // local pointer on remote parent thread
348    cxy_t          parent_cxy;       // parent thread cluster
349    process_t    * parent_process;   // local pointer on parent process
350    xptr_t         parent_gpt_xp;    // extended pointer on parent thread GPT
351
352    void         * func;             // parent thread entry_func
353    void         * args;             // parent thread entry_args
354    intptr_t       base;             // parent thread u_stack_base
355    uint32_t       size;             // parent thread u_stack_size
356    uint32_t       flags;            // parent_thread flags
357    vpn_t          vpn_base;         // parent thread stack vpn_base
358    vpn_t          vpn_size;         // parent thread stack vpn_size
359    reg_t        * uzone;            // parent thread pointer on uzone 
360
361    vseg_t       * vseg;             // child thread STACK vseg
362
363#if DEBUG_THREAD_USER_FORK
364uint32_t cycle = (uint32_t)hal_get_cycles();
365if( DEBUG_THREAD_USER_FORK < cycle )
366printk("\n[DBG] %s : thread %x enter / child_process %x / cycle %d\n",
367__FUNCTION__, CURRENT_THREAD, child_process->pid, cycle );
368#endif
369
370    // select a target core in local cluster
371    core_lid = cluster_select_local_core();
372
373    // get cluster and local pointer on parent thread descriptor
374    parent_cxy = GET_CXY( parent_thread_xp );
375    parent_ptr = (thread_t *)GET_PTR( parent_thread_xp );
376
377    // get relevant fields from parent thread
378    func  = (void *)  hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->entry_func    ));
379    args  = (void *)  hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->entry_args    ));
380    base  = (intptr_t)hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->u_stack_base  ));
381    size  = (uint32_t)hal_remote_lw ( XPTR( parent_cxy , &parent_ptr->u_stack_size  ));
382    flags =           hal_remote_lw ( XPTR( parent_cxy , &parent_ptr->flags         ));
383    uzone = (reg_t *) hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->uzone_current ));
384
385    vpn_base = base >> CONFIG_PPM_PAGE_SHIFT;
386    vpn_size = size >> CONFIG_PPM_PAGE_SHIFT;
387
388    // get pointer on parent process in parent thread cluster
389    parent_process = (process_t *)hal_remote_lpt( XPTR( parent_cxy,
390                                                        &parent_ptr->process ) );
391 
392    // get extended pointer on parent GPT in parent thread cluster
393    parent_gpt_xp = XPTR( parent_cxy , &parent_process->vmm.gpt );
394
395    // allocate memory for child thread descriptor
396    child_ptr = thread_alloc();
397    if( child_ptr == NULL )
398    {
399        printk("\n[ERROR] in %s : cannot allocate new thread\n", __FUNCTION__ );
400        return -1;
401    }
402
403    // initialize thread descriptor
404    error = thread_init( child_ptr,
405                         child_process,
406                         THREAD_USER,
407                         func,
408                         args,
409                         core_lid,
410                         base,
411                         size );
412    if( error )
413    {
414            printk("\n[ERROR] in %s : cannot initialize child thread\n", __FUNCTION__ );
415        thread_release( child_ptr );
416        return EINVAL;
417    }
418
419    // return child pointer
420    *child_thread = child_ptr;
421
422    // set detached flag if required
423    if( flags & THREAD_FLAG_DETACHED ) child_ptr->flags = THREAD_FLAG_DETACHED;
424
425    // update uzone pointer in child thread descriptor
426    child_ptr->uzone_current = (char *)((intptr_t)uzone +
427                                        (intptr_t)child_ptr - 
428                                        (intptr_t)parent_ptr );
429 
430
431    // allocate CPU context for child thread
432        if( hal_cpu_context_alloc( child_ptr ) )
433    {
434            printk("\n[ERROR] in %s : cannot allocate CPU context\n", __FUNCTION__ );
435        thread_release( child_ptr );
436        return -1;
437    }
438
439    // allocate FPU context for child thread
440        if( hal_fpu_context_alloc( child_ptr ) )
441    {
442            printk("\n[ERROR] in %s : cannot allocate FPU context\n", __FUNCTION__ );
443        thread_release( child_ptr );
444        return -1;
445    }
446
447    // create and initialize STACK vseg
448    vseg = vseg_alloc();
449    vseg_init( vseg,
450               VSEG_TYPE_STACK,
451               base,
452               size,
453               vpn_base,
454               vpn_size,
455               0, 0, XPTR_NULL,                         // not a file vseg
456               local_cxy );
457
458    // register STACK vseg in local child VSL
459    vseg_attach( &child_process->vmm , vseg );
460
461    // copy all valid STACK GPT entries   
462    vpn_t          vpn;
463    bool_t         mapped;
464    ppn_t          ppn;
465    for( vpn = vpn_base ; vpn < (vpn_base + vpn_size) ; vpn++ )
466    {
467        error = hal_gpt_pte_copy( &child_process->vmm.gpt,
468                                  parent_gpt_xp,
469                                  vpn,
470                                  true,                 // set cow
471                                  &ppn,
472                                  &mapped );
473        if( error )
474        {
475            vseg_detach( &child_process->vmm , vseg );
476            vseg_free( vseg );
477            thread_release( child_ptr );
478            printk("\n[ERROR] in %s : cannot update child GPT\n", __FUNCTION__ );
479            return -1;
480        }
481
482        // increment pending forks counter for the page if mapped
483        if( mapped )
484        {
485            xptr_t   page_xp  = ppm_ppn2page( ppn );
486            cxy_t    page_cxy = GET_CXY( page_xp );
487            page_t * page_ptr = (page_t *)GET_PTR( page_xp );
488            hal_remote_atomic_add( XPTR( page_cxy , &page_ptr->forks ) , 1 );
489
490#if (DEBUG_THREAD_USER_FORK & 1)
491cycle = (uint32_t)hal_get_cycles();
492if( DEBUG_THREAD_USER_FORK < cycle )
493printk("\n[DBG] %s : thread %x copied stack PTE to child GPT : vpn %x\n",
494__FUNCTION__, CURRENT_THREAD, vpn );
495#endif
496
497        }
498    }
499
500    // set COW flag for all mapped entries of STAK vseg in parent thread GPT
501    hal_gpt_set_cow( parent_gpt_xp,
502                     vpn_base,
503                     vpn_size );
504 
505#if DEBUG_THREAD_USER_FORK
506cycle = (uint32_t)hal_get_cycles();
507if( DEBUG_THREAD_USER_FORK < cycle )
508printk("\n[DBG] %s : thread %x exit / child_process %x / child_thread %x / cycle %d\n",
509__FUNCTION__, CURRENT_THREAD, child_process->pid, child_ptr, cycle );
510#endif
511
512        return 0;
513
514}  // end thread_user_fork()
515
516/////////////////////////////////////////////////////////
517error_t thread_kernel_create( thread_t     ** new_thread,
518                              thread_type_t   type,
519                              void          * func,
520                              void          * args,
521                                              lid_t           core_lid )
522{
523    error_t        error;
524        thread_t     * thread;       // pointer on new thread descriptor
525
526    assert( ( (type == THREAD_IDLE) || (type == THREAD_RPC) || (type == THREAD_DEV) ) ,
527    __FUNCTION__ , "illegal thread type" );
528
529    assert( (core_lid < LOCAL_CLUSTER->cores_nr) ,
530            __FUNCTION__ , "illegal core_lid" );
531
532#if DEBUG_THREAD_KERNEL_CREATE
533uint32_t cycle = (uint32_t)hal_get_cycles();
534if( DEBUG_THREAD_KERNEL_CREATE < cycle )
535printk("\n[DBG] %s : thread %x enter / requested_type %s / cycle %d\n",
536__FUNCTION__, CURRENT_THREAD, thread, thread_type_str(type), cycle );
537#endif
538
539    // allocate memory for new thread descriptor
540    thread = thread_alloc();
541
542    if( thread == NULL ) return ENOMEM;
543
544    // initialize thread descriptor
545    error = thread_init( thread,
546                         &process_zero,
547                         type,
548                         func,
549                         args,
550                         core_lid,
551                         0 , 0 );  // no user stack for a kernel thread
552
553    if( error ) // release allocated memory for thread descriptor
554    {
555        thread_release( thread );
556        return EINVAL;
557    }
558
559    // allocate & initialize CPU context
560        hal_cpu_context_create( thread );
561
562#if DEBUG_THREAD_KERNEL_CREATE
563cycle = (uint32_t)hal_get_cycles();
564if( DEBUG_THREAD_KERNEL_CREATE < cycle )
565printk("\n[DBG] %s : thread %x exit / new_thread %x / type %s / cycle %d\n",
566__FUNCTION__, CURRENT_THREAD, thread, thread_type_str(type), cycle );
567#endif
568
569    *new_thread = thread;
570        return 0;
571
572} // end thread_kernel_create()
573
574/////////////////////////////////////////////////
575error_t thread_idle_init( thread_t      * thread,
576                          thread_type_t   type,
577                          void          * func,
578                          void          * args,
579                                          lid_t           core_lid )
580{
581    assert( (type == THREAD_IDLE) , __FUNCTION__ , "illegal thread type" );
582
583    assert( (core_lid < LOCAL_CLUSTER->cores_nr) , __FUNCTION__ , "illegal core index" );
584
585    error_t  error = thread_init( thread,
586                                  &process_zero,
587                                  type,
588                                  func,
589                                  args,
590                                  core_lid,
591                                  0 , 0 );   // no user stack for a kernel thread
592
593    // allocate & initialize CPU context if success
594    if( error == 0 ) hal_cpu_context_create( thread );
595
596    return error;
597
598}  // end thread_idle_init()
599
600///////////////////////////////////////////////////////////////////////////////////////
601// TODO: check that all memory dynamically allocated during thread execution
602// has been released, using a cache of mmap requests. [AG]
603///////////////////////////////////////////////////////////////////////////////////////
604void thread_destroy( thread_t * thread )
605{
606    reg_t        save_sr;
607
608    process_t  * process    = thread->process;
609    core_t     * core       = thread->core;
610
611#if DEBUG_THREAD_DESTROY
612uint32_t cycle = (uint32_t)hal_get_cycles();
613if( DEBUG_THREAD_DESTROY < cycle )
614printk("\n[DBG] %s : thread %x enter to destroy thread %x in process %x / cycle %d\n",
615__FUNCTION__, CURRENT_THREAD, thread, process->pid, cycle );
616#endif
617
618    assert( (thread->local_locks == 0) , __FUNCTION__ , "all local locks not released" );
619
620    assert( (thread->remote_locks == 0) , __FUNCTION__ , "all remote locks not released" );
621
622    // update intrumentation values
623        process->vmm.pgfault_nr += thread->info.pgfault_nr;
624
625    // release memory allocated for CPU context and FPU context
626        hal_cpu_context_destroy( thread );
627        if ( thread->type == THREAD_USER ) hal_fpu_context_destroy( thread );
628       
629    // release FPU ownership if required
630        hal_disable_irq( &save_sr );
631        if( core->fpu_owner == thread )
632        {
633                core->fpu_owner = NULL;
634                hal_fpu_disable();
635        }
636        hal_restore_irq( save_sr );
637
638    // remove thread from process th_tbl[]
639    process_remove_thread( thread );
640       
641    // update DQDT
642    dqdt_update_threads( -1 );
643
644    // invalidate thread descriptor
645        thread->signature = 0;
646
647    // release memory for thread descriptor
648    thread_release( thread );
649
650#if DEBUG_THREAD_DESTROY
651cycle = (uint32_t)hal_get_cycles();
652if( DEBUG_THREAD_DESTROY < cycle )
653printk("\n[DBG] %s : thread %x exit / destroyed thread %x in process %x / cycle %d\n",
654__FUNCTION__, CURRENT_THREAD, thread, process->pid, cycle );
655#endif
656
657}   // end thread_destroy()
658
659//////////////////////////////////////////////////
660inline void thread_set_req_ack( thread_t * target,
661                                uint32_t * rsp_count )
662{
663    reg_t    save_sr;   // for critical section
664
665    // get pointer on target thread scheduler
666    scheduler_t * sched = &target->core->scheduler;
667
668    // wait scheduler ready to handle a new request
669    while( sched->req_ack_pending ) asm volatile( "nop" );
670   
671    // enter critical section
672    hal_disable_irq( &save_sr );
673     
674    // set request in target thread scheduler
675    sched->req_ack_pending = true;
676
677    // set ack request in target thread "flags"
678    hal_atomic_or( &target->flags , THREAD_FLAG_REQ_ACK );
679
680    // set pointer on responses counter in target thread
681    target->ack_rsp_count = rsp_count;
682   
683    // exit critical section
684    hal_restore_irq( save_sr );
685
686    hal_fence();
687
688}  // thread_set_req_ack()
689
690/////////////////////////////////////////////////////
691inline void thread_reset_req_ack( thread_t * target )
692{
693    reg_t    save_sr;   // for critical section
694
695    // get pointer on target thread scheduler
696    scheduler_t * sched = &target->core->scheduler;
697
698    // check signal pending in scheduler
699    assert( sched->req_ack_pending , __FUNCTION__ , "no pending signal" );
700   
701    // enter critical section
702    hal_disable_irq( &save_sr );
703     
704    // reset signal in scheduler
705    sched->req_ack_pending = false;
706
707    // reset signal in thread "flags"
708    hal_atomic_and( &target->flags , ~THREAD_FLAG_REQ_ACK );
709
710    // reset pointer on responses counter
711    target->ack_rsp_count = NULL;
712   
713    // exit critical section
714    hal_restore_irq( save_sr );
715
716    hal_fence();
717
718}  // thread_reset_req_ack()
719
720////////////////////////////////
721inline bool_t thread_can_yield()
722{
723    thread_t * this = CURRENT_THREAD;
724    return (this->local_locks == 0) && (this->remote_locks == 0);
725}
726
727/////////////////////////
728void thread_check_sched()
729{
730    thread_t * this = CURRENT_THREAD;
731
732        if( (this->local_locks == 0) && 
733        (this->remote_locks == 0) &&
734        (this->flags & THREAD_FLAG_SCHED) ) 
735    {
736        this->flags &= ~THREAD_FLAG_SCHED;
737        sched_yield( "delayed scheduling" );
738    }
739
740}  // end thread_check_sched()
741
742//////////////////////////////////////
743void thread_block( xptr_t   thread_xp,
744                   uint32_t cause )
745{
746    // get thread cluster and local pointer
747    cxy_t      cxy = GET_CXY( thread_xp );
748    thread_t * ptr = GET_PTR( thread_xp );
749
750    // set blocking cause
751    hal_remote_atomic_or( XPTR( cxy , &ptr->blocked ) , cause );
752    hal_fence();
753
754#if DEBUG_THREAD_BLOCK
755uint32_t cycle = (uint32_t)hal_get_cycles();
756if( DEBUG_THREAD_BLOCK < cycle )
757printk("\n[DBG] %s : thread %x blocked thread %x / cause %x / cycle %d\n",
758__FUNCTION__ , CURRENT_THREAD , ptr , cause , cycle );
759#endif
760
761#if (DEBUG_THREAD_BLOCK & 1)
762if( DEBUG_THREAD_BLOCK < cycle )
763sched_display( ptr->core->lid );
764#endif
765
766} // end thread_block()
767
768////////////////////////////////////////////
769uint32_t thread_unblock( xptr_t   thread_xp,
770                         uint32_t cause )
771{
772    // get thread cluster and local pointer
773    cxy_t      cxy = GET_CXY( thread_xp );
774    thread_t * ptr = GET_PTR( thread_xp );
775
776    // reset blocking cause
777    uint32_t previous = hal_remote_atomic_and( XPTR( cxy , &ptr->blocked ) , ~cause );
778    hal_fence();
779
780#if DEBUG_THREAD_BLOCK
781uint32_t cycle = (uint32_t)hal_get_cycles();
782if( DEBUG_THREAD_BLOCK < cycle )
783printk("\n[DBG] %s : thread %x unblocked thread %x / cause %x / cycle %d\n",
784__FUNCTION__ , CURRENT_THREAD , ptr , cause , cycle );
785#endif
786
787#if (DEBUG_THREAD_BLOCK & 1)
788if( DEBUG_THREAD_BLOCK < cycle )
789sched_display( ptr->core->lid );
790#endif
791
792    // return a non zero value if the cause bit is modified
793    return( previous & cause );
794
795}  // end thread_unblock()
796
797/*
798
799////////////////////////////////////
800void thread_kill( xptr_t  target_xp,
801                  bool_t  is_exit,
802                  bool_t  is_forced )
803{
804    reg_t       save_sr;                // for critical section
805    bool_t      attached;               // target thread in attached mode
806    bool_t      join_done;              // joining thread arrived first
807    xptr_t      killer_xp;              // extended pointer on killer thread (this)
808    thread_t  * killer_ptr;             // pointer on killer thread (this)
809    cxy_t       target_cxy;             // target thread cluster     
810    thread_t  * target_ptr;             // pointer on target thread
811    xptr_t      joining_xp;             // extended pointer on joining thread
812    thread_t  * joining_ptr;            // pointer on joining thread
813    cxy_t       joining_cxy;            // joining thread cluster
814    pid_t       target_pid;             // target process PID
815    cxy_t       owner_cxy;              // target process owner cluster
816    trdid_t     target_trdid;           // target thread identifier
817    ltid_t      target_ltid;            // target thread local index
818    xptr_t      process_state_xp;       // extended pointer on <term_state> in process
819
820    xptr_t      target_flags_xp;        // extended pointer on target thread <flags>
821    xptr_t      target_join_lock_xp;    // extended pointer on target thread <join_lock>
822    xptr_t      target_join_xp_xp;      // extended pointer on target thread <join_xp>
823    xptr_t      target_process_xp;      // extended pointer on target thread <process>
824
825    process_t * target_process;         // pointer on target thread process
826
827    // get target thread pointer and cluster
828    target_cxy = GET_CXY( target_xp );
829    target_ptr = GET_PTR( target_xp );
830
831    // get killer thread pointers
832    killer_ptr = CURRENT_THREAD;
833    killer_xp  = XPTR( local_cxy , killer_ptr );
834
835#if DEBUG_THREAD_DELETE
836uint32_t cycle  = (uint32_t)hal_get_cycles;
837if( DEBUG_THREAD_DELETE < cycle )
838printk("\n[DBG] %s : thread %x enter for target thread %x / cycle %d\n",
839__FUNCTION__, killer_ptr, target_ptr, cycle );
840#endif
841
842    // block the target thread
843    thread_block( target_xp , THREAD_BLOCKED_GLOBAL );
844
845    // get target thread attached mode
846    target_flags_xp = XPTR( target_cxy , &target_ptr->flags );
847    attached = ((hal_remote_lw( target_flags_xp ) & THREAD_FLAG_DETACHED) == 0);
848
849    // synchronize with the joining thread
850    // if the target thread is attached && not forced
851
852    if( attached  && (is_forced == false) )
853    {
854        // build extended pointers on target thread join fields
855        target_join_lock_xp  = XPTR( target_cxy , &target_ptr->join_lock );
856        target_join_xp_xp    = XPTR( target_cxy , &target_ptr->join_xp );
857
858        // enter critical section
859        hal_disable_irq( &save_sr );
860
861        // take the join_lock in target thread descriptor
862        remote_spinlock_lock( target_join_lock_xp );
863
864        // get join_done from target thread descriptor
865        join_done = ((hal_remote_lw( target_flags_xp ) & THREAD_FLAG_JOIN_DONE) != 0);
866   
867        if( join_done )     // joining thread arrived first
868        {
869            // get extended pointer on joining thread
870            joining_xp  = (xptr_t)hal_remote_lwd( target_join_xp_xp );
871            joining_ptr = GET_PTR( joining_xp );
872            joining_cxy = GET_CXY( joining_xp );
873           
874            // reset the join_done flag in target thread
875            hal_remote_atomic_and( target_flags_xp , ~THREAD_FLAG_JOIN_DONE );
876
877            // unblock the joining thread
878            thread_unblock( joining_xp , THREAD_BLOCKED_JOIN );
879
880            // release the join_lock in target thread descriptor
881            remote_spinlock_unlock( target_join_lock_xp );
882
883            // restore IRQs
884            hal_restore_irq( save_sr );
885        }
886        else                // this thread arrived first
887        {
888            // set the kill_done flag in target thread
889            hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_KILL_DONE );
890
891            // block this thread on BLOCKED_JOIN
892            thread_block( killer_xp , THREAD_BLOCKED_JOIN );
893
894            // set extended pointer on killer thread in target thread
895            hal_remote_swd( target_join_xp_xp , killer_xp );
896
897            // release the join_lock in target thread descriptor
898            remote_spinlock_unlock( target_join_lock_xp );
899
900            // deschedule
901            sched_yield( "killer thread wait joining thread" );
902
903            // restore IRQs
904            hal_restore_irq( save_sr );
905        }
906    }  // end if attached
907
908    // - if the target thread is the main thread
909    //   => synchronize with the parent process main thread
910    // - if the target thread is not the main thread
911    //   => simply mark the target thread for delete
912
913    // get pointer on target thread process
914    target_process_xp  = XPTR( target_cxy , &target_ptr->process );
915    target_process     = (process_t *)hal_remote_lpt( target_process_xp );
916
917        // get target process owner cluster
918        target_pid = hal_remote_lw( XPTR( target_cxy , &target_process->pid ) );
919    owner_cxy = CXY_FROM_PID( target_pid );
920
921    // get target thread local index
922    target_trdid = hal_remote_lw( XPTR( target_cxy , &target_ptr->trdid ) );
923    target_ltid  = LTID_FROM_TRDID( target_trdid );
924
925    if( (owner_cxy == target_cxy) && (target_ltid == 0) )     // main thread
926    {
927        // get extended pointer on term_state in target process owner cluster
928        process_state_xp = XPTR( owner_cxy , &target_process->term_state );
929
930        // set termination info in target process owner 
931        if( is_exit ) hal_remote_atomic_or( process_state_xp , PROCESS_TERM_EXIT );
932        else          hal_remote_atomic_or( process_state_xp , PROCESS_TERM_KILL );
933
934#if DEBUG_THREAD_DELETE
935cycle  = (uint32_t)hal_get_cycles;
936if( DEBUG_THREAD_DELETE < cycle )
937printk("\n[DBG] %s : thread %x exit for thread %x / main thread / cycle %d\n",
938__FUNCTION__, killer_ptr, target_ptr, cycle );
939#endif
940
941    }
942    else                                                      // main thread
943    {
944        // set the REQ_DELETE flag in target thread descriptor
945        hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
946
947#if DEBUG_THREAD_DELETE
948cycle  = (uint32_t)hal_get_cycles;
949if( DEBUG_THREAD_DELETE < cycle )
950printk("\n[DBG] %s : thread %x exit for thread %x / not the main thread / cycle %d\n",
951__FUNCTION__, killer_ptr, target_ptr, cycle );
952#endif
953
954    }
955
956}  // end thread_kill()
957
958*/
959
960//////////////////////////////////////
961void thread_delete( xptr_t  target_xp,
962                    pid_t   pid,
963                    bool_t  is_forced )
964{
965    reg_t       save_sr;                // for critical section
966    bool_t      target_join_done;       // joining thread arrived first
967    bool_t      target_attached;        // target thread attached
968    xptr_t      killer_xp;              // extended pointer on killer thread (this)
969    thread_t  * killer_ptr;             // pointer on killer thread (this)
970    cxy_t       target_cxy;             // target thread cluster     
971    thread_t  * target_ptr;             // pointer on target thread
972    xptr_t      target_flags_xp;        // extended pointer on target thread <flags>
973    uint32_t    target_flags;           // target thread <flags> value
974    xptr_t      target_join_lock_xp;    // extended pointer on target thread <join_lock>
975    xptr_t      target_join_xp_xp;      // extended pointer on target thread <join_xp>
976    trdid_t     target_trdid;           // target thread identifier
977    ltid_t      target_ltid;            // target thread local index
978    xptr_t      joining_xp;             // extended pointer on joining thread
979    thread_t  * joining_ptr;            // pointer on joining thread
980    cxy_t       joining_cxy;            // joining thread cluster
981    cxy_t       owner_cxy;              // process owner cluster
982
983
984    // get target thread pointers, identifiers, and flags
985    target_cxy      = GET_CXY( target_xp );
986    target_ptr      = GET_PTR( target_xp );
987    target_trdid    = hal_remote_lw( XPTR( target_cxy , &target_ptr->trdid ) );
988    target_ltid     = LTID_FROM_TRDID( target_trdid );
989    target_flags_xp = XPTR( target_cxy , &target_ptr->flags ); 
990    target_flags    = hal_remote_lw( target_flags_xp );
991
992    // get killer thread pointers
993    killer_ptr = CURRENT_THREAD;
994    killer_xp  = XPTR( local_cxy , killer_ptr );
995
996#if DEBUG_THREAD_DELETE
997uint32_t cycle  = (uint32_t)hal_get_cycles;
998if( DEBUG_THREAD_DELETE < cycle )
999printk("\n[DBG] %s : killer thread %x enter for target thread %x / cycle %d\n",
1000__FUNCTION__, killer_ptr, target_ptr, cycle );
1001#endif
1002
1003    // target thread cannot be the main thread, because the main thread
1004    // must be deleted by the parent process sys_wait() function
1005    owner_cxy = CXY_FROM_PID( pid );
1006    assert( ((owner_cxy != target_cxy) || (target_ltid != 0)), __FUNCTION__,
1007    "tharget thread cannot be the main thread\n" );
1008
1009    // block the target thread
1010    thread_block( target_xp , THREAD_BLOCKED_GLOBAL );
1011
1012    // get attached from target flag descriptor
1013    target_attached = ((hal_remote_lw( target_flags_xp ) & THREAD_FLAG_DETACHED) != 0);
1014
1015    // synchronize with the joining thread if the target thread is attached
1016    if( target_attached && (is_forced == false) )
1017    {
1018        // build extended pointers on target thread join fields
1019        target_join_lock_xp  = XPTR( target_cxy , &target_ptr->join_lock );
1020        target_join_xp_xp    = XPTR( target_cxy , &target_ptr->join_xp );
1021
1022        // enter critical section
1023        hal_disable_irq( &save_sr );
1024
1025        // take the join_lock in target thread descriptor
1026        remote_spinlock_lock( target_join_lock_xp );
1027
1028        // get join_done from target thread descriptor
1029        target_join_done = ((hal_remote_lw( target_flags_xp ) & THREAD_FLAG_JOIN_DONE) != 0);
1030   
1031        if( target_join_done )  // joining thread arrived first => unblock the joining thread
1032        {
1033            // get extended pointer on joining thread
1034            joining_xp  = (xptr_t)hal_remote_lwd( target_join_xp_xp );
1035            joining_ptr = GET_PTR( joining_xp );
1036            joining_cxy = GET_CXY( joining_xp );
1037           
1038            // reset the join_done flag in target thread
1039            hal_remote_atomic_and( target_flags_xp , ~THREAD_FLAG_JOIN_DONE );
1040
1041            // unblock the joining thread
1042            thread_unblock( joining_xp , THREAD_BLOCKED_JOIN );
1043
1044            // release the join_lock in target thread descriptor
1045            remote_spinlock_unlock( target_join_lock_xp );
1046
1047            // restore IRQs
1048            hal_restore_irq( save_sr );
1049        }
1050        else                // this thread arrived first => register flags and deschedule
1051        {
1052            // set the kill_done flag in target thread
1053            hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_KILL_DONE );
1054
1055            // block this thread on BLOCKED_JOIN
1056            thread_block( killer_xp , THREAD_BLOCKED_JOIN );
1057
1058            // set extended pointer on killer thread in target thread
1059            hal_remote_swd( target_join_xp_xp , killer_xp );
1060
1061            // release the join_lock in target thread descriptor
1062            remote_spinlock_unlock( target_join_lock_xp );
1063
1064            // deschedule
1065            sched_yield( "killer thread wait joining thread" );
1066
1067            // restore IRQs
1068            hal_restore_irq( save_sr );
1069        }
1070    }  // end if attached
1071
1072    // set the REQ_DELETE flag in target thread descriptor
1073    hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
1074
1075#if DEBUG_THREAD_DELETE
1076cycle  = (uint32_t)hal_get_cycles;
1077if( DEBUG_THREAD_DELETE < cycle )
1078printk("\n[DBG] %s : killer thread %x exit for target thread %x / cycle %d\n",
1079__FUNCTION__, killer_ptr, target_ptr, cycle );
1080#endif
1081
1082}  // end thread_delete()
1083
1084
1085
1086///////////////////////
1087void thread_idle_func()
1088{
1089    while( 1 )
1090    {
1091        // unmask IRQs
1092        hal_enable_irq( NULL );
1093
1094        if( CONFIG_THREAD_IDLE_MODE_SLEEP ) // force core to low-power mode
1095        {
1096
1097#if DEBUG_THREAD_IDLE
1098uint32_t cycle  = (uint32_t)hal_get_cycles;
1099thread_t * this = CURRENT_THREAD;
1100if( DEBUG_THREAD_IDLE < cycle )
1101printk("\n[DBG] %s : idle thread %x on core[%x,%d] goes to sleep / cycle %d\n",
1102__FUNCTION__, this, local_cxy, this->core->lid, cycle );
1103#endif
1104
1105            hal_core_sleep();
1106
1107#if DEBUG_THREAD_IDLE
1108cycle  = (uint32_t)hal_get_cycles;
1109if( DEBUG_THREAD_IDLE < cycle )
1110printk("\n[DBG] %s : idle thread %x on core[%x,%d] wake up / cycle %d\n",
1111__FUNCTION__, this, local_cxy, this->core->lid, cycle );
1112#endif
1113
1114        }
1115        else                                // search a runable thread
1116        {
1117            sched_yield( "IDLE" );
1118        }
1119    }
1120}  // end thread_idle()
1121
1122
1123/////////////////////////////////////////////////
1124void thread_user_time_update( thread_t * thread )
1125{
1126    // TODO
1127    // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ );
1128}
1129
1130///////////////////////////////////////////////////
1131void thread_kernel_time_update( thread_t * thread )
1132{
1133    // TODO
1134    // printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ );
1135}
1136
1137/////////////////////////////////////
1138xptr_t thread_get_xptr( pid_t    pid,
1139                        trdid_t  trdid )
1140{
1141    cxy_t         target_cxy;          // target thread cluster identifier
1142    ltid_t        target_thread_ltid;  // target thread local index
1143    thread_t    * target_thread_ptr;   // target thread local pointer
1144    xptr_t        target_process_xp;   // extended pointer on target process descriptor
1145    process_t   * target_process_ptr;  // local pointer on target process descriptor
1146    pid_t         target_process_pid;  // target process identifier
1147    xlist_entry_t root;                // root of list of process in target cluster
1148    xptr_t        lock_xp;             // extended pointer on lock protecting  this list
1149
1150    // get target cluster identifier and local thread identifier
1151    target_cxy         = CXY_FROM_TRDID( trdid );
1152    target_thread_ltid = LTID_FROM_TRDID( trdid );
1153
1154    // check trdid argument
1155        if( (target_thread_ltid >= CONFIG_THREAD_MAX_PER_CLUSTER) || 
1156        cluster_is_undefined( target_cxy ) )         return XPTR_NULL;
1157
1158    // get root of list of process descriptors in target cluster
1159    hal_remote_memcpy( XPTR( local_cxy  , &root ),
1160                       XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ),
1161                       sizeof(xlist_entry_t) );
1162
1163    // get extended pointer on lock protecting the list of processes
1164    lock_xp = XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_lock );
1165
1166    // take the lock protecting the list of processes in target cluster
1167    remote_spinlock_lock( lock_xp );
1168
1169    // loop on list of process in target cluster to find the PID process
1170    xptr_t  iter;
1171    bool_t  found = false;
1172    XLIST_FOREACH( XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_root ) , iter )
1173    {
1174        target_process_xp  = XLIST_ELEMENT( iter , process_t , local_list );
1175        target_process_ptr = (process_t *)GET_PTR( target_process_xp );
1176        target_process_pid = hal_remote_lw( XPTR( target_cxy , &target_process_ptr->pid ) );
1177        if( target_process_pid == pid )
1178        {
1179            found = true;
1180            break;
1181        }
1182    }
1183
1184    // release the lock protecting the list of processes in target cluster
1185    remote_spinlock_unlock( lock_xp );
1186
1187    // check PID found
1188    if( found == false ) return XPTR_NULL;
1189
1190    // get target thread local pointer
1191    xptr_t xp = XPTR( target_cxy , &target_process_ptr->th_tbl[target_thread_ltid] );
1192    target_thread_ptr = (thread_t *)hal_remote_lpt( xp );
1193
1194    if( target_thread_ptr == NULL )  return XPTR_NULL;
1195
1196    return XPTR( target_cxy , target_thread_ptr );
1197}
1198
Note: See TracBrowser for help on using the repository browser.