Chromium Code Reviews| Index: src/base/platform/platform-aix.cc |
| diff --git a/src/base/platform/platform-freebsd.cc b/src/base/platform/platform-aix.cc |
| similarity index 60% |
| copy from src/base/platform/platform-freebsd.cc |
| copy to src/base/platform/platform-aix.cc |
| index 58316f8bc1a9abf0ce86549cc0976fce1120ea2d..7f0cd6f7b82f3d72c09411506efad555c4bb57ff 100644 |
| --- a/src/base/platform/platform-freebsd.cc |
| +++ b/src/base/platform/platform-aix.cc |
| @@ -1,29 +1,28 @@ |
| -// Copyright 2012 the V8 project authors. All rights reserved. |
| +// Copyright 2014 the V8 project authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -// Platform-specific code for FreeBSD goes here. For the POSIX-compatible |
| -// parts, the implementation is in platform-posix.cc. |
| +// Platform specific code for AIX goes here. For the POSIX comaptible parts |
| +// the implementation is in platform-posix.cc. |
| #include <pthread.h> |
| #include <semaphore.h> |
| #include <signal.h> |
| +#include <stdio.h> |
| #include <stdlib.h> |
| #include <sys/resource.h> |
| #include <sys/time.h> |
| -#include <sys/types.h> |
| #include <sys/ucontext.h> |
| -#include <sys/fcntl.h> // open |
| -#include <sys/mman.h> // mmap & munmap |
| -#include <sys/stat.h> // open |
| -#include <sys/types.h> // mmap & munmap |
| -#include <unistd.h> // getpagesize |
| -// If you don't have execinfo.h then you need devel/libexecinfo from ports. |
| #include <errno.h> |
| +#include <fcntl.h> // open |
| #include <limits.h> |
| #include <stdarg.h> |
| #include <strings.h> // index |
| +#include <sys/mman.h> // mmap & munmap |
| +#include <sys/stat.h> // open |
| +#include <sys/types.h> // mmap & munmap |
| +#include <unistd.h> // getpagesize |
| #include <cmath> |
| @@ -37,30 +36,60 @@ namespace v8 { |
| namespace base { |
| +static inline void* mmapHelper(size_t len, int prot, int flags, int fildes, |
| + off_t off) { |
| + void* addr = OS::GetRandomMmapAddr(); |
| +#if V8_TARGET_ARCH_PPC64 |
| + if (addr) { |
| + // We must use MAP_FIXED here to avoid the 0x07000000_00000000 |
| + // range, which causes loss of precision if addresses are |
| + // 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
|
| + DCHECK(!(flags & MAP_VARIABLE)); |
| + void* mbase; |
| + for (int i = 0; i < 10; i++) { |
| + mbase = mmap(addr, len, prot, flags | MAP_FIXED, fildes, off); |
| + if (mbase != MAP_FAILED) return mbase; |
| + // Try again with a different random address. |
| + addr = OS::GetRandomMmapAddr(); |
| + } |
| + } |
| +// Fall through if we can't get an address in the range we want |
| +// after multiple attempts -- just let the system decide (without |
| +// MAP_FIXED this time). |
| + |
| +// N.B. This case should never happen given the size of the |
| +// randomized range -- in fact looping at all is extremely rare. If |
| +// we ever do hit this case, it is better to let the system allocate |
| +// a high address (and bet against its unlikely consumption |
| +// introspectively as a double) than to fail outright. |
| +#endif |
| + return mmap(addr, len, prot, flags, fildes, off); |
| +} |
| + |
| + |
| const char* OS::LocalTimezone(double time, TimezoneCache* cache) { |
| if (std::isnan(time)) return ""; |
| - time_t tv = static_cast<time_t>(std::floor(time/msPerSecond)); |
| + time_t tv = static_cast<time_t>(floor(time / msPerSecond)); |
| struct tm* t = localtime(&tv); |
| if (NULL == t) return ""; |
| - return t->tm_zone; |
| + return tzname[0]; // The location of the timezone string on AIX. |
| } |
| double OS::LocalTimeOffset(TimezoneCache* cache) { |
| - time_t tv = time(NULL); |
| - struct tm* t = localtime(&tv); |
| - // tm_gmtoff includes any daylight savings offset, so subtract it. |
| - return static_cast<double>(t->tm_gmtoff * msPerSecond - |
| - (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); |
| + // On AIX, struct tm does not contain a tm_gmtoff field. |
| + time_t utc = time(NULL); |
| + DCHECK(utc != -1); |
| + struct tm* loc = localtime(&utc); |
| + DCHECK(loc != NULL); |
| + return static_cast<double>((mktime(loc) - utc) * msPerSecond); |
| } |
| -void* OS::Allocate(const size_t requested, |
| - size_t* allocated, |
| - bool executable) { |
| +void* OS::Allocate(const size_t requested, size_t* allocated, bool executable) { |
| const size_t msize = RoundUp(requested, getpagesize()); |
| int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); |
| - void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0); |
| + void* mbase = mmapHelper(msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| if (mbase == MAP_FAILED) return NULL; |
| *allocated = msize; |
| @@ -71,10 +100,11 @@ void* OS::Allocate(const size_t requested, |
| class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
| public: |
| PosixMemoryMappedFile(FILE* file, void* memory, int size) |
| - : file_(file), memory_(memory), size_(size) { } |
| + : file_(file), memory_(memory), size_(size) {} |
| virtual ~PosixMemoryMappedFile(); |
| virtual void* memory() { return memory_; } |
| virtual int size() { return size_; } |
| + |
| private: |
| FILE* file_; |
| void* memory_; |
| @@ -90,13 +120,13 @@ OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { |
| int size = ftell(file); |
| void* memory = |
| - mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| + mmapHelper(size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| return new PosixMemoryMappedFile(file, memory, size); |
| } |
| OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| - void* initial) { |
| + void* initial) { |
| FILE* file = fopen(name, "w+"); |
| if (file == NULL) return NULL; |
| int result = fwrite(initial, size, 1, file); |
| @@ -105,7 +135,7 @@ OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| return NULL; |
| } |
| void* memory = |
| - mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| + mmapHelper(size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| return new PosixMemoryMappedFile(file, memory, size); |
| } |
| @@ -131,23 +161,22 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { |
| addr_buffer[0] = '0'; |
| addr_buffer[1] = 'x'; |
| addr_buffer[10] = 0; |
| - ssize_t bytes_read = read(fd, addr_buffer + 2, 8); |
| - if (bytes_read < 8) break; |
| + 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
|
| + if (rc < 8) break; |
| unsigned start = StringToLong(addr_buffer); |
| - bytes_read = read(fd, addr_buffer + 2, 1); |
| - if (bytes_read < 1) break; |
| + rc = read(fd, addr_buffer + 2, 1); |
| + if (rc < 1) break; |
| if (addr_buffer[2] != '-') break; |
| - bytes_read = read(fd, addr_buffer + 2, 8); |
| - if (bytes_read < 8) break; |
| + rc = read(fd, addr_buffer + 2, 8); |
| + if (rc < 8) break; |
| unsigned end = StringToLong(addr_buffer); |
| char buffer[MAP_LENGTH]; |
| - bytes_read = -1; |
| + int bytes_read = -1; |
| do { |
| bytes_read++; |
| - if (bytes_read >= MAP_LENGTH - 1) |
| - break; |
| - bytes_read = read(fd, buffer + bytes_read, 1); |
| - if (bytes_read < 1) break; |
| + if (bytes_read >= MAP_LENGTH - 1) break; |
| + rc = read(fd, buffer + bytes_read, 1); |
| + if (rc < 1) break; |
| } while (buffer[bytes_read] != '\n'); |
| buffer[bytes_read] = 0; |
| // Ignore mappings that are not executable. |
| @@ -163,34 +192,28 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { |
| } |
| -void OS::SignalCodeMovingGC() { |
| -} |
| - |
| +void OS::SignalCodeMovingGC() {} |
| // Constants used for mmap. |
| static const int kMmapFd = -1; |
| static const int kMmapFdOffset = 0; |
| - |
| -VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } |
| +VirtualMemory::VirtualMemory() : address_(NULL), size_(0) {} |
| VirtualMemory::VirtualMemory(size_t size) |
| - : address_(ReserveRegion(size)), size_(size) { } |
| + : address_(ReserveRegion(size)), size_(size) {} |
| VirtualMemory::VirtualMemory(size_t size, size_t alignment) |
| : address_(NULL), size_(0) { |
| DCHECK((alignment % OS::AllocateAlignment()) == 0); |
| - size_t request_size = RoundUp(size + alignment, |
| - static_cast<intptr_t>(OS::AllocateAlignment())); |
| - void* reservation = mmap(OS::GetRandomMmapAddr(), |
| - request_size, |
| - PROT_NONE, |
| - MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
| - kMmapFd, |
| - kMmapFdOffset); |
| + size_t request_size = |
| + RoundUp(size + alignment, static_cast<intptr_t>(OS::AllocateAlignment())); |
| + void* reservation = |
| + mmapHelper(request_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, kMmapFd, |
| + kMmapFdOffset); |
| if (reservation == MAP_FAILED) return; |
| uint8_t* base = static_cast<uint8_t*>(reservation); |
| @@ -229,9 +252,7 @@ VirtualMemory::~VirtualMemory() { |
| } |
| -bool VirtualMemory::IsReserved() { |
| - return address_ != NULL; |
| -} |
| +bool VirtualMemory::IsReserved() { return address_ != NULL; } |
| void VirtualMemory::Reset() { |
| @@ -257,12 +278,8 @@ bool VirtualMemory::Guard(void* address) { |
| void* VirtualMemory::ReserveRegion(size_t size) { |
| - void* result = mmap(OS::GetRandomMmapAddr(), |
| - size, |
| - PROT_NONE, |
| - MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
| - kMmapFd, |
| - kMmapFdOffset); |
| + void* result = mmapHelper(size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
| + kMmapFd, kMmapFdOffset); |
| if (result == MAP_FAILED) return NULL; |
| @@ -271,26 +288,21 @@ void* VirtualMemory::ReserveRegion(size_t size) { |
| bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { |
| +#if defined(__native_client__) |
| + // The Native Client port of V8 uses an interpreter, |
| + // so code pages don't need PROT_EXEC. |
| + int prot = PROT_READ | PROT_WRITE; |
| +#else |
| int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| - if (MAP_FAILED == mmap(base, |
| - size, |
| - prot, |
| - MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
| - kMmapFd, |
| - kMmapFdOffset)) { |
| - return false; |
| - } |
| +#endif |
| + if (mprotect(base, size, prot) == -1) return false; |
| + |
| return true; |
| } |
| bool VirtualMemory::UncommitRegion(void* base, size_t size) { |
| - return mmap(base, |
| - size, |
| - PROT_NONE, |
| - MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED, |
| - kMmapFd, |
| - kMmapFdOffset) != MAP_FAILED; |
| + return mprotect(base, size, PROT_NONE) != -1; |
| } |
| @@ -299,9 +311,6 @@ bool VirtualMemory::ReleaseRegion(void* base, size_t size) { |
| } |
| -bool VirtualMemory::HasLazyCommits() { |
| - // TODO(alph): implement for the platform. |
| - return false; |
| +bool VirtualMemory::HasLazyCommits() { return true; } |
| } |
| - |
| -} } // namespace v8::base |
| +} // namespace v8::base |