f16f329fc35e33feeb114f7eb175c2465b768e55
[platform/upstream/v8.git] / src / base / platform / platform-macos.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 MacOS goes here. For the POSIX-compatible
6 // parts, the implementation is in platform-posix.cc.
7
8 #include <dlfcn.h>
9 #include <mach/mach_init.h>
10 #include <mach-o/dyld.h>
11 #include <mach-o/getsect.h>
12 #include <sys/mman.h>
13 #include <unistd.h>
14
15 #include <AvailabilityMacros.h>
16
17 #include <errno.h>
18 #include <libkern/OSAtomic.h>
19 #include <mach/mach.h>
20 #include <mach/semaphore.h>
21 #include <mach/task.h>
22 #include <mach/vm_statistics.h>
23 #include <pthread.h>
24 #include <semaphore.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/resource.h>
30 #include <sys/sysctl.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33
34 #include <cmath>
35
36 #undef MAP_TYPE
37
38 #include "src/base/macros.h"
39 #include "src/base/platform/platform.h"
40
41
42 namespace v8 {
43 namespace base {
44
45
46 // Constants used for mmap.
47 // kMmapFd is used to pass vm_alloc flags to tag the region with the user
48 // defined tag 255 This helps identify V8-allocated regions in memory analysis
49 // tools like vmmap(1).
50 static const int kMmapFd = VM_MAKE_TAG(255);
51 static const off_t kMmapFdOffset = 0;
52
53
54 void* OS::Allocate(const size_t requested,
55                    size_t* allocated,
56                    bool is_executable) {
57   const size_t msize = RoundUp(requested, getpagesize());
58   int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
59   void* mbase = mmap(OS::GetRandomMmapAddr(),
60                      msize,
61                      prot,
62                      MAP_PRIVATE | MAP_ANON,
63                      kMmapFd,
64                      kMmapFdOffset);
65   if (mbase == MAP_FAILED) return NULL;
66   *allocated = msize;
67   return mbase;
68 }
69
70
71 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
72   std::vector<SharedLibraryAddress> result;
73   unsigned int images_count = _dyld_image_count();
74   for (unsigned int i = 0; i < images_count; ++i) {
75     const mach_header* header = _dyld_get_image_header(i);
76     if (header == NULL) continue;
77 #if V8_HOST_ARCH_X64
78     uint64_t size;
79     char* code_ptr = getsectdatafromheader_64(
80         reinterpret_cast<const mach_header_64*>(header),
81         SEG_TEXT,
82         SECT_TEXT,
83         &size);
84 #else
85     unsigned int size;
86     char* code_ptr = getsectdatafromheader(header, SEG_TEXT, SECT_TEXT, &size);
87 #endif
88     if (code_ptr == NULL) continue;
89     const uintptr_t slide = _dyld_get_image_vmaddr_slide(i);
90     const uintptr_t start = reinterpret_cast<uintptr_t>(code_ptr) + slide;
91     result.push_back(
92         SharedLibraryAddress(_dyld_get_image_name(i), start, start + size));
93   }
94   return result;
95 }
96
97
98 void OS::SignalCodeMovingGC() {
99 }
100
101
102 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
103   if (std::isnan(time)) return "";
104   time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
105   struct tm* t = localtime(&tv);  // NOLINT(runtime/threadsafe_fn)
106   if (NULL == t) return "";
107   return t->tm_zone;
108 }
109
110
111 double OS::LocalTimeOffset(TimezoneCache* cache) {
112   time_t tv = time(NULL);
113   struct tm* t = localtime(&tv);  // NOLINT(runtime/threadsafe_fn)
114   // tm_gmtoff includes any daylight savings offset, so subtract it.
115   return static_cast<double>(t->tm_gmtoff * msPerSecond -
116                              (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
117 }
118
119
120 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
121
122
123 VirtualMemory::VirtualMemory(size_t size)
124     : address_(ReserveRegion(size)), size_(size) { }
125
126
127 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
128     : address_(NULL), size_(0) {
129   DCHECK((alignment % OS::AllocateAlignment()) == 0);
130   size_t request_size = RoundUp(size + alignment,
131                                 static_cast<intptr_t>(OS::AllocateAlignment()));
132   void* reservation = mmap(OS::GetRandomMmapAddr(),
133                            request_size,
134                            PROT_NONE,
135                            MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
136                            kMmapFd,
137                            kMmapFdOffset);
138   if (reservation == MAP_FAILED) return;
139
140   uint8_t* base = static_cast<uint8_t*>(reservation);
141   uint8_t* aligned_base = RoundUp(base, alignment);
142   DCHECK_LE(base, aligned_base);
143
144   // Unmap extra memory reserved before and after the desired block.
145   if (aligned_base != base) {
146     size_t prefix_size = static_cast<size_t>(aligned_base - base);
147     OS::Free(base, prefix_size);
148     request_size -= prefix_size;
149   }
150
151   size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
152   DCHECK_LE(aligned_size, request_size);
153
154   if (aligned_size != request_size) {
155     size_t suffix_size = request_size - aligned_size;
156     OS::Free(aligned_base + aligned_size, suffix_size);
157     request_size -= suffix_size;
158   }
159
160   DCHECK(aligned_size == request_size);
161
162   address_ = static_cast<void*>(aligned_base);
163   size_ = aligned_size;
164 }
165
166
167 VirtualMemory::~VirtualMemory() {
168   if (IsReserved()) {
169     bool result = ReleaseRegion(address(), size());
170     DCHECK(result);
171     USE(result);
172   }
173 }
174
175
176 bool VirtualMemory::IsReserved() {
177   return address_ != NULL;
178 }
179
180
181 void VirtualMemory::Reset() {
182   address_ = NULL;
183   size_ = 0;
184 }
185
186
187 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
188   return CommitRegion(address, size, is_executable);
189 }
190
191
192 bool VirtualMemory::Uncommit(void* address, size_t size) {
193   return UncommitRegion(address, size);
194 }
195
196
197 bool VirtualMemory::Guard(void* address) {
198   OS::Guard(address, OS::CommitPageSize());
199   return true;
200 }
201
202
203 void* VirtualMemory::ReserveRegion(size_t size) {
204   void* result = mmap(OS::GetRandomMmapAddr(),
205                       size,
206                       PROT_NONE,
207                       MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
208                       kMmapFd,
209                       kMmapFdOffset);
210
211   if (result == MAP_FAILED) return NULL;
212
213   return result;
214 }
215
216
217 bool VirtualMemory::CommitRegion(void* address,
218                                  size_t size,
219                                  bool is_executable) {
220   int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
221   if (MAP_FAILED == mmap(address,
222                          size,
223                          prot,
224                          MAP_PRIVATE | MAP_ANON | MAP_FIXED,
225                          kMmapFd,
226                          kMmapFdOffset)) {
227     return false;
228   }
229   return true;
230 }
231
232
233 bool VirtualMemory::UncommitRegion(void* address, size_t size) {
234   return mmap(address,
235               size,
236               PROT_NONE,
237               MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
238               kMmapFd,
239               kMmapFdOffset) != MAP_FAILED;
240 }
241
242
243 bool VirtualMemory::ReleaseRegion(void* address, size_t size) {
244   return munmap(address, size) == 0;
245 }
246
247
248 bool VirtualMemory::HasLazyCommits() {
249   return false;
250 }
251
252 } }  // namespace v8::base