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.
5 // Platform specific code for AIX goes here. For the POSIX comaptible parts
6 // the implementation is in platform-posix.cc.
13 #include <sys/resource.h>
15 #include <sys/ucontext.h>
18 #include <fcntl.h> // open
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
31 #include "src/base/macros.h"
32 #include "src/base/platform/platform.h"
39 static inline void* mmapHelper(size_t len, int prot, int flags, int fildes,
41 void* addr = OS::GetRandomMmapAddr();
42 return mmap(addr, len, prot, flags, fildes, off);
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* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn)
50 if (NULL == t) return "";
51 return tzname[0]; // The location of the timezone string on AIX.
55 double OS::LocalTimeOffset(TimezoneCache* cache) {
56 // On AIX, struct tm does not contain a tm_gmtoff field.
57 time_t utc = time(NULL);
59 struct tm* loc = localtime(&utc); // NOLINT(runtime/threadsafe_fn)
61 return static_cast<double>((mktime(loc) - utc) * msPerSecond);
65 void* OS::Allocate(const size_t requested, size_t* allocated, bool executable) {
66 const size_t msize = RoundUp(requested, getpagesize());
67 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0);
68 void* mbase = mmapHelper(msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
70 if (mbase == MAP_FAILED) return NULL;
76 static unsigned StringToLong(char* buffer) {
77 return static_cast<unsigned>(strtol(buffer, NULL, 16)); // NOLINT
81 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
82 std::vector<SharedLibraryAddress> result;
83 static const int MAP_LENGTH = 1024;
84 int fd = open("/proc/self/maps", O_RDONLY);
85 if (fd < 0) return result;
91 ssize_t rc = read(fd, addr_buffer + 2, 8);
93 unsigned start = StringToLong(addr_buffer);
94 rc = read(fd, addr_buffer + 2, 1);
96 if (addr_buffer[2] != '-') break;
97 rc = read(fd, addr_buffer + 2, 8);
99 unsigned end = StringToLong(addr_buffer);
100 char buffer[MAP_LENGTH];
104 if (bytes_read >= MAP_LENGTH - 1) break;
105 rc = read(fd, buffer + bytes_read, 1);
107 } while (buffer[bytes_read] != '\n');
108 buffer[bytes_read] = 0;
109 // Ignore mappings that are not executable.
110 if (buffer[3] != 'x') continue;
111 char* start_of_path = index(buffer, '/');
112 // There may be no filename in this line. Skip to next.
113 if (start_of_path == NULL) continue;
114 buffer[bytes_read] = 0;
115 result.push_back(SharedLibraryAddress(start_of_path, start, end));
122 void OS::SignalCodeMovingGC() {}
125 // Constants used for mmap.
126 static const int kMmapFd = -1;
127 static const int kMmapFdOffset = 0;
129 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) {}
132 VirtualMemory::VirtualMemory(size_t size)
133 : address_(ReserveRegion(size)), size_(size) {}
136 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
137 : address_(NULL), size_(0) {
138 DCHECK((alignment % OS::AllocateAlignment()) == 0);
139 size_t request_size =
140 RoundUp(size + alignment, static_cast<intptr_t>(OS::AllocateAlignment()));
142 mmapHelper(request_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, kMmapFd,
144 if (reservation == MAP_FAILED) return;
146 uint8_t* base = static_cast<uint8_t*>(reservation);
147 uint8_t* aligned_base = RoundUp(base, alignment);
148 DCHECK_LE(base, aligned_base);
150 // Unmap extra memory reserved before and after the desired block.
151 if (aligned_base != base) {
152 size_t prefix_size = static_cast<size_t>(aligned_base - base);
153 OS::Free(base, prefix_size);
154 request_size -= prefix_size;
157 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
158 DCHECK_LE(aligned_size, request_size);
160 if (aligned_size != request_size) {
161 size_t suffix_size = request_size - aligned_size;
162 OS::Free(aligned_base + aligned_size, suffix_size);
163 request_size -= suffix_size;
166 DCHECK(aligned_size == request_size);
168 address_ = static_cast<void*>(aligned_base);
169 size_ = aligned_size;
173 VirtualMemory::~VirtualMemory() {
175 bool result = ReleaseRegion(address(), size());
182 bool VirtualMemory::IsReserved() { return address_ != NULL; }
185 void VirtualMemory::Reset() {
191 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
192 return CommitRegion(address, size, is_executable);
196 bool VirtualMemory::Uncommit(void* address, size_t size) {
197 return UncommitRegion(address, size);
201 bool VirtualMemory::Guard(void* address) {
202 OS::Guard(address, OS::CommitPageSize());
207 void* VirtualMemory::ReserveRegion(size_t size) {
208 void* result = mmapHelper(size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
209 kMmapFd, kMmapFdOffset);
211 if (result == MAP_FAILED) return NULL;
217 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
218 #if defined(__native_client__)
219 // The Native Client port of V8 uses an interpreter,
220 // so code pages don't need PROT_EXEC.
221 int prot = PROT_READ | PROT_WRITE;
223 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
225 if (mprotect(base, size, prot) == -1) return false;
231 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
232 return mprotect(base, size, PROT_NONE) != -1;
236 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
237 return munmap(base, size) == 0;
241 bool VirtualMemory::HasLazyCommits() { return true; }