[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 | doregister(curr_cluster, this);
|
---|
| 538 |
|
---|
| 539 | monitors{ &self_mon_p, 1, (fptr_t)0 };
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | //-----------------------------------------------------------------------------
|
---|
| 543 | // Processor
|
---|
| 544 | // Construct the processor context of non-main processors
|
---|
| 545 | static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) {
|
---|
[c18bf9e] | 546 | (this.self){ info };
|
---|
[e660761] | 547 | this.proc = proc;
|
---|
| 548 | }
|
---|
| 549 |
|
---|
[e84ab3d] | 550 | static void init(processor & this, const char name[], cluster & _cltr, thread$ * initT) with( this ) {
|
---|
[e660761] | 551 | this.name = name;
|
---|
| 552 | this.cltr = &_cltr;
|
---|
[431cd4f] | 553 | this.rdq.its = 0;
|
---|
| 554 | this.rdq.itr = 0;
|
---|
[884f3f67] | 555 | this.rdq.id = 0;
|
---|
[145dcd5] | 556 | this.rdq.target = MAX;
|
---|
| 557 | this.rdq.last = MAX;
|
---|
| 558 | this.rdq.cpu = 0;
|
---|
| 559 | // this.rdq.cutoff = 0ull;
|
---|
[e660761] | 560 | do_terminate = false;
|
---|
| 561 | preemption_alarm = 0p;
|
---|
| 562 | pending_preemption = false;
|
---|
| 563 |
|
---|
[78da4ab] | 564 | this.io.ctx = 0p;
|
---|
[dddb3dd0] | 565 | this.io.pending = false;
|
---|
| 566 | this.io.dirty = false;
|
---|
| 567 |
|
---|
[a5e7233] | 568 | this.init.thrd = initT;
|
---|
[a1538cd] | 569 |
|
---|
[a67c5b6] | 570 | this.local_data = 0p;
|
---|
| 571 |
|
---|
[22226e4] | 572 | idle_wctx.evfd = eventfd(0, 0);
|
---|
| 573 | if (idle_wctx.evfd < 0) {
|
---|
[dddb3dd0] | 574 | abort("KERNEL ERROR: PROCESSOR EVENTFD - %s\n", strerror(errno));
|
---|
| 575 | }
|
---|
[78da4ab] | 576 |
|
---|
[22226e4] | 577 | idle_wctx.sem = 0;
|
---|
[efa28d5] | 578 | idle_wctx.wake__time = 0;
|
---|
[7cf3b1d] | 579 |
|
---|
| 580 | // I'm assuming these two are reserved for standard input and output
|
---|
| 581 | // so I'm using them as sentinels with idle_wctx.
|
---|
[22226e4] | 582 | /* paranoid */ verify( idle_wctx.evfd != 0 );
|
---|
| 583 | /* paranoid */ verify( idle_wctx.evfd != 1 );
|
---|
[7cf3b1d] | 584 |
|
---|
[e660761] | 585 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 586 | print_stats = 0;
|
---|
| 587 | print_halts = false;
|
---|
| 588 | #endif
|
---|
| 589 |
|
---|
| 590 | __cfadbg_print_safe(runtime_core, "Kernel : core %p created\n", &this);
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | // Not a ctor, it just preps the destruction but should not destroy members
|
---|
| 594 | static void deinit(processor & this) {
|
---|
[22226e4] | 595 | close(this.idle_wctx.evfd);
|
---|
[e660761] | 596 | }
|
---|
| 597 |
|
---|
[c18bf9e] | 598 | void ?{}(processor & this, const char name[], cluster & _cltr, thread$ * initT) libcfa_public {
|
---|
[454f478] | 599 | ( this.terminated ){};
|
---|
[e660761] | 600 | ( this.runner ){};
|
---|
[62502cc4] | 601 |
|
---|
| 602 | disable_interrupts();
|
---|
[a5e7233] | 603 | init( this, name, _cltr, initT );
|
---|
[a3821fa] | 604 | enable_interrupts();
|
---|
[e660761] | 605 |
|
---|
| 606 | __cfadbg_print_safe(runtime_core, "Kernel : Starting core %p\n", &this);
|
---|
| 607 |
|
---|
| 608 | this.stack = __create_pthread( &this.kernel_thread, __invoke_processor, (void *)&this );
|
---|
[a5e7233] | 609 | }
|
---|
[e660761] | 610 |
|
---|
[c18bf9e] | 611 | void ?{}(processor & this, const char name[], cluster & _cltr) libcfa_public {
|
---|
[a5e7233] | 612 | (this){name, _cltr, 0p};
|
---|
[e660761] | 613 | }
|
---|
| 614 |
|
---|
[a182ad5] | 615 | extern size_t __page_size;
|
---|
[c18bf9e] | 616 | void ^?{}(processor & this) libcfa_public with( this ) {
|
---|
[7d0ebd0] | 617 | /* paranoid */ verify( !__atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) );
|
---|
| 618 | __cfadbg_print_safe(runtime_core, "Kernel : core %p signaling termination\n", &this);
|
---|
[e660761] | 619 |
|
---|
[7d0ebd0] | 620 | __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED);
|
---|
| 621 | __disable_interrupts_checked();
|
---|
[e660761] | 622 | __wake_proc( &this );
|
---|
[7d0ebd0] | 623 | __enable_interrupts_checked();
|
---|
[e660761] | 624 |
|
---|
[7d0ebd0] | 625 | wait( terminated );
|
---|
| 626 | /* paranoid */ verify( active_processor() != &this);
|
---|
[e660761] | 627 |
|
---|
[bfcf6b9] | 628 | __destroy_pthread( kernel_thread, this.stack, 0p );
|
---|
[e660761] | 629 |
|
---|
[62502cc4] | 630 | disable_interrupts();
|
---|
| 631 | deinit( this );
|
---|
[a3821fa] | 632 | enable_interrupts();
|
---|
[e660761] | 633 | }
|
---|
| 634 |
|
---|
| 635 | //-----------------------------------------------------------------------------
|
---|
| 636 | // Cluster
|
---|
[6a9b12b] | 637 | static void ?{}(__cluster_proc_list & this) {
|
---|
[7cf3b1d] | 638 | this.fdw = 0p;
|
---|
[1eb239e4] | 639 | this.idle = 0;
|
---|
| 640 | this.total = 0;
|
---|
| 641 | }
|
---|
| 642 |
|
---|
[c18bf9e] | 643 | void ?{}(cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params) libcfa_public with( this ) {
|
---|
[e660761] | 644 | this.name = name;
|
---|
| 645 | this.preemption_rate = preemption_rate;
|
---|
[884f3f67] | 646 | this.sched.readyQ.data = 0p;
|
---|
| 647 | this.sched.readyQ.tscs = 0p;
|
---|
| 648 | this.sched.readyQ.count = 0;
|
---|
| 649 | this.sched.io.tscs = 0p;
|
---|
[0c3aa67] | 650 | this.sched.io.data = 0p;
|
---|
[884f3f67] | 651 | this.sched.caches = 0p;
|
---|
[e660761] | 652 |
|
---|
| 653 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 654 | print_stats = 0;
|
---|
| 655 | stats = alloc();
|
---|
| 656 | __init_stats( stats );
|
---|
| 657 | #endif
|
---|
| 658 |
|
---|
[cd5b58f] | 659 | threads{};
|
---|
[e660761] | 660 |
|
---|
[78da4ab] | 661 | io.arbiter = create();
|
---|
| 662 | io.params = io_params;
|
---|
| 663 |
|
---|
[8b74fa7] | 664 | managed.procs = 0p;
|
---|
| 665 | managed.cnt = 0;
|
---|
| 666 |
|
---|
[e660761] | 667 | doregister(this);
|
---|
| 668 |
|
---|
| 669 | // Lock the RWlock so no-one pushes/pops while we are changing the queue
|
---|
[772411a] | 670 | disable_interrupts();
|
---|
[e660761] | 671 | uint_fast32_t last_size = ready_mutate_lock();
|
---|
| 672 |
|
---|
| 673 | // Adjust the ready queue size
|
---|
[a017ee7] | 674 | ready_queue_grow( &this );
|
---|
[e660761] | 675 |
|
---|
| 676 | // Unlock the RWlock
|
---|
| 677 | ready_mutate_unlock( last_size );
|
---|
[a3821fa] | 678 | enable_interrupts( false ); // Don't poll, could be in main cluster
|
---|
[e660761] | 679 | }
|
---|
| 680 |
|
---|
[c18bf9e] | 681 | void ^?{}(cluster & this) libcfa_public {
|
---|
[8b74fa7] | 682 | set_concurrency( this, 0 );
|
---|
| 683 |
|
---|
[78da4ab] | 684 | destroy(this.io.arbiter);
|
---|
[e660761] | 685 |
|
---|
| 686 | // Lock the RWlock so no-one pushes/pops while we are changing the queue
|
---|
[772411a] | 687 | disable_interrupts();
|
---|
[e660761] | 688 | uint_fast32_t last_size = ready_mutate_lock();
|
---|
| 689 |
|
---|
| 690 | // Adjust the ready queue size
|
---|
[a017ee7] | 691 | ready_queue_shrink( &this );
|
---|
[e660761] | 692 |
|
---|
| 693 | // Unlock the RWlock
|
---|
| 694 | ready_mutate_unlock( last_size );
|
---|
[884f3f67] | 695 |
|
---|
| 696 | ready_queue_close( &this );
|
---|
| 697 | /* paranoid */ verify( this.sched.readyQ.data == 0p );
|
---|
| 698 | /* paranoid */ verify( this.sched.readyQ.tscs == 0p );
|
---|
| 699 | /* paranoid */ verify( this.sched.readyQ.count == 0 );
|
---|
| 700 | /* paranoid */ verify( this.sched.io.tscs == 0p );
|
---|
| 701 | /* paranoid */ verify( this.sched.caches == 0p );
|
---|
| 702 |
|
---|
[a3821fa] | 703 | enable_interrupts( false ); // Don't poll, could be in main cluster
|
---|
[e660761] | 704 |
|
---|
[884f3f67] | 705 |
|
---|
[e660761] | 706 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 707 | if( 0 != this.print_stats ) {
|
---|
[1b033b8] | 708 | __print_stats( this.stats, this.print_stats, "Cluster", this.name, (void*)&this );
|
---|
[e660761] | 709 | }
|
---|
[73f4d08] | 710 | #if defined(CFA_STATS_ARRAY)
|
---|
| 711 | __flush_stat( this.stats, "Cluster", &this );
|
---|
| 712 | #endif
|
---|
[e660761] | 713 | free( this.stats );
|
---|
| 714 | #endif
|
---|
| 715 |
|
---|
| 716 | unregister(this);
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 | //=============================================================================================
|
---|
| 720 | // Miscellaneous Initialization
|
---|
| 721 | //=============================================================================================
|
---|
| 722 | //-----------------------------------------------------------------------------
|
---|
| 723 | // Global Queues
|
---|
| 724 | static void doregister( cluster & cltr ) {
|
---|
| 725 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
|
---|
| 726 | push_front( __cfa_dbg_global_clusters.list, cltr );
|
---|
| 727 | unlock ( __cfa_dbg_global_clusters.lock );
|
---|
| 728 | }
|
---|
| 729 |
|
---|
| 730 | static void unregister( cluster & cltr ) {
|
---|
| 731 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
|
---|
| 732 | remove( __cfa_dbg_global_clusters.list, cltr );
|
---|
| 733 | unlock( __cfa_dbg_global_clusters.lock );
|
---|
| 734 | }
|
---|
| 735 |
|
---|
[e84ab3d] | 736 | void doregister( cluster * cltr, thread$ & thrd ) {
|
---|
[e660761] | 737 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2);
|
---|
| 738 | cltr->nthreads += 1;
|
---|
[cd5b58f] | 739 | insert_first(cltr->threads, thrd);
|
---|
[e660761] | 740 | unlock (cltr->thread_list_lock);
|
---|
| 741 | }
|
---|
| 742 |
|
---|
[e84ab3d] | 743 | void unregister( cluster * cltr, thread$ & thrd ) {
|
---|
[e660761] | 744 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2);
|
---|
[cd5b58f] | 745 | {
|
---|
| 746 | tytagref( dlink(thread$), dlink(thread$) ) ?`inner( thread$ & this ) = void;
|
---|
| 747 | with( DLINK_VIA( thread$, thread$.cltr_link ) )
|
---|
| 748 | remove( thrd );
|
---|
| 749 | cltr->nthreads -= 1;
|
---|
| 750 | }
|
---|
[e660761] | 751 | unlock(cltr->thread_list_lock);
|
---|
| 752 | }
|
---|
| 753 |
|
---|
[c993b15] | 754 | static void register_tls( processor * this ) {
|
---|
| 755 | // Register and Lock the RWlock so no-one pushes/pops while we are changing the queue
|
---|
| 756 | uint_fast32_t last_size;
|
---|
| 757 | [this->unique_id, last_size] = ready_mutate_register();
|
---|
| 758 |
|
---|
[145dcd5] | 759 | this->rdq.cpu = __kernel_getcpu();
|
---|
| 760 |
|
---|
[c993b15] | 761 | this->cltr->procs.total += 1u;
|
---|
| 762 | insert_last(this->cltr->procs.actives, *this);
|
---|
| 763 |
|
---|
| 764 | // Adjust the ready queue size
|
---|
| 765 | ready_queue_grow( this->cltr );
|
---|
| 766 |
|
---|
| 767 | // Unlock the RWlock
|
---|
| 768 | ready_mutate_unlock( last_size );
|
---|
| 769 | }
|
---|
| 770 |
|
---|
| 771 |
|
---|
| 772 | static void unregister_tls( processor * this ) {
|
---|
| 773 | // Lock the RWlock so no-one pushes/pops while we are changing the queue
|
---|
| 774 | uint_fast32_t last_size = ready_mutate_lock();
|
---|
| 775 | this->cltr->procs.total -= 1u;
|
---|
| 776 | remove(*this);
|
---|
| 777 |
|
---|
| 778 | // clear the cluster so nothing gets pushed to local queues
|
---|
| 779 | cluster * cltr = this->cltr;
|
---|
| 780 | this->cltr = 0p;
|
---|
| 781 |
|
---|
| 782 | // Adjust the ready queue size
|
---|
| 783 | ready_queue_shrink( cltr );
|
---|
| 784 |
|
---|
| 785 | // Unlock the RWlock and unregister: we don't need the read_lock any more
|
---|
| 786 | ready_mutate_unregister( this->unique_id, last_size );
|
---|
| 787 | }
|
---|
| 788 |
|
---|
[e660761] | 789 | static void check( int ret, const char func[] ) {
|
---|
| 790 | if ( ret ) { // pthread routines return errno values
|
---|
| 791 | abort( "%s : internal error, error(%d) %s.", func, ret, strerror( ret ) );
|
---|
| 792 | } // if
|
---|
| 793 | } // Abort
|
---|
| 794 |
|
---|
| 795 | void * __create_pthread( pthread_t * pthread, void * (*start)(void *), void * arg ) {
|
---|
| 796 | pthread_attr_t attr;
|
---|
| 797 |
|
---|
[95dab9e] | 798 | check( __cfaabi_pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
|
---|
[e660761] | 799 |
|
---|
[09ae8a6] | 800 | size_t stacksize = max( PTHREAD_STACK_MIN, DEFAULT_STACK_SIZE );
|
---|
[e660761] | 801 |
|
---|
| 802 | void * stack;
|
---|
[97229d6] | 803 | #if CFA_PROCESSOR_USE_MMAP
|
---|
[a182ad5] | 804 | stacksize = ceiling( stacksize, __page_size ) + __page_size;
|
---|
[dd92fe9] | 805 | stack = mmap(0p, stacksize, __map_prot, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
|
---|
[97229d6] | 806 | if(stack == ((void*)-1)) {
|
---|
| 807 | abort( "pthread stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) );
|
---|
| 808 | }
|
---|
[a182ad5] | 809 | if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
|
---|
[97229d6] | 810 | abort( "pthread stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
|
---|
| 811 | } // if
|
---|
| 812 | #else
|
---|
| 813 | __cfaabi_dbg_debug_do(
|
---|
[a182ad5] | 814 | stack = memalign( __page_size, stacksize + __page_size );
|
---|
[97229d6] | 815 | // pthread has no mechanism to create the guard page in user supplied stack.
|
---|
[a182ad5] | 816 | if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
|
---|
[97229d6] | 817 | abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
|
---|
| 818 | } // if
|
---|
| 819 | );
|
---|
| 820 | __cfaabi_dbg_no_debug_do(
|
---|
| 821 | stack = malloc( stacksize );
|
---|
| 822 | );
|
---|
| 823 | #endif
|
---|
| 824 |
|
---|
[95dab9e] | 825 | check( __cfaabi_pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
|
---|
| 826 | check( __cfaabi_pthread_create( pthread, &attr, start, arg ), "pthread_create" );
|
---|
[e660761] | 827 | return stack;
|
---|
[88cafe7] | 828 | }
|
---|
[f2384c9a] | 829 |
|
---|
[bfcf6b9] | 830 | void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ) {
|
---|
[95dab9e] | 831 | int err = __cfaabi_pthread_join( pthread, retval );
|
---|
[bfcf6b9] | 832 | if( err != 0 ) abort("KERNEL ERROR: joining pthread %p caused error %s\n", (void*)pthread, strerror(err));
|
---|
| 833 |
|
---|
[97229d6] | 834 | #if CFA_PROCESSOR_USE_MMAP
|
---|
| 835 | pthread_attr_t attr;
|
---|
[bfcf6b9] | 836 |
|
---|
[95dab9e] | 837 | check( __cfaabi_pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
|
---|
[bfcf6b9] | 838 |
|
---|
[97229d6] | 839 | size_t stacksize;
|
---|
| 840 | // default stack size, normally defined by shell limit
|
---|
[95dab9e] | 841 | check( __cfaabi_pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
|
---|
[97229d6] | 842 | assert( stacksize >= PTHREAD_STACK_MIN );
|
---|
[a182ad5] | 843 | stacksize += __page_size;
|
---|
[bfcf6b9] | 844 |
|
---|
[97229d6] | 845 | if(munmap(stack, stacksize) == -1) {
|
---|
| 846 | abort( "pthread stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) );
|
---|
| 847 | }
|
---|
| 848 | #else
|
---|
[72a3aff] | 849 | __cfaabi_dbg_debug_do(
|
---|
| 850 | // pthread has no mechanism to create the guard page in user supplied stack.
|
---|
[a182ad5] | 851 | if ( mprotect( stack, __page_size, __map_prot ) == -1 ) {
|
---|
[72a3aff] | 852 | abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
|
---|
| 853 | } // if
|
---|
| 854 | );
|
---|
[97229d6] | 855 | free( stack );
|
---|
| 856 | #endif
|
---|
[bfcf6b9] | 857 | }
|
---|
| 858 |
|
---|
[8b74fa7] | 859 | unsigned set_concurrency( cluster & this, unsigned new ) libcfa_public {
|
---|
| 860 | unsigned old = this.managed.cnt;
|
---|
| 861 |
|
---|
| 862 | __cfadbg_print_safe(runtime_core, "Kernel : resizing cluster from %u to %u\n", old, (unsigned)new);
|
---|
| 863 |
|
---|
| 864 | // Delete all the old unneeded procs
|
---|
| 865 | if(old > new) for(i; (unsigned)new ~ old) {
|
---|
| 866 | __cfadbg_print_safe(runtime_core, "Kernel : destroying %u\n", i);
|
---|
| 867 | delete( this.managed.procs[i] );
|
---|
| 868 | }
|
---|
| 869 |
|
---|
| 870 | // Allocate new array (uses realloc and memcpies the data)
|
---|
| 871 | this.managed.procs = alloc( new, this.managed.procs`realloc );
|
---|
| 872 | this.managed.cnt = new;
|
---|
| 873 |
|
---|
| 874 | // Create the desired new procs
|
---|
| 875 | if(old < new) for(i; old ~ new) {
|
---|
| 876 | __cfadbg_print_safe(runtime_core, "Kernel : constructing %u\n", i);
|
---|
| 877 | (*(this.managed.procs[i] = alloc())){ this };
|
---|
| 878 | }
|
---|
| 879 |
|
---|
| 880 | // return the old count
|
---|
| 881 | return old;
|
---|
| 882 | }
|
---|
| 883 |
|
---|
[f2384c9a] | 884 | #if defined(__CFA_WITH_VERIFY__)
|
---|
| 885 | static bool verify_fwd_bck_rng(void) {
|
---|
[8fc652e0] | 886 | __cfaabi_tls.ready_rng.fwd_seed = 25214903917_l64u * (rdtscl() ^ (uintptr_t)&verify_fwd_bck_rng);
|
---|
[f2384c9a] | 887 |
|
---|
| 888 | unsigned values[10];
|
---|
| 889 | for(i; 10) {
|
---|
| 890 | values[i] = __tls_rand_fwd();
|
---|
| 891 | }
|
---|
| 892 |
|
---|
| 893 | __tls_rand_advance_bck();
|
---|
| 894 |
|
---|
| 895 | for ( i; 9 -~= 0 ) {
|
---|
| 896 | if(values[i] != __tls_rand_bck()) {
|
---|
| 897 | return false;
|
---|
| 898 | }
|
---|
| 899 | }
|
---|
| 900 |
|
---|
| 901 | return true;
|
---|
| 902 | }
|
---|
[eaf269d] | 903 | #endif
|
---|