1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Platform-specific code for Linux goes here. For the POSIX-compatible
6 // parts, the implementation is in platform-posix.cc.
13 #include <sys/resource.h>
16 // Ubuntu Dapper requires memory pages to be marked as
17 // executable. Otherwise, OS raises an exception when executing code
20 #include <fcntl.h> // open
22 #include <strings.h> // index
23 #include <sys/mman.h> // mmap & munmap
24 #include <sys/stat.h> // open
25 #include <sys/types.h> // mmap & munmap
26 #include <unistd.h> // sysconf
28 // GLibc on ARM defines mcontext_t has a typedef for 'struct sigcontext'.
29 // Old versions of the C library <signal.h> didn't define the type.
30 #if defined(__ANDROID__) && !defined(__BIONIC_HAVE_UCONTEXT_T) && \
31 (defined(__arm__) || defined(__aarch64__)) && \
32 !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT)
33 #include <asm/sigcontext.h> // NOLINT
36 #if defined(LEAK_SANITIZER)
37 #include <sanitizer/lsan_interface.h>
44 #include "src/base/macros.h"
45 #include "src/base/platform/platform.h"
48 #if !defined(MAP_NORESERVE)
49 // PNaCL doesn't have this, so we always grab all of the memory, which is bad.
50 #define MAP_NORESERVE 0
53 #include <sys/prctl.h>
54 #include <sys/syscall.h>
63 bool OS::ArmUsingHardFloat() {
64 // GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify
65 // the Floating Point ABI used (PCS stands for Procedure Call Standard).
66 // We use these as well as a couple of other defines to statically determine
68 // GCC versions 4.4 and below don't support hard-fp.
69 // GCC versions 4.5 may support hard-fp without defining __ARM_PCS or
72 #define GCC_VERSION (__GNUC__ * 10000 \
73 + __GNUC_MINOR__ * 100 \
74 + __GNUC_PATCHLEVEL__)
75 #if GCC_VERSION >= 40600
76 #if defined(__ARM_PCS_VFP)
82 #elif GCC_VERSION < 40500
86 #if defined(__ARM_PCS_VFP)
88 #elif defined(__ARM_PCS) || defined(__SOFTFP__) || defined(__SOFTFP) || \
92 #error "Your version of GCC does not report the FP ABI compiled for." \
93 "Please report it on this issue" \
94 "http://code.google.com/p/v8/issues/detail?id=2140"
101 #endif // def __arm__
104 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
106 // Missing support for tm_zone field.
109 if (std::isnan(time)) return "";
110 time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
111 struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn)
112 if (!t || !t->tm_zone) return "";
118 double OS::LocalTimeOffset(TimezoneCache* cache) {
120 // Missing support for tm_zone field.
123 time_t tv = time(NULL);
124 struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn)
125 // tm_gmtoff includes any daylight savings offset, so subtract it.
126 return static_cast<double>(t->tm_gmtoff * msPerSecond -
127 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
132 void* OS::Allocate(const size_t requested,
134 bool is_executable) {
135 const size_t msize = RoundUp(requested, AllocateAlignment());
136 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
137 void* addr = OS::GetRandomMmapAddr();
138 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
139 if (mbase == MAP_FAILED) return NULL;
145 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
146 std::vector<SharedLibraryAddress> result;
147 // This function assumes that the layout of the file is as follows:
148 // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
149 // If we encounter an unexpected situation we abort scanning further entries.
150 FILE* fp = fopen("/proc/self/maps", "r");
151 if (fp == NULL) return result;
153 // Allocate enough room to be able to store a full file name.
154 const int kLibNameLen = FILENAME_MAX + 1;
155 char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
157 // This loop will terminate once the scanning hits an EOF.
159 uintptr_t start, end;
160 char attr_r, attr_w, attr_x, attr_p;
161 // Parse the addresses and permission bits at the beginning of the line.
162 if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
163 if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
166 if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
167 // Found a read-only executable entry. Skip characters until we reach
168 // the beginning of the filename or the end of the line.
171 } while ((c != EOF) && (c != '\n') && (c != '/') && (c != '['));
172 if (c == EOF) break; // EOF: Was unexpected, just exit.
174 // Process the filename if found.
175 if ((c == '/') || (c == '[')) {
176 // Push the '/' or '[' back into the stream to be read below.
179 // Read to the end of the line. Exit if the read fails.
180 if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
182 // Drop the newline character read by fgets. We do not need to check
183 // for a zero-length string because we know that we at least read the
184 // '/' or '[' character.
185 lib_name[strlen(lib_name) - 1] = '\0';
187 // No library name found, just record the raw address range.
188 snprintf(lib_name, kLibNameLen,
189 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
191 result.push_back(SharedLibraryAddress(lib_name, start, end));
193 // Entry not describing executable data. Skip to end of line to set up
194 // reading the next entry.
197 } while ((c != EOF) && (c != '\n'));
207 void OS::SignalCodeMovingGC() {
208 // Support for ll_prof.py.
210 // The Linux profiler built into the kernel logs all mmap's with
211 // PROT_EXEC so that analysis tools can properly attribute ticks. We
212 // do a mmap with a name known by ll_prof.py and immediately munmap
213 // it. This injects a GC marker into the stream of events generated
214 // by the kernel and allows us to synchronize V8 code log and the
216 long size = sysconf(_SC_PAGESIZE); // NOLINT(runtime/int)
217 FILE* f = fopen(OS::GetGCFakeMMapFile(), "w+");
219 OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile());
222 void* addr = mmap(OS::GetRandomMmapAddr(), size,
224 // The Native Client port of V8 uses an interpreter,
225 // so code pages don't need PROT_EXEC.
228 PROT_READ | PROT_EXEC,
230 MAP_PRIVATE, fileno(f), 0);
231 DCHECK_NE(MAP_FAILED, addr);
232 OS::Free(addr, size);
237 // Constants used for mmap.
238 static const int kMmapFd = -1;
239 static const int kMmapFdOffset = 0;
242 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
245 VirtualMemory::VirtualMemory(size_t size)
246 : address_(ReserveRegion(size)), size_(size) { }
249 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
250 : address_(NULL), size_(0) {
251 DCHECK((alignment % OS::AllocateAlignment()) == 0);
252 size_t request_size = RoundUp(size + alignment,
253 static_cast<intptr_t>(OS::AllocateAlignment()));
254 void* reservation = mmap(OS::GetRandomMmapAddr(),
257 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
260 if (reservation == MAP_FAILED) return;
262 uint8_t* base = static_cast<uint8_t*>(reservation);
263 uint8_t* aligned_base = RoundUp(base, alignment);
264 DCHECK_LE(base, aligned_base);
266 // Unmap extra memory reserved before and after the desired block.
267 if (aligned_base != base) {
268 size_t prefix_size = static_cast<size_t>(aligned_base - base);
269 OS::Free(base, prefix_size);
270 request_size -= prefix_size;
273 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
274 DCHECK_LE(aligned_size, request_size);
276 if (aligned_size != request_size) {
277 size_t suffix_size = request_size - aligned_size;
278 OS::Free(aligned_base + aligned_size, suffix_size);
279 request_size -= suffix_size;
282 DCHECK(aligned_size == request_size);
284 address_ = static_cast<void*>(aligned_base);
285 size_ = aligned_size;
286 #if defined(LEAK_SANITIZER)
287 __lsan_register_root_region(address_, size_);
292 VirtualMemory::~VirtualMemory() {
294 bool result = ReleaseRegion(address(), size());
301 bool VirtualMemory::IsReserved() {
302 return address_ != NULL;
306 void VirtualMemory::Reset() {
312 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
313 CHECK(InVM(address, size));
314 return CommitRegion(address, size, is_executable);
318 bool VirtualMemory::Uncommit(void* address, size_t size) {
319 CHECK(InVM(address, size));
320 return UncommitRegion(address, size);
324 bool VirtualMemory::Guard(void* address) {
325 CHECK(InVM(address, OS::CommitPageSize()));
326 OS::Guard(address, OS::CommitPageSize());
331 void* VirtualMemory::ReserveRegion(size_t size) {
332 void* result = mmap(OS::GetRandomMmapAddr(),
335 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
339 if (result == MAP_FAILED) return NULL;
341 #if defined(LEAK_SANITIZER)
342 __lsan_register_root_region(result, size);
348 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
350 // The Native Client port of V8 uses an interpreter,
351 // so code pages don't need PROT_EXEC.
352 int prot = PROT_READ | PROT_WRITE;
354 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
356 if (MAP_FAILED == mmap(base,
359 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
369 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
373 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED,
375 kMmapFdOffset) != MAP_FAILED;
379 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
380 #if defined(LEAK_SANITIZER)
381 __lsan_unregister_root_region(base, size);
383 return munmap(base, size) == 0;
387 bool VirtualMemory::HasLazyCommits() {
391 } } // namespace v8::base