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