[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 |
---|
[e3fea42] | 12 | // Last Modified On : Tue Feb 4 13:03:15 2020 |
---|
| 13 | // Update Count : 58 |
---|
[8118303] | 14 | // |
---|
| 15 | |
---|
[2026bb6] | 16 | #define __cforall_thread__ |
---|
[4069faad] | 17 | // #define __CFA_DEBUG_PRINT_RUNTIME_CORE__ |
---|
[2026bb6] | 18 | |
---|
[8118303] | 19 | //C Includes |
---|
[c84e80a] | 20 | #include <stddef.h> |
---|
[214e8da] | 21 | #include <errno.h> |
---|
[ea8b2f7] | 22 | #include <string.h> |
---|
[eb2e723] | 23 | extern "C" { |
---|
[9d944b2] | 24 | #include <stdio.h> |
---|
[8fcbb4c] | 25 | #include <fenv.h> |
---|
[eb2e723] | 26 | #include <sys/resource.h> |
---|
[58b6d1b] | 27 | #include <signal.h> |
---|
[9d944b2] | 28 | #include <unistd.h> |
---|
[27f5f71] | 29 | #include <limits.h> // PTHREAD_STACK_MIN |
---|
[1a3040c] | 30 | #include <sys/mman.h> // mprotect |
---|
[eb2e723] | 31 | } |
---|
[8118303] | 32 | |
---|
| 33 | //CFA Includes |
---|
[58b6d1b] | 34 | #include "time.hfa" |
---|
[73abe95] | 35 | #include "kernel_private.hfa" |
---|
| 36 | #include "preemption.hfa" |
---|
| 37 | #include "startup.hfa" |
---|
[8118303] | 38 | |
---|
| 39 | //Private includes |
---|
| 40 | #define __CFA_INVOKE_PRIVATE__ |
---|
| 41 | #include "invoke.h" |
---|
| 42 | |
---|
[4069faad] | 43 | |
---|
[deca0f5] | 44 | //----------------------------------------------------------------------------- |
---|
| 45 | // Some assembly required |
---|
[1805b1b] | 46 | #if defined( __i386 ) |
---|
[deca0f5] | 47 | #define CtxGet( ctx ) \ |
---|
| 48 | __asm__ volatile ( \ |
---|
| 49 | "movl %%esp,%0\n"\ |
---|
| 50 | "movl %%ebp,%1\n"\ |
---|
| 51 | : "=rm" (ctx.SP),\ |
---|
| 52 | "=rm" (ctx.FP) \ |
---|
| 53 | ) |
---|
| 54 | |
---|
| 55 | // mxcr : SSE Status and Control bits (control bits are preserved across function calls) |
---|
| 56 | // fcw : X87 FPU control word (preserved across function calls) |
---|
| 57 | #define __x87_store \ |
---|
| 58 | uint32_t __mxcr; \ |
---|
| 59 | uint16_t __fcw; \ |
---|
| 60 | __asm__ volatile ( \ |
---|
| 61 | "stmxcsr %0\n" \ |
---|
| 62 | "fnstcw %1\n" \ |
---|
| 63 | : "=m" (__mxcr),\ |
---|
| 64 | "=m" (__fcw) \ |
---|
| 65 | ) |
---|
| 66 | |
---|
| 67 | #define __x87_load \ |
---|
| 68 | __asm__ volatile ( \ |
---|
| 69 | "fldcw %1\n" \ |
---|
| 70 | "ldmxcsr %0\n" \ |
---|
| 71 | ::"m" (__mxcr),\ |
---|
| 72 | "m" (__fcw) \ |
---|
| 73 | ) |
---|
| 74 | |
---|
| 75 | #elif defined( __x86_64 ) |
---|
| 76 | #define CtxGet( ctx ) \ |
---|
| 77 | __asm__ volatile ( \ |
---|
| 78 | "movq %%rsp,%0\n"\ |
---|
| 79 | "movq %%rbp,%1\n"\ |
---|
| 80 | : "=rm" (ctx.SP),\ |
---|
| 81 | "=rm" (ctx.FP) \ |
---|
| 82 | ) |
---|
| 83 | |
---|
| 84 | #define __x87_store \ |
---|
| 85 | uint32_t __mxcr; \ |
---|
| 86 | uint16_t __fcw; \ |
---|
| 87 | __asm__ volatile ( \ |
---|
| 88 | "stmxcsr %0\n" \ |
---|
| 89 | "fnstcw %1\n" \ |
---|
| 90 | : "=m" (__mxcr),\ |
---|
| 91 | "=m" (__fcw) \ |
---|
| 92 | ) |
---|
| 93 | |
---|
| 94 | #define __x87_load \ |
---|
| 95 | __asm__ volatile ( \ |
---|
| 96 | "fldcw %1\n" \ |
---|
| 97 | "ldmxcsr %0\n" \ |
---|
| 98 | :: "m" (__mxcr),\ |
---|
| 99 | "m" (__fcw) \ |
---|
| 100 | ) |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | #elif defined( __ARM_ARCH ) |
---|
| 104 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 105 | "mov %0,%%sp\n" \ |
---|
| 106 | "mov %1,%%r11\n" \ |
---|
| 107 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
| 108 | #else |
---|
| 109 | #error unknown hardware architecture |
---|
| 110 | #endif |
---|
| 111 | |
---|
| 112 | //----------------------------------------------------------------------------- |
---|
[2ac095d] | 113 | //Start and stop routine for the kernel, declared first to make sure they run first |
---|
[8c50aed] | 114 | static void __kernel_startup (void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) )); |
---|
| 115 | static void __kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) )); |
---|
[2ac095d] | 116 | |
---|
[92e7631] | 117 | //----------------------------------------------------------------------------- |
---|
| 118 | // Kernel Scheduling logic |
---|
| 119 | static $thread * __next_thread(cluster * this); |
---|
| 120 | static void __run_thread(processor * this, $thread * dst); |
---|
| 121 | static $thread * __halt(processor * this); |
---|
| 122 | static bool __wake_one(cluster * cltr, bool was_empty); |
---|
| 123 | static bool __wake_proc(processor *); |
---|
| 124 | |
---|
[8def349] | 125 | //----------------------------------------------------------------------------- |
---|
| 126 | // Kernel storage |
---|
[b2f6113] | 127 | KERNEL_STORAGE(cluster, mainCluster); |
---|
| 128 | KERNEL_STORAGE(processor, mainProcessor); |
---|
[ac2b598] | 129 | KERNEL_STORAGE($thread, mainThread); |
---|
[b2f6113] | 130 | KERNEL_STORAGE(__stack_t, mainThreadCtx); |
---|
[8def349] | 131 | |
---|
[de6319f] | 132 | cluster * mainCluster; |
---|
| 133 | processor * mainProcessor; |
---|
[ac2b598] | 134 | $thread * mainThread; |
---|
[eb2e723] | 135 | |
---|
[ea8b2f7] | 136 | extern "C" { |
---|
[1805b1b] | 137 | struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters; |
---|
[ea8b2f7] | 138 | } |
---|
[de94a60] | 139 | |
---|
[b2f6113] | 140 | size_t __page_size = 0; |
---|
| 141 | |
---|
[bd98b58] | 142 | //----------------------------------------------------------------------------- |
---|
| 143 | // Global state |
---|
[afc2427] | 144 | thread_local struct KernelThreadData kernelTLS __attribute__ ((tls_model ( "initial-exec" ))) = { |
---|
[1805b1b] | 145 | NULL, // cannot use 0p |
---|
[b10affd] | 146 | NULL, |
---|
[09d4b22] | 147 | { 1, false, false }, |
---|
[21184e3] | 148 | 6u //this should be seeded better but due to a bug calling rdtsc doesn't work |
---|
[b10affd] | 149 | }; |
---|
[c84e80a] | 150 | |
---|
| 151 | //----------------------------------------------------------------------------- |
---|
[de6319f] | 152 | // Struct to steal stack |
---|
[8def349] | 153 | struct current_stack_info_t { |
---|
[1805b1b] | 154 | __stack_t * storage; // pointer to stack object |
---|
| 155 | void * base; // base of stack |
---|
| 156 | void * limit; // stack grows towards stack limit |
---|
| 157 | void * context; // address of cfa_context_t |
---|
[c84e80a] | 158 | }; |
---|
| 159 | |
---|
[242a902] | 160 | void ?{}( current_stack_info_t & this ) { |
---|
[b2f6113] | 161 | __stack_context_t ctx; |
---|
| 162 | CtxGet( ctx ); |
---|
| 163 | this.base = ctx.FP; |
---|
[8def349] | 164 | |
---|
| 165 | rlimit r; |
---|
[132fad4] | 166 | getrlimit( RLIMIT_STACK, &r); |
---|
[69a61d2] | 167 | size_t size = r.rlim_cur; |
---|
[8def349] | 168 | |
---|
[69a61d2] | 169 | this.limit = (void *)(((intptr_t)this.base) - size); |
---|
[9236060] | 170 | this.context = &storage_mainThreadCtx; |
---|
[8def349] | 171 | } |
---|
| 172 | |
---|
[de6319f] | 173 | //----------------------------------------------------------------------------- |
---|
| 174 | // Main thread construction |
---|
[8def349] | 175 | |
---|
[ac2b598] | 176 | void ?{}( $coroutine & this, current_stack_info_t * info) with( this ) { |
---|
[b2f6113] | 177 | stack.storage = info->storage; |
---|
| 178 | with(*stack.storage) { |
---|
| 179 | limit = info->limit; |
---|
| 180 | base = info->base; |
---|
| 181 | } |
---|
[ffe2fad] | 182 | __attribute__((may_alias)) intptr_t * istorage = (intptr_t*) &stack.storage; |
---|
| 183 | *istorage |= 0x1; |
---|
[65deb18] | 184 | name = "Main Thread"; |
---|
| 185 | state = Start; |
---|
[1805b1b] | 186 | starter = 0p; |
---|
| 187 | last = 0p; |
---|
| 188 | cancellation = 0p; |
---|
[8def349] | 189 | } |
---|
| 190 | |
---|
[ac2b598] | 191 | void ?{}( $thread & this, current_stack_info_t * info) with( this ) { |
---|
[e8e457e] | 192 | state = Start; |
---|
[65deb18] | 193 | self_cor{ info }; |
---|
[82c948c] | 194 | curr_cor = &self_cor; |
---|
[de6319f] | 195 | curr_cluster = mainCluster; |
---|
[82c948c] | 196 | self_mon.owner = &this; |
---|
| 197 | self_mon.recursion = 1; |
---|
| 198 | self_mon_p = &self_mon; |
---|
[1805b1b] | 199 | next = 0p; |
---|
[de94a60] | 200 | |
---|
[1805b1b] | 201 | node.next = 0p; |
---|
| 202 | node.prev = 0p; |
---|
[a1a17a74] | 203 | doregister(curr_cluster, this); |
---|
[82c948c] | 204 | |
---|
| 205 | monitors{ &self_mon_p, 1, (fptr_t)0 }; |
---|
[8def349] | 206 | } |
---|
[c84e80a] | 207 | |
---|
[8def349] | 208 | //----------------------------------------------------------------------------- |
---|
| 209 | // Processor coroutine |
---|
[de6319f] | 210 | void ?{}(processorCtx_t & this) { |
---|
[39fea2f] | 211 | |
---|
[8def349] | 212 | } |
---|
| 213 | |
---|
[39fea2f] | 214 | // Construct the processor context of non-main processors |
---|
[c29c342] | 215 | static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) { |
---|
[242a902] | 216 | (this.__cor){ info }; |
---|
| 217 | this.proc = proc; |
---|
[8def349] | 218 | } |
---|
| 219 | |
---|
[c7a900a] | 220 | static void * __invoke_processor(void * arg); |
---|
[8c50aed] | 221 | |
---|
[e3fea42] | 222 | void ?{}(processor & this, const char name[], cluster & cltr) with( this ) { |
---|
[de6319f] | 223 | this.name = name; |
---|
| 224 | this.cltr = &cltr; |
---|
[c40e7c5] | 225 | terminated{ 0 }; |
---|
[b0c7419] | 226 | destroyer = 0p; |
---|
[c40e7c5] | 227 | do_terminate = false; |
---|
[1805b1b] | 228 | preemption_alarm = 0p; |
---|
[c40e7c5] | 229 | pending_preemption = false; |
---|
[094476d] | 230 | runner.proc = &this; |
---|
[8def349] | 231 | |
---|
[92e7631] | 232 | idle{}; |
---|
[6b4cdd3] | 233 | |
---|
[4069faad] | 234 | __cfadbg_print_safe(runtime_core, "Kernel : Starting core %p\n", &this); |
---|
[8c50aed] | 235 | |
---|
[c7a900a] | 236 | this.stack = __create_pthread( &this.kernel_thread, __invoke_processor, (void *)&this ); |
---|
[8c50aed] | 237 | |
---|
[4069faad] | 238 | __cfadbg_print_safe(runtime_core, "Kernel : core %p created\n", &this); |
---|
[c84e80a] | 239 | } |
---|
| 240 | |
---|
[65deb18] | 241 | void ^?{}(processor & this) with( this ){ |
---|
[ea8b2f7] | 242 | if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) { |
---|
[4069faad] | 243 | __cfadbg_print_safe(runtime_core, "Kernel : core %p signaling termination\n", &this); |
---|
[85b1deb] | 244 | |
---|
| 245 | __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED); |
---|
[92e7631] | 246 | __wake_proc( &this ); |
---|
[85b1deb] | 247 | |
---|
[65deb18] | 248 | P( terminated ); |
---|
[14a61b5] | 249 | verify( kernelTLS.this_processor != &this); |
---|
[8def349] | 250 | } |
---|
[6b4cdd3] | 251 | |
---|
[c66f6cb] | 252 | int err = pthread_join( kernel_thread, 0p ); |
---|
| 253 | if( err != 0 ) abort("KERNEL ERROR: joining processor %p caused error %s\n", &this, strerror(err)); |
---|
| 254 | |
---|
[27f5f71] | 255 | free( this.stack ); |
---|
[8def349] | 256 | } |
---|
| 257 | |
---|
[b6f2b21] | 258 | void ?{}(cluster & this, const char name[], Duration preemption_rate, int io_flags) with( this ) { |
---|
[de6319f] | 259 | this.name = name; |
---|
| 260 | this.preemption_rate = preemption_rate; |
---|
[65deb18] | 261 | ready_queue{}; |
---|
| 262 | ready_queue_lock{}; |
---|
[de94a60] | 263 | |
---|
[038be32] | 264 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 265 | print_stats = false; |
---|
| 266 | #endif |
---|
| 267 | |
---|
[de94a60] | 268 | procs{ __get }; |
---|
| 269 | idles{ __get }; |
---|
[a1a17a74] | 270 | threads{ __get }; |
---|
[de94a60] | 271 | |
---|
[b6f2b21] | 272 | __kernel_io_startup( this, io_flags, &this == mainCluster ); |
---|
[92976d9] | 273 | |
---|
[de94a60] | 274 | doregister(this); |
---|
[8def349] | 275 | } |
---|
| 276 | |
---|
[242a902] | 277 | void ^?{}(cluster & this) { |
---|
[f6660520] | 278 | __kernel_io_shutdown( this, &this == mainCluster ); |
---|
[92976d9] | 279 | |
---|
[de94a60] | 280 | unregister(this); |
---|
[c84e80a] | 281 | } |
---|
| 282 | |
---|
[75f3522] | 283 | //============================================================================================= |
---|
| 284 | // Kernel Scheduling logic |
---|
| 285 | //============================================================================================= |
---|
[8fcbb4c] | 286 | //Main of the processor contexts |
---|
[83a071f9] | 287 | void main(processorCtx_t & runner) { |
---|
[21184e3] | 288 | // Because of a bug, we couldn't initialized the seed on construction |
---|
| 289 | // Do it here |
---|
[57c764c] | 290 | kernelTLS.rand_seed ^= rdtscl(); |
---|
[21184e3] | 291 | |
---|
[83a071f9] | 292 | processor * this = runner.proc; |
---|
[094476d] | 293 | verify(this); |
---|
[c81ebf9] | 294 | |
---|
[4069faad] | 295 | __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this); |
---|
[8118303] | 296 | |
---|
[de94a60] | 297 | doregister(this->cltr, this); |
---|
| 298 | |
---|
[75f3522] | 299 | { |
---|
[c81ebf9] | 300 | // Setup preemption data |
---|
| 301 | preemption_scope scope = { this }; |
---|
| 302 | |
---|
[4069faad] | 303 | __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this); |
---|
[8118303] | 304 | |
---|
[ac2b598] | 305 | $thread * readyThread = 0p; |
---|
[1a3040c] | 306 | for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ ) { |
---|
[92e7631] | 307 | // Try to get the next thread |
---|
[8c50aed] | 308 | readyThread = __next_thread( this->cltr ); |
---|
[75f3522] | 309 | |
---|
[92e7631] | 310 | // If no ready thread |
---|
| 311 | if( readyThread == 0p ) { |
---|
| 312 | // Block until a thread is ready |
---|
| 313 | readyThread = __halt(this); |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | // Check if we actually found a thread |
---|
| 317 | if( readyThread ) { |
---|
[3381ed7] | 318 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[92e7631] | 319 | /* paranoid */ verifyf( readyThread->state == Ready || readyThread->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", readyThread->state, readyThread->preempted); |
---|
[3381ed7] | 320 | /* paranoid */ verifyf( readyThread->next == 0p, "Expected null got %p", readyThread->next ); |
---|
[4e6fb8e] | 321 | |
---|
[92e7631] | 322 | // We found a thread run it |
---|
[8c50aed] | 323 | __run_thread(this, readyThread); |
---|
[75f3522] | 324 | |
---|
[3381ed7] | 325 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[c81ebf9] | 326 | } |
---|
| 327 | } |
---|
| 328 | |
---|
[4069faad] | 329 | __cfadbg_print_safe(runtime_core, "Kernel : core %p stopping\n", this); |
---|
[c84e80a] | 330 | } |
---|
[8118303] | 331 | |
---|
[de94a60] | 332 | unregister(this->cltr, this); |
---|
| 333 | |
---|
[92e7631] | 334 | V( this->terminated ); |
---|
[bdeba0b] | 335 | |
---|
[4069faad] | 336 | __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this); |
---|
[92e7631] | 337 | |
---|
| 338 | // HACK : the coroutine context switch expects this_thread to be set |
---|
| 339 | // and it make sense for it to be set in all other cases except here |
---|
| 340 | // fake it |
---|
| 341 | if( this == mainProcessor ) kernelTLS.this_thread = mainThread; |
---|
[c84e80a] | 342 | } |
---|
| 343 | |
---|
[5c1a531] | 344 | static int * __volatile_errno() __attribute__((noinline)); |
---|
| 345 | static int * __volatile_errno() { asm(""); return &errno; } |
---|
| 346 | |
---|
[14a61b5] | 347 | // KERNEL ONLY |
---|
[1c273d0] | 348 | // runThread runs a thread by context switching |
---|
| 349 | // from the processor coroutine to the target thread |
---|
[ac2b598] | 350 | static void __run_thread(processor * this, $thread * thrd_dst) { |
---|
| 351 | $coroutine * proc_cor = get_coroutine(this->runner); |
---|
[1c273d0] | 352 | |
---|
[14a61b5] | 353 | // Update global state |
---|
[e8e457e] | 354 | kernelTLS.this_thread = thrd_dst; |
---|
| 355 | |
---|
[9f575ea] | 356 | // set state of processor coroutine to inactive |
---|
| 357 | verify(proc_cor->state == Active); |
---|
[ae7be7a] | 358 | proc_cor->state = Blocked; |
---|
[e8e457e] | 359 | |
---|
[9f575ea] | 360 | // Actually run the thread |
---|
[3381ed7] | 361 | RUNNING: while(true) { |
---|
[9f575ea] | 362 | if(unlikely(thrd_dst->preempted)) { |
---|
[3381ed7] | 363 | thrd_dst->preempted = __NO_PREEMPTION; |
---|
[92e7631] | 364 | verify(thrd_dst->state == Active || thrd_dst->state == Rerun); |
---|
[9f575ea] | 365 | } else { |
---|
[92e7631] | 366 | verify(thrd_dst->state == Blocked || thrd_dst->state == Ready); // Ready means scheduled normally, blocked means rerun |
---|
[9f575ea] | 367 | thrd_dst->state = Active; |
---|
| 368 | } |
---|
| 369 | |
---|
[ae66348] | 370 | __cfaabi_dbg_debug_do( |
---|
[276ae57e] | 371 | thrd_dst->park_stale = true; |
---|
| 372 | thrd_dst->unpark_stale = true; |
---|
[ae66348] | 373 | ) |
---|
| 374 | |
---|
[3381ed7] | 375 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[210b8b3] | 376 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); // add escape condition if we are setting up the processor |
---|
| 377 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); // add escape condition if we are setting up the processor |
---|
[3381ed7] | 378 | |
---|
[9f575ea] | 379 | // set context switch to the thread that the processor is executing |
---|
| 380 | verify( thrd_dst->context.SP ); |
---|
[c7a900a] | 381 | __cfactx_switch( &proc_cor->context, &thrd_dst->context ); |
---|
| 382 | // when __cfactx_switch returns we are back in the processor coroutine |
---|
[9f575ea] | 383 | |
---|
[210b8b3] | 384 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); |
---|
| 385 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); |
---|
[3381ed7] | 386 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[75f3522] | 387 | |
---|
[3381ed7] | 388 | |
---|
| 389 | // We just finished running a thread, there are a few things that could have happened. |
---|
| 390 | // 1 - Regular case : the thread has blocked and now one has scheduled it yet. |
---|
| 391 | // 2 - Racy case : the thread has blocked but someone has already tried to schedule it. |
---|
| 392 | // 4 - Preempted |
---|
| 393 | // In case 1, we may have won a race so we can't write to the state again. |
---|
| 394 | // In case 2, we lost the race so we now own the thread. |
---|
| 395 | |
---|
| 396 | if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) { |
---|
| 397 | // The thread was preempted, reschedule it and reset the flag |
---|
[8c50aed] | 398 | __schedule_thread( thrd_dst ); |
---|
[3381ed7] | 399 | break RUNNING; |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | // set state of processor coroutine to active and the thread to inactive |
---|
| 403 | static_assert(sizeof(thrd_dst->state) == sizeof(int)); |
---|
[ae7be7a] | 404 | enum coroutine_state old_state = __atomic_exchange_n(&thrd_dst->state, Blocked, __ATOMIC_SEQ_CST); |
---|
| 405 | __cfaabi_dbg_debug_do( thrd_dst->park_result = old_state; ) |
---|
[3381ed7] | 406 | switch(old_state) { |
---|
| 407 | case Halted: |
---|
| 408 | // The thread has halted, it should never be scheduled/run again, leave it back to Halted and move on |
---|
| 409 | thrd_dst->state = Halted; |
---|
[b0c7419] | 410 | |
---|
| 411 | // We may need to wake someone up here since |
---|
[ae66348] | 412 | unpark( this->destroyer __cfaabi_dbg_ctx2 ); |
---|
[b0c7419] | 413 | this->destroyer = 0p; |
---|
[3381ed7] | 414 | break RUNNING; |
---|
| 415 | case Active: |
---|
| 416 | // This is case 1, the regular case, nothing more is needed |
---|
| 417 | break RUNNING; |
---|
| 418 | case Rerun: |
---|
| 419 | // This is case 2, the racy case, someone tried to run this thread before it finished blocking |
---|
| 420 | // In this case, just run it again. |
---|
| 421 | continue RUNNING; |
---|
| 422 | default: |
---|
| 423 | // This makes no sense, something is wrong abort |
---|
[ae7be7a] | 424 | abort("Finished running a thread that was Blocked/Start/Primed %d\n", old_state); |
---|
[3381ed7] | 425 | } |
---|
[9f575ea] | 426 | } |
---|
| 427 | |
---|
| 428 | // Just before returning to the processor, set the processor coroutine to active |
---|
[e8e457e] | 429 | proc_cor->state = Active; |
---|
[ae7be7a] | 430 | kernelTLS.this_thread = 0p; |
---|
[75f3522] | 431 | } |
---|
| 432 | |
---|
[14a61b5] | 433 | // KERNEL_ONLY |
---|
[b0c7419] | 434 | void returnToKernel() { |
---|
[3381ed7] | 435 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[ac2b598] | 436 | $coroutine * proc_cor = get_coroutine(kernelTLS.this_processor->runner); |
---|
| 437 | $thread * thrd_src = kernelTLS.this_thread; |
---|
[e8e457e] | 438 | |
---|
[9f575ea] | 439 | // Run the thread on this processor |
---|
| 440 | { |
---|
| 441 | int local_errno = *__volatile_errno(); |
---|
| 442 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
| 443 | __x87_store; |
---|
| 444 | #endif |
---|
| 445 | verify( proc_cor->context.SP ); |
---|
[c7a900a] | 446 | __cfactx_switch( &thrd_src->context, &proc_cor->context ); |
---|
[9f575ea] | 447 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
| 448 | __x87_load; |
---|
| 449 | #endif |
---|
| 450 | *__volatile_errno() = local_errno; |
---|
| 451 | } |
---|
[deca0f5] | 452 | |
---|
[3381ed7] | 453 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[210b8b3] | 454 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) < ((uintptr_t)__get_stack(thrd_src->curr_cor)->base ), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too small.\n", thrd_src ); |
---|
| 455 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) > ((uintptr_t)__get_stack(thrd_src->curr_cor)->limit), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too large.\n", thrd_src ); |
---|
[c84e80a] | 456 | } |
---|
| 457 | |
---|
[14a61b5] | 458 | // KERNEL_ONLY |
---|
[0c92c9f] | 459 | // Context invoker for processors |
---|
| 460 | // This is the entry point for processors (kernel threads) |
---|
| 461 | // It effectively constructs a coroutine by stealing the pthread stack |
---|
[c7a900a] | 462 | static void * __invoke_processor(void * arg) { |
---|
[8def349] | 463 | processor * proc = (processor *) arg; |
---|
[14a61b5] | 464 | kernelTLS.this_processor = proc; |
---|
[1805b1b] | 465 | kernelTLS.this_thread = 0p; |
---|
[14a61b5] | 466 | kernelTLS.preemption_state.[enabled, disable_count] = [false, 1]; |
---|
[8def349] | 467 | // SKULLDUGGERY: We want to create a context for the processor coroutine |
---|
| 468 | // which is needed for the 2-step context switch. However, there is no reason |
---|
[1c273d0] | 469 | // to waste the perfectly valid stack create by pthread. |
---|
[8def349] | 470 | current_stack_info_t info; |
---|
[b2f6113] | 471 | __stack_t ctx; |
---|
| 472 | info.storage = &ctx; |
---|
[094476d] | 473 | (proc->runner){ proc, &info }; |
---|
[8def349] | 474 | |
---|
[b2f6113] | 475 | __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage); |
---|
[8fcbb4c] | 476 | |
---|
[0c92c9f] | 477 | //Set global state |
---|
[1805b1b] | 478 | kernelTLS.this_thread = 0p; |
---|
[8def349] | 479 | |
---|
| 480 | //We now have a proper context from which to schedule threads |
---|
[4069faad] | 481 | __cfadbg_print_safe(runtime_core, "Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx); |
---|
[8def349] | 482 | |
---|
[1c273d0] | 483 | // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't |
---|
| 484 | // resume it to start it like it normally would, it will just context switch |
---|
| 485 | // back to here. Instead directly call the main since we already are on the |
---|
[8def349] | 486 | // appropriate stack. |
---|
[094476d] | 487 | get_coroutine(proc->runner)->state = Active; |
---|
| 488 | main( proc->runner ); |
---|
| 489 | get_coroutine(proc->runner)->state = Halted; |
---|
[8def349] | 490 | |
---|
[0c92c9f] | 491 | // Main routine of the core returned, the core is now fully terminated |
---|
[4069faad] | 492 | __cfadbg_print_safe(runtime_core, "Kernel : core %p main ended (%p)\n", proc, &proc->runner); |
---|
[8def349] | 493 | |
---|
[1805b1b] | 494 | return 0p; |
---|
[c84e80a] | 495 | } |
---|
| 496 | |
---|
[e3fea42] | 497 | static void Abort( int ret, const char func[] ) { |
---|
[1a3040c] | 498 | if ( ret ) { // pthread routines return errno values |
---|
[1805b1b] | 499 | abort( "%s : internal error, error(%d) %s.", func, ret, strerror( ret ) ); |
---|
[27f5f71] | 500 | } // if |
---|
[1805b1b] | 501 | } // Abort |
---|
| 502 | |
---|
[8c50aed] | 503 | void * __create_pthread( pthread_t * pthread, void * (*start)(void *), void * arg ) { |
---|
[1805b1b] | 504 | pthread_attr_t attr; |
---|
| 505 | |
---|
| 506 | Abort( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute |
---|
| 507 | |
---|
[27f5f71] | 508 | size_t stacksize; |
---|
[09d4b22] | 509 | // default stack size, normally defined by shell limit |
---|
[1a3040c] | 510 | Abort( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" ); |
---|
[27f5f71] | 511 | assert( stacksize >= PTHREAD_STACK_MIN ); |
---|
[1a3040c] | 512 | |
---|
[09d4b22] | 513 | void * stack; |
---|
| 514 | __cfaabi_dbg_debug_do( |
---|
| 515 | stack = memalign( __page_size, stacksize + __page_size ); |
---|
| 516 | // pthread has no mechanism to create the guard page in user supplied stack. |
---|
| 517 | if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) { |
---|
| 518 | abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) ); |
---|
| 519 | } // if |
---|
| 520 | ); |
---|
| 521 | __cfaabi_dbg_no_debug_do( |
---|
| 522 | stack = malloc( stacksize ); |
---|
| 523 | ); |
---|
[1a3040c] | 524 | |
---|
[09f357ec] | 525 | Abort( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" ); |
---|
[27f5f71] | 526 | |
---|
[1805b1b] | 527 | Abort( pthread_create( pthread, &attr, start, arg ), "pthread_create" ); |
---|
| 528 | return stack; |
---|
| 529 | } |
---|
[27f5f71] | 530 | |
---|
[14a61b5] | 531 | // KERNEL_ONLY |
---|
[8c50aed] | 532 | static void __kernel_first_resume( processor * this ) { |
---|
[ac2b598] | 533 | $thread * src = mainThread; |
---|
| 534 | $coroutine * dst = get_coroutine(this->runner); |
---|
[b69ea6b] | 535 | |
---|
[14a61b5] | 536 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[b69ea6b] | 537 | |
---|
[09f357ec] | 538 | kernelTLS.this_thread->curr_cor = dst; |
---|
[b2f6113] | 539 | __stack_prepare( &dst->stack, 65000 ); |
---|
[c7a900a] | 540 | __cfactx_start(main, dst, this->runner, __cfactx_invoke_coroutine); |
---|
[b69ea6b] | 541 | |
---|
[14a61b5] | 542 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[b69ea6b] | 543 | |
---|
[e8e457e] | 544 | dst->last = &src->self_cor; |
---|
| 545 | dst->starter = dst->starter ? dst->starter : &src->self_cor; |
---|
[b69ea6b] | 546 | |
---|
[92e7631] | 547 | // make sure the current state is still correct |
---|
| 548 | /* paranoid */ verify(src->state == Ready); |
---|
[b69ea6b] | 549 | |
---|
| 550 | // context switch to specified coroutine |
---|
[69a61d2] | 551 | verify( dst->context.SP ); |
---|
[c7a900a] | 552 | __cfactx_switch( &src->context, &dst->context ); |
---|
| 553 | // when __cfactx_switch returns we are back in the src coroutine |
---|
[b69ea6b] | 554 | |
---|
[09f357ec] | 555 | mainThread->curr_cor = &mainThread->self_cor; |
---|
| 556 | |
---|
[92e7631] | 557 | // make sure the current state has been update |
---|
| 558 | /* paranoid */ verify(src->state == Active); |
---|
[b69ea6b] | 559 | |
---|
[14a61b5] | 560 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[b69ea6b] | 561 | } |
---|
| 562 | |
---|
[e8e457e] | 563 | // KERNEL_ONLY |
---|
[8c50aed] | 564 | static void __kernel_last_resume( processor * this ) { |
---|
[ac2b598] | 565 | $coroutine * src = &mainThread->self_cor; |
---|
| 566 | $coroutine * dst = get_coroutine(this->runner); |
---|
[e8e457e] | 567 | |
---|
| 568 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 569 | verify( dst->starter == src ); |
---|
| 570 | verify( dst->context.SP ); |
---|
| 571 | |
---|
[f586539] | 572 | // SKULLDUGGERY in debug the processors check that the |
---|
| 573 | // stack is still within the limit of the stack limits after running a thread. |
---|
| 574 | // that check doesn't make sense if we context switch to the processor using the |
---|
| 575 | // coroutine semantics. Since this is a special case, use the current context |
---|
| 576 | // info to populate these fields. |
---|
| 577 | __cfaabi_dbg_debug_do( |
---|
| 578 | __stack_context_t ctx; |
---|
| 579 | CtxGet( ctx ); |
---|
| 580 | mainThread->context.SP = ctx.SP; |
---|
| 581 | mainThread->context.FP = ctx.FP; |
---|
| 582 | ) |
---|
| 583 | |
---|
[e8e457e] | 584 | // context switch to the processor |
---|
[c7a900a] | 585 | __cfactx_switch( &src->context, &dst->context ); |
---|
[e8e457e] | 586 | } |
---|
| 587 | |
---|
[8def349] | 588 | //----------------------------------------------------------------------------- |
---|
| 589 | // Scheduler routines |
---|
[14a61b5] | 590 | // KERNEL ONLY |
---|
[ac2b598] | 591 | void __schedule_thread( $thread * thrd ) with( *thrd->curr_cluster ) { |
---|
[9f575ea] | 592 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[3381ed7] | 593 | /* paranoid */ #if defined( __CFA_WITH_VERIFY__ ) |
---|
[ae7be7a] | 594 | /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION, |
---|
[3381ed7] | 595 | "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
[b0c7419] | 596 | /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active || thrd->state == Rerun, |
---|
[3381ed7] | 597 | "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
| 598 | /* paranoid */ #endif |
---|
[9f575ea] | 599 | /* paranoid */ verifyf( thrd->next == 0p, "Expected null got %p", thrd->next ); |
---|
| 600 | |
---|
[ae7be7a] | 601 | if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready; |
---|
| 602 | |
---|
[9f575ea] | 603 | lock ( ready_queue_lock __cfaabi_dbg_ctx2 ); |
---|
| 604 | bool was_empty = !(ready_queue != 0); |
---|
| 605 | append( ready_queue, thrd ); |
---|
| 606 | unlock( ready_queue_lock ); |
---|
[6b4cdd3] | 607 | |
---|
[92e7631] | 608 | __wake_one(thrd->curr_cluster, was_empty); |
---|
[1c273d0] | 609 | |
---|
[9f575ea] | 610 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[db6f06a] | 611 | } |
---|
| 612 | |
---|
[14a61b5] | 613 | // KERNEL ONLY |
---|
[ac2b598] | 614 | static $thread * __next_thread(cluster * this) with( *this ) { |
---|
[3381ed7] | 615 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 616 | |
---|
[65deb18] | 617 | lock( ready_queue_lock __cfaabi_dbg_ctx2 ); |
---|
[ac2b598] | 618 | $thread * head = pop_head( ready_queue ); |
---|
[65deb18] | 619 | unlock( ready_queue_lock ); |
---|
[eb2e723] | 620 | |
---|
[3381ed7] | 621 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 622 | return head; |
---|
[75f3522] | 623 | } |
---|
| 624 | |
---|
[2d8f7b0] | 625 | // KERNEL ONLY unpark with out disabling interrupts |
---|
| 626 | void __unpark( $thread * thrd __cfaabi_dbg_ctx_param2 ) { |
---|
[3381ed7] | 627 | static_assert(sizeof(thrd->state) == sizeof(int)); |
---|
[ae7be7a] | 628 | |
---|
[ae66348] | 629 | // record activity |
---|
| 630 | __cfaabi_dbg_record_thrd( *thrd, false, caller ); |
---|
| 631 | |
---|
[b0c7419] | 632 | enum coroutine_state old_state = __atomic_exchange_n(&thrd->state, Rerun, __ATOMIC_SEQ_CST); |
---|
[ae7be7a] | 633 | __cfaabi_dbg_debug_do( thrd->unpark_result = old_state; ) |
---|
[3381ed7] | 634 | switch(old_state) { |
---|
| 635 | case Active: |
---|
| 636 | // Wake won the race, the thread will reschedule/rerun itself |
---|
| 637 | break; |
---|
[ae7be7a] | 638 | case Blocked: |
---|
[3381ed7] | 639 | /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION ); |
---|
[0b33412] | 640 | |
---|
[3381ed7] | 641 | // Wake lost the race, |
---|
[ae7be7a] | 642 | thrd->state = Blocked; |
---|
[8c50aed] | 643 | __schedule_thread( thrd ); |
---|
[3381ed7] | 644 | break; |
---|
| 645 | case Rerun: |
---|
| 646 | abort("More than one thread attempted to schedule thread %p\n", thrd); |
---|
| 647 | break; |
---|
| 648 | case Halted: |
---|
| 649 | case Start: |
---|
| 650 | case Primed: |
---|
| 651 | default: |
---|
| 652 | // This makes no sense, something is wrong abort |
---|
| 653 | abort(); |
---|
| 654 | } |
---|
[2d8f7b0] | 655 | } |
---|
| 656 | |
---|
| 657 | void unpark( $thread * thrd __cfaabi_dbg_ctx_param2 ) { |
---|
| 658 | if( !thrd ) return; |
---|
| 659 | |
---|
| 660 | disable_interrupts(); |
---|
| 661 | __unpark( thrd __cfaabi_dbg_ctx_fwd2 ); |
---|
[36982fc] | 662 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[db6f06a] | 663 | } |
---|
| 664 | |
---|
[ae66348] | 665 | void park( __cfaabi_dbg_ctx_param ) { |
---|
[3381ed7] | 666 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
[82ff5845] | 667 | disable_interrupts(); |
---|
[3381ed7] | 668 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 669 | /* paranoid */ verify( kernelTLS.this_thread->preempted == __NO_PREEMPTION ); |
---|
[0b33412] | 670 | |
---|
[ae66348] | 671 | // record activity |
---|
| 672 | __cfaabi_dbg_record_thrd( *kernelTLS.this_thread, true, caller ); |
---|
| 673 | |
---|
[82c948c] | 674 | returnToKernel(); |
---|
[0b33412] | 675 | |
---|
[3381ed7] | 676 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[36982fc] | 677 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[3381ed7] | 678 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
[eb2e723] | 679 | |
---|
[0c78741] | 680 | } |
---|
| 681 | |
---|
[3381ed7] | 682 | // KERNEL ONLY |
---|
[b0c7419] | 683 | void __leave_thread() { |
---|
[3381ed7] | 684 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[82c948c] | 685 | returnToKernel(); |
---|
[b0c7419] | 686 | abort(); |
---|
[0c78741] | 687 | } |
---|
| 688 | |
---|
[3381ed7] | 689 | // KERNEL ONLY |
---|
| 690 | bool force_yield( __Preemption_Reason reason ) { |
---|
| 691 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
[09800e9] | 692 | disable_interrupts(); |
---|
[3381ed7] | 693 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[09800e9] | 694 | |
---|
[ac2b598] | 695 | $thread * thrd = kernelTLS.this_thread; |
---|
[b0c7419] | 696 | /* paranoid */ verify(thrd->state == Active || thrd->state == Rerun); |
---|
[3381ed7] | 697 | |
---|
| 698 | // SKULLDUGGERY: It is possible that we are preempting this thread just before |
---|
| 699 | // it was going to park itself. If that is the case and it is already using the |
---|
| 700 | // intrusive fields then we can't use them to preempt the thread |
---|
| 701 | // If that is the case, abandon the preemption. |
---|
| 702 | bool preempted = false; |
---|
| 703 | if(thrd->next == 0p) { |
---|
| 704 | preempted = true; |
---|
| 705 | thrd->preempted = reason; |
---|
| 706 | returnToKernel(); |
---|
| 707 | } |
---|
[09800e9] | 708 | |
---|
[3381ed7] | 709 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 710 | enable_interrupts_noPoll(); |
---|
| 711 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
[f2b12406] | 712 | |
---|
[3381ed7] | 713 | return preempted; |
---|
[f2b12406] | 714 | } |
---|
| 715 | |
---|
[fa21ac9] | 716 | //============================================================================================= |
---|
| 717 | // Kernel Setup logic |
---|
| 718 | //============================================================================================= |
---|
[eb2e723] | 719 | //----------------------------------------------------------------------------- |
---|
| 720 | // Kernel boot procedures |
---|
[8c50aed] | 721 | static void __kernel_startup(void) { |
---|
[14a61b5] | 722 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[4069faad] | 723 | __cfadbg_print_safe(runtime_core, "Kernel : Starting\n"); |
---|
[eb2e723] | 724 | |
---|
[b2f6113] | 725 | __page_size = sysconf( _SC_PAGESIZE ); |
---|
| 726 | |
---|
[ea8b2f7] | 727 | __cfa_dbg_global_clusters.list{ __get }; |
---|
| 728 | __cfa_dbg_global_clusters.lock{}; |
---|
[de94a60] | 729 | |
---|
[de6319f] | 730 | // Initialize the main cluster |
---|
| 731 | mainCluster = (cluster *)&storage_mainCluster; |
---|
| 732 | (*mainCluster){"Main Cluster"}; |
---|
| 733 | |
---|
[4069faad] | 734 | __cfadbg_print_safe(runtime_core, "Kernel : Main cluster ready\n"); |
---|
[de6319f] | 735 | |
---|
[eb2e723] | 736 | // Start by initializing the main thread |
---|
[1c273d0] | 737 | // SKULLDUGGERY: the mainThread steals the process main thread |
---|
[969b3fe] | 738 | // which will then be scheduled by the mainProcessor normally |
---|
[ac2b598] | 739 | mainThread = ($thread *)&storage_mainThread; |
---|
[8fcbb4c] | 740 | current_stack_info_t info; |
---|
[b2f6113] | 741 | info.storage = (__stack_t*)&storage_mainThreadCtx; |
---|
[83a071f9] | 742 | (*mainThread){ &info }; |
---|
[eb2e723] | 743 | |
---|
[4069faad] | 744 | __cfadbg_print_safe(runtime_core, "Kernel : Main thread ready\n"); |
---|
[fa21ac9] | 745 | |
---|
[bd98b58] | 746 | |
---|
[de6319f] | 747 | |
---|
| 748 | // Construct the processor context of the main processor |
---|
| 749 | void ?{}(processorCtx_t & this, processor * proc) { |
---|
| 750 | (this.__cor){ "Processor" }; |
---|
[1805b1b] | 751 | this.__cor.starter = 0p; |
---|
[de6319f] | 752 | this.proc = proc; |
---|
| 753 | } |
---|
| 754 | |
---|
| 755 | void ?{}(processor & this) with( this ) { |
---|
| 756 | name = "Main Processor"; |
---|
| 757 | cltr = mainCluster; |
---|
| 758 | terminated{ 0 }; |
---|
| 759 | do_terminate = false; |
---|
[1805b1b] | 760 | preemption_alarm = 0p; |
---|
[de6319f] | 761 | pending_preemption = false; |
---|
| 762 | kernel_thread = pthread_self(); |
---|
| 763 | |
---|
| 764 | runner{ &this }; |
---|
[4069faad] | 765 | __cfadbg_print_safe(runtime_core, "Kernel : constructed main processor context %p\n", &runner); |
---|
[de6319f] | 766 | } |
---|
[fa21ac9] | 767 | |
---|
[969b3fe] | 768 | // Initialize the main processor and the main processor ctx |
---|
[eb2e723] | 769 | // (the coroutine that contains the processing control flow) |
---|
[969b3fe] | 770 | mainProcessor = (processor *)&storage_mainProcessor; |
---|
[de6319f] | 771 | (*mainProcessor){}; |
---|
[eb2e723] | 772 | |
---|
[dcb42b8] | 773 | //initialize the global state variables |
---|
[14a61b5] | 774 | kernelTLS.this_processor = mainProcessor; |
---|
| 775 | kernelTLS.this_thread = mainThread; |
---|
[eb2e723] | 776 | |
---|
[82ff5845] | 777 | // Enable preemption |
---|
| 778 | kernel_start_preemption(); |
---|
| 779 | |
---|
[969b3fe] | 780 | // Add the main thread to the ready queue |
---|
| 781 | // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread |
---|
[8c50aed] | 782 | __schedule_thread(mainThread); |
---|
[969b3fe] | 783 | |
---|
| 784 | // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX |
---|
[c7a900a] | 785 | // context. Hence, the main thread does not begin through __cfactx_invoke_thread, like all other threads. The trick here is that |
---|
[1c273d0] | 786 | // mainThread is on the ready queue when this call is made. |
---|
[8c50aed] | 787 | __kernel_first_resume( kernelTLS.this_processor ); |
---|
[eb2e723] | 788 | |
---|
[dcb42b8] | 789 | |
---|
| 790 | // THE SYSTEM IS NOW COMPLETELY RUNNING |
---|
[f6660520] | 791 | |
---|
| 792 | |
---|
| 793 | // Now that the system is up, finish creating systems that need threading |
---|
| 794 | __kernel_io_finish_start( *mainCluster ); |
---|
| 795 | |
---|
| 796 | |
---|
| 797 | __cfadbg_print_safe(runtime_core, "Kernel : Started\n--------------------------------------------------\n\n"); |
---|
[82ff5845] | 798 | |
---|
[14a61b5] | 799 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[36982fc] | 800 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[afd550c] | 801 | verify( TL_GET( preemption_state.enabled ) ); |
---|
[eb2e723] | 802 | } |
---|
| 803 | |
---|
[8c50aed] | 804 | static void __kernel_shutdown(void) { |
---|
[f6660520] | 805 | //Before we start shutting things down, wait for systems that need threading to shutdown |
---|
| 806 | __kernel_io_prepare_stop( *mainCluster ); |
---|
[eb2e723] | 807 | |
---|
[92e7631] | 808 | /* paranoid */ verify( TL_GET( preemption_state.enabled ) ); |
---|
[4e6fb8e] | 809 | disable_interrupts(); |
---|
[92e7631] | 810 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[4e6fb8e] | 811 | |
---|
[f6660520] | 812 | __cfadbg_print_safe(runtime_core, "\n--------------------------------------------------\nKernel : Shutting down\n"); |
---|
| 813 | |
---|
[969b3fe] | 814 | // SKULLDUGGERY: Notify the mainProcessor it needs to terminates. |
---|
[dcb42b8] | 815 | // When its coroutine terminates, it return control to the mainThread |
---|
| 816 | // which is currently here |
---|
[ea8b2f7] | 817 | __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE); |
---|
[8c50aed] | 818 | __kernel_last_resume( kernelTLS.this_processor ); |
---|
[ea8b2f7] | 819 | mainThread->self_cor.state = Halted; |
---|
[eb2e723] | 820 | |
---|
[dcb42b8] | 821 | // THE SYSTEM IS NOW COMPLETELY STOPPED |
---|
[eb2e723] | 822 | |
---|
[82ff5845] | 823 | // Disable preemption |
---|
| 824 | kernel_stop_preemption(); |
---|
| 825 | |
---|
[969b3fe] | 826 | // Destroy the main processor and its context in reverse order of construction |
---|
[dcb42b8] | 827 | // These were manually constructed so we need manually destroy them |
---|
[c66f6cb] | 828 | void ^?{}(processor & this) with( this ){ |
---|
| 829 | /* paranoid */ verify( this.do_terminate == true ); |
---|
| 830 | } |
---|
| 831 | |
---|
[210b8b3] | 832 | ^(*mainProcessor){}; |
---|
[eb2e723] | 833 | |
---|
[dcb42b8] | 834 | // Final step, destroy the main thread since it is no longer needed |
---|
| 835 | // Since we provided a stack to this taxk it will not destroy anything |
---|
[210b8b3] | 836 | /* paranoid */ verify(mainThread->self_cor.stack.storage == (__stack_t*)(((uintptr_t)&storage_mainThreadCtx)| 0x1)); |
---|
| 837 | ^(*mainThread){}; |
---|
[eb2e723] | 838 | |
---|
[92976d9] | 839 | ^(*mainCluster){}; |
---|
| 840 | |
---|
[ea8b2f7] | 841 | ^(__cfa_dbg_global_clusters.list){}; |
---|
| 842 | ^(__cfa_dbg_global_clusters.lock){}; |
---|
[a1a17a74] | 843 | |
---|
[4069faad] | 844 | __cfadbg_print_safe(runtime_core, "Kernel : Shutdown complete\n"); |
---|
[9d944b2] | 845 | } |
---|
| 846 | |
---|
[14a61b5] | 847 | //============================================================================================= |
---|
[92e7631] | 848 | // Kernel Idle Sleep |
---|
[14a61b5] | 849 | //============================================================================================= |
---|
[92e7631] | 850 | static $thread * __halt(processor * this) with( *this ) { |
---|
| 851 | if( do_terminate ) return 0p; |
---|
[ea8b2f7] | 852 | |
---|
[92e7631] | 853 | // First, lock the cluster idle |
---|
| 854 | lock( cltr->idle_lock __cfaabi_dbg_ctx2 ); |
---|
| 855 | |
---|
| 856 | // Check if we can find a thread |
---|
| 857 | if( $thread * found = __next_thread( cltr ) ) { |
---|
| 858 | unlock( cltr->idle_lock ); |
---|
| 859 | return found; |
---|
[6b4cdd3] | 860 | } |
---|
[14a61b5] | 861 | |
---|
[92e7631] | 862 | // Move this processor from the active list to the idle list |
---|
| 863 | move_to_front(cltr->procs, cltr->idles, *this); |
---|
[14a61b5] | 864 | |
---|
[92e7631] | 865 | // Unlock the idle lock so we don't go to sleep with a lock |
---|
| 866 | unlock (cltr->idle_lock); |
---|
| 867 | |
---|
| 868 | // We are ready to sleep |
---|
[4069faad] | 869 | __cfadbg_print_safe(runtime_core, "Kernel : Processor %p ready to sleep\n", this); |
---|
[92e7631] | 870 | wait( idle ); |
---|
[14a61b5] | 871 | |
---|
[92e7631] | 872 | // We have woken up |
---|
[4069faad] | 873 | __cfadbg_print_safe(runtime_core, "Kernel : Processor %p woke up and ready to run\n", this); |
---|
[14a61b5] | 874 | |
---|
[92e7631] | 875 | // Get ourself off the idle list |
---|
[6b4cdd3] | 876 | with( *cltr ) { |
---|
[92e7631] | 877 | lock (idle_lock __cfaabi_dbg_ctx2); |
---|
| 878 | move_to_front(idles, procs, *this); |
---|
| 879 | unlock(idle_lock); |
---|
[6b4cdd3] | 880 | } |
---|
[92e7631] | 881 | |
---|
| 882 | // Don't check the ready queue again, we may not be in a position to run a thread |
---|
| 883 | return 0p; |
---|
| 884 | } |
---|
| 885 | |
---|
| 886 | // Wake a thread from the front if there are any |
---|
| 887 | static bool __wake_one(cluster * this, __attribute__((unused)) bool force) { |
---|
| 888 | // if we don't want to force check if we know it's false |
---|
[4069faad] | 889 | // if( !this->idles.head && !force ) return false; |
---|
[92e7631] | 890 | |
---|
| 891 | // First, lock the cluster idle |
---|
| 892 | lock( this->idle_lock __cfaabi_dbg_ctx2 ); |
---|
| 893 | |
---|
| 894 | // Check if there is someone to wake up |
---|
| 895 | if( !this->idles.head ) { |
---|
| 896 | // Nope unlock and return false |
---|
| 897 | unlock( this->idle_lock ); |
---|
| 898 | return false; |
---|
| 899 | } |
---|
| 900 | |
---|
| 901 | // Wake them up |
---|
[4069faad] | 902 | __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this->idles.head); |
---|
[92e7631] | 903 | post( this->idles.head->idle ); |
---|
| 904 | |
---|
| 905 | // Unlock and return true |
---|
| 906 | unlock( this->idle_lock ); |
---|
| 907 | return true; |
---|
| 908 | } |
---|
| 909 | |
---|
| 910 | // Unconditionnaly wake a thread |
---|
| 911 | static bool __wake_proc(processor * this) { |
---|
[4069faad] | 912 | __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this); |
---|
[92e7631] | 913 | return post( this->idle ); |
---|
[6b4cdd3] | 914 | } |
---|
| 915 | |
---|
[dbe9b08] | 916 | //============================================================================================= |
---|
| 917 | // Unexpected Terminating logic |
---|
| 918 | //============================================================================================= |
---|
[ea7d2b0] | 919 | static __spinlock_t kernel_abort_lock; |
---|
[9d944b2] | 920 | static bool kernel_abort_called = false; |
---|
| 921 | |
---|
[afd550c] | 922 | void * kernel_abort(void) __attribute__ ((__nothrow__)) { |
---|
[9d944b2] | 923 | // abort cannot be recursively entered by the same or different processors because all signal handlers return when |
---|
| 924 | // the globalAbort flag is true. |
---|
[36982fc] | 925 | lock( kernel_abort_lock __cfaabi_dbg_ctx2 ); |
---|
[9d944b2] | 926 | |
---|
| 927 | // first task to abort ? |
---|
[de94a60] | 928 | if ( kernel_abort_called ) { // not first task to abort ? |
---|
[ea7d2b0] | 929 | unlock( kernel_abort_lock ); |
---|
[1c273d0] | 930 | |
---|
[9d944b2] | 931 | sigset_t mask; |
---|
| 932 | sigemptyset( &mask ); |
---|
[de94a60] | 933 | sigaddset( &mask, SIGALRM ); // block SIGALRM signals |
---|
[8a13c47] | 934 | sigaddset( &mask, SIGUSR1 ); // block SIGALRM signals |
---|
| 935 | sigsuspend( &mask ); // block the processor to prevent further damage during abort |
---|
| 936 | _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it |
---|
[de94a60] | 937 | } |
---|
| 938 | else { |
---|
| 939 | kernel_abort_called = true; |
---|
| 940 | unlock( kernel_abort_lock ); |
---|
[9d944b2] | 941 | } |
---|
| 942 | |
---|
[14a61b5] | 943 | return kernelTLS.this_thread; |
---|
[9d944b2] | 944 | } |
---|
| 945 | |
---|
| 946 | void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) { |
---|
[ac2b598] | 947 | $thread * thrd = kernel_data; |
---|
[9d944b2] | 948 | |
---|
[de94a60] | 949 | if(thrd) { |
---|
| 950 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd ); |
---|
[1c40091] | 951 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
[de94a60] | 952 | |
---|
[212c2187] | 953 | if ( &thrd->self_cor != thrd->curr_cor ) { |
---|
| 954 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor ); |
---|
[1c40091] | 955 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
[de94a60] | 956 | } |
---|
| 957 | else { |
---|
[1c40091] | 958 | __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 ); |
---|
[de94a60] | 959 | } |
---|
[1c273d0] | 960 | } |
---|
[9d944b2] | 961 | else { |
---|
[de94a60] | 962 | int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" ); |
---|
[1c40091] | 963 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
[9d944b2] | 964 | } |
---|
| 965 | } |
---|
| 966 | |
---|
[2b8bc41] | 967 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) { |
---|
[14a61b5] | 968 | return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2; |
---|
[2b8bc41] | 969 | } |
---|
| 970 | |
---|
[de94a60] | 971 | static __spinlock_t kernel_debug_lock; |
---|
| 972 | |
---|
[9d944b2] | 973 | extern "C" { |
---|
[1c40091] | 974 | void __cfaabi_bits_acquire() { |
---|
[36982fc] | 975 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 ); |
---|
[9d944b2] | 976 | } |
---|
| 977 | |
---|
[1c40091] | 978 | void __cfaabi_bits_release() { |
---|
[ea7d2b0] | 979 | unlock( kernel_debug_lock ); |
---|
[9d944b2] | 980 | } |
---|
[8118303] | 981 | } |
---|
| 982 | |
---|
[fa21ac9] | 983 | //============================================================================================= |
---|
| 984 | // Kernel Utilities |
---|
| 985 | //============================================================================================= |
---|
[bd98b58] | 986 | //----------------------------------------------------------------------------- |
---|
| 987 | // Locks |
---|
[242a902] | 988 | void ?{}( semaphore & this, int count = 1 ) { |
---|
| 989 | (this.lock){}; |
---|
| 990 | this.count = count; |
---|
| 991 | (this.waiting){}; |
---|
[db6f06a] | 992 | } |
---|
[242a902] | 993 | void ^?{}(semaphore & this) {} |
---|
[db6f06a] | 994 | |
---|
[71c8b7e] | 995 | bool P(semaphore & this) with( this ){ |
---|
[65deb18] | 996 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 997 | count -= 1; |
---|
| 998 | if ( count < 0 ) { |
---|
[bdeba0b] | 999 | // queue current task |
---|
[14a61b5] | 1000 | append( waiting, kernelTLS.this_thread ); |
---|
[bdeba0b] | 1001 | |
---|
| 1002 | // atomically release spin lock and block |
---|
[3381ed7] | 1003 | unlock( lock ); |
---|
[ae66348] | 1004 | park( __cfaabi_dbg_ctx ); |
---|
[71c8b7e] | 1005 | return true; |
---|
[8def349] | 1006 | } |
---|
[4e6fb8e] | 1007 | else { |
---|
[65deb18] | 1008 | unlock( lock ); |
---|
[71c8b7e] | 1009 | return false; |
---|
[4e6fb8e] | 1010 | } |
---|
[bd98b58] | 1011 | } |
---|
| 1012 | |
---|
[f0ce5f4] | 1013 | bool V(semaphore & this) with( this ) { |
---|
[ac2b598] | 1014 | $thread * thrd = 0p; |
---|
[65deb18] | 1015 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 1016 | count += 1; |
---|
| 1017 | if ( count <= 0 ) { |
---|
[bdeba0b] | 1018 | // remove task at head of waiting list |
---|
[65deb18] | 1019 | thrd = pop_head( waiting ); |
---|
[bd98b58] | 1020 | } |
---|
[bdeba0b] | 1021 | |
---|
[65deb18] | 1022 | unlock( lock ); |
---|
[bdeba0b] | 1023 | |
---|
| 1024 | // make new owner |
---|
[ae66348] | 1025 | unpark( thrd __cfaabi_dbg_ctx2 ); |
---|
[f0ce5f4] | 1026 | |
---|
[d384787] | 1027 | return thrd != 0p; |
---|
| 1028 | } |
---|
| 1029 | |
---|
| 1030 | bool V(semaphore & this, unsigned diff) with( this ) { |
---|
| 1031 | $thread * thrd = 0p; |
---|
| 1032 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 1033 | int release = max(-count, (int)diff); |
---|
| 1034 | count += diff; |
---|
| 1035 | for(release) { |
---|
| 1036 | unpark( pop_head( waiting ) __cfaabi_dbg_ctx2 ); |
---|
| 1037 | } |
---|
| 1038 | |
---|
| 1039 | unlock( lock ); |
---|
| 1040 | |
---|
[f0ce5f4] | 1041 | return thrd != 0p; |
---|
[bd98b58] | 1042 | } |
---|
| 1043 | |
---|
[f7d6bb0] | 1044 | //----------------------------------------------------------------------------- |
---|
[de94a60] | 1045 | // Global Queues |
---|
| 1046 | void doregister( cluster & cltr ) { |
---|
[ea8b2f7] | 1047 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2); |
---|
| 1048 | push_front( __cfa_dbg_global_clusters.list, cltr ); |
---|
| 1049 | unlock ( __cfa_dbg_global_clusters.lock ); |
---|
[de94a60] | 1050 | } |
---|
[f7d6bb0] | 1051 | |
---|
[de94a60] | 1052 | void unregister( cluster & cltr ) { |
---|
[ea8b2f7] | 1053 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2); |
---|
| 1054 | remove( __cfa_dbg_global_clusters.list, cltr ); |
---|
| 1055 | unlock( __cfa_dbg_global_clusters.lock ); |
---|
[de94a60] | 1056 | } |
---|
[f7d6bb0] | 1057 | |
---|
[ac2b598] | 1058 | void doregister( cluster * cltr, $thread & thrd ) { |
---|
[a1a17a74] | 1059 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2); |
---|
[d4e68a6] | 1060 | cltr->nthreads += 1; |
---|
[a1a17a74] | 1061 | push_front(cltr->threads, thrd); |
---|
| 1062 | unlock (cltr->thread_list_lock); |
---|
| 1063 | } |
---|
| 1064 | |
---|
[ac2b598] | 1065 | void unregister( cluster * cltr, $thread & thrd ) { |
---|
[a1a17a74] | 1066 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2); |
---|
| 1067 | remove(cltr->threads, thrd ); |
---|
[d4e68a6] | 1068 | cltr->nthreads -= 1; |
---|
[a1a17a74] | 1069 | unlock(cltr->thread_list_lock); |
---|
| 1070 | } |
---|
[9181f1d] | 1071 | |
---|
[de94a60] | 1072 | void doregister( cluster * cltr, processor * proc ) { |
---|
[92e7631] | 1073 | lock (cltr->idle_lock __cfaabi_dbg_ctx2); |
---|
[d4e68a6] | 1074 | cltr->nprocessors += 1; |
---|
[639991a] | 1075 | push_front(cltr->procs, *proc); |
---|
[92e7631] | 1076 | unlock (cltr->idle_lock); |
---|
[de94a60] | 1077 | } |
---|
| 1078 | |
---|
| 1079 | void unregister( cluster * cltr, processor * proc ) { |
---|
[92e7631] | 1080 | lock (cltr->idle_lock __cfaabi_dbg_ctx2); |
---|
[639991a] | 1081 | remove(cltr->procs, *proc ); |
---|
[d4e68a6] | 1082 | cltr->nprocessors -= 1; |
---|
[92e7631] | 1083 | unlock(cltr->idle_lock); |
---|
[de94a60] | 1084 | } |
---|
| 1085 | |
---|
| 1086 | //----------------------------------------------------------------------------- |
---|
| 1087 | // Debug |
---|
| 1088 | __cfaabi_dbg_debug_do( |
---|
[1997b4e] | 1089 | extern "C" { |
---|
[ae66348] | 1090 | void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) { |
---|
[1997b4e] | 1091 | this.prev_name = prev_name; |
---|
| 1092 | this.prev_thrd = kernelTLS.this_thread; |
---|
| 1093 | } |
---|
[ae66348] | 1094 | |
---|
| 1095 | void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]) { |
---|
| 1096 | if(park) { |
---|
[f0ce5f4] | 1097 | this.park_caller = prev_name; |
---|
| 1098 | this.park_stale = false; |
---|
[ae66348] | 1099 | } |
---|
| 1100 | else { |
---|
[f0ce5f4] | 1101 | this.unpark_caller = prev_name; |
---|
| 1102 | this.unpark_stale = false; |
---|
[ae66348] | 1103 | } |
---|
| 1104 | } |
---|
[9181f1d] | 1105 | } |
---|
[f7d6bb0] | 1106 | ) |
---|
[2026bb6] | 1107 | |
---|
| 1108 | //----------------------------------------------------------------------------- |
---|
| 1109 | // Debug |
---|
[8c50aed] | 1110 | bool threading_enabled(void) __attribute__((const)) { |
---|
[2026bb6] | 1111 | return true; |
---|
| 1112 | } |
---|
[8118303] | 1113 | // Local Variables: // |
---|
| 1114 | // mode: c // |
---|
| 1115 | // tab-width: 4 // |
---|
| 1116 | // End: // |
---|