// -*- Mode: CFA -*- // // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // signal.c -- // // Author : Thierry Delisle // Created On : Mon Jun 5 14:20:42 2017 // Last Modified By : Thierry Delisle // Last Modified On : -- // Update Count : 0 // #include "libhdr.h" #include "preemption.h" extern "C" { #include #include #define __USE_GNU #include #undef __USE_GNU #include #include #include } #ifdef __USE_STREAM__ #include "fstream" #endif #define __CFA_DEFAULT_PREEMPTION__ 10000 __attribute__((weak)) unsigned int default_preemption() { return __CFA_DEFAULT_PREEMPTION__; } #define __CFA_SIGCXT__ ucontext_t * #define __CFA_SIGPARMS__ __attribute__((unused)) int sig, __attribute__((unused)) siginfo_t *sfp, __attribute__((unused)) __CFA_SIGCXT__ cxt static void preempt( processor * this ); static void timeout( thread_desc * this ); void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ); void sigHandler_alarm ( __CFA_SIGPARMS__ ); void sigHandler_segv ( __CFA_SIGPARMS__ ); void sigHandler_abort ( __CFA_SIGPARMS__ ); static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags ); LIB_DEBUG_DO( bool validate( alarm_list_t * this ); ) #ifdef __x86_64__ #define CFA_REG_IP REG_RIP #else #define CFA_REG_IP REG_EIP #endif //============================================================================================= // Kernel Preemption logic //============================================================================================= void tick_preemption() { alarm_list_t * alarms = &systemProcessor->alarms; __cfa_time_t currtime = __kernel_get_time(); // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Ticking preemption @ %llu\n", currtime ); while( alarms->head && alarms->head->alarm < currtime ) { alarm_node_t * node = pop(alarms); // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ticking %p\n", node ); if( node->kernel_alarm ) { preempt( node->proc ); } else { timeout( node->thrd ); } verify( validate( alarms ) ); __cfa_time_t period = node->period; if( period > 0 ) { node->alarm = currtime + period; // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Reinsert %p @ %llu (%llu + %llu)\n", node, node->alarm, currtime, period ); insert( alarms, node ); } else { node->set = false; } } if( alarms->head ) { __kernel_set_timer( alarms->head->alarm - currtime ); } verify( validate( alarms ) ); // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ticking preemption done\n" ); } void update_preemption( processor * this, __cfa_time_t duration ) { LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Processor : %p updating preemption to %llu\n", this, duration ); alarm_node_t * alarm = this->preemption_alarm; duration *= 1000; // Alarms need to be enabled if ( duration > 0 && !alarm->set ) { alarm->alarm = __kernel_get_time() + duration; alarm->period = duration; register_self( alarm ); } // Zero duraction but alarm is set else if ( duration == 0 && alarm->set ) { unregister_self( alarm ); alarm->alarm = 0; alarm->period = 0; } // If alarm is different from previous, change it else if ( duration > 0 && alarm->period != duration ) { unregister_self( alarm ); alarm->alarm = __kernel_get_time() + duration; alarm->period = duration; register_self( alarm ); } } //============================================================================================= // Kernel Signal Tools //============================================================================================= LIB_DEBUG_DO( static thread_local void * last_interrupt = 0; ) extern "C" { void disable_interrupts() { __attribute__((unused)) unsigned short new_val = __atomic_add_fetch_2( &disable_preempt_count, 1, __ATOMIC_SEQ_CST ); verify( new_val < (unsigned short)65_000 ); verify( new_val != (unsigned short) 0 ); } void enable_interrupts_noRF() { __attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST ); verify( prev != (unsigned short) 0 ); } void enable_interrupts( DEBUG_CTX_PARAM ) { processor * proc = this_processor; thread_desc * thrd = this_thread; unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST ); verify( prev != (unsigned short) 0 ); if( prev == 1 && proc->pending_preemption ) { proc->pending_preemption = false; BlockInternal( thrd ); } LIB_DEBUG_DO( proc->last_enable = caller; ) } } static inline void signal_unblock( int sig ) { sigset_t mask; sigemptyset( &mask ); sigaddset( &mask, sig ); if ( pthread_sigmask( SIG_UNBLOCK, &mask, NULL ) == -1 ) { abortf( "internal error, pthread_sigmask" ); } } static inline void signal_block( int sig ) { sigset_t mask; sigemptyset( &mask ); sigaddset( &mask, sig ); if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) { abortf( "internal error, pthread_sigmask" ); } } static inline bool preemption_ready() { return disable_preempt_count == 0 && !preemption_in_progress; } static inline void defer_ctxSwitch() { this_processor->pending_preemption = true; } static inline void defer_alarm() { systemProcessor->pending_alarm = true; } static void preempt( processor * this ) { pthread_kill( this->kernel_thread, SIGUSR1 ); } static void timeout( thread_desc * this ) { //TODO : implement waking threads } //============================================================================================= // Kernel Signal Startup/Shutdown logic //============================================================================================= static pthread_t alarm_thread; void * alarm_loop( __attribute__((unused)) void * args ); void kernel_start_preemption() { LIB_DEBUG_PRINT_SAFE("Kernel : Starting preemption\n"); __kernel_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO ); // __kernel_sigaction( SIGSEGV, sigHandler_segv , SA_SIGINFO ); // __kernel_sigaction( SIGBUS , sigHandler_segv , SA_SIGINFO ); signal_block( SIGALRM ); pthread_create( &alarm_thread, NULL, alarm_loop, NULL ); } void kernel_stop_preemption() { LIB_DEBUG_PRINT_SAFE("Kernel : Preemption stopping\n"); sigset_t mask; sigfillset( &mask ); sigprocmask( SIG_BLOCK, &mask, NULL ); sigval val = { 1 }; pthread_sigqueue( alarm_thread, SIGALRM, val ); pthread_join( alarm_thread, NULL ); LIB_DEBUG_PRINT_SAFE("Kernel : Preemption stopped\n"); } void ?{}( preemption_scope * this, processor * proc ) { (&this->alarm){ proc }; this->proc = proc; this->proc->preemption_alarm = &this->alarm; update_preemption( this->proc, this->proc->preemption ); } void ^?{}( preemption_scope * this ) { disable_interrupts(); update_preemption( this->proc, 0 ); } //============================================================================================= // Kernel Signal Handlers //============================================================================================= void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) { LIB_DEBUG_DO( last_interrupt = (void *)(cxt->uc_mcontext.gregs[CFA_REG_IP]); ) if( preemption_ready() ) { preemption_in_progress = true; signal_unblock( SIGUSR1 ); this_processor->pending_preemption = false; preemption_in_progress = false; BlockInternal( (thread_desc*)this_thread ); } else { defer_ctxSwitch(); } } void * alarm_loop( __attribute__((unused)) void * args ) { sigset_t mask; sigemptyset( &mask ); sigaddset( &mask, SIGALRM ); if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) { abortf( "internal error, pthread_sigmask" ); } while( true ) { siginfo_t info; int sig = sigwaitinfo( &mask, &info ); assertf(sig == SIGALRM, "Kernel Internal Error, sigwait: Unexpected signal %d (%d : %d)\n", sig, info.si_code, info.si_value.sival_int); LIB_DEBUG_PRINT_SAFE("Kernel : Caught alarm from %d with %d\n", info.si_code, info.si_value.sival_int ); switch( info.si_code ) { case SI_TIMER: case SI_KERNEL: LIB_DEBUG_PRINT_SAFE("Kernel : Preemption thread tick\n"); lock( &systemProcessor->alarm_lock DEBUG_CTX2 ); tick_preemption(); unlock( &systemProcessor->alarm_lock ); break; case SI_QUEUE: goto EXIT; } } EXIT: LIB_DEBUG_PRINT_SAFE("Kernel : Preemption thread stopping\n"); return NULL; } static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags ) { struct sigaction act; act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler; act.sa_flags = flags; if ( sigaction( sig, &act, NULL ) == -1 ) { LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, " __kernel_sigaction( sig:%d, handler:%p, flags:%d ), problem installing signal handler, error(%d) %s.\n", sig, handler, flags, errno, strerror( errno ) ); _exit( EXIT_FAILURE ); } } typedef void (*sa_handler_t)(int); static void __kernel_sigdefault( int sig ) { struct sigaction act; // act.sa_handler = SIG_DFL; act.sa_flags = 0; sigemptyset( &act.sa_mask ); if ( sigaction( sig, &act, NULL ) == -1 ) { LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, " __kernel_sigdefault( sig:%d ), problem reseting signal handler, error(%d) %s.\n", sig, errno, strerror( errno ) ); _exit( EXIT_FAILURE ); } } //============================================================================================= // Terminating Signals logic //============================================================================================= LIB_DEBUG_DO( static void __kernel_backtrace( int start ) { // skip first N stack frames enum { Frames = 50 }; void * array[Frames]; int size = backtrace( array, Frames ); char ** messages = backtrace_symbols( array, size ); // find executable name *index( messages[0], '(' ) = '\0'; #ifdef __USE_STREAM__ serr | "Stack back trace for:" | messages[0] | endl; #else fprintf( stderr, "Stack back trace for: %s\n", messages[0]); #endif // skip last 2 stack frames after main for ( int i = start; i < size && messages != NULL; i += 1 ) { char * name = NULL; char * offset_begin = NULL; char * offset_end = NULL; for ( char *p = messages[i]; *p; ++p ) { // find parantheses and +offset if ( *p == '(' ) { name = p; } else if ( *p == '+' ) { offset_begin = p; } else if ( *p == ')' ) { offset_end = p; break; } } // if line contains symbol print it int frameNo = i - start; if ( name && offset_begin && offset_end && name < offset_begin ) { // delimit strings *name++ = '\0'; *offset_begin++ = '\0'; *offset_end++ = '\0'; #ifdef __USE_STREAM__ serr | "(" | frameNo | ")" | messages[i] | ":" | name | "+" | offset_begin | offset_end | endl; #else fprintf( stderr, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end); #endif } // otherwise, print the whole line else { #ifdef __USE_STREAM__ serr | "(" | frameNo | ")" | messages[i] | endl; #else fprintf( stderr, "(%i) %s\n", frameNo, messages[i] ); #endif } } free( messages ); } ) // void sigHandler_segv( __CFA_SIGPARMS__ ) { // LIB_DEBUG_DO( // #ifdef __USE_STREAM__ // serr | "*CFA runtime error* program cfa-cpp terminated with" // | (sig == SIGSEGV ? "segment fault." : "bus error.") // | endl; // #else // fprintf( stderr, "*CFA runtime error* program cfa-cpp terminated with %s\n", sig == SIGSEGV ? "segment fault." : "bus error." ); // #endif // // skip first 2 stack frames // __kernel_backtrace( 1 ); // ) // exit( EXIT_FAILURE ); // } // void sigHandler_abort( __CFA_SIGPARMS__ ) { // // skip first 6 stack frames // LIB_DEBUG_DO( __kernel_backtrace( 6 ); ) // // reset default signal handler // __kernel_sigdefault( SIGABRT ); // raise( SIGABRT ); // }