1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
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.
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.
28 // Platform-specific code for Linux goes here. For the POSIX-compatible
29 // parts, the implementation is in platform-posix.cc.
32 #include <semaphore.h>
34 #include <sys/prctl.h>
36 #include <sys/resource.h>
37 #include <sys/syscall.h>
38 #include <sys/types.h>
41 // Ubuntu Dapper requires memory pages to be marked as
42 // executable. Otherwise, OS raises an exception when executing code
44 #include <sys/types.h> // mmap & munmap
45 #include <sys/mman.h> // mmap & munmap
46 #include <sys/stat.h> // open
47 #include <fcntl.h> // open
48 #include <unistd.h> // sysconf
49 #include <strings.h> // index
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>
60 #if defined(LEAK_SANITIZER)
61 #include <sanitizer/lsan_interface.h>
69 #include "v8threads.h"
70 #include "vm-state-inl.h"
79 bool OS::ArmUsingHardFloat() {
80 // GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify
81 // the Floating Point ABI used (PCS stands for Procedure Call Standard).
82 // We use these as well as a couple of other defines to statically determine
84 // GCC versions 4.4 and below don't support hard-fp.
85 // GCC versions 4.5 may support hard-fp without defining __ARM_PCS or
88 #define GCC_VERSION (__GNUC__ * 10000 \
89 + __GNUC_MINOR__ * 100 \
90 + __GNUC_PATCHLEVEL__)
91 #if GCC_VERSION >= 40600
92 #if defined(__ARM_PCS_VFP)
98 #elif GCC_VERSION < 40500
102 #if defined(__ARM_PCS_VFP)
104 #elif defined(__ARM_PCS) || defined(__SOFTFP__) || defined(__SOFTFP) || \
108 #error "Your version of GCC does not report the FP ABI compiled for." \
109 "Please report it on this issue" \
110 "http://code.google.com/p/v8/issues/detail?id=2140"
117 #endif // def __arm__
120 const char* OS::LocalTimezone(double time) {
121 if (std::isnan(time)) return "";
122 time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
123 struct tm* t = localtime(&tv);
124 if (NULL == t) return "";
129 double OS::LocalTimeOffset() {
130 time_t tv = time(NULL);
131 struct tm* t = localtime(&tv);
132 // tm_gmtoff includes any daylight savings offset, so subtract it.
133 return static_cast<double>(t->tm_gmtoff * msPerSecond -
134 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
138 void* OS::Allocate(const size_t requested,
140 bool is_executable) {
141 const size_t msize = RoundUp(requested, AllocateAlignment());
142 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
143 void* addr = OS::GetRandomMmapAddr();
144 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
145 if (mbase == MAP_FAILED) {
146 LOG(i::Isolate::Current(),
147 StringEvent("OS::Allocate", "mmap failed"));
155 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
157 PosixMemoryMappedFile(FILE* file, void* memory, int size)
158 : file_(file), memory_(memory), size_(size) { }
159 virtual ~PosixMemoryMappedFile();
160 virtual void* memory() { return memory_; }
161 virtual int size() { return size_; }
169 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
170 FILE* file = fopen(name, "r+");
171 if (file == NULL) return NULL;
173 fseek(file, 0, SEEK_END);
174 int size = ftell(file);
177 mmap(OS::GetRandomMmapAddr(),
179 PROT_READ | PROT_WRITE,
183 return new PosixMemoryMappedFile(file, memory, size);
187 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
189 FILE* file = fopen(name, "w+");
190 if (file == NULL) return NULL;
191 int result = fwrite(initial, size, 1, file);
197 mmap(OS::GetRandomMmapAddr(),
199 PROT_READ | PROT_WRITE,
203 return new PosixMemoryMappedFile(file, memory, size);
207 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
208 if (memory_) OS::Free(memory_, size_);
213 void OS::LogSharedLibraryAddresses(Isolate* isolate) {
214 // This function assumes that the layout of the file is as follows:
215 // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
216 // If we encounter an unexpected situation we abort scanning further entries.
217 FILE* fp = fopen("/proc/self/maps", "r");
218 if (fp == NULL) return;
220 // Allocate enough room to be able to store a full file name.
221 const int kLibNameLen = FILENAME_MAX + 1;
222 char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
224 // This loop will terminate once the scanning hits an EOF.
226 uintptr_t start, end;
227 char attr_r, attr_w, attr_x, attr_p;
228 // Parse the addresses and permission bits at the beginning of the line.
229 if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
230 if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
233 if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
234 // Found a read-only executable entry. Skip characters until we reach
235 // the beginning of the filename or the end of the line.
238 } while ((c != EOF) && (c != '\n') && (c != '/') && (c != '['));
239 if (c == EOF) break; // EOF: Was unexpected, just exit.
241 // Process the filename if found.
242 if ((c == '/') || (c == '[')) {
243 // Push the '/' or '[' back into the stream to be read below.
246 // Read to the end of the line. Exit if the read fails.
247 if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
249 // Drop the newline character read by fgets. We do not need to check
250 // for a zero-length string because we know that we at least read the
251 // '/' or '[' character.
252 lib_name[strlen(lib_name) - 1] = '\0';
254 // No library name found, just record the raw address range.
255 snprintf(lib_name, kLibNameLen,
256 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
258 LOG(isolate, SharedLibraryEvent(lib_name, start, end));
260 // Entry not describing executable data. Skip to end of line to set up
261 // reading the next entry.
264 } while ((c != EOF) && (c != '\n'));
273 void OS::SignalCodeMovingGC() {
274 // Support for ll_prof.py.
276 // The Linux profiler built into the kernel logs all mmap's with
277 // PROT_EXEC so that analysis tools can properly attribute ticks. We
278 // do a mmap with a name known by ll_prof.py and immediately munmap
279 // it. This injects a GC marker into the stream of events generated
280 // by the kernel and allows us to synchronize V8 code log and the
282 int size = sysconf(_SC_PAGESIZE);
283 FILE* f = fopen(FLAG_gc_fake_mmap, "w+");
285 OS::PrintError("Failed to open %s\n", FLAG_gc_fake_mmap);
288 void* addr = mmap(OS::GetRandomMmapAddr(),
290 #if defined(__native_client__)
291 // The Native Client port of V8 uses an interpreter,
292 // so code pages don't need PROT_EXEC.
295 PROT_READ | PROT_EXEC,
300 ASSERT(addr != MAP_FAILED);
301 OS::Free(addr, size);
306 // Constants used for mmap.
307 static const int kMmapFd = -1;
308 static const int kMmapFdOffset = 0;
311 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
314 VirtualMemory::VirtualMemory(size_t size)
315 : address_(ReserveRegion(size)), size_(size) { }
318 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
319 : address_(NULL), size_(0) {
320 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
321 size_t request_size = RoundUp(size + alignment,
322 static_cast<intptr_t>(OS::AllocateAlignment()));
323 void* reservation = mmap(OS::GetRandomMmapAddr(),
326 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
329 if (reservation == MAP_FAILED) return;
331 Address base = static_cast<Address>(reservation);
332 Address aligned_base = RoundUp(base, alignment);
333 ASSERT_LE(base, aligned_base);
335 // Unmap extra memory reserved before and after the desired block.
336 if (aligned_base != base) {
337 size_t prefix_size = static_cast<size_t>(aligned_base - base);
338 OS::Free(base, prefix_size);
339 request_size -= prefix_size;
342 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
343 ASSERT_LE(aligned_size, request_size);
345 if (aligned_size != request_size) {
346 size_t suffix_size = request_size - aligned_size;
347 OS::Free(aligned_base + aligned_size, suffix_size);
348 request_size -= suffix_size;
351 ASSERT(aligned_size == request_size);
353 address_ = static_cast<void*>(aligned_base);
354 size_ = aligned_size;
355 #if defined(LEAK_SANITIZER)
356 __lsan_register_root_region(address_, size_);
361 VirtualMemory::~VirtualMemory() {
363 bool result = ReleaseRegion(address(), size());
370 bool VirtualMemory::IsReserved() {
371 return address_ != NULL;
375 void VirtualMemory::Reset() {
381 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
382 return CommitRegion(address, size, is_executable);
386 bool VirtualMemory::Uncommit(void* address, size_t size) {
387 return UncommitRegion(address, size);
391 bool VirtualMemory::Guard(void* address) {
392 OS::Guard(address, OS::CommitPageSize());
397 void* VirtualMemory::ReserveRegion(size_t size) {
398 void* result = mmap(OS::GetRandomMmapAddr(),
401 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
405 if (result == MAP_FAILED) return NULL;
407 #if defined(LEAK_SANITIZER)
408 __lsan_register_root_region(result, size);
414 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
415 #if defined(__native_client__)
416 // The Native Client port of V8 uses an interpreter,
417 // so code pages don't need PROT_EXEC.
418 int prot = PROT_READ | PROT_WRITE;
420 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
422 if (MAP_FAILED == mmap(base,
425 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
435 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
439 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED,
441 kMmapFdOffset) != MAP_FAILED;
445 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
446 #if defined(LEAK_SANITIZER)
447 __lsan_unregister_root_region(base, size);
449 return munmap(base, size) == 0;
453 bool VirtualMemory::HasLazyCommits() {
457 } } // namespace v8::internal