[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 |
---|
[1c40091] | 12 | // Last Modified On : Thu Nov 21 16:46:59 2019 |
---|
| 13 | // Update Count : 27 |
---|
[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> |
---|
[eb2e723] | 28 | } |
---|
[8118303] | 29 | |
---|
| 30 | //CFA Includes |
---|
[58b6d1b] | 31 | #include "time.hfa" |
---|
[73abe95] | 32 | #include "kernel_private.hfa" |
---|
| 33 | #include "preemption.hfa" |
---|
| 34 | #include "startup.hfa" |
---|
[8118303] | 35 | |
---|
| 36 | //Private includes |
---|
| 37 | #define __CFA_INVOKE_PRIVATE__ |
---|
| 38 | #include "invoke.h" |
---|
| 39 | |
---|
[deca0f5] | 40 | //----------------------------------------------------------------------------- |
---|
| 41 | // Some assembly required |
---|
| 42 | #if defined( __i386 ) |
---|
| 43 | #define CtxGet( ctx ) \ |
---|
| 44 | __asm__ volatile ( \ |
---|
| 45 | "movl %%esp,%0\n"\ |
---|
| 46 | "movl %%ebp,%1\n"\ |
---|
| 47 | : "=rm" (ctx.SP),\ |
---|
| 48 | "=rm" (ctx.FP) \ |
---|
| 49 | ) |
---|
| 50 | |
---|
| 51 | // mxcr : SSE Status and Control bits (control bits are preserved across function calls) |
---|
| 52 | // fcw : X87 FPU control word (preserved across function calls) |
---|
| 53 | #define __x87_store \ |
---|
| 54 | uint32_t __mxcr; \ |
---|
| 55 | uint16_t __fcw; \ |
---|
| 56 | __asm__ volatile ( \ |
---|
| 57 | "stmxcsr %0\n" \ |
---|
| 58 | "fnstcw %1\n" \ |
---|
| 59 | : "=m" (__mxcr),\ |
---|
| 60 | "=m" (__fcw) \ |
---|
| 61 | ) |
---|
| 62 | |
---|
| 63 | #define __x87_load \ |
---|
| 64 | __asm__ volatile ( \ |
---|
| 65 | "fldcw %1\n" \ |
---|
| 66 | "ldmxcsr %0\n" \ |
---|
| 67 | ::"m" (__mxcr),\ |
---|
| 68 | "m" (__fcw) \ |
---|
| 69 | ) |
---|
| 70 | |
---|
| 71 | #elif defined( __x86_64 ) |
---|
| 72 | #define CtxGet( ctx ) \ |
---|
| 73 | __asm__ volatile ( \ |
---|
| 74 | "movq %%rsp,%0\n"\ |
---|
| 75 | "movq %%rbp,%1\n"\ |
---|
| 76 | : "=rm" (ctx.SP),\ |
---|
| 77 | "=rm" (ctx.FP) \ |
---|
| 78 | ) |
---|
| 79 | |
---|
| 80 | #define __x87_store \ |
---|
| 81 | uint32_t __mxcr; \ |
---|
| 82 | uint16_t __fcw; \ |
---|
| 83 | __asm__ volatile ( \ |
---|
| 84 | "stmxcsr %0\n" \ |
---|
| 85 | "fnstcw %1\n" \ |
---|
| 86 | : "=m" (__mxcr),\ |
---|
| 87 | "=m" (__fcw) \ |
---|
| 88 | ) |
---|
| 89 | |
---|
| 90 | #define __x87_load \ |
---|
| 91 | __asm__ volatile ( \ |
---|
| 92 | "fldcw %1\n" \ |
---|
| 93 | "ldmxcsr %0\n" \ |
---|
| 94 | :: "m" (__mxcr),\ |
---|
| 95 | "m" (__fcw) \ |
---|
| 96 | ) |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | #elif defined( __ARM_ARCH ) |
---|
| 100 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 101 | "mov %0,%%sp\n" \ |
---|
| 102 | "mov %1,%%r11\n" \ |
---|
| 103 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
| 104 | #else |
---|
| 105 | #error unknown hardware architecture |
---|
| 106 | #endif |
---|
| 107 | |
---|
| 108 | //----------------------------------------------------------------------------- |
---|
[2ac095d] | 109 | //Start and stop routine for the kernel, declared first to make sure they run first |
---|
[c29c342] | 110 | static void kernel_startup(void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) )); |
---|
| 111 | static void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) )); |
---|
[2ac095d] | 112 | |
---|
[8def349] | 113 | //----------------------------------------------------------------------------- |
---|
| 114 | // Kernel storage |
---|
[b2f6113] | 115 | KERNEL_STORAGE(cluster, mainCluster); |
---|
| 116 | KERNEL_STORAGE(processor, mainProcessor); |
---|
| 117 | KERNEL_STORAGE(thread_desc, mainThread); |
---|
| 118 | KERNEL_STORAGE(__stack_t, mainThreadCtx); |
---|
[8def349] | 119 | |
---|
[de6319f] | 120 | cluster * mainCluster; |
---|
| 121 | processor * mainProcessor; |
---|
[348006f] | 122 | thread_desc * mainThread; |
---|
[eb2e723] | 123 | |
---|
[ea8b2f7] | 124 | extern "C" { |
---|
| 125 | struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters; |
---|
| 126 | } |
---|
[de94a60] | 127 | |
---|
[b2f6113] | 128 | size_t __page_size = 0; |
---|
| 129 | |
---|
[bd98b58] | 130 | //----------------------------------------------------------------------------- |
---|
| 131 | // Global state |
---|
[afc2427] | 132 | thread_local struct KernelThreadData kernelTLS __attribute__ ((tls_model ( "initial-exec" ))) = { |
---|
[b10affd] | 133 | NULL, |
---|
| 134 | NULL, |
---|
[21184e3] | 135 | { 1, false, false }, |
---|
| 136 | 6u //this should be seeded better but due to a bug calling rdtsc doesn't work |
---|
[b10affd] | 137 | }; |
---|
[c84e80a] | 138 | |
---|
| 139 | //----------------------------------------------------------------------------- |
---|
[de6319f] | 140 | // Struct to steal stack |
---|
[8def349] | 141 | struct current_stack_info_t { |
---|
[b2f6113] | 142 | __stack_t * storage; // pointer to stack object |
---|
[8def349] | 143 | void *base; // base of stack |
---|
| 144 | void *limit; // stack grows towards stack limit |
---|
| 145 | void *context; // address of cfa_context_t |
---|
[c84e80a] | 146 | }; |
---|
| 147 | |
---|
[242a902] | 148 | void ?{}( current_stack_info_t & this ) { |
---|
[b2f6113] | 149 | __stack_context_t ctx; |
---|
| 150 | CtxGet( ctx ); |
---|
| 151 | this.base = ctx.FP; |
---|
[8def349] | 152 | |
---|
| 153 | rlimit r; |
---|
[132fad4] | 154 | getrlimit( RLIMIT_STACK, &r); |
---|
[69a61d2] | 155 | size_t size = r.rlim_cur; |
---|
[8def349] | 156 | |
---|
[69a61d2] | 157 | this.limit = (void *)(((intptr_t)this.base) - size); |
---|
[9236060] | 158 | this.context = &storage_mainThreadCtx; |
---|
[8def349] | 159 | } |
---|
| 160 | |
---|
[de6319f] | 161 | //----------------------------------------------------------------------------- |
---|
| 162 | // Main thread construction |
---|
[8def349] | 163 | |
---|
[65deb18] | 164 | void ?{}( coroutine_desc & this, current_stack_info_t * info) with( this ) { |
---|
[b2f6113] | 165 | stack.storage = info->storage; |
---|
| 166 | with(*stack.storage) { |
---|
| 167 | limit = info->limit; |
---|
| 168 | base = info->base; |
---|
| 169 | } |
---|
[ffe2fad] | 170 | __attribute__((may_alias)) intptr_t * istorage = (intptr_t*) &stack.storage; |
---|
| 171 | *istorage |= 0x1; |
---|
[65deb18] | 172 | name = "Main Thread"; |
---|
| 173 | state = Start; |
---|
| 174 | starter = NULL; |
---|
[b2f6113] | 175 | last = NULL; |
---|
| 176 | cancellation = NULL; |
---|
[8def349] | 177 | } |
---|
| 178 | |
---|
[65deb18] | 179 | void ?{}( thread_desc & this, current_stack_info_t * info) with( this ) { |
---|
[e8e457e] | 180 | state = Start; |
---|
[65deb18] | 181 | self_cor{ info }; |
---|
[82c948c] | 182 | curr_cor = &self_cor; |
---|
[de6319f] | 183 | curr_cluster = mainCluster; |
---|
[82c948c] | 184 | self_mon.owner = &this; |
---|
| 185 | self_mon.recursion = 1; |
---|
| 186 | self_mon_p = &self_mon; |
---|
| 187 | next = NULL; |
---|
[de94a60] | 188 | |
---|
| 189 | node.next = NULL; |
---|
| 190 | node.prev = NULL; |
---|
[a1a17a7] | 191 | doregister(curr_cluster, this); |
---|
[82c948c] | 192 | |
---|
| 193 | monitors{ &self_mon_p, 1, (fptr_t)0 }; |
---|
[8def349] | 194 | } |
---|
[c84e80a] | 195 | |
---|
[8def349] | 196 | //----------------------------------------------------------------------------- |
---|
| 197 | // Processor coroutine |
---|
[de6319f] | 198 | void ?{}(processorCtx_t & this) { |
---|
[39fea2f] | 199 | |
---|
[8def349] | 200 | } |
---|
| 201 | |
---|
[39fea2f] | 202 | // Construct the processor context of non-main processors |
---|
[c29c342] | 203 | static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) { |
---|
[242a902] | 204 | (this.__cor){ info }; |
---|
| 205 | this.proc = proc; |
---|
[8def349] | 206 | } |
---|
| 207 | |
---|
[c29c342] | 208 | static void start(processor * this); |
---|
[de6319f] | 209 | void ?{}(processor & this, const char * name, cluster & cltr) with( this ) { |
---|
| 210 | this.name = name; |
---|
| 211 | this.cltr = &cltr; |
---|
[c40e7c5] | 212 | terminated{ 0 }; |
---|
| 213 | do_terminate = false; |
---|
| 214 | preemption_alarm = NULL; |
---|
| 215 | pending_preemption = false; |
---|
[094476d] | 216 | runner.proc = &this; |
---|
[8def349] | 217 | |
---|
[85b1deb] | 218 | idleLock{}; |
---|
[6b4cdd3] | 219 | |
---|
[242a902] | 220 | start( &this ); |
---|
[c84e80a] | 221 | } |
---|
| 222 | |
---|
[65deb18] | 223 | void ^?{}(processor & this) with( this ){ |
---|
[ea8b2f7] | 224 | if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) { |
---|
[36982fc] | 225 | __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this); |
---|
[85b1deb] | 226 | |
---|
| 227 | __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED); |
---|
| 228 | wake( &this ); |
---|
| 229 | |
---|
[65deb18] | 230 | P( terminated ); |
---|
[14a61b5] | 231 | verify( kernelTLS.this_processor != &this); |
---|
[8def349] | 232 | } |
---|
[6b4cdd3] | 233 | |
---|
[85b1deb] | 234 | pthread_join( kernel_thread, NULL ); |
---|
[8def349] | 235 | } |
---|
| 236 | |
---|
[de6319f] | 237 | void ?{}(cluster & this, const char * name, Duration preemption_rate) with( this ) { |
---|
| 238 | this.name = name; |
---|
| 239 | this.preemption_rate = preemption_rate; |
---|
[65deb18] | 240 | ready_queue{}; |
---|
| 241 | ready_queue_lock{}; |
---|
[de94a60] | 242 | |
---|
| 243 | procs{ __get }; |
---|
| 244 | idles{ __get }; |
---|
[a1a17a7] | 245 | threads{ __get }; |
---|
[de94a60] | 246 | |
---|
| 247 | doregister(this); |
---|
[8def349] | 248 | } |
---|
| 249 | |
---|
[242a902] | 250 | void ^?{}(cluster & this) { |
---|
[de94a60] | 251 | unregister(this); |
---|
[c84e80a] | 252 | } |
---|
| 253 | |
---|
[75f3522] | 254 | //============================================================================================= |
---|
| 255 | // Kernel Scheduling logic |
---|
| 256 | //============================================================================================= |
---|
[c29c342] | 257 | static void runThread(processor * this, thread_desc * dst); |
---|
| 258 | static void finishRunning(processor * this); |
---|
| 259 | static void halt(processor * this); |
---|
| 260 | |
---|
[8fcbb4c] | 261 | //Main of the processor contexts |
---|
[83a071f9] | 262 | void main(processorCtx_t & runner) { |
---|
[21184e3] | 263 | // Because of a bug, we couldn't initialized the seed on construction |
---|
| 264 | // Do it here |
---|
[57c764c] | 265 | kernelTLS.rand_seed ^= rdtscl(); |
---|
[21184e3] | 266 | |
---|
[83a071f9] | 267 | processor * this = runner.proc; |
---|
[094476d] | 268 | verify(this); |
---|
[c81ebf9] | 269 | |
---|
[36982fc] | 270 | __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this); |
---|
[8118303] | 271 | |
---|
[de94a60] | 272 | doregister(this->cltr, this); |
---|
| 273 | |
---|
[75f3522] | 274 | { |
---|
[c81ebf9] | 275 | // Setup preemption data |
---|
| 276 | preemption_scope scope = { this }; |
---|
| 277 | |
---|
[36982fc] | 278 | __cfaabi_dbg_print_safe("Kernel : core %p started\n", this); |
---|
[8118303] | 279 | |
---|
[c81ebf9] | 280 | thread_desc * readyThread = NULL; |
---|
[ea8b2f7] | 281 | for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ ) |
---|
[75f3522] | 282 | { |
---|
[c81ebf9] | 283 | readyThread = nextThread( this->cltr ); |
---|
[75f3522] | 284 | |
---|
[c81ebf9] | 285 | if(readyThread) |
---|
| 286 | { |
---|
[14a61b5] | 287 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[4e6fb8e] | 288 | |
---|
[c81ebf9] | 289 | runThread(this, readyThread); |
---|
[75f3522] | 290 | |
---|
[14a61b5] | 291 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[4e6fb8e] | 292 | |
---|
[c81ebf9] | 293 | //Some actions need to be taken from the kernel |
---|
| 294 | finishRunning(this); |
---|
| 295 | |
---|
| 296 | spin_count = 0; |
---|
| 297 | } |
---|
| 298 | else |
---|
| 299 | { |
---|
[ea8b2f7] | 300 | // spin(this, &spin_count); |
---|
| 301 | halt(this); |
---|
[c81ebf9] | 302 | } |
---|
| 303 | } |
---|
| 304 | |
---|
[36982fc] | 305 | __cfaabi_dbg_print_safe("Kernel : core %p stopping\n", this); |
---|
[c84e80a] | 306 | } |
---|
[8118303] | 307 | |
---|
[de94a60] | 308 | unregister(this->cltr, this); |
---|
| 309 | |
---|
[4cedd9f] | 310 | V( this->terminated ); |
---|
[bdeba0b] | 311 | |
---|
[36982fc] | 312 | __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this); |
---|
[c84e80a] | 313 | } |
---|
| 314 | |
---|
[5c1a531] | 315 | static int * __volatile_errno() __attribute__((noinline)); |
---|
| 316 | static int * __volatile_errno() { asm(""); return &errno; } |
---|
| 317 | |
---|
[14a61b5] | 318 | // KERNEL ONLY |
---|
[1c273d0] | 319 | // runThread runs a thread by context switching |
---|
| 320 | // from the processor coroutine to the target thread |
---|
[e8e457e] | 321 | static void runThread(processor * this, thread_desc * thrd_dst) { |
---|
[094476d] | 322 | coroutine_desc * proc_cor = get_coroutine(this->runner); |
---|
[1c273d0] | 323 | |
---|
[14a61b5] | 324 | // Reset the terminating actions here |
---|
[db6f06a] | 325 | this->finish.action_code = No_Action; |
---|
[8fcbb4c] | 326 | |
---|
[14a61b5] | 327 | // Update global state |
---|
[e8e457e] | 328 | kernelTLS.this_thread = thrd_dst; |
---|
| 329 | |
---|
| 330 | // set state of processor coroutine to inactive and the thread to active |
---|
| 331 | proc_cor->state = proc_cor->state == Halted ? Halted : Inactive; |
---|
| 332 | thrd_dst->state = Active; |
---|
| 333 | |
---|
| 334 | // set context switch to the thread that the processor is executing |
---|
| 335 | verify( thrd_dst->context.SP ); |
---|
| 336 | CtxSwitch( &proc_cor->context, &thrd_dst->context ); |
---|
| 337 | // when CtxSwitch returns we are back in the processor coroutine |
---|
[75f3522] | 338 | |
---|
[e8e457e] | 339 | // set state of processor coroutine to active and the thread to inactive |
---|
| 340 | thrd_dst->state = thrd_dst->state == Halted ? Halted : Inactive; |
---|
| 341 | proc_cor->state = Active; |
---|
[75f3522] | 342 | } |
---|
| 343 | |
---|
[14a61b5] | 344 | // KERNEL_ONLY |
---|
[c29c342] | 345 | static void returnToKernel() { |
---|
[14a61b5] | 346 | coroutine_desc * proc_cor = get_coroutine(kernelTLS.this_processor->runner); |
---|
[e8e457e] | 347 | thread_desc * thrd_src = kernelTLS.this_thread; |
---|
| 348 | |
---|
| 349 | // set state of current coroutine to inactive |
---|
| 350 | thrd_src->state = thrd_src->state == Halted ? Halted : Inactive; |
---|
| 351 | proc_cor->state = Active; |
---|
[5c1a531] | 352 | int local_errno = *__volatile_errno(); |
---|
[deca0f5] | 353 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
| 354 | __x87_store; |
---|
| 355 | #endif |
---|
[e8e457e] | 356 | |
---|
| 357 | // set new coroutine that the processor is executing |
---|
| 358 | // and context switch to it |
---|
| 359 | verify( proc_cor->context.SP ); |
---|
| 360 | CtxSwitch( &thrd_src->context, &proc_cor->context ); |
---|
| 361 | |
---|
| 362 | // set state of new coroutine to active |
---|
| 363 | proc_cor->state = proc_cor->state == Halted ? Halted : Inactive; |
---|
| 364 | thrd_src->state = Active; |
---|
[deca0f5] | 365 | |
---|
| 366 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
| 367 | __x87_load; |
---|
| 368 | #endif |
---|
[5c1a531] | 369 | *__volatile_errno() = local_errno; |
---|
[82c948c] | 370 | } |
---|
| 371 | |
---|
[14a61b5] | 372 | // KERNEL_ONLY |
---|
[1c273d0] | 373 | // Once a thread has finished running, some of |
---|
[75f3522] | 374 | // its final actions must be executed from the kernel |
---|
[c29c342] | 375 | static void finishRunning(processor * this) with( this->finish ) { |
---|
[09800e9] | 376 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 377 | choose( action_code ) { |
---|
| 378 | case No_Action: |
---|
| 379 | break; |
---|
| 380 | case Release: |
---|
[65deb18] | 381 | unlock( *lock ); |
---|
[09800e9] | 382 | case Schedule: |
---|
[65deb18] | 383 | ScheduleThread( thrd ); |
---|
[09800e9] | 384 | case Release_Schedule: |
---|
[65deb18] | 385 | unlock( *lock ); |
---|
| 386 | ScheduleThread( thrd ); |
---|
[09800e9] | 387 | case Release_Multi: |
---|
[65deb18] | 388 | for(int i = 0; i < lock_count; i++) { |
---|
| 389 | unlock( *locks[i] ); |
---|
[0c78741] | 390 | } |
---|
[09800e9] | 391 | case Release_Multi_Schedule: |
---|
[65deb18] | 392 | for(int i = 0; i < lock_count; i++) { |
---|
| 393 | unlock( *locks[i] ); |
---|
[0c78741] | 394 | } |
---|
[65deb18] | 395 | for(int i = 0; i < thrd_count; i++) { |
---|
| 396 | ScheduleThread( thrds[i] ); |
---|
[0c78741] | 397 | } |
---|
[09800e9] | 398 | case Callback: |
---|
| 399 | callback(); |
---|
| 400 | default: |
---|
| 401 | abort("KERNEL ERROR: Unexpected action to run after thread"); |
---|
[8fcbb4c] | 402 | } |
---|
[c84e80a] | 403 | } |
---|
| 404 | |
---|
[14a61b5] | 405 | // KERNEL_ONLY |
---|
[0c92c9f] | 406 | // Context invoker for processors |
---|
| 407 | // This is the entry point for processors (kernel threads) |
---|
| 408 | // It effectively constructs a coroutine by stealing the pthread stack |
---|
[c29c342] | 409 | static void * CtxInvokeProcessor(void * arg) { |
---|
[8def349] | 410 | processor * proc = (processor *) arg; |
---|
[14a61b5] | 411 | kernelTLS.this_processor = proc; |
---|
| 412 | kernelTLS.this_thread = NULL; |
---|
| 413 | kernelTLS.preemption_state.[enabled, disable_count] = [false, 1]; |
---|
[8def349] | 414 | // SKULLDUGGERY: We want to create a context for the processor coroutine |
---|
| 415 | // which is needed for the 2-step context switch. However, there is no reason |
---|
[1c273d0] | 416 | // to waste the perfectly valid stack create by pthread. |
---|
[8def349] | 417 | current_stack_info_t info; |
---|
[b2f6113] | 418 | __stack_t ctx; |
---|
| 419 | info.storage = &ctx; |
---|
[094476d] | 420 | (proc->runner){ proc, &info }; |
---|
[8def349] | 421 | |
---|
[b2f6113] | 422 | __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage); |
---|
[8fcbb4c] | 423 | |
---|
[0c92c9f] | 424 | //Set global state |
---|
[14a61b5] | 425 | kernelTLS.this_thread = NULL; |
---|
[8def349] | 426 | |
---|
| 427 | //We now have a proper context from which to schedule threads |
---|
[094476d] | 428 | __cfaabi_dbg_print_safe("Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx); |
---|
[8def349] | 429 | |
---|
[1c273d0] | 430 | // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't |
---|
| 431 | // resume it to start it like it normally would, it will just context switch |
---|
| 432 | // back to here. Instead directly call the main since we already are on the |
---|
[8def349] | 433 | // appropriate stack. |
---|
[094476d] | 434 | get_coroutine(proc->runner)->state = Active; |
---|
| 435 | main( proc->runner ); |
---|
| 436 | get_coroutine(proc->runner)->state = Halted; |
---|
[8def349] | 437 | |
---|
[0c92c9f] | 438 | // Main routine of the core returned, the core is now fully terminated |
---|
[094476d] | 439 | __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner); |
---|
[8def349] | 440 | |
---|
| 441 | return NULL; |
---|
[c84e80a] | 442 | } |
---|
| 443 | |
---|
[c29c342] | 444 | static void start(processor * this) { |
---|
[36982fc] | 445 | __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this); |
---|
[82ff5845] | 446 | |
---|
[8fcbb4c] | 447 | pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this ); |
---|
[eb2e723] | 448 | |
---|
[36982fc] | 449 | __cfaabi_dbg_print_safe("Kernel : core %p started\n", this); |
---|
[eb2e723] | 450 | } |
---|
| 451 | |
---|
[14a61b5] | 452 | // KERNEL_ONLY |
---|
[e8e457e] | 453 | void kernel_first_resume( processor * this ) { |
---|
| 454 | thread_desc * src = mainThread; |
---|
[094476d] | 455 | coroutine_desc * dst = get_coroutine(this->runner); |
---|
[b69ea6b] | 456 | |
---|
[14a61b5] | 457 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[b69ea6b] | 458 | |
---|
[b2f6113] | 459 | __stack_prepare( &dst->stack, 65000 ); |
---|
[094476d] | 460 | CtxStart(&this->runner, CtxInvokeCoroutine); |
---|
[b69ea6b] | 461 | |
---|
[14a61b5] | 462 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[b69ea6b] | 463 | |
---|
[e8e457e] | 464 | dst->last = &src->self_cor; |
---|
| 465 | dst->starter = dst->starter ? dst->starter : &src->self_cor; |
---|
[b69ea6b] | 466 | |
---|
| 467 | // set state of current coroutine to inactive |
---|
| 468 | src->state = src->state == Halted ? Halted : Inactive; |
---|
| 469 | |
---|
| 470 | // context switch to specified coroutine |
---|
[69a61d2] | 471 | verify( dst->context.SP ); |
---|
[b2f6113] | 472 | CtxSwitch( &src->context, &dst->context ); |
---|
[b69ea6b] | 473 | // when CtxSwitch returns we are back in the src coroutine |
---|
| 474 | |
---|
| 475 | // set state of new coroutine to active |
---|
| 476 | src->state = Active; |
---|
| 477 | |
---|
[14a61b5] | 478 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[b69ea6b] | 479 | } |
---|
| 480 | |
---|
[e8e457e] | 481 | // KERNEL_ONLY |
---|
| 482 | void kernel_last_resume( processor * this ) { |
---|
| 483 | coroutine_desc * src = &mainThread->self_cor; |
---|
| 484 | coroutine_desc * dst = get_coroutine(this->runner); |
---|
| 485 | |
---|
| 486 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 487 | verify( dst->starter == src ); |
---|
| 488 | verify( dst->context.SP ); |
---|
| 489 | |
---|
| 490 | // context switch to the processor |
---|
| 491 | CtxSwitch( &src->context, &dst->context ); |
---|
| 492 | } |
---|
| 493 | |
---|
[8def349] | 494 | //----------------------------------------------------------------------------- |
---|
| 495 | // Scheduler routines |
---|
[14a61b5] | 496 | |
---|
| 497 | // KERNEL ONLY |
---|
[348006f] | 498 | void ScheduleThread( thread_desc * thrd ) { |
---|
[135b431] | 499 | verify( thrd ); |
---|
[e8e457e] | 500 | verify( thrd->state != Halted ); |
---|
[1c273d0] | 501 | |
---|
[14a61b5] | 502 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[690f13c] | 503 | |
---|
[4aa2fb2] | 504 | verifyf( thrd->next == NULL, "Expected null got %p", thrd->next ); |
---|
[1c273d0] | 505 | |
---|
[de6319f] | 506 | with( *thrd->curr_cluster ) { |
---|
[65deb18] | 507 | lock ( ready_queue_lock __cfaabi_dbg_ctx2 ); |
---|
[6b4cdd3] | 508 | bool was_empty = !(ready_queue != 0); |
---|
[65deb18] | 509 | append( ready_queue, thrd ); |
---|
| 510 | unlock( ready_queue_lock ); |
---|
[6b4cdd3] | 511 | |
---|
[85b1deb] | 512 | if(was_empty) { |
---|
[6b4cdd3] | 513 | lock (proc_list_lock __cfaabi_dbg_ctx2); |
---|
| 514 | if(idles) { |
---|
[85b1deb] | 515 | wake_fast(idles.head); |
---|
[6b4cdd3] | 516 | } |
---|
| 517 | unlock (proc_list_lock); |
---|
| 518 | } |
---|
[85b1deb] | 519 | else if( struct processor * idle = idles.head ) { |
---|
| 520 | wake_fast(idle); |
---|
| 521 | } |
---|
| 522 | |
---|
[65deb18] | 523 | } |
---|
[1c273d0] | 524 | |
---|
[14a61b5] | 525 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[db6f06a] | 526 | } |
---|
| 527 | |
---|
[14a61b5] | 528 | // KERNEL ONLY |
---|
[65deb18] | 529 | thread_desc * nextThread(cluster * this) with( *this ) { |
---|
[14a61b5] | 530 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[65deb18] | 531 | lock( ready_queue_lock __cfaabi_dbg_ctx2 ); |
---|
| 532 | thread_desc * head = pop_head( ready_queue ); |
---|
| 533 | unlock( ready_queue_lock ); |
---|
[14a61b5] | 534 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[db6f06a] | 535 | return head; |
---|
[eb2e723] | 536 | } |
---|
| 537 | |
---|
[82ff5845] | 538 | void BlockInternal() { |
---|
| 539 | disable_interrupts(); |
---|
[14a61b5] | 540 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[82c948c] | 541 | returnToKernel(); |
---|
[14a61b5] | 542 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[36982fc] | 543 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[75f3522] | 544 | } |
---|
| 545 | |
---|
[ea7d2b0] | 546 | void BlockInternal( __spinlock_t * lock ) { |
---|
[82ff5845] | 547 | disable_interrupts(); |
---|
[14a61b5] | 548 | with( *kernelTLS.this_processor ) { |
---|
[de6319f] | 549 | finish.action_code = Release; |
---|
| 550 | finish.lock = lock; |
---|
| 551 | } |
---|
[0b33412] | 552 | |
---|
[afd550c] | 553 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[82c948c] | 554 | returnToKernel(); |
---|
[afd550c] | 555 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[0b33412] | 556 | |
---|
[36982fc] | 557 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[db6f06a] | 558 | } |
---|
| 559 | |
---|
[82ff5845] | 560 | void BlockInternal( thread_desc * thrd ) { |
---|
| 561 | disable_interrupts(); |
---|
[14a61b5] | 562 | with( * kernelTLS.this_processor ) { |
---|
[de6319f] | 563 | finish.action_code = Schedule; |
---|
| 564 | finish.thrd = thrd; |
---|
| 565 | } |
---|
[0b33412] | 566 | |
---|
[14a61b5] | 567 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[82c948c] | 568 | returnToKernel(); |
---|
[14a61b5] | 569 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[0b33412] | 570 | |
---|
[36982fc] | 571 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[db6f06a] | 572 | } |
---|
| 573 | |
---|
[ea7d2b0] | 574 | void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) { |
---|
[97e3296] | 575 | assert(thrd); |
---|
[82ff5845] | 576 | disable_interrupts(); |
---|
[14a61b5] | 577 | with( * kernelTLS.this_processor ) { |
---|
[de6319f] | 578 | finish.action_code = Release_Schedule; |
---|
| 579 | finish.lock = lock; |
---|
| 580 | finish.thrd = thrd; |
---|
| 581 | } |
---|
[0b33412] | 582 | |
---|
[14a61b5] | 583 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[82c948c] | 584 | returnToKernel(); |
---|
[14a61b5] | 585 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[0b33412] | 586 | |
---|
[36982fc] | 587 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[eb2e723] | 588 | } |
---|
| 589 | |
---|
[ea7d2b0] | 590 | void BlockInternal(__spinlock_t * locks [], unsigned short count) { |
---|
[82ff5845] | 591 | disable_interrupts(); |
---|
[14a61b5] | 592 | with( * kernelTLS.this_processor ) { |
---|
[de6319f] | 593 | finish.action_code = Release_Multi; |
---|
| 594 | finish.locks = locks; |
---|
| 595 | finish.lock_count = count; |
---|
| 596 | } |
---|
[0b33412] | 597 | |
---|
[14a61b5] | 598 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[82c948c] | 599 | returnToKernel(); |
---|
[14a61b5] | 600 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[0b33412] | 601 | |
---|
[36982fc] | 602 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[0c78741] | 603 | } |
---|
| 604 | |
---|
[ea7d2b0] | 605 | void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) { |
---|
[82ff5845] | 606 | disable_interrupts(); |
---|
[14a61b5] | 607 | with( *kernelTLS.this_processor ) { |
---|
[de6319f] | 608 | finish.action_code = Release_Multi_Schedule; |
---|
| 609 | finish.locks = locks; |
---|
| 610 | finish.lock_count = lock_count; |
---|
| 611 | finish.thrds = thrds; |
---|
| 612 | finish.thrd_count = thrd_count; |
---|
| 613 | } |
---|
[0b33412] | 614 | |
---|
[14a61b5] | 615 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[82c948c] | 616 | returnToKernel(); |
---|
[14a61b5] | 617 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[0b33412] | 618 | |
---|
[36982fc] | 619 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[0c78741] | 620 | } |
---|
| 621 | |
---|
[09800e9] | 622 | void BlockInternal(__finish_callback_fptr_t callback) { |
---|
| 623 | disable_interrupts(); |
---|
| 624 | with( *kernelTLS.this_processor ) { |
---|
| 625 | finish.action_code = Callback; |
---|
| 626 | finish.callback = callback; |
---|
| 627 | } |
---|
| 628 | |
---|
| 629 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 630 | returnToKernel(); |
---|
| 631 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 632 | |
---|
| 633 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
| 634 | } |
---|
| 635 | |
---|
[14a61b5] | 636 | // KERNEL ONLY |
---|
[ea7d2b0] | 637 | void LeaveThread(__spinlock_t * lock, thread_desc * thrd) { |
---|
[14a61b5] | 638 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 639 | with( * kernelTLS.this_processor ) { |
---|
[de6319f] | 640 | finish.action_code = thrd ? Release_Schedule : Release; |
---|
| 641 | finish.lock = lock; |
---|
| 642 | finish.thrd = thrd; |
---|
| 643 | } |
---|
[f2b12406] | 644 | |
---|
[82c948c] | 645 | returnToKernel(); |
---|
[f2b12406] | 646 | } |
---|
| 647 | |
---|
[fa21ac9] | 648 | //============================================================================================= |
---|
| 649 | // Kernel Setup logic |
---|
| 650 | //============================================================================================= |
---|
[eb2e723] | 651 | //----------------------------------------------------------------------------- |
---|
| 652 | // Kernel boot procedures |
---|
[c29c342] | 653 | static void kernel_startup(void) { |
---|
[14a61b5] | 654 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[36982fc] | 655 | __cfaabi_dbg_print_safe("Kernel : Starting\n"); |
---|
[eb2e723] | 656 | |
---|
[b2f6113] | 657 | __page_size = sysconf( _SC_PAGESIZE ); |
---|
| 658 | |
---|
[ea8b2f7] | 659 | __cfa_dbg_global_clusters.list{ __get }; |
---|
| 660 | __cfa_dbg_global_clusters.lock{}; |
---|
[de94a60] | 661 | |
---|
[de6319f] | 662 | // Initialize the main cluster |
---|
| 663 | mainCluster = (cluster *)&storage_mainCluster; |
---|
| 664 | (*mainCluster){"Main Cluster"}; |
---|
| 665 | |
---|
| 666 | __cfaabi_dbg_print_safe("Kernel : Main cluster ready\n"); |
---|
| 667 | |
---|
[eb2e723] | 668 | // Start by initializing the main thread |
---|
[1c273d0] | 669 | // SKULLDUGGERY: the mainThread steals the process main thread |
---|
[969b3fe] | 670 | // which will then be scheduled by the mainProcessor normally |
---|
| 671 | mainThread = (thread_desc *)&storage_mainThread; |
---|
[8fcbb4c] | 672 | current_stack_info_t info; |
---|
[b2f6113] | 673 | info.storage = (__stack_t*)&storage_mainThreadCtx; |
---|
[83a071f9] | 674 | (*mainThread){ &info }; |
---|
[eb2e723] | 675 | |
---|
[36982fc] | 676 | __cfaabi_dbg_print_safe("Kernel : Main thread ready\n"); |
---|
[fa21ac9] | 677 | |
---|
[bd98b58] | 678 | |
---|
[de6319f] | 679 | |
---|
| 680 | // Construct the processor context of the main processor |
---|
| 681 | void ?{}(processorCtx_t & this, processor * proc) { |
---|
| 682 | (this.__cor){ "Processor" }; |
---|
| 683 | this.__cor.starter = NULL; |
---|
| 684 | this.proc = proc; |
---|
| 685 | } |
---|
| 686 | |
---|
| 687 | void ?{}(processor & this) with( this ) { |
---|
| 688 | name = "Main Processor"; |
---|
| 689 | cltr = mainCluster; |
---|
| 690 | terminated{ 0 }; |
---|
| 691 | do_terminate = false; |
---|
| 692 | preemption_alarm = NULL; |
---|
| 693 | pending_preemption = false; |
---|
| 694 | kernel_thread = pthread_self(); |
---|
| 695 | |
---|
| 696 | runner{ &this }; |
---|
| 697 | __cfaabi_dbg_print_safe("Kernel : constructed main processor context %p\n", &runner); |
---|
| 698 | } |
---|
[fa21ac9] | 699 | |
---|
[969b3fe] | 700 | // Initialize the main processor and the main processor ctx |
---|
[eb2e723] | 701 | // (the coroutine that contains the processing control flow) |
---|
[969b3fe] | 702 | mainProcessor = (processor *)&storage_mainProcessor; |
---|
[de6319f] | 703 | (*mainProcessor){}; |
---|
[eb2e723] | 704 | |
---|
[dcb42b8] | 705 | //initialize the global state variables |
---|
[14a61b5] | 706 | kernelTLS.this_processor = mainProcessor; |
---|
| 707 | kernelTLS.this_thread = mainThread; |
---|
[eb2e723] | 708 | |
---|
[82ff5845] | 709 | // Enable preemption |
---|
| 710 | kernel_start_preemption(); |
---|
| 711 | |
---|
[969b3fe] | 712 | // Add the main thread to the ready queue |
---|
| 713 | // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread |
---|
| 714 | ScheduleThread(mainThread); |
---|
| 715 | |
---|
| 716 | // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX |
---|
[dcb42b8] | 717 | // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that |
---|
[1c273d0] | 718 | // mainThread is on the ready queue when this call is made. |
---|
[14a61b5] | 719 | kernel_first_resume( kernelTLS.this_processor ); |
---|
[eb2e723] | 720 | |
---|
[dcb42b8] | 721 | |
---|
| 722 | |
---|
| 723 | // THE SYSTEM IS NOW COMPLETELY RUNNING |
---|
[36982fc] | 724 | __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n"); |
---|
[82ff5845] | 725 | |
---|
[14a61b5] | 726 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[36982fc] | 727 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[afd550c] | 728 | verify( TL_GET( preemption_state.enabled ) ); |
---|
[eb2e723] | 729 | } |
---|
| 730 | |
---|
[c29c342] | 731 | static void kernel_shutdown(void) { |
---|
[36982fc] | 732 | __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n"); |
---|
[eb2e723] | 733 | |
---|
[afd550c] | 734 | verify( TL_GET( preemption_state.enabled ) ); |
---|
[4e6fb8e] | 735 | disable_interrupts(); |
---|
[14a61b5] | 736 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[4e6fb8e] | 737 | |
---|
[969b3fe] | 738 | // SKULLDUGGERY: Notify the mainProcessor it needs to terminates. |
---|
[dcb42b8] | 739 | // When its coroutine terminates, it return control to the mainThread |
---|
| 740 | // which is currently here |
---|
[ea8b2f7] | 741 | __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE); |
---|
[e8e457e] | 742 | kernel_last_resume( kernelTLS.this_processor ); |
---|
[ea8b2f7] | 743 | mainThread->self_cor.state = Halted; |
---|
[eb2e723] | 744 | |
---|
[dcb42b8] | 745 | // THE SYSTEM IS NOW COMPLETELY STOPPED |
---|
[eb2e723] | 746 | |
---|
[82ff5845] | 747 | // Disable preemption |
---|
| 748 | kernel_stop_preemption(); |
---|
| 749 | |
---|
[969b3fe] | 750 | // Destroy the main processor and its context in reverse order of construction |
---|
[dcb42b8] | 751 | // These were manually constructed so we need manually destroy them |
---|
[094476d] | 752 | ^(mainProcessor->runner){}; |
---|
[969b3fe] | 753 | ^(mainProcessor){}; |
---|
[eb2e723] | 754 | |
---|
[dcb42b8] | 755 | // Final step, destroy the main thread since it is no longer needed |
---|
| 756 | // Since we provided a stack to this taxk it will not destroy anything |
---|
[eb2e723] | 757 | ^(mainThread){}; |
---|
| 758 | |
---|
[ea8b2f7] | 759 | ^(__cfa_dbg_global_clusters.list){}; |
---|
| 760 | ^(__cfa_dbg_global_clusters.lock){}; |
---|
[a1a17a7] | 761 | |
---|
[36982fc] | 762 | __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n"); |
---|
[9d944b2] | 763 | } |
---|
| 764 | |
---|
[14a61b5] | 765 | //============================================================================================= |
---|
| 766 | // Kernel Quiescing |
---|
| 767 | //============================================================================================= |
---|
[c29c342] | 768 | static void halt(processor * this) with( *this ) { |
---|
[85b1deb] | 769 | // verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) ); |
---|
[ea8b2f7] | 770 | |
---|
[6b4cdd3] | 771 | with( *cltr ) { |
---|
| 772 | lock (proc_list_lock __cfaabi_dbg_ctx2); |
---|
| 773 | remove (procs, *this); |
---|
| 774 | push_front(idles, *this); |
---|
| 775 | unlock (proc_list_lock); |
---|
| 776 | } |
---|
[14a61b5] | 777 | |
---|
[6b4cdd3] | 778 | __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this); |
---|
[14a61b5] | 779 | |
---|
[85b1deb] | 780 | wait( idleLock ); |
---|
[14a61b5] | 781 | |
---|
[6b4cdd3] | 782 | __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this); |
---|
[14a61b5] | 783 | |
---|
[6b4cdd3] | 784 | with( *cltr ) { |
---|
| 785 | lock (proc_list_lock __cfaabi_dbg_ctx2); |
---|
| 786 | remove (idles, *this); |
---|
| 787 | push_front(procs, *this); |
---|
| 788 | unlock (proc_list_lock); |
---|
| 789 | } |
---|
| 790 | } |
---|
| 791 | |
---|
[dbe9b08] | 792 | //============================================================================================= |
---|
| 793 | // Unexpected Terminating logic |
---|
| 794 | //============================================================================================= |
---|
[ea7d2b0] | 795 | static __spinlock_t kernel_abort_lock; |
---|
[9d944b2] | 796 | static bool kernel_abort_called = false; |
---|
| 797 | |
---|
[afd550c] | 798 | void * kernel_abort(void) __attribute__ ((__nothrow__)) { |
---|
[9d944b2] | 799 | // abort cannot be recursively entered by the same or different processors because all signal handlers return when |
---|
| 800 | // the globalAbort flag is true. |
---|
[36982fc] | 801 | lock( kernel_abort_lock __cfaabi_dbg_ctx2 ); |
---|
[9d944b2] | 802 | |
---|
| 803 | // first task to abort ? |
---|
[de94a60] | 804 | if ( kernel_abort_called ) { // not first task to abort ? |
---|
[ea7d2b0] | 805 | unlock( kernel_abort_lock ); |
---|
[1c273d0] | 806 | |
---|
[9d944b2] | 807 | sigset_t mask; |
---|
| 808 | sigemptyset( &mask ); |
---|
[de94a60] | 809 | sigaddset( &mask, SIGALRM ); // block SIGALRM signals |
---|
| 810 | sigsuspend( &mask ); // block the processor to prevent further damage during abort |
---|
| 811 | _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it |
---|
| 812 | } |
---|
| 813 | else { |
---|
| 814 | kernel_abort_called = true; |
---|
| 815 | unlock( kernel_abort_lock ); |
---|
[9d944b2] | 816 | } |
---|
| 817 | |
---|
[14a61b5] | 818 | return kernelTLS.this_thread; |
---|
[9d944b2] | 819 | } |
---|
| 820 | |
---|
| 821 | void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) { |
---|
| 822 | thread_desc * thrd = kernel_data; |
---|
| 823 | |
---|
[de94a60] | 824 | if(thrd) { |
---|
| 825 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd ); |
---|
[1c40091] | 826 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
[de94a60] | 827 | |
---|
[212c2187] | 828 | if ( &thrd->self_cor != thrd->curr_cor ) { |
---|
| 829 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor ); |
---|
[1c40091] | 830 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
[de94a60] | 831 | } |
---|
| 832 | else { |
---|
[1c40091] | 833 | __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 ); |
---|
[de94a60] | 834 | } |
---|
[1c273d0] | 835 | } |
---|
[9d944b2] | 836 | else { |
---|
[de94a60] | 837 | int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" ); |
---|
[1c40091] | 838 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
[9d944b2] | 839 | } |
---|
| 840 | } |
---|
| 841 | |
---|
[2b8bc41] | 842 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) { |
---|
[14a61b5] | 843 | return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2; |
---|
[2b8bc41] | 844 | } |
---|
| 845 | |
---|
[de94a60] | 846 | static __spinlock_t kernel_debug_lock; |
---|
| 847 | |
---|
[9d944b2] | 848 | extern "C" { |
---|
[1c40091] | 849 | void __cfaabi_bits_acquire() { |
---|
[36982fc] | 850 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 ); |
---|
[9d944b2] | 851 | } |
---|
| 852 | |
---|
[1c40091] | 853 | void __cfaabi_bits_release() { |
---|
[ea7d2b0] | 854 | unlock( kernel_debug_lock ); |
---|
[9d944b2] | 855 | } |
---|
[8118303] | 856 | } |
---|
| 857 | |
---|
[fa21ac9] | 858 | //============================================================================================= |
---|
| 859 | // Kernel Utilities |
---|
| 860 | //============================================================================================= |
---|
[bd98b58] | 861 | //----------------------------------------------------------------------------- |
---|
| 862 | // Locks |
---|
[242a902] | 863 | void ?{}( semaphore & this, int count = 1 ) { |
---|
| 864 | (this.lock){}; |
---|
| 865 | this.count = count; |
---|
| 866 | (this.waiting){}; |
---|
[db6f06a] | 867 | } |
---|
[242a902] | 868 | void ^?{}(semaphore & this) {} |
---|
[db6f06a] | 869 | |
---|
[65deb18] | 870 | void P(semaphore & this) with( this ){ |
---|
| 871 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 872 | count -= 1; |
---|
| 873 | if ( count < 0 ) { |
---|
[bdeba0b] | 874 | // queue current task |
---|
[14a61b5] | 875 | append( waiting, kernelTLS.this_thread ); |
---|
[bdeba0b] | 876 | |
---|
| 877 | // atomically release spin lock and block |
---|
[65deb18] | 878 | BlockInternal( &lock ); |
---|
[8def349] | 879 | } |
---|
[4e6fb8e] | 880 | else { |
---|
[65deb18] | 881 | unlock( lock ); |
---|
[4e6fb8e] | 882 | } |
---|
[bd98b58] | 883 | } |
---|
| 884 | |
---|
[65deb18] | 885 | void V(semaphore & this) with( this ) { |
---|
[bdeba0b] | 886 | thread_desc * thrd = NULL; |
---|
[65deb18] | 887 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 888 | count += 1; |
---|
| 889 | if ( count <= 0 ) { |
---|
[bdeba0b] | 890 | // remove task at head of waiting list |
---|
[65deb18] | 891 | thrd = pop_head( waiting ); |
---|
[bd98b58] | 892 | } |
---|
[bdeba0b] | 893 | |
---|
[65deb18] | 894 | unlock( lock ); |
---|
[bdeba0b] | 895 | |
---|
| 896 | // make new owner |
---|
| 897 | WakeThread( thrd ); |
---|
[bd98b58] | 898 | } |
---|
| 899 | |
---|
[f7d6bb0] | 900 | //----------------------------------------------------------------------------- |
---|
[de94a60] | 901 | // Global Queues |
---|
| 902 | void doregister( cluster & cltr ) { |
---|
[ea8b2f7] | 903 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2); |
---|
| 904 | push_front( __cfa_dbg_global_clusters.list, cltr ); |
---|
| 905 | unlock ( __cfa_dbg_global_clusters.lock ); |
---|
[de94a60] | 906 | } |
---|
[f7d6bb0] | 907 | |
---|
[de94a60] | 908 | void unregister( cluster & cltr ) { |
---|
[ea8b2f7] | 909 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2); |
---|
| 910 | remove( __cfa_dbg_global_clusters.list, cltr ); |
---|
| 911 | unlock( __cfa_dbg_global_clusters.lock ); |
---|
[de94a60] | 912 | } |
---|
[f7d6bb0] | 913 | |
---|
[a1a17a7] | 914 | void doregister( cluster * cltr, thread_desc & thrd ) { |
---|
| 915 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2); |
---|
[d4e68a6] | 916 | cltr->nthreads += 1; |
---|
[a1a17a7] | 917 | push_front(cltr->threads, thrd); |
---|
| 918 | unlock (cltr->thread_list_lock); |
---|
| 919 | } |
---|
| 920 | |
---|
| 921 | void unregister( cluster * cltr, thread_desc & thrd ) { |
---|
| 922 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2); |
---|
| 923 | remove(cltr->threads, thrd ); |
---|
[d4e68a6] | 924 | cltr->nthreads -= 1; |
---|
[a1a17a7] | 925 | unlock(cltr->thread_list_lock); |
---|
| 926 | } |
---|
[9181f1d] | 927 | |
---|
[de94a60] | 928 | void doregister( cluster * cltr, processor * proc ) { |
---|
[639991a] | 929 | lock (cltr->proc_list_lock __cfaabi_dbg_ctx2); |
---|
[d4e68a6] | 930 | cltr->nprocessors += 1; |
---|
[639991a] | 931 | push_front(cltr->procs, *proc); |
---|
| 932 | unlock (cltr->proc_list_lock); |
---|
[de94a60] | 933 | } |
---|
| 934 | |
---|
| 935 | void unregister( cluster * cltr, processor * proc ) { |
---|
[639991a] | 936 | lock (cltr->proc_list_lock __cfaabi_dbg_ctx2); |
---|
| 937 | remove(cltr->procs, *proc ); |
---|
[d4e68a6] | 938 | cltr->nprocessors -= 1; |
---|
[639991a] | 939 | unlock(cltr->proc_list_lock); |
---|
[de94a60] | 940 | } |
---|
| 941 | |
---|
| 942 | //----------------------------------------------------------------------------- |
---|
| 943 | // Debug |
---|
| 944 | __cfaabi_dbg_debug_do( |
---|
[1997b4e] | 945 | extern "C" { |
---|
| 946 | void __cfaabi_dbg_record(__spinlock_t & this, const char * prev_name) { |
---|
| 947 | this.prev_name = prev_name; |
---|
| 948 | this.prev_thrd = kernelTLS.this_thread; |
---|
| 949 | } |
---|
[9181f1d] | 950 | } |
---|
[f7d6bb0] | 951 | ) |
---|
[2026bb6] | 952 | |
---|
| 953 | //----------------------------------------------------------------------------- |
---|
| 954 | // Debug |
---|
| 955 | bool threading_enabled(void) { |
---|
| 956 | return true; |
---|
| 957 | } |
---|
[8118303] | 958 | // Local Variables: // |
---|
| 959 | // mode: c // |
---|
| 960 | // tab-width: 4 // |
---|
| 961 | // End: // |
---|