source: libcfa/src/concurrency/kernel/startup.cfa@ bb58825

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since bb58825 was 28c35e2, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Fixed startup to use proper protections

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