blob: 103fd6ce054e6fe17cfbff9fbf97e38567a27ab9 [file] [log] [blame]
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00001// Copyright 2012 the V8 project authors. All rights reserved.
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000028// Platform specific code for FreeBSD goes here. For the POSIX comaptible parts
29// the implementation is in platform-posix.cc.
ager@chromium.orga74f0da2008-12-03 16:05:52 +000030
31#include <pthread.h>
32#include <semaphore.h>
33#include <signal.h>
34#include <sys/time.h>
35#include <sys/resource.h>
ager@chromium.org381abbb2009-02-25 13:23:22 +000036#include <sys/types.h>
ager@chromium.orga74f0da2008-12-03 16:05:52 +000037#include <sys/ucontext.h>
38#include <stdlib.h>
39
40#include <sys/types.h> // mmap & munmap
41#include <sys/mman.h> // mmap & munmap
42#include <sys/stat.h> // open
43#include <sys/fcntl.h> // open
44#include <unistd.h> // getpagesize
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +000045// If you don't have execinfo.h then you need devel/libexecinfo from ports.
ager@chromium.orga74f0da2008-12-03 16:05:52 +000046#include <strings.h> // index
47#include <errno.h>
48#include <stdarg.h>
49#include <limits.h>
50
51#undef MAP_TYPE
52
53#include "v8.h"
lrn@chromium.org1c092762011-05-09 09:42:16 +000054#include "v8threads.h"
ager@chromium.orga74f0da2008-12-03 16:05:52 +000055
56#include "platform.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000057#include "vm-state-inl.h"
ager@chromium.orga74f0da2008-12-03 16:05:52 +000058
59
kasperl@chromium.org71affb52009-05-26 05:44:31 +000060namespace v8 {
61namespace internal {
ager@chromium.orga74f0da2008-12-03 16:05:52 +000062
ager@chromium.orga74f0da2008-12-03 16:05:52 +000063
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000064const char* OS::LocalTimezone(double time) {
ulan@chromium.org77ca49a2013-04-22 09:43:56 +000065 if (std::isnan(time)) return "";
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000066 time_t tv = static_cast<time_t>(floor(time/msPerSecond));
67 struct tm* t = localtime(&tv);
68 if (NULL == t) return "";
69 return t->tm_zone;
70}
71
72
73double OS::LocalTimeOffset() {
74 time_t tv = time(NULL);
75 struct tm* t = localtime(&tv);
76 // tm_gmtoff includes any daylight savings offset, so subtract it.
77 return static_cast<double>(t->tm_gmtoff * msPerSecond -
78 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
79}
80
81
ager@chromium.orga74f0da2008-12-03 16:05:52 +000082void* OS::Allocate(const size_t requested,
83 size_t* allocated,
84 bool executable) {
85 const size_t msize = RoundUp(requested, getpagesize());
86 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0);
87 void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
88
89 if (mbase == MAP_FAILED) {
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +000090 LOG(Isolate::Current(), StringEvent("OS::Allocate", "mmap failed"));
ager@chromium.orga74f0da2008-12-03 16:05:52 +000091 return NULL;
92 }
93 *allocated = msize;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000094 return mbase;
95}
96
97
ager@chromium.orga74f0da2008-12-03 16:05:52 +000098class PosixMemoryMappedFile : public OS::MemoryMappedFile {
99 public:
100 PosixMemoryMappedFile(FILE* file, void* memory, int size)
101 : file_(file), memory_(memory), size_(size) { }
102 virtual ~PosixMemoryMappedFile();
103 virtual void* memory() { return memory_; }
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000104 virtual int size() { return size_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000105 private:
106 FILE* file_;
107 void* memory_;
108 int size_;
109};
110
111
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000112OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000113 FILE* file = fopen(name, "r+");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000114 if (file == NULL) return NULL;
115
116 fseek(file, 0, SEEK_END);
117 int size = ftell(file);
118
119 void* memory =
120 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
121 return new PosixMemoryMappedFile(file, memory, size);
122}
123
124
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000125OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
126 void* initial) {
127 FILE* file = fopen(name, "w+");
128 if (file == NULL) return NULL;
129 int result = fwrite(initial, size, 1, file);
130 if (result < 1) {
131 fclose(file);
132 return NULL;
133 }
134 void* memory =
135 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
136 return new PosixMemoryMappedFile(file, memory, size);
137}
138
139
140PosixMemoryMappedFile::~PosixMemoryMappedFile() {
141 if (memory_) munmap(memory_, size_);
142 fclose(file_);
143}
144
145
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000146static unsigned StringToLong(char* buffer) {
147 return static_cast<unsigned>(strtol(buffer, NULL, 16)); // NOLINT
148}
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000149
150
dslomov@chromium.orge97852d2013-09-12 09:02:59 +0000151void OS::LogSharedLibraryAddresses(Isolate* isolate) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000152 static const int MAP_LENGTH = 1024;
153 int fd = open("/proc/self/maps", O_RDONLY);
154 if (fd < 0) return;
155 while (true) {
156 char addr_buffer[11];
157 addr_buffer[0] = '0';
158 addr_buffer[1] = 'x';
159 addr_buffer[10] = 0;
160 int result = read(fd, addr_buffer + 2, 8);
161 if (result < 8) break;
162 unsigned start = StringToLong(addr_buffer);
163 result = read(fd, addr_buffer + 2, 1);
164 if (result < 1) break;
165 if (addr_buffer[2] != '-') break;
166 result = read(fd, addr_buffer + 2, 8);
167 if (result < 8) break;
168 unsigned end = StringToLong(addr_buffer);
169 char buffer[MAP_LENGTH];
170 int bytes_read = -1;
171 do {
172 bytes_read++;
173 if (bytes_read >= MAP_LENGTH - 1)
174 break;
175 result = read(fd, buffer + bytes_read, 1);
176 if (result < 1) break;
177 } while (buffer[bytes_read] != '\n');
178 buffer[bytes_read] = 0;
179 // Ignore mappings that are not executable.
180 if (buffer[3] != 'x') continue;
181 char* start_of_path = index(buffer, '/');
182 // There may be no filename in this line. Skip to next.
183 if (start_of_path == NULL) continue;
184 buffer[bytes_read] = 0;
dslomov@chromium.orge97852d2013-09-12 09:02:59 +0000185 LOG(isolate SharedLibraryEvent(start_of_path, start, end));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000186 }
187 close(fd);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000188}
189
190
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000191void OS::SignalCodeMovingGC() {
192}
193
194
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000195
196// Constants used for mmap.
197static const int kMmapFd = -1;
198static const int kMmapFdOffset = 0;
199
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000200
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000201VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000202
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000203
204VirtualMemory::VirtualMemory(size_t size)
205 : address_(ReserveRegion(size)), size_(size) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000206
207
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000208VirtualMemory::VirtualMemory(size_t size, size_t alignment)
209 : address_(NULL), size_(0) {
210 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
211 size_t request_size = RoundUp(size + alignment,
212 static_cast<intptr_t>(OS::AllocateAlignment()));
213 void* reservation = mmap(OS::GetRandomMmapAddr(),
214 request_size,
215 PROT_NONE,
216 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
217 kMmapFd,
218 kMmapFdOffset);
219 if (reservation == MAP_FAILED) return;
220
221 Address base = static_cast<Address>(reservation);
222 Address aligned_base = RoundUp(base, alignment);
223 ASSERT_LE(base, aligned_base);
224
225 // Unmap extra memory reserved before and after the desired block.
226 if (aligned_base != base) {
227 size_t prefix_size = static_cast<size_t>(aligned_base - base);
228 OS::Free(base, prefix_size);
229 request_size -= prefix_size;
230 }
231
232 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
233 ASSERT_LE(aligned_size, request_size);
234
235 if (aligned_size != request_size) {
236 size_t suffix_size = request_size - aligned_size;
237 OS::Free(aligned_base + aligned_size, suffix_size);
238 request_size -= suffix_size;
239 }
240
241 ASSERT(aligned_size == request_size);
242
243 address_ = static_cast<void*>(aligned_base);
244 size_ = aligned_size;
245}
246
247
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000248VirtualMemory::~VirtualMemory() {
249 if (IsReserved()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000250 bool result = ReleaseRegion(address(), size());
251 ASSERT(result);
252 USE(result);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000253 }
254}
255
256
257bool VirtualMemory::IsReserved() {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000258 return address_ != NULL;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000259}
260
261
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000262void VirtualMemory::Reset() {
263 address_ = NULL;
264 size_ = 0;
265}
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000266
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000267
268bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
269 return CommitRegion(address, size, is_executable);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000270}
271
272
273bool VirtualMemory::Uncommit(void* address, size_t size) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000274 return UncommitRegion(address, size);
275}
276
277
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000278bool VirtualMemory::Guard(void* address) {
279 OS::Guard(address, OS::CommitPageSize());
280 return true;
281}
282
283
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000284void* VirtualMemory::ReserveRegion(size_t size) {
285 void* result = mmap(OS::GetRandomMmapAddr(),
286 size,
287 PROT_NONE,
288 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
289 kMmapFd,
290 kMmapFdOffset);
291
292 if (result == MAP_FAILED) return NULL;
293
294 return result;
295}
296
297
298bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
299 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
300 if (MAP_FAILED == mmap(base,
301 size,
302 prot,
303 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
304 kMmapFd,
305 kMmapFdOffset)) {
306 return false;
307 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000308 return true;
309}
310
311
312bool VirtualMemory::UncommitRegion(void* base, size_t size) {
313 return mmap(base,
314 size,
315 PROT_NONE,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000316 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000317 kMmapFd,
318 kMmapFdOffset) != MAP_FAILED;
319}
320
321
322bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
323 return munmap(base, size) == 0;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000324}
325
326
danno@chromium.org72204d52012-10-31 10:02:10 +0000327bool VirtualMemory::HasLazyCommits() {
328 // TODO(alph): implement for the platform.
329 return false;
330}
331
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000332} } // namespace v8::internal