OSDN Git Service

PR preprocessor/30805:
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-stchop-vxworks.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                 GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                 --
4 --                                                                          --
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      --
6 --                                                                          --
7 --                                  B o d y                                 --
8 --                                                                          --
9 --          Copyright (C) 1999-2007, 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 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.                                              --
21 --                                                                          --
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.                                      --
28 --                                                                          --
29 -- GNARL was developed by the GNARL team at Florida State University.       --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc.     --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
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.
37
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.
41
42 with Ada.Exceptions;
43
44 with System.Storage_Elements; use System.Storage_Elements;
45 with System.Parameters; use System.Parameters;
46 with System.Soft_Links;
47 with Interfaces.C;
48
49 package body System.Stack_Checking.Operations is
50
51    --  In order to have stack checking working appropriately on VxWorks we need
52    --  to extract the stack size information from the VxWorks kernel itself. It
53    --  means that the library for showing task-related information needs to be
54    --  linked into the VxWorks system, when using stack checking. The TaskShow
55    --  library can be linked into the VxWorks system by either:
56
57    --    * defining INCLUDE_SHOW_ROUTINES in config.h when using
58    --      configuration header files, or
59
60    --    * selecting INCLUDE_TASK_SHOW when using the Tornado project
61    --      facility.
62
63    function Set_Stack_Info
64      (Stack : not null access Stack_Access) return Stack_Access;
65
66    --  The function Set_Stack_Info is the actual function that updates the
67    --  cache containing a pointer to the Stack_Info. It may also be used for
68    --  detecting asynchronous abort in combination with Invalidate_Self_Cache.
69
70    --  Set_Stack_Info should do the following things in order:
71    --     1) Get the Stack_Access value for the current task
72    --     2) Set Stack.all to the value obtained in 1)
73    --     3) Optionally Poll to check for asynchronous abort
74
75    --  This order is important because if at any time a write to the stack
76    --  cache is pending, that write should be followed by a Poll to prevent
77    --  loosing signals.
78
79    --  Note: This function must be compiled with Polling turned off
80
81    --  Note: on systems like VxWorks and Linux with real thread-local storage,
82    --        Set_Stack_Info should return an access value for such local
83    --        storage. In those cases the cache will always be up-to-date.
84
85    --  The following constants should be imported from some system-specific
86    --  constants package. The constants must be static for performance reasons.
87
88    ----------------------------
89    -- Invalidate_Stack_Cache --
90    ----------------------------
91
92    procedure Invalidate_Stack_Cache (Any_Stack : Stack_Access) is
93       pragma Warnings (Off, Any_Stack);
94    begin
95       Cache := Null_Stack;
96    end Invalidate_Stack_Cache;
97
98    -----------------------------
99    -- Notify_Stack_Attributes --
100    -----------------------------
101
102    procedure Notify_Stack_Attributes
103      (Initial_SP : System.Address;
104       Size       : System.Storage_Elements.Storage_Offset)
105    is
106       --  We retrieve the attributes directly from Set_Stack_Info below, so
107       --  this implementation has nothing to do.
108
109       pragma Unreferenced (Initial_SP);
110       pragma Unreferenced (Size);
111
112    begin
113       null;
114    end Notify_Stack_Attributes;
115
116    --------------------
117    -- Set_Stack_Info --
118    --------------------
119
120    function Set_Stack_Info
121      (Stack : not null access Stack_Access) return Stack_Access
122    is
123       type OS_Stack_Info is record
124          Size  : Interfaces.C.int;
125          Base  : System.Address;
126          Limit : System.Address;
127       end record;
128       pragma Convention (C, OS_Stack_Info);
129       --  Type representing the information that we want to extract from the
130       --  underlying kernel.
131
132       procedure Get_Stack_Info (Stack : not null access OS_Stack_Info);
133       pragma Import (C, Get_Stack_Info, "__gnat_get_stack_info");
134       --  Procedure that fills the stack information associated to the
135       --  currently executing task.
136
137       My_Stack  : Stack_Access;
138       Task_Info : aliased OS_Stack_Info;
139
140    begin
141       --  The order of steps 1 .. 3 is important, see specification
142
143       --  1) Get the Stack_Access value for the current task
144
145       My_Stack := Soft_Links.Get_Stack_Info.all;
146
147       if My_Stack.Base = Null_Address then
148
149          --  First invocation. Ask the VxWorks kernel about stack values
150
151          Get_Stack_Info (Task_Info'Access);
152
153          My_Stack.Size  := Storage_Elements.Storage_Offset (Task_Info.Size);
154          My_Stack.Base  := Task_Info.Base;
155          My_Stack.Limit := Task_Info.Limit;
156
157       end if;
158
159       --  2) Set Stack.all to the value obtained in 1)
160
161       Stack.all := My_Stack;
162
163       --  3) Optionally Poll to check for asynchronous abort
164
165       if Soft_Links.Check_Abort_Status.all /= 0 then
166          raise Standard'Abort_Signal;
167       end if;
168
169       --  Never trust the cached value, return local copy!
170
171       return My_Stack;
172    end Set_Stack_Info;
173
174    -----------------
175    -- Stack_Check --
176    -----------------
177
178    function Stack_Check
179      (Stack_Address : System.Address) return Stack_Access
180    is
181       type Frame_Marker is null record;
182
183       Marker        : Frame_Marker;
184       Cached_Stack  : constant Stack_Access := Cache;
185       Frame_Address : constant System.Address := Marker'Address;
186
187    begin
188       --  The parameter may have wrapped around in System.Address arithmetics.
189       --  In that case, we have no other choices than raising the exception.
190
191       if (Stack_Grows_Down and then Stack_Address > Frame_Address)
192         or else (not Stack_Grows_Down and then Stack_Address < Frame_Address)
193       then
194          Ada.Exceptions.Raise_Exception
195            (E       => Storage_Error'Identity,
196             Message => "stack overflow detected");
197       end if;
198
199       --  This function first does a "cheap" check which is correct if it
200       --  succeeds. In case of failure, the full check is done. Ideally the
201       --  cheap check should be done in an optimized manner, or be inlined.
202
203       if (Stack_Grows_Down
204           and then Frame_Address <= Cached_Stack.Base
205           and then Stack_Address > Cached_Stack.Limit)
206         or else (not Stack_Grows_Down
207                    and then Frame_Address >= Cached_Stack.Base
208                    and then Stack_Address < Cached_Stack.Limit)
209       then
210          --  Cached_Stack is valid as it passed the stack check
211
212          return Cached_Stack;
213       end if;
214
215       Full_Check :
216       declare
217          My_Stack : constant Stack_Access := Set_Stack_Info (Cache'Access);
218          --  At this point Stack.all might already be invalid, so it is
219          --  essential to use our local copy of Stack!
220
221       begin
222          if (Stack_Grows_Down
223                and then Stack_Address < My_Stack.Limit)
224            or else (not Stack_Grows_Down
225                       and then Stack_Address > My_Stack.Limit)
226          then
227             Ada.Exceptions.Raise_Exception
228               (E       => Storage_Error'Identity,
229                Message => "stack overflow detected");
230          end if;
231
232          return My_Stack;
233       end Full_Check;
234    end Stack_Check;
235
236    ------------------------
237    -- Update_Stack_Cache --
238    ------------------------
239
240    procedure Update_Stack_Cache (Stack : Stack_Access) is
241    begin
242       if not Multi_Processor then
243          Cache := Stack;
244       end if;
245    end Update_Stack_Cache;
246
247 end System.Stack_Checking.Operations;