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