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