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