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