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

Last change on this file since 532 was 531, checked in by nicolas.van.phan@…, 6 years ago

Fix error in thread debug prints

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