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