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