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