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.
12 #include <sys/resource.h>
14 #include <sys/types.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);
112 if (NULL == t) 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);
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 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
147 PosixMemoryMappedFile(FILE* file, void* memory, int size)
148 : file_(file), memory_(memory), size_(size) { }
149 virtual ~PosixMemoryMappedFile();
150 virtual void* memory() { return memory_; }
151 virtual int size() { return size_; }
159 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
160 FILE* file = fopen(name, "r+");
161 if (file == NULL) return NULL;
163 fseek(file, 0, SEEK_END);
164 int size = ftell(file);
167 mmap(OS::GetRandomMmapAddr(),
169 PROT_READ | PROT_WRITE,
173 return new PosixMemoryMappedFile(file, memory, size);
177 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
179 FILE* file = fopen(name, "w+");
180 if (file == NULL) return NULL;
181 int result = fwrite(initial, size, 1, file);
187 mmap(OS::GetRandomMmapAddr(),
189 PROT_READ | PROT_WRITE,
193 return new PosixMemoryMappedFile(file, memory, size);
197 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
198 if (memory_) OS::Free(memory_, size_);
203 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
204 std::vector<SharedLibraryAddress> result;
205 // This function assumes that the layout of the file is as follows:
206 // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
207 // If we encounter an unexpected situation we abort scanning further entries.
208 FILE* fp = fopen("/proc/self/maps", "r");
209 if (fp == NULL) return result;
211 // Allocate enough room to be able to store a full file name.
212 const int kLibNameLen = FILENAME_MAX + 1;
213 char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
215 // This loop will terminate once the scanning hits an EOF.
217 uintptr_t start, end;
218 char attr_r, attr_w, attr_x, attr_p;
219 // Parse the addresses and permission bits at the beginning of the line.
220 if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
221 if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
224 if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
225 // Found a read-only executable entry. Skip characters until we reach
226 // the beginning of the filename or the end of the line.
229 } while ((c != EOF) && (c != '\n') && (c != '/') && (c != '['));
230 if (c == EOF) break; // EOF: Was unexpected, just exit.
232 // Process the filename if found.
233 if ((c == '/') || (c == '[')) {
234 // Push the '/' or '[' back into the stream to be read below.
237 // Read to the end of the line. Exit if the read fails.
238 if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
240 // Drop the newline character read by fgets. We do not need to check
241 // for a zero-length string because we know that we at least read the
242 // '/' or '[' character.
243 lib_name[strlen(lib_name) - 1] = '\0';
245 // No library name found, just record the raw address range.
246 snprintf(lib_name, kLibNameLen,
247 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
249 result.push_back(SharedLibraryAddress(lib_name, start, end));
251 // Entry not describing executable data. Skip to end of line to set up
252 // reading the next entry.
255 } while ((c != EOF) && (c != '\n'));
265 void OS::SignalCodeMovingGC() {
266 // Support for ll_prof.py.
268 // The Linux profiler built into the kernel logs all mmap's with
269 // PROT_EXEC so that analysis tools can properly attribute ticks. We
270 // do a mmap with a name known by ll_prof.py and immediately munmap
271 // it. This injects a GC marker into the stream of events generated
272 // by the kernel and allows us to synchronize V8 code log and the
274 int size = sysconf(_SC_PAGESIZE);
275 FILE* f = fopen(OS::GetGCFakeMMapFile(), "w+");
277 OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile());
280 void* addr = mmap(OS::GetRandomMmapAddr(), size,
282 // The Native Client port of V8 uses an interpreter,
283 // so code pages don't need PROT_EXEC.
286 PROT_READ | PROT_EXEC,
288 MAP_PRIVATE, fileno(f), 0);
289 DCHECK(addr != MAP_FAILED);
290 OS::Free(addr, size);
295 // Constants used for mmap.
296 static const int kMmapFd = -1;
297 static const int kMmapFdOffset = 0;
300 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
303 VirtualMemory::VirtualMemory(size_t size)
304 : address_(ReserveRegion(size)), size_(size) { }
307 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
308 : address_(NULL), size_(0) {
309 DCHECK((alignment % OS::AllocateAlignment()) == 0);
310 size_t request_size = RoundUp(size + alignment,
311 static_cast<intptr_t>(OS::AllocateAlignment()));
312 void* reservation = mmap(OS::GetRandomMmapAddr(),
315 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
318 if (reservation == MAP_FAILED) return;
320 uint8_t* base = static_cast<uint8_t*>(reservation);
321 uint8_t* aligned_base = RoundUp(base, alignment);
322 DCHECK_LE(base, aligned_base);
324 // Unmap extra memory reserved before and after the desired block.
325 if (aligned_base != base) {
326 size_t prefix_size = static_cast<size_t>(aligned_base - base);
327 OS::Free(base, prefix_size);
328 request_size -= prefix_size;
331 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
332 DCHECK_LE(aligned_size, request_size);
334 if (aligned_size != request_size) {
335 size_t suffix_size = request_size - aligned_size;
336 OS::Free(aligned_base + aligned_size, suffix_size);
337 request_size -= suffix_size;
340 DCHECK(aligned_size == request_size);
342 address_ = static_cast<void*>(aligned_base);
343 size_ = aligned_size;
344 #if defined(LEAK_SANITIZER)
345 __lsan_register_root_region(address_, size_);
350 VirtualMemory::~VirtualMemory() {
352 bool result = ReleaseRegion(address(), size());
359 bool VirtualMemory::IsReserved() {
360 return address_ != NULL;
364 void VirtualMemory::Reset() {
370 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
371 return CommitRegion(address, size, is_executable);
375 bool VirtualMemory::Uncommit(void* address, size_t size) {
376 return UncommitRegion(address, size);
380 bool VirtualMemory::Guard(void* address) {
381 OS::Guard(address, OS::CommitPageSize());
386 void* VirtualMemory::ReserveRegion(size_t size) {
387 void* result = mmap(OS::GetRandomMmapAddr(),
390 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
394 if (result == MAP_FAILED) return NULL;
396 #if defined(LEAK_SANITIZER)
397 __lsan_register_root_region(result, size);
403 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
405 // The Native Client port of V8 uses an interpreter,
406 // so code pages don't need PROT_EXEC.
407 int prot = PROT_READ | PROT_WRITE;
409 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
411 if (MAP_FAILED == mmap(base,
414 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
424 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
428 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED,
430 kMmapFdOffset) != MAP_FAILED;
434 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
435 #if defined(LEAK_SANITIZER)
436 __lsan_unregister_root_region(base, size);
438 return munmap(base, size) == 0;
442 bool VirtualMemory::HasLazyCommits() {
446 } } // namespace v8::base