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