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