1 This patch is needed to build e2fsprogs, util-linux.
3 Taken from https://salsa.debian.org/glibc-team/glibc/-/blob/glibc-2.31/debian/patches/hurd-i386/tg-hurdsig-SA_SIGINFO.diff
5 From: Jeremie Koenig <jk@jk.fr.eu.org>
6 Subject: [PATCH] implement SA_SIGINFO signal handlers.
8 52baaca Hurd signals: Copy bits/sigaction.h
9 4232c66 Hurd signals: SA_SIGINFO support
10 1831cfe Hurd signals: Use POSIX sigcodes
13 hurd/hurd/signal.h | 5 +
17 sysdeps/mach/hurd/bits/sigaction.h | 86 +++++++++++++++++++++
18 sysdeps/mach/hurd/i386/bits/sigcontext.h | 4
19 sysdeps/mach/hurd/i386/exc2signal.c | 123 +++++++++++++++++++-----------
20 sysdeps/mach/hurd/i386/trampoline.c | 125 ++++++++++++++++++++++++++++---
21 sysdeps/mach/hurd/kill.c | 2
22 sysdeps/mach/hurd/setitimer.c | 2
23 10 files changed, 293 insertions(+), 64 deletions(-)
25 Index: glibc-2.31/hurd/hurd/signal.h
26 ===================================================================
27 --- glibc-2.31.orig/hurd/hurd/signal.h
28 +++ glibc-2.31/hurd/hurd/signal.h
29 @@ -290,6 +290,11 @@ extern int _hurd_raise_signal (struct hu
30 extern void _hurd_exception2signal (struct hurd_signal_detail *detail,
33 +/* Translate a Mach exception into a signal with a legacy sigcode. */
35 +extern void _hurd_exception2signal_legacy (struct hurd_signal_detail *detail,
39 /* Make the thread described by SS take the signal described by SIGNO and
40 DETAIL. If the process is traced, this will in fact stop with a SIGNO
41 Index: glibc-2.31/hurd/hurdfault.c
42 ===================================================================
43 --- glibc-2.31.orig/hurd/hurdfault.c
44 +++ glibc-2.31/hurd/hurdfault.c
45 @@ -70,7 +70,7 @@ _hurdsig_fault_catch_exception_raise (ma
46 codes into a signal number and subcode. */
47 _hurd_exception2signal (&d, &signo);
49 - return HURD_PREEMPT_SIGNAL_P (&_hurdsig_fault_preemptor, signo, d.code)
50 + return HURD_PREEMPT_SIGNAL_P (&_hurdsig_fault_preemptor, signo, d.exc_subcode)
54 Index: glibc-2.31/hurd/hurdinit.c
55 ===================================================================
56 --- glibc-2.31.orig/hurd/hurdinit.c
57 +++ glibc-2.31/hurd/hurdinit.c
58 @@ -177,7 +177,7 @@ _hurd_new_proc_init (char **argv,
59 /* This process is "traced", meaning it should stop on signals or exec.
60 We are all set up now to handle signals. Stop ourselves, to inform
61 our parent (presumably a debugger) that the exec has completed. */
62 - __msg_sig_post (_hurd_msgport, SIGTRAP, 0, __mach_task_self ());
63 + __msg_sig_post (_hurd_msgport, SIGTRAP, TRAP_TRACE, __mach_task_self ());
66 #include <shlib-compat.h>
67 Index: glibc-2.31/hurd/hurdsig.c
68 ===================================================================
69 --- glibc-2.31.orig/hurd/hurdsig.c
70 +++ glibc-2.31/hurd/hurdsig.c
71 @@ -730,7 +730,7 @@ post_signal (struct hurd_sigstate *ss,
72 { /* PE cannot be null. */
75 - if (HURD_PREEMPT_SIGNAL_P (pe, signo, detail->code))
76 + if (HURD_PREEMPT_SIGNAL_P (pe, signo, detail->exc_subcode))
80 @@ -1374,7 +1374,7 @@ _S_msg_sig_post (mach_port_t me,
81 if (err = signal_allowed (signo, refport))
85 + d.code = d.exc_subcode = sigcode;
88 /* Post the signal to a global receiver thread (or mark it pending in
89 @@ -1403,7 +1403,7 @@ _S_msg_sig_post_untraced (mach_port_t me
90 if (err = signal_allowed (signo, refport))
94 + d.code = d.exc_subcode = sigcode;
97 /* Post the signal to the designated signal-receiving thread. This will
98 Index: glibc-2.31/sysdeps/mach/hurd/bits/sigaction.h
99 ===================================================================
101 +++ glibc-2.31/sysdeps/mach/hurd/bits/sigaction.h
103 +/* Copyright (C) 1991-2015 Free Software Foundation, Inc.
104 + This file is part of the GNU C Library.
106 + The GNU C Library is free software; you can redistribute it and/or
107 + modify it under the terms of the GNU Lesser General Public
108 + License as published by the Free Software Foundation; either
109 + version 2.1 of the License, or (at your option) any later version.
111 + The GNU C Library is distributed in the hope that it will be useful,
112 + but WITHOUT ANY WARRANTY; without even the implied warranty of
113 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
114 + Lesser General Public License for more details.
116 + You should have received a copy of the GNU Lesser General Public
117 + License along with the GNU C Library; if not, write to the Free
118 + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
121 +#ifndef _BITS_SIGACTION_H
122 +#define _BITS_SIGACTION_H 1
125 +# error "Never include <bits/sigaction.h> directly; use <signal.h> instead."
128 +/* These definitions match those used by the 4.4 BSD kernel.
129 + If the operating system has a `sigaction' system call that correctly
130 + implements the POSIX.1 behavior, there should be a system-dependent
131 + version of this file that defines `struct sigaction' and the `SA_*'
132 + constants appropriately. */
134 +/* Structure describing the action to be taken when a signal arrives. */
137 + /* Signal handler. */
138 +#if defined __USE_POSIX199309 || defined __USE_XOPEN_EXTENDED
141 + /* Used if SA_SIGINFO is not set. */
142 + __sighandler_t sa_handler;
143 + /* Used if SA_SIGINFO is set. */
144 + void (*sa_sigaction) (int, siginfo_t *, void *);
146 + __sigaction_handler;
147 +# define sa_handler __sigaction_handler.sa_handler
148 +# define sa_sigaction __sigaction_handler.sa_sigaction
150 + __sighandler_t sa_handler;
153 + /* Additional set of signals to be blocked. */
154 + __sigset_t sa_mask;
156 + /* Special flags. */
160 +/* Bits in `sa_flags'. */
161 +#if defined __USE_XOPEN_EXTENDED || defined __USE_MISC
162 +# define SA_ONSTACK 0x0001 /* Take signal on signal stack. */
164 +#if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8
165 +# define SA_RESTART 0x0002 /* Restart syscall on signal return. */
166 +# define SA_NODEFER 0x0010 /* Don't automatically block the signal when
167 + its handler is being executed. */
168 +# define SA_RESETHAND 0x0004 /* Reset to SIG_DFL on entry to handler. */
170 +#define SA_NOCLDSTOP 0x0008 /* Don't send SIGCHLD when children stop. */
171 +#define SA_SIGINFO 0x0040 /* Signal handler with SA_SIGINFO args */
174 +# define SA_INTERRUPT 0 /* Historical no-op ("not SA_RESTART"). */
176 +/* Some aliases for the SA_ constants. */
177 +# define SA_NOMASK SA_NODEFER
178 +# define SA_ONESHOT SA_RESETHAND
179 +# define SA_STACK SA_ONSTACK
183 +/* Values for the HOW argument to `sigprocmask'. */
184 +#define SIG_BLOCK 1 /* Block signals. */
185 +#define SIG_UNBLOCK 2 /* Unblock signals. */
186 +#define SIG_SETMASK 3 /* Set the set of blocked signals. */
189 Index: glibc-2.31/sysdeps/mach/hurd/i386/bits/sigcontext.h
190 ===================================================================
191 --- glibc-2.31.orig/sysdeps/mach/hurd/i386/bits/sigcontext.h
192 +++ glibc-2.31/sysdeps/mach/hurd/i386/bits/sigcontext.h
193 @@ -97,6 +97,10 @@ struct sigcontext
197 +/* The deprecated sigcode values below are passed as an extra, non-portable
198 + argument to regular signal handlers. You should use SA_SIGINFO handlers
199 + instead, which use the standard POSIX signal codes. */
201 /* Codes for SIGFPE. */
202 #define FPE_INTOVF_TRAP 0x1 /* integer overflow */
203 #define FPE_INTDIV_FAULT 0x2 /* integer divide by zero */
204 Index: glibc-2.31/sysdeps/mach/hurd/i386/exc2signal.c
205 ===================================================================
206 --- glibc-2.31.orig/sysdeps/mach/hurd/i386/exc2signal.c
207 +++ glibc-2.31/sysdeps/mach/hurd/i386/exc2signal.c
209 /* Translate the Mach exception codes, as received in an `exception_raise' RPC,
210 into a signal number and signal subcode. */
213 -_hurd_exception2signal (struct hurd_signal_detail *detail, int *signo)
215 +exception2signal (struct hurd_signal_detail *detail, int *signo, int posix)
219 @@ -36,44 +36,62 @@ _hurd_exception2signal (struct hurd_sign
223 - if (detail->exc_code == KERN_INVALID_ADDRESS
224 - || detail->exc_code == KERN_PROTECTION_FAILURE
225 - || detail->exc_code == KERN_WRITE_PROTECTION_FAILURE)
229 - detail->code = detail->exc_subcode;
230 + switch (detail->exc_code)
232 + case KERN_INVALID_ADDRESS:
233 + case KERN_MEMORY_FAILURE:
235 + detail->code = posix ? SEGV_MAPERR : detail->exc_subcode;
238 + case KERN_PROTECTION_FAILURE:
239 + case KERN_WRITE_PROTECTION_FAILURE:
241 + detail->code = posix ? SEGV_ACCERR : detail->exc_subcode;
246 + detail->code = posix ? BUS_ADRERR : detail->exc_subcode;
249 detail->error = detail->exc_code;
252 case EXC_BAD_INSTRUCTION:
254 - if (detail->exc_code == EXC_I386_INVOP)
255 - detail->code = ILL_INVOPR_FAULT;
256 - else if (detail->exc_code == EXC_I386_STKFLT)
257 - detail->code = ILL_STACK_FAULT;
260 + switch (detail->exc_code)
262 + case EXC_I386_INVOP:
263 + detail->code = posix ? ILL_ILLOPC : ILL_INVOPR_FAULT;
266 + case EXC_I386_STKFLT:
267 + detail->code = posix ? ILL_BADSTK : ILL_STACK_FAULT;
278 switch (detail->exc_code)
280 case EXC_I386_DIV: /* integer divide by zero */
282 - detail->code = FPE_INTDIV_FAULT;
283 + detail->code = posix ? FPE_INTDIV : FPE_INTDIV_FAULT;
286 case EXC_I386_INTO: /* integer overflow */
288 - detail->code = FPE_INTOVF_TRAP;
289 + detail->code = posix ? FPE_INTOVF : FPE_INTOVF_TRAP;
292 /* These aren't anywhere documented or used in Mach 3.0. */
294 case EXC_I386_EXTOVR:
300 @@ -82,51 +100,43 @@ _hurd_exception2signal (struct hurd_sign
301 Give an error code corresponding to the first bit set. */
302 if (detail->exc_subcode & FPS_IE)
305 - detail->code = ILL_FPEOPR_FAULT;
306 + /* NB: We used to send SIGILL here but we can't distinguish
307 + POSIX vs. legacy with respect to what signal we send. */
308 + detail->code = posix ? FPE_FLTINV : 0 /*ILL_FPEOPR_FAULT*/;
310 else if (detail->exc_subcode & FPS_DE)
313 - detail->code = FPE_FLTDNR_FAULT;
314 + detail->code = posix ? FPE_FLTUND : FPE_FLTDNR_FAULT;
316 else if (detail->exc_subcode & FPS_ZE)
319 - detail->code = FPE_FLTDIV_FAULT;
320 + detail->code = posix ? FPE_FLTDIV : FPE_FLTDIV_FAULT;
322 else if (detail->exc_subcode & FPS_OE)
325 - detail->code = FPE_FLTOVF_FAULT;
326 + detail->code = posix ? FPE_FLTOVF : FPE_FLTOVF_FAULT;
328 else if (detail->exc_subcode & FPS_UE)
331 - detail->code = FPE_FLTUND_FAULT;
332 + detail->code = posix ? FPE_FLTUND : FPE_FLTUND_FAULT;
334 else if (detail->exc_subcode & FPS_PE)
337 - detail->code = FPE_FLTINX_FAULT;
338 + detail->code = posix ? FPE_FLTRES : FPE_FLTINX_FAULT;
347 /* These two can only be arithmetic exceptions if we
348 - are in V86 mode, which sounds like emulation to me.
349 - (See Mach 3.0 i386/trap.c.) */
350 + are in V86 mode. (See Mach 3.0 i386/trap.c.) */
353 - detail->code = FPE_EMERR_FAULT;
354 + detail->code = posix ? 0 : FPE_EMERR_FAULT;
358 - detail->code = FPE_EMBND_FAULT;
359 + detail->code = posix ? FPE_FLTSUB : FPE_EMBND_FAULT;
363 @@ -143,7 +153,7 @@ _hurd_exception2signal (struct hurd_sign
364 if (detail->exc_code == EXC_I386_BOUND)
367 - detail->code = FPE_SUBRNG_FAULT;
368 + detail->code = posix ? FPE_FLTSUB : FPE_SUBRNG_FAULT;
372 @@ -154,13 +164,34 @@ _hurd_exception2signal (struct hurd_sign
376 - if (detail->exc_code == EXC_I386_SGL)
377 - detail->code = DBG_SINGLE_TRAP;
378 - else if (detail->exc_code == EXC_I386_BPT)
379 - detail->code = DBG_BRKPNT_FAULT;
382 + switch (detail->exc_code)
385 + detail->code = posix ? TRAP_BRKPT : DBG_SINGLE_TRAP;
389 + detail->code = posix ? TRAP_BRKPT : DBG_BRKPNT_FAULT;
399 libc_hidden_def (_hurd_exception2signal)
402 +_hurd_exception2signal (struct hurd_signal_detail *detail, int *signo)
404 + exception2signal (detail, signo, 1);
408 +_hurd_exception2signal_legacy (struct hurd_signal_detail *detail, int *signo)
410 + exception2signal (detail, signo, 0);
413 Index: glibc-2.31/sysdeps/mach/hurd/i386/trampoline.c
414 ===================================================================
415 --- glibc-2.31.orig/sysdeps/mach/hurd/i386/trampoline.c
416 +++ glibc-2.31/sysdeps/mach/hurd/i386/trampoline.c
418 #include <hurd/signal.h>
419 #include <hurd/userlink.h>
420 #include <thread_state.h>
421 +#include <mach/exception.h>
422 #include <mach/machine/eflags.h>
425 #include "hurdfault.h"
426 #include <intr-msg.h>
427 +#include <sys/ucontext.h>
430 +/* Fill in a siginfo_t structure for SA_SIGINFO-enabled handlers. */
431 +static void fill_siginfo (siginfo_t *si, int signo,
432 + const struct hurd_signal_detail *detail,
433 + const struct machine_thread_all_state *state)
435 + si->si_signo = signo;
436 + si->si_errno = detail->error;
437 + si->si_code = detail->code;
439 + /* XXX We would need a protocol change for sig_post to include
440 + * this information. */
444 + /* Address of the faulting instruction or memory access. */
445 + if (detail->exc == EXC_BAD_ACCESS)
446 + si->si_addr = (void *) detail->exc_subcode;
448 + si->si_addr = (void *) state->basic.eip;
450 + /* XXX On SIGCHLD, this should be the exit status of the child
451 + * process. We would need a protocol change for the proc server
452 + * to send this information along with the signal. */
455 + si->si_band = 0; /* SIGPOLL is not supported yet. */
456 + si->si_value.sival_int = 0; /* sigqueue() is not supported yet. */
459 +/* Fill in a ucontext_t structure SA_SIGINFO-enabled handlers. */
460 +static void fill_ucontext (ucontext_t *uc, const struct sigcontext *sc)
463 + uc->uc_link = NULL;
464 + uc->uc_sigmask = sc->sc_mask;
465 + uc->uc_stack.ss_sp = (__ptr_t) sc->sc_esp;
466 + uc->uc_stack.ss_size = 0;
467 + uc->uc_stack.ss_flags = 0;
470 + memcpy (&uc->uc_mcontext.gregs[REG_GS], &sc->sc_gs,
471 + (REG_TRAPNO - REG_GS) * sizeof (int));
472 + uc->uc_mcontext.gregs[REG_TRAPNO] = 0;
473 + uc->uc_mcontext.gregs[REG_ERR] = 0;
474 + memcpy (&uc->uc_mcontext.gregs[REG_EIP], &sc->sc_eip,
475 + (NGREG - REG_EIP) * sizeof (int));
477 + /* XXX FPU state. */
478 + memset (&uc->uc_mcontext.fpregs, 0, sizeof (fpregset_t));
482 _hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
483 int signo, struct hurd_signal_detail *detail,
484 @@ -43,15 +96,38 @@ _hurd_setup_sighandler (struct hurd_sigs
489 - struct sigcontext *scp; /* Points to ctx, below. */
492 + /* Extra arguments for traditional signal handlers */
496 + struct sigcontext *scp; /* Points to ctx, below. */
499 + /* Extra arguments for SA_SIGINFO handlers */
502 + siginfo_t *siginfop; /* Points to siginfo, below. */
503 + ucontext_t *uctxp; /* Points to uctx, below. */
506 void *sigreturn_addr;
507 void *sigreturn_returns_here;
508 struct sigcontext *return_scp; /* Same; arg to sigreturn. */
510 + /* NB: sigreturn assumes link is next to ctx. */
511 struct sigcontext ctx;
512 struct hurd_userlink link;
513 + ucontext_t ucontext;
517 + /* sigaction for preemptors */
518 + static const struct sigaction legacy_sigaction = {
519 + .sa_flags = SA_RESTART
524 /* We have a previous sigcontext that sigreturn was about
525 @@ -94,9 +170,13 @@ _hurd_setup_sighandler (struct hurd_sigs
526 the SP on sigreturn. */
527 state->basic.uesp = state->basic.ecx;
529 - /* XXX what if handler != action->handler (for instance, if a signal
530 - * preemptor took over) ? */
531 action = & _hurd_sigstate_actions (ss) [signo];
532 + if ( (action->sa_flags & SA_SIGINFO)
533 + && handler != (__sighandler_t) action->sa_sigaction
534 + || !(action->sa_flags & SA_SIGINFO)
535 + && handler != action->sa_handler)
536 + /* A signal preemptor took over, use legacy semantic. */
537 + action = &legacy_sigaction;
539 if ((action->sa_flags & SA_ONSTACK)
540 && !(ss->sigaltstack.ss_flags & (SS_DISABLE|SS_ONSTACK)))
541 @@ -140,15 +220,9 @@ _hurd_setup_sighandler (struct hurd_sigs
542 = &stackframe->link.thread.next;
543 ss->active_resources = &stackframe->link;
545 - /* Set up the arguments for the signal handler. */
546 - stackframe->signo = signo;
547 - stackframe->sigcode = detail->code;
548 - stackframe->scp = stackframe->return_scp = scp = &stackframe->ctx;
549 - stackframe->sigreturn_addr = &__sigreturn;
550 - stackframe->sigreturn_returns_here = firewall; /* Crash on return. */
552 /* Set up the sigcontext from the current state of the thread. */
554 + scp = &stackframe->ctx;
555 scp->sc_onstack = ss->sigaltstack.ss_flags & SS_ONSTACK ? 1 : 0;
557 /* struct sigcontext is laid out so that starting at sc_gs mimics a
558 @@ -162,6 +236,35 @@ _hurd_setup_sighandler (struct hurd_sigs
559 &state->fpu, &scp->sc_i386_float_state,
560 sizeof (state->fpu));
562 + /* Set up the arguments for the signal handler. */
563 + stackframe->signo = signo;
564 + if (action->sa_flags & SA_SIGINFO)
566 + stackframe->posix.siginfop = &stackframe->siginfo;
567 + stackframe->posix.uctxp = &stackframe->ucontext;
568 + fill_siginfo (&stackframe->siginfo, signo, detail, state);
569 + fill_ucontext (&stackframe->ucontext, scp);
576 + _hurd_exception2signal_legacy (detail, &nsigno);
577 + assert (nsigno == signo);
582 + stackframe->legacy.sigcode = detail->code;
583 + stackframe->legacy.scp = &stackframe->ctx;
586 + /* Set up the bottom of the stack. */
587 + stackframe->sigreturn_addr = &__sigreturn;
588 + stackframe->sigreturn_returns_here = firewall; /* Crash on return. */
589 + stackframe->return_scp = &stackframe->ctx;
591 _hurdsig_end_catch_fault ();
594 Index: glibc-2.31/sysdeps/mach/hurd/kill.c
595 ===================================================================
596 --- glibc-2.31.orig/sysdeps/mach/hurd/kill.c
597 +++ glibc-2.31/sysdeps/mach/hurd/kill.c
598 @@ -64,7 +64,7 @@ __kill (pid_t pid, int sig)
600 if (msgport != MACH_PORT_NULL)
601 /* Send a signal message to his message port. */
602 - return __msg_sig_post (msgport, sig, 0, refport);
603 + return __msg_sig_post (msgport, sig, SI_USER, refport);
605 /* The process has no message port. Perhaps try direct
606 frobnication of the task. */
607 Index: glibc-2.31/sysdeps/mach/hurd/setitimer.c
608 ===================================================================
609 --- glibc-2.31.orig/sysdeps/mach/hurd/setitimer.c
610 +++ glibc-2.31/sysdeps/mach/hurd/setitimer.c
611 @@ -105,7 +105,7 @@ timer_thread (void)
612 __msg_sig_post_request (_hurd_msgport,
614 MACH_MSG_TYPE_MAKE_SEND_ONCE,
615 - SIGALRM, 0, __mach_task_self ());
616 + SIGALRM, SI_TIMER, __mach_task_self ());
619 case MACH_RCV_INTERRUPTED:
620 Index: glibc-2.31/sysdeps/mach/hurd/i386/sigcontextinfo.h
621 ===================================================================
622 --- glibc-2.31.orig/sysdeps/mach/hurd/i386/sigcontextinfo.h
623 +++ glibc-2.31/sysdeps/mach/hurd/i386/sigcontextinfo.h
625 #ifndef _SIGCONTEXTINFO_H
626 #define _SIGCONTEXTINFO_H
628 -#define SIGCONTEXT struct sigcontext
629 static inline uintptr_t
630 -sigcontext_get_pc (struct sigcontext ctx)
631 +sigcontext_get_pc (struct ucontext_t *ctx)
634 + return ctx->uc_mcontext.gregs[REG_EIP];