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