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