Chromium Code Reviews| 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 #if V8_TARGET_ARCH_PPC64 | |
| 43 if (addr) { | |
| 44 // We must use MAP_FIXED here to avoid the 0x07000000_00000000 | |
| 45 // range, which causes loss of precision if addresses are | |
| 46 // converted to double precision numbers. | |
|
Sven Panne
2015/01/27 11:47:05
Huh? Where do we convert addresses to doubles?
michael_dawson
2015/01/29 00:08:29
The conversion to doubles happens in the test ccte
Sven Panne
2015/01/29 09:54:33
Exactly, this seems to be the right approach. Let'
michael_dawson
2015/01/29 18:21:58
I found an existing issue(2857-https://code.google
| |
| 47 DCHECK(!(flags & MAP_VARIABLE)); | |
| 48 void* mbase; | |
| 49 for (int i = 0; i < 10; i++) { | |
| 50 mbase = mmap(addr, len, prot, flags | MAP_FIXED, fildes, off); | |
| 51 if (mbase != MAP_FAILED) return mbase; | |
| 52 // Try again with a different random address. | |
| 53 addr = OS::GetRandomMmapAddr(); | |
| 54 } | |
| 55 } | |
| 56 // Fall through if we can't get an address in the range we want | |
| 57 // after multiple attempts -- just let the system decide (without | |
| 58 // MAP_FIXED this time). | |
| 59 | |
| 60 // N.B. This case should never happen given the size of the | |
| 61 // randomized range -- in fact looping at all is extremely rare. If | |
| 62 // we ever do hit this case, it is better to let the system allocate | |
| 63 // a high address (and bet against its unlikely consumption | |
| 64 // introspectively as a double) than to fail outright. | |
| 65 #endif | |
| 66 return mmap(addr, len, prot, flags, fildes, off); | |
| 67 } | |
| 68 | |
| 69 | |
| 40 const char* OS::LocalTimezone(double time, TimezoneCache* cache) { | 70 const char* OS::LocalTimezone(double time, TimezoneCache* cache) { |
| 41 if (std::isnan(time)) return ""; | 71 if (std::isnan(time)) return ""; |
| 42 time_t tv = static_cast<time_t>(std::floor(time/msPerSecond)); | 72 time_t tv = static_cast<time_t>(floor(time / msPerSecond)); |
| 43 struct tm* t = localtime(&tv); | 73 struct tm* t = localtime(&tv); |
| 44 if (NULL == t) return ""; | 74 if (NULL == t) return ""; |
| 45 return t->tm_zone; | 75 return tzname[0]; // The location of the timezone string on AIX. |
| 46 } | 76 } |
| 47 | 77 |
| 48 | 78 |
| 49 double OS::LocalTimeOffset(TimezoneCache* cache) { | 79 double OS::LocalTimeOffset(TimezoneCache* cache) { |
| 50 time_t tv = time(NULL); | 80 // On AIX, struct tm does not contain a tm_gmtoff field. |
| 51 struct tm* t = localtime(&tv); | 81 time_t utc = time(NULL); |
| 52 // tm_gmtoff includes any daylight savings offset, so subtract it. | 82 DCHECK(utc != -1); |
| 53 return static_cast<double>(t->tm_gmtoff * msPerSecond - | 83 struct tm* loc = localtime(&utc); |
| 54 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); | 84 DCHECK(loc != NULL); |
| 85 return static_cast<double>((mktime(loc) - utc) * msPerSecond); | |
| 55 } | 86 } |
| 56 | 87 |
| 57 | 88 |
| 58 void* OS::Allocate(const size_t requested, | 89 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()); | 90 const size_t msize = RoundUp(requested, getpagesize()); |
| 62 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); | 91 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); |
| 63 void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0); | 92 void* mbase = mmapHelper(msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 64 | 93 |
| 65 if (mbase == MAP_FAILED) return NULL; | 94 if (mbase == MAP_FAILED) return NULL; |
| 66 *allocated = msize; | 95 *allocated = msize; |
| 67 return mbase; | 96 return mbase; |
| 68 } | 97 } |
| 69 | 98 |
| 70 | 99 |
| 71 class PosixMemoryMappedFile : public OS::MemoryMappedFile { | 100 class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
| 72 public: | 101 public: |
| 73 PosixMemoryMappedFile(FILE* file, void* memory, int size) | 102 PosixMemoryMappedFile(FILE* file, void* memory, int size) |
| 74 : file_(file), memory_(memory), size_(size) { } | 103 : file_(file), memory_(memory), size_(size) {} |
| 75 virtual ~PosixMemoryMappedFile(); | 104 virtual ~PosixMemoryMappedFile(); |
| 76 virtual void* memory() { return memory_; } | 105 virtual void* memory() { return memory_; } |
| 77 virtual int size() { return size_; } | 106 virtual int size() { return size_; } |
| 107 | |
| 78 private: | 108 private: |
| 79 FILE* file_; | 109 FILE* file_; |
| 80 void* memory_; | 110 void* memory_; |
| 81 int size_; | 111 int size_; |
| 82 }; | 112 }; |
| 83 | 113 |
| 84 | 114 |
| 85 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { | 115 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { |
| 86 FILE* file = fopen(name, "r+"); | 116 FILE* file = fopen(name, "r+"); |
| 87 if (file == NULL) return NULL; | 117 if (file == NULL) return NULL; |
| 88 | 118 |
| 89 fseek(file, 0, SEEK_END); | 119 fseek(file, 0, SEEK_END); |
| 90 int size = ftell(file); | 120 int size = ftell(file); |
| 91 | 121 |
| 92 void* memory = | 122 void* memory = |
| 93 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); | 123 mmapHelper(size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 94 return new PosixMemoryMappedFile(file, memory, size); | 124 return new PosixMemoryMappedFile(file, memory, size); |
| 95 } | 125 } |
| 96 | 126 |
| 97 | 127 |
| 98 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, | 128 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| 99 void* initial) { | 129 void* initial) { |
| 100 FILE* file = fopen(name, "w+"); | 130 FILE* file = fopen(name, "w+"); |
| 101 if (file == NULL) return NULL; | 131 if (file == NULL) return NULL; |
| 102 int result = fwrite(initial, size, 1, file); | 132 int result = fwrite(initial, size, 1, file); |
| 103 if (result < 1) { | 133 if (result < 1) { |
| 104 fclose(file); | 134 fclose(file); |
| 105 return NULL; | 135 return NULL; |
| 106 } | 136 } |
| 107 void* memory = | 137 void* memory = |
| 108 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); | 138 mmapHelper(size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 109 return new PosixMemoryMappedFile(file, memory, size); | 139 return new PosixMemoryMappedFile(file, memory, size); |
| 110 } | 140 } |
| 111 | 141 |
| 112 | 142 |
| 113 PosixMemoryMappedFile::~PosixMemoryMappedFile() { | 143 PosixMemoryMappedFile::~PosixMemoryMappedFile() { |
| 114 if (memory_) munmap(memory_, size_); | 144 if (memory_) munmap(memory_, size_); |
| 115 fclose(file_); | 145 fclose(file_); |
| 116 } | 146 } |
| 117 | 147 |
| 118 | 148 |
| 119 static unsigned StringToLong(char* buffer) { | 149 static unsigned StringToLong(char* buffer) { |
| 120 return static_cast<unsigned>(strtol(buffer, NULL, 16)); // NOLINT | 150 return static_cast<unsigned>(strtol(buffer, NULL, 16)); // NOLINT |
| 121 } | 151 } |
| 122 | 152 |
| 123 | 153 |
| 124 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { | 154 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { |
| 125 std::vector<SharedLibraryAddress> result; | 155 std::vector<SharedLibraryAddress> result; |
| 126 static const int MAP_LENGTH = 1024; | 156 static const int MAP_LENGTH = 1024; |
| 127 int fd = open("/proc/self/maps", O_RDONLY); | 157 int fd = open("/proc/self/maps", O_RDONLY); |
| 128 if (fd < 0) return result; | 158 if (fd < 0) return result; |
| 129 while (true) { | 159 while (true) { |
| 130 char addr_buffer[11]; | 160 char addr_buffer[11]; |
| 131 addr_buffer[0] = '0'; | 161 addr_buffer[0] = '0'; |
| 132 addr_buffer[1] = 'x'; | 162 addr_buffer[1] = 'x'; |
| 133 addr_buffer[10] = 0; | 163 addr_buffer[10] = 0; |
| 134 ssize_t bytes_read = read(fd, addr_buffer + 2, 8); | 164 int rc = read(fd, addr_buffer + 2, 8); |
|
Sven Panne
2015/01/27 11:47:05
Does "read" really return an int on AIX? I can't f
michael_dawson
2015/01/29 00:08:29
You're right it does not return an int as per http
| |
| 135 if (bytes_read < 8) break; | 165 if (rc < 8) break; |
| 136 unsigned start = StringToLong(addr_buffer); | 166 unsigned start = StringToLong(addr_buffer); |
| 137 bytes_read = read(fd, addr_buffer + 2, 1); | 167 rc = read(fd, addr_buffer + 2, 1); |
| 138 if (bytes_read < 1) break; | 168 if (rc < 1) break; |
| 139 if (addr_buffer[2] != '-') break; | 169 if (addr_buffer[2] != '-') break; |
| 140 bytes_read = read(fd, addr_buffer + 2, 8); | 170 rc = read(fd, addr_buffer + 2, 8); |
| 141 if (bytes_read < 8) break; | 171 if (rc < 8) break; |
| 142 unsigned end = StringToLong(addr_buffer); | 172 unsigned end = StringToLong(addr_buffer); |
| 143 char buffer[MAP_LENGTH]; | 173 char buffer[MAP_LENGTH]; |
| 144 bytes_read = -1; | 174 int bytes_read = -1; |
| 145 do { | 175 do { |
| 146 bytes_read++; | 176 bytes_read++; |
| 147 if (bytes_read >= MAP_LENGTH - 1) | 177 if (bytes_read >= MAP_LENGTH - 1) break; |
| 148 break; | 178 rc = read(fd, buffer + bytes_read, 1); |
| 149 bytes_read = read(fd, buffer + bytes_read, 1); | 179 if (rc < 1) break; |
| 150 if (bytes_read < 1) break; | |
| 151 } while (buffer[bytes_read] != '\n'); | 180 } while (buffer[bytes_read] != '\n'); |
| 152 buffer[bytes_read] = 0; | 181 buffer[bytes_read] = 0; |
| 153 // Ignore mappings that are not executable. | 182 // Ignore mappings that are not executable. |
| 154 if (buffer[3] != 'x') continue; | 183 if (buffer[3] != 'x') continue; |
| 155 char* start_of_path = index(buffer, '/'); | 184 char* start_of_path = index(buffer, '/'); |
| 156 // There may be no filename in this line. Skip to next. | 185 // There may be no filename in this line. Skip to next. |
| 157 if (start_of_path == NULL) continue; | 186 if (start_of_path == NULL) continue; |
| 158 buffer[bytes_read] = 0; | 187 buffer[bytes_read] = 0; |
| 159 result.push_back(SharedLibraryAddress(start_of_path, start, end)); | 188 result.push_back(SharedLibraryAddress(start_of_path, start, end)); |
| 160 } | 189 } |
| 161 close(fd); | 190 close(fd); |
| 162 return result; | 191 return result; |
| 163 } | 192 } |
| 164 | 193 |
| 165 | 194 |
| 166 void OS::SignalCodeMovingGC() { | 195 void OS::SignalCodeMovingGC() {} |
| 167 } | |
| 168 | |
| 169 | 196 |
| 170 | 197 |
| 171 // Constants used for mmap. | 198 // Constants used for mmap. |
| 172 static const int kMmapFd = -1; | 199 static const int kMmapFd = -1; |
| 173 static const int kMmapFdOffset = 0; | 200 static const int kMmapFdOffset = 0; |
| 174 | 201 |
| 175 | 202 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) {} |
| 176 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } | |
| 177 | 203 |
| 178 | 204 |
| 179 VirtualMemory::VirtualMemory(size_t size) | 205 VirtualMemory::VirtualMemory(size_t size) |
| 180 : address_(ReserveRegion(size)), size_(size) { } | 206 : address_(ReserveRegion(size)), size_(size) {} |
| 181 | 207 |
| 182 | 208 |
| 183 VirtualMemory::VirtualMemory(size_t size, size_t alignment) | 209 VirtualMemory::VirtualMemory(size_t size, size_t alignment) |
| 184 : address_(NULL), size_(0) { | 210 : address_(NULL), size_(0) { |
| 185 DCHECK((alignment % OS::AllocateAlignment()) == 0); | 211 DCHECK((alignment % OS::AllocateAlignment()) == 0); |
| 186 size_t request_size = RoundUp(size + alignment, | 212 size_t request_size = |
| 187 static_cast<intptr_t>(OS::AllocateAlignment())); | 213 RoundUp(size + alignment, static_cast<intptr_t>(OS::AllocateAlignment())); |
| 188 void* reservation = mmap(OS::GetRandomMmapAddr(), | 214 void* reservation = |
| 189 request_size, | 215 mmapHelper(request_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, kMmapFd, |
| 190 PROT_NONE, | 216 kMmapFdOffset); |
| 191 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, | |
| 192 kMmapFd, | |
| 193 kMmapFdOffset); | |
| 194 if (reservation == MAP_FAILED) return; | 217 if (reservation == MAP_FAILED) return; |
| 195 | 218 |
| 196 uint8_t* base = static_cast<uint8_t*>(reservation); | 219 uint8_t* base = static_cast<uint8_t*>(reservation); |
| 197 uint8_t* aligned_base = RoundUp(base, alignment); | 220 uint8_t* aligned_base = RoundUp(base, alignment); |
| 198 DCHECK_LE(base, aligned_base); | 221 DCHECK_LE(base, aligned_base); |
| 199 | 222 |
| 200 // Unmap extra memory reserved before and after the desired block. | 223 // Unmap extra memory reserved before and after the desired block. |
| 201 if (aligned_base != base) { | 224 if (aligned_base != base) { |
| 202 size_t prefix_size = static_cast<size_t>(aligned_base - base); | 225 size_t prefix_size = static_cast<size_t>(aligned_base - base); |
| 203 OS::Free(base, prefix_size); | 226 OS::Free(base, prefix_size); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 222 | 245 |
| 223 VirtualMemory::~VirtualMemory() { | 246 VirtualMemory::~VirtualMemory() { |
| 224 if (IsReserved()) { | 247 if (IsReserved()) { |
| 225 bool result = ReleaseRegion(address(), size()); | 248 bool result = ReleaseRegion(address(), size()); |
| 226 DCHECK(result); | 249 DCHECK(result); |
| 227 USE(result); | 250 USE(result); |
| 228 } | 251 } |
| 229 } | 252 } |
| 230 | 253 |
| 231 | 254 |
| 232 bool VirtualMemory::IsReserved() { | 255 bool VirtualMemory::IsReserved() { return address_ != NULL; } |
| 233 return address_ != NULL; | |
| 234 } | |
| 235 | 256 |
| 236 | 257 |
| 237 void VirtualMemory::Reset() { | 258 void VirtualMemory::Reset() { |
| 238 address_ = NULL; | 259 address_ = NULL; |
| 239 size_ = 0; | 260 size_ = 0; |
| 240 } | 261 } |
| 241 | 262 |
| 242 | 263 |
| 243 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { | 264 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
| 244 return CommitRegion(address, size, is_executable); | 265 return CommitRegion(address, size, is_executable); |
| 245 } | 266 } |
| 246 | 267 |
| 247 | 268 |
| 248 bool VirtualMemory::Uncommit(void* address, size_t size) { | 269 bool VirtualMemory::Uncommit(void* address, size_t size) { |
| 249 return UncommitRegion(address, size); | 270 return UncommitRegion(address, size); |
| 250 } | 271 } |
| 251 | 272 |
| 252 | 273 |
| 253 bool VirtualMemory::Guard(void* address) { | 274 bool VirtualMemory::Guard(void* address) { |
| 254 OS::Guard(address, OS::CommitPageSize()); | 275 OS::Guard(address, OS::CommitPageSize()); |
| 255 return true; | 276 return true; |
| 256 } | 277 } |
| 257 | 278 |
| 258 | 279 |
| 259 void* VirtualMemory::ReserveRegion(size_t size) { | 280 void* VirtualMemory::ReserveRegion(size_t size) { |
| 260 void* result = mmap(OS::GetRandomMmapAddr(), | 281 void* result = mmapHelper(size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
| 261 size, | 282 kMmapFd, kMmapFdOffset); |
| 262 PROT_NONE, | |
| 263 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, | |
| 264 kMmapFd, | |
| 265 kMmapFdOffset); | |
| 266 | 283 |
| 267 if (result == MAP_FAILED) return NULL; | 284 if (result == MAP_FAILED) return NULL; |
| 268 | 285 |
| 269 return result; | 286 return result; |
| 270 } | 287 } |
| 271 | 288 |
| 272 | 289 |
| 273 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { | 290 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { |
| 291 #if defined(__native_client__) | |
| 292 // The Native Client port of V8 uses an interpreter, | |
| 293 // so code pages don't need PROT_EXEC. | |
| 294 int prot = PROT_READ | PROT_WRITE; | |
| 295 #else | |
| 274 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); | 296 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| 275 if (MAP_FAILED == mmap(base, | 297 #endif |
| 276 size, | 298 if (mprotect(base, size, prot) == -1) return false; |
| 277 prot, | 299 |
| 278 MAP_PRIVATE | MAP_ANON | MAP_FIXED, | |
| 279 kMmapFd, | |
| 280 kMmapFdOffset)) { | |
| 281 return false; | |
| 282 } | |
| 283 return true; | 300 return true; |
| 284 } | 301 } |
| 285 | 302 |
| 286 | 303 |
| 287 bool VirtualMemory::UncommitRegion(void* base, size_t size) { | 304 bool VirtualMemory::UncommitRegion(void* base, size_t size) { |
| 288 return mmap(base, | 305 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 } | 306 } |
| 295 | 307 |
| 296 | 308 |
| 297 bool VirtualMemory::ReleaseRegion(void* base, size_t size) { | 309 bool VirtualMemory::ReleaseRegion(void* base, size_t size) { |
| 298 return munmap(base, size) == 0; | 310 return munmap(base, size) == 0; |
| 299 } | 311 } |
| 300 | 312 |
| 301 | 313 |
| 302 bool VirtualMemory::HasLazyCommits() { | 314 bool VirtualMemory::HasLazyCommits() { return true; } |
| 303 // TODO(alph): implement for the platform. | |
| 304 return false; | |
| 305 } | 315 } |
| 306 | 316 } // namespace v8::base |
| 307 } } // namespace v8::base | |
| OLD | NEW |