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