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

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

First import

File size: 19.6 KB
Line 
1/*
2 * thread.c -  implementation of thread operations (user & kernel)
3 *
4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *         Mohamed Lamine Karaoui (2015)
6 *         Alain Greiner (2016)
7 *
8 * Copyright (c) UPMC Sorbonne Universites
9 *
10 * This file is part of ALMOS-MKH..
11 *
12 * ALMOS-MKH. is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2.0 of the License.
15 *
16 * ALMOS-MKH. is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#include <almos_config.h>
27#include <hal_types.h>
28#include <hal_context.h>
29#include <hal_irqmask.h>
30#include <hal_special.h>
31#include <hal_remote.h>
32#include <memcpy.h>
33#include <printk.h>
34#include <cluster.h>
35#include <process.h>
36#include <scheduler.h>
37#include <dev_icu.h>
38#include <core.h>
39#include <list.h>
40#include <xlist.h>
41#include <page.h>
42#include <kmem.h>
43#include <ppm.h>
44#include <thread.h>
45
46//////////////////////////////////////////////////////////////////////////////////////
47// Extern global variables
48//////////////////////////////////////////////////////////////////////////////////////
49
50extern process_t      process_zero;
51
52//////////////////////////////////////////////////////////////////////////////////////
53//   global variables for display / must be consistant with enum in "thread.h"
54//////////////////////////////////////////////////////////////////////////////////////
55
56const char* thread_type_name[THREAD_TYPES_NR] =
57{
58        "USER",
59        "RPC"
60        "KERNEL",
61        "IDLE", 
62};
63
64/////////////////////////////////////////////////////////////////////////////////////
65// This static function makes the actual allocation and initialisation for a thread
66// descriptor. It is called by the three functions:
67// - thread_user_create()
68// - thread_user_copy()
69// - thread_kernel_create()
70/////////////////////////////////////////////////////////////////////////////////////
71// @ new_thread : buffer for new thread pointer.
72// @ process    : local pointer on process descriptor.
73// @ type       : thread type.
74// @ func       : local pointer on thread entry function.
75// @ args       : local pointer on thread entry function arguments.
76// @ core_lid   : target core local index.
77/////////////////////////////////////////////////////////////////////////////////////
78static error_t __thread_create( thread_t     ** new_thread,
79                                process_t     * process,
80                                thread_type_t   type,
81                                void          * func,
82                                void          * args,
83                                lid_t           core_lid,
84                                intptr_t        u_stack_base,
85                                uint32_t        u_stack_size )
86{
87    error_t        error;
88        thread_t     * thread;     // pointer on thread descriptor
89        page_t       * page;       // pointer on page descriptor containing thread descriptor
90        kmem_req_t     req;        // kmem request
91    trdid_t        trdid;      // allocated thread identifier
92
93        cluster_t    * local_cluster = LOCAL_CLUSTER;
94
95        // allocates memory for thread descriptor + kernel stack
96        req.type  = KMEM_PAGE;
97        req.size  = CONFIG_THREAD_PAGE_ORDER;
98        req.flags = AF_KERNEL | AF_ZERO;
99        page      = kmem_alloc( &req );
100        if( page == NULL ) return ENOMEM;
101
102    // get pointer on new thread descriptor
103        thread = (thread_t *)ppm_page2base( page );
104
105    // register new thread in local process descriptor, and get a TRDID
106    spinlock_lock( &process->th_lock );
107    error = process_register_thread( process, thread , &trdid );
108    spinlock_unlock( &process->th_lock );
109
110    if( error ) 
111    {
112        // release allocated memory for thread descriptor
113            req.type  = KMEM_PAGE;
114        req.ptr   = page;
115        kmem_free( &req );
116        return EAGAIN;
117    }
118 
119        // Initialize new thread descriptor
120    thread->trdid           = trdid;
121        thread->type            = type; 
122    thread->quantum         = 0;            // TODO
123    thread->ticks_nr        = 0;            // TODO
124    thread->time_last_check = 0;
125        thread->core            = &local_cluster->core_tbl[core_lid];
126        thread->process         = process;
127    thread->page            = page;
128
129    thread->local_locks     = 0;
130    list_root_init( &thread->locks_root );
131
132    thread->remote_locks    = 0;
133    xlist_root_init( XPTR( local_cxy , &thread->xlocks_root ) );
134
135    thread->u_stack_base    = u_stack_base;     
136    thread->u_stack_size    = u_stack_size;
137    thread->k_stack_base    = (intptr_t)thread;     
138    thread->k_stack_size    = CONFIG_PPM_PAGE_SIZE << CONFIG_THREAD_PAGE_ORDER;
139
140    thread->entry_func      = func;         // thread entry point
141    thread->entry_args      = args;         // thread function arguments
142    thread->flags           = 0;            // all flags reset 
143    thread->signals         = 0;            // no pending signal
144    thread->errno           = 0;            // no error detected
145    thread->fork_user       = 0;            // no fork required
146    thread->fork_cxy        = 0;
147
148    // thread blocked
149    thread->blocked = THREAD_BLOCKED_GLOBAL;
150
151    // reset children list
152    xlist_root_init( XPTR( local_cxy , &thread->children_root ) );
153    thread->children_nr = 0;
154
155    // reset sched list and brothers list
156    list_entry_init( &thread->sched_list );
157    xlist_entry_init( XPTR( local_cxy , &thread->brothers_list ) );
158
159    // reset thread info
160    memset( &thread->info , 0 , sizeof(thread_info_t) );
161
162    // initialise signature
163        thread->signature = THREAD_SIGNATURE;
164
165    // update local DQDT
166    dqdt_local_update_threads( 1 );
167
168    // register new thread in core scheduler
169    sched_register_thread( thread->core , thread );
170
171    *new_thread = thread;
172        return 0;
173} // end __thread_create()
174
175
176/////////////////////////////////////////////////////////
177error_t thread_user_create( thread_t       ** new_thread,
178                            pthread_attr_t  * attr,
179                            intptr_t          u_stack_base,
180                            uint32_t          u_stack_size )
181{
182    error_t        error;
183        thread_t     * thread;       // pointer on created thread descriptor
184    process_t    * process;      // pointer to local process descriptor
185    lid_t          core_lid;     // selected core local index
186
187        cluster_t    * local_cluster = LOCAL_CLUSTER;
188
189    // select a target core in local cluster
190    if( attr->flags & PT_FLAG_CORE_DEFINED ) core_lid = attr->lid;
191    else                                     core_lid = cluster_select_local_core();
192
193    // check core local index
194    if( core_lid >= local_cluster->cores_nr ) return EINVAL;
195
196    // get process descriptor local copy
197    process = process_get_local_copy( attr->pid );
198    if( process == NULL ) return ENOMEM;
199
200    // make allocation / initialisation
201    error = __thread_create( &thread,
202                             process,
203                             THREAD_USER,
204                             attr->entry_func,
205                             attr->entry_args,
206                             core_lid,
207                             u_stack_base,
208                             u_stack_size );
209    if( error ) return ENOMEM;
210
211    // set LOADABLE flag / set ATTACHED flag if required
212    thread->flags = THREAD_FLAG_LOADABLE;
213    if( attr->flags & PT_FLAG_DETACH ) thread->flags |= THREAD_FLAG_DETACHED;
214
215    // allocate & initialise CPU context
216        error = hal_cpu_context_create( thread ); 
217    if( error ) return ENOMEM;
218
219    // allocate & initialise FPU context
220    error = hal_fpu_context_create( thread ); 
221    if( error ) return ENOMEM;
222
223    thread_dmsg("INFO : %s thread %x for process %x on core %d in cluster %x\n", 
224                 __FUNCTION__, thread->trdid, process->pid, core_lid, local_cluster->cxy );
225
226    *new_thread = thread;
227        return 0;
228} // end thread_user_create()
229
230
231/////////////////////////////////////////////////
232error_t thread_user_fork( thread_t ** new_thread,
233                          process_t * process,
234                          intptr_t    u_stack_base,
235                          uint32_t    u_stack_size )
236{
237    error_t        error;
238        thread_t     * new;          // pointer on thread descriptor
239    lid_t          core_lid;     // selected core local index
240
241    // select a target core in local cluster
242    core_lid = cluster_select_local_core();
243
244    // get pointer on calling thread descriptor
245    thread_t * this = CURRENT_THREAD;
246
247    // make allocation / initialisation
248    error = __thread_create( &new,
249                             process,
250                             THREAD_USER,
251                             this->entry_func,
252                             this->entry_args,
253                             core_lid,
254                             u_stack_base,
255                             u_stack_size );
256    if( error ) return ENOMEM;
257
258    // set ATTACHED flag if set in this thread
259    if( this->signals & THREAD_FLAG_DETACHED ) new->signals = THREAD_FLAG_DETACHED;
260
261    // allocate & initialise CPU context from calling thread
262        error = hal_cpu_context_copy( new , this ); 
263    if( error ) return ENOMEM;
264
265    // allocate & initialise FPU context from calling thread
266        error = hal_fpu_context_copy( new , this ); 
267    if( error ) return ENOMEM;
268
269    thread_dmsg("INFO : %s thread %x for process %x on core %d in cluster %x\n", 
270                 __FUNCTION__, new->trdid, process->pid, core_lid, local_cluster->cxy );
271
272    *new_thread = new;
273        return 0;
274} // end thread_user_fork()
275
276
277
278/////////////////////////////////////////////////////////
279error_t thread_kernel_create( thread_t     ** new_thread,
280                              thread_type_t   type,
281                              void          * func, 
282                              void          * args, 
283                                              lid_t           core_lid )
284{
285    error_t        error;
286        thread_t     * new;        // pointer on new thread descriptor
287
288    cluster_t    * local_cluster = LOCAL_CLUSTER;
289
290    // check type argument
291    if( (type != THREAD_KERNEL) && (type != THREAD_RPC) && 
292        (type != THREAD_IDLE) && (type != THREAD_DEV) )
293    {
294        printk("ERROR : %s received an illegal thread type\n", __FUNCTION__ );
295        hal_core_sleep();
296    }
297
298    // check core local index
299    if( core_lid >= local_cluster->cores_nr )
300    {
301        printk("ERROR : %s received an illegal core_lid\n", __FUNCTION__ );
302        hal_core_sleep();
303    }
304
305    // make allocation / initialisation
306    error = __thread_create( &new,
307                             &process_zero,
308                             type,
309                             func,
310                             args,
311                             core_lid,
312                             0 , 0 );  // no user stack for a kernel thread
313    if( error ) 
314    {
315        printk("ERROR : %s cannot create thread\n", __FUNCTION__ );
316        hal_core_sleep();
317    }
318
319    // allocate & initialise CPU context
320        hal_cpu_context_create( new ); 
321
322    thread_dmsg("INFO : %s thread %x in cluster %x on core %d\n", 
323                 __FUNCTION__ , new->trdid , local_cluster->cxy , core_lid );
324
325    *new_thread = new; 
326        return 0;
327} // end thread_kernel_create()
328
329
330///////////////////////////////////////////////////////////////////////////////////////
331// TODO: check that all memory dynamically allocated during thread execution
332// has been released, using a cache of mmap and malloc requests. [AG]
333///////////////////////////////////////////////////////////////////////////////////////
334void thread_destroy( thread_t * thread )
335{
336        uint32_t     tm_start;
337        uint32_t     tm_end;
338    uint32_t     state;
339
340    process_t  * process    = thread->process;
341    core_t     * core       = thread->core;
342
343    if( thread->children_nr )
344    {
345        printk("\n[PANIC] in %s : thread %x for process %x on core %d in cluster %x"
346               " has still attached children\n",
347               __FUNCTION__, thread->trdid, process->pid, core->lid, local_cxy );
348        hal_core_sleep();
349    }
350
351    if( (thread->local_locks != 0) || (thread->remote_locks != 0) )
352    {
353        printk("\n[PANIC] in %s : thread %x for process %x on core %d in cluster %x"
354               " did not released all locks\n",
355               __FUNCTION__, thread->trdid, process->pid, core->lid, local_cxy );
356        hal_core_sleep();
357    }
358   
359        tm_start = hal_time_stamp();
360
361    // update intrumentation values
362    uint32_t pgfaults = thread->info.pgfault_nr;
363    uint32_t u_errors = thread->info.u_err_nr;
364    uint32_t m_errors = thread->info.m_err_nr;
365
366        process->vmm.pgfault_nr += pgfaults;
367        process->vmm.u_err_nr   += u_errors;
368        process->vmm.m_err_nr   += m_errors;
369
370    // release memory allocated for CPU context and FPU context
371        hal_cpu_context_destroy( thread );
372        hal_fpu_context_destroy( thread );
373       
374    // release FPU if required
375    // TODO This should be done before calling thread_destroy()
376        hal_disable_irq( &state );
377        if( core->fpu_owner == thread )
378        {
379                core->fpu_owner = NULL;
380                hal_fpu_disable();
381        }
382        hal_restore_irq( state );
383
384    // remove thread from process th_tbl[]
385    // TODO This should be done before calling thread_destroy()
386    ltid_t ltid = LTID_FROM_TRDID( thread->trdid );
387
388        spinlock_lock( &process->th_lock );
389        process->th_tbl[ltid] = XPTR_NULL;
390        process->th_nr--;
391        spinlock_unlock( &process->th_lock );
392       
393    // invalidate thread descriptor
394        thread->signature = 0;
395
396    // release memory for thread descriptor
397        kmem_req_t   req; 
398        req.type     = KMEM_PAGE; 
399        req.ptr      = ppm_base2page( thread );
400        kmem_free(&req);
401
402        tm_end = hal_time_stamp();
403
404        thread_dmsg("INFO : %s destroy thread %x for process %x on core %d in cluster %x\n"
405                 " / duration = %d / page_faults = %d / ticks = %d\n",
406                       __FUNCTION__, trdid , pid , core_lid , local_cxy ,
407                       tm_end - tm_start , pgfaults , ticks );
408
409}  // end thread_destroy()
410
411
412/////////////////////////////////////////////////
413void thread_child_parent_link( xptr_t  xp_parent,
414                               xptr_t  xp_child )
415{
416    // get extended pointers on children list root
417    cxy_t      parent_cxy = GET_CXY( xp_parent );   
418    thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent );
419    xptr_t     root       = XPTR( parent_cxy , &parent_ptr->children_root );
420
421    // get extended pointer on children list entry
422    cxy_t      child_cxy  = GET_CXY( xp_child );   
423    thread_t * child_ptr  = (thread_t *)GET_PTR( xp_child );
424    xptr_t     entry      = XPTR( child_cxy , &child_ptr->brothers_list );
425
426    // set the link
427    xlist_add_first( root , entry );
428    hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr ) , 1 );
429} 
430
431///////////////////////////////////////////////////
432void thread_child_parent_unlink( xptr_t  xp_parent,
433                                 xptr_t  xp_child )
434{
435    // get extended pointer on children list lock
436    cxy_t      parent_cxy = GET_CXY( xp_parent );   
437    thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent );
438    xptr_t     lock       = XPTR( parent_cxy , &parent_ptr->children_lock );
439
440    // get extended pointer on children list entry
441    cxy_t      child_cxy  = GET_CXY( xp_child );   
442    thread_t * child_ptr  = (thread_t *)GET_PTR( xp_child );
443    xptr_t     entry      = XPTR( child_cxy , &child_ptr->brothers_list );
444
445    // get the lock
446    remote_spinlock_lock( lock );
447
448    // remove the link
449    xlist_unlink( entry );
450    hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr ) , -1 );
451   
452    // release the lock
453    remote_spinlock_unlock( lock );
454}
455
456/////////////////////////////////////////////////
457inline void thread_set_signal( thread_t * thread,
458                               uint32_t   mask )
459{
460    hal_atomic_or( &thread->signals , mask );
461}
462 
463///////////////////////////////////////////////////
464inline void thread_reset_signal( thread_t * thread,
465                                 uint32_t   mask )
466{
467    hal_atomic_and( &thread->signals , ~mask );
468}
469 
470//////////////////////////////////
471inline bool_t thread_is_joinable()
472{
473    thread_t * this = CURRENT_THREAD;
474    return( (this->brothers_list.next != XPTR_NULL) &&
475            (this->brothers_list.pred != XPTR_NULL) );
476}
477
478//////////////////////////////////
479inline bool_t thread_is_runnable()
480{
481    thread_t * this = CURRENT_THREAD;
482    return( this->blocked == 0 );
483}
484
485////////////////////////////////
486inline bool_t thread_can_yield()
487{
488    thread_t * this = CURRENT_THREAD;
489    return ( (this->local_locks == 0) && (this->remote_locks == 0) );
490}
491
492///////////////////////////
493bool_t thread_check_sched()
494{
495        thread_t * this = CURRENT_THREAD;
496
497    // check locks count
498        if( (this->local_locks != 0) || (this->remote_locks != 0) ) return false;
499
500    // compute elapsed time, taking into account 32 bits register wrap
501    uint32_t elapsed;
502    uint32_t time_now   = hal_time_stamp();
503    uint32_t time_last  = this->time_last_check;
504    if( time_now < time_last ) elapsed = (0xFFFFFFFF - time_last) + time_now;
505        else                       elapsed = time_now - time_last;
506
507    // update thread time
508    this->time_last_check = time_now;
509
510        // check elapsed time
511        if( elapsed < CONFIG_CORE_CHECK_EVERY ) return false;
512    else                                    return true;
513}
514
515/////////////////////
516error_t thread_exit()
517{
518    uint32_t   sr_save;
519
520        thread_t * this = CURRENT_THREAD;
521
522    // test if this thread can be descheduled
523        if( !thread_can_yield() )
524        {
525        printk("ERROR in %s : thread %x in process %x on core %d in cluster %x\n"
526               " did not released all locks\n",
527               __FUNCTION__ , this->trdid , this->process->pid ,
528               CURRENT_CORE->lid , local_cxy );
529        return EINVAL;
530    }
531
532    if( this->flags & THREAD_FLAG_DETACHED )
533    {
534        // if detached set signal and set blocking cause atomically
535        hal_disable_irq( &sr_save );
536        thread_set_signal( this , THREAD_SIG_KILL );
537        thread_block( this , THREAD_BLOCKED_EXIT );
538        hal_restore_irq( sr_save );
539    }
540    else 
541    {
542        // if attached, set blocking cause
543        thread_block( this , THREAD_BLOCKED_EXIT );
544    }
545
546    // deschedule
547    sched_yield();
548    return 0;
549
550} // end thread_exit()
551
552/////////////////////////////////////
553void thread_block( thread_t * thread,
554                   uint32_t   cause )
555{
556    // set blocking cause
557    hal_atomic_or( &thread->blocked , cause );
558
559}  // end thread_block()
560
561////////////////////////////////////
562void thread_unblock( xptr_t   thread,
563                    uint32_t cause )
564{
565    // get thread cluster and local pointer
566    cxy_t      cxy = GET_CXY( thread ); 
567    thread_t * ptr = (thread_t *)GET_PTR( thread );
568
569    // reset blocking cause
570    hal_remote_atomic_and( XPTR( cxy , &ptr->blocked ) , ~cause );
571
572}  // end thread_unblock()
573
574/////////////////////////////////////
575void thread_kill( thread_t * target )
576{
577    // set SIG_KILL signal in target thread descriptor
578    thread_set_signal( target , THREAD_SIG_KILL );
579
580    // set the global blocked bit in target thread descriptor.
581    thread_block( target , THREAD_BLOCKED_GLOBAL );
582
583    // send an IPI to reschedule the target thread core.
584    dev_icu_send_ipi( local_cxy , target->core->lid );
585
586}  // end thread_kill()
587
588
589/////////////////////////
590void * thread_idle_func()
591{
592    while( 1 )
593    {
594        thread_dmsg("INFO : core %d in cluster %x goes to sleeping state at cycle\n",
595                     core->lid , cluster->cxy, hal_time_stamp() );
596
597        // force core to sleeping state
598        hal_core_sleep();
599
600        thread_dmsg("INFO : core %d in cluster %x wake up at cycle %d\n",
601                     core->lid , cluster->cxy, hal_time_stamp() );
602
603        // force scheduling at wake-up
604        sched_yield();
605   }
606}  // end thread_idle()
607
608
Note: See TracBrowser for help on using the repository browser.