Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / nanojit / avmplus.cpp
1 /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
3 /* ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1 (the
7  * "License"); you may not use this file except in compliance with the License. You may obtain
8  * a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
11  * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
12  * language governing rights and limitations under the License.
13  *
14  * The Original Code is [Open Source Virtual Machine.]
15  *
16  * The Initial Developer of the Original Code is Adobe System Incorporated.  Portions created
17  * by the Initial Developer are Copyright (C)[ 2004-2006 ] Adobe Systems Incorporated. All Rights
18  * Reserved.
19  *
20  * Contributor(s): Adobe AS3 Team
21  *                 Andreas Gal <gal@mozilla.com>
22  *
23  * Alternatively, the contents of this file may be used under the terms of either the GNU
24  * General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public
25  * License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the
26  * LGPL are applicable instead of those above. If you wish to allow use of your version of this
27  * file only under the terms of either the GPL or the LGPL, and not to allow others to use your
28  * version of this file under the terms of the MPL, indicate your decision by deleting provisions
29  * above and replace them with the notice and other provisions required by the GPL or the
30  * LGPL. If you do not delete the provisions above, a recipient may use your version of this file
31  * under the terms of any one of the MPL, the GPL or the LGPL.
32  *
33  ***** END LICENSE BLOCK ***** */
34
35 #include <signal.h>
36 #include "nanojit.h"
37
38 #ifdef SOLARIS
39     typedef caddr_t maddr_ptr;
40 #else
41     typedef void *maddr_ptr;
42 #endif
43
44 using namespace avmplus;
45
46 nanojit::Config AvmCore::config;
47
48 void
49 avmplus::AvmLog(char const *msg, ...) {
50     va_list ap;
51     va_start(ap, msg);
52     VMPI_vfprintf(stderr, msg, ap);
53     va_end(ap);
54 }
55
56 #ifdef _DEBUG
57 namespace avmplus {
58     void AvmAssertFail(const char* /* msg */) {
59         fflush(stderr);
60 #if defined(WIN32)
61         DebugBreak();
62         exit(3);
63 #elif defined(__APPLE__)
64         /*
65          * On Mac OS X, Breakpad ignores signals. Only real Mach exceptions are
66          * trapped.
67          */
68         *((int *) NULL) = 0;  /* To continue from here in GDB: "return" then "continue". */
69         raise(SIGABRT);  /* In case above statement gets nixed by the optimizer. */
70 #else
71         raise(SIGABRT);  /* To continue from here in GDB: "signal 0". */
72 #endif
73     }
74 }
75 #endif
76
77 #ifdef WINCE
78
79 // Due to the per-process heap slots on Windows Mobile, we can often run in to OOM
80 // situations.  jemalloc has worked around this problem, and so we use it here.
81 // Using posix_memalign (or other malloc)functions) here only works because the OS
82 // and hardware doesn't check for the execute bit being set.
83
84 #ifndef MOZ_MEMORY
85 #error MOZ_MEMORY required for building on WINCE
86 #endif
87
88 void*
89 nanojit::CodeAlloc::allocCodeChunk(size_t nbytes) {
90     void * buffer;
91     posix_memalign(&buffer, 4096, nbytes);
92     VMPI_setPageProtection(buffer, nbytes, true /* exec */, true /* write */);
93     return buffer;
94 }
95
96 void
97 nanojit::CodeAlloc::freeCodeChunk(void *p, size_t nbytes) {
98     VMPI_setPageProtection(p, nbytes, false /* exec */, true /* write */);
99     ::free(p);
100 }
101
102 #elif defined(WIN32)
103
104 void*
105 nanojit::CodeAlloc::allocCodeChunk(size_t nbytes) {
106     return VirtualAlloc(NULL,
107                         nbytes,
108                         MEM_COMMIT | MEM_RESERVE,
109                         PAGE_EXECUTE_READWRITE);
110 }
111
112 void
113 nanojit::CodeAlloc::freeCodeChunk(void *p, size_t) {
114     VirtualFree(p, 0, MEM_RELEASE);
115 }
116
117 #elif defined(AVMPLUS_OS2)
118
119 void*
120 nanojit::CodeAlloc::allocCodeChunk(size_t nbytes) {
121
122     // alloc from high memory, fallback to low memory if that fails
123     void * addr;
124     if (DosAllocMem(&addr, nbytes, OBJ_ANY |
125                     PAG_COMMIT | PAG_READ | PAG_WRITE | PAG_EXECUTE)) {
126         if (DosAllocMem(&addr, nbytes,
127                         PAG_COMMIT | PAG_READ | PAG_WRITE | PAG_EXECUTE)) {
128             return 0;
129         }
130     }
131     return addr;
132 }
133
134 void
135 nanojit::CodeAlloc::freeCodeChunk(void *p, size_t nbytes) {
136     DosFreeMem(p);
137 }
138
139 #elif defined(AVMPLUS_UNIX)
140
141 void*
142 nanojit::CodeAlloc::allocCodeChunk(size_t nbytes) {
143     return mmap(NULL,
144                 nbytes,
145                 PROT_READ | PROT_WRITE | PROT_EXEC,
146                 MAP_PRIVATE | MAP_ANON,
147                 -1,
148                 0);
149 }
150
151 void
152 nanojit::CodeAlloc::freeCodeChunk(void *p, size_t nbytes) {
153     munmap((maddr_ptr)p, nbytes);
154 }
155
156 #else // !WIN32 && !AVMPLUS_OS2 && !AVMPLUS_UNIX
157
158 void*
159 nanojit::CodeAlloc::allocCodeChunk(size_t nbytes) {
160     void* mem = valloc(nbytes);
161     VMPI_setPageProtection(mem, nbytes, true /* exec */, true /* write */);
162     return mem;
163 }
164
165 void
166 nanojit::CodeAlloc::freeCodeChunk(void *p, size_t nbytes) {
167     VMPI_setPageProtection(p, nbytes, false /* exec */, true /* write */);
168     ::free(p);
169 }
170
171 #endif // WIN32
172
173 // All of the allocCodeChunk/freeCodeChunk implementations above allocate
174 // code memory as RWX and then free it, so the explicit page protection api's
175 // below are no-ops.
176
177 void
178 nanojit::CodeAlloc::markCodeChunkWrite(void*, size_t)
179 {}
180
181 void
182 nanojit::CodeAlloc::markCodeChunkExec(void*, size_t)
183 {}
184