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