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