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>
15 #include <sys/types.h>
18 #include <fcntl.h> // open
20 #include <strings.h> // index
21 #include <sys/mman.h> // mmap & munmap
22 #include <sys/stat.h> // open
23 #include <unistd.h> // sysconf
29 #include "src/base/macros.h"
30 #include "src/base/platform/platform.h"
37 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
38 if (std::isnan(time)) return "";
39 time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
40 struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn)
41 if (NULL == t) return "";
46 double OS::LocalTimeOffset(TimezoneCache* cache) {
47 time_t tv = time(NULL);
48 struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn)
49 // tm_gmtoff includes any daylight savings offset, so subtract it.
50 return static_cast<double>(t->tm_gmtoff * msPerSecond -
51 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
55 void* OS::Allocate(const size_t requested,
58 const size_t msize = RoundUp(requested, AllocateAlignment());
59 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
60 void* addr = OS::GetRandomMmapAddr();
61 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
62 if (mbase == MAP_FAILED) return NULL;
68 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
69 std::vector<SharedLibraryAddress> result;
70 // This function assumes that the layout of the file is as follows:
71 // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
72 // If we encounter an unexpected situation we abort scanning further entries.
73 FILE* fp = fopen("/proc/self/maps", "r");
74 if (fp == NULL) return result;
76 // Allocate enough room to be able to store a full file name.
77 const int kLibNameLen = FILENAME_MAX + 1;
78 char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
80 // This loop will terminate once the scanning hits an EOF.
83 char attr_r, attr_w, attr_x, attr_p;
84 // Parse the addresses and permission bits at the beginning of the line.
85 if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
86 if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
89 if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
90 // Found a read-only executable entry. Skip characters until we reach
91 // the beginning of the filename or the end of the line.
94 } while ((c != EOF) && (c != '\n') && (c != '/'));
95 if (c == EOF) break; // EOF: Was unexpected, just exit.
97 // Process the filename if found.
99 ungetc(c, fp); // Push the '/' back into the stream to be read below.
101 // Read to the end of the line. Exit if the read fails.
102 if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
104 // Drop the newline character read by fgets. We do not need to check
105 // for a zero-length string because we know that we at least read the
107 lib_name[strlen(lib_name) - 1] = '\0';
109 // No library name found, just record the raw address range.
110 snprintf(lib_name, kLibNameLen,
111 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
113 result.push_back(SharedLibraryAddress(lib_name, start, end));
115 // Entry not describing executable data. Skip to end of line to set up
116 // reading the next entry.
119 } while ((c != EOF) && (c != '\n'));
129 void OS::SignalCodeMovingGC() {
130 // Support for ll_prof.py.
132 // The Linux profiler built into the kernel logs all mmap's with
133 // PROT_EXEC so that analysis tools can properly attribute ticks. We
134 // do a mmap with a name known by ll_prof.py and immediately munmap
135 // it. This injects a GC marker into the stream of events generated
136 // by the kernel and allows us to synchronize V8 code log and the
138 int size = sysconf(_SC_PAGESIZE);
139 FILE* f = fopen(OS::GetGCFakeMMapFile(), "w+");
141 OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile());
144 void* addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE,
146 DCHECK(addr != MAP_FAILED);
147 OS::Free(addr, size);
153 // Constants used for mmap.
154 static const int kMmapFd = -1;
155 static const int kMmapFdOffset = 0;
158 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
161 VirtualMemory::VirtualMemory(size_t size)
162 : address_(ReserveRegion(size)), size_(size) { }
165 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
166 : address_(NULL), size_(0) {
167 DCHECK((alignment % OS::AllocateAlignment()) == 0);
168 size_t request_size = RoundUp(size + alignment,
169 static_cast<intptr_t>(OS::AllocateAlignment()));
170 void* reservation = mmap(OS::GetRandomMmapAddr(),
173 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
176 if (reservation == MAP_FAILED) return;
178 uint8_t* base = static_cast<uint8_t*>(reservation);
179 uint8_t* aligned_base = RoundUp(base, alignment);
180 DCHECK_LE(base, aligned_base);
182 // Unmap extra memory reserved before and after the desired block.
183 if (aligned_base != base) {
184 size_t prefix_size = static_cast<size_t>(aligned_base - base);
185 OS::Free(base, prefix_size);
186 request_size -= prefix_size;
189 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
190 DCHECK_LE(aligned_size, request_size);
192 if (aligned_size != request_size) {
193 size_t suffix_size = request_size - aligned_size;
194 OS::Free(aligned_base + aligned_size, suffix_size);
195 request_size -= suffix_size;
198 DCHECK(aligned_size == request_size);
200 address_ = static_cast<void*>(aligned_base);
201 size_ = aligned_size;
205 VirtualMemory::~VirtualMemory() {
207 bool result = ReleaseRegion(address(), size());
214 bool VirtualMemory::IsReserved() {
215 return address_ != NULL;
219 void VirtualMemory::Reset() {
225 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
226 return CommitRegion(address, size, is_executable);
230 bool VirtualMemory::Uncommit(void* address, size_t size) {
231 return UncommitRegion(address, size);
235 bool VirtualMemory::Guard(void* address) {
236 OS::Guard(address, OS::CommitPageSize());
241 void* VirtualMemory::ReserveRegion(size_t size) {
242 void* result = mmap(OS::GetRandomMmapAddr(),
245 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
249 if (result == MAP_FAILED) return NULL;
255 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
256 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
257 if (MAP_FAILED == mmap(base,
260 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
269 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
273 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
275 kMmapFdOffset) != MAP_FAILED;
279 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
280 return munmap(base, size) == 0;
284 bool VirtualMemory::HasLazyCommits() {
285 // TODO(alph): implement for the platform.
289 } } // namespace v8::base