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