jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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 | |
| 28 | // Platform specific code for Solaris 10 goes here. For the POSIX comaptible |
| 29 | // parts the implementation is in platform-posix.cc. |
| 30 | |
| 31 | #ifdef __sparc |
| 32 | # error "V8 does not support the SPARC CPU architecture." |
| 33 | #endif |
| 34 | |
| 35 | #include <sys/stack.h> // for stack alignment |
| 36 | #include <unistd.h> // getpagesize(), usleep() |
| 37 | #include <sys/mman.h> // mmap() |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 38 | #include <ucontext.h> // walkstack(), getcontext() |
| 39 | #include <dlfcn.h> // dladdr |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 40 | #include <pthread.h> |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 41 | #include <semaphore.h> |
| 42 | #include <time.h> |
| 43 | #include <sys/time.h> // gettimeofday(), timeradd() |
| 44 | #include <errno.h> |
| 45 | #include <ieeefp.h> // finite() |
| 46 | #include <signal.h> // sigemptyset(), etc |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 47 | #include <sys/regset.h> |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | #undef MAP_TYPE |
| 51 | |
| 52 | #include "v8.h" |
| 53 | |
danno@chromium.org | 8c0a43f | 2012-04-03 08:37:53 +0000 | [diff] [blame] | 54 | #include "platform-posix.h" |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 55 | #include "platform.h" |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 56 | #include "v8threads.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 57 | #include "vm-state-inl.h" |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 58 | |
| 59 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 60 | // It seems there is a bug in some Solaris distributions (experienced in |
| 61 | // SunOS 5.10 Generic_141445-09) which make it difficult or impossible to |
| 62 | // access signbit() despite the availability of other C99 math functions. |
| 63 | #ifndef signbit |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 64 | namespace std { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 65 | // Test sign - usually defined in math.h |
| 66 | int signbit(double x) { |
| 67 | // We need to take care of the special case of both positive and negative |
| 68 | // versions of zero. |
| 69 | if (x == 0) { |
| 70 | return fpclass(x) & FP_NZERO; |
| 71 | } else { |
| 72 | // This won't detect negative NaN but that should be okay since we don't |
| 73 | // assume that behavior. |
| 74 | return x < 0; |
| 75 | } |
| 76 | } |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 77 | } // namespace std |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 78 | #endif // signbit |
| 79 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 80 | namespace v8 { |
| 81 | namespace internal { |
| 82 | |
| 83 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 84 | const char* OS::LocalTimezone(double time) { |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 85 | if (std::isnan(time)) return ""; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 86 | time_t tv = static_cast<time_t>(floor(time/msPerSecond)); |
| 87 | struct tm* t = localtime(&tv); |
| 88 | if (NULL == t) return ""; |
| 89 | return tzname[0]; // The location of the timezone string on Solaris. |
| 90 | } |
| 91 | |
| 92 | |
| 93 | double OS::LocalTimeOffset() { |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 94 | tzset(); |
| 95 | return -static_cast<double>(timezone * msPerSecond); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 99 | void* OS::Allocate(const size_t requested, |
| 100 | size_t* allocated, |
| 101 | bool is_executable) { |
| 102 | const size_t msize = RoundUp(requested, getpagesize()); |
| 103 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| 104 | void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0); |
| 105 | |
| 106 | if (mbase == MAP_FAILED) { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame^] | 107 | LOG(Isolate::Current(), StringEvent("OS::Allocate", "mmap failed")); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 108 | return NULL; |
| 109 | } |
| 110 | *allocated = msize; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 111 | return mbase; |
| 112 | } |
| 113 | |
| 114 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 115 | void OS::DumpBacktrace() { |
| 116 | // Currently unsupported. |
| 117 | } |
| 118 | |
| 119 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 120 | class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
| 121 | public: |
| 122 | PosixMemoryMappedFile(FILE* file, void* memory, int size) |
| 123 | : file_(file), memory_(memory), size_(size) { } |
| 124 | virtual ~PosixMemoryMappedFile(); |
| 125 | virtual void* memory() { return memory_; } |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 126 | virtual int size() { return size_; } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 127 | private: |
| 128 | FILE* file_; |
| 129 | void* memory_; |
| 130 | int size_; |
| 131 | }; |
| 132 | |
| 133 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 134 | OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 135 | FILE* file = fopen(name, "r+"); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 136 | if (file == NULL) return NULL; |
| 137 | |
| 138 | fseek(file, 0, SEEK_END); |
| 139 | int size = ftell(file); |
| 140 | |
| 141 | void* memory = |
| 142 | mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 143 | return new PosixMemoryMappedFile(file, memory, size); |
| 144 | } |
| 145 | |
| 146 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 147 | OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| 148 | void* initial) { |
| 149 | FILE* file = fopen(name, "w+"); |
| 150 | if (file == NULL) return NULL; |
| 151 | int result = fwrite(initial, size, 1, file); |
| 152 | if (result < 1) { |
| 153 | fclose(file); |
| 154 | return NULL; |
| 155 | } |
| 156 | void* memory = |
| 157 | mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 158 | return new PosixMemoryMappedFile(file, memory, size); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | PosixMemoryMappedFile::~PosixMemoryMappedFile() { |
| 163 | if (memory_) munmap(memory_, size_); |
| 164 | fclose(file_); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | void OS::LogSharedLibraryAddresses() { |
| 169 | } |
| 170 | |
| 171 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 172 | void OS::SignalCodeMovingGC() { |
| 173 | } |
| 174 | |
| 175 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 176 | struct StackWalker { |
| 177 | Vector<OS::StackFrame>& frames; |
| 178 | int index; |
| 179 | }; |
| 180 | |
| 181 | |
| 182 | static int StackWalkCallback(uintptr_t pc, int signo, void* data) { |
| 183 | struct StackWalker* walker = static_cast<struct StackWalker*>(data); |
| 184 | Dl_info info; |
| 185 | |
| 186 | int i = walker->index; |
| 187 | |
| 188 | walker->frames[i].address = reinterpret_cast<void*>(pc); |
| 189 | |
| 190 | // Make sure line termination is in place. |
| 191 | walker->frames[i].text[OS::kStackWalkMaxTextLen - 1] = '\0'; |
| 192 | |
| 193 | Vector<char> text = MutableCStrVector(walker->frames[i].text, |
| 194 | OS::kStackWalkMaxTextLen); |
| 195 | |
| 196 | if (dladdr(reinterpret_cast<void*>(pc), &info) == 0) { |
| 197 | OS::SNPrintF(text, "[0x%p]", pc); |
| 198 | } else if ((info.dli_fname != NULL && info.dli_sname != NULL)) { |
| 199 | // We have symbol info. |
| 200 | OS::SNPrintF(text, "%s'%s+0x%x", info.dli_fname, info.dli_sname, pc); |
| 201 | } else { |
| 202 | // No local symbol info. |
| 203 | OS::SNPrintF(text, |
| 204 | "%s'0x%p [0x%p]", |
| 205 | info.dli_fname, |
| 206 | pc - reinterpret_cast<uintptr_t>(info.dli_fbase), |
| 207 | pc); |
| 208 | } |
| 209 | walker->index++; |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 214 | int OS::StackWalk(Vector<OS::StackFrame> frames) { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 215 | ucontext_t ctx; |
| 216 | struct StackWalker walker = { frames, 0 }; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 217 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 218 | if (getcontext(&ctx) < 0) return kStackWalkError; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 219 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 220 | if (!walkcontext(&ctx, StackWalkCallback, &walker)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 221 | return kStackWalkError; |
| 222 | } |
| 223 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 224 | return walker.index; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | |
| 228 | // Constants used for mmap. |
| 229 | static const int kMmapFd = -1; |
| 230 | static const int kMmapFdOffset = 0; |
| 231 | |
| 232 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 233 | VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } |
| 234 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 235 | |
| 236 | VirtualMemory::VirtualMemory(size_t size) |
| 237 | : address_(ReserveRegion(size)), size_(size) { } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 238 | |
| 239 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 240 | VirtualMemory::VirtualMemory(size_t size, size_t alignment) |
| 241 | : address_(NULL), size_(0) { |
| 242 | ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment()))); |
| 243 | size_t request_size = RoundUp(size + alignment, |
| 244 | static_cast<intptr_t>(OS::AllocateAlignment())); |
| 245 | void* reservation = mmap(OS::GetRandomMmapAddr(), |
| 246 | request_size, |
| 247 | PROT_NONE, |
| 248 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, |
| 249 | kMmapFd, |
| 250 | kMmapFdOffset); |
| 251 | if (reservation == MAP_FAILED) return; |
| 252 | |
| 253 | Address base = static_cast<Address>(reservation); |
| 254 | Address aligned_base = RoundUp(base, alignment); |
| 255 | ASSERT_LE(base, aligned_base); |
| 256 | |
| 257 | // Unmap extra memory reserved before and after the desired block. |
| 258 | if (aligned_base != base) { |
| 259 | size_t prefix_size = static_cast<size_t>(aligned_base - base); |
| 260 | OS::Free(base, prefix_size); |
| 261 | request_size -= prefix_size; |
| 262 | } |
| 263 | |
| 264 | size_t aligned_size = RoundUp(size, OS::AllocateAlignment()); |
| 265 | ASSERT_LE(aligned_size, request_size); |
| 266 | |
| 267 | if (aligned_size != request_size) { |
| 268 | size_t suffix_size = request_size - aligned_size; |
| 269 | OS::Free(aligned_base + aligned_size, suffix_size); |
| 270 | request_size -= suffix_size; |
| 271 | } |
| 272 | |
| 273 | ASSERT(aligned_size == request_size); |
| 274 | |
| 275 | address_ = static_cast<void*>(aligned_base); |
| 276 | size_ = aligned_size; |
| 277 | } |
| 278 | |
| 279 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 280 | VirtualMemory::~VirtualMemory() { |
| 281 | if (IsReserved()) { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 282 | bool result = ReleaseRegion(address(), size()); |
| 283 | ASSERT(result); |
| 284 | USE(result); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | |
| 289 | bool VirtualMemory::IsReserved() { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 290 | return address_ != NULL; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 294 | void VirtualMemory::Reset() { |
| 295 | address_ = NULL; |
| 296 | size_ = 0; |
| 297 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 298 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 299 | |
| 300 | bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
| 301 | return CommitRegion(address, size, is_executable); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
| 305 | bool VirtualMemory::Uncommit(void* address, size_t size) { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 306 | return UncommitRegion(address, size); |
| 307 | } |
| 308 | |
| 309 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 310 | bool VirtualMemory::Guard(void* address) { |
| 311 | OS::Guard(address, OS::CommitPageSize()); |
| 312 | return true; |
| 313 | } |
| 314 | |
| 315 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 316 | void* VirtualMemory::ReserveRegion(size_t size) { |
| 317 | void* result = mmap(OS::GetRandomMmapAddr(), |
| 318 | size, |
| 319 | PROT_NONE, |
| 320 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, |
| 321 | kMmapFd, |
| 322 | kMmapFdOffset); |
| 323 | |
| 324 | if (result == MAP_FAILED) return NULL; |
| 325 | |
| 326 | return result; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { |
| 331 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| 332 | if (MAP_FAILED == mmap(base, |
| 333 | size, |
| 334 | prot, |
| 335 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, |
| 336 | kMmapFd, |
| 337 | kMmapFdOffset)) { |
| 338 | return false; |
| 339 | } |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 340 | return true; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | bool VirtualMemory::UncommitRegion(void* base, size_t size) { |
| 345 | return mmap(base, |
| 346 | size, |
| 347 | PROT_NONE, |
| 348 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED, |
| 349 | kMmapFd, |
| 350 | kMmapFdOffset) != MAP_FAILED; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | bool VirtualMemory::ReleaseRegion(void* base, size_t size) { |
| 355 | return munmap(base, size) == 0; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 359 | bool VirtualMemory::HasLazyCommits() { |
| 360 | // TODO(alph): implement for the platform. |
| 361 | return false; |
| 362 | } |
| 363 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 364 | } } // namespace v8::internal |