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 --
10 -- Copyright (C) 1998-2001 Free Software Fundation --
12 -- GNARL is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNARL; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNARL was developed by the GNARL team at Florida State University. It is --
31 -- now maintained by Ada Core Technologies, Inc. (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 -- This is the IRIX & NT version of this package.
37 with Ada
.Task_Identification
;
41 -- used for Raise_Exception
43 with System
.OS_Interface
;
44 -- used for intr_attach
46 with System
.Storage_Elements
;
47 -- used for To_Address
50 with System
.Task_Primitives
.Operations
;
57 with System
.Tasking
.Utilities
;
58 -- used for Make_Independent
60 with System
.Tasking
.Rendezvous
;
61 -- used for Call_Simple
63 with System
.Tasking
.Initialization
;
64 -- used for Defer_Abort
67 with System
.Interrupt_Management
;
69 with System
.Parameters
;
70 -- used for Single_Lock
75 with Unchecked_Conversion
;
77 package body System
.Interrupts
is
82 use System
.OS_Interface
;
85 package STPO
renames System
.Task_Primitives
.Operations
;
86 package IMNG
renames System
.Interrupt_Management
;
88 subtype int
is Interfaces
.C
.int
;
90 function To_System
is new Unchecked_Conversion
91 (Ada
.Task_Identification
.Task_Id
, Task_ID
);
93 type Handler_Kind
is (Unknown
, Task_Entry
, Protected_Procedure
);
95 type Handler_Desc
is record
96 Kind
: Handler_Kind
:= Unknown
;
99 H
: Parameterless_Handler
;
100 Static
: Boolean := False;
103 task type Server_Task
(Interrupt
: Interrupt_ID
) is
104 pragma Interrupt_Priority
(System
.Interrupt_Priority
'Last);
107 type Server_Task_Access
is access Server_Task
;
109 Attached_Interrupts
: array (Interrupt_ID
) of Boolean;
110 Handlers
: array (Interrupt_ID
) of Task_ID
;
111 Descriptors
: array (Interrupt_ID
) of Handler_Desc
;
112 Interrupt_Count
: array (Interrupt_ID
) of Integer := (others => 0);
114 pragma Volatile_Components
(Interrupt_Count
);
116 procedure Attach_Handler
117 (New_Handler
: Parameterless_Handler
;
118 Interrupt
: Interrupt_ID
;
120 Restoration
: Boolean);
121 -- This internal procedure is needed to finalize protected objects
122 -- that contain interrupt handlers.
124 procedure Signal_Handler
(Sig
: Interrupt_ID
);
125 -- This procedure is used to handle all the signals.
127 -- Type and Head, Tail of the list containing Registered Interrupt
128 -- Handlers. These definitions are used to register the handlers
129 -- specified by the pragma Interrupt_Handler.
132 -- Handler Registration:
135 type Registered_Handler
;
136 type R_Link
is access all Registered_Handler
;
138 type Registered_Handler
is record
139 H
: System
.Address
:= System
.Null_Address
;
140 Next
: R_Link
:= null;
143 Registered_Handlers
: R_Link
:= null;
145 function Is_Registered
(Handler
: Parameterless_Handler
) return Boolean;
146 -- See if the Handler has been "pragma"ed using Interrupt_Handler.
147 -- Always consider a null handler as registered.
149 type Handler_Ptr
is access procedure (Sig
: Interrupt_ID
);
151 function TISR
is new Unchecked_Conversion
(Handler_Ptr
, isr_address
);
153 procedure Signal_Handler
(Sig
: Interrupt_ID
) is
154 Handler
: Task_ID
renames Handlers
(Sig
);
156 if Intr_Attach_Reset
and then
157 intr_attach
(int
(Sig
), TISR
(Signal_Handler
'Access)) = FUNC_ERR
162 if Handler
/= null then
163 Interrupt_Count
(Sig
) := Interrupt_Count
(Sig
) + 1;
164 STPO
.Wakeup
(Handler
, Interrupt_Server_Idle_Sleep
);
172 function Is_Reserved
(Interrupt
: Interrupt_ID
) return Boolean is
174 return IMNG
.Reserve
(IMNG
.Interrupt_ID
(Interrupt
));
177 -----------------------
178 -- Is_Entry_Attached --
179 -----------------------
181 function Is_Entry_Attached
(Interrupt
: Interrupt_ID
) return Boolean is
183 if Is_Reserved
(Interrupt
) then
184 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
185 Interrupt_ID
'Image (Interrupt
) & " is reserved");
188 return Descriptors
(Interrupt
).T
/= Null_Task
;
189 end Is_Entry_Attached
;
191 -------------------------
192 -- Is_Handler_Attached --
193 -------------------------
195 function Is_Handler_Attached
(Interrupt
: Interrupt_ID
) return Boolean is
197 if Is_Reserved
(Interrupt
) then
198 Raise_Exception
(Program_Error
'Identity, "Interrupt" &
199 Interrupt_ID
'Image (Interrupt
) & " is reserved");
202 return Descriptors
(Interrupt
).Kind
/= Unknown
;
203 end Is_Handler_Attached
;
209 function Is_Ignored
(Interrupt
: Interrupt_ID
) return Boolean is
219 function Unblocked_By
(Interrupt
: Interrupt_ID
) return Task_ID
is
225 ----------------------
226 -- Ignore_Interrupt --
227 ----------------------
229 procedure Ignore_Interrupt
(Interrupt
: Interrupt_ID
) is
232 end Ignore_Interrupt
;
234 ------------------------
235 -- Unignore_Interrupt --
236 ------------------------
238 procedure Unignore_Interrupt
(Interrupt
: Interrupt_ID
) is
241 end Unignore_Interrupt
;
243 -------------------------------------
244 -- Has_Interrupt_Or_Attach_Handler --
245 -------------------------------------
247 function Has_Interrupt_Or_Attach_Handler
248 (Object
: access Dynamic_Interrupt_Protection
) return Boolean is
251 end Has_Interrupt_Or_Attach_Handler
;
257 procedure Finalize
(Object
: in out Static_Interrupt_Protection
) is
259 -- ??? loop to be executed only when we're not doing library level
260 -- finalization, since in this case all interrupt tasks are gone.
262 for N
in reverse Object
.Previous_Handlers
'Range loop
264 (New_Handler
=> Object
.Previous_Handlers
(N
).Handler
,
265 Interrupt
=> Object
.Previous_Handlers
(N
).Interrupt
,
266 Static
=> Object
.Previous_Handlers
(N
).Static
,
267 Restoration
=> True);
270 Tasking
.Protected_Objects
.Entries
.Finalize
271 (Tasking
.Protected_Objects
.Entries
.Protection_Entries
(Object
));
274 -------------------------------------
275 -- Has_Interrupt_Or_Attach_Handler --
276 -------------------------------------
278 function Has_Interrupt_Or_Attach_Handler
279 (Object
: access Static_Interrupt_Protection
)
284 end Has_Interrupt_Or_Attach_Handler
;
286 ----------------------
287 -- Install_Handlers --
288 ----------------------
290 procedure Install_Handlers
291 (Object
: access Static_Interrupt_Protection
;
292 New_Handlers
: in New_Handler_Array
)
295 for N
in New_Handlers
'Range loop
297 -- We need a lock around this ???
299 Object
.Previous_Handlers
(N
).Interrupt
:= New_Handlers
(N
).Interrupt
;
300 Object
.Previous_Handlers
(N
).Static
:= Descriptors
301 (New_Handlers
(N
).Interrupt
).Static
;
303 -- We call Exchange_Handler and not directly Interrupt_Manager.
304 -- Exchange_Handler so we get the Is_Reserved check.
307 (Old_Handler
=> Object
.Previous_Handlers
(N
).Handler
,
308 New_Handler
=> New_Handlers
(N
).Handler
,
309 Interrupt
=> New_Handlers
(N
).Interrupt
,
312 end Install_Handlers
;
314 ---------------------
315 -- Current_Handler --
316 ---------------------
318 function Current_Handler
(Interrupt
: Interrupt_ID
)
319 return Parameterless_Handler
is
321 if Is_Reserved
(Interrupt
) then
325 if Descriptors
(Interrupt
).Kind
= Protected_Procedure
then
326 return Descriptors
(Interrupt
).H
;
336 procedure Attach_Handler
337 (New_Handler
: Parameterless_Handler
;
338 Interrupt
: Interrupt_ID
;
339 Static
: Boolean := False) is
341 Attach_Handler
(New_Handler
, Interrupt
, Static
, False);
344 procedure Attach_Handler
345 (New_Handler
: Parameterless_Handler
;
346 Interrupt
: Interrupt_ID
;
348 Restoration
: Boolean)
350 New_Task
: Server_Task_Access
;
353 if Is_Reserved
(Interrupt
) then
357 if not Restoration
and then not Static
359 -- Tries to overwrite a static Interrupt Handler with a
362 and then (Descriptors
(Interrupt
).Static
364 -- The new handler is not specified as an
365 -- Interrupt Handler by a pragma.
367 or else not Is_Registered
(New_Handler
))
369 Raise_Exception
(Program_Error
'Identity,
370 "Trying to overwrite a static Interrupt Handler with a " &
374 if Handlers
(Interrupt
) = null then
375 New_Task
:= new Server_Task
(Interrupt
);
376 Handlers
(Interrupt
) := To_System
(New_Task
.all'Identity);
379 if intr_attach
(int
(Interrupt
),
380 TISR
(Signal_Handler
'Access)) = FUNC_ERR
385 if New_Handler
= null then
387 -- The null handler means we are detaching the handler.
389 Attached_Interrupts
(Interrupt
) := False;
390 Descriptors
(Interrupt
) :=
391 (Kind
=> Unknown
, T
=> null, E
=> 0, H
=> null, Static
=> False);
394 Descriptors
(Interrupt
).Kind
:= Protected_Procedure
;
395 Descriptors
(Interrupt
).H
:= New_Handler
;
396 Descriptors
(Interrupt
).Static
:= Static
;
397 Attached_Interrupts
(Interrupt
) := True;
401 ----------------------
402 -- Exchange_Handler --
403 ----------------------
405 procedure Exchange_Handler
406 (Old_Handler
: out Parameterless_Handler
;
407 New_Handler
: Parameterless_Handler
;
408 Interrupt
: Interrupt_ID
;
409 Static
: Boolean := False) is
411 if Is_Reserved
(Interrupt
) then
415 if Descriptors
(Interrupt
).Kind
= Task_Entry
then
417 -- In case we have an Interrupt Entry already installed.
418 -- raise a program error. (propagate it to the caller).
420 Raise_Exception
(Program_Error
'Identity,
421 "An interrupt is already installed");
424 Old_Handler
:= Current_Handler
(Interrupt
);
425 Attach_Handler
(New_Handler
, Interrupt
, Static
);
426 end Exchange_Handler
;
432 procedure Detach_Handler
433 (Interrupt
: Interrupt_ID
;
434 Static
: Boolean := False) is
436 if Is_Reserved
(Interrupt
) then
440 if Descriptors
(Interrupt
).Kind
= Task_Entry
then
441 Raise_Exception
(Program_Error
'Identity,
442 "Trying to detach an Interrupt Entry");
445 if not Static
and then Descriptors
(Interrupt
).Static
then
446 Raise_Exception
(Program_Error
'Identity,
447 "Trying to detach a static Interrupt Handler");
450 Attached_Interrupts
(Interrupt
) := False;
451 Descriptors
(Interrupt
) :=
452 (Kind
=> Unknown
, T
=> null, E
=> 0, H
=> null, Static
=> False);
454 if intr_attach
(int
(Interrupt
), null) = FUNC_ERR
then
463 function Reference
(Interrupt
: Interrupt_ID
) return System
.Address
is
464 Signal
: System
.Address
:=
465 System
.Storage_Elements
.To_Address
466 (System
.Storage_Elements
.Integer_Address
(Interrupt
));
469 if Is_Reserved
(Interrupt
) then
470 -- Only usable Interrupts can be used for binding it to an Entry.
477 --------------------------------
478 -- Register_Interrupt_Handler --
479 --------------------------------
481 procedure Register_Interrupt_Handler
(Handler_Addr
: System
.Address
) is
483 Registered_Handlers
:=
484 new Registered_Handler
'(H => Handler_Addr, Next => Registered_Handlers);
485 end Register_Interrupt_Handler;
491 -- See if the Handler has been "pragma"ed using Interrupt_Handler.
492 -- Always consider a null handler as registered.
494 function Is_Registered (Handler : Parameterless_Handler) return Boolean is
495 Ptr : R_Link := Registered_Handlers;
497 type Fat_Ptr is record
498 Object_Addr : System.Address;
499 Handler_Addr : System.Address;
502 function To_Fat_Ptr is new Unchecked_Conversion
503 (Parameterless_Handler, Fat_Ptr);
508 if Handler = null then
512 Fat := To_Fat_Ptr (Handler);
514 while Ptr /= null loop
516 if Ptr.H = Fat.Handler_Addr then
526 -----------------------------
527 -- Bind_Interrupt_To_Entry --
528 -----------------------------
530 procedure Bind_Interrupt_To_Entry
532 E : Task_Entry_Index;
533 Int_Ref : System.Address)
535 Interrupt : constant Interrupt_ID :=
536 Interrupt_ID (Storage_Elements.To_Integer (Int_Ref));
538 New_Task : Server_Task_Access;
541 if Is_Reserved (Interrupt) then
545 if Descriptors (Interrupt).Kind /= Unknown then
546 Raise_Exception (Program_Error'Identity,
547 "A binding for this interrupt is already present");
550 if Handlers (Interrupt) = null then
551 New_Task := new Server_Task (Interrupt);
552 Handlers (Interrupt) := To_System (New_Task.all'Identity);
555 if intr_attach (int (Interrupt),
556 TISR (Signal_Handler'Access)) = FUNC_ERR
561 Descriptors (Interrupt).Kind := Task_Entry;
562 Descriptors (Interrupt).T := T;
563 Descriptors (Interrupt).E := E;
565 -- Indicate the attachment of Interrupt Entry in ATCB.
566 -- This is need so that when an Interrupt Entry task terminates
567 -- the binding can be cleaned. The call to unbinding must be
568 -- make by the task before it terminates.
570 T.Interrupt_Entry := True;
572 Attached_Interrupts (Interrupt) := True;
573 end Bind_Interrupt_To_Entry;
575 ------------------------------
576 -- Detach_Interrupt_Entries --
577 ------------------------------
579 procedure Detach_Interrupt_Entries (T : Task_ID) is
581 for I in Interrupt_ID loop
582 if not Is_Reserved (I) then
583 if Descriptors (I).Kind = Task_Entry and then
584 Descriptors (I).T = T then
585 Attached_Interrupts (I) := False;
586 Descriptors (I).Kind := Unknown;
588 if intr_attach (int (I), null) = FUNC_ERR then
595 -- Indicate in ATCB that no Interrupt Entries are attached.
597 T.Interrupt_Entry := True;
598 end Detach_Interrupt_Entries;
600 ---------------------
601 -- Block_Interrupt --
602 ---------------------
604 procedure Block_Interrupt (Interrupt : Interrupt_ID) is
609 -----------------------
610 -- Unblock_Interrupt --
611 -----------------------
613 procedure Unblock_Interrupt (Interrupt : Interrupt_ID) is
616 end Unblock_Interrupt;
622 function Is_Blocked (Interrupt : Interrupt_ID) return Boolean is
628 task body Server_Task is
629 Desc : Handler_Desc renames Descriptors (Interrupt);
630 Self_Id : Task_ID := STPO.Self;
631 Temp : Parameterless_Handler;
634 Utilities.Make_Independent;
637 while Interrupt_Count (Interrupt) > 0 loop
638 Interrupt_Count (Interrupt) := Interrupt_Count (Interrupt) - 1;
644 Rendezvous.Call_Simple (Desc.T, Desc.E, Null_Address);
645 when Protected_Procedure =>
654 Initialization.Defer_Abort (Self_Id);
660 STPO.Write_Lock (Self_Id);
661 Self_Id.Common.State := Interrupt_Server_Idle_Sleep;
662 STPO.Sleep (Self_Id, Interrupt_Server_Idle_Sleep);
663 Self_Id.Common.State := Runnable;
664 STPO.Unlock (Self_Id);
670 Initialization.Undefer_Abort (Self_Id);
672 -- Undefer abort here to allow a window for this task
673 -- to be aborted at the time of system shutdown.
678 end System.Interrupts;