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