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