OSDN Git Service

2003-12-11 Ed Falis <falis@gnat.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / 5zintman.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-2003 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 VxWorks version of this package.
35
36 --  It is likely to need tailoring to fit each operating system
37 --  and machine architecture.
38
39 --  PLEASE DO NOT add any dependences on other packages.
40 --  This package is designed to work with or without tasking support.
41
42 --  See the other warnings in the package specification before making
43 --  any modifications to this file.
44
45 --  Make a careful study of all signals available under the OS,
46 --  to see which need to be reserved, kept always unmasked,
47 --  or kept always unmasked.
48 --  Be on the lookout for special signals that
49 --  may be used by the thread library.
50
51 with Interfaces.C;
52
53 with System.OS_Interface;
54 --  used for various Constants, Signal and types
55
56 package body System.Interrupt_Management is
57
58    use System.OS_Interface;
59    use type Interfaces.C.int;
60
61    type Signal_List is array (Signal_ID range <>) of Signal_ID;
62    Exception_Signals : constant Signal_List (1 .. 4) :=
63                          (SIGFPE, SIGILL, SIGSEGV, SIGBUS);
64
65    --  Keep these variables global so that they are initialized only once
66    --  What are "these variables" ???, I see only one
67
68    Exception_Action : aliased struct_sigaction;
69
70    procedure Map_And_Raise_Exception (signo : Signal);
71    pragma Import (C, Map_And_Raise_Exception, "__gnat_map_signal");
72    --  Map signal to Ada exception and raise it.  Different versions
73    --  of VxWorks need different mappings.
74
75    -----------------------
76    -- Local Subprograms --
77    -----------------------
78
79    procedure Notify_Exception (signo : Signal);
80    --  Identify the Ada exception to be raised using
81    --  the information when the system received a synchronous signal.
82
83    ----------------------
84    -- Notify_Exception --
85    ----------------------
86
87    procedure Notify_Exception (signo : Signal) is
88       Mask   : aliased sigset_t;
89       Result : int;
90       My_Id  : t_id;
91
92    begin
93       Result := pthread_sigmask (SIG_SETMASK, null, Mask'Unchecked_Access);
94       Result := sigdelset (Mask'Access, signo);
95       Result := pthread_sigmask (SIG_SETMASK, Mask'Unchecked_Access, null);
96
97       --  VxWorks will suspend the task when it gets a hardware
98       --  exception.  We take the liberty of resuming the task
99       --  for the application.
100
101       My_Id := taskIdSelf;
102
103       if taskIsSuspended (My_Id) /= 0 then
104          Result := taskResume (My_Id);
105       end if;
106
107       Map_And_Raise_Exception (signo);
108    end Notify_Exception;
109
110    ---------------------------
111    -- Initialize_Interrupts --
112    ---------------------------
113
114    --  Since there is no signal inheritance between VxWorks tasks, we need
115    --  to initialize signal handling in each task.
116
117    procedure Initialize_Interrupts is
118       Result  : int;
119       old_act : aliased struct_sigaction;
120
121    begin
122       for J in Exception_Signals'Range loop
123          Result :=
124            sigaction
125              (Signal (Exception_Signals (J)), Exception_Action'Access,
126               old_act'Unchecked_Access);
127          pragma Assert (Result = 0);
128       end loop;
129    end Initialize_Interrupts;
130
131 begin
132    declare
133       mask   : aliased sigset_t;
134       Result : int;
135
136       function State (Int : Interrupt_ID) return Character;
137       pragma Import (C, State, "__gnat_get_interrupt_state");
138       --  Get interrupt state.  Defined in a-init.c
139       --  The input argument is the interrupt number,
140       --  and the result is one of the following:
141
142       Runtime : constant Character := 'r';
143       Default : constant Character := 's';
144       --    'n'   this interrupt not set by any Interrupt_State pragma
145       --    'u'   Interrupt_State pragma set state to User
146       --    'r'   Interrupt_State pragma set state to Runtime
147       --    's'   Interrupt_State pragma set state to System (use "default"
148       --           system handler)
149
150    begin
151       --  Initialize signal handling
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_Signal := SIGABRT;
157
158       Exception_Action.sa_handler := Notify_Exception'Address;
159       Exception_Action.sa_flags := SA_ONSTACK;
160       Result := sigemptyset (mask'Access);
161       pragma Assert (Result = 0);
162
163       for J in Exception_Signals'Range loop
164          Result := sigaddset (mask'Access, Signal (Exception_Signals (J)));
165          pragma Assert (Result = 0);
166       end loop;
167
168       Exception_Action.sa_mask := mask;
169
170       --  Initialize hardware interrupt handling
171
172       pragma Assert (Reserve = (Interrupt_ID'Range => False));
173
174       --  Check all interrupts for state that requires keeping them reserved
175
176       for J in Interrupt_ID'Range loop
177          if State (J) = Default or else State (J) = Runtime then
178             Reserve (J) := True;
179          end if;
180       end loop;
181
182       --  Add exception signals to the set of unmasked signals
183
184       for J in Exception_Signals'Range loop
185          Keep_Unmasked (Exception_Signals (J)) := True;
186       end loop;
187
188       --  The abort signal must also be unmasked
189
190       Keep_Unmasked (Abort_Task_Signal) := True;
191    end;
192 end System.Interrupt_Management;