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