[e660761] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2020 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/startup.cfa -- |
---|
| 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
| 10 | // Created On : Thu Jul 30 15:12:54 2020 |
---|
| 11 | // Last Modified By : |
---|
| 12 | // Last Modified On : |
---|
| 13 | // Update Count : |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #define __cforall_thread__ |
---|
[43784ac] | 17 | #define _GNU_SOURCE |
---|
[e660761] | 18 | |
---|
| 19 | // C Includes |
---|
| 20 | #include <errno.h> // errno |
---|
[f558b5f] | 21 | #include <signal.h> |
---|
[e660761] | 22 | #include <string.h> // strerror |
---|
| 23 | #include <unistd.h> // sysconf |
---|
[f558b5f] | 24 | |
---|
[e660761] | 25 | extern "C" { |
---|
| 26 | #include <limits.h> // PTHREAD_STACK_MIN |
---|
[f558b5f] | 27 | #include <unistd.h> // syscall |
---|
[dddb3dd0] | 28 | #include <sys/eventfd.h> // eventfd |
---|
[e660761] | 29 | #include <sys/mman.h> // mprotect |
---|
| 30 | #include <sys/resource.h> // getrlimit |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | // CFA Includes |
---|
| 34 | #include "kernel_private.hfa" |
---|
| 35 | #include "startup.hfa" // STARTUP_PRIORITY_XXX |
---|
[145dcd5] | 36 | #include "limits.hfa" |
---|
[bfcf6b9] | 37 | #include "math.hfa" |
---|
[e660761] | 38 | |
---|
[97229d6] | 39 | #define CFA_PROCESSOR_USE_MMAP 0 |
---|
| 40 | |
---|
[e660761] | 41 | //----------------------------------------------------------------------------- |
---|
| 42 | // Some assembly required |
---|
| 43 | #if defined( __i386 ) |
---|
[88cafe7] | 44 | #define CtxGet( ctx ) __asm__ volatile ( \ |
---|
| 45 | "movl %%esp,%0\n" \ |
---|
| 46 | "movl %%ebp,%1\n" \ |
---|
| 47 | : "=rm" (ctx.SP), \ |
---|
| 48 | "=rm" (ctx.FP) \ |
---|
| 49 | ) |
---|
[e660761] | 50 | #elif defined( __x86_64 ) |
---|
[88cafe7] | 51 | #define CtxGet( ctx ) __asm__ volatile ( \ |
---|
| 52 | "movq %%rsp,%0\n" \ |
---|
| 53 | "movq %%rbp,%1\n" \ |
---|
| 54 | : "=rm" (ctx.SP), \ |
---|
| 55 | "=rm" (ctx.FP) \ |
---|
| 56 | ) |
---|
| 57 | #elif defined( __aarch64__ ) |
---|
| 58 | #define CtxGet( ctx ) __asm__ volatile ( \ |
---|
| 59 | "mov %0, sp\n" \ |
---|
| 60 | "mov %1, fp\n" \ |
---|
| 61 | : "=rm" (ctx.SP), \ |
---|
| 62 | "=rm" (ctx.FP) \ |
---|
| 63 | ) |
---|
[e660761] | 64 | #else |
---|
| 65 | #error unknown hardware architecture |
---|
| 66 | #endif |
---|
| 67 | |
---|
| 68 | //----------------------------------------------------------------------------- |
---|
| 69 | // Start and stop routine for the kernel, declared first to make sure they run first |
---|
| 70 | static void __kernel_startup (void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) )); |
---|
| 71 | static void __kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) )); |
---|
| 72 | |
---|
| 73 | //----------------------------------------------------------------------------- |
---|
| 74 | // Static Forward Declarations |
---|
| 75 | struct current_stack_info_t; |
---|
| 76 | |
---|
| 77 | static void * __invoke_processor(void * arg); |
---|
| 78 | static void __kernel_first_resume( processor * this ); |
---|
| 79 | static void __kernel_last_resume ( processor * this ); |
---|
[e84ab3d] | 80 | static void init(processor & this, const char name[], cluster & _cltr, thread$ * initT); |
---|
[e660761] | 81 | static void deinit(processor & this); |
---|
| 82 | static void doregister( struct cluster & cltr ); |
---|
| 83 | static void unregister( struct cluster & cltr ); |
---|
[c993b15] | 84 | static void register_tls( processor * this ); |
---|
| 85 | static void unregister_tls( processor * this ); |
---|
[e84ab3d] | 86 | static void ?{}( coroutine$ & this, current_stack_info_t * info); |
---|
| 87 | static void ?{}( thread$ & this, current_stack_info_t * info); |
---|
[e660761] | 88 | static void ?{}(processorCtx_t & this) {} |
---|
| 89 | static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info); |
---|
| 90 | |
---|
[f2384c9a] | 91 | #if defined(__CFA_WITH_VERIFY__) |
---|
| 92 | static bool verify_fwd_bck_rng(void); |
---|
| 93 | #endif |
---|
| 94 | |
---|
[e660761] | 95 | //----------------------------------------------------------------------------- |
---|
| 96 | // Forward Declarations for other modules |
---|
| 97 | extern void __kernel_alarm_startup(void); |
---|
| 98 | extern void __kernel_alarm_shutdown(void); |
---|
| 99 | |
---|
| 100 | //----------------------------------------------------------------------------- |
---|
| 101 | // Other Forward Declarations |
---|
[1eb239e4] | 102 | extern void __wake_proc(processor *); |
---|
[7dd98b6] | 103 | extern int cfa_main_returned; // from interpose.cfa |
---|
[1959528] | 104 | extern uint32_t __global_random_seed; |
---|
[e660761] | 105 | |
---|
| 106 | //----------------------------------------------------------------------------- |
---|
| 107 | // Kernel storage |
---|
| 108 | KERNEL_STORAGE(cluster, mainCluster); |
---|
| 109 | KERNEL_STORAGE(processor, mainProcessor); |
---|
[e84ab3d] | 110 | KERNEL_STORAGE(thread$, mainThread); |
---|
[e660761] | 111 | KERNEL_STORAGE(__stack_t, mainThreadCtx); |
---|
| 112 | KERNEL_STORAGE(__scheduler_RWLock_t, __scheduler_lock); |
---|
| 113 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 114 | KERNEL_STORAGE(__stats_t, mainProcStats); |
---|
| 115 | #endif |
---|
| 116 | |
---|
| 117 | cluster * mainCluster; |
---|
| 118 | processor * mainProcessor; |
---|
[e84ab3d] | 119 | thread$ * mainThread; |
---|
[e660761] | 120 | __scheduler_RWLock_t * __scheduler_lock; |
---|
| 121 | |
---|
| 122 | extern "C" { |
---|
| 123 | struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters; |
---|
| 124 | } |
---|
| 125 | |
---|
[dd92fe9] | 126 | extern size_t __page_size; |
---|
[28c35e2] | 127 | extern int __map_prot; |
---|
[e660761] | 128 | |
---|
| 129 | //----------------------------------------------------------------------------- |
---|
| 130 | // Global state |
---|
[8fc652e0] | 131 | thread_local struct KernelThreadData __cfaabi_tls __attribute__ ((tls_model ( "initial-exec" ))) @= { |
---|
[e660761] | 132 | NULL, // cannot use 0p |
---|
| 133 | NULL, |
---|
[c993b15] | 134 | false, |
---|
[e660761] | 135 | { 1, false, false }, |
---|
[c993b15] | 136 | 0, |
---|
| 137 | { 0, 0 }, |
---|
| 138 | NULL, |
---|
| 139 | #ifdef __CFA_WITH_VERIFY__ |
---|
| 140 | false, |
---|
| 141 | 0, |
---|
| 142 | #endif |
---|
[e660761] | 143 | }; |
---|
| 144 | |
---|
[3489ea6] | 145 | #if defined(CFA_HAVE_LINUX_LIBRSEQ) |
---|
| 146 | // No data needed |
---|
| 147 | #elif defined(CFA_HAVE_LINUX_RSEQ_H) |
---|
| 148 | extern "Cforall" { |
---|
[f558b5f] | 149 | __attribute__((aligned(128))) thread_local volatile struct rseq __cfaabi_rseq @= { |
---|
| 150 | .cpu_id : RSEQ_CPU_ID_UNINITIALIZED, |
---|
| 151 | }; |
---|
[3489ea6] | 152 | } |
---|
| 153 | #else |
---|
| 154 | // No data needed |
---|
| 155 | #endif |
---|
| 156 | |
---|
[e660761] | 157 | //----------------------------------------------------------------------------- |
---|
| 158 | // Struct to steal stack |
---|
| 159 | struct current_stack_info_t { |
---|
| 160 | __stack_t * storage; // pointer to stack object |
---|
| 161 | void * base; // base of stack |
---|
| 162 | void * limit; // stack grows towards stack limit |
---|
| 163 | void * context; // address of cfa_context_t |
---|
| 164 | }; |
---|
| 165 | |
---|
| 166 | void ?{}( current_stack_info_t & this ) { |
---|
| 167 | __stack_context_t ctx; |
---|
| 168 | CtxGet( ctx ); |
---|
| 169 | this.base = ctx.FP; |
---|
| 170 | |
---|
| 171 | rlimit r; |
---|
| 172 | getrlimit( RLIMIT_STACK, &r); |
---|
| 173 | size_t size = r.rlim_cur; |
---|
| 174 | |
---|
| 175 | this.limit = (void *)(((intptr_t)this.base) - size); |
---|
| 176 | this.context = &storage_mainThreadCtx; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | //============================================================================================= |
---|
| 181 | // Kernel Setup logic |
---|
| 182 | //============================================================================================= |
---|
| 183 | //----------------------------------------------------------------------------- |
---|
| 184 | // Kernel boot procedures |
---|
| 185 | static void __kernel_startup(void) { |
---|
[8fc652e0] | 186 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[e660761] | 187 | __cfadbg_print_safe(runtime_core, "Kernel : Starting\n"); |
---|
| 188 | |
---|
| 189 | __cfa_dbg_global_clusters.list{ __get }; |
---|
| 190 | __cfa_dbg_global_clusters.lock{}; |
---|
| 191 | |
---|
[f2384c9a] | 192 | /* paranoid */ verify( verify_fwd_bck_rng() ); |
---|
| 193 | |
---|
[e660761] | 194 | // Initialize the global scheduler lock |
---|
| 195 | __scheduler_lock = (__scheduler_RWLock_t*)&storage___scheduler_lock; |
---|
| 196 | (*__scheduler_lock){}; |
---|
| 197 | |
---|
| 198 | // Initialize the main cluster |
---|
| 199 | mainCluster = (cluster *)&storage_mainCluster; |
---|
| 200 | (*mainCluster){"Main Cluster", 0}; |
---|
| 201 | |
---|
| 202 | __cfadbg_print_safe(runtime_core, "Kernel : Main cluster ready\n"); |
---|
| 203 | |
---|
| 204 | // Construct the processor context of the main processor |
---|
| 205 | void ?{}(processorCtx_t & this, processor * proc) { |
---|
| 206 | (this.__cor){ "Processor" }; |
---|
| 207 | this.__cor.starter = 0p; |
---|
| 208 | this.proc = proc; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | void ?{}(processor & this) with( this ) { |
---|
[454f478] | 212 | ( this.terminated ){}; |
---|
[e660761] | 213 | ( this.runner ){}; |
---|
[a5e7233] | 214 | init( this, "Main Processor", *mainCluster, 0p ); |
---|
[e660761] | 215 | kernel_thread = pthread_self(); |
---|
| 216 | |
---|
| 217 | runner{ &this }; |
---|
| 218 | __cfadbg_print_safe(runtime_core, "Kernel : constructed main processor context %p\n", &runner); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | // Initialize the main processor and the main processor ctx |
---|
| 222 | // (the coroutine that contains the processing control flow) |
---|
| 223 | mainProcessor = (processor *)&storage_mainProcessor; |
---|
| 224 | (*mainProcessor){}; |
---|
| 225 | |
---|
[c993b15] | 226 | register_tls( mainProcessor ); |
---|
| 227 | |
---|
[24e321c] | 228 | // Start by initializing the main thread |
---|
| 229 | // SKULLDUGGERY: the mainThread steals the process main thread |
---|
| 230 | // which will then be scheduled by the mainProcessor normally |
---|
| 231 | mainThread = (thread$ *)&storage_mainThread; |
---|
| 232 | current_stack_info_t info; |
---|
| 233 | info.storage = (__stack_t*)&storage_mainThreadCtx; |
---|
| 234 | (*mainThread){ &info }; |
---|
| 235 | |
---|
| 236 | __cfadbg_print_safe(runtime_core, "Kernel : Main thread ready\n"); |
---|
| 237 | |
---|
[e660761] | 238 | //initialize the global state variables |
---|
[8fc652e0] | 239 | __cfaabi_tls.this_processor = mainProcessor; |
---|
| 240 | __cfaabi_tls.this_thread = mainThread; |
---|
[e660761] | 241 | |
---|
| 242 | #if !defined( __CFA_NO_STATISTICS__ ) |
---|
[8fc652e0] | 243 | __cfaabi_tls.this_stats = (__stats_t *)& storage_mainProcStats; |
---|
| 244 | __init_stats( __cfaabi_tls.this_stats ); |
---|
[e660761] | 245 | #endif |
---|
[a67c5b6] | 246 | mainProcessor->local_data = &__cfaabi_tls; |
---|
[e660761] | 247 | |
---|
| 248 | // Enable preemption |
---|
| 249 | __kernel_alarm_startup(); |
---|
| 250 | |
---|
| 251 | // Add the main thread to the ready queue |
---|
| 252 | // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread |
---|
[24e321c] | 253 | schedule_thread$(mainThread, UNPARK_LOCAL); |
---|
[e660761] | 254 | |
---|
| 255 | // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX |
---|
| 256 | // context. Hence, the main thread does not begin through __cfactx_invoke_thread, like all other threads. The trick here is that |
---|
| 257 | // mainThread is on the ready queue when this call is made. |
---|
[8fc652e0] | 258 | __kernel_first_resume( __cfaabi_tls.this_processor ); |
---|
[e660761] | 259 | |
---|
| 260 | |
---|
| 261 | // THE SYSTEM IS NOW COMPLETELY RUNNING |
---|
| 262 | |
---|
| 263 | __cfadbg_print_safe(runtime_core, "Kernel : Started\n--------------------------------------------------\n\n"); |
---|
| 264 | |
---|
[8fc652e0] | 265 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[a3821fa] | 266 | enable_interrupts(); |
---|
[8fc652e0] | 267 | /* paranoid */ verify( __preemption_enabled() ); |
---|
| 268 | |
---|
[e660761] | 269 | } |
---|
| 270 | |
---|
| 271 | static void __kernel_shutdown(void) { |
---|
[7dd98b6] | 272 | if(!cfa_main_returned) return; |
---|
[8fc652e0] | 273 | /* paranoid */ verify( __preemption_enabled() ); |
---|
[e660761] | 274 | disable_interrupts(); |
---|
[8fc652e0] | 275 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[e660761] | 276 | |
---|
| 277 | __cfadbg_print_safe(runtime_core, "\n--------------------------------------------------\nKernel : Shutting down\n"); |
---|
| 278 | |
---|
| 279 | // SKULLDUGGERY: Notify the mainProcessor it needs to terminates. |
---|
| 280 | // When its coroutine terminates, it return control to the mainThread |
---|
| 281 | // which is currently here |
---|
[7d0ebd0] | 282 | /* paranoid */ verify( !__atomic_load_n(&mainProcessor->do_terminate, __ATOMIC_ACQUIRE) ); |
---|
[e660761] | 283 | __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE); |
---|
[7d0ebd0] | 284 | __wake_proc( mainProcessor ); |
---|
[8fc652e0] | 285 | __kernel_last_resume( __cfaabi_tls.this_processor ); |
---|
[e660761] | 286 | mainThread->self_cor.state = Halted; |
---|
| 287 | |
---|
| 288 | // THE SYSTEM IS NOW COMPLETELY STOPPED |
---|
| 289 | |
---|
| 290 | // Disable preemption |
---|
| 291 | __kernel_alarm_shutdown(); |
---|
| 292 | |
---|
[a5a01faa] | 293 | #if !defined( __CFA_NO_STATISTICS__ ) |
---|
| 294 | __stats_t * st = (__stats_t *)& storage_mainProcStats; |
---|
| 295 | __tally_stats(mainCluster->stats, st); |
---|
| 296 | if( 0 != mainProcessor->print_stats ) { |
---|
| 297 | __print_stats( st, mainProcessor->print_stats, "Processor ", mainProcessor->name, (void*)mainProcessor ); |
---|
| 298 | } |
---|
[73f4d08] | 299 | #if defined(CFA_STATS_ARRAY) |
---|
| 300 | __flush_stat( st, "Processor", mainProcessor ); |
---|
| 301 | #endif |
---|
[a5a01faa] | 302 | #endif |
---|
| 303 | |
---|
[a67c5b6] | 304 | mainProcessor->local_data = 0p; |
---|
| 305 | |
---|
[c993b15] | 306 | unregister_tls( mainProcessor ); |
---|
| 307 | |
---|
[e660761] | 308 | // Destroy the main processor and its context in reverse order of construction |
---|
| 309 | // These were manually constructed so we need manually destroy them |
---|
| 310 | void ^?{}(processor & this) with( this ){ |
---|
| 311 | deinit( this ); |
---|
| 312 | |
---|
| 313 | /* paranoid */ verify( this.do_terminate == true ); |
---|
| 314 | __cfaabi_dbg_print_safe("Kernel : destroyed main processor context %p\n", &runner); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | ^(*mainProcessor){}; |
---|
| 318 | |
---|
| 319 | // Final step, destroy the main thread since it is no longer needed |
---|
| 320 | |
---|
| 321 | // Since we provided a stack to this taxk it will not destroy anything |
---|
| 322 | /* paranoid */ verify(mainThread->self_cor.stack.storage == (__stack_t*)(((uintptr_t)&storage_mainThreadCtx)| 0x1)); |
---|
| 323 | ^(*mainThread){}; |
---|
| 324 | |
---|
| 325 | ^(*mainCluster){}; |
---|
| 326 | |
---|
| 327 | ^(*__scheduler_lock){}; |
---|
| 328 | |
---|
| 329 | ^(__cfa_dbg_global_clusters.list){}; |
---|
| 330 | ^(__cfa_dbg_global_clusters.lock){}; |
---|
| 331 | |
---|
| 332 | __cfadbg_print_safe(runtime_core, "Kernel : Shutdown complete\n"); |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | //============================================================================================= |
---|
| 336 | // Kernel Initial Scheduling logic |
---|
| 337 | //============================================================================================= |
---|
| 338 | |
---|
| 339 | // Context invoker for processors |
---|
| 340 | // This is the entry point for processors (kernel threads) *except* for the main processor |
---|
| 341 | // It effectively constructs a coroutine by stealing the pthread stack |
---|
| 342 | static void * __invoke_processor(void * arg) { |
---|
| 343 | #if !defined( __CFA_NO_STATISTICS__ ) |
---|
| 344 | __stats_t local_stats; |
---|
| 345 | __init_stats( &local_stats ); |
---|
[8fc652e0] | 346 | __cfaabi_tls.this_stats = &local_stats; |
---|
[e660761] | 347 | #endif |
---|
| 348 | |
---|
| 349 | processor * proc = (processor *) arg; |
---|
[8fc652e0] | 350 | __cfaabi_tls.this_processor = proc; |
---|
| 351 | __cfaabi_tls.this_thread = 0p; |
---|
| 352 | __cfaabi_tls.preemption_state.[enabled, disable_count] = [false, 1]; |
---|
[a67c5b6] | 353 | proc->local_data = &__cfaabi_tls; |
---|
[c993b15] | 354 | |
---|
| 355 | register_tls( proc ); |
---|
| 356 | |
---|
[e660761] | 357 | // SKULLDUGGERY: We want to create a context for the processor coroutine |
---|
| 358 | // which is needed for the 2-step context switch. However, there is no reason |
---|
| 359 | // to waste the perfectly valid stack create by pthread. |
---|
| 360 | current_stack_info_t info; |
---|
| 361 | __stack_t ctx; |
---|
| 362 | info.storage = &ctx; |
---|
| 363 | (proc->runner){ proc, &info }; |
---|
| 364 | |
---|
| 365 | __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage); |
---|
| 366 | |
---|
| 367 | //Set global state |
---|
[8fc652e0] | 368 | __cfaabi_tls.this_thread = 0p; |
---|
[e660761] | 369 | |
---|
| 370 | //We now have a proper context from which to schedule threads |
---|
| 371 | __cfadbg_print_safe(runtime_core, "Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx); |
---|
| 372 | |
---|
| 373 | // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't |
---|
| 374 | // resume it to start it like it normally would, it will just context switch |
---|
| 375 | // back to here. Instead directly call the main since we already are on the |
---|
| 376 | // appropriate stack. |
---|
| 377 | get_coroutine(proc->runner)->state = Active; |
---|
| 378 | main( proc->runner ); |
---|
| 379 | get_coroutine(proc->runner)->state = Halted; |
---|
| 380 | |
---|
| 381 | // Main routine of the core returned, the core is now fully terminated |
---|
| 382 | __cfadbg_print_safe(runtime_core, "Kernel : core %p main ended (%p)\n", proc, &proc->runner); |
---|
| 383 | |
---|
| 384 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 385 | __tally_stats(proc->cltr->stats, &local_stats); |
---|
| 386 | if( 0 != proc->print_stats ) { |
---|
[1b033b8] | 387 | __print_stats( &local_stats, proc->print_stats, "Processor ", proc->name, (void*)proc ); |
---|
[e660761] | 388 | } |
---|
[73f4d08] | 389 | #if defined(CFA_STATS_ARRAY) |
---|
| 390 | __flush_stat( &local_stats, "Processor", proc ); |
---|
| 391 | #endif |
---|
[e660761] | 392 | #endif |
---|
| 393 | |
---|
[a67c5b6] | 394 | proc->local_data = 0p; |
---|
| 395 | |
---|
[c993b15] | 396 | unregister_tls( proc ); |
---|
| 397 | |
---|
[e660761] | 398 | return 0p; |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | static void __kernel_first_resume( processor * this ) { |
---|
[e84ab3d] | 402 | thread$ * src = mainThread; |
---|
| 403 | coroutine$ * dst = get_coroutine(this->runner); |
---|
[e660761] | 404 | |
---|
[8fc652e0] | 405 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[e660761] | 406 | |
---|
[8fc652e0] | 407 | __cfaabi_tls.this_thread->curr_cor = dst; |
---|
[eaf269d] | 408 | __stack_prepare( &dst->stack, DEFAULT_STACK_SIZE ); |
---|
[e660761] | 409 | __cfactx_start(main, dst, this->runner, __cfactx_invoke_coroutine); |
---|
| 410 | |
---|
[8fc652e0] | 411 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[e660761] | 412 | |
---|
| 413 | dst->last = &src->self_cor; |
---|
| 414 | dst->starter = dst->starter ? dst->starter : &src->self_cor; |
---|
| 415 | |
---|
| 416 | // make sure the current state is still correct |
---|
| 417 | /* paranoid */ verify(src->state == Ready); |
---|
[ab5baab] | 418 | src->corctx_flag = true; |
---|
[e660761] | 419 | |
---|
| 420 | // context switch to specified coroutine |
---|
| 421 | verify( dst->context.SP ); |
---|
| 422 | __cfactx_switch( &src->context, &dst->context ); |
---|
| 423 | // when __cfactx_switch returns we are back in the src coroutine |
---|
| 424 | |
---|
| 425 | mainThread->curr_cor = &mainThread->self_cor; |
---|
| 426 | |
---|
| 427 | // make sure the current state has been update |
---|
| 428 | /* paranoid */ verify(src->state == Active); |
---|
| 429 | |
---|
[8fc652e0] | 430 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[e660761] | 431 | } |
---|
| 432 | |
---|
| 433 | // KERNEL_ONLY |
---|
| 434 | static void __kernel_last_resume( processor * this ) { |
---|
[e84ab3d] | 435 | coroutine$ * src = &mainThread->self_cor; |
---|
| 436 | coroutine$ * dst = get_coroutine(this->runner); |
---|
[e660761] | 437 | |
---|
[8fc652e0] | 438 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 439 | /* paranoid */ verify( dst->starter == src ); |
---|
| 440 | /* paranoid */ verify( dst->context.SP ); |
---|
[e660761] | 441 | |
---|
| 442 | // SKULLDUGGERY in debug the processors check that the |
---|
| 443 | // stack is still within the limit of the stack limits after running a thread. |
---|
| 444 | // that check doesn't make sense if we context switch to the processor using the |
---|
| 445 | // coroutine semantics. Since this is a special case, use the current context |
---|
| 446 | // info to populate these fields. |
---|
| 447 | __cfaabi_dbg_debug_do( |
---|
| 448 | __stack_context_t ctx; |
---|
| 449 | CtxGet( ctx ); |
---|
| 450 | mainThread->context.SP = ctx.SP; |
---|
| 451 | mainThread->context.FP = ctx.FP; |
---|
| 452 | ) |
---|
| 453 | |
---|
| 454 | // context switch to the processor |
---|
| 455 | __cfactx_switch( &src->context, &dst->context ); |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | |
---|
| 459 | //============================================================================================= |
---|
| 460 | // Kernel Object Constructors logic |
---|
| 461 | //============================================================================================= |
---|
| 462 | //----------------------------------------------------------------------------- |
---|
| 463 | // Main thread construction |
---|
[e84ab3d] | 464 | static void ?{}( coroutine$ & this, current_stack_info_t * info) with( this ) { |
---|
[e660761] | 465 | stack.storage = info->storage; |
---|
| 466 | with(*stack.storage) { |
---|
| 467 | limit = info->limit; |
---|
| 468 | base = info->base; |
---|
| 469 | } |
---|
| 470 | __attribute__((may_alias)) intptr_t * istorage = (intptr_t*) &stack.storage; |
---|
| 471 | *istorage |= 0x1; |
---|
| 472 | name = "Main Thread"; |
---|
| 473 | state = Start; |
---|
| 474 | starter = 0p; |
---|
| 475 | last = 0p; |
---|
| 476 | cancellation = 0p; |
---|
| 477 | } |
---|
| 478 | |
---|
[e84ab3d] | 479 | static void ?{}( thread$ & this, current_stack_info_t * info) with( this ) { |
---|
[6a77224] | 480 | ticket = TICKET_RUNNING; |
---|
[e660761] | 481 | state = Start; |
---|
| 482 | self_cor{ info }; |
---|
| 483 | curr_cor = &self_cor; |
---|
| 484 | curr_cluster = mainCluster; |
---|
| 485 | self_mon.owner = &this; |
---|
| 486 | self_mon.recursion = 1; |
---|
| 487 | self_mon_p = &self_mon; |
---|
| 488 | link.next = 0p; |
---|
[ef94ae7] | 489 | link.ts = -1llu; |
---|
[24e321c] | 490 | preferred = ready_queue_new_preferred(); |
---|
[89eff25] | 491 | last_proc = 0p; |
---|
[1959528] | 492 | random_state = __global_random_seed; |
---|
[b4b63e8] | 493 | #if defined( __CFA_WITH_VERIFY__ ) |
---|
[ac12f1f] | 494 | canary = 0x0D15EA5E0D15EA5Ep; |
---|
[b4b63e8] | 495 | #endif |
---|
[e660761] | 496 | |
---|
| 497 | node.next = 0p; |
---|
| 498 | node.prev = 0p; |
---|
| 499 | doregister(curr_cluster, this); |
---|
| 500 | |
---|
| 501 | monitors{ &self_mon_p, 1, (fptr_t)0 }; |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | //----------------------------------------------------------------------------- |
---|
| 505 | // Processor |
---|
| 506 | // Construct the processor context of non-main processors |
---|
| 507 | static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) { |
---|
| 508 | (this.__cor){ info }; |
---|
| 509 | this.proc = proc; |
---|
| 510 | } |
---|
| 511 | |
---|
[e84ab3d] | 512 | static void init(processor & this, const char name[], cluster & _cltr, thread$ * initT) with( this ) { |
---|
[e660761] | 513 | this.name = name; |
---|
| 514 | this.cltr = &_cltr; |
---|
[431cd4f] | 515 | this.rdq.its = 0; |
---|
| 516 | this.rdq.itr = 0; |
---|
[145dcd5] | 517 | this.rdq.id = MAX; |
---|
| 518 | this.rdq.target = MAX; |
---|
| 519 | this.rdq.last = MAX; |
---|
| 520 | this.rdq.cpu = 0; |
---|
| 521 | // this.rdq.cutoff = 0ull; |
---|
[e660761] | 522 | do_terminate = false; |
---|
| 523 | preemption_alarm = 0p; |
---|
| 524 | pending_preemption = false; |
---|
| 525 | |
---|
[78da4ab] | 526 | this.io.ctx = 0p; |
---|
[dddb3dd0] | 527 | this.io.pending = false; |
---|
| 528 | this.io.dirty = false; |
---|
| 529 | |
---|
[a5e7233] | 530 | this.init.thrd = initT; |
---|
[a1538cd] | 531 | |
---|
[a67c5b6] | 532 | this.local_data = 0p; |
---|
| 533 | |
---|
[1757f98] | 534 | this.idle_fd = eventfd(0, 0); |
---|
| 535 | if (idle_fd < 0) { |
---|
[dddb3dd0] | 536 | abort("KERNEL ERROR: PROCESSOR EVENTFD - %s\n", strerror(errno)); |
---|
| 537 | } |
---|
[78da4ab] | 538 | |
---|
[7cf3b1d] | 539 | this.idle_wctx.fd = 0; |
---|
| 540 | |
---|
| 541 | // I'm assuming these two are reserved for standard input and output |
---|
| 542 | // so I'm using them as sentinels with idle_wctx. |
---|
| 543 | /* paranoid */ verify( this.idle_fd != 0 ); |
---|
| 544 | /* paranoid */ verify( this.idle_fd != 1 ); |
---|
| 545 | |
---|
[e660761] | 546 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 547 | print_stats = 0; |
---|
| 548 | print_halts = false; |
---|
| 549 | #endif |
---|
| 550 | |
---|
| 551 | __cfadbg_print_safe(runtime_core, "Kernel : core %p created\n", &this); |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | // Not a ctor, it just preps the destruction but should not destroy members |
---|
| 555 | static void deinit(processor & this) { |
---|
[1757f98] | 556 | close(this.idle_fd); |
---|
[e660761] | 557 | } |
---|
| 558 | |
---|
[e84ab3d] | 559 | void ?{}(processor & this, const char name[], cluster & _cltr, thread$ * initT) { |
---|
[454f478] | 560 | ( this.terminated ){}; |
---|
[e660761] | 561 | ( this.runner ){}; |
---|
[62502cc4] | 562 | |
---|
| 563 | disable_interrupts(); |
---|
[a5e7233] | 564 | init( this, name, _cltr, initT ); |
---|
[a3821fa] | 565 | enable_interrupts(); |
---|
[e660761] | 566 | |
---|
| 567 | __cfadbg_print_safe(runtime_core, "Kernel : Starting core %p\n", &this); |
---|
| 568 | |
---|
| 569 | this.stack = __create_pthread( &this.kernel_thread, __invoke_processor, (void *)&this ); |
---|
[a5e7233] | 570 | } |
---|
[e660761] | 571 | |
---|
[a5e7233] | 572 | void ?{}(processor & this, const char name[], cluster & _cltr) { |
---|
| 573 | (this){name, _cltr, 0p}; |
---|
[e660761] | 574 | } |
---|
| 575 | |
---|
[bfcf6b9] | 576 | extern size_t __page_size; |
---|
[e660761] | 577 | void ^?{}(processor & this) with( this ){ |
---|
[7d0ebd0] | 578 | /* paranoid */ verify( !__atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ); |
---|
| 579 | __cfadbg_print_safe(runtime_core, "Kernel : core %p signaling termination\n", &this); |
---|
[e660761] | 580 | |
---|
[7d0ebd0] | 581 | __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED); |
---|
| 582 | __disable_interrupts_checked(); |
---|
[e660761] | 583 | __wake_proc( &this ); |
---|
[7d0ebd0] | 584 | __enable_interrupts_checked(); |
---|
[e660761] | 585 | |
---|
[7d0ebd0] | 586 | wait( terminated ); |
---|
| 587 | /* paranoid */ verify( active_processor() != &this); |
---|
[e660761] | 588 | |
---|
[bfcf6b9] | 589 | __destroy_pthread( kernel_thread, this.stack, 0p ); |
---|
[e660761] | 590 | |
---|
[62502cc4] | 591 | disable_interrupts(); |
---|
| 592 | deinit( this ); |
---|
[a3821fa] | 593 | enable_interrupts(); |
---|
[e660761] | 594 | } |
---|
| 595 | |
---|
| 596 | //----------------------------------------------------------------------------- |
---|
| 597 | // Cluster |
---|
[6a9b12b] | 598 | static void ?{}(__cluster_proc_list & this) { |
---|
[7cf3b1d] | 599 | this.fdw = 0p; |
---|
[1eb239e4] | 600 | this.idle = 0; |
---|
| 601 | this.total = 0; |
---|
| 602 | } |
---|
| 603 | |
---|
[e660761] | 604 | void ?{}(cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params) with( this ) { |
---|
| 605 | this.name = name; |
---|
| 606 | this.preemption_rate = preemption_rate; |
---|
| 607 | ready_queue{}; |
---|
| 608 | |
---|
| 609 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 610 | print_stats = 0; |
---|
| 611 | stats = alloc(); |
---|
| 612 | __init_stats( stats ); |
---|
| 613 | #endif |
---|
| 614 | |
---|
| 615 | threads{ __get }; |
---|
| 616 | |
---|
[78da4ab] | 617 | io.arbiter = create(); |
---|
| 618 | io.params = io_params; |
---|
| 619 | |
---|
[e660761] | 620 | doregister(this); |
---|
| 621 | |
---|
| 622 | // Lock the RWlock so no-one pushes/pops while we are changing the queue |
---|
[772411a] | 623 | disable_interrupts(); |
---|
[e660761] | 624 | uint_fast32_t last_size = ready_mutate_lock(); |
---|
| 625 | |
---|
| 626 | // Adjust the ready queue size |
---|
[a017ee7] | 627 | ready_queue_grow( &this ); |
---|
[e660761] | 628 | |
---|
| 629 | // Unlock the RWlock |
---|
| 630 | ready_mutate_unlock( last_size ); |
---|
[a3821fa] | 631 | enable_interrupts( false ); // Don't poll, could be in main cluster |
---|
[e660761] | 632 | } |
---|
| 633 | |
---|
| 634 | void ^?{}(cluster & this) { |
---|
[78da4ab] | 635 | destroy(this.io.arbiter); |
---|
[e660761] | 636 | |
---|
| 637 | // Lock the RWlock so no-one pushes/pops while we are changing the queue |
---|
[772411a] | 638 | disable_interrupts(); |
---|
[e660761] | 639 | uint_fast32_t last_size = ready_mutate_lock(); |
---|
| 640 | |
---|
| 641 | // Adjust the ready queue size |
---|
[a017ee7] | 642 | ready_queue_shrink( &this ); |
---|
[e660761] | 643 | |
---|
| 644 | // Unlock the RWlock |
---|
| 645 | ready_mutate_unlock( last_size ); |
---|
[a3821fa] | 646 | enable_interrupts( false ); // Don't poll, could be in main cluster |
---|
[e660761] | 647 | |
---|
| 648 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 649 | if( 0 != this.print_stats ) { |
---|
[1b033b8] | 650 | __print_stats( this.stats, this.print_stats, "Cluster", this.name, (void*)&this ); |
---|
[e660761] | 651 | } |
---|
[73f4d08] | 652 | #if defined(CFA_STATS_ARRAY) |
---|
| 653 | __flush_stat( this.stats, "Cluster", &this ); |
---|
| 654 | #endif |
---|
[e660761] | 655 | free( this.stats ); |
---|
| 656 | #endif |
---|
| 657 | |
---|
| 658 | unregister(this); |
---|
| 659 | } |
---|
| 660 | |
---|
| 661 | //============================================================================================= |
---|
| 662 | // Miscellaneous Initialization |
---|
| 663 | //============================================================================================= |
---|
| 664 | //----------------------------------------------------------------------------- |
---|
| 665 | // Global Queues |
---|
| 666 | static void doregister( cluster & cltr ) { |
---|
| 667 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2); |
---|
| 668 | push_front( __cfa_dbg_global_clusters.list, cltr ); |
---|
| 669 | unlock ( __cfa_dbg_global_clusters.lock ); |
---|
| 670 | } |
---|
| 671 | |
---|
| 672 | static void unregister( cluster & cltr ) { |
---|
| 673 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2); |
---|
| 674 | remove( __cfa_dbg_global_clusters.list, cltr ); |
---|
| 675 | unlock( __cfa_dbg_global_clusters.lock ); |
---|
| 676 | } |
---|
| 677 | |
---|
[e84ab3d] | 678 | void doregister( cluster * cltr, thread$ & thrd ) { |
---|
[e660761] | 679 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2); |
---|
| 680 | cltr->nthreads += 1; |
---|
| 681 | push_front(cltr->threads, thrd); |
---|
| 682 | unlock (cltr->thread_list_lock); |
---|
| 683 | } |
---|
| 684 | |
---|
[e84ab3d] | 685 | void unregister( cluster * cltr, thread$ & thrd ) { |
---|
[e660761] | 686 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2); |
---|
| 687 | remove(cltr->threads, thrd ); |
---|
| 688 | cltr->nthreads -= 1; |
---|
| 689 | unlock(cltr->thread_list_lock); |
---|
| 690 | } |
---|
| 691 | |
---|
[c993b15] | 692 | static void register_tls( processor * this ) { |
---|
| 693 | // Register and Lock the RWlock so no-one pushes/pops while we are changing the queue |
---|
| 694 | uint_fast32_t last_size; |
---|
| 695 | [this->unique_id, last_size] = ready_mutate_register(); |
---|
| 696 | |
---|
[145dcd5] | 697 | this->rdq.cpu = __kernel_getcpu(); |
---|
| 698 | |
---|
[c993b15] | 699 | this->cltr->procs.total += 1u; |
---|
| 700 | insert_last(this->cltr->procs.actives, *this); |
---|
| 701 | |
---|
| 702 | // Adjust the ready queue size |
---|
| 703 | ready_queue_grow( this->cltr ); |
---|
| 704 | |
---|
| 705 | // Unlock the RWlock |
---|
| 706 | ready_mutate_unlock( last_size ); |
---|
| 707 | } |
---|
| 708 | |
---|
| 709 | |
---|
| 710 | static void unregister_tls( processor * this ) { |
---|
| 711 | // Lock the RWlock so no-one pushes/pops while we are changing the queue |
---|
| 712 | uint_fast32_t last_size = ready_mutate_lock(); |
---|
| 713 | this->cltr->procs.total -= 1u; |
---|
| 714 | remove(*this); |
---|
| 715 | |
---|
| 716 | // clear the cluster so nothing gets pushed to local queues |
---|
| 717 | cluster * cltr = this->cltr; |
---|
| 718 | this->cltr = 0p; |
---|
| 719 | |
---|
| 720 | // Adjust the ready queue size |
---|
| 721 | ready_queue_shrink( cltr ); |
---|
| 722 | |
---|
| 723 | // Unlock the RWlock and unregister: we don't need the read_lock any more |
---|
| 724 | ready_mutate_unregister( this->unique_id, last_size ); |
---|
| 725 | } |
---|
| 726 | |
---|
[e660761] | 727 | static void check( int ret, const char func[] ) { |
---|
| 728 | if ( ret ) { // pthread routines return errno values |
---|
| 729 | abort( "%s : internal error, error(%d) %s.", func, ret, strerror( ret ) ); |
---|
| 730 | } // if |
---|
| 731 | } // Abort |
---|
| 732 | |
---|
| 733 | void * __create_pthread( pthread_t * pthread, void * (*start)(void *), void * arg ) { |
---|
| 734 | pthread_attr_t attr; |
---|
| 735 | |
---|
| 736 | check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute |
---|
| 737 | |
---|
[eaf269d] | 738 | size_t stacksize = DEFAULT_STACK_SIZE; |
---|
[e660761] | 739 | |
---|
| 740 | void * stack; |
---|
[97229d6] | 741 | #if CFA_PROCESSOR_USE_MMAP |
---|
| 742 | stacksize = ceiling( stacksize, __page_size ) + __page_size; |
---|
[dd92fe9] | 743 | stack = mmap(0p, stacksize, __map_prot, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); |
---|
[97229d6] | 744 | if(stack == ((void*)-1)) { |
---|
| 745 | abort( "pthread stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) ); |
---|
| 746 | } |
---|
| 747 | if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) { |
---|
| 748 | abort( "pthread stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) ); |
---|
| 749 | } // if |
---|
| 750 | #else |
---|
| 751 | __cfaabi_dbg_debug_do( |
---|
| 752 | stack = memalign( __page_size, stacksize + __page_size ); |
---|
| 753 | // pthread has no mechanism to create the guard page in user supplied stack. |
---|
| 754 | if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) { |
---|
| 755 | abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) ); |
---|
| 756 | } // if |
---|
| 757 | ); |
---|
| 758 | __cfaabi_dbg_no_debug_do( |
---|
| 759 | stack = malloc( stacksize ); |
---|
| 760 | ); |
---|
| 761 | #endif |
---|
| 762 | |
---|
[e660761] | 763 | check( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" ); |
---|
| 764 | check( pthread_create( pthread, &attr, start, arg ), "pthread_create" ); |
---|
| 765 | return stack; |
---|
[88cafe7] | 766 | } |
---|
[f2384c9a] | 767 | |
---|
[bfcf6b9] | 768 | void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ) { |
---|
| 769 | int err = pthread_join( pthread, retval ); |
---|
| 770 | if( err != 0 ) abort("KERNEL ERROR: joining pthread %p caused error %s\n", (void*)pthread, strerror(err)); |
---|
| 771 | |
---|
[97229d6] | 772 | #if CFA_PROCESSOR_USE_MMAP |
---|
| 773 | pthread_attr_t attr; |
---|
[bfcf6b9] | 774 | |
---|
[97229d6] | 775 | check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute |
---|
[bfcf6b9] | 776 | |
---|
[97229d6] | 777 | size_t stacksize; |
---|
| 778 | // default stack size, normally defined by shell limit |
---|
| 779 | check( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" ); |
---|
| 780 | assert( stacksize >= PTHREAD_STACK_MIN ); |
---|
| 781 | stacksize += __page_size; |
---|
[bfcf6b9] | 782 | |
---|
[97229d6] | 783 | if(munmap(stack, stacksize) == -1) { |
---|
| 784 | abort( "pthread stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) ); |
---|
| 785 | } |
---|
| 786 | #else |
---|
[72a3aff] | 787 | __cfaabi_dbg_debug_do( |
---|
| 788 | // pthread has no mechanism to create the guard page in user supplied stack. |
---|
[28c35e2] | 789 | if ( mprotect( stack, __page_size, __map_prot ) == -1 ) { |
---|
[72a3aff] | 790 | abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) ); |
---|
| 791 | } // if |
---|
| 792 | ); |
---|
[97229d6] | 793 | free( stack ); |
---|
| 794 | #endif |
---|
[bfcf6b9] | 795 | } |
---|
| 796 | |
---|
[f2384c9a] | 797 | #if defined(__CFA_WITH_VERIFY__) |
---|
| 798 | static bool verify_fwd_bck_rng(void) { |
---|
[8fc652e0] | 799 | __cfaabi_tls.ready_rng.fwd_seed = 25214903917_l64u * (rdtscl() ^ (uintptr_t)&verify_fwd_bck_rng); |
---|
[f2384c9a] | 800 | |
---|
| 801 | unsigned values[10]; |
---|
| 802 | for(i; 10) { |
---|
| 803 | values[i] = __tls_rand_fwd(); |
---|
| 804 | } |
---|
| 805 | |
---|
| 806 | __tls_rand_advance_bck(); |
---|
| 807 | |
---|
| 808 | for ( i; 9 -~= 0 ) { |
---|
| 809 | if(values[i] != __tls_rand_bck()) { |
---|
| 810 | return false; |
---|
| 811 | } |
---|
| 812 | } |
---|
| 813 | |
---|
| 814 | return true; |
---|
| 815 | } |
---|
[eaf269d] | 816 | #endif |
---|