aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/mach/hurd/i386/exc2signal.c
blob: d9a8b4ff820f2645e8159e1f0f1688d15d299434 (plain)
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
/* Translate Mach exception codes into signal numbers.  i386 version.
   Copyright (C) 1991-2021 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library 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 2.1 of the License, or (at your option) any later version.

   The GNU C Library 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 the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <hurd.h>
#include <hurd/signal.h>
#include <mach/exception.h>

/* Translate the Mach exception codes, as received in an `exception_raise' RPC,
   into a signal number and signal subcode.  */

static void
exception2signal (struct hurd_signal_detail *detail, int *signo, int posix)
{
  detail->error = 0;

  switch (detail->exc)
    {
    default:
      *signo = SIGIOT;
      detail->code = detail->exc;
      break;

    case EXC_BAD_ACCESS:
      switch (detail->exc_code)
        {
	case KERN_INVALID_ADDRESS:
	case KERN_MEMORY_FAILURE:
	  *signo = SIGSEGV;
	  detail->code = posix ? SEGV_MAPERR : detail->exc_subcode;
	  break;

	case KERN_PROTECTION_FAILURE:
	case KERN_WRITE_PROTECTION_FAILURE:
	  *signo = SIGSEGV;
	  detail->code = posix ? SEGV_ACCERR : detail->exc_subcode;
	  break;

	default:
	  *signo = SIGBUS;
	  detail->code = posix ? BUS_ADRERR : detail->exc_subcode;
	  break;
	}
      detail->error = detail->exc_code;
      break;

    case EXC_BAD_INSTRUCTION:
      *signo = SIGILL;
      switch (detail->exc_code)
        {
	case EXC_I386_INVOP:
	  detail->code = posix ? ILL_ILLOPC : ILL_INVOPR_FAULT;
	  break;

	case EXC_I386_STKFLT:
	  detail->code = posix ? ILL_BADSTK : ILL_STACK_FAULT;
	  break;

	default:
	  detail->code = 0;
	  break;
	}
      break;

    case EXC_ARITHMETIC:
      *signo = SIGFPE;
      switch (detail->exc_code)
	{
	case EXC_I386_DIV:	/* integer divide by zero */
	  detail->code = posix ? FPE_INTDIV : FPE_INTDIV_FAULT;
	  break;

	case EXC_I386_INTO:	/* integer overflow */
	  detail->code = posix ? FPE_INTOVF : FPE_INTOVF_TRAP;
	  break;

	  /* These aren't anywhere documented or used in Mach 3.0.  */
	case EXC_I386_NOEXT:
	case EXC_I386_EXTOVR:
	default:
	  detail->code = 0;
	  break;

	case EXC_I386_EXTERR:
	  /* Subcode is the fp_status word saved by the hardware.
	     Give an error code corresponding to the first bit set.  */
	  if (detail->exc_subcode & FPS_IE)
	    {
	      /* NB: We used to send SIGILL here but we can't distinguish
		 POSIX vs. legacy with respect to what signal we send.  */
	      detail->code = posix ? FPE_FLTINV : 0 /*ILL_FPEOPR_FAULT*/;
	    }
	  else if (detail->exc_subcode & FPS_DE)
	    {
	      detail->code = posix ? FPE_FLTUND : FPE_FLTDNR_FAULT;
	    }
	  else if (detail->exc_subcode & FPS_ZE)
	    {
	      detail->code = posix ? FPE_FLTDIV : FPE_FLTDIV_FAULT;
	    }
	  else if (detail->exc_subcode & FPS_OE)
	    {
	      detail->code = posix ? FPE_FLTOVF : FPE_FLTOVF_FAULT;
	    }
	  else if (detail->exc_subcode & FPS_UE)
	    {
	      detail->code = posix ? FPE_FLTUND : FPE_FLTUND_FAULT;
	    }
	  else if (detail->exc_subcode & FPS_PE)
	    {
	      detail->code = posix ? FPE_FLTRES : FPE_FLTINX_FAULT;
	    }
	  else
	    {
	      detail->code = 0;
	    }
	  break;

	  /* These two can only be arithmetic exceptions if we
	     are in V86 mode.  (See Mach 3.0 i386/trap.c.)  */
	case EXC_I386_EMERR:
	  detail->code = posix ? 0 : FPE_EMERR_FAULT;
	  break;
	case EXC_I386_BOUND:
	  detail->code = posix ? FPE_FLTSUB : FPE_EMBND_FAULT;
	  break;
	}
      break;

    case EXC_EMULATION:
      /* 3.0 doesn't give this one, why, I don't know.  */
      *signo = SIGEMT;
      detail->code = 0;
      break;

    case EXC_SOFTWARE:
      /* The only time we get this in Mach 3.0
	 is for an out of bounds trap.  */
      if (detail->exc_code == EXC_I386_BOUND)
	{
	  *signo = SIGFPE;
	  detail->code = posix ? FPE_FLTSUB : FPE_SUBRNG_FAULT;
	}
      else
	{
	  *signo = SIGEMT;
	  detail->code = 0;
	}
      break;

    case EXC_BREAKPOINT:
      *signo = SIGTRAP;
      switch (detail->exc_code)
        {
	case EXC_I386_SGL:
	  detail->code = posix ? TRAP_BRKPT : DBG_SINGLE_TRAP;
	  break;

	case EXC_I386_BPT:
	  detail->code = posix ? TRAP_BRKPT : DBG_BRKPNT_FAULT;
	  break;

	default:
	  detail->code = 0;
	  break;
	}
      break;
    }
}
libc_hidden_def (_hurd_exception2signal)

void
_hurd_exception2signal (struct hurd_signal_detail *detail, int *signo)
{
  exception2signal (detail, signo, 1);
}

void
_hurd_exception2signal_legacy (struct hurd_signal_detail *detail, int *signo)
{
  exception2signal (detail, signo, 0);
}