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

Last change on this file since 312 was 311, checked in by max@…, 7 years ago

Introduce hal_cpu_context_switch.

File size: 12.3 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>
[1]25#include <hal_types.h>
26#include <hal_irqmask.h>
27#include <hal_context.h>
28#include <printk.h>
29#include <list.h>
30#include <core.h>
31#include <thread.h>
[296]32#include <chdev.h>
[1]33#include <scheduler.h>
34
[296]35///////////////////////////////////////////////////////////////////////////////////////////
36// Extern global variables
37///////////////////////////////////////////////////////////////////////////////////////////
[1]38
[296]39extern chdev_directory_t    chdev_dir;            // allocated in kernel_init.c file
40
41
[1]42////////////////////////////////
43void sched_init( core_t * core )
44{
45    scheduler_t * sched = &core->scheduler;
46
47    sched->u_threads_nr   = 0;
48    sched->k_threads_nr   = 0;
49
[279]50    sched->current        = CURRENT_THREAD;
51    sched->idle           = NULL;             // initialized in kernel_init()
52    sched->u_last         = NULL;             // initialized in sched_register_thread()
53    sched->k_last         = NULL;             // initialized in sched_register_thread()
[1]54
55    // initialise threads lists
56    list_root_init( &sched->u_root );
57    list_root_init( &sched->k_root );
58
59}  // end sched_init()
60
61////////////////////////////////////////////
62void sched_register_thread( core_t   * core,
63                            thread_t * thread )
64{
65    scheduler_t * sched = &core->scheduler;
66    thread_type_t type  = thread->type;
67
68    // take lock protecting sheduler lists
69    spinlock_lock( &sched->lock );
70
71    if( type == THREAD_USER )
72    {
[279]73        // register thread in scheduler user list
[1]74        list_add_last( &sched->u_root , &thread->sched_list );
75        sched->u_threads_nr++;
[279]76
77        // initialize u_last field if first user thread
78        if( sched->u_last == NULL ) sched->u_last = &thread->sched_list;
[1]79    }
80    else // kernel thread
81    {
[279]82        // register thread in scheduler kernel list
[1]83        list_add_last( &sched->k_root , &thread->sched_list );
84        sched->k_threads_nr++;
[279]85
86        // initialize k_last field if first kernel thread
87        if( sched->k_last == NULL ) sched->k_last = &thread->sched_list; 
[1]88    }
89
90    // release lock
91    spinlock_unlock( &sched->lock );
92
93}  // end sched_register()
94
95/////////////////////////////////////////////
96void sched_remove_thread( thread_t * thread )
97{
98    core_t       * core  = thread->core;
99    scheduler_t  * sched = &core->scheduler;
100    thread_type_t  type  = thread->type;
101
102    // take lock protecting sheduler lists
103    spinlock_lock( &sched->lock );
104
105    if( type == THREAD_USER )
106    {
[279]107        // remove thread from user list
[1]108        list_unlink( &thread->sched_list );
109        sched->u_threads_nr--;
[279]110
111        // reset the u_last field if list empty
112        if( sched->u_threads_nr == 0 ) sched->u_last = NULL;
[1]113    }
114    else // kernel thread
115    {
[279]116        // remove thread from kernel list
[1]117        list_unlink( &thread->sched_list );
118        sched->k_threads_nr--;
[279]119
120        // reset the k_last field if list empty
121        if( sched->k_threads_nr == 0 ) sched->k_last = NULL;
[1]122    }
123
124    // release lock
125    spinlock_unlock( &sched->lock );
126
127}  // end sched_remove()
128
129///////////////////////////////////////////
130void sched_kill_thread( thread_t * thread )
131{
132    // check thread locks
133    if( thread_can_yield() == false )
134    {
[14]135        printk("\n[PANIC] in %s : thread %x in process %x on core[%x][%d]"
[1]136               " did not released all locks\n",
137               __FUNCTION__ , thread->trdid , thread->process->pid, 
[14]138               local_cxy , thread->core->lid ); 
[1]139        hal_core_sleep();
140    }
141
142    // remove thread from scheduler
143    sched_remove_thread( thread );
144
145    // reset the THREAD_SIG_KILL signal
146    thread_reset_signal( thread , THREAD_SIG_KILL );
147
148}  // end sched_kill_thread()
149
150////////////////////////////////////////
151thread_t * sched_select( core_t * core )
152{
[296]153    thread_t    * thread;
[1]154
155    scheduler_t * sched = &core->scheduler;
156
[296]157    sched_dmsg("\n[INFO] %s : enter core[%x,%d] / cycle %d\n",
158    __FUNCTION__ , local_cxy , core->lid , hal_time_stamp() );
159
[1]160    // take lock protecting sheduler lists
161    spinlock_lock( &sched->lock );
162
163    list_entry_t * current;
164    list_entry_t * last;
165
[296]166    // first : scan the kernel threads list if not empty
[279]167    if( list_is_empty( &sched->k_root ) == false )
[1]168    {
[279]169        last    = sched->k_last;
170        current = sched->k_last;
171        do
172        {
173            // get next entry in kernel list
174            current = list_next( &sched->k_root , current );
[1]175
[279]176            // skip the root that does not contain a thread
177            if( current == NULL ) current = sched->k_root.next;
[1]178
[279]179            // get thread pointer for this entry
180            thread = LIST_ELEMENT( current , thread_t , sched_list );
[1]181
[296]182            // return thread if not idle_thread and runnable
183            if( (thread->type != THREAD_IDLE) && (thread->blocked == 0) ) 
[279]184            {
185                // release lock
186                spinlock_unlock( &sched->lock );
[296]187
188                sched_dmsg("\n[INFO] %s : exit core[%x,%d] / k_thread = %x / cycle %d\n",
189                __FUNCTION__ , local_cxy , core->lid , thread->trdid , hal_time_stamp() );
190
[279]191                return thread;
192            }
[1]193        }
[279]194        while( current != last );
[1]195    }
196
[296]197    // second : scan the user threads list if not empty
[279]198    if( list_is_empty( &sched->u_root ) == false )
[1]199    {
[279]200        last    = sched->u_last;
201        current = sched->u_last;
202        do
203        {
204            // get next entry in user list
205            current = list_next( &sched->u_root , current );
[1]206
[279]207            // skip the root that does not contain a thread
208            if( current == NULL ) current = sched->u_root.next;
[1]209
[279]210            // get thread pointer for this entry
211            thread = LIST_ELEMENT( current , thread_t , sched_list );
[1]212
[279]213            // return thread if runnable
214            if( thread->blocked == 0 )
215            {
216                // release lock
217                spinlock_unlock( &sched->lock );
[296]218
219                sched_dmsg("\n[INFO] %s : exit core[%x,%d] / u_thread = %x / cycle %d\n",
220                __FUNCTION__ , local_cxy , core->lid , thread->trdid , hal_time_stamp() );
[279]221                return thread;
222            }
[1]223        }
[279]224        while( current != last );
[1]225    }
226
227    // release lock
228    spinlock_unlock( &sched->lock );
229
[296]230    sched_dmsg("\n[INFO] %s : exit core[%x,%d] / idle = %x / cycle %d\n",
231    __FUNCTION__ , local_cxy , core->lid , sched->idle->trdid , hal_time_stamp() );
232
[279]233    // third : return idle thread if no runnable thread
[1]234    return sched->idle;
235
[296]236}  // end sched_select()
[1]237
238//////////////////////////////////////////
239void sched_handle_signals( core_t * core )
240{
241    list_entry_t * iter;
242    thread_t     * thread;
243    scheduler_t  * sched = &core->scheduler;
244
[296]245    sched_dmsg("\n[INFO] %s : enter / thread %x on core[%x,%d]\n",
246    __FUNCTION__, CURRENT_THREAD->trdid , local_cxy , core->lid );
247
[1]248    // take lock protecting threads lists
249    spinlock_lock( &sched->lock );
250
251    // handle user threads
252    LIST_FOREACH( &sched->u_root , iter )
253    {
254        thread = LIST_ELEMENT( iter , thread_t , sched_list );
255        if( thread->signals & THREAD_SIG_KILL )  sched_kill_thread( thread );
256    }
257
258    // handle kernel threads
259    LIST_FOREACH( &sched->k_root , iter )
260    {
261        thread = LIST_ELEMENT( iter , thread_t , sched_list );
262        if( thread->signals & THREAD_SIG_KILL )  sched_kill_thread( thread );
263    }
264
265    // release lock
266    spinlock_unlock( &sched->lock );
267
[296]268    sched_dmsg("\n[INFO] %s : exit / thread %x on core[%x,%d]\n",
269    __FUNCTION__, CURRENT_THREAD->trdid , local_cxy , core->lid );
270
[1]271} // end sched_handle_signals()
272
[296]273///////////////////////////////////
274void sched_yield( thread_t * next )
[1]275{
[60]276    reg_t         sr_save;
[1]277
278    thread_t    * current = CURRENT_THREAD;
279    core_t      * core    = current->core;
[279]280    scheduler_t * sched   = &core->scheduler;
[1]281
[296]282    sched_dmsg("\n[INFO] %s : thread %x on core[%x,%d] enter / cycle %d\n",
283    __FUNCTION__, current->trdid, local_cxy, core->lid, hal_time_stamp() );
[1]284
[296]285    // check calling thread released all locks
286    assert( (thread_can_yield() == true), __FUNCTION__, "locks not released\n");
[1]287
288    // first loop on all threads to handle pending signals
289    sched_handle_signals( core );
290
[296]291    // second loop on threads to select next thread if required
292    if( next == NULL ) next = sched_select( core );
[1]293
[296]294    // check next thread attached to same core as the calling thread
295    assert( (next->core == current->core), __FUNCTION__ , "next core != current core\n");
296
297    // check next thread not blocked
298    assert( (next->blocked == 0), __FUNCTION__ , "next thread is blocked\n");
299
300    // switch contexts and update scheduler state if next != current
301        if( next != current )
[1]302    {
[296]303        sched_dmsg("\n[INFO] %s : trd %x (%s) on core[%x,%d] => trd %x (%s) / cycle %d\n",
304        __FUNCTION__, current->trdid, thread_type_str(current->type), local_cxy, core->lid, 
305        next->trdid, thread_type_str(next->type), hal_time_stamp() );
[1]306
[296]307        // calling thread desactivate IRQs
308        hal_disable_irq( &sr_save );
[279]309
[296]310        // update scheduler
[279]311        if( current->type == THREAD_USER ) sched->u_last = &current->sched_list;
312        else                               sched->k_last = &current->sched_list;
313        sched->current = next;
[1]314
[296]315        // handle FPU
[306]316            if( next->type == THREAD_USER )
[296]317        {
318                if( next == core->fpu_owner )  hal_fpu_enable();
319                else                           hal_fpu_disable();
320        }
[1]321
[296]322        // switch contexts
[311]323        hal_cpu_context_switch( current , next );
[1]324
[296]325        // restore IRQs when calling thread resume
326        hal_restore_irq( sr_save );
[1]327
[296]328        sched_dmsg("\n[INFO] %s : thread %x on core[%x,%d] / cycle %d\n",
329        __FUNCTION__, current->trdid, local_cxy, core->lid, hal_time_stamp() );
330    }
331    else
332    {
333        sched_dmsg("\n[INFO] %s : thread %x on core[%x,%d] continue / cycle %d\n",
334        __FUNCTION__, current->trdid, local_cxy, core->lid, hal_time_stamp() );
335    }
[1]336}  // end sched_yield()
337
[296]338////////////////////
339void sched_display()
[1]340{
[296]341    list_entry_t * iter;
342    thread_t     * thread;
343    uint32_t       save_sr;
[1]344
[296]345    thread_t     * current = CURRENT_THREAD;
346    core_t       * core    = current->core;
347    scheduler_t  * sched   = &core->scheduler;
348   
349    // get pointers on TXT0 chdev
350    xptr_t    txt0_xp  = chdev_dir.txt[0];
351    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
352    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
[1]353
[296]354    // get extended pointer on remote TXT0 chdev lock
355    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
[1]356
[296]357    // get TXT0 lock in busy waiting mode
358    remote_spinlock_lock_busy( lock_xp , &save_sr );
359
360    nolock_printk("\n********** scheduler state for core[%x,%d] **********************\n"
361           "kernel_threads = %d / user_threads = %d / current = %x\n",
362            local_cxy , core->lid, 
363            sched->k_threads_nr, sched->u_threads_nr, sched->current->trdid );
364
365    // display kernel threads
366    LIST_FOREACH( &sched->k_root , iter )
[1]367    {
[296]368        thread = LIST_ELEMENT( iter , thread_t , sched_list );
369        nolock_printk(" - type = %s / trdid = %x / pid = %x / func = %x / blocked_vect = %x\n",
370        thread_type_str( thread->type ), thread->trdid, thread->process->pid,
371        thread->entry_func, thread->blocked );
[1]372    }
373
[296]374    // display user threads
375    LIST_FOREACH( &sched->u_root , iter )
[1]376    {
[296]377        thread = LIST_ELEMENT( iter , thread_t , sched_list );
378        nolock_printk(" - type = %s / trdid = %x / pid = %x / func = %x / blocked_vect = %x\n",
379        thread_type_str( thread->type ), thread->trdid, thread->process->pid,
380        thread->entry_func, thread->blocked );
[1]381    }
382
[296]383    // release TXT0 lock
384    remote_spinlock_unlock_busy( lock_xp , save_sr );
[1]385
[296]386}  // end sched_display()
[1]387
Note: See TracBrowser for help on using the repository browser.