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