| [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
|
|---|
| [e3fea42] | 12 | // Last Modified On : Tue Feb 4 13:03:15 2020
|
|---|
| 13 | // Update Count : 58
|
|---|
| [8118303] | 14 | //
|
|---|
| 15 |
|
|---|
| [2026bb6] | 16 | #define __cforall_thread__
|
|---|
| 17 |
|
|---|
| [8118303] | 18 | //C Includes
|
|---|
| [c84e80a] | 19 | #include <stddef.h>
|
|---|
| [214e8da] | 20 | #include <errno.h>
|
|---|
| [ea8b2f7] | 21 | #include <string.h>
|
|---|
| [eb2e723] | 22 | extern "C" {
|
|---|
| [9d944b2] | 23 | #include <stdio.h>
|
|---|
| [8fcbb4c] | 24 | #include <fenv.h>
|
|---|
| [eb2e723] | 25 | #include <sys/resource.h>
|
|---|
| [58b6d1b] | 26 | #include <signal.h>
|
|---|
| [9d944b2] | 27 | #include <unistd.h>
|
|---|
| [27f5f71] | 28 | #include <limits.h> // PTHREAD_STACK_MIN
|
|---|
| [1a3040c] | 29 | #include <sys/mman.h> // mprotect
|
|---|
| [eb2e723] | 30 | }
|
|---|
| [8118303] | 31 |
|
|---|
| 32 | //CFA Includes
|
|---|
| [58b6d1b] | 33 | #include "time.hfa"
|
|---|
| [73abe95] | 34 | #include "kernel_private.hfa"
|
|---|
| 35 | #include "preemption.hfa"
|
|---|
| 36 | #include "startup.hfa"
|
|---|
| [8118303] | 37 |
|
|---|
| 38 | //Private includes
|
|---|
| 39 | #define __CFA_INVOKE_PRIVATE__
|
|---|
| 40 | #include "invoke.h"
|
|---|
| 41 |
|
|---|
| [deca0f5] | 42 | //-----------------------------------------------------------------------------
|
|---|
| 43 | // Some assembly required
|
|---|
| [1805b1b] | 44 | #if defined( __i386 )
|
|---|
| [deca0f5] | 45 | #define CtxGet( ctx ) \
|
|---|
| 46 | __asm__ volatile ( \
|
|---|
| 47 | "movl %%esp,%0\n"\
|
|---|
| 48 | "movl %%ebp,%1\n"\
|
|---|
| 49 | : "=rm" (ctx.SP),\
|
|---|
| 50 | "=rm" (ctx.FP) \
|
|---|
| 51 | )
|
|---|
| 52 |
|
|---|
| 53 | // mxcr : SSE Status and Control bits (control bits are preserved across function calls)
|
|---|
| 54 | // fcw : X87 FPU control word (preserved across function calls)
|
|---|
| 55 | #define __x87_store \
|
|---|
| 56 | uint32_t __mxcr; \
|
|---|
| 57 | uint16_t __fcw; \
|
|---|
| 58 | __asm__ volatile ( \
|
|---|
| 59 | "stmxcsr %0\n" \
|
|---|
| 60 | "fnstcw %1\n" \
|
|---|
| 61 | : "=m" (__mxcr),\
|
|---|
| 62 | "=m" (__fcw) \
|
|---|
| 63 | )
|
|---|
| 64 |
|
|---|
| 65 | #define __x87_load \
|
|---|
| 66 | __asm__ volatile ( \
|
|---|
| 67 | "fldcw %1\n" \
|
|---|
| 68 | "ldmxcsr %0\n" \
|
|---|
| 69 | ::"m" (__mxcr),\
|
|---|
| 70 | "m" (__fcw) \
|
|---|
| 71 | )
|
|---|
| 72 |
|
|---|
| 73 | #elif defined( __x86_64 )
|
|---|
| 74 | #define CtxGet( ctx ) \
|
|---|
| 75 | __asm__ volatile ( \
|
|---|
| 76 | "movq %%rsp,%0\n"\
|
|---|
| 77 | "movq %%rbp,%1\n"\
|
|---|
| 78 | : "=rm" (ctx.SP),\
|
|---|
| 79 | "=rm" (ctx.FP) \
|
|---|
| 80 | )
|
|---|
| 81 |
|
|---|
| 82 | #define __x87_store \
|
|---|
| 83 | uint32_t __mxcr; \
|
|---|
| 84 | uint16_t __fcw; \
|
|---|
| 85 | __asm__ volatile ( \
|
|---|
| 86 | "stmxcsr %0\n" \
|
|---|
| 87 | "fnstcw %1\n" \
|
|---|
| 88 | : "=m" (__mxcr),\
|
|---|
| 89 | "=m" (__fcw) \
|
|---|
| 90 | )
|
|---|
| 91 |
|
|---|
| 92 | #define __x87_load \
|
|---|
| 93 | __asm__ volatile ( \
|
|---|
| 94 | "fldcw %1\n" \
|
|---|
| 95 | "ldmxcsr %0\n" \
|
|---|
| 96 | :: "m" (__mxcr),\
|
|---|
| 97 | "m" (__fcw) \
|
|---|
| 98 | )
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 | #elif defined( __ARM_ARCH )
|
|---|
| 102 | #define CtxGet( ctx ) __asm__ ( \
|
|---|
| 103 | "mov %0,%%sp\n" \
|
|---|
| 104 | "mov %1,%%r11\n" \
|
|---|
| 105 | : "=rm" (ctx.SP), "=rm" (ctx.FP) )
|
|---|
| 106 | #else
|
|---|
| 107 | #error unknown hardware architecture
|
|---|
| 108 | #endif
|
|---|
| 109 |
|
|---|
| 110 | //-----------------------------------------------------------------------------
|
|---|
| [2ac095d] | 111 | //Start and stop routine for the kernel, declared first to make sure they run first
|
|---|
| [8c50aed] | 112 | static void __kernel_startup (void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
|
|---|
| 113 | static void __kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
|
|---|
| [2ac095d] | 114 |
|
|---|
| [8def349] | 115 | //-----------------------------------------------------------------------------
|
|---|
| 116 | // Kernel storage
|
|---|
| [b2f6113] | 117 | KERNEL_STORAGE(cluster, mainCluster);
|
|---|
| 118 | KERNEL_STORAGE(processor, mainProcessor);
|
|---|
| [ac2b598] | 119 | KERNEL_STORAGE($thread, mainThread);
|
|---|
| [b2f6113] | 120 | KERNEL_STORAGE(__stack_t, mainThreadCtx);
|
|---|
| [8def349] | 121 |
|
|---|
| [de6319f] | 122 | cluster * mainCluster;
|
|---|
| 123 | processor * mainProcessor;
|
|---|
| [ac2b598] | 124 | $thread * mainThread;
|
|---|
| [eb2e723] | 125 |
|
|---|
| [ea8b2f7] | 126 | extern "C" {
|
|---|
| [1805b1b] | 127 | struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters;
|
|---|
| [ea8b2f7] | 128 | }
|
|---|
| [de94a60] | 129 |
|
|---|
| [b2f6113] | 130 | size_t __page_size = 0;
|
|---|
| 131 |
|
|---|
| [bd98b58] | 132 | //-----------------------------------------------------------------------------
|
|---|
| 133 | // Global state
|
|---|
| [afc2427] | 134 | thread_local struct KernelThreadData kernelTLS __attribute__ ((tls_model ( "initial-exec" ))) = {
|
|---|
| [1805b1b] | 135 | NULL, // cannot use 0p
|
|---|
| [b10affd] | 136 | NULL,
|
|---|
| [09d4b22] | 137 | { 1, false, false },
|
|---|
| [21184e3] | 138 | 6u //this should be seeded better but due to a bug calling rdtsc doesn't work
|
|---|
| [b10affd] | 139 | };
|
|---|
| [c84e80a] | 140 |
|
|---|
| 141 | //-----------------------------------------------------------------------------
|
|---|
| [de6319f] | 142 | // Struct to steal stack
|
|---|
| [8def349] | 143 | struct current_stack_info_t {
|
|---|
| [1805b1b] | 144 | __stack_t * storage; // pointer to stack object
|
|---|
| 145 | void * base; // base of stack
|
|---|
| 146 | void * limit; // stack grows towards stack limit
|
|---|
| 147 | void * context; // address of cfa_context_t
|
|---|
| [c84e80a] | 148 | };
|
|---|
| 149 |
|
|---|
| [242a902] | 150 | void ?{}( current_stack_info_t & this ) {
|
|---|
| [b2f6113] | 151 | __stack_context_t ctx;
|
|---|
| 152 | CtxGet( ctx );
|
|---|
| 153 | this.base = ctx.FP;
|
|---|
| [8def349] | 154 |
|
|---|
| 155 | rlimit r;
|
|---|
| [132fad4] | 156 | getrlimit( RLIMIT_STACK, &r);
|
|---|
| [69a61d2] | 157 | size_t size = r.rlim_cur;
|
|---|
| [8def349] | 158 |
|
|---|
| [69a61d2] | 159 | this.limit = (void *)(((intptr_t)this.base) - size);
|
|---|
| [9236060] | 160 | this.context = &storage_mainThreadCtx;
|
|---|
| [8def349] | 161 | }
|
|---|
| 162 |
|
|---|
| [de6319f] | 163 | //-----------------------------------------------------------------------------
|
|---|
| 164 | // Main thread construction
|
|---|
| [8def349] | 165 |
|
|---|
| [ac2b598] | 166 | void ?{}( $coroutine & this, current_stack_info_t * info) with( this ) {
|
|---|
| [b2f6113] | 167 | stack.storage = info->storage;
|
|---|
| 168 | with(*stack.storage) {
|
|---|
| 169 | limit = info->limit;
|
|---|
| 170 | base = info->base;
|
|---|
| 171 | }
|
|---|
| [ffe2fad] | 172 | __attribute__((may_alias)) intptr_t * istorage = (intptr_t*) &stack.storage;
|
|---|
| 173 | *istorage |= 0x1;
|
|---|
| [65deb18] | 174 | name = "Main Thread";
|
|---|
| 175 | state = Start;
|
|---|
| [1805b1b] | 176 | starter = 0p;
|
|---|
| 177 | last = 0p;
|
|---|
| 178 | cancellation = 0p;
|
|---|
| [8def349] | 179 | }
|
|---|
| 180 |
|
|---|
| [ac2b598] | 181 | void ?{}( $thread & this, current_stack_info_t * info) with( this ) {
|
|---|
| [e8e457e] | 182 | state = Start;
|
|---|
| [65deb18] | 183 | self_cor{ info };
|
|---|
| [82c948c] | 184 | curr_cor = &self_cor;
|
|---|
| [de6319f] | 185 | curr_cluster = mainCluster;
|
|---|
| [82c948c] | 186 | self_mon.owner = &this;
|
|---|
| 187 | self_mon.recursion = 1;
|
|---|
| 188 | self_mon_p = &self_mon;
|
|---|
| [1805b1b] | 189 | next = 0p;
|
|---|
| [de94a60] | 190 |
|
|---|
| [1805b1b] | 191 | node.next = 0p;
|
|---|
| 192 | node.prev = 0p;
|
|---|
| [a1a17a74] | 193 | doregister(curr_cluster, this);
|
|---|
| [82c948c] | 194 |
|
|---|
| 195 | monitors{ &self_mon_p, 1, (fptr_t)0 };
|
|---|
| [8def349] | 196 | }
|
|---|
| [c84e80a] | 197 |
|
|---|
| [8def349] | 198 | //-----------------------------------------------------------------------------
|
|---|
| 199 | // Processor coroutine
|
|---|
| [de6319f] | 200 | void ?{}(processorCtx_t & this) {
|
|---|
| [39fea2f] | 201 |
|
|---|
| [8def349] | 202 | }
|
|---|
| 203 |
|
|---|
| [39fea2f] | 204 | // Construct the processor context of non-main processors
|
|---|
| [c29c342] | 205 | static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) {
|
|---|
| [242a902] | 206 | (this.__cor){ info };
|
|---|
| 207 | this.proc = proc;
|
|---|
| [8def349] | 208 | }
|
|---|
| 209 |
|
|---|
| [c7a900a] | 210 | static void * __invoke_processor(void * arg);
|
|---|
| [8c50aed] | 211 |
|
|---|
| [e3fea42] | 212 | void ?{}(processor & this, const char name[], cluster & cltr) with( this ) {
|
|---|
| [de6319f] | 213 | this.name = name;
|
|---|
| 214 | this.cltr = &cltr;
|
|---|
| [c40e7c5] | 215 | terminated{ 0 };
|
|---|
| [b0c7419] | 216 | destroyer = 0p;
|
|---|
| [c40e7c5] | 217 | do_terminate = false;
|
|---|
| [1805b1b] | 218 | preemption_alarm = 0p;
|
|---|
| [c40e7c5] | 219 | pending_preemption = false;
|
|---|
| [094476d] | 220 | runner.proc = &this;
|
|---|
| [8def349] | 221 |
|
|---|
| [85b1deb] | 222 | idleLock{};
|
|---|
| [6b4cdd3] | 223 |
|
|---|
| [8c50aed] | 224 | __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", &this);
|
|---|
| 225 |
|
|---|
| [c7a900a] | 226 | this.stack = __create_pthread( &this.kernel_thread, __invoke_processor, (void *)&this );
|
|---|
| [8c50aed] | 227 |
|
|---|
| 228 | __cfaabi_dbg_print_safe("Kernel : core %p started\n", &this);
|
|---|
| [c84e80a] | 229 | }
|
|---|
| 230 |
|
|---|
| [65deb18] | 231 | void ^?{}(processor & this) with( this ){
|
|---|
| [ea8b2f7] | 232 | if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) {
|
|---|
| [36982fc] | 233 | __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
|
|---|
| [85b1deb] | 234 |
|
|---|
| 235 | __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED);
|
|---|
| 236 | wake( &this );
|
|---|
| 237 |
|
|---|
| [65deb18] | 238 | P( terminated );
|
|---|
| [14a61b5] | 239 | verify( kernelTLS.this_processor != &this);
|
|---|
| [8def349] | 240 | }
|
|---|
| [6b4cdd3] | 241 |
|
|---|
| [1805b1b] | 242 | pthread_join( kernel_thread, 0p );
|
|---|
| [27f5f71] | 243 | free( this.stack );
|
|---|
| [8def349] | 244 | }
|
|---|
| 245 |
|
|---|
| [e3fea42] | 246 | void ?{}(cluster & this, const char name[], Duration preemption_rate) with( this ) {
|
|---|
| [de6319f] | 247 | this.name = name;
|
|---|
| 248 | this.preemption_rate = preemption_rate;
|
|---|
| [65deb18] | 249 | ready_queue{};
|
|---|
| 250 | ready_queue_lock{};
|
|---|
| [de94a60] | 251 |
|
|---|
| 252 | procs{ __get };
|
|---|
| 253 | idles{ __get };
|
|---|
| [a1a17a74] | 254 | threads{ __get };
|
|---|
| [de94a60] | 255 |
|
|---|
| 256 | doregister(this);
|
|---|
| [8def349] | 257 | }
|
|---|
| 258 |
|
|---|
| [242a902] | 259 | void ^?{}(cluster & this) {
|
|---|
| [de94a60] | 260 | unregister(this);
|
|---|
| [c84e80a] | 261 | }
|
|---|
| 262 |
|
|---|
| [75f3522] | 263 | //=============================================================================================
|
|---|
| 264 | // Kernel Scheduling logic
|
|---|
| 265 | //=============================================================================================
|
|---|
| [ac2b598] | 266 | static $thread * __next_thread(cluster * this);
|
|---|
| 267 | static void __run_thread(processor * this, $thread * dst);
|
|---|
| [8c50aed] | 268 | static void __halt(processor * this);
|
|---|
| [c29c342] | 269 |
|
|---|
| [8fcbb4c] | 270 | //Main of the processor contexts
|
|---|
| [83a071f9] | 271 | void main(processorCtx_t & runner) {
|
|---|
| [21184e3] | 272 | // Because of a bug, we couldn't initialized the seed on construction
|
|---|
| 273 | // Do it here
|
|---|
| [57c764c4] | 274 | kernelTLS.rand_seed ^= rdtscl();
|
|---|
| [21184e3] | 275 |
|
|---|
| [83a071f9] | 276 | processor * this = runner.proc;
|
|---|
| [094476d] | 277 | verify(this);
|
|---|
| [c81ebf9] | 278 |
|
|---|
| [36982fc] | 279 | __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this);
|
|---|
| [8118303] | 280 |
|
|---|
| [de94a60] | 281 | doregister(this->cltr, this);
|
|---|
| 282 |
|
|---|
| [75f3522] | 283 | {
|
|---|
| [c81ebf9] | 284 | // Setup preemption data
|
|---|
| 285 | preemption_scope scope = { this };
|
|---|
| 286 |
|
|---|
| [36982fc] | 287 | __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
|
|---|
| [8118303] | 288 |
|
|---|
| [ac2b598] | 289 | $thread * readyThread = 0p;
|
|---|
| [1a3040c] | 290 | for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ ) {
|
|---|
| [8c50aed] | 291 | readyThread = __next_thread( this->cltr );
|
|---|
| [75f3522] | 292 |
|
|---|
| [1a3040c] | 293 | if(readyThread) {
|
|---|
| [3381ed7] | 294 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [ae7be7a] | 295 | /* paranoid */ verifyf( readyThread->state == Blocked || readyThread->state == Start || readyThread->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", readyThread->state, readyThread->preempted);
|
|---|
| [3381ed7] | 296 | /* paranoid */ verifyf( readyThread->next == 0p, "Expected null got %p", readyThread->next );
|
|---|
| [4e6fb8e] | 297 |
|
|---|
| [8c50aed] | 298 | __run_thread(this, readyThread);
|
|---|
| [75f3522] | 299 |
|
|---|
| [3381ed7] | 300 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [c81ebf9] | 301 |
|
|---|
| 302 | spin_count = 0;
|
|---|
| [1a3040c] | 303 | } else {
|
|---|
| [ea8b2f7] | 304 | // spin(this, &spin_count);
|
|---|
| [8c50aed] | 305 | __halt(this);
|
|---|
| [c81ebf9] | 306 | }
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| [36982fc] | 309 | __cfaabi_dbg_print_safe("Kernel : core %p stopping\n", this);
|
|---|
| [c84e80a] | 310 | }
|
|---|
| [8118303] | 311 |
|
|---|
| [de94a60] | 312 | unregister(this->cltr, this);
|
|---|
| 313 |
|
|---|
| [f0ce5f4] | 314 | bool signalled = V( this->terminated );
|
|---|
| 315 | if(signalled)
|
|---|
| [bdeba0b] | 316 |
|
|---|
| [36982fc] | 317 | __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this);
|
|---|
| [c84e80a] | 318 | }
|
|---|
| 319 |
|
|---|
| [5c1a531] | 320 | static int * __volatile_errno() __attribute__((noinline));
|
|---|
| 321 | static int * __volatile_errno() { asm(""); return &errno; }
|
|---|
| 322 |
|
|---|
| [14a61b5] | 323 | // KERNEL ONLY
|
|---|
| [1c273d0] | 324 | // runThread runs a thread by context switching
|
|---|
| 325 | // from the processor coroutine to the target thread
|
|---|
| [ac2b598] | 326 | static void __run_thread(processor * this, $thread * thrd_dst) {
|
|---|
| 327 | $coroutine * proc_cor = get_coroutine(this->runner);
|
|---|
| [1c273d0] | 328 |
|
|---|
| [14a61b5] | 329 | // Update global state
|
|---|
| [e8e457e] | 330 | kernelTLS.this_thread = thrd_dst;
|
|---|
| 331 |
|
|---|
| [9f575ea] | 332 | // set state of processor coroutine to inactive
|
|---|
| 333 | verify(proc_cor->state == Active);
|
|---|
| [ae7be7a] | 334 | proc_cor->state = Blocked;
|
|---|
| [e8e457e] | 335 |
|
|---|
| [9f575ea] | 336 | // Actually run the thread
|
|---|
| [3381ed7] | 337 | RUNNING: while(true) {
|
|---|
| [9f575ea] | 338 | if(unlikely(thrd_dst->preempted)) {
|
|---|
| [3381ed7] | 339 | thrd_dst->preempted = __NO_PREEMPTION;
|
|---|
| [b0c7419] | 340 | verify(thrd_dst->state == Active || thrd_dst->state == Rerun);
|
|---|
| [9f575ea] | 341 | } else {
|
|---|
| [ae7be7a] | 342 | verify(thrd_dst->state == Start || thrd_dst->state == Primed || thrd_dst->state == Blocked);
|
|---|
| [9f575ea] | 343 | thrd_dst->state = Active;
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| [ae66348] | 346 | __cfaabi_dbg_debug_do(
|
|---|
| [276ae57e] | 347 | thrd_dst->park_stale = true;
|
|---|
| 348 | thrd_dst->unpark_stale = true;
|
|---|
| [ae66348] | 349 | )
|
|---|
| 350 |
|
|---|
| [3381ed7] | 351 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [210b8b3] | 352 | /* 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
|
|---|
| 353 | /* 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] | 354 |
|
|---|
| [9f575ea] | 355 | // set context switch to the thread that the processor is executing
|
|---|
| 356 | verify( thrd_dst->context.SP );
|
|---|
| [c7a900a] | 357 | __cfactx_switch( &proc_cor->context, &thrd_dst->context );
|
|---|
| 358 | // when __cfactx_switch returns we are back in the processor coroutine
|
|---|
| [9f575ea] | 359 |
|
|---|
| [210b8b3] | 360 | /* 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 );
|
|---|
| 361 | /* 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 );
|
|---|
| [3381ed7] | 362 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [75f3522] | 363 |
|
|---|
| [3381ed7] | 364 |
|
|---|
| 365 | // We just finished running a thread, there are a few things that could have happened.
|
|---|
| 366 | // 1 - Regular case : the thread has blocked and now one has scheduled it yet.
|
|---|
| 367 | // 2 - Racy case : the thread has blocked but someone has already tried to schedule it.
|
|---|
| 368 | // 4 - Preempted
|
|---|
| 369 | // In case 1, we may have won a race so we can't write to the state again.
|
|---|
| 370 | // In case 2, we lost the race so we now own the thread.
|
|---|
| 371 |
|
|---|
| 372 | if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) {
|
|---|
| 373 | // The thread was preempted, reschedule it and reset the flag
|
|---|
| [8c50aed] | 374 | __schedule_thread( thrd_dst );
|
|---|
| [3381ed7] | 375 | break RUNNING;
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | // set state of processor coroutine to active and the thread to inactive
|
|---|
| 379 | static_assert(sizeof(thrd_dst->state) == sizeof(int));
|
|---|
| [ae7be7a] | 380 | enum coroutine_state old_state = __atomic_exchange_n(&thrd_dst->state, Blocked, __ATOMIC_SEQ_CST);
|
|---|
| 381 | __cfaabi_dbg_debug_do( thrd_dst->park_result = old_state; )
|
|---|
| [3381ed7] | 382 | switch(old_state) {
|
|---|
| 383 | case Halted:
|
|---|
| 384 | // The thread has halted, it should never be scheduled/run again, leave it back to Halted and move on
|
|---|
| 385 | thrd_dst->state = Halted;
|
|---|
| [b0c7419] | 386 |
|
|---|
| 387 | // We may need to wake someone up here since
|
|---|
| [ae66348] | 388 | unpark( this->destroyer __cfaabi_dbg_ctx2 );
|
|---|
| [b0c7419] | 389 | this->destroyer = 0p;
|
|---|
| [3381ed7] | 390 | break RUNNING;
|
|---|
| 391 | case Active:
|
|---|
| 392 | // This is case 1, the regular case, nothing more is needed
|
|---|
| 393 | break RUNNING;
|
|---|
| 394 | case Rerun:
|
|---|
| 395 | // This is case 2, the racy case, someone tried to run this thread before it finished blocking
|
|---|
| 396 | // In this case, just run it again.
|
|---|
| 397 | continue RUNNING;
|
|---|
| 398 | default:
|
|---|
| 399 | // This makes no sense, something is wrong abort
|
|---|
| [ae7be7a] | 400 | abort("Finished running a thread that was Blocked/Start/Primed %d\n", old_state);
|
|---|
| [3381ed7] | 401 | }
|
|---|
| [9f575ea] | 402 | }
|
|---|
| 403 |
|
|---|
| 404 | // Just before returning to the processor, set the processor coroutine to active
|
|---|
| [e8e457e] | 405 | proc_cor->state = Active;
|
|---|
| [ae7be7a] | 406 | kernelTLS.this_thread = 0p;
|
|---|
| [75f3522] | 407 | }
|
|---|
| 408 |
|
|---|
| [14a61b5] | 409 | // KERNEL_ONLY
|
|---|
| [b0c7419] | 410 | void returnToKernel() {
|
|---|
| [3381ed7] | 411 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [ac2b598] | 412 | $coroutine * proc_cor = get_coroutine(kernelTLS.this_processor->runner);
|
|---|
| 413 | $thread * thrd_src = kernelTLS.this_thread;
|
|---|
| [e8e457e] | 414 |
|
|---|
| [9f575ea] | 415 | // Run the thread on this processor
|
|---|
| 416 | {
|
|---|
| 417 | int local_errno = *__volatile_errno();
|
|---|
| 418 | #if defined( __i386 ) || defined( __x86_64 )
|
|---|
| 419 | __x87_store;
|
|---|
| 420 | #endif
|
|---|
| 421 | verify( proc_cor->context.SP );
|
|---|
| [c7a900a] | 422 | __cfactx_switch( &thrd_src->context, &proc_cor->context );
|
|---|
| [9f575ea] | 423 | #if defined( __i386 ) || defined( __x86_64 )
|
|---|
| 424 | __x87_load;
|
|---|
| 425 | #endif
|
|---|
| 426 | *__volatile_errno() = local_errno;
|
|---|
| 427 | }
|
|---|
| [deca0f5] | 428 |
|
|---|
| [3381ed7] | 429 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [210b8b3] | 430 | /* 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 );
|
|---|
| 431 | /* 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] | 432 | }
|
|---|
| 433 |
|
|---|
| [14a61b5] | 434 | // KERNEL_ONLY
|
|---|
| [0c92c9f] | 435 | // Context invoker for processors
|
|---|
| 436 | // This is the entry point for processors (kernel threads)
|
|---|
| 437 | // It effectively constructs a coroutine by stealing the pthread stack
|
|---|
| [c7a900a] | 438 | static void * __invoke_processor(void * arg) {
|
|---|
| [8def349] | 439 | processor * proc = (processor *) arg;
|
|---|
| [14a61b5] | 440 | kernelTLS.this_processor = proc;
|
|---|
| [1805b1b] | 441 | kernelTLS.this_thread = 0p;
|
|---|
| [14a61b5] | 442 | kernelTLS.preemption_state.[enabled, disable_count] = [false, 1];
|
|---|
| [8def349] | 443 | // SKULLDUGGERY: We want to create a context for the processor coroutine
|
|---|
| 444 | // which is needed for the 2-step context switch. However, there is no reason
|
|---|
| [1c273d0] | 445 | // to waste the perfectly valid stack create by pthread.
|
|---|
| [8def349] | 446 | current_stack_info_t info;
|
|---|
| [b2f6113] | 447 | __stack_t ctx;
|
|---|
| 448 | info.storage = &ctx;
|
|---|
| [094476d] | 449 | (proc->runner){ proc, &info };
|
|---|
| [8def349] | 450 |
|
|---|
| [b2f6113] | 451 | __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage);
|
|---|
| [8fcbb4c] | 452 |
|
|---|
| [0c92c9f] | 453 | //Set global state
|
|---|
| [1805b1b] | 454 | kernelTLS.this_thread = 0p;
|
|---|
| [8def349] | 455 |
|
|---|
| 456 | //We now have a proper context from which to schedule threads
|
|---|
| [094476d] | 457 | __cfaabi_dbg_print_safe("Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx);
|
|---|
| [8def349] | 458 |
|
|---|
| [1c273d0] | 459 | // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
|
|---|
| 460 | // resume it to start it like it normally would, it will just context switch
|
|---|
| 461 | // back to here. Instead directly call the main since we already are on the
|
|---|
| [8def349] | 462 | // appropriate stack.
|
|---|
| [094476d] | 463 | get_coroutine(proc->runner)->state = Active;
|
|---|
| 464 | main( proc->runner );
|
|---|
| 465 | get_coroutine(proc->runner)->state = Halted;
|
|---|
| [8def349] | 466 |
|
|---|
| [0c92c9f] | 467 | // Main routine of the core returned, the core is now fully terminated
|
|---|
| [094476d] | 468 | __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner);
|
|---|
| [8def349] | 469 |
|
|---|
| [1805b1b] | 470 | return 0p;
|
|---|
| [c84e80a] | 471 | }
|
|---|
| 472 |
|
|---|
| [e3fea42] | 473 | static void Abort( int ret, const char func[] ) {
|
|---|
| [1a3040c] | 474 | if ( ret ) { // pthread routines return errno values
|
|---|
| [1805b1b] | 475 | abort( "%s : internal error, error(%d) %s.", func, ret, strerror( ret ) );
|
|---|
| [27f5f71] | 476 | } // if
|
|---|
| [1805b1b] | 477 | } // Abort
|
|---|
| 478 |
|
|---|
| [8c50aed] | 479 | void * __create_pthread( pthread_t * pthread, void * (*start)(void *), void * arg ) {
|
|---|
| [1805b1b] | 480 | pthread_attr_t attr;
|
|---|
| 481 |
|
|---|
| 482 | Abort( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
|
|---|
| 483 |
|
|---|
| [27f5f71] | 484 | size_t stacksize;
|
|---|
| [09d4b22] | 485 | // default stack size, normally defined by shell limit
|
|---|
| [1a3040c] | 486 | Abort( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
|
|---|
| [27f5f71] | 487 | assert( stacksize >= PTHREAD_STACK_MIN );
|
|---|
| [1a3040c] | 488 |
|
|---|
| [09d4b22] | 489 | void * stack;
|
|---|
| 490 | __cfaabi_dbg_debug_do(
|
|---|
| 491 | stack = memalign( __page_size, stacksize + __page_size );
|
|---|
| 492 | // pthread has no mechanism to create the guard page in user supplied stack.
|
|---|
| 493 | if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
|
|---|
| 494 | abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
|
|---|
| 495 | } // if
|
|---|
| 496 | );
|
|---|
| 497 | __cfaabi_dbg_no_debug_do(
|
|---|
| 498 | stack = malloc( stacksize );
|
|---|
| 499 | );
|
|---|
| [1a3040c] | 500 |
|
|---|
| [09f357ec] | 501 | Abort( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
|
|---|
| [27f5f71] | 502 |
|
|---|
| [1805b1b] | 503 | Abort( pthread_create( pthread, &attr, start, arg ), "pthread_create" );
|
|---|
| 504 | return stack;
|
|---|
| 505 | }
|
|---|
| [27f5f71] | 506 |
|
|---|
| [14a61b5] | 507 | // KERNEL_ONLY
|
|---|
| [8c50aed] | 508 | static void __kernel_first_resume( processor * this ) {
|
|---|
| [ac2b598] | 509 | $thread * src = mainThread;
|
|---|
| 510 | $coroutine * dst = get_coroutine(this->runner);
|
|---|
| [b69ea6b] | 511 |
|
|---|
| [14a61b5] | 512 | verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [b69ea6b] | 513 |
|
|---|
| [09f357ec] | 514 | kernelTLS.this_thread->curr_cor = dst;
|
|---|
| [b2f6113] | 515 | __stack_prepare( &dst->stack, 65000 );
|
|---|
| [c7a900a] | 516 | __cfactx_start(main, dst, this->runner, __cfactx_invoke_coroutine);
|
|---|
| [b69ea6b] | 517 |
|
|---|
| [14a61b5] | 518 | verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [b69ea6b] | 519 |
|
|---|
| [e8e457e] | 520 | dst->last = &src->self_cor;
|
|---|
| 521 | dst->starter = dst->starter ? dst->starter : &src->self_cor;
|
|---|
| [b69ea6b] | 522 |
|
|---|
| 523 | // set state of current coroutine to inactive
|
|---|
| [ae7be7a] | 524 | src->state = src->state == Halted ? Halted : Blocked;
|
|---|
| [b69ea6b] | 525 |
|
|---|
| 526 | // context switch to specified coroutine
|
|---|
| [69a61d2] | 527 | verify( dst->context.SP );
|
|---|
| [c7a900a] | 528 | __cfactx_switch( &src->context, &dst->context );
|
|---|
| 529 | // when __cfactx_switch returns we are back in the src coroutine
|
|---|
| [b69ea6b] | 530 |
|
|---|
| [09f357ec] | 531 | mainThread->curr_cor = &mainThread->self_cor;
|
|---|
| 532 |
|
|---|
| [b69ea6b] | 533 | // set state of new coroutine to active
|
|---|
| 534 | src->state = Active;
|
|---|
| 535 |
|
|---|
| [14a61b5] | 536 | verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [b69ea6b] | 537 | }
|
|---|
| 538 |
|
|---|
| [e8e457e] | 539 | // KERNEL_ONLY
|
|---|
| [8c50aed] | 540 | static void __kernel_last_resume( processor * this ) {
|
|---|
| [ac2b598] | 541 | $coroutine * src = &mainThread->self_cor;
|
|---|
| 542 | $coroutine * dst = get_coroutine(this->runner);
|
|---|
| [e8e457e] | 543 |
|
|---|
| 544 | verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| 545 | verify( dst->starter == src );
|
|---|
| 546 | verify( dst->context.SP );
|
|---|
| 547 |
|
|---|
| 548 | // context switch to the processor
|
|---|
| [c7a900a] | 549 | __cfactx_switch( &src->context, &dst->context );
|
|---|
| [e8e457e] | 550 | }
|
|---|
| 551 |
|
|---|
| [8def349] | 552 | //-----------------------------------------------------------------------------
|
|---|
| 553 | // Scheduler routines
|
|---|
| [14a61b5] | 554 | // KERNEL ONLY
|
|---|
| [ac2b598] | 555 | void __schedule_thread( $thread * thrd ) with( *thrd->curr_cluster ) {
|
|---|
| [9f575ea] | 556 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [3381ed7] | 557 | /* paranoid */ #if defined( __CFA_WITH_VERIFY__ )
|
|---|
| [ae7be7a] | 558 | /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION,
|
|---|
| [3381ed7] | 559 | "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted );
|
|---|
| [b0c7419] | 560 | /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active || thrd->state == Rerun,
|
|---|
| [3381ed7] | 561 | "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted );
|
|---|
| 562 | /* paranoid */ #endif
|
|---|
| [9f575ea] | 563 | /* paranoid */ verifyf( thrd->next == 0p, "Expected null got %p", thrd->next );
|
|---|
| 564 |
|
|---|
| [ae7be7a] | 565 | if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready;
|
|---|
| 566 |
|
|---|
| [9f575ea] | 567 | lock ( ready_queue_lock __cfaabi_dbg_ctx2 );
|
|---|
| 568 | bool was_empty = !(ready_queue != 0);
|
|---|
| 569 | append( ready_queue, thrd );
|
|---|
| 570 | unlock( ready_queue_lock );
|
|---|
| [6b4cdd3] | 571 |
|
|---|
| [9f575ea] | 572 | if(was_empty) {
|
|---|
| 573 | lock (proc_list_lock __cfaabi_dbg_ctx2);
|
|---|
| 574 | if(idles) {
|
|---|
| 575 | wake_fast(idles.head);
|
|---|
| [85b1deb] | 576 | }
|
|---|
| [9f575ea] | 577 | unlock (proc_list_lock);
|
|---|
| 578 | }
|
|---|
| 579 | else if( struct processor * idle = idles.head ) {
|
|---|
| 580 | wake_fast(idle);
|
|---|
| [65deb18] | 581 | }
|
|---|
| [1c273d0] | 582 |
|
|---|
| [9f575ea] | 583 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [db6f06a] | 584 | }
|
|---|
| 585 |
|
|---|
| [14a61b5] | 586 | // KERNEL ONLY
|
|---|
| [ac2b598] | 587 | static $thread * __next_thread(cluster * this) with( *this ) {
|
|---|
| [3381ed7] | 588 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| 589 |
|
|---|
| [65deb18] | 590 | lock( ready_queue_lock __cfaabi_dbg_ctx2 );
|
|---|
| [ac2b598] | 591 | $thread * head = pop_head( ready_queue );
|
|---|
| [65deb18] | 592 | unlock( ready_queue_lock );
|
|---|
| [eb2e723] | 593 |
|
|---|
| [3381ed7] | 594 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| 595 | return head;
|
|---|
| [75f3522] | 596 | }
|
|---|
| 597 |
|
|---|
| [ae66348] | 598 | void unpark( $thread * thrd __cfaabi_dbg_ctx_param2 ) {
|
|---|
| [3381ed7] | 599 | if( !thrd ) return;
|
|---|
| [0b33412] | 600 |
|
|---|
| [82ff5845] | 601 | disable_interrupts();
|
|---|
| [3381ed7] | 602 | static_assert(sizeof(thrd->state) == sizeof(int));
|
|---|
| [ae7be7a] | 603 |
|
|---|
| [ae66348] | 604 | // record activity
|
|---|
| 605 | __cfaabi_dbg_record_thrd( *thrd, false, caller );
|
|---|
| 606 |
|
|---|
| [b0c7419] | 607 | enum coroutine_state old_state = __atomic_exchange_n(&thrd->state, Rerun, __ATOMIC_SEQ_CST);
|
|---|
| [ae7be7a] | 608 | __cfaabi_dbg_debug_do( thrd->unpark_result = old_state; )
|
|---|
| [3381ed7] | 609 | switch(old_state) {
|
|---|
| 610 | case Active:
|
|---|
| 611 | // Wake won the race, the thread will reschedule/rerun itself
|
|---|
| 612 | break;
|
|---|
| [ae7be7a] | 613 | case Blocked:
|
|---|
| [3381ed7] | 614 | /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION );
|
|---|
| [0b33412] | 615 |
|
|---|
| [3381ed7] | 616 | // Wake lost the race,
|
|---|
| [ae7be7a] | 617 | thrd->state = Blocked;
|
|---|
| [8c50aed] | 618 | __schedule_thread( thrd );
|
|---|
| [3381ed7] | 619 | break;
|
|---|
| 620 | case Rerun:
|
|---|
| 621 | abort("More than one thread attempted to schedule thread %p\n", thrd);
|
|---|
| 622 | break;
|
|---|
| 623 | case Halted:
|
|---|
| 624 | case Start:
|
|---|
| 625 | case Primed:
|
|---|
| 626 | default:
|
|---|
| 627 | // This makes no sense, something is wrong abort
|
|---|
| 628 | abort();
|
|---|
| 629 | }
|
|---|
| [36982fc] | 630 | enable_interrupts( __cfaabi_dbg_ctx );
|
|---|
| [db6f06a] | 631 | }
|
|---|
| 632 |
|
|---|
| [ae66348] | 633 | void park( __cfaabi_dbg_ctx_param ) {
|
|---|
| [3381ed7] | 634 | /* paranoid */ verify( kernelTLS.preemption_state.enabled );
|
|---|
| [82ff5845] | 635 | disable_interrupts();
|
|---|
| [3381ed7] | 636 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| 637 | /* paranoid */ verify( kernelTLS.this_thread->preempted == __NO_PREEMPTION );
|
|---|
| [0b33412] | 638 |
|
|---|
| [ae66348] | 639 | // record activity
|
|---|
| 640 | __cfaabi_dbg_record_thrd( *kernelTLS.this_thread, true, caller );
|
|---|
| 641 |
|
|---|
| [82c948c] | 642 | returnToKernel();
|
|---|
| [0b33412] | 643 |
|
|---|
| [3381ed7] | 644 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [36982fc] | 645 | enable_interrupts( __cfaabi_dbg_ctx );
|
|---|
| [3381ed7] | 646 | /* paranoid */ verify( kernelTLS.preemption_state.enabled );
|
|---|
| [eb2e723] | 647 |
|
|---|
| [0c78741] | 648 | }
|
|---|
| 649 |
|
|---|
| [3381ed7] | 650 | // KERNEL ONLY
|
|---|
| [b0c7419] | 651 | void __leave_thread() {
|
|---|
| [3381ed7] | 652 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [82c948c] | 653 | returnToKernel();
|
|---|
| [b0c7419] | 654 | abort();
|
|---|
| [0c78741] | 655 | }
|
|---|
| 656 |
|
|---|
| [3381ed7] | 657 | // KERNEL ONLY
|
|---|
| 658 | bool force_yield( __Preemption_Reason reason ) {
|
|---|
| 659 | /* paranoid */ verify( kernelTLS.preemption_state.enabled );
|
|---|
| [09800e9] | 660 | disable_interrupts();
|
|---|
| [3381ed7] | 661 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [09800e9] | 662 |
|
|---|
| [ac2b598] | 663 | $thread * thrd = kernelTLS.this_thread;
|
|---|
| [b0c7419] | 664 | /* paranoid */ verify(thrd->state == Active || thrd->state == Rerun);
|
|---|
| [3381ed7] | 665 |
|
|---|
| 666 | // SKULLDUGGERY: It is possible that we are preempting this thread just before
|
|---|
| 667 | // it was going to park itself. If that is the case and it is already using the
|
|---|
| 668 | // intrusive fields then we can't use them to preempt the thread
|
|---|
| 669 | // If that is the case, abandon the preemption.
|
|---|
| 670 | bool preempted = false;
|
|---|
| 671 | if(thrd->next == 0p) {
|
|---|
| 672 | preempted = true;
|
|---|
| 673 | thrd->preempted = reason;
|
|---|
| 674 | returnToKernel();
|
|---|
| 675 | }
|
|---|
| [09800e9] | 676 |
|
|---|
| [3381ed7] | 677 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| 678 | enable_interrupts_noPoll();
|
|---|
| 679 | /* paranoid */ verify( kernelTLS.preemption_state.enabled );
|
|---|
| [f2b12406] | 680 |
|
|---|
| [3381ed7] | 681 | return preempted;
|
|---|
| [f2b12406] | 682 | }
|
|---|
| 683 |
|
|---|
| [fa21ac9] | 684 | //=============================================================================================
|
|---|
| 685 | // Kernel Setup logic
|
|---|
| 686 | //=============================================================================================
|
|---|
| [eb2e723] | 687 | //-----------------------------------------------------------------------------
|
|---|
| 688 | // Kernel boot procedures
|
|---|
| [8c50aed] | 689 | static void __kernel_startup(void) {
|
|---|
| [14a61b5] | 690 | verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [36982fc] | 691 | __cfaabi_dbg_print_safe("Kernel : Starting\n");
|
|---|
| [eb2e723] | 692 |
|
|---|
| [b2f6113] | 693 | __page_size = sysconf( _SC_PAGESIZE );
|
|---|
| 694 |
|
|---|
| [ea8b2f7] | 695 | __cfa_dbg_global_clusters.list{ __get };
|
|---|
| 696 | __cfa_dbg_global_clusters.lock{};
|
|---|
| [de94a60] | 697 |
|
|---|
| [de6319f] | 698 | // Initialize the main cluster
|
|---|
| 699 | mainCluster = (cluster *)&storage_mainCluster;
|
|---|
| 700 | (*mainCluster){"Main Cluster"};
|
|---|
| 701 |
|
|---|
| 702 | __cfaabi_dbg_print_safe("Kernel : Main cluster ready\n");
|
|---|
| 703 |
|
|---|
| [eb2e723] | 704 | // Start by initializing the main thread
|
|---|
| [1c273d0] | 705 | // SKULLDUGGERY: the mainThread steals the process main thread
|
|---|
| [969b3fe] | 706 | // which will then be scheduled by the mainProcessor normally
|
|---|
| [ac2b598] | 707 | mainThread = ($thread *)&storage_mainThread;
|
|---|
| [8fcbb4c] | 708 | current_stack_info_t info;
|
|---|
| [b2f6113] | 709 | info.storage = (__stack_t*)&storage_mainThreadCtx;
|
|---|
| [83a071f9] | 710 | (*mainThread){ &info };
|
|---|
| [eb2e723] | 711 |
|
|---|
| [36982fc] | 712 | __cfaabi_dbg_print_safe("Kernel : Main thread ready\n");
|
|---|
| [fa21ac9] | 713 |
|
|---|
| [bd98b58] | 714 |
|
|---|
| [de6319f] | 715 |
|
|---|
| 716 | // Construct the processor context of the main processor
|
|---|
| 717 | void ?{}(processorCtx_t & this, processor * proc) {
|
|---|
| 718 | (this.__cor){ "Processor" };
|
|---|
| [1805b1b] | 719 | this.__cor.starter = 0p;
|
|---|
| [de6319f] | 720 | this.proc = proc;
|
|---|
| 721 | }
|
|---|
| 722 |
|
|---|
| 723 | void ?{}(processor & this) with( this ) {
|
|---|
| 724 | name = "Main Processor";
|
|---|
| 725 | cltr = mainCluster;
|
|---|
| 726 | terminated{ 0 };
|
|---|
| 727 | do_terminate = false;
|
|---|
| [1805b1b] | 728 | preemption_alarm = 0p;
|
|---|
| [de6319f] | 729 | pending_preemption = false;
|
|---|
| 730 | kernel_thread = pthread_self();
|
|---|
| 731 |
|
|---|
| 732 | runner{ &this };
|
|---|
| 733 | __cfaabi_dbg_print_safe("Kernel : constructed main processor context %p\n", &runner);
|
|---|
| 734 | }
|
|---|
| [fa21ac9] | 735 |
|
|---|
| [969b3fe] | 736 | // Initialize the main processor and the main processor ctx
|
|---|
| [eb2e723] | 737 | // (the coroutine that contains the processing control flow)
|
|---|
| [969b3fe] | 738 | mainProcessor = (processor *)&storage_mainProcessor;
|
|---|
| [de6319f] | 739 | (*mainProcessor){};
|
|---|
| [eb2e723] | 740 |
|
|---|
| [dcb42b8] | 741 | //initialize the global state variables
|
|---|
| [14a61b5] | 742 | kernelTLS.this_processor = mainProcessor;
|
|---|
| 743 | kernelTLS.this_thread = mainThread;
|
|---|
| [eb2e723] | 744 |
|
|---|
| [82ff5845] | 745 | // Enable preemption
|
|---|
| 746 | kernel_start_preemption();
|
|---|
| 747 |
|
|---|
| [969b3fe] | 748 | // Add the main thread to the ready queue
|
|---|
| 749 | // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
|
|---|
| [8c50aed] | 750 | __schedule_thread(mainThread);
|
|---|
| [969b3fe] | 751 |
|
|---|
| 752 | // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
|
|---|
| [c7a900a] | 753 | // context. Hence, the main thread does not begin through __cfactx_invoke_thread, like all other threads. The trick here is that
|
|---|
| [1c273d0] | 754 | // mainThread is on the ready queue when this call is made.
|
|---|
| [8c50aed] | 755 | __kernel_first_resume( kernelTLS.this_processor );
|
|---|
| [eb2e723] | 756 |
|
|---|
| [dcb42b8] | 757 |
|
|---|
| 758 |
|
|---|
| 759 | // THE SYSTEM IS NOW COMPLETELY RUNNING
|
|---|
| [36982fc] | 760 | __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n");
|
|---|
| [82ff5845] | 761 |
|
|---|
| [14a61b5] | 762 | verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [36982fc] | 763 | enable_interrupts( __cfaabi_dbg_ctx );
|
|---|
| [afd550c] | 764 | verify( TL_GET( preemption_state.enabled ) );
|
|---|
| [eb2e723] | 765 | }
|
|---|
| 766 |
|
|---|
| [8c50aed] | 767 | static void __kernel_shutdown(void) {
|
|---|
| [36982fc] | 768 | __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
|
|---|
| [eb2e723] | 769 |
|
|---|
| [afd550c] | 770 | verify( TL_GET( preemption_state.enabled ) );
|
|---|
| [4e6fb8e] | 771 | disable_interrupts();
|
|---|
| [14a61b5] | 772 | verify( ! kernelTLS.preemption_state.enabled );
|
|---|
| [4e6fb8e] | 773 |
|
|---|
| [969b3fe] | 774 | // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
|
|---|
| [dcb42b8] | 775 | // When its coroutine terminates, it return control to the mainThread
|
|---|
| 776 | // which is currently here
|
|---|
| [ea8b2f7] | 777 | __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);
|
|---|
| [8c50aed] | 778 | __kernel_last_resume( kernelTLS.this_processor );
|
|---|
| [ea8b2f7] | 779 | mainThread->self_cor.state = Halted;
|
|---|
| [eb2e723] | 780 |
|
|---|
| [dcb42b8] | 781 | // THE SYSTEM IS NOW COMPLETELY STOPPED
|
|---|
| [eb2e723] | 782 |
|
|---|
| [82ff5845] | 783 | // Disable preemption
|
|---|
| 784 | kernel_stop_preemption();
|
|---|
| 785 |
|
|---|
| [969b3fe] | 786 | // Destroy the main processor and its context in reverse order of construction
|
|---|
| [dcb42b8] | 787 | // These were manually constructed so we need manually destroy them
|
|---|
| [210b8b3] | 788 | ^(*mainProcessor){};
|
|---|
| [eb2e723] | 789 |
|
|---|
| [dcb42b8] | 790 | // Final step, destroy the main thread since it is no longer needed
|
|---|
| 791 | // Since we provided a stack to this taxk it will not destroy anything
|
|---|
| [210b8b3] | 792 | /* paranoid */ verify(mainThread->self_cor.stack.storage == (__stack_t*)(((uintptr_t)&storage_mainThreadCtx)| 0x1));
|
|---|
| 793 | ^(*mainThread){};
|
|---|
| [eb2e723] | 794 |
|
|---|
| [ea8b2f7] | 795 | ^(__cfa_dbg_global_clusters.list){};
|
|---|
| 796 | ^(__cfa_dbg_global_clusters.lock){};
|
|---|
| [a1a17a74] | 797 |
|
|---|
| [36982fc] | 798 | __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
|
|---|
| [9d944b2] | 799 | }
|
|---|
| 800 |
|
|---|
| [14a61b5] | 801 | //=============================================================================================
|
|---|
| 802 | // Kernel Quiescing
|
|---|
| 803 | //=============================================================================================
|
|---|
| [8c50aed] | 804 | static void __halt(processor * this) with( *this ) {
|
|---|
| [85b1deb] | 805 | // verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
|
|---|
| [ea8b2f7] | 806 |
|
|---|
| [6b4cdd3] | 807 | with( *cltr ) {
|
|---|
| 808 | lock (proc_list_lock __cfaabi_dbg_ctx2);
|
|---|
| 809 | remove (procs, *this);
|
|---|
| 810 | push_front(idles, *this);
|
|---|
| 811 | unlock (proc_list_lock);
|
|---|
| 812 | }
|
|---|
| [14a61b5] | 813 |
|
|---|
| [6b4cdd3] | 814 | __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this);
|
|---|
| [14a61b5] | 815 |
|
|---|
| [85b1deb] | 816 | wait( idleLock );
|
|---|
| [14a61b5] | 817 |
|
|---|
| [6b4cdd3] | 818 | __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this);
|
|---|
| [14a61b5] | 819 |
|
|---|
| [6b4cdd3] | 820 | with( *cltr ) {
|
|---|
| 821 | lock (proc_list_lock __cfaabi_dbg_ctx2);
|
|---|
| 822 | remove (idles, *this);
|
|---|
| 823 | push_front(procs, *this);
|
|---|
| 824 | unlock (proc_list_lock);
|
|---|
| 825 | }
|
|---|
| 826 | }
|
|---|
| 827 |
|
|---|
| [dbe9b08] | 828 | //=============================================================================================
|
|---|
| 829 | // Unexpected Terminating logic
|
|---|
| 830 | //=============================================================================================
|
|---|
| [ea7d2b0] | 831 | static __spinlock_t kernel_abort_lock;
|
|---|
| [9d944b2] | 832 | static bool kernel_abort_called = false;
|
|---|
| 833 |
|
|---|
| [afd550c] | 834 | void * kernel_abort(void) __attribute__ ((__nothrow__)) {
|
|---|
| [9d944b2] | 835 | // abort cannot be recursively entered by the same or different processors because all signal handlers return when
|
|---|
| 836 | // the globalAbort flag is true.
|
|---|
| [36982fc] | 837 | lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
|
|---|
| [9d944b2] | 838 |
|
|---|
| 839 | // first task to abort ?
|
|---|
| [de94a60] | 840 | if ( kernel_abort_called ) { // not first task to abort ?
|
|---|
| [ea7d2b0] | 841 | unlock( kernel_abort_lock );
|
|---|
| [1c273d0] | 842 |
|
|---|
| [9d944b2] | 843 | sigset_t mask;
|
|---|
| 844 | sigemptyset( &mask );
|
|---|
| [de94a60] | 845 | sigaddset( &mask, SIGALRM ); // block SIGALRM signals
|
|---|
| [8a13c47] | 846 | sigaddset( &mask, SIGUSR1 ); // block SIGALRM signals
|
|---|
| 847 | sigsuspend( &mask ); // block the processor to prevent further damage during abort
|
|---|
| 848 | _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it
|
|---|
| [de94a60] | 849 | }
|
|---|
| 850 | else {
|
|---|
| 851 | kernel_abort_called = true;
|
|---|
| 852 | unlock( kernel_abort_lock );
|
|---|
| [9d944b2] | 853 | }
|
|---|
| 854 |
|
|---|
| [14a61b5] | 855 | return kernelTLS.this_thread;
|
|---|
| [9d944b2] | 856 | }
|
|---|
| 857 |
|
|---|
| 858 | void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
|
|---|
| [ac2b598] | 859 | $thread * thrd = kernel_data;
|
|---|
| [9d944b2] | 860 |
|
|---|
| [de94a60] | 861 | if(thrd) {
|
|---|
| 862 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
|
|---|
| [1c40091] | 863 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
|---|
| [de94a60] | 864 |
|
|---|
| [212c2187] | 865 | if ( &thrd->self_cor != thrd->curr_cor ) {
|
|---|
| 866 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
|
|---|
| [1c40091] | 867 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
|---|
| [de94a60] | 868 | }
|
|---|
| 869 | else {
|
|---|
| [1c40091] | 870 | __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 );
|
|---|
| [de94a60] | 871 | }
|
|---|
| [1c273d0] | 872 | }
|
|---|
| [9d944b2] | 873 | else {
|
|---|
| [de94a60] | 874 | int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
|
|---|
| [1c40091] | 875 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
|---|
| [9d944b2] | 876 | }
|
|---|
| 877 | }
|
|---|
| 878 |
|
|---|
| [2b8bc41] | 879 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
|
|---|
| [14a61b5] | 880 | return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2;
|
|---|
| [2b8bc41] | 881 | }
|
|---|
| 882 |
|
|---|
| [de94a60] | 883 | static __spinlock_t kernel_debug_lock;
|
|---|
| 884 |
|
|---|
| [9d944b2] | 885 | extern "C" {
|
|---|
| [1c40091] | 886 | void __cfaabi_bits_acquire() {
|
|---|
| [36982fc] | 887 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
|
|---|
| [9d944b2] | 888 | }
|
|---|
| 889 |
|
|---|
| [1c40091] | 890 | void __cfaabi_bits_release() {
|
|---|
| [ea7d2b0] | 891 | unlock( kernel_debug_lock );
|
|---|
| [9d944b2] | 892 | }
|
|---|
| [8118303] | 893 | }
|
|---|
| 894 |
|
|---|
| [fa21ac9] | 895 | //=============================================================================================
|
|---|
| 896 | // Kernel Utilities
|
|---|
| 897 | //=============================================================================================
|
|---|
| [bd98b58] | 898 | //-----------------------------------------------------------------------------
|
|---|
| 899 | // Locks
|
|---|
| [242a902] | 900 | void ?{}( semaphore & this, int count = 1 ) {
|
|---|
| 901 | (this.lock){};
|
|---|
| 902 | this.count = count;
|
|---|
| 903 | (this.waiting){};
|
|---|
| [db6f06a] | 904 | }
|
|---|
| [242a902] | 905 | void ^?{}(semaphore & this) {}
|
|---|
| [db6f06a] | 906 |
|
|---|
| [65deb18] | 907 | void P(semaphore & this) with( this ){
|
|---|
| 908 | lock( lock __cfaabi_dbg_ctx2 );
|
|---|
| 909 | count -= 1;
|
|---|
| 910 | if ( count < 0 ) {
|
|---|
| [bdeba0b] | 911 | // queue current task
|
|---|
| [14a61b5] | 912 | append( waiting, kernelTLS.this_thread );
|
|---|
| [bdeba0b] | 913 |
|
|---|
| 914 | // atomically release spin lock and block
|
|---|
| [3381ed7] | 915 | unlock( lock );
|
|---|
| [ae66348] | 916 | park( __cfaabi_dbg_ctx );
|
|---|
| [8def349] | 917 | }
|
|---|
| [4e6fb8e] | 918 | else {
|
|---|
| [65deb18] | 919 | unlock( lock );
|
|---|
| [4e6fb8e] | 920 | }
|
|---|
| [bd98b58] | 921 | }
|
|---|
| 922 |
|
|---|
| [f0ce5f4] | 923 | bool V(semaphore & this) with( this ) {
|
|---|
| [ac2b598] | 924 | $thread * thrd = 0p;
|
|---|
| [65deb18] | 925 | lock( lock __cfaabi_dbg_ctx2 );
|
|---|
| 926 | count += 1;
|
|---|
| 927 | if ( count <= 0 ) {
|
|---|
| [bdeba0b] | 928 | // remove task at head of waiting list
|
|---|
| [65deb18] | 929 | thrd = pop_head( waiting );
|
|---|
| [bd98b58] | 930 | }
|
|---|
| [bdeba0b] | 931 |
|
|---|
| [65deb18] | 932 | unlock( lock );
|
|---|
| [bdeba0b] | 933 |
|
|---|
| 934 | // make new owner
|
|---|
| [ae66348] | 935 | unpark( thrd __cfaabi_dbg_ctx2 );
|
|---|
| [f0ce5f4] | 936 |
|
|---|
| 937 | return thrd != 0p;
|
|---|
| [bd98b58] | 938 | }
|
|---|
| 939 |
|
|---|
| [f7d6bb0] | 940 | //-----------------------------------------------------------------------------
|
|---|
| [de94a60] | 941 | // Global Queues
|
|---|
| 942 | void doregister( cluster & cltr ) {
|
|---|
| [ea8b2f7] | 943 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
|
|---|
| 944 | push_front( __cfa_dbg_global_clusters.list, cltr );
|
|---|
| 945 | unlock ( __cfa_dbg_global_clusters.lock );
|
|---|
| [de94a60] | 946 | }
|
|---|
| [f7d6bb0] | 947 |
|
|---|
| [de94a60] | 948 | void unregister( cluster & cltr ) {
|
|---|
| [ea8b2f7] | 949 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
|
|---|
| 950 | remove( __cfa_dbg_global_clusters.list, cltr );
|
|---|
| 951 | unlock( __cfa_dbg_global_clusters.lock );
|
|---|
| [de94a60] | 952 | }
|
|---|
| [f7d6bb0] | 953 |
|
|---|
| [ac2b598] | 954 | void doregister( cluster * cltr, $thread & thrd ) {
|
|---|
| [a1a17a74] | 955 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2);
|
|---|
| [d4e68a6] | 956 | cltr->nthreads += 1;
|
|---|
| [a1a17a74] | 957 | push_front(cltr->threads, thrd);
|
|---|
| 958 | unlock (cltr->thread_list_lock);
|
|---|
| 959 | }
|
|---|
| 960 |
|
|---|
| [ac2b598] | 961 | void unregister( cluster * cltr, $thread & thrd ) {
|
|---|
| [a1a17a74] | 962 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2);
|
|---|
| 963 | remove(cltr->threads, thrd );
|
|---|
| [d4e68a6] | 964 | cltr->nthreads -= 1;
|
|---|
| [a1a17a74] | 965 | unlock(cltr->thread_list_lock);
|
|---|
| 966 | }
|
|---|
| [9181f1d] | 967 |
|
|---|
| [de94a60] | 968 | void doregister( cluster * cltr, processor * proc ) {
|
|---|
| [639991a] | 969 | lock (cltr->proc_list_lock __cfaabi_dbg_ctx2);
|
|---|
| [d4e68a6] | 970 | cltr->nprocessors += 1;
|
|---|
| [639991a] | 971 | push_front(cltr->procs, *proc);
|
|---|
| 972 | unlock (cltr->proc_list_lock);
|
|---|
| [de94a60] | 973 | }
|
|---|
| 974 |
|
|---|
| 975 | void unregister( cluster * cltr, processor * proc ) {
|
|---|
| [639991a] | 976 | lock (cltr->proc_list_lock __cfaabi_dbg_ctx2);
|
|---|
| 977 | remove(cltr->procs, *proc );
|
|---|
| [d4e68a6] | 978 | cltr->nprocessors -= 1;
|
|---|
| [639991a] | 979 | unlock(cltr->proc_list_lock);
|
|---|
| [de94a60] | 980 | }
|
|---|
| 981 |
|
|---|
| 982 | //-----------------------------------------------------------------------------
|
|---|
| 983 | // Debug
|
|---|
| 984 | __cfaabi_dbg_debug_do(
|
|---|
| [1997b4e] | 985 | extern "C" {
|
|---|
| [ae66348] | 986 | void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) {
|
|---|
| [1997b4e] | 987 | this.prev_name = prev_name;
|
|---|
| 988 | this.prev_thrd = kernelTLS.this_thread;
|
|---|
| 989 | }
|
|---|
| [ae66348] | 990 |
|
|---|
| 991 | void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]) {
|
|---|
| 992 | if(park) {
|
|---|
| [f0ce5f4] | 993 | this.park_caller = prev_name;
|
|---|
| 994 | this.park_stale = false;
|
|---|
| [ae66348] | 995 | }
|
|---|
| 996 | else {
|
|---|
| [f0ce5f4] | 997 | this.unpark_caller = prev_name;
|
|---|
| 998 | this.unpark_stale = false;
|
|---|
| [ae66348] | 999 | }
|
|---|
| 1000 | }
|
|---|
| [9181f1d] | 1001 | }
|
|---|
| [f7d6bb0] | 1002 | )
|
|---|
| [2026bb6] | 1003 |
|
|---|
| 1004 | //-----------------------------------------------------------------------------
|
|---|
| 1005 | // Debug
|
|---|
| [8c50aed] | 1006 | bool threading_enabled(void) __attribute__((const)) {
|
|---|
| [2026bb6] | 1007 | return true;
|
|---|
| 1008 | }
|
|---|
| [8118303] | 1009 | // Local Variables: //
|
|---|
| 1010 | // mode: c //
|
|---|
| 1011 | // tab-width: 4 //
|
|---|
| 1012 | // End: //
|
|---|