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.
10 #include <semaphore.h>
13 #include <sys/resource.h>
15 #include <sys/types.h>
18 // QNX requires memory pages to be marked as executable.
19 // Otherwise, the OS raises an exception when executing code in that page.
21 #include <fcntl.h> // open
23 #include <strings.h> // index
24 #include <sys/mman.h> // mmap & munmap
25 #include <sys/procfs.h>
26 #include <sys/stat.h> // open
27 #include <sys/types.h> // mmap & munmap
28 #include <unistd.h> // sysconf
34 #include "src/base/macros.h"
35 #include "src/base/platform/platform.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) return NULL;
120 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
122 PosixMemoryMappedFile(FILE* file, void* memory, int size)
123 : file_(file), memory_(memory), size_(size) { }
124 virtual ~PosixMemoryMappedFile();
125 virtual void* memory() { return memory_; }
126 virtual int size() { return size_; }
134 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
135 FILE* file = fopen(name, "r+");
136 if (file == NULL) return NULL;
138 fseek(file, 0, SEEK_END);
139 int size = ftell(file);
142 mmap(OS::GetRandomMmapAddr(),
144 PROT_READ | PROT_WRITE,
148 return new PosixMemoryMappedFile(file, memory, size);
152 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
154 FILE* file = fopen(name, "w+");
155 if (file == NULL) return NULL;
156 int result = fwrite(initial, size, 1, file);
162 mmap(OS::GetRandomMmapAddr(),
164 PROT_READ | PROT_WRITE,
168 return new PosixMemoryMappedFile(file, memory, size);
172 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
173 if (memory_) OS::Free(memory_, size_);
178 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
179 std::vector<SharedLibraryAddress> result;
180 procfs_mapinfo *mapinfos = NULL, *mapinfo;
184 procfs_debuginfo info;
188 char buf[PATH_MAX + 1];
189 snprintf(buf, PATH_MAX + 1, "/proc/%d/as", getpid());
191 if ((proc_fd = open(buf, O_RDONLY)) == -1) {
196 /* Get the number of map entries. */
197 if (devctl(proc_fd, DCMD_PROC_MAPINFO, NULL, 0, &num) != EOK) {
202 mapinfos = reinterpret_cast<procfs_mapinfo *>(
203 malloc(num * sizeof(procfs_mapinfo)));
204 if (mapinfos == NULL) {
209 /* Fill the map entries. */
210 if (devctl(proc_fd, DCMD_PROC_PAGEDATA,
211 mapinfos, num * sizeof(procfs_mapinfo), &num) != EOK) {
217 for (i = 0; i < num; i++) {
218 mapinfo = mapinfos + i;
219 if (mapinfo->flags & MAP_ELF) {
220 map.info.vaddr = mapinfo->vaddr;
221 if (devctl(proc_fd, DCMD_PROC_MAPDEBUG, &map, sizeof(map), 0) != EOK) {
224 result.push_back(SharedLibraryAddress(
225 map.info.path, mapinfo->vaddr, mapinfo->vaddr + mapinfo->size));
234 void OS::SignalCodeMovingGC() {
238 // Constants used for mmap.
239 static const int kMmapFd = -1;
240 static const int kMmapFdOffset = 0;
243 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
246 VirtualMemory::VirtualMemory(size_t size)
247 : address_(ReserveRegion(size)), size_(size) { }
250 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
251 : address_(NULL), size_(0) {
252 DCHECK((alignment % OS::AllocateAlignment()) == 0);
253 size_t request_size = RoundUp(size + alignment,
254 static_cast<intptr_t>(OS::AllocateAlignment()));
255 void* reservation = mmap(OS::GetRandomMmapAddr(),
258 MAP_PRIVATE | MAP_ANONYMOUS | MAP_LAZY,
261 if (reservation == MAP_FAILED) return;
263 uint8_t* base = static_cast<uint8_t*>(reservation);
264 uint8_t* aligned_base = RoundUp(base, alignment);
265 DCHECK_LE(base, aligned_base);
267 // Unmap extra memory reserved before and after the desired block.
268 if (aligned_base != base) {
269 size_t prefix_size = static_cast<size_t>(aligned_base - base);
270 OS::Free(base, prefix_size);
271 request_size -= prefix_size;
274 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
275 DCHECK_LE(aligned_size, request_size);
277 if (aligned_size != request_size) {
278 size_t suffix_size = request_size - aligned_size;
279 OS::Free(aligned_base + aligned_size, suffix_size);
280 request_size -= suffix_size;
283 DCHECK(aligned_size == request_size);
285 address_ = static_cast<void*>(aligned_base);
286 size_ = aligned_size;
290 VirtualMemory::~VirtualMemory() {
292 bool result = ReleaseRegion(address(), size());
299 bool VirtualMemory::IsReserved() {
300 return address_ != NULL;
304 void VirtualMemory::Reset() {
310 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
311 return CommitRegion(address, size, is_executable);
315 bool VirtualMemory::Uncommit(void* address, size_t size) {
316 return UncommitRegion(address, size);
320 bool VirtualMemory::Guard(void* address) {
321 OS::Guard(address, OS::CommitPageSize());
326 void* VirtualMemory::ReserveRegion(size_t size) {
327 void* result = mmap(OS::GetRandomMmapAddr(),
330 MAP_PRIVATE | MAP_ANONYMOUS | MAP_LAZY,
334 if (result == MAP_FAILED) return NULL;
340 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
341 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
342 if (MAP_FAILED == mmap(base,
345 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
355 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
359 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | MAP_LAZY,
361 kMmapFdOffset) != MAP_FAILED;
365 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
366 return munmap(base, size) == 0;
370 bool VirtualMemory::HasLazyCommits() {
374 } } // namespace v8::base