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) 1998-2014, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 -- This is the NT version of this package
34 with Ada.Task_Identification;
35 with Ada.Unchecked_Conversion;
39 with System.Storage_Elements;
40 with System.Task_Primitives.Operations;
41 with System.Tasking.Utilities;
42 with System.Tasking.Rendezvous;
43 with System.Tasking.Initialization;
44 with System.Interrupt_Management;
45 with System.Parameters;
47 package body System.Interrupts is
51 use System.OS_Interface;
54 package STPO renames System.Task_Primitives.Operations;
55 package IMNG renames System.Interrupt_Management;
57 subtype int is Interfaces.C.int;
59 function To_System is new Ada.Unchecked_Conversion
60 (Ada.Task_Identification.Task_Id, Task_Id);
62 type Handler_Kind is (Unknown, Task_Entry, Protected_Procedure);
64 type Handler_Desc is record
65 Kind : Handler_Kind := Unknown;
68 H : Parameterless_Handler;
69 Static : Boolean := False;
72 task type Server_Task (Interrupt : Interrupt_ID) is
73 pragma Interrupt_Priority (System.Interrupt_Priority'Last);
76 type Server_Task_Access is access Server_Task;
78 Handlers : array (Interrupt_ID) of Task_Id;
79 Descriptors : array (Interrupt_ID) of Handler_Desc;
80 Interrupt_Count : array (Interrupt_ID) of Integer := (others => 0);
82 pragma Volatile_Components (Interrupt_Count);
84 procedure Attach_Handler
85 (New_Handler : Parameterless_Handler;
86 Interrupt : Interrupt_ID;
88 Restoration : Boolean);
89 -- This internal procedure is needed to finalize protected objects that
90 -- contain interrupt handlers.
92 procedure Signal_Handler (Sig : Interrupt_ID);
93 pragma Convention (C, Signal_Handler);
94 -- This procedure is used to handle all the signals
96 -- Type and Head, Tail of the list containing Registered Interrupt
97 -- Handlers. These definitions are used to register the handlers
98 -- specified by the pragma Interrupt_Handler.
100 --------------------------
101 -- Handler Registration --
102 --------------------------
104 type Registered_Handler;
105 type R_Link is access all Registered_Handler;
107 type Registered_Handler is record
108 H : System.Address := System.Null_Address;
109 Next : R_Link := null;
112 Registered_Handlers : R_Link := null;
114 function Is_Registered (Handler : Parameterless_Handler) return Boolean;
115 -- See if the Handler has been "pragma"ed using Interrupt_Handler.
116 -- Always consider a null handler as registered.
118 type Handler_Ptr is access procedure (Sig : Interrupt_ID);
119 pragma Convention (C, Handler_Ptr);
121 function TISR is new Ada.Unchecked_Conversion (Handler_Ptr, isr_address);
127 procedure Signal_Handler (Sig : Interrupt_ID) is
128 Handler : Task_Id renames Handlers (Sig);
131 if Intr_Attach_Reset and then
132 intr_attach (int (Sig), TISR (Signal_Handler'Access)) = FUNC_ERR
137 if Handler /= null then
138 Interrupt_Count (Sig) := Interrupt_Count (Sig) + 1;
139 STPO.Wakeup (Handler, Interrupt_Server_Idle_Sleep);
147 function Is_Reserved (Interrupt : Interrupt_ID) return Boolean is
149 return IMNG.Reserve (IMNG.Interrupt_ID (Interrupt));
152 -----------------------
153 -- Is_Entry_Attached --
154 -----------------------
156 function Is_Entry_Attached (Interrupt : Interrupt_ID) return Boolean is
158 if Is_Reserved (Interrupt) then
159 raise Program_Error with
160 "interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
163 return Descriptors (Interrupt).T /= Null_Task;
164 end Is_Entry_Attached;
166 -------------------------
167 -- Is_Handler_Attached --
168 -------------------------
170 function Is_Handler_Attached (Interrupt : Interrupt_ID) return Boolean is
172 if Is_Reserved (Interrupt) then
173 raise Program_Error with
174 "interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
176 return Descriptors (Interrupt).Kind /= Unknown;
178 end Is_Handler_Attached;
184 function Is_Ignored (Interrupt : Interrupt_ID) return Boolean is
194 function Unblocked_By (Interrupt : Interrupt_ID) return Task_Id is
200 ----------------------
201 -- Ignore_Interrupt --
202 ----------------------
204 procedure Ignore_Interrupt (Interrupt : Interrupt_ID) is
207 end Ignore_Interrupt;
209 ------------------------
210 -- Unignore_Interrupt --
211 ------------------------
213 procedure Unignore_Interrupt (Interrupt : Interrupt_ID) is
216 end Unignore_Interrupt;
218 -------------------------------------
219 -- Has_Interrupt_Or_Attach_Handler --
220 -------------------------------------
222 function Has_Interrupt_Or_Attach_Handler
223 (Object : access Dynamic_Interrupt_Protection) return Boolean
225 pragma Unreferenced (Object);
228 end Has_Interrupt_Or_Attach_Handler;
234 procedure Finalize (Object : in out Static_Interrupt_Protection) is
236 -- ??? loop to be executed only when we're not doing library level
237 -- finalization, since in this case all interrupt tasks are gone.
239 for N in reverse Object.Previous_Handlers'Range loop
241 (New_Handler => Object.Previous_Handlers (N).Handler,
242 Interrupt => Object.Previous_Handlers (N).Interrupt,
243 Static => Object.Previous_Handlers (N).Static,
244 Restoration => True);
247 Tasking.Protected_Objects.Entries.Finalize
248 (Tasking.Protected_Objects.Entries.Protection_Entries (Object));
251 -------------------------------------
252 -- Has_Interrupt_Or_Attach_Handler --
253 -------------------------------------
255 function Has_Interrupt_Or_Attach_Handler
256 (Object : access Static_Interrupt_Protection) return Boolean
258 pragma Unreferenced (Object);
261 end Has_Interrupt_Or_Attach_Handler;
263 ----------------------
264 -- Install_Handlers --
265 ----------------------
267 procedure Install_Handlers
268 (Object : access Static_Interrupt_Protection;
269 New_Handlers : New_Handler_Array)
272 for N in New_Handlers'Range loop
274 -- We need a lock around this ???
276 Object.Previous_Handlers (N).Interrupt := New_Handlers (N).Interrupt;
277 Object.Previous_Handlers (N).Static := Descriptors
278 (New_Handlers (N).Interrupt).Static;
280 -- We call Exchange_Handler and not directly Interrupt_Manager.
281 -- Exchange_Handler so we get the Is_Reserved check.
284 (Old_Handler => Object.Previous_Handlers (N).Handler,
285 New_Handler => New_Handlers (N).Handler,
286 Interrupt => New_Handlers (N).Interrupt,
289 end Install_Handlers;
291 ---------------------------------
292 -- Install_Restricted_Handlers --
293 ---------------------------------
295 procedure Install_Restricted_Handlers
296 (Prio : Any_Priority;
297 Handlers : New_Handler_Array)
299 pragma Unreferenced (Prio);
301 for N in Handlers'Range loop
302 Attach_Handler (Handlers (N).Handler, Handlers (N).Interrupt, True);
304 end Install_Restricted_Handlers;
306 ---------------------
307 -- Current_Handler --
308 ---------------------
310 function Current_Handler
311 (Interrupt : Interrupt_ID) return Parameterless_Handler
314 if Is_Reserved (Interrupt) then
318 if Descriptors (Interrupt).Kind = Protected_Procedure then
319 return Descriptors (Interrupt).H;
329 procedure Attach_Handler
330 (New_Handler : Parameterless_Handler;
331 Interrupt : Interrupt_ID;
332 Static : Boolean := False)
335 Attach_Handler (New_Handler, Interrupt, Static, False);
338 procedure Attach_Handler
339 (New_Handler : Parameterless_Handler;
340 Interrupt : Interrupt_ID;
342 Restoration : Boolean)
344 New_Task : Server_Task_Access;
347 if Is_Reserved (Interrupt) then
351 if not Restoration and then not Static
353 -- Tries to overwrite a static Interrupt Handler with dynamic handle
356 (Descriptors (Interrupt).Static
358 -- New handler not specified as an Interrupt Handler by a pragma
360 or else not Is_Registered (New_Handler))
362 raise Program_Error with
363 "trying to overwrite a static interrupt handler with a " &
367 if Handlers (Interrupt) = null then
368 New_Task := new Server_Task (Interrupt);
369 Handlers (Interrupt) := To_System (New_Task.all'Identity);
372 if intr_attach (int (Interrupt),
373 TISR (Signal_Handler'Access)) = FUNC_ERR
378 if New_Handler = null then
380 -- The null handler means we are detaching the handler
382 Descriptors (Interrupt) :=
383 (Kind => Unknown, T => null, E => 0, H => null, Static => False);
386 Descriptors (Interrupt).Kind := Protected_Procedure;
387 Descriptors (Interrupt).H := New_Handler;
388 Descriptors (Interrupt).Static := Static;
392 ----------------------
393 -- Exchange_Handler --
394 ----------------------
396 procedure Exchange_Handler
397 (Old_Handler : out Parameterless_Handler;
398 New_Handler : Parameterless_Handler;
399 Interrupt : Interrupt_ID;
400 Static : Boolean := False)
403 if Is_Reserved (Interrupt) then
407 if Descriptors (Interrupt).Kind = Task_Entry then
409 -- In case we have an Interrupt Entry already installed, raise a
410 -- program error (propagate it to the caller).
412 raise Program_Error with "an interrupt is already installed";
415 Old_Handler := Current_Handler (Interrupt);
416 Attach_Handler (New_Handler, Interrupt, Static);
418 end Exchange_Handler;
424 procedure Detach_Handler
425 (Interrupt : Interrupt_ID;
426 Static : Boolean := False)
429 if Is_Reserved (Interrupt) then
433 if Descriptors (Interrupt).Kind = Task_Entry then
434 raise Program_Error with "trying to detach an interrupt entry";
437 if not Static and then Descriptors (Interrupt).Static then
438 raise Program_Error with
439 "trying to detach a static interrupt handler";
442 Descriptors (Interrupt) :=
443 (Kind => Unknown, T => null, E => 0, H => null, Static => False);
445 if intr_attach (int (Interrupt), null) = FUNC_ERR then
454 function Reference (Interrupt : Interrupt_ID) return System.Address is
455 Signal : constant System.Address :=
456 System.Storage_Elements.To_Address
457 (System.Storage_Elements.Integer_Address (Interrupt));
460 if Is_Reserved (Interrupt) then
462 -- Only usable Interrupts can be used for binding it to an Entry
470 --------------------------------
471 -- Register_Interrupt_Handler --
472 --------------------------------
474 procedure Register_Interrupt_Handler (Handler_Addr : System.Address) is
476 Registered_Handlers :=
477 new Registered_Handler'(H => Handler_Addr, Next => Registered_Handlers);
478 end Register_Interrupt_Handler;
484 -- See if the Handler has been "pragma"ed using Interrupt_Handler.
485 -- Always consider a null handler as registered.
487 function Is_Registered (Handler : Parameterless_Handler) return Boolean is
488 Ptr : R_Link := Registered_Handlers;
490 type Fat_Ptr is record
491 Object_Addr : System.Address;
492 Handler_Addr : System.Address;
495 function To_Fat_Ptr is new Ada.Unchecked_Conversion
496 (Parameterless_Handler, Fat_Ptr);
501 if Handler = null then
505 Fat := To_Fat_Ptr (Handler);
507 while Ptr /= null loop
508 if Ptr.H = Fat.Handler_Addr then
518 -----------------------------
519 -- Bind_Interrupt_To_Entry --
520 -----------------------------
522 procedure Bind_Interrupt_To_Entry
524 E : Task_Entry_Index;
525 Int_Ref : System.Address)
527 Interrupt : constant Interrupt_ID :=
528 Interrupt_ID (Storage_Elements.To_Integer (Int_Ref));
530 New_Task : Server_Task_Access;
533 if Is_Reserved (Interrupt) then
537 if Descriptors (Interrupt).Kind /= Unknown then
538 raise Program_Error with
539 "a binding for this interrupt is already present";
542 if Handlers (Interrupt) = null then
543 New_Task := new Server_Task (Interrupt);
544 Handlers (Interrupt) := To_System (New_Task.all'Identity);
547 if intr_attach (int (Interrupt),
548 TISR (Signal_Handler'Access)) = FUNC_ERR
553 Descriptors (Interrupt).Kind := Task_Entry;
554 Descriptors (Interrupt).T := T;
555 Descriptors (Interrupt).E := E;
557 -- Indicate the attachment of Interrupt Entry in ATCB. This is needed so
558 -- that when an Interrupt Entry task terminates the binding can be
559 -- cleaned up. The call to unbinding must be make by the task before it
562 T.Interrupt_Entry := True;
563 end Bind_Interrupt_To_Entry;
565 ------------------------------
566 -- Detach_Interrupt_Entries --
567 ------------------------------
569 procedure Detach_Interrupt_Entries (T : Task_Id) is
571 for J in Interrupt_ID loop
572 if not Is_Reserved (J) then
573 if Descriptors (J).Kind = Task_Entry
574 and then Descriptors (J).T = T
576 Descriptors (J).Kind := Unknown;
578 if intr_attach (int (J), null) = FUNC_ERR then
585 -- Indicate in ATCB that no Interrupt Entries are attached
587 T.Interrupt_Entry := True;
588 end Detach_Interrupt_Entries;
590 ---------------------
591 -- Block_Interrupt --
592 ---------------------
594 procedure Block_Interrupt (Interrupt : Interrupt_ID) is
599 -----------------------
600 -- Unblock_Interrupt --
601 -----------------------
603 procedure Unblock_Interrupt (Interrupt : Interrupt_ID) is
606 end Unblock_Interrupt;
612 function Is_Blocked (Interrupt : Interrupt_ID) return Boolean is
618 task body Server_Task is
619 Desc : Handler_Desc renames Descriptors (Interrupt);
620 Self_Id : constant Task_Id := STPO.Self;
621 Temp : Parameterless_Handler;
624 Utilities.Make_Independent;
627 while Interrupt_Count (Interrupt) > 0 loop
628 Interrupt_Count (Interrupt) := Interrupt_Count (Interrupt) - 1;
634 Rendezvous.Call_Simple (Desc.T, Desc.E, Null_Address);
635 when Protected_Procedure =>
644 Initialization.Defer_Abort (Self_Id);
650 STPO.Write_Lock (Self_Id);
651 Self_Id.Common.State := Interrupt_Server_Idle_Sleep;
652 STPO.Sleep (Self_Id, Interrupt_Server_Idle_Sleep);
653 Self_Id.Common.State := Runnable;
654 STPO.Unlock (Self_Id);
660 Initialization.Undefer_Abort (Self_Id);
662 -- Undefer abort here to allow a window for this task to be aborted
663 -- at the time of system shutdown.
668 end System.Interrupts;