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