| [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
|
|---|
| [bb83b47] | 12 | // Last Modified On : Thu Jul 9 06:22:54 2020
|
|---|
| 13 | // Update Count : 66
|
|---|
| [8118303] | 14 | //
|
|---|
| 15 |
|
|---|
| [2026bb6] | 16 | #define __cforall_thread__
|
|---|
| [4069faad] | 17 | // #define __CFA_DEBUG_PRINT_RUNTIME_CORE__
|
|---|
| [2026bb6] | 18 |
|
|---|
| [8118303] | 19 | //C Includes
|
|---|
| [c84e80a] | 20 | #include <stddef.h>
|
|---|
| [214e8da] | 21 | #include <errno.h>
|
|---|
| [ea8b2f7] | 22 | #include <string.h>
|
|---|
| [9d944b2] | 23 | #include <stdio.h>
|
|---|
| [8fcbb4c] | 24 | #include <fenv.h>
|
|---|
| [58b6d1b] | 25 | #include <signal.h>
|
|---|
| [9d944b2] | 26 | #include <unistd.h>
|
|---|
| [27f5f71] | 27 | #include <limits.h> // PTHREAD_STACK_MIN
|
|---|
| [1a3040c] | 28 | #include <sys/mman.h> // mprotect
|
|---|
| [ada0246d] | 29 | extern "C" {
|
|---|
| 30 | #include <sys/resource.h>
|
|---|
| [eb2e723] | 31 | }
|
|---|
| [8118303] | 32 |
|
|---|
| 33 | //CFA Includes
|
|---|
| [58b6d1b] | 34 | #include "time.hfa"
|
|---|
| [73abe95] | 35 | #include "kernel_private.hfa"
|
|---|
| 36 | #include "preemption.hfa"
|
|---|
| 37 | #include "startup.hfa"
|
|---|
| [8118303] | 38 |
|
|---|
| 39 | //Private includes
|
|---|
| 40 | #define __CFA_INVOKE_PRIVATE__
|
|---|
| 41 | #include "invoke.h"
|
|---|
| 42 |
|
|---|
| [4069faad] | 43 |
|
|---|
| [deca0f5] | 44 | //-----------------------------------------------------------------------------
|
|---|
| 45 | // Some assembly required
|
|---|
| [1805b1b] | 46 | #if defined( __i386 )
|
|---|
| [deca0f5] | 47 | #define CtxGet( ctx ) \
|
|---|
| 48 | __asm__ volatile ( \
|
|---|
| 49 | "movl %%esp,%0\n"\
|
|---|
| 50 | "movl %%ebp,%1\n"\
|
|---|
| 51 | : "=rm" (ctx.SP),\
|
|---|
| 52 | "=rm" (ctx.FP) \
|
|---|
| 53 | )
|
|---|
| 54 |
|
|---|
| 55 | // mxcr : SSE Status and Control bits (control bits are preserved across function calls)
|
|---|
| 56 | // fcw : X87 FPU control word (preserved across function calls)
|
|---|
| 57 | #define __x87_store \
|
|---|
| 58 | uint32_t __mxcr; \
|
|---|
| 59 | uint16_t __fcw; \
|
|---|
| 60 | __asm__ volatile ( \
|
|---|
| 61 | "stmxcsr %0\n" \
|
|---|
| 62 | "fnstcw %1\n" \
|
|---|
| 63 | : "=m" (__mxcr),\
|
|---|
| 64 | "=m" (__fcw) \
|
|---|
| 65 | )
|
|---|
| 66 |
|
|---|
| 67 | #define __x87_load \
|
|---|
| 68 | __asm__ volatile ( \
|
|---|
| 69 | "fldcw %1\n" \
|
|---|
| 70 | "ldmxcsr %0\n" \
|
|---|
| 71 | ::"m" (__mxcr),\
|
|---|
| 72 | "m" (__fcw) \
|
|---|
| 73 | )
|
|---|
| 74 |
|
|---|
| 75 | #elif defined( __x86_64 )
|
|---|
| 76 | #define CtxGet( ctx ) \
|
|---|
| 77 | __asm__ volatile ( \
|
|---|
| 78 | "movq %%rsp,%0\n"\
|
|---|
| 79 | "movq %%rbp,%1\n"\
|
|---|
| 80 | : "=rm" (ctx.SP),\
|
|---|
| 81 | "=rm" (ctx.FP) \
|
|---|
| 82 | )
|
|---|
| 83 |
|
|---|
| 84 | #define __x87_store \
|
|---|
| 85 | uint32_t __mxcr; \
|
|---|
| 86 | uint16_t __fcw; \
|
|---|
| 87 | __asm__ volatile ( \
|
|---|
| 88 | "stmxcsr %0\n" \
|
|---|
| 89 | "fnstcw %1\n" \
|
|---|
| 90 | : "=m" (__mxcr),\
|
|---|
| 91 | "=m" (__fcw) \
|
|---|
| 92 | )
|
|---|
| 93 |
|
|---|
| 94 | #define __x87_load \
|
|---|
| 95 | __asm__ volatile ( \
|
|---|
| 96 | "fldcw %1\n" \
|
|---|
| 97 | "ldmxcsr %0\n" \
|
|---|
| 98 | :: "m" (__mxcr),\
|
|---|
| 99 | "m" (__fcw) \
|
|---|
| 100 | )
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 | #elif defined( __ARM_ARCH )
|
|---|
| 104 | #define CtxGet( ctx ) __asm__ ( \
|
|---|
| 105 | "mov %0,%%sp\n" \
|
|---|
| 106 | "mov %1,%%r11\n" \
|
|---|
| 107 | : "=rm" (ctx.SP), "=rm" (ctx.FP) )
|
|---|
| 108 | #else
|
|---|
| 109 | #error unknown hardware architecture
|
|---|
| 110 | #endif
|
|---|
| 111 |
|
|---|
| 112 | //-----------------------------------------------------------------------------
|
|---|
| [2ac095d] | 113 | //Start and stop routine for the kernel, declared first to make sure they run first
|
|---|
| [8c50aed] | 114 | static void __kernel_startup (void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
|
|---|
| 115 | static void __kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
|
|---|
| [2ac095d] | 116 |
|
|---|
| [92e7631] | 117 | //-----------------------------------------------------------------------------
|
|---|
| 118 | // Kernel Scheduling logic
|
|---|
| 119 | static $thread * __next_thread(cluster * this);
|
|---|
| [64a7146] | 120 | static bool __has_next_thread(cluster * this);
|
|---|
| [92e7631] | 121 | static void __run_thread(processor * this, $thread * dst);
|
|---|
| 122 | static bool __wake_proc(processor *);
|
|---|
| [64a7146] | 123 | static bool __wake_one(struct __processor_id_t * id, cluster * cltr);
|
|---|
| 124 | static void __halt(processor * this);
|
|---|
| [2ac095d] | 125 |
|
|---|
| [8def349] | 126 | //-----------------------------------------------------------------------------
|
|---|
| 127 | // Kernel storage
|
|---|
| [b388ee81] | 128 | KERNEL_STORAGE(cluster, mainCluster);
|
|---|
| 129 | KERNEL_STORAGE(processor, mainProcessor);
|
|---|
| 130 | KERNEL_STORAGE($thread, mainThread);
|
|---|
| 131 | KERNEL_STORAGE(__stack_t, mainThreadCtx);
|
|---|
| [f00b26d4] | 132 | KERNEL_STORAGE(io_context, mainPollerThread);
|
|---|
| [b388ee81] | 133 | KERNEL_STORAGE(__scheduler_RWLock_t, __scheduler_lock);
|
|---|
| [8834751] | 134 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| 135 | KERNEL_STORAGE(__stats_t, mainProcStats);
|
|---|
| 136 | #endif
|
|---|
| [8def349] | 137 |
|
|---|
| [b388ee81] | 138 | cluster * mainCluster;
|
|---|
| 139 | processor * mainProcessor;
|
|---|
| 140 | $thread * mainThread;
|
|---|
| 141 | __scheduler_RWLock_t * __scheduler_lock;
|
|---|
| [eb2e723] | 142 |
|
|---|
| [ea8b2f7] | 143 | extern "C" {
|
|---|
| [1805b1b] | 144 | struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters;
|
|---|
| [ea8b2f7] | 145 | }
|
|---|
| [de94a60] | 146 |
|
|---|
| [b2f6113] | 147 | size_t __page_size = 0;
|
|---|
| 148 |
|
|---|
| [bd98b58] | 149 | //-----------------------------------------------------------------------------
|
|---|
| 150 | // Global state
|
|---|
| [bb83b47] | 151 | thread_local struct KernelThreadData kernelTLS __attribute__ ((tls_model ( "initial-exec" ))) @= {
|
|---|
| [1805b1b] | 152 | NULL, // cannot use 0p
|
|---|
| [b10affd] | 153 | NULL,
|
|---|
| [8834751] | 154 | NULL,
|
|---|
| [21184e3] | 155 | { 1, false, false },
|
|---|
| [b10affd] | 156 | };
|
|---|
| [c84e80a] | 157 |
|
|---|
| 158 | //-----------------------------------------------------------------------------
|
|---|
| [de6319f] | 159 | // Struct to steal stack
|
|---|
| [8def349] | 160 | struct current_stack_info_t {
|
|---|
| [1805b1b] | 161 | __stack_t * storage; // pointer to stack object
|
|---|
| 162 | void * base; // base of stack
|
|---|
| 163 | void * limit; // stack grows towards stack limit
|
|---|
| 164 | void * context; // address of cfa_context_t
|
|---|
| [c84e80a] | 165 | };
|
|---|
| 166 |
|
|---|
| [242a902] | 167 | void ?{}( current_stack_info_t & this ) {
|
|---|
| [b2f6113] | 168 | __stack_context_t ctx;
|
|---|
| 169 | CtxGet( ctx );
|
|---|
| 170 | this.base = ctx.FP;
|
|---|
| [8def349] | 171 |
|
|---|
| 172 | rlimit r;
|
|---|
| [132fad4] | 173 | getrlimit( RLIMIT_STACK, &r);
|
|---|
| [69a61d2] | 174 | size_t size = r.rlim_cur;
|
|---|
| [8def349] | 175 |
|
|---|
| [69a61d2] | 176 | this.limit = (void *)(((intptr_t)this.base) - size);
|
|---|
| [9236060] | 177 | this.context = &storage_mainThreadCtx;
|
|---|
| [8def349] | 178 | }
|
|---|
| 179 |
|
|---|
| [de6319f] | 180 | //-----------------------------------------------------------------------------
|
|---|
| 181 | // Main thread construction
|
|---|
| [8def349] | 182 |
|
|---|
| [ac2b598] | 183 | void ?{}( $coroutine & this, current_stack_info_t * info) with( this ) {
|
|---|
| [b2f6113] | 184 | stack.storage = info->storage;
|
|---|
| 185 | with(*stack.storage) {
|
|---|
| 186 | limit = info->limit;
|
|---|
| 187 | base = info->base;
|
|---|
| 188 | }
|
|---|
| [ffe2fad] | 189 | __attribute__((may_alias)) intptr_t * istorage = (intptr_t*) &stack.storage;
|
|---|
| 190 | *istorage |= 0x1;
|
|---|
| [65deb18] | 191 | name = "Main Thread";
|
|---|
| 192 | state = Start;
|
|---|
| [1805b1b] | 193 | starter = 0p;
|
|---|
| 194 | last = 0p;
|
|---|
| 195 | cancellation = 0p;
|
|---|
| [8def349] | 196 | }
|
|---|
| 197 |
|
|---|
| [ac2b598] | 198 | void ?{}( $thread & this, current_stack_info_t * info) with( this ) {
|
|---|
| [ff79d5e] | 199 | ticket = 1;
|
|---|
| [e8e457e] | 200 | state = Start;
|
|---|
| [65deb18] | 201 | self_cor{ info };
|
|---|
| [82c948c] | 202 | curr_cor = &self_cor;
|
|---|
| [de6319f] | 203 | curr_cluster = mainCluster;
|
|---|
| [82c948c] | 204 | self_mon.owner = &this;
|
|---|
| 205 | self_mon.recursion = 1;
|
|---|
| 206 | self_mon_p = &self_mon;
|
|---|
| [b798713] | 207 | link.next = 0p;
|
|---|
| 208 | link.prev = 0p;
|
|---|
| [de94a60] | 209 |
|
|---|
| [1805b1b] | 210 | node.next = 0p;
|
|---|
| 211 | node.prev = 0p;
|
|---|
| [a1a17a74] | 212 | doregister(curr_cluster, this);
|
|---|
| [82c948c] | 213 |
|
|---|
| 214 | monitors{ &self_mon_p, 1, (fptr_t)0 };
|
|---|
| [8def349] | 215 | }
|
|---|
| [c84e80a] | 216 |
|
|---|
| [8def349] | 217 | //-----------------------------------------------------------------------------
|
|---|
| 218 | // Processor coroutine
|
|---|
| [de6319f] | 219 | void ?{}(processorCtx_t & this) {
|
|---|
| [39fea2f] | 220 |
|
|---|
| [8def349] | 221 | }
|
|---|
| 222 |
|
|---|
| [39fea2f] | 223 | // Construct the processor context of non-main processors
|
|---|
| [c29c342] | 224 | static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) {
|
|---|
| [242a902] | 225 | (this.__cor){ info };
|
|---|
| 226 | this.proc = proc;
|
|---|
| [8def349] | 227 | }
|
|---|
| 228 |
|
|---|
| [c7a900a] | 229 | static void * __invoke_processor(void * arg);
|
|---|
| [8c50aed] | 230 |
|
|---|
| [28d73c1] | 231 | static init(processor & this, const char name[], cluster & _cltr) with( this ) {
|
|---|
| [de6319f] | 232 | this.name = name;
|
|---|
| [13c5e19] | 233 | this.cltr = &_cltr;
|
|---|
| [7768b8d] | 234 | id = -1u;
|
|---|
| [b0c7419] | 235 | destroyer = 0p;
|
|---|
| [c40e7c5] | 236 | do_terminate = false;
|
|---|
| [1805b1b] | 237 | preemption_alarm = 0p;
|
|---|
| [c40e7c5] | 238 | pending_preemption = false;
|
|---|
| [8def349] | 239 |
|
|---|
| [c34ebf2] | 240 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| [69fbc61] | 241 | print_stats = 0;
|
|---|
| [c34ebf2] | 242 | print_halts = false;
|
|---|
| 243 | #endif
|
|---|
| 244 |
|
|---|
| [320ec6fc] | 245 | int target = __atomic_add_fetch( &cltr->nprocessors, 1u, __ATOMIC_SEQ_CST );
|
|---|
| [28d73c1] | 246 |
|
|---|
| 247 | id = doregister((__processor_id_t*)&this);
|
|---|
| 248 |
|
|---|
| 249 | // Lock the RWlock so no-one pushes/pops while we are changing the queue
|
|---|
| 250 | uint_fast32_t last_size = ready_mutate_lock();
|
|---|
| 251 |
|
|---|
| 252 | // Adjust the ready queue size
|
|---|
| [320ec6fc] | 253 | ready_queue_grow( cltr, target );
|
|---|
| [28d73c1] | 254 |
|
|---|
| 255 | // Unlock the RWlock
|
|---|
| 256 | ready_mutate_unlock( last_size );
|
|---|
| 257 |
|
|---|
| 258 | __cfadbg_print_safe(runtime_core, "Kernel : core %p created\n", &this);
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | // Not a ctor, it just preps the destruction but should not destroy members
|
|---|
| 262 | void deinit(processor & this) {
|
|---|
| [320ec6fc] | 263 |
|
|---|
| 264 | int target = __atomic_sub_fetch( &this.cltr->nprocessors, 1u, __ATOMIC_SEQ_CST );
|
|---|
| 265 |
|
|---|
| [28d73c1] | 266 | // Lock the RWlock so no-one pushes/pops while we are changing the queue
|
|---|
| 267 | uint_fast32_t last_size = ready_mutate_lock();
|
|---|
| 268 |
|
|---|
| 269 | // Adjust the ready queue size
|
|---|
| [320ec6fc] | 270 | ready_queue_shrink( this.cltr, target );
|
|---|
| [28d73c1] | 271 |
|
|---|
| 272 | // Make sure we aren't on the idle queue
|
|---|
| 273 | unsafe_remove( this.cltr->idles, &this );
|
|---|
| 274 |
|
|---|
| 275 | // Unlock the RWlock
|
|---|
| 276 | ready_mutate_unlock( last_size );
|
|---|
| 277 |
|
|---|
| 278 | // Finally we don't need the read_lock any more
|
|---|
| 279 | unregister((__processor_id_t*)&this);
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | void ?{}(processor & this, const char name[], cluster & _cltr) {
|
|---|
| 283 | ( this.idle ){};
|
|---|
| 284 | ( this.terminated ){ 0 };
|
|---|
| 285 | ( this.runner ){};
|
|---|
| 286 | init( this, name, _cltr );
|
|---|
| [6b4cdd3] | 287 |
|
|---|
| [4069faad] | 288 | __cfadbg_print_safe(runtime_core, "Kernel : Starting core %p\n", &this);
|
|---|
| [8c50aed] | 289 |
|
|---|
| [c7a900a] | 290 | this.stack = __create_pthread( &this.kernel_thread, __invoke_processor, (void *)&this );
|
|---|
| [8c50aed] | 291 |
|
|---|
| [c84e80a] | 292 | }
|
|---|
| 293 |
|
|---|
| [65deb18] | 294 | void ^?{}(processor & this) with( this ){
|
|---|
| [ea8b2f7] | 295 | if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) {
|
|---|
| [4069faad] | 296 | __cfadbg_print_safe(runtime_core, "Kernel : core %p signaling termination\n", &this);
|
|---|
| [85b1deb] | 297 |
|
|---|
| 298 | __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED);
|
|---|
| [92e7631] | 299 | __wake_proc( &this );
|
|---|
| [85b1deb] | 300 |
|
|---|
| [65deb18] | 301 | P( terminated );
|
|---|
| [14a61b5] | 302 | verify( kernelTLS.this_processor != &this);
|
|---|
| [8def349] | 303 | }
|
|---|
| [6b4cdd3] | 304 |
|
|---|
| [c66f6cb] | 305 | int err = pthread_join( kernel_thread, 0p );
|
|---|
| 306 | if( err != 0 ) abort("KERNEL ERROR: joining processor %p caused error %s\n", &this, strerror(err));
|
|---|
| 307 |
|
|---|
| [27f5f71] | 308 | free( this.stack );
|
|---|
| [13c5e19] | 309 |
|
|---|
| [28d73c1] | 310 | deinit( this );
|
|---|
| [8def349] | 311 | }
|
|---|
| 312 |
|
|---|
| [f00b26d4] | 313 | void ?{}(cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params) with( this ) {
|
|---|
| [de6319f] | 314 | this.name = name;
|
|---|
| 315 | this.preemption_rate = preemption_rate;
|
|---|
| [13c5e19] | 316 | this.nprocessors = 0;
|
|---|
| [65deb18] | 317 | ready_queue{};
|
|---|
| [de94a60] | 318 |
|
|---|
| [038be32] | 319 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| [69fbc61] | 320 | print_stats = 0;
|
|---|
| [8834751] | 321 | stats = alloc();
|
|---|
| 322 | __init_stats( stats );
|
|---|
| [038be32] | 323 | #endif
|
|---|
| 324 |
|
|---|
| [a1a17a74] | 325 | threads{ __get };
|
|---|
| [de94a60] | 326 |
|
|---|
| 327 | doregister(this);
|
|---|
| [39fc03e] | 328 |
|
|---|
| 329 | // Lock the RWlock so no-one pushes/pops while we are changing the queue
|
|---|
| 330 | uint_fast32_t last_size = ready_mutate_lock();
|
|---|
| 331 |
|
|---|
| 332 | // Adjust the ready queue size
|
|---|
| 333 | ready_queue_grow( &this, 0 );
|
|---|
| 334 |
|
|---|
| 335 | // Unlock the RWlock
|
|---|
| 336 | ready_mutate_unlock( last_size );
|
|---|
| 337 |
|
|---|
| [f00b26d4] | 338 | this.io.cnt = num_io;
|
|---|
| 339 | this.io.ctxs = aalloc(num_io);
|
|---|
| 340 | for(i; this.io.cnt) {
|
|---|
| 341 | (this.io.ctxs[i]){ this, io_params };
|
|---|
| 342 | }
|
|---|
| [8def349] | 343 | }
|
|---|
| 344 |
|
|---|
| [242a902] | 345 | void ^?{}(cluster & this) {
|
|---|
| [f00b26d4] | 346 | for(i; this.io.cnt) {
|
|---|
| 347 | ^(this.io.ctxs[i]){ true };
|
|---|
| 348 | }
|
|---|
| 349 | free(this.io.ctxs);
|
|---|
| [92976d9] | 350 |
|
|---|
| [39fc03e] | 351 | // Lock the RWlock so no-one pushes/pops while we are changing the queue
|
|---|
| 352 | uint_fast32_t last_size = ready_mutate_lock();
|
|---|
| 353 |
|
|---|
| 354 | // Adjust the ready queue size
|
|---|
| 355 | ready_queue_shrink( &this, 0 );
|
|---|
| 356 |
|
|---|
| 357 | // Unlock the RWlock
|
|---|
| 358 | ready_mutate_unlock( last_size );
|
|---|
| 359 |
|
|---|
| [8834751] | 360 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| [69fbc61] | 361 | if( 0 != this.print_stats ) {
|
|---|
| 362 | __print_stats( this.stats, this.print_stats, true, this.name, (void*)&this );
|
|---|
| [8834751] | 363 | }
|
|---|
| 364 | free( this.stats );
|
|---|
| 365 | #endif
|
|---|
| 366 |
|
|---|
| [de94a60] | 367 | unregister(this);
|
|---|
| [c84e80a] | 368 | }
|
|---|
| 369 |
|
|---|
| [75f3522] | 370 | //=============================================================================================
|
|---|
| 371 | // Kernel Scheduling logic
|
|---|
| 372 | //=============================================================================================
|
|---|
| [8fcbb4c] | 373 | //Main of the processor contexts
|
|---|
| [83a071f9] | 374 | void main(processorCtx_t & runner) {
|
|---|
| [21184e3] | 375 | // Because of a bug, we couldn't initialized the seed on construction
|
|---|
| 376 | // Do it here
|
|---|
| [7768b8d] | 377 | kernelTLS.rand_seed ^= rdtscl();
|
|---|
| [21184e3] | 378 |
|
|---|
| [83a071f9] | 379 | processor * this = runner.proc;
|
|---|
| [094476d] | 380 | verify(this);
|
|---|
| [c81ebf9] | 381 |
|
|---|
| [4069faad] | 382 | __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this);
|
|---|
| [28d73c1] | 383 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| 384 | if( this->print_halts ) {
|
|---|
| 385 | __cfaabi_bits_print_safe( STDOUT_FILENO, "Processor : %d - %s (%p)\n", this->id, this->name, (void*)this);
|
|---|
| 386 | }
|
|---|
| 387 | #endif
|
|---|
| [b798713] | 388 |
|
|---|
| [75f3522] | 389 | {
|
|---|
| [c81ebf9] | 390 | // Setup preemption data
|
|---|
| 391 | preemption_scope scope = { this };
|
|---|
| 392 |
|
|---|
| [4069faad] | 393 | __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this);
|
|---|
| [8118303] | 394 |
|
|---|
| [ac2b598] | 395 | $thread * readyThread = 0p;
|
|---|
| [398e8e9] | 396 | for( unsigned int spin_count = 0;; spin_count++ ) {
|
|---|
| [92e7631] | 397 | // Try to get the next thread
|
|---|
| [8c50aed] | 398 | readyThread = __next_thread( this->cltr );
|
|---|
| [75f3522] | 399 |
|
|---|
| [92e7631] | 400 | // Check if we actually found a thread
|
|---|
| 401 | if( readyThread ) {
|
|---|
| [3381ed7] | 402 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [92e7631] | 403 | /* paranoid */ verifyf( readyThread->state == Ready || readyThread->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", readyThread->state, readyThread->preempted);
|
|---|
| [504a7dc] | 404 | /* paranoid */ verifyf( readyThread->link.next == 0p, "Expected null got %p", readyThread->link.next );
|
|---|
| [04b5cef] | 405 | __builtin_prefetch( readyThread->context.SP );
|
|---|
| [4e6fb8e] | 406 |
|
|---|
| [92e7631] | 407 | // We found a thread run it
|
|---|
| [8c50aed] | 408 | __run_thread(this, readyThread);
|
|---|
| [c81ebf9] | 409 |
|
|---|
| [3381ed7] | 410 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [c81ebf9] | 411 | }
|
|---|
| [398e8e9] | 412 |
|
|---|
| 413 | if(__atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST)) break;
|
|---|
| 414 |
|
|---|
| 415 | if( !readyThread ) {
|
|---|
| [64a7146] | 416 | // Block until a thread is ready
|
|---|
| 417 | __halt(this);
|
|---|
| 418 | }
|
|---|
| [c81ebf9] | 419 | }
|
|---|
| 420 |
|
|---|
| [4069faad] | 421 | __cfadbg_print_safe(runtime_core, "Kernel : core %p stopping\n", this);
|
|---|
| [c84e80a] | 422 | }
|
|---|
| [8118303] | 423 |
|
|---|
| [4cedd9f] | 424 | V( this->terminated );
|
|---|
| [bdeba0b] | 425 |
|
|---|
| [28d73c1] | 426 | if(this == mainProcessor) {
|
|---|
| [6a490b2] | 427 | // HACK : the coroutine context switch expects this_thread to be set
|
|---|
| 428 | // and it make sense for it to be set in all other cases except here
|
|---|
| 429 | // fake it
|
|---|
| 430 | kernelTLS.this_thread = mainThread;
|
|---|
| 431 | }
|
|---|
| [7768b8d] | 432 |
|
|---|
| [4069faad] | 433 | __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this);
|
|---|
| [c84e80a] | 434 | }
|
|---|
| 435 |
|
|---|
| [5c1a531] | 436 | static int * __volatile_errno() __attribute__((noinline));
|
|---|
| 437 | static int * __volatile_errno() { asm(""); return &errno; }
|
|---|
| 438 |
|
|---|
| [14a61b5] | 439 | // KERNEL ONLY
|
|---|
| [1c273d0] | 440 | // runThread runs a thread by context switching
|
|---|
| 441 | // from the processor coroutine to the target thread
|
|---|
| [ac2b598] | 442 | static void __run_thread(processor * this, $thread * thrd_dst) {
|
|---|
| 443 | $coroutine * proc_cor = get_coroutine(this->runner);
|
|---|
| [8fcbb4c] | 444 |
|
|---|
| [14a61b5] | 445 | // Update global state
|
|---|
| [e8e457e] | 446 | kernelTLS.this_thread = thrd_dst;
|
|---|
| 447 |
|
|---|
| [9f575ea] | 448 | // set state of processor coroutine to inactive
|
|---|
| 449 | verify(proc_cor->state == Active);
|
|---|
| [ae7be7a] | 450 | proc_cor->state = Blocked;
|
|---|
| [e8e457e] | 451 |
|
|---|
| [9f575ea] | 452 | // Actually run the thread
|
|---|
| [3381ed7] | 453 | RUNNING: while(true) {
|
|---|
| [ff79d5e] | 454 | thrd_dst->preempted = __NO_PREEMPTION;
|
|---|
| 455 | thrd_dst->state = Active;
|
|---|
| [e8e457e] | 456 |
|
|---|
| [ae66348] | 457 | __cfaabi_dbg_debug_do(
|
|---|
| [276ae57e] | 458 | thrd_dst->park_stale = true;
|
|---|
| 459 | thrd_dst->unpark_stale = true;
|
|---|
| [ae66348] | 460 | )
|
|---|
| [75f3522] | 461 |
|
|---|
| [3381ed7] | 462 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [a7b486b] | 463 | /* paranoid */ verify( kernelTLS.this_thread == thrd_dst );
|
|---|
| [210b8b3] | 464 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); // add escape condition if we are setting up the processor
|
|---|
| 465 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); // add escape condition if we are setting up the processor
|
|---|
| [3381ed7] | 466 |
|
|---|
| [9f575ea] | 467 | // set context switch to the thread that the processor is executing
|
|---|
| 468 | verify( thrd_dst->context.SP );
|
|---|
| [c7a900a] | 469 | __cfactx_switch( &proc_cor->context, &thrd_dst->context );
|
|---|
| 470 | // when __cfactx_switch returns we are back in the processor coroutine
|
|---|
| [9f575ea] | 471 |
|
|---|
| [210b8b3] | 472 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst );
|
|---|
| 473 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst );
|
|---|
| [a7b486b] | 474 | /* paranoid */ verify( kernelTLS.this_thread == thrd_dst );
|
|---|
| [3381ed7] | 475 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [75f3522] | 476 |
|
|---|
| [3381ed7] | 477 |
|
|---|
| 478 | // We just finished running a thread, there are a few things that could have happened.
|
|---|
| 479 | // 1 - Regular case : the thread has blocked and now one has scheduled it yet.
|
|---|
| 480 | // 2 - Racy case : the thread has blocked but someone has already tried to schedule it.
|
|---|
| 481 | // 4 - Preempted
|
|---|
| 482 | // In case 1, we may have won a race so we can't write to the state again.
|
|---|
| 483 | // In case 2, we lost the race so we now own the thread.
|
|---|
| 484 |
|
|---|
| 485 | if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) {
|
|---|
| 486 | // The thread was preempted, reschedule it and reset the flag
|
|---|
| [9b1dcc2] | 487 | __schedule_thread( (__processor_id_t*)this, thrd_dst );
|
|---|
| [3381ed7] | 488 | break RUNNING;
|
|---|
| 489 | }
|
|---|
| [75f3522] | 490 |
|
|---|
| [ff79d5e] | 491 | if(unlikely(thrd_dst->state == Halted)) {
|
|---|
| 492 | // The thread has halted, it should never be scheduled/run again
|
|---|
| 493 | // We may need to wake someone up here since
|
|---|
| 494 | unpark( this->destroyer __cfaabi_dbg_ctx2 );
|
|---|
| 495 | this->destroyer = 0p;
|
|---|
| 496 | break RUNNING;
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | /* paranoid */ verify( thrd_dst->state == Active );
|
|---|
| 500 | thrd_dst->state = Blocked;
|
|---|
| 501 |
|
|---|
| [3381ed7] | 502 | // set state of processor coroutine to active and the thread to inactive
|
|---|
| [ff79d5e] | 503 | int old_ticket = __atomic_fetch_sub(&thrd_dst->ticket, 1, __ATOMIC_SEQ_CST);
|
|---|
| 504 | __cfaabi_dbg_debug_do( thrd_dst->park_result = old_ticket; )
|
|---|
| 505 | switch(old_ticket) {
|
|---|
| 506 | case 1:
|
|---|
| [3381ed7] | 507 | // This is case 1, the regular case, nothing more is needed
|
|---|
| 508 | break RUNNING;
|
|---|
| [ff79d5e] | 509 | case 2:
|
|---|
| [3381ed7] | 510 | // This is case 2, the racy case, someone tried to run this thread before it finished blocking
|
|---|
| 511 | // In this case, just run it again.
|
|---|
| 512 | continue RUNNING;
|
|---|
| 513 | default:
|
|---|
| 514 | // This makes no sense, something is wrong abort
|
|---|
| [ff79d5e] | 515 | abort();
|
|---|
| [3381ed7] | 516 | }
|
|---|
| [9f575ea] | 517 | }
|
|---|
| [e8e457e] | 518 |
|
|---|
| [9f575ea] | 519 | // Just before returning to the processor, set the processor coroutine to active
|
|---|
| [e8e457e] | 520 | proc_cor->state = Active;
|
|---|
| [ae7be7a] | 521 | kernelTLS.this_thread = 0p;
|
|---|
| [82c948c] | 522 | }
|
|---|
| 523 |
|
|---|
| [14a61b5] | 524 | // KERNEL_ONLY
|
|---|
| [b0c7419] | 525 | void returnToKernel() {
|
|---|
| [3381ed7] | 526 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [ac2b598] | 527 | $coroutine * proc_cor = get_coroutine(kernelTLS.this_processor->runner);
|
|---|
| 528 | $thread * thrd_src = kernelTLS.this_thread;
|
|---|
| [e8e457e] | 529 |
|
|---|
| [29cb302] | 530 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| 531 | struct processor * last_proc = kernelTLS.this_processor;
|
|---|
| 532 | #endif
|
|---|
| 533 |
|
|---|
| [9f575ea] | 534 | // Run the thread on this processor
|
|---|
| 535 | {
|
|---|
| 536 | int local_errno = *__volatile_errno();
|
|---|
| 537 | #if defined( __i386 ) || defined( __x86_64 )
|
|---|
| 538 | __x87_store;
|
|---|
| 539 | #endif
|
|---|
| 540 | verify( proc_cor->context.SP );
|
|---|
| [c7a900a] | 541 | __cfactx_switch( &thrd_src->context, &proc_cor->context );
|
|---|
| [9f575ea] | 542 | #if defined( __i386 ) || defined( __x86_64 )
|
|---|
| 543 | __x87_load;
|
|---|
| 544 | #endif
|
|---|
| 545 | *__volatile_errno() = local_errno;
|
|---|
| [8fcbb4c] | 546 | }
|
|---|
| [deca0f5] | 547 |
|
|---|
| [29cb302] | 548 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| 549 | if(last_proc != kernelTLS.this_processor) {
|
|---|
| 550 | __tls_stats()->ready.threads.migration++;
|
|---|
| 551 | }
|
|---|
| 552 | #endif
|
|---|
| 553 |
|
|---|
| [3381ed7] | 554 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [210b8b3] | 555 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) < ((uintptr_t)__get_stack(thrd_src->curr_cor)->base ), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too small.\n", thrd_src );
|
|---|
| 556 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) > ((uintptr_t)__get_stack(thrd_src->curr_cor)->limit), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too large.\n", thrd_src );
|
|---|
| [c84e80a] | 557 | }
|
|---|
| 558 |
|
|---|
| [14a61b5] | 559 | // KERNEL_ONLY
|
|---|
| [0c92c9f] | 560 | // Context invoker for processors
|
|---|
| 561 | // This is the entry point for processors (kernel threads)
|
|---|
| 562 | // It effectively constructs a coroutine by stealing the pthread stack
|
|---|
| [c7a900a] | 563 | static void * __invoke_processor(void * arg) {
|
|---|
| [8834751] | 564 | #if !defined( __CFA_NO_STATISTICS__ )
|
|---|
| 565 | __stats_t local_stats;
|
|---|
| 566 | __init_stats( &local_stats );
|
|---|
| 567 | kernelTLS.this_stats = &local_stats;
|
|---|
| 568 | #endif
|
|---|
| 569 |
|
|---|
| [8def349] | 570 | processor * proc = (processor *) arg;
|
|---|
| [14a61b5] | 571 | kernelTLS.this_processor = proc;
|
|---|
| [1805b1b] | 572 | kernelTLS.this_thread = 0p;
|
|---|
| [14a61b5] | 573 | kernelTLS.preemption_state.[enabled, disable_count] = [false, 1];
|
|---|
| [8def349] | 574 | // SKULLDUGGERY: We want to create a context for the processor coroutine
|
|---|
| 575 | // which is needed for the 2-step context switch. However, there is no reason
|
|---|
| [1c273d0] | 576 | // to waste the perfectly valid stack create by pthread.
|
|---|
| [8def349] | 577 | current_stack_info_t info;
|
|---|
| [b2f6113] | 578 | __stack_t ctx;
|
|---|
| 579 | info.storage = &ctx;
|
|---|
| [094476d] | 580 | (proc->runner){ proc, &info };
|
|---|
| [8def349] | 581 |
|
|---|
| [b2f6113] | 582 | __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage);
|
|---|
| [8fcbb4c] | 583 |
|
|---|
| [0c92c9f] | 584 | //Set global state
|
|---|
| [1805b1b] | 585 | kernelTLS.this_thread = 0p;
|
|---|
| [8def349] | 586 |
|
|---|
| 587 | //We now have a proper context from which to schedule threads
|
|---|
| [4069faad] | 588 | __cfadbg_print_safe(runtime_core, "Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx);
|
|---|
| [8def349] | 589 |
|
|---|
| [1c273d0] | 590 | // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
|
|---|
| 591 | // resume it to start it like it normally would, it will just context switch
|
|---|
| 592 | // back to here. Instead directly call the main since we already are on the
|
|---|
| [8def349] | 593 | // appropriate stack.
|
|---|
| [094476d] | 594 | get_coroutine(proc->runner)->state = Active;
|
|---|
| 595 | main( proc->runner );
|
|---|
| 596 | get_coroutine(proc->runner)->state = Halted;
|
|---|
| [8def349] | 597 |
|
|---|
| [0c92c9f] | 598 | // Main routine of the core returned, the core is now fully terminated
|
|---|
| [4069faad] | 599 | __cfadbg_print_safe(runtime_core, "Kernel : core %p main ended (%p)\n", proc, &proc->runner);
|
|---|
| [8def349] | 600 |
|
|---|
| [8834751] | 601 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| 602 | __tally_stats(proc->cltr->stats, &local_stats);
|
|---|
| [69fbc61] | 603 | if( 0 != proc->print_stats ) {
|
|---|
| 604 | __print_stats( &local_stats, proc->print_stats, true, proc->name, (void*)proc );
|
|---|
| [c34ebf2] | 605 | }
|
|---|
| [8834751] | 606 | #endif
|
|---|
| 607 |
|
|---|
| [1805b1b] | 608 | return 0p;
|
|---|
| [c84e80a] | 609 | }
|
|---|
| 610 |
|
|---|
| [e3fea42] | 611 | static void Abort( int ret, const char func[] ) {
|
|---|
| [1a3040c] | 612 | if ( ret ) { // pthread routines return errno values
|
|---|
| [1805b1b] | 613 | abort( "%s : internal error, error(%d) %s.", func, ret, strerror( ret ) );
|
|---|
| [27f5f71] | 614 | } // if
|
|---|
| [1805b1b] | 615 | } // Abort
|
|---|
| 616 |
|
|---|
| [8c50aed] | 617 | void * __create_pthread( pthread_t * pthread, void * (*start)(void *), void * arg ) {
|
|---|
| [1805b1b] | 618 | pthread_attr_t attr;
|
|---|
| 619 |
|
|---|
| 620 | Abort( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
|
|---|
| 621 |
|
|---|
| [27f5f71] | 622 | size_t stacksize;
|
|---|
| [09d4b22] | 623 | // default stack size, normally defined by shell limit
|
|---|
| [1a3040c] | 624 | Abort( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
|
|---|
| [27f5f71] | 625 | assert( stacksize >= PTHREAD_STACK_MIN );
|
|---|
| [1a3040c] | 626 |
|
|---|
| [09d4b22] | 627 | void * stack;
|
|---|
| 628 | __cfaabi_dbg_debug_do(
|
|---|
| 629 | stack = memalign( __page_size, stacksize + __page_size );
|
|---|
| 630 | // pthread has no mechanism to create the guard page in user supplied stack.
|
|---|
| 631 | if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
|
|---|
| 632 | abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
|
|---|
| 633 | } // if
|
|---|
| 634 | );
|
|---|
| 635 | __cfaabi_dbg_no_debug_do(
|
|---|
| 636 | stack = malloc( stacksize );
|
|---|
| 637 | );
|
|---|
| [1a3040c] | 638 |
|
|---|
| [f80f840] | 639 | Abort( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
|
|---|
| [27f5f71] | 640 |
|
|---|
| [1805b1b] | 641 | Abort( pthread_create( pthread, &attr, start, arg ), "pthread_create" );
|
|---|
| 642 | return stack;
|
|---|
| [c84e80a] | 643 | }
|
|---|
| 644 |
|
|---|
| [14a61b5] | 645 | // KERNEL_ONLY
|
|---|
| [8c50aed] | 646 | static void __kernel_first_resume( processor * this ) {
|
|---|
| [ac2b598] | 647 | $thread * src = mainThread;
|
|---|
| 648 | $coroutine * dst = get_coroutine(this->runner);
|
|---|
| [b69ea6b] | 649 |
|
|---|
| [14a61b5] | 650 | verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [b69ea6b] | 651 |
|
|---|
| [09f357ec] | 652 | kernelTLS.this_thread->curr_cor = dst;
|
|---|
| [b2f6113] | 653 | __stack_prepare( &dst->stack, 65000 );
|
|---|
| [c7a900a] | 654 | __cfactx_start(main, dst, this->runner, __cfactx_invoke_coroutine);
|
|---|
| [b69ea6b] | 655 |
|
|---|
| [14a61b5] | 656 | verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [b69ea6b] | 657 |
|
|---|
| [e8e457e] | 658 | dst->last = &src->self_cor;
|
|---|
| 659 | dst->starter = dst->starter ? dst->starter : &src->self_cor;
|
|---|
| [b69ea6b] | 660 |
|
|---|
| [92e7631] | 661 | // make sure the current state is still correct
|
|---|
| 662 | /* paranoid */ verify(src->state == Ready);
|
|---|
| [b69ea6b] | 663 |
|
|---|
| 664 | // context switch to specified coroutine
|
|---|
| [69a61d2] | 665 | verify( dst->context.SP );
|
|---|
| [c7a900a] | 666 | __cfactx_switch( &src->context, &dst->context );
|
|---|
| 667 | // when __cfactx_switch returns we are back in the src coroutine
|
|---|
| [b69ea6b] | 668 |
|
|---|
| [09f357ec] | 669 | mainThread->curr_cor = &mainThread->self_cor;
|
|---|
| 670 |
|
|---|
| [92e7631] | 671 | // make sure the current state has been update
|
|---|
| 672 | /* paranoid */ verify(src->state == Active);
|
|---|
| [b69ea6b] | 673 |
|
|---|
| [14a61b5] | 674 | verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [b69ea6b] | 675 | }
|
|---|
| 676 |
|
|---|
| [e8e457e] | 677 | // KERNEL_ONLY
|
|---|
| [8c50aed] | 678 | static void __kernel_last_resume( processor * this ) {
|
|---|
| [ac2b598] | 679 | $coroutine * src = &mainThread->self_cor;
|
|---|
| 680 | $coroutine * dst = get_coroutine(this->runner);
|
|---|
| [e8e457e] | 681 |
|
|---|
| 682 | verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| 683 | verify( dst->starter == src );
|
|---|
| 684 | verify( dst->context.SP );
|
|---|
| 685 |
|
|---|
| [f586539] | 686 | // SKULLDUGGERY in debug the processors check that the
|
|---|
| 687 | // stack is still within the limit of the stack limits after running a thread.
|
|---|
| 688 | // that check doesn't make sense if we context switch to the processor using the
|
|---|
| 689 | // coroutine semantics. Since this is a special case, use the current context
|
|---|
| 690 | // info to populate these fields.
|
|---|
| 691 | __cfaabi_dbg_debug_do(
|
|---|
| 692 | __stack_context_t ctx;
|
|---|
| 693 | CtxGet( ctx );
|
|---|
| 694 | mainThread->context.SP = ctx.SP;
|
|---|
| 695 | mainThread->context.FP = ctx.FP;
|
|---|
| 696 | )
|
|---|
| 697 |
|
|---|
| [e8e457e] | 698 | // context switch to the processor
|
|---|
| [c7a900a] | 699 | __cfactx_switch( &src->context, &dst->context );
|
|---|
| [e8e457e] | 700 | }
|
|---|
| 701 |
|
|---|
| [8def349] | 702 | //-----------------------------------------------------------------------------
|
|---|
| 703 | // Scheduler routines
|
|---|
| [14a61b5] | 704 | // KERNEL ONLY
|
|---|
| [9b1dcc2] | 705 | void __schedule_thread( struct __processor_id_t * id, $thread * thrd ) {
|
|---|
| [6a490b2] | 706 | /* paranoid */ verify( thrd );
|
|---|
| 707 | /* paranoid */ verify( thrd->state != Halted );
|
|---|
| [9f575ea] | 708 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [3381ed7] | 709 | /* paranoid */ #if defined( __CFA_WITH_VERIFY__ )
|
|---|
| [504a7dc] | 710 | /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION,
|
|---|
| 711 | "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted );
|
|---|
| [ff79d5e] | 712 | /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active,
|
|---|
| [504a7dc] | 713 | "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted );
|
|---|
| [3381ed7] | 714 | /* paranoid */ #endif
|
|---|
| [6a490b2] | 715 | /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next );
|
|---|
| [9f575ea] | 716 |
|
|---|
| [ae7be7a] | 717 | if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready;
|
|---|
| [6b4cdd3] | 718 |
|
|---|
| [9b1dcc2] | 719 | ready_schedule_lock ( id );
|
|---|
| [504a7dc] | 720 | push( thrd->curr_cluster, thrd );
|
|---|
| [b798713] | 721 |
|
|---|
| [68f36f4] | 722 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| 723 | bool woke =
|
|---|
| 724 | #endif
|
|---|
| 725 | __wake_one(id, thrd->curr_cluster);
|
|---|
| 726 |
|
|---|
| 727 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| 728 | if(woke) __tls_stats()->ready.sleep.wakes++;
|
|---|
| 729 | #endif
|
|---|
| [9b1dcc2] | 730 | ready_schedule_unlock( id );
|
|---|
| [1c273d0] | 731 |
|
|---|
| [9f575ea] | 732 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [db6f06a] | 733 | }
|
|---|
| 734 |
|
|---|
| [14a61b5] | 735 | // KERNEL ONLY
|
|---|
| [ac2b598] | 736 | static $thread * __next_thread(cluster * this) with( *this ) {
|
|---|
| [3381ed7] | 737 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [7768b8d] | 738 |
|
|---|
| [9b1dcc2] | 739 | ready_schedule_lock ( (__processor_id_t*)kernelTLS.this_processor );
|
|---|
| [6a490b2] | 740 | $thread * head = pop( this );
|
|---|
| [9b1dcc2] | 741 | ready_schedule_unlock( (__processor_id_t*)kernelTLS.this_processor );
|
|---|
| [7768b8d] | 742 |
|
|---|
| [3381ed7] | 743 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [db6f06a] | 744 | return head;
|
|---|
| [eb2e723] | 745 | }
|
|---|
| 746 |
|
|---|
| [64a7146] | 747 | // KERNEL ONLY
|
|---|
| 748 | static bool __has_next_thread(cluster * this) with( *this ) {
|
|---|
| 749 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| 750 |
|
|---|
| 751 | ready_schedule_lock ( (__processor_id_t*)kernelTLS.this_processor );
|
|---|
| 752 | bool not_empty = query( this );
|
|---|
| 753 | ready_schedule_unlock( (__processor_id_t*)kernelTLS.this_processor );
|
|---|
| 754 |
|
|---|
| 755 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| 756 | return not_empty;
|
|---|
| 757 | }
|
|---|
| 758 |
|
|---|
| [2d8f7b0] | 759 | // KERNEL ONLY unpark with out disabling interrupts
|
|---|
| [9b1dcc2] | 760 | void __unpark( struct __processor_id_t * id, $thread * thrd __cfaabi_dbg_ctx_param2 ) {
|
|---|
| [ae66348] | 761 | // record activity
|
|---|
| 762 | __cfaabi_dbg_record_thrd( *thrd, false, caller );
|
|---|
| 763 |
|
|---|
| [ff79d5e] | 764 | int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST);
|
|---|
| 765 | __cfaabi_dbg_debug_do( thrd->unpark_result = old_ticket; thrd->unpark_state = thrd->state; )
|
|---|
| 766 | switch(old_ticket) {
|
|---|
| 767 | case 1:
|
|---|
| [3381ed7] | 768 | // Wake won the race, the thread will reschedule/rerun itself
|
|---|
| 769 | break;
|
|---|
| [ff79d5e] | 770 | case 0:
|
|---|
| [3381ed7] | 771 | /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION );
|
|---|
| [ff79d5e] | 772 | /* paranoid */ verify( thrd->state == Blocked );
|
|---|
| [0b33412] | 773 |
|
|---|
| [3381ed7] | 774 | // Wake lost the race,
|
|---|
| [9b1dcc2] | 775 | __schedule_thread( id, thrd );
|
|---|
| [3381ed7] | 776 | break;
|
|---|
| 777 | default:
|
|---|
| 778 | // This makes no sense, something is wrong abort
|
|---|
| 779 | abort();
|
|---|
| [de6319f] | 780 | }
|
|---|
| [db6f06a] | 781 | }
|
|---|
| 782 |
|
|---|
| [2d8f7b0] | 783 | void unpark( $thread * thrd __cfaabi_dbg_ctx_param2 ) {
|
|---|
| 784 | if( !thrd ) return;
|
|---|
| [db6f06a] | 785 |
|
|---|
| [82ff5845] | 786 | disable_interrupts();
|
|---|
| [9b1dcc2] | 787 | __unpark( (__processor_id_t*)kernelTLS.this_processor, thrd __cfaabi_dbg_ctx_fwd2 );
|
|---|
| [36982fc] | 788 | enable_interrupts( __cfaabi_dbg_ctx );
|
|---|
| [eb2e723] | 789 | }
|
|---|
| 790 |
|
|---|
| [ae66348] | 791 | void park( __cfaabi_dbg_ctx_param ) {
|
|---|
| [3381ed7] | 792 | /* paranoid */ verify( kernelTLS.preemption_state.enabled );
|
|---|
| [82ff5845] | 793 | disable_interrupts();
|
|---|
| [3381ed7] | 794 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| 795 | /* paranoid */ verify( kernelTLS.this_thread->preempted == __NO_PREEMPTION );
|
|---|
| [0b33412] | 796 |
|
|---|
| [ae66348] | 797 | // record activity
|
|---|
| 798 | __cfaabi_dbg_record_thrd( *kernelTLS.this_thread, true, caller );
|
|---|
| [0b33412] | 799 |
|
|---|
| [82c948c] | 800 | returnToKernel();
|
|---|
| [0b33412] | 801 |
|
|---|
| [3381ed7] | 802 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [36982fc] | 803 | enable_interrupts( __cfaabi_dbg_ctx );
|
|---|
| [3381ed7] | 804 | /* paranoid */ verify( kernelTLS.preemption_state.enabled );
|
|---|
| [0c78741] | 805 |
|
|---|
| 806 | }
|
|---|
| [09800e9] | 807 |
|
|---|
| [3381ed7] | 808 | // KERNEL ONLY
|
|---|
| [b0c7419] | 809 | void __leave_thread() {
|
|---|
| [3381ed7] | 810 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [09800e9] | 811 | returnToKernel();
|
|---|
| [b0c7419] | 812 | abort();
|
|---|
| [09800e9] | 813 | }
|
|---|
| 814 |
|
|---|
| [14a61b5] | 815 | // KERNEL ONLY
|
|---|
| [3381ed7] | 816 | bool force_yield( __Preemption_Reason reason ) {
|
|---|
| 817 | /* paranoid */ verify( kernelTLS.preemption_state.enabled );
|
|---|
| [09800e9] | 818 | disable_interrupts();
|
|---|
| [3381ed7] | 819 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [09800e9] | 820 |
|
|---|
| [ac2b598] | 821 | $thread * thrd = kernelTLS.this_thread;
|
|---|
| [ff79d5e] | 822 | /* paranoid */ verify(thrd->state == Active);
|
|---|
| [3381ed7] | 823 |
|
|---|
| 824 | // SKULLDUGGERY: It is possible that we are preempting this thread just before
|
|---|
| 825 | // it was going to park itself. If that is the case and it is already using the
|
|---|
| 826 | // intrusive fields then we can't use them to preempt the thread
|
|---|
| 827 | // If that is the case, abandon the preemption.
|
|---|
| 828 | bool preempted = false;
|
|---|
| [504a7dc] | 829 | if(thrd->link.next == 0p) {
|
|---|
| [3381ed7] | 830 | preempted = true;
|
|---|
| 831 | thrd->preempted = reason;
|
|---|
| 832 | returnToKernel();
|
|---|
| [de6319f] | 833 | }
|
|---|
| [f2b12406] | 834 |
|
|---|
| [3381ed7] | 835 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| 836 | enable_interrupts_noPoll();
|
|---|
| 837 | /* paranoid */ verify( kernelTLS.preemption_state.enabled );
|
|---|
| [f2b12406] | 838 |
|
|---|
| [3381ed7] | 839 | return preempted;
|
|---|
| [f2b12406] | 840 | }
|
|---|
| 841 |
|
|---|
| [fa21ac9] | 842 | //=============================================================================================
|
|---|
| 843 | // Kernel Setup logic
|
|---|
| 844 | //=============================================================================================
|
|---|
| [eb2e723] | 845 | //-----------------------------------------------------------------------------
|
|---|
| 846 | // Kernel boot procedures
|
|---|
| [8c50aed] | 847 | static void __kernel_startup(void) {
|
|---|
| [14a61b5] | 848 | verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [4069faad] | 849 | __cfadbg_print_safe(runtime_core, "Kernel : Starting\n");
|
|---|
| [eb2e723] | 850 |
|
|---|
| [b2f6113] | 851 | __page_size = sysconf( _SC_PAGESIZE );
|
|---|
| 852 |
|
|---|
| [ea8b2f7] | 853 | __cfa_dbg_global_clusters.list{ __get };
|
|---|
| 854 | __cfa_dbg_global_clusters.lock{};
|
|---|
| [de94a60] | 855 |
|
|---|
| [b388ee81] | 856 | // Initialize the global scheduler lock
|
|---|
| 857 | __scheduler_lock = (__scheduler_RWLock_t*)&storage___scheduler_lock;
|
|---|
| 858 | (*__scheduler_lock){};
|
|---|
| 859 |
|
|---|
| [de6319f] | 860 | // Initialize the main cluster
|
|---|
| 861 | mainCluster = (cluster *)&storage_mainCluster;
|
|---|
| [f00b26d4] | 862 | (*mainCluster){"Main Cluster", 0};
|
|---|
| [de6319f] | 863 |
|
|---|
| [4069faad] | 864 | __cfadbg_print_safe(runtime_core, "Kernel : Main cluster ready\n");
|
|---|
| [de6319f] | 865 |
|
|---|
| [eb2e723] | 866 | // Start by initializing the main thread
|
|---|
| [1c273d0] | 867 | // SKULLDUGGERY: the mainThread steals the process main thread
|
|---|
| [969b3fe] | 868 | // which will then be scheduled by the mainProcessor normally
|
|---|
| [ac2b598] | 869 | mainThread = ($thread *)&storage_mainThread;
|
|---|
| [8fcbb4c] | 870 | current_stack_info_t info;
|
|---|
| [b2f6113] | 871 | info.storage = (__stack_t*)&storage_mainThreadCtx;
|
|---|
| [83a071f9] | 872 | (*mainThread){ &info };
|
|---|
| [eb2e723] | 873 |
|
|---|
| [4069faad] | 874 | __cfadbg_print_safe(runtime_core, "Kernel : Main thread ready\n");
|
|---|
| [fa21ac9] | 875 |
|
|---|
| [bd98b58] | 876 |
|
|---|
| [de6319f] | 877 |
|
|---|
| 878 | // Construct the processor context of the main processor
|
|---|
| 879 | void ?{}(processorCtx_t & this, processor * proc) {
|
|---|
| 880 | (this.__cor){ "Processor" };
|
|---|
| [1805b1b] | 881 | this.__cor.starter = 0p;
|
|---|
| [de6319f] | 882 | this.proc = proc;
|
|---|
| 883 | }
|
|---|
| 884 |
|
|---|
| 885 | void ?{}(processor & this) with( this ) {
|
|---|
| [28d73c1] | 886 | ( this.idle ){};
|
|---|
| 887 | ( this.terminated ){ 0 };
|
|---|
| 888 | ( this.runner ){};
|
|---|
| 889 | init( this, "Main Processor", *mainCluster );
|
|---|
| [de6319f] | 890 | kernel_thread = pthread_self();
|
|---|
| [c34ebf2] | 891 |
|
|---|
| [de6319f] | 892 | runner{ &this };
|
|---|
| [4069faad] | 893 | __cfadbg_print_safe(runtime_core, "Kernel : constructed main processor context %p\n", &runner);
|
|---|
| [de6319f] | 894 | }
|
|---|
| [fa21ac9] | 895 |
|
|---|
| [969b3fe] | 896 | // Initialize the main processor and the main processor ctx
|
|---|
| [eb2e723] | 897 | // (the coroutine that contains the processing control flow)
|
|---|
| [969b3fe] | 898 | mainProcessor = (processor *)&storage_mainProcessor;
|
|---|
| [de6319f] | 899 | (*mainProcessor){};
|
|---|
| [eb2e723] | 900 |
|
|---|
| [dcb42b8] | 901 | //initialize the global state variables
|
|---|
| [14a61b5] | 902 | kernelTLS.this_processor = mainProcessor;
|
|---|
| 903 | kernelTLS.this_thread = mainThread;
|
|---|
| [eb2e723] | 904 |
|
|---|
| [8834751] | 905 | #if !defined( __CFA_NO_STATISTICS__ )
|
|---|
| 906 | kernelTLS.this_stats = (__stats_t *)& storage_mainProcStats;
|
|---|
| 907 | __init_stats( kernelTLS.this_stats );
|
|---|
| 908 | #endif
|
|---|
| 909 |
|
|---|
| [f00b26d4] | 910 | // Start IO
|
|---|
| 911 | __kernel_io_startup();
|
|---|
| 912 |
|
|---|
| [82ff5845] | 913 | // Enable preemption
|
|---|
| 914 | kernel_start_preemption();
|
|---|
| 915 |
|
|---|
| [969b3fe] | 916 | // Add the main thread to the ready queue
|
|---|
| 917 | // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
|
|---|
| [9b1dcc2] | 918 | __schedule_thread((__processor_id_t *)mainProcessor, mainThread);
|
|---|
| [969b3fe] | 919 |
|
|---|
| 920 | // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
|
|---|
| [c7a900a] | 921 | // context. Hence, the main thread does not begin through __cfactx_invoke_thread, like all other threads. The trick here is that
|
|---|
| [1c273d0] | 922 | // mainThread is on the ready queue when this call is made.
|
|---|
| [8c50aed] | 923 | __kernel_first_resume( kernelTLS.this_processor );
|
|---|
| [dcb42b8] | 924 |
|
|---|
| 925 |
|
|---|
| 926 | // THE SYSTEM IS NOW COMPLETELY RUNNING
|
|---|
| [f6660520] | 927 |
|
|---|
| 928 |
|
|---|
| 929 | // Now that the system is up, finish creating systems that need threading
|
|---|
| [f00b26d4] | 930 | mainCluster->io.ctxs = (io_context *)&storage_mainPollerThread;
|
|---|
| 931 | mainCluster->io.cnt = 1;
|
|---|
| 932 | (*mainCluster->io.ctxs){ *mainCluster };
|
|---|
| [f6660520] | 933 |
|
|---|
| 934 | __cfadbg_print_safe(runtime_core, "Kernel : Started\n--------------------------------------------------\n\n");
|
|---|
| [82ff5845] | 935 |
|
|---|
| [14a61b5] | 936 | verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [36982fc] | 937 | enable_interrupts( __cfaabi_dbg_ctx );
|
|---|
| [afd550c] | 938 | verify( TL_GET( preemption_state.enabled ) );
|
|---|
| [eb2e723] | 939 | }
|
|---|
| 940 |
|
|---|
| [8c50aed] | 941 | static void __kernel_shutdown(void) {
|
|---|
| [f6660520] | 942 | //Before we start shutting things down, wait for systems that need threading to shutdown
|
|---|
| [f00b26d4] | 943 | ^(*mainCluster->io.ctxs){};
|
|---|
| 944 | mainCluster->io.cnt = 0;
|
|---|
| 945 | mainCluster->io.ctxs = 0p;
|
|---|
| [eb2e723] | 946 |
|
|---|
| [92e7631] | 947 | /* paranoid */ verify( TL_GET( preemption_state.enabled ) );
|
|---|
| [4e6fb8e] | 948 | disable_interrupts();
|
|---|
| [92e7631] | 949 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [4e6fb8e] | 950 |
|
|---|
| [f6660520] | 951 | __cfadbg_print_safe(runtime_core, "\n--------------------------------------------------\nKernel : Shutting down\n");
|
|---|
| [4e6fb8e] | 952 |
|
|---|
| [969b3fe] | 953 | // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
|
|---|
| [dcb42b8] | 954 | // When its coroutine terminates, it return control to the mainThread
|
|---|
| 955 | // which is currently here
|
|---|
| [ea8b2f7] | 956 | __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);
|
|---|
| [8c50aed] | 957 | __kernel_last_resume( kernelTLS.this_processor );
|
|---|
| [ea8b2f7] | 958 | mainThread->self_cor.state = Halted;
|
|---|
| [eb2e723] | 959 |
|
|---|
| [dcb42b8] | 960 | // THE SYSTEM IS NOW COMPLETELY STOPPED
|
|---|
| [eb2e723] | 961 |
|
|---|
| [82ff5845] | 962 | // Disable preemption
|
|---|
| 963 | kernel_stop_preemption();
|
|---|
| 964 |
|
|---|
| [f00b26d4] | 965 | // Stop IO
|
|---|
| 966 | __kernel_io_shutdown();
|
|---|
| 967 |
|
|---|
| [969b3fe] | 968 | // Destroy the main processor and its context in reverse order of construction
|
|---|
| [dcb42b8] | 969 | // These were manually constructed so we need manually destroy them
|
|---|
| [c66f6cb] | 970 | void ^?{}(processor & this) with( this ){
|
|---|
| [28d73c1] | 971 | deinit( this );
|
|---|
| 972 |
|
|---|
| [c66f6cb] | 973 | /* paranoid */ verify( this.do_terminate == true );
|
|---|
| [b798713] | 974 | __cfaabi_dbg_print_safe("Kernel : destroyed main processor context %p\n", &runner);
|
|---|
| 975 | }
|
|---|
| 976 |
|
|---|
| [7768b8d] | 977 | ^(*mainProcessor){};
|
|---|
| [eb2e723] | 978 |
|
|---|
| [dcb42b8] | 979 | // Final step, destroy the main thread since it is no longer needed
|
|---|
| [6a490b2] | 980 |
|
|---|
| [dcb42b8] | 981 | // Since we provided a stack to this taxk it will not destroy anything
|
|---|
| [210b8b3] | 982 | /* paranoid */ verify(mainThread->self_cor.stack.storage == (__stack_t*)(((uintptr_t)&storage_mainThreadCtx)| 0x1));
|
|---|
| [7768b8d] | 983 | ^(*mainThread){};
|
|---|
| 984 |
|
|---|
| 985 | ^(*mainCluster){};
|
|---|
| [eb2e723] | 986 |
|
|---|
| [b388ee81] | 987 | ^(*__scheduler_lock){};
|
|---|
| 988 |
|
|---|
| [ea8b2f7] | 989 | ^(__cfa_dbg_global_clusters.list){};
|
|---|
| 990 | ^(__cfa_dbg_global_clusters.lock){};
|
|---|
| [a1a17a74] | 991 |
|
|---|
| [4069faad] | 992 | __cfadbg_print_safe(runtime_core, "Kernel : Shutdown complete\n");
|
|---|
| [9d944b2] | 993 | }
|
|---|
| 994 |
|
|---|
| [14a61b5] | 995 | //=============================================================================================
|
|---|
| [92e7631] | 996 | // Kernel Idle Sleep
|
|---|
| [14a61b5] | 997 | //=============================================================================================
|
|---|
| [64a7146] | 998 | // Wake a thread from the front if there are any
|
|---|
| 999 | static bool __wake_one(struct __processor_id_t * id, cluster * this) {
|
|---|
| 1000 | /* paranoid */ verify( ready_schedule_islocked( id ) );
|
|---|
| [14a61b5] | 1001 |
|
|---|
| [64a7146] | 1002 | // Check if there is a sleeping processor
|
|---|
| 1003 | processor * p = pop(this->idles);
|
|---|
| [14a61b5] | 1004 |
|
|---|
| [64a7146] | 1005 | // If no one is sleeping, we are done
|
|---|
| 1006 | if( 0p == p ) return false;
|
|---|
| [14a61b5] | 1007 |
|
|---|
| [64a7146] | 1008 | // We found a processor, wake it up
|
|---|
| 1009 | post( p->idle );
|
|---|
| [14a61b5] | 1010 |
|
|---|
| [64a7146] | 1011 | return true;
|
|---|
| 1012 | }
|
|---|
| [14a61b5] | 1013 |
|
|---|
| [64a7146] | 1014 | // Unconditionnaly wake a thread
|
|---|
| 1015 | static bool __wake_proc(processor * this) {
|
|---|
| 1016 | __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this);
|
|---|
| [14a61b5] | 1017 |
|
|---|
| [64a7146] | 1018 | disable_interrupts();
|
|---|
| 1019 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| 1020 | bool ret = post( this->idle );
|
|---|
| 1021 | enable_interrupts( __cfaabi_dbg_ctx );
|
|---|
| [92e7631] | 1022 |
|
|---|
| [64a7146] | 1023 | return ret;
|
|---|
| [92e7631] | 1024 | }
|
|---|
| 1025 |
|
|---|
| [64a7146] | 1026 | static void __halt(processor * this) with( *this ) {
|
|---|
| 1027 | if( do_terminate ) return;
|
|---|
| [8e16177] | 1028 |
|
|---|
| [68f36f4] | 1029 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| 1030 | __tls_stats()->ready.sleep.halts++;
|
|---|
| 1031 | #endif
|
|---|
| [64a7146] | 1032 | // Push self to queue
|
|---|
| 1033 | push(cltr->idles, *this);
|
|---|
| [8834751] | 1034 |
|
|---|
| [64a7146] | 1035 | // Makre sure we don't miss a thread
|
|---|
| 1036 | if( __has_next_thread(cltr) ) {
|
|---|
| 1037 | // A thread was posted, make sure a processor is woken up
|
|---|
| 1038 | struct __processor_id_t *id = (struct __processor_id_t *) this;
|
|---|
| 1039 | ready_schedule_lock ( id );
|
|---|
| 1040 | __wake_one( id, cltr );
|
|---|
| 1041 | ready_schedule_unlock( id );
|
|---|
| [68f36f4] | 1042 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| 1043 | __tls_stats()->ready.sleep.cancels++;
|
|---|
| 1044 | #endif
|
|---|
| [64a7146] | 1045 | }
|
|---|
| [8e16177] | 1046 |
|
|---|
| [c34ebf2] | 1047 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| 1048 | if(this->print_halts) {
|
|---|
| [f2b18d01] | 1049 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->id, rdtscl());
|
|---|
| [c34ebf2] | 1050 | }
|
|---|
| 1051 | #endif
|
|---|
| 1052 |
|
|---|
| [64a7146] | 1053 | wait( idle );
|
|---|
| [c34ebf2] | 1054 |
|
|---|
| 1055 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| 1056 | if(this->print_halts) {
|
|---|
| [f2b18d01] | 1057 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->id, rdtscl());
|
|---|
| [c34ebf2] | 1058 | }
|
|---|
| 1059 | #endif
|
|---|
| [6b4cdd3] | 1060 | }
|
|---|
| 1061 |
|
|---|
| [dbe9b08] | 1062 | //=============================================================================================
|
|---|
| 1063 | // Unexpected Terminating logic
|
|---|
| 1064 | //=============================================================================================
|
|---|
| [ea7d2b0] | 1065 | static __spinlock_t kernel_abort_lock;
|
|---|
| [9d944b2] | 1066 | static bool kernel_abort_called = false;
|
|---|
| 1067 |
|
|---|
| [afd550c] | 1068 | void * kernel_abort(void) __attribute__ ((__nothrow__)) {
|
|---|
| [9d944b2] | 1069 | // abort cannot be recursively entered by the same or different processors because all signal handlers return when
|
|---|
| 1070 | // the globalAbort flag is true.
|
|---|
| [36982fc] | 1071 | lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
|
|---|
| [9d944b2] | 1072 |
|
|---|
| 1073 | // first task to abort ?
|
|---|
| [de94a60] | 1074 | if ( kernel_abort_called ) { // not first task to abort ?
|
|---|
| [ea7d2b0] | 1075 | unlock( kernel_abort_lock );
|
|---|
| [1c273d0] | 1076 |
|
|---|
| [9d944b2] | 1077 | sigset_t mask;
|
|---|
| 1078 | sigemptyset( &mask );
|
|---|
| [de94a60] | 1079 | sigaddset( &mask, SIGALRM ); // block SIGALRM signals
|
|---|
| [8a13c47] | 1080 | sigaddset( &mask, SIGUSR1 ); // block SIGALRM signals
|
|---|
| 1081 | sigsuspend( &mask ); // block the processor to prevent further damage during abort
|
|---|
| 1082 | _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it
|
|---|
| [de94a60] | 1083 | }
|
|---|
| 1084 | else {
|
|---|
| 1085 | kernel_abort_called = true;
|
|---|
| 1086 | unlock( kernel_abort_lock );
|
|---|
| [9d944b2] | 1087 | }
|
|---|
| 1088 |
|
|---|
| [14a61b5] | 1089 | return kernelTLS.this_thread;
|
|---|
| [9d944b2] | 1090 | }
|
|---|
| 1091 |
|
|---|
| 1092 | void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
|
|---|
| [ac2b598] | 1093 | $thread * thrd = kernel_data;
|
|---|
| [9d944b2] | 1094 |
|
|---|
| [de94a60] | 1095 | if(thrd) {
|
|---|
| 1096 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
|
|---|
| [1c40091] | 1097 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
|---|
| [de94a60] | 1098 |
|
|---|
| [212c2187] | 1099 | if ( &thrd->self_cor != thrd->curr_cor ) {
|
|---|
| 1100 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
|
|---|
| [1c40091] | 1101 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
|---|
| [de94a60] | 1102 | }
|
|---|
| 1103 | else {
|
|---|
| [1c40091] | 1104 | __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 );
|
|---|
| [de94a60] | 1105 | }
|
|---|
| [1c273d0] | 1106 | }
|
|---|
| [9d944b2] | 1107 | else {
|
|---|
| [de94a60] | 1108 | int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
|
|---|
| [1c40091] | 1109 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
|---|
| [9d944b2] | 1110 | }
|
|---|
| 1111 | }
|
|---|
| 1112 |
|
|---|
| [2b8bc41] | 1113 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
|
|---|
| [14a61b5] | 1114 | return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2;
|
|---|
| [2b8bc41] | 1115 | }
|
|---|
| 1116 |
|
|---|
| [de94a60] | 1117 | static __spinlock_t kernel_debug_lock;
|
|---|
| 1118 |
|
|---|
| [9d944b2] | 1119 | extern "C" {
|
|---|
| [1c40091] | 1120 | void __cfaabi_bits_acquire() {
|
|---|
| [36982fc] | 1121 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
|
|---|
| [9d944b2] | 1122 | }
|
|---|
| 1123 |
|
|---|
| [1c40091] | 1124 | void __cfaabi_bits_release() {
|
|---|
| [ea7d2b0] | 1125 | unlock( kernel_debug_lock );
|
|---|
| [9d944b2] | 1126 | }
|
|---|
| [8118303] | 1127 | }
|
|---|
| 1128 |
|
|---|
| [fa21ac9] | 1129 | //=============================================================================================
|
|---|
| 1130 | // Kernel Utilities
|
|---|
| 1131 | //=============================================================================================
|
|---|
| [bd98b58] | 1132 | //-----------------------------------------------------------------------------
|
|---|
| 1133 | // Locks
|
|---|
| [242a902] | 1134 | void ?{}( semaphore & this, int count = 1 ) {
|
|---|
| 1135 | (this.lock){};
|
|---|
| 1136 | this.count = count;
|
|---|
| 1137 | (this.waiting){};
|
|---|
| [db6f06a] | 1138 | }
|
|---|
| [242a902] | 1139 | void ^?{}(semaphore & this) {}
|
|---|
| [db6f06a] | 1140 |
|
|---|
| [71c8b7e] | 1141 | bool P(semaphore & this) with( this ){
|
|---|
| [65deb18] | 1142 | lock( lock __cfaabi_dbg_ctx2 );
|
|---|
| 1143 | count -= 1;
|
|---|
| 1144 | if ( count < 0 ) {
|
|---|
| [bdeba0b] | 1145 | // queue current task
|
|---|
| [14a61b5] | 1146 | append( waiting, kernelTLS.this_thread );
|
|---|
| [bdeba0b] | 1147 |
|
|---|
| 1148 | // atomically release spin lock and block
|
|---|
| [3381ed7] | 1149 | unlock( lock );
|
|---|
| [ae66348] | 1150 | park( __cfaabi_dbg_ctx );
|
|---|
| [71c8b7e] | 1151 | return true;
|
|---|
| [8def349] | 1152 | }
|
|---|
| [4e6fb8e] | 1153 | else {
|
|---|
| [65deb18] | 1154 | unlock( lock );
|
|---|
| [71c8b7e] | 1155 | return false;
|
|---|
| [4e6fb8e] | 1156 | }
|
|---|
| [bd98b58] | 1157 | }
|
|---|
| 1158 |
|
|---|
| [f0ce5f4] | 1159 | bool V(semaphore & this) with( this ) {
|
|---|
| [ac2b598] | 1160 | $thread * thrd = 0p;
|
|---|
| [65deb18] | 1161 | lock( lock __cfaabi_dbg_ctx2 );
|
|---|
| 1162 | count += 1;
|
|---|
| 1163 | if ( count <= 0 ) {
|
|---|
| [bdeba0b] | 1164 | // remove task at head of waiting list
|
|---|
| [65deb18] | 1165 | thrd = pop_head( waiting );
|
|---|
| [bd98b58] | 1166 | }
|
|---|
| [bdeba0b] | 1167 |
|
|---|
| [65deb18] | 1168 | unlock( lock );
|
|---|
| [bdeba0b] | 1169 |
|
|---|
| 1170 | // make new owner
|
|---|
| [ae66348] | 1171 | unpark( thrd __cfaabi_dbg_ctx2 );
|
|---|
| [f0ce5f4] | 1172 |
|
|---|
| [d384787] | 1173 | return thrd != 0p;
|
|---|
| 1174 | }
|
|---|
| 1175 |
|
|---|
| 1176 | bool V(semaphore & this, unsigned diff) with( this ) {
|
|---|
| 1177 | $thread * thrd = 0p;
|
|---|
| 1178 | lock( lock __cfaabi_dbg_ctx2 );
|
|---|
| 1179 | int release = max(-count, (int)diff);
|
|---|
| 1180 | count += diff;
|
|---|
| 1181 | for(release) {
|
|---|
| 1182 | unpark( pop_head( waiting ) __cfaabi_dbg_ctx2 );
|
|---|
| 1183 | }
|
|---|
| 1184 |
|
|---|
| 1185 | unlock( lock );
|
|---|
| 1186 |
|
|---|
| [f0ce5f4] | 1187 | return thrd != 0p;
|
|---|
| [bd98b58] | 1188 | }
|
|---|
| 1189 |
|
|---|
| [f7d6bb0] | 1190 | //-----------------------------------------------------------------------------
|
|---|
| [de94a60] | 1191 | // Global Queues
|
|---|
| 1192 | void doregister( cluster & cltr ) {
|
|---|
| [ea8b2f7] | 1193 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
|
|---|
| 1194 | push_front( __cfa_dbg_global_clusters.list, cltr );
|
|---|
| 1195 | unlock ( __cfa_dbg_global_clusters.lock );
|
|---|
| [de94a60] | 1196 | }
|
|---|
| [f7d6bb0] | 1197 |
|
|---|
| [de94a60] | 1198 | void unregister( cluster & cltr ) {
|
|---|
| [ea8b2f7] | 1199 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
|
|---|
| 1200 | remove( __cfa_dbg_global_clusters.list, cltr );
|
|---|
| 1201 | unlock( __cfa_dbg_global_clusters.lock );
|
|---|
| [de94a60] | 1202 | }
|
|---|
| [f7d6bb0] | 1203 |
|
|---|
| [ac2b598] | 1204 | void doregister( cluster * cltr, $thread & thrd ) {
|
|---|
| [a1a17a74] | 1205 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2);
|
|---|
| [d4e68a6] | 1206 | cltr->nthreads += 1;
|
|---|
| [a1a17a74] | 1207 | push_front(cltr->threads, thrd);
|
|---|
| 1208 | unlock (cltr->thread_list_lock);
|
|---|
| 1209 | }
|
|---|
| 1210 |
|
|---|
| [ac2b598] | 1211 | void unregister( cluster * cltr, $thread & thrd ) {
|
|---|
| [a1a17a74] | 1212 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2);
|
|---|
| 1213 | remove(cltr->threads, thrd );
|
|---|
| [d4e68a6] | 1214 | cltr->nthreads -= 1;
|
|---|
| [a1a17a74] | 1215 | unlock(cltr->thread_list_lock);
|
|---|
| 1216 | }
|
|---|
| [9181f1d] | 1217 |
|
|---|
| [de94a60] | 1218 | //-----------------------------------------------------------------------------
|
|---|
| 1219 | // Debug
|
|---|
| 1220 | __cfaabi_dbg_debug_do(
|
|---|
| [1997b4e] | 1221 | extern "C" {
|
|---|
| [ae66348] | 1222 | void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) {
|
|---|
| [1997b4e] | 1223 | this.prev_name = prev_name;
|
|---|
| 1224 | this.prev_thrd = kernelTLS.this_thread;
|
|---|
| 1225 | }
|
|---|
| [ae66348] | 1226 |
|
|---|
| 1227 | void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]) {
|
|---|
| 1228 | if(park) {
|
|---|
| [f0ce5f4] | 1229 | this.park_caller = prev_name;
|
|---|
| 1230 | this.park_stale = false;
|
|---|
| [ae66348] | 1231 | }
|
|---|
| 1232 | else {
|
|---|
| [f0ce5f4] | 1233 | this.unpark_caller = prev_name;
|
|---|
| 1234 | this.unpark_stale = false;
|
|---|
| [ae66348] | 1235 | }
|
|---|
| 1236 | }
|
|---|
| [9181f1d] | 1237 | }
|
|---|
| [f7d6bb0] | 1238 | )
|
|---|
| [2026bb6] | 1239 |
|
|---|
| 1240 | //-----------------------------------------------------------------------------
|
|---|
| 1241 | // Debug
|
|---|
| [8c50aed] | 1242 | bool threading_enabled(void) __attribute__((const)) {
|
|---|
| [2026bb6] | 1243 | return true;
|
|---|
| 1244 | }
|
|---|
| [c34ebf2] | 1245 |
|
|---|
| 1246 | //-----------------------------------------------------------------------------
|
|---|
| 1247 | // Statistics
|
|---|
| 1248 | #if !defined(__CFA_NO_STATISTICS__)
|
|---|
| 1249 | void print_halts( processor & this ) {
|
|---|
| 1250 | this.print_halts = true;
|
|---|
| 1251 | }
|
|---|
| 1252 | #endif
|
|---|
| [8118303] | 1253 | // Local Variables: //
|
|---|
| 1254 | // mode: c //
|
|---|
| 1255 | // tab-width: 4 //
|
|---|
| 1256 | // End: //
|
|---|