This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathshim_signal.c
820 lines (684 loc) · 26 KB
/
shim_signal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/* Copyright (C) 2014 Stony Brook University
This file is part of Graphene Library OS.
Graphene Library OS is free software: you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Graphene Library OS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/*
* shim_signal.c
*
* This file contains codes to handle signals and exceptions passed from PAL.
*/
#include <shim_internal.h>
#include <shim_utils.h>
#include <shim_table.h>
#include <shim_thread.h>
#include <shim_handle.h>
#include <shim_vma.h>
#include <shim_checkpoint.h>
#include <shim_signal.h>
#include <shim_unistd.h>
#include <pal.h>
static struct shim_signal **
allocate_signal_log (struct shim_thread * thread, int sig)
{
if (!thread->signal_logs)
return NULL;
struct shim_signal_log * log = &thread->signal_logs[sig - 1];
int head, tail, old_tail;
do {
head = atomic_read(&log->head);
old_tail = tail = atomic_read(&log->tail);
if (head == tail + 1 || (!head && tail == (MAX_SIGNAL_LOG - 1)))
return NULL;
tail = (tail == MAX_SIGNAL_LOG - 1) ? 0 : tail + 1;
} while (atomic_cmpxchg(&log->tail, old_tail, tail) == tail);
debug("signal_logs[%d]: head=%d, tail=%d (counter = %ld)\n", sig - 1,
head, tail, thread->has_signal.counter + 1);
atomic_inc(&thread->has_signal);
return &log->logs[old_tail];
}
static struct shim_signal *
fetch_signal_log (struct shim_thread * thread, int sig)
{
struct shim_signal_log * log = &thread->signal_logs[sig - 1];
struct shim_signal * signal = NULL;
int head, tail, old_head;
while (1) {
old_head = head = atomic_read(&log->head);
tail = atomic_read(&log->tail);
if (head == tail)
return NULL;
if (!(signal = log->logs[head]))
return NULL;
log->logs[head] = NULL;
head = (head == MAX_SIGNAL_LOG - 1) ? 0 : head + 1;
if (atomic_cmpxchg(&log->head, old_head, head) == old_head)
break;
log->logs[old_head] = signal;
}
debug("signal_logs[%d]: head=%d, tail=%d\n", sig -1, head, tail);
atomic_dec(&thread->has_signal);
return signal;
}
static void
__handle_one_signal (shim_tcb_t * tcb, int sig, struct shim_signal * signal);
static void __store_info (siginfo_t * info, struct shim_signal * signal)
{
if (info)
memcpy(&signal->info, info, sizeof(siginfo_t));
}
void __store_context (shim_tcb_t * tcb, PAL_CONTEXT * pal_context,
struct shim_signal * signal)
{
ucontext_t * context = &signal->context;
if (tcb && tcb->context.regs && tcb->context.regs->orig_rax) {
struct shim_context * ct = &tcb->context;
if (ct->regs) {
struct shim_regs * regs = ct->regs;
context->uc_mcontext.gregs[REG_RIP] = regs->rip;
context->uc_mcontext.gregs[REG_EFL] = regs->rflags;
context->uc_mcontext.gregs[REG_R15] = regs->r15;
context->uc_mcontext.gregs[REG_R14] = regs->r14;
context->uc_mcontext.gregs[REG_R13] = regs->r13;
context->uc_mcontext.gregs[REG_R12] = regs->r12;
context->uc_mcontext.gregs[REG_R11] = regs->r11;
context->uc_mcontext.gregs[REG_R10] = regs->r10;
context->uc_mcontext.gregs[REG_R9] = regs->r9;
context->uc_mcontext.gregs[REG_R8] = regs->r8;
context->uc_mcontext.gregs[REG_RCX] = regs->rcx;
context->uc_mcontext.gregs[REG_RDX] = regs->rdx;
context->uc_mcontext.gregs[REG_RSI] = regs->rsi;
context->uc_mcontext.gregs[REG_RDI] = regs->rdi;
context->uc_mcontext.gregs[REG_RBX] = regs->rbx;
context->uc_mcontext.gregs[REG_RBP] = regs->rbp;
context->uc_mcontext.gregs[REG_RSP] = regs->rsp;
}
signal->context_stored = true;
return;
}
if (pal_context) {
memcpy(context->uc_mcontext.gregs, pal_context, sizeof(PAL_CONTEXT));
signal->context_stored = true;
}
}
void deliver_signal (siginfo_t * info, PAL_CONTEXT * context)
{
shim_tcb_t * tcb = shim_get_tls();
assert(tcb);
// Signals should not be delivered before the user process starts
// or after the user process dies.
if (!tcb->tp || !cur_thread_is_alive())
return;
struct shim_thread * cur_thread = (struct shim_thread *) tcb->tp;
int sig = info->si_signo;
__disable_preempt(tcb);
struct shim_signal * signal = __alloca(sizeof(struct shim_signal));
/* save in signal */
memset(signal, 0, sizeof(struct shim_signal));
__store_info(info, signal);
__store_context(tcb, context, signal);
signal->pal_context = context;
if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1 ||
__sigismember(&cur_thread->signal_mask, sig)) {
struct shim_signal ** signal_log = NULL;
if ((signal = malloc_copy(signal,sizeof(struct shim_signal))) &&
(signal_log = allocate_signal_log(cur_thread, sig))) {
*signal_log = signal;
}
if (signal && !signal_log) {
SYS_PRINTF("signal queue is full (TID = %u, SIG = %d)\n",
tcb->tid, sig);
free(signal);
}
} else {
__handle_signal(tcb, sig);
__handle_one_signal(tcb, sig, signal);
}
__enable_preempt(tcb);
}
#define ALLOC_SIGINFO(signo, code, member, value) \
({ \
siginfo_t * _info = __alloca(sizeof(siginfo_t)); \
memset(_info, 0, sizeof(siginfo_t)); \
_info->si_signo = (signo); \
_info->si_code = (code); \
_info->member = (value); \
_info; \
})
#ifdef __x86_64__
#define IP rip
#else
#define IP eip
#endif
static inline bool context_is_internal(PAL_CONTEXT * context)
{
return context &&
(void *) context->IP >= (void *) &__code_address &&
(void *) context->IP < (void *) &__code_address_end;
}
static inline void internal_fault(const char* errstr,
PAL_NUM addr, PAL_CONTEXT * context)
{
IDTYPE tid = get_cur_tid();
if (context_is_internal(context))
SYS_PRINTF("%s at 0x%08lx (IP = +0x%lx, VMID = %u, TID = %u)\n", errstr,
addr, (void *) context->IP - (void *) &__load_address,
cur_process.vmid, is_internal_tid(tid) ? 0 : tid);
else
SYS_PRINTF("%s at 0x%08lx (IP = 0x%08lx, VMID = %u, TID = %u)\n", errstr,
addr, context ? context->IP : 0,
cur_process.vmid, is_internal_tid(tid) ? 0 : tid);
PAUSE();
}
static void arithmetic_error_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
{
if (is_internal_tid(get_cur_tid()) || context_is_internal(context)) {
internal_fault("Internal arithmetic fault", arg, context);
} else {
if (context)
debug("arithmetic fault at 0x%08lx\n", context->IP);
deliver_signal(ALLOC_SIGINFO(SIGFPE, FPE_INTDIV,
si_addr, (void *) arg), context);
}
DkExceptionReturn(event);
}
static void memfault_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
{
shim_tcb_t * tcb = shim_get_tls();
assert(tcb);
if (tcb->test_range.cont_addr && arg
&& (void *) arg >= tcb->test_range.start
&& (void *) arg <= tcb->test_range.end) {
assert(context);
context->rip = (PAL_NUM) tcb->test_range.cont_addr;
goto ret_exception;
}
if (is_internal_tid(get_cur_tid()) || context_is_internal(context)) {
internal_fault("Internal memory fault", arg, context);
goto ret_exception;
}
if (context)
debug("memory fault at 0x%08lx (IP = 0x%08lx)\n", arg, context->IP);
struct shim_vma_val vma;
int signo = SIGSEGV;
int code;
if (!arg) {
code = SEGV_MAPERR;
} else if (!lookup_vma((void *) arg, &vma)) {
if (vma.flags & VMA_INTERNAL) {
internal_fault("Internal memory fault with VMA", arg, context);
goto ret_exception;
}
if (vma.file && vma.file->type == TYPE_FILE) {
/* DEP 3/3/17: If the mapping exceeds end of a file (but is in the VMA)
* then return a SIGBUS. */
uintptr_t eof_in_vma = (uintptr_t) vma.addr + vma.offset + vma.file->info.file.size;
if (arg > eof_in_vma) {
signo = SIGBUS;
code = BUS_ADRERR;
} else if ((context->err & 4) && !(vma.flags & PROT_WRITE)) {
/* DEP 3/3/17: If the page fault gives a write error, and
* the VMA is read-only, return SIGSEGV+SEGV_ACCERR */
signo = SIGSEGV;
code = SEGV_ACCERR;
} else {
/* XXX: need more sophisticated judgement */
signo = SIGBUS;
code = BUS_ADRERR;
}
} else {
code = SEGV_ACCERR;
}
} else {
code = SEGV_MAPERR;
}
deliver_signal(ALLOC_SIGINFO(signo, code, si_addr, (void *) arg), context);
ret_exception:
DkExceptionReturn(event);
}
/*
* Helper function for test_user_memory / test_user_string; they behave
* differently for different PALs:
*
* - For Linux-SGX, the faulting address is not propagated in memfault
* exception (SGX v1 does not write address in SSA frame, SGX v2 writes
* it only at a granularity of 4K pages). Thus, we cannot rely on
* exception handling to compare against tcb.test_range.start/end.
* Instead, traverse VMAs to see if [addr, addr+size) is addressable;
* before traversing VMAs, grab a VMA lock.
*
* - For other PALs, we touch one byte of each page in [addr, addr+size).
* If some byte is not addressable, exception is raised. memfault_upcall
* handles this exception and resumes execution from ret_fault.
*
* The second option is faster in fault-free case but cannot be used under
* SGX PAL. We use the best option for each PAL for now. */
static bool is_sgx_pal(void) {
static struct atomic_int sgx_pal = { .counter = 0 };
static struct atomic_int inited = { .counter = 0 };
if (!atomic_read(&inited)) {
/* Ensure that is_sgx_pal is updated before initialized */
atomic_set(&sgx_pal, strcmp_static(PAL_CB(host_type), "Linux-SGX"));
MB();
atomic_set(&inited, 1);
}
MB();
return atomic_read(&sgx_pal) != 0;
}
/*
* 'test_user_memory' and 'test_user_string' are helper functions for testing
* if a user-given buffer or data structure is readable / writable (according
* to the system call semantics). If the memory test fails, the system call
* should return -EFAULT or -EINVAL accordingly. These helper functions cannot
* guarantee further corruption of the buffer, or if the buffer is unmapped
* with a concurrent system call. The purpose of these functions is simply for
* the compatibility with programs that rely on the error numbers, such as the
* LTP test suite. */
bool test_user_memory (void * addr, size_t size, bool write)
{
if (!size)
return false;
if (!access_ok(addr, size))
return true;
/* SGX path: check if [addr, addr+size) is addressable (in some VMA) */
if (is_sgx_pal())
return !is_in_adjacent_vmas(addr, size);
/* Non-SGX path: check if [addr, addr+size) is addressable by touching
* a byte of each page; invalid access will be caught in memfault_upcall */
shim_tcb_t * tcb = shim_get_tls();
assert(tcb && tcb->tp);
__disable_preempt(tcb);
bool has_fault = true;
/* Add the memory region to the watch list. This is not racy because
* each thread has its own record. */
assert(!tcb->test_range.cont_addr);
tcb->test_range.cont_addr = &&ret_fault;
tcb->test_range.start = addr;
tcb->test_range.end = addr + size - 1;
/* Try to read or write into one byte inside each page */
void * tmp = addr;
while (tmp <= addr + size - 1) {
if (write) {
*(volatile char *) tmp = *(volatile char *) tmp;
} else {
*(volatile char *) tmp;
}
tmp = ALIGN_UP(tmp + 1);
}
has_fault = false; /* All accesses have passed. Nothing wrong. */
ret_fault:
/* If any read or write into the target region causes an exception,
* the control flow will immediately jump to here. */
tcb->test_range.cont_addr = NULL;
tcb->test_range.start = tcb->test_range.end = NULL;
__enable_preempt(tcb);
return has_fault;
}
/*
* This function tests a user string with unknown length. It only tests
* whether the memory is readable.
*/
bool test_user_string (const char * addr)
{
if (!access_ok(addr, 1))
return true;
size_t size, maxlen;
const char * next = ALIGN_UP(addr + 1);
/* SGX path: check if [addr, addr+size) is addressable (in some VMA). */
if (is_sgx_pal()) {
/* We don't know length but using unprotected strlen() is dangerous
* so we check string in chunks of 4K pages. */
do {
maxlen = next - addr;
if (!access_ok(addr, maxlen) || !is_in_adjacent_vmas((void*) addr, maxlen))
return true;
size = strnlen(addr, maxlen);
addr = next;
next = ALIGN_UP(addr + 1);
} while (size == maxlen);
return false;
}
/* Non-SGX path: check if [addr, addr+size) is addressable by touching
* a byte of each page; invalid access will be caught in memfault_upcall. */
shim_tcb_t * tcb = shim_get_tls();
assert(tcb && tcb->tp);
__disable_preempt(tcb);
bool has_fault = true;
assert(!tcb->test_range.cont_addr);
tcb->test_range.cont_addr = &&ret_fault;
do {
/* Add the memory region to the watch list. This is not racy because
* each thread has its own record. */
tcb->test_range.start = (void *) addr;
tcb->test_range.end = (void *) (next - 1);
maxlen = next - addr;
if (!access_ok(addr, maxlen))
return true;
*(volatile char *) addr; /* try to read one byte from the page */
size = strnlen(addr, maxlen);
addr = next;
next = ALIGN_UP(addr + 1);
} while (size == maxlen);
has_fault = false; /* All accesses have passed. Nothing wrong. */
ret_fault:
/* If any read or write into the target region causes an exception,
* the control flow will immediately jump to here. */
tcb->test_range.cont_addr = NULL;
tcb->test_range.start = tcb->test_range.end = NULL;
__enable_preempt(tcb);
return has_fault;
}
void __attribute__((weak)) syscall_wrapper(void)
{
/*
* work around for link.
* syscalldb.S is excluded for libsysdb_debug.so so it fails to link
* due to missing syscall_wrapper.
*/
}
static void illegal_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
{
struct shim_vma_val vma;
if (!is_internal_tid(get_cur_tid()) &&
!context_is_internal(context) &&
!(lookup_vma((void *) arg, &vma)) &&
!(vma.flags & VMA_INTERNAL)) {
if (context)
debug("illegal instruction at 0x%08lx\n", context->IP);
uint8_t * rip = (uint8_t*)context->IP;
/*
* Emulate syscall instruction (opcode 0x0f 0x05);
* syscall instruction is prohibited in
* Linux-SGX PAL and raises a SIGILL exception and
* Linux PAL with seccomp and raise SIGSYS exception.
*/
#if 0
if (rip[-2] == 0x0f && rip[-1] == 0x05) {
/* TODO: once finished, remove "#if 0" above. */
/*
* SIGSYS case (can happen with Linux PAL with seccomp)
* rip points to the address after syscall instruction
* %rcx: syscall instruction must put an
* instruction-after-syscall in rcx
*/
context->rax = siginfo->si_syscall; /* PAL_CONTEXT doesn't
* include a member
* corresponding to
* siginfo_t::si_syscall yet.
*/
context->rcx = (long)rip;
context->r11 = context->efl;
context->rip = (long)&syscall_wrapper;
} else
#endif
if (rip[0] == 0x0f && rip[1] == 0x05) {
/*
* SIGILL case (can happen in Linux-SGX PAL)
* %rcx: syscall instruction must put an instruction-after-syscall
* in rcx. See the syscall_wrapper in syscallas.S
* TODO: check SIGILL and ILL_ILLOPN
*/
context->rcx = (long)rip + 2;
context->r11 = context->efl;
context->rip = (long)&syscall_wrapper;
} else {
deliver_signal(ALLOC_SIGINFO(SIGILL, ILL_ILLOPC,
si_addr, (void *) arg), context);
}
} else {
internal_fault("Internal illegal fault", arg, context);
}
DkExceptionReturn(event);
}
static void quit_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
{
__UNUSED(arg);
__UNUSED(context);
if (!is_internal_tid(get_cur_tid())) {
deliver_signal(ALLOC_SIGINFO(SIGTERM, SI_USER, si_pid, 0), NULL);
}
DkExceptionReturn(event);
}
static void suspend_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
{
__UNUSED(arg);
__UNUSED(context);
if (!is_internal_tid(get_cur_tid())) {
deliver_signal(ALLOC_SIGINFO(SIGINT, SI_USER, si_pid, 0), NULL);
}
DkExceptionReturn(event);
}
static void resume_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
{
__UNUSED(arg);
__UNUSED(context);
shim_tcb_t * tcb = shim_get_tls();
if (!tcb || !tcb->tp)
return;
if (!is_internal_tid(get_cur_tid())) {
__disable_preempt(tcb);
if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1) {
tcb->context.preempt |= SIGNAL_DELAYED;
} else {
__handle_signal(tcb, 0);
}
__enable_preempt(tcb);
}
DkExceptionReturn(event);
}
int init_signal (void)
{
DkSetExceptionHandler(&arithmetic_error_upcall, PAL_EVENT_ARITHMETIC_ERROR);
DkSetExceptionHandler(&memfault_upcall, PAL_EVENT_MEMFAULT);
DkSetExceptionHandler(&illegal_upcall, PAL_EVENT_ILLEGAL);
DkSetExceptionHandler(&quit_upcall, PAL_EVENT_QUIT);
DkSetExceptionHandler(&suspend_upcall, PAL_EVENT_SUSPEND);
DkSetExceptionHandler(&resume_upcall, PAL_EVENT_RESUME);
return 0;
}
__sigset_t * get_sig_mask (struct shim_thread * thread)
{
if (!thread)
thread = get_cur_thread();
assert(thread);
return &(thread->signal_mask);
}
__sigset_t * set_sig_mask (struct shim_thread * thread,
const __sigset_t * set)
{
if (!thread)
thread = get_cur_thread();
assert(thread);
if (set)
memcpy(&thread->signal_mask, set, sizeof(__sigset_t));
return &thread->signal_mask;
}
static void (*default_sighandler[NUM_SIGS]) (int, siginfo_t *, void *);
static void
__handle_one_signal (shim_tcb_t * tcb, int sig, struct shim_signal * signal)
{
struct shim_thread * thread = (struct shim_thread *) tcb->tp;
struct shim_signal_handle * sighdl = &thread->signal_handles[sig - 1];
void (*handler) (int, siginfo_t *, void *) = NULL;
if (signal->info.si_signo == SIGCP) {
join_checkpoint(thread, SI_CP_SESSION(&signal->info));
return;
}
debug("%s handled\n", signal_name(sig));
lock(&thread->lock);
if (sighdl->action) {
struct __kernel_sigaction * act = sighdl->action;
/* This is a workaround. The truth is that many program will
use sa_handler as sa_sigaction, because sa_sigaction is
not supported in amd64 */
#ifdef __i386__
handler = (void (*) (int, siginfo_t *, void *)) act->_u._sa_handler;
if (act->sa_flags & SA_SIGINFO)
sa_handler = act->_u._sa_sigaction;
#else
handler = (void (*) (int, siginfo_t *, void *)) act->k_sa_handler;
#endif
if (act->sa_flags & SA_RESETHAND) {
sighdl->action = NULL;
free(act);
}
}
unlock(&thread->lock);
if ((void *) handler == (void *) 1) /* SIG_IGN */
return;
if (!handler && !(handler = default_sighandler[sig - 1]))
return;
/* if the context is never stored in the signal, it means the
signal is handled during system calls, and before the thread
is resumed. */
if (!signal->context_stored)
__store_context(tcb, NULL, signal);
struct shim_context * context = NULL;
if (tcb->context.regs && tcb->context.regs->orig_rax) {
context = __alloca(sizeof(struct shim_context));
memcpy(context, &tcb->context, sizeof(struct shim_context));
tcb->context.regs->orig_rax = 0;
tcb->context.next = context;
}
debug("run signal handler %p (%d, %p, %p)\n", handler, sig, &signal->info,
&signal->context);
(*handler) (sig, &signal->info, &signal->context);
if (context)
memcpy(&tcb->context, context, sizeof(struct shim_context));
if (signal->pal_context)
memcpy(signal->pal_context, signal->context.uc_mcontext.gregs,
sizeof(PAL_CONTEXT));
}
void __handle_signal (shim_tcb_t * tcb, int sig)
{
struct shim_thread * thread = (struct shim_thread *) tcb->tp;
int begin_sig = 1, end_sig = NUM_KNOWN_SIGS;
if (sig)
end_sig = (begin_sig = sig) + 1;
sig = begin_sig;
while (atomic_read(&thread->has_signal)) {
struct shim_signal * signal = NULL;
for ( ; sig < end_sig ; sig++)
if (!__sigismember(&thread->signal_mask, sig) &&
(signal = fetch_signal_log(thread, sig)))
break;
if (!signal)
break;
if (!signal->context_stored)
__store_context(tcb, NULL, signal);
__handle_one_signal(tcb, sig, signal);
free(signal);
DkThreadYieldExecution();
tcb->context.preempt &= ~SIGNAL_DELAYED;
}
}
void handle_signal (bool delayed_only)
{
shim_tcb_t * tcb = shim_get_tls();
assert(tcb);
struct shim_thread * thread = (struct shim_thread *) tcb->tp;
/* Fast path */
if (!thread || !thread->has_signal.counter)
return;
__disable_preempt(tcb);
if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1) {
debug("signal delayed (%ld)\n", tcb->context.preempt & ~SIGNAL_DELAYED);
tcb->context.preempt |= SIGNAL_DELAYED;
} else if (!(delayed_only && !(tcb->context.preempt & SIGNAL_DELAYED))) {
__handle_signal(tcb, 0);
}
__enable_preempt(tcb);
debug("__enable_preempt: %s:%d\n", __FILE__, __LINE__);
}
void append_signal (struct shim_thread * thread, int sig, siginfo_t * info,
bool wakeup)
{
struct shim_signal * signal = malloc(sizeof(struct shim_signal));
if (!signal)
return;
/* save in signal */
if (info) {
__store_info(info, signal);
signal->context_stored = false;
} else {
memset(signal, 0, sizeof(struct shim_signal));
}
struct shim_signal ** signal_log = allocate_signal_log(thread, sig);
if (signal_log) {
*signal_log = signal;
if (wakeup) {
debug("resuming thread %u\n", thread->tid);
thread_wakeup(thread);
DkThreadResume(thread->pal_handle);
}
} else {
SYS_PRINTF("signal queue is full (TID = %u, SIG = %d)\n",
thread->tid, sig);
free(signal);
}
}
#define __WCOREDUMP_BIT 0x80
static void sighandler_kill (int sig, siginfo_t * info, void * ucontext)
{
int sig_without_coredump_bit = sig & ~(__WCOREDUMP_BIT);
__UNUSED(ucontext);
debug("killed by %s\n", signal_name(sig_without_coredump_bit));
if (!info->si_pid)
switch(sig) {
case SIGTERM:
case SIGINT:
shim_do_kill(-1, sig_without_coredump_bit);
break;
}
try_process_exit(0, sig);
DkThreadExit();
}
static void sighandler_core (int sig, siginfo_t * info, void * ucontext)
{
/* NOTE: This implementation only indicates the core dump for wait4()
* and friends. No actual core-dump file is created. */
sig = __WCOREDUMP_BIT | sig;
sighandler_kill(sig, info, ucontext);
}
static void (*default_sighandler[NUM_SIGS]) (int, siginfo_t *, void *) =
{
/* SIGHUP */ &sighandler_kill,
/* SIGINT */ &sighandler_kill,
/* SIGQUIT */ &sighandler_core,
/* SIGILL */ &sighandler_core,
/* SIGTRAP */ &sighandler_core,
/* SIGABRT */ &sighandler_core,
/* SIGBUS */ &sighandler_core,
/* SIGFPE */ &sighandler_core,
/* SIGKILL */ &sighandler_kill,
/* SIGUSR1 */ &sighandler_kill,
/* SIGSEGV */ &sighandler_core,
/* SIGUSR2 */ &sighandler_kill,
/* SIGPIPE */ &sighandler_kill,
/* SIGALRM */ &sighandler_kill,
/* SIGTERM */ &sighandler_kill,
/* SIGSTKFLT */ NULL,
/* SIGCHLD */ NULL,
/* SIGCONT */ NULL,
/* SIGSTOP */ NULL,
/* SIGTSTP */ NULL,
/* SIGTTIN */ NULL,
/* SIGTTOU */ NULL,
/* SIGURG */ NULL,
/* SIGXCPU */ &sighandler_core,
/* SIGXFSZ */ &sighandler_core,
/* SIGVTALRM */ &sighandler_kill,
/* SIGPROF */ &sighandler_kill,
/* SIGWINCH */ NULL,
/* SIGIO */ &sighandler_kill,
/* SIGPWR */ &sighandler_kill,
/* SIGSYS */ &sighandler_core,
/* SIGRTMIN */ NULL,
};