OSDN Git Service

* config/pa/fptr.c: Update license header.
[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-2006, 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    -- Set_Stack_Info --
100    --------------------
101
102    function Set_Stack_Info
103      (Stack : not null access Stack_Access) return Stack_Access
104    is
105       type OS_Stack_Info is record
106          Size  : Interfaces.C.int;
107          Base  : System.Address;
108          Limit : System.Address;
109       end record;
110       pragma Convention (C, OS_Stack_Info);
111       --  Type representing the information that we want to extract from the
112       --  underlying kernel.
113
114       procedure Get_Stack_Info (Stack : not null access OS_Stack_Info);
115       pragma Import (C, Get_Stack_Info, "__gnat_get_stack_info");
116       --  Procedure that fills the stack information associated to the
117       --  currently executing task.
118
119       My_Stack  : Stack_Access;
120       Task_Info : aliased OS_Stack_Info;
121
122    begin
123       --  The order of steps 1 .. 3 is important, see specification.
124
125       --  1) Get the Stack_Access value for the current task
126
127       My_Stack := Soft_Links.Get_Stack_Info.all;
128
129       if My_Stack.Base = Null_Address then
130
131          --  First invocation. Ask the VxWorks kernel about stack values
132
133          Get_Stack_Info (Task_Info'Access);
134
135          My_Stack.Size  := Storage_Elements.Storage_Offset (Task_Info.Size);
136          My_Stack.Base  := Task_Info.Base;
137          My_Stack.Limit := Task_Info.Limit;
138
139       end if;
140
141       --  2) Set Stack.all to the value obtained in 1)
142
143       Stack.all := My_Stack;
144
145       --  3) Optionally Poll to check for asynchronous abort
146
147       if Soft_Links.Check_Abort_Status.all /= 0 then
148          raise Standard'Abort_Signal;
149       end if;
150
151       --  Never trust the cached value, return local copy!
152
153       return My_Stack;
154    end Set_Stack_Info;
155
156    -----------------
157    -- Stack_Check --
158    -----------------
159
160    function Stack_Check
161      (Stack_Address : System.Address) return Stack_Access
162    is
163       type Frame_Marker is null record;
164
165       Marker        : Frame_Marker;
166       Cached_Stack  : constant Stack_Access := Cache;
167       Frame_Address : constant System.Address := Marker'Address;
168
169    begin
170       --  The parameter may have wrapped around in System.Address arithmetics.
171       --  In that case, we have no other choices than raising the exception.
172
173       if (Stack_Grows_Down and then Stack_Address > Frame_Address)
174         or else (not Stack_Grows_Down and then Stack_Address < Frame_Address)
175       then
176          Ada.Exceptions.Raise_Exception
177            (E       => Storage_Error'Identity,
178             Message => "stack overflow detected");
179       end if;
180
181       --  This function first does a "cheap" check which is correct if it
182       --  succeeds. In case of failure, the full check is done. Ideally the
183       --  cheap check should be done in an optimized manner, or be inlined.
184
185       if (Stack_Grows_Down
186           and then Frame_Address <= Cached_Stack.Base
187           and then Stack_Address > Cached_Stack.Limit)
188         or else (not Stack_Grows_Down
189                    and then Frame_Address >= Cached_Stack.Base
190                    and then Stack_Address < Cached_Stack.Limit)
191       then
192          --  Cached_Stack is valid as it passed the stack check
193
194          return Cached_Stack;
195       end if;
196
197       Full_Check :
198       declare
199          My_Stack : constant Stack_Access := Set_Stack_Info (Cache'Access);
200          --  At this point Stack.all might already be invalid, so it is
201          --  essential to use our local copy of Stack!
202
203       begin
204          if (Stack_Grows_Down
205                and then Stack_Address < My_Stack.Limit)
206            or else (not Stack_Grows_Down
207                       and then Stack_Address > My_Stack.Limit)
208          then
209             Ada.Exceptions.Raise_Exception
210               (E       => Storage_Error'Identity,
211                Message => "stack overflow detected");
212          end if;
213
214          return My_Stack;
215       end Full_Check;
216    end Stack_Check;
217
218    ------------------------
219    -- Update_Stack_Cache --
220    ------------------------
221
222    procedure Update_Stack_Cache (Stack : Stack_Access) is
223    begin
224       if not Multi_Processor then
225          Cache := Stack;
226       end if;
227    end Update_Stack_Cache;
228
229 end System.Stack_Checking.Operations;