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

Last change on this file since 414 was 409, checked in by alain, 7 years ago

Fix bugs in exec

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