[c81ebf9] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 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 | // signal.c --
|
---|
| 8 | //
|
---|
| 9 | // Author : Thierry Delisle
|
---|
| 10 | // Created On : Mon Jun 5 14:20:42 2017
|
---|
[6b0b624] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[258e6ad5] | 12 | // Last Modified On : Tue Jan 23 17:59:30 2018
|
---|
| 13 | // Update Count : 7
|
---|
[c81ebf9] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #include "preemption.h"
|
---|
| 17 |
|
---|
[b158d8f] | 18 | #define ftype `ftype`
|
---|
[c81ebf9] | 19 | extern "C" {
|
---|
[82ff5845] | 20 | #include <errno.h>
|
---|
| 21 | #include <stdio.h>
|
---|
| 22 | #include <string.h>
|
---|
| 23 | #include <unistd.h>
|
---|
[c81ebf9] | 24 | }
|
---|
[b158d8f] | 25 | #undef ftype
|
---|
[c81ebf9] | 26 |
|
---|
[dbe9b08] | 27 | #include "bits/signal.h"
|
---|
[82ff5845] | 28 |
|
---|
[969b3fe] | 29 | //TODO move to defaults
|
---|
[82ff5845] | 30 | #define __CFA_DEFAULT_PREEMPTION__ 10000
|
---|
[c81ebf9] | 31 |
|
---|
[969b3fe] | 32 | //TODO move to defaults
|
---|
[c81ebf9] | 33 | __attribute__((weak)) unsigned int default_preemption() {
|
---|
| 34 | return __CFA_DEFAULT_PREEMPTION__;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[969b3fe] | 37 | // FwdDeclarations : timeout handlers
|
---|
[c81ebf9] | 38 | static void preempt( processor * this );
|
---|
| 39 | static void timeout( thread_desc * this );
|
---|
| 40 |
|
---|
[969b3fe] | 41 | // FwdDeclarations : Signal handlers
|
---|
[82ff5845] | 42 | void sigHandler_ctxSwitch( __CFA_SIGPARMS__ );
|
---|
[1c273d0] | 43 | void sigHandler_segv ( __CFA_SIGPARMS__ );
|
---|
| 44 | void sigHandler_abort ( __CFA_SIGPARMS__ );
|
---|
[82ff5845] | 45 |
|
---|
[969b3fe] | 46 | // FwdDeclarations : alarm thread main
|
---|
| 47 | void * alarm_loop( __attribute__((unused)) void * args );
|
---|
| 48 |
|
---|
| 49 | // Machine specific register name
|
---|
[b158d8f] | 50 | #if defined(__x86_64__)
|
---|
[b2b44d8] | 51 | #define CFA_REG_IP gregs[REG_RIP]
|
---|
[b158d8f] | 52 | #elif defined(__i386__)
|
---|
[b2b44d8] | 53 | #define CFA_REG_IP gregs[REG_EIP]
|
---|
[b158d8f] | 54 | #elif defined(__ARM_ARCH__)
|
---|
[b2b44d8] | 55 | #define CFA_REG_IP arm_pc
|
---|
[cd17862] | 56 | #endif
|
---|
| 57 |
|
---|
[969b3fe] | 58 | KERNEL_STORAGE(event_kernel_t, event_kernel); // private storage for event kernel
|
---|
| 59 | event_kernel_t * event_kernel; // kernel public handle to even kernel
|
---|
| 60 | static pthread_t alarm_thread; // pthread handle to alarm thread
|
---|
| 61 |
|
---|
[65deb18] | 62 | void ?{}(event_kernel_t & this) with( this ) {
|
---|
| 63 | alarms{};
|
---|
| 64 | lock{};
|
---|
[969b3fe] | 65 | }
|
---|
[82ff5845] | 66 |
|
---|
[c81ebf9] | 67 | //=============================================================================================
|
---|
| 68 | // Kernel Preemption logic
|
---|
| 69 | //=============================================================================================
|
---|
| 70 |
|
---|
[969b3fe] | 71 | // Get next expired node
|
---|
| 72 | static inline alarm_node_t * get_expired( alarm_list_t * alarms, __cfa_time_t currtime ) {
|
---|
| 73 | if( !alarms->head ) return NULL; // If no alarms return null
|
---|
| 74 | if( alarms->head->alarm >= currtime ) return NULL; // If alarms head not expired return null
|
---|
| 75 | return pop(alarms); // Otherwise just pop head
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | // Tick one frame of the Discrete Event Simulation for alarms
|
---|
[c81ebf9] | 79 | void tick_preemption() {
|
---|
[969b3fe] | 80 | alarm_node_t * node = NULL; // Used in the while loop but cannot be declared in the while condition
|
---|
| 81 | alarm_list_t * alarms = &event_kernel->alarms; // Local copy for ease of reading
|
---|
| 82 | __cfa_time_t currtime = __kernel_get_time(); // Check current time once so we everything "happens at once"
|
---|
[8cb529e] | 83 |
|
---|
[969b3fe] | 84 | //Loop throught every thing expired
|
---|
| 85 | while( node = get_expired( alarms, currtime ) ) {
|
---|
[1c273d0] | 86 |
|
---|
[969b3fe] | 87 | // Check if this is a kernel
|
---|
[c81ebf9] | 88 | if( node->kernel_alarm ) {
|
---|
| 89 | preempt( node->proc );
|
---|
| 90 | }
|
---|
| 91 | else {
|
---|
| 92 | timeout( node->thrd );
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[969b3fe] | 95 | // Check if this is a periodic alarm
|
---|
[8cb529e] | 96 | __cfa_time_t period = node->period;
|
---|
| 97 | if( period > 0 ) {
|
---|
[969b3fe] | 98 | node->alarm = currtime + period; // Alarm is periodic, add currtime to it (used cached current time)
|
---|
| 99 | insert( alarms, node ); // Reinsert the node for the next time it triggers
|
---|
[c81ebf9] | 100 | }
|
---|
| 101 | else {
|
---|
[969b3fe] | 102 | node->set = false; // Node is one-shot, just mark it as not pending
|
---|
[c81ebf9] | 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[969b3fe] | 106 | // If there are still alarms pending, reset the timer
|
---|
| 107 | if( alarms->head ) { __kernel_set_timer( alarms->head->alarm - currtime ); }
|
---|
[c81ebf9] | 108 | }
|
---|
| 109 |
|
---|
[969b3fe] | 110 | // Update the preemption of a processor and notify interested parties
|
---|
[c81ebf9] | 111 | void update_preemption( processor * this, __cfa_time_t duration ) {
|
---|
| 112 | alarm_node_t * alarm = this->preemption_alarm;
|
---|
| 113 |
|
---|
| 114 | // Alarms need to be enabled
|
---|
| 115 | if ( duration > 0 && !alarm->set ) {
|
---|
| 116 | alarm->alarm = __kernel_get_time() + duration;
|
---|
| 117 | alarm->period = duration;
|
---|
| 118 | register_self( alarm );
|
---|
| 119 | }
|
---|
| 120 | // Zero duraction but alarm is set
|
---|
| 121 | else if ( duration == 0 && alarm->set ) {
|
---|
| 122 | unregister_self( alarm );
|
---|
| 123 | alarm->alarm = 0;
|
---|
| 124 | alarm->period = 0;
|
---|
| 125 | }
|
---|
| 126 | // If alarm is different from previous, change it
|
---|
| 127 | else if ( duration > 0 && alarm->period != duration ) {
|
---|
| 128 | unregister_self( alarm );
|
---|
| 129 | alarm->alarm = __kernel_get_time() + duration;
|
---|
| 130 | alarm->period = duration;
|
---|
| 131 | register_self( alarm );
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | //=============================================================================================
|
---|
[cd17862] | 136 | // Kernel Signal Tools
|
---|
[c81ebf9] | 137 | //=============================================================================================
|
---|
| 138 |
|
---|
[36982fc] | 139 | __cfaabi_dbg_debug_do( static thread_local void * last_interrupt = 0; )
|
---|
[b227f68] | 140 |
|
---|
[82ff5845] | 141 | extern "C" {
|
---|
[969b3fe] | 142 | // Disable interrupts by incrementing the counter
|
---|
[82ff5845] | 143 | void disable_interrupts() {
|
---|
[d0a045c7] | 144 | preemption_enabled = false;
|
---|
| 145 | __attribute__((unused)) unsigned short new_val = disable_preempt_count + 1;
|
---|
| 146 | disable_preempt_count = new_val;
|
---|
[969b3fe] | 147 | verify( new_val < 65_000u ); // If this triggers someone is disabling interrupts without enabling them
|
---|
[82ff5845] | 148 | }
|
---|
| 149 |
|
---|
[969b3fe] | 150 | // Enable interrupts by decrementing the counter
|
---|
| 151 | // If counter reaches 0, execute any pending CtxSwitch
|
---|
[36982fc] | 152 | void enable_interrupts( __cfaabi_dbg_ctx_param ) {
|
---|
[65deb18] | 153 | processor * proc = this_processor; // Cache the processor now since interrupts can start happening after the atomic add
|
---|
[969b3fe] | 154 | thread_desc * thrd = this_thread; // Cache the thread now since interrupts can start happening after the atomic add
|
---|
| 155 |
|
---|
[d0a045c7] | 156 | unsigned short prev = disable_preempt_count;
|
---|
| 157 | disable_preempt_count -= 1;
|
---|
[969b3fe] | 158 | verify( prev != 0u ); // If this triggers someone is enabled already enabled interruptsverify( prev != 0u );
|
---|
| 159 |
|
---|
| 160 | // Check if we need to prempt the thread because an interrupt was missed
|
---|
[d0a045c7] | 161 | if( prev == 1 ) {
|
---|
| 162 | preemption_enabled = true;
|
---|
| 163 | if( proc->pending_preemption ) {
|
---|
| 164 | proc->pending_preemption = false;
|
---|
| 165 | BlockInternal( thrd );
|
---|
| 166 | }
|
---|
[82ff5845] | 167 | }
|
---|
[4e6fb8e] | 168 |
|
---|
[969b3fe] | 169 | // For debugging purposes : keep track of the last person to enable the interrupts
|
---|
[36982fc] | 170 | __cfaabi_dbg_debug_do( proc->last_enable = caller; )
|
---|
[82ff5845] | 171 | }
|
---|
[969b3fe] | 172 |
|
---|
| 173 | // Disable interrupts by incrementint the counter
|
---|
| 174 | // Don't execute any pending CtxSwitch even if counter reaches 0
|
---|
| 175 | void enable_interrupts_noPoll() {
|
---|
[d0a045c7] | 176 | unsigned short prev = disable_preempt_count;
|
---|
| 177 | disable_preempt_count -= 1;
|
---|
[2e9aed4] | 178 | verifyf( prev != 0u, "Incremented from %u\n", prev ); // If this triggers someone is enabled already enabled interrupts
|
---|
[d0a045c7] | 179 | if( prev == 1 ) {
|
---|
| 180 | preemption_enabled = true;
|
---|
| 181 | }
|
---|
[969b3fe] | 182 | }
|
---|
[82ff5845] | 183 | }
|
---|
| 184 |
|
---|
[969b3fe] | 185 | // sigprocmask wrapper : unblock a single signal
|
---|
[1c273d0] | 186 | static inline void signal_unblock( int sig ) {
|
---|
[82ff5845] | 187 | sigset_t mask;
|
---|
| 188 | sigemptyset( &mask );
|
---|
[1c273d0] | 189 | sigaddset( &mask, sig );
|
---|
[82ff5845] | 190 |
|
---|
[47ecf2b] | 191 | if ( pthread_sigmask( SIG_UNBLOCK, &mask, NULL ) == -1 ) {
|
---|
| 192 | abortf( "internal error, pthread_sigmask" );
|
---|
[cd17862] | 193 | }
|
---|
[82ff5845] | 194 | }
|
---|
| 195 |
|
---|
[969b3fe] | 196 | // sigprocmask wrapper : block a single signal
|
---|
[cd17862] | 197 | static inline void signal_block( int sig ) {
|
---|
| 198 | sigset_t mask;
|
---|
| 199 | sigemptyset( &mask );
|
---|
| 200 | sigaddset( &mask, sig );
|
---|
[47ecf2b] | 201 |
|
---|
[cd17862] | 202 | if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) {
|
---|
| 203 | abortf( "internal error, pthread_sigmask" );
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
[47ecf2b] | 206 |
|
---|
[969b3fe] | 207 | // kill wrapper : signal a processor
|
---|
[cd17862] | 208 | static void preempt( processor * this ) {
|
---|
| 209 | pthread_kill( this->kernel_thread, SIGUSR1 );
|
---|
[1c273d0] | 210 | }
|
---|
[82ff5845] | 211 |
|
---|
[969b3fe] | 212 | // reserved for future use
|
---|
[cd17862] | 213 | static void timeout( thread_desc * this ) {
|
---|
| 214 | //TODO : implement waking threads
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[969b3fe] | 217 |
|
---|
| 218 | // Check if a CtxSwitch signal handler shoud defer
|
---|
| 219 | // If true : preemption is safe
|
---|
| 220 | // If false : preemption is unsafe and marked as pending
|
---|
| 221 | static inline bool preemption_ready() {
|
---|
[d0a045c7] | 222 | bool ready = preemption_enabled && !preemption_in_progress; // Check if preemption is safe
|
---|
[969b3fe] | 223 | this_processor->pending_preemption = !ready; // Adjust the pending flag accordingly
|
---|
| 224 | return ready;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
[cd17862] | 227 | //=============================================================================================
|
---|
| 228 | // Kernel Signal Startup/Shutdown logic
|
---|
| 229 | //=============================================================================================
|
---|
| 230 |
|
---|
[969b3fe] | 231 | // Startup routine to activate preemption
|
---|
| 232 | // Called from kernel_startup
|
---|
[cd17862] | 233 | void kernel_start_preemption() {
|
---|
[36982fc] | 234 | __cfaabi_dbg_print_safe("Kernel : Starting preemption\n");
|
---|
[969b3fe] | 235 |
|
---|
| 236 | // Start with preemption disabled until ready
|
---|
[d0a045c7] | 237 | preemption_enabled = false;
|
---|
[969b3fe] | 238 | disable_preempt_count = 1;
|
---|
| 239 |
|
---|
| 240 | // Initialize the event kernel
|
---|
| 241 | event_kernel = (event_kernel_t *)&storage_event_kernel;
|
---|
[9236060] | 242 | (*event_kernel){};
|
---|
[969b3fe] | 243 |
|
---|
| 244 | // Setup proper signal handlers
|
---|
[ed235b6] | 245 | __kernel_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO | SA_RESTART ); // CtxSwitch handler
|
---|
[cd17862] | 246 |
|
---|
| 247 | signal_block( SIGALRM );
|
---|
| 248 |
|
---|
| 249 | pthread_create( &alarm_thread, NULL, alarm_loop, NULL );
|
---|
| 250 | }
|
---|
| 251 |
|
---|
[969b3fe] | 252 | // Shutdown routine to deactivate preemption
|
---|
| 253 | // Called from kernel_shutdown
|
---|
[cd17862] | 254 | void kernel_stop_preemption() {
|
---|
[36982fc] | 255 | __cfaabi_dbg_print_safe("Kernel : Preemption stopping\n");
|
---|
[d6ff3ff] | 256 |
|
---|
[969b3fe] | 257 | // Block all signals since we are already shutting down
|
---|
[cd17862] | 258 | sigset_t mask;
|
---|
| 259 | sigfillset( &mask );
|
---|
| 260 | sigprocmask( SIG_BLOCK, &mask, NULL );
|
---|
| 261 |
|
---|
[969b3fe] | 262 | // Notify the alarm thread of the shutdown
|
---|
[a0b3e32] | 263 | sigval val = { 1 };
|
---|
| 264 | pthread_sigqueue( alarm_thread, SIGALRM, val );
|
---|
[969b3fe] | 265 |
|
---|
| 266 | // Wait for the preemption thread to finish
|
---|
[cd17862] | 267 | pthread_join( alarm_thread, NULL );
|
---|
[969b3fe] | 268 |
|
---|
| 269 | // Preemption is now fully stopped
|
---|
| 270 |
|
---|
[36982fc] | 271 | __cfaabi_dbg_print_safe("Kernel : Preemption stopped\n");
|
---|
[cd17862] | 272 | }
|
---|
| 273 |
|
---|
[969b3fe] | 274 | // Raii ctor/dtor for the preemption_scope
|
---|
| 275 | // Used by thread to control when they want to receive preemption signals
|
---|
[242a902] | 276 | void ?{}( preemption_scope & this, processor * proc ) {
|
---|
[9236060] | 277 | (this.alarm){ proc, zero_time, zero_time };
|
---|
[242a902] | 278 | this.proc = proc;
|
---|
| 279 | this.proc->preemption_alarm = &this.alarm;
|
---|
[969b3fe] | 280 |
|
---|
[9236060] | 281 | update_preemption( this.proc, from_us(this.proc->cltr->preemption) );
|
---|
[cd17862] | 282 | }
|
---|
| 283 |
|
---|
[242a902] | 284 | void ^?{}( preemption_scope & this ) {
|
---|
[cd17862] | 285 | disable_interrupts();
|
---|
| 286 |
|
---|
[9236060] | 287 | update_preemption( this.proc, zero_time );
|
---|
[cd17862] | 288 | }
|
---|
| 289 |
|
---|
| 290 | //=============================================================================================
|
---|
| 291 | // Kernel Signal Handlers
|
---|
| 292 | //=============================================================================================
|
---|
[47ecf2b] | 293 |
|
---|
[969b3fe] | 294 | // Context switch signal handler
|
---|
| 295 | // Receives SIGUSR1 signal and causes the current thread to yield
|
---|
[1c273d0] | 296 | void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) {
|
---|
[b2b44d8] | 297 | __cfaabi_dbg_debug_do( last_interrupt = (void *)(cxt->uc_mcontext.CFA_REG_IP); )
|
---|
[969b3fe] | 298 |
|
---|
[b2b44d8] | 299 | // Check if it is safe to preempt here
|
---|
[969b3fe] | 300 | if( !preemption_ready() ) { return; }
|
---|
| 301 |
|
---|
[2e9aed4] | 302 | __cfaabi_dbg_print_buffer_decl(" KERNEL: preempting core %p (%p).\n", this_processor, this_thread);
|
---|
[05615ba] | 303 |
|
---|
[969b3fe] | 304 | preemption_in_progress = true; // Sync flag : prevent recursive calls to the signal handler
|
---|
| 305 | signal_unblock( SIGUSR1 ); // We are about to CtxSwitch out of the signal handler, let other handlers in
|
---|
| 306 | preemption_in_progress = false; // Clear the in progress flag
|
---|
| 307 |
|
---|
| 308 | // Preemption can occur here
|
---|
| 309 |
|
---|
| 310 | BlockInternal( (thread_desc*)this_thread ); // Do the actual CtxSwitch
|
---|
[c81ebf9] | 311 | }
|
---|
| 312 |
|
---|
[969b3fe] | 313 | // Main of the alarm thread
|
---|
| 314 | // Waits on SIGALRM and send SIGUSR1 to whom ever needs it
|
---|
[cd17862] | 315 | void * alarm_loop( __attribute__((unused)) void * args ) {
|
---|
[969b3fe] | 316 | // Block sigalrms to control when they arrive
|
---|
[cd17862] | 317 | sigset_t mask;
|
---|
| 318 | sigemptyset( &mask );
|
---|
| 319 | sigaddset( &mask, SIGALRM );
|
---|
| 320 |
|
---|
| 321 | if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) {
|
---|
| 322 | abortf( "internal error, pthread_sigmask" );
|
---|
[82ff5845] | 323 | }
|
---|
[c81ebf9] | 324 |
|
---|
[969b3fe] | 325 | // Main loop
|
---|
[cd17862] | 326 | while( true ) {
|
---|
[969b3fe] | 327 | // Wait for a sigalrm
|
---|
[a0b3e32] | 328 | siginfo_t info;
|
---|
| 329 | int sig = sigwaitinfo( &mask, &info );
|
---|
[969b3fe] | 330 |
|
---|
[e2f7bc3] | 331 | if( sig < 0 ) {
|
---|
| 332 | //Error!
|
---|
| 333 | int err = errno;
|
---|
| 334 | switch( err ) {
|
---|
| 335 | case EAGAIN :
|
---|
| 336 | case EINTR :
|
---|
| 337 | continue;
|
---|
| 338 | case EINVAL :
|
---|
| 339 | abortf("Timeout was invalid.");
|
---|
| 340 | default:
|
---|
| 341 | abortf("Unhandled error %d", err);
|
---|
| 342 | }
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[969b3fe] | 345 | // If another signal arrived something went wrong
|
---|
[8cb529e] | 346 | assertf(sig == SIGALRM, "Kernel Internal Error, sigwait: Unexpected signal %d (%d : %d)\n", sig, info.si_code, info.si_value.sival_int);
|
---|
| 347 |
|
---|
[36982fc] | 348 | // __cfaabi_dbg_print_safe("Kernel : Caught alarm from %d with %d\n", info.si_code, info.si_value.sival_int );
|
---|
[969b3fe] | 349 | // Switch on the code (a.k.a. the sender) to
|
---|
[8cb529e] | 350 | switch( info.si_code )
|
---|
[a0b3e32] | 351 | {
|
---|
[969b3fe] | 352 | // Timers can apparently be marked as sent for the kernel
|
---|
| 353 | // In either case, tick preemption
|
---|
[8cb529e] | 354 | case SI_TIMER:
|
---|
| 355 | case SI_KERNEL:
|
---|
[36982fc] | 356 | // __cfaabi_dbg_print_safe("Kernel : Preemption thread tick\n");
|
---|
| 357 | lock( event_kernel->lock __cfaabi_dbg_ctx2 );
|
---|
[8cb529e] | 358 | tick_preemption();
|
---|
[ea7d2b0] | 359 | unlock( event_kernel->lock );
|
---|
[8cb529e] | 360 | break;
|
---|
[969b3fe] | 361 | // Signal was not sent by the kernel but by an other thread
|
---|
[8cb529e] | 362 | case SI_QUEUE:
|
---|
[969b3fe] | 363 | // For now, other thread only signal the alarm thread to shut it down
|
---|
| 364 | // If this needs to change use info.si_value and handle the case here
|
---|
[8cb529e] | 365 | goto EXIT;
|
---|
[cd17862] | 366 | }
|
---|
| 367 | }
|
---|
[a0b3e32] | 368 |
|
---|
[8cb529e] | 369 | EXIT:
|
---|
[36982fc] | 370 | __cfaabi_dbg_print_safe("Kernel : Preemption thread stopping\n");
|
---|
[a0b3e32] | 371 | return NULL;
|
---|
[82ff5845] | 372 | }
|
---|
| 373 |
|
---|
[6b0b624] | 374 | // Local Variables: //
|
---|
| 375 | // mode: c //
|
---|
| 376 | // tab-width: 4 //
|
---|
| 377 | // End: //
|
---|