• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 AIX goes here. For the POSIX comaptible parts
6 // the implementation is in platform-posix.cc.
7 
8 #include <pthread.h>
9 #include <semaphore.h>
10 #include <signal.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <sys/resource.h>
14 #include <sys/time.h>
15 #include <sys/ucontext.h>
16 
17 #include <errno.h>
18 #include <fcntl.h>  // open
19 #include <limits.h>
20 #include <stdarg.h>
21 #include <strings.h>    // index
22 #include <sys/mman.h>   // mmap & munmap
23 #include <sys/stat.h>   // open
24 #include <sys/types.h>  // mmap & munmap
25 #include <unistd.h>     // getpagesize
26 
27 #include <cmath>
28 
29 #undef MAP_TYPE
30 
31 #include "src/base/macros.h"
32 #include "src/base/platform/platform.h"
33 
34 
35 namespace v8 {
36 namespace base {
37 
38 
mmapHelper(size_t len,int prot,int flags,int fildes,off_t off)39 static inline void* mmapHelper(size_t len, int prot, int flags, int fildes,
40                                off_t off) {
41   void* addr = OS::GetRandomMmapAddr();
42   return mmap(addr, len, prot, flags, fildes, off);
43 }
44 
45 
LocalTimezone(double time,TimezoneCache * cache)46 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
47   if (std::isnan(time)) return "";
48   time_t tv = static_cast<time_t>(floor(time / msPerSecond));
49   struct tm tm;
50   struct tm* t = localtime_r(&tv, &tm);
51   if (NULL == t) return "";
52   return tzname[0];  // The location of the timezone string on AIX.
53 }
54 
55 
LocalTimeOffset(TimezoneCache * cache)56 double OS::LocalTimeOffset(TimezoneCache* cache) {
57   // On AIX, struct tm does not contain a tm_gmtoff field.
58   time_t utc = time(NULL);
59   DCHECK(utc != -1);
60   struct tm tm;
61   struct tm* loc = localtime_r(&utc, &tm);
62   DCHECK(loc != NULL);
63   return static_cast<double>((mktime(loc) - utc) * msPerSecond);
64 }
65 
66 
Allocate(const size_t requested,size_t * allocated,bool executable)67 void* OS::Allocate(const size_t requested, size_t* allocated, bool executable) {
68   const size_t msize = RoundUp(requested, getpagesize());
69   int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0);
70   void* mbase = mmapHelper(msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
71 
72   if (mbase == MAP_FAILED) return NULL;
73   *allocated = msize;
74   return mbase;
75 }
76 
77 
StringToLong(char * buffer)78 static unsigned StringToLong(char* buffer) {
79   return static_cast<unsigned>(strtol(buffer, NULL, 16));  // NOLINT
80 }
81 
82 
GetSharedLibraryAddresses()83 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
84   std::vector<SharedLibraryAddress> result;
85   static const int MAP_LENGTH = 1024;
86   int fd = open("/proc/self/maps", O_RDONLY);
87   if (fd < 0) return result;
88   while (true) {
89     char addr_buffer[11];
90     addr_buffer[0] = '0';
91     addr_buffer[1] = 'x';
92     addr_buffer[10] = 0;
93     ssize_t rc = read(fd, addr_buffer + 2, 8);
94     if (rc < 8) break;
95     unsigned start = StringToLong(addr_buffer);
96     rc = read(fd, addr_buffer + 2, 1);
97     if (rc < 1) break;
98     if (addr_buffer[2] != '-') break;
99     rc = read(fd, addr_buffer + 2, 8);
100     if (rc < 8) break;
101     unsigned end = StringToLong(addr_buffer);
102     char buffer[MAP_LENGTH];
103     int bytes_read = -1;
104     do {
105       bytes_read++;
106       if (bytes_read >= MAP_LENGTH - 1) break;
107       rc = read(fd, buffer + bytes_read, 1);
108       if (rc < 1) break;
109     } while (buffer[bytes_read] != '\n');
110     buffer[bytes_read] = 0;
111     // Ignore mappings that are not executable.
112     if (buffer[3] != 'x') continue;
113     char* start_of_path = index(buffer, '/');
114     // There may be no filename in this line.  Skip to next.
115     if (start_of_path == NULL) continue;
116     buffer[bytes_read] = 0;
117     result.push_back(SharedLibraryAddress(start_of_path, start, end));
118   }
119   close(fd);
120   return result;
121 }
122 
123 
SignalCodeMovingGC()124 void OS::SignalCodeMovingGC() {}
125 
126 
127 // Constants used for mmap.
128 static const int kMmapFd = -1;
129 static const int kMmapFdOffset = 0;
130 
VirtualMemory()131 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) {}
132 
133 
VirtualMemory(size_t size)134 VirtualMemory::VirtualMemory(size_t size)
135     : address_(ReserveRegion(size)), size_(size) {}
136 
137 
VirtualMemory(size_t size,size_t alignment)138 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
139     : address_(NULL), size_(0) {
140   DCHECK((alignment % OS::AllocateAlignment()) == 0);
141   size_t request_size =
142       RoundUp(size + alignment, static_cast<intptr_t>(OS::AllocateAlignment()));
143   void* reservation =
144       mmapHelper(request_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, kMmapFd,
145                  kMmapFdOffset);
146   if (reservation == MAP_FAILED) return;
147 
148   uint8_t* base = static_cast<uint8_t*>(reservation);
149   uint8_t* aligned_base = RoundUp(base, alignment);
150   DCHECK_LE(base, aligned_base);
151 
152   // Unmap extra memory reserved before and after the desired block.
153   if (aligned_base != base) {
154     size_t prefix_size = static_cast<size_t>(aligned_base - base);
155     OS::Free(base, prefix_size);
156     request_size -= prefix_size;
157   }
158 
159   size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
160   DCHECK_LE(aligned_size, request_size);
161 
162   if (aligned_size != request_size) {
163     size_t suffix_size = request_size - aligned_size;
164     OS::Free(aligned_base + aligned_size, suffix_size);
165     request_size -= suffix_size;
166   }
167 
168   DCHECK(aligned_size == request_size);
169 
170   address_ = static_cast<void*>(aligned_base);
171   size_ = aligned_size;
172 }
173 
174 
~VirtualMemory()175 VirtualMemory::~VirtualMemory() {
176   if (IsReserved()) {
177     bool result = ReleaseRegion(address(), size());
178     DCHECK(result);
179     USE(result);
180   }
181 }
182 
183 
IsReserved()184 bool VirtualMemory::IsReserved() { return address_ != NULL; }
185 
186 
Reset()187 void VirtualMemory::Reset() {
188   address_ = NULL;
189   size_ = 0;
190 }
191 
192 
Commit(void * address,size_t size,bool is_executable)193 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
194   return CommitRegion(address, size, is_executable);
195 }
196 
197 
Uncommit(void * address,size_t size)198 bool VirtualMemory::Uncommit(void* address, size_t size) {
199   return UncommitRegion(address, size);
200 }
201 
202 
Guard(void * address)203 bool VirtualMemory::Guard(void* address) {
204   OS::Guard(address, OS::CommitPageSize());
205   return true;
206 }
207 
208 
ReserveRegion(size_t size)209 void* VirtualMemory::ReserveRegion(size_t size) {
210   void* result = mmapHelper(size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
211                             kMmapFd, kMmapFdOffset);
212 
213   if (result == MAP_FAILED) return NULL;
214 
215   return result;
216 }
217 
218 
CommitRegion(void * base,size_t size,bool is_executable)219 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
220   int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
221 
222   if (mprotect(base, size, prot) == -1) return false;
223 
224   return true;
225 }
226 
227 
UncommitRegion(void * base,size_t size)228 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
229   return mprotect(base, size, PROT_NONE) != -1;
230 }
231 
ReleasePartialRegion(void * base,size_t size,void * free_start,size_t free_size)232 bool VirtualMemory::ReleasePartialRegion(void* base, size_t size,
233                                          void* free_start, size_t free_size) {
234   return munmap(free_start, free_size) == 0;
235 }
236 
237 
ReleaseRegion(void * base,size_t size)238 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
239   return munmap(base, size) == 0;
240 }
241 
242 
HasLazyCommits()243 bool VirtualMemory::HasLazyCommits() { return true; }
244 }  // namespace base
245 }  // namespace v8
246