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