Debian Codegraph @

gcc/ada/libgnarl/s-intman__cheribsd.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . I N T E R R U P T _ M A N A G E M E N T --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 -- This is the POSIX threads version of this package, adapted for CheriBSD
33
34 -- Make a careful study of all signals available under the OS, to see which
35 -- need to be reserved, kept always unmasked, or kept always unmasked. Be on
36 -- the lookout for special signals that may be used by the thread library.
37
38 -- Since this is a multi target file, the signal <-> exception mapping
39 -- is simple minded. If you need a more precise and target specific
40 -- signal handling, create a new s-intman.adb that will fit your needs.
41
42 -- This file assumes that:
43
44 -- SIGFPE, SIGILL, SIGSEGV and SIGBUS exist. They are mapped as follows:
45 -- SIGPFE => Constraint_Error
46 -- SIGILL => Program_Error
47 -- SIGSEGV => Storage_Error
48 -- SIGBUS => Storage_Error
49
50 -- SIGINT exists and will be kept unmasked unless the pragma
51 -- Unreserve_All_Interrupts is specified anywhere in the application.
52
53 -- System.OS_Interface contains the following:
54 -- SIGADAABORT: the signal that will be used to abort tasks.
55 -- Unmasked: the OS specific set of signals that should be unmasked in
56 -- all the threads. SIGADAABORT is unmasked by
57 -- default
58 -- Reserved: the OS specific set of signals that are reserved.
59
60 with Interfaces.CHERI.Exceptions;
61 with System.Task_Primitives;
62
63 package body System.Interrupt_Management is
64
65 use Interfaces.C;
66 use Interfaces.CHERI.Exceptions;
67 use System.OS_Interface;
68
69 type Interrupt_List is array (Interrupt_ID range <>) of Interrupt_ID;
70 Exception_Interrupts : constant Interrupt_List :=
71 [SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGPROT];
72
73 Unreserve_All_Interrupts : constant Interfaces.C.int;
74 pragma Import
75 (C, Unreserve_All_Interrupts, "__gl_unreserve_all_interrupts");
76
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
80
81 function State (Int : Interrupt_ID) return Character;
82 pragma Import (C, State, "__gnat_get_interrupt_state");
83 -- Get interrupt state. Defined in init.c The input argument is the
84 -- interrupt number, and the result is one of the following:
85
86 User : constant Character := 'u';
87 Runtime : constant Character := 'r';
88 Default : constant Character := 's';
89 -- 'n' this interrupt not set by any Interrupt_State pragma
90 -- 'u' Interrupt_State pragma set state to User
91 -- 'r' Interrupt_State pragma set state to Runtime
92 -- 's' Interrupt_State pragma set state to System (use "default"
93 -- system handler)
94
95 procedure Notify_Exception
96 (signo : Signal;
97 siginfo : access siginfo_t;
98 ucontext : System.Address);
99 -- This function identifies the Ada exception to be raised using the
100 -- information when the system received a synchronous signal. Since this
101 -- function is machine and OS dependent, different code has to be provided
102 -- for different target.
103
104 ----------------------
105 -- Notify_Exception --
106 ----------------------
107
108 Signal_Mask : aliased sigset_t;
109 -- The set of signals handled by Notify_Exception
110
111 procedure Notify_Exception
112 (signo : Signal;
113 siginfo : access siginfo_t;
114 ucontext : System.Address)
115 is
116 Result : Interfaces.C.int;
117
118 begin
119 -- With the __builtin_longjmp, the signal mask is not restored, so we
120 -- need to restore it explicitly.
121
122 Result := pthread_sigmask (SIG_UNBLOCK, Signal_Mask'Access, null);
123 pragma Assert (Result = 0);
124
125 -- Perform the necessary context adjustments prior to a raise
126 -- from a signal handler.
127
128 Adjust_Context_For_Raise (signo, ucontext);
129
130 -- Check that treatment of exception propagation here is consistent with
131 -- treatment of the abort signal in System.Task_Primitives.Operations.
132
133 case signo is
134 when SIGFPE => raise Constraint_Error;
135 when SIGILL => raise Program_Error;
136 when SIGSEGV => raise Storage_Error;
137 when SIGBUS => raise Storage_Error;
138 when SIGPROT =>
139 case siginfo.all.si_code is
140 when PROT_CHERI_TAG => raise Capability_Tag_Error;
141 when PROT_CHERI_SEALED => raise Capability_Sealed_Error;
142 when PROT_CHERI_UNALIGNED_BASE => raise Storage_Error;
143
144 when PROT_CHERI_BOUNDS
145 | PROT_CHERI_IMPRECISE =>
146 raise Capability_Bound_Error;
147
148 when PROT_CHERI_TYPE
149 | PROT_CHERI_PERM
150 | PROT_CHERI_STORELOCAL
151 | PROT_CHERI_CINVOKE
152 | PROT_CHERI_SYSREG =>
153 raise Capability_Permission_Error;
154
155 when others =>
156 raise Storage_Error;
157 end case;
158
159 when others =>
160 null;
161 end case;
162 end Notify_Exception;
163
164 ----------------
165 -- Initialize --
166 ----------------
167
168 Initialized : Boolean := False;
169
170 procedure Initialize is
171 act : aliased struct_sigaction;
172 old_act : aliased struct_sigaction;
173 Result : System.OS_Interface.int;
174
175 Use_Alternate_Stack : constant Boolean :=
176 System.Task_Primitives.Alternate_Stack_Size /= 0;
177 -- Whether to use an alternate signal stack for stack overflows
178
179 Sigsetable_Signal_Mask : aliased sigset_t;
180
181 Interrupts_Default_To_System : Integer;
182 pragma Import (C, Interrupts_Default_To_System,
183 "__gl_interrupts_default_to_system");
184
185 begin
186 if Initialized then
187 return;
188 end if;
189
190 Initialized := True;
191
192 -- Need to call pthread_init very early because it is doing signal
193 -- initializations.
194
195 pthread_init;
196
197 Abort_Task_Interrupt := SIGADAABORT;
198
199 act.sa_handler := Notify_Exception'Address;
200
201 -- Setting SA_SIGINFO asks the kernel to pass more than just the signal
202 -- number argument to the handler when it is called. The set of extra
203 -- parameters includes a pointer to the interrupted context, which the
204 -- ZCX propagation scheme needs.
205
206 -- Most man pages for sigaction mention that sa_sigaction should be set
207 -- instead of sa_handler when SA_SIGINFO is on. In practice, the two
208 -- fields are actually union'ed and located at the same offset.
209
210 -- On some targets, we set sa_flags to SA_NODEFER so that during the
211 -- handler execution we do not change the Signal_Mask to be masked for
212 -- the Signal.
213
214 -- This is a temporary fix to the problem that the Signal_Mask is not
215 -- restored after the exception (longjmp) from the handler. The right
216 -- fix should be made in sigsetjmp so that we save the Signal_Set and
217 -- restore it after a longjmp.
218
219 -- Since SA_NODEFER is obsolete, instead we reset explicitly the mask
220 -- in the exception handler.
221
222 Result := sigemptyset (Signal_Mask'Access);
223 pragma Assert (Result = 0);
224
225 -- Add signals that map to Ada exceptions to the mask
226
227 for J in Exception_Interrupts'Range loop
228 if State (Exception_Interrupts (J)) /= Default then
229 Result := sigaddset (Signal_Mask'Access,
230 Signal (Exception_Interrupts (J)));
231 pragma Assert (Result = 0);
232 end if;
233 end loop;
234
235 act.sa_mask := Signal_Mask;
236
237 pragma Assert (Keep_Unmasked = [Interrupt_ID'Range => False]);
238 pragma Assert (Reserve = [Interrupt_ID'Range => False]);
239
240 -- Process state of exception signals
241
242 for J in Exception_Interrupts'Range loop
243 if State (Exception_Interrupts (J)) /= User then
244 Keep_Unmasked (Exception_Interrupts (J)) := True;
245 Reserve (Exception_Interrupts (J)) := True;
246
247 if State (Exception_Interrupts (J)) /= Default then
248 act.sa_flags := SA_SIGINFO;
249
250 if Use_Alternate_Stack
251 and then Exception_Interrupts (J) = SIGSEGV
252 then
253 act.sa_flags := act.sa_flags + SA_ONSTACK;
254 end if;
255
256 Result :=
257 sigaction
258 (Signal (Exception_Interrupts (J)), act'Unchecked_Access,
259 old_act'Unchecked_Access);
260 pragma Assert (Result = 0);
261 end if;
262 end if;
263 end loop;
264
265 if State (Abort_Task_Interrupt) /= User then
266 Keep_Unmasked (Abort_Task_Interrupt) := True;
267 Reserve (Abort_Task_Interrupt) := True;
268 end if;
269
270 -- Set SIGINT to unmasked state as long as it is not in "User" state.
271 -- Check for Unreserve_All_Interrupts last.
272
273 if State (SIGINT) /= User then
274 Keep_Unmasked (SIGINT) := True;
275 Reserve (SIGINT) := True;
276 end if;
277
278 -- Check all signals for state that requires keeping them unmasked and
279 -- reserved.
280
281 -- Unmasking might involve explicit operations later on, typically
282 -- performed on the entire set of relevant signals gathered together
283 -- by way of sigset_t mask. Doing anything of this kind is forbidden
284 -- for very specific signals and would trip assertion checks if
285 -- attempted. Check for this here, preventing the Keep_Unmasked
286 -- request upfront. This is of particular relevance for
287 -- Interrupts_System_By_Default as it would lead to such requests
288 -- for every signal not altered otherwise.
289
290 Result := sigemptyset (Sigsetable_Signal_Mask'Access);
291 pragma Assert (Result = 0);
292
293 for J in Interrupt_ID'Range loop
294 if State (J) = Default or else State (J) = Runtime then
295 if Interrupts_Default_To_System = 0 or else
296 sigaddset (Sigsetable_Signal_Mask'Access, Signal (J)) = 0
297 then
298 Keep_Unmasked (J) := True;
299 end if;
300 Reserve (J) := True;
301 end if;
302 end loop;
303
304 -- Add the set of signals that must always be unmasked for this target
305
306 for J in Unmasked'Range loop
307 Keep_Unmasked (Interrupt_ID (Unmasked (J))) := True;
308 Reserve (Interrupt_ID (Unmasked (J))) := True;
309 end loop;
310
311 -- Add target-specific reserved signals
312
313 for J in Reserved'Range loop
314 Reserve (Interrupt_ID (Reserved (J))) := True;
315 end loop;
316
317 -- Process pragma Unreserve_All_Interrupts. This overrides any settings
318 -- due to pragma Interrupt_State:
319
320 if Unreserve_All_Interrupts /= 0 then
321 Keep_Unmasked (SIGINT) := False;
322 Reserve (SIGINT) := False;
323 end if;
324
325 -- We do not really have Signal 0. We just use this value to identify
326 -- non-existent signals (see s-intnam.ads). Therefore, Signal should not
327 -- be used in all signal related operations hence mark it as reserved.
328
329 Reserve (0) := True;
330 end Initialize;
331
332 end System.Interrupt_Management;