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