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

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

Use panic().

File size: 12.1 KB
Line 
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
24#include <kernel_config.h>
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>
32#include <chdev.h>
33#include <scheduler.h>
34
35///////////////////////////////////////////////////////////////////////////////////////////
36// Extern global variables
37///////////////////////////////////////////////////////////////////////////////////////////
38
39extern chdev_directory_t    chdev_dir;            // allocated in kernel_init.c file
40
41
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
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()
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    {
73        // register thread in scheduler user list
74        list_add_last( &sched->u_root , &thread->sched_list );
75        sched->u_threads_nr++;
76
77        // initialize u_last field if first user thread
78        if( sched->u_last == NULL ) sched->u_last = &thread->sched_list;
79    }
80    else // kernel thread
81    {
82        // register thread in scheduler kernel list
83        list_add_last( &sched->k_root , &thread->sched_list );
84        sched->k_threads_nr++;
85
86        // initialize k_last field if first kernel thread
87        if( sched->k_last == NULL ) sched->k_last = &thread->sched_list; 
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    {
107        // remove thread from user list
108        list_unlink( &thread->sched_list );
109        sched->u_threads_nr--;
110
111        // reset the u_last field if list empty
112        if( sched->u_threads_nr == 0 ) sched->u_last = NULL;
113    }
114    else // kernel thread
115    {
116        // remove thread from kernel list
117        list_unlink( &thread->sched_list );
118        sched->k_threads_nr--;
119
120        // reset the k_last field if list empty
121        if( sched->k_threads_nr == 0 ) sched->k_last = NULL;
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    {
135        panic("thread %x in process %x on core[%x][%d]"
136              " did not released all locks",
137              thread->trdid , thread->process->pid, 
138              local_cxy , thread->core->lid ); 
139    }
140
141    // remove thread from scheduler
142    sched_remove_thread( thread );
143
144    // reset the THREAD_SIG_KILL signal
145    thread_reset_signal( thread , THREAD_SIG_KILL );
146
147}  // end sched_kill_thread()
148
149////////////////////////////////////////
150thread_t * sched_select( core_t * core )
151{
152    thread_t    * thread;
153
154    scheduler_t * sched = &core->scheduler;
155
156    sched_dmsg("\n[INFO] %s : enter core[%x,%d] / cycle %d\n",
157    __FUNCTION__ , local_cxy , core->lid , hal_time_stamp() );
158
159    // take lock protecting sheduler lists
160    spinlock_lock( &sched->lock );
161
162    list_entry_t * current;
163    list_entry_t * last;
164
165    // first : scan the kernel threads list if not empty
166    if( list_is_empty( &sched->k_root ) == false )
167    {
168        last    = sched->k_last;
169        current = sched->k_last;
170        do
171        {
172            // get next entry in kernel list
173            current = list_next( &sched->k_root , current );
174
175            // skip the root that does not contain a thread
176            if( current == NULL ) current = sched->k_root.next;
177
178            // get thread pointer for this entry
179            thread = LIST_ELEMENT( current , thread_t , sched_list );
180
181            // return thread if not idle_thread and runnable
182            if( (thread->type != THREAD_IDLE) && (thread->blocked == 0) ) 
183            {
184                // release lock
185                spinlock_unlock( &sched->lock );
186
187                sched_dmsg("\n[INFO] %s : exit core[%x,%d] / k_thread = %x / cycle %d\n",
188                __FUNCTION__ , local_cxy , core->lid , thread->trdid , hal_time_stamp() );
189
190                return thread;
191            }
192        }
193        while( current != last );
194    }
195
196    // second : scan the user threads list if not empty
197    if( list_is_empty( &sched->u_root ) == false )
198    {
199        last    = sched->u_last;
200        current = sched->u_last;
201        do
202        {
203            // get next entry in user list
204            current = list_next( &sched->u_root , current );
205
206            // skip the root that does not contain a thread
207            if( current == NULL ) current = sched->u_root.next;
208
209            // get thread pointer for this entry
210            thread = LIST_ELEMENT( current , thread_t , sched_list );
211
212            // return thread if runnable
213            if( thread->blocked == 0 )
214            {
215                // release lock
216                spinlock_unlock( &sched->lock );
217
218                sched_dmsg("\n[INFO] %s : exit core[%x,%d] / u_thread = %x / cycle %d\n",
219                __FUNCTION__ , local_cxy , core->lid , thread->trdid , hal_time_stamp() );
220                return thread;
221            }
222        }
223        while( current != last );
224    }
225
226    // release lock
227    spinlock_unlock( &sched->lock );
228
229    sched_dmsg("\n[INFO] %s : exit core[%x,%d] / idle = %x / cycle %d\n",
230    __FUNCTION__ , local_cxy , core->lid , sched->idle->trdid , hal_time_stamp() );
231
232    // third : return idle thread if no runnable thread
233    return sched->idle;
234
235}  // end sched_select()
236
237//////////////////////////////////////////
238void sched_handle_signals( core_t * core )
239{
240    list_entry_t * iter;
241    thread_t     * thread;
242    scheduler_t  * sched = &core->scheduler;
243
244    sched_dmsg("\n[INFO] %s : enter / thread %x on core[%x,%d]\n",
245    __FUNCTION__, CURRENT_THREAD->trdid , local_cxy , core->lid );
246
247    // take lock protecting threads lists
248    spinlock_lock( &sched->lock );
249
250    // handle user threads
251    LIST_FOREACH( &sched->u_root , iter )
252    {
253        thread = LIST_ELEMENT( iter , thread_t , sched_list );
254        if( thread->signals & THREAD_SIG_KILL )  sched_kill_thread( thread );
255    }
256
257    // handle kernel threads
258    LIST_FOREACH( &sched->k_root , iter )
259    {
260        thread = LIST_ELEMENT( iter , thread_t , sched_list );
261        if( thread->signals & THREAD_SIG_KILL )  sched_kill_thread( thread );
262    }
263
264    // release lock
265    spinlock_unlock( &sched->lock );
266
267    sched_dmsg("\n[INFO] %s : exit / thread %x on core[%x,%d]\n",
268    __FUNCTION__, CURRENT_THREAD->trdid , local_cxy , core->lid );
269
270} // end sched_handle_signals()
271
272///////////////////////////////////
273void sched_yield( thread_t * next )
274{
275    reg_t         sr_save;
276
277    thread_t    * current = CURRENT_THREAD;
278    core_t      * core    = current->core;
279    scheduler_t * sched   = &core->scheduler;
280
281    sched_dmsg("\n[INFO] %s : thread %x on core[%x,%d] enter / cycle %d\n",
282    __FUNCTION__, current->trdid, local_cxy, core->lid, hal_time_stamp() );
283
284    // delay the yield if current thread has locks
285    if( thread_can_yield() == false )
286    {
287        current->flags |= THREAD_FLAG_SCHED;
288        return;
289    }
290
291    // first loop on all threads to handle pending signals
292    sched_handle_signals( core );
293
294    // second loop on threads to select next thread if required
295    if( next == NULL ) next = sched_select( core );
296
297    // check next thread attached to same core as the calling thread
298    assert( (next->core == current->core), __FUNCTION__ , "next core != current core\n");
299
300    // check next thread not blocked
301    assert( (next->blocked == 0), __FUNCTION__ , "next thread is blocked\n");
302
303    // switch contexts and update scheduler state if next != current
304        if( next != current )
305    {
306        sched_dmsg("\n[INFO] %s : trd %x (%s) on core[%x,%d] => trd %x (%s) / cycle %d\n",
307        __FUNCTION__, current->trdid, thread_type_str(current->type), local_cxy, core->lid, 
308        next->trdid, thread_type_str(next->type), hal_time_stamp() );
309
310        // calling thread desactivate IRQs
311        hal_disable_irq( &sr_save );
312
313        // update scheduler
314        if( current->type == THREAD_USER ) sched->u_last = &current->sched_list;
315        else                               sched->k_last = &current->sched_list;
316        sched->current = next;
317
318        // handle FPU
319            if( next->type == THREAD_USER )
320        {
321                if( next == core->fpu_owner )  hal_fpu_enable();
322                else                           hal_fpu_disable();
323        }
324
325        // switch contexts
326        hal_cpu_context_switch( current , next );
327
328        // restore IRQs when calling thread resume
329        hal_restore_irq( sr_save );
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    }
336}  // end sched_yield()
337
338////////////////////
339void sched_display()
340{
341    list_entry_t * iter;
342    thread_t     * thread;
343    uint32_t       save_sr;
344
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 );
353
354    // get extended pointer on remote TXT0 chdev lock
355    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
356
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 )
367    {
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 );
372    }
373
374    // display user threads
375    LIST_FOREACH( &sched->u_root , iter )
376    {
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 );
381    }
382
383    // release TXT0 lock
384    remote_spinlock_unlock_busy( lock_xp , save_sr );
385
386}  // end sched_display()
387
Note: See TracBrowser for help on using the repository browser.