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 | #include "math.hfa"
|
---|
32 |
|
---|
33 | //-----------------------------------------------------------------------------
|
---|
34 | // Some assembly required
|
---|
35 | #if defined( __i386 )
|
---|
36 | #define CtxGet( ctx ) __asm__ volatile ( \
|
---|
37 | "movl %%esp,%0\n" \
|
---|
38 | "movl %%ebp,%1\n" \
|
---|
39 | : "=rm" (ctx.SP), \
|
---|
40 | "=rm" (ctx.FP) \
|
---|
41 | )
|
---|
42 | #elif defined( __x86_64 )
|
---|
43 | #define CtxGet( ctx ) __asm__ volatile ( \
|
---|
44 | "movq %%rsp,%0\n" \
|
---|
45 | "movq %%rbp,%1\n" \
|
---|
46 | : "=rm" (ctx.SP), \
|
---|
47 | "=rm" (ctx.FP) \
|
---|
48 | )
|
---|
49 | #elif defined( __aarch64__ )
|
---|
50 | #define CtxGet( ctx ) __asm__ volatile ( \
|
---|
51 | "mov %0, sp\n" \
|
---|
52 | "mov %1, fp\n" \
|
---|
53 | : "=rm" (ctx.SP), \
|
---|
54 | "=rm" (ctx.FP) \
|
---|
55 | )
|
---|
56 | #else
|
---|
57 | #error unknown hardware architecture
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | //-----------------------------------------------------------------------------
|
---|
61 | // Start and stop routine for the kernel, declared first to make sure they run first
|
---|
62 | static void __kernel_startup (void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
|
---|
63 | static void __kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
|
---|
64 |
|
---|
65 | //-----------------------------------------------------------------------------
|
---|
66 | // Static Forward Declarations
|
---|
67 | struct current_stack_info_t;
|
---|
68 |
|
---|
69 | static void * __invoke_processor(void * arg);
|
---|
70 | static void __kernel_first_resume( processor * this );
|
---|
71 | static void __kernel_last_resume ( processor * this );
|
---|
72 | static void init(processor & this, const char name[], cluster & _cltr);
|
---|
73 | static void deinit(processor & this);
|
---|
74 | static void doregister( struct cluster & cltr );
|
---|
75 | static void unregister( struct cluster & cltr );
|
---|
76 | static void ?{}( $coroutine & this, current_stack_info_t * info);
|
---|
77 | static void ?{}( $thread & this, current_stack_info_t * info);
|
---|
78 | static void ?{}(processorCtx_t & this) {}
|
---|
79 | static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info);
|
---|
80 |
|
---|
81 | #if defined(__CFA_WITH_VERIFY__)
|
---|
82 | static bool verify_fwd_bck_rng(void);
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | //-----------------------------------------------------------------------------
|
---|
86 | // Forward Declarations for other modules
|
---|
87 | extern void __kernel_alarm_startup(void);
|
---|
88 | extern void __kernel_alarm_shutdown(void);
|
---|
89 | extern void __kernel_io_startup (void);
|
---|
90 | extern void __kernel_io_shutdown(void);
|
---|
91 |
|
---|
92 | //-----------------------------------------------------------------------------
|
---|
93 | // Other Forward Declarations
|
---|
94 | extern void __wake_proc(processor *);
|
---|
95 |
|
---|
96 | //-----------------------------------------------------------------------------
|
---|
97 | // Kernel storage
|
---|
98 | KERNEL_STORAGE(cluster, mainCluster);
|
---|
99 | KERNEL_STORAGE(processor, mainProcessor);
|
---|
100 | KERNEL_STORAGE($thread, mainThread);
|
---|
101 | KERNEL_STORAGE(__stack_t, mainThreadCtx);
|
---|
102 | KERNEL_STORAGE(io_context, mainPollerThread);
|
---|
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 |
|
---|
117 | size_t __page_size = 0;
|
---|
118 |
|
---|
119 | //-----------------------------------------------------------------------------
|
---|
120 | // Global state
|
---|
121 | thread_local struct KernelThreadData __cfaabi_tls __attribute__ ((tls_model ( "initial-exec" ))) @= {
|
---|
122 | NULL, // cannot use 0p
|
---|
123 | NULL,
|
---|
124 | NULL,
|
---|
125 | NULL,
|
---|
126 | { 1, false, false },
|
---|
127 | };
|
---|
128 |
|
---|
129 | //-----------------------------------------------------------------------------
|
---|
130 | // Struct to steal stack
|
---|
131 | struct current_stack_info_t {
|
---|
132 | __stack_t * storage; // pointer to stack object
|
---|
133 | void * base; // base of stack
|
---|
134 | void * limit; // stack grows towards stack limit
|
---|
135 | void * context; // address of cfa_context_t
|
---|
136 | };
|
---|
137 |
|
---|
138 | void ?{}( current_stack_info_t & this ) {
|
---|
139 | __stack_context_t ctx;
|
---|
140 | CtxGet( ctx );
|
---|
141 | this.base = ctx.FP;
|
---|
142 |
|
---|
143 | rlimit r;
|
---|
144 | getrlimit( RLIMIT_STACK, &r);
|
---|
145 | size_t size = r.rlim_cur;
|
---|
146 |
|
---|
147 | this.limit = (void *)(((intptr_t)this.base) - size);
|
---|
148 | this.context = &storage_mainThreadCtx;
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 |
|
---|
153 | //=============================================================================================
|
---|
154 | // Kernel Setup logic
|
---|
155 | //=============================================================================================
|
---|
156 | //-----------------------------------------------------------------------------
|
---|
157 | // Kernel boot procedures
|
---|
158 | static void __kernel_startup(void) {
|
---|
159 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
160 | __cfadbg_print_safe(runtime_core, "Kernel : Starting\n");
|
---|
161 |
|
---|
162 | __page_size = sysconf( _SC_PAGESIZE );
|
---|
163 |
|
---|
164 | __cfa_dbg_global_clusters.list{ __get };
|
---|
165 | __cfa_dbg_global_clusters.lock{};
|
---|
166 |
|
---|
167 | /* paranoid */ verify( verify_fwd_bck_rng() );
|
---|
168 |
|
---|
169 | // Initialize the global scheduler lock
|
---|
170 | __scheduler_lock = (__scheduler_RWLock_t*)&storage___scheduler_lock;
|
---|
171 | (*__scheduler_lock){};
|
---|
172 |
|
---|
173 | // Initialize the main cluster
|
---|
174 | mainCluster = (cluster *)&storage_mainCluster;
|
---|
175 | (*mainCluster){"Main Cluster", 0};
|
---|
176 |
|
---|
177 | __cfadbg_print_safe(runtime_core, "Kernel : Main cluster ready\n");
|
---|
178 |
|
---|
179 | // Start by initializing the main thread
|
---|
180 | // SKULLDUGGERY: the mainThread steals the process main thread
|
---|
181 | // which will then be scheduled by the mainProcessor normally
|
---|
182 | mainThread = ($thread *)&storage_mainThread;
|
---|
183 | current_stack_info_t info;
|
---|
184 | info.storage = (__stack_t*)&storage_mainThreadCtx;
|
---|
185 | (*mainThread){ &info };
|
---|
186 |
|
---|
187 | __cfadbg_print_safe(runtime_core, "Kernel : Main thread ready\n");
|
---|
188 |
|
---|
189 |
|
---|
190 |
|
---|
191 | // Construct the processor context of the main processor
|
---|
192 | void ?{}(processorCtx_t & this, processor * proc) {
|
---|
193 | (this.__cor){ "Processor" };
|
---|
194 | this.__cor.starter = 0p;
|
---|
195 | this.proc = proc;
|
---|
196 | }
|
---|
197 |
|
---|
198 | void ?{}(processor & this) with( this ) {
|
---|
199 | ( this.idle ){};
|
---|
200 | ( this.terminated ){ 0 };
|
---|
201 | ( this.runner ){};
|
---|
202 | init( this, "Main Processor", *mainCluster );
|
---|
203 | kernel_thread = pthread_self();
|
---|
204 |
|
---|
205 | runner{ &this };
|
---|
206 | __cfadbg_print_safe(runtime_core, "Kernel : constructed main processor context %p\n", &runner);
|
---|
207 | }
|
---|
208 |
|
---|
209 | // Initialize the main processor and the main processor ctx
|
---|
210 | // (the coroutine that contains the processing control flow)
|
---|
211 | mainProcessor = (processor *)&storage_mainProcessor;
|
---|
212 | (*mainProcessor){};
|
---|
213 |
|
---|
214 | //initialize the global state variables
|
---|
215 | __cfaabi_tls.this_processor = mainProcessor;
|
---|
216 | __cfaabi_tls.this_proc_id = (__processor_id_t*)mainProcessor;
|
---|
217 | __cfaabi_tls.this_thread = mainThread;
|
---|
218 |
|
---|
219 | #if !defined( __CFA_NO_STATISTICS__ )
|
---|
220 | __cfaabi_tls.this_stats = (__stats_t *)& storage_mainProcStats;
|
---|
221 | __init_stats( __cfaabi_tls.this_stats );
|
---|
222 | #endif
|
---|
223 |
|
---|
224 | // Enable preemption
|
---|
225 | __kernel_alarm_startup();
|
---|
226 |
|
---|
227 | // Start IO
|
---|
228 | __kernel_io_startup();
|
---|
229 |
|
---|
230 | // Add the main thread to the ready queue
|
---|
231 | // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
|
---|
232 | __schedule_thread(mainThread);
|
---|
233 |
|
---|
234 | // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
|
---|
235 | // context. Hence, the main thread does not begin through __cfactx_invoke_thread, like all other threads. The trick here is that
|
---|
236 | // mainThread is on the ready queue when this call is made.
|
---|
237 | __kernel_first_resume( __cfaabi_tls.this_processor );
|
---|
238 |
|
---|
239 |
|
---|
240 | // THE SYSTEM IS NOW COMPLETELY RUNNING
|
---|
241 |
|
---|
242 |
|
---|
243 | // SKULLDUGGERY: The constructor for the mainCluster will call alloc with a dimension of 0
|
---|
244 | // malloc *can* return a non-null value, we should free it if that is the case
|
---|
245 | free( mainCluster->io.ctxs );
|
---|
246 |
|
---|
247 | // Now that the system is up, finish creating systems that need threading
|
---|
248 | mainCluster->io.ctxs = (io_context *)&storage_mainPollerThread;
|
---|
249 | mainCluster->io.cnt = 1;
|
---|
250 | (*mainCluster->io.ctxs){ *mainCluster };
|
---|
251 |
|
---|
252 | __cfadbg_print_safe(runtime_core, "Kernel : Started\n--------------------------------------------------\n\n");
|
---|
253 |
|
---|
254 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
255 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
256 | /* paranoid */ verify( __preemption_enabled() );
|
---|
257 |
|
---|
258 | }
|
---|
259 |
|
---|
260 | static void __kernel_shutdown(void) {
|
---|
261 | //Before we start shutting things down, wait for systems that need threading to shutdown
|
---|
262 | ^(*mainCluster->io.ctxs){};
|
---|
263 | mainCluster->io.cnt = 0;
|
---|
264 | mainCluster->io.ctxs = 0p;
|
---|
265 |
|
---|
266 | /* paranoid */ verify( __preemption_enabled() );
|
---|
267 | disable_interrupts();
|
---|
268 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
269 |
|
---|
270 | __cfadbg_print_safe(runtime_core, "\n--------------------------------------------------\nKernel : Shutting down\n");
|
---|
271 |
|
---|
272 | // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
|
---|
273 | // When its coroutine terminates, it return control to the mainThread
|
---|
274 | // which is currently here
|
---|
275 | __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);
|
---|
276 | __kernel_last_resume( __cfaabi_tls.this_processor );
|
---|
277 | mainThread->self_cor.state = Halted;
|
---|
278 |
|
---|
279 | // THE SYSTEM IS NOW COMPLETELY STOPPED
|
---|
280 |
|
---|
281 | // Disable preemption
|
---|
282 | __kernel_alarm_shutdown();
|
---|
283 |
|
---|
284 | // Stop IO
|
---|
285 | __kernel_io_shutdown();
|
---|
286 |
|
---|
287 | // Destroy the main processor and its context in reverse order of construction
|
---|
288 | // These were manually constructed so we need manually destroy them
|
---|
289 | void ^?{}(processor & this) with( this ){
|
---|
290 | deinit( this );
|
---|
291 |
|
---|
292 | /* paranoid */ verify( this.do_terminate == true );
|
---|
293 | __cfaabi_dbg_print_safe("Kernel : destroyed main processor context %p\n", &runner);
|
---|
294 | }
|
---|
295 |
|
---|
296 | ^(*mainProcessor){};
|
---|
297 |
|
---|
298 | // Final step, destroy the main thread since it is no longer needed
|
---|
299 |
|
---|
300 | // Since we provided a stack to this taxk it will not destroy anything
|
---|
301 | /* paranoid */ verify(mainThread->self_cor.stack.storage == (__stack_t*)(((uintptr_t)&storage_mainThreadCtx)| 0x1));
|
---|
302 | ^(*mainThread){};
|
---|
303 |
|
---|
304 | ^(*mainCluster){};
|
---|
305 |
|
---|
306 | ^(*__scheduler_lock){};
|
---|
307 |
|
---|
308 | ^(__cfa_dbg_global_clusters.list){};
|
---|
309 | ^(__cfa_dbg_global_clusters.lock){};
|
---|
310 |
|
---|
311 | __cfadbg_print_safe(runtime_core, "Kernel : Shutdown complete\n");
|
---|
312 | }
|
---|
313 |
|
---|
314 | //=============================================================================================
|
---|
315 | // Kernel Initial Scheduling logic
|
---|
316 | //=============================================================================================
|
---|
317 |
|
---|
318 | // Context invoker for processors
|
---|
319 | // This is the entry point for processors (kernel threads) *except* for the main processor
|
---|
320 | // It effectively constructs a coroutine by stealing the pthread stack
|
---|
321 | static void * __invoke_processor(void * arg) {
|
---|
322 | #if !defined( __CFA_NO_STATISTICS__ )
|
---|
323 | __stats_t local_stats;
|
---|
324 | __init_stats( &local_stats );
|
---|
325 | __cfaabi_tls.this_stats = &local_stats;
|
---|
326 | #endif
|
---|
327 |
|
---|
328 | processor * proc = (processor *) arg;
|
---|
329 | __cfaabi_tls.this_processor = proc;
|
---|
330 | __cfaabi_tls.this_proc_id = (__processor_id_t*)proc;
|
---|
331 | __cfaabi_tls.this_thread = 0p;
|
---|
332 | __cfaabi_tls.preemption_state.[enabled, disable_count] = [false, 1];
|
---|
333 | // SKULLDUGGERY: We want to create a context for the processor coroutine
|
---|
334 | // which is needed for the 2-step context switch. However, there is no reason
|
---|
335 | // to waste the perfectly valid stack create by pthread.
|
---|
336 | current_stack_info_t info;
|
---|
337 | __stack_t ctx;
|
---|
338 | info.storage = &ctx;
|
---|
339 | (proc->runner){ proc, &info };
|
---|
340 |
|
---|
341 | __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage);
|
---|
342 |
|
---|
343 | //Set global state
|
---|
344 | __cfaabi_tls.this_thread = 0p;
|
---|
345 |
|
---|
346 | //We now have a proper context from which to schedule threads
|
---|
347 | __cfadbg_print_safe(runtime_core, "Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx);
|
---|
348 |
|
---|
349 | // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
|
---|
350 | // resume it to start it like it normally would, it will just context switch
|
---|
351 | // back to here. Instead directly call the main since we already are on the
|
---|
352 | // appropriate stack.
|
---|
353 | get_coroutine(proc->runner)->state = Active;
|
---|
354 | main( proc->runner );
|
---|
355 | get_coroutine(proc->runner)->state = Halted;
|
---|
356 |
|
---|
357 | // Main routine of the core returned, the core is now fully terminated
|
---|
358 | __cfadbg_print_safe(runtime_core, "Kernel : core %p main ended (%p)\n", proc, &proc->runner);
|
---|
359 |
|
---|
360 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
361 | __tally_stats(proc->cltr->stats, &local_stats);
|
---|
362 | if( 0 != proc->print_stats ) {
|
---|
363 | __print_stats( &local_stats, proc->print_stats, "Processor ", proc->name, (void*)proc );
|
---|
364 | }
|
---|
365 | #endif
|
---|
366 |
|
---|
367 | return 0p;
|
---|
368 | }
|
---|
369 |
|
---|
370 | static void __kernel_first_resume( processor * this ) {
|
---|
371 | $thread * src = mainThread;
|
---|
372 | $coroutine * dst = get_coroutine(this->runner);
|
---|
373 |
|
---|
374 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
375 |
|
---|
376 | __cfaabi_tls.this_thread->curr_cor = dst;
|
---|
377 | __stack_prepare( &dst->stack, 65000 );
|
---|
378 | __cfactx_start(main, dst, this->runner, __cfactx_invoke_coroutine);
|
---|
379 |
|
---|
380 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
381 |
|
---|
382 | dst->last = &src->self_cor;
|
---|
383 | dst->starter = dst->starter ? dst->starter : &src->self_cor;
|
---|
384 |
|
---|
385 | // make sure the current state is still correct
|
---|
386 | /* paranoid */ verify(src->state == Ready);
|
---|
387 |
|
---|
388 | // context switch to specified coroutine
|
---|
389 | verify( dst->context.SP );
|
---|
390 | __cfactx_switch( &src->context, &dst->context );
|
---|
391 | // when __cfactx_switch returns we are back in the src coroutine
|
---|
392 |
|
---|
393 | mainThread->curr_cor = &mainThread->self_cor;
|
---|
394 |
|
---|
395 | // make sure the current state has been update
|
---|
396 | /* paranoid */ verify(src->state == Active);
|
---|
397 |
|
---|
398 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
399 | }
|
---|
400 |
|
---|
401 | // KERNEL_ONLY
|
---|
402 | static void __kernel_last_resume( processor * this ) {
|
---|
403 | $coroutine * src = &mainThread->self_cor;
|
---|
404 | $coroutine * dst = get_coroutine(this->runner);
|
---|
405 |
|
---|
406 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
407 | /* paranoid */ verify( dst->starter == src );
|
---|
408 | /* paranoid */ verify( dst->context.SP );
|
---|
409 |
|
---|
410 | // SKULLDUGGERY in debug the processors check that the
|
---|
411 | // stack is still within the limit of the stack limits after running a thread.
|
---|
412 | // that check doesn't make sense if we context switch to the processor using the
|
---|
413 | // coroutine semantics. Since this is a special case, use the current context
|
---|
414 | // info to populate these fields.
|
---|
415 | __cfaabi_dbg_debug_do(
|
---|
416 | __stack_context_t ctx;
|
---|
417 | CtxGet( ctx );
|
---|
418 | mainThread->context.SP = ctx.SP;
|
---|
419 | mainThread->context.FP = ctx.FP;
|
---|
420 | )
|
---|
421 |
|
---|
422 | // context switch to the processor
|
---|
423 | __cfactx_switch( &src->context, &dst->context );
|
---|
424 | }
|
---|
425 |
|
---|
426 |
|
---|
427 | //=============================================================================================
|
---|
428 | // Kernel Object Constructors logic
|
---|
429 | //=============================================================================================
|
---|
430 | //-----------------------------------------------------------------------------
|
---|
431 | // Main thread construction
|
---|
432 | static void ?{}( $coroutine & this, current_stack_info_t * info) with( this ) {
|
---|
433 | stack.storage = info->storage;
|
---|
434 | with(*stack.storage) {
|
---|
435 | limit = info->limit;
|
---|
436 | base = info->base;
|
---|
437 | }
|
---|
438 | __attribute__((may_alias)) intptr_t * istorage = (intptr_t*) &stack.storage;
|
---|
439 | *istorage |= 0x1;
|
---|
440 | name = "Main Thread";
|
---|
441 | state = Start;
|
---|
442 | starter = 0p;
|
---|
443 | last = 0p;
|
---|
444 | cancellation = 0p;
|
---|
445 | }
|
---|
446 |
|
---|
447 | static void ?{}( $thread & this, current_stack_info_t * info) with( this ) {
|
---|
448 | ticket = TICKET_RUNNING;
|
---|
449 | state = Start;
|
---|
450 | self_cor{ info };
|
---|
451 | curr_cor = &self_cor;
|
---|
452 | curr_cluster = mainCluster;
|
---|
453 | self_mon.owner = &this;
|
---|
454 | self_mon.recursion = 1;
|
---|
455 | self_mon_p = &self_mon;
|
---|
456 | link.next = 0p;
|
---|
457 | link.prev = 0p;
|
---|
458 | #if defined( __CFA_WITH_VERIFY__ )
|
---|
459 | canary = 0x0D15EA5E0D15EA5Ep;
|
---|
460 | #endif
|
---|
461 |
|
---|
462 | node.next = 0p;
|
---|
463 | node.prev = 0p;
|
---|
464 | doregister(curr_cluster, this);
|
---|
465 |
|
---|
466 | monitors{ &self_mon_p, 1, (fptr_t)0 };
|
---|
467 | }
|
---|
468 |
|
---|
469 | //-----------------------------------------------------------------------------
|
---|
470 | // Processor
|
---|
471 | // Construct the processor context of non-main processors
|
---|
472 | static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) {
|
---|
473 | (this.__cor){ info };
|
---|
474 | this.proc = proc;
|
---|
475 | }
|
---|
476 |
|
---|
477 | static void init(processor & this, const char name[], cluster & _cltr) with( this ) {
|
---|
478 | this.name = name;
|
---|
479 | this.cltr = &_cltr;
|
---|
480 | full_proc = true;
|
---|
481 | do_terminate = false;
|
---|
482 | preemption_alarm = 0p;
|
---|
483 | pending_preemption = false;
|
---|
484 |
|
---|
485 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
486 | print_stats = 0;
|
---|
487 | print_halts = false;
|
---|
488 | #endif
|
---|
489 |
|
---|
490 | lock( this.cltr->idles );
|
---|
491 | int target = this.cltr->idles.total += 1u;
|
---|
492 | unlock( this.cltr->idles );
|
---|
493 |
|
---|
494 | id = doregister((__processor_id_t*)&this);
|
---|
495 |
|
---|
496 | // Lock the RWlock so no-one pushes/pops while we are changing the queue
|
---|
497 | uint_fast32_t last_size = ready_mutate_lock();
|
---|
498 |
|
---|
499 | // Adjust the ready queue size
|
---|
500 | ready_queue_grow( cltr, target );
|
---|
501 |
|
---|
502 | // Unlock the RWlock
|
---|
503 | ready_mutate_unlock( last_size );
|
---|
504 |
|
---|
505 | __cfadbg_print_safe(runtime_core, "Kernel : core %p created\n", &this);
|
---|
506 | }
|
---|
507 |
|
---|
508 | // Not a ctor, it just preps the destruction but should not destroy members
|
---|
509 | static void deinit(processor & this) {
|
---|
510 | lock( this.cltr->idles );
|
---|
511 | int target = this.cltr->idles.total -= 1u;
|
---|
512 | unlock( this.cltr->idles );
|
---|
513 |
|
---|
514 | // Lock the RWlock so no-one pushes/pops while we are changing the queue
|
---|
515 | uint_fast32_t last_size = ready_mutate_lock();
|
---|
516 |
|
---|
517 | // Adjust the ready queue size
|
---|
518 | ready_queue_shrink( this.cltr, target );
|
---|
519 |
|
---|
520 | // Unlock the RWlock
|
---|
521 | ready_mutate_unlock( last_size );
|
---|
522 |
|
---|
523 | // Finally we don't need the read_lock any more
|
---|
524 | unregister((__processor_id_t*)&this);
|
---|
525 | }
|
---|
526 |
|
---|
527 | void ?{}(processor & this, const char name[], cluster & _cltr) {
|
---|
528 | ( this.idle ){};
|
---|
529 | ( this.terminated ){ 0 };
|
---|
530 | ( this.runner ){};
|
---|
531 |
|
---|
532 | disable_interrupts();
|
---|
533 | init( this, name, _cltr );
|
---|
534 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
535 |
|
---|
536 | __cfadbg_print_safe(runtime_core, "Kernel : Starting core %p\n", &this);
|
---|
537 |
|
---|
538 | this.stack = __create_pthread( &this.kernel_thread, __invoke_processor, (void *)&this );
|
---|
539 |
|
---|
540 | }
|
---|
541 |
|
---|
542 | extern size_t __page_size;
|
---|
543 | void ^?{}(processor & this) with( this ){
|
---|
544 | if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) {
|
---|
545 | __cfadbg_print_safe(runtime_core, "Kernel : core %p signaling termination\n", &this);
|
---|
546 |
|
---|
547 | __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED);
|
---|
548 | __wake_proc( &this );
|
---|
549 |
|
---|
550 | P( terminated );
|
---|
551 | /* paranoid */ verify( active_processor() != &this);
|
---|
552 | }
|
---|
553 |
|
---|
554 | __destroy_pthread( kernel_thread, this.stack, 0p );
|
---|
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, "Cluster", 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 | #warning due to the thunk problem, stack creation uses mmap, revert to malloc once this goes away
|
---|
680 | // __cfaabi_dbg_debug_do(
|
---|
681 | // stack = memalign( __page_size, stacksize + __page_size );
|
---|
682 | // // pthread has no mechanism to create the guard page in user supplied stack.
|
---|
683 | // if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
|
---|
684 | // abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
|
---|
685 | // } // if
|
---|
686 | // );
|
---|
687 | // __cfaabi_dbg_no_debug_do(
|
---|
688 | // stack = malloc( stacksize );
|
---|
689 | // );
|
---|
690 | stacksize = ceiling( stacksize, __page_size ) + __page_size;
|
---|
691 | stack = mmap(0p, stacksize, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
|
---|
692 | if(stack == ((void*)-1)) {
|
---|
693 | abort( "pthread stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) );
|
---|
694 | }
|
---|
695 | if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
|
---|
696 | abort( "pthread stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
|
---|
697 | } // if
|
---|
698 |
|
---|
699 | check( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
|
---|
700 |
|
---|
701 | check( pthread_create( pthread, &attr, start, arg ), "pthread_create" );
|
---|
702 | return stack;
|
---|
703 | }
|
---|
704 |
|
---|
705 | void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ) {
|
---|
706 | int err = pthread_join( pthread, retval );
|
---|
707 | if( err != 0 ) abort("KERNEL ERROR: joining pthread %p caused error %s\n", (void*)pthread, strerror(err));
|
---|
708 |
|
---|
709 | pthread_attr_t attr;
|
---|
710 |
|
---|
711 | check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
|
---|
712 |
|
---|
713 | size_t stacksize;
|
---|
714 | // default stack size, normally defined by shell limit
|
---|
715 | check( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
|
---|
716 | assert( stacksize >= PTHREAD_STACK_MIN );
|
---|
717 | stacksize += __page_size;
|
---|
718 |
|
---|
719 | if(munmap(stack, stacksize) == -1) {
|
---|
720 | abort( "pthread stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) );
|
---|
721 | }
|
---|
722 | }
|
---|
723 |
|
---|
724 |
|
---|
725 | #if defined(__CFA_WITH_VERIFY__)
|
---|
726 | static bool verify_fwd_bck_rng(void) {
|
---|
727 | __cfaabi_tls.ready_rng.fwd_seed = 25214903917_l64u * (rdtscl() ^ (uintptr_t)&verify_fwd_bck_rng);
|
---|
728 |
|
---|
729 | unsigned values[10];
|
---|
730 | for(i; 10) {
|
---|
731 | values[i] = __tls_rand_fwd();
|
---|
732 | }
|
---|
733 |
|
---|
734 | __tls_rand_advance_bck();
|
---|
735 |
|
---|
736 | for ( i; 9 -~= 0 ) {
|
---|
737 | if(values[i] != __tls_rand_bck()) {
|
---|
738 | return false;
|
---|
739 | }
|
---|
740 | }
|
---|
741 |
|
---|
742 | return true;
|
---|
743 | }
|
---|
744 | #endif
|
---|