1 // Copyright 2012 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 Solaris 10 goes here. For the POSIX-compatible
6 // parts, the implementation is in platform-posix.cc.
9 # error "V8 does not support the SPARC CPU architecture."
12 #include <dlfcn.h> // dladdr
14 #include <ieeefp.h> // finite()
16 #include <semaphore.h>
17 #include <signal.h> // sigemptyset(), etc
18 #include <sys/mman.h> // mmap()
19 #include <sys/regset.h>
20 #include <sys/stack.h> // for stack alignment
21 #include <sys/time.h> // gettimeofday(), timeradd()
23 #include <ucontext.h> // walkstack(), getcontext()
24 #include <unistd.h> // getpagesize(), usleep()
30 #include "src/base/macros.h"
31 #include "src/base/platform/platform.h"
34 // It seems there is a bug in some Solaris distributions (experienced in
35 // SunOS 5.10 Generic_141445-09) which make it difficult or impossible to
36 // access signbit() despite the availability of other C99 math functions.
39 // Test sign - usually defined in math.h
40 int signbit(double x) {
41 // We need to take care of the special case of both positive and negative
44 return fpclass(x) & FP_NZERO;
46 // This won't detect negative NaN but that should be okay since we don't
47 // assume that behavior.
58 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
59 if (std::isnan(time)) return "";
60 time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
61 struct tm* t = localtime(&tv);
62 if (NULL == t) return "";
63 return tzname[0]; // The location of the timezone string on Solaris.
67 double OS::LocalTimeOffset(TimezoneCache* cache) {
69 return -static_cast<double>(timezone * msPerSecond);
73 void* OS::Allocate(const size_t requested,
76 const size_t msize = RoundUp(requested, getpagesize());
77 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
78 void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
80 if (mbase == MAP_FAILED) return NULL;
86 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
88 PosixMemoryMappedFile(FILE* file, void* memory, int size)
89 : file_(file), memory_(memory), size_(size) { }
90 virtual ~PosixMemoryMappedFile();
91 virtual void* memory() { return memory_; }
92 virtual int size() { return size_; }
100 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
101 FILE* file = fopen(name, "r+");
102 if (file == NULL) return NULL;
104 fseek(file, 0, SEEK_END);
105 int size = ftell(file);
108 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
109 return new PosixMemoryMappedFile(file, memory, size);
113 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
115 FILE* file = fopen(name, "w+");
116 if (file == NULL) return NULL;
117 int result = fwrite(initial, size, 1, file);
123 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
124 return new PosixMemoryMappedFile(file, memory, size);
128 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
129 if (memory_) munmap(memory_, size_);
134 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
135 return std::vector<SharedLibraryAddress>();
139 void OS::SignalCodeMovingGC() {
143 // Constants used for mmap.
144 static const int kMmapFd = -1;
145 static const int kMmapFdOffset = 0;
148 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
151 VirtualMemory::VirtualMemory(size_t size)
152 : address_(ReserveRegion(size)), size_(size) { }
155 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
156 : address_(NULL), size_(0) {
157 DCHECK(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
158 size_t request_size = RoundUp(size + alignment,
159 static_cast<intptr_t>(OS::AllocateAlignment()));
160 void* reservation = mmap(OS::GetRandomMmapAddr(),
163 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
166 if (reservation == MAP_FAILED) return;
168 uint8_t* base = static_cast<uint8_t*>(reservation);
169 uint8_t* aligned_base = RoundUp(base, alignment);
170 DCHECK_LE(base, aligned_base);
172 // Unmap extra memory reserved before and after the desired block.
173 if (aligned_base != base) {
174 size_t prefix_size = static_cast<size_t>(aligned_base - base);
175 OS::Free(base, prefix_size);
176 request_size -= prefix_size;
179 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
180 DCHECK_LE(aligned_size, request_size);
182 if (aligned_size != request_size) {
183 size_t suffix_size = request_size - aligned_size;
184 OS::Free(aligned_base + aligned_size, suffix_size);
185 request_size -= suffix_size;
188 DCHECK(aligned_size == request_size);
190 address_ = static_cast<void*>(aligned_base);
191 size_ = aligned_size;
195 VirtualMemory::~VirtualMemory() {
197 bool result = ReleaseRegion(address(), size());
204 bool VirtualMemory::IsReserved() {
205 return address_ != NULL;
209 void VirtualMemory::Reset() {
215 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
216 return CommitRegion(address, size, is_executable);
220 bool VirtualMemory::Uncommit(void* address, size_t size) {
221 return UncommitRegion(address, size);
225 bool VirtualMemory::Guard(void* address) {
226 OS::Guard(address, OS::CommitPageSize());
231 void* VirtualMemory::ReserveRegion(size_t size) {
232 void* result = mmap(OS::GetRandomMmapAddr(),
235 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
239 if (result == MAP_FAILED) return NULL;
245 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
246 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
247 if (MAP_FAILED == mmap(base,
250 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
259 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
263 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED,
265 kMmapFdOffset) != MAP_FAILED;
269 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
270 return munmap(base, size) == 0;
274 bool VirtualMemory::HasLazyCommits() {
275 // TODO(alph): implement for the platform.
279 } } // namespace v8::base