source: trunk/kernel/kern/scheduler.c @ 459

Last change on this file since 459 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: 18.0 KB
RevLine 
[1]1/*
2 * scheduler.c - Core scheduler implementation.
3 *
4 * Author    Alain Greiner (2016)
5 *
6 * Copyright (c)  UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH. is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH. is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
[14]24#include <kernel_config.h>
[457]25#include <hal_kernel_types.h>
[407]26#include <hal_switch.h>
[1]27#include <hal_irqmask.h>
28#include <hal_context.h>
29#include <printk.h>
30#include <list.h>
31#include <core.h>
32#include <thread.h>
[296]33#include <chdev.h>
[1]34#include <scheduler.h>
35
[443]36
[296]37///////////////////////////////////////////////////////////////////////////////////////////
38// Extern global variables
39///////////////////////////////////////////////////////////////////////////////////////////
[1]40
[443]41uint32_t   idle_thread_count;
42uint32_t   idle_thread_count_active;
[296]43
[443]44extern chdev_directory_t    chdev_dir;          // allocated in kernel_init.c file
45extern uint32_t             switch_save_sr[];   // allocated in kernel_init.c file
46
[1]47////////////////////////////////
48void sched_init( core_t * core )
49{
50    scheduler_t * sched = &core->scheduler;
51
52    sched->u_threads_nr   = 0;
53    sched->k_threads_nr   = 0;
54
[279]55    sched->current        = CURRENT_THREAD;
[443]56    sched->idle           = NULL;               // initialized in kernel_init()
57    sched->u_last         = NULL;               // initialized in sched_register_thread()
58    sched->k_last         = NULL;               // initialized in sched_register_thread()
[1]59
60    // initialise threads lists
61    list_root_init( &sched->u_root );
62    list_root_init( &sched->k_root );
63
[443]64    sched->req_ack_pending = false;             // no pending request
65    sched->trace           = false;             // context switches trace desactivated
[409]66
[1]67}  // end sched_init()
68
69////////////////////////////////////////////
70void sched_register_thread( core_t   * core,
71                            thread_t * thread )
72{
73    scheduler_t * sched = &core->scheduler;
74    thread_type_t type  = thread->type;
75
76    // take lock protecting sheduler lists
77    spinlock_lock( &sched->lock );
78
79    if( type == THREAD_USER )
80    {
81        list_add_last( &sched->u_root , &thread->sched_list );
82        sched->u_threads_nr++;
[279]83        if( sched->u_last == NULL ) sched->u_last = &thread->sched_list;
[1]84    }
85    else // kernel thread
86    {
87        list_add_last( &sched->k_root , &thread->sched_list );
88        sched->k_threads_nr++;
[279]89        if( sched->k_last == NULL ) sched->k_last = &thread->sched_list; 
[1]90    }
91
92    // release lock
[428]93    hal_fence();
[1]94    spinlock_unlock( &sched->lock );
95
[409]96}  // end sched_register_thread()
[1]97
[408]98//////////////////////////////////////////////
99thread_t * sched_select( scheduler_t * sched )
[1]100{
[408]101    thread_t     * thread;
102    list_entry_t * current;
103    list_entry_t * last;
[437]104    list_entry_t * root;
105    bool_t         done;
[450]106    uint32_t       count;
[1]107
108    // take lock protecting sheduler lists
109    spinlock_lock( &sched->lock );
110
[437]111    // first : scan the kernel threads list if not empty
[279]112    if( list_is_empty( &sched->k_root ) == false )
[1]113    {
[437]114        root    = &sched->k_root;
[279]115        last    = sched->k_last;
[450]116        done    = false;
117        count   = 0;
[437]118        current = last;
119
120        while( done == false )
[279]121        {
[450]122            assert( (count < sched->k_threads_nr), __FUNCTION__, "bad kernel threads list" );
123
[279]124            // get next entry in kernel list
[437]125            current = current->next;
[1]126
[437]127            // check exit condition
128            if( current == last ) done = true;
129
[279]130            // skip the root that does not contain a thread
[437]131            if( current == root ) continue;
[450]132            else                  count++;
[1]133
[279]134            // get thread pointer for this entry
135            thread = LIST_ELEMENT( current , thread_t , sched_list );
[1]136
[450]137            // select kernel thread if non blocked and non THREAD_IDLE
[440]138            if( (thread->blocked == 0)  && (thread->type != THREAD_IDLE) )
[279]139            {
[438]140                spinlock_unlock( &sched->lock );
141                return thread;
142            }
[437]143        } // end loop on kernel threads
[450]144    } // end kernel threads
[437]145
146    // second : scan the user threads list if not empty
[279]147    if( list_is_empty( &sched->u_root ) == false )
[1]148    {
[437]149        root    = &sched->u_root;
[279]150        last    = sched->u_last;
[450]151        done    = false;
152        count   = 0;
[437]153        current = last;
154
155        while( done == false )
[279]156        {
[450]157            assert( (count < sched->u_threads_nr), __FUNCTION__, "bad user threads list" );
158
[279]159            // get next entry in user list
[437]160            current = current->next;
[1]161
[437]162            // check exit condition
163            if( current == last ) done = true;
164
[279]165            // skip the root that does not contain a thread
[437]166            if( current == root ) continue;
[450]167            else                  count++;
[1]168
[279]169            // get thread pointer for this entry
170            thread = LIST_ELEMENT( current , thread_t , sched_list );
[1]171
[450]172            // select thread if non blocked
[279]173            if( thread->blocked == 0 )
174            {
175                spinlock_unlock( &sched->lock );
176                return thread;
177            }
[437]178        } // end loop on user threads
[450]179    } // end user threads
[1]180
[437]181    // third : return idle thread if no other runnable thread
[1]182    spinlock_unlock( &sched->lock );
183    return sched->idle;
184
[296]185}  // end sched_select()
[1]186
[416]187///////////////////////////////////////////
[433]188void sched_handle_signals( core_t * core )
[1]189{
[437]190
[1]191    list_entry_t * iter;
[440]192    list_entry_t * root;
[1]193    thread_t     * thread;
[428]194    process_t    * process;
[443]195    bool_t         last_thread;
[409]196
[440]197    // get pointer on scheduler
[1]198    scheduler_t  * sched = &core->scheduler;
199
[440]200    // get pointer on user threads root
201    root = &sched->u_root;
202
[1]203    // take lock protecting threads lists
204    spinlock_lock( &sched->lock );
205
[440]206    // We use a while to scan the user threads, to control the iterator increment,
207    // because some threads will be destroyed, and we cannot use a LIST_FOREACH()
208
209    // initialise list iterator
210    iter = root->next;
211
[416]212    // scan all user threads
[440]213    while( iter != root )
[1]214    {
[440]215        // get pointer on thread
[1]216        thread = LIST_ELEMENT( iter , thread_t , sched_list );
217
[440]218        // increment iterator
219        iter = iter->next;
220
[416]221        // handle REQ_ACK
222        if( thread->flags & THREAD_FLAG_REQ_ACK )
[408]223        {
[416]224            // check thread blocked
225            assert( (thread->blocked & THREAD_BLOCKED_GLOBAL) , 
226            __FUNCTION__ , "thread not blocked" );
227 
228            // decrement response counter
229            hal_atomic_add( thread->ack_rsp_count , -1 );
[408]230
[416]231            // reset REQ_ACK in thread descriptor
232            thread_reset_req_ack( thread );
[408]233        }
[416]234
235        // handle REQ_DELETE
236        if( thread->flags & THREAD_FLAG_REQ_DELETE )
237        {
[428]238            // get thread process descriptor
239            process = thread->process;
[416]240
241                // release FPU if required
242                if( thread->core->fpu_owner == thread )  thread->core->fpu_owner = NULL;
243
[428]244            // remove thread from scheduler (scheduler lock already taken)
245            uint32_t threads_nr = sched->u_threads_nr;
[440]246
[428]247            assert( (threads_nr != 0) , __FUNCTION__ , "u_threads_nr cannot be 0\n" );
[440]248
[428]249            sched->u_threads_nr = threads_nr - 1;
[416]250            list_unlink( &thread->sched_list );
[450]251            if( sched->u_last == &thread->sched_list )
252            {
253                if( threads_nr == 1 ) 
254                {
255                    sched->u_last = NULL;
256                }
257                else if( sched->u_root.next == &thread->sched_list )
258                {
259                    sched->u_last = sched->u_root.pred;
260                }
261                else
262                {
263                    sched->u_last = sched->u_root.next;
264                }
265            }
[416]266
[450]267            // delete thread descriptor
[443]268            last_thread = thread_destroy( thread );
[416]269
[438]270#if DEBUG_SCHED_HANDLE_SIGNALS
[440]271uint32_t cycle = (uint32_t)hal_get_cycles();
[438]272if( DEBUG_SCHED_HANDLE_SIGNALS < cycle )
[445]273printk("\n[DBG] %s : thread %x in process %x on core[%x,%d] deleted / cycle %d\n",
[443]274__FUNCTION__ , thread->trdid , process->pid , local_cxy , thread->core->lid , cycle );
[433]275#endif
[416]276            // destroy process descriptor if no more threads
[443]277            if( last_thread ) 
[428]278            {
279                // delete process   
280                process_destroy( process );
281
[438]282#if DEBUG_SCHED_HANDLE_SIGNALS
[433]283cycle = (uint32_t)hal_get_cycles();
[438]284if( DEBUG_SCHED_HANDLE_SIGNALS < cycle )
[443]285printk("\n[DBG] %s : process %x in cluster %x deleted / cycle %d\n",
286__FUNCTION__ , process->pid , local_cxy , cycle );
[433]287#endif
[428]288            }
[416]289        }
[1]290    }
291
292    // release lock
[428]293    hal_fence();
[1]294    spinlock_unlock( &sched->lock );
295
[433]296} // end sched_handle_signals()
[416]297
[408]298////////////////////////////////
299void sched_yield( char * cause )
[1]300{
[407]301    thread_t    * next;
[1]302    thread_t    * current = CURRENT_THREAD;
[409]303    core_t      * core    = current->core;
304    scheduler_t * sched   = &core->scheduler;
[407]305 
[438]306#if (DEBUG_SCHED_YIELD & 0x1)
[443]307if( sched->trace )
[433]308sched_display( core->lid );
[407]309#endif
[1]310
[337]311    // delay the yield if current thread has locks
[407]312    if( (current->local_locks != 0) || (current->remote_locks != 0) )
[337]313    {
314        current->flags |= THREAD_FLAG_SCHED;
315        return;
316    }
[1]317
[435]318    // enter critical section / save SR in current thread descriptor
319    hal_disable_irq( &CURRENT_THREAD->save_sr );
[408]320
[407]321    // loop on threads to select next thread
[408]322    next = sched_select( sched );
[1]323
[436]324    // check next thread kernel_stack overflow
[443]325    assert( (next->signature == THREAD_SIGNATURE), __FUNCTION__ , 
326    "kernel stack overflow for thread %x on core[%x,%d] \n", next, local_cxy, core->lid );
[436]327
[296]328    // check next thread attached to same core as the calling thread
[443]329    assert( (next->core == current->core), __FUNCTION__ , 
330    "next core %x != current core %x\n", next->core, current->core );
[296]331
[407]332    // check next thread not blocked when type != IDLE
[428]333    assert( ((next->blocked == 0) || (next->type == THREAD_IDLE)) , __FUNCTION__ ,
[407]334    "next thread %x (%s) is blocked on core[%x,%d]\n", 
[409]335    next->trdid , thread_type_str(next->type) , local_cxy , core->lid );
[296]336
337    // switch contexts and update scheduler state if next != current
338        if( next != current )
[1]339    {
340
[438]341#if DEBUG_SCHED_YIELD
[443]342if( sched->trace )
[433]343printk("\n[DBG] %s : core[%x,%d] / cause = %s\n"
[408]344"      thread %x (%s) (%x,%x) => thread %x (%s) (%x,%x) / cycle %d\n",
[409]345__FUNCTION__, local_cxy, core->lid, cause, 
[443]346current, thread_type_str(current->type), current->process->pid, current->trdid,next ,
347thread_type_str(next->type) , next->process->pid , next->trdid , (uint32_t)hal_get_cycles() );
[433]348#endif
[279]349
[296]350        // update scheduler
[408]351        sched->current = next;
352        if( next->type == THREAD_USER ) sched->u_last = &next->sched_list;
353        else                            sched->k_last = &next->sched_list;
[1]354
[407]355        // handle FPU ownership
[306]356            if( next->type == THREAD_USER )
[296]357        {
[407]358                if( next == current->core->fpu_owner )  hal_fpu_enable();
359                else                                    hal_fpu_disable();
[296]360        }
[1]361
[435]362        // switch CPU from current thread context to new thread context
[407]363        hal_do_cpu_switch( current->cpu_context, next->cpu_context );
[296]364    }
365    else
366    {
[407]367
[443]368#if DEBUG_SCHED_YIELD
369if( sched->trace )
[435]370printk("\n[DBG] %s : core[%x,%d] / cause = %s\n"
371"      thread %x (%s) (%x,%x) continue / cycle %d\n",
[443]372__FUNCTION__, local_cxy, core->lid, cause, current, thread_type_str(current->type),
373current->process->pid, current->trdid, (uint32_t)hal_get_cycles() );
[428]374#endif
[407]375
[296]376    }
[408]377
[416]378    // handle pending requests for all threads executing on this core.
[433]379    sched_handle_signals( core );
[409]380
[435]381    // exit critical section / restore SR from current thread descriptor
382    hal_restore_irq( CURRENT_THREAD->save_sr );
[408]383
[1]384}  // end sched_yield()
385
[407]386
387///////////////////////////////
388void sched_display( lid_t lid )
[1]389{
[296]390    list_entry_t * iter;
391    thread_t     * thread;
392    uint32_t       save_sr;
[1]393
[436]394    assert( (lid < LOCAL_CLUSTER->cores_nr), __FUNCTION__, "illegal core index %d\n", lid);
[407]395
396    core_t       * core    = &LOCAL_CLUSTER->core_tbl[lid];
[296]397    scheduler_t  * sched   = &core->scheduler;
398   
399    // get pointers on TXT0 chdev
[407]400    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
[296]401    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
402    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
[1]403
[296]404    // get extended pointer on remote TXT0 chdev lock
405    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
[1]406
[296]407    // get TXT0 lock in busy waiting mode
408    remote_spinlock_lock_busy( lock_xp , &save_sr );
409
[437]410    nolock_printk("\n***** threads on core[%x,%d] / current %x / cycle %d\n",
[443]411    local_cxy , core->lid, sched->current, (uint32_t)hal_get_cycles() );
[296]412
413    // display kernel threads
414    LIST_FOREACH( &sched->k_root , iter )
[1]415    {
[296]416        thread = LIST_ELEMENT( iter , thread_t , sched_list );
[408]417        if (thread->type == THREAD_DEV) 
418        {
[416]419            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X / %s\n",
[408]420            thread_type_str( thread->type ), thread->process->pid, thread->trdid,
[416]421            thread, thread->blocked, thread->flags, thread->chdev->name );
[408]422        }
423        else
424        {
[437]425            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
[408]426            thread_type_str( thread->type ), thread->process->pid, thread->trdid,
[437]427            thread, thread->blocked, thread->flags );
[408]428        }
[1]429    }
430
[296]431    // display user threads
432    LIST_FOREACH( &sched->u_root , iter )
[1]433    {
[296]434        thread = LIST_ELEMENT( iter , thread_t , sched_list );
[416]435        nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
[408]436        thread_type_str( thread->type ), thread->process->pid, thread->trdid,
[416]437        thread, thread->blocked, thread->flags );
[1]438    }
439
[296]440    // release TXT0 lock
441    remote_spinlock_unlock_busy( lock_xp , save_sr );
[1]442
[296]443}  // end sched_display()
[1]444
[450]445/////////////////////////////////////
446void sched_remote_display( cxy_t cxy,
447                           lid_t lid )
448{
449    thread_t     * thread;
450    uint32_t       save_sr;
451
452    // check cxy
453    bool_t undefined = cluster_is_undefined( cxy );
454    assert( (undefined == false), __FUNCTION__, "illegal cluster %x\n", cxy );
455
456    // check lid
457    uint32_t cores = hal_remote_lw( XPTR( cxy , &LOCAL_CLUSTER->cores_nr ) );
458    assert( (lid < cores), __FUNCTION__, "illegal core index %d\n", lid);
459
460    // get local pointer on target scheduler
461    core_t      * core  = &LOCAL_CLUSTER->core_tbl[lid];
462    scheduler_t * sched = &core->scheduler;
463
464    // get local pointer on current thread in target scheduler
465    thread_t * current = hal_remote_lpt( XPTR( cxy, &sched->current ) );
466
467    // get local pointer on the first kernel and user threads list_entry
468    list_entry_t * k_entry = hal_remote_lpt( XPTR( cxy , &sched->k_root.next ) );
469    list_entry_t * u_entry = hal_remote_lpt( XPTR( cxy , &sched->u_root.next ) );
470   
471    // get pointers on TXT0 chdev
472    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
473    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
474    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
475
476    // get extended pointer on remote TXT0 chdev lock
477    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
478
479    // get TXT0 lock in busy waiting mode
480    remote_spinlock_lock_busy( lock_xp , &save_sr );
481
482    // display header
483    nolock_printk("\n***** threads on core[%x,%d] / current %x / cycle %d\n",
484    cxy , lid, current, (uint32_t)hal_get_cycles() );
485
486    // display kernel threads
487    while( k_entry != &sched->k_root )
488    {
489        // get local pointer on kernel_thread
490        thread = LIST_ELEMENT( k_entry , thread_t , sched_list );
491
492        // get relevant thead info
493        thread_type_t type    = hal_remote_lw ( XPTR( cxy , &thread->type ) );
494        trdid_t       trdid   = hal_remote_lw ( XPTR( cxy , &thread->trdid ) );
495        uint32_t      blocked = hal_remote_lw ( XPTR( cxy , &thread->blocked ) );
496        uint32_t      flags   = hal_remote_lw ( XPTR( cxy , &thread->flags ) );
497        process_t *   process = hal_remote_lpt( XPTR( cxy , &thread->process ) );
498        pid_t         pid     = hal_remote_lw ( XPTR( cxy , &process->pid ) );
499
500        // display thread info
501        if (type == THREAD_DEV) 
502        {
503            char      name[16];
504            chdev_t * chdev = hal_remote_lpt( XPTR( cxy , &thread->chdev ) );
505            hal_remote_strcpy( XPTR( local_cxy , name ), XPTR( cxy , &chdev->name ) );
506
507            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X / %s\n",
508            thread_type_str( type ), pid, trdid, thread, blocked, flags, name );
509        }
510        else
511        {
512            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
513            thread_type_str( type ), pid, trdid, thread, blocked, flags );
514        }
515
516        // get next remote kernel thread list_entry
517        k_entry = hal_remote_lpt( XPTR( cxy , &k_entry->next ) );
518    }
519
520    // display user threads
521    while( u_entry != &sched->u_root )
522    {
523        // get local pointer on user_thread
524        thread = LIST_ELEMENT( u_entry , thread_t , sched_list );
525
526        // get relevant thead info
527        thread_type_t type    = hal_remote_lw ( XPTR( cxy , &thread->type ) );
528        trdid_t       trdid   = hal_remote_lw ( XPTR( cxy , &thread->trdid ) );
529        uint32_t      blocked = hal_remote_lw ( XPTR( cxy , &thread->blocked ) );
530        uint32_t      flags   = hal_remote_lw ( XPTR( cxy , &thread->flags ) );
531        process_t *   process = hal_remote_lpt( XPTR( cxy , &thread->process ) );
532        pid_t         pid     = hal_remote_lw ( XPTR( cxy , &process->pid ) );
533
534        nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
535        thread_type_str( type ), pid, trdid, thread, blocked, flags );
536
537        // get next user thread list_entry
538        u_entry = hal_remote_lpt( XPTR( cxy , &u_entry->next ) );
539    }
540
541    // release TXT0 lock
542    remote_spinlock_unlock_busy( lock_xp , save_sr );
543
544}  // end sched_remote_display()
545
Note: See TracBrowser for help on using the repository browser.