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

Last change on this file since 458 was 457, checked in by alain, 6 years ago

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

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