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