Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jsnativestack.cpp
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 et sw=4 tw=80:
3  */
4 /* ***** BEGIN LICENSE BLOCK *****
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is Mozilla code.
18  *
19  * The Initial Developer of the Original Code is the Mozilla Corporation.
20  * Portions created by the Initial Developer are Copyright (C) 2009
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either of the GNU General Public License Version 2 or later (the "GPL"),
27  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39 #include <stdlib.h>
40 #include "jstypes.h"
41 #include "jsnativestack.h"
42
43 #ifdef XP_WIN
44 # include "jswin.h"
45
46 #elif defined(XP_OS2)
47 # define INCL_DOSPROCESS
48 # include <os2.h>
49
50 #elif defined(XP_MACOSX) || defined(DARWIN) || defined(XP_UNIX)
51 # include <pthread.h>
52
53 # if defined(__FreeBSD__)
54 #  include <pthread_np.h>
55 # endif
56
57 #else
58 # error "Unsupported platform"
59
60 #endif
61
62 namespace js {
63
64 #if defined(XP_WIN) && defined(WINCE)
65
66 inline bool
67 isPageWritable(void *page)
68 {
69     MEMORY_BASIC_INFORMATION memoryInformation;
70     jsuword result = VirtualQuery(page, &memoryInformation, sizeof(memoryInformation));
71
72     /* return false on error, including ptr outside memory */
73     if (result != sizeof(memoryInformation))
74         return false;
75
76     jsuword protect = memoryInformation.Protect & ~(PAGE_GUARD | PAGE_NOCACHE);
77     return protect == PAGE_READWRITE ||
78            protect == PAGE_WRITECOPY ||
79            protect == PAGE_EXECUTE_READWRITE ||
80            protect == PAGE_EXECUTE_WRITECOPY;
81 }
82
83 void *
84 GetNativeStackBase()
85 {
86     /* find the address of this stack frame by taking the address of a local variable */
87     bool isGrowingDownward = JS_STACK_GROWTH_DIRECTION < 0;
88     void *thisFrame = (void *)(&isGrowingDownward);
89
90     static jsuword pageSize = 0;
91     if (!pageSize) {
92         SYSTEM_INFO systemInfo;
93         GetSystemInfo(&systemInfo);
94         pageSize = systemInfo.dwPageSize;
95     }
96
97     /* scan all of memory starting from this frame, and return the last writeable page found */
98     register char *currentPage = (char *)((jsuword)thisFrame & ~(pageSize - 1));
99     if (isGrowingDownward) {
100         while (currentPage > 0) {
101             /* check for underflow */
102             if (currentPage >= (char *)pageSize)
103                 currentPage -= pageSize;
104             else
105                 currentPage = 0;
106             if (!isPageWritable(currentPage))
107                 return currentPage + pageSize;
108         }
109         return 0;
110     } else {
111         while (true) {
112             /* guaranteed to complete because isPageWritable returns false at end of memory */
113             currentPage += pageSize;
114             if (!isPageWritable(currentPage))
115                 return currentPage;
116         }
117     }
118 }
119
120 #elif defined(XP_WIN)
121
122 void *
123 GetNativeStackBaseImpl()
124 {
125 # if defined(_M_IX86) && defined(_MSC_VER)
126     /*
127      * offset 0x18 from the FS segment register gives a pointer to
128      * the thread information block for the current thread
129      */
130     NT_TIB* pTib;
131     __asm {
132         MOV EAX, FS:[18h]
133         MOV pTib, EAX
134     }
135     return static_cast<void*>(pTib->StackBase);
136
137 # elif defined(_M_X64)
138     PNT_TIB64 pTib = reinterpret_cast<PNT_TIB64>(NtCurrentTeb());
139     return reinterpret_cast<void*>(pTib->StackBase);
140
141 # elif defined(_WIN32) && defined(__GNUC__)
142     NT_TIB* pTib;
143     asm ("movl %%fs:0x18, %0\n" : "=r" (pTib));
144     return static_cast<void*>(pTib->StackBase);
145
146 # endif
147 }
148
149 #elif defined(SOLARIS)
150
151 #include <ucontext.h>
152
153 JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0);
154
155 void *
156 GetNativeStackBaseImpl()
157 {
158     stack_t st;
159     stack_getbounds(&st);
160     return static_cast<char*>(st.ss_sp) + st.ss_size;
161 }
162
163 #elif defined(XP_OS2)
164
165 void *
166 GetNativeStackBaseImpl()
167 {
168     PTIB  ptib;
169     PPIB  ppib;
170
171     DosGetInfoBlocks(&ptib, &ppib);
172     return ptib->tib_pstacklimit;
173 }
174
175 #elif defined(SOLARIS)
176
177 #include <ucontext.h>
178
179 void *
180 GetNativeStackBaseImpl()
181 {
182     stack_t st;
183     stack_getbounds(&st);
184     return static_cast<char*>(st.ss_sp) + st.ss_size;
185 }
186
187 #else /* XP_UNIX */
188
189 void *
190 GetNativeStackBaseImpl()
191 {
192     pthread_t thread = pthread_self();
193 # if defined(XP_MACOSX) || defined(DARWIN)
194     return pthread_get_stackaddr_np(thread);
195
196 # else
197     pthread_attr_t sattr;
198     pthread_attr_init(&sattr);
199 #  if defined(PTHREAD_NP_H) || defined(_PTHREAD_NP_H_) || defined(NETBSD)
200     /* e.g. on FreeBSD 4.8 or newer, neundorf@kde.org */
201     pthread_attr_get_np(thread, &sattr);
202 #  else
203     /*
204      * FIXME: this function is non-portable;
205      * other POSIX systems may have different np alternatives
206      */
207     pthread_getattr_np(thread, &sattr);
208 #  endif
209
210     void *stackBase = 0;
211     size_t stackSize = 0;
212 #  ifdef DEBUG
213     int rc = 
214 #  endif
215         pthread_attr_getstack(&sattr, &stackBase, &stackSize);
216     JS_ASSERT(!rc);
217     JS_ASSERT(stackBase);
218     pthread_attr_destroy(&sattr);
219
220 #  if JS_STACK_GROWTH_DIRECTION > 0
221     return stackBase;
222 #  else
223     return static_cast<char*>(stackBase) + stackSize;
224 #  endif
225 # endif
226 }
227
228 #endif /* !XP_WIN */
229
230 } /* namespace js */