[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
|
---|
[381fdee] | 12 | // Last Modified On : Thu Feb 8 23:52:19 2018
|
---|
| 13 | // Update Count : 5
|
---|
[8118303] | 14 | //
|
---|
| 15 |
|
---|
| 16 | //C Includes
|
---|
[c84e80a] | 17 | #include <stddef.h>
|
---|
[eb2e723] | 18 | extern "C" {
|
---|
[9d944b2] | 19 | #include <stdio.h>
|
---|
[8fcbb4c] | 20 | #include <fenv.h>
|
---|
[eb2e723] | 21 | #include <sys/resource.h>
|
---|
[9d944b2] | 22 | #include <signal.h>
|
---|
| 23 | #include <unistd.h>
|
---|
[eb2e723] | 24 | }
|
---|
[8118303] | 25 |
|
---|
| 26 | //CFA Includes
|
---|
[2ac095d] | 27 | #include "kernel_private.h"
|
---|
[c81ebf9] | 28 | #include "preemption.h"
|
---|
[2ac095d] | 29 | #include "startup.h"
|
---|
[8118303] | 30 |
|
---|
| 31 | //Private includes
|
---|
| 32 | #define __CFA_INVOKE_PRIVATE__
|
---|
| 33 | #include "invoke.h"
|
---|
| 34 |
|
---|
[2ac095d] | 35 | //Start and stop routine for the kernel, declared first to make sure they run first
|
---|
| 36 | void kernel_startup(void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
|
---|
| 37 | void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
|
---|
| 38 |
|
---|
[8def349] | 39 | //-----------------------------------------------------------------------------
|
---|
| 40 | // Kernel storage
|
---|
[969b3fe] | 41 | KERNEL_STORAGE(cluster, mainCluster);
|
---|
| 42 | KERNEL_STORAGE(processor, mainProcessor);
|
---|
| 43 | KERNEL_STORAGE(processorCtx_t, mainProcessorCtx);
|
---|
| 44 | KERNEL_STORAGE(thread_desc, mainThread);
|
---|
[f2b12406] | 45 | KERNEL_STORAGE(machine_context_t, mainThreadCtx);
|
---|
[8def349] | 46 |
|
---|
[969b3fe] | 47 | cluster * mainCluster;
|
---|
| 48 | processor * mainProcessor;
|
---|
[348006f] | 49 | thread_desc * mainThread;
|
---|
[eb2e723] | 50 |
|
---|
[bd98b58] | 51 | //-----------------------------------------------------------------------------
|
---|
| 52 | // Global state
|
---|
| 53 |
|
---|
[9cc0472] | 54 | thread_local coroutine_desc * volatile this_coroutine;
|
---|
| 55 | thread_local thread_desc * volatile this_thread;
|
---|
| 56 | thread_local processor * volatile this_processor;
|
---|
[969b3fe] | 57 |
|
---|
[d6ff3ff] | 58 | volatile thread_local bool preemption_in_progress = 0;
|
---|
[d0a045c7] | 59 | volatile thread_local bool preemption_enabled = false;
|
---|
[1c273d0] | 60 | volatile thread_local unsigned short disable_preempt_count = 1;
|
---|
[c84e80a] | 61 |
|
---|
| 62 | //-----------------------------------------------------------------------------
|
---|
[8def349] | 63 | // Main thread construction
|
---|
| 64 | struct current_stack_info_t {
|
---|
[1c273d0] | 65 | machine_context_t ctx;
|
---|
[8def349] | 66 | unsigned int size; // size of stack
|
---|
| 67 | void *base; // base of stack
|
---|
| 68 | void *storage; // pointer to stack
|
---|
| 69 | void *limit; // stack grows towards stack limit
|
---|
| 70 | void *context; // address of cfa_context_t
|
---|
| 71 | void *top; // address of top of storage
|
---|
[c84e80a] | 72 | };
|
---|
| 73 |
|
---|
[242a902] | 74 | void ?{}( current_stack_info_t & this ) {
|
---|
| 75 | CtxGet( this.ctx );
|
---|
| 76 | this.base = this.ctx.FP;
|
---|
| 77 | this.storage = this.ctx.SP;
|
---|
[8def349] | 78 |
|
---|
| 79 | rlimit r;
|
---|
[132fad4] | 80 | getrlimit( RLIMIT_STACK, &r);
|
---|
[242a902] | 81 | this.size = r.rlim_cur;
|
---|
[8def349] | 82 |
|
---|
[242a902] | 83 | this.limit = (void *)(((intptr_t)this.base) - this.size);
|
---|
[9236060] | 84 | this.context = &storage_mainThreadCtx;
|
---|
[242a902] | 85 | this.top = this.base;
|
---|
[8def349] | 86 | }
|
---|
| 87 |
|
---|
[65deb18] | 88 | void ?{}( coStack_t & this, current_stack_info_t * info) with( this ) {
|
---|
| 89 | size = info->size;
|
---|
| 90 | storage = info->storage;
|
---|
| 91 | limit = info->limit;
|
---|
| 92 | base = info->base;
|
---|
| 93 | context = info->context;
|
---|
| 94 | top = info->top;
|
---|
| 95 | userStack = true;
|
---|
[8def349] | 96 | }
|
---|
| 97 |
|
---|
[65deb18] | 98 | void ?{}( coroutine_desc & this, current_stack_info_t * info) with( this ) {
|
---|
| 99 | stack{ info };
|
---|
| 100 | name = "Main Thread";
|
---|
| 101 | errno_ = 0;
|
---|
| 102 | state = Start;
|
---|
| 103 | starter = NULL;
|
---|
[8def349] | 104 | }
|
---|
| 105 |
|
---|
[65deb18] | 106 | void ?{}( thread_desc & this, current_stack_info_t * info) with( this ) {
|
---|
| 107 | self_cor{ info };
|
---|
[82c948c] | 108 | curr_cor = &self_cor;
|
---|
| 109 | self_mon.owner = &this;
|
---|
| 110 | self_mon.recursion = 1;
|
---|
| 111 | self_mon_p = &self_mon;
|
---|
| 112 | next = NULL;
|
---|
| 113 | __cfaabi_dbg_debug_do(
|
---|
| 114 | dbg_next = NULL;
|
---|
| 115 | dbg_prev = NULL;
|
---|
| 116 | __cfaabi_dbg_thread_register(&this);
|
---|
| 117 | )
|
---|
| 118 |
|
---|
| 119 | monitors{ &self_mon_p, 1, (fptr_t)0 };
|
---|
[8def349] | 120 | }
|
---|
[c84e80a] | 121 |
|
---|
[8def349] | 122 | //-----------------------------------------------------------------------------
|
---|
| 123 | // Processor coroutine
|
---|
[39fea2f] | 124 |
|
---|
| 125 | // Construct the processor context of the main processor
|
---|
[242a902] | 126 | void ?{}(processorCtx_t & this, processor * proc) {
|
---|
| 127 | (this.__cor){ "Processor" };
|
---|
[b462670] | 128 | this.__cor.starter = NULL;
|
---|
[242a902] | 129 | this.proc = proc;
|
---|
| 130 | proc->runner = &this;
|
---|
[8def349] | 131 | }
|
---|
| 132 |
|
---|
[39fea2f] | 133 | // Construct the processor context of non-main processors
|
---|
[242a902] | 134 | void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) {
|
---|
| 135 | (this.__cor){ info };
|
---|
| 136 | this.proc = proc;
|
---|
| 137 | proc->runner = &this;
|
---|
[8def349] | 138 | }
|
---|
| 139 |
|
---|
[242a902] | 140 | void ?{}(processor & this) {
|
---|
[969b3fe] | 141 | this{ mainCluster };
|
---|
[8def349] | 142 | }
|
---|
| 143 |
|
---|
[c40e7c5] | 144 | void ?{}(processor & this, cluster * cltr) with( this ) {
|
---|
[242a902] | 145 | this.cltr = cltr;
|
---|
[c40e7c5] | 146 | terminated{ 0 };
|
---|
| 147 | do_terminate = false;
|
---|
| 148 | preemption_alarm = NULL;
|
---|
| 149 | pending_preemption = false;
|
---|
[8def349] | 150 |
|
---|
[242a902] | 151 | start( &this );
|
---|
[c84e80a] | 152 | }
|
---|
| 153 |
|
---|
[c40e7c5] | 154 | void ?{}(processor & this, cluster * cltr, processorCtx_t & runner) with( this ) {
|
---|
[242a902] | 155 | this.cltr = cltr;
|
---|
[c40e7c5] | 156 | terminated{ 0 };
|
---|
| 157 | do_terminate = false;
|
---|
| 158 | preemption_alarm = NULL;
|
---|
| 159 | pending_preemption = false;
|
---|
| 160 | kernel_thread = pthread_self();
|
---|
[8def349] | 161 |
|
---|
[83a071f9] | 162 | this.runner = &runner;
|
---|
[36982fc] | 163 | __cfaabi_dbg_print_safe("Kernel : constructing main processor context %p\n", &runner);
|
---|
[83a071f9] | 164 | runner{ &this };
|
---|
[8def349] | 165 | }
|
---|
| 166 |
|
---|
[65deb18] | 167 | void ^?{}(processor & this) with( this ){
|
---|
| 168 | if( ! do_terminate ) {
|
---|
[36982fc] | 169 | __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
|
---|
[4dad189] | 170 | terminate(&this);
|
---|
[65deb18] | 171 | P( terminated );
|
---|
| 172 | pthread_join( kernel_thread, NULL );
|
---|
[8def349] | 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[65deb18] | 176 | void ?{}(cluster & this) with( this ) {
|
---|
| 177 | ready_queue{};
|
---|
| 178 | ready_queue_lock{};
|
---|
[e60e0dc] | 179 |
|
---|
[65deb18] | 180 | preemption = default_preemption();
|
---|
[8def349] | 181 | }
|
---|
| 182 |
|
---|
[242a902] | 183 | void ^?{}(cluster & this) {
|
---|
[1c273d0] | 184 |
|
---|
[c84e80a] | 185 | }
|
---|
| 186 |
|
---|
[75f3522] | 187 | //=============================================================================================
|
---|
| 188 | // Kernel Scheduling logic
|
---|
| 189 | //=============================================================================================
|
---|
[8fcbb4c] | 190 | //Main of the processor contexts
|
---|
[83a071f9] | 191 | void main(processorCtx_t & runner) {
|
---|
| 192 | processor * this = runner.proc;
|
---|
[c81ebf9] | 193 |
|
---|
[36982fc] | 194 | __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this);
|
---|
[8118303] | 195 |
|
---|
[75f3522] | 196 | {
|
---|
[c81ebf9] | 197 | // Setup preemption data
|
---|
| 198 | preemption_scope scope = { this };
|
---|
| 199 |
|
---|
[36982fc] | 200 | __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
|
---|
[8118303] | 201 |
|
---|
[c81ebf9] | 202 | thread_desc * readyThread = NULL;
|
---|
[e60e0dc] | 203 | for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ )
|
---|
[75f3522] | 204 | {
|
---|
[c81ebf9] | 205 | readyThread = nextThread( this->cltr );
|
---|
[75f3522] | 206 |
|
---|
[c81ebf9] | 207 | if(readyThread)
|
---|
| 208 | {
|
---|
[d0a045c7] | 209 | verify( !preemption_enabled );
|
---|
[4e6fb8e] | 210 |
|
---|
[c81ebf9] | 211 | runThread(this, readyThread);
|
---|
[75f3522] | 212 |
|
---|
[d0a045c7] | 213 | verify( !preemption_enabled );
|
---|
[4e6fb8e] | 214 |
|
---|
[c81ebf9] | 215 | //Some actions need to be taken from the kernel
|
---|
| 216 | finishRunning(this);
|
---|
| 217 |
|
---|
| 218 | spin_count = 0;
|
---|
| 219 | }
|
---|
| 220 | else
|
---|
| 221 | {
|
---|
| 222 | spin(this, &spin_count);
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[36982fc] | 226 | __cfaabi_dbg_print_safe("Kernel : core %p stopping\n", this);
|
---|
[c84e80a] | 227 | }
|
---|
[8118303] | 228 |
|
---|
[4cedd9f] | 229 | V( this->terminated );
|
---|
[bdeba0b] | 230 |
|
---|
[36982fc] | 231 | __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this);
|
---|
[c84e80a] | 232 | }
|
---|
| 233 |
|
---|
[1c273d0] | 234 | // runThread runs a thread by context switching
|
---|
| 235 | // from the processor coroutine to the target thread
|
---|
[348006f] | 236 | void runThread(processor * this, thread_desc * dst) {
|
---|
[82c948c] | 237 | assert(dst->curr_cor);
|
---|
[83a071f9] | 238 | coroutine_desc * proc_cor = get_coroutine(*this->runner);
|
---|
[82c948c] | 239 | coroutine_desc * thrd_cor = dst->curr_cor;
|
---|
[1c273d0] | 240 |
|
---|
[75f3522] | 241 | //Reset the terminating actions here
|
---|
[db6f06a] | 242 | this->finish.action_code = No_Action;
|
---|
[8fcbb4c] | 243 |
|
---|
[75f3522] | 244 | //Update global state
|
---|
[1c273d0] | 245 | this_thread = dst;
|
---|
[75f3522] | 246 |
|
---|
| 247 | // Context Switch to the thread
|
---|
| 248 | ThreadCtxSwitch(proc_cor, thrd_cor);
|
---|
| 249 | // when ThreadCtxSwitch returns we are back in the processor coroutine
|
---|
| 250 | }
|
---|
| 251 |
|
---|
[82c948c] | 252 | void returnToKernel() {
|
---|
| 253 | coroutine_desc * proc_cor = get_coroutine(*this_processor->runner);
|
---|
| 254 | coroutine_desc * thrd_cor = this_thread->curr_cor = this_coroutine;
|
---|
| 255 | ThreadCtxSwitch(thrd_cor, proc_cor);
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[1c273d0] | 258 | // Once a thread has finished running, some of
|
---|
[75f3522] | 259 | // its final actions must be executed from the kernel
|
---|
[65deb18] | 260 | void finishRunning(processor * this) with( this->finish ) {
|
---|
| 261 | if( action_code == Release ) {
|
---|
[d0a045c7] | 262 | verify( !preemption_enabled );
|
---|
[65deb18] | 263 | unlock( *lock );
|
---|
[db6f06a] | 264 | }
|
---|
[65deb18] | 265 | else if( action_code == Schedule ) {
|
---|
| 266 | ScheduleThread( thrd );
|
---|
[db6f06a] | 267 | }
|
---|
[65deb18] | 268 | else if( action_code == Release_Schedule ) {
|
---|
[d0a045c7] | 269 | verify( !preemption_enabled );
|
---|
[65deb18] | 270 | unlock( *lock );
|
---|
| 271 | ScheduleThread( thrd );
|
---|
[db6f06a] | 272 | }
|
---|
[65deb18] | 273 | else if( action_code == Release_Multi ) {
|
---|
[d0a045c7] | 274 | verify( !preemption_enabled );
|
---|
[65deb18] | 275 | for(int i = 0; i < lock_count; i++) {
|
---|
| 276 | unlock( *locks[i] );
|
---|
[0c78741] | 277 | }
|
---|
| 278 | }
|
---|
[65deb18] | 279 | else if( action_code == Release_Multi_Schedule ) {
|
---|
| 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 | }
|
---|
| 286 | }
|
---|
[db6f06a] | 287 | else {
|
---|
[65deb18] | 288 | assert(action_code == No_Action);
|
---|
[8fcbb4c] | 289 | }
|
---|
[c84e80a] | 290 | }
|
---|
| 291 |
|
---|
[0c92c9f] | 292 | // Handles spinning logic
|
---|
| 293 | // TODO : find some strategy to put cores to sleep after some time
|
---|
[c84e80a] | 294 | void spin(processor * this, unsigned int * spin_count) {
|
---|
| 295 | (*spin_count)++;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
[0c92c9f] | 298 | // Context invoker for processors
|
---|
| 299 | // This is the entry point for processors (kernel threads)
|
---|
| 300 | // It effectively constructs a coroutine by stealing the pthread stack
|
---|
[8def349] | 301 | void * CtxInvokeProcessor(void * arg) {
|
---|
| 302 | processor * proc = (processor *) arg;
|
---|
| 303 | this_processor = proc;
|
---|
[1c273d0] | 304 | this_coroutine = NULL;
|
---|
| 305 | this_thread = NULL;
|
---|
[d0a045c7] | 306 | preemption_enabled = false;
|
---|
[4e6fb8e] | 307 | disable_preempt_count = 1;
|
---|
[8def349] | 308 | // SKULLDUGGERY: We want to create a context for the processor coroutine
|
---|
| 309 | // which is needed for the 2-step context switch. However, there is no reason
|
---|
[1c273d0] | 310 | // to waste the perfectly valid stack create by pthread.
|
---|
[8def349] | 311 | current_stack_info_t info;
|
---|
| 312 | machine_context_t ctx;
|
---|
| 313 | info.context = &ctx;
|
---|
| 314 | processorCtx_t proc_cor_storage = { proc, &info };
|
---|
| 315 |
|
---|
[36982fc] | 316 | __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", proc_cor_storage.__cor.stack.base);
|
---|
[8fcbb4c] | 317 |
|
---|
[0c92c9f] | 318 | //Set global state
|
---|
[1c273d0] | 319 | this_coroutine = &proc->runner->__cor;
|
---|
| 320 | this_thread = NULL;
|
---|
[8def349] | 321 |
|
---|
| 322 | //We now have a proper context from which to schedule threads
|
---|
[36982fc] | 323 | __cfaabi_dbg_print_safe("Kernel : core %p created (%p, %p)\n", proc, proc->runner, &ctx);
|
---|
[8def349] | 324 |
|
---|
[1c273d0] | 325 | // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
|
---|
| 326 | // resume it to start it like it normally would, it will just context switch
|
---|
| 327 | // back to here. Instead directly call the main since we already are on the
|
---|
[8def349] | 328 | // appropriate stack.
|
---|
[17af7d1] | 329 | proc_cor_storage.__cor.state = Active;
|
---|
[83a071f9] | 330 | main( proc_cor_storage );
|
---|
[4aa2fb2] | 331 | proc_cor_storage.__cor.state = Halted;
|
---|
[8def349] | 332 |
|
---|
[0c92c9f] | 333 | // Main routine of the core returned, the core is now fully terminated
|
---|
[36982fc] | 334 | __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, proc->runner);
|
---|
[8def349] | 335 |
|
---|
| 336 | return NULL;
|
---|
[c84e80a] | 337 | }
|
---|
| 338 |
|
---|
[8def349] | 339 | void start(processor * this) {
|
---|
[36982fc] | 340 | __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this);
|
---|
[82ff5845] | 341 |
|
---|
[8fcbb4c] | 342 | pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
|
---|
[eb2e723] | 343 |
|
---|
[36982fc] | 344 | __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
|
---|
[eb2e723] | 345 | }
|
---|
| 346 |
|
---|
[8def349] | 347 | //-----------------------------------------------------------------------------
|
---|
| 348 | // Scheduler routines
|
---|
[348006f] | 349 | void ScheduleThread( thread_desc * thrd ) {
|
---|
[1c273d0] | 350 | // if( !thrd ) return;
|
---|
[135b431] | 351 | verify( thrd );
|
---|
[b18830e] | 352 | verify( thrd->self_cor.state != Halted );
|
---|
[1c273d0] | 353 |
|
---|
[d0a045c7] | 354 | verify( !preemption_enabled );
|
---|
[690f13c] | 355 |
|
---|
[4aa2fb2] | 356 | verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
|
---|
[1c273d0] | 357 |
|
---|
[65deb18] | 358 | with( *this_processor->cltr ) {
|
---|
| 359 | lock ( ready_queue_lock __cfaabi_dbg_ctx2 );
|
---|
| 360 | append( ready_queue, thrd );
|
---|
| 361 | unlock( ready_queue_lock );
|
---|
| 362 | }
|
---|
[1c273d0] | 363 |
|
---|
[d0a045c7] | 364 | verify( !preemption_enabled );
|
---|
[db6f06a] | 365 | }
|
---|
| 366 |
|
---|
[65deb18] | 367 | thread_desc * nextThread(cluster * this) with( *this ) {
|
---|
[d0a045c7] | 368 | verify( !preemption_enabled );
|
---|
[65deb18] | 369 | lock( ready_queue_lock __cfaabi_dbg_ctx2 );
|
---|
| 370 | thread_desc * head = pop_head( ready_queue );
|
---|
| 371 | unlock( ready_queue_lock );
|
---|
[d0a045c7] | 372 | verify( !preemption_enabled );
|
---|
[db6f06a] | 373 | return head;
|
---|
[eb2e723] | 374 | }
|
---|
| 375 |
|
---|
[82ff5845] | 376 | void BlockInternal() {
|
---|
| 377 | disable_interrupts();
|
---|
[d0a045c7] | 378 | verify( !preemption_enabled );
|
---|
[82c948c] | 379 | returnToKernel();
|
---|
[d0a045c7] | 380 | verify( !preemption_enabled );
|
---|
[36982fc] | 381 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
[75f3522] | 382 | }
|
---|
| 383 |
|
---|
[ea7d2b0] | 384 | void BlockInternal( __spinlock_t * lock ) {
|
---|
[82ff5845] | 385 | disable_interrupts();
|
---|
[89a3df5] | 386 | this_processor->finish.action_code = Release;
|
---|
[65deb18] | 387 | this_processor->finish.lock = lock;
|
---|
[0b33412] | 388 |
|
---|
[d0a045c7] | 389 | verify( !preemption_enabled );
|
---|
[82c948c] | 390 | returnToKernel();
|
---|
[d0a045c7] | 391 | verify( !preemption_enabled );
|
---|
[0b33412] | 392 |
|
---|
[36982fc] | 393 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
[db6f06a] | 394 | }
|
---|
| 395 |
|
---|
[82ff5845] | 396 | void BlockInternal( thread_desc * thrd ) {
|
---|
| 397 | disable_interrupts();
|
---|
[89a3df5] | 398 | this_processor->finish.action_code = Schedule;
|
---|
[65deb18] | 399 | this_processor->finish.thrd = thrd;
|
---|
[0b33412] | 400 |
|
---|
[d0a045c7] | 401 | verify( !preemption_enabled );
|
---|
[82c948c] | 402 | returnToKernel();
|
---|
[d0a045c7] | 403 | verify( !preemption_enabled );
|
---|
[0b33412] | 404 |
|
---|
[36982fc] | 405 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
[db6f06a] | 406 | }
|
---|
| 407 |
|
---|
[ea7d2b0] | 408 | void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) {
|
---|
[97e3296] | 409 | assert(thrd);
|
---|
[82ff5845] | 410 | disable_interrupts();
|
---|
[89a3df5] | 411 | this_processor->finish.action_code = Release_Schedule;
|
---|
[65deb18] | 412 | this_processor->finish.lock = lock;
|
---|
| 413 | this_processor->finish.thrd = thrd;
|
---|
[0b33412] | 414 |
|
---|
[d0a045c7] | 415 | verify( !preemption_enabled );
|
---|
[82c948c] | 416 | returnToKernel();
|
---|
[d0a045c7] | 417 | verify( !preemption_enabled );
|
---|
[0b33412] | 418 |
|
---|
[36982fc] | 419 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
[eb2e723] | 420 | }
|
---|
| 421 |
|
---|
[ea7d2b0] | 422 | void BlockInternal(__spinlock_t * locks [], unsigned short count) {
|
---|
[82ff5845] | 423 | disable_interrupts();
|
---|
[0c78741] | 424 | this_processor->finish.action_code = Release_Multi;
|
---|
[65deb18] | 425 | this_processor->finish.locks = locks;
|
---|
| 426 | this_processor->finish.lock_count = count;
|
---|
[0b33412] | 427 |
|
---|
[d0a045c7] | 428 | verify( !preemption_enabled );
|
---|
[82c948c] | 429 | returnToKernel();
|
---|
[d0a045c7] | 430 | verify( !preemption_enabled );
|
---|
[0b33412] | 431 |
|
---|
[36982fc] | 432 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
[0c78741] | 433 | }
|
---|
| 434 |
|
---|
[ea7d2b0] | 435 | void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) {
|
---|
[82ff5845] | 436 | disable_interrupts();
|
---|
[0c78741] | 437 | this_processor->finish.action_code = Release_Multi_Schedule;
|
---|
[65deb18] | 438 | this_processor->finish.locks = locks;
|
---|
| 439 | this_processor->finish.lock_count = lock_count;
|
---|
| 440 | this_processor->finish.thrds = thrds;
|
---|
| 441 | this_processor->finish.thrd_count = thrd_count;
|
---|
[0b33412] | 442 |
|
---|
[d0a045c7] | 443 | verify( !preemption_enabled );
|
---|
[82c948c] | 444 | returnToKernel();
|
---|
[d0a045c7] | 445 | verify( !preemption_enabled );
|
---|
[0b33412] | 446 |
|
---|
[36982fc] | 447 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
[0c78741] | 448 | }
|
---|
| 449 |
|
---|
[ea7d2b0] | 450 | void LeaveThread(__spinlock_t * lock, thread_desc * thrd) {
|
---|
[d0a045c7] | 451 | verify( !preemption_enabled );
|
---|
[f2b12406] | 452 | this_processor->finish.action_code = thrd ? Release_Schedule : Release;
|
---|
[65deb18] | 453 | this_processor->finish.lock = lock;
|
---|
| 454 | this_processor->finish.thrd = thrd;
|
---|
[f2b12406] | 455 |
|
---|
[82c948c] | 456 | returnToKernel();
|
---|
[f2b12406] | 457 | }
|
---|
| 458 |
|
---|
[fa21ac9] | 459 | //=============================================================================================
|
---|
| 460 | // Kernel Setup logic
|
---|
| 461 | //=============================================================================================
|
---|
[eb2e723] | 462 | //-----------------------------------------------------------------------------
|
---|
| 463 | // Kernel boot procedures
|
---|
| 464 | void kernel_startup(void) {
|
---|
[36982fc] | 465 | __cfaabi_dbg_print_safe("Kernel : Starting\n");
|
---|
[eb2e723] | 466 |
|
---|
| 467 | // Start by initializing the main thread
|
---|
[1c273d0] | 468 | // SKULLDUGGERY: the mainThread steals the process main thread
|
---|
[969b3fe] | 469 | // which will then be scheduled by the mainProcessor normally
|
---|
| 470 | mainThread = (thread_desc *)&storage_mainThread;
|
---|
[8fcbb4c] | 471 | current_stack_info_t info;
|
---|
[83a071f9] | 472 | (*mainThread){ &info };
|
---|
[eb2e723] | 473 |
|
---|
[36982fc] | 474 | __cfaabi_dbg_print_safe("Kernel : Main thread ready\n");
|
---|
[fa21ac9] | 475 |
|
---|
[969b3fe] | 476 | // Initialize the main cluster
|
---|
| 477 | mainCluster = (cluster *)&storage_mainCluster;
|
---|
[9236060] | 478 | (*mainCluster){};
|
---|
[bd98b58] | 479 |
|
---|
[36982fc] | 480 | __cfaabi_dbg_print_safe("Kernel : main cluster ready\n");
|
---|
[fa21ac9] | 481 |
|
---|
[969b3fe] | 482 | // Initialize the main processor and the main processor ctx
|
---|
[eb2e723] | 483 | // (the coroutine that contains the processing control flow)
|
---|
[969b3fe] | 484 | mainProcessor = (processor *)&storage_mainProcessor;
|
---|
[9236060] | 485 | (*mainProcessor){ mainCluster, *(processorCtx_t *)&storage_mainProcessorCtx };
|
---|
[eb2e723] | 486 |
|
---|
[dcb42b8] | 487 | //initialize the global state variables
|
---|
[969b3fe] | 488 | this_processor = mainProcessor;
|
---|
[1c273d0] | 489 | this_thread = mainThread;
|
---|
[b18830e] | 490 | this_coroutine = &mainThread->self_cor;
|
---|
[eb2e723] | 491 |
|
---|
[82ff5845] | 492 | // Enable preemption
|
---|
| 493 | kernel_start_preemption();
|
---|
| 494 |
|
---|
[969b3fe] | 495 | // Add the main thread to the ready queue
|
---|
| 496 | // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
|
---|
| 497 | ScheduleThread(mainThread);
|
---|
| 498 |
|
---|
| 499 | // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
|
---|
[dcb42b8] | 500 | // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
|
---|
[1c273d0] | 501 | // mainThread is on the ready queue when this call is made.
|
---|
[9236060] | 502 | resume( *mainProcessor->runner );
|
---|
[eb2e723] | 503 |
|
---|
[dcb42b8] | 504 |
|
---|
| 505 |
|
---|
| 506 | // THE SYSTEM IS NOW COMPLETELY RUNNING
|
---|
[36982fc] | 507 | __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n");
|
---|
[82ff5845] | 508 |
|
---|
[36982fc] | 509 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
[eb2e723] | 510 | }
|
---|
| 511 |
|
---|
[dcb42b8] | 512 | void kernel_shutdown(void) {
|
---|
[36982fc] | 513 | __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
|
---|
[eb2e723] | 514 |
|
---|
[4e6fb8e] | 515 | disable_interrupts();
|
---|
| 516 |
|
---|
[969b3fe] | 517 | // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
|
---|
[dcb42b8] | 518 | // When its coroutine terminates, it return control to the mainThread
|
---|
| 519 | // which is currently here
|
---|
[969b3fe] | 520 | mainProcessor->do_terminate = true;
|
---|
[82c948c] | 521 | returnToKernel();
|
---|
[eb2e723] | 522 |
|
---|
[dcb42b8] | 523 | // THE SYSTEM IS NOW COMPLETELY STOPPED
|
---|
[eb2e723] | 524 |
|
---|
[82ff5845] | 525 | // Disable preemption
|
---|
| 526 | kernel_stop_preemption();
|
---|
| 527 |
|
---|
[969b3fe] | 528 | // Destroy the main processor and its context in reverse order of construction
|
---|
[dcb42b8] | 529 | // These were manually constructed so we need manually destroy them
|
---|
[9236060] | 530 | ^(*mainProcessor->runner){};
|
---|
[969b3fe] | 531 | ^(mainProcessor){};
|
---|
[eb2e723] | 532 |
|
---|
[dcb42b8] | 533 | // Final step, destroy the main thread since it is no longer needed
|
---|
| 534 | // Since we provided a stack to this taxk it will not destroy anything
|
---|
[eb2e723] | 535 | ^(mainThread){};
|
---|
| 536 |
|
---|
[36982fc] | 537 | __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
|
---|
[9d944b2] | 538 | }
|
---|
| 539 |
|
---|
[dbe9b08] | 540 | //=============================================================================================
|
---|
| 541 | // Unexpected Terminating logic
|
---|
| 542 | //=============================================================================================
|
---|
| 543 |
|
---|
| 544 |
|
---|
[ea7d2b0] | 545 | static __spinlock_t kernel_abort_lock;
|
---|
| 546 | static __spinlock_t kernel_debug_lock;
|
---|
[9d944b2] | 547 | static bool kernel_abort_called = false;
|
---|
| 548 |
|
---|
| 549 | void * kernel_abort (void) __attribute__ ((__nothrow__)) {
|
---|
| 550 | // abort cannot be recursively entered by the same or different processors because all signal handlers return when
|
---|
| 551 | // the globalAbort flag is true.
|
---|
[36982fc] | 552 | lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
|
---|
[9d944b2] | 553 |
|
---|
| 554 | // first task to abort ?
|
---|
| 555 | if ( !kernel_abort_called ) { // not first task to abort ?
|
---|
| 556 | kernel_abort_called = true;
|
---|
[ea7d2b0] | 557 | unlock( kernel_abort_lock );
|
---|
[1c273d0] | 558 | }
|
---|
[9d944b2] | 559 | else {
|
---|
[ea7d2b0] | 560 | unlock( kernel_abort_lock );
|
---|
[1c273d0] | 561 |
|
---|
[9d944b2] | 562 | sigset_t mask;
|
---|
| 563 | sigemptyset( &mask );
|
---|
| 564 | sigaddset( &mask, SIGALRM ); // block SIGALRM signals
|
---|
| 565 | sigaddset( &mask, SIGUSR1 ); // block SIGUSR1 signals
|
---|
| 566 | sigsuspend( &mask ); // block the processor to prevent further damage during abort
|
---|
[1c273d0] | 567 | _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it
|
---|
[9d944b2] | 568 | }
|
---|
| 569 |
|
---|
[1c273d0] | 570 | return this_thread;
|
---|
[9d944b2] | 571 | }
|
---|
| 572 |
|
---|
| 573 | void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
|
---|
| 574 | thread_desc * thrd = kernel_data;
|
---|
| 575 |
|
---|
[b18830e] | 576 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing task %.256s (%p)", thrd->self_cor.name, thrd );
|
---|
[36982fc] | 577 | __cfaabi_dbg_bits_write( abort_text, len );
|
---|
[9d944b2] | 578 |
|
---|
[1c273d0] | 579 | if ( thrd != this_coroutine ) {
|
---|
| 580 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine->name, this_coroutine );
|
---|
[36982fc] | 581 | __cfaabi_dbg_bits_write( abort_text, len );
|
---|
[1c273d0] | 582 | }
|
---|
[9d944b2] | 583 | else {
|
---|
[36982fc] | 584 | __cfaabi_dbg_bits_write( ".\n", 2 );
|
---|
[9d944b2] | 585 | }
|
---|
| 586 | }
|
---|
| 587 |
|
---|
[2b8bc41] | 588 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
|
---|
| 589 | return get_coroutine(this_thread) == get_coroutine(mainThread) ? 4 : 2;
|
---|
| 590 | }
|
---|
| 591 |
|
---|
[9d944b2] | 592 | extern "C" {
|
---|
[36982fc] | 593 | void __cfaabi_dbg_bits_acquire() {
|
---|
| 594 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
|
---|
[9d944b2] | 595 | }
|
---|
| 596 |
|
---|
[36982fc] | 597 | void __cfaabi_dbg_bits_release() {
|
---|
[ea7d2b0] | 598 | unlock( kernel_debug_lock );
|
---|
[9d944b2] | 599 | }
|
---|
[8118303] | 600 | }
|
---|
| 601 |
|
---|
[fa21ac9] | 602 | //=============================================================================================
|
---|
| 603 | // Kernel Utilities
|
---|
| 604 | //=============================================================================================
|
---|
[bd98b58] | 605 | //-----------------------------------------------------------------------------
|
---|
| 606 | // Locks
|
---|
[242a902] | 607 | void ?{}( semaphore & this, int count = 1 ) {
|
---|
| 608 | (this.lock){};
|
---|
| 609 | this.count = count;
|
---|
| 610 | (this.waiting){};
|
---|
[db6f06a] | 611 | }
|
---|
[242a902] | 612 | void ^?{}(semaphore & this) {}
|
---|
[db6f06a] | 613 |
|
---|
[65deb18] | 614 | void P(semaphore & this) with( this ){
|
---|
| 615 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
| 616 | count -= 1;
|
---|
| 617 | if ( count < 0 ) {
|
---|
[bdeba0b] | 618 | // queue current task
|
---|
[65deb18] | 619 | append( waiting, (thread_desc *)this_thread );
|
---|
[bdeba0b] | 620 |
|
---|
| 621 | // atomically release spin lock and block
|
---|
[65deb18] | 622 | BlockInternal( &lock );
|
---|
[8def349] | 623 | }
|
---|
[4e6fb8e] | 624 | else {
|
---|
[65deb18] | 625 | unlock( lock );
|
---|
[4e6fb8e] | 626 | }
|
---|
[bd98b58] | 627 | }
|
---|
| 628 |
|
---|
[65deb18] | 629 | void V(semaphore & this) with( this ) {
|
---|
[bdeba0b] | 630 | thread_desc * thrd = NULL;
|
---|
[65deb18] | 631 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
| 632 | count += 1;
|
---|
| 633 | if ( count <= 0 ) {
|
---|
[bdeba0b] | 634 | // remove task at head of waiting list
|
---|
[65deb18] | 635 | thrd = pop_head( waiting );
|
---|
[bd98b58] | 636 | }
|
---|
[bdeba0b] | 637 |
|
---|
[65deb18] | 638 | unlock( lock );
|
---|
[bdeba0b] | 639 |
|
---|
| 640 | // make new owner
|
---|
| 641 | WakeThread( thrd );
|
---|
[bd98b58] | 642 | }
|
---|
| 643 |
|
---|
[f7d6bb0] | 644 | //-----------------------------------------------------------------------------
|
---|
| 645 | // Debug
|
---|
| 646 | __cfaabi_dbg_debug_do(
|
---|
| 647 | struct {
|
---|
| 648 | thread_desc * tail;
|
---|
| 649 | } __cfaabi_dbg_thread_list = { NULL };
|
---|
| 650 |
|
---|
| 651 | void __cfaabi_dbg_thread_register( thread_desc * thrd ) {
|
---|
| 652 | if( !__cfaabi_dbg_thread_list.tail ) {
|
---|
| 653 | __cfaabi_dbg_thread_list.tail = thrd;
|
---|
| 654 | return;
|
---|
| 655 | }
|
---|
| 656 | __cfaabi_dbg_thread_list.tail->dbg_next = thrd;
|
---|
| 657 | thrd->dbg_prev = __cfaabi_dbg_thread_list.tail;
|
---|
| 658 | __cfaabi_dbg_thread_list.tail = thrd;
|
---|
| 659 | }
|
---|
| 660 |
|
---|
| 661 | void __cfaabi_dbg_thread_unregister( thread_desc * thrd ) {
|
---|
| 662 | thread_desc * prev = thrd->dbg_prev;
|
---|
| 663 | thread_desc * next = thrd->dbg_next;
|
---|
| 664 |
|
---|
| 665 | if( next ) { next->dbg_prev = prev; }
|
---|
| 666 | else {
|
---|
| 667 | assert( __cfaabi_dbg_thread_list.tail == thrd );
|
---|
| 668 | __cfaabi_dbg_thread_list.tail = prev;
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | if( prev ) { prev->dbg_next = next; }
|
---|
| 672 |
|
---|
| 673 | thrd->dbg_prev = NULL;
|
---|
| 674 | thrd->dbg_next = NULL;
|
---|
| 675 | }
|
---|
| 676 | )
|
---|
[8118303] | 677 | // Local Variables: //
|
---|
| 678 | // mode: c //
|
---|
| 679 | // tab-width: 4 //
|
---|
| 680 | // End: //
|
---|