[8118303] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // kernel.c -- |
---|
| 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
[75f3522] | 10 | // Created On : Tue Jan 17 12:27:26 2017 |
---|
[6b0b624] | 11 | // Last Modified By : Peter A. Buhr |
---|
[bb83b47] | 12 | // Last Modified On : Thu Jul 9 06:22:54 2020 |
---|
| 13 | // Update Count : 66 |
---|
[8118303] | 14 | // |
---|
| 15 | |
---|
[2026bb6] | 16 | #define __cforall_thread__ |
---|
[4069faad] | 17 | // #define __CFA_DEBUG_PRINT_RUNTIME_CORE__ |
---|
[2026bb6] | 18 | |
---|
[8118303] | 19 | //C Includes |
---|
[214e8da] | 20 | #include <errno.h> |
---|
[9d944b2] | 21 | #include <stdio.h> |
---|
[58b6d1b] | 22 | #include <signal.h> |
---|
[9d944b2] | 23 | #include <unistd.h> |
---|
[8118303] | 24 | |
---|
| 25 | //CFA Includes |
---|
[73abe95] | 26 | #include "kernel_private.hfa" |
---|
| 27 | #include "preemption.hfa" |
---|
[8118303] | 28 | |
---|
| 29 | //Private includes |
---|
| 30 | #define __CFA_INVOKE_PRIVATE__ |
---|
| 31 | #include "invoke.h" |
---|
| 32 | |
---|
[4069faad] | 33 | |
---|
[deca0f5] | 34 | //----------------------------------------------------------------------------- |
---|
| 35 | // Some assembly required |
---|
[1805b1b] | 36 | #if defined( __i386 ) |
---|
[deca0f5] | 37 | // mxcr : SSE Status and Control bits (control bits are preserved across function calls) |
---|
| 38 | // fcw : X87 FPU control word (preserved across function calls) |
---|
| 39 | #define __x87_store \ |
---|
| 40 | uint32_t __mxcr; \ |
---|
| 41 | uint16_t __fcw; \ |
---|
| 42 | __asm__ volatile ( \ |
---|
| 43 | "stmxcsr %0\n" \ |
---|
| 44 | "fnstcw %1\n" \ |
---|
| 45 | : "=m" (__mxcr),\ |
---|
| 46 | "=m" (__fcw) \ |
---|
| 47 | ) |
---|
| 48 | |
---|
| 49 | #define __x87_load \ |
---|
| 50 | __asm__ volatile ( \ |
---|
| 51 | "fldcw %1\n" \ |
---|
| 52 | "ldmxcsr %0\n" \ |
---|
| 53 | ::"m" (__mxcr),\ |
---|
| 54 | "m" (__fcw) \ |
---|
| 55 | ) |
---|
| 56 | |
---|
| 57 | #elif defined( __x86_64 ) |
---|
| 58 | #define __x87_store \ |
---|
| 59 | uint32_t __mxcr; \ |
---|
| 60 | uint16_t __fcw; \ |
---|
| 61 | __asm__ volatile ( \ |
---|
| 62 | "stmxcsr %0\n" \ |
---|
| 63 | "fnstcw %1\n" \ |
---|
| 64 | : "=m" (__mxcr),\ |
---|
| 65 | "=m" (__fcw) \ |
---|
| 66 | ) |
---|
| 67 | |
---|
| 68 | #define __x87_load \ |
---|
| 69 | __asm__ volatile ( \ |
---|
| 70 | "fldcw %1\n" \ |
---|
| 71 | "ldmxcsr %0\n" \ |
---|
| 72 | :: "m" (__mxcr),\ |
---|
| 73 | "m" (__fcw) \ |
---|
| 74 | ) |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | #elif defined( __ARM_ARCH ) |
---|
| 78 | #else |
---|
| 79 | #error unknown hardware architecture |
---|
| 80 | #endif |
---|
| 81 | |
---|
[e660761] | 82 | extern $thread * mainThread; |
---|
| 83 | extern processor * mainProcessor; |
---|
[2ac095d] | 84 | |
---|
[92e7631] | 85 | //----------------------------------------------------------------------------- |
---|
| 86 | // Kernel Scheduling logic |
---|
| 87 | static $thread * __next_thread(cluster * this); |
---|
[1eb239e4] | 88 | static $thread * __next_thread_slow(cluster * this); |
---|
[92e7631] | 89 | static void __run_thread(processor * this, $thread * dst); |
---|
[1eb239e4] | 90 | static void __wake_one(struct __processor_id_t * id, cluster * cltr); |
---|
| 91 | |
---|
| 92 | static void push (__cluster_idles & idles, processor & proc); |
---|
| 93 | static void remove(__cluster_idles & idles, processor & proc); |
---|
| 94 | static [unsigned idle, unsigned total, * processor] query( & __cluster_idles idles ); |
---|
| 95 | |
---|
[c84e80a] | 96 | |
---|
[75f3522] | 97 | //============================================================================================= |
---|
| 98 | // Kernel Scheduling logic |
---|
| 99 | //============================================================================================= |
---|
[8fcbb4c] | 100 | //Main of the processor contexts |
---|
[83a071f9] | 101 | void main(processorCtx_t & runner) { |
---|
[21184e3] | 102 | // Because of a bug, we couldn't initialized the seed on construction |
---|
| 103 | // Do it here |
---|
[7768b8d] | 104 | kernelTLS.rand_seed ^= rdtscl(); |
---|
[21184e3] | 105 | |
---|
[83a071f9] | 106 | processor * this = runner.proc; |
---|
[094476d] | 107 | verify(this); |
---|
[c81ebf9] | 108 | |
---|
[4069faad] | 109 | __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this); |
---|
[28d73c1] | 110 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 111 | if( this->print_halts ) { |
---|
| 112 | __cfaabi_bits_print_safe( STDOUT_FILENO, "Processor : %d - %s (%p)\n", this->id, this->name, (void*)this); |
---|
| 113 | } |
---|
| 114 | #endif |
---|
[b798713] | 115 | |
---|
[75f3522] | 116 | { |
---|
[c81ebf9] | 117 | // Setup preemption data |
---|
| 118 | preemption_scope scope = { this }; |
---|
| 119 | |
---|
[4069faad] | 120 | __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this); |
---|
[8118303] | 121 | |
---|
[ac2b598] | 122 | $thread * readyThread = 0p; |
---|
[1eb239e4] | 123 | MAIN_LOOP: |
---|
| 124 | for() { |
---|
[92e7631] | 125 | // Try to get the next thread |
---|
[8c50aed] | 126 | readyThread = __next_thread( this->cltr ); |
---|
[75f3522] | 127 | |
---|
[1eb239e4] | 128 | if( !readyThread ) { |
---|
| 129 | readyThread = __next_thread_slow( this->cltr ); |
---|
| 130 | } |
---|
[4e6fb8e] | 131 | |
---|
[1eb239e4] | 132 | HALT: |
---|
| 133 | if( !readyThread ) { |
---|
| 134 | // Don't block if we are done |
---|
| 135 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP; |
---|
[c81ebf9] | 136 | |
---|
[1eb239e4] | 137 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 138 | __tls_stats()->ready.sleep.halts++; |
---|
| 139 | #endif |
---|
[398e8e9] | 140 | |
---|
[1eb239e4] | 141 | // Push self to idle stack |
---|
| 142 | push(this->cltr->idles, * this); |
---|
[398e8e9] | 143 | |
---|
[1eb239e4] | 144 | // Confirm the ready-queue is empty |
---|
| 145 | readyThread = __next_thread_slow( this->cltr ); |
---|
| 146 | if( readyThread ) { |
---|
| 147 | // A thread was found, cancel the halt |
---|
| 148 | remove(this->cltr->idles, * this); |
---|
| 149 | |
---|
| 150 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 151 | __tls_stats()->ready.sleep.cancels++; |
---|
| 152 | #endif |
---|
| 153 | |
---|
| 154 | // continue the mai loop |
---|
| 155 | break HALT; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 159 | if(this->print_halts) { |
---|
| 160 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->id, rdtscl()); |
---|
| 161 | } |
---|
| 162 | #endif |
---|
| 163 | |
---|
| 164 | wait( this->idle ); |
---|
| 165 | |
---|
| 166 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 167 | if(this->print_halts) { |
---|
| 168 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->id, rdtscl()); |
---|
| 169 | } |
---|
| 170 | #endif |
---|
| 171 | |
---|
| 172 | // We were woken up, remove self from idle |
---|
| 173 | remove(this->cltr->idles, * this); |
---|
| 174 | |
---|
| 175 | // DON'T just proceed, start looking again |
---|
| 176 | continue MAIN_LOOP; |
---|
[64a7146] | 177 | } |
---|
[1eb239e4] | 178 | |
---|
| 179 | /* paranoid */ verify( readyThread ); |
---|
| 180 | |
---|
| 181 | // We found a thread run it |
---|
| 182 | __run_thread(this, readyThread); |
---|
| 183 | |
---|
| 184 | // Are we done? |
---|
| 185 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP; |
---|
[c81ebf9] | 186 | } |
---|
| 187 | |
---|
[4069faad] | 188 | __cfadbg_print_safe(runtime_core, "Kernel : core %p stopping\n", this); |
---|
[c84e80a] | 189 | } |
---|
[8118303] | 190 | |
---|
[4cedd9f] | 191 | V( this->terminated ); |
---|
[bdeba0b] | 192 | |
---|
[28d73c1] | 193 | if(this == mainProcessor) { |
---|
[6a490b2] | 194 | // HACK : the coroutine context switch expects this_thread to be set |
---|
| 195 | // and it make sense for it to be set in all other cases except here |
---|
| 196 | // fake it |
---|
| 197 | kernelTLS.this_thread = mainThread; |
---|
| 198 | } |
---|
[7768b8d] | 199 | |
---|
[4069faad] | 200 | __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this); |
---|
[c84e80a] | 201 | } |
---|
| 202 | |
---|
[5c1a531] | 203 | static int * __volatile_errno() __attribute__((noinline)); |
---|
| 204 | static int * __volatile_errno() { asm(""); return &errno; } |
---|
| 205 | |
---|
[14a61b5] | 206 | // KERNEL ONLY |
---|
[1c273d0] | 207 | // runThread runs a thread by context switching |
---|
| 208 | // from the processor coroutine to the target thread |
---|
[ac2b598] | 209 | static void __run_thread(processor * this, $thread * thrd_dst) { |
---|
[1eb239e4] | 210 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 211 | /* paranoid */ verifyf( thrd_dst->state == Ready || thrd_dst->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", thrd_dst->state, thrd_dst->preempted); |
---|
| 212 | /* paranoid */ verifyf( thrd_dst->link.next == 0p, "Expected null got %p", thrd_dst->link.next ); |
---|
| 213 | __builtin_prefetch( thrd_dst->context.SP ); |
---|
| 214 | |
---|
[ac2b598] | 215 | $coroutine * proc_cor = get_coroutine(this->runner); |
---|
[8fcbb4c] | 216 | |
---|
[14a61b5] | 217 | // Update global state |
---|
[e8e457e] | 218 | kernelTLS.this_thread = thrd_dst; |
---|
| 219 | |
---|
[9f575ea] | 220 | // set state of processor coroutine to inactive |
---|
| 221 | verify(proc_cor->state == Active); |
---|
[ae7be7a] | 222 | proc_cor->state = Blocked; |
---|
[e8e457e] | 223 | |
---|
[9f575ea] | 224 | // Actually run the thread |
---|
[3381ed7] | 225 | RUNNING: while(true) { |
---|
[ff79d5e] | 226 | thrd_dst->preempted = __NO_PREEMPTION; |
---|
| 227 | thrd_dst->state = Active; |
---|
[e8e457e] | 228 | |
---|
[ae66348] | 229 | __cfaabi_dbg_debug_do( |
---|
[276ae57e] | 230 | thrd_dst->park_stale = true; |
---|
| 231 | thrd_dst->unpark_stale = true; |
---|
[ae66348] | 232 | ) |
---|
[75f3522] | 233 | |
---|
[3381ed7] | 234 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[a7b486b] | 235 | /* paranoid */ verify( kernelTLS.this_thread == thrd_dst ); |
---|
[210b8b3] | 236 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); // add escape condition if we are setting up the processor |
---|
| 237 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); // add escape condition if we are setting up the processor |
---|
[3381ed7] | 238 | |
---|
[9f575ea] | 239 | // set context switch to the thread that the processor is executing |
---|
| 240 | verify( thrd_dst->context.SP ); |
---|
[c7a900a] | 241 | __cfactx_switch( &proc_cor->context, &thrd_dst->context ); |
---|
| 242 | // when __cfactx_switch returns we are back in the processor coroutine |
---|
[9f575ea] | 243 | |
---|
[210b8b3] | 244 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); |
---|
| 245 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); |
---|
[a7b486b] | 246 | /* paranoid */ verify( kernelTLS.this_thread == thrd_dst ); |
---|
[3381ed7] | 247 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[75f3522] | 248 | |
---|
[3381ed7] | 249 | |
---|
| 250 | // We just finished running a thread, there are a few things that could have happened. |
---|
| 251 | // 1 - Regular case : the thread has blocked and now one has scheduled it yet. |
---|
| 252 | // 2 - Racy case : the thread has blocked but someone has already tried to schedule it. |
---|
| 253 | // 4 - Preempted |
---|
| 254 | // In case 1, we may have won a race so we can't write to the state again. |
---|
| 255 | // In case 2, we lost the race so we now own the thread. |
---|
| 256 | |
---|
| 257 | if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) { |
---|
| 258 | // The thread was preempted, reschedule it and reset the flag |
---|
[9b1dcc2] | 259 | __schedule_thread( (__processor_id_t*)this, thrd_dst ); |
---|
[3381ed7] | 260 | break RUNNING; |
---|
| 261 | } |
---|
[75f3522] | 262 | |
---|
[ff79d5e] | 263 | if(unlikely(thrd_dst->state == Halted)) { |
---|
| 264 | // The thread has halted, it should never be scheduled/run again |
---|
| 265 | // We may need to wake someone up here since |
---|
| 266 | unpark( this->destroyer __cfaabi_dbg_ctx2 ); |
---|
| 267 | this->destroyer = 0p; |
---|
| 268 | break RUNNING; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | /* paranoid */ verify( thrd_dst->state == Active ); |
---|
| 272 | thrd_dst->state = Blocked; |
---|
| 273 | |
---|
[3381ed7] | 274 | // set state of processor coroutine to active and the thread to inactive |
---|
[ff79d5e] | 275 | int old_ticket = __atomic_fetch_sub(&thrd_dst->ticket, 1, __ATOMIC_SEQ_CST); |
---|
| 276 | __cfaabi_dbg_debug_do( thrd_dst->park_result = old_ticket; ) |
---|
| 277 | switch(old_ticket) { |
---|
| 278 | case 1: |
---|
[3381ed7] | 279 | // This is case 1, the regular case, nothing more is needed |
---|
| 280 | break RUNNING; |
---|
[ff79d5e] | 281 | case 2: |
---|
[3381ed7] | 282 | // This is case 2, the racy case, someone tried to run this thread before it finished blocking |
---|
| 283 | // In this case, just run it again. |
---|
| 284 | continue RUNNING; |
---|
| 285 | default: |
---|
| 286 | // This makes no sense, something is wrong abort |
---|
[ff79d5e] | 287 | abort(); |
---|
[3381ed7] | 288 | } |
---|
[9f575ea] | 289 | } |
---|
[e8e457e] | 290 | |
---|
[9f575ea] | 291 | // Just before returning to the processor, set the processor coroutine to active |
---|
[e8e457e] | 292 | proc_cor->state = Active; |
---|
[ae7be7a] | 293 | kernelTLS.this_thread = 0p; |
---|
[1eb239e4] | 294 | |
---|
| 295 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[82c948c] | 296 | } |
---|
| 297 | |
---|
[14a61b5] | 298 | // KERNEL_ONLY |
---|
[b0c7419] | 299 | void returnToKernel() { |
---|
[3381ed7] | 300 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[ac2b598] | 301 | $coroutine * proc_cor = get_coroutine(kernelTLS.this_processor->runner); |
---|
| 302 | $thread * thrd_src = kernelTLS.this_thread; |
---|
[e8e457e] | 303 | |
---|
[29cb302] | 304 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 305 | struct processor * last_proc = kernelTLS.this_processor; |
---|
| 306 | #endif |
---|
| 307 | |
---|
[9f575ea] | 308 | // Run the thread on this processor |
---|
| 309 | { |
---|
| 310 | int local_errno = *__volatile_errno(); |
---|
| 311 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
| 312 | __x87_store; |
---|
| 313 | #endif |
---|
| 314 | verify( proc_cor->context.SP ); |
---|
[c7a900a] | 315 | __cfactx_switch( &thrd_src->context, &proc_cor->context ); |
---|
[9f575ea] | 316 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
| 317 | __x87_load; |
---|
| 318 | #endif |
---|
| 319 | *__volatile_errno() = local_errno; |
---|
[8fcbb4c] | 320 | } |
---|
[deca0f5] | 321 | |
---|
[29cb302] | 322 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 323 | if(last_proc != kernelTLS.this_processor) { |
---|
| 324 | __tls_stats()->ready.threads.migration++; |
---|
| 325 | } |
---|
| 326 | #endif |
---|
| 327 | |
---|
[3381ed7] | 328 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[210b8b3] | 329 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) < ((uintptr_t)__get_stack(thrd_src->curr_cor)->base ), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too small.\n", thrd_src ); |
---|
| 330 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) > ((uintptr_t)__get_stack(thrd_src->curr_cor)->limit), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too large.\n", thrd_src ); |
---|
[c84e80a] | 331 | } |
---|
| 332 | |
---|
[8def349] | 333 | //----------------------------------------------------------------------------- |
---|
| 334 | // Scheduler routines |
---|
[14a61b5] | 335 | // KERNEL ONLY |
---|
[9b1dcc2] | 336 | void __schedule_thread( struct __processor_id_t * id, $thread * thrd ) { |
---|
[6a490b2] | 337 | /* paranoid */ verify( thrd ); |
---|
| 338 | /* paranoid */ verify( thrd->state != Halted ); |
---|
[9f575ea] | 339 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[3381ed7] | 340 | /* paranoid */ #if defined( __CFA_WITH_VERIFY__ ) |
---|
[504a7dc] | 341 | /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION, |
---|
| 342 | "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
[ff79d5e] | 343 | /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active, |
---|
[504a7dc] | 344 | "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
[3381ed7] | 345 | /* paranoid */ #endif |
---|
[6a490b2] | 346 | /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next ); |
---|
[9f575ea] | 347 | |
---|
[ae7be7a] | 348 | if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready; |
---|
[6b4cdd3] | 349 | |
---|
[9b1dcc2] | 350 | ready_schedule_lock ( id ); |
---|
[504a7dc] | 351 | push( thrd->curr_cluster, thrd ); |
---|
[1eb239e4] | 352 | __wake_one(id, thrd->curr_cluster); |
---|
[9b1dcc2] | 353 | ready_schedule_unlock( id ); |
---|
[1c273d0] | 354 | |
---|
[9f575ea] | 355 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[db6f06a] | 356 | } |
---|
| 357 | |
---|
[14a61b5] | 358 | // KERNEL ONLY |
---|
[1eb239e4] | 359 | static inline $thread * __next_thread(cluster * this) with( *this ) { |
---|
[3381ed7] | 360 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[7768b8d] | 361 | |
---|
[9b1dcc2] | 362 | ready_schedule_lock ( (__processor_id_t*)kernelTLS.this_processor ); |
---|
[1eb239e4] | 363 | $thread * thrd = pop( this ); |
---|
[9b1dcc2] | 364 | ready_schedule_unlock( (__processor_id_t*)kernelTLS.this_processor ); |
---|
[7768b8d] | 365 | |
---|
[3381ed7] | 366 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[1eb239e4] | 367 | return thrd; |
---|
[eb2e723] | 368 | } |
---|
| 369 | |
---|
[64a7146] | 370 | // KERNEL ONLY |
---|
[1eb239e4] | 371 | static inline $thread * __next_thread_slow(cluster * this) with( *this ) { |
---|
[64a7146] | 372 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 373 | |
---|
| 374 | ready_schedule_lock ( (__processor_id_t*)kernelTLS.this_processor ); |
---|
[1eb239e4] | 375 | $thread * thrd = pop_slow( this ); |
---|
[64a7146] | 376 | ready_schedule_unlock( (__processor_id_t*)kernelTLS.this_processor ); |
---|
| 377 | |
---|
| 378 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[1eb239e4] | 379 | return thrd; |
---|
[64a7146] | 380 | } |
---|
| 381 | |
---|
[2d8f7b0] | 382 | // KERNEL ONLY unpark with out disabling interrupts |
---|
[9b1dcc2] | 383 | void __unpark( struct __processor_id_t * id, $thread * thrd __cfaabi_dbg_ctx_param2 ) { |
---|
[ae66348] | 384 | // record activity |
---|
| 385 | __cfaabi_dbg_record_thrd( *thrd, false, caller ); |
---|
| 386 | |
---|
[ff79d5e] | 387 | int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST); |
---|
| 388 | __cfaabi_dbg_debug_do( thrd->unpark_result = old_ticket; thrd->unpark_state = thrd->state; ) |
---|
| 389 | switch(old_ticket) { |
---|
| 390 | case 1: |
---|
[3381ed7] | 391 | // Wake won the race, the thread will reschedule/rerun itself |
---|
| 392 | break; |
---|
[ff79d5e] | 393 | case 0: |
---|
[3381ed7] | 394 | /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION ); |
---|
[ff79d5e] | 395 | /* paranoid */ verify( thrd->state == Blocked ); |
---|
[0b33412] | 396 | |
---|
[3381ed7] | 397 | // Wake lost the race, |
---|
[9b1dcc2] | 398 | __schedule_thread( id, thrd ); |
---|
[3381ed7] | 399 | break; |
---|
| 400 | default: |
---|
| 401 | // This makes no sense, something is wrong abort |
---|
| 402 | abort(); |
---|
[de6319f] | 403 | } |
---|
[db6f06a] | 404 | } |
---|
| 405 | |
---|
[2d8f7b0] | 406 | void unpark( $thread * thrd __cfaabi_dbg_ctx_param2 ) { |
---|
| 407 | if( !thrd ) return; |
---|
[db6f06a] | 408 | |
---|
[82ff5845] | 409 | disable_interrupts(); |
---|
[9b1dcc2] | 410 | __unpark( (__processor_id_t*)kernelTLS.this_processor, thrd __cfaabi_dbg_ctx_fwd2 ); |
---|
[36982fc] | 411 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[eb2e723] | 412 | } |
---|
| 413 | |
---|
[ae66348] | 414 | void park( __cfaabi_dbg_ctx_param ) { |
---|
[3381ed7] | 415 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
[82ff5845] | 416 | disable_interrupts(); |
---|
[3381ed7] | 417 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 418 | /* paranoid */ verify( kernelTLS.this_thread->preempted == __NO_PREEMPTION ); |
---|
[0b33412] | 419 | |
---|
[ae66348] | 420 | // record activity |
---|
| 421 | __cfaabi_dbg_record_thrd( *kernelTLS.this_thread, true, caller ); |
---|
[0b33412] | 422 | |
---|
[82c948c] | 423 | returnToKernel(); |
---|
[0b33412] | 424 | |
---|
[3381ed7] | 425 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[36982fc] | 426 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[3381ed7] | 427 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
[0c78741] | 428 | |
---|
| 429 | } |
---|
[09800e9] | 430 | |
---|
[3381ed7] | 431 | // KERNEL ONLY |
---|
[b0c7419] | 432 | void __leave_thread() { |
---|
[3381ed7] | 433 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[09800e9] | 434 | returnToKernel(); |
---|
[b0c7419] | 435 | abort(); |
---|
[09800e9] | 436 | } |
---|
| 437 | |
---|
[14a61b5] | 438 | // KERNEL ONLY |
---|
[3381ed7] | 439 | bool force_yield( __Preemption_Reason reason ) { |
---|
| 440 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
[09800e9] | 441 | disable_interrupts(); |
---|
[3381ed7] | 442 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[09800e9] | 443 | |
---|
[ac2b598] | 444 | $thread * thrd = kernelTLS.this_thread; |
---|
[ff79d5e] | 445 | /* paranoid */ verify(thrd->state == Active); |
---|
[3381ed7] | 446 | |
---|
| 447 | // SKULLDUGGERY: It is possible that we are preempting this thread just before |
---|
| 448 | // it was going to park itself. If that is the case and it is already using the |
---|
| 449 | // intrusive fields then we can't use them to preempt the thread |
---|
| 450 | // If that is the case, abandon the preemption. |
---|
| 451 | bool preempted = false; |
---|
[504a7dc] | 452 | if(thrd->link.next == 0p) { |
---|
[3381ed7] | 453 | preempted = true; |
---|
| 454 | thrd->preempted = reason; |
---|
| 455 | returnToKernel(); |
---|
[de6319f] | 456 | } |
---|
[f2b12406] | 457 | |
---|
[3381ed7] | 458 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 459 | enable_interrupts_noPoll(); |
---|
| 460 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
[f2b12406] | 461 | |
---|
[3381ed7] | 462 | return preempted; |
---|
[f2b12406] | 463 | } |
---|
| 464 | |
---|
[14a61b5] | 465 | //============================================================================================= |
---|
[92e7631] | 466 | // Kernel Idle Sleep |
---|
[14a61b5] | 467 | //============================================================================================= |
---|
[64a7146] | 468 | // Wake a thread from the front if there are any |
---|
[1eb239e4] | 469 | static void __wake_one(struct __processor_id_t * id, cluster * this) { |
---|
| 470 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[64a7146] | 471 | /* paranoid */ verify( ready_schedule_islocked( id ) ); |
---|
[14a61b5] | 472 | |
---|
[64a7146] | 473 | // Check if there is a sleeping processor |
---|
[1eb239e4] | 474 | processor * p; |
---|
| 475 | unsigned idle; |
---|
| 476 | unsigned total; |
---|
| 477 | [idle, total, p] = query(this->idles); |
---|
[14a61b5] | 478 | |
---|
[64a7146] | 479 | // If no one is sleeping, we are done |
---|
[1eb239e4] | 480 | if( idle == 0 ) return; |
---|
[14a61b5] | 481 | |
---|
[64a7146] | 482 | // We found a processor, wake it up |
---|
| 483 | post( p->idle ); |
---|
[14a61b5] | 484 | |
---|
[1eb239e4] | 485 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 486 | __tls_stats()->ready.sleep.wakes++; |
---|
| 487 | #endif |
---|
| 488 | |
---|
| 489 | /* paranoid */ verify( ready_schedule_islocked( id ) ); |
---|
| 490 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 491 | |
---|
| 492 | return; |
---|
[64a7146] | 493 | } |
---|
[14a61b5] | 494 | |
---|
[64a7146] | 495 | // Unconditionnaly wake a thread |
---|
[1eb239e4] | 496 | void __wake_proc(processor * this) { |
---|
[64a7146] | 497 | __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this); |
---|
[14a61b5] | 498 | |
---|
[64a7146] | 499 | disable_interrupts(); |
---|
| 500 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 501 | bool ret = post( this->idle ); |
---|
| 502 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[92e7631] | 503 | } |
---|
| 504 | |
---|
[1eb239e4] | 505 | static void push (__cluster_idles & this, processor & proc) { |
---|
| 506 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 507 | lock( this ); |
---|
| 508 | this.idle++; |
---|
| 509 | /* paranoid */ verify( this.idle <= this.total ); |
---|
[8e16177] | 510 | |
---|
[1eb239e4] | 511 | insert_first(this.list, proc); |
---|
| 512 | unlock( this ); |
---|
| 513 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 514 | } |
---|
[8e16177] | 515 | |
---|
[1eb239e4] | 516 | static void remove(__cluster_idles & this, processor & proc) { |
---|
| 517 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 518 | lock( this ); |
---|
| 519 | this.idle--; |
---|
| 520 | /* paranoid */ verify( this.idle >= 0 ); |
---|
[c34ebf2] | 521 | |
---|
[1eb239e4] | 522 | remove(proc); |
---|
| 523 | unlock( this ); |
---|
| 524 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 525 | } |
---|
[c34ebf2] | 526 | |
---|
[1eb239e4] | 527 | static [unsigned idle, unsigned total, * processor] query( & __cluster_idles this ) { |
---|
| 528 | for() { |
---|
| 529 | uint64_t l = __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST); |
---|
| 530 | if( 1 == (l % 2) ) { Pause(); continue; } |
---|
| 531 | unsigned idle = this.idle; |
---|
| 532 | unsigned total = this.total; |
---|
| 533 | processor * proc = &this.list`first; |
---|
[dab09ad] | 534 | // Thread fence is unnecessary, but gcc-8 and older incorrectly reorder code without it |
---|
| 535 | __atomic_thread_fence(__ATOMIC_SEQ_CST); |
---|
[1eb239e4] | 536 | if(l != __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST)) { Pause(); continue; } |
---|
| 537 | return [idle, total, proc]; |
---|
| 538 | } |
---|
[6b4cdd3] | 539 | } |
---|
| 540 | |
---|
[dbe9b08] | 541 | //============================================================================================= |
---|
| 542 | // Unexpected Terminating logic |
---|
| 543 | //============================================================================================= |
---|
[ea7d2b0] | 544 | static __spinlock_t kernel_abort_lock; |
---|
[9d944b2] | 545 | static bool kernel_abort_called = false; |
---|
| 546 | |
---|
[afd550c] | 547 | void * kernel_abort(void) __attribute__ ((__nothrow__)) { |
---|
[9d944b2] | 548 | // abort cannot be recursively entered by the same or different processors because all signal handlers return when |
---|
| 549 | // the globalAbort flag is true. |
---|
[36982fc] | 550 | lock( kernel_abort_lock __cfaabi_dbg_ctx2 ); |
---|
[9d944b2] | 551 | |
---|
| 552 | // first task to abort ? |
---|
[de94a60] | 553 | if ( kernel_abort_called ) { // not first task to abort ? |
---|
[ea7d2b0] | 554 | unlock( kernel_abort_lock ); |
---|
[1c273d0] | 555 | |
---|
[9d944b2] | 556 | sigset_t mask; |
---|
| 557 | sigemptyset( &mask ); |
---|
[de94a60] | 558 | sigaddset( &mask, SIGALRM ); // block SIGALRM signals |
---|
[8a13c47] | 559 | sigaddset( &mask, SIGUSR1 ); // block SIGALRM signals |
---|
| 560 | sigsuspend( &mask ); // block the processor to prevent further damage during abort |
---|
| 561 | _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it |
---|
[de94a60] | 562 | } |
---|
| 563 | else { |
---|
| 564 | kernel_abort_called = true; |
---|
| 565 | unlock( kernel_abort_lock ); |
---|
[9d944b2] | 566 | } |
---|
| 567 | |
---|
[14a61b5] | 568 | return kernelTLS.this_thread; |
---|
[9d944b2] | 569 | } |
---|
| 570 | |
---|
| 571 | void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) { |
---|
[b81fd95] | 572 | $thread * thrd = ( $thread * ) kernel_data; |
---|
[9d944b2] | 573 | |
---|
[de94a60] | 574 | if(thrd) { |
---|
| 575 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd ); |
---|
[1c40091] | 576 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
[de94a60] | 577 | |
---|
[212c2187] | 578 | if ( &thrd->self_cor != thrd->curr_cor ) { |
---|
| 579 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor ); |
---|
[1c40091] | 580 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
[de94a60] | 581 | } |
---|
| 582 | else { |
---|
[1c40091] | 583 | __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 ); |
---|
[de94a60] | 584 | } |
---|
[1c273d0] | 585 | } |
---|
[9d944b2] | 586 | else { |
---|
[de94a60] | 587 | int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" ); |
---|
[1c40091] | 588 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
[9d944b2] | 589 | } |
---|
| 590 | } |
---|
| 591 | |
---|
[2b8bc41] | 592 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) { |
---|
[14a61b5] | 593 | return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2; |
---|
[2b8bc41] | 594 | } |
---|
| 595 | |
---|
[de94a60] | 596 | static __spinlock_t kernel_debug_lock; |
---|
| 597 | |
---|
[9d944b2] | 598 | extern "C" { |
---|
[1c40091] | 599 | void __cfaabi_bits_acquire() { |
---|
[36982fc] | 600 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 ); |
---|
[9d944b2] | 601 | } |
---|
| 602 | |
---|
[1c40091] | 603 | void __cfaabi_bits_release() { |
---|
[ea7d2b0] | 604 | unlock( kernel_debug_lock ); |
---|
[9d944b2] | 605 | } |
---|
[8118303] | 606 | } |
---|
| 607 | |
---|
[fa21ac9] | 608 | //============================================================================================= |
---|
| 609 | // Kernel Utilities |
---|
| 610 | //============================================================================================= |
---|
[bd98b58] | 611 | //----------------------------------------------------------------------------- |
---|
| 612 | // Locks |
---|
[242a902] | 613 | void ?{}( semaphore & this, int count = 1 ) { |
---|
| 614 | (this.lock){}; |
---|
| 615 | this.count = count; |
---|
| 616 | (this.waiting){}; |
---|
[db6f06a] | 617 | } |
---|
[242a902] | 618 | void ^?{}(semaphore & this) {} |
---|
[db6f06a] | 619 | |
---|
[71c8b7e] | 620 | bool P(semaphore & this) with( this ){ |
---|
[65deb18] | 621 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 622 | count -= 1; |
---|
| 623 | if ( count < 0 ) { |
---|
[bdeba0b] | 624 | // queue current task |
---|
[14a61b5] | 625 | append( waiting, kernelTLS.this_thread ); |
---|
[bdeba0b] | 626 | |
---|
| 627 | // atomically release spin lock and block |
---|
[3381ed7] | 628 | unlock( lock ); |
---|
[ae66348] | 629 | park( __cfaabi_dbg_ctx ); |
---|
[71c8b7e] | 630 | return true; |
---|
[8def349] | 631 | } |
---|
[4e6fb8e] | 632 | else { |
---|
[65deb18] | 633 | unlock( lock ); |
---|
[71c8b7e] | 634 | return false; |
---|
[4e6fb8e] | 635 | } |
---|
[bd98b58] | 636 | } |
---|
| 637 | |
---|
[f0ce5f4] | 638 | bool V(semaphore & this) with( this ) { |
---|
[ac2b598] | 639 | $thread * thrd = 0p; |
---|
[65deb18] | 640 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 641 | count += 1; |
---|
| 642 | if ( count <= 0 ) { |
---|
[bdeba0b] | 643 | // remove task at head of waiting list |
---|
[65deb18] | 644 | thrd = pop_head( waiting ); |
---|
[bd98b58] | 645 | } |
---|
[bdeba0b] | 646 | |
---|
[65deb18] | 647 | unlock( lock ); |
---|
[bdeba0b] | 648 | |
---|
| 649 | // make new owner |
---|
[ae66348] | 650 | unpark( thrd __cfaabi_dbg_ctx2 ); |
---|
[f0ce5f4] | 651 | |
---|
[d384787] | 652 | return thrd != 0p; |
---|
| 653 | } |
---|
| 654 | |
---|
| 655 | bool V(semaphore & this, unsigned diff) with( this ) { |
---|
| 656 | $thread * thrd = 0p; |
---|
| 657 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 658 | int release = max(-count, (int)diff); |
---|
| 659 | count += diff; |
---|
| 660 | for(release) { |
---|
| 661 | unpark( pop_head( waiting ) __cfaabi_dbg_ctx2 ); |
---|
| 662 | } |
---|
| 663 | |
---|
| 664 | unlock( lock ); |
---|
| 665 | |
---|
[f0ce5f4] | 666 | return thrd != 0p; |
---|
[bd98b58] | 667 | } |
---|
| 668 | |
---|
[de94a60] | 669 | //----------------------------------------------------------------------------- |
---|
| 670 | // Debug |
---|
| 671 | __cfaabi_dbg_debug_do( |
---|
[1997b4e] | 672 | extern "C" { |
---|
[ae66348] | 673 | void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) { |
---|
[1997b4e] | 674 | this.prev_name = prev_name; |
---|
| 675 | this.prev_thrd = kernelTLS.this_thread; |
---|
| 676 | } |
---|
[ae66348] | 677 | |
---|
| 678 | void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]) { |
---|
| 679 | if(park) { |
---|
[f0ce5f4] | 680 | this.park_caller = prev_name; |
---|
| 681 | this.park_stale = false; |
---|
[ae66348] | 682 | } |
---|
| 683 | else { |
---|
[f0ce5f4] | 684 | this.unpark_caller = prev_name; |
---|
| 685 | this.unpark_stale = false; |
---|
[ae66348] | 686 | } |
---|
| 687 | } |
---|
[9181f1d] | 688 | } |
---|
[f7d6bb0] | 689 | ) |
---|
[2026bb6] | 690 | |
---|
| 691 | //----------------------------------------------------------------------------- |
---|
| 692 | // Debug |
---|
[8c50aed] | 693 | bool threading_enabled(void) __attribute__((const)) { |
---|
[2026bb6] | 694 | return true; |
---|
| 695 | } |
---|
[c34ebf2] | 696 | |
---|
| 697 | //----------------------------------------------------------------------------- |
---|
| 698 | // Statistics |
---|
| 699 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 700 | void print_halts( processor & this ) { |
---|
| 701 | this.print_halts = true; |
---|
| 702 | } |
---|
| 703 | #endif |
---|
[8118303] | 704 | // Local Variables: // |
---|
| 705 | // mode: c // |
---|
| 706 | // tab-width: 4 // |
---|
| 707 | // End: // |
---|