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

Last change on this file since 458 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
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_kernel_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///////////////////////////////////////////////////////////////////////////////////////////
38// Extern global variables
39///////////////////////////////////////////////////////////////////////////////////////////
40
41uint32_t   idle_thread_count;
42uint32_t   idle_thread_count_active;
43
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
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
55    sched->current        = CURRENT_THREAD;
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()
59
60    // initialise threads lists
61    list_root_init( &sched->u_root );
62    list_root_init( &sched->k_root );
63
64    sched->req_ack_pending = false;             // no pending request
65    sched->trace           = false;             // context switches trace desactivated
66
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++;
83        if( sched->u_last == NULL ) sched->u_last = &thread->sched_list;
84    }
85    else // kernel thread
86    {
87        list_add_last( &sched->k_root , &thread->sched_list );
88        sched->k_threads_nr++;
89        if( sched->k_last == NULL ) sched->k_last = &thread->sched_list; 
90    }
91
92    // release lock
93    hal_fence();
94    spinlock_unlock( &sched->lock );
95
96}  // end sched_register_thread()
97
98//////////////////////////////////////////////
99thread_t * sched_select( scheduler_t * sched )
100{
101    thread_t     * thread;
102    list_entry_t * current;
103    list_entry_t * last;
104    list_entry_t * root;
105    bool_t         done;
106    uint32_t       count;
107
108    // take lock protecting sheduler lists
109    spinlock_lock( &sched->lock );
110
111    // first : scan the kernel threads list if not empty
112    if( list_is_empty( &sched->k_root ) == false )
113    {
114        root    = &sched->k_root;
115        last    = sched->k_last;
116        done    = false;
117        count   = 0;
118        current = last;
119
120        while( done == false )
121        {
122            assert( (count < sched->k_threads_nr), __FUNCTION__, "bad kernel threads list" );
123
124            // get next entry in kernel list
125            current = current->next;
126
127            // check exit condition
128            if( current == last ) done = true;
129
130            // skip the root that does not contain a thread
131            if( current == root ) continue;
132            else                  count++;
133
134            // get thread pointer for this entry
135            thread = LIST_ELEMENT( current , thread_t , sched_list );
136
137            // select kernel thread if non blocked and non THREAD_IDLE
138            if( (thread->blocked == 0)  && (thread->type != THREAD_IDLE) )
139            {
140                spinlock_unlock( &sched->lock );
141                return thread;
142            }
143        } // end loop on kernel threads
144    } // end kernel threads
145
146    // second : scan the user threads list if not empty
147    if( list_is_empty( &sched->u_root ) == false )
148    {
149        root    = &sched->u_root;
150        last    = sched->u_last;
151        done    = false;
152        count   = 0;
153        current = last;
154
155        while( done == false )
156        {
157            assert( (count < sched->u_threads_nr), __FUNCTION__, "bad user threads list" );
158
159            // get next entry in user list
160            current = current->next;
161
162            // check exit condition
163            if( current == last ) done = true;
164
165            // skip the root that does not contain a thread
166            if( current == root ) continue;
167            else                  count++;
168
169            // get thread pointer for this entry
170            thread = LIST_ELEMENT( current , thread_t , sched_list );
171
172            // select thread if non blocked
173            if( thread->blocked == 0 )
174            {
175                spinlock_unlock( &sched->lock );
176                return thread;
177            }
178        } // end loop on user threads
179    } // end user threads
180
181    // third : return idle thread if no other runnable thread
182    spinlock_unlock( &sched->lock );
183    return sched->idle;
184
185}  // end sched_select()
186
187///////////////////////////////////////////
188void sched_handle_signals( core_t * core )
189{
190
191    list_entry_t * iter;
192    list_entry_t * root;
193    thread_t     * thread;
194    process_t    * process;
195    bool_t         last_thread;
196
197    // get pointer on scheduler
198    scheduler_t  * sched = &core->scheduler;
199
200    // get pointer on user threads root
201    root = &sched->u_root;
202
203    // take lock protecting threads lists
204    spinlock_lock( &sched->lock );
205
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
212    // scan all user threads
213    while( iter != root )
214    {
215        // get pointer on thread
216        thread = LIST_ELEMENT( iter , thread_t , sched_list );
217
218        // increment iterator
219        iter = iter->next;
220
221        // handle REQ_ACK
222        if( thread->flags & THREAD_FLAG_REQ_ACK )
223        {
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 );
230
231            // reset REQ_ACK in thread descriptor
232            thread_reset_req_ack( thread );
233        }
234
235        // handle REQ_DELETE
236        if( thread->flags & THREAD_FLAG_REQ_DELETE )
237        {
238            // get thread process descriptor
239            process = thread->process;
240
241                // release FPU if required
242                if( thread->core->fpu_owner == thread )  thread->core->fpu_owner = NULL;
243
244            // remove thread from scheduler (scheduler lock already taken)
245            uint32_t threads_nr = sched->u_threads_nr;
246
247            assert( (threads_nr != 0) , __FUNCTION__ , "u_threads_nr cannot be 0\n" );
248
249            sched->u_threads_nr = threads_nr - 1;
250            list_unlink( &thread->sched_list );
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            }
266
267            // delete thread descriptor
268            last_thread = thread_destroy( thread );
269
270#if DEBUG_SCHED_HANDLE_SIGNALS
271uint32_t cycle = (uint32_t)hal_get_cycles();
272if( DEBUG_SCHED_HANDLE_SIGNALS < cycle )
273printk("\n[DBG] %s : thread %x in process %x on core[%x,%d] deleted / cycle %d\n",
274__FUNCTION__ , thread->trdid , process->pid , local_cxy , thread->core->lid , cycle );
275#endif
276            // destroy process descriptor if no more threads
277            if( last_thread ) 
278            {
279                // delete process   
280                process_destroy( process );
281
282#if DEBUG_SCHED_HANDLE_SIGNALS
283cycle = (uint32_t)hal_get_cycles();
284if( DEBUG_SCHED_HANDLE_SIGNALS < cycle )
285printk("\n[DBG] %s : process %x in cluster %x deleted / cycle %d\n",
286__FUNCTION__ , process->pid , local_cxy , cycle );
287#endif
288            }
289        }
290    }
291
292    // release lock
293    hal_fence();
294    spinlock_unlock( &sched->lock );
295
296} // end sched_handle_signals()
297
298////////////////////////////////
299void sched_yield( char * cause )
300{
301    thread_t    * next;
302    thread_t    * current = CURRENT_THREAD;
303    core_t      * core    = current->core;
304    scheduler_t * sched   = &core->scheduler;
305 
306#if (DEBUG_SCHED_YIELD & 0x1)
307if( sched->trace )
308sched_display( core->lid );
309#endif
310
311    // delay the yield if current thread has locks
312    if( (current->local_locks != 0) || (current->remote_locks != 0) )
313    {
314        current->flags |= THREAD_FLAG_SCHED;
315        return;
316    }
317
318    // enter critical section / save SR in current thread descriptor
319    hal_disable_irq( &CURRENT_THREAD->save_sr );
320
321    // loop on threads to select next thread
322    next = sched_select( sched );
323
324    // check next thread kernel_stack overflow
325    assert( (next->signature == THREAD_SIGNATURE), __FUNCTION__ , 
326    "kernel stack overflow for thread %x on core[%x,%d] \n", next, local_cxy, core->lid );
327
328    // check next thread attached to same core as the calling thread
329    assert( (next->core == current->core), __FUNCTION__ , 
330    "next core %x != current core %x\n", next->core, current->core );
331
332    // check next thread not blocked when type != IDLE
333    assert( ((next->blocked == 0) || (next->type == THREAD_IDLE)) , __FUNCTION__ ,
334    "next thread %x (%s) is blocked on core[%x,%d]\n", 
335    next->trdid , thread_type_str(next->type) , local_cxy , core->lid );
336
337    // switch contexts and update scheduler state if next != current
338        if( next != current )
339    {
340
341#if DEBUG_SCHED_YIELD
342if( sched->trace )
343printk("\n[DBG] %s : core[%x,%d] / cause = %s\n"
344"      thread %x (%s) (%x,%x) => thread %x (%s) (%x,%x) / cycle %d\n",
345__FUNCTION__, local_cxy, core->lid, cause, 
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() );
348#endif
349
350        // update scheduler
351        sched->current = next;
352        if( next->type == THREAD_USER ) sched->u_last = &next->sched_list;
353        else                            sched->k_last = &next->sched_list;
354
355        // handle FPU ownership
356            if( next->type == THREAD_USER )
357        {
358                if( next == current->core->fpu_owner )  hal_fpu_enable();
359                else                                    hal_fpu_disable();
360        }
361
362        // switch CPU from current thread context to new thread context
363        hal_do_cpu_switch( current->cpu_context, next->cpu_context );
364    }
365    else
366    {
367
368#if DEBUG_SCHED_YIELD
369if( sched->trace )
370printk("\n[DBG] %s : core[%x,%d] / cause = %s\n"
371"      thread %x (%s) (%x,%x) continue / cycle %d\n",
372__FUNCTION__, local_cxy, core->lid, cause, current, thread_type_str(current->type),
373current->process->pid, current->trdid, (uint32_t)hal_get_cycles() );
374#endif
375
376    }
377
378    // handle pending requests for all threads executing on this core.
379    sched_handle_signals( core );
380
381    // exit critical section / restore SR from current thread descriptor
382    hal_restore_irq( CURRENT_THREAD->save_sr );
383
384}  // end sched_yield()
385
386
387///////////////////////////////
388void sched_display( lid_t lid )
389{
390    list_entry_t * iter;
391    thread_t     * thread;
392    uint32_t       save_sr;
393
394    assert( (lid < LOCAL_CLUSTER->cores_nr), __FUNCTION__, "illegal core index %d\n", lid);
395
396    core_t       * core    = &LOCAL_CLUSTER->core_tbl[lid];
397    scheduler_t  * sched   = &core->scheduler;
398   
399    // get pointers on TXT0 chdev
400    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
401    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
402    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
403
404    // get extended pointer on remote TXT0 chdev lock
405    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
406
407    // get TXT0 lock in busy waiting mode
408    remote_spinlock_lock_busy( lock_xp , &save_sr );
409
410    nolock_printk("\n***** threads on core[%x,%d] / current %x / cycle %d\n",
411    local_cxy , core->lid, sched->current, (uint32_t)hal_get_cycles() );
412
413    // display kernel threads
414    LIST_FOREACH( &sched->k_root , iter )
415    {
416        thread = LIST_ELEMENT( iter , thread_t , sched_list );
417        if (thread->type == THREAD_DEV) 
418        {
419            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X / %s\n",
420            thread_type_str( thread->type ), thread->process->pid, thread->trdid,
421            thread, thread->blocked, thread->flags, thread->chdev->name );
422        }
423        else
424        {
425            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
426            thread_type_str( thread->type ), thread->process->pid, thread->trdid,
427            thread, thread->blocked, thread->flags );
428        }
429    }
430
431    // display user threads
432    LIST_FOREACH( &sched->u_root , iter )
433    {
434        thread = LIST_ELEMENT( iter , thread_t , sched_list );
435        nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
436        thread_type_str( thread->type ), thread->process->pid, thread->trdid,
437        thread, thread->blocked, thread->flags );
438    }
439
440    // release TXT0 lock
441    remote_spinlock_unlock_busy( lock_xp , save_sr );
442
443}  // end sched_display()
444
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.