OSDN Git Service

2007-04-20 Vincent Celier <celier@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-intman-solaris.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 a Solaris version of this package.
35
36 --  Make a careful study of all signals available under the OS,
37 --  to see which need to be reserved, kept always unmasked,
38 --  or kept always unmasked.
39
40 --  Be on the lookout for special signals that
41 --  may be used by the thread library.
42
43 package body System.Interrupt_Management is
44
45    use Interfaces.C;
46    use System.OS_Interface;
47
48    type Interrupt_List is array (Interrupt_ID range <>) of Interrupt_ID;
49
50    Exception_Interrupts : constant Interrupt_List :=
51      (SIGFPE, SIGILL, SIGSEGV, SIGBUS);
52
53    Unreserve_All_Interrupts : Interfaces.C.int;
54    pragma Import
55      (C, Unreserve_All_Interrupts, "__gl_unreserve_all_interrupts");
56
57    function State (Int : Interrupt_ID) return Character;
58    pragma Import (C, State, "__gnat_get_interrupt_state");
59    --  Get interrupt state.  Defined in init.c
60    --  The input argument is the interrupt number,
61    --  and the result is one of the following:
62
63    User    : constant Character := 'u';
64    Runtime : constant Character := 'r';
65    Default : constant Character := 's';
66    --    'n'   this interrupt not set by any Interrupt_State pragma
67    --    'u'   Interrupt_State pragma set state to User
68    --    'r'   Interrupt_State pragma set state to Runtime
69    --    's'   Interrupt_State pragma set state to System (use "default"
70    --           system handler)
71
72    ----------------------
73    -- Notify_Exception --
74    ----------------------
75
76    --  This function identifies the Ada exception to be raised using
77    --  the information when the system received a synchronous signal.
78    --  Since this function is machine and OS dependent, different code
79    --  has to be provided for different target.
80
81    procedure Notify_Exception
82      (signo   : Signal;
83       info    : access siginfo_t;
84       context : access ucontext_t);
85
86    ----------------------
87    -- Notify_Exception --
88    ----------------------
89
90    procedure Notify_Exception
91      (signo   : Signal;
92       info    : access siginfo_t;
93       context : access ucontext_t)
94    is
95       pragma Unreferenced (context);
96    begin
97       --  Check that treatment of exception propagation here
98       --  is consistent with treatment of the abort signal in
99       --  System.Task_Primitives.Operations.
100
101       case signo is
102          when SIGFPE =>
103             case info.si_code is
104                when  FPE_INTDIV |
105                      FPE_INTOVF |
106                      FPE_FLTDIV |
107                      FPE_FLTOVF |
108                      FPE_FLTUND |
109                      FPE_FLTRES |
110                      FPE_FLTINV |
111                      FPE_FLTSUB =>
112
113                   raise Constraint_Error;
114
115                when others =>
116                   pragma Assert (False);
117                   null;
118             end case;
119
120          when SIGILL | SIGSEGV | SIGBUS  =>
121             raise Storage_Error;
122
123          when others =>
124             pragma Assert (False);
125             null;
126       end case;
127    end Notify_Exception;
128
129    ----------------
130    -- Initialize --
131    ----------------
132
133    Initialized : Boolean := False;
134
135    procedure Initialize is
136       act     : aliased struct_sigaction;
137       old_act : aliased struct_sigaction;
138       mask    : aliased sigset_t;
139       Result  : Interfaces.C.int;
140
141    begin
142       if Initialized then
143          return;
144       end if;
145
146       Initialized := True;
147
148       --  Need to call pthread_init very early because it is doing signal
149       --  initializations.
150
151       pthread_init;
152
153       --  Change this if you want to use another signal for task abort.
154       --  SIGTERM might be a good one.
155
156       Abort_Task_Interrupt := SIGABRT;
157
158       act.sa_handler := Notify_Exception'Address;
159
160       --  Set sa_flags to SA_NODEFER so that during the handler execution
161       --  we do not change the Signal_Mask to be masked for the Signal.
162       --  This is a temporary fix to the problem that the Signal_Mask is
163       --  not restored after the exception (longjmp) from the handler.
164       --  The right fix should be made in sigsetjmp so that we save
165       --  the Signal_Set and restore it after a longjmp.
166
167       --  In that case, this field should be changed back to 0. ??? (Dong-Ik)
168
169       act.sa_flags := 16;
170
171       Result := sigemptyset (mask'Access);
172       pragma Assert (Result = 0);
173
174       --  ??? For the same reason explained above, we can't mask these
175       --  signals because otherwise we won't be able to catch more than
176       --  one signal.
177
178       act.sa_mask := mask;
179
180       pragma Assert (Keep_Unmasked = (Interrupt_ID'Range => False));
181       pragma Assert (Reserve = (Interrupt_ID'Range => False));
182
183       for J in Exception_Interrupts'Range loop
184          if State (Exception_Interrupts (J)) /= User then
185             Keep_Unmasked (Exception_Interrupts (J)) := True;
186             Reserve (Exception_Interrupts (J)) := True;
187
188             if State (Exception_Interrupts (J)) /= Default then
189                Result :=
190                  sigaction
191                  (Signal (Exception_Interrupts (J)), act'Unchecked_Access,
192                   old_act'Unchecked_Access);
193                pragma Assert (Result = 0);
194             end if;
195          end if;
196       end loop;
197
198       if State (Abort_Task_Interrupt) /= User then
199          Keep_Unmasked (Abort_Task_Interrupt) := True;
200          Reserve (Abort_Task_Interrupt) := True;
201       end if;
202
203       --  Set SIGINT to unmasked state as long as it's
204       --  not in "User" state.  Check for Unreserve_All_Interrupts last
205
206       if State (SIGINT) /= User then
207          Keep_Unmasked (SIGINT) := True;
208          Reserve (SIGINT) := True;
209       end if;
210
211       --  Check all signals for state that requires keeping them
212       --  unmasked and reserved
213
214       for J in Interrupt_ID'Range loop
215          if State (J) = Default or else State (J) = Runtime then
216             Keep_Unmasked (J) := True;
217             Reserve (J) := True;
218          end if;
219       end loop;
220
221       --  Add the set of signals that must always be unmasked for this target
222
223       for J in Unmasked'Range loop
224          Keep_Unmasked (Interrupt_ID (Unmasked (J))) := True;
225          Reserve (Interrupt_ID (Unmasked (J))) := True;
226       end loop;
227
228       --  Add target-specific reserved signals
229
230       for J in Reserved'Range loop
231          Reserve (Interrupt_ID (Reserved (J))) := True;
232       end loop;
233
234       --  Process pragma Unreserve_All_Interrupts. This overrides any
235       --  settings due to pragma Interrupt_State:
236
237       if Unreserve_All_Interrupts /= 0 then
238          Keep_Unmasked (SIGINT) := False;
239          Reserve (SIGINT) := False;
240       end if;
241
242       --  We do not have Signal 0 in reality. We just use this value
243       --  to identify not existing signals (see s-intnam.ads). Therefore,
244       --  Signal 0 should not be used in all signal related operations hence
245       --  mark it as reserved.
246
247       Reserve (0) := True;
248    end Initialize;
249
250 end System.Interrupt_Management;