Debian Codegraph @

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