| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Platform-specific code for FreeBSD goes here. For the POSIX-compatible | 5 // Platform specific code for AIX goes here. For the POSIX comaptible parts |
| 6 // parts, the implementation is in platform-posix.cc. | 6 // the implementation is in platform-posix.cc. |
| 7 | 7 |
| 8 #include <pthread.h> | 8 #include <pthread.h> |
| 9 #include <semaphore.h> | 9 #include <semaphore.h> |
| 10 #include <signal.h> | 10 #include <signal.h> |
| 11 #include <stdio.h> |
| 11 #include <stdlib.h> | 12 #include <stdlib.h> |
| 12 #include <sys/resource.h> | 13 #include <sys/resource.h> |
| 13 #include <sys/time.h> | 14 #include <sys/time.h> |
| 14 #include <sys/types.h> | |
| 15 #include <sys/ucontext.h> | 15 #include <sys/ucontext.h> |
| 16 | 16 |
| 17 #include <sys/fcntl.h> // open | 17 #include <errno.h> |
| 18 #include <fcntl.h> // open |
| 19 #include <limits.h> |
| 20 #include <stdarg.h> |
| 21 #include <strings.h> // index |
| 18 #include <sys/mman.h> // mmap & munmap | 22 #include <sys/mman.h> // mmap & munmap |
| 19 #include <sys/stat.h> // open | 23 #include <sys/stat.h> // open |
| 20 #include <sys/types.h> // mmap & munmap | 24 #include <sys/types.h> // mmap & munmap |
| 21 #include <unistd.h> // getpagesize | 25 #include <unistd.h> // getpagesize |
| 22 // If you don't have execinfo.h then you need devel/libexecinfo from ports. | |
| 23 #include <errno.h> | |
| 24 #include <limits.h> | |
| 25 #include <stdarg.h> | |
| 26 #include <strings.h> // index | |
| 27 | 26 |
| 28 #include <cmath> | 27 #include <cmath> |
| 29 | 28 |
| 30 #undef MAP_TYPE | 29 #undef MAP_TYPE |
| 31 | 30 |
| 32 #include "src/base/macros.h" | 31 #include "src/base/macros.h" |
| 33 #include "src/base/platform/platform.h" | 32 #include "src/base/platform/platform.h" |
| 34 | 33 |
| 35 | 34 |
| 36 namespace v8 { | 35 namespace v8 { |
| 37 namespace base { | 36 namespace base { |
| 38 | 37 |
| 39 | 38 |
| 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 |
| 40 const char* OS::LocalTimezone(double time, TimezoneCache* cache) { | 46 const char* OS::LocalTimezone(double time, TimezoneCache* cache) { |
| 41 if (std::isnan(time)) return ""; | 47 if (std::isnan(time)) return ""; |
| 42 time_t tv = static_cast<time_t>(std::floor(time/msPerSecond)); | 48 time_t tv = static_cast<time_t>(floor(time / msPerSecond)); |
| 43 struct tm* t = localtime(&tv); | 49 struct tm* t = localtime(&tv); |
| 44 if (NULL == t) return ""; | 50 if (NULL == t) return ""; |
| 45 return t->tm_zone; | 51 return tzname[0]; // The location of the timezone string on AIX. |
| 46 } | 52 } |
| 47 | 53 |
| 48 | 54 |
| 49 double OS::LocalTimeOffset(TimezoneCache* cache) { | 55 double OS::LocalTimeOffset(TimezoneCache* cache) { |
| 50 time_t tv = time(NULL); | 56 // On AIX, struct tm does not contain a tm_gmtoff field. |
| 51 struct tm* t = localtime(&tv); | 57 time_t utc = time(NULL); |
| 52 // tm_gmtoff includes any daylight savings offset, so subtract it. | 58 DCHECK(utc != -1); |
| 53 return static_cast<double>(t->tm_gmtoff * msPerSecond - | 59 struct tm* loc = localtime(&utc); |
| 54 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); | 60 DCHECK(loc != NULL); |
| 61 return static_cast<double>((mktime(loc) - utc) * msPerSecond); |
| 55 } | 62 } |
| 56 | 63 |
| 57 | 64 |
| 58 void* OS::Allocate(const size_t requested, | 65 void* OS::Allocate(const size_t requested, size_t* allocated, bool executable) { |
| 59 size_t* allocated, | |
| 60 bool executable) { | |
| 61 const size_t msize = RoundUp(requested, getpagesize()); | 66 const size_t msize = RoundUp(requested, getpagesize()); |
| 62 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); | 67 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); |
| 63 void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0); | 68 void* mbase = mmapHelper(msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 64 | 69 |
| 65 if (mbase == MAP_FAILED) return NULL; | 70 if (mbase == MAP_FAILED) return NULL; |
| 66 *allocated = msize; | 71 *allocated = msize; |
| 67 return mbase; | 72 return mbase; |
| 68 } | 73 } |
| 69 | 74 |
| 70 | 75 |
| 71 class PosixMemoryMappedFile : public OS::MemoryMappedFile { | 76 class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
| 72 public: | 77 public: |
| 73 PosixMemoryMappedFile(FILE* file, void* memory, int size) | 78 PosixMemoryMappedFile(FILE* file, void* memory, int size) |
| 74 : file_(file), memory_(memory), size_(size) { } | 79 : file_(file), memory_(memory), size_(size) {} |
| 75 virtual ~PosixMemoryMappedFile(); | 80 virtual ~PosixMemoryMappedFile(); |
| 76 virtual void* memory() { return memory_; } | 81 virtual void* memory() { return memory_; } |
| 77 virtual int size() { return size_; } | 82 virtual int size() { return size_; } |
| 83 |
| 78 private: | 84 private: |
| 79 FILE* file_; | 85 FILE* file_; |
| 80 void* memory_; | 86 void* memory_; |
| 81 int size_; | 87 int size_; |
| 82 }; | 88 }; |
| 83 | 89 |
| 84 | 90 |
| 85 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { | 91 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { |
| 86 FILE* file = fopen(name, "r+"); | 92 FILE* file = fopen(name, "r+"); |
| 87 if (file == NULL) return NULL; | 93 if (file == NULL) return NULL; |
| 88 | 94 |
| 89 fseek(file, 0, SEEK_END); | 95 fseek(file, 0, SEEK_END); |
| 90 int size = ftell(file); | 96 int size = ftell(file); |
| 91 | 97 |
| 92 void* memory = | 98 void* memory = |
| 93 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); | 99 mmapHelper(size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 94 return new PosixMemoryMappedFile(file, memory, size); | 100 return new PosixMemoryMappedFile(file, memory, size); |
| 95 } | 101 } |
| 96 | 102 |
| 97 | 103 |
| 98 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, | 104 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| 99 void* initial) { | 105 void* initial) { |
| 100 FILE* file = fopen(name, "w+"); | 106 FILE* file = fopen(name, "w+"); |
| 101 if (file == NULL) return NULL; | 107 if (file == NULL) return NULL; |
| 102 int result = fwrite(initial, size, 1, file); | 108 int result = fwrite(initial, size, 1, file); |
| 103 if (result < 1) { | 109 if (result < 1) { |
| 104 fclose(file); | 110 fclose(file); |
| 105 return NULL; | 111 return NULL; |
| 106 } | 112 } |
| 107 void* memory = | 113 void* memory = |
| 108 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); | 114 mmapHelper(size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 109 return new PosixMemoryMappedFile(file, memory, size); | 115 return new PosixMemoryMappedFile(file, memory, size); |
| 110 } | 116 } |
| 111 | 117 |
| 112 | 118 |
| 113 PosixMemoryMappedFile::~PosixMemoryMappedFile() { | 119 PosixMemoryMappedFile::~PosixMemoryMappedFile() { |
| 114 if (memory_) munmap(memory_, size_); | 120 if (memory_) munmap(memory_, size_); |
| 115 fclose(file_); | 121 fclose(file_); |
| 116 } | 122 } |
| 117 | 123 |
| 118 | 124 |
| 119 static unsigned StringToLong(char* buffer) { | 125 static unsigned StringToLong(char* buffer) { |
| 120 return static_cast<unsigned>(strtol(buffer, NULL, 16)); // NOLINT | 126 return static_cast<unsigned>(strtol(buffer, NULL, 16)); // NOLINT |
| 121 } | 127 } |
| 122 | 128 |
| 123 | 129 |
| 124 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { | 130 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { |
| 125 std::vector<SharedLibraryAddress> result; | 131 std::vector<SharedLibraryAddress> result; |
| 126 static const int MAP_LENGTH = 1024; | 132 static const int MAP_LENGTH = 1024; |
| 127 int fd = open("/proc/self/maps", O_RDONLY); | 133 int fd = open("/proc/self/maps", O_RDONLY); |
| 128 if (fd < 0) return result; | 134 if (fd < 0) return result; |
| 129 while (true) { | 135 while (true) { |
| 130 char addr_buffer[11]; | 136 char addr_buffer[11]; |
| 131 addr_buffer[0] = '0'; | 137 addr_buffer[0] = '0'; |
| 132 addr_buffer[1] = 'x'; | 138 addr_buffer[1] = 'x'; |
| 133 addr_buffer[10] = 0; | 139 addr_buffer[10] = 0; |
| 134 ssize_t bytes_read = read(fd, addr_buffer + 2, 8); | 140 ssize_t rc = read(fd, addr_buffer + 2, 8); |
| 135 if (bytes_read < 8) break; | 141 if (rc < 8) break; |
| 136 unsigned start = StringToLong(addr_buffer); | 142 unsigned start = StringToLong(addr_buffer); |
| 137 bytes_read = read(fd, addr_buffer + 2, 1); | 143 rc = read(fd, addr_buffer + 2, 1); |
| 138 if (bytes_read < 1) break; | 144 if (rc < 1) break; |
| 139 if (addr_buffer[2] != '-') break; | 145 if (addr_buffer[2] != '-') break; |
| 140 bytes_read = read(fd, addr_buffer + 2, 8); | 146 rc = read(fd, addr_buffer + 2, 8); |
| 141 if (bytes_read < 8) break; | 147 if (rc < 8) break; |
| 142 unsigned end = StringToLong(addr_buffer); | 148 unsigned end = StringToLong(addr_buffer); |
| 143 char buffer[MAP_LENGTH]; | 149 char buffer[MAP_LENGTH]; |
| 144 bytes_read = -1; | 150 int bytes_read = -1; |
| 145 do { | 151 do { |
| 146 bytes_read++; | 152 bytes_read++; |
| 147 if (bytes_read >= MAP_LENGTH - 1) | 153 if (bytes_read >= MAP_LENGTH - 1) break; |
| 148 break; | 154 rc = read(fd, buffer + bytes_read, 1); |
| 149 bytes_read = read(fd, buffer + bytes_read, 1); | 155 if (rc < 1) break; |
| 150 if (bytes_read < 1) break; | |
| 151 } while (buffer[bytes_read] != '\n'); | 156 } while (buffer[bytes_read] != '\n'); |
| 152 buffer[bytes_read] = 0; | 157 buffer[bytes_read] = 0; |
| 153 // Ignore mappings that are not executable. | 158 // Ignore mappings that are not executable. |
| 154 if (buffer[3] != 'x') continue; | 159 if (buffer[3] != 'x') continue; |
| 155 char* start_of_path = index(buffer, '/'); | 160 char* start_of_path = index(buffer, '/'); |
| 156 // There may be no filename in this line. Skip to next. | 161 // There may be no filename in this line. Skip to next. |
| 157 if (start_of_path == NULL) continue; | 162 if (start_of_path == NULL) continue; |
| 158 buffer[bytes_read] = 0; | 163 buffer[bytes_read] = 0; |
| 159 result.push_back(SharedLibraryAddress(start_of_path, start, end)); | 164 result.push_back(SharedLibraryAddress(start_of_path, start, end)); |
| 160 } | 165 } |
| 161 close(fd); | 166 close(fd); |
| 162 return result; | 167 return result; |
| 163 } | 168 } |
| 164 | 169 |
| 165 | 170 |
| 166 void OS::SignalCodeMovingGC() { | 171 void OS::SignalCodeMovingGC() {} |
| 167 } | |
| 168 | |
| 169 | 172 |
| 170 | 173 |
| 171 // Constants used for mmap. | 174 // Constants used for mmap. |
| 172 static const int kMmapFd = -1; | 175 static const int kMmapFd = -1; |
| 173 static const int kMmapFdOffset = 0; | 176 static const int kMmapFdOffset = 0; |
| 174 | 177 |
| 175 | 178 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) {} |
| 176 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } | |
| 177 | 179 |
| 178 | 180 |
| 179 VirtualMemory::VirtualMemory(size_t size) | 181 VirtualMemory::VirtualMemory(size_t size) |
| 180 : address_(ReserveRegion(size)), size_(size) { } | 182 : address_(ReserveRegion(size)), size_(size) {} |
| 181 | 183 |
| 182 | 184 |
| 183 VirtualMemory::VirtualMemory(size_t size, size_t alignment) | 185 VirtualMemory::VirtualMemory(size_t size, size_t alignment) |
| 184 : address_(NULL), size_(0) { | 186 : address_(NULL), size_(0) { |
| 185 DCHECK((alignment % OS::AllocateAlignment()) == 0); | 187 DCHECK((alignment % OS::AllocateAlignment()) == 0); |
| 186 size_t request_size = RoundUp(size + alignment, | 188 size_t request_size = |
| 187 static_cast<intptr_t>(OS::AllocateAlignment())); | 189 RoundUp(size + alignment, static_cast<intptr_t>(OS::AllocateAlignment())); |
| 188 void* reservation = mmap(OS::GetRandomMmapAddr(), | 190 void* reservation = |
| 189 request_size, | 191 mmapHelper(request_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, kMmapFd, |
| 190 PROT_NONE, | 192 kMmapFdOffset); |
| 191 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, | |
| 192 kMmapFd, | |
| 193 kMmapFdOffset); | |
| 194 if (reservation == MAP_FAILED) return; | 193 if (reservation == MAP_FAILED) return; |
| 195 | 194 |
| 196 uint8_t* base = static_cast<uint8_t*>(reservation); | 195 uint8_t* base = static_cast<uint8_t*>(reservation); |
| 197 uint8_t* aligned_base = RoundUp(base, alignment); | 196 uint8_t* aligned_base = RoundUp(base, alignment); |
| 198 DCHECK_LE(base, aligned_base); | 197 DCHECK_LE(base, aligned_base); |
| 199 | 198 |
| 200 // Unmap extra memory reserved before and after the desired block. | 199 // Unmap extra memory reserved before and after the desired block. |
| 201 if (aligned_base != base) { | 200 if (aligned_base != base) { |
| 202 size_t prefix_size = static_cast<size_t>(aligned_base - base); | 201 size_t prefix_size = static_cast<size_t>(aligned_base - base); |
| 203 OS::Free(base, prefix_size); | 202 OS::Free(base, prefix_size); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 222 | 221 |
| 223 VirtualMemory::~VirtualMemory() { | 222 VirtualMemory::~VirtualMemory() { |
| 224 if (IsReserved()) { | 223 if (IsReserved()) { |
| 225 bool result = ReleaseRegion(address(), size()); | 224 bool result = ReleaseRegion(address(), size()); |
| 226 DCHECK(result); | 225 DCHECK(result); |
| 227 USE(result); | 226 USE(result); |
| 228 } | 227 } |
| 229 } | 228 } |
| 230 | 229 |
| 231 | 230 |
| 232 bool VirtualMemory::IsReserved() { | 231 bool VirtualMemory::IsReserved() { return address_ != NULL; } |
| 233 return address_ != NULL; | |
| 234 } | |
| 235 | 232 |
| 236 | 233 |
| 237 void VirtualMemory::Reset() { | 234 void VirtualMemory::Reset() { |
| 238 address_ = NULL; | 235 address_ = NULL; |
| 239 size_ = 0; | 236 size_ = 0; |
| 240 } | 237 } |
| 241 | 238 |
| 242 | 239 |
| 243 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { | 240 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
| 244 return CommitRegion(address, size, is_executable); | 241 return CommitRegion(address, size, is_executable); |
| 245 } | 242 } |
| 246 | 243 |
| 247 | 244 |
| 248 bool VirtualMemory::Uncommit(void* address, size_t size) { | 245 bool VirtualMemory::Uncommit(void* address, size_t size) { |
| 249 return UncommitRegion(address, size); | 246 return UncommitRegion(address, size); |
| 250 } | 247 } |
| 251 | 248 |
| 252 | 249 |
| 253 bool VirtualMemory::Guard(void* address) { | 250 bool VirtualMemory::Guard(void* address) { |
| 254 OS::Guard(address, OS::CommitPageSize()); | 251 OS::Guard(address, OS::CommitPageSize()); |
| 255 return true; | 252 return true; |
| 256 } | 253 } |
| 257 | 254 |
| 258 | 255 |
| 259 void* VirtualMemory::ReserveRegion(size_t size) { | 256 void* VirtualMemory::ReserveRegion(size_t size) { |
| 260 void* result = mmap(OS::GetRandomMmapAddr(), | 257 void* result = mmapHelper(size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
| 261 size, | 258 kMmapFd, kMmapFdOffset); |
| 262 PROT_NONE, | |
| 263 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, | |
| 264 kMmapFd, | |
| 265 kMmapFdOffset); | |
| 266 | 259 |
| 267 if (result == MAP_FAILED) return NULL; | 260 if (result == MAP_FAILED) return NULL; |
| 268 | 261 |
| 269 return result; | 262 return result; |
| 270 } | 263 } |
| 271 | 264 |
| 272 | 265 |
| 273 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { | 266 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { |
| 267 #if defined(__native_client__) |
| 268 // The Native Client port of V8 uses an interpreter, |
| 269 // so code pages don't need PROT_EXEC. |
| 270 int prot = PROT_READ | PROT_WRITE; |
| 271 #else |
| 274 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); | 272 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| 275 if (MAP_FAILED == mmap(base, | 273 #endif |
| 276 size, | 274 if (mprotect(base, size, prot) == -1) return false; |
| 277 prot, | 275 |
| 278 MAP_PRIVATE | MAP_ANON | MAP_FIXED, | |
| 279 kMmapFd, | |
| 280 kMmapFdOffset)) { | |
| 281 return false; | |
| 282 } | |
| 283 return true; | 276 return true; |
| 284 } | 277 } |
| 285 | 278 |
| 286 | 279 |
| 287 bool VirtualMemory::UncommitRegion(void* base, size_t size) { | 280 bool VirtualMemory::UncommitRegion(void* base, size_t size) { |
| 288 return mmap(base, | 281 return mprotect(base, size, PROT_NONE) != -1; |
| 289 size, | |
| 290 PROT_NONE, | |
| 291 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED, | |
| 292 kMmapFd, | |
| 293 kMmapFdOffset) != MAP_FAILED; | |
| 294 } | 282 } |
| 295 | 283 |
| 296 | 284 |
| 297 bool VirtualMemory::ReleaseRegion(void* base, size_t size) { | 285 bool VirtualMemory::ReleaseRegion(void* base, size_t size) { |
| 298 return munmap(base, size) == 0; | 286 return munmap(base, size) == 0; |
| 299 } | 287 } |
| 300 | 288 |
| 301 | 289 |
| 302 bool VirtualMemory::HasLazyCommits() { | 290 bool VirtualMemory::HasLazyCommits() { return true; } |
| 303 // TODO(alph): implement for the platform. | |
| 304 return false; | |
| 305 } | 291 } |
| 306 | 292 } // namespace v8::base |
| 307 } } // namespace v8::base | |
| OLD | NEW |