s-interr-sigaction.adb [plain text]
with Ada.Task_Identification;
with Ada.Exceptions;
with System.OS_Interface;
with System.Storage_Elements;
with System.Task_Primitives.Operations;
with System.Tasking.Utilities;
with System.Tasking.Rendezvous;
with System.Tasking.Initialization;
with System.Interrupt_Management;
with System.Parameters;
with Interfaces.C;
with Unchecked_Conversion;
package body System.Interrupts is
use Parameters;
use Tasking;
use Ada.Exceptions;
use System.OS_Interface;
use Interfaces.C;
package STPO renames System.Task_Primitives.Operations;
package IMNG renames System.Interrupt_Management;
subtype int is Interfaces.C.int;
function To_System is new Unchecked_Conversion
(Ada.Task_Identification.Task_Id, Task_Id);
type Handler_Kind is (Unknown, Task_Entry, Protected_Procedure);
type Handler_Desc is record
Kind : Handler_Kind := Unknown;
T : Task_Id;
E : Task_Entry_Index;
H : Parameterless_Handler;
Static : Boolean := False;
end record;
task type Server_Task (Interrupt : Interrupt_ID) is
pragma Interrupt_Priority (System.Interrupt_Priority'Last);
end Server_Task;
type Server_Task_Access is access Server_Task;
Handlers : array (Interrupt_ID) of Task_Id;
Descriptors : array (Interrupt_ID) of Handler_Desc;
Interrupt_Count : array (Interrupt_ID) of Integer := (others => 0);
pragma Volatile_Components (Interrupt_Count);
procedure Attach_Handler
(New_Handler : Parameterless_Handler;
Interrupt : Interrupt_ID;
Static : Boolean;
Restoration : Boolean);
procedure Signal_Handler (Sig : Interrupt_ID);
type Registered_Handler;
type R_Link is access all Registered_Handler;
type Registered_Handler is record
H : System.Address := System.Null_Address;
Next : R_Link := null;
end record;
Registered_Handlers : R_Link := null;
function Is_Registered (Handler : Parameterless_Handler) return Boolean;
type Handler_Ptr is access procedure (Sig : Interrupt_ID);
function TISR is new Unchecked_Conversion (Handler_Ptr, isr_address);
procedure Signal_Handler (Sig : Interrupt_ID) is
Handler : Task_Id renames Handlers (Sig);
begin
if Intr_Attach_Reset and then
intr_attach (int (Sig), TISR (Signal_Handler'Access)) = FUNC_ERR
then
raise Program_Error;
end if;
if Handler /= null then
Interrupt_Count (Sig) := Interrupt_Count (Sig) + 1;
STPO.Wakeup (Handler, Interrupt_Server_Idle_Sleep);
end if;
end Signal_Handler;
function Is_Reserved (Interrupt : Interrupt_ID) return Boolean is
begin
return IMNG.Reserve (IMNG.Interrupt_ID (Interrupt));
end Is_Reserved;
function Is_Entry_Attached (Interrupt : Interrupt_ID) return Boolean is
begin
if Is_Reserved (Interrupt) then
Raise_Exception (Program_Error'Identity, "Interrupt" &
Interrupt_ID'Image (Interrupt) & " is reserved");
end if;
return Descriptors (Interrupt).T /= Null_Task;
end Is_Entry_Attached;
function Is_Handler_Attached (Interrupt : Interrupt_ID) return Boolean is
begin
if Is_Reserved (Interrupt) then
Raise_Exception (Program_Error'Identity, "Interrupt" &
Interrupt_ID'Image (Interrupt) & " is reserved");
end if;
return Descriptors (Interrupt).Kind /= Unknown;
end Is_Handler_Attached;
function Is_Ignored (Interrupt : Interrupt_ID) return Boolean is
begin
raise Program_Error;
return False;
end Is_Ignored;
function Unblocked_By (Interrupt : Interrupt_ID) return Task_Id is
begin
raise Program_Error;
return Null_Task;
end Unblocked_By;
procedure Ignore_Interrupt (Interrupt : Interrupt_ID) is
begin
raise Program_Error;
end Ignore_Interrupt;
procedure Unignore_Interrupt (Interrupt : Interrupt_ID) is
begin
raise Program_Error;
end Unignore_Interrupt;
function Has_Interrupt_Or_Attach_Handler
(Object : access Dynamic_Interrupt_Protection) return Boolean
is
pragma Unreferenced (Object);
begin
return True;
end Has_Interrupt_Or_Attach_Handler;
procedure Finalize (Object : in out Static_Interrupt_Protection) is
begin
for N in reverse Object.Previous_Handlers'Range loop
Attach_Handler
(New_Handler => Object.Previous_Handlers (N).Handler,
Interrupt => Object.Previous_Handlers (N).Interrupt,
Static => Object.Previous_Handlers (N).Static,
Restoration => True);
end loop;
Tasking.Protected_Objects.Entries.Finalize
(Tasking.Protected_Objects.Entries.Protection_Entries (Object));
end Finalize;
function Has_Interrupt_Or_Attach_Handler
(Object : access Static_Interrupt_Protection) return Boolean
is
pragma Unreferenced (Object);
begin
return True;
end Has_Interrupt_Or_Attach_Handler;
procedure Install_Handlers
(Object : access Static_Interrupt_Protection;
New_Handlers : New_Handler_Array)
is
begin
for N in New_Handlers'Range loop
Object.Previous_Handlers (N).Interrupt := New_Handlers (N).Interrupt;
Object.Previous_Handlers (N).Static := Descriptors
(New_Handlers (N).Interrupt).Static;
Exchange_Handler
(Old_Handler => Object.Previous_Handlers (N).Handler,
New_Handler => New_Handlers (N).Handler,
Interrupt => New_Handlers (N).Interrupt,
Static => True);
end loop;
end Install_Handlers;
function Current_Handler
(Interrupt : Interrupt_ID) return Parameterless_Handler
is
begin
if Is_Reserved (Interrupt) then
raise Program_Error;
end if;
if Descriptors (Interrupt).Kind = Protected_Procedure then
return Descriptors (Interrupt).H;
else
return null;
end if;
end Current_Handler;
procedure Attach_Handler
(New_Handler : Parameterless_Handler;
Interrupt : Interrupt_ID;
Static : Boolean := False) is
begin
Attach_Handler (New_Handler, Interrupt, Static, False);
end Attach_Handler;
procedure Attach_Handler
(New_Handler : Parameterless_Handler;
Interrupt : Interrupt_ID;
Static : Boolean;
Restoration : Boolean)
is
New_Task : Server_Task_Access;
begin
if Is_Reserved (Interrupt) then
raise Program_Error;
end if;
if not Restoration and then not Static
and then (Descriptors (Interrupt).Static
or else not Is_Registered (New_Handler))
then
Raise_Exception (Program_Error'Identity,
"Trying to overwrite a static Interrupt Handler with a " &
"dynamic Handler");
end if;
if Handlers (Interrupt) = null then
New_Task := new Server_Task (Interrupt);
Handlers (Interrupt) := To_System (New_Task.all'Identity);
end if;
if intr_attach (int (Interrupt),
TISR (Signal_Handler'Access)) = FUNC_ERR
then
raise Program_Error;
end if;
if New_Handler = null then
Descriptors (Interrupt) :=
(Kind => Unknown, T => null, E => 0, H => null, Static => False);
else
Descriptors (Interrupt).Kind := Protected_Procedure;
Descriptors (Interrupt).H := New_Handler;
Descriptors (Interrupt).Static := Static;
end if;
end Attach_Handler;
procedure Exchange_Handler
(Old_Handler : out Parameterless_Handler;
New_Handler : Parameterless_Handler;
Interrupt : Interrupt_ID;
Static : Boolean := False)
is
begin
if Is_Reserved (Interrupt) then
raise Program_Error;
end if;
if Descriptors (Interrupt).Kind = Task_Entry then
Raise_Exception (Program_Error'Identity,
"An interrupt is already installed");
end if;
Old_Handler := Current_Handler (Interrupt);
Attach_Handler (New_Handler, Interrupt, Static);
end Exchange_Handler;
procedure Detach_Handler
(Interrupt : Interrupt_ID;
Static : Boolean := False)
is
begin
if Is_Reserved (Interrupt) then
raise Program_Error;
end if;
if Descriptors (Interrupt).Kind = Task_Entry then
Raise_Exception (Program_Error'Identity,
"Trying to detach an Interrupt Entry");
end if;
if not Static and then Descriptors (Interrupt).Static then
Raise_Exception (Program_Error'Identity,
"Trying to detach a static Interrupt Handler");
end if;
Descriptors (Interrupt) :=
(Kind => Unknown, T => null, E => 0, H => null, Static => False);
if intr_attach (int (Interrupt), null) = FUNC_ERR then
raise Program_Error;
end if;
end Detach_Handler;
function Reference (Interrupt : Interrupt_ID) return System.Address is
Signal : constant System.Address :=
System.Storage_Elements.To_Address
(System.Storage_Elements.Integer_Address (Interrupt));
begin
if Is_Reserved (Interrupt) then
raise Program_Error;
end if;
return Signal;
end Reference;
procedure Register_Interrupt_Handler (Handler_Addr : System.Address) is
begin
Registered_Handlers :=
new Registered_Handler'(H => Handler_Addr, Next => Registered_Handlers);
end Register_Interrupt_Handler;
function Is_Registered (Handler : Parameterless_Handler) return Boolean is
Ptr : R_Link := Registered_Handlers;
type Fat_Ptr is record
Object_Addr : System.Address;
Handler_Addr : System.Address;
end record;
function To_Fat_Ptr is new Unchecked_Conversion
(Parameterless_Handler, Fat_Ptr);
Fat : Fat_Ptr;
begin
if Handler = null then
return True;
end if;
Fat := To_Fat_Ptr (Handler);
while Ptr /= null loop
if Ptr.H = Fat.Handler_Addr then
return True;
end if;
Ptr := Ptr.Next;
end loop;
return False;
end Is_Registered;
procedure Bind_Interrupt_To_Entry
(T : Task_Id;
E : Task_Entry_Index;
Int_Ref : System.Address)
is
Interrupt : constant Interrupt_ID :=
Interrupt_ID (Storage_Elements.To_Integer (Int_Ref));
New_Task : Server_Task_Access;
begin
if Is_Reserved (Interrupt) then
raise Program_Error;
end if;
if Descriptors (Interrupt).Kind /= Unknown then
Raise_Exception (Program_Error'Identity,
"A binding for this interrupt is already present");
end if;
if Handlers (Interrupt) = null then
New_Task := new Server_Task (Interrupt);
Handlers (Interrupt) := To_System (New_Task.all'Identity);
end if;
if intr_attach (int (Interrupt),
TISR (Signal_Handler'Access)) = FUNC_ERR
then
raise Program_Error;
end if;
Descriptors (Interrupt).Kind := Task_Entry;
Descriptors (Interrupt).T := T;
Descriptors (Interrupt).E := E;
T.Interrupt_Entry := True;
end Bind_Interrupt_To_Entry;
procedure Detach_Interrupt_Entries (T : Task_Id) is
begin
for J in Interrupt_ID loop
if not Is_Reserved (J) then
if Descriptors (J).Kind = Task_Entry
and then Descriptors (J).T = T
then
Descriptors (J).Kind := Unknown;
if intr_attach (int (J), null) = FUNC_ERR then
raise Program_Error;
end if;
end if;
end if;
end loop;
T.Interrupt_Entry := True;
end Detach_Interrupt_Entries;
procedure Block_Interrupt (Interrupt : Interrupt_ID) is
begin
raise Program_Error;
end Block_Interrupt;
procedure Unblock_Interrupt (Interrupt : Interrupt_ID) is
begin
raise Program_Error;
end Unblock_Interrupt;
function Is_Blocked (Interrupt : Interrupt_ID) return Boolean is
begin
raise Program_Error;
return False;
end Is_Blocked;
task body Server_Task is
Desc : Handler_Desc renames Descriptors (Interrupt);
Self_Id : constant Task_Id := STPO.Self;
Temp : Parameterless_Handler;
begin
Utilities.Make_Independent;
loop
while Interrupt_Count (Interrupt) > 0 loop
Interrupt_Count (Interrupt) := Interrupt_Count (Interrupt) - 1;
begin
case Desc.Kind is
when Unknown =>
null;
when Task_Entry =>
Rendezvous.Call_Simple (Desc.T, Desc.E, Null_Address);
when Protected_Procedure =>
Temp := Desc.H;
Temp.all;
end case;
exception
when others => null;
end;
end loop;
Initialization.Defer_Abort (Self_Id);
if Single_Lock then
STPO.Lock_RTS;
end if;
STPO.Write_Lock (Self_Id);
Self_Id.Common.State := Interrupt_Server_Idle_Sleep;
STPO.Sleep (Self_Id, Interrupt_Server_Idle_Sleep);
Self_Id.Common.State := Runnable;
STPO.Unlock (Self_Id);
if Single_Lock then
STPO.Unlock_RTS;
end if;
Initialization.Undefer_Abort (Self_Id);
end loop;
end Server_Task;
end System.Interrupts;