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