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