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