Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / js / src / jsnativestack.cpp
blob0366d25eeba8c5f6fca6c797ebbd39e525468e14
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
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/
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.
17 * The Original Code is Mozilla code.
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.
23 * Contributor(s):
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.
37 * ***** END LICENSE BLOCK ***** */
39 #include <stdlib.h>
40 #include "jstypes.h"
41 #include "jsnativestack.h"
43 #ifdef XP_WIN
44 # include "jswin.h"
46 #elif defined(XP_OS2)
47 # define INCL_DOSPROCESS
48 # include <os2.h>
50 #elif defined(XP_MACOSX) || defined(DARWIN) || defined(XP_UNIX)
51 # include <pthread.h>
53 # if defined(__FreeBSD__)
54 # include <pthread_np.h>
55 # endif
57 #else
58 # error "Unsupported platform"
60 #endif
62 namespace js {
64 #if defined(XP_WIN) && defined(WINCE)
66 inline bool
67 isPageWritable(void *page)
69 MEMORY_BASIC_INFORMATION memoryInformation;
70 jsuword result = VirtualQuery(page, &memoryInformation, sizeof(memoryInformation));
72 /* return false on error, including ptr outside memory */
73 if (result != sizeof(memoryInformation))
74 return false;
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;
83 void *
84 GetNativeStackBase()
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);
90 static jsuword pageSize = 0;
91 if (!pageSize) {
92 SYSTEM_INFO systemInfo;
93 GetSystemInfo(&systemInfo);
94 pageSize = systemInfo.dwPageSize;
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;
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;
120 #elif defined(XP_WIN)
122 void *
123 GetNativeStackBaseImpl()
125 # if defined(_M_IX86) && defined(_MSC_VER)
127 * offset 0x18 from the FS segment register gives a pointer to
128 * the thread information block for the current thread
130 NT_TIB* pTib;
131 __asm {
132 MOV EAX, FS:[18h]
133 MOV pTib, EAX
135 return static_cast<void*>(pTib->StackBase);
137 # elif defined(_M_X64)
138 PNT_TIB64 pTib = reinterpret_cast<PNT_TIB64>(NtCurrentTeb());
139 return reinterpret_cast<void*>(pTib->StackBase);
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);
146 # endif
149 #elif defined(SOLARIS)
151 #include <ucontext.h>
153 JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0);
155 void *
156 GetNativeStackBaseImpl()
158 stack_t st;
159 stack_getbounds(&st);
160 return static_cast<char*>(st.ss_sp) + st.ss_size;
163 #elif defined(XP_OS2)
165 void *
166 GetNativeStackBaseImpl()
168 PTIB ptib;
169 PPIB ppib;
171 DosGetInfoBlocks(&ptib, &ppib);
172 return ptib->tib_pstacklimit;
175 #elif defined(SOLARIS)
177 #include <ucontext.h>
179 void *
180 GetNativeStackBaseImpl()
182 stack_t st;
183 stack_getbounds(&st);
184 return static_cast<char*>(st.ss_sp) + st.ss_size;
187 #else /* XP_UNIX */
189 void *
190 GetNativeStackBaseImpl()
192 pthread_t thread = pthread_self();
193 # if defined(XP_MACOSX) || defined(DARWIN)
194 return pthread_get_stackaddr_np(thread);
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
204 * FIXME: this function is non-portable;
205 * other POSIX systems may have different np alternatives
207 pthread_getattr_np(thread, &sattr);
208 # endif
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);
220 # if JS_STACK_GROWTH_DIRECTION > 0
221 return stackBase;
222 # else
223 return static_cast<char*>(stackBase) + stackSize;
224 # endif
225 # endif
228 #endif /* !XP_WIN */
230 } /* namespace js */