[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 |
---|
[0190480] | 12 | // Last Modified On : Mon Aug 31 07:08:20 2020 |
---|
| 13 | // Update Count : 71 |
---|
[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> |
---|
[dddb3dd0] | 24 | extern "C" { |
---|
| 25 | #include <sys/eventfd.h> |
---|
| 26 | } |
---|
[8118303] | 27 | |
---|
| 28 | //CFA Includes |
---|
[73abe95] | 29 | #include "kernel_private.hfa" |
---|
| 30 | #include "preemption.hfa" |
---|
[8118303] | 31 | |
---|
| 32 | //Private includes |
---|
| 33 | #define __CFA_INVOKE_PRIVATE__ |
---|
| 34 | #include "invoke.h" |
---|
| 35 | |
---|
[89eff25] | 36 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 37 | #define __STATS( ...) __VA_ARGS__ |
---|
| 38 | #else |
---|
| 39 | #define __STATS( ...) |
---|
| 40 | #endif |
---|
[4069faad] | 41 | |
---|
[deca0f5] | 42 | //----------------------------------------------------------------------------- |
---|
| 43 | // Some assembly required |
---|
[1805b1b] | 44 | #if defined( __i386 ) |
---|
[deca0f5] | 45 | // mxcr : SSE Status and Control bits (control bits are preserved across function calls) |
---|
| 46 | // fcw : X87 FPU control word (preserved across function calls) |
---|
| 47 | #define __x87_store \ |
---|
| 48 | uint32_t __mxcr; \ |
---|
| 49 | uint16_t __fcw; \ |
---|
| 50 | __asm__ volatile ( \ |
---|
| 51 | "stmxcsr %0\n" \ |
---|
| 52 | "fnstcw %1\n" \ |
---|
| 53 | : "=m" (__mxcr),\ |
---|
| 54 | "=m" (__fcw) \ |
---|
| 55 | ) |
---|
| 56 | |
---|
| 57 | #define __x87_load \ |
---|
| 58 | __asm__ volatile ( \ |
---|
| 59 | "fldcw %1\n" \ |
---|
| 60 | "ldmxcsr %0\n" \ |
---|
| 61 | ::"m" (__mxcr),\ |
---|
| 62 | "m" (__fcw) \ |
---|
| 63 | ) |
---|
| 64 | |
---|
| 65 | #elif defined( __x86_64 ) |
---|
| 66 | #define __x87_store \ |
---|
| 67 | uint32_t __mxcr; \ |
---|
| 68 | uint16_t __fcw; \ |
---|
| 69 | __asm__ volatile ( \ |
---|
| 70 | "stmxcsr %0\n" \ |
---|
| 71 | "fnstcw %1\n" \ |
---|
| 72 | : "=m" (__mxcr),\ |
---|
| 73 | "=m" (__fcw) \ |
---|
| 74 | ) |
---|
| 75 | |
---|
| 76 | #define __x87_load \ |
---|
| 77 | __asm__ volatile ( \ |
---|
| 78 | "fldcw %1\n" \ |
---|
| 79 | "ldmxcsr %0\n" \ |
---|
| 80 | :: "m" (__mxcr),\ |
---|
| 81 | "m" (__fcw) \ |
---|
| 82 | ) |
---|
| 83 | |
---|
[0190480] | 84 | #elif defined( __arm__ ) |
---|
| 85 | #define __x87_store |
---|
| 86 | #define __x87_load |
---|
| 87 | |
---|
| 88 | #elif defined( __aarch64__ ) |
---|
[74f5c83] | 89 | #define __x87_store \ |
---|
| 90 | uint32_t __fpcntl[2]; \ |
---|
| 91 | __asm__ volatile ( \ |
---|
| 92 | "mrs x9, FPCR\n" \ |
---|
| 93 | "mrs x10, FPSR\n" \ |
---|
| 94 | "stp x9, x10, %0\n" \ |
---|
| 95 | : "=m" (__fpcntl) : : "x9", "x10" \ |
---|
| 96 | ) |
---|
| 97 | |
---|
| 98 | #define __x87_load \ |
---|
| 99 | __asm__ volatile ( \ |
---|
| 100 | "ldp x9, x10, %0\n" \ |
---|
| 101 | "msr FPSR, x10\n" \ |
---|
| 102 | "msr FPCR, x9\n" \ |
---|
| 103 | : "=m" (__fpcntl) : : "x9", "x10" \ |
---|
| 104 | ) |
---|
| 105 | |
---|
[deca0f5] | 106 | #else |
---|
[0190480] | 107 | #error unsupported hardware architecture |
---|
[deca0f5] | 108 | #endif |
---|
| 109 | |
---|
[e660761] | 110 | extern $thread * mainThread; |
---|
| 111 | extern processor * mainProcessor; |
---|
[2ac095d] | 112 | |
---|
[92e7631] | 113 | //----------------------------------------------------------------------------- |
---|
| 114 | // Kernel Scheduling logic |
---|
| 115 | static $thread * __next_thread(cluster * this); |
---|
[1eb239e4] | 116 | static $thread * __next_thread_slow(cluster * this); |
---|
[c6c7e6c] | 117 | static inline bool __must_unpark( $thread * thrd ) __attribute((nonnull(1))); |
---|
[92e7631] | 118 | static void __run_thread(processor * this, $thread * dst); |
---|
[e873838] | 119 | static void __wake_one(cluster * cltr); |
---|
[1eb239e4] | 120 | |
---|
[6a9b12b] | 121 | static void mark_idle (__cluster_proc_list & idles, processor & proc); |
---|
| 122 | static void mark_awake(__cluster_proc_list & idles, processor & proc); |
---|
| 123 | static [unsigned idle, unsigned total, * processor] query_idles( & __cluster_proc_list idles ); |
---|
[1eb239e4] | 124 | |
---|
[dddb3dd0] | 125 | extern void __cfa_io_start( processor * ); |
---|
[c1c95b1] | 126 | extern bool __cfa_io_drain( processor * ); |
---|
[dddb3dd0] | 127 | extern void __cfa_io_flush( processor * ); |
---|
| 128 | extern void __cfa_io_stop ( processor * ); |
---|
[c1c95b1] | 129 | static inline bool __maybe_io_drain( processor * ); |
---|
[dddb3dd0] | 130 | |
---|
| 131 | extern void __disable_interrupts_hard(); |
---|
| 132 | extern void __enable_interrupts_hard(); |
---|
[c84e80a] | 133 | |
---|
[a3821fa] | 134 | static inline void __disable_interrupts_checked() { |
---|
| 135 | /* paranoid */ verify( __preemption_enabled() ); |
---|
| 136 | disable_interrupts(); |
---|
| 137 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | static inline void __enable_interrupts_checked( bool poll = true ) { |
---|
| 141 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 142 | enable_interrupts( poll ); |
---|
| 143 | /* paranoid */ verify( __preemption_enabled() ); |
---|
| 144 | } |
---|
| 145 | |
---|
[75f3522] | 146 | //============================================================================================= |
---|
| 147 | // Kernel Scheduling logic |
---|
| 148 | //============================================================================================= |
---|
[8fcbb4c] | 149 | //Main of the processor contexts |
---|
[83a071f9] | 150 | void main(processorCtx_t & runner) { |
---|
[21184e3] | 151 | // Because of a bug, we couldn't initialized the seed on construction |
---|
| 152 | // Do it here |
---|
[8fc652e0] | 153 | __cfaabi_tls.rand_seed ^= rdtscl(); |
---|
| 154 | __cfaabi_tls.ready_rng.fwd_seed = 25214903917_l64u * (rdtscl() ^ (uintptr_t)&runner); |
---|
[f2384c9a] | 155 | __tls_rand_advance_bck(); |
---|
[21184e3] | 156 | |
---|
[83a071f9] | 157 | processor * this = runner.proc; |
---|
[094476d] | 158 | verify(this); |
---|
[c81ebf9] | 159 | |
---|
[dddb3dd0] | 160 | __cfa_io_start( this ); |
---|
| 161 | |
---|
[4069faad] | 162 | __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this); |
---|
[28d73c1] | 163 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 164 | if( this->print_halts ) { |
---|
[c993b15] | 165 | __cfaabi_bits_print_safe( STDOUT_FILENO, "Processor : %d - %s (%p)\n", this->unique_id, this->name, (void*)this); |
---|
[28d73c1] | 166 | } |
---|
| 167 | #endif |
---|
[b798713] | 168 | |
---|
[75f3522] | 169 | { |
---|
[c81ebf9] | 170 | // Setup preemption data |
---|
| 171 | preemption_scope scope = { this }; |
---|
| 172 | |
---|
[89eff25] | 173 | __STATS( unsigned long long last_tally = rdtscl(); ) |
---|
[325e6ea] | 174 | |
---|
[a5e7233] | 175 | // if we need to run some special setup, now is the time to do it. |
---|
| 176 | if(this->init.thrd) { |
---|
| 177 | this->init.thrd->curr_cluster = this->cltr; |
---|
| 178 | __run_thread(this, this->init.thrd); |
---|
| 179 | } |
---|
[325e6ea] | 180 | |
---|
[4069faad] | 181 | __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this); |
---|
[8118303] | 182 | |
---|
[ac2b598] | 183 | $thread * readyThread = 0p; |
---|
[1eb239e4] | 184 | MAIN_LOOP: |
---|
| 185 | for() { |
---|
[34b2796] | 186 | #if 1 |
---|
[dddb3dd0] | 187 | // Check if there is pending io |
---|
| 188 | __maybe_io_drain( this ); |
---|
| 189 | |
---|
[92e7631] | 190 | // Try to get the next thread |
---|
[8c50aed] | 191 | readyThread = __next_thread( this->cltr ); |
---|
[75f3522] | 192 | |
---|
[1eb239e4] | 193 | if( !readyThread ) { |
---|
[dddb3dd0] | 194 | __cfa_io_flush( this ); |
---|
[1eb239e4] | 195 | readyThread = __next_thread_slow( this->cltr ); |
---|
| 196 | } |
---|
[4e6fb8e] | 197 | |
---|
[1eb239e4] | 198 | HALT: |
---|
| 199 | if( !readyThread ) { |
---|
| 200 | // Don't block if we are done |
---|
| 201 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP; |
---|
[c81ebf9] | 202 | |
---|
[1eb239e4] | 203 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 204 | __tls_stats()->ready.sleep.halts++; |
---|
| 205 | #endif |
---|
[398e8e9] | 206 | |
---|
[1eb239e4] | 207 | // Push self to idle stack |
---|
[6a9b12b] | 208 | mark_idle(this->cltr->procs, * this); |
---|
[398e8e9] | 209 | |
---|
[1eb239e4] | 210 | // Confirm the ready-queue is empty |
---|
| 211 | readyThread = __next_thread_slow( this->cltr ); |
---|
| 212 | if( readyThread ) { |
---|
| 213 | // A thread was found, cancel the halt |
---|
[6a9b12b] | 214 | mark_awake(this->cltr->procs, * this); |
---|
[1eb239e4] | 215 | |
---|
| 216 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 217 | __tls_stats()->ready.sleep.cancels++; |
---|
| 218 | #endif |
---|
| 219 | |
---|
| 220 | // continue the mai loop |
---|
| 221 | break HALT; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 225 | if(this->print_halts) { |
---|
[c993b15] | 226 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->unique_id, rdtscl()); |
---|
[1eb239e4] | 227 | } |
---|
| 228 | #endif |
---|
| 229 | |
---|
[dddb3dd0] | 230 | __cfadbg_print_safe(runtime_core, "Kernel : core %p waiting on eventfd %d\n", this, this->idle); |
---|
| 231 | |
---|
| 232 | __disable_interrupts_hard(); |
---|
| 233 | eventfd_t val; |
---|
| 234 | eventfd_read( this->idle, &val ); |
---|
| 235 | __enable_interrupts_hard(); |
---|
[1eb239e4] | 236 | |
---|
| 237 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 238 | if(this->print_halts) { |
---|
[c993b15] | 239 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->unique_id, rdtscl()); |
---|
[1eb239e4] | 240 | } |
---|
| 241 | #endif |
---|
| 242 | |
---|
| 243 | // We were woken up, remove self from idle |
---|
[6a9b12b] | 244 | mark_awake(this->cltr->procs, * this); |
---|
[1eb239e4] | 245 | |
---|
| 246 | // DON'T just proceed, start looking again |
---|
| 247 | continue MAIN_LOOP; |
---|
[64a7146] | 248 | } |
---|
[1eb239e4] | 249 | |
---|
| 250 | /* paranoid */ verify( readyThread ); |
---|
| 251 | |
---|
[dddb3dd0] | 252 | // Reset io dirty bit |
---|
| 253 | this->io.dirty = false; |
---|
| 254 | |
---|
[1eb239e4] | 255 | // We found a thread run it |
---|
| 256 | __run_thread(this, readyThread); |
---|
| 257 | |
---|
| 258 | // Are we done? |
---|
| 259 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP; |
---|
[325e6ea] | 260 | |
---|
| 261 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 262 | unsigned long long curr = rdtscl(); |
---|
| 263 | if(curr > (last_tally + 500000000)) { |
---|
| 264 | __tally_stats(this->cltr->stats, __cfaabi_tls.this_stats); |
---|
| 265 | last_tally = curr; |
---|
| 266 | } |
---|
| 267 | #endif |
---|
[dddb3dd0] | 268 | |
---|
| 269 | if(this->io.pending && !this->io.dirty) { |
---|
| 270 | __cfa_io_flush( this ); |
---|
| 271 | } |
---|
[89eff25] | 272 | |
---|
[34b2796] | 273 | #else |
---|
| 274 | |
---|
| 275 | SEARCH: { |
---|
| 276 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 277 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
| 278 | |
---|
| 279 | // First, lock the scheduler since we are searching for a thread |
---|
| 280 | ready_schedule_lock(); |
---|
| 281 | |
---|
| 282 | // Try to get the next thread |
---|
| 283 | readyThread = pop_fast( this->cltr ); |
---|
| 284 | if(readyThread) { ready_schedule_unlock(); break SEARCH; } |
---|
| 285 | |
---|
| 286 | // If we can't find a thread, might as well flush any outstanding I/O |
---|
| 287 | if(this->io.pending) { __cfa_io_flush( this ); } |
---|
| 288 | |
---|
| 289 | // Spin a little on I/O, just in case |
---|
| 290 | for(25) { |
---|
| 291 | __maybe_io_drain( this ); |
---|
| 292 | readyThread = pop_fast( this->cltr ); |
---|
| 293 | if(readyThread) { ready_schedule_unlock(); break SEARCH; } |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | // no luck, try stealing a few times |
---|
| 297 | for(25) { |
---|
| 298 | if( __maybe_io_drain( this ) ) { |
---|
| 299 | readyThread = pop_fast( this->cltr ); |
---|
| 300 | } else { |
---|
| 301 | readyThread = pop_slow( this->cltr ); |
---|
| 302 | } |
---|
| 303 | if(readyThread) { ready_schedule_unlock(); break SEARCH; } |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | // still no luck, search for a thread |
---|
| 307 | readyThread = pop_search( this->cltr ); |
---|
| 308 | if(readyThread) { ready_schedule_unlock(); break SEARCH; } |
---|
| 309 | |
---|
| 310 | // Don't block if we are done |
---|
| 311 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP; |
---|
| 312 | |
---|
| 313 | __STATS( __tls_stats()->ready.sleep.halts++; ) |
---|
| 314 | |
---|
| 315 | // Push self to idle stack |
---|
| 316 | ready_schedule_unlock(); |
---|
| 317 | mark_idle(this->cltr->procs, * this); |
---|
| 318 | ready_schedule_lock(); |
---|
| 319 | |
---|
| 320 | // Confirm the ready-queue is empty |
---|
| 321 | __maybe_io_drain( this ); |
---|
| 322 | readyThread = pop_search( this->cltr ); |
---|
| 323 | ready_schedule_unlock(); |
---|
| 324 | |
---|
| 325 | if( readyThread ) { |
---|
| 326 | // A thread was found, cancel the halt |
---|
| 327 | mark_awake(this->cltr->procs, * this); |
---|
| 328 | |
---|
| 329 | __STATS( __tls_stats()->ready.sleep.cancels++; ) |
---|
| 330 | |
---|
| 331 | // continue the main loop |
---|
| 332 | break SEARCH; |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | __STATS( if(this->print_halts) __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->id, rdtscl()); ) |
---|
| 336 | __cfadbg_print_safe(runtime_core, "Kernel : core %p waiting on eventfd %d\n", this, this->idle); |
---|
| 337 | |
---|
| 338 | // __disable_interrupts_hard(); |
---|
| 339 | eventfd_t val; |
---|
| 340 | eventfd_read( this->idle, &val ); |
---|
| 341 | // __enable_interrupts_hard(); |
---|
| 342 | |
---|
| 343 | __STATS( if(this->print_halts) __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->id, rdtscl()); ) |
---|
| 344 | |
---|
| 345 | // We were woken up, remove self from idle |
---|
| 346 | mark_awake(this->cltr->procs, * this); |
---|
| 347 | |
---|
| 348 | // DON'T just proceed, start looking again |
---|
| 349 | continue MAIN_LOOP; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | RUN_THREAD: |
---|
| 353 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
| 354 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 355 | /* paranoid */ verify( readyThread ); |
---|
| 356 | |
---|
| 357 | // Reset io dirty bit |
---|
| 358 | this->io.dirty = false; |
---|
| 359 | |
---|
| 360 | // We found a thread run it |
---|
| 361 | __run_thread(this, readyThread); |
---|
| 362 | |
---|
| 363 | // Are we done? |
---|
| 364 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP; |
---|
| 365 | |
---|
| 366 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 367 | unsigned long long curr = rdtscl(); |
---|
| 368 | if(curr > (last_tally + 500000000)) { |
---|
| 369 | __tally_stats(this->cltr->stats, __cfaabi_tls.this_stats); |
---|
| 370 | last_tally = curr; |
---|
| 371 | } |
---|
| 372 | #endif |
---|
| 373 | |
---|
| 374 | if(this->io.pending && !this->io.dirty) { |
---|
| 375 | __cfa_io_flush( this ); |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | ready_schedule_lock(); |
---|
| 379 | __maybe_io_drain( this ); |
---|
| 380 | ready_schedule_unlock(); |
---|
| 381 | #endif |
---|
[c81ebf9] | 382 | } |
---|
| 383 | |
---|
[4069faad] | 384 | __cfadbg_print_safe(runtime_core, "Kernel : core %p stopping\n", this); |
---|
[c84e80a] | 385 | } |
---|
[8118303] | 386 | |
---|
[dddb3dd0] | 387 | __cfa_io_stop( this ); |
---|
| 388 | |
---|
[454f478] | 389 | post( this->terminated ); |
---|
[bdeba0b] | 390 | |
---|
[28d73c1] | 391 | if(this == mainProcessor) { |
---|
[6a490b2] | 392 | // HACK : the coroutine context switch expects this_thread to be set |
---|
| 393 | // and it make sense for it to be set in all other cases except here |
---|
| 394 | // fake it |
---|
[8fc652e0] | 395 | __cfaabi_tls.this_thread = mainThread; |
---|
[6a490b2] | 396 | } |
---|
[7768b8d] | 397 | |
---|
[4069faad] | 398 | __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this); |
---|
[c84e80a] | 399 | } |
---|
| 400 | |
---|
[5c1a531] | 401 | static int * __volatile_errno() __attribute__((noinline)); |
---|
| 402 | static int * __volatile_errno() { asm(""); return &errno; } |
---|
| 403 | |
---|
[14a61b5] | 404 | // KERNEL ONLY |
---|
[1c273d0] | 405 | // runThread runs a thread by context switching |
---|
| 406 | // from the processor coroutine to the target thread |
---|
[ac2b598] | 407 | static void __run_thread(processor * this, $thread * thrd_dst) { |
---|
[8fc652e0] | 408 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[1eb239e4] | 409 | /* paranoid */ verifyf( thrd_dst->state == Ready || thrd_dst->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", thrd_dst->state, thrd_dst->preempted); |
---|
| 410 | /* paranoid */ verifyf( thrd_dst->link.next == 0p, "Expected null got %p", thrd_dst->link.next ); |
---|
| 411 | __builtin_prefetch( thrd_dst->context.SP ); |
---|
| 412 | |
---|
[dddb3dd0] | 413 | __cfadbg_print_safe(runtime_core, "Kernel : core %p running thread %p (%s)\n", this, thrd_dst, thrd_dst->self_cor.name); |
---|
| 414 | |
---|
[ac2b598] | 415 | $coroutine * proc_cor = get_coroutine(this->runner); |
---|
[8fcbb4c] | 416 | |
---|
[9f575ea] | 417 | // set state of processor coroutine to inactive |
---|
| 418 | verify(proc_cor->state == Active); |
---|
[ae7be7a] | 419 | proc_cor->state = Blocked; |
---|
[e8e457e] | 420 | |
---|
[9f575ea] | 421 | // Actually run the thread |
---|
[3381ed7] | 422 | RUNNING: while(true) { |
---|
[ff79d5e] | 423 | thrd_dst->preempted = __NO_PREEMPTION; |
---|
| 424 | thrd_dst->state = Active; |
---|
[e8e457e] | 425 | |
---|
[58d64a4] | 426 | // Update global state |
---|
[8fc652e0] | 427 | kernelTLS().this_thread = thrd_dst; |
---|
[75f3522] | 428 | |
---|
[8fc652e0] | 429 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 430 | /* paranoid */ verify( kernelTLS().this_thread == thrd_dst ); |
---|
[9d6e1b8a] | 431 | /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr ); |
---|
[b4b63e8] | 432 | /* paranoid */ verify( thrd_dst->context.SP ); |
---|
[5afb49a] | 433 | /* paranoid */ verify( thrd_dst->state != Halted ); |
---|
[210b8b3] | 434 | /* 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 |
---|
| 435 | /* 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 |
---|
[ac12f1f] | 436 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary ); |
---|
[b4b63e8] | 437 | |
---|
[3381ed7] | 438 | |
---|
[58d64a4] | 439 | |
---|
[9f575ea] | 440 | // set context switch to the thread that the processor is executing |
---|
[c7a900a] | 441 | __cfactx_switch( &proc_cor->context, &thrd_dst->context ); |
---|
| 442 | // when __cfactx_switch returns we are back in the processor coroutine |
---|
[9f575ea] | 443 | |
---|
[ac12f1f] | 444 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary ); |
---|
[210b8b3] | 445 | /* 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 ); |
---|
| 446 | /* 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 ); |
---|
[b4b63e8] | 447 | /* paranoid */ verify( thrd_dst->context.SP ); |
---|
[9d6e1b8a] | 448 | /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr ); |
---|
[8fc652e0] | 449 | /* paranoid */ verify( kernelTLS().this_thread == thrd_dst ); |
---|
| 450 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[75f3522] | 451 | |
---|
[58d64a4] | 452 | // Reset global state |
---|
[8fc652e0] | 453 | kernelTLS().this_thread = 0p; |
---|
[3381ed7] | 454 | |
---|
| 455 | // We just finished running a thread, there are a few things that could have happened. |
---|
| 456 | // 1 - Regular case : the thread has blocked and now one has scheduled it yet. |
---|
| 457 | // 2 - Racy case : the thread has blocked but someone has already tried to schedule it. |
---|
| 458 | // 4 - Preempted |
---|
| 459 | // In case 1, we may have won a race so we can't write to the state again. |
---|
| 460 | // In case 2, we lost the race so we now own the thread. |
---|
| 461 | |
---|
| 462 | if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) { |
---|
| 463 | // The thread was preempted, reschedule it and reset the flag |
---|
[254ad1b] | 464 | schedule_thread$( thrd_dst ); |
---|
[3381ed7] | 465 | break RUNNING; |
---|
| 466 | } |
---|
[75f3522] | 467 | |
---|
[3ea8ad1] | 468 | if(unlikely(thrd_dst->state == Halting)) { |
---|
[ff79d5e] | 469 | // The thread has halted, it should never be scheduled/run again |
---|
[5afb49a] | 470 | // finish the thread |
---|
| 471 | __thread_finish( thrd_dst ); |
---|
[ff79d5e] | 472 | break RUNNING; |
---|
| 473 | } |
---|
| 474 | |
---|
| 475 | /* paranoid */ verify( thrd_dst->state == Active ); |
---|
| 476 | thrd_dst->state = Blocked; |
---|
| 477 | |
---|
[3381ed7] | 478 | // set state of processor coroutine to active and the thread to inactive |
---|
[ff79d5e] | 479 | int old_ticket = __atomic_fetch_sub(&thrd_dst->ticket, 1, __ATOMIC_SEQ_CST); |
---|
| 480 | switch(old_ticket) { |
---|
[6a77224] | 481 | case TICKET_RUNNING: |
---|
[3381ed7] | 482 | // This is case 1, the regular case, nothing more is needed |
---|
| 483 | break RUNNING; |
---|
[6a77224] | 484 | case TICKET_UNBLOCK: |
---|
[ec43cf9] | 485 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 486 | __tls_stats()->ready.threads.threads++; |
---|
[73f4d08] | 487 | __push_stat( __tls_stats(), __tls_stats()->ready.threads.threads, false, "Processor", this ); |
---|
[ec43cf9] | 488 | #endif |
---|
[3381ed7] | 489 | // This is case 2, the racy case, someone tried to run this thread before it finished blocking |
---|
| 490 | // In this case, just run it again. |
---|
| 491 | continue RUNNING; |
---|
| 492 | default: |
---|
| 493 | // This makes no sense, something is wrong abort |
---|
[ff79d5e] | 494 | abort(); |
---|
[3381ed7] | 495 | } |
---|
[9f575ea] | 496 | } |
---|
[e8e457e] | 497 | |
---|
[9f575ea] | 498 | // Just before returning to the processor, set the processor coroutine to active |
---|
[e8e457e] | 499 | proc_cor->state = Active; |
---|
[1eb239e4] | 500 | |
---|
[dddb3dd0] | 501 | __cfadbg_print_safe(runtime_core, "Kernel : core %p finished running thread %p\n", this, thrd_dst); |
---|
| 502 | |
---|
[ec43cf9] | 503 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 504 | __tls_stats()->ready.threads.threads--; |
---|
[73f4d08] | 505 | __push_stat( __tls_stats(), __tls_stats()->ready.threads.threads, false, "Processor", this ); |
---|
[ec43cf9] | 506 | #endif |
---|
| 507 | |
---|
[8fc652e0] | 508 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[82c948c] | 509 | } |
---|
| 510 | |
---|
[14a61b5] | 511 | // KERNEL_ONLY |
---|
[b0c7419] | 512 | void returnToKernel() { |
---|
[8fc652e0] | 513 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 514 | $coroutine * proc_cor = get_coroutine(kernelTLS().this_processor->runner); |
---|
| 515 | $thread * thrd_src = kernelTLS().this_thread; |
---|
[e8e457e] | 516 | |
---|
[89eff25] | 517 | __STATS( thrd_src->last_proc = kernelTLS().this_processor; ) |
---|
[29cb302] | 518 | |
---|
[9f575ea] | 519 | // Run the thread on this processor |
---|
| 520 | { |
---|
| 521 | int local_errno = *__volatile_errno(); |
---|
| 522 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
| 523 | __x87_store; |
---|
| 524 | #endif |
---|
[b4b63e8] | 525 | /* paranoid */ verify( proc_cor->context.SP ); |
---|
[ac12f1f] | 526 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_src->canary ); |
---|
[c7a900a] | 527 | __cfactx_switch( &thrd_src->context, &proc_cor->context ); |
---|
[ac12f1f] | 528 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_src->canary ); |
---|
[9f575ea] | 529 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
| 530 | __x87_load; |
---|
| 531 | #endif |
---|
| 532 | *__volatile_errno() = local_errno; |
---|
[8fcbb4c] | 533 | } |
---|
[deca0f5] | 534 | |
---|
[29cb302] | 535 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[89eff25] | 536 | /* paranoid */ verify( thrd_src->last_proc != 0p ); |
---|
| 537 | if(thrd_src->last_proc != kernelTLS().this_processor) { |
---|
[29cb302] | 538 | __tls_stats()->ready.threads.migration++; |
---|
| 539 | } |
---|
| 540 | #endif |
---|
| 541 | |
---|
[8fc652e0] | 542 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[210b8b3] | 543 | /* 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 ); |
---|
| 544 | /* 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] | 545 | } |
---|
| 546 | |
---|
[8def349] | 547 | //----------------------------------------------------------------------------- |
---|
| 548 | // Scheduler routines |
---|
[14a61b5] | 549 | // KERNEL ONLY |
---|
[e9c0b4c] | 550 | static void __schedule_thread( $thread * thrd ) { |
---|
[8fc652e0] | 551 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[254ad1b] | 552 | /* paranoid */ verify( ready_schedule_islocked()); |
---|
[6a490b2] | 553 | /* paranoid */ verify( thrd ); |
---|
| 554 | /* paranoid */ verify( thrd->state != Halted ); |
---|
[9d6e1b8a] | 555 | /* paranoid */ verify( thrd->curr_cluster ); |
---|
[3381ed7] | 556 | /* paranoid */ #if defined( __CFA_WITH_VERIFY__ ) |
---|
[504a7dc] | 557 | /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION, |
---|
| 558 | "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
[ff79d5e] | 559 | /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active, |
---|
[504a7dc] | 560 | "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
[3381ed7] | 561 | /* paranoid */ #endif |
---|
[6a490b2] | 562 | /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next ); |
---|
[ac12f1f] | 563 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd->canary ); |
---|
[b4b63e8] | 564 | |
---|
[9f575ea] | 565 | |
---|
[ae7be7a] | 566 | if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready; |
---|
[6b4cdd3] | 567 | |
---|
[ec43cf9] | 568 | // Dereference the thread now because once we push it, there is not guaranteed it's still valid. |
---|
| 569 | struct cluster * cl = thrd->curr_cluster; |
---|
[89eff25] | 570 | __STATS(bool outside = thrd->last_proc && thrd->last_proc != kernelTLS().this_processor; ) |
---|
[32a8b61] | 571 | |
---|
[254ad1b] | 572 | // push the thread to the cluster ready-queue |
---|
| 573 | push( cl, thrd ); |
---|
[32a8b61] | 574 | |
---|
[254ad1b] | 575 | // variable thrd is no longer safe to use |
---|
[734908c] | 576 | thrd = 0xdeaddeaddeaddeadp; |
---|
[32a8b61] | 577 | |
---|
[254ad1b] | 578 | // wake the cluster using the save variable. |
---|
| 579 | __wake_one( cl ); |
---|
[1c273d0] | 580 | |
---|
[ec43cf9] | 581 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 582 | if( kernelTLS().this_stats ) { |
---|
| 583 | __tls_stats()->ready.threads.threads++; |
---|
[89eff25] | 584 | if(outside) { |
---|
| 585 | __tls_stats()->ready.threads.extunpark++; |
---|
| 586 | } |
---|
[73f4d08] | 587 | __push_stat( __tls_stats(), __tls_stats()->ready.threads.threads, false, "Processor", kernelTLS().this_processor ); |
---|
[ec43cf9] | 588 | } |
---|
| 589 | else { |
---|
| 590 | __atomic_fetch_add(&cl->stats->ready.threads.threads, 1, __ATOMIC_RELAXED); |
---|
[89eff25] | 591 | __atomic_fetch_add(&cl->stats->ready.threads.extunpark, 1, __ATOMIC_RELAXED); |
---|
[73f4d08] | 592 | __push_stat( cl->stats, cl->stats->ready.threads.threads, true, "Cluster", cl ); |
---|
[ec43cf9] | 593 | } |
---|
| 594 | #endif |
---|
| 595 | |
---|
[254ad1b] | 596 | /* paranoid */ verify( ready_schedule_islocked()); |
---|
[8fc652e0] | 597 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[db6f06a] | 598 | } |
---|
| 599 | |
---|
[254ad1b] | 600 | void schedule_thread$( $thread * thrd ) { |
---|
| 601 | ready_schedule_lock(); |
---|
| 602 | __schedule_thread( thrd ); |
---|
| 603 | ready_schedule_unlock(); |
---|
| 604 | } |
---|
| 605 | |
---|
[14a61b5] | 606 | // KERNEL ONLY |
---|
[1eb239e4] | 607 | static inline $thread * __next_thread(cluster * this) with( *this ) { |
---|
[8fc652e0] | 608 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[7768b8d] | 609 | |
---|
[e873838] | 610 | ready_schedule_lock(); |
---|
[431cd4f] | 611 | $thread * thrd = pop_fast( this ); |
---|
[e873838] | 612 | ready_schedule_unlock(); |
---|
[7768b8d] | 613 | |
---|
[8fc652e0] | 614 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[1eb239e4] | 615 | return thrd; |
---|
[eb2e723] | 616 | } |
---|
| 617 | |
---|
[64a7146] | 618 | // KERNEL ONLY |
---|
[1eb239e4] | 619 | static inline $thread * __next_thread_slow(cluster * this) with( *this ) { |
---|
[8fc652e0] | 620 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[64a7146] | 621 | |
---|
[e873838] | 622 | ready_schedule_lock(); |
---|
[fc59df78] | 623 | $thread * thrd; |
---|
| 624 | for(25) { |
---|
| 625 | thrd = pop_slow( this ); |
---|
| 626 | if(thrd) goto RET; |
---|
| 627 | } |
---|
| 628 | thrd = pop_search( this ); |
---|
| 629 | |
---|
| 630 | RET: |
---|
[e873838] | 631 | ready_schedule_unlock(); |
---|
[64a7146] | 632 | |
---|
[8fc652e0] | 633 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[1eb239e4] | 634 | return thrd; |
---|
[64a7146] | 635 | } |
---|
| 636 | |
---|
[c6c7e6c] | 637 | static inline bool __must_unpark( $thread * thrd ) { |
---|
[ff79d5e] | 638 | int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST); |
---|
| 639 | switch(old_ticket) { |
---|
[6a77224] | 640 | case TICKET_RUNNING: |
---|
[3381ed7] | 641 | // Wake won the race, the thread will reschedule/rerun itself |
---|
[c6c7e6c] | 642 | return false; |
---|
[6a77224] | 643 | case TICKET_BLOCKED: |
---|
[3381ed7] | 644 | /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION ); |
---|
[ff79d5e] | 645 | /* paranoid */ verify( thrd->state == Blocked ); |
---|
[c6c7e6c] | 646 | return true; |
---|
[3381ed7] | 647 | default: |
---|
| 648 | // This makes no sense, something is wrong abort |
---|
[7ee8153] | 649 | abort("Thread %p (%s) has mismatch park/unpark\n", thrd, thrd->self_cor.name); |
---|
[de6319f] | 650 | } |
---|
[eb2e723] | 651 | } |
---|
| 652 | |
---|
[e9c0b4c] | 653 | void __kernel_unpark( $thread * thrd ) { |
---|
| 654 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 655 | /* paranoid */ verify( ready_schedule_islocked()); |
---|
| 656 | |
---|
| 657 | if( !thrd ) return; |
---|
| 658 | |
---|
| 659 | if(__must_unpark(thrd)) { |
---|
| 660 | // Wake lost the race, |
---|
| 661 | __schedule_thread( thrd ); |
---|
| 662 | } |
---|
| 663 | |
---|
| 664 | /* paranoid */ verify( ready_schedule_islocked()); |
---|
| 665 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 666 | } |
---|
| 667 | |
---|
[c6c7e6c] | 668 | void unpark( $thread * thrd ) { |
---|
| 669 | if( !thrd ) return; |
---|
[0b33412] | 670 | |
---|
[c6c7e6c] | 671 | if(__must_unpark(thrd)) { |
---|
| 672 | disable_interrupts(); |
---|
[a3821fa] | 673 | // Wake lost the race, |
---|
[254ad1b] | 674 | schedule_thread$( thrd ); |
---|
[a3821fa] | 675 | enable_interrupts(false); |
---|
[de6319f] | 676 | } |
---|
[eb2e723] | 677 | } |
---|
[0b33412] | 678 | |
---|
[e235429] | 679 | void park( void ) { |
---|
[a3821fa] | 680 | __disable_interrupts_checked(); |
---|
| 681 | /* paranoid */ verify( kernelTLS().this_thread->preempted == __NO_PREEMPTION ); |
---|
| 682 | returnToKernel(); |
---|
| 683 | __enable_interrupts_checked(); |
---|
[0c78741] | 684 | |
---|
| 685 | } |
---|
[09800e9] | 686 | |
---|
[5afb49a] | 687 | extern "C" { |
---|
| 688 | // Leave the thread monitor |
---|
| 689 | // last routine called by a thread. |
---|
| 690 | // Should never return |
---|
| 691 | void __cfactx_thrd_leave() { |
---|
[8fc652e0] | 692 | $thread * thrd = active_thread(); |
---|
[5afb49a] | 693 | $monitor * this = &thrd->self_mon; |
---|
| 694 | |
---|
| 695 | // Lock the monitor now |
---|
| 696 | lock( this->lock __cfaabi_dbg_ctx2 ); |
---|
| 697 | |
---|
| 698 | disable_interrupts(); |
---|
| 699 | |
---|
[9d6e1b8a] | 700 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 701 | /* paranoid */ verify( thrd->state == Active ); |
---|
| 702 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd->canary ); |
---|
| 703 | /* paranoid */ verify( kernelTLS().this_thread == thrd ); |
---|
| 704 | /* paranoid */ verify( thrd->context.SP ); |
---|
| 705 | /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) > ((uintptr_t)__get_stack(thrd->curr_cor)->limit), "ERROR : $thread %p has been corrupted.\n StackPointer too large.\n", thrd ); |
---|
| 706 | /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) < ((uintptr_t)__get_stack(thrd->curr_cor)->base ), "ERROR : $thread %p has been corrupted.\n StackPointer too small.\n", thrd ); |
---|
| 707 | |
---|
[3ea8ad1] | 708 | thrd->state = Halting; |
---|
[58688bf] | 709 | if( TICKET_RUNNING != thrd->ticket ) { abort( "Thread terminated with pending unpark" ); } |
---|
[9d6e1b8a] | 710 | if( thrd != this->owner ) { abort( "Thread internal monitor has incorrect owner" ); } |
---|
| 711 | if( this->recursion != 1) { abort( "Thread internal monitor has unbalanced recursion" ); } |
---|
[5afb49a] | 712 | |
---|
| 713 | // Leave the thread |
---|
| 714 | returnToKernel(); |
---|
| 715 | |
---|
| 716 | // Control flow should never reach here! |
---|
[9d6e1b8a] | 717 | abort(); |
---|
[5afb49a] | 718 | } |
---|
[09800e9] | 719 | } |
---|
| 720 | |
---|
[14a61b5] | 721 | // KERNEL ONLY |
---|
[3381ed7] | 722 | bool force_yield( __Preemption_Reason reason ) { |
---|
[a3821fa] | 723 | __disable_interrupts_checked(); |
---|
| 724 | $thread * thrd = kernelTLS().this_thread; |
---|
| 725 | /* paranoid */ verify(thrd->state == Active); |
---|
| 726 | |
---|
| 727 | // SKULLDUGGERY: It is possible that we are preempting this thread just before |
---|
| 728 | // it was going to park itself. If that is the case and it is already using the |
---|
| 729 | // intrusive fields then we can't use them to preempt the thread |
---|
| 730 | // If that is the case, abandon the preemption. |
---|
| 731 | bool preempted = false; |
---|
| 732 | if(thrd->link.next == 0p) { |
---|
| 733 | preempted = true; |
---|
| 734 | thrd->preempted = reason; |
---|
| 735 | returnToKernel(); |
---|
| 736 | } |
---|
| 737 | __enable_interrupts_checked( false ); |
---|
[3381ed7] | 738 | return preempted; |
---|
[f2b12406] | 739 | } |
---|
| 740 | |
---|
[14a61b5] | 741 | //============================================================================================= |
---|
[92e7631] | 742 | // Kernel Idle Sleep |
---|
[14a61b5] | 743 | //============================================================================================= |
---|
[64a7146] | 744 | // Wake a thread from the front if there are any |
---|
[e873838] | 745 | static void __wake_one(cluster * this) { |
---|
[8fc652e0] | 746 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[e873838] | 747 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
[14a61b5] | 748 | |
---|
[64a7146] | 749 | // Check if there is a sleeping processor |
---|
[1eb239e4] | 750 | processor * p; |
---|
| 751 | unsigned idle; |
---|
| 752 | unsigned total; |
---|
[6a9b12b] | 753 | [idle, total, p] = query_idles(this->procs); |
---|
[14a61b5] | 754 | |
---|
[64a7146] | 755 | // If no one is sleeping, we are done |
---|
[1eb239e4] | 756 | if( idle == 0 ) return; |
---|
[14a61b5] | 757 | |
---|
[64a7146] | 758 | // We found a processor, wake it up |
---|
[dddb3dd0] | 759 | eventfd_t val; |
---|
| 760 | val = 1; |
---|
| 761 | eventfd_write( p->idle, val ); |
---|
[14a61b5] | 762 | |
---|
[1eb239e4] | 763 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[5cb51502] | 764 | if( kernelTLS().this_stats ) { |
---|
| 765 | __tls_stats()->ready.sleep.wakes++; |
---|
| 766 | } |
---|
| 767 | else { |
---|
| 768 | __atomic_fetch_add(&this->stats->ready.sleep.wakes, 1, __ATOMIC_RELAXED); |
---|
| 769 | } |
---|
[1eb239e4] | 770 | #endif |
---|
| 771 | |
---|
[e873838] | 772 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
[8fc652e0] | 773 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[1eb239e4] | 774 | |
---|
| 775 | return; |
---|
[64a7146] | 776 | } |
---|
[14a61b5] | 777 | |
---|
[64a7146] | 778 | // Unconditionnaly wake a thread |
---|
[1eb239e4] | 779 | void __wake_proc(processor * this) { |
---|
[64a7146] | 780 | __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this); |
---|
[14a61b5] | 781 | |
---|
[a3821fa] | 782 | __disable_interrupts_checked(); |
---|
[8fc652e0] | 783 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[dddb3dd0] | 784 | eventfd_t val; |
---|
| 785 | val = 1; |
---|
[55d6affb] | 786 | eventfd_write( this->idle, val ); |
---|
[a3821fa] | 787 | __enable_interrupts_checked(); |
---|
[92e7631] | 788 | } |
---|
| 789 | |
---|
[6a9b12b] | 790 | static void mark_idle(__cluster_proc_list & this, processor & proc) { |
---|
[8fc652e0] | 791 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[1eb239e4] | 792 | lock( this ); |
---|
| 793 | this.idle++; |
---|
| 794 | /* paranoid */ verify( this.idle <= this.total ); |
---|
[fc59b580] | 795 | remove(proc); |
---|
[6a9b12b] | 796 | insert_first(this.idles, proc); |
---|
[1eb239e4] | 797 | unlock( this ); |
---|
[8fc652e0] | 798 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[1eb239e4] | 799 | } |
---|
[8e16177] | 800 | |
---|
[6a9b12b] | 801 | static void mark_awake(__cluster_proc_list & this, processor & proc) { |
---|
[8fc652e0] | 802 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[1eb239e4] | 803 | lock( this ); |
---|
| 804 | this.idle--; |
---|
| 805 | /* paranoid */ verify( this.idle >= 0 ); |
---|
| 806 | remove(proc); |
---|
[fc59b580] | 807 | insert_last(this.actives, proc); |
---|
[1eb239e4] | 808 | unlock( this ); |
---|
[8fc652e0] | 809 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[1eb239e4] | 810 | } |
---|
[c34ebf2] | 811 | |
---|
[6a9b12b] | 812 | static [unsigned idle, unsigned total, * processor] query_idles( & __cluster_proc_list this ) { |
---|
| 813 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 814 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
| 815 | |
---|
[1eb239e4] | 816 | for() { |
---|
| 817 | uint64_t l = __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST); |
---|
| 818 | if( 1 == (l % 2) ) { Pause(); continue; } |
---|
| 819 | unsigned idle = this.idle; |
---|
| 820 | unsigned total = this.total; |
---|
[6a9b12b] | 821 | processor * proc = &this.idles`first; |
---|
[7fdae38] | 822 | // Compiler fence is unnecessary, but gcc-8 and older incorrectly reorder code without it |
---|
| 823 | asm volatile("": : :"memory"); |
---|
[1eb239e4] | 824 | if(l != __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST)) { Pause(); continue; } |
---|
| 825 | return [idle, total, proc]; |
---|
| 826 | } |
---|
[6a9b12b] | 827 | |
---|
| 828 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
| 829 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[6b4cdd3] | 830 | } |
---|
| 831 | |
---|
[dbe9b08] | 832 | //============================================================================================= |
---|
| 833 | // Unexpected Terminating logic |
---|
| 834 | //============================================================================================= |
---|
[92bfda0] | 835 | void __kernel_abort_msg( char * abort_text, int abort_text_size ) { |
---|
| 836 | $thread * thrd = __cfaabi_tls.this_thread; |
---|
[9d944b2] | 837 | |
---|
[de94a60] | 838 | if(thrd) { |
---|
| 839 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd ); |
---|
[1c40091] | 840 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
[de94a60] | 841 | |
---|
[212c2187] | 842 | if ( &thrd->self_cor != thrd->curr_cor ) { |
---|
| 843 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor ); |
---|
[1c40091] | 844 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
[de94a60] | 845 | } |
---|
| 846 | else { |
---|
[1c40091] | 847 | __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 ); |
---|
[de94a60] | 848 | } |
---|
[1c273d0] | 849 | } |
---|
[9d944b2] | 850 | else { |
---|
[de94a60] | 851 | int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" ); |
---|
[1c40091] | 852 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
[9d944b2] | 853 | } |
---|
| 854 | } |
---|
| 855 | |
---|
[92bfda0] | 856 | int __kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) { |
---|
| 857 | return get_coroutine(__cfaabi_tls.this_thread) == get_coroutine(mainThread) ? 4 : 2; |
---|
[2b8bc41] | 858 | } |
---|
| 859 | |
---|
[de94a60] | 860 | static __spinlock_t kernel_debug_lock; |
---|
| 861 | |
---|
[9d944b2] | 862 | extern "C" { |
---|
[1c40091] | 863 | void __cfaabi_bits_acquire() { |
---|
[36982fc] | 864 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 ); |
---|
[9d944b2] | 865 | } |
---|
| 866 | |
---|
[1c40091] | 867 | void __cfaabi_bits_release() { |
---|
[ea7d2b0] | 868 | unlock( kernel_debug_lock ); |
---|
[9d944b2] | 869 | } |
---|
[8118303] | 870 | } |
---|
| 871 | |
---|
[fa21ac9] | 872 | //============================================================================================= |
---|
| 873 | // Kernel Utilities |
---|
| 874 | //============================================================================================= |
---|
[dddb3dd0] | 875 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
| 876 | #include "io/types.hfa" |
---|
| 877 | #endif |
---|
| 878 | |
---|
[c1c95b1] | 879 | static inline bool __maybe_io_drain( processor * proc ) { |
---|
[e9c0b4c] | 880 | bool ret = false; |
---|
[dddb3dd0] | 881 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
| 882 | __cfadbg_print_safe(runtime_core, "Kernel : core %p checking io for ring %d\n", proc, proc->io.ctx->fd); |
---|
| 883 | |
---|
| 884 | // Check if we should drain the queue |
---|
| 885 | $io_context * ctx = proc->io.ctx; |
---|
| 886 | unsigned head = *ctx->cq.head; |
---|
| 887 | unsigned tail = *ctx->cq.tail; |
---|
[c1c95b1] | 888 | if(head == tail) return false; |
---|
[b7fd2db6] | 889 | ready_schedule_lock(); |
---|
[e9c0b4c] | 890 | ret = __cfa_io_drain( proc ); |
---|
[b7fd2db6] | 891 | ready_schedule_unlock(); |
---|
[dddb3dd0] | 892 | #endif |
---|
[e9c0b4c] | 893 | return ret; |
---|
[dddb3dd0] | 894 | } |
---|
| 895 | |
---|
[de94a60] | 896 | //----------------------------------------------------------------------------- |
---|
| 897 | // Debug |
---|
| 898 | __cfaabi_dbg_debug_do( |
---|
[1997b4e] | 899 | extern "C" { |
---|
[ae66348] | 900 | void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) { |
---|
[1997b4e] | 901 | this.prev_name = prev_name; |
---|
[8fc652e0] | 902 | this.prev_thrd = kernelTLS().this_thread; |
---|
[1997b4e] | 903 | } |
---|
[9181f1d] | 904 | } |
---|
[f7d6bb0] | 905 | ) |
---|
[2026bb6] | 906 | |
---|
| 907 | //----------------------------------------------------------------------------- |
---|
| 908 | // Debug |
---|
[8c50aed] | 909 | bool threading_enabled(void) __attribute__((const)) { |
---|
[2026bb6] | 910 | return true; |
---|
| 911 | } |
---|
[c34ebf2] | 912 | |
---|
| 913 | //----------------------------------------------------------------------------- |
---|
| 914 | // Statistics |
---|
| 915 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 916 | void print_halts( processor & this ) { |
---|
| 917 | this.print_halts = true; |
---|
| 918 | } |
---|
[58688bf] | 919 | |
---|
| 920 | void print_stats_now( cluster & this, int flags ) { |
---|
[1b033b8] | 921 | __print_stats( this.stats, this.print_stats, "Cluster", this.name, (void*)&this ); |
---|
| 922 | } |
---|
[c34ebf2] | 923 | #endif |
---|
[8118303] | 924 | // Local Variables: // |
---|
| 925 | // mode: c // |
---|
| 926 | // tab-width: 4 // |
---|
| 927 | // End: // |
---|