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