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