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