OSDN Git Service

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