1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . I N T E R R U P T S --
9 -- Copyright (C) 1998-2001 Free Software Fundation --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- This is the IRIX & NT version of this package.
36 with Ada
.Task_Identification
;
40 -- used for Raise_Exception
42 with System
.OS_Interface
;
43 -- used for intr_attach
45 with System
.Storage_Elements
;
46 -- used for To_Address
49 with System
.Task_Primitives
.Operations
;
56 with System
.Tasking
.Utilities
;
57 -- used for Make_Independent
59 with System
.Tasking
.Rendezvous
;
60 -- used for Call_Simple
62 with System
.Tasking
.Initialization
;
63 -- used for Defer_Abort
66 with System
.Interrupt_Management
;
68 with System
.Parameters
;
69 -- used for Single_Lock
74 with Unchecked_Conversion
;
76 package body System
.Interrupts
is
81 use System
.OS_Interface
;
84 package STPO
renames System
.Task_Primitives
.Operations
;
85 package IMNG
renames System
.Interrupt_Management
;
87 subtype int
is Interfaces
.C
.int
;
89 function To_System
is new Unchecked_Conversion
90 (Ada
.Task_Identification
.Task_Id
, Task_ID
);
92 type Handler_Kind
is (Unknown
, Task_Entry
, Protected_Procedure
);
94 type Handler_Desc
is record
95 Kind
: Handler_Kind
:= Unknown
;
98 H
: Parameterless_Handler
;
99 Static
: Boolean := False;
102 task type Server_Task
(Interrupt
: Interrupt_ID
) is
103 pragma Interrupt_Priority
(System
.Interrupt_Priority
'Last);
106 type Server_Task_Access
is access Server_Task
;
108 Attached_Interrupts
: array (Interrupt_ID
) of Boolean;
109 Handlers
: array (Interrupt_ID
) of Task_ID
;
110 Descriptors
: array (Interrupt_ID
) of Handler_Desc
;
111 Interrupt_Count
: array (Interrupt_ID
) of Integer := (others => 0);
113 pragma Volatile_Components
(Interrupt_Count
);
115 procedure Attach_Handler
116 (New_Handler
: Parameterless_Handler
;
117 Interrupt
: Interrupt_ID
;
119 Restoration
: Boolean);
120 -- This internal procedure is needed to finalize protected objects
121 -- that contain interrupt handlers.
123 procedure Signal_Handler
(Sig
: Interrupt_ID
);
124 -- This procedure is used to handle all the signals.
126 -- Type and Head, Tail of the list containing Registered Interrupt
127 -- Handlers. These definitions are used to register the handlers
128 -- specified by the pragma Interrupt_Handler.
131 -- Handler Registration:
134 type Registered_Handler
;
135 type R_Link
is access all Registered_Handler
;
137 type Registered_Handler
is record
138 H
: System
.Address
:= System
.Null_Address
;
139 Next
: R_Link
:= null;
142 Registered_Handlers
: R_Link
:= null;
144 function Is_Registered
(Handler
: Parameterless_Handler
) return Boolean;
145 -- See if the Handler has been "pragma"ed using Interrupt_Handler.
146 -- Always consider a null handler as registered.
148 type Handler_Ptr
is access procedure (Sig
: Interrupt_ID
);
150 function TISR
is new Unchecked_Conversion
(Handler_Ptr
, isr_address
);
152 procedure Signal_Handler
(Sig
: Interrupt_ID
) is
153 Handler
: Task_ID
renames Handlers
(Sig
);
155 if Intr_Attach_Reset
and then
156 intr_attach
(int
(Sig
), TISR
(Signal_Handler
'Access)) = FUNC_ERR
161 if Handler
/= null then
162 Interrupt_Count
(Sig
) := Interrupt_Count
(Sig
) + 1;
163 STPO
.Wakeup
(Handler
, Interrupt_Server_Idle_Sleep
);
171 function Is_Reserved
(Interrupt
: Interrupt_ID
) return Boolean is
173 return IMNG
.Reserve
(IMNG
.Interrupt_ID
(Interrupt
));
176 -----------------------
177 -- Is_Entry_Attached --
178 -----------------------
180 function Is_Entry_Attached
(Interrupt
: Interrupt_ID
) return Boolean is
182 if Is_Reserved
(Interrupt
) then
183 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
184 Interrupt_ID
'Image (Interrupt
) & " is reserved");
187 return Descriptors
(Interrupt
).T
/= Null_Task
;
188 end Is_Entry_Attached
;
190 -------------------------
191 -- Is_Handler_Attached --
192 -------------------------
194 function Is_Handler_Attached
(Interrupt
: Interrupt_ID
) return Boolean is
196 if Is_Reserved
(Interrupt
) then
197 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
198 Interrupt_ID
'Image (Interrupt
) & " is reserved");
201 return Descriptors
(Interrupt
).Kind
/= Unknown
;
202 end Is_Handler_Attached
;
208 function Is_Ignored
(Interrupt
: Interrupt_ID
) return Boolean is
218 function Unblocked_By
(Interrupt
: Interrupt_ID
) return Task_ID
is
224 ----------------------
225 -- Ignore_Interrupt --
226 ----------------------
228 procedure Ignore_Interrupt
(Interrupt
: Interrupt_ID
) is
231 end Ignore_Interrupt
;
233 ------------------------
234 -- Unignore_Interrupt --
235 ------------------------
237 procedure Unignore_Interrupt
(Interrupt
: Interrupt_ID
) is
240 end Unignore_Interrupt
;
242 -------------------------------------
243 -- Has_Interrupt_Or_Attach_Handler --
244 -------------------------------------
246 function Has_Interrupt_Or_Attach_Handler
247 (Object
: access Dynamic_Interrupt_Protection
) return Boolean is
250 end Has_Interrupt_Or_Attach_Handler
;
256 procedure Finalize
(Object
: in out Static_Interrupt_Protection
) is
258 -- ??? loop to be executed only when we're not doing library level
259 -- finalization, since in this case all interrupt tasks are gone.
261 for N
in reverse Object
.Previous_Handlers
'Range loop
263 (New_Handler
=> Object
.Previous_Handlers
(N
).Handler
,
264 Interrupt
=> Object
.Previous_Handlers
(N
).Interrupt
,
265 Static
=> Object
.Previous_Handlers
(N
).Static
,
266 Restoration
=> True);
269 Tasking
.Protected_Objects
.Entries
.Finalize
270 (Tasking
.Protected_Objects
.Entries
.Protection_Entries
(Object
));
273 -------------------------------------
274 -- Has_Interrupt_Or_Attach_Handler --
275 -------------------------------------
277 function Has_Interrupt_Or_Attach_Handler
278 (Object
: access Static_Interrupt_Protection
)
283 end Has_Interrupt_Or_Attach_Handler
;
285 ----------------------
286 -- Install_Handlers --
287 ----------------------
289 procedure Install_Handlers
290 (Object
: access Static_Interrupt_Protection
;
291 New_Handlers
: in New_Handler_Array
)
294 for N
in New_Handlers
'Range loop
296 -- We need a lock around this ???
298 Object
.Previous_Handlers
(N
).Interrupt
:= New_Handlers
(N
).Interrupt
;
299 Object
.Previous_Handlers
(N
).Static
:= Descriptors
300 (New_Handlers
(N
).Interrupt
).Static
;
302 -- We call Exchange_Handler and not directly Interrupt_Manager.
303 -- Exchange_Handler so we get the Is_Reserved check.
306 (Old_Handler
=> Object
.Previous_Handlers
(N
).Handler
,
307 New_Handler
=> New_Handlers
(N
).Handler
,
308 Interrupt
=> New_Handlers
(N
).Interrupt
,
311 end Install_Handlers
;
313 ---------------------
314 -- Current_Handler --
315 ---------------------
317 function Current_Handler
(Interrupt
: Interrupt_ID
)
318 return Parameterless_Handler
is
320 if Is_Reserved
(Interrupt
) then
324 if Descriptors
(Interrupt
).Kind
= Protected_Procedure
then
325 return Descriptors
(Interrupt
).H
;
335 procedure Attach_Handler
336 (New_Handler
: Parameterless_Handler
;
337 Interrupt
: Interrupt_ID
;
338 Static
: Boolean := False) is
340 Attach_Handler
(New_Handler
, Interrupt
, Static
, False);
343 procedure Attach_Handler
344 (New_Handler
: Parameterless_Handler
;
345 Interrupt
: Interrupt_ID
;
347 Restoration
: Boolean)
349 New_Task
: Server_Task_Access
;
352 if Is_Reserved
(Interrupt
) then
356 if not Restoration
and then not Static
358 -- Tries to overwrite a static Interrupt Handler with a
361 and then (Descriptors
(Interrupt
).Static
363 -- The new handler is not specified as an
364 -- Interrupt Handler by a pragma.
366 or else not Is_Registered
(New_Handler
))
368 Raise_Exception
(Program_Error
'Identity,
369 "Trying to overwrite a static Interrupt Handler with a " &
373 if Handlers
(Interrupt
) = null then
374 New_Task
:= new Server_Task
(Interrupt
);
375 Handlers
(Interrupt
) := To_System
(New_Task
.all'Identity);
378 if intr_attach
(int
(Interrupt
),
379 TISR
(Signal_Handler
'Access)) = FUNC_ERR
384 if New_Handler
= null then
386 -- The null handler means we are detaching the handler.
388 Attached_Interrupts
(Interrupt
) := False;
389 Descriptors
(Interrupt
) :=
390 (Kind
=> Unknown
, T
=> null, E
=> 0, H
=> null, Static
=> False);
393 Descriptors
(Interrupt
).Kind
:= Protected_Procedure
;
394 Descriptors
(Interrupt
).H
:= New_Handler
;
395 Descriptors
(Interrupt
).Static
:= Static
;
396 Attached_Interrupts
(Interrupt
) := True;
400 ----------------------
401 -- Exchange_Handler --
402 ----------------------
404 procedure Exchange_Handler
405 (Old_Handler
: out Parameterless_Handler
;
406 New_Handler
: Parameterless_Handler
;
407 Interrupt
: Interrupt_ID
;
408 Static
: Boolean := False) is
410 if Is_Reserved
(Interrupt
) then
414 if Descriptors
(Interrupt
).Kind
= Task_Entry
then
416 -- In case we have an Interrupt Entry already installed.
417 -- raise a program error. (propagate it to the caller).
419 Raise_Exception
(Program_Error
'Identity,
420 "An interrupt is already installed");
423 Old_Handler
:= Current_Handler
(Interrupt
);
424 Attach_Handler
(New_Handler
, Interrupt
, Static
);
425 end Exchange_Handler
;
431 procedure Detach_Handler
432 (Interrupt
: Interrupt_ID
;
433 Static
: Boolean := False) is
435 if Is_Reserved
(Interrupt
) then
439 if Descriptors
(Interrupt
).Kind
= Task_Entry
then
440 Raise_Exception
(Program_Error
'Identity,
441 "Trying to detach an Interrupt Entry");
444 if not Static
and then Descriptors
(Interrupt
).Static
then
445 Raise_Exception
(Program_Error
'Identity,
446 "Trying to detach a static Interrupt Handler");
449 Attached_Interrupts
(Interrupt
) := False;
450 Descriptors
(Interrupt
) :=
451 (Kind
=> Unknown
, T
=> null, E
=> 0, H
=> null, Static
=> False);
453 if intr_attach
(int
(Interrupt
), null) = FUNC_ERR
then
462 function Reference
(Interrupt
: Interrupt_ID
) return System
.Address
is
463 Signal
: System
.Address
:=
464 System
.Storage_Elements
.To_Address
465 (System
.Storage_Elements
.Integer_Address
(Interrupt
));
468 if Is_Reserved
(Interrupt
) then
469 -- Only usable Interrupts can be used for binding it to an Entry.
476 --------------------------------
477 -- Register_Interrupt_Handler --
478 --------------------------------
480 procedure Register_Interrupt_Handler
(Handler_Addr
: System
.Address
) is
482 Registered_Handlers
:=
483 new Registered_Handler
'(H => Handler_Addr, Next => Registered_Handlers);
484 end Register_Interrupt_Handler;
490 -- See if the Handler has been "pragma"ed using Interrupt_Handler.
491 -- Always consider a null handler as registered.
493 function Is_Registered (Handler : Parameterless_Handler) return Boolean is
494 Ptr : R_Link := Registered_Handlers;
496 type Fat_Ptr is record
497 Object_Addr : System.Address;
498 Handler_Addr : System.Address;
501 function To_Fat_Ptr is new Unchecked_Conversion
502 (Parameterless_Handler, Fat_Ptr);
507 if Handler = null then
511 Fat := To_Fat_Ptr (Handler);
513 while Ptr /= null loop
515 if Ptr.H = Fat.Handler_Addr then
525 -----------------------------
526 -- Bind_Interrupt_To_Entry --
527 -----------------------------
529 procedure Bind_Interrupt_To_Entry
531 E : Task_Entry_Index;
532 Int_Ref : System.Address)
534 Interrupt : constant Interrupt_ID :=
535 Interrupt_ID (Storage_Elements.To_Integer (Int_Ref));
537 New_Task : Server_Task_Access;
540 if Is_Reserved (Interrupt) then
544 if Descriptors (Interrupt).Kind /= Unknown then
545 Raise_Exception (Program_Error'Identity,
546 "A binding for this interrupt is already present");
549 if Handlers (Interrupt) = null then
550 New_Task := new Server_Task (Interrupt);
551 Handlers (Interrupt) := To_System (New_Task.all'Identity);
554 if intr_attach (int (Interrupt),
555 TISR (Signal_Handler'Access)) = FUNC_ERR
560 Descriptors (Interrupt).Kind := Task_Entry;
561 Descriptors (Interrupt).T := T;
562 Descriptors (Interrupt).E := E;
564 -- Indicate the attachment of Interrupt Entry in ATCB.
565 -- This is need so that when an Interrupt Entry task terminates
566 -- the binding can be cleaned. The call to unbinding must be
567 -- make by the task before it terminates.
569 T.Interrupt_Entry := True;
571 Attached_Interrupts (Interrupt) := True;
572 end Bind_Interrupt_To_Entry;
574 ------------------------------
575 -- Detach_Interrupt_Entries --
576 ------------------------------
578 procedure Detach_Interrupt_Entries (T : Task_ID) is
580 for I in Interrupt_ID loop
581 if not Is_Reserved (I) then
582 if Descriptors (I).Kind = Task_Entry and then
583 Descriptors (I).T = T then
584 Attached_Interrupts (I) := False;
585 Descriptors (I).Kind := Unknown;
587 if intr_attach (int (I), null) = FUNC_ERR then
594 -- Indicate in ATCB that no Interrupt Entries are attached.
596 T.Interrupt_Entry := True;
597 end Detach_Interrupt_Entries;
599 ---------------------
600 -- Block_Interrupt --
601 ---------------------
603 procedure Block_Interrupt (Interrupt : Interrupt_ID) is
608 -----------------------
609 -- Unblock_Interrupt --
610 -----------------------
612 procedure Unblock_Interrupt (Interrupt : Interrupt_ID) is
615 end Unblock_Interrupt;
621 function Is_Blocked (Interrupt : Interrupt_ID) return Boolean is
627 task body Server_Task is
628 Desc : Handler_Desc renames Descriptors (Interrupt);
629 Self_Id : Task_ID := STPO.Self;
630 Temp : Parameterless_Handler;
633 Utilities.Make_Independent;
636 while Interrupt_Count (Interrupt) > 0 loop
637 Interrupt_Count (Interrupt) := Interrupt_Count (Interrupt) - 1;
643 Rendezvous.Call_Simple (Desc.T, Desc.E, Null_Address);
644 when Protected_Procedure =>
653 Initialization.Defer_Abort (Self_Id);
659 STPO.Write_Lock (Self_Id);
660 Self_Id.Common.State := Interrupt_Server_Idle_Sleep;
661 STPO.Sleep (Self_Id, Interrupt_Server_Idle_Sleep);
662 Self_Id.Common.State := Runnable;
663 STPO.Unlock (Self_Id);
669 Initialization.Undefer_Abort (Self_Id);
671 -- Undefer abort here to allow a window for this task
672 -- to be aborted at the time of system shutdown.
677 end System.Interrupts;