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