source: libcfa/src/concurrency/kernel/startup.cfa@ 19a8c40

ADT ast-experimental
Last change on this file since 19a8c40 was 639e4fc, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Changed cluster link to use explicit type to avoid anonymous names in symbols gdb cares about

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