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