Home | History | Annotate | Download | only in src
      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.
      4 
      5 // Platform-specific code for QNX goes here. For the POSIX-compatible
      6 // parts the implementation is in platform-posix.cc.
      7 
      8 #include <pthread.h>
      9 #include <semaphore.h>
     10 #include <signal.h>
     11 #include <sys/time.h>
     12 #include <sys/resource.h>
     13 #include <sys/types.h>
     14 #include <stdlib.h>
     15 #include <ucontext.h>
     16 #include <backtrace.h>
     17 
     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
     26 #include <errno.h>
     27 #include <stdarg.h>
     28 #include <sys/procfs.h>
     29 
     30 #undef MAP_TYPE
     31 
     32 #include "src/v8.h"
     33 
     34 #include "src/platform.h"
     35 
     36 
     37 namespace v8 {
     38 namespace internal {
     39 
     40 // 0 is never a valid thread id on Qnx since tids and pids share a
     41 // name space and pid 0 is reserved (see man 2 kill).
     42 static const pthread_t kNoThread = (pthread_t) 0;
     43 
     44 
     45 #ifdef __arm__
     46 
     47 bool OS::ArmUsingHardFloat() {
     48   // GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify
     49   // the Floating Point ABI used (PCS stands for Procedure Call Standard).
     50   // We use these as well as a couple of other defines to statically determine
     51   // what FP ABI used.
     52   // GCC versions 4.4 and below don't support hard-fp.
     53   // GCC versions 4.5 may support hard-fp without defining __ARM_PCS or
     54   // __ARM_PCS_VFP.
     55 
     56 #define GCC_VERSION (__GNUC__ * 10000                                          \
     57                      + __GNUC_MINOR__ * 100                                    \
     58                      + __GNUC_PATCHLEVEL__)
     59 #if GCC_VERSION >= 40600
     60 #if defined(__ARM_PCS_VFP)
     61   return true;
     62 #else
     63   return false;
     64 #endif
     65 
     66 #elif GCC_VERSION < 40500
     67   return false;
     68 
     69 #else
     70 #if defined(__ARM_PCS_VFP)
     71   return true;
     72 #elif defined(__ARM_PCS) || defined(__SOFTFP__) || defined(__SOFTFP) || \
     73       !defined(__VFP_FP__)
     74   return false;
     75 #else
     76 #error "Your version of GCC does not report the FP ABI compiled for."          \
     77        "Please report it on this issue"                                        \
     78        "http://code.google.com/p/v8/issues/detail?id=2140"
     79 
     80 #endif
     81 #endif
     82 #undef GCC_VERSION
     83 }
     84 
     85 #endif  // __arm__
     86 
     87 
     88 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
     89   if (std::isnan(time)) return "";
     90   time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
     91   struct tm* t = localtime(&tv);
     92   if (NULL == t) return "";
     93   return t->tm_zone;
     94 }
     95 
     96 
     97 double OS::LocalTimeOffset(TimezoneCache* cache) {
     98   time_t tv = time(NULL);
     99   struct tm* t = localtime(&tv);
    100   // tm_gmtoff includes any daylight savings offset, so subtract it.
    101   return static_cast<double>(t->tm_gmtoff * msPerSecond -
    102                              (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
    103 }
    104 
    105 
    106 void* OS::Allocate(const size_t requested,
    107                    size_t* allocated,
    108                    bool is_executable) {
    109   const size_t msize = RoundUp(requested, AllocateAlignment());
    110   int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
    111   void* addr = OS::GetRandomMmapAddr();
    112   void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    113   if (mbase == MAP_FAILED) return NULL;
    114   *allocated = msize;
    115   return mbase;
    116 }
    117 
    118 
    119 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
    120  public:
    121   PosixMemoryMappedFile(FILE* file, void* memory, int size)
    122     : file_(file), memory_(memory), size_(size) { }
    123   virtual ~PosixMemoryMappedFile();
    124   virtual void* memory() { return memory_; }
    125   virtual int size() { return size_; }
    126  private:
    127   FILE* file_;
    128   void* memory_;
    129   int size_;
    130 };
    131 
    132 
    133 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
    134   FILE* file = fopen(name, "r+");
    135   if (file == NULL) return NULL;
    136 
    137   fseek(file, 0, SEEK_END);
    138   int size = ftell(file);
    139 
    140   void* memory =
    141       mmap(OS::GetRandomMmapAddr(),
    142            size,
    143            PROT_READ | PROT_WRITE,
    144            MAP_SHARED,
    145            fileno(file),
    146            0);
    147   return new PosixMemoryMappedFile(file, memory, size);
    148 }
    149 
    150 
    151 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
    152     void* initial) {
    153   FILE* file = fopen(name, "w+");
    154   if (file == NULL) return NULL;
    155   int result = fwrite(initial, size, 1, file);
    156   if (result < 1) {
    157     fclose(file);
    158     return NULL;
    159   }
    160   void* memory =
    161       mmap(OS::GetRandomMmapAddr(),
    162            size,
    163            PROT_READ | PROT_WRITE,
    164            MAP_SHARED,
    165            fileno(file),
    166            0);
    167   return new PosixMemoryMappedFile(file, memory, size);
    168 }
    169 
    170 
    171 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
    172   if (memory_) OS::Free(memory_, size_);
    173   fclose(file_);
    174 }
    175 
    176 
    177 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
    178   std::vector<SharedLibraryAddress> result;
    179   procfs_mapinfo *mapinfos = NULL, *mapinfo;
    180   int proc_fd, num, i;
    181 
    182   struct {
    183     procfs_debuginfo info;
    184     char buff[PATH_MAX];
    185   } map;
    186 
    187   char buf[PATH_MAX + 1];
    188   snprintf(buf, PATH_MAX + 1, "/proc/%d/as", getpid());
    189 
    190   if ((proc_fd = open(buf, O_RDONLY)) == -1) {
    191     close(proc_fd);
    192     return result;
    193   }
    194 
    195   /* Get the number of map entries.  */
    196   if (devctl(proc_fd, DCMD_PROC_MAPINFO, NULL, 0, &num) != EOK) {
    197     close(proc_fd);
    198     return result;
    199   }
    200 
    201   mapinfos = reinterpret_cast<procfs_mapinfo *>(
    202       malloc(num * sizeof(procfs_mapinfo)));
    203   if (mapinfos == NULL) {
    204     close(proc_fd);
    205     return result;
    206   }
    207 
    208   /* Fill the map entries.  */
    209   if (devctl(proc_fd, DCMD_PROC_PAGEDATA,
    210       mapinfos, num * sizeof(procfs_mapinfo), &num) != EOK) {
    211     free(mapinfos);
    212     close(proc_fd);
    213     return result;
    214   }
    215 
    216   for (i = 0; i < num; i++) {
    217     mapinfo = mapinfos + i;
    218     if (mapinfo->flags & MAP_ELF) {
    219       map.info.vaddr = mapinfo->vaddr;
    220       if (devctl(proc_fd, DCMD_PROC_MAPDEBUG, &map, sizeof(map), 0) != EOK) {
    221         continue;
    222       }
    223       result.push_back(SharedLibraryAddress(
    224           map.info.path, mapinfo->vaddr, mapinfo->vaddr + mapinfo->size));
    225     }
    226   }
    227   free(mapinfos);
    228   close(proc_fd);
    229   return result;
    230 }
    231 
    232 
    233 void OS::SignalCodeMovingGC() {
    234 }
    235 
    236 
    237 // Constants used for mmap.
    238 static const int kMmapFd = -1;
    239 static const int kMmapFdOffset = 0;
    240 
    241 
    242 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
    243 
    244 
    245 VirtualMemory::VirtualMemory(size_t size)
    246     : address_(ReserveRegion(size)), size_(size) { }
    247 
    248 
    249 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
    250     : address_(NULL), size_(0) {
    251   ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
    252   size_t request_size = RoundUp(size + alignment,
    253                                 static_cast<intptr_t>(OS::AllocateAlignment()));
    254   void* reservation = mmap(OS::GetRandomMmapAddr(),
    255                            request_size,
    256                            PROT_NONE,
    257                            MAP_PRIVATE | MAP_ANONYMOUS | MAP_LAZY,
    258                            kMmapFd,
    259                            kMmapFdOffset);
    260   if (reservation == MAP_FAILED) return;
    261 
    262   Address base = static_cast<Address>(reservation);
    263   Address aligned_base = RoundUp(base, alignment);
    264   ASSERT_LE(base, aligned_base);
    265 
    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;
    271   }
    272 
    273   size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
    274   ASSERT_LE(aligned_size, request_size);
    275 
    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;
    280   }
    281 
    282   ASSERT(aligned_size == request_size);
    283 
    284   address_ = static_cast<void*>(aligned_base);
    285   size_ = aligned_size;
    286 }
    287 
    288 
    289 VirtualMemory::~VirtualMemory() {
    290   if (IsReserved()) {
    291     bool result = ReleaseRegion(address(), size());
    292     ASSERT(result);
    293     USE(result);
    294   }
    295 }
    296 
    297 
    298 bool VirtualMemory::IsReserved() {
    299   return address_ != NULL;
    300 }
    301 
    302 
    303 void VirtualMemory::Reset() {
    304   address_ = NULL;
    305   size_ = 0;
    306 }
    307 
    308 
    309 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
    310   return CommitRegion(address, size, is_executable);
    311 }
    312 
    313 
    314 bool VirtualMemory::Uncommit(void* address, size_t size) {
    315   return UncommitRegion(address, size);
    316 }
    317 
    318 
    319 bool VirtualMemory::Guard(void* address) {
    320   OS::Guard(address, OS::CommitPageSize());
    321   return true;
    322 }
    323 
    324 
    325 void* VirtualMemory::ReserveRegion(size_t size) {
    326   void* result = mmap(OS::GetRandomMmapAddr(),
    327                       size,
    328                       PROT_NONE,
    329                       MAP_PRIVATE | MAP_ANONYMOUS | MAP_LAZY,
    330                       kMmapFd,
    331                       kMmapFdOffset);
    332 
    333   if (result == MAP_FAILED) return NULL;
    334 
    335   return result;
    336 }
    337 
    338 
    339 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
    340   int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
    341   if (MAP_FAILED == mmap(base,
    342                          size,
    343                          prot,
    344                          MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
    345                          kMmapFd,
    346                          kMmapFdOffset)) {
    347     return false;
    348   }
    349 
    350   return true;
    351 }
    352 
    353 
    354 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
    355   return mmap(base,
    356               size,
    357               PROT_NONE,
    358               MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | MAP_LAZY,
    359               kMmapFd,
    360               kMmapFdOffset) != MAP_FAILED;
    361 }
    362 
    363 
    364 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
    365   return munmap(base, size) == 0;
    366 }
    367 
    368 
    369 bool VirtualMemory::HasLazyCommits() {
    370   return false;
    371 }
    372 
    373 } }  // namespace v8::internal
    374