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