1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
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.
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.
28 // Platform-specific code for FreeBSD goes here. For the POSIX-compatible
29 // parts, the implementation is in platform-posix.cc.
32 #include <semaphore.h>
35 #include <sys/resource.h>
36 #include <sys/types.h>
37 #include <sys/ucontext.h>
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
45 // If you don't have execinfo.h then you need devel/libexecinfo from ports.
46 #include <strings.h> // index
54 #include "v8threads.h"
57 #include "vm-state-inl.h"
64 const char* OS::LocalTimezone(double time) {
65 if (std::isnan(time)) return "";
66 time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
67 struct tm* t = localtime(&tv);
68 if (NULL == t) return "";
73 double 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));
82 void* OS::Allocate(const size_t requested,
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);
89 if (mbase == MAP_FAILED) {
90 LOG(Isolate::Current(), StringEvent("OS::Allocate", "mmap failed"));
98 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
100 PosixMemoryMappedFile(FILE* file, void* memory, int size)
101 : file_(file), memory_(memory), size_(size) { }
102 virtual ~PosixMemoryMappedFile();
103 virtual void* memory() { return memory_; }
104 virtual int size() { return size_; }
112 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
113 FILE* file = fopen(name, "r+");
114 if (file == NULL) return NULL;
116 fseek(file, 0, SEEK_END);
117 int size = ftell(file);
120 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
121 return new PosixMemoryMappedFile(file, memory, size);
125 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
127 FILE* file = fopen(name, "w+");
128 if (file == NULL) return NULL;
129 int result = fwrite(initial, size, 1, file);
135 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
136 return new PosixMemoryMappedFile(file, memory, size);
140 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
141 if (memory_) munmap(memory_, size_);
146 static unsigned StringToLong(char* buffer) {
147 return static_cast<unsigned>(strtol(buffer, NULL, 16)); // NOLINT
151 void OS::LogSharedLibraryAddresses(Isolate* isolate) {
152 static const int MAP_LENGTH = 1024;
153 int fd = open("/proc/self/maps", O_RDONLY);
156 char addr_buffer[11];
157 addr_buffer[0] = '0';
158 addr_buffer[1] = 'x';
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];
173 if (bytes_read >= MAP_LENGTH - 1)
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;
185 LOG(isolate, SharedLibraryEvent(start_of_path, start, end));
191 void OS::SignalCodeMovingGC() {
196 // Constants used for mmap.
197 static const int kMmapFd = -1;
198 static const int kMmapFdOffset = 0;
201 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
204 VirtualMemory::VirtualMemory(size_t size)
205 : address_(ReserveRegion(size)), size_(size) { }
208 VirtualMemory::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(),
216 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
219 if (reservation == MAP_FAILED) return;
221 Address base = static_cast<Address>(reservation);
222 Address aligned_base = RoundUp(base, alignment);
223 ASSERT_LE(base, aligned_base);
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;
232 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
233 ASSERT_LE(aligned_size, request_size);
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;
241 ASSERT(aligned_size == request_size);
243 address_ = static_cast<void*>(aligned_base);
244 size_ = aligned_size;
248 VirtualMemory::~VirtualMemory() {
250 bool result = ReleaseRegion(address(), size());
257 bool VirtualMemory::IsReserved() {
258 return address_ != NULL;
262 void VirtualMemory::Reset() {
268 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
269 return CommitRegion(address, size, is_executable);
273 bool VirtualMemory::Uncommit(void* address, size_t size) {
274 return UncommitRegion(address, size);
278 bool VirtualMemory::Guard(void* address) {
279 OS::Guard(address, OS::CommitPageSize());
284 void* VirtualMemory::ReserveRegion(size_t size) {
285 void* result = mmap(OS::GetRandomMmapAddr(),
288 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
292 if (result == MAP_FAILED) return NULL;
298 bool 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,
303 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
312 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
316 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
318 kMmapFdOffset) != MAP_FAILED;
322 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
323 return munmap(base, size) == 0;
327 bool VirtualMemory::HasLazyCommits() {
328 // TODO(alph): implement for the platform.
332 } } // namespace v8::internal