s-interr-vxworks.adb [plain text]
with Unchecked_Conversion;
with System.OS_Interface; use System.OS_Interface;
with Interfaces.VxWorks;
with Ada.Task_Identification;
with Ada.Exceptions;
with System.Interrupt_Management;
with System.Task_Primitives.Operations;
with System.Storage_Elements;
with System.Tasking.Utilities;
with System.Tasking.Rendezvous;
pragma Elaborate_All (System.Tasking.Rendezvous);
package body System.Interrupts is
use Tasking;
use Ada.Exceptions;
package POP renames System.Task_Primitives.Operations;
function To_Ada is new Unchecked_Conversion
(System.Tasking.Task_Id, Ada.Task_Identification.Task_Id);
function To_System is new Unchecked_Conversion
(Ada.Task_Identification.Task_Id, Task_Id);
task Interrupt_Manager is
entry Detach_Interrupt_Entries (T : Task_Id);
entry Attach_Handler
(New_Handler : Parameterless_Handler;
Interrupt : Interrupt_ID;
Static : Boolean;
Restoration : Boolean := False);
entry Exchange_Handler
(Old_Handler : out Parameterless_Handler;
New_Handler : Parameterless_Handler;
Interrupt : Interrupt_ID;
Static : Boolean);
entry Detach_Handler
(Interrupt : Interrupt_ID;
Static : Boolean);
entry Bind_Interrupt_To_Entry
(T : Task_Id;
E : Task_Entry_Index;
Interrupt : Interrupt_ID);
pragma Interrupt_Priority (System.Interrupt_Priority'First);
end Interrupt_Manager;
task type Interrupt_Server_Task
(Interrupt : Interrupt_ID; Int_Sema : SEM_ID) is
pragma Interrupt_Priority (System.Interrupt_Priority'First + 2);
end Interrupt_Server_Task;
type Interrupt_Task_Access is access Interrupt_Server_Task;
type Entry_Assoc is record
T : Task_Id;
E : Task_Entry_Index;
end record;
type Handler_Assoc is record
H : Parameterless_Handler;
Static : Boolean; end record;
User_Handler : array (Interrupt_ID) of Handler_Assoc :=
(others => (null, Static => False));
pragma Volatile_Components (User_Handler);
User_Entry : array (Interrupt_ID) of Entry_Assoc :=
(others => (T => Null_Task, E => Null_Task_Entry));
pragma Volatile_Components (User_Entry);
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_Handler_Head : R_Link := null;
Registered_Handler_Tail : R_Link := null;
Server_ID : array (Interrupt_ID) of System.Tasking.Task_Id :=
(others => System.Tasking.Null_Task);
pragma Atomic_Components (Server_ID);
Semaphore_ID_Map : array
(Interrupt_ID range 0 .. System.OS_Interface.Max_HW_Interrupt)
of SEM_ID := (others => 0);
Interrupt_Access_Hold : Interrupt_Task_Access;
Default_Handler : array (HW_Interrupt) of Interfaces.VxWorks.VOIDFUNCPTR;
procedure Check_Reserved_Interrupt (Interrupt : Interrupt_ID);
procedure Finalize_Interrupt_Servers;
function Is_Registered (Handler : Parameterless_Handler) return Boolean;
procedure Notify_Interrupt (Param : System.Address);
procedure Install_Default_Action (Interrupt : HW_Interrupt);
procedure Install_Umbrella_Handler
(Interrupt : HW_Interrupt;
Handler : Interfaces.VxWorks.VOIDFUNCPTR);
procedure Unimplemented (Feature : String);
pragma No_Return (Unimplemented);
procedure Attach_Handler
(New_Handler : Parameterless_Handler;
Interrupt : Interrupt_ID;
Static : Boolean := False) is
begin
Check_Reserved_Interrupt (Interrupt);
Interrupt_Manager.Attach_Handler (New_Handler, Interrupt, Static);
end Attach_Handler;
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));
begin
Check_Reserved_Interrupt (Interrupt);
Interrupt_Manager.Bind_Interrupt_To_Entry (T, E, Interrupt);
end Bind_Interrupt_To_Entry;
procedure Block_Interrupt (Interrupt : Interrupt_ID) is
begin
Unimplemented ("Block_Interrupt");
end Block_Interrupt;
procedure Check_Reserved_Interrupt (Interrupt : Interrupt_ID) is
begin
if Is_Reserved (Interrupt) then
Raise_Exception
(Program_Error'Identity,
"Interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved");
else
return;
end if;
end Check_Reserved_Interrupt;
function Current_Handler
(Interrupt : Interrupt_ID) return Parameterless_Handler
is
begin
Check_Reserved_Interrupt (Interrupt);
return User_Handler (Interrupt).H;
end Current_Handler;
procedure Detach_Handler
(Interrupt : Interrupt_ID;
Static : Boolean := False) is
begin
Check_Reserved_Interrupt (Interrupt);
Interrupt_Manager.Detach_Handler (Interrupt, Static);
end Detach_Handler;
procedure Detach_Interrupt_Entries (T : Task_Id) is
begin
Interrupt_Manager.Detach_Interrupt_Entries (T);
end Detach_Interrupt_Entries;
procedure Exchange_Handler
(Old_Handler : out Parameterless_Handler;
New_Handler : Parameterless_Handler;
Interrupt : Interrupt_ID;
Static : Boolean := False)
is
begin
Check_Reserved_Interrupt (Interrupt);
Interrupt_Manager.Exchange_Handler
(Old_Handler, New_Handler, Interrupt, Static);
end Exchange_Handler;
procedure Finalize (Object : in out Static_Interrupt_Protection) is
begin
if not Interrupt_Manager'Terminated then
for N in reverse Object.Previous_Handlers'Range loop
Interrupt_Manager.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;
end if;
Tasking.Protected_Objects.Entries.Finalize
(Tasking.Protected_Objects.Entries.Protection_Entries (Object));
end Finalize;
procedure Finalize_Interrupt_Servers is
HW_Interrupts : constant Boolean := HW_Interrupt'Last >= 0;
begin
if HW_Interrupts then
for Int in HW_Interrupt loop
if Server_ID (Interrupt_ID (Int)) /= null
and then
not Ada.Task_Identification.Is_Terminated
(To_Ada (Server_ID (Interrupt_ID (Int))))
then
Interrupt_Manager.Attach_Handler
(New_Handler => null,
Interrupt => Interrupt_ID (Int),
Static => True,
Restoration => True);
end if;
end loop;
end if;
end Finalize_Interrupt_Servers;
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;
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 Ignore_Interrupt (Interrupt : Interrupt_ID) is
begin
Unimplemented ("Ignore_Interrupt");
end Ignore_Interrupt;
procedure Install_Default_Action (Interrupt : HW_Interrupt) is
begin
Interfaces.VxWorks.intVecSet
(Interfaces.VxWorks.INUM_TO_IVEC (Integer (Interrupt)),
Default_Handler (Interrupt));
Default_Handler (Interrupt) := null;
end Install_Default_Action;
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 := User_Handler
(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;
procedure Install_Umbrella_Handler
(Interrupt : HW_Interrupt;
Handler : Interfaces.VxWorks.VOIDFUNCPTR)
is
use Interfaces.VxWorks;
Vec : constant Interrupt_Vector :=
INUM_TO_IVEC (Interfaces.VxWorks.int (Interrupt));
Old_Handler : constant VOIDFUNCPTR :=
intVecGet
(INUM_TO_IVEC (Interfaces.VxWorks.int (Interrupt)));
Stat : Interfaces.VxWorks.STATUS;
pragma Unreferenced (Stat);
begin
if Default_Handler (Interrupt) = null then
Stat :=
intConnect (Vec, Handler, System.Address (Interrupt));
Default_Handler (Interrupt) := Old_Handler;
end if;
end Install_Umbrella_Handler;
function Is_Blocked (Interrupt : Interrupt_ID) return Boolean is
begin
Unimplemented ("Is_Blocked");
return False;
end Is_Blocked;
function Is_Entry_Attached (Interrupt : Interrupt_ID) return Boolean is
begin
Check_Reserved_Interrupt (Interrupt);
return User_Entry (Interrupt).T /= Null_Task;
end Is_Entry_Attached;
function Is_Handler_Attached (Interrupt : Interrupt_ID) return Boolean is
begin
Check_Reserved_Interrupt (Interrupt);
return User_Handler (Interrupt).H /= null;
end Is_Handler_Attached;
function Is_Ignored (Interrupt : Interrupt_ID) return Boolean is
begin
Unimplemented ("Is_Ignored");
return False;
end Is_Ignored;
function Is_Registered (Handler : Parameterless_Handler) return Boolean is
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);
Ptr : R_Link;
Fat : Fat_Ptr;
begin
if Handler = null then
return True;
end if;
Fat := To_Fat_Ptr (Handler);
Ptr := Registered_Handler_Head;
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;
function Is_Reserved (Interrupt : Interrupt_ID) return Boolean is
use System.Interrupt_Management;
begin
return Reserve (System.Interrupt_Management.Interrupt_ID (Interrupt));
end Is_Reserved;
procedure Notify_Interrupt (Param : System.Address) is
Interrupt : constant Interrupt_ID := Interrupt_ID (Param);
Discard_Result : STATUS;
pragma Unreferenced (Discard_Result);
begin
Discard_Result := semGive (Semaphore_ID_Map (Interrupt));
end Notify_Interrupt;
function Reference (Interrupt : Interrupt_ID) return System.Address is
begin
Check_Reserved_Interrupt (Interrupt);
return Storage_Elements.To_Address
(Storage_Elements.Integer_Address (Interrupt));
end Reference;
procedure Register_Interrupt_Handler (Handler_Addr : System.Address) is
New_Node_Ptr : R_Link;
begin
pragma Assert (Handler_Addr /= System.Null_Address);
New_Node_Ptr := new Registered_Handler;
New_Node_Ptr.H := Handler_Addr;
if Registered_Handler_Head = null then
Registered_Handler_Head := New_Node_Ptr;
Registered_Handler_Tail := New_Node_Ptr;
else
Registered_Handler_Tail.Next := New_Node_Ptr;
Registered_Handler_Tail := New_Node_Ptr;
end if;
end Register_Interrupt_Handler;
procedure Unblock_Interrupt (Interrupt : Interrupt_ID) is
begin
Unimplemented ("Unblock_Interrupt");
end Unblock_Interrupt;
function Unblocked_By
(Interrupt : Interrupt_ID) return System.Tasking.Task_Id
is
begin
Unimplemented ("Unblocked_By");
return Null_Task;
end Unblocked_By;
procedure Unignore_Interrupt (Interrupt : Interrupt_ID) is
begin
Unimplemented ("Unignore_Interrupt");
end Unignore_Interrupt;
procedure Unimplemented (Feature : String) is
begin
Raise_Exception
(Program_Error'Identity,
Feature & " not implemented on VxWorks");
end Unimplemented;
task body Interrupt_Manager is
procedure Bind_Handler (Interrupt : Interrupt_ID);
procedure Unbind_Handler (Interrupt : Interrupt_ID);
procedure Unprotected_Exchange_Handler
(Old_Handler : out Parameterless_Handler;
New_Handler : Parameterless_Handler;
Interrupt : Interrupt_ID;
Static : Boolean;
Restoration : Boolean := False);
procedure Unprotected_Detach_Handler
(Interrupt : Interrupt_ID;
Static : Boolean);
procedure Bind_Handler (Interrupt : Interrupt_ID) is
begin
Install_Umbrella_Handler
(HW_Interrupt (Interrupt), Notify_Interrupt'Access);
end Bind_Handler;
procedure Unbind_Handler (Interrupt : Interrupt_ID) is
S : STATUS;
use type STATUS;
begin
Install_Default_Action (HW_Interrupt (Interrupt));
S := semFlush (Semaphore_ID_Map (Interrupt));
pragma Assert (S = 0);
end Unbind_Handler;
procedure Unprotected_Detach_Handler
(Interrupt : Interrupt_ID;
Static : Boolean)
is
Old_Handler : Parameterless_Handler;
begin
if User_Entry (Interrupt).T /= Null_Task then
Raise_Exception (Program_Error'Identity,
"An interrupt entry is already installed");
end if;
if not Static and then User_Handler (Interrupt).Static then
Raise_Exception (Program_Error'Identity,
"Trying to detach a static Interrupt Handler");
end if;
Old_Handler := User_Handler (Interrupt).H;
User_Handler (Interrupt).H := null;
User_Handler (Interrupt).Static := False;
if Old_Handler /= null then
Unbind_Handler (Interrupt);
end if;
end Unprotected_Detach_Handler;
procedure Unprotected_Exchange_Handler
(Old_Handler : out Parameterless_Handler;
New_Handler : Parameterless_Handler;
Interrupt : Interrupt_ID;
Static : Boolean;
Restoration : Boolean := False)
is
begin
if User_Entry (Interrupt).T /= Null_Task then
Raise_Exception
(Program_Error'Identity,
"An interrupt is already installed");
end if;
if not Restoration and then not Static
and then (User_Handler (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;
Old_Handler := User_Handler (Interrupt).H;
User_Handler (Interrupt).H := New_Handler;
if New_Handler = null then
User_Handler (Interrupt).Static := False;
else
User_Handler (Interrupt).Static := Static;
end if;
if New_Handler /= null
and then
(Server_ID (Interrupt) = Null_Task
or else
Ada.Task_Identification.Is_Terminated
(To_Ada (Server_ID (Interrupt))))
then
Interrupt_Access_Hold :=
new Interrupt_Server_Task
(Interrupt, semBCreate (SEM_Q_FIFO, SEM_EMPTY));
Server_ID (Interrupt) :=
To_System (Interrupt_Access_Hold.all'Identity);
end if;
if (New_Handler = null) and then Old_Handler /= null then
Unbind_Handler (Interrupt);
elsif Old_Handler = null then
Bind_Handler (Interrupt);
end if;
end Unprotected_Exchange_Handler;
begin
System.Tasking.Utilities.Make_Independent;
loop
declare
Old_Handler : Parameterless_Handler;
begin
select
accept Attach_Handler
(New_Handler : Parameterless_Handler;
Interrupt : Interrupt_ID;
Static : Boolean;
Restoration : Boolean := False)
do
Unprotected_Exchange_Handler
(Old_Handler, New_Handler, Interrupt, Static, Restoration);
end Attach_Handler;
or
accept Exchange_Handler
(Old_Handler : out Parameterless_Handler;
New_Handler : Parameterless_Handler;
Interrupt : Interrupt_ID;
Static : Boolean)
do
Unprotected_Exchange_Handler
(Old_Handler, New_Handler, Interrupt, Static);
end Exchange_Handler;
or
accept Detach_Handler
(Interrupt : Interrupt_ID;
Static : Boolean)
do
Unprotected_Detach_Handler (Interrupt, Static);
end Detach_Handler;
or
accept Bind_Interrupt_To_Entry
(T : Task_Id;
E : Task_Entry_Index;
Interrupt : Interrupt_ID)
do
if User_Handler (Interrupt).H /= null
or else User_Entry (Interrupt).T /= Null_Task
then
Raise_Exception
(Program_Error'Identity,
"A binding for this interrupt is already present");
end if;
User_Entry (Interrupt) := Entry_Assoc'(T => T, E => E);
T.Interrupt_Entry := True;
if Server_ID (Interrupt) = Null_Task
or else
Ada.Task_Identification.Is_Terminated
(To_Ada (Server_ID (Interrupt)))
then
Interrupt_Access_Hold := new Interrupt_Server_Task
(Interrupt, semBCreate (SEM_Q_FIFO, SEM_EMPTY));
Server_ID (Interrupt) :=
To_System (Interrupt_Access_Hold.all'Identity);
end if;
Bind_Handler (Interrupt);
end Bind_Interrupt_To_Entry;
or
accept Detach_Interrupt_Entries (T : Task_Id) do
for Int in Interrupt_ID'Range loop
if not Is_Reserved (Int) then
if User_Entry (Int).T = T then
User_Entry (Int) :=
Entry_Assoc'
(T => Null_Task, E => Null_Task_Entry);
Unbind_Handler (Int);
end if;
end if;
end loop;
T.Interrupt_Entry := False;
end Detach_Interrupt_Entries;
end select;
exception
when Program_Error =>
null;
when others =>
pragma Assert (False);
null;
end;
end loop;
exception
when Standard'Abort_Signal =>
Finalize_Interrupt_Servers;
raise;
end Interrupt_Manager;
task body Interrupt_Server_Task is
Self_Id : constant Task_Id := Self;
Tmp_Handler : Parameterless_Handler;
Tmp_ID : Task_Id;
Tmp_Entry_Index : Task_Entry_Index;
S : STATUS;
use type STATUS;
begin
System.Tasking.Utilities.Make_Independent;
Semaphore_ID_Map (Interrupt) := Int_Sema;
loop
S := semTake (Int_Sema, WAIT_FOREVER);
pragma Assert (S = 0);
if User_Handler (Interrupt).H /= null then
Tmp_Handler := User_Handler (Interrupt).H;
Tmp_Handler.all;
elsif User_Entry (Interrupt).T /= Null_Task then
Tmp_ID := User_Entry (Interrupt).T;
Tmp_Entry_Index := User_Entry (Interrupt).E;
System.Tasking.Rendezvous.Call_Simple
(Tmp_ID, Tmp_Entry_Index, System.Null_Address);
else
POP.Write_Lock (Self_Id);
S := semDelete (Int_Sema);
pragma Assert (S = 0);
Semaphore_ID_Map (Interrupt) := 0;
Server_ID (Interrupt) := Null_Task;
POP.Unlock (Self_Id);
exit;
end if;
end loop;
end Interrupt_Server_Task;
begin
Interrupt_Manager_ID := To_System (Interrupt_Manager'Identity);
end System.Interrupts;