1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . S T A C K _ C H E C K I N G . O P E R A T I O N S --
9 -- Copyright (C) 1999-2006, Free Software Foundation, Inc. --
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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 VxWorks version of this package.
35 -- This file should be kept synchronized with the general implementation
36 -- provided by s-stchop.adb.
38 pragma Restrictions (No_Elaboration_Code);
39 -- We want to guarantee the absence of elaboration code because the
40 -- binder does not handle references to this package.
44 with System.Storage_Elements; use System.Storage_Elements;
45 with System.Parameters; use System.Parameters;
46 with System.Soft_Links;
48 with System.OS_Interface;
50 package body System.Stack_Checking.Operations is
52 -- In order to have stack checking working appropriately on VxWorks we need
53 -- to extract the stack size information from the VxWorks kernel itself. It
54 -- means that the library for showing task-related information needs to be
55 -- linked into the VxWorks system, when using stack checking. The TaskShow
56 -- library can be linked into the VxWorks system by either:
58 -- * defining INCLUDE_SHOW_ROUTINES in config.h when using
59 -- configuration header files, or
61 -- * selecting INCLUDE_TASK_SHOW when using the Tornado project
64 function Set_Stack_Info
65 (Stack : not null access Stack_Access) return Stack_Access;
67 -- The function Set_Stack_Info is the actual function that updates the
68 -- cache containing a pointer to the Stack_Info. It may also be used for
69 -- detecting asynchronous abort in combination with Invalidate_Self_Cache.
71 -- Set_Stack_Info should do the following things in order:
72 -- 1) Get the Stack_Access value for the current task
73 -- 2) Set Stack.all to the value obtained in 1)
74 -- 3) Optionally Poll to check for asynchronous abort
76 -- This order is important because if at any time a write to the stack
77 -- cache is pending, that write should be followed by a Poll to prevent
80 -- Note: This function must be compiled with Polling turned off
82 -- Note: on systems like VxWorks and OS/2 with real thread-local storage,
83 -- Set_Stack_Info should return an access value for such local
84 -- storage. In those cases the cache will always be up-to-date.
85 -- Fix examples??? Linux???
87 -- The following constants should be imported from some system-specific
88 -- constants package. The constants must be static for performance reasons.
90 ----------------------------
91 -- Invalidate_Stack_Cache --
92 ----------------------------
94 procedure Invalidate_Stack_Cache (Any_Stack : Stack_Access) is
95 pragma Warnings (Off, Any_Stack);
98 end Invalidate_Stack_Cache;
104 function Set_Stack_Info
105 (Stack : not null access Stack_Access) return Stack_Access
107 -- Task descriptor that is handled internally by the VxWorks kernel
109 type Td_Events_Storage is array (1 .. 4) of Interfaces.C.int;
110 pragma Convention (C, Td_Events_Storage);
112 type Task_Descriptor is record
113 T_Id : Interfaces.C.int; -- task identifier
114 Td_Name : System.Address; -- task name
115 Td_Priority : Interfaces.C.int; -- task priority
116 Td_Status : Interfaces.C.int; -- task status
117 Td_Options : Interfaces.C.int; -- task option bits (see below)
118 Td_Entry : System.Address; -- original entry point of task
119 Td_Sp : System.Address; -- saved stack pointer
120 Td_PStackBase : System.Address; -- the bottom of the stack
121 Td_PStackLimit : System.Address; -- the effective end of the stack
122 Td_PStackEnd : System.Address; -- the actual end of the stack
123 Td_StackSize : Interfaces.C.int; -- size of stack in bytes
124 Td_StackCurrent : Interfaces.C.int; -- current stack usage in bytes
125 Td_StackHigh : Interfaces.C.int; -- maximum stack usage in bytes
126 Td_StackMargin : Interfaces.C.int; -- current stack margin in bytes
127 Td_ErrorStatus : Interfaces.C.int; -- most recent task error status
128 Td_Delay : Interfaces.C.int; -- delay/timeout ticks
129 Td_Events : Td_Events_Storage; -- task events, post t2.0
131 pragma Convention (C, Task_Descriptor);
133 -- This VxWorks procedure fills in a specified task descriptor
134 -- for a specified task.
135 procedure TaskInfoGet
136 (T_Id : System.OS_Interface.t_id;
137 Task_Desc : not null access Task_Descriptor);
138 pragma Import (C, TaskInfoGet, "taskInfoGet");
140 My_Stack : Stack_Access;
141 Task_Desc : aliased Task_Descriptor;
144 -- The order of steps 1 .. 3 is important, see specification.
146 -- 1) Get the Stack_Access value for the current task
148 My_Stack := Soft_Links.Get_Stack_Info.all;
150 if My_Stack.Base = Null_Address then
152 -- First invocation. Ask the VxWorks kernel about stack values
154 TaskInfoGet (System.OS_Interface.taskIdSelf, Task_Desc'Access);
157 System.Storage_Elements.Storage_Offset (Task_Desc.Td_StackSize);
158 My_Stack.Base := Task_Desc.Td_PStackBase;
159 My_Stack.Limit := Task_Desc.Td_PStackLimit;
163 -- 2) Set Stack.all to the value obtained in 1)
165 Stack.all := My_Stack;
167 -- 3) Optionally Poll to check for asynchronous abort
169 if Soft_Links.Check_Abort_Status.all /= 0 then
170 raise Standard'Abort_Signal;
173 -- Never trust the cached value, return local copy!
183 (Stack_Address : System.Address) return Stack_Access
185 type Frame_Marker is null record;
187 Marker : Frame_Marker;
188 Cached_Stack : constant Stack_Access := Cache;
189 Frame_Address : constant System.Address := Marker'Address;
192 -- The parameter may have wrapped around in System.Address arithmetics.
193 -- In that case, we have no other choices than raising the exception.
195 if (Stack_Grows_Down and then Stack_Address > Frame_Address)
196 or else (not Stack_Grows_Down and then Stack_Address < Frame_Address)
198 Ada.Exceptions.Raise_Exception
199 (E => Storage_Error'Identity,
200 Message => "stack overflow detected");
203 -- This function first does a "cheap" check which is correct if it
204 -- succeeds. In case of failure, the full check is done. Ideally the
205 -- cheap check should be done in an optimized manner, or be inlined.
208 and then Frame_Address <= Cached_Stack.Base
209 and then Stack_Address > Cached_Stack.Limit)
210 or else (not Stack_Grows_Down
211 and then Frame_Address >= Cached_Stack.Base
212 and then Stack_Address < Cached_Stack.Limit)
214 -- Cached_Stack is valid as it passed the stack check
221 My_Stack : constant Stack_Access := Set_Stack_Info (Cache'Access);
222 -- At this point Stack.all might already be invalid, so it is
223 -- essential to use our local copy of Stack!
227 and then Stack_Address < My_Stack.Limit)
228 or else (not Stack_Grows_Down
229 and then Stack_Address > My_Stack.Limit)
231 Ada.Exceptions.Raise_Exception
232 (E => Storage_Error'Identity,
233 Message => "stack overflow detected");
240 ------------------------
241 -- Update_Stack_Cache --
242 ------------------------
244 procedure Update_Stack_Cache (Stack : Stack_Access) is
246 if not Multi_Processor then
249 end Update_Stack_Cache;
251 end System.Stack_Checking.Operations;