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.
5 // Platform-specific code for OpenBSD and NetBSD goes here. For the
6 // POSIX-compatible parts, the implementation is in platform-posix.cc.
12 #include <sys/resource.h>
13 #include <sys/syscall.h>
14 #include <sys/types.h>
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
31 #include "v8threads.h"
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 "";
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));
56 void* OS::Allocate(const size_t requested,
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"));
73 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
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_; }
87 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
88 FILE* file = fopen(name, "r+");
89 if (file == NULL) return NULL;
91 fseek(file, 0, SEEK_END);
92 int size = ftell(file);
95 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
96 return new PosixMemoryMappedFile(file, memory, size);
100 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
102 FILE* file = fopen(name, "w+");
103 if (file == NULL) return NULL;
104 int result = fwrite(initial, size, 1, file);
110 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
111 return new PosixMemoryMappedFile(file, memory, size);
115 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
116 if (memory_) OS::Free(memory_, size_);
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;
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));
132 // This loop will terminate once the scanning hits an EOF.
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;
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.
146 } while ((c != EOF) && (c != '\n') && (c != '/'));
147 if (c == EOF) break; // EOF: Was unexpected, just exit.
149 // Process the filename if found.
151 ungetc(c, fp); // Push the '/' back into the stream to be read below.
153 // Read to the end of the line. Exit if the read fails.
154 if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
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
159 lib_name[strlen(lib_name) - 1] = '\0';
161 // No library name found, just record the raw address range.
162 snprintf(lib_name, kLibNameLen,
163 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
165 LOG(isolate, SharedLibraryEvent(lib_name, start, end));
167 // Entry not describing executable data. Skip to end of line to set up
168 // reading the next entry.
171 } while ((c != EOF) && (c != '\n'));
180 void OS::SignalCodeMovingGC() {
181 // Support for ll_prof.py.
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
189 int size = sysconf(_SC_PAGESIZE);
190 FILE* f = fopen(FLAG_gc_fake_mmap, "w+");
192 OS::PrintError("Failed to open %s\n", FLAG_gc_fake_mmap);
195 void* addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE,
197 ASSERT(addr != MAP_FAILED);
198 OS::Free(addr, size);
204 // Constants used for mmap.
205 static const int kMmapFd = -1;
206 static const int kMmapFdOffset = 0;
209 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
212 VirtualMemory::VirtualMemory(size_t size)
213 : address_(ReserveRegion(size)), size_(size) { }
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(),
224 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
227 if (reservation == MAP_FAILED) return;
229 Address base = static_cast<Address>(reservation);
230 Address aligned_base = RoundUp(base, alignment);
231 ASSERT_LE(base, aligned_base);
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;
240 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
241 ASSERT_LE(aligned_size, request_size);
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;
249 ASSERT(aligned_size == request_size);
251 address_ = static_cast<void*>(aligned_base);
252 size_ = aligned_size;
256 VirtualMemory::~VirtualMemory() {
258 bool result = ReleaseRegion(address(), size());
265 bool VirtualMemory::IsReserved() {
266 return address_ != NULL;
270 void VirtualMemory::Reset() {
276 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
277 return CommitRegion(address, size, is_executable);
281 bool VirtualMemory::Uncommit(void* address, size_t size) {
282 return UncommitRegion(address, size);
286 bool VirtualMemory::Guard(void* address) {
287 OS::Guard(address, OS::CommitPageSize());
292 void* VirtualMemory::ReserveRegion(size_t size) {
293 void* result = mmap(OS::GetRandomMmapAddr(),
296 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
300 if (result == MAP_FAILED) return NULL;
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,
311 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
320 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
324 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
326 kMmapFdOffset) != MAP_FAILED;
330 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
331 return munmap(base, size) == 0;
335 bool VirtualMemory::HasLazyCommits() {
336 // TODO(alph): implement for the platform.
340 } } // namespace v8::internal