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 <sys/stack.h> // for stack alignment
13 #include <unistd.h> // getpagesize(), usleep()
14 #include <sys/mman.h> // mmap()
15 #include <ucontext.h> // walkstack(), getcontext()
16 #include <dlfcn.h> // dladdr
18 #include <semaphore.h>
20 #include <sys/time.h> // gettimeofday(), timeradd()
22 #include <ieeefp.h> // finite()
23 #include <signal.h> // sigemptyset(), etc
24 #include <sys/regset.h>
32 #include "v8threads.h"
35 // It seems there is a bug in some Solaris distributions (experienced in
36 // SunOS 5.10 Generic_141445-09) which make it difficult or impossible to
37 // access signbit() despite the availability of other C99 math functions.
40 // Test sign - usually defined in math.h
41 int signbit(double x) {
42 // We need to take care of the special case of both positive and negative
45 return fpclass(x) & FP_NZERO;
47 // This won't detect negative NaN but that should be okay since we don't
48 // assume that behavior.
59 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
60 if (std::isnan(time)) return "";
61 time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
62 struct tm* t = localtime(&tv);
63 if (NULL == t) return "";
64 return tzname[0]; // The location of the timezone string on Solaris.
68 double OS::LocalTimeOffset(TimezoneCache* cache) {
70 return -static_cast<double>(timezone * msPerSecond);
74 void* OS::Allocate(const size_t requested,
77 const size_t msize = RoundUp(requested, getpagesize());
78 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
79 void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
81 if (mbase == MAP_FAILED) {
82 LOG(Isolate::Current(), StringEvent("OS::Allocate", "mmap failed"));
90 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
92 PosixMemoryMappedFile(FILE* file, void* memory, int size)
93 : file_(file), memory_(memory), size_(size) { }
94 virtual ~PosixMemoryMappedFile();
95 virtual void* memory() { return memory_; }
96 virtual int size() { return size_; }
104 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
105 FILE* file = fopen(name, "r+");
106 if (file == NULL) return NULL;
108 fseek(file, 0, SEEK_END);
109 int size = ftell(file);
112 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
113 return new PosixMemoryMappedFile(file, memory, size);
117 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
119 FILE* file = fopen(name, "w+");
120 if (file == NULL) return NULL;
121 int result = fwrite(initial, size, 1, file);
127 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
128 return new PosixMemoryMappedFile(file, memory, size);
132 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
133 if (memory_) munmap(memory_, size_);
138 void OS::LogSharedLibraryAddresses(Isolate* isolate) {
142 void OS::SignalCodeMovingGC() {
147 Vector<OS::StackFrame>& frames;
152 static int StackWalkCallback(uintptr_t pc, int signo, void* data) {
153 struct StackWalker* walker = static_cast<struct StackWalker*>(data);
156 int i = walker->index;
158 walker->frames[i].address = reinterpret_cast<void*>(pc);
160 // Make sure line termination is in place.
161 walker->frames[i].text[OS::kStackWalkMaxTextLen - 1] = '\0';
163 Vector<char> text = MutableCStrVector(walker->frames[i].text,
164 OS::kStackWalkMaxTextLen);
166 if (dladdr(reinterpret_cast<void*>(pc), &info) == 0) {
167 OS::SNPrintF(text, "[0x%p]", pc);
168 } else if ((info.dli_fname != NULL && info.dli_sname != NULL)) {
169 // We have symbol info.
170 OS::SNPrintF(text, "%s'%s+0x%x", info.dli_fname, info.dli_sname, pc);
172 // No local symbol info.
176 pc - reinterpret_cast<uintptr_t>(info.dli_fbase),
184 // Constants used for mmap.
185 static const int kMmapFd = -1;
186 static const int kMmapFdOffset = 0;
189 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
192 VirtualMemory::VirtualMemory(size_t size)
193 : address_(ReserveRegion(size)), size_(size) { }
196 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
197 : address_(NULL), size_(0) {
198 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
199 size_t request_size = RoundUp(size + alignment,
200 static_cast<intptr_t>(OS::AllocateAlignment()));
201 void* reservation = mmap(OS::GetRandomMmapAddr(),
204 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
207 if (reservation == MAP_FAILED) return;
209 Address base = static_cast<Address>(reservation);
210 Address aligned_base = RoundUp(base, alignment);
211 ASSERT_LE(base, aligned_base);
213 // Unmap extra memory reserved before and after the desired block.
214 if (aligned_base != base) {
215 size_t prefix_size = static_cast<size_t>(aligned_base - base);
216 OS::Free(base, prefix_size);
217 request_size -= prefix_size;
220 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
221 ASSERT_LE(aligned_size, request_size);
223 if (aligned_size != request_size) {
224 size_t suffix_size = request_size - aligned_size;
225 OS::Free(aligned_base + aligned_size, suffix_size);
226 request_size -= suffix_size;
229 ASSERT(aligned_size == request_size);
231 address_ = static_cast<void*>(aligned_base);
232 size_ = aligned_size;
236 VirtualMemory::~VirtualMemory() {
238 bool result = ReleaseRegion(address(), size());
245 bool VirtualMemory::IsReserved() {
246 return address_ != NULL;
250 void VirtualMemory::Reset() {
256 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
257 return CommitRegion(address, size, is_executable);
261 bool VirtualMemory::Uncommit(void* address, size_t size) {
262 return UncommitRegion(address, size);
266 bool VirtualMemory::Guard(void* address) {
267 OS::Guard(address, OS::CommitPageSize());
272 void* VirtualMemory::ReserveRegion(size_t size) {
273 void* result = mmap(OS::GetRandomMmapAddr(),
276 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
280 if (result == MAP_FAILED) return NULL;
286 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
287 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
288 if (MAP_FAILED == mmap(base,
291 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
300 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
304 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED,
306 kMmapFdOffset) != MAP_FAILED;
310 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
311 return munmap(base, size) == 0;
315 bool VirtualMemory::HasLazyCommits() {
316 // TODO(alph): implement for the platform.
320 } } // namespace v8::internal