source: libcfa/src/concurrency/preemption.cfa@ 5c04e82

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 5c04e82 was a33c113, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Minor changes so using the global RWlock is more concise.

  • Property mode set to 100644
File size: 26.7 KB
Line 
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
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Nov 6 07:42:13 2020
13// Update Count : 54
14//
15
16#define __cforall_thread__
17// #define __CFA_DEBUG_PRINT_PREEMPTION__
18
19#include "preemption.hfa"
20#include <assert.h>
21
22#include <errno.h>
23#include <stdio.h>
24#include <string.h>
25#include <unistd.h>
26#include <limits.h> // PTHREAD_STACK_MIN
27
28#include "bits/signal.hfa"
29#include "kernel_private.hfa"
30
31
32#if !defined(__CFA_DEFAULT_PREEMPTION__)
33#define __CFA_DEFAULT_PREEMPTION__ 10`ms
34#endif
35
36__attribute__((weak)) Duration default_preemption() {
37 const char * preempt_rate_s = getenv("CFA_DEFAULT_PREEMPTION");
38 if(!preempt_rate_s) {
39 __cfadbg_print_safe(preemption, "No CFA_DEFAULT_PREEMPTION in ENV\n");
40 return __CFA_DEFAULT_PREEMPTION__;
41 }
42
43 char * endptr = 0p;
44 long int preempt_rate_l = strtol(preempt_rate_s, &endptr, 10);
45 if(preempt_rate_l < 0 || preempt_rate_l > 65535) {
46 __cfadbg_print_safe(preemption, "CFA_DEFAULT_PREEMPTION out of range : %ld\n", preempt_rate_l);
47 return __CFA_DEFAULT_PREEMPTION__;
48 }
49 if('\0' != *endptr) {
50 __cfadbg_print_safe(preemption, "CFA_DEFAULT_PREEMPTION not a decimal number : %s\n", preempt_rate_s);
51 return __CFA_DEFAULT_PREEMPTION__;
52 }
53
54 return preempt_rate_l`ms;
55}
56
57// FwdDeclarations : timeout handlers
58static void preempt( processor * this );
59static void timeout( $thread * this );
60
61// FwdDeclarations : Signal handlers
62static void sigHandler_ctxSwitch( __CFA_SIGPARMS__ );
63static void sigHandler_alarm ( __CFA_SIGPARMS__ );
64static void sigHandler_segv ( __CFA_SIGPARMS__ );
65static void sigHandler_ill ( __CFA_SIGPARMS__ );
66static void sigHandler_fpe ( __CFA_SIGPARMS__ );
67static void sigHandler_abort ( __CFA_SIGPARMS__ );
68
69// FwdDeclarations : alarm thread main
70static void * alarm_loop( __attribute__((unused)) void * args );
71
72// Machine specific register name
73#if defined( __i386 )
74#define CFA_REG_IP gregs[REG_EIP]
75#elif defined( __x86_64 )
76#define CFA_REG_IP gregs[REG_RIP]
77#elif defined( __arm__ )
78#define CFA_REG_IP arm_pc
79#elif defined( __aarch64__ )
80#define CFA_REG_IP pc
81#else
82#error unsupported hardware architecture
83#endif
84
85KERNEL_STORAGE(event_kernel_t, event_kernel); // private storage for event kernel
86event_kernel_t * event_kernel; // kernel public handle to even kernel
87static pthread_t alarm_thread; // pthread handle to alarm thread
88static void * alarm_stack; // pthread stack for alarm thread
89
90static void ?{}(event_kernel_t & this) with( this ) {
91 alarms{};
92 lock{};
93}
94
95enum {
96 PREEMPT_NORMAL = 0,
97 PREEMPT_TERMINATE = 1,
98};
99
100//=============================================================================================
101// Kernel Preemption logic
102//=============================================================================================
103
104// Get next expired node
105static inline alarm_node_t * get_expired( alarm_list_t * alarms, Time currtime ) {
106 if( ! & (*alarms)`first ) return 0p; // If no alarms return null
107 if( (*alarms)`first.alarm >= currtime ) return 0p; // If alarms head not expired return null
108 return pop(alarms); // Otherwise just pop head
109}
110
111// Tick one frame of the Discrete Event Simulation for alarms
112static void tick_preemption(void) {
113 alarm_node_t * node = 0p; // Used in the while loop but cannot be declared in the while condition
114 alarm_list_t * alarms = &event_kernel->alarms; // Local copy for ease of reading
115 Time currtime = __kernel_get_time(); // Check current time once so everything "happens at once"
116
117 //Loop throught every thing expired
118 while( node = get_expired( alarms, currtime ) ) {
119 __cfadbg_print_buffer_decl( preemption, " KERNEL: preemption tick %lu\n", currtime.tn);
120 Duration period = node->period;
121 if( period == 0) {
122 node->set = false; // Node is one-shot, just mark it as not pending
123 }
124
125 __cfadbg_print_buffer_local( preemption, " KERNEL: alarm ticking node %p.\n", node );
126
127
128 // Check if this is a kernel
129 if( node->type == Kernel ) {
130 preempt( node->proc );
131 }
132 else if( node->type == User ) {
133 __cfadbg_print_buffer_local( preemption, " KERNEL: alarm unparking %p.\n", node->thrd );
134 timeout( node->thrd );
135 }
136 else {
137 node->callback(*node);
138 }
139
140 // Check if this is a periodic alarm
141 if( period > 0 ) {
142 __cfadbg_print_buffer_local( preemption, " KERNEL: alarm period is %lu.\n", period`ns );
143 node->alarm = currtime + period; // Alarm is periodic, add currtime to it (used cached current time)
144 insert( alarms, node ); // Reinsert the node for the next time it triggers
145 }
146 }
147
148 // If there are still alarms pending, reset the timer
149 if( & (*alarms)`first ) {
150 Duration delta = (*alarms)`first.alarm - currtime;
151 Duration capped = max(delta, 50`us);
152 __kernel_set_timer( capped );
153 }
154}
155
156// Update the preemption of a processor and notify interested parties
157void update_preemption( processor * this, Duration duration ) {
158 alarm_node_t * alarm = this->preemption_alarm;
159
160 // Alarms need to be enabled
161 if ( duration > 0 && ! alarm->set ) {
162 alarm->alarm = __kernel_get_time() + duration;
163 alarm->period = duration;
164 register_self( alarm );
165 }
166 // Zero duration but alarm is set
167 else if ( duration == 0 && alarm->set ) {
168 unregister_self( alarm );
169 alarm->alarm = 0;
170 alarm->period = 0;
171 }
172 // If alarm is different from previous, change it
173 else if ( duration > 0 && alarm->period != duration ) {
174 unregister_self( alarm );
175 alarm->alarm = __kernel_get_time() + duration;
176 alarm->period = duration;
177 register_self( alarm );
178 }
179}
180
181//=============================================================================================
182// Kernel Signal Tools
183//=============================================================================================
184// In a user-level threading system, there are handful of thread-local variables where this problem occurs on the ARM.
185//
186// For each kernel thread running user-level threads, there is a flag variable to indicate if interrupts are
187// enabled/disabled for that kernel thread. Therefore, this variable is made thread local.
188//
189// For example, this code fragment sets the state of the "interrupt" variable in thread-local memory.
190//
191// _Thread_local volatile int interrupts;
192// int main() {
193// interrupts = 0; // disable interrupts }
194//
195// which generates the following code on the ARM
196//
197// (gdb) disassemble main
198// Dump of assembler code for function main:
199// 0x0000000000000610 <+0>: mrs x1, tpidr_el0
200// 0x0000000000000614 <+4>: mov w0, #0x0 // #0
201// 0x0000000000000618 <+8>: add x1, x1, #0x0, lsl #12
202// 0x000000000000061c <+12>: add x1, x1, #0x10
203// 0x0000000000000620 <+16>: str wzr, [x1]
204// 0x0000000000000624 <+20>: ret
205//
206// The mrs moves a pointer from coprocessor register tpidr_el0 into register x1. Register w0 is set to 0. The two adds
207// increase the TLS pointer with the displacement (offset) 0x10, which is the location in the TSL of variable
208// "interrupts". Finally, 0 is stored into "interrupts" through the pointer in register x1 that points into the
209// TSL. Now once x1 has the pointer to the location of the TSL for kernel thread N, it can be be preempted at a
210// user-level and the user thread is put on the user-level ready-queue. When the preempted thread gets to the front of
211// the user-level ready-queue it is run on kernel thread M. It now stores 0 into "interrupts" back on kernel thread N,
212// turning off interrupt on the wrong kernel thread.
213//
214// On the x86, the following code is generated for the same code fragment.
215//
216// (gdb) disassemble main
217// Dump of assembler code for function main:
218// 0x0000000000400420 <+0>: movl $0x0,%fs:0xfffffffffffffffc
219// 0x000000000040042c <+12>: xor %eax,%eax
220// 0x000000000040042e <+14>: retq
221//
222// and there is base-displacement addressing used to atomically reset variable "interrupts" off of the TSL pointer in
223// register "fs".
224//
225// Hence, the ARM has base-displacement address for the general purpose registers, BUT not to the coprocessor
226// registers. As a result, generating the address for the write into variable "interrupts" is no longer atomic.
227//
228// Note this problem does NOT occur when just using multiple kernel threads because the preemption ALWAYS restarts the
229// thread on the same kernel thread.
230//
231// The obvious question is why does ARM use a coprocessor register to store the TSL pointer given that coprocessor
232// registers are second-class registers with respect to the instruction set. One possible answer is that they did not
233// want to dedicate one of the general registers to hold the TLS pointer and there was a free coprocessor register
234// available.
235
236//-----------------------------------------------------------------------------
237// Some assembly required
238#define __cfaasm_label(label, when) when: asm volatile goto(".global __cfaasm_" #label "_" #when "\n" "__cfaasm_" #label "_" #when ":":::"memory":when)
239
240//----------
241// special case for preemption since used often
242bool __preemption_enabled() {
243 // create a assembler label before
244 // marked as clobber all to avoid movement
245 __cfaasm_label(check, before);
246
247 // access tls as normal
248 bool enabled = __cfaabi_tls.preemption_state.enabled;
249
250 // create a assembler label after
251 // marked as clobber all to avoid movement
252 __cfaasm_label(check, after);
253 return enabled;
254}
255
256struct asm_region {
257 void * before;
258 void * after;
259};
260
261static inline bool __cfaasm_in( void * ip, struct asm_region & region ) {
262 return ip >= region.before && ip <= region.after;
263}
264
265
266//----------
267// Get data from the TLS block
268// struct asm_region __cfaasm_get;
269uintptr_t __cfatls_get( unsigned long int offset ) __attribute__((__noinline__)); //no inline to avoid problems
270uintptr_t __cfatls_get( unsigned long int offset ) {
271 // create a assembler label before
272 // marked as clobber all to avoid movement
273 __cfaasm_label(get, before);
274
275 // access tls as normal (except for pointer arithmetic)
276 uintptr_t val = *(uintptr_t*)((uintptr_t)&__cfaabi_tls + offset);
277
278 // create a assembler label after
279 // marked as clobber all to avoid movement
280 __cfaasm_label(get, after);
281 return val;
282}
283
284extern "C" {
285 // Disable interrupts by incrementing the counter
286 void disable_interrupts() {
287 // create a assembler label before
288 // marked as clobber all to avoid movement
289 __cfaasm_label(dsable, before);
290
291 with( __cfaabi_tls.preemption_state ) {
292 #if GCC_VERSION > 50000
293 static_assert(__atomic_always_lock_free(sizeof(enabled), &enabled), "Must be lock-free");
294 #endif
295
296 // Set enabled flag to false
297 // should be atomic to avoid preemption in the middle of the operation.
298 // use memory order RELAXED since there is no inter-thread on this variable requirements
299 __atomic_store_n(&enabled, false, __ATOMIC_RELAXED);
300
301 // Signal the compiler that a fence is needed but only for signal handlers
302 __atomic_signal_fence(__ATOMIC_ACQUIRE);
303
304 __attribute__((unused)) unsigned short new_val = disable_count + 1;
305 disable_count = new_val;
306 verify( new_val < 65_000u ); // If this triggers someone is disabling interrupts without enabling them
307 }
308
309 // create a assembler label after
310 // marked as clobber all to avoid movement
311 __cfaasm_label(dsable, after);
312
313 }
314
315 // Enable interrupts by decrementing the counter
316 // If counter reaches 0, execute any pending __cfactx_switch
317 void enable_interrupts( __cfaabi_dbg_ctx_param ) {
318 // Cache the processor now since interrupts can start happening after the atomic store
319 processor * proc = __cfaabi_tls.this_processor;
320 /* paranoid */ verify( proc );
321
322 with( __cfaabi_tls.preemption_state ){
323 unsigned short prev = disable_count;
324 disable_count -= 1;
325
326 // If this triggers someone is enabled already enabled interruptsverify( prev != 0u );
327 /* paranoid */ verify( prev != 0u );
328
329 // Check if we need to prempt the thread because an interrupt was missed
330 if( prev == 1 ) {
331 #if GCC_VERSION > 50000
332 static_assert(__atomic_always_lock_free(sizeof(enabled), &enabled), "Must be lock-free");
333 #endif
334
335 // Set enabled flag to true
336 // should be atomic to avoid preemption in the middle of the operation.
337 // use memory order RELAXED since there is no inter-thread on this variable requirements
338 __atomic_store_n(&enabled, true, __ATOMIC_RELAXED);
339
340 // Signal the compiler that a fence is needed but only for signal handlers
341 __atomic_signal_fence(__ATOMIC_RELEASE);
342 if( proc->pending_preemption ) {
343 proc->pending_preemption = false;
344 force_yield( __POLL_PREEMPTION );
345 }
346 }
347 }
348
349 // For debugging purposes : keep track of the last person to enable the interrupts
350 __cfaabi_dbg_debug_do( proc->last_enable = caller; )
351 }
352
353 // Disable interrupts by incrementint the counter
354 // Don't execute any pending __cfactx_switch even if counter reaches 0
355 void enable_interrupts_noPoll() {
356 unsigned short prev = __cfaabi_tls.preemption_state.disable_count;
357 __cfaabi_tls.preemption_state.disable_count -= 1;
358 // If this triggers someone is enabled already enabled interrupts
359 /* paranoid */ verifyf( prev != 0u, "Incremented from %u\n", prev );
360 if( prev == 1 ) {
361 #if GCC_VERSION > 50000
362 static_assert(__atomic_always_lock_free(sizeof(__cfaabi_tls.preemption_state.enabled), &__cfaabi_tls.preemption_state.enabled), "Must be lock-free");
363 #endif
364 // Set enabled flag to true
365 // should be atomic to avoid preemption in the middle of the operation.
366 // use memory order RELAXED since there is no inter-thread on this variable requirements
367 __atomic_store_n(&__cfaabi_tls.preemption_state.enabled, true, __ATOMIC_RELAXED);
368
369 // Signal the compiler that a fence is needed but only for signal handlers
370 __atomic_signal_fence(__ATOMIC_RELEASE);
371 }
372 }
373}
374
375//-----------------------------------------------------------------------------
376// Kernel Signal Debug
377void __cfaabi_check_preemption() {
378 bool ready = __preemption_enabled();
379 if(!ready) { abort("Preemption should be ready"); }
380
381 __cfaasm_label(debug, before);
382
383 sigset_t oldset;
384 int ret;
385 ret = pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset); // workaround trac#208: cast should be unnecessary
386 if(ret != 0) { abort("ERROR sigprocmask returned %d", ret); }
387
388 ret = sigismember(&oldset, SIGUSR1);
389 if(ret < 0) { abort("ERROR sigismember returned %d", ret); }
390 if(ret == 1) { abort("ERROR SIGUSR1 is disabled"); }
391
392 ret = sigismember(&oldset, SIGALRM);
393 if(ret < 0) { abort("ERROR sigismember returned %d", ret); }
394 if(ret == 0) { abort("ERROR SIGALRM is enabled"); }
395
396 ret = sigismember(&oldset, SIGTERM);
397 if(ret < 0) { abort("ERROR sigismember returned %d", ret); }
398 if(ret == 1) { abort("ERROR SIGTERM is disabled"); }
399
400 __cfaasm_label(debug, after);
401}
402
403#ifdef __CFA_WITH_VERIFY__
404bool __cfaabi_dbg_in_kernel() {
405 return !__preemption_enabled();
406}
407#endif
408
409#undef __cfaasm_label
410
411//-----------------------------------------------------------------------------
412// Signal handling
413
414// sigprocmask wrapper : unblock a single signal
415static inline void signal_unblock( int sig ) {
416 sigset_t mask;
417 sigemptyset( &mask );
418 sigaddset( &mask, sig );
419
420 if ( pthread_sigmask( SIG_UNBLOCK, &mask, 0p ) == -1 ) {
421 abort( "internal error, pthread_sigmask" );
422 }
423}
424
425// sigprocmask wrapper : block a single signal
426static inline void signal_block( int sig ) {
427 sigset_t mask;
428 sigemptyset( &mask );
429 sigaddset( &mask, sig );
430
431 if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {
432 abort( "internal error, pthread_sigmask" );
433 }
434}
435
436// kill wrapper : signal a processor
437static void preempt( processor * this ) {
438 sigval_t value = { PREEMPT_NORMAL };
439 pthread_sigqueue( this->kernel_thread, SIGUSR1, value );
440}
441
442// reserved for future use
443static void timeout( $thread * this ) {
444 unpark( this );
445}
446
447void __disable_interrupts_hard() {
448 sigset_t oldset;
449 int ret;
450 ret = pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset); // workaround trac#208: cast should be unnecessary
451 if(ret != 0) { abort("ERROR sigprocmask returned %d", ret); }
452
453 ret = sigismember(&oldset, SIGUSR1);
454 if(ret < 0) { abort("ERROR sigismember returned %d", ret); }
455 if(ret == 1) { abort("ERROR SIGUSR1 is disabled"); }
456
457 ret = sigismember(&oldset, SIGALRM);
458 if(ret < 0) { abort("ERROR sigismember returned %d", ret); }
459 if(ret == 0) { abort("ERROR SIGALRM is enabled"); }
460
461 signal_block( SIGUSR1 );
462}
463
464void __enable_interrupts_hard() {
465 signal_unblock( SIGUSR1 );
466
467 sigset_t oldset;
468 int ret;
469 ret = pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset); // workaround trac#208: cast should be unnecessary
470 if(ret != 0) { abort("ERROR sigprocmask returned %d", ret); }
471
472 ret = sigismember(&oldset, SIGUSR1);
473 if(ret < 0) { abort("ERROR sigismember returned %d", ret); }
474 if(ret == 1) { abort("ERROR SIGUSR1 is disabled"); }
475
476 ret = sigismember(&oldset, SIGALRM);
477 if(ret < 0) { abort("ERROR sigismember returned %d", ret); }
478 if(ret == 0) { abort("ERROR SIGALRM is enabled"); }
479}
480
481//-----------------------------------------------------------------------------
482// Some assembly required
483#if defined( __i386 )
484 #ifdef __PIC__
485 #define RELOC_PRELUDE( label ) \
486 "calll .Lcfaasm_prelude_" #label "$pb\n\t" \
487 ".Lcfaasm_prelude_" #label "$pb:\n\t" \
488 "popl %%eax\n\t" \
489 ".Lcfaasm_prelude_" #label "_end:\n\t" \
490 "addl $_GLOBAL_OFFSET_TABLE_+(.Lcfaasm_prelude_" #label "_end-.Lcfaasm_prelude_" #label "$pb), %%eax\n\t"
491 #define RELOC_PREFIX ""
492 #define RELOC_SUFFIX "@GOT(%%eax)"
493 #else
494 #define RELOC_PREFIX "$"
495 #define RELOC_SUFFIX ""
496 #endif
497 #define __cfaasm_label( label ) struct asm_region label = \
498 ({ \
499 struct asm_region region; \
500 asm( \
501 RELOC_PRELUDE( label ) \
502 "movl " RELOC_PREFIX "__cfaasm_" #label "_before" RELOC_SUFFIX ", %[vb]\n\t" \
503 "movl " RELOC_PREFIX "__cfaasm_" #label "_after" RELOC_SUFFIX ", %[va]\n\t" \
504 : [vb]"=r"(region.before), [va]"=r"(region.after) \
505 ); \
506 region; \
507 });
508#elif defined( __x86_64 )
509 #ifdef __PIC__
510 #define RELOC_PREFIX ""
511 #define RELOC_SUFFIX "@GOTPCREL(%%rip)"
512 #else
513 #define RELOC_PREFIX "$"
514 #define RELOC_SUFFIX ""
515 #endif
516 #define __cfaasm_label( label ) struct asm_region label = \
517 ({ \
518 struct asm_region region; \
519 asm( \
520 "movq " RELOC_PREFIX "__cfaasm_" #label "_before" RELOC_SUFFIX ", %[vb]\n\t" \
521 "movq " RELOC_PREFIX "__cfaasm_" #label "_after" RELOC_SUFFIX ", %[va]\n\t" \
522 : [vb]"=r"(region.before), [va]"=r"(region.after) \
523 ); \
524 region; \
525 });
526#elif defined( __aarch64__ )
527 #ifdef __PIC__
528 // Note that this works only for gcc
529 #define __cfaasm_label( label ) struct asm_region label = \
530 ({ \
531 struct asm_region region; \
532 asm( \
533 "adrp %[vb], _GLOBAL_OFFSET_TABLE_" "\n\t" \
534 "ldr %[vb], [%[vb], #:gotpage_lo15:__cfaasm_" #label "_before]" "\n\t" \
535 "adrp %[va], _GLOBAL_OFFSET_TABLE_" "\n\t" \
536 "ldr %[va], [%[va], #:gotpage_lo15:__cfaasm_" #label "_after]" "\n\t" \
537 : [vb]"=r"(region.before), [va]"=r"(region.after) \
538 ); \
539 region; \
540 });
541 #else
542 #error this is not the right thing to do
543 /*
544 #define __cfaasm_label( label ) struct asm_region label = \
545 ({ \
546 struct asm_region region; \
547 asm( \
548 "adrp %[vb], __cfaasm_" #label "_before" "\n\t" \
549 "add %[vb], %[vb], :lo12:__cfaasm_" #label "_before" "\n\t" \
550 "adrp %[va], :got:__cfaasm_" #label "_after" "\n\t" \
551 "add %[va], %[va], :lo12:__cfaasm_" #label "_after" "\n\t" \
552 : [vb]"=r"(region.before), [va]"=r"(region.after) \
553 ); \
554 region; \
555 });
556 */
557 #endif
558#else
559 #error unknown hardware architecture
560#endif
561
562// KERNEL ONLY
563// Check if a __cfactx_switch signal handler shoud defer
564// If true : preemption is safe
565// If false : preemption is unsafe and marked as pending
566static inline bool preemption_ready( void * ip ) {
567 // Get all the region for which it is not safe to preempt
568 __cfaasm_label( get );
569 __cfaasm_label( check );
570 __cfaasm_label( dsable );
571 __cfaasm_label( debug );
572
573 // Check if preemption is safe
574 bool ready = true;
575 if( __cfaasm_in( ip, get ) ) { ready = false; goto EXIT; };
576 if( __cfaasm_in( ip, check ) ) { ready = false; goto EXIT; };
577 if( __cfaasm_in( ip, dsable ) ) { ready = false; goto EXIT; };
578 if( __cfaasm_in( ip, debug ) ) { ready = false; goto EXIT; };
579 if( !__cfaabi_tls.preemption_state.enabled) { ready = false; goto EXIT; };
580 if( __cfaabi_tls.preemption_state.in_progress ) { ready = false; goto EXIT; };
581
582EXIT:
583 // Adjust the pending flag accordingly
584 __cfaabi_tls.this_processor->pending_preemption = !ready;
585 return ready;
586}
587
588//=============================================================================================
589// Kernel Signal Startup/Shutdown logic
590//=============================================================================================
591
592// Startup routine to activate preemption
593// Called from kernel_startup
594void __kernel_alarm_startup() {
595 __cfaabi_dbg_print_safe( "Kernel : Starting preemption\n" );
596
597 // Start with preemption disabled until ready
598 __cfaabi_tls.preemption_state.enabled = false;
599 __cfaabi_tls.preemption_state.disable_count = 1;
600
601 // Initialize the event kernel
602 event_kernel = (event_kernel_t *)&storage_event_kernel;
603 (*event_kernel){};
604
605 // Setup proper signal handlers
606 __cfaabi_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO ); // __cfactx_switch handler
607 __cfaabi_sigaction( SIGALRM, sigHandler_alarm , SA_SIGINFO ); // debug handler
608
609 signal_block( SIGALRM );
610
611 alarm_stack = __create_pthread( &alarm_thread, alarm_loop, 0p );
612}
613
614// Shutdown routine to deactivate preemption
615// Called from kernel_shutdown
616void __kernel_alarm_shutdown() {
617 __cfaabi_dbg_print_safe( "Kernel : Preemption stopping\n" );
618
619 // Block all signals since we are already shutting down
620 sigset_t mask;
621 sigfillset( &mask );
622 sigprocmask( SIG_BLOCK, &mask, 0p );
623
624 // Notify the alarm thread of the shutdown
625 sigval val = { 1 };
626 pthread_sigqueue( alarm_thread, SIGALRM, val );
627
628 // Wait for the preemption thread to finish
629
630 __destroy_pthread( alarm_thread, alarm_stack, 0p );
631
632 // Preemption is now fully stopped
633
634 __cfaabi_dbg_print_safe( "Kernel : Preemption stopped\n" );
635}
636
637// Prevent preemption since we are about to start terminating things
638void __kernel_abort_lock(void) {
639 signal_block( SIGUSR1 );
640}
641
642// Raii ctor/dtor for the preemption_scope
643// Used by thread to control when they want to receive preemption signals
644void ?{}( preemption_scope & this, processor * proc ) {
645 (this.alarm){ proc, (Time){ 0 }, 0`s };
646 this.proc = proc;
647 this.proc->preemption_alarm = &this.alarm;
648
649 update_preemption( this.proc, this.proc->cltr->preemption_rate );
650}
651
652void ^?{}( preemption_scope & this ) {
653 disable_interrupts();
654
655 update_preemption( this.proc, 0`s );
656}
657
658//=============================================================================================
659// Kernel Signal Handlers
660//=============================================================================================
661__cfaabi_dbg_debug_do( static thread_local void * last_interrupt = 0; )
662
663// Context switch signal handler
664// Receives SIGUSR1 signal and causes the current thread to yield
665static void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) {
666 void * ip = (void *)(cxt->uc_mcontext.CFA_REG_IP);
667 __cfaabi_dbg_debug_do( last_interrupt = ip; )
668
669 // SKULLDUGGERY: if a thread creates a processor and the immediately deletes it,
670 // the interrupt that is supposed to force the kernel thread to preempt might arrive
671 // before the kernel thread has even started running. When that happens, an interrupt
672 // with a null 'this_processor' will be caught, just ignore it.
673 if(! __cfaabi_tls.this_processor ) return;
674
675 choose(sfp->si_value.sival_int) {
676 case PREEMPT_NORMAL : ;// Normal case, nothing to do here
677 case PREEMPT_TERMINATE: verify( __atomic_load_n( &__cfaabi_tls.this_processor->do_terminate, __ATOMIC_SEQ_CST ) );
678 default:
679 abort( "internal error, signal value is %d", sfp->si_value.sival_int );
680 }
681
682 // Check if it is safe to preempt here
683 if( !preemption_ready( ip ) ) { return; }
684
685 __cfaabi_dbg_print_buffer_decl( " KERNEL: preempting core %p (%p @ %p).\n", __cfaabi_tls.this_processor, __cfaabi_tls.this_thread, (void *)(cxt->uc_mcontext.CFA_REG_IP) );
686
687 // Sync flag : prevent recursive calls to the signal handler
688 __cfaabi_tls.preemption_state.in_progress = true;
689
690 // Clear sighandler mask before context switching.
691 #if GCC_VERSION > 50000
692 static_assert( sizeof( sigset_t ) == sizeof( cxt->uc_sigmask ), "Expected cxt->uc_sigmask to be of sigset_t" );
693 #endif
694 if ( pthread_sigmask( SIG_SETMASK, (sigset_t *)&(cxt->uc_sigmask), 0p ) == -1 ) {
695 abort( "internal error, sigprocmask" );
696 }
697
698 // Clear the in progress flag
699 __cfaabi_tls.preemption_state.in_progress = false;
700
701 // Preemption can occur here
702
703 force_yield( __ALARM_PREEMPTION ); // Do the actual __cfactx_switch
704}
705
706static void sigHandler_alarm( __CFA_SIGPARMS__ ) {
707 abort("SIGALRM should never reach the signal handler");
708}
709
710// Main of the alarm thread
711// Waits on SIGALRM and send SIGUSR1 to whom ever needs it
712static void * alarm_loop( __attribute__((unused)) void * args ) {
713 __processor_id_t id;
714 register_proc_id(&id);
715 __cfaabi_tls.this_proc_id = &id;
716
717
718 // Block sigalrms to control when they arrive
719 sigset_t mask;
720 sigfillset(&mask);
721 if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {
722 abort( "internal error, pthread_sigmask" );
723 }
724
725 sigemptyset( &mask );
726 sigaddset( &mask, SIGALRM );
727
728 // Main loop
729 while( true ) {
730 // Wait for a sigalrm
731 siginfo_t info;
732 int sig = sigwaitinfo( &mask, &info );
733
734 if( sig < 0 ) {
735 //Error!
736 int err = errno;
737 switch( err ) {
738 case EAGAIN :
739 case EINTR :
740 {__cfaabi_dbg_print_buffer_decl( " KERNEL: Spurious wakeup %d.\n", err );}
741 continue;
742 case EINVAL :
743 abort( "Timeout was invalid." );
744 default:
745 abort( "Unhandled error %d", err);
746 }
747 }
748
749 // If another signal arrived something went wrong
750 assertf(sig == SIGALRM, "Kernel Internal Error, sigwait: Unexpected signal %d (%d : %d)\n", sig, info.si_code, info.si_value.sival_int);
751
752 // __cfaabi_dbg_print_safe( "Kernel : Caught alarm from %d with %d\n", info.si_code, info.si_value.sival_int );
753 // Switch on the code (a.k.a. the sender) to
754 switch( info.si_code )
755 {
756 // Timers can apparently be marked as sent for the kernel
757 // In either case, tick preemption
758 case SI_TIMER:
759 case SI_KERNEL:
760 // __cfaabi_dbg_print_safe( "Kernel : Preemption thread tick\n" );
761 lock( event_kernel->lock __cfaabi_dbg_ctx2 );
762 tick_preemption();
763 unlock( event_kernel->lock );
764 break;
765 // Signal was not sent by the kernel but by an other thread
766 case SI_QUEUE:
767 // For now, other thread only signal the alarm thread to shut it down
768 // If this needs to change use info.si_value and handle the case here
769 goto EXIT;
770 }
771 }
772
773EXIT:
774 __cfaabi_dbg_print_safe( "Kernel : Preemption thread stopping\n" );
775 register_proc_id(&id);
776
777 return 0p;
778}
779
780// Local Variables: //
781// mode: c //
782// tab-width: 4 //
783// End: //
Note: See TracBrowser for help on using the repository browser.