1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 et sw=4 tw=80:
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
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.
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 ***** */
41 #include "jsnativestack.h"
47 # define INCL_DOSPROCESS
50 #elif defined(XP_MACOSX) || defined(DARWIN) || defined(XP_UNIX)
53 # if defined(__FreeBSD__)
54 # include <pthread_np.h>
58 # error "Unsupported platform"
64 #if defined(XP_WIN) && defined(WINCE)
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))
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;
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;
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;
106 if (!isPageWritable(currentPage))
107 return currentPage + pageSize;
112 /* guaranteed to complete because isPageWritable returns false at end of memory */
113 currentPage += pageSize;
114 if (!isPageWritable(currentPage))
120 #elif defined(XP_WIN)
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
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__)
143 asm ("movl %%fs:0x18, %0\n" : "=r" (pTib));
144 return static_cast<void*>(pTib->StackBase);
149 #elif defined(SOLARIS)
151 #include <ucontext.h>
153 JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0);
156 GetNativeStackBaseImpl()
159 stack_getbounds(&st);
160 return static_cast<char*>(st.ss_sp) + st.ss_size;
163 #elif defined(XP_OS2)
166 GetNativeStackBaseImpl()
171 DosGetInfoBlocks(&ptib, &ppib);
172 return ptib->tib_pstacklimit;
175 #elif defined(SOLARIS)
177 #include <ucontext.h>
180 GetNativeStackBaseImpl()
183 stack_getbounds(&st);
184 return static_cast<char*>(st.ss_sp) + st.ss_size;
190 GetNativeStackBaseImpl()
192 pthread_t thread = pthread_self();
193 # if defined(XP_MACOSX) || defined(DARWIN)
194 return pthread_get_stackaddr_np(thread);
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);
204 * FIXME: this function is non-portable;
205 * other POSIX systems may have different np alternatives
207 pthread_getattr_np(thread, &sattr);
211 size_t stackSize = 0;
215 pthread_attr_getstack(&sattr, &stackBase, &stackSize);
217 JS_ASSERT(stackBase);
218 pthread_attr_destroy(&sattr);
220 # if JS_STACK_GROWTH_DIRECTION > 0
223 return static_cast<char*>(stackBase) + stackSize;