| // Copyright 2012 the V8 project authors. All rights reserved. |
| // Redistribution and use in source and binary forms, with or without |
| // modification, are permitted provided that the following conditions are |
| // met: |
| // |
| // * Redistributions of source code must retain the above copyright |
| // notice, this list of conditions and the following disclaimer. |
| // * Redistributions in binary form must reproduce the above |
| // copyright notice, this list of conditions and the following |
| // disclaimer in the documentation and/or other materials provided |
| // with the distribution. |
| // * Neither the name of Google Inc. nor the names of its |
| // contributors may be used to endorse or promote products derived |
| // from this software without specific prior written permission. |
| // |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| // Platform specific code for Linux goes here. For the POSIX comaptible parts |
| // the implementation is in platform-posix.cc. |
| |
| #include <pthread.h> |
| #include <semaphore.h> |
| #include <signal.h> |
| #include <sys/prctl.h> |
| #include <sys/time.h> |
| #include <sys/resource.h> |
| #include <sys/syscall.h> |
| #include <sys/types.h> |
| #include <stdlib.h> |
| |
| #if defined(__GLIBC__) && !defined(__UCLIBC__) |
| #include <execinfo.h> |
| #include <cxxabi.h> |
| #endif |
| |
| // Ubuntu Dapper requires memory pages to be marked as |
| // executable. Otherwise, OS raises an exception when executing code |
| // in that page. |
| #include <sys/types.h> // mmap & munmap |
| #include <sys/mman.h> // mmap & munmap |
| #include <sys/stat.h> // open |
| #include <fcntl.h> // open |
| #include <unistd.h> // sysconf |
| #include <strings.h> // index |
| #include <errno.h> |
| #include <stdarg.h> |
| |
| // GLibc on ARM defines mcontext_t has a typedef for 'struct sigcontext'. |
| // Old versions of the C library <signal.h> didn't define the type. |
| #if defined(__ANDROID__) && !defined(__BIONIC_HAVE_UCONTEXT_T) && \ |
| defined(__arm__) && !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT) |
| #include <asm/sigcontext.h> |
| #endif |
| |
| #undef MAP_TYPE |
| |
| #include "v8.h" |
| |
| #include "platform-posix.h" |
| #include "platform.h" |
| #include "v8threads.h" |
| #include "vm-state-inl.h" |
| |
| |
| namespace v8 { |
| namespace internal { |
| |
| |
| double ceiling(double x) { |
| return ceil(x); |
| } |
| |
| |
| static Mutex* limit_mutex = NULL; |
| |
| |
| uint64_t OS::CpuFeaturesImpliedByPlatform() { |
| return 0; // Linux runs on anything. |
| } |
| |
| |
| #ifdef __arm__ |
| static bool CPUInfoContainsString(const char * search_string) { |
| const char* file_name = "/proc/cpuinfo"; |
| // This is written as a straight shot one pass parser |
| // and not using STL string and ifstream because, |
| // on Linux, it's reading from a (non-mmap-able) |
| // character special device. |
| FILE* f = NULL; |
| const char* what = search_string; |
| |
| if (NULL == (f = fopen(file_name, "r"))) { |
| OS::PrintError("Failed to open /proc/cpuinfo\n"); |
| return false; |
| } |
| |
| int k; |
| while (EOF != (k = fgetc(f))) { |
| if (k == *what) { |
| ++what; |
| while ((*what != '\0') && (*what == fgetc(f))) { |
| ++what; |
| } |
| if (*what == '\0') { |
| fclose(f); |
| return true; |
| } else { |
| what = search_string; |
| } |
| } |
| } |
| fclose(f); |
| |
| // Did not find string in the proc file. |
| return false; |
| } |
| |
| |
| bool OS::ArmCpuHasFeature(CpuFeature feature) { |
| const char* search_string = NULL; |
| // Simple detection of VFP at runtime for Linux. |
| // It is based on /proc/cpuinfo, which reveals hardware configuration |
| // to user-space applications. According to ARM (mid 2009), no similar |
| // facility is universally available on the ARM architectures, |
| // so it's up to individual OSes to provide such. |
| switch (feature) { |
| case VFP3: |
| search_string = "vfpv3"; |
| break; |
| case NEON: |
| search_string = "neon"; |
| break; |
| case ARMv7: |
| search_string = "ARMv7"; |
| break; |
| case SUDIV: |
| search_string = "idiva"; |
| break; |
| case VFP32DREGS: |
| // This case is handled specially below. |
| break; |
| default: |
| UNREACHABLE(); |
| } |
| |
| if (feature == VFP32DREGS) { |
| return ArmCpuHasFeature(VFP3) && !CPUInfoContainsString("d16"); |
| } |
| |
| if (CPUInfoContainsString(search_string)) { |
| return true; |
| } |
| |
| if (feature == VFP3) { |
| // Some old kernels will report vfp not vfpv3. Here we make a last attempt |
| // to detect vfpv3 by checking for vfp *and* neon, since neon is only |
| // available on architectures with vfpv3. |
| // Checking neon on its own is not enough as it is possible to have neon |
| // without vfp. |
| if (CPUInfoContainsString("vfp") && CPUInfoContainsString("neon")) { |
| return true; |
| } |
| } |
| |
| return false; |
| } |
| |
| |
| CpuImplementer OS::GetCpuImplementer() { |
| static bool use_cached_value = false; |
| static CpuImplementer cached_value = UNKNOWN_IMPLEMENTER; |
| if (use_cached_value) { |
| return cached_value; |
| } |
| if (CPUInfoContainsString("CPU implementer\t: 0x41")) { |
| cached_value = ARM_IMPLEMENTER; |
| } else if (CPUInfoContainsString("CPU implementer\t: 0x51")) { |
| cached_value = QUALCOMM_IMPLEMENTER; |
| } else { |
| cached_value = UNKNOWN_IMPLEMENTER; |
| } |
| use_cached_value = true; |
| return cached_value; |
| } |
| |
| |
| CpuPart OS::GetCpuPart(CpuImplementer implementer) { |
| static bool use_cached_value = false; |
| static CpuPart cached_value = CPU_UNKNOWN; |
| if (use_cached_value) { |
| return cached_value; |
| } |
| if (implementer == ARM_IMPLEMENTER) { |
| if (CPUInfoContainsString("CPU part\t: 0xc0f")) { |
| cached_value = CORTEX_A15; |
| } else if (CPUInfoContainsString("CPU part\t: 0xc0c")) { |
| cached_value = CORTEX_A12; |
| } else if (CPUInfoContainsString("CPU part\t: 0xc09")) { |
| cached_value = CORTEX_A9; |
| } else if (CPUInfoContainsString("CPU part\t: 0xc08")) { |
| cached_value = CORTEX_A8; |
| } else if (CPUInfoContainsString("CPU part\t: 0xc07")) { |
| cached_value = CORTEX_A7; |
| } else if (CPUInfoContainsString("CPU part\t: 0xc05")) { |
| cached_value = CORTEX_A5; |
| } else { |
| cached_value = CPU_UNKNOWN; |
| } |
| } else { |
| cached_value = CPU_UNKNOWN; |
| } |
| use_cached_value = true; |
| return cached_value; |
| } |
| |
| |
| bool OS::ArmUsingHardFloat() { |
| // GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify |
| // the Floating Point ABI used (PCS stands for Procedure Call Standard). |
| // We use these as well as a couple of other defines to statically determine |
| // what FP ABI used. |
| // GCC versions 4.4 and below don't support hard-fp. |
| // GCC versions 4.5 may support hard-fp without defining __ARM_PCS or |
| // __ARM_PCS_VFP. |
| |
| #define GCC_VERSION (__GNUC__ * 10000 \ |
| + __GNUC_MINOR__ * 100 \ |
| + __GNUC_PATCHLEVEL__) |
| #if GCC_VERSION >= 40600 |
| #if defined(__ARM_PCS_VFP) |
| return true; |
| #else |
| return false; |
| #endif |
| |
| #elif GCC_VERSION < 40500 |
| return false; |
| |
| #else |
| #if defined(__ARM_PCS_VFP) |
| return true; |
| #elif defined(__ARM_PCS) || defined(__SOFTFP) || !defined(__VFP_FP__) |
| return false; |
| #else |
| #error "Your version of GCC does not report the FP ABI compiled for." \ |
| "Please report it on this issue" \ |
| "http://code.google.com/p/v8/issues/detail?id=2140" |
| |
| #endif |
| #endif |
| #undef GCC_VERSION |
| } |
| |
| #endif // def __arm__ |
| |
| |
| #ifdef __mips__ |
| bool OS::MipsCpuHasFeature(CpuFeature feature) { |
| const char* search_string = NULL; |
| const char* file_name = "/proc/cpuinfo"; |
| // Simple detection of FPU at runtime for Linux. |
| // It is based on /proc/cpuinfo, which reveals hardware configuration |
| // to user-space applications. According to MIPS (early 2010), no similar |
| // facility is universally available on the MIPS architectures, |
| // so it's up to individual OSes to provide such. |
| // |
| // This is written as a straight shot one pass parser |
| // and not using STL string and ifstream because, |
| // on Linux, it's reading from a (non-mmap-able) |
| // character special device. |
| |
| switch (feature) { |
| case FPU: |
| search_string = "FPU"; |
| break; |
| default: |
| UNREACHABLE(); |
| } |
| |
| FILE* f = NULL; |
| const char* what = search_string; |
| |
| if (NULL == (f = fopen(file_name, "r"))) { |
| OS::PrintError("Failed to open /proc/cpuinfo\n"); |
| return false; |
| } |
| |
| int k; |
| while (EOF != (k = fgetc(f))) { |
| if (k == *what) { |
| ++what; |
| while ((*what != '\0') && (*what == fgetc(f))) { |
| ++what; |
| } |
| if (*what == '\0') { |
| fclose(f); |
| return true; |
| } else { |
| what = search_string; |
| } |
| } |
| } |
| fclose(f); |
| |
| // Did not find string in the proc file. |
| return false; |
| } |
| #endif // def __mips__ |
| |
| |
| int OS::ActivationFrameAlignment() { |
| #if V8_TARGET_ARCH_ARM |
| // On EABI ARM targets this is required for fp correctness in the |
| // runtime system. |
| return 8; |
| #elif V8_TARGET_ARCH_MIPS |
| return 8; |
| #endif |
| // With gcc 4.4 the tree vectorization optimizer can generate code |
| // that requires 16 byte alignment such as movdqa on x86. |
| return 16; |
| } |
| |
| |
| const char* OS::LocalTimezone(double time) { |
| if (std::isnan(time)) return ""; |
| time_t tv = static_cast<time_t>(floor(time/msPerSecond)); |
| struct tm* t = localtime(&tv); |
| if (NULL == t) return ""; |
| return t->tm_zone; |
| } |
| |
| |
| double OS::LocalTimeOffset() { |
| time_t tv = time(NULL); |
| struct tm* t = localtime(&tv); |
| // tm_gmtoff includes any daylight savings offset, so subtract it. |
| return static_cast<double>(t->tm_gmtoff * msPerSecond - |
| (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); |
| } |
| |
| |
| // We keep the lowest and highest addresses mapped as a quick way of |
| // determining that pointers are outside the heap (used mostly in assertions |
| // and verification). The estimate is conservative, i.e., not all addresses in |
| // 'allocated' space are actually allocated to our heap. The range is |
| // [lowest, highest), inclusive on the low and and exclusive on the high end. |
| static void* lowest_ever_allocated = reinterpret_cast<void*>(-1); |
| static void* highest_ever_allocated = reinterpret_cast<void*>(0); |
| |
| |
| static void UpdateAllocatedSpaceLimits(void* address, int size) { |
| ASSERT(limit_mutex != NULL); |
| ScopedLock lock(limit_mutex); |
| |
| lowest_ever_allocated = Min(lowest_ever_allocated, address); |
| highest_ever_allocated = |
| Max(highest_ever_allocated, |
| reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size)); |
| } |
| |
| |
| bool OS::IsOutsideAllocatedSpace(void* address) { |
| return address < lowest_ever_allocated || address >= highest_ever_allocated; |
| } |
| |
| |
| void* OS::Allocate(const size_t requested, |
| size_t* allocated, |
| bool is_executable) { |
| const size_t msize = RoundUp(requested, AllocateAlignment()); |
| int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| void* addr = OS::GetRandomMmapAddr(); |
| void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| if (mbase == MAP_FAILED) { |
| LOG(i::Isolate::Current(), |
| StringEvent("OS::Allocate", "mmap failed")); |
| return NULL; |
| } |
| *allocated = msize; |
| UpdateAllocatedSpaceLimits(mbase, msize); |
| return mbase; |
| } |
| |
| |
| void OS::DumpBacktrace() { |
| // backtrace is a glibc extension. |
| #if defined(__GLIBC__) && !defined(__UCLIBC__) |
| POSIXBacktraceHelper<backtrace, backtrace_symbols>::DumpBacktrace(); |
| #endif |
| } |
| |
| |
| class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
| public: |
| PosixMemoryMappedFile(FILE* file, void* memory, int size) |
| : file_(file), memory_(memory), size_(size) { } |
| virtual ~PosixMemoryMappedFile(); |
| virtual void* memory() { return memory_; } |
| virtual int size() { return size_; } |
| private: |
| FILE* file_; |
| void* memory_; |
| int size_; |
| }; |
| |
| |
| OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { |
| FILE* file = fopen(name, "r+"); |
| if (file == NULL) return NULL; |
| |
| fseek(file, 0, SEEK_END); |
| int size = ftell(file); |
| |
| void* memory = |
| mmap(OS::GetRandomMmapAddr(), |
| size, |
| PROT_READ | PROT_WRITE, |
| MAP_SHARED, |
| fileno(file), |
| 0); |
| return new PosixMemoryMappedFile(file, memory, size); |
| } |
| |
| |
| OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| void* initial) { |
| FILE* file = fopen(name, "w+"); |
| if (file == NULL) return NULL; |
| int result = fwrite(initial, size, 1, file); |
| if (result < 1) { |
| fclose(file); |
| return NULL; |
| } |
| void* memory = |
| mmap(OS::GetRandomMmapAddr(), |
| size, |
| PROT_READ | PROT_WRITE, |
| MAP_SHARED, |
| fileno(file), |
| 0); |
| return new PosixMemoryMappedFile(file, memory, size); |
| } |
| |
| |
| PosixMemoryMappedFile::~PosixMemoryMappedFile() { |
| if (memory_) OS::Free(memory_, size_); |
| fclose(file_); |
| } |
| |
| |
| void OS::LogSharedLibraryAddresses() { |
| // This function assumes that the layout of the file is as follows: |
| // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name] |
| // If we encounter an unexpected situation we abort scanning further entries. |
| FILE* fp = fopen("/proc/self/maps", "r"); |
| if (fp == NULL) return; |
| |
| // Allocate enough room to be able to store a full file name. |
| const int kLibNameLen = FILENAME_MAX + 1; |
| char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen)); |
| |
| i::Isolate* isolate = ISOLATE; |
| // This loop will terminate once the scanning hits an EOF. |
| while (true) { |
| uintptr_t start, end; |
| char attr_r, attr_w, attr_x, attr_p; |
| // Parse the addresses and permission bits at the beginning of the line. |
| if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break; |
| if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break; |
| |
| int c; |
| if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') { |
| // Found a read-only executable entry. Skip characters until we reach |
| // the beginning of the filename or the end of the line. |
| do { |
| c = getc(fp); |
| } while ((c != EOF) && (c != '\n') && (c != '/') && (c != '[')); |
| if (c == EOF) break; // EOF: Was unexpected, just exit. |
| |
| // Process the filename if found. |
| if ((c == '/') || (c == '[')) { |
| // Push the '/' or '[' back into the stream to be read below. |
| ungetc(c, fp); |
| |
| // Read to the end of the line. Exit if the read fails. |
| if (fgets(lib_name, kLibNameLen, fp) == NULL) break; |
| |
| // Drop the newline character read by fgets. We do not need to check |
| // for a zero-length string because we know that we at least read the |
| // '/' or '[' character. |
| lib_name[strlen(lib_name) - 1] = '\0'; |
| } else { |
| // No library name found, just record the raw address range. |
| snprintf(lib_name, kLibNameLen, |
| "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end); |
| } |
| LOG(isolate, SharedLibraryEvent(lib_name, start, end)); |
| } else { |
| // Entry not describing executable data. Skip to end of line to set up |
| // reading the next entry. |
| do { |
| c = getc(fp); |
| } while ((c != EOF) && (c != '\n')); |
| if (c == EOF) break; |
| } |
| } |
| free(lib_name); |
| fclose(fp); |
| } |
| |
| |
| void OS::SignalCodeMovingGC() { |
| // Support for ll_prof.py. |
| // |
| // The Linux profiler built into the kernel logs all mmap's with |
| // PROT_EXEC so that analysis tools can properly attribute ticks. We |
| // do a mmap with a name known by ll_prof.py and immediately munmap |
| // it. This injects a GC marker into the stream of events generated |
| // by the kernel and allows us to synchronize V8 code log and the |
| // kernel log. |
| int size = sysconf(_SC_PAGESIZE); |
| FILE* f = fopen(FLAG_gc_fake_mmap, "w+"); |
| if (f == NULL) { |
| OS::PrintError("Failed to open %s\n", FLAG_gc_fake_mmap); |
| OS::Abort(); |
| } |
| void* addr = mmap(OS::GetRandomMmapAddr(), |
| size, |
| #if defined(__native_client__) |
| // The Native Client port of V8 uses an interpreter, |
| // so code pages don't need PROT_EXEC. |
| PROT_READ, |
| #else |
| PROT_READ | PROT_EXEC, |
| #endif |
| MAP_PRIVATE, |
| fileno(f), |
| 0); |
| ASSERT(addr != MAP_FAILED); |
| OS::Free(addr, size); |
| fclose(f); |
| } |
| |
| |
| int OS::StackWalk(Vector<OS::StackFrame> frames) { |
| // backtrace is a glibc extension. |
| #if defined(__GLIBC__) && !defined(__UCLIBC__) |
| return POSIXBacktraceHelper<backtrace, backtrace_symbols>::StackWalk(frames); |
| #else |
| return 0; |
| #endif |
| } |
| |
| |
| // Constants used for mmap. |
| static const int kMmapFd = -1; |
| static const int kMmapFdOffset = 0; |
| |
| |
| VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } |
| |
| |
| VirtualMemory::VirtualMemory(size_t size) |
| : address_(ReserveRegion(size)), size_(size) { } |
| |
| |
| VirtualMemory::VirtualMemory(size_t size, size_t alignment) |
| : address_(NULL), size_(0) { |
| ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment()))); |
| size_t request_size = RoundUp(size + alignment, |
| static_cast<intptr_t>(OS::AllocateAlignment())); |
| void* reservation = mmap(OS::GetRandomMmapAddr(), |
| request_size, |
| PROT_NONE, |
| MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, |
| kMmapFd, |
| kMmapFdOffset); |
| if (reservation == MAP_FAILED) return; |
| |
| Address base = static_cast<Address>(reservation); |
| Address aligned_base = RoundUp(base, alignment); |
| ASSERT_LE(base, aligned_base); |
| |
| // Unmap extra memory reserved before and after the desired block. |
| if (aligned_base != base) { |
| size_t prefix_size = static_cast<size_t>(aligned_base - base); |
| OS::Free(base, prefix_size); |
| request_size -= prefix_size; |
| } |
| |
| size_t aligned_size = RoundUp(size, OS::AllocateAlignment()); |
| ASSERT_LE(aligned_size, request_size); |
| |
| if (aligned_size != request_size) { |
| size_t suffix_size = request_size - aligned_size; |
| OS::Free(aligned_base + aligned_size, suffix_size); |
| request_size -= suffix_size; |
| } |
| |
| ASSERT(aligned_size == request_size); |
| |
| address_ = static_cast<void*>(aligned_base); |
| size_ = aligned_size; |
| } |
| |
| |
| VirtualMemory::~VirtualMemory() { |
| if (IsReserved()) { |
| bool result = ReleaseRegion(address(), size()); |
| ASSERT(result); |
| USE(result); |
| } |
| } |
| |
| |
| bool VirtualMemory::IsReserved() { |
| return address_ != NULL; |
| } |
| |
| |
| void VirtualMemory::Reset() { |
| address_ = NULL; |
| size_ = 0; |
| } |
| |
| |
| bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
| return CommitRegion(address, size, is_executable); |
| } |
| |
| |
| bool VirtualMemory::Uncommit(void* address, size_t size) { |
| return UncommitRegion(address, size); |
| } |
| |
| |
| bool VirtualMemory::Guard(void* address) { |
| OS::Guard(address, OS::CommitPageSize()); |
| return true; |
| } |
| |
| |
| void* VirtualMemory::ReserveRegion(size_t size) { |
| void* result = mmap(OS::GetRandomMmapAddr(), |
| size, |
| PROT_NONE, |
| MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, |
| kMmapFd, |
| kMmapFdOffset); |
| |
| if (result == MAP_FAILED) return NULL; |
| |
| return result; |
| } |
| |
| |
| bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { |
| #if defined(__native_client__) |
| // The Native Client port of V8 uses an interpreter, |
| // so code pages don't need PROT_EXEC. |
| int prot = PROT_READ | PROT_WRITE; |
| #else |
| int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| #endif |
| if (MAP_FAILED == mmap(base, |
| size, |
| prot, |
| MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, |
| kMmapFd, |
| kMmapFdOffset)) { |
| return false; |
| } |
| |
| UpdateAllocatedSpaceLimits(base, size); |
| return true; |
| } |
| |
| |
| bool VirtualMemory::UncommitRegion(void* base, size_t size) { |
| return mmap(base, |
| size, |
| PROT_NONE, |
| MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED, |
| kMmapFd, |
| kMmapFdOffset) != MAP_FAILED; |
| } |
| |
| |
| bool VirtualMemory::ReleaseRegion(void* base, size_t size) { |
| return munmap(base, size) == 0; |
| } |
| |
| |
| bool VirtualMemory::HasLazyCommits() { |
| return true; |
| } |
| |
| |
| class LinuxSemaphore : public Semaphore { |
| public: |
| explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); } |
| virtual ~LinuxSemaphore() { sem_destroy(&sem_); } |
| |
| virtual void Wait(); |
| virtual bool Wait(int timeout); |
| virtual void Signal() { sem_post(&sem_); } |
| private: |
| sem_t sem_; |
| }; |
| |
| |
| void LinuxSemaphore::Wait() { |
| while (true) { |
| int result = sem_wait(&sem_); |
| if (result == 0) return; // Successfully got semaphore. |
| CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. |
| } |
| } |
| |
| |
| #ifndef TIMEVAL_TO_TIMESPEC |
| #define TIMEVAL_TO_TIMESPEC(tv, ts) do { \ |
| (ts)->tv_sec = (tv)->tv_sec; \ |
| (ts)->tv_nsec = (tv)->tv_usec * 1000; \ |
| } while (false) |
| #endif |
| |
| |
| bool LinuxSemaphore::Wait(int timeout) { |
| const long kOneSecondMicros = 1000000; // NOLINT |
| |
| // Split timeout into second and nanosecond parts. |
| struct timeval delta; |
| delta.tv_usec = timeout % kOneSecondMicros; |
| delta.tv_sec = timeout / kOneSecondMicros; |
| |
| struct timeval current_time; |
| // Get the current time. |
| if (gettimeofday(¤t_time, NULL) == -1) { |
| return false; |
| } |
| |
| // Calculate time for end of timeout. |
| struct timeval end_time; |
| timeradd(¤t_time, &delta, &end_time); |
| |
| struct timespec ts; |
| TIMEVAL_TO_TIMESPEC(&end_time, &ts); |
| // Wait for semaphore signalled or timeout. |
| while (true) { |
| int result = sem_timedwait(&sem_, &ts); |
| if (result == 0) return true; // Successfully got semaphore. |
| if (result > 0) { |
| // For glibc prior to 2.3.4 sem_timedwait returns the error instead of -1. |
| errno = result; |
| result = -1; |
| } |
| if (result == -1 && errno == ETIMEDOUT) return false; // Timeout. |
| CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. |
| } |
| } |
| |
| |
| Semaphore* OS::CreateSemaphore(int count) { |
| return new LinuxSemaphore(count); |
| } |
| |
| |
| void OS::SetUp() { |
| // Seed the random number generator. We preserve microsecond resolution. |
| uint64_t seed = Ticks() ^ (getpid() << 16); |
| srandom(static_cast<unsigned int>(seed)); |
| limit_mutex = CreateMutex(); |
| } |
| |
| |
| void OS::TearDown() { |
| delete limit_mutex; |
| } |
| |
| |
| } } // namespace v8::internal |