1 // Copyright 2013 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 QNX goes here. For the POSIX-compatible
6 // parts the implementation is in platform-posix.cc.
12 #include <sys/resource.h>
13 #include <sys/types.h>
16 #include <backtrace.h>
18 // QNX requires memory pages to be marked as executable.
19 // Otherwise, the OS raises an exception when executing code in that page.
20 #include <sys/types.h> // mmap & munmap
21 #include <sys/mman.h> // mmap & munmap
22 #include <sys/stat.h> // open
23 #include <fcntl.h> // open
24 #include <unistd.h> // sysconf
25 #include <strings.h> // index
28 #include <sys/procfs.h>
35 #include "v8threads.h"
41 // 0 is never a valid thread id on Qnx since tids and pids share a
42 // name space and pid 0 is reserved (see man 2 kill).
43 static const pthread_t kNoThread = (pthread_t) 0;
48 bool OS::ArmUsingHardFloat() {
49 // GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify
50 // the Floating Point ABI used (PCS stands for Procedure Call Standard).
51 // We use these as well as a couple of other defines to statically determine
53 // GCC versions 4.4 and below don't support hard-fp.
54 // GCC versions 4.5 may support hard-fp without defining __ARM_PCS or
57 #define GCC_VERSION (__GNUC__ * 10000 \
58 + __GNUC_MINOR__ * 100 \
59 + __GNUC_PATCHLEVEL__)
60 #if GCC_VERSION >= 40600
61 #if defined(__ARM_PCS_VFP)
67 #elif GCC_VERSION < 40500
71 #if defined(__ARM_PCS_VFP)
73 #elif defined(__ARM_PCS) || defined(__SOFTFP__) || defined(__SOFTFP) || \
77 #error "Your version of GCC does not report the FP ABI compiled for." \
78 "Please report it on this issue" \
79 "http://code.google.com/p/v8/issues/detail?id=2140"
89 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
90 if (std::isnan(time)) return "";
91 time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
92 struct tm* t = localtime(&tv);
93 if (NULL == t) return "";
98 double OS::LocalTimeOffset(TimezoneCache* cache) {
99 time_t tv = time(NULL);
100 struct tm* t = localtime(&tv);
101 // tm_gmtoff includes any daylight savings offset, so subtract it.
102 return static_cast<double>(t->tm_gmtoff * msPerSecond -
103 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
107 void* OS::Allocate(const size_t requested,
109 bool is_executable) {
110 const size_t msize = RoundUp(requested, AllocateAlignment());
111 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
112 void* addr = OS::GetRandomMmapAddr();
113 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
114 if (mbase == MAP_FAILED) {
115 LOG(i::Isolate::Current(),
116 StringEvent("OS::Allocate", "mmap failed"));
124 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
126 PosixMemoryMappedFile(FILE* file, void* memory, int size)
127 : file_(file), memory_(memory), size_(size) { }
128 virtual ~PosixMemoryMappedFile();
129 virtual void* memory() { return memory_; }
130 virtual int size() { return size_; }
138 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
139 FILE* file = fopen(name, "r+");
140 if (file == NULL) return NULL;
142 fseek(file, 0, SEEK_END);
143 int size = ftell(file);
146 mmap(OS::GetRandomMmapAddr(),
148 PROT_READ | PROT_WRITE,
152 return new PosixMemoryMappedFile(file, memory, size);
156 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
158 FILE* file = fopen(name, "w+");
159 if (file == NULL) return NULL;
160 int result = fwrite(initial, size, 1, file);
166 mmap(OS::GetRandomMmapAddr(),
168 PROT_READ | PROT_WRITE,
172 return new PosixMemoryMappedFile(file, memory, size);
176 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
177 if (memory_) OS::Free(memory_, size_);
182 void OS::LogSharedLibraryAddresses(Isolate* isolate) {
183 procfs_mapinfo *mapinfos = NULL, *mapinfo;
187 procfs_debuginfo info;
191 char buf[PATH_MAX + 1];
192 snprintf(buf, PATH_MAX + 1, "/proc/%d/as", getpid());
194 if ((proc_fd = open(buf, O_RDONLY)) == -1) {
199 /* Get the number of map entries. */
200 if (devctl(proc_fd, DCMD_PROC_MAPINFO, NULL, 0, &num) != EOK) {
205 mapinfos = reinterpret_cast<procfs_mapinfo *>(
206 malloc(num * sizeof(procfs_mapinfo)));
207 if (mapinfos == NULL) {
212 /* Fill the map entries. */
213 if (devctl(proc_fd, DCMD_PROC_PAGEDATA,
214 mapinfos, num * sizeof(procfs_mapinfo), &num) != EOK) {
220 for (i = 0; i < num; i++) {
221 mapinfo = mapinfos + i;
222 if (mapinfo->flags & MAP_ELF) {
223 map.info.vaddr = mapinfo->vaddr;
224 if (devctl(proc_fd, DCMD_PROC_MAPDEBUG, &map, sizeof(map), 0) != EOK) {
227 LOG(isolate, SharedLibraryEvent(map.info.path,
229 mapinfo->vaddr + mapinfo->size));
237 void OS::SignalCodeMovingGC() {
241 // Constants used for mmap.
242 static const int kMmapFd = -1;
243 static const int kMmapFdOffset = 0;
246 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
249 VirtualMemory::VirtualMemory(size_t size)
250 : address_(ReserveRegion(size)), size_(size) { }
253 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
254 : address_(NULL), size_(0) {
255 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
256 size_t request_size = RoundUp(size + alignment,
257 static_cast<intptr_t>(OS::AllocateAlignment()));
258 void* reservation = mmap(OS::GetRandomMmapAddr(),
261 MAP_PRIVATE | MAP_ANONYMOUS | MAP_LAZY,
264 if (reservation == MAP_FAILED) return;
266 Address base = static_cast<Address>(reservation);
267 Address aligned_base = RoundUp(base, alignment);
268 ASSERT_LE(base, aligned_base);
270 // Unmap extra memory reserved before and after the desired block.
271 if (aligned_base != base) {
272 size_t prefix_size = static_cast<size_t>(aligned_base - base);
273 OS::Free(base, prefix_size);
274 request_size -= prefix_size;
277 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
278 ASSERT_LE(aligned_size, request_size);
280 if (aligned_size != request_size) {
281 size_t suffix_size = request_size - aligned_size;
282 OS::Free(aligned_base + aligned_size, suffix_size);
283 request_size -= suffix_size;
286 ASSERT(aligned_size == request_size);
288 address_ = static_cast<void*>(aligned_base);
289 size_ = aligned_size;
293 VirtualMemory::~VirtualMemory() {
295 bool result = ReleaseRegion(address(), size());
302 bool VirtualMemory::IsReserved() {
303 return address_ != NULL;
307 void VirtualMemory::Reset() {
313 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
314 return CommitRegion(address, size, is_executable);
318 bool VirtualMemory::Uncommit(void* address, size_t size) {
319 return UncommitRegion(address, size);
323 bool VirtualMemory::Guard(void* address) {
324 OS::Guard(address, OS::CommitPageSize());
329 void* VirtualMemory::ReserveRegion(size_t size) {
330 void* result = mmap(OS::GetRandomMmapAddr(),
333 MAP_PRIVATE | MAP_ANONYMOUS | MAP_LAZY,
337 if (result == MAP_FAILED) return NULL;
343 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
344 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
345 if (MAP_FAILED == mmap(base,
348 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
358 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
362 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | MAP_LAZY,
364 kMmapFdOffset) != MAP_FAILED;
368 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
369 return munmap(base, size) == 0;
373 bool VirtualMemory::HasLazyCommits() {
377 } } // namespace v8::internal