OSDN Git Service

2005-03-08 Geert Bosch <bosch@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-intman-posix.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                GNU ADA 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-2005, 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 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.                                                      --
21 --                                                                          --
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.                                      --
28 --                                                                          --
29 -- GNARL was developed by the GNARL team at Florida State University.       --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc.     --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  This is the POSIX threads version of this package
35
36 --  PLEASE DO NOT add any dependences on other packages. ??? why not ???
37 --  This package is designed to work with or without tasking support.
38
39 --  See the other warnings in the package specification before making
40 --  any modifications to this file.
41
42 --  Make a careful study of all signals available under the OS, to see which
43 --  need to be reserved, kept always unmasked, or kept always unmasked. Be on
44 --  the lookout for special signals that may be used by the thread library.
45
46 --  Since this is a multi target file, the signal <-> exception mapping
47 --  is simple minded. If you need a more precise and target specific
48 --  signal handling, create a new s-intman.adb that will fit your needs.
49
50 --  This file assumes that:
51
52 --    SIGFPE, SIGILL, SIGSEGV and SIGBUS exist. They are mapped as follows:
53 --      SIGPFE  => Constraint_Error
54 --      SIGILL  => Program_Error
55 --      SIGSEGV => Storage_Error
56 --      SIGBUS  => Storage_Error
57
58 --    SIGINT exists and will be kept unmasked unless the pragma
59 --     Unreserve_All_Interrupts is specified anywhere in the application.
60
61 --    System.OS_Interface contains the following:
62 --      SIGADAABORT: the signal that will be used to abort tasks.
63 --      Unmasked: the OS specific set of signals that should be unmasked in
64 --                all the threads. SIGADAABORT is unmasked by
65 --                default
66 --      Reserved: the OS specific set of signals that are reserved.
67
68 with Interfaces.C;
69 --  used for int and other types
70
71 with System.OS_Interface;
72 --  used for various Constants, Signal and types
73
74 package body System.Interrupt_Management is
75
76    use Interfaces.C;
77    use System.OS_Interface;
78
79    type Interrupt_List is array (Interrupt_ID range <>) of Interrupt_ID;
80    Exception_Interrupts : constant Interrupt_List :=
81      (SIGFPE, SIGILL, SIGSEGV, SIGBUS);
82
83    Unreserve_All_Interrupts : Interfaces.C.int;
84    pragma Import
85      (C, Unreserve_All_Interrupts, "__gl_unreserve_all_interrupts");
86
87    -----------------------
88    -- Local Subprograms --
89    -----------------------
90
91    procedure Notify_Exception
92      (signo    : Signal;
93       siginfo  : System.Address;
94       ucontext : System.Address);
95    --  This function identifies the Ada exception to be raised using
96    --  the information when the system received a synchronous signal.
97    --  Since this function is machine and OS dependent, different code
98    --  has to be provided for different target.
99
100    ----------------------
101    -- Notify_Exception --
102    ----------------------
103
104    Signal_Mask : aliased sigset_t;
105    --  The set of signals handled by Notify_Exception
106
107    procedure Notify_Exception
108      (signo    : Signal;
109       siginfo  : System.Address;
110       ucontext : System.Address)
111    is
112       pragma Unreferenced (siginfo);
113
114       --  The GCC unwinder requires adjustments to the signal's machine
115       --  context to be able to properly unwind through the signal handler.
116       --  This is achieved by the target specific subprogram below, provided
117       --  by init.c to be usable by the non-tasking handler also.
118
119       procedure Adjust_Context_For_Raise
120         (signo    : Signal;
121          ucontext : System.Address);
122       pragma Import
123         (C, Adjust_Context_For_Raise, "__gnat_adjust_context_for_raise");
124
125       Result  : Interfaces.C.int;
126
127    begin
128       --  With the __builtin_longjmp, the signal mask is not restored, so we
129       --  need to restore it explicitely.
130
131       Result := pthread_sigmask (SIG_UNBLOCK, Signal_Mask'Access, null);
132       pragma Assert (Result = 0);
133
134       --  Perform the necessary context adjustments required by the GCC/ZCX
135       --  unwinder, harmless in the SJLJ case.
136
137       Adjust_Context_For_Raise (signo, ucontext);
138
139       --  Check that treatment of exception propagation here
140       --  is consistent with treatment of the abort signal in
141       --  System.Task_Primitives.Operations.
142
143       case signo is
144          when SIGFPE =>
145             raise Constraint_Error;
146          when SIGILL =>
147             raise Program_Error;
148          when SIGSEGV =>
149             raise Storage_Error;
150          when SIGBUS =>
151             raise Storage_Error;
152          when others =>
153             null;
154       end case;
155    end Notify_Exception;
156
157    ---------------------------
158    -- Initialize_Interrupts --
159    ---------------------------
160
161    --  Nothing needs to be done on this platform.
162
163    procedure Initialize_Interrupts is
164    begin
165       null;
166    end Initialize_Interrupts;
167
168 -------------------------
169 -- Package Elaboration --
170 -------------------------
171
172 begin
173    declare
174       act     : aliased struct_sigaction;
175       old_act : aliased struct_sigaction;
176       Result  : System.OS_Interface.int;
177
178       function State (Int : Interrupt_ID) return Character;
179       pragma Import (C, State, "__gnat_get_interrupt_state");
180       --  Get interrupt state. Defined in a-init.c
181       --  The input argument is the interrupt number,
182       --  and the result is one of the following:
183
184       User    : constant Character := 'u';
185       Runtime : constant Character := 'r';
186       Default : constant Character := 's';
187       --    'n'   this interrupt not set by any Interrupt_State pragma
188       --    'u'   Interrupt_State pragma set state to User
189       --    'r'   Interrupt_State pragma set state to Runtime
190       --    's'   Interrupt_State pragma set state to System (use "default"
191       --           system handler)
192
193    begin
194       --  Need to call pthread_init very early because it is doing signal
195       --  initializations.
196
197       pthread_init;
198
199       Abort_Task_Interrupt := SIGADAABORT;
200
201       act.sa_handler := Notify_Exception'Address;
202
203       act.sa_flags := SA_SIGINFO;
204
205       --  Setting SA_SIGINFO asks the kernel to pass more than just the signal
206       --  number argument to the handler when it is called. The set of extra
207       --  parameters includes a pointer to the interrupted context, which the
208       --  ZCX propagation scheme needs.
209
210       --  Most man pages for sigaction mention that sa_sigaction should be set
211       --  instead of sa_handler when SA_SIGINFO is on.  In practice, the two
212       --  fields are actually union'ed and located at the same offset.
213
214       --  On some targets, we set sa_flags to SA_NODEFER so that during the
215       --  handler execution we do not change the Signal_Mask to be masked for
216       --  the Signal.
217
218       --  This is a temporary fix to the problem that the Signal_Mask is
219       --  not restored after the exception (longjmp) from the handler.
220       --  The right fix should be made in sigsetjmp so that we save
221       --  the Signal_Set and restore it after a longjmp.
222
223       --  Since SA_NODEFER is obsolete, instead we reset explicitely
224       --  the mask in the exception handler.
225
226       Result := sigemptyset (Signal_Mask'Access);
227       pragma Assert (Result = 0);
228
229       --  Add signals that map to Ada exceptions to the mask.
230       for J in Exception_Interrupts'Range loop
231          if State (Exception_Interrupts (J)) /= Default  then
232             Result :=
233             sigaddset (Signal_Mask'Access, Signal (Exception_Interrupts (J)));
234             pragma Assert (Result = 0);
235          end if;
236       end loop;
237
238       act.sa_mask := Signal_Mask;
239
240       pragma Assert (Keep_Unmasked = (Interrupt_ID'Range => False));
241       pragma Assert (Reserve = (Interrupt_ID'Range => False));
242
243       --  Process state of exception signals
244       for J in Exception_Interrupts'Range loop
245          if State (Exception_Interrupts (J)) /= User then
246             Keep_Unmasked (Exception_Interrupts (J)) := True;
247             Reserve (Exception_Interrupts (J)) := True;
248
249             if State (Exception_Interrupts (J)) /= Default then
250                Result :=
251                  sigaction
252                  (Signal (Exception_Interrupts (J)), act'Unchecked_Access,
253                   old_act'Unchecked_Access);
254                pragma Assert (Result = 0);
255             end if;
256          end if;
257       end loop;
258
259       if State (Abort_Task_Interrupt) /= User then
260          Keep_Unmasked (Abort_Task_Interrupt) := True;
261          Reserve (Abort_Task_Interrupt) := True;
262       end if;
263
264       --  Set SIGINT to unmasked state as long as it is not in "User"
265       --  state. Check for Unreserve_All_Interrupts last
266
267       if State (SIGINT) /= User then
268          Keep_Unmasked (SIGINT) := True;
269          Reserve (SIGINT) := True;
270       end if;
271
272       --  Check all signals for state that requires keeping them
273       --  unmasked and reserved
274
275       for J in Interrupt_ID'Range loop
276          if State (J) = Default or else State (J) = Runtime then
277             Keep_Unmasked (J) := True;
278             Reserve (J) := True;
279          end if;
280       end loop;
281
282       --  Add the set of signals that must always be unmasked for this target
283
284       for J in Unmasked'Range loop
285          Keep_Unmasked (Interrupt_ID (Unmasked (J))) := True;
286          Reserve (Interrupt_ID (Unmasked (J))) := True;
287       end loop;
288
289       --  Add target-specific reserved signals
290
291       for J in Reserved'Range loop
292          Reserve (Interrupt_ID (Reserved (J))) := True;
293       end loop;
294
295       --  Process pragma Unreserve_All_Interrupts. This overrides any
296       --  settings due to pragma Interrupt_State:
297
298       if Unreserve_All_Interrupts /= 0 then
299          Keep_Unmasked (SIGINT) := False;
300          Reserve (SIGINT) := False;
301       end if;
302
303       --  We do not have Signal 0 in reality. We just use this value
304       --  to identify non-existent signals (see s-intnam.ads). Therefore,
305       --  Signal 0 should not be used in all signal related operations hence
306       --  mark it as reserved.
307
308       Reserve (0) := True;
309    end;
310 end System.Interrupt_Management;