1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . I N T E R R U P T S --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
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. --
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. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
34 -- Note: the compiler generates direct calls to this interface, via Rtsfind.
35 -- Any changes to this interface may require corresponding compiler changes.
37 -- This package encapsulates the implementation of interrupt or signal
38 -- handlers. It is logically an extension of the body of Ada.Interrupts. It
39 -- is made a child of System to allow visibility of various runtime system
40 -- internal data and operations.
42 -- See System.Interrupt_Management for core interrupt/signal interfaces
44 -- These two packages are separated to allow System.Interrupt_Management to be
45 -- used without requiring the whole tasking implementation to be linked and
49 with System
.Tasking
.Protected_Objects
.Entries
;
50 with System
.OS_Interface
;
52 package System
.Interrupts
is
54 pragma Elaborate_Body
;
55 -- Comment needed on why this is here ???
57 -------------------------
58 -- Constants and types --
59 -------------------------
61 Default_Interrupt_Priority
: constant System
.Interrupt_Priority
:=
62 System
.Interrupt_Priority
'Last;
63 -- Default value used when a pragma Interrupt_Handler or Attach_Handler is
64 -- specified without an Interrupt_Priority pragma, see D.3(10).
66 type Ada_Interrupt_ID
is range 0 .. System
.OS_Interface
.Max_Interrupt
;
67 -- Avoid inheritance by Ada.Interrupts.Interrupt_ID of unwanted operations
69 type Interrupt_ID
is range 0 .. System
.OS_Interface
.Max_Interrupt
;
71 subtype System_Interrupt_Id
is Interrupt_ID
;
72 -- This synonym is introduced so that the type is accessible through
73 -- rtsfind, otherwise the name clashes with its homonym in Ada.Interrupts.
75 type Parameterless_Handler
is access protected procedure;
77 ----------------------
78 -- General services --
79 ----------------------
81 -- Attempt to attach a Handler to an Interrupt to which an Entry is
82 -- already bound will raise a Program_Error.
84 function Is_Reserved
(Interrupt
: Interrupt_ID
) return Boolean;
86 function Is_Entry_Attached
(Interrupt
: Interrupt_ID
) return Boolean;
88 function Is_Handler_Attached
(Interrupt
: Interrupt_ID
) return Boolean;
90 function Current_Handler
91 (Interrupt
: Interrupt_ID
) return Parameterless_Handler
;
93 -- Calling the following procedures with New_Handler = null and Static =
94 -- true means that we want to modify the current handler regardless of the
95 -- previous handler's binding status. (i.e. we do not care whether it is a
96 -- dynamic or static handler)
98 procedure Attach_Handler
99 (New_Handler
: Parameterless_Handler
;
100 Interrupt
: Interrupt_ID
;
101 Static
: Boolean := False);
103 procedure Exchange_Handler
104 (Old_Handler
: out Parameterless_Handler
;
105 New_Handler
: Parameterless_Handler
;
106 Interrupt
: Interrupt_ID
;
107 Static
: Boolean := False);
109 procedure Detach_Handler
110 (Interrupt
: Interrupt_ID
;
111 Static
: Boolean := False);
114 (Interrupt
: Interrupt_ID
) return System
.Address
;
116 --------------------------------
117 -- Interrupt Entries Services --
118 --------------------------------
120 -- Routines needed for Interrupt Entries
122 procedure Bind_Interrupt_To_Entry
123 (T
: System
.Tasking
.Task_Id
;
124 E
: System
.Tasking
.Task_Entry_Index
;
125 Int_Ref
: System
.Address
);
126 -- Bind the given interrupt to the given entry. If the interrupt is
127 -- already bound to another entry, Program_Error will be raised.
129 procedure Detach_Interrupt_Entries
(T
: System
.Tasking
.Task_Id
);
130 -- This procedure detaches all the Interrupt Entries bound to a task
132 ------------------------------
133 -- POSIX.5 Signals Services --
134 ------------------------------
136 -- Routines needed for POSIX dot5 POSIX_Signals
138 procedure Block_Interrupt
(Interrupt
: Interrupt_ID
);
139 -- Block the Interrupt on the process level
141 procedure Unblock_Interrupt
(Interrupt
: Interrupt_ID
);
143 function Unblocked_By
144 (Interrupt
: Interrupt_ID
) return System
.Tasking
.Task_Id
;
145 -- It returns the ID of the last Task which Unblocked this Interrupt.
146 -- It returns Null_Task if no tasks have ever requested the Unblocking
147 -- operation or the Interrupt is currently Blocked.
149 function Is_Blocked
(Interrupt
: Interrupt_ID
) return Boolean;
150 -- Comment needed ???
152 procedure Ignore_Interrupt
(Interrupt
: Interrupt_ID
);
153 -- Set the sigaction for the interrupt to SIG_IGN
155 procedure Unignore_Interrupt
(Interrupt
: Interrupt_ID
);
156 -- Comment needed ???
158 function Is_Ignored
(Interrupt
: Interrupt_ID
) return Boolean;
159 -- Comment needed ???
161 -- Note : Direct calls to sigaction, sigprocmask, thr_sigsetmask or any
162 -- other low-level interface that changes the signal action or signal mask
163 -- needs a careful thought.
165 -- One may achieve the effect of system calls first making RTS blocked (by
166 -- calling Block_Interrupt) for the signal under consideration. This will
167 -- make all the tasks in RTS blocked for the Interrupt.
169 ----------------------
170 -- Protection Types --
171 ----------------------
173 -- Routines and types needed to implement Interrupt_Handler and
176 -- There are two kinds of protected objects that deal with interrupts:
178 -- (1) Only Interrupt_Handler pragmas are used. We need to be able to tell
179 -- if an Interrupt_Handler applies to a given procedure, so
180 -- Register_Interrupt_Handler has to be called for all the potential
181 -- handlers, it should be done by calling Register_Interrupt_Handler with
182 -- the handler code address. On finalization, which can happen only has
183 -- part of library level finalization since PO with Interrupt_Handler
184 -- pragmas can only be declared at library level, nothing special needs to
185 -- be done since the default handlers have been restored as part of task
186 -- completion which is done just before global finalization.
187 -- Dynamic_Interrupt_Protection should be used in this case.
189 -- (2) Attach_Handler pragmas are used, and possibly Interrupt_Handler
190 -- pragma. We need to attach the handlers to the given interrupts when the
191 -- object is elaborated. This should be done by constructing an array of
192 -- pairs (interrupt, handler) from the pragmas and calling Install_Handlers
193 -- with it (types to be used are New_Handler_Item and New_Handler_Array).
194 -- On finalization, we need to restore the handlers that were installed
195 -- before the elaboration of the PO, so we need to store these previous
196 -- handlers. This is also done by Install_Handlers, the room for these
197 -- informations is provided by adding a discriminant which is the number
198 -- of Attach_Handler pragmas and an array of this size in the protection
199 -- type, Static_Interrupt_Protection.
201 procedure Register_Interrupt_Handler
202 (Handler_Addr
: System
.Address
);
203 -- This routine should be called by the compiler to allow the handler be
204 -- used as an Interrupt Handler. That means call this procedure for each
205 -- pragma Interrupt_Handler providing the address of the handler (not
206 -- including the pointer to the actual PO, this way this routine is called
207 -- only once for each type definition of PO).
209 type Static_Handler_Index
is range 0 .. Integer'Last;
210 subtype Positive_Static_Handler_Index
is
211 Static_Handler_Index
range 1 .. Static_Handler_Index
'Last;
212 -- Comment needed ???
214 type Previous_Handler_Item
is record
215 Interrupt
: Interrupt_ID
;
216 Handler
: Parameterless_Handler
;
219 -- Contains all the information needed to restore a previous handler
221 type Previous_Handler_Array
is array
222 (Positive_Static_Handler_Index
range <>) of Previous_Handler_Item
;
224 type New_Handler_Item
is record
225 Interrupt
: Interrupt_ID
;
226 Handler
: Parameterless_Handler
;
228 -- Contains all the information from an Attach_Handler pragma
230 type New_Handler_Array
is
231 array (Positive_Static_Handler_Index
range <>) of New_Handler_Item
;
232 -- Comment needed ???
236 type Dynamic_Interrupt_Protection
is new
237 Tasking
.Protected_Objects
.Entries
.Protection_Entries
with null record;
239 -- ??? Finalize is not overloaded since we currently have no
240 -- way to detach the handlers during library level finalization.
242 function Has_Interrupt_Or_Attach_Handler
243 (Object
: access Dynamic_Interrupt_Protection
) return Boolean;
248 type Static_Interrupt_Protection
249 (Num_Entries
: Tasking
.Protected_Objects
.Protected_Entry_Index
;
250 Num_Attach_Handler
: Static_Handler_Index
)
252 Tasking
.Protected_Objects
.Entries
.Protection_Entries
(Num_Entries
) with
254 Previous_Handlers
: Previous_Handler_Array
(1 .. Num_Attach_Handler
);
257 function Has_Interrupt_Or_Attach_Handler
258 (Object
: access Static_Interrupt_Protection
) return Boolean;
261 procedure Finalize
(Object
: in out Static_Interrupt_Protection
);
262 -- Restore previous handlers as required by C.3.1(12) then call
263 -- Finalize (Protection).
265 procedure Install_Handlers
266 (Object
: access Static_Interrupt_Protection
;
267 New_Handlers
: New_Handler_Array
);
268 -- Store the old handlers in Object.Previous_Handlers and install
269 -- the new static handlers.
271 procedure Install_Restricted_Handlers
(Handlers
: New_Handler_Array
);
272 -- Install the static Handlers for the given interrupts and do not store
273 -- previously installed handlers. This procedure is used when the Ravenscar
274 -- restrictions are in place since in that case there are only
275 -- library-level protected handlers that will be installed at
276 -- initialization and never be replaced.
278 end System
.Interrupts
;