blob: b8b96025e1fae2307006f10392b1cca70ef257d7 [file] [log] [blame]
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000028// Platform specific code for Linux goes here. For the POSIX comaptible parts
29// the implementation is in platform-posix.cc.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000030
31#include <pthread.h>
32#include <semaphore.h>
33#include <signal.h>
lrn@chromium.org5d00b602011-01-05 09:51:43 +000034#include <sys/prctl.h>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035#include <sys/time.h>
36#include <sys/resource.h>
lrn@chromium.org303ada72010-10-27 09:33:13 +000037#include <sys/syscall.h>
ager@chromium.org381abbb2009-02-25 13:23:22 +000038#include <sys/types.h>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039#include <stdlib.h>
40
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +000041#if defined(__GLIBC__) && !defined(__UCLIBC__)
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000042#include <execinfo.h>
43#include <cxxabi.h>
44#endif
45
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046// Ubuntu Dapper requires memory pages to be marked as
47// executable. Otherwise, OS raises an exception when executing code
48// in that page.
49#include <sys/types.h> // mmap & munmap
ager@chromium.org236ad962008-09-25 09:45:57 +000050#include <sys/mman.h> // mmap & munmap
51#include <sys/stat.h> // open
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000052#include <fcntl.h> // open
53#include <unistd.h> // sysconf
ager@chromium.org236ad962008-09-25 09:45:57 +000054#include <strings.h> // index
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055#include <errno.h>
56#include <stdarg.h>
57
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +000058// GLibc on ARM defines mcontext_t has a typedef for 'struct sigcontext'.
59// Old versions of the C library <signal.h> didn't define the type.
60#if defined(__ANDROID__) && !defined(__BIONIC_HAVE_UCONTEXT_T) && \
61 defined(__arm__) && !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT)
62#include <asm/sigcontext.h>
63#endif
64
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065#undef MAP_TYPE
66
67#include "v8.h"
68
danno@chromium.org8c0a43f2012-04-03 08:37:53 +000069#include "platform-posix.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070#include "platform.h"
ager@chromium.orga1645e22009-09-09 19:27:10 +000071#include "v8threads.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000072#include "vm-state-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073
74
kasperl@chromium.org71affb52009-05-26 05:44:31 +000075namespace v8 {
76namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078
ager@chromium.orgc4c92722009-11-18 14:12:51 +000079#ifdef __arm__
danno@chromium.org169691d2013-07-15 08:01:13 +000080
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +000081bool OS::ArmUsingHardFloat() {
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +000082 // GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify
83 // the Floating Point ABI used (PCS stands for Procedure Call Standard).
84 // We use these as well as a couple of other defines to statically determine
85 // what FP ABI used.
86 // GCC versions 4.4 and below don't support hard-fp.
87 // GCC versions 4.5 may support hard-fp without defining __ARM_PCS or
88 // __ARM_PCS_VFP.
89
90#define GCC_VERSION (__GNUC__ * 10000 \
91 + __GNUC_MINOR__ * 100 \
92 + __GNUC_PATCHLEVEL__)
93#if GCC_VERSION >= 40600
94#if defined(__ARM_PCS_VFP)
95 return true;
96#else
97 return false;
98#endif
99
100#elif GCC_VERSION < 40500
101 return false;
102
103#else
104#if defined(__ARM_PCS_VFP)
105 return true;
danno@chromium.org59400602013-08-13 17:09:37 +0000106#elif defined(__ARM_PCS) || defined(__SOFTFP__) || defined(__SOFTFP) || \
107 !defined(__VFP_FP__)
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000108 return false;
109#else
110#error "Your version of GCC does not report the FP ABI compiled for." \
111 "Please report it on this issue" \
112 "http://code.google.com/p/v8/issues/detail?id=2140"
113
114#endif
115#endif
116#undef GCC_VERSION
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000117}
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000118
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000119#endif // def __arm__
120
121
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000122const char* OS::LocalTimezone(double time) {
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000123 if (std::isnan(time)) return "";
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000124 time_t tv = static_cast<time_t>(floor(time/msPerSecond));
125 struct tm* t = localtime(&tv);
126 if (NULL == t) return "";
127 return t->tm_zone;
128}
129
130
131double OS::LocalTimeOffset() {
132 time_t tv = time(NULL);
133 struct tm* t = localtime(&tv);
134 // tm_gmtoff includes any daylight savings offset, so subtract it.
135 return static_cast<double>(t->tm_gmtoff * msPerSecond -
136 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
137}
138
139
kasper.lund7276f142008-07-30 08:49:36 +0000140void* OS::Allocate(const size_t requested,
141 size_t* allocated,
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000142 bool is_executable) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000143 const size_t msize = RoundUp(requested, AllocateAlignment());
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000144 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000145 void* addr = OS::GetRandomMmapAddr();
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000146 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147 if (mbase == MAP_FAILED) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000148 LOG(i::Isolate::Current(),
149 StringEvent("OS::Allocate", "mmap failed"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150 return NULL;
151 }
152 *allocated = msize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153 return mbase;
154}
155
156
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000157void OS::DumpBacktrace() {
danno@chromium.org169691d2013-07-15 08:01:13 +0000158 // backtrace is a glibc extension.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000159#if defined(__GLIBC__) && !defined(__UCLIBC__)
danno@chromium.org169691d2013-07-15 08:01:13 +0000160 POSIXBacktraceHelper<backtrace, backtrace_symbols>::DumpBacktrace();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000161#endif
162}
163
164
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165class PosixMemoryMappedFile : public OS::MemoryMappedFile {
166 public:
167 PosixMemoryMappedFile(FILE* file, void* memory, int size)
168 : file_(file), memory_(memory), size_(size) { }
169 virtual ~PosixMemoryMappedFile();
170 virtual void* memory() { return memory_; }
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000171 virtual int size() { return size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172 private:
173 FILE* file_;
174 void* memory_;
175 int size_;
176};
177
178
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000179OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000180 FILE* file = fopen(name, "r+");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000181 if (file == NULL) return NULL;
182
183 fseek(file, 0, SEEK_END);
184 int size = ftell(file);
185
186 void* memory =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000187 mmap(OS::GetRandomMmapAddr(),
188 size,
189 PROT_READ | PROT_WRITE,
190 MAP_SHARED,
191 fileno(file),
192 0);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000193 return new PosixMemoryMappedFile(file, memory, size);
194}
195
196
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
198 void* initial) {
199 FILE* file = fopen(name, "w+");
200 if (file == NULL) return NULL;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000201 int result = fwrite(initial, size, 1, file);
202 if (result < 1) {
203 fclose(file);
204 return NULL;
205 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206 void* memory =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000207 mmap(OS::GetRandomMmapAddr(),
208 size,
209 PROT_READ | PROT_WRITE,
210 MAP_SHARED,
211 fileno(file),
212 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213 return new PosixMemoryMappedFile(file, memory, size);
214}
215
216
217PosixMemoryMappedFile::~PosixMemoryMappedFile() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000218 if (memory_) OS::Free(memory_, size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219 fclose(file_);
220}
221
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222
dslomov@chromium.orge97852d2013-09-12 09:02:59 +0000223void OS::LogSharedLibraryAddresses(Isolate* isolate) {
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000224 // This function assumes that the layout of the file is as follows:
225 // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
226 // If we encounter an unexpected situation we abort scanning further entries.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000227 FILE* fp = fopen("/proc/self/maps", "r");
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000228 if (fp == NULL) return;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000229
230 // Allocate enough room to be able to store a full file name.
231 const int kLibNameLen = FILENAME_MAX + 1;
232 char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
233
234 // This loop will terminate once the scanning hits an EOF.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235 while (true) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000236 uintptr_t start, end;
237 char attr_r, attr_w, attr_x, attr_p;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000238 // Parse the addresses and permission bits at the beginning of the line.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000239 if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
240 if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000241
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000242 int c;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000243 if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
244 // Found a read-only executable entry. Skip characters until we reach
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000245 // the beginning of the filename or the end of the line.
246 do {
247 c = getc(fp);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000248 } while ((c != EOF) && (c != '\n') && (c != '/') && (c != '['));
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000249 if (c == EOF) break; // EOF: Was unexpected, just exit.
250
251 // Process the filename if found.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000252 if ((c == '/') || (c == '[')) {
253 // Push the '/' or '[' back into the stream to be read below.
254 ungetc(c, fp);
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000255
256 // Read to the end of the line. Exit if the read fails.
257 if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
258
259 // Drop the newline character read by fgets. We do not need to check
260 // for a zero-length string because we know that we at least read the
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000261 // '/' or '[' character.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000262 lib_name[strlen(lib_name) - 1] = '\0';
263 } else {
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000264 // No library name found, just record the raw address range.
265 snprintf(lib_name, kLibNameLen,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000266 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
267 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000268 LOG(isolate, SharedLibraryEvent(lib_name, start, end));
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000269 } else {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000270 // Entry not describing executable data. Skip to end of line to set up
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000271 // reading the next entry.
272 do {
273 c = getc(fp);
274 } while ((c != EOF) && (c != '\n'));
275 if (c == EOF) break;
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000276 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 }
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000278 free(lib_name);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000279 fclose(fp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280}
281
282
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000283void OS::SignalCodeMovingGC() {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000284 // Support for ll_prof.py.
285 //
286 // The Linux profiler built into the kernel logs all mmap's with
287 // PROT_EXEC so that analysis tools can properly attribute ticks. We
288 // do a mmap with a name known by ll_prof.py and immediately munmap
289 // it. This injects a GC marker into the stream of events generated
290 // by the kernel and allows us to synchronize V8 code log and the
291 // kernel log.
292 int size = sysconf(_SC_PAGESIZE);
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000293 FILE* f = fopen(FLAG_gc_fake_mmap, "w+");
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000294 if (f == NULL) {
295 OS::PrintError("Failed to open %s\n", FLAG_gc_fake_mmap);
296 OS::Abort();
297 }
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000298 void* addr = mmap(OS::GetRandomMmapAddr(),
299 size,
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000300#if defined(__native_client__)
301 // The Native Client port of V8 uses an interpreter,
302 // so code pages don't need PROT_EXEC.
303 PROT_READ,
304#else
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000305 PROT_READ | PROT_EXEC,
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000306#endif
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000307 MAP_PRIVATE,
308 fileno(f),
309 0);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000310 ASSERT(addr != MAP_FAILED);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000311 OS::Free(addr, size);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000312 fclose(f);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000313}
314
315
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000316int OS::StackWalk(Vector<OS::StackFrame> frames) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000317 // backtrace is a glibc extension.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000318#if defined(__GLIBC__) && !defined(__UCLIBC__)
danno@chromium.org169691d2013-07-15 08:01:13 +0000319 return POSIXBacktraceHelper<backtrace, backtrace_symbols>::StackWalk(frames);
320#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000321 return 0;
danno@chromium.org169691d2013-07-15 08:01:13 +0000322#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000323}
324
325
326// Constants used for mmap.
327static const int kMmapFd = -1;
328static const int kMmapFdOffset = 0;
329
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000330
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000331VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000333
334VirtualMemory::VirtualMemory(size_t size)
335 : address_(ReserveRegion(size)), size_(size) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336
337
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000338VirtualMemory::VirtualMemory(size_t size, size_t alignment)
339 : address_(NULL), size_(0) {
340 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
341 size_t request_size = RoundUp(size + alignment,
342 static_cast<intptr_t>(OS::AllocateAlignment()));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000343 void* reservation = mmap(OS::GetRandomMmapAddr(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000344 request_size,
345 PROT_NONE,
346 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
347 kMmapFd,
348 kMmapFdOffset);
349 if (reservation == MAP_FAILED) return;
350
351 Address base = static_cast<Address>(reservation);
352 Address aligned_base = RoundUp(base, alignment);
353 ASSERT_LE(base, aligned_base);
354
355 // Unmap extra memory reserved before and after the desired block.
356 if (aligned_base != base) {
357 size_t prefix_size = static_cast<size_t>(aligned_base - base);
358 OS::Free(base, prefix_size);
359 request_size -= prefix_size;
360 }
361
362 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
363 ASSERT_LE(aligned_size, request_size);
364
365 if (aligned_size != request_size) {
366 size_t suffix_size = request_size - aligned_size;
367 OS::Free(aligned_base + aligned_size, suffix_size);
368 request_size -= suffix_size;
369 }
370
371 ASSERT(aligned_size == request_size);
372
373 address_ = static_cast<void*>(aligned_base);
374 size_ = aligned_size;
375}
376
377
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378VirtualMemory::~VirtualMemory() {
379 if (IsReserved()) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000380 bool result = ReleaseRegion(address(), size());
381 ASSERT(result);
382 USE(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 }
384}
385
386
387bool VirtualMemory::IsReserved() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000388 return address_ != NULL;
389}
390
391
392void VirtualMemory::Reset() {
393 address_ = NULL;
394 size_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395}
396
397
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000398bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000399 return CommitRegion(address, size, is_executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400}
401
402
403bool VirtualMemory::Uncommit(void* address, size_t size) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000404 return UncommitRegion(address, size);
405}
406
407
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000408bool VirtualMemory::Guard(void* address) {
409 OS::Guard(address, OS::CommitPageSize());
410 return true;
411}
412
413
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000414void* VirtualMemory::ReserveRegion(size_t size) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000415 void* result = mmap(OS::GetRandomMmapAddr(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000416 size,
417 PROT_NONE,
418 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
419 kMmapFd,
420 kMmapFdOffset);
421
422 if (result == MAP_FAILED) return NULL;
423
424 return result;
425}
426
427
428bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000429#if defined(__native_client__)
430 // The Native Client port of V8 uses an interpreter,
431 // so code pages don't need PROT_EXEC.
432 int prot = PROT_READ | PROT_WRITE;
433#else
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000434 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000435#endif
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000436 if (MAP_FAILED == mmap(base,
437 size,
438 prot,
439 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
440 kMmapFd,
441 kMmapFdOffset)) {
442 return false;
443 }
444
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000445 return true;
446}
447
448
449bool VirtualMemory::UncommitRegion(void* base, size_t size) {
450 return mmap(base,
451 size,
452 PROT_NONE,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000453 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000454 kMmapFd,
455 kMmapFdOffset) != MAP_FAILED;
456}
457
458
459bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
460 return munmap(base, size) == 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461}
462
463
danno@chromium.org72204d52012-10-31 10:02:10 +0000464bool VirtualMemory::HasLazyCommits() {
465 return true;
466}
467
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468} } // namespace v8::internal