Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / v8 / src / platform-openbsd.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 OpenBSD and NetBSD goes here. For the
6 // POSIX-compatible parts, the implementation is in platform-posix.cc.
7
8 #include <pthread.h>
9 #include <semaphore.h>
10 #include <signal.h>
11 #include <sys/time.h>
12 #include <sys/resource.h>
13 #include <sys/syscall.h>
14 #include <sys/types.h>
15 #include <stdlib.h>
16
17 #include <sys/types.h>  // mmap & munmap
18 #include <sys/mman.h>   // mmap & munmap
19 #include <sys/stat.h>   // open
20 #include <fcntl.h>      // open
21 #include <unistd.h>     // sysconf
22 #include <strings.h>    // index
23 #include <errno.h>
24 #include <stdarg.h>
25
26 #undef MAP_TYPE
27
28 #include "v8.h"
29
30 #include "platform.h"
31 #include "v8threads.h"
32
33
34 namespace v8 {
35 namespace internal {
36
37
38 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
39   if (std::isnan(time)) return "";
40   time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
41   struct tm* t = localtime(&tv);
42   if (NULL == t) return "";
43   return t->tm_zone;
44 }
45
46
47 double OS::LocalTimeOffset(TimezoneCache* cache) {
48   time_t tv = time(NULL);
49   struct tm* t = localtime(&tv);
50   // tm_gmtoff includes any daylight savings offset, so subtract it.
51   return static_cast<double>(t->tm_gmtoff * msPerSecond -
52                              (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
53 }
54
55
56 void* OS::Allocate(const size_t requested,
57                    size_t* allocated,
58                    bool is_executable) {
59   const size_t msize = RoundUp(requested, AllocateAlignment());
60   int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
61   void* addr = OS::GetRandomMmapAddr();
62   void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
63   if (mbase == MAP_FAILED) {
64     LOG(i::Isolate::Current(),
65         StringEvent("OS::Allocate", "mmap failed"));
66     return NULL;
67   }
68   *allocated = msize;
69   return mbase;
70 }
71
72
73 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
74  public:
75   PosixMemoryMappedFile(FILE* file, void* memory, int size)
76     : file_(file), memory_(memory), size_(size) { }
77   virtual ~PosixMemoryMappedFile();
78   virtual void* memory() { return memory_; }
79   virtual int size() { return size_; }
80  private:
81   FILE* file_;
82   void* memory_;
83   int size_;
84 };
85
86
87 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
88   FILE* file = fopen(name, "r+");
89   if (file == NULL) return NULL;
90
91   fseek(file, 0, SEEK_END);
92   int size = ftell(file);
93
94   void* memory =
95       mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
96   return new PosixMemoryMappedFile(file, memory, size);
97 }
98
99
100 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
101     void* initial) {
102   FILE* file = fopen(name, "w+");
103   if (file == NULL) return NULL;
104   int result = fwrite(initial, size, 1, file);
105   if (result < 1) {
106     fclose(file);
107     return NULL;
108   }
109   void* memory =
110       mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
111   return new PosixMemoryMappedFile(file, memory, size);
112 }
113
114
115 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
116   if (memory_) OS::Free(memory_, size_);
117   fclose(file_);
118 }
119
120
121 void OS::LogSharedLibraryAddresses(Isolate* isolate) {
122   // This function assumes that the layout of the file is as follows:
123   // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
124   // If we encounter an unexpected situation we abort scanning further entries.
125   FILE* fp = fopen("/proc/self/maps", "r");
126   if (fp == NULL) return;
127
128   // Allocate enough room to be able to store a full file name.
129   const int kLibNameLen = FILENAME_MAX + 1;
130   char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
131
132   // This loop will terminate once the scanning hits an EOF.
133   while (true) {
134     uintptr_t start, end;
135     char attr_r, attr_w, attr_x, attr_p;
136     // Parse the addresses and permission bits at the beginning of the line.
137     if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
138     if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
139
140     int c;
141     if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
142       // Found a read-only executable entry. Skip characters until we reach
143       // the beginning of the filename or the end of the line.
144       do {
145         c = getc(fp);
146       } while ((c != EOF) && (c != '\n') && (c != '/'));
147       if (c == EOF) break;  // EOF: Was unexpected, just exit.
148
149       // Process the filename if found.
150       if (c == '/') {
151         ungetc(c, fp);  // Push the '/' back into the stream to be read below.
152
153         // Read to the end of the line. Exit if the read fails.
154         if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
155
156         // Drop the newline character read by fgets. We do not need to check
157         // for a zero-length string because we know that we at least read the
158         // '/' character.
159         lib_name[strlen(lib_name) - 1] = '\0';
160       } else {
161         // No library name found, just record the raw address range.
162         snprintf(lib_name, kLibNameLen,
163                  "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
164       }
165       LOG(isolate, SharedLibraryEvent(lib_name, start, end));
166     } else {
167       // Entry not describing executable data. Skip to end of line to set up
168       // reading the next entry.
169       do {
170         c = getc(fp);
171       } while ((c != EOF) && (c != '\n'));
172       if (c == EOF) break;
173     }
174   }
175   free(lib_name);
176   fclose(fp);
177 }
178
179
180 void OS::SignalCodeMovingGC() {
181   // Support for ll_prof.py.
182   //
183   // The Linux profiler built into the kernel logs all mmap's with
184   // PROT_EXEC so that analysis tools can properly attribute ticks. We
185   // do a mmap with a name known by ll_prof.py and immediately munmap
186   // it. This injects a GC marker into the stream of events generated
187   // by the kernel and allows us to synchronize V8 code log and the
188   // kernel log.
189   int size = sysconf(_SC_PAGESIZE);
190   FILE* f = fopen(FLAG_gc_fake_mmap, "w+");
191   if (f == NULL) {
192     OS::PrintError("Failed to open %s\n", FLAG_gc_fake_mmap);
193     OS::Abort();
194   }
195   void* addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE,
196                     fileno(f), 0);
197   ASSERT(addr != MAP_FAILED);
198   OS::Free(addr, size);
199   fclose(f);
200 }
201
202
203
204 // Constants used for mmap.
205 static const int kMmapFd = -1;
206 static const int kMmapFdOffset = 0;
207
208
209 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
210
211
212 VirtualMemory::VirtualMemory(size_t size)
213     : address_(ReserveRegion(size)), size_(size) { }
214
215
216 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
217     : address_(NULL), size_(0) {
218   ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
219   size_t request_size = RoundUp(size + alignment,
220                                 static_cast<intptr_t>(OS::AllocateAlignment()));
221   void* reservation = mmap(OS::GetRandomMmapAddr(),
222                            request_size,
223                            PROT_NONE,
224                            MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
225                            kMmapFd,
226                            kMmapFdOffset);
227   if (reservation == MAP_FAILED) return;
228
229   Address base = static_cast<Address>(reservation);
230   Address aligned_base = RoundUp(base, alignment);
231   ASSERT_LE(base, aligned_base);
232
233   // Unmap extra memory reserved before and after the desired block.
234   if (aligned_base != base) {
235     size_t prefix_size = static_cast<size_t>(aligned_base - base);
236     OS::Free(base, prefix_size);
237     request_size -= prefix_size;
238   }
239
240   size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
241   ASSERT_LE(aligned_size, request_size);
242
243   if (aligned_size != request_size) {
244     size_t suffix_size = request_size - aligned_size;
245     OS::Free(aligned_base + aligned_size, suffix_size);
246     request_size -= suffix_size;
247   }
248
249   ASSERT(aligned_size == request_size);
250
251   address_ = static_cast<void*>(aligned_base);
252   size_ = aligned_size;
253 }
254
255
256 VirtualMemory::~VirtualMemory() {
257   if (IsReserved()) {
258     bool result = ReleaseRegion(address(), size());
259     ASSERT(result);
260     USE(result);
261   }
262 }
263
264
265 bool VirtualMemory::IsReserved() {
266   return address_ != NULL;
267 }
268
269
270 void VirtualMemory::Reset() {
271   address_ = NULL;
272   size_ = 0;
273 }
274
275
276 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
277   return CommitRegion(address, size, is_executable);
278 }
279
280
281 bool VirtualMemory::Uncommit(void* address, size_t size) {
282   return UncommitRegion(address, size);
283 }
284
285
286 bool VirtualMemory::Guard(void* address) {
287   OS::Guard(address, OS::CommitPageSize());
288   return true;
289 }
290
291
292 void* VirtualMemory::ReserveRegion(size_t size) {
293   void* result = mmap(OS::GetRandomMmapAddr(),
294                       size,
295                       PROT_NONE,
296                       MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
297                       kMmapFd,
298                       kMmapFdOffset);
299
300   if (result == MAP_FAILED) return NULL;
301
302   return result;
303 }
304
305
306 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
307   int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
308   if (MAP_FAILED == mmap(base,
309                          size,
310                          prot,
311                          MAP_PRIVATE | MAP_ANON | MAP_FIXED,
312                          kMmapFd,
313                          kMmapFdOffset)) {
314     return false;
315   }
316   return true;
317 }
318
319
320 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
321   return mmap(base,
322               size,
323               PROT_NONE,
324               MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
325               kMmapFd,
326               kMmapFdOffset) != MAP_FAILED;
327 }
328
329
330 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
331   return munmap(base, size) == 0;
332 }
333
334
335 bool VirtualMemory::HasLazyCommits() {
336   // TODO(alph): implement for the platform.
337   return false;
338 }
339
340 } }  // namespace v8::internal