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.
4
5 // Platform-specific code for OpenBSD and NetBSD goes here. For the
6 // POSIX-compatible parts, the implementation is in platform-posix.cc.
7
8 #include <pthread.h>
9 #include <semaphore.h>
10 #include <signal.h>
11 #include <stdlib.h>
12 #include <sys/resource.h>
13 #include <sys/syscall.h>
14 #include <sys/time.h>
15 #include <sys/types.h>
16
17 #include <errno.h>
18 #include <fcntl.h> // open
19 #include <stdarg.h>
20 #include <strings.h> // index
21 #include <sys/mman.h> // mmap & munmap
22 #include <sys/stat.h> // open
23 #include <unistd.h> // sysconf
24
25 #include <cmath>
26
27 #undef MAP_TYPE
28
29 #include "src/base/macros.h"
30 #include "src/base/platform/platform.h"
31
32
33 namespace v8 {
34 namespace base {
35
36
LocalTimezone(double time,TimezoneCache * cache)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 tm;
41 struct tm* t = localtime_r(&tv, &tm);
42 if (NULL == t) return "";
43 return t->tm_zone;
44 }
45
46
LocalTimeOffset(TimezoneCache * cache)47 double OS::LocalTimeOffset(TimezoneCache* cache) {
48 time_t tv = time(NULL);
49 struct tm tm;
50 struct tm* t = localtime_r(&tv, &tm);
51 // tm_gmtoff includes any daylight savings offset, so subtract it.
52 return static_cast<double>(t->tm_gmtoff * msPerSecond -
53 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
54 }
55
56
Allocate(const size_t requested,size_t * allocated,bool is_executable)57 void* OS::Allocate(const size_t requested,
58 size_t* allocated,
59 bool is_executable) {
60 const size_t msize = RoundUp(requested, AllocateAlignment());
61 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
62 void* addr = OS::GetRandomMmapAddr();
63 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
64 if (mbase == MAP_FAILED) return NULL;
65 *allocated = msize;
66 return mbase;
67 }
68
69
GetSharedLibraryAddresses()70 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
71 std::vector<SharedLibraryAddress> result;
72 // This function assumes that the layout of the file is as follows:
73 // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
74 // If we encounter an unexpected situation we abort scanning further entries.
75 FILE* fp = fopen("/proc/self/maps", "r");
76 if (fp == NULL) return result;
77
78 // Allocate enough room to be able to store a full file name.
79 const int kLibNameLen = FILENAME_MAX + 1;
80 char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
81
82 // This loop will terminate once the scanning hits an EOF.
83 while (true) {
84 uintptr_t start, end;
85 char attr_r, attr_w, attr_x, attr_p;
86 // Parse the addresses and permission bits at the beginning of the line.
87 if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
88 if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
89
90 int c;
91 if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
92 // Found a read-only executable entry. Skip characters until we reach
93 // the beginning of the filename or the end of the line.
94 do {
95 c = getc(fp);
96 } while ((c != EOF) && (c != '\n') && (c != '/'));
97 if (c == EOF) break; // EOF: Was unexpected, just exit.
98
99 // Process the filename if found.
100 if (c == '/') {
101 ungetc(c, fp); // Push the '/' back into the stream to be read below.
102
103 // Read to the end of the line. Exit if the read fails.
104 if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
105
106 // Drop the newline character read by fgets. We do not need to check
107 // for a zero-length string because we know that we at least read the
108 // '/' character.
109 lib_name[strlen(lib_name) - 1] = '\0';
110 } else {
111 // No library name found, just record the raw address range.
112 snprintf(lib_name, kLibNameLen,
113 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
114 }
115 result.push_back(SharedLibraryAddress(lib_name, start, end));
116 } else {
117 // Entry not describing executable data. Skip to end of line to set up
118 // reading the next entry.
119 do {
120 c = getc(fp);
121 } while ((c != EOF) && (c != '\n'));
122 if (c == EOF) break;
123 }
124 }
125 free(lib_name);
126 fclose(fp);
127 return result;
128 }
129
130
SignalCodeMovingGC()131 void OS::SignalCodeMovingGC() {
132 // Support for ll_prof.py.
133 //
134 // The Linux profiler built into the kernel logs all mmap's with
135 // PROT_EXEC so that analysis tools can properly attribute ticks. We
136 // do a mmap with a name known by ll_prof.py and immediately munmap
137 // it. This injects a GC marker into the stream of events generated
138 // by the kernel and allows us to synchronize V8 code log and the
139 // kernel log.
140 int size = sysconf(_SC_PAGESIZE);
141 FILE* f = fopen(OS::GetGCFakeMMapFile(), "w+");
142 if (f == NULL) {
143 OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile());
144 OS::Abort();
145 }
146 void* addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE,
147 fileno(f), 0);
148 DCHECK(addr != MAP_FAILED);
149 OS::Free(addr, size);
150 fclose(f);
151 }
152
153
154
155 // Constants used for mmap.
156 static const int kMmapFd = -1;
157 static const int kMmapFdOffset = 0;
158
159
VirtualMemory()160 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
161
162
VirtualMemory(size_t size)163 VirtualMemory::VirtualMemory(size_t size)
164 : address_(ReserveRegion(size)), size_(size) { }
165
166
VirtualMemory(size_t size,size_t alignment)167 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
168 : address_(NULL), size_(0) {
169 DCHECK((alignment % OS::AllocateAlignment()) == 0);
170 size_t request_size = RoundUp(size + alignment,
171 static_cast<intptr_t>(OS::AllocateAlignment()));
172 void* reservation = mmap(OS::GetRandomMmapAddr(),
173 request_size,
174 PROT_NONE,
175 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
176 kMmapFd,
177 kMmapFdOffset);
178 if (reservation == MAP_FAILED) return;
179
180 uint8_t* base = static_cast<uint8_t*>(reservation);
181 uint8_t* aligned_base = RoundUp(base, alignment);
182 DCHECK_LE(base, aligned_base);
183
184 // Unmap extra memory reserved before and after the desired block.
185 if (aligned_base != base) {
186 size_t prefix_size = static_cast<size_t>(aligned_base - base);
187 OS::Free(base, prefix_size);
188 request_size -= prefix_size;
189 }
190
191 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
192 DCHECK_LE(aligned_size, request_size);
193
194 if (aligned_size != request_size) {
195 size_t suffix_size = request_size - aligned_size;
196 OS::Free(aligned_base + aligned_size, suffix_size);
197 request_size -= suffix_size;
198 }
199
200 DCHECK(aligned_size == request_size);
201
202 address_ = static_cast<void*>(aligned_base);
203 size_ = aligned_size;
204 }
205
206
~VirtualMemory()207 VirtualMemory::~VirtualMemory() {
208 if (IsReserved()) {
209 bool result = ReleaseRegion(address(), size());
210 DCHECK(result);
211 USE(result);
212 }
213 }
214
215
IsReserved()216 bool VirtualMemory::IsReserved() {
217 return address_ != NULL;
218 }
219
220
Reset()221 void VirtualMemory::Reset() {
222 address_ = NULL;
223 size_ = 0;
224 }
225
226
Commit(void * address,size_t size,bool is_executable)227 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
228 return CommitRegion(address, size, is_executable);
229 }
230
231
Uncommit(void * address,size_t size)232 bool VirtualMemory::Uncommit(void* address, size_t size) {
233 return UncommitRegion(address, size);
234 }
235
236
Guard(void * address)237 bool VirtualMemory::Guard(void* address) {
238 OS::Guard(address, OS::CommitPageSize());
239 return true;
240 }
241
242
ReserveRegion(size_t size)243 void* VirtualMemory::ReserveRegion(size_t size) {
244 void* result = mmap(OS::GetRandomMmapAddr(),
245 size,
246 PROT_NONE,
247 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
248 kMmapFd,
249 kMmapFdOffset);
250
251 if (result == MAP_FAILED) return NULL;
252
253 return result;
254 }
255
256
CommitRegion(void * base,size_t size,bool is_executable)257 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
258 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
259 if (MAP_FAILED == mmap(base,
260 size,
261 prot,
262 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
263 kMmapFd,
264 kMmapFdOffset)) {
265 return false;
266 }
267 return true;
268 }
269
270
UncommitRegion(void * base,size_t size)271 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
272 return mmap(base,
273 size,
274 PROT_NONE,
275 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
276 kMmapFd,
277 kMmapFdOffset) != MAP_FAILED;
278 }
279
ReleasePartialRegion(void * base,size_t size,void * free_start,size_t free_size)280 bool VirtualMemory::ReleasePartialRegion(void* base, size_t size,
281 void* free_start, size_t free_size) {
282 return munmap(free_start, free_size) == 0;
283 }
284
ReleaseRegion(void * base,size_t size)285 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
286 return munmap(base, size) == 0;
287 }
288
289
HasLazyCommits()290 bool VirtualMemory::HasLazyCommits() {
291 // TODO(alph): implement for the platform.
292 return false;
293 }
294
295 } // namespace base
296 } // namespace v8
297