source: libcfa/src/concurrency/kernel/startup.cfa @ 90ecade

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 90ecade was 97229d6, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Changed stack creation to toggle between mmap and malloc based on the defines CFA_COROUTINE_USE_MMAP and CFA_PROCESSOR_USE_MMAP

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