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