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 OpenBSD and NetBSD goes here. For the
29 // POSIX-compatible parts, the implementation is in platform-posix.cc.
32 #include <semaphore.h>
35 #include <sys/resource.h>
36 #include <sys/syscall.h>
37 #include <sys/types.h>
40 #include <sys/types.h> // mmap & munmap
41 #include <sys/mman.h> // mmap & munmap
42 #include <sys/stat.h> // open
43 #include <fcntl.h> // open
44 #include <unistd.h> // sysconf
45 #include <strings.h> // index
54 #include "v8threads.h"
55 #include "vm-state-inl.h"
62 const char* OS::LocalTimezone(double time) {
63 if (std::isnan(time)) return "";
64 time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
65 struct tm* t = localtime(&tv);
66 if (NULL == t) return "";
71 double OS::LocalTimeOffset() {
72 time_t tv = time(NULL);
73 struct tm* t = localtime(&tv);
74 // tm_gmtoff includes any daylight savings offset, so subtract it.
75 return static_cast<double>(t->tm_gmtoff * msPerSecond -
76 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
80 void* OS::Allocate(const size_t requested,
83 const size_t msize = RoundUp(requested, AllocateAlignment());
84 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
85 void* addr = OS::GetRandomMmapAddr();
86 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
87 if (mbase == MAP_FAILED) {
88 LOG(i::Isolate::Current(),
89 StringEvent("OS::Allocate", "mmap failed"));
97 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
99 PosixMemoryMappedFile(FILE* file, void* memory, int size)
100 : file_(file), memory_(memory), size_(size) { }
101 virtual ~PosixMemoryMappedFile();
102 virtual void* memory() { return memory_; }
103 virtual int size() { return size_; }
111 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
112 FILE* file = fopen(name, "r+");
113 if (file == NULL) return NULL;
115 fseek(file, 0, SEEK_END);
116 int size = ftell(file);
119 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
120 return new PosixMemoryMappedFile(file, memory, size);
124 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
126 FILE* file = fopen(name, "w+");
127 if (file == NULL) return NULL;
128 int result = fwrite(initial, size, 1, file);
134 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
135 return new PosixMemoryMappedFile(file, memory, size);
139 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
140 if (memory_) OS::Free(memory_, size_);
145 void OS::LogSharedLibraryAddresses(Isolate* isolate) {
146 // This function assumes that the layout of the file is as follows:
147 // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
148 // If we encounter an unexpected situation we abort scanning further entries.
149 FILE* fp = fopen("/proc/self/maps", "r");
150 if (fp == NULL) return;
152 // Allocate enough room to be able to store a full file name.
153 const int kLibNameLen = FILENAME_MAX + 1;
154 char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
156 // This loop will terminate once the scanning hits an EOF.
158 uintptr_t start, end;
159 char attr_r, attr_w, attr_x, attr_p;
160 // Parse the addresses and permission bits at the beginning of the line.
161 if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
162 if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
165 if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
166 // Found a read-only executable entry. Skip characters until we reach
167 // the beginning of the filename or the end of the line.
170 } while ((c != EOF) && (c != '\n') && (c != '/'));
171 if (c == EOF) break; // EOF: Was unexpected, just exit.
173 // Process the filename if found.
175 ungetc(c, fp); // Push the '/' back into the stream to be read below.
177 // Read to the end of the line. Exit if the read fails.
178 if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
180 // Drop the newline character read by fgets. We do not need to check
181 // for a zero-length string because we know that we at least read the
183 lib_name[strlen(lib_name) - 1] = '\0';
185 // No library name found, just record the raw address range.
186 snprintf(lib_name, kLibNameLen,
187 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
189 LOG(isolate, SharedLibraryEvent(lib_name, start, end));
191 // Entry not describing executable data. Skip to end of line to set up
192 // reading the next entry.
195 } while ((c != EOF) && (c != '\n'));
204 void OS::SignalCodeMovingGC() {
205 // Support for ll_prof.py.
207 // The Linux profiler built into the kernel logs all mmap's with
208 // PROT_EXEC so that analysis tools can properly attribute ticks. We
209 // do a mmap with a name known by ll_prof.py and immediately munmap
210 // it. This injects a GC marker into the stream of events generated
211 // by the kernel and allows us to synchronize V8 code log and the
213 int size = sysconf(_SC_PAGESIZE);
214 FILE* f = fopen(FLAG_gc_fake_mmap, "w+");
216 OS::PrintError("Failed to open %s\n", FLAG_gc_fake_mmap);
219 void* addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE,
221 ASSERT(addr != MAP_FAILED);
222 OS::Free(addr, size);
228 // Constants used for mmap.
229 static const int kMmapFd = -1;
230 static const int kMmapFdOffset = 0;
233 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
236 VirtualMemory::VirtualMemory(size_t size)
237 : address_(ReserveRegion(size)), size_(size) { }
240 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
241 : address_(NULL), size_(0) {
242 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
243 size_t request_size = RoundUp(size + alignment,
244 static_cast<intptr_t>(OS::AllocateAlignment()));
245 void* reservation = mmap(OS::GetRandomMmapAddr(),
248 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
251 if (reservation == MAP_FAILED) return;
253 Address base = static_cast<Address>(reservation);
254 Address aligned_base = RoundUp(base, alignment);
255 ASSERT_LE(base, aligned_base);
257 // Unmap extra memory reserved before and after the desired block.
258 if (aligned_base != base) {
259 size_t prefix_size = static_cast<size_t>(aligned_base - base);
260 OS::Free(base, prefix_size);
261 request_size -= prefix_size;
264 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
265 ASSERT_LE(aligned_size, request_size);
267 if (aligned_size != request_size) {
268 size_t suffix_size = request_size - aligned_size;
269 OS::Free(aligned_base + aligned_size, suffix_size);
270 request_size -= suffix_size;
273 ASSERT(aligned_size == request_size);
275 address_ = static_cast<void*>(aligned_base);
276 size_ = aligned_size;
280 VirtualMemory::~VirtualMemory() {
282 bool result = ReleaseRegion(address(), size());
289 bool VirtualMemory::IsReserved() {
290 return address_ != NULL;
294 void VirtualMemory::Reset() {
300 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
301 return CommitRegion(address, size, is_executable);
305 bool VirtualMemory::Uncommit(void* address, size_t size) {
306 return UncommitRegion(address, size);
310 bool VirtualMemory::Guard(void* address) {
311 OS::Guard(address, OS::CommitPageSize());
316 void* VirtualMemory::ReserveRegion(size_t size) {
317 void* result = mmap(OS::GetRandomMmapAddr(),
320 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
324 if (result == MAP_FAILED) return NULL;
330 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
331 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
332 if (MAP_FAILED == mmap(base,
335 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
344 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
348 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
350 kMmapFdOffset) != MAP_FAILED;
354 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
355 return munmap(base, size) == 0;
359 bool VirtualMemory::HasLazyCommits() {
360 // TODO(alph): implement for the platform.
364 } } // namespace v8::internal