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