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