[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 | |
---|
[409] | 60 | sched->sig_pending = false; // no pending signal |
---|
| 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 |
---|
| 88 | spinlock_unlock( &sched->lock ); |
---|
| 89 | |
---|
[409] | 90 | } // end sched_register_thread() |
---|
[1] | 91 | |
---|
| 92 | ///////////////////////////////////////////// |
---|
| 93 | void sched_remove_thread( thread_t * thread ) |
---|
| 94 | { |
---|
[409] | 95 | scheduler_t * sched = &thread->core->scheduler; |
---|
| 96 | thread_type_t type = thread->type; |
---|
[1] | 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--; |
---|
[279] | 105 | if( sched->u_threads_nr == 0 ) sched->u_last = NULL; |
---|
[1] | 106 | } |
---|
[409] | 107 | else // kernel thread |
---|
[1] | 108 | { |
---|
| 109 | list_unlink( &thread->sched_list ); |
---|
| 110 | sched->k_threads_nr--; |
---|
[279] | 111 | if( sched->k_threads_nr == 0 ) sched->k_last = NULL; |
---|
[1] | 112 | } |
---|
| 113 | |
---|
[409] | 114 | // release lock |
---|
[1] | 115 | spinlock_unlock( &sched->lock ); |
---|
| 116 | |
---|
[409] | 117 | } // end sched_remove_thread() |
---|
[1] | 118 | |
---|
[408] | 119 | ////////////////////////////////////////////// |
---|
| 120 | thread_t * sched_select( scheduler_t * sched ) |
---|
[1] | 121 | { |
---|
[408] | 122 | thread_t * thread; |
---|
| 123 | list_entry_t * current; |
---|
| 124 | list_entry_t * last; |
---|
[1] | 125 | |
---|
| 126 | // take lock protecting sheduler lists |
---|
| 127 | spinlock_lock( &sched->lock ); |
---|
| 128 | |
---|
[407] | 129 | // first loop : scan the kernel threads list if not empty |
---|
[279] | 130 | if( list_is_empty( &sched->k_root ) == false ) |
---|
[1] | 131 | { |
---|
[279] | 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 ); |
---|
[1] | 138 | |
---|
[279] | 139 | // skip the root that does not contain a thread |
---|
| 140 | if( current == NULL ) current = sched->k_root.next; |
---|
[1] | 141 | |
---|
[279] | 142 | // get thread pointer for this entry |
---|
| 143 | thread = LIST_ELEMENT( current , thread_t , sched_list ); |
---|
[1] | 144 | |
---|
[407] | 145 | // analyse kernel thread type |
---|
| 146 | switch( thread->type ) |
---|
[279] | 147 | { |
---|
[407] | 148 | case THREAD_IDLE: // skip IDLE thread |
---|
| 149 | break; |
---|
[296] | 150 | |
---|
[407] | 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; |
---|
[296] | 159 | |
---|
[408] | 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) ) |
---|
[407] | 163 | { |
---|
| 164 | spinlock_unlock( &sched->lock ); |
---|
| 165 | return thread; |
---|
| 166 | } |
---|
| 167 | break; |
---|
| 168 | } // end switch type |
---|
[1] | 169 | } |
---|
[279] | 170 | while( current != last ); |
---|
[1] | 171 | } |
---|
| 172 | |
---|
[407] | 173 | // second loop : scan the user threads list if not empty |
---|
[279] | 174 | if( list_is_empty( &sched->u_root ) == false ) |
---|
[1] | 175 | { |
---|
[279] | 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 ); |
---|
[1] | 182 | |
---|
[279] | 183 | // skip the root that does not contain a thread |
---|
| 184 | if( current == NULL ) current = sched->u_root.next; |
---|
[1] | 185 | |
---|
[279] | 186 | // get thread pointer for this entry |
---|
| 187 | thread = LIST_ELEMENT( current , thread_t , sched_list ); |
---|
[1] | 188 | |
---|
[279] | 189 | // return thread if runnable |
---|
| 190 | if( thread->blocked == 0 ) |
---|
| 191 | { |
---|
| 192 | spinlock_unlock( &sched->lock ); |
---|
| 193 | return thread; |
---|
| 194 | } |
---|
[1] | 195 | } |
---|
[279] | 196 | while( current != last ); |
---|
[1] | 197 | } |
---|
| 198 | |
---|
[407] | 199 | // third : return idle thread if no runnable thread |
---|
[1] | 200 | spinlock_unlock( &sched->lock ); |
---|
| 201 | return sched->idle; |
---|
| 202 | |
---|
[296] | 203 | } // end sched_select() |
---|
[1] | 204 | |
---|
| 205 | ////////////////////////////////////////// |
---|
| 206 | void sched_handle_signals( core_t * core ) |
---|
| 207 | { |
---|
| 208 | list_entry_t * iter; |
---|
| 209 | thread_t * thread; |
---|
[409] | 210 | |
---|
[1] | 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 | |
---|
[409] | 221 | if( thread->flags & THREAD_FLAG_SIGNAL ) // thread has signal |
---|
[408] | 222 | { |
---|
[409] | 223 | // decrement response counter to acknowledge signal |
---|
| 224 | hal_atomic_add( thread->sig_rsp_count , -1 ); |
---|
[408] | 225 | |
---|
[409] | 226 | // reset signal |
---|
| 227 | thread_reset_signal( thread ); |
---|
[408] | 228 | } |
---|
[1] | 229 | } |
---|
| 230 | |
---|
| 231 | // release lock |
---|
| 232 | spinlock_unlock( &sched->lock ); |
---|
| 233 | |
---|
| 234 | } // end sched_handle_signals() |
---|
| 235 | |
---|
[408] | 236 | //////////////////////////////// |
---|
| 237 | void sched_yield( char * cause ) |
---|
[1] | 238 | { |
---|
[407] | 239 | thread_t * next; |
---|
[1] | 240 | thread_t * current = CURRENT_THREAD; |
---|
[409] | 241 | core_t * core = current->core; |
---|
| 242 | scheduler_t * sched = &core->scheduler; |
---|
[407] | 243 | |
---|
| 244 | #if( CONFIG_SCHED_DEBUG & 0x1 ) |
---|
[409] | 245 | if( hal_time_stamp() > CONFIG_SCHED_DEBUG ) sched_display( core->lid ); |
---|
[407] | 246 | #endif |
---|
[1] | 247 | |
---|
[337] | 248 | // delay the yield if current thread has locks |
---|
[407] | 249 | if( (current->local_locks != 0) || (current->remote_locks != 0) ) |
---|
[337] | 250 | { |
---|
| 251 | current->flags |= THREAD_FLAG_SCHED; |
---|
| 252 | return; |
---|
| 253 | } |
---|
[1] | 254 | |
---|
[408] | 255 | // enter critical section / save SR in current thread context |
---|
| 256 | hal_disable_irq( ¤t->save_sr ); |
---|
| 257 | |
---|
[407] | 258 | // loop on threads to select next thread |
---|
[408] | 259 | next = sched_select( sched ); |
---|
[1] | 260 | |
---|
[296] | 261 | // check next thread attached to same core as the calling thread |
---|
[407] | 262 | assert( (next->core == current->core), __FUNCTION__ , |
---|
| 263 | "next core != current core\n"); |
---|
[296] | 264 | |
---|
[407] | 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", |
---|
[409] | 268 | next->trdid , thread_type_str(next->type) , local_cxy , core->lid ); |
---|
[296] | 269 | |
---|
| 270 | // switch contexts and update scheduler state if next != current |
---|
| 271 | if( next != current ) |
---|
[1] | 272 | { |
---|
| 273 | |
---|
[408] | 274 | sched_dmsg("\n[DBG] %s : core[%x,%d] / cause = %s\n" |
---|
| 275 | " thread %x (%s) (%x,%x) => thread %x (%s) (%x,%x) / cycle %d\n", |
---|
[409] | 276 | __FUNCTION__, local_cxy, core->lid, cause, |
---|
[407] | 277 | current, thread_type_str(current->type), current->process->pid, current->trdid, |
---|
| 278 | next , thread_type_str(next->type) , next->process->pid , next->trdid, |
---|
[408] | 279 | (uint32_t)hal_get_cycles() ); |
---|
[279] | 280 | |
---|
[296] | 281 | // update scheduler |
---|
[408] | 282 | sched->current = next; |
---|
| 283 | if( next->type == THREAD_USER ) sched->u_last = &next->sched_list; |
---|
| 284 | else sched->k_last = &next->sched_list; |
---|
[1] | 285 | |
---|
[407] | 286 | // handle FPU ownership |
---|
[306] | 287 | if( next->type == THREAD_USER ) |
---|
[296] | 288 | { |
---|
[407] | 289 | if( next == current->core->fpu_owner ) hal_fpu_enable(); |
---|
| 290 | else hal_fpu_disable(); |
---|
[296] | 291 | } |
---|
[1] | 292 | |
---|
[407] | 293 | // switch CPU from calling thread context to new thread context |
---|
| 294 | hal_do_cpu_switch( current->cpu_context, next->cpu_context ); |
---|
[296] | 295 | } |
---|
| 296 | else |
---|
| 297 | { |
---|
[407] | 298 | |
---|
[408] | 299 | sched_dmsg("\n[DBG] %s : core[%x,%d] / cause = %s\n" |
---|
| 300 | " thread %x (%s) (%x,%x) continue / cycle %d\n", |
---|
[409] | 301 | __FUNCTION__, local_cxy, core->lid, cause, |
---|
[408] | 302 | current, thread_type_str(current->type), current->process->pid, current->trdid, |
---|
| 303 | (uint32_t)hal_get_cycles() ); |
---|
[407] | 304 | |
---|
[296] | 305 | } |
---|
[408] | 306 | |
---|
[409] | 307 | // handle signals for all threads executing on this core. |
---|
| 308 | sched_handle_signals( core ); |
---|
| 309 | |
---|
[408] | 310 | // exit critical section / restore SR from next thread context |
---|
| 311 | hal_restore_irq( next->save_sr ); |
---|
| 312 | |
---|
[1] | 313 | } // end sched_yield() |
---|
| 314 | |
---|
[407] | 315 | |
---|
| 316 | /////////////////////////////// |
---|
| 317 | void sched_display( lid_t lid ) |
---|
[1] | 318 | { |
---|
[296] | 319 | list_entry_t * iter; |
---|
| 320 | thread_t * thread; |
---|
| 321 | uint32_t save_sr; |
---|
[1] | 322 | |
---|
[407] | 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]; |
---|
[296] | 331 | scheduler_t * sched = &core->scheduler; |
---|
| 332 | |
---|
| 333 | // get pointers on TXT0 chdev |
---|
[407] | 334 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
[296] | 335 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
| 336 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
[1] | 337 | |
---|
[296] | 338 | // get extended pointer on remote TXT0 chdev lock |
---|
| 339 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
[1] | 340 | |
---|
[296] | 341 | // get TXT0 lock in busy waiting mode |
---|
| 342 | remote_spinlock_lock_busy( lock_xp , &save_sr ); |
---|
| 343 | |
---|
[407] | 344 | nolock_printk("\n***** scheduler state for core[%x,%d] at cycle %d\n" |
---|
[408] | 345 | "kernel_threads = %d / user_threads = %d / current = (%x,%x)\n", |
---|
[407] | 346 | local_cxy , core->lid, hal_time_stamp(), |
---|
| 347 | sched->k_threads_nr, sched->u_threads_nr, |
---|
[408] | 348 | sched->current->process->pid , sched->current->trdid ); |
---|
[296] | 349 | |
---|
| 350 | // display kernel threads |
---|
| 351 | LIST_FOREACH( &sched->k_root , iter ) |
---|
[1] | 352 | { |
---|
[296] | 353 | thread = LIST_ELEMENT( iter , thread_t , sched_list ); |
---|
[408] | 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 | } |
---|
[1] | 366 | } |
---|
| 367 | |
---|
[296] | 368 | // display user threads |
---|
| 369 | LIST_FOREACH( &sched->u_root , iter ) |
---|
[1] | 370 | { |
---|
[296] | 371 | thread = LIST_ELEMENT( iter , thread_t , sched_list ); |
---|
[408] | 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 ); |
---|
[1] | 375 | } |
---|
| 376 | |
---|
[296] | 377 | // release TXT0 lock |
---|
| 378 | remote_spinlock_unlock_busy( lock_xp , save_sr ); |
---|
[1] | 379 | |
---|
[296] | 380 | } // end sched_display() |
---|
[1] | 381 | |
---|