1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
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 --
9 -- Copyright (C) 1991-2002 Florida State University --
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 2, or (at your option) any later ver- --
14 -- sion. GNARL 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNARL was developed by the GNARL team at Florida State University. It is --
30 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
31 -- State University (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 -- This is the GNU/Linux version of this package
37 -- This file performs the system-dependent translation between machine
38 -- exceptions and the Ada exceptions, if any, that should be raised when they
39 -- occur. This version works for the x86 running linux.
41 -- This is a Sun OS (FSU THREADS) version of this package
43 -- PLEASE DO NOT add any dependences on other packages. ??? why not ???
44 -- This package is designed to work with or without tasking support.
46 -- Make a careful study of all signals available under the OS, to see which
47 -- need to be reserved, kept always unmasked, or kept always unmasked. Be on
48 -- the lookout for special signals that may be used by the thread library.
50 -- The definitions of "reserved" differ slightly between the ARM and POSIX.
51 -- Here is the ARM definition of reserved interrupt:
53 -- The set of reserved interrupts is implementation defined. A reserved
54 -- interrupt is either an interrupt for which user-defined handlers are not
55 -- supported, or one which already has an attached handler by some other
56 -- implementation-defined means. Program units can be connected to
57 -- non-reserved interrupts.
59 -- POSIX.5b/.5c specifies further:
61 -- Signals which the application cannot accept, and for which the application
62 -- cannot modify the signal action or masking, because the signals are
63 -- reserved for use by the Ada language implementation. The reserved signals
64 -- defined by this standard are Signal_Abort, Signal_Alarm,
65 -- Signal_Floating_Point_Error, Signal_Illegal_Instruction,
66 -- Signal_Segmentation_Violation, Signal_Bus_Error. If the implementation
67 -- supports any signals besides those defined by this standard, the
68 -- implementation may also reserve some of those.
70 -- The signals defined by POSIX.5b/.5c that are not specified as being
71 -- reserved are SIGHUP, SIGINT, SIGPIPE, SIGQUIT, SIGTERM, SIGUSR1, SIGUSR2,
72 -- SIGCHLD, SIGCONT, SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGIO SIGURG, and all
73 -- the real-time signals.
75 -- Beware of reserving signals that POSIX.5b/.5c require to be available for
76 -- users. POSIX.5b/.5c say:
78 -- An implementation shall not impose restrictions on the ability of an
79 -- application to send, accept, block, or ignore the signals defined by this
80 -- standard, except as specified in this standard.
82 -- Here are some other relevant requirements from POSIX.5b/.5c:
84 -- For the environment task, the initial signal mask is that specified for
87 -- It is anticipated that the paragraph above may be modified by a future
88 -- revision of this standard, to require that the realtime signals always be
89 -- initially masked for a process that is an Ada active partition.
91 -- For all other tasks, the initial signal mask shall include all the signals
92 -- that are not reserved signals and are not bound to entries of the task.
95 -- used for int and other types
97 with System.Error_Reporting;
100 with System.OS_Interface;
101 -- used for various Constants, Signal and types
104 -- used for Exception_Id
105 -- Raise_From_Signal_Handler
107 with System.Soft_Links;
108 -- used for Get_Machine_State_Addr
110 with Unchecked_Conversion;
112 package body System.Interrupt_Management is
115 use System.Error_Reporting;
116 use System.OS_Interface;
118 package TSL renames System.Soft_Links;
120 type Interrupt_List is array (Interrupt_ID range <>) of Interrupt_ID;
121 Exception_Interrupts : constant Interrupt_List :=
122 (SIGFPE, SIGILL, SIGSEGV);
124 Unreserve_All_Interrupts : Interfaces.C.int;
126 (C, Unreserve_All_Interrupts, "__gl_unreserve_all_interrupts");
128 subtype int is Interfaces.C.int;
129 subtype unsigned_short is Interfaces.C.unsigned_short;
130 subtype unsigned_long is Interfaces.C.unsigned_long;
132 ----------------------
133 -- Notify_Exception --
134 ----------------------
136 Signal_Mask : aliased sigset_t;
137 -- The set of signals handled by Notify_Exception
139 -- This function identifies the Ada exception to be raised using
140 -- the information when the system received a synchronous signal.
141 -- Since this function is machine and OS dependent, different code
142 -- has to be provided for different target.
144 procedure Notify_Exception
158 trapno : unsigned_long;
162 eflags : unsigned_long;
163 esp_at_signal : unsigned_long;
165 fpstate : System.Address;
166 oldmask : unsigned_long;
167 cr2 : unsigned_long);
169 procedure Notify_Exception
183 trapno : unsigned_long;
187 eflags : unsigned_long;
188 esp_at_signal : unsigned_long;
190 fpstate : System.Address;
191 oldmask : unsigned_long;
195 function To_Machine_State_Ptr is new
196 Unchecked_Conversion (Address, Machine_State_Ptr);
198 -- These are not directly visible
200 procedure Raise_From_Signal_Handler
201 (E : Ada.Exceptions.Exception_Id;
204 (Ada, Raise_From_Signal_Handler,
205 "ada__exceptions__raise_from_signal_handler");
206 pragma No_Return (Raise_From_Signal_Handler);
208 mstate : Machine_State_Ptr;
209 message : aliased constant String := "" & ASCII.Nul;
210 -- a null terminated String.
216 -- Raise_From_Signal_Handler makes sure that the exception is raised
217 -- safely from this signal handler.
219 -- ??? The original signal mask (the one we had before coming into this
220 -- signal catching function) should be restored by
221 -- Raise_From_Signal_Handler. For now, restore it explicitly
223 Result := pthread_sigmask (SIG_UNBLOCK, Signal_Mask'Access, null);
224 pragma Assert (Result = 0);
226 -- Check that treatment of exception propagation here
227 -- is consistent with treatment of the abort signal in
228 -- System.Task_Primitives.Operations.
230 mstate := To_Machine_State_Ptr (TSL.Get_Machine_State_Addr.all);
233 mstate.esp := esp_at_signal;
240 Raise_From_Signal_Handler
241 (Constraint_Error'Identity, message'Address);
243 Raise_From_Signal_Handler
244 (Constraint_Error'Identity, message'Address);
246 Raise_From_Signal_Handler
247 (Storage_Error'Identity, message'Address);
249 if Shutdown ("Unexpected signal") then
253 end Notify_Exception;
255 ---------------------------
256 -- Initialize_Interrupts --
257 ---------------------------
259 -- Nothing needs to be done on this platform.
261 procedure Initialize_Interrupts is
264 end Initialize_Interrupts;
268 act : aliased struct_sigaction;
269 old_act : aliased struct_sigaction;
274 -- Need to call pthread_init very early because it is doing signal
279 Abort_Task_Interrupt := SIGADAABORT;
281 act.sa_handler := Notify_Exception'Address;
284 -- On some targets, we set sa_flags to SA_NODEFER so that during the
285 -- handler execution we do not change the Signal_Mask to be masked for
287 -- This is a temporary fix to the problem that the Signal_Mask is
288 -- not restored after the exception (longjmp) from the handler.
289 -- The right fix should be made in sigsetjmp so that we save
290 -- the Signal_Set and restore it after a longjmp.
291 -- Since SA_NODEFER is obsolete, instead we reset explicitly
292 -- the mask in the exception handler.
294 Result := sigemptyset (Signal_Mask'Access);
295 pragma Assert (Result = 0);
297 for J in Exception_Interrupts'Range loop
299 sigaddset (Signal_Mask'Access, Signal (Exception_Interrupts (J)));
300 pragma Assert (Result = 0);
303 act.sa_mask := Signal_Mask;
305 for J in Exception_Interrupts'Range loop
306 Keep_Unmasked (Exception_Interrupts (J)) := True;
309 (Signal (Exception_Interrupts (J)),
310 act'Unchecked_Access,
311 old_act'Unchecked_Access);
312 pragma Assert (Result = 0);
315 Keep_Unmasked (Abort_Task_Interrupt) := True;
317 -- By keeping SIGINT unmasked, allow the user to do a Ctrl-C, but in the
318 -- same time, disable the ability of handling this signal
319 -- via Ada.Interrupts.
320 -- The pragma Unreserve_All_Interrupts allows the user to
321 -- change this behavior.
323 if Unreserve_All_Interrupts = 0 then
324 Keep_Unmasked (SIGINT) := True;
327 for J in Unmasked'Range loop
328 Keep_Unmasked (Interrupt_ID (Unmasked (J))) := True;
331 Reserve := Keep_Unmasked or Keep_Masked;
333 for J in Reserved'Range loop
334 Reserve (Interrupt_ID (Reserved (J))) := True;
338 -- We do not have Signal 0 in reality. We just use this value
339 -- to identify non-existent signals (see s-intnam.ads). Therefore,
340 -- Signal 0 should not be used in all signal related operations hence
341 -- mark it as reserved.
344 end System.Interrupt_Management;