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