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