rs6000: load high and low part of 128bit vector independently [PR110040]
[official-gcc.git] / gcc / ada / libgnarl / s-interr__sigaction.adb
blob34aeca95585a1044118c6b1a57b1e82bb6333321
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT 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) 1998-2024, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT 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. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 -- This is the NT version of this package
34 with Ada.Task_Identification;
35 with Ada.Unchecked_Conversion;
37 with Interfaces.C;
39 with System.Storage_Elements;
40 with System.Task_Primitives.Operations;
41 with System.Tasking.Utilities;
42 with System.Tasking.Rendezvous;
43 with System.Tasking.Initialization;
44 with System.Interrupt_Management;
46 package body System.Interrupts is
48 use Tasking;
49 use System.OS_Interface;
50 use Interfaces.C;
52 package STPO renames System.Task_Primitives.Operations;
53 package IMNG renames System.Interrupt_Management;
55 subtype int is Interfaces.C.int;
57 function To_System is new Ada.Unchecked_Conversion
58 (Ada.Task_Identification.Task_Id, Task_Id);
60 type Handler_Kind is (Unknown, Task_Entry, Protected_Procedure);
62 type Handler_Desc is record
63 Kind : Handler_Kind := Unknown;
64 T : Task_Id;
65 E : Task_Entry_Index;
66 H : Parameterless_Handler;
67 Static : Boolean := False;
68 end record;
70 task type Server_Task (Interrupt : Interrupt_ID) is
71 pragma Interrupt_Priority (System.Interrupt_Priority'Last);
72 end Server_Task;
74 type Server_Task_Access is access Server_Task;
76 Handlers : array (Interrupt_ID) of Task_Id;
77 Descriptors : array (Interrupt_ID) of Handler_Desc;
78 Interrupt_Count : array (Interrupt_ID) of Integer := (others => 0);
80 pragma Volatile_Components (Interrupt_Count);
82 procedure Attach_Handler
83 (New_Handler : Parameterless_Handler;
84 Interrupt : Interrupt_ID;
85 Static : Boolean;
86 Restoration : Boolean);
87 -- This internal procedure is needed to finalize protected objects that
88 -- contain interrupt handlers.
90 procedure Signal_Handler (Sig : Interrupt_ID);
91 pragma Convention (C, Signal_Handler);
92 -- This procedure is used to handle all the signals
94 -- Type and the list containing Registered Interrupt Handlers. These
95 -- definitions are used to register the handlers specified by the pragma
96 -- Interrupt_Handler.
98 --------------------------
99 -- Handler Registration --
100 --------------------------
102 type Registered_Handler;
103 type R_Link is access all Registered_Handler;
105 type Registered_Handler is record
106 H : System.Address;
107 Next : R_Link;
108 end record;
110 Registered_Handlers : R_Link := null;
112 function Is_Registered (Handler : Parameterless_Handler) return Boolean;
113 -- See if the Handler has been "pragma"ed using Interrupt_Handler.
114 -- Always consider a null handler as registered.
116 type Handler_Ptr is access procedure (Sig : Interrupt_ID);
117 pragma Convention (C, Handler_Ptr);
119 function TISR is new Ada.Unchecked_Conversion (Handler_Ptr, isr_address);
121 --------------------
122 -- Signal_Handler --
123 --------------------
125 procedure Signal_Handler (Sig : Interrupt_ID) is
126 Handler : Task_Id renames Handlers (Sig);
128 begin
129 if Intr_Attach_Reset and then
130 intr_attach (int (Sig), TISR (Signal_Handler'Access)) = FUNC_ERR
131 then
132 raise Program_Error;
133 end if;
135 if Handler /= null then
136 Interrupt_Count (Sig) := Interrupt_Count (Sig) + 1;
137 STPO.Wakeup (Handler, Interrupt_Server_Idle_Sleep);
138 end if;
139 end Signal_Handler;
141 -----------------
142 -- Is_Reserved --
143 -----------------
145 function Is_Reserved (Interrupt : Interrupt_ID) return Boolean is
146 begin
147 return IMNG.Reserve (IMNG.Interrupt_ID (Interrupt));
148 end Is_Reserved;
150 -----------------------
151 -- Is_Entry_Attached --
152 -----------------------
154 function Is_Entry_Attached (Interrupt : Interrupt_ID) return Boolean is
155 begin
156 if Is_Reserved (Interrupt) then
157 raise Program_Error with
158 "interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
159 end if;
161 return Descriptors (Interrupt).T /= Null_Task;
162 end Is_Entry_Attached;
164 -------------------------
165 -- Is_Handler_Attached --
166 -------------------------
168 function Is_Handler_Attached (Interrupt : Interrupt_ID) return Boolean is
169 begin
170 if Is_Reserved (Interrupt) then
171 raise Program_Error with
172 "interrupt" & Interrupt_ID'Image (Interrupt) & " is reserved";
173 else
174 return Descriptors (Interrupt).Kind /= Unknown;
175 end if;
176 end Is_Handler_Attached;
178 ----------------
179 -- Is_Ignored --
180 ----------------
182 function Is_Ignored (Interrupt : Interrupt_ID) return Boolean is
183 begin
184 raise Program_Error;
185 return False;
186 end Is_Ignored;
188 ------------------
189 -- Unblocked_By --
190 ------------------
192 function Unblocked_By (Interrupt : Interrupt_ID) return Task_Id is
193 begin
194 raise Program_Error;
195 return Null_Task;
196 end Unblocked_By;
198 ----------------------
199 -- Ignore_Interrupt --
200 ----------------------
202 procedure Ignore_Interrupt (Interrupt : Interrupt_ID) is
203 begin
204 raise Program_Error;
205 end Ignore_Interrupt;
207 ------------------------
208 -- Unignore_Interrupt --
209 ------------------------
211 procedure Unignore_Interrupt (Interrupt : Interrupt_ID) is
212 begin
213 raise Program_Error;
214 end Unignore_Interrupt;
216 -------------------------------------
217 -- Has_Interrupt_Or_Attach_Handler --
218 -------------------------------------
220 function Has_Interrupt_Or_Attach_Handler
221 (Object : access Dynamic_Interrupt_Protection) return Boolean
223 pragma Unreferenced (Object);
224 begin
225 return True;
226 end Has_Interrupt_Or_Attach_Handler;
228 --------------
229 -- Finalize --
230 --------------
232 procedure Finalize (Object : in out Static_Interrupt_Protection) is
233 begin
234 -- ??? loop to be executed only when we're not doing library level
235 -- finalization, since in this case all interrupt tasks are gone.
237 for N in reverse Object.Previous_Handlers'Range loop
238 Attach_Handler
239 (New_Handler => Object.Previous_Handlers (N).Handler,
240 Interrupt => Object.Previous_Handlers (N).Interrupt,
241 Static => Object.Previous_Handlers (N).Static,
242 Restoration => True);
243 end loop;
245 Tasking.Protected_Objects.Entries.Finalize
246 (Tasking.Protected_Objects.Entries.Protection_Entries (Object));
247 end Finalize;
249 -------------------------------------
250 -- Has_Interrupt_Or_Attach_Handler --
251 -------------------------------------
253 function Has_Interrupt_Or_Attach_Handler
254 (Object : access Static_Interrupt_Protection) return Boolean
256 pragma Unreferenced (Object);
257 begin
258 return True;
259 end Has_Interrupt_Or_Attach_Handler;
261 ----------------------
262 -- Install_Handlers --
263 ----------------------
265 procedure Install_Handlers
266 (Object : access Static_Interrupt_Protection;
267 New_Handlers : New_Handler_Array)
269 begin
270 for N in New_Handlers'Range loop
272 -- We need a lock around this ???
274 Object.Previous_Handlers (N).Interrupt := New_Handlers (N).Interrupt;
275 Object.Previous_Handlers (N).Static := Descriptors
276 (New_Handlers (N).Interrupt).Static;
278 -- We call Exchange_Handler and not directly Interrupt_Manager.
279 -- Exchange_Handler so we get the Is_Reserved check.
281 Exchange_Handler
282 (Old_Handler => Object.Previous_Handlers (N).Handler,
283 New_Handler => New_Handlers (N).Handler,
284 Interrupt => New_Handlers (N).Interrupt,
285 Static => True);
286 end loop;
287 end Install_Handlers;
289 ---------------------------------
290 -- Install_Restricted_Handlers --
291 ---------------------------------
293 procedure Install_Restricted_Handlers
294 (Prio : Interrupt_Priority;
295 Handlers : New_Handler_Array)
297 pragma Unreferenced (Prio);
298 begin
299 for N in Handlers'Range loop
300 Attach_Handler (Handlers (N).Handler, Handlers (N).Interrupt, True);
301 end loop;
302 end Install_Restricted_Handlers;
304 ---------------------
305 -- Current_Handler --
306 ---------------------
308 function Current_Handler
309 (Interrupt : Interrupt_ID) return Parameterless_Handler
311 begin
312 if Is_Reserved (Interrupt) then
313 raise Program_Error;
314 end if;
316 if Descriptors (Interrupt).Kind = Protected_Procedure then
317 return Descriptors (Interrupt).H;
318 else
319 return null;
320 end if;
321 end Current_Handler;
323 --------------------
324 -- Attach_Handler --
325 --------------------
327 procedure Attach_Handler
328 (New_Handler : Parameterless_Handler;
329 Interrupt : Interrupt_ID;
330 Static : Boolean := False)
332 begin
333 Attach_Handler (New_Handler, Interrupt, Static, False);
334 end Attach_Handler;
336 procedure Attach_Handler
337 (New_Handler : Parameterless_Handler;
338 Interrupt : Interrupt_ID;
339 Static : Boolean;
340 Restoration : Boolean)
342 New_Task : Server_Task_Access;
344 begin
345 if Is_Reserved (Interrupt) then
346 raise Program_Error;
347 end if;
349 if not Restoration and then not Static
351 -- Tries to overwrite a static Interrupt Handler with dynamic handle
353 and then
354 (Descriptors (Interrupt).Static
356 -- New handler not specified as an Interrupt Handler by a pragma
358 or else not Is_Registered (New_Handler))
359 then
360 raise Program_Error with
361 "trying to overwrite a static interrupt handler with a " &
362 "dynamic handler";
363 end if;
365 if Handlers (Interrupt) = null then
366 New_Task := new Server_Task (Interrupt);
367 Handlers (Interrupt) := To_System (New_Task.all'Identity);
368 end if;
370 if intr_attach (int (Interrupt),
371 TISR (Signal_Handler'Access)) = FUNC_ERR
372 then
373 raise Program_Error;
374 end if;
376 if New_Handler = null then
378 -- The null handler means we are detaching the handler
380 Descriptors (Interrupt) :=
381 (Kind => Unknown, T => null, E => 0, H => null, Static => False);
383 else
384 Descriptors (Interrupt).Kind := Protected_Procedure;
385 Descriptors (Interrupt).H := New_Handler;
386 Descriptors (Interrupt).Static := Static;
387 end if;
388 end Attach_Handler;
390 ----------------------
391 -- Exchange_Handler --
392 ----------------------
394 procedure Exchange_Handler
395 (Old_Handler : out Parameterless_Handler;
396 New_Handler : Parameterless_Handler;
397 Interrupt : Interrupt_ID;
398 Static : Boolean := False)
400 begin
401 if Is_Reserved (Interrupt) then
402 raise Program_Error;
403 end if;
405 if Descriptors (Interrupt).Kind = Task_Entry then
407 -- In case we have an Interrupt Entry already installed, raise a
408 -- program error (propagate it to the caller).
410 raise Program_Error with "an interrupt is already installed";
412 else
413 Old_Handler := Current_Handler (Interrupt);
414 Attach_Handler (New_Handler, Interrupt, Static);
415 end if;
416 end Exchange_Handler;
418 --------------------
419 -- Detach_Handler --
420 --------------------
422 procedure Detach_Handler
423 (Interrupt : Interrupt_ID;
424 Static : Boolean := False)
426 begin
427 if Is_Reserved (Interrupt) then
428 raise Program_Error;
429 end if;
431 if Descriptors (Interrupt).Kind = Task_Entry then
432 raise Program_Error with "trying to detach an interrupt entry";
433 end if;
435 if not Static and then Descriptors (Interrupt).Static then
436 raise Program_Error with
437 "trying to detach a static interrupt handler";
438 end if;
440 Descriptors (Interrupt) :=
441 (Kind => Unknown, T => null, E => 0, H => null, Static => False);
443 if intr_attach (int (Interrupt), null) = FUNC_ERR then
444 raise Program_Error;
445 end if;
446 end Detach_Handler;
448 ---------------
449 -- Reference --
450 ---------------
452 function Reference (Interrupt : Interrupt_ID) return System.Address is
453 Signal : constant System.Address :=
454 System.Storage_Elements.To_Address
455 (System.Storage_Elements.Integer_Address (Interrupt));
457 begin
458 if Is_Reserved (Interrupt) then
460 -- Only usable Interrupts can be used for binding it to an Entry
462 raise Program_Error;
463 end if;
465 return Signal;
466 end Reference;
468 --------------------------------
469 -- Register_Interrupt_Handler --
470 --------------------------------
472 procedure Register_Interrupt_Handler (Handler_Addr : System.Address) is
473 begin
474 -- This routine registers a handler as usable for dynamic interrupt
475 -- handler association. Routines attaching and detaching handlers
476 -- dynamically should determine whether the handler is registered.
477 -- Program_Error should be raised if it is not registered.
479 -- Pragma Interrupt_Handler can only appear in a library level PO
480 -- definition and instantiation. Therefore, we do not need to implement
481 -- an unregister operation. Nor do we need to protect the queue
482 -- structure with a lock.
484 pragma Assert (Handler_Addr /= System.Null_Address);
486 Registered_Handlers :=
487 new Registered_Handler'(H => Handler_Addr, Next => Registered_Handlers);
488 end Register_Interrupt_Handler;
490 -------------------
491 -- Is_Registered --
492 -------------------
494 -- See if the Handler has been "pragma"ed using Interrupt_Handler.
495 -- Always consider a null handler as registered.
497 function Is_Registered (Handler : Parameterless_Handler) return Boolean is
498 Ptr : R_Link := Registered_Handlers;
500 type Acc_Proc is access procedure;
502 type Fat_Ptr is record
503 Object_Addr : System.Address;
504 Handler_Addr : Acc_Proc;
505 end record;
507 function To_Fat_Ptr is new Ada.Unchecked_Conversion
508 (Parameterless_Handler, Fat_Ptr);
510 Fat : Fat_Ptr;
512 begin
513 if Handler = null then
514 return True;
515 end if;
517 Fat := To_Fat_Ptr (Handler);
519 while Ptr /= null loop
520 if Ptr.H = Fat.Handler_Addr.all'Address then
521 return True;
522 end if;
524 Ptr := Ptr.Next;
525 end loop;
527 return False;
528 end Is_Registered;
530 -----------------------------
531 -- Bind_Interrupt_To_Entry --
532 -----------------------------
534 procedure Bind_Interrupt_To_Entry
535 (T : Task_Id;
536 E : Task_Entry_Index;
537 Int_Ref : System.Address)
539 Interrupt : constant Interrupt_ID :=
540 Interrupt_ID (Storage_Elements.To_Integer (Int_Ref));
542 New_Task : Server_Task_Access;
544 begin
545 if Is_Reserved (Interrupt) then
546 raise Program_Error;
547 end if;
549 if Descriptors (Interrupt).Kind /= Unknown then
550 raise Program_Error with
551 "a binding for this interrupt is already present";
552 end if;
554 if Handlers (Interrupt) = null then
555 New_Task := new Server_Task (Interrupt);
556 Handlers (Interrupt) := To_System (New_Task.all'Identity);
557 end if;
559 if intr_attach (int (Interrupt),
560 TISR (Signal_Handler'Access)) = FUNC_ERR
561 then
562 raise Program_Error;
563 end if;
565 Descriptors (Interrupt).Kind := Task_Entry;
566 Descriptors (Interrupt).T := T;
567 Descriptors (Interrupt).E := E;
569 -- Indicate the attachment of Interrupt Entry in ATCB. This is needed so
570 -- that when an Interrupt Entry task terminates the binding can be
571 -- cleaned up. The call to unbinding must be make by the task before it
572 -- terminates.
574 T.Interrupt_Entry := True;
575 end Bind_Interrupt_To_Entry;
577 ------------------------------
578 -- Detach_Interrupt_Entries --
579 ------------------------------
581 procedure Detach_Interrupt_Entries (T : Task_Id) is
582 begin
583 for J in Interrupt_ID loop
584 if not Is_Reserved (J) then
585 if Descriptors (J).Kind = Task_Entry
586 and then Descriptors (J).T = T
587 then
588 Descriptors (J).Kind := Unknown;
590 if intr_attach (int (J), null) = FUNC_ERR then
591 raise Program_Error;
592 end if;
593 end if;
594 end if;
595 end loop;
597 -- Indicate in ATCB that no Interrupt Entries are attached
599 T.Interrupt_Entry := True;
600 end Detach_Interrupt_Entries;
602 ---------------------
603 -- Block_Interrupt --
604 ---------------------
606 procedure Block_Interrupt (Interrupt : Interrupt_ID) is
607 begin
608 raise Program_Error;
609 end Block_Interrupt;
611 -----------------------
612 -- Unblock_Interrupt --
613 -----------------------
615 procedure Unblock_Interrupt (Interrupt : Interrupt_ID) is
616 begin
617 raise Program_Error;
618 end Unblock_Interrupt;
620 ----------------
621 -- Is_Blocked --
622 ----------------
624 function Is_Blocked (Interrupt : Interrupt_ID) return Boolean is
625 begin
626 raise Program_Error;
627 return False;
628 end Is_Blocked;
630 task body Server_Task is
631 Ignore : constant Boolean := Utilities.Make_Independent;
633 Desc : Handler_Desc renames Descriptors (Interrupt);
634 Self_Id : constant Task_Id := STPO.Self;
635 Temp : Parameterless_Handler;
637 begin
638 loop
639 while Interrupt_Count (Interrupt) > 0 loop
640 Interrupt_Count (Interrupt) := Interrupt_Count (Interrupt) - 1;
641 begin
642 case Desc.Kind is
643 when Unknown =>
644 null;
645 when Task_Entry =>
646 Rendezvous.Call_Simple (Desc.T, Desc.E, Null_Address);
647 when Protected_Procedure =>
648 Temp := Desc.H;
649 Temp.all;
650 end case;
651 exception
652 when others => null;
653 end;
654 end loop;
656 Initialization.Defer_Abort (Self_Id);
657 STPO.Write_Lock (Self_Id);
658 Self_Id.Common.State := Interrupt_Server_Idle_Sleep;
659 STPO.Sleep (Self_Id, Interrupt_Server_Idle_Sleep);
660 Self_Id.Common.State := Runnable;
661 STPO.Unlock (Self_Id);
662 Initialization.Undefer_Abort (Self_Id);
664 -- Undefer abort here to allow a window for this task to be aborted
665 -- at the time of system shutdown.
667 end loop;
668 end Server_Task;
670 end System.Interrupts;