Debian Codegraph @

gcc/ada/libgnarl/s-interr__hwint.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . I N T E R R U P T S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, 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 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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 -- This is reasonably generic version of this package, supporting vectored
33 -- hardware interrupts using non-RTOS specific adapter routines which should
34 -- easily implemented on any RTOS capable of supporting GNAT.
35
36 -- Invariants:
37
38 -- There is no more than one interrupt per Interrupt_Server_Task and no more
39 -- than one Interrupt_Server_Task per interrupt. If an interrupt handler is
40 -- detached, the corresponding Interrupt_Server_Task is terminated.
41
42 -- Within this package, the lock L is used to protect the various status
43 -- tables. If there is a Server_Task associated with a signal or interrupt,
44 -- we use the per-task lock of the Server_Task instead so that we protect the
45 -- status between Interrupt_Manager and Server_Task. Protection among service
46 -- requests are ensured via user calls to the Interrupt_Manager entries.
47
48 with Ada.Unchecked_Conversion;
49 with Ada.Task_Identification;
50
51 with Interfaces.C; use Interfaces.C;
52 with System.OS_Interface; use System.OS_Interface;
53 with System.Interrupt_Management;
54 with System.Task_Primitives.Operations;
55 with System.Storage_Elements;
56 with System.Tasking.Utilities;
57
58 with System.Tasking.Rendezvous;
59 pragma Elaborate_All (System.Tasking.Rendezvous);
60
61 package body System.Interrupts is
62
63 use Tasking;
64
65 package POP renames System.Task_Primitives.Operations;
66
67 function To_Ada is new Ada.Unchecked_Conversion
68 (System.Tasking.Task_Id, Ada.Task_Identification.Task_Id);
69
70 function To_System is new Ada.Unchecked_Conversion
71 (Ada.Task_Identification.Task_Id, Task_Id);
72
73 -----------------
74 -- Local Tasks --
75 -----------------
76
77 -- WARNING: System.Tasking.Stages performs calls to this task with low-
78 -- level constructs. Do not change this spec without synchronizing it.
79
80 task Interrupt_Manager is
81 entry Detach_Interrupt_Entries (T : Task_Id);
82
83 entry Attach_Handler
84 (New_Handler : Parameterless_Handler;
85 Interrupt : Interrupt_ID;
86 Static : Boolean;
87 Restoration : Boolean := False);
88
89 entry Exchange_Handler
90 (Old_Handler : out Parameterless_Handler;
91 New_Handler : Parameterless_Handler;
92 Interrupt : Interrupt_ID;
93 Static : Boolean);
94
95 entry Detach_Handler
96 (Interrupt : Interrupt_ID;
97 Static : Boolean);
98
99 entry Bind_Interrupt_To_Entry
100 (T : Task_Id;
101 E : Task_Entry_Index;
102 Interrupt : Interrupt_ID);
103
104 pragma Interrupt_Priority (System.Interrupt_Priority'First);
105 end Interrupt_Manager;
106
107 task type Interrupt_Server_Task
108 (Interrupt : Interrupt_ID;
109 Int_Sema : Binary_Semaphore_Id)
110 is
111 -- Server task for vectored hardware interrupt handling
112
113 pragma Interrupt_Priority (System.Interrupt_Priority'First + 2);
114 end Interrupt_Server_Task;
115
116 type Interrupt_Task_Access is access Interrupt_Server_Task;
117
118 -------------------------------
119 -- Local Types and Variables --
120 -------------------------------
121
122 type Entry_Assoc is record
123 T : Task_Id;
124 E : Task_Entry_Index;
125 end record;
126
127 type Handler_Assoc is record
128 H : Parameterless_Handler;
129 Static : Boolean; -- Indicates static binding;
130 end record;
131
132 User_Handler : array (Interrupt_ID) of Handler_Assoc :=
133 (others => (null, Static => False));
134 pragma Volatile_Components (User_Handler);
135 -- Holds the protected procedure handler (if any) and its Static
136 -- information for each interrupt. A handler is static if and only if it
137 -- is specified through the pragma Attach_Handler.
138
139 User_Entry : array (Interrupt_ID) of Entry_Assoc :=
140 (others => (T => Null_Task, E => Null_Task_Entry));
141 pragma Volatile_Components (User_Entry);
142 -- Holds the task and entry index (if any) for each interrupt
143
144 -- Type and the list containing Registered Interrupt Handlers. These
145 -- definitions are used to register the handlers specified by the pragma
146 -- Interrupt_Handler.
147
148 --------------------------
149 -- Handler Registration --
150 --------------------------
151
152 type Registered_Handler;
153 type R_Link is access all Registered_Handler;
154
155 type Registered_Handler is record
156 H : System.Address;
157 Next : R_Link;
158 end record;
159
160 Registered_Handlers : R_Link := null;
161
162 Server_ID : array (Interrupt_ID) of System.Tasking.Task_Id :=
163 (others => System.Tasking.Null_Task);
164 pragma Atomic_Components (Server_ID);
165 -- Holds the Task_Id of the Server_Task for each interrupt / signal.
166 -- Task_Id is needed to accomplish locking per interrupt base. Also
167 -- is needed to determine whether to create a new Server_Task.
168
169 Semaphore_ID_Map : array
170 (Interrupt_ID range 0 .. System.OS_Interface.Max_HW_Interrupt) of
171 Binary_Semaphore_Id := (others => 0);
172 -- Array of binary semaphores associated with vectored interrupts. Note
173 -- that the last bound should be Max_HW_Interrupt, but this will raise
174 -- Storage_Error if Num_HW_Interrupts is null so use extra 4 bytes instead.
175
176 Interrupt_Access_Hold : Interrupt_Task_Access;
177 -- Variable for allocating an Interrupt_Server_Task
178
179 Handler_Installed : array (HW_Interrupt) of Boolean := (others => False);
180 -- True if Notify_Interrupt was connected to the interrupt. Handlers can
181 -- be connected but disconnection is not possible on VxWorks. Therefore
182 -- we ensure Notify_Installed is connected at most once.
183
184 -----------------------
185 -- Local Subprograms --
186 -----------------------
187
188 procedure Check_Reserved_Interrupt (Interrupt : Interrupt_ID);
189 -- Check if Id is a reserved interrupt, and if so raise Program_Error
190 -- with an appropriate message, otherwise return.
191
192 procedure Finalize_Interrupt_Servers;
193 -- Unbind the handlers for hardware interrupt server tasks at program
194 -- termination.
195
196 function Is_Registered (Handler : Parameterless_Handler) return Boolean;
197 -- See if Handler has been "pragma"ed using Interrupt_Handler.
198 -- Always consider a null handler as registered.
199
200 procedure Notify_Interrupt (Param : System.Address);
201 pragma Convention (C, Notify_Interrupt);
202 -- Umbrella handler for vectored interrupts (not signals)
203
204 procedure Install_Umbrella_Handler
205 (Interrupt : HW_Interrupt;
206 Handler : System.OS_Interface.Interrupt_Handler);
207 -- Install the runtime umbrella handler for a vectored hardware
208 -- interrupt
209
210 procedure Unimplemented (Feature : String);
211 pragma No_Return (Unimplemented);
212 -- Used to mark a call to an unimplemented function. Raises Program_Error
213 -- with an appropriate message noting that Feature is unimplemented.
214
215 --------------------
216 -- Attach_Handler --
217 --------------------
218
219 -- Calling this procedure with New_Handler = null and Static = True
220 -- means we want to detach the current handler regardless of the previous
221 -- handler's binding status (i.e. do not care if it is a dynamic or static
222 -- handler).
223
224 -- This option is needed so that during the finalization of a PO, we can
225 -- detach handlers attached through pragma Attach_Handler.
226
227 procedure Attach_Handler
228 (New_Handler : Parameterless_Handler;
229 Interrupt : Interrupt_ID;
230 Static : Boolean := False) is
231 begin
232 Check_Reserved_Interrupt (Interrupt);
233 Interrupt_Manager.Attach_Handler (New_Handler, Interrupt, Static);
234 end Attach_Handler;
235
236 -----------------------------
237 -- Bind_Interrupt_To_Entry --
238 -----------------------------
239
240 -- This procedure raises a Program_Error if it tries to
241 -- bind an interrupt to which an Entry or a Procedure is
242 -- already bound.
243
244 procedure Bind_Interrupt_To_Entry
245 (T : Task_Id;
246 E : Task_Entry_Index;
247 Int_Ref : System.Address)
248 is
249 Interrupt : constant Interrupt_ID :=
250 Interrupt_ID (Storage_Elements.To_Integer (Int_Ref));
251 begin
252 Check_Reserved_Interrupt (Interrupt);
253 Interrupt_Manager.Bind_Interrupt_To_Entry (T, E, Interrupt);
254 end Bind_Interrupt_To_Entry;
255
256 ---------------------
257 -- Block_Interrupt --
258 ---------------------
259
260 procedure Block_Interrupt (Interrupt : Interrupt_ID) is
261 begin
262 Unimplemented ("Block_Interrupt");
263 end Block_Interrupt;
264
265 ------------------------------
266 -- Check_Reserved_Interrupt --
267 ------------------------------
268
269 procedure Check_Reserved_Interrupt (Interrupt : Interrupt_ID) is
270 begin
271 if Is_Reserved (Interrupt) then
272 raise Program_Error with
273 "interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
274 else
275 return;
276 end if;
277 end Check_Reserved_Interrupt;
278
279 ---------------------
280 -- Current_Handler --
281 ---------------------
282
283 function Current_Handler
284 (Interrupt : Interrupt_ID) return Parameterless_Handler
285 is
286 begin
287 Check_Reserved_Interrupt (Interrupt);
288
289 -- ??? Since Parameterless_Handler is not Atomic, the current
290 -- implementation is wrong. We need a new service in Interrupt_Manager
291 -- to ensure atomicity.
292
293 return User_Handler (Interrupt).H;
294 end Current_Handler;
295
296 --------------------
297 -- Detach_Handler --
298 --------------------
299
300 -- Calling this procedure with Static = True means we want to Detach the
301 -- current handler regardless of the previous handler's binding status
302 -- (i.e. do not care if it is a dynamic or static handler).
303
304 -- This option is needed so that during the finalization of a PO, we can
305 -- detach handlers attached through pragma Attach_Handler.
306
307 procedure Detach_Handler
308 (Interrupt : Interrupt_ID;
309 Static : Boolean := False)
310 is
311 begin
312 Check_Reserved_Interrupt (Interrupt);
313 Interrupt_Manager.Detach_Handler (Interrupt, Static);
314 end Detach_Handler;
315
316 ------------------------------
317 -- Detach_Interrupt_Entries --
318 ------------------------------
319
320 procedure Detach_Interrupt_Entries (T : Task_Id) is
321 begin
322 Interrupt_Manager.Detach_Interrupt_Entries (T);
323 end Detach_Interrupt_Entries;
324
325 ----------------------
326 -- Exchange_Handler --
327 ----------------------
328
329 -- Calling this procedure with New_Handler = null and Static = True
330 -- means we want to detach the current handler regardless of the previous
331 -- handler's binding status (i.e. we do not care if it is a dynamic or
332 -- static handler).
333
334 -- This option is needed so that during the finalization of a PO, we can
335 -- detach handlers attached through pragma Attach_Handler.
336
337 procedure Exchange_Handler
338 (Old_Handler : out Parameterless_Handler;
339 New_Handler : Parameterless_Handler;
340 Interrupt : Interrupt_ID;
341 Static : Boolean := False)
342 is
343 begin
344 Check_Reserved_Interrupt (Interrupt);
345 Interrupt_Manager.Exchange_Handler
346 (Old_Handler, New_Handler, Interrupt, Static);
347 end Exchange_Handler;
348
349 --------------
350 -- Finalize --
351 --------------
352
353 procedure Finalize (Object : in out Static_Interrupt_Protection) is
354 begin
355 -- ??? loop to be executed only when we're not doing library level
356 -- finalization, since in this case all interrupt / signal tasks are
357 -- gone.
358
359 if not Interrupt_Manager'Terminated then
360 for N in reverse Object.Previous_Handlers'Range loop
361 Interrupt_Manager.Attach_Handler
362 (New_Handler => Object.Previous_Handlers (N).Handler,
363 Interrupt => Object.Previous_Handlers (N).Interrupt,
364 Static => Object.Previous_Handlers (N).Static,
365 Restoration => True);
366 end loop;
367 end if;
368
369 Tasking.Protected_Objects.Entries.Finalize
370 (Tasking.Protected_Objects.Entries.Protection_Entries (Object));
371 end Finalize;
372
373 --------------------------------
374 -- Finalize_Interrupt_Servers --
375 --------------------------------
376
377 -- Restore default handlers for interrupt servers
378
379 -- This is called by the Interrupt_Manager task when it receives the abort
380 -- signal during program finalization.
381
382 procedure Finalize_Interrupt_Servers is
383 HW_Interrupts : constant Boolean := HW_Interrupt'Last >= 0;
384 begin
385 if HW_Interrupts then
386 for Int in HW_Interrupt loop
387 if Server_ID (Interrupt_ID (Int)) /= null
388 and then
389 not Ada.Task_Identification.Is_Terminated
390 (To_Ada (Server_ID (Interrupt_ID (Int))))
391 then
392 Interrupt_Manager.Attach_Handler
393 (New_Handler => null,
394 Interrupt => Interrupt_ID (Int),
395 Static => True,
396 Restoration => True);
397 end if;
398 end loop;
399 end if;
400 end Finalize_Interrupt_Servers;
401
402 -------------------------------------
403 -- Has_Interrupt_Or_Attach_Handler --
404 -------------------------------------
405
406 function Has_Interrupt_Or_Attach_Handler
407 (Object : access Dynamic_Interrupt_Protection)
408 return Boolean
409 is
410 pragma Unreferenced (Object);
411 begin
412 return True;
413 end Has_Interrupt_Or_Attach_Handler;
414
415 function Has_Interrupt_Or_Attach_Handler
416 (Object : access Static_Interrupt_Protection)
417 return Boolean
418 is
419 pragma Unreferenced (Object);
420 begin
421 return True;
422 end Has_Interrupt_Or_Attach_Handler;
423
424 ----------------------
425 -- Ignore_Interrupt --
426 ----------------------
427
428 procedure Ignore_Interrupt (Interrupt : Interrupt_ID) is
429 begin
430 Unimplemented ("Ignore_Interrupt");
431 end Ignore_Interrupt;
432
433 ----------------------
434 -- Install_Handlers --
435 ----------------------
436
437 procedure Install_Handlers
438 (Object : access Static_Interrupt_Protection;
439 New_Handlers : New_Handler_Array)
440 is
441 begin
442 for N in New_Handlers'Range loop
443
444 -- We need a lock around this ???
445
446 Object.Previous_Handlers (N).Interrupt := New_Handlers (N).Interrupt;
447 Object.Previous_Handlers (N).Static := User_Handler
448 (New_Handlers (N).Interrupt).Static;
449
450 -- We call Exchange_Handler and not directly Interrupt_Manager.
451 -- Exchange_Handler so we get the Is_Reserved check.
452
453 Exchange_Handler
454 (Old_Handler => Object.Previous_Handlers (N).Handler,
455 New_Handler => New_Handlers (N).Handler,
456 Interrupt => New_Handlers (N).Interrupt,
457 Static => True);
458 end loop;
459 end Install_Handlers;
460
461 ---------------------------------
462 -- Install_Restricted_Handlers --
463 ---------------------------------
464
465 procedure Install_Restricted_Handlers
466 (Prio : Interrupt_Priority;
467 Handlers : New_Handler_Array)
468 is
469 pragma Unreferenced (Prio);
470 begin
471 for N in Handlers'Range loop
472 Attach_Handler (Handlers (N).Handler, Handlers (N).Interrupt, True);
473 end loop;
474 end Install_Restricted_Handlers;
475
476 ------------------------------
477 -- Install_Umbrella_Handler --
478 ------------------------------
479
480 procedure Install_Umbrella_Handler
481 (Interrupt : HW_Interrupt;
482 Handler : System.OS_Interface.Interrupt_Handler)
483 is
484 Vec : constant Interrupt_Vector :=
485 Interrupt_Number_To_Vector
486 (Interfaces.C.int (Interrupt));
487
488 Status : Interfaces.C.int;
489
490 begin
491 -- Only install umbrella handler when no Ada handler has already been
492 -- installed. Note that the interrupt number is passed as a parameter
493 -- when an interrupt occurs, so the umbrella handler has a different
494 -- wrapper generated by intConnect for each interrupt number.
495
496 if not Handler_Installed (Interrupt) then
497 Status :=
498 Interrupt_Connect (Vec, Handler, System.Address (Interrupt));
499 pragma Assert (Status = 0);
500
501 Handler_Installed (Interrupt) := True;
502 end if;
503 end Install_Umbrella_Handler;
504
505 ----------------
506 -- Is_Blocked --
507 ----------------
508
509 function Is_Blocked (Interrupt : Interrupt_ID) return Boolean is
510 begin
511 Unimplemented ("Is_Blocked");
512 return False;
513 end Is_Blocked;
514
515 -----------------------
516 -- Is_Entry_Attached --
517 -----------------------
518
519 function Is_Entry_Attached (Interrupt : Interrupt_ID) return Boolean is
520 begin
521 Check_Reserved_Interrupt (Interrupt);
522 return User_Entry (Interrupt).T /= Null_Task;
523 end Is_Entry_Attached;
524
525 -------------------------
526 -- Is_Handler_Attached --
527 -------------------------
528
529 function Is_Handler_Attached (Interrupt : Interrupt_ID) return Boolean is
530 begin
531 Check_Reserved_Interrupt (Interrupt);
532 return User_Handler (Interrupt).H /= null;
533 end Is_Handler_Attached;
534
535 ----------------
536 -- Is_Ignored --
537 ----------------
538
539 function Is_Ignored (Interrupt : Interrupt_ID) return Boolean is
540 begin
541 Unimplemented ("Is_Ignored");
542 return False;
543 end Is_Ignored;
544
545 -------------------
546 -- Is_Registered --
547 -------------------
548
549 function Is_Registered (Handler : Parameterless_Handler) return Boolean is
550 Ptr : R_Link := Registered_Handlers;
551
552 type Acc_Proc is access procedure;
553
554 type Fat_Ptr is record
555 Object_Addr : System.Address;
556 Handler_Addr : Acc_Proc;
557 end record;
558
559 function To_Fat_Ptr is new Ada.Unchecked_Conversion
560 (Parameterless_Handler, Fat_Ptr);
561
562 Fat : Fat_Ptr;
563
564 begin
565 if Handler = null then
566 return True;
567 end if;
568
569 Fat := To_Fat_Ptr (Handler);
570
571 while Ptr /= null loop
572 if Ptr.H = Fat.Handler_Addr.all'Address then
573 return True;
574 end if;
575
576 Ptr := Ptr.Next;
577 end loop;
578
579 return False;
580 end Is_Registered;
581
582 -----------------
583 -- Is_Reserved --
584 -----------------
585
586 function Is_Reserved (Interrupt : Interrupt_ID) return Boolean is
587 use System.Interrupt_Management;
588 begin
589 return Reserve (System.Interrupt_Management.Interrupt_ID (Interrupt));
590 end Is_Reserved;
591
592 ----------------------
593 -- Notify_Interrupt --
594 ----------------------
595
596 -- Umbrella handler for vectored hardware interrupts (as opposed to signals
597 -- and exceptions). As opposed to the signal implementation, this handler
598 -- is installed in the vector table when the first Ada handler is attached
599 -- to the interrupt. However because VxWorks don't support disconnecting
600 -- handlers, this subprogram always test whether or not an Ada handler is
601 -- effectively attached.
602
603 -- Otherwise, the handler that existed prior to program startup is in the
604 -- vector table. This ensures that handlers installed by the BSP are active
605 -- unless explicitly replaced in the program text.
606
607 -- Each Interrupt_Server_Task has an associated binary semaphore on which
608 -- it pends once it's been started. This routine determines The appropriate
609 -- semaphore and issues a semGive call, waking the server task. When
610 -- a handler is unbound, System.Interrupts.Unbind_Handler issues a
611 -- Binary_Semaphore_Flush, and the server task deletes its semaphore
612 -- and terminates.
613
614 procedure Notify_Interrupt (Param : System.Address) is
615 Interrupt : constant Interrupt_ID := Interrupt_ID (Param);
616 Id : constant Binary_Semaphore_Id := Semaphore_ID_Map (Interrupt);
617 Status : Interfaces.C.int;
618 begin
619 if Id /= 0 then
620 Status := Binary_Semaphore_Release (Id);
621 pragma Assert (Status = 0);
622 end if;
623 end Notify_Interrupt;
624
625 ---------------
626 -- Reference --
627 ---------------
628
629 function Reference (Interrupt : Interrupt_ID) return System.Address is
630 begin
631 Check_Reserved_Interrupt (Interrupt);
632 return Storage_Elements.To_Address
633 (Storage_Elements.Integer_Address (Interrupt));
634 end Reference;
635
636 --------------------------------
637 -- Register_Interrupt_Handler --
638 --------------------------------
639
640 procedure Register_Interrupt_Handler (Handler_Addr : System.Address) is
641 begin
642 -- This routine registers a handler as usable for dynamic interrupt
643 -- handler association. Routines attaching and detaching handlers
644 -- dynamically should determine whether the handler is registered.
645 -- Program_Error should be raised if it is not registered.
646
647 -- Pragma Interrupt_Handler can only appear in a library level PO
648 -- definition and instantiation. Therefore, we do not need to implement
649 -- an unregister operation. Nor do we need to protect the queue
650 -- structure with a lock.
651
652 pragma Assert (Handler_Addr /= System.Null_Address);
653
654 Registered_Handlers :=
655 new Registered_Handler'(H => Handler_Addr, Next => Registered_Handlers);
656 end Register_Interrupt_Handler;
657
658 -----------------------
659 -- Unblock_Interrupt --
660 -----------------------
661
662 procedure Unblock_Interrupt (Interrupt : Interrupt_ID) is
663 begin
664 Unimplemented ("Unblock_Interrupt");
665 end Unblock_Interrupt;
666
667 ------------------
668 -- Unblocked_By --
669 ------------------
670
671 function Unblocked_By
672 (Interrupt : Interrupt_ID) return System.Tasking.Task_Id
673 is
674 begin
675 Unimplemented ("Unblocked_By");
676 return Null_Task;
677 end Unblocked_By;
678
679 ------------------------
680 -- Unignore_Interrupt --
681 ------------------------
682
683 procedure Unignore_Interrupt (Interrupt : Interrupt_ID) is
684 begin
685 Unimplemented ("Unignore_Interrupt");
686 end Unignore_Interrupt;
687
688 -------------------
689 -- Unimplemented --
690 -------------------
691
692 procedure Unimplemented (Feature : String) is
693 begin
694 raise Program_Error with Feature & " not implemented on VxWorks";
695 end Unimplemented;
696
697 -----------------------
698 -- Interrupt_Manager --
699 -----------------------
700
701 task body Interrupt_Manager is
702 -- By making this task independent of any master, when the process goes
703 -- away, the Interrupt_Manager will terminate gracefully.
704
705 Ignore : constant Boolean := System.Tasking.Utilities.Make_Independent;
706 pragma Unreferenced (Ignore);
707
708 --------------------
709 -- Local Routines --
710 --------------------
711
712 procedure Bind_Handler (Interrupt : Interrupt_ID);
713 -- This procedure does not do anything if a signal is blocked.
714 -- Otherwise, we have to interrupt Server_Task for status change
715 -- through a wakeup signal.
716
717 procedure Unbind_Handler (Interrupt : Interrupt_ID);
718 -- This procedure does not do anything if a signal is blocked.
719 -- Otherwise, we have to interrupt Server_Task for status change
720 -- through an abort signal.
721
722 procedure Unprotected_Exchange_Handler
723 (Old_Handler : out Parameterless_Handler;
724 New_Handler : Parameterless_Handler;
725 Interrupt : Interrupt_ID;
726 Static : Boolean;
727 Restoration : Boolean := False);
728
729 procedure Unprotected_Detach_Handler
730 (Interrupt : Interrupt_ID;
731 Static : Boolean);
732
733 ------------------
734 -- Bind_Handler --
735 ------------------
736
737 procedure Bind_Handler (Interrupt : Interrupt_ID) is
738 begin
739 Install_Umbrella_Handler
740 (HW_Interrupt (Interrupt), Notify_Interrupt'Access);
741 end Bind_Handler;
742
743 --------------------
744 -- Unbind_Handler --
745 --------------------
746
747 procedure Unbind_Handler (Interrupt : Interrupt_ID) is
748 Status : Interfaces.C.int;
749
750 begin
751 -- Flush server task off semaphore, allowing it to terminate
752
753 Status := Binary_Semaphore_Flush (Semaphore_ID_Map (Interrupt));
754 pragma Assert (Status = 0);
755 end Unbind_Handler;
756
757 --------------------------------
758 -- Unprotected_Detach_Handler --
759 --------------------------------
760
761 procedure Unprotected_Detach_Handler
762 (Interrupt : Interrupt_ID;
763 Static : Boolean)
764 is
765 Old_Handler : Parameterless_Handler;
766 begin
767 if User_Entry (Interrupt).T /= Null_Task then
768
769 -- If an interrupt entry is installed raise Program_Error
770 -- (propagate it to the caller).
771
772 raise Program_Error with
773 "an interrupt entry is already installed";
774 end if;
775
776 -- Note : Static = True will pass the following check. This is the
777 -- case when we want to detach a handler regardless of the static
778 -- status of the Current_Handler.
779
780 if not Static and then User_Handler (Interrupt).Static then
781
782 -- Trying to detach a static Interrupt Handler, raise
783 -- Program_Error.
784
785 raise Program_Error with
786 "trying to detach a static Interrupt Handler";
787 end if;
788
789 Old_Handler := User_Handler (Interrupt).H;
790
791 -- The new handler
792
793 User_Handler (Interrupt).H := null;
794 User_Handler (Interrupt).Static := False;
795
796 if Old_Handler /= null then
797 Unbind_Handler (Interrupt);
798 end if;
799 end Unprotected_Detach_Handler;
800
801 ----------------------------------
802 -- Unprotected_Exchange_Handler --
803 ----------------------------------
804
805 procedure Unprotected_Exchange_Handler
806 (Old_Handler : out Parameterless_Handler;
807 New_Handler : Parameterless_Handler;
808 Interrupt : Interrupt_ID;
809 Static : Boolean;
810 Restoration : Boolean := False)
811 is
812 begin
813 if User_Entry (Interrupt).T /= Null_Task then
814
815 -- If an interrupt entry is already installed, raise
816 -- Program_Error (propagate it to the caller).
817
818 raise Program_Error with "an interrupt is already installed";
819 end if;
820
821 -- Note : A null handler with Static = True will pass the following
822 -- check. This is the case when we want to detach a handler
823 -- regardless of the Static status of Current_Handler.
824
825 -- We don't check anything if Restoration is True, since we may be
826 -- detaching a static handler to restore a dynamic one.
827
828 if not Restoration and then not Static
829 and then (User_Handler (Interrupt).Static
830
831 -- Trying to overwrite a static Interrupt Handler with a dynamic
832 -- Handler
833
834 -- The new handler is not specified as an Interrupt Handler by a
835 -- pragma.
836
837 or else not Is_Registered (New_Handler))
838 then
839 raise Program_Error with
840 "trying to overwrite a static interrupt handler with a "
841 & "dynamic handler";
842 end if;
843
844 -- Save the old handler
845
846 Old_Handler := User_Handler (Interrupt).H;
847
848 -- The new handler
849
850 User_Handler (Interrupt).H := New_Handler;
851
852 if New_Handler = null then
853
854 -- The null handler means we are detaching the handler
855
856 User_Handler (Interrupt).Static := False;
857
858 else
859 User_Handler (Interrupt).Static := Static;
860 end if;
861
862 -- Invoke a corresponding Server_Task if not yet created. Place
863 -- Task_Id info in Server_ID array.
864
865 if New_Handler /= null
866 and then
867 (Server_ID (Interrupt) = Null_Task
868 or else
869 Ada.Task_Identification.Is_Terminated
870 (To_Ada (Server_ID (Interrupt))))
871 then
872 Interrupt_Access_Hold :=
873 new Interrupt_Server_Task (Interrupt, Binary_Semaphore_Create);
874 Server_ID (Interrupt) :=
875 To_System (Interrupt_Access_Hold.all'Identity);
876 end if;
877
878 if New_Handler = null and then Old_Handler /= null then
879
880 -- Restore default handler
881
882 Unbind_Handler (Interrupt);
883
884 elsif Old_Handler = null then
885
886 -- Save default handler
887
888 Bind_Handler (Interrupt);
889 end if;
890 end Unprotected_Exchange_Handler;
891
892 -- Start of processing for Interrupt_Manager
893
894 begin
895 loop
896 -- A block is needed to absorb Program_Error exception
897
898 declare
899 Old_Handler : Parameterless_Handler;
900
901 begin
902 select
903 accept Attach_Handler
904 (New_Handler : Parameterless_Handler;
905 Interrupt : Interrupt_ID;
906 Static : Boolean;
907 Restoration : Boolean := False)
908 do
909 Unprotected_Exchange_Handler
910 (Old_Handler, New_Handler, Interrupt, Static, Restoration);
911 end Attach_Handler;
912
913 or
914 accept Exchange_Handler
915 (Old_Handler : out Parameterless_Handler;
916 New_Handler : Parameterless_Handler;
917 Interrupt : Interrupt_ID;
918 Static : Boolean)
919 do
920 Unprotected_Exchange_Handler
921 (Old_Handler, New_Handler, Interrupt, Static);
922 end Exchange_Handler;
923
924 or
925 accept Detach_Handler
926 (Interrupt : Interrupt_ID;
927 Static : Boolean)
928 do
929 Unprotected_Detach_Handler (Interrupt, Static);
930 end Detach_Handler;
931
932 or
933 accept Bind_Interrupt_To_Entry
934 (T : Task_Id;
935 E : Task_Entry_Index;
936 Interrupt : Interrupt_ID)
937 do
938 -- If there is a binding already (either a procedure or an
939 -- entry), raise Program_Error (propagate it to the caller).
940
941 if User_Handler (Interrupt).H /= null
942 or else User_Entry (Interrupt).T /= Null_Task
943 then
944 raise Program_Error with
945 "a binding for this interrupt is already present";
946 end if;
947
948 User_Entry (Interrupt) := Entry_Assoc'(T => T, E => E);
949
950 -- Indicate the attachment of interrupt entry in the ATCB.
951 -- This is needed so when an interrupt entry task terminates
952 -- the binding can be cleaned. The call to unbinding must be
953 -- make by the task before it terminates.
954
955 T.Interrupt_Entry := True;
956
957 -- Invoke a corresponding Server_Task if not yet created.
958 -- Place Task_Id info in Server_ID array.
959
960 if Server_ID (Interrupt) = Null_Task
961 or else
962 Ada.Task_Identification.Is_Terminated
963 (To_Ada (Server_ID (Interrupt)))
964 then
965 Interrupt_Access_Hold := new Interrupt_Server_Task
966 (Interrupt, Binary_Semaphore_Create);
967 Server_ID (Interrupt) :=
968 To_System (Interrupt_Access_Hold.all'Identity);
969 end if;
970
971 Bind_Handler (Interrupt);
972 end Bind_Interrupt_To_Entry;
973
974 or
975 accept Detach_Interrupt_Entries (T : Task_Id) do
976 for Int in Interrupt_ID'Range loop
977 if not Is_Reserved (Int) then
978 if User_Entry (Int).T = T then
979 User_Entry (Int) :=
980 Entry_Assoc'
981 (T => Null_Task, E => Null_Task_Entry);
982 Unbind_Handler (Int);
983 end if;
984 end if;
985 end loop;
986
987 -- Indicate in ATCB that no interrupt entries are attached
988
989 T.Interrupt_Entry := False;
990 end Detach_Interrupt_Entries;
991 end select;
992
993 exception
994 -- If there is a Program_Error we just want to propagate it to
995 -- the caller and do not want to stop this task.
996
997 when Program_Error =>
998 null;
999
1000 when others =>
1001 pragma Assert (Standard.False);
1002 null;
1003 end;
1004 end loop;
1005
1006 exception
1007 when Standard'Abort_Signal =>
1008
1009 -- Flush interrupt server semaphores, so they can terminate
1010
1011 Finalize_Interrupt_Servers;
1012 raise;
1013 end Interrupt_Manager;
1014
1015 ---------------------------
1016 -- Interrupt_Server_Task --
1017 ---------------------------
1018
1019 -- Server task for vectored hardware interrupt handling
1020
1021 task body Interrupt_Server_Task is
1022 Ignore : constant Boolean := System.Tasking.Utilities.Make_Independent;
1023
1024 Self_Id : constant Task_Id := Self;
1025 Tmp_Handler : Parameterless_Handler;
1026 Tmp_ID : Task_Id;
1027 Tmp_Entry_Index : Task_Entry_Index;
1028 Status : Interfaces.C.int;
1029
1030 begin
1031 Semaphore_ID_Map (Interrupt) := Int_Sema;
1032
1033 loop
1034 -- Pend on semaphore that will be triggered by the umbrella handler
1035 -- when the associated interrupt comes in.
1036
1037 Status := Binary_Semaphore_Obtain (Int_Sema);
1038 pragma Assert (Status = 0);
1039
1040 if User_Handler (Interrupt).H /= null then
1041
1042 -- Protected procedure handler
1043
1044 Tmp_Handler := User_Handler (Interrupt).H;
1045 Tmp_Handler.all;
1046
1047 elsif User_Entry (Interrupt).T /= Null_Task then
1048
1049 -- Interrupt entry handler
1050
1051 Tmp_ID := User_Entry (Interrupt).T;
1052 Tmp_Entry_Index := User_Entry (Interrupt).E;
1053 System.Tasking.Rendezvous.Call_Simple
1054 (Tmp_ID, Tmp_Entry_Index, System.Null_Address);
1055
1056 else
1057 -- Semaphore has been flushed by an unbind operation in the
1058 -- Interrupt_Manager. Terminate the server task.
1059
1060 -- Wait for the Interrupt_Manager to complete its work
1061
1062 POP.Write_Lock (Self_Id);
1063
1064 -- Unassociate the interrupt handler
1065
1066 Semaphore_ID_Map (Interrupt) := 0;
1067
1068 -- Delete the associated semaphore
1069
1070 Status := Binary_Semaphore_Delete (Int_Sema);
1071
1072 pragma Assert (Status = 0);
1073
1074 -- Set status for the Interrupt_Manager
1075
1076 Server_ID (Interrupt) := Null_Task;
1077 POP.Unlock (Self_Id);
1078
1079 exit;
1080 end if;
1081 end loop;
1082 end Interrupt_Server_Task;
1083
1084 begin
1085 -- Get Interrupt_Manager's ID so that Abort_Interrupt can be sent
1086
1087 Interrupt_Manager_ID := To_System (Interrupt_Manager'Identity);
1088 end System.Interrupts;