1 ------------------------------------------------------------------------------
3 -- GNU ADA 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-2003, 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 ------------------------------------------------------------------------------
36 -- All user-handleable signals are masked at all times in all
37 -- tasks/threads except possibly for the Interrupt_Manager task.
39 -- When a user task wants to have the effect of masking/unmasking an
40 -- signal, it must call Block_Interrupt/Unblock_Interrupt, which
41 -- will have the effect of unmasking/masking the signal in the
42 -- Interrupt_Manager task. These comments do not apply to vectored
43 -- hardware interrupts, which may be masked or unmasked using routined
44 -- interfaced to the relevant VxWorks system calls.
46 -- Once we associate a Signal_Server_Task with an signal, the task never
47 -- goes away, and we never remove the association. On the other hand, it
48 -- is more convenient to terminate an associated Interrupt_Server_Task
49 -- for a vectored hardware interrupt (since we use a binary semaphore
50 -- for synchronization with the umbrella handler).
52 -- There is no more than one signal per Signal_Server_Task and no more than
53 -- one Signal_Server_Task per signal. The same relation holds for hardware
54 -- interrupts and Interrupt_Server_Task's at any given time. That is,
55 -- only one non-terminated Interrupt_Server_Task exists for a give
56 -- interrupt at any time.
58 -- Within this package, the lock L is used to protect the various status
59 -- tables. If there is a Server_Task associated with a signal or interrupt,
60 -- we use the per-task lock of the Server_Task instead so that we protect the
61 -- status between Interrupt_Manager and Server_Task. Protection among
62 -- service requests are ensured via user calls to the Interrupt_Manager
65 -- This is the VxWorks version of this package, supporting vectored hardware
68 with Unchecked_Conversion;
70 with System.OS_Interface; use System.OS_Interface;
72 with Interfaces.VxWorks;
74 with Ada.Task_Identification;
75 -- used for Task_ID type
78 -- used for Raise_Exception
80 with System.Interrupt_Management;
83 with System.Task_Primitives.Operations;
84 -- used for Write_Lock
91 with System.Storage_Elements;
92 -- used for To_Address
101 -- Interrupt_Manager_ID
103 with System.Tasking.Utilities;
104 -- used for Make_Independent
106 with System.Tasking.Rendezvous;
107 -- used for Call_Simple
108 pragma Elaborate_All (System.Tasking.Rendezvous);
110 package body System.Interrupts is
115 package POP renames System.Task_Primitives.Operations;
117 function To_Ada is new Unchecked_Conversion
118 (System.Tasking.Task_ID, Ada.Task_Identification.Task_Id);
120 function To_System is new Unchecked_Conversion
121 (Ada.Task_Identification.Task_Id, Task_ID);
127 -- WARNING: System.Tasking.Stages performs calls to this task
128 -- with low-level constructs. Do not change this spec without synchro-
131 task Interrupt_Manager is
132 entry Detach_Interrupt_Entries (T : Task_ID);
135 (New_Handler : Parameterless_Handler;
136 Interrupt : Interrupt_ID;
138 Restoration : Boolean := False);
140 entry Exchange_Handler
141 (Old_Handler : out Parameterless_Handler;
142 New_Handler : Parameterless_Handler;
143 Interrupt : Interrupt_ID;
147 (Interrupt : Interrupt_ID;
150 entry Bind_Interrupt_To_Entry
152 E : Task_Entry_Index;
153 Interrupt : Interrupt_ID);
155 pragma Interrupt_Priority (System.Interrupt_Priority'First);
156 end Interrupt_Manager;
158 task type Interrupt_Server_Task
159 (Interrupt : Interrupt_ID; Int_Sema : SEM_ID) is
160 -- Server task for vectored hardware interrupt handling
161 pragma Interrupt_Priority (System.Interrupt_Priority'First + 2);
162 end Interrupt_Server_Task;
164 type Interrupt_Task_Access is access Interrupt_Server_Task;
166 -------------------------------
167 -- Local Types and Variables --
168 -------------------------------
170 type Entry_Assoc is record
172 E : Task_Entry_Index;
175 type Handler_Assoc is record
176 H : Parameterless_Handler;
177 Static : Boolean; -- Indicates static binding;
180 User_Handler : array (Interrupt_ID) of Handler_Assoc :=
181 (others => (null, Static => False));
182 pragma Volatile_Components (User_Handler);
183 -- Holds the protected procedure handler (if any) and its Static
184 -- information for each interrupt or signal. A handler is static
185 -- iff it is specified through the pragma Attach_Handler.
187 User_Entry : array (Interrupt_ID) of Entry_Assoc :=
188 (others => (T => Null_Task, E => Null_Task_Entry));
189 pragma Volatile_Components (User_Entry);
190 -- Holds the task and entry index (if any) for each interrupt / signal
192 -- Type and Head, Tail of the list containing Registered Interrupt
193 -- Handlers. These definitions are used to register the handlers
194 -- specified by the pragma Interrupt_Handler.
196 type Registered_Handler;
197 type R_Link is access all Registered_Handler;
199 type Registered_Handler is record
200 H : System.Address := System.Null_Address;
201 Next : R_Link := null;
204 Registered_Handler_Head : R_Link := null;
205 Registered_Handler_Tail : R_Link := null;
207 Server_ID : array (Interrupt_ID) of System.Tasking.Task_ID :=
208 (others => System.Tasking.Null_Task);
209 pragma Atomic_Components (Server_ID);
210 -- Holds the Task_ID of the Server_Task for each interrupt / signal.
211 -- Task_ID is needed to accomplish locking per interrupt base. Also
212 -- is needed to determine whether to create a new Server_Task.
214 Semaphore_ID_Map : array
215 (Interrupt_ID range 0 .. System.OS_Interface.Max_HW_Interrupt)
216 of SEM_ID := (others => 0);
217 -- Array of binary semaphores associated with vectored interrupts
218 -- Note that the last bound should be Max_HW_Interrupt, but this will raise
219 -- Storage_Error if Num_HW_Interrupts is null, so use an extra 4 bytes
222 Interrupt_Access_Hold : Interrupt_Task_Access;
223 -- Variable for allocating an Interrupt_Server_Task
225 Default_Handler : array (HW_Interrupt) of Interfaces.VxWorks.VOIDFUNCPTR;
226 -- Vectored interrupt handlers installed prior to program startup.
227 -- These are saved only when the umbrella handler is installed for
228 -- a given interrupt number.
230 -----------------------
231 -- Local Subprograms --
232 -----------------------
234 procedure Check_Reserved_Interrupt (Interrupt : Interrupt_ID);
235 -- Check if Id is a reserved interrupt, and if so raise Program_Error
236 -- with an appropriate message, otherwise return.
238 procedure Finalize_Interrupt_Servers;
239 -- Unbind the handlers for hardware interrupt server tasks at program
242 function Is_Registered (Handler : Parameterless_Handler) return Boolean;
243 -- See if Handler has been "pragma"ed using Interrupt_Handler.
244 -- Always consider a null handler as registered.
246 procedure Notify_Interrupt (Param : System.Address);
247 -- Umbrella handler for vectored interrupts (not signals)
249 procedure Install_Default_Action (Interrupt : HW_Interrupt);
250 -- Restore a handler that was in place prior to program execution
252 procedure Install_Umbrella_Handler
253 (Interrupt : HW_Interrupt;
254 Handler : Interfaces.VxWorks.VOIDFUNCPTR);
255 -- Install the runtime umbrella handler for a vectored hardware
258 procedure Unimplemented (Feature : String);
259 pragma No_Return (Unimplemented);
260 -- Used to mark a call to an unimplemented function. Raises Program_Error
261 -- with an appropriate message noting that Feature is unimplemented.
267 -- Calling this procedure with New_Handler = null and Static = True
268 -- means we want to detach the current handler regardless of the
269 -- previous handler's binding status (ie. do not care if it is a
270 -- dynamic or static handler).
272 -- This option is needed so that during the finalization of a PO, we
273 -- can detach handlers attached through pragma Attach_Handler.
275 procedure Attach_Handler
276 (New_Handler : Parameterless_Handler;
277 Interrupt : Interrupt_ID;
278 Static : Boolean := False) is
280 Check_Reserved_Interrupt (Interrupt);
281 Interrupt_Manager.Attach_Handler (New_Handler, Interrupt, Static);
284 -----------------------------
285 -- Bind_Interrupt_To_Entry --
286 -----------------------------
288 -- This procedure raises a Program_Error if it tries to
289 -- bind an interrupt to which an Entry or a Procedure is
292 procedure Bind_Interrupt_To_Entry
294 E : Task_Entry_Index;
295 Int_Ref : System.Address)
297 Interrupt : constant Interrupt_ID :=
298 Interrupt_ID (Storage_Elements.To_Integer (Int_Ref));
301 Check_Reserved_Interrupt (Interrupt);
302 Interrupt_Manager.Bind_Interrupt_To_Entry (T, E, Interrupt);
303 end Bind_Interrupt_To_Entry;
305 ---------------------
306 -- Block_Interrupt --
307 ---------------------
309 procedure Block_Interrupt (Interrupt : Interrupt_ID) is
311 Unimplemented ("Block_Interrupt");
314 ------------------------------
315 -- Check_Reserved_Interrupt --
316 ------------------------------
318 procedure Check_Reserved_Interrupt (Interrupt : Interrupt_ID) is
320 if Is_Reserved (Interrupt) then
322 (Program_Error'Identity,
323 "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved");
327 end Check_Reserved_Interrupt;
329 ---------------------
330 -- Current_Handler --
331 ---------------------
333 function Current_Handler
334 (Interrupt : Interrupt_ID) return Parameterless_Handler is
336 Check_Reserved_Interrupt (Interrupt);
338 -- ??? Since Parameterless_Handler is not Atomic, the
339 -- current implementation is wrong. We need a new service in
340 -- Interrupt_Manager to ensure atomicity.
342 return User_Handler (Interrupt).H;
349 -- Calling this procedure with Static = True means we want to Detach the
350 -- current handler regardless of the previous handler's binding status
351 -- (i.e. do not care if it is a dynamic or static handler).
353 -- This option is needed so that during the finalization of a PO, we can
354 -- detach handlers attached through pragma Attach_Handler.
356 procedure Detach_Handler
357 (Interrupt : Interrupt_ID;
358 Static : Boolean := False) is
360 Check_Reserved_Interrupt (Interrupt);
361 Interrupt_Manager.Detach_Handler (Interrupt, Static);
364 ------------------------------
365 -- Detach_Interrupt_Entries --
366 ------------------------------
368 procedure Detach_Interrupt_Entries (T : Task_ID) is
370 Interrupt_Manager.Detach_Interrupt_Entries (T);
371 end Detach_Interrupt_Entries;
373 ----------------------
374 -- Exchange_Handler --
375 ----------------------
377 -- Calling this procedure with New_Handler = null and Static = True
378 -- means we want to detach the current handler regardless of the
379 -- previous handler's binding status (ie. do not care if it is a
380 -- dynamic or static handler).
382 -- This option is needed so that during the finalization of a PO, we
383 -- can detach handlers attached through pragma Attach_Handler.
385 procedure Exchange_Handler
386 (Old_Handler : out Parameterless_Handler;
387 New_Handler : Parameterless_Handler;
388 Interrupt : Interrupt_ID;
389 Static : Boolean := False) is
391 Check_Reserved_Interrupt (Interrupt);
392 Interrupt_Manager.Exchange_Handler
393 (Old_Handler, New_Handler, Interrupt, Static);
394 end Exchange_Handler;
400 procedure Finalize (Object : in out Static_Interrupt_Protection) is
402 -- ??? loop to be executed only when we're not doing library level
403 -- finalization, since in this case all interrupt / signal tasks are
406 if not Interrupt_Manager'Terminated then
407 for N in reverse Object.Previous_Handlers'Range loop
408 Interrupt_Manager.Attach_Handler
409 (New_Handler => Object.Previous_Handlers (N).Handler,
410 Interrupt => Object.Previous_Handlers (N).Interrupt,
411 Static => Object.Previous_Handlers (N).Static,
412 Restoration => True);
416 Tasking.Protected_Objects.Entries.Finalize
417 (Tasking.Protected_Objects.Entries.Protection_Entries (Object));
420 --------------------------------
421 -- Finalize_Interrupt_Servers --
422 --------------------------------
424 -- Restore default handlers for interrupt servers.
425 -- This is called by the Interrupt_Manager task when it receives the abort
426 -- signal during program finalization.
428 procedure Finalize_Interrupt_Servers is
430 if HW_Interrupt'Last >= 0 then
431 for Int in HW_Interrupt loop
432 if Server_ID (Interrupt_ID (Int)) /= null
434 not Ada.Task_Identification.Is_Terminated
435 (To_Ada (Server_ID (Interrupt_ID (Int))))
437 Interrupt_Manager.Attach_Handler
438 (New_Handler => null,
439 Interrupt => Interrupt_ID (Int),
441 Restoration => True);
445 end Finalize_Interrupt_Servers;
447 -------------------------------------
448 -- Has_Interrupt_Or_Attach_Handler --
449 -------------------------------------
451 function Has_Interrupt_Or_Attach_Handler
452 (Object : access Dynamic_Interrupt_Protection)
455 pragma Unreferenced (Object);
459 end Has_Interrupt_Or_Attach_Handler;
461 function Has_Interrupt_Or_Attach_Handler
462 (Object : access Static_Interrupt_Protection)
465 pragma Unreferenced (Object);
469 end Has_Interrupt_Or_Attach_Handler;
471 ----------------------
472 -- Ignore_Interrupt --
473 ----------------------
475 procedure Ignore_Interrupt (Interrupt : Interrupt_ID) is
477 Unimplemented ("Ignore_Interrupt");
478 end Ignore_Interrupt;
480 ----------------------------
481 -- Install_Default_Action --
482 ----------------------------
484 procedure Install_Default_Action (Interrupt : HW_Interrupt) is
486 -- Restore original interrupt handler
488 Interfaces.VxWorks.intVecSet
489 (Interfaces.VxWorks.INUM_TO_IVEC (Integer (Interrupt)),
490 Default_Handler (Interrupt));
491 Default_Handler (Interrupt) := null;
492 end Install_Default_Action;
494 ----------------------
495 -- Install_Handlers --
496 ----------------------
498 procedure Install_Handlers
499 (Object : access Static_Interrupt_Protection;
500 New_Handlers : New_Handler_Array) is
502 for N in New_Handlers'Range loop
503 -- We need a lock around this ???
505 Object.Previous_Handlers (N).Interrupt := New_Handlers (N).Interrupt;
506 Object.Previous_Handlers (N).Static := User_Handler
507 (New_Handlers (N).Interrupt).Static;
509 -- We call Exchange_Handler and not directly Interrupt_Manager.
510 -- Exchange_Handler so we get the Is_Reserved check.
513 (Old_Handler => Object.Previous_Handlers (N).Handler,
514 New_Handler => New_Handlers (N).Handler,
515 Interrupt => New_Handlers (N).Interrupt,
518 end Install_Handlers;
520 ------------------------------
521 -- Install_Umbrella_Handler --
522 ------------------------------
524 procedure Install_Umbrella_Handler
525 (Interrupt : HW_Interrupt;
526 Handler : Interfaces.VxWorks.VOIDFUNCPTR)
528 use Interfaces.VxWorks;
530 Vec : constant Interrupt_Vector :=
531 INUM_TO_IVEC (Interfaces.VxWorks.int (Interrupt));
532 Old_Handler : constant VOIDFUNCPTR :=
533 intVecGet (INUM_TO_IVEC (Interfaces.VxWorks.int (Interrupt)));
534 Stat : Interfaces.VxWorks.STATUS;
537 -- Only install umbrella handler when no Ada handler has already been
538 -- installed. Note that the interrupt number is passed as a parameter
539 -- when an interrupt occurs, so the umbrella handler has a different
540 -- wrapper generated by intConnect for each interrupt number.
542 if Default_Handler (Interrupt) = null then
544 intConnect (Vec, VOIDFUNCPTR (Handler), System.Address (Interrupt));
545 Default_Handler (Interrupt) := Old_Handler;
547 end Install_Umbrella_Handler;
553 function Is_Blocked (Interrupt : Interrupt_ID) return Boolean is
555 Unimplemented ("Is_Blocked");
559 -----------------------
560 -- Is_Entry_Attached --
561 -----------------------
563 function Is_Entry_Attached (Interrupt : Interrupt_ID) return Boolean is
565 Check_Reserved_Interrupt (Interrupt);
566 return User_Entry (Interrupt).T /= Null_Task;
567 end Is_Entry_Attached;
569 -------------------------
570 -- Is_Handler_Attached --
571 -------------------------
573 function Is_Handler_Attached (Interrupt : Interrupt_ID) return Boolean is
575 Check_Reserved_Interrupt (Interrupt);
576 return User_Handler (Interrupt).H /= null;
577 end Is_Handler_Attached;
583 function Is_Ignored (Interrupt : Interrupt_ID) return Boolean is
585 Unimplemented ("Is_Ignored");
593 function Is_Registered (Handler : Parameterless_Handler) return Boolean is
594 type Fat_Ptr is record
595 Object_Addr : System.Address;
596 Handler_Addr : System.Address;
599 function To_Fat_Ptr is new Unchecked_Conversion
600 (Parameterless_Handler, Fat_Ptr);
606 if Handler = null then
610 Fat := To_Fat_Ptr (Handler);
612 Ptr := Registered_Handler_Head;
614 while (Ptr /= null) loop
615 if Ptr.H = Fat.Handler_Addr then
629 function Is_Reserved (Interrupt : Interrupt_ID) return Boolean is
630 use System.Interrupt_Management;
632 return Reserve (System.Interrupt_Management.Interrupt_ID (Interrupt));
635 ----------------------
636 -- Notify_Interrupt --
637 ----------------------
639 -- Umbrella handler for vectored hardware interrupts (as opposed to
640 -- signals and exceptions). As opposed to the signal implementation,
641 -- this handler is only installed in the vector table while there is
642 -- an active association of an Ada handler to the interrupt.
644 -- Otherwise, the handler that existed prior to program startup is
645 -- in the vector table. This ensures that handlers installed by
646 -- the BSP are active unless explicitly replaced in the program text.
648 -- Each Interrupt_Server_Task has an associated binary semaphore
649 -- on which it pends once it's been started. This routine determines
650 -- The appropriate semaphore and and issues a semGive call, waking
651 -- the server task. When a handler is unbound,
652 -- System.Interrupts.Unbind_Handler issues a semFlush, and the
653 -- server task deletes its semaphore and terminates.
655 procedure Notify_Interrupt (Param : System.Address) is
656 Interrupt : Interrupt_ID := Interrupt_ID (Param);
657 Discard_Result : STATUS;
660 Discard_Result := semGive (Semaphore_ID_Map (Interrupt));
661 end Notify_Interrupt;
667 function Reference (Interrupt : Interrupt_ID) return System.Address is
669 Check_Reserved_Interrupt (Interrupt);
670 return Storage_Elements.To_Address
671 (Storage_Elements.Integer_Address (Interrupt));
674 --------------------------------
675 -- Register_Interrupt_Handler --
676 --------------------------------
678 procedure Register_Interrupt_Handler (Handler_Addr : System.Address) is
679 New_Node_Ptr : R_Link;
681 -- This routine registers a handler as usable for dynamic
682 -- interrupt handler association. Routines attaching and detaching
683 -- handlers dynamically should determine whether the handler is
684 -- registered. Program_Error should be raised if it is not registered.
686 -- Pragma Interrupt_Handler can only appear in a library
687 -- level PO definition and instantiation. Therefore, we do not need
688 -- to implement an unregister operation. Nor do we need to
689 -- protect the queue structure with a lock.
691 pragma Assert (Handler_Addr /= System.Null_Address);
693 New_Node_Ptr := new Registered_Handler;
694 New_Node_Ptr.H := Handler_Addr;
696 if Registered_Handler_Head = null then
697 Registered_Handler_Head := New_Node_Ptr;
698 Registered_Handler_Tail := New_Node_Ptr;
701 Registered_Handler_Tail.Next := New_Node_Ptr;
702 Registered_Handler_Tail := New_Node_Ptr;
704 end Register_Interrupt_Handler;
706 -----------------------
707 -- Unblock_Interrupt --
708 -----------------------
710 procedure Unblock_Interrupt (Interrupt : Interrupt_ID) is
712 Unimplemented ("Unblock_Interrupt");
713 end Unblock_Interrupt;
719 function Unblocked_By
720 (Interrupt : Interrupt_ID) return System.Tasking.Task_ID is
722 Unimplemented ("Unblocked_By");
726 ------------------------
727 -- Unignore_Interrupt --
728 ------------------------
730 procedure Unignore_Interrupt (Interrupt : Interrupt_ID) is
732 Unimplemented ("Unignore_Interrupt");
733 end Unignore_Interrupt;
739 procedure Unimplemented (Feature : String) is
742 (Program_Error'Identity,
743 Feature & " not implemented on VxWorks");
746 -----------------------
747 -- Interrupt_Manager --
748 -----------------------
750 task body Interrupt_Manager is
756 procedure Bind_Handler (Interrupt : Interrupt_ID);
757 -- This procedure does not do anything if a signal is blocked.
758 -- Otherwise, we have to interrupt Server_Task for status change through
761 procedure Unbind_Handler (Interrupt : Interrupt_ID);
762 -- This procedure does not do anything if a signal is blocked.
763 -- Otherwise, we have to interrupt Server_Task for status change
764 -- through an abort signal.
766 procedure Unprotected_Exchange_Handler
767 (Old_Handler : out Parameterless_Handler;
768 New_Handler : Parameterless_Handler;
769 Interrupt : Interrupt_ID;
771 Restoration : Boolean := False);
773 procedure Unprotected_Detach_Handler
774 (Interrupt : Interrupt_ID;
781 procedure Bind_Handler (Interrupt : Interrupt_ID) is
783 Install_Umbrella_Handler
784 (HW_Interrupt (Interrupt), Notify_Interrupt'Access);
791 procedure Unbind_Handler (Interrupt : Interrupt_ID) is
796 -- Hardware interrupt
798 Install_Default_Action (HW_Interrupt (Interrupt));
800 -- Flush server task off semaphore, allowing it to terminate
802 S := semFlush (Semaphore_ID_Map (Interrupt));
803 pragma Assert (S = 0);
806 --------------------------------
807 -- Unprotected_Detach_Handler --
808 --------------------------------
810 procedure Unprotected_Detach_Handler
811 (Interrupt : Interrupt_ID;
814 Old_Handler : Parameterless_Handler;
816 if User_Entry (Interrupt).T /= Null_Task then
817 -- If an interrupt entry is installed raise
818 -- Program_Error. (propagate it to the caller).
820 Raise_Exception (Program_Error'Identity,
821 "An interrupt entry is already installed");
824 -- Note : Static = True will pass the following check. This is the
825 -- case when we want to detach a handler regardless of the static
826 -- status of the Current_Handler.
828 if not Static and then User_Handler (Interrupt).Static then
829 -- Trying to detach a static Interrupt Handler.
830 -- raise Program_Error.
832 Raise_Exception (Program_Error'Identity,
833 "Trying to detach a static Interrupt Handler");
836 Old_Handler := User_Handler (Interrupt).H;
840 User_Handler (Interrupt).H := null;
841 User_Handler (Interrupt).Static := False;
843 if Old_Handler /= null then
844 Unbind_Handler (Interrupt);
846 end Unprotected_Detach_Handler;
848 ----------------------------------
849 -- Unprotected_Exchange_Handler --
850 ----------------------------------
852 procedure Unprotected_Exchange_Handler
853 (Old_Handler : out Parameterless_Handler;
854 New_Handler : Parameterless_Handler;
855 Interrupt : Interrupt_ID;
857 Restoration : Boolean := False) is
859 if User_Entry (Interrupt).T /= Null_Task then
860 -- If an interrupt entry is already installed, raise
861 -- Program_Error. (propagate it to the caller).
864 (Program_Error'Identity,
865 "An interrupt is already installed");
868 -- Note : A null handler with Static = True will
869 -- pass the following check. This is the case when we want to
870 -- detach a handler regardless of the Static status
871 -- of Current_Handler.
872 -- We don't check anything if Restoration is True, since we
873 -- may be detaching a static handler to restore a dynamic one.
875 if not Restoration and then not Static
876 and then (User_Handler (Interrupt).Static
878 -- Trying to overwrite a static Interrupt Handler with a
881 -- The new handler is not specified as an
882 -- Interrupt Handler by a pragma.
884 or else not Is_Registered (New_Handler))
887 (Program_Error'Identity,
888 "Trying to overwrite a static Interrupt Handler with a " &
892 -- Save the old handler
894 Old_Handler := User_Handler (Interrupt).H;
898 User_Handler (Interrupt).H := New_Handler;
900 if New_Handler = null then
902 -- The null handler means we are detaching the handler.
904 User_Handler (Interrupt).Static := False;
907 User_Handler (Interrupt).Static := Static;
910 -- Invoke a corresponding Server_Task if not yet created.
911 -- Place Task_ID info in Server_ID array.
913 if New_Handler /= null
915 (Server_ID (Interrupt) = Null_Task
917 Ada.Task_Identification.Is_Terminated
918 (To_Ada (Server_ID (Interrupt))))
920 Interrupt_Access_Hold :=
921 new Interrupt_Server_Task
922 (Interrupt, semBCreate (SEM_Q_FIFO, SEM_EMPTY));
923 Server_ID (Interrupt) :=
924 To_System (Interrupt_Access_Hold.all'Identity);
927 if (New_Handler = null) and then Old_Handler /= null then
928 -- Restore default handler
930 Unbind_Handler (Interrupt);
932 elsif Old_Handler = null then
933 -- Save default handler
935 Bind_Handler (Interrupt);
937 end Unprotected_Exchange_Handler;
939 -- Start of processing for Interrupt_Manager
942 -- By making this task independent of any master, when the process
943 -- goes away, the Interrupt_Manager will terminate gracefully.
945 System.Tasking.Utilities.Make_Independent;
948 -- A block is needed to absorb Program_Error exception
951 Old_Handler : Parameterless_Handler;
955 accept Attach_Handler
956 (New_Handler : Parameterless_Handler;
957 Interrupt : Interrupt_ID;
959 Restoration : Boolean := False)
961 Unprotected_Exchange_Handler
962 (Old_Handler, New_Handler, Interrupt, Static, Restoration);
966 accept Exchange_Handler
967 (Old_Handler : out Parameterless_Handler;
968 New_Handler : Parameterless_Handler;
969 Interrupt : Interrupt_ID;
972 Unprotected_Exchange_Handler
973 (Old_Handler, New_Handler, Interrupt, Static);
974 end Exchange_Handler;
977 accept Detach_Handler
978 (Interrupt : Interrupt_ID;
981 Unprotected_Detach_Handler (Interrupt, Static);
984 accept Bind_Interrupt_To_Entry
986 E : Task_Entry_Index;
987 Interrupt : Interrupt_ID)
989 -- If there is a binding already (either a procedure or an
990 -- entry), raise Program_Error (propagate it to the caller).
992 if User_Handler (Interrupt).H /= null
993 or else User_Entry (Interrupt).T /= Null_Task
996 (Program_Error'Identity,
997 "A binding for this interrupt is already present");
1000 User_Entry (Interrupt) := Entry_Assoc'(T => T, E => E);
1002 -- Indicate the attachment of interrupt entry in the ATCB.
1003 -- This is needed so when an interrupt entry task terminates
1004 -- the binding can be cleaned. The call to unbinding must be
1005 -- make by the task before it terminates.
1007 T.Interrupt_Entry := True;
1009 -- Invoke a corresponding Server_Task if not yet created.
1010 -- Place Task_ID info in Server_ID array.
1012 if Server_ID (Interrupt) = Null_Task
1014 Ada.Task_Identification.Is_Terminated
1015 (To_Ada (Server_ID (Interrupt)))
1017 Interrupt_Access_Hold := new Interrupt_Server_Task
1018 (Interrupt, semBCreate (SEM_Q_FIFO, SEM_EMPTY));
1019 Server_ID (Interrupt) :=
1020 To_System (Interrupt_Access_Hold.all'Identity);
1023 Bind_Handler (Interrupt);
1024 end Bind_Interrupt_To_Entry;
1027 accept Detach_Interrupt_Entries (T : Task_ID) do
1028 for Int in Interrupt_ID'Range loop
1029 if not Is_Reserved (Int) then
1030 if User_Entry (Int).T = T then
1033 (T => Null_Task, E => Null_Task_Entry);
1034 Unbind_Handler (Int);
1039 -- Indicate in ATCB that no interrupt entries are attached.
1041 T.Interrupt_Entry := False;
1042 end Detach_Interrupt_Entries;
1046 -- If there is a Program_Error we just want to propagate it to
1047 -- the caller and do not want to stop this task.
1049 when Program_Error =>
1053 pragma Assert (False);
1059 when Standard'Abort_Signal =>
1060 -- Flush interrupt server semaphores, so they can terminate
1061 Finalize_Interrupt_Servers;
1063 end Interrupt_Manager;
1065 ---------------------------
1066 -- Interrupt_Server_Task --
1067 ---------------------------
1069 -- Server task for vectored hardware interrupt handling
1071 task body Interrupt_Server_Task is
1072 Self_Id : constant Task_ID := Self;
1073 Tmp_Handler : Parameterless_Handler;
1075 Tmp_Entry_Index : Task_Entry_Index;
1081 System.Tasking.Utilities.Make_Independent;
1082 Semaphore_ID_Map (Interrupt) := Int_Sema;
1085 -- Pend on semaphore that will be triggered by the
1086 -- umbrella handler when the associated interrupt comes in
1088 S := semTake (Int_Sema, WAIT_FOREVER);
1089 pragma Assert (S = 0);
1091 if User_Handler (Interrupt).H /= null then
1093 -- Protected procedure handler
1095 Tmp_Handler := User_Handler (Interrupt).H;
1098 elsif User_Entry (Interrupt).T /= Null_Task then
1100 -- Interrupt entry handler
1102 Tmp_ID := User_Entry (Interrupt).T;
1103 Tmp_Entry_Index := User_Entry (Interrupt).E;
1104 System.Tasking.Rendezvous.Call_Simple
1105 (Tmp_ID, Tmp_Entry_Index, System.Null_Address);
1108 -- Semaphore has been flushed by an unbind operation in
1109 -- the Interrupt_Manager. Terminate the server task.
1111 -- Wait for the Interrupt_Manager to complete its work
1113 POP.Write_Lock (Self_Id);
1115 -- Delete the associated semaphore
1117 S := semDelete (Int_Sema);
1119 pragma Assert (S = 0);
1121 -- Set status for the Interrupt_Manager
1123 Semaphore_ID_Map (Interrupt) := 0;
1124 Server_ID (Interrupt) := Null_Task;
1125 POP.Unlock (Self_Id);
1130 end Interrupt_Server_Task;
1133 -- Get Interrupt_Manager's ID so that Abort_Interrupt can be sent.
1135 Interrupt_Manager_ID := To_System (Interrupt_Manager'Identity);
1136 end System.Interrupts;