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 <sys/types.h> // mmap & munmap
24 #include <unistd.h> // sysconf
30 #include "src/base/macros.h"
31 #include "src/base/platform/platform.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) return NULL;
69 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
71 PosixMemoryMappedFile(FILE* file, void* memory, int size)
72 : file_(file), memory_(memory), size_(size) { }
73 virtual ~PosixMemoryMappedFile();
74 virtual void* memory() { return memory_; }
75 virtual int size() { return size_; }
83 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
84 FILE* file = fopen(name, "r+");
85 if (file == NULL) return NULL;
87 fseek(file, 0, SEEK_END);
88 int size = ftell(file);
91 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
92 return new PosixMemoryMappedFile(file, memory, size);
96 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
98 FILE* file = fopen(name, "w+");
99 if (file == NULL) return NULL;
100 int result = fwrite(initial, size, 1, file);
106 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
107 return new PosixMemoryMappedFile(file, memory, size);
111 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
112 if (memory_) OS::Free(memory_, size_);
117 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
118 std::vector<SharedLibraryAddress> result;
119 // This function assumes that the layout of the file is as follows:
120 // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
121 // If we encounter an unexpected situation we abort scanning further entries.
122 FILE* fp = fopen("/proc/self/maps", "r");
123 if (fp == NULL) return result;
125 // Allocate enough room to be able to store a full file name.
126 const int kLibNameLen = FILENAME_MAX + 1;
127 char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
129 // This loop will terminate once the scanning hits an EOF.
131 uintptr_t start, end;
132 char attr_r, attr_w, attr_x, attr_p;
133 // Parse the addresses and permission bits at the beginning of the line.
134 if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
135 if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
138 if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
139 // Found a read-only executable entry. Skip characters until we reach
140 // the beginning of the filename or the end of the line.
143 } while ((c != EOF) && (c != '\n') && (c != '/'));
144 if (c == EOF) break; // EOF: Was unexpected, just exit.
146 // Process the filename if found.
148 ungetc(c, fp); // Push the '/' back into the stream to be read below.
150 // Read to the end of the line. Exit if the read fails.
151 if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
153 // Drop the newline character read by fgets. We do not need to check
154 // for a zero-length string because we know that we at least read the
156 lib_name[strlen(lib_name) - 1] = '\0';
158 // No library name found, just record the raw address range.
159 snprintf(lib_name, kLibNameLen,
160 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
162 result.push_back(SharedLibraryAddress(lib_name, start, end));
164 // Entry not describing executable data. Skip to end of line to set up
165 // reading the next entry.
168 } while ((c != EOF) && (c != '\n'));
178 void OS::SignalCodeMovingGC() {
179 // Support for ll_prof.py.
181 // The Linux profiler built into the kernel logs all mmap's with
182 // PROT_EXEC so that analysis tools can properly attribute ticks. We
183 // do a mmap with a name known by ll_prof.py and immediately munmap
184 // it. This injects a GC marker into the stream of events generated
185 // by the kernel and allows us to synchronize V8 code log and the
187 int size = sysconf(_SC_PAGESIZE);
188 FILE* f = fopen(OS::GetGCFakeMMapFile(), "w+");
190 OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile());
193 void* addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE,
195 DCHECK(addr != MAP_FAILED);
196 OS::Free(addr, size);
202 // Constants used for mmap.
203 static const int kMmapFd = -1;
204 static const int kMmapFdOffset = 0;
207 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
210 VirtualMemory::VirtualMemory(size_t size)
211 : address_(ReserveRegion(size)), size_(size) { }
214 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
215 : address_(NULL), size_(0) {
216 DCHECK((alignment % OS::AllocateAlignment()) == 0);
217 size_t request_size = RoundUp(size + alignment,
218 static_cast<intptr_t>(OS::AllocateAlignment()));
219 void* reservation = mmap(OS::GetRandomMmapAddr(),
222 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
225 if (reservation == MAP_FAILED) return;
227 uint8_t* base = static_cast<uint8_t*>(reservation);
228 uint8_t* aligned_base = RoundUp(base, alignment);
229 DCHECK_LE(base, aligned_base);
231 // Unmap extra memory reserved before and after the desired block.
232 if (aligned_base != base) {
233 size_t prefix_size = static_cast<size_t>(aligned_base - base);
234 OS::Free(base, prefix_size);
235 request_size -= prefix_size;
238 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
239 DCHECK_LE(aligned_size, request_size);
241 if (aligned_size != request_size) {
242 size_t suffix_size = request_size - aligned_size;
243 OS::Free(aligned_base + aligned_size, suffix_size);
244 request_size -= suffix_size;
247 DCHECK(aligned_size == request_size);
249 address_ = static_cast<void*>(aligned_base);
250 size_ = aligned_size;
254 VirtualMemory::~VirtualMemory() {
256 bool result = ReleaseRegion(address(), size());
263 bool VirtualMemory::IsReserved() {
264 return address_ != NULL;
268 void VirtualMemory::Reset() {
274 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
275 return CommitRegion(address, size, is_executable);
279 bool VirtualMemory::Uncommit(void* address, size_t size) {
280 return UncommitRegion(address, size);
284 bool VirtualMemory::Guard(void* address) {
285 OS::Guard(address, OS::CommitPageSize());
290 void* VirtualMemory::ReserveRegion(size_t size) {
291 void* result = mmap(OS::GetRandomMmapAddr(),
294 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
298 if (result == MAP_FAILED) return NULL;
304 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
305 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
306 if (MAP_FAILED == mmap(base,
309 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
318 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
322 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
324 kMmapFdOffset) != MAP_FAILED;
328 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
329 return munmap(base, size) == 0;
333 bool VirtualMemory::HasLazyCommits() {
334 // TODO(alph): implement for the platform.
338 } } // namespace v8::base