Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | |
| 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)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 40 | struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 41 | if (NULL == t) return ""; |
| 42 | return t->tm_zone; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | double OS::LocalTimeOffset(TimezoneCache* cache) { |
| 47 | time_t tv = time(NULL); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 48 | struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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)); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | void* OS::Allocate(const size_t requested, |
| 56 | size_t* allocated, |
| 57 | bool is_executable) { |
| 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; |
| 63 | *allocated = msize; |
| 64 | return mbase; |
| 65 | } |
| 66 | |
| 67 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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; |
| 75 | |
| 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)); |
| 79 | |
| 80 | // This loop will terminate once the scanning hits an EOF. |
| 81 | while (true) { |
| 82 | uintptr_t start, end; |
| 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; |
| 87 | |
| 88 | int c; |
| 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. |
| 92 | do { |
| 93 | c = getc(fp); |
| 94 | } while ((c != EOF) && (c != '\n') && (c != '/')); |
| 95 | if (c == EOF) break; // EOF: Was unexpected, just exit. |
| 96 | |
| 97 | // Process the filename if found. |
| 98 | if (c == '/') { |
| 99 | ungetc(c, fp); // Push the '/' back into the stream to be read below. |
| 100 | |
| 101 | // Read to the end of the line. Exit if the read fails. |
| 102 | if (fgets(lib_name, kLibNameLen, fp) == NULL) break; |
| 103 | |
| 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 |
| 106 | // '/' character. |
| 107 | lib_name[strlen(lib_name) - 1] = '\0'; |
| 108 | } else { |
| 109 | // No library name found, just record the raw address range. |
| 110 | snprintf(lib_name, kLibNameLen, |
| 111 | "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end); |
| 112 | } |
| 113 | result.push_back(SharedLibraryAddress(lib_name, start, end)); |
| 114 | } else { |
| 115 | // Entry not describing executable data. Skip to end of line to set up |
| 116 | // reading the next entry. |
| 117 | do { |
| 118 | c = getc(fp); |
| 119 | } while ((c != EOF) && (c != '\n')); |
| 120 | if (c == EOF) break; |
| 121 | } |
| 122 | } |
| 123 | free(lib_name); |
| 124 | fclose(fp); |
| 125 | return result; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void OS::SignalCodeMovingGC() { |
| 130 | // Support for ll_prof.py. |
| 131 | // |
| 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 |
| 137 | // kernel log. |
| 138 | int size = sysconf(_SC_PAGESIZE); |
| 139 | FILE* f = fopen(OS::GetGCFakeMMapFile(), "w+"); |
| 140 | if (f == NULL) { |
| 141 | OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile()); |
| 142 | OS::Abort(); |
| 143 | } |
| 144 | void* addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE, |
| 145 | fileno(f), 0); |
| 146 | DCHECK(addr != MAP_FAILED); |
| 147 | OS::Free(addr, size); |
| 148 | fclose(f); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | |
| 153 | // Constants used for mmap. |
| 154 | static const int kMmapFd = -1; |
| 155 | static const int kMmapFdOffset = 0; |
| 156 | |
| 157 | |
| 158 | VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } |
| 159 | |
| 160 | |
| 161 | VirtualMemory::VirtualMemory(size_t size) |
| 162 | : address_(ReserveRegion(size)), size_(size) { } |
| 163 | |
| 164 | |
| 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(), |
| 171 | request_size, |
| 172 | PROT_NONE, |
| 173 | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
| 174 | kMmapFd, |
| 175 | kMmapFdOffset); |
| 176 | if (reservation == MAP_FAILED) return; |
| 177 | |
| 178 | uint8_t* base = static_cast<uint8_t*>(reservation); |
| 179 | uint8_t* aligned_base = RoundUp(base, alignment); |
| 180 | DCHECK_LE(base, aligned_base); |
| 181 | |
| 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; |
| 187 | } |
| 188 | |
| 189 | size_t aligned_size = RoundUp(size, OS::AllocateAlignment()); |
| 190 | DCHECK_LE(aligned_size, request_size); |
| 191 | |
| 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; |
| 196 | } |
| 197 | |
| 198 | DCHECK(aligned_size == request_size); |
| 199 | |
| 200 | address_ = static_cast<void*>(aligned_base); |
| 201 | size_ = aligned_size; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | VirtualMemory::~VirtualMemory() { |
| 206 | if (IsReserved()) { |
| 207 | bool result = ReleaseRegion(address(), size()); |
| 208 | DCHECK(result); |
| 209 | USE(result); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | |
| 214 | bool VirtualMemory::IsReserved() { |
| 215 | return address_ != NULL; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | void VirtualMemory::Reset() { |
| 220 | address_ = NULL; |
| 221 | size_ = 0; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
| 226 | return CommitRegion(address, size, is_executable); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | bool VirtualMemory::Uncommit(void* address, size_t size) { |
| 231 | return UncommitRegion(address, size); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | bool VirtualMemory::Guard(void* address) { |
| 236 | OS::Guard(address, OS::CommitPageSize()); |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | void* VirtualMemory::ReserveRegion(size_t size) { |
| 242 | void* result = mmap(OS::GetRandomMmapAddr(), |
| 243 | size, |
| 244 | PROT_NONE, |
| 245 | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
| 246 | kMmapFd, |
| 247 | kMmapFdOffset); |
| 248 | |
| 249 | if (result == MAP_FAILED) return NULL; |
| 250 | |
| 251 | return result; |
| 252 | } |
| 253 | |
| 254 | |
| 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, |
| 258 | size, |
| 259 | prot, |
| 260 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
| 261 | kMmapFd, |
| 262 | kMmapFdOffset)) { |
| 263 | return false; |
| 264 | } |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | |
| 269 | bool VirtualMemory::UncommitRegion(void* base, size_t size) { |
| 270 | return mmap(base, |
| 271 | size, |
| 272 | PROT_NONE, |
| 273 | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED, |
| 274 | kMmapFd, |
| 275 | kMmapFdOffset) != MAP_FAILED; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | bool VirtualMemory::ReleaseRegion(void* base, size_t size) { |
| 280 | return munmap(base, size) == 0; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | bool VirtualMemory::HasLazyCommits() { |
| 285 | // TODO(alph): implement for the platform. |
| 286 | return false; |
| 287 | } |
| 288 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 289 | } // namespace base |
| 290 | } // namespace v8 |