ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // 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.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 28 | // Platform specific code for Linux goes here. For the POSIX comaptible parts |
| 29 | // the implementation is in platform-posix.cc. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 30 | |
| 31 | #include <pthread.h> |
| 32 | #include <semaphore.h> |
| 33 | #include <signal.h> |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 34 | #include <sys/prctl.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include <sys/time.h> |
| 36 | #include <sys/resource.h> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 37 | #include <sys/syscall.h> |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 38 | #include <sys/types.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | |
| 41 | // Ubuntu Dapper requires memory pages to be marked as |
| 42 | // executable. Otherwise, OS raises an exception when executing code |
| 43 | // in that page. |
| 44 | #include <sys/types.h> // mmap & munmap |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 45 | #include <sys/mman.h> // mmap & munmap |
| 46 | #include <sys/stat.h> // open |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 47 | #include <fcntl.h> // open |
| 48 | #include <unistd.h> // sysconf |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 49 | #include <strings.h> // index |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | #include <errno.h> |
| 51 | #include <stdarg.h> |
| 52 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 53 | // GLibc on ARM defines mcontext_t has a typedef for 'struct sigcontext'. |
| 54 | // Old versions of the C library <signal.h> didn't define the type. |
| 55 | #if defined(__ANDROID__) && !defined(__BIONIC_HAVE_UCONTEXT_T) && \ |
| 56 | defined(__arm__) && !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT) |
| 57 | #include <asm/sigcontext.h> |
| 58 | #endif |
| 59 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 60 | #undef MAP_TYPE |
| 61 | |
| 62 | #include "v8.h" |
| 63 | |
| 64 | #include "platform.h" |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 65 | #include "v8threads.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 66 | #include "vm-state-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 67 | |
| 68 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 69 | namespace v8 { |
| 70 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 71 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 72 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 73 | #ifdef __arm__ |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 74 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 75 | bool OS::ArmUsingHardFloat() { |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 76 | // GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify |
| 77 | // the Floating Point ABI used (PCS stands for Procedure Call Standard). |
| 78 | // We use these as well as a couple of other defines to statically determine |
| 79 | // what FP ABI used. |
| 80 | // GCC versions 4.4 and below don't support hard-fp. |
| 81 | // GCC versions 4.5 may support hard-fp without defining __ARM_PCS or |
| 82 | // __ARM_PCS_VFP. |
| 83 | |
| 84 | #define GCC_VERSION (__GNUC__ * 10000 \ |
| 85 | + __GNUC_MINOR__ * 100 \ |
| 86 | + __GNUC_PATCHLEVEL__) |
| 87 | #if GCC_VERSION >= 40600 |
| 88 | #if defined(__ARM_PCS_VFP) |
| 89 | return true; |
| 90 | #else |
| 91 | return false; |
| 92 | #endif |
| 93 | |
| 94 | #elif GCC_VERSION < 40500 |
| 95 | return false; |
| 96 | |
| 97 | #else |
| 98 | #if defined(__ARM_PCS_VFP) |
| 99 | return true; |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 100 | #elif defined(__ARM_PCS) || defined(__SOFTFP__) || defined(__SOFTFP) || \ |
| 101 | !defined(__VFP_FP__) |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 102 | return false; |
| 103 | #else |
| 104 | #error "Your version of GCC does not report the FP ABI compiled for." \ |
| 105 | "Please report it on this issue" \ |
| 106 | "http://code.google.com/p/v8/issues/detail?id=2140" |
| 107 | |
| 108 | #endif |
| 109 | #endif |
| 110 | #undef GCC_VERSION |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 111 | } |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 112 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 113 | #endif // def __arm__ |
| 114 | |
| 115 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 116 | const char* OS::LocalTimezone(double time) { |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 117 | if (std::isnan(time)) return ""; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 118 | time_t tv = static_cast<time_t>(floor(time/msPerSecond)); |
| 119 | struct tm* t = localtime(&tv); |
| 120 | if (NULL == t) return ""; |
| 121 | return t->tm_zone; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | double OS::LocalTimeOffset() { |
| 126 | time_t tv = time(NULL); |
| 127 | struct tm* t = localtime(&tv); |
| 128 | // tm_gmtoff includes any daylight savings offset, so subtract it. |
| 129 | return static_cast<double>(t->tm_gmtoff * msPerSecond - |
| 130 | (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); |
| 131 | } |
| 132 | |
| 133 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 134 | void* OS::Allocate(const size_t requested, |
| 135 | size_t* allocated, |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 136 | bool is_executable) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 137 | const size_t msize = RoundUp(requested, AllocateAlignment()); |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 138 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 139 | void* addr = OS::GetRandomMmapAddr(); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 140 | void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 141 | if (mbase == MAP_FAILED) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 142 | LOG(i::Isolate::Current(), |
| 143 | StringEvent("OS::Allocate", "mmap failed")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 144 | return NULL; |
| 145 | } |
| 146 | *allocated = msize; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 147 | return mbase; |
| 148 | } |
| 149 | |
| 150 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 151 | class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
| 152 | public: |
| 153 | PosixMemoryMappedFile(FILE* file, void* memory, int size) |
| 154 | : file_(file), memory_(memory), size_(size) { } |
| 155 | virtual ~PosixMemoryMappedFile(); |
| 156 | virtual void* memory() { return memory_; } |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 157 | virtual int size() { return size_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | private: |
| 159 | FILE* file_; |
| 160 | void* memory_; |
| 161 | int size_; |
| 162 | }; |
| 163 | |
| 164 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 165 | OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 166 | FILE* file = fopen(name, "r+"); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 167 | if (file == NULL) return NULL; |
| 168 | |
| 169 | fseek(file, 0, SEEK_END); |
| 170 | int size = ftell(file); |
| 171 | |
| 172 | void* memory = |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 173 | mmap(OS::GetRandomMmapAddr(), |
| 174 | size, |
| 175 | PROT_READ | PROT_WRITE, |
| 176 | MAP_SHARED, |
| 177 | fileno(file), |
| 178 | 0); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 179 | return new PosixMemoryMappedFile(file, memory, size); |
| 180 | } |
| 181 | |
| 182 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 183 | OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| 184 | void* initial) { |
| 185 | FILE* file = fopen(name, "w+"); |
| 186 | if (file == NULL) return NULL; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 187 | int result = fwrite(initial, size, 1, file); |
| 188 | if (result < 1) { |
| 189 | fclose(file); |
| 190 | return NULL; |
| 191 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 192 | void* memory = |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 193 | mmap(OS::GetRandomMmapAddr(), |
| 194 | size, |
| 195 | PROT_READ | PROT_WRITE, |
| 196 | MAP_SHARED, |
| 197 | fileno(file), |
| 198 | 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 199 | return new PosixMemoryMappedFile(file, memory, size); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | PosixMemoryMappedFile::~PosixMemoryMappedFile() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 204 | if (memory_) OS::Free(memory_, size_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 205 | fclose(file_); |
| 206 | } |
| 207 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 208 | |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 209 | void OS::LogSharedLibraryAddresses(Isolate* isolate) { |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 210 | // This function assumes that the layout of the file is as follows: |
| 211 | // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name] |
| 212 | // If we encounter an unexpected situation we abort scanning further entries. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 213 | FILE* fp = fopen("/proc/self/maps", "r"); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 214 | if (fp == NULL) return; |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 215 | |
| 216 | // Allocate enough room to be able to store a full file name. |
| 217 | const int kLibNameLen = FILENAME_MAX + 1; |
| 218 | char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen)); |
| 219 | |
| 220 | // This loop will terminate once the scanning hits an EOF. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 221 | while (true) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 222 | uintptr_t start, end; |
| 223 | char attr_r, attr_w, attr_x, attr_p; |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 224 | // Parse the addresses and permission bits at the beginning of the line. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 225 | if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break; |
| 226 | if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break; |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 227 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 228 | int c; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 229 | if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') { |
| 230 | // Found a read-only executable entry. Skip characters until we reach |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 231 | // the beginning of the filename or the end of the line. |
| 232 | do { |
| 233 | c = getc(fp); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 234 | } while ((c != EOF) && (c != '\n') && (c != '/') && (c != '[')); |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 235 | if (c == EOF) break; // EOF: Was unexpected, just exit. |
| 236 | |
| 237 | // Process the filename if found. |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 238 | if ((c == '/') || (c == '[')) { |
| 239 | // Push the '/' or '[' back into the stream to be read below. |
| 240 | ungetc(c, fp); |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 241 | |
| 242 | // Read to the end of the line. Exit if the read fails. |
| 243 | if (fgets(lib_name, kLibNameLen, fp) == NULL) break; |
| 244 | |
| 245 | // Drop the newline character read by fgets. We do not need to check |
| 246 | // for a zero-length string because we know that we at least read the |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 247 | // '/' or '[' character. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 248 | lib_name[strlen(lib_name) - 1] = '\0'; |
| 249 | } else { |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 250 | // No library name found, just record the raw address range. |
| 251 | snprintf(lib_name, kLibNameLen, |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 252 | "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end); |
| 253 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 254 | LOG(isolate, SharedLibraryEvent(lib_name, start, end)); |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 255 | } else { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 256 | // Entry not describing executable data. Skip to end of line to set up |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 257 | // reading the next entry. |
| 258 | do { |
| 259 | c = getc(fp); |
| 260 | } while ((c != EOF) && (c != '\n')); |
| 261 | if (c == EOF) break; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 262 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 263 | } |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 264 | free(lib_name); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 265 | fclose(fp); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 269 | void OS::SignalCodeMovingGC() { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 270 | // Support for ll_prof.py. |
| 271 | // |
| 272 | // The Linux profiler built into the kernel logs all mmap's with |
| 273 | // PROT_EXEC so that analysis tools can properly attribute ticks. We |
| 274 | // do a mmap with a name known by ll_prof.py and immediately munmap |
| 275 | // it. This injects a GC marker into the stream of events generated |
| 276 | // by the kernel and allows us to synchronize V8 code log and the |
| 277 | // kernel log. |
| 278 | int size = sysconf(_SC_PAGESIZE); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 279 | FILE* f = fopen(FLAG_gc_fake_mmap, "w+"); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 280 | if (f == NULL) { |
| 281 | OS::PrintError("Failed to open %s\n", FLAG_gc_fake_mmap); |
| 282 | OS::Abort(); |
| 283 | } |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 284 | void* addr = mmap(OS::GetRandomMmapAddr(), |
| 285 | size, |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 286 | #if defined(__native_client__) |
| 287 | // The Native Client port of V8 uses an interpreter, |
| 288 | // so code pages don't need PROT_EXEC. |
| 289 | PROT_READ, |
| 290 | #else |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 291 | PROT_READ | PROT_EXEC, |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 292 | #endif |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 293 | MAP_PRIVATE, |
| 294 | fileno(f), |
| 295 | 0); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 296 | ASSERT(addr != MAP_FAILED); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 297 | OS::Free(addr, size); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 298 | fclose(f); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 302 | // Constants used for mmap. |
| 303 | static const int kMmapFd = -1; |
| 304 | static const int kMmapFdOffset = 0; |
| 305 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 306 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 307 | VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 308 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 309 | |
| 310 | VirtualMemory::VirtualMemory(size_t size) |
| 311 | : address_(ReserveRegion(size)), size_(size) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 312 | |
| 313 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 314 | VirtualMemory::VirtualMemory(size_t size, size_t alignment) |
| 315 | : address_(NULL), size_(0) { |
| 316 | ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment()))); |
| 317 | size_t request_size = RoundUp(size + alignment, |
| 318 | static_cast<intptr_t>(OS::AllocateAlignment())); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 319 | void* reservation = mmap(OS::GetRandomMmapAddr(), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 320 | request_size, |
| 321 | PROT_NONE, |
| 322 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, |
| 323 | kMmapFd, |
| 324 | kMmapFdOffset); |
| 325 | if (reservation == MAP_FAILED) return; |
| 326 | |
| 327 | Address base = static_cast<Address>(reservation); |
| 328 | Address aligned_base = RoundUp(base, alignment); |
| 329 | ASSERT_LE(base, aligned_base); |
| 330 | |
| 331 | // Unmap extra memory reserved before and after the desired block. |
| 332 | if (aligned_base != base) { |
| 333 | size_t prefix_size = static_cast<size_t>(aligned_base - base); |
| 334 | OS::Free(base, prefix_size); |
| 335 | request_size -= prefix_size; |
| 336 | } |
| 337 | |
| 338 | size_t aligned_size = RoundUp(size, OS::AllocateAlignment()); |
| 339 | ASSERT_LE(aligned_size, request_size); |
| 340 | |
| 341 | if (aligned_size != request_size) { |
| 342 | size_t suffix_size = request_size - aligned_size; |
| 343 | OS::Free(aligned_base + aligned_size, suffix_size); |
| 344 | request_size -= suffix_size; |
| 345 | } |
| 346 | |
| 347 | ASSERT(aligned_size == request_size); |
| 348 | |
| 349 | address_ = static_cast<void*>(aligned_base); |
| 350 | size_ = aligned_size; |
| 351 | } |
| 352 | |
| 353 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 354 | VirtualMemory::~VirtualMemory() { |
| 355 | if (IsReserved()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 356 | bool result = ReleaseRegion(address(), size()); |
| 357 | ASSERT(result); |
| 358 | USE(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
| 362 | |
| 363 | bool VirtualMemory::IsReserved() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 364 | return address_ != NULL; |
| 365 | } |
| 366 | |
| 367 | |
| 368 | void VirtualMemory::Reset() { |
| 369 | address_ = NULL; |
| 370 | size_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 374 | bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 375 | return CommitRegion(address, size, is_executable); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | |
| 379 | bool VirtualMemory::Uncommit(void* address, size_t size) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 380 | return UncommitRegion(address, size); |
| 381 | } |
| 382 | |
| 383 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 384 | bool VirtualMemory::Guard(void* address) { |
| 385 | OS::Guard(address, OS::CommitPageSize()); |
| 386 | return true; |
| 387 | } |
| 388 | |
| 389 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 390 | void* VirtualMemory::ReserveRegion(size_t size) { |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 391 | void* result = mmap(OS::GetRandomMmapAddr(), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 392 | size, |
| 393 | PROT_NONE, |
| 394 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, |
| 395 | kMmapFd, |
| 396 | kMmapFdOffset); |
| 397 | |
| 398 | if (result == MAP_FAILED) return NULL; |
| 399 | |
| 400 | return result; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 405 | #if defined(__native_client__) |
| 406 | // The Native Client port of V8 uses an interpreter, |
| 407 | // so code pages don't need PROT_EXEC. |
| 408 | int prot = PROT_READ | PROT_WRITE; |
| 409 | #else |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 410 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 411 | #endif |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 412 | if (MAP_FAILED == mmap(base, |
| 413 | size, |
| 414 | prot, |
| 415 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, |
| 416 | kMmapFd, |
| 417 | kMmapFdOffset)) { |
| 418 | return false; |
| 419 | } |
| 420 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 421 | return true; |
| 422 | } |
| 423 | |
| 424 | |
| 425 | bool VirtualMemory::UncommitRegion(void* base, size_t size) { |
| 426 | return mmap(base, |
| 427 | size, |
| 428 | PROT_NONE, |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 429 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 430 | kMmapFd, |
| 431 | kMmapFdOffset) != MAP_FAILED; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | bool VirtualMemory::ReleaseRegion(void* base, size_t size) { |
| 436 | return munmap(base, size) == 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 440 | bool VirtualMemory::HasLazyCommits() { |
| 441 | return true; |
| 442 | } |
| 443 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 444 | } } // namespace v8::internal |