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