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 --
11 -- Copyright (C) 1991-2001 Florida State University --
13 -- GNARL is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNARL; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNARL was developed by the GNARL team at Florida State University. It is --
32 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
33 -- State University (http://www.gnat.com). --
35 ------------------------------------------------------------------------------
37 -- This is the GNU/Linux version of this package
39 -- This file performs the system-dependent translation between machine
40 -- exceptions and the Ada exceptions, if any, that should be raised when they
41 -- occur. This version works for the x86 running linux.
43 -- This is a Sun OS (FSU THREADS) version of this package
45 -- PLEASE DO NOT add any dependences on other packages. ??? why not ???
46 -- This package is designed to work with or without tasking support.
48 -- Make a careful study of all signals available under the OS, to see which
49 -- need to be reserved, kept always unmasked, or kept always unmasked. Be on
50 -- the lookout for special signals that may be used by the thread library.
52 -- The definitions of "reserved" differ slightly between the ARM and POSIX.
53 -- Here is the ARM definition of reserved interrupt:
55 -- The set of reserved interrupts is implementation defined. A reserved
56 -- interrupt is either an interrupt for which user-defined handlers are not
57 -- supported, or one which already has an attached handler by some other
58 -- implementation-defined means. Program units can be connected to
59 -- non-reserved interrupts.
61 -- POSIX.5b/.5c specifies further:
63 -- Signals which the application cannot accept, and for which the application
64 -- cannot modify the signal action or masking, because the signals are
65 -- reserved for use by the Ada language implementation. The reserved signals
66 -- defined by this standard are Signal_Abort, Signal_Alarm,
67 -- Signal_Floating_Point_Error, Signal_Illegal_Instruction,
68 -- Signal_Segmentation_Violation, Signal_Bus_Error. If the implementation
69 -- supports any signals besides those defined by this standard, the
70 -- implementation may also reserve some of those.
72 -- The signals defined by POSIX.5b/.5c that are not specified as being
73 -- reserved are SIGHUP, SIGINT, SIGPIPE, SIGQUIT, SIGTERM, SIGUSR1, SIGUSR2,
74 -- SIGCHLD, SIGCONT, SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGIO SIGURG, and all
75 -- the real-time signals.
77 -- Beware of reserving signals that POSIX.5b/.5c require to be available for
78 -- users. POSIX.5b/.5c say:
80 -- An implementation shall not impose restrictions on the ability of an
81 -- application to send, accept, block, or ignore the signals defined by this
82 -- standard, except as specified in this standard.
84 -- Here are some other relevant requirements from POSIX.5b/.5c:
86 -- For the environment task, the initial signal mask is that specified for
89 -- It is anticipated that the paragraph above may be modified by a future
90 -- revision of this standard, to require that the realtime signals always be
91 -- initially masked for a process that is an Ada active partition.
93 -- For all other tasks, the initial signal mask shall include all the signals
94 -- that are not reserved signals and are not bound to entries of the task.
97 -- used for int and other types
99 with System
.Error_Reporting
;
102 with System
.OS_Interface
;
103 -- used for various Constants, Signal and types
106 -- used for Exception_Id
107 -- Raise_From_Signal_Handler
109 with System
.Soft_Links
;
110 -- used for Get_Machine_State_Addr
112 with Unchecked_Conversion
;
114 package body System
.Interrupt_Management
is
117 use System
.Error_Reporting
;
118 use System
.OS_Interface
;
120 package TSL
renames System
.Soft_Links
;
122 type Interrupt_List
is array (Interrupt_ID
range <>) of Interrupt_ID
;
123 Exception_Interrupts
: constant Interrupt_List
:=
124 (SIGFPE
, SIGILL
, SIGSEGV
);
126 Unreserve_All_Interrupts
: Interfaces
.C
.int
;
128 (C
, Unreserve_All_Interrupts
, "__gl_unreserve_all_interrupts");
130 subtype int
is Interfaces
.C
.int
;
131 subtype unsigned_short
is Interfaces
.C
.unsigned_short
;
132 subtype unsigned_long
is Interfaces
.C
.unsigned_long
;
134 ----------------------
135 -- Notify_Exception --
136 ----------------------
138 Signal_Mask
: aliased sigset_t
;
139 -- The set of signals handled by Notify_Exception
141 -- This function identifies the Ada exception to be raised using
142 -- the information when the system received a synchronous signal.
143 -- Since this function is machine and OS dependent, different code
144 -- has to be provided for different target.
146 procedure Notify_Exception
160 trapno
: unsigned_long
;
164 eflags
: unsigned_long
;
165 esp_at_signal
: unsigned_long
;
167 fpstate
: System
.Address
;
168 oldmask
: unsigned_long
;
169 cr2
: unsigned_long
);
171 procedure Notify_Exception
185 trapno
: unsigned_long
;
189 eflags
: unsigned_long
;
190 esp_at_signal
: unsigned_long
;
192 fpstate
: System
.Address
;
193 oldmask
: unsigned_long
;
197 function To_Machine_State_Ptr
is new
198 Unchecked_Conversion
(Address
, Machine_State_Ptr
);
200 -- These are not directly visible
202 procedure Raise_From_Signal_Handler
203 (E
: Ada
.Exceptions
.Exception_Id
;
206 (Ada
, Raise_From_Signal_Handler
,
207 "ada__exceptions__raise_from_signal_handler");
208 pragma No_Return
(Raise_From_Signal_Handler
);
210 mstate
: Machine_State_Ptr
;
211 message
: aliased constant String := "" & ASCII
.Nul
;
212 -- a null terminated String.
218 -- Raise_From_Signal_Handler makes sure that the exception is raised
219 -- safely from this signal handler.
221 -- ??? The original signal mask (the one we had before coming into this
222 -- signal catching function) should be restored by
223 -- Raise_From_Signal_Handler. For now, restore it explicitely
225 Result
:= pthread_sigmask
(SIG_UNBLOCK
, Signal_Mask
'Access, null);
226 pragma Assert
(Result
= 0);
228 -- Check that treatment of exception propagation here
229 -- is consistent with treatment of the abort signal in
230 -- System.Task_Primitives.Operations.
232 mstate
:= To_Machine_State_Ptr
(TSL
.Get_Machine_State_Addr
.all);
235 mstate
.esp
:= esp_at_signal
;
242 Raise_From_Signal_Handler
243 (Constraint_Error
'Identity, message
'Address);
245 Raise_From_Signal_Handler
246 (Constraint_Error
'Identity, message
'Address);
248 Raise_From_Signal_Handler
249 (Storage_Error
'Identity, message
'Address);
251 if Shutdown
("Unexpected signal") then
255 end Notify_Exception
;
257 ---------------------------
258 -- Initialize_Interrupts --
259 ---------------------------
261 -- Nothing needs to be done on this platform.
263 procedure Initialize_Interrupts
is
266 end Initialize_Interrupts
;
270 act
: aliased struct_sigaction
;
271 old_act
: aliased struct_sigaction
;
276 -- Need to call pthread_init very early because it is doing signal
281 Abort_Task_Interrupt
:= SIGADAABORT
;
283 act
.sa_handler
:= Notify_Exception
'Address;
286 -- On some targets, we set sa_flags to SA_NODEFER so that during the
287 -- handler execution we do not change the Signal_Mask to be masked for
289 -- This is a temporary fix to the problem that the Signal_Mask is
290 -- not restored after the exception (longjmp) from the handler.
291 -- The right fix should be made in sigsetjmp so that we save
292 -- the Signal_Set and restore it after a longjmp.
293 -- Since SA_NODEFER is obsolete, instead we reset explicitely
294 -- the mask in the exception handler.
296 Result
:= sigemptyset
(Signal_Mask
'Access);
297 pragma Assert
(Result
= 0);
299 for J
in Exception_Interrupts
'Range loop
301 sigaddset
(Signal_Mask
'Access, Signal
(Exception_Interrupts
(J
)));
302 pragma Assert
(Result
= 0);
305 act
.sa_mask
:= Signal_Mask
;
309 (Signal
(SIGFPE
), act
'Unchecked_Access,
310 old_act
'Unchecked_Access);
311 pragma Assert
(Result
= 0);
313 for J
in Exception_Interrupts
'First + 1 .. Exception_Interrupts
'Last loop
314 Keep_Unmasked
(Exception_Interrupts
(J
)) := True;
315 if Unreserve_All_Interrupts
= 0 then
318 (Signal
(Exception_Interrupts
(J
)),
319 act
'Unchecked_Access,
320 old_act
'Unchecked_Access);
321 pragma Assert
(Result
= 0);
325 Keep_Unmasked
(Abort_Task_Interrupt
) := True;
326 Keep_Unmasked
(SIGXCPU
) := True;
327 Keep_Unmasked
(SIGBUS
) := True;
328 Keep_Unmasked
(SIGFPE
) := True;
330 -- By keeping SIGINT unmasked, allow the user to do a Ctrl-C, but in the
331 -- same time, disable the ability of handling this signal
332 -- via Ada.Interrupts.
333 -- The pragma Unreserve_All_Interrupts let the user the ability to
334 -- change this behavior.
336 if Unreserve_All_Interrupts
= 0 then
337 Keep_Unmasked
(SIGINT
) := True;
340 for J
in Unmasked
'Range loop
341 Keep_Unmasked
(Interrupt_ID
(Unmasked
(J
))) := True;
344 Reserve
:= Keep_Unmasked
or Keep_Masked
;
346 for J
in Reserved
'Range loop
347 Reserve
(Interrupt_ID
(Reserved
(J
))) := True;
351 -- We do not have Signal 0 in reality. We just use this value
352 -- to identify non-existent signals (see s-intnam.ads). Therefore,
353 -- Signal 0 should not be used in all signal related operations hence
354 -- mark it as reserved.
357 end System
.Interrupt_Management
;