OSDN Git Service

* sysdep.c: Problem discovered during IA64 VMS port.
[pf3gnuchains/gcc-fork.git] / gcc / ada / 5zinterr.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                GNU ADA 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-2003, 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 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.                                                      --
21 --                                                                          --
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.                                      --
28 --                                                                          --
29 -- GNARL was developed by the GNARL team at Florida State University.       --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc.     --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  Invariants:
35
36 --  All user-handleable signals are masked at all times in all
37 --  tasks/threads except possibly for the Interrupt_Manager task.
38
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.
45
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).
51
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.
57
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
63 --  entries.
64
65 --  This is the VxWorks version of this package, supporting vectored hardware
66 --  interrupts.
67
68 with Unchecked_Conversion;
69
70 with System.OS_Interface; use System.OS_Interface;
71
72 with Interfaces.VxWorks;
73
74 with Ada.Task_Identification;
75 --  used for Task_ID type
76
77 with Ada.Exceptions;
78 --  used for Raise_Exception
79
80 with System.Interrupt_Management;
81 --  used for Reserve
82
83 with System.Task_Primitives.Operations;
84 --  used for Write_Lock
85 --           Unlock
86 --           Abort
87 --           Wakeup_Task
88 --           Sleep
89 --           Initialize_Lock
90
91 with System.Storage_Elements;
92 --  used for To_Address
93 --           To_Integer
94 --           Integer_Address
95
96 with System.Tasking;
97 --  used for Task_ID
98 --           Task_Entry_Index
99 --           Null_Task
100 --           Self
101 --           Interrupt_Manager_ID
102
103 with System.Tasking.Utilities;
104 --  used for Make_Independent
105
106 with System.Tasking.Rendezvous;
107 --  used for Call_Simple
108 pragma Elaborate_All (System.Tasking.Rendezvous);
109
110 package body System.Interrupts is
111
112    use Tasking;
113    use Ada.Exceptions;
114
115    package POP renames System.Task_Primitives.Operations;
116
117    function To_Ada is new Unchecked_Conversion
118      (System.Tasking.Task_ID, Ada.Task_Identification.Task_Id);
119
120    function To_System is new Unchecked_Conversion
121      (Ada.Task_Identification.Task_Id, Task_ID);
122
123    -----------------
124    -- Local Tasks --
125    -----------------
126
127    --  WARNING: System.Tasking.Stages performs calls to this task
128    --  with low-level constructs. Do not change this spec without synchro-
129    --  nizing it.
130
131    task Interrupt_Manager is
132       entry Detach_Interrupt_Entries (T : Task_ID);
133
134       entry Attach_Handler
135         (New_Handler : Parameterless_Handler;
136          Interrupt   : Interrupt_ID;
137          Static      : Boolean;
138          Restoration : Boolean := False);
139
140       entry Exchange_Handler
141         (Old_Handler : out Parameterless_Handler;
142          New_Handler : Parameterless_Handler;
143          Interrupt   : Interrupt_ID;
144          Static      : Boolean);
145
146       entry Detach_Handler
147         (Interrupt : Interrupt_ID;
148          Static    : Boolean);
149
150       entry Bind_Interrupt_To_Entry
151         (T         : Task_ID;
152          E         : Task_Entry_Index;
153          Interrupt : Interrupt_ID);
154
155       pragma Interrupt_Priority (System.Interrupt_Priority'First);
156    end Interrupt_Manager;
157
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;
163
164    type Interrupt_Task_Access is access Interrupt_Server_Task;
165
166    -------------------------------
167    -- Local Types and Variables --
168    -------------------------------
169
170    type Entry_Assoc is record
171       T : Task_ID;
172       E : Task_Entry_Index;
173    end record;
174
175    type Handler_Assoc is record
176       H      : Parameterless_Handler;
177       Static : Boolean;   --  Indicates static binding;
178    end record;
179
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.
186
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
191
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.
195
196    type Registered_Handler;
197    type R_Link is access all Registered_Handler;
198
199    type Registered_Handler is record
200       H    : System.Address := System.Null_Address;
201       Next : R_Link := null;
202    end record;
203
204    Registered_Handler_Head : R_Link := null;
205    Registered_Handler_Tail : R_Link := null;
206
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.
213
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
220    --  instead.
221
222    Interrupt_Access_Hold : Interrupt_Task_Access;
223    --  Variable for allocating an Interrupt_Server_Task
224
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.
229
230    -----------------------
231    -- Local Subprograms --
232    -----------------------
233
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.
237
238    procedure Finalize_Interrupt_Servers;
239    --  Unbind the handlers for hardware interrupt server tasks at program
240    --  termination.
241
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.
245
246    procedure Notify_Interrupt (Param : System.Address);
247    --  Umbrella handler for vectored interrupts (not signals)
248
249    procedure Install_Default_Action (Interrupt : HW_Interrupt);
250    --  Restore a handler that was in place prior to program execution
251
252    procedure Install_Umbrella_Handler
253      (Interrupt : HW_Interrupt;
254       Handler   : Interfaces.VxWorks.VOIDFUNCPTR);
255    --  Install the runtime umbrella handler for a vectored hardware
256    --  interrupt
257
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.
262
263    --------------------
264    -- Attach_Handler --
265    --------------------
266
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).
271
272    --  This option is needed so that during the finalization of a PO, we
273    --  can detach handlers attached through pragma Attach_Handler.
274
275    procedure Attach_Handler
276      (New_Handler : Parameterless_Handler;
277       Interrupt   : Interrupt_ID;
278       Static      : Boolean := False) is
279    begin
280       Check_Reserved_Interrupt (Interrupt);
281       Interrupt_Manager.Attach_Handler (New_Handler, Interrupt, Static);
282    end Attach_Handler;
283
284    -----------------------------
285    -- Bind_Interrupt_To_Entry --
286    -----------------------------
287
288    --  This procedure raises a Program_Error if it tries to
289    --  bind an interrupt to which an Entry or a Procedure is
290    --  already bound.
291
292    procedure Bind_Interrupt_To_Entry
293      (T       : Task_ID;
294       E       : Task_Entry_Index;
295       Int_Ref : System.Address)
296    is
297       Interrupt : constant Interrupt_ID :=
298         Interrupt_ID (Storage_Elements.To_Integer (Int_Ref));
299
300    begin
301       Check_Reserved_Interrupt (Interrupt);
302       Interrupt_Manager.Bind_Interrupt_To_Entry (T, E, Interrupt);
303    end Bind_Interrupt_To_Entry;
304
305    ---------------------
306    -- Block_Interrupt --
307    ---------------------
308
309    procedure Block_Interrupt (Interrupt : Interrupt_ID) is
310    begin
311       Unimplemented ("Block_Interrupt");
312    end Block_Interrupt;
313
314    ------------------------------
315    -- Check_Reserved_Interrupt --
316    ------------------------------
317
318    procedure Check_Reserved_Interrupt (Interrupt : Interrupt_ID) is
319    begin
320       if Is_Reserved (Interrupt) then
321          Raise_Exception
322            (Program_Error'Identity,
323             "Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved");
324       else
325          return;
326       end if;
327    end Check_Reserved_Interrupt;
328
329    ---------------------
330    -- Current_Handler --
331    ---------------------
332
333    function Current_Handler
334      (Interrupt : Interrupt_ID) return Parameterless_Handler is
335    begin
336       Check_Reserved_Interrupt (Interrupt);
337
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.
341
342       return User_Handler (Interrupt).H;
343    end Current_Handler;
344
345    --------------------
346    -- Detach_Handler --
347    --------------------
348
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).
352
353    --  This option is needed so that during the finalization of a PO, we can
354    --  detach handlers attached through pragma Attach_Handler.
355
356    procedure Detach_Handler
357      (Interrupt : Interrupt_ID;
358       Static    : Boolean := False) is
359    begin
360       Check_Reserved_Interrupt (Interrupt);
361       Interrupt_Manager.Detach_Handler (Interrupt, Static);
362    end Detach_Handler;
363
364    ------------------------------
365    -- Detach_Interrupt_Entries --
366    ------------------------------
367
368    procedure Detach_Interrupt_Entries (T : Task_ID) is
369    begin
370       Interrupt_Manager.Detach_Interrupt_Entries (T);
371    end Detach_Interrupt_Entries;
372
373    ----------------------
374    -- Exchange_Handler --
375    ----------------------
376
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).
381
382    --  This option is needed so that during the finalization of a PO, we
383    --  can detach handlers attached through pragma Attach_Handler.
384
385    procedure Exchange_Handler
386      (Old_Handler : out Parameterless_Handler;
387       New_Handler : Parameterless_Handler;
388       Interrupt   : Interrupt_ID;
389       Static      : Boolean := False) is
390    begin
391       Check_Reserved_Interrupt (Interrupt);
392       Interrupt_Manager.Exchange_Handler
393         (Old_Handler, New_Handler, Interrupt, Static);
394    end Exchange_Handler;
395
396    --------------
397    -- Finalize --
398    --------------
399
400    procedure Finalize (Object : in out Static_Interrupt_Protection) is
401    begin
402       --  ??? loop to be executed only when we're not doing library level
403       --  finalization, since in this case all interrupt / signal tasks are
404       --  gone.
405
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);
413          end loop;
414       end if;
415
416       Tasking.Protected_Objects.Entries.Finalize
417         (Tasking.Protected_Objects.Entries.Protection_Entries (Object));
418    end Finalize;
419
420    --------------------------------
421    -- Finalize_Interrupt_Servers --
422    --------------------------------
423
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.
427
428    procedure Finalize_Interrupt_Servers is
429    begin
430       if HW_Interrupt'Last >= 0 then
431          for Int in HW_Interrupt loop
432             if Server_ID (Interrupt_ID (Int)) /= null
433               and then
434                 not Ada.Task_Identification.Is_Terminated
435                  (To_Ada (Server_ID (Interrupt_ID (Int))))
436             then
437                Interrupt_Manager.Attach_Handler
438                  (New_Handler => null,
439                   Interrupt => Interrupt_ID (Int),
440                   Static => True,
441                   Restoration => True);
442             end if;
443          end loop;
444       end if;
445    end Finalize_Interrupt_Servers;
446
447    -------------------------------------
448    -- Has_Interrupt_Or_Attach_Handler --
449    -------------------------------------
450
451    function Has_Interrupt_Or_Attach_Handler
452      (Object : access Dynamic_Interrupt_Protection)
453       return   Boolean
454    is
455       pragma Unreferenced (Object);
456
457    begin
458       return True;
459    end Has_Interrupt_Or_Attach_Handler;
460
461    function Has_Interrupt_Or_Attach_Handler
462      (Object : access Static_Interrupt_Protection)
463       return   Boolean
464    is
465       pragma Unreferenced (Object);
466
467    begin
468       return True;
469    end Has_Interrupt_Or_Attach_Handler;
470
471    ----------------------
472    -- Ignore_Interrupt --
473    ----------------------
474
475    procedure Ignore_Interrupt (Interrupt : Interrupt_ID) is
476    begin
477       Unimplemented ("Ignore_Interrupt");
478    end Ignore_Interrupt;
479
480    ----------------------------
481    -- Install_Default_Action --
482    ----------------------------
483
484    procedure Install_Default_Action (Interrupt : HW_Interrupt) is
485    begin
486       --  Restore original interrupt handler
487
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;
493
494    ----------------------
495    -- Install_Handlers --
496    ----------------------
497
498    procedure Install_Handlers
499      (Object       : access Static_Interrupt_Protection;
500       New_Handlers : New_Handler_Array) is
501    begin
502       for N in New_Handlers'Range loop
503          --  We need a lock around this ???
504
505          Object.Previous_Handlers (N).Interrupt := New_Handlers (N).Interrupt;
506          Object.Previous_Handlers (N).Static    := User_Handler
507            (New_Handlers (N).Interrupt).Static;
508
509          --  We call Exchange_Handler and not directly Interrupt_Manager.
510          --  Exchange_Handler so we get the Is_Reserved check.
511
512          Exchange_Handler
513            (Old_Handler => Object.Previous_Handlers (N).Handler,
514             New_Handler => New_Handlers (N).Handler,
515             Interrupt   => New_Handlers (N).Interrupt,
516             Static      => True);
517       end loop;
518    end Install_Handlers;
519
520    ------------------------------
521    -- Install_Umbrella_Handler --
522    ------------------------------
523
524    procedure Install_Umbrella_Handler
525      (Interrupt : HW_Interrupt;
526       Handler   : Interfaces.VxWorks.VOIDFUNCPTR)
527    is
528       use Interfaces.VxWorks;
529
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;
535
536    begin
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.
541
542       if Default_Handler (Interrupt) = null then
543          Stat :=
544            intConnect (Vec, VOIDFUNCPTR (Handler), System.Address (Interrupt));
545          Default_Handler (Interrupt) := Old_Handler;
546       end if;
547    end Install_Umbrella_Handler;
548
549    ----------------
550    -- Is_Blocked --
551    ----------------
552
553    function Is_Blocked (Interrupt : Interrupt_ID) return Boolean is
554    begin
555       Unimplemented ("Is_Blocked");
556       return False;
557    end Is_Blocked;
558
559    -----------------------
560    -- Is_Entry_Attached --
561    -----------------------
562
563    function Is_Entry_Attached (Interrupt : Interrupt_ID) return Boolean is
564    begin
565       Check_Reserved_Interrupt (Interrupt);
566       return User_Entry (Interrupt).T /= Null_Task;
567    end Is_Entry_Attached;
568
569    -------------------------
570    -- Is_Handler_Attached --
571    -------------------------
572
573    function Is_Handler_Attached (Interrupt : Interrupt_ID) return Boolean is
574    begin
575       Check_Reserved_Interrupt (Interrupt);
576       return User_Handler (Interrupt).H /= null;
577    end Is_Handler_Attached;
578
579    ----------------
580    -- Is_Ignored --
581    ----------------
582
583    function Is_Ignored (Interrupt : Interrupt_ID) return Boolean is
584    begin
585       Unimplemented ("Is_Ignored");
586       return False;
587    end Is_Ignored;
588
589    -------------------
590    -- Is_Registered --
591    -------------------
592
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;
597       end record;
598
599       function To_Fat_Ptr is new Unchecked_Conversion
600         (Parameterless_Handler, Fat_Ptr);
601
602       Ptr : R_Link;
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       Ptr := Registered_Handler_Head;
613
614       while (Ptr /= null) loop
615          if Ptr.H = Fat.Handler_Addr then
616             return True;
617          end if;
618
619          Ptr := Ptr.Next;
620       end loop;
621
622       return False;
623    end Is_Registered;
624
625    -----------------
626    -- Is_Reserved --
627    -----------------
628
629    function Is_Reserved (Interrupt : Interrupt_ID) return Boolean is
630       use System.Interrupt_Management;
631    begin
632       return Reserve (System.Interrupt_Management.Interrupt_ID (Interrupt));
633    end Is_Reserved;
634
635    ----------------------
636    -- Notify_Interrupt --
637    ----------------------
638
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.
643
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.
647
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.
654
655    procedure Notify_Interrupt (Param : System.Address) is
656       Interrupt      : Interrupt_ID := Interrupt_ID (Param);
657       Discard_Result : STATUS;
658
659    begin
660       Discard_Result := semGive (Semaphore_ID_Map (Interrupt));
661    end Notify_Interrupt;
662
663    ---------------
664    -- Reference --
665    ---------------
666
667    function Reference (Interrupt : Interrupt_ID) return System.Address is
668    begin
669       Check_Reserved_Interrupt (Interrupt);
670       return Storage_Elements.To_Address
671         (Storage_Elements.Integer_Address (Interrupt));
672    end Reference;
673
674    --------------------------------
675    -- Register_Interrupt_Handler --
676    --------------------------------
677
678    procedure Register_Interrupt_Handler (Handler_Addr : System.Address) is
679       New_Node_Ptr : R_Link;
680    begin
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.
685
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.
690
691       pragma Assert (Handler_Addr /= System.Null_Address);
692
693       New_Node_Ptr := new Registered_Handler;
694       New_Node_Ptr.H := Handler_Addr;
695
696       if Registered_Handler_Head = null then
697          Registered_Handler_Head := New_Node_Ptr;
698          Registered_Handler_Tail := New_Node_Ptr;
699
700       else
701          Registered_Handler_Tail.Next := New_Node_Ptr;
702          Registered_Handler_Tail := New_Node_Ptr;
703       end if;
704    end Register_Interrupt_Handler;
705
706    -----------------------
707    -- Unblock_Interrupt --
708    -----------------------
709
710    procedure Unblock_Interrupt (Interrupt : Interrupt_ID) is
711    begin
712       Unimplemented ("Unblock_Interrupt");
713    end Unblock_Interrupt;
714
715    ------------------
716    -- Unblocked_By --
717    ------------------
718
719    function Unblocked_By
720      (Interrupt : Interrupt_ID) return System.Tasking.Task_ID is
721    begin
722       Unimplemented ("Unblocked_By");
723       return Null_Task;
724    end Unblocked_By;
725
726    ------------------------
727    -- Unignore_Interrupt --
728    ------------------------
729
730    procedure Unignore_Interrupt (Interrupt : Interrupt_ID) is
731    begin
732       Unimplemented ("Unignore_Interrupt");
733    end Unignore_Interrupt;
734
735    -------------------
736    -- Unimplemented --
737    -------------------
738
739    procedure Unimplemented (Feature : String) is
740    begin
741       Raise_Exception
742         (Program_Error'Identity,
743          Feature & " not implemented on VxWorks");
744    end Unimplemented;
745
746    -----------------------
747    -- Interrupt_Manager --
748    -----------------------
749
750    task body Interrupt_Manager is
751
752       --------------------
753       -- Local Routines --
754       --------------------
755
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
759       --  a wakeup signal.
760
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.
765
766       procedure Unprotected_Exchange_Handler
767         (Old_Handler : out Parameterless_Handler;
768          New_Handler : Parameterless_Handler;
769          Interrupt   : Interrupt_ID;
770          Static      : Boolean;
771          Restoration : Boolean := False);
772
773       procedure Unprotected_Detach_Handler
774         (Interrupt : Interrupt_ID;
775          Static    : Boolean);
776
777       ------------------
778       -- Bind_Handler --
779       ------------------
780
781       procedure Bind_Handler (Interrupt : Interrupt_ID) is
782       begin
783          Install_Umbrella_Handler
784            (HW_Interrupt (Interrupt), Notify_Interrupt'Access);
785       end Bind_Handler;
786
787       --------------------
788       -- Unbind_Handler --
789       --------------------
790
791       procedure Unbind_Handler (Interrupt : Interrupt_ID) is
792          S : STATUS;
793          use type STATUS;
794
795       begin
796          --  Hardware interrupt
797
798          Install_Default_Action (HW_Interrupt (Interrupt));
799
800          --  Flush server task off semaphore, allowing it to terminate
801
802          S := semFlush (Semaphore_ID_Map (Interrupt));
803          pragma Assert (S = 0);
804       end Unbind_Handler;
805
806       --------------------------------
807       -- Unprotected_Detach_Handler --
808       --------------------------------
809
810       procedure Unprotected_Detach_Handler
811         (Interrupt : Interrupt_ID;
812          Static    : Boolean)
813       is
814          Old_Handler : Parameterless_Handler;
815       begin
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).
819
820             Raise_Exception (Program_Error'Identity,
821               "An interrupt entry is already installed");
822          end if;
823
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.
827
828          if not Static and then User_Handler (Interrupt).Static then
829             --  Trying to detach a static Interrupt Handler.
830             --  raise Program_Error.
831
832             Raise_Exception (Program_Error'Identity,
833               "Trying to detach a static Interrupt Handler");
834          end if;
835
836          Old_Handler := User_Handler (Interrupt).H;
837
838          --  The new handler
839
840          User_Handler (Interrupt).H := null;
841          User_Handler (Interrupt).Static := False;
842
843          if Old_Handler /= null then
844             Unbind_Handler (Interrupt);
845          end if;
846       end Unprotected_Detach_Handler;
847
848       ----------------------------------
849       -- Unprotected_Exchange_Handler --
850       ----------------------------------
851
852       procedure Unprotected_Exchange_Handler
853         (Old_Handler : out Parameterless_Handler;
854          New_Handler : Parameterless_Handler;
855          Interrupt   : Interrupt_ID;
856          Static      : Boolean;
857          Restoration : Boolean := False) is
858       begin
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).
862
863             Raise_Exception
864               (Program_Error'Identity,
865                "An interrupt is already installed");
866          end if;
867
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.
874
875          if not Restoration and then not Static
876            and then (User_Handler (Interrupt).Static
877
878             --  Trying to overwrite a static Interrupt Handler with a
879             --  dynamic Handler
880
881             --  The new handler is not specified as an
882             --  Interrupt Handler by a pragma.
883
884            or else not Is_Registered (New_Handler))
885          then
886             Raise_Exception
887               (Program_Error'Identity,
888                "Trying to overwrite a static Interrupt Handler with a " &
889                "dynamic Handler");
890          end if;
891
892          --  Save the old handler
893
894          Old_Handler := User_Handler (Interrupt).H;
895
896          --  The new handler
897
898          User_Handler (Interrupt).H := New_Handler;
899
900          if New_Handler = null then
901
902             --  The null handler means we are detaching the handler.
903
904             User_Handler (Interrupt).Static := False;
905
906          else
907             User_Handler (Interrupt).Static := Static;
908          end if;
909
910          --  Invoke a corresponding Server_Task if not yet created.
911          --  Place Task_ID info in Server_ID array.
912
913          if New_Handler /= null
914            and then
915             (Server_ID (Interrupt) = Null_Task
916               or else
917                 Ada.Task_Identification.Is_Terminated
918                   (To_Ada (Server_ID (Interrupt))))
919          then
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);
925          end if;
926
927          if (New_Handler = null) and then Old_Handler /= null then
928             --  Restore default handler
929
930             Unbind_Handler (Interrupt);
931
932          elsif Old_Handler = null then
933             --  Save default handler
934
935             Bind_Handler (Interrupt);
936          end if;
937       end Unprotected_Exchange_Handler;
938
939       --  Start of processing for Interrupt_Manager
940
941    begin
942       --  By making this task independent of any master, when the process
943       --  goes away, the Interrupt_Manager will terminate gracefully.
944
945       System.Tasking.Utilities.Make_Independent;
946
947       loop
948          --  A block is needed to absorb Program_Error exception
949
950          declare
951             Old_Handler : Parameterless_Handler;
952
953          begin
954             select
955                accept Attach_Handler
956                  (New_Handler : Parameterless_Handler;
957                   Interrupt   : Interrupt_ID;
958                   Static      : Boolean;
959                   Restoration : Boolean := False)
960                do
961                   Unprotected_Exchange_Handler
962                     (Old_Handler, New_Handler, Interrupt, Static, Restoration);
963                end Attach_Handler;
964
965             or
966                accept Exchange_Handler
967                  (Old_Handler : out Parameterless_Handler;
968                   New_Handler : Parameterless_Handler;
969                   Interrupt   : Interrupt_ID;
970                   Static      : Boolean)
971                do
972                   Unprotected_Exchange_Handler
973                     (Old_Handler, New_Handler, Interrupt, Static);
974                end Exchange_Handler;
975
976             or
977                accept Detach_Handler
978                   (Interrupt   : Interrupt_ID;
979                    Static      : Boolean)
980                do
981                   Unprotected_Detach_Handler (Interrupt, Static);
982                end Detach_Handler;
983             or
984                accept Bind_Interrupt_To_Entry
985                  (T       : Task_ID;
986                   E       : Task_Entry_Index;
987                   Interrupt : Interrupt_ID)
988                do
989                   --  If there is a binding already (either a procedure or an
990                   --  entry), raise Program_Error (propagate it to the caller).
991
992                   if User_Handler (Interrupt).H /= null
993                     or else User_Entry (Interrupt).T /= Null_Task
994                   then
995                      Raise_Exception
996                        (Program_Error'Identity,
997                         "A binding for this interrupt is already present");
998                   end if;
999
1000                   User_Entry (Interrupt) := Entry_Assoc'(T => T, E => E);
1001
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.
1006
1007                   T.Interrupt_Entry := True;
1008
1009                   --  Invoke a corresponding Server_Task if not yet created.
1010                   --  Place Task_ID info in Server_ID array.
1011
1012                   if Server_ID (Interrupt) = Null_Task
1013                     or else
1014                       Ada.Task_Identification.Is_Terminated
1015                         (To_Ada (Server_ID (Interrupt)))
1016                   then
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);
1021                   end if;
1022
1023                   Bind_Handler (Interrupt);
1024                end Bind_Interrupt_To_Entry;
1025
1026             or
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
1031                            User_Entry (Int) :=
1032                              Entry_Assoc'
1033                                (T => Null_Task, E => Null_Task_Entry);
1034                            Unbind_Handler (Int);
1035                         end if;
1036                      end if;
1037                   end loop;
1038
1039                   --  Indicate in ATCB that no interrupt entries are attached.
1040
1041                   T.Interrupt_Entry := False;
1042                end Detach_Interrupt_Entries;
1043             end select;
1044
1045          exception
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.
1048
1049             when Program_Error =>
1050                null;
1051
1052             when others =>
1053                pragma Assert (False);
1054                null;
1055          end;
1056       end loop;
1057
1058    exception
1059       when Standard'Abort_Signal =>
1060          --  Flush interrupt server semaphores, so they can terminate
1061          Finalize_Interrupt_Servers;
1062          raise;
1063    end Interrupt_Manager;
1064
1065    ---------------------------
1066    -- Interrupt_Server_Task --
1067    ---------------------------
1068
1069    --  Server task for vectored hardware interrupt handling
1070
1071    task body Interrupt_Server_Task is
1072       Self_Id         : constant Task_ID := Self;
1073       Tmp_Handler     : Parameterless_Handler;
1074       Tmp_ID          : Task_ID;
1075       Tmp_Entry_Index : Task_Entry_Index;
1076       S               : STATUS;
1077
1078       use type STATUS;
1079
1080    begin
1081       System.Tasking.Utilities.Make_Independent;
1082       Semaphore_ID_Map (Interrupt) := Int_Sema;
1083
1084       loop
1085          --  Pend on semaphore that will be triggered by the
1086          --  umbrella handler when the associated interrupt comes in
1087
1088          S := semTake (Int_Sema, WAIT_FOREVER);
1089          pragma Assert (S = 0);
1090
1091          if User_Handler (Interrupt).H /= null then
1092
1093             --  Protected procedure handler
1094
1095             Tmp_Handler := User_Handler (Interrupt).H;
1096             Tmp_Handler.all;
1097
1098          elsif User_Entry (Interrupt).T /= Null_Task then
1099
1100             --  Interrupt entry handler
1101
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);
1106
1107          else
1108             --  Semaphore has been flushed by an unbind operation in
1109             --  the Interrupt_Manager. Terminate the server task.
1110
1111             --  Wait for the Interrupt_Manager to complete its work
1112
1113             POP.Write_Lock (Self_Id);
1114
1115             --  Delete the associated semaphore
1116
1117             S := semDelete (Int_Sema);
1118
1119             pragma Assert (S = 0);
1120
1121             --  Set status for the Interrupt_Manager
1122
1123             Semaphore_ID_Map (Interrupt) := 0;
1124             Server_ID (Interrupt) := Null_Task;
1125             POP.Unlock (Self_Id);
1126
1127             exit;
1128          end if;
1129       end loop;
1130    end Interrupt_Server_Task;
1131
1132 begin
1133    --  Get Interrupt_Manager's ID so that Abort_Interrupt can be sent.
1134
1135    Interrupt_Manager_ID := To_System (Interrupt_Manager'Identity);
1136 end System.Interrupts;