OSDN Git Service

* sysdep.c: Problem discovered during IA64 VMS port.
[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-2002 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 with Ada.Exceptions;
57 --  used for Raise_Exception
58
59 package body System.Interrupt_Management is
60
61    use Ada.Exceptions;
62    use System.OS_Interface;
63    use type Interfaces.C.int;
64
65    type Interrupt_List is array (Interrupt_ID range <>) of Interrupt_ID;
66    Exception_Interrupts : constant Interrupt_List (1 .. 4) :=
67      (SIGFPE, SIGILL, SIGSEGV, SIGBUS);
68
69    --  Keep these variables global so that they are initialized only once.
70
71    Exception_Action : aliased struct_sigaction;
72
73    ----------------------
74    -- Notify_Exception --
75    ----------------------
76
77    procedure Notify_Exception (signo : Signal);
78    --  Identify the Ada exception to be raised using
79    --  the information when the system received a synchronous signal.
80
81    procedure Notify_Exception (signo : Signal) is
82       Mask   : aliased sigset_t;
83       Result : int;
84       My_Id  : t_id;
85
86    begin
87       Result := pthread_sigmask (SIG_SETMASK, null, Mask'Unchecked_Access);
88       Result := sigdelset (Mask'Access, signo);
89       Result := pthread_sigmask (SIG_SETMASK, Mask'Unchecked_Access, null);
90
91       --  VxWorks will suspend the task when it gets a hardware
92       --  exception.  We take the liberty of resuming the task
93       --  for the application.
94
95       My_Id := taskIdSelf;
96
97       if taskIsSuspended (My_Id) /= 0 then
98          Result := taskResume (My_Id);
99       end if;
100
101       case signo is
102          when SIGFPE =>
103             Raise_Exception (Constraint_Error'Identity, "SIGFPE");
104          when SIGILL =>
105             Raise_Exception (Constraint_Error'Identity, "SIGILL");
106          when SIGSEGV =>
107             Raise_Exception
108               (Program_Error'Identity,
109                "stack overflow or erroneous memory access");
110          when SIGBUS =>
111             Raise_Exception (Program_Error'Identity, "SIGBUS");
112          when others =>
113             Raise_Exception (Program_Error'Identity, "unhandled signal");
114       end case;
115    end Notify_Exception;
116
117    ---------------------------
118    -- Initialize_Interrupts --
119    ---------------------------
120
121    --  Since there is no signal inheritance between VxWorks tasks, we need
122    --  to initialize signal handling in each task.
123
124    procedure Initialize_Interrupts is
125       Result  : int;
126       old_act : aliased struct_sigaction;
127
128    begin
129       for J in Exception_Interrupts'Range loop
130          Result :=
131            sigaction
132              (Signal (Exception_Interrupts (J)), Exception_Action'Access,
133               old_act'Unchecked_Access);
134          pragma Assert (Result = 0);
135       end loop;
136    end Initialize_Interrupts;
137
138 begin
139    declare
140       mask   : aliased sigset_t;
141       Result : int;
142
143       function State (Int : Interrupt_ID) return Character;
144       pragma Import (C, State, "__gnat_get_interrupt_state");
145       --  Get interrupt state.  Defined in a-init.c
146       --  The input argument is the interrupt number,
147       --  and the result is one of the following:
148
149       Runtime : constant Character := 'r';
150       Default : constant Character := 's';
151       --    'n'   this interrupt not set by any Interrupt_State pragma
152       --    'u'   Interrupt_State pragma set state to User
153       --    'r'   Interrupt_State pragma set state to Runtime
154       --    's'   Interrupt_State pragma set state to System (use "default"
155       --           system handler)
156
157    begin
158       --  Initialize signal handling
159
160       --  Change this if you want to use another signal for task abort.
161       --  SIGTERM might be a good one.
162
163       Abort_Task_Interrupt := SIGABRT;
164
165       Exception_Action.sa_handler := Notify_Exception'Address;
166       Exception_Action.sa_flags := SA_ONSTACK;
167       Result := sigemptyset (mask'Access);
168       pragma Assert (Result = 0);
169
170       for J in Exception_Interrupts'Range loop
171          Result := sigaddset (mask'Access, Signal (Exception_Interrupts (J)));
172          pragma Assert (Result = 0);
173       end loop;
174
175       Exception_Action.sa_mask := mask;
176
177       --  Initialize hardware interrupt handling
178
179       pragma Assert (Reserve = (Interrupt_ID'Range => False));
180
181       --  Check all interrupts for state that requires keeping them reserved
182
183       for J in Interrupt_ID'Range loop
184          if State (J) = Default or else State (J) = Runtime then
185             Reserve (J) := True;
186          end if;
187       end loop;
188    end;
189 end System.Interrupt_Management;