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