Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / v8 / src / platform-solaris.cc
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.
4
5 // Platform-specific code for Solaris 10 goes here. For the POSIX-compatible
6 // parts, the implementation is in platform-posix.cc.
7
8 #ifdef __sparc
9 # error "V8 does not support the SPARC CPU architecture."
10 #endif
11
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
17 #include <pthread.h>
18 #include <semaphore.h>
19 #include <time.h>
20 #include <sys/time.h>  // gettimeofday(), timeradd()
21 #include <errno.h>
22 #include <ieeefp.h>  // finite()
23 #include <signal.h>  // sigemptyset(), etc
24 #include <sys/regset.h>
25
26
27 #undef MAP_TYPE
28
29 #include "v8.h"
30
31 #include "platform.h"
32 #include "v8threads.h"
33
34
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.
38 #ifndef signbit
39 namespace std {
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
43   // versions of zero.
44   if (x == 0) {
45     return fpclass(x) & FP_NZERO;
46   } else {
47     // This won't detect negative NaN but that should be okay since we don't
48     // assume that behavior.
49     return x < 0;
50   }
51 }
52 }  // namespace std
53 #endif  // signbit
54
55 namespace v8 {
56 namespace internal {
57
58
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.
65 }
66
67
68 double OS::LocalTimeOffset(TimezoneCache* cache) {
69   tzset();
70   return -static_cast<double>(timezone * msPerSecond);
71 }
72
73
74 void* OS::Allocate(const size_t requested,
75                    size_t* allocated,
76                    bool is_executable) {
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);
80
81   if (mbase == MAP_FAILED) {
82     LOG(Isolate::Current(), StringEvent("OS::Allocate", "mmap failed"));
83     return NULL;
84   }
85   *allocated = msize;
86   return mbase;
87 }
88
89
90 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
91  public:
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_; }
97  private:
98   FILE* file_;
99   void* memory_;
100   int size_;
101 };
102
103
104 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
105   FILE* file = fopen(name, "r+");
106   if (file == NULL) return NULL;
107
108   fseek(file, 0, SEEK_END);
109   int size = ftell(file);
110
111   void* memory =
112       mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
113   return new PosixMemoryMappedFile(file, memory, size);
114 }
115
116
117 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
118     void* initial) {
119   FILE* file = fopen(name, "w+");
120   if (file == NULL) return NULL;
121   int result = fwrite(initial, size, 1, file);
122   if (result < 1) {
123     fclose(file);
124     return NULL;
125   }
126   void* memory =
127       mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
128   return new PosixMemoryMappedFile(file, memory, size);
129 }
130
131
132 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
133   if (memory_) munmap(memory_, size_);
134   fclose(file_);
135 }
136
137
138 void OS::LogSharedLibraryAddresses(Isolate* isolate) {
139 }
140
141
142 void OS::SignalCodeMovingGC() {
143 }
144
145
146 struct StackWalker {
147   Vector<OS::StackFrame>& frames;
148   int index;
149 };
150
151
152 static int StackWalkCallback(uintptr_t pc, int signo, void* data) {
153   struct StackWalker* walker = static_cast<struct StackWalker*>(data);
154   Dl_info info;
155
156   int i = walker->index;
157
158   walker->frames[i].address = reinterpret_cast<void*>(pc);
159
160   // Make sure line termination is in place.
161   walker->frames[i].text[OS::kStackWalkMaxTextLen - 1] = '\0';
162
163   Vector<char> text = MutableCStrVector(walker->frames[i].text,
164                                         OS::kStackWalkMaxTextLen);
165
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);
171   } else {
172     // No local symbol info.
173     OS::SNPrintF(text,
174                  "%s'0x%p [0x%p]",
175                  info.dli_fname,
176                  pc - reinterpret_cast<uintptr_t>(info.dli_fbase),
177                  pc);
178   }
179   walker->index++;
180   return 0;
181 }
182
183
184 // Constants used for mmap.
185 static const int kMmapFd = -1;
186 static const int kMmapFdOffset = 0;
187
188
189 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
190
191
192 VirtualMemory::VirtualMemory(size_t size)
193     : address_(ReserveRegion(size)), size_(size) { }
194
195
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(),
202                            request_size,
203                            PROT_NONE,
204                            MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
205                            kMmapFd,
206                            kMmapFdOffset);
207   if (reservation == MAP_FAILED) return;
208
209   Address base = static_cast<Address>(reservation);
210   Address aligned_base = RoundUp(base, alignment);
211   ASSERT_LE(base, aligned_base);
212
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;
218   }
219
220   size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
221   ASSERT_LE(aligned_size, request_size);
222
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;
227   }
228
229   ASSERT(aligned_size == request_size);
230
231   address_ = static_cast<void*>(aligned_base);
232   size_ = aligned_size;
233 }
234
235
236 VirtualMemory::~VirtualMemory() {
237   if (IsReserved()) {
238     bool result = ReleaseRegion(address(), size());
239     ASSERT(result);
240     USE(result);
241   }
242 }
243
244
245 bool VirtualMemory::IsReserved() {
246   return address_ != NULL;
247 }
248
249
250 void VirtualMemory::Reset() {
251   address_ = NULL;
252   size_ = 0;
253 }
254
255
256 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
257   return CommitRegion(address, size, is_executable);
258 }
259
260
261 bool VirtualMemory::Uncommit(void* address, size_t size) {
262   return UncommitRegion(address, size);
263 }
264
265
266 bool VirtualMemory::Guard(void* address) {
267   OS::Guard(address, OS::CommitPageSize());
268   return true;
269 }
270
271
272 void* VirtualMemory::ReserveRegion(size_t size) {
273   void* result = mmap(OS::GetRandomMmapAddr(),
274                       size,
275                       PROT_NONE,
276                       MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
277                       kMmapFd,
278                       kMmapFdOffset);
279
280   if (result == MAP_FAILED) return NULL;
281
282   return result;
283 }
284
285
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,
289                          size,
290                          prot,
291                          MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
292                          kMmapFd,
293                          kMmapFdOffset)) {
294     return false;
295   }
296   return true;
297 }
298
299
300 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
301   return mmap(base,
302               size,
303               PROT_NONE,
304               MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED,
305               kMmapFd,
306               kMmapFdOffset) != MAP_FAILED;
307 }
308
309
310 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
311   return munmap(base, size) == 0;
312 }
313
314
315 bool VirtualMemory::HasLazyCommits() {
316   // TODO(alph): implement for the platform.
317   return false;
318 }
319
320 } }  // namespace v8::internal