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 FreeBSD goes here. For the POSIX-compatible |
| 6 | // 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/time.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <sys/ucontext.h> |
| 16 | |
| 17 | #include <sys/fcntl.h> // open |
| 18 | #include <sys/mman.h> // mmap & munmap |
| 19 | #include <sys/stat.h> // open |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 20 | #include <unistd.h> // getpagesize |
| 21 | // If you don't have execinfo.h then you need devel/libexecinfo from ports. |
| 22 | #include <errno.h> |
| 23 | #include <limits.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <strings.h> // index |
| 26 | |
| 27 | #include <cmath> |
| 28 | |
| 29 | #undef MAP_TYPE |
| 30 | |
| 31 | #include "src/base/macros.h" |
| 32 | #include "src/base/platform/platform.h" |
| 33 | |
| 34 | |
| 35 | namespace v8 { |
| 36 | namespace base { |
| 37 | |
| 38 | |
| 39 | const char* OS::LocalTimezone(double time, TimezoneCache* cache) { |
| 40 | if (std::isnan(time)) return ""; |
| 41 | time_t tv = static_cast<time_t>(std::floor(time/msPerSecond)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 42 | struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 43 | if (NULL == t) return ""; |
| 44 | return t->tm_zone; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | double OS::LocalTimeOffset(TimezoneCache* cache) { |
| 49 | time_t tv = time(NULL); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 50 | struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | |
| 57 | void* OS::Allocate(const size_t requested, |
| 58 | size_t* allocated, |
| 59 | bool executable) { |
| 60 | const size_t msize = RoundUp(requested, getpagesize()); |
| 61 | int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); |
| 62 | void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0); |
| 63 | |
| 64 | if (mbase == MAP_FAILED) return NULL; |
| 65 | *allocated = msize; |
| 66 | return mbase; |
| 67 | } |
| 68 | |
| 69 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 70 | static unsigned StringToLong(char* buffer) { |
| 71 | return static_cast<unsigned>(strtol(buffer, NULL, 16)); // NOLINT |
| 72 | } |
| 73 | |
| 74 | |
| 75 | std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { |
| 76 | std::vector<SharedLibraryAddress> result; |
| 77 | static const int MAP_LENGTH = 1024; |
| 78 | int fd = open("/proc/self/maps", O_RDONLY); |
| 79 | if (fd < 0) return result; |
| 80 | while (true) { |
| 81 | char addr_buffer[11]; |
| 82 | addr_buffer[0] = '0'; |
| 83 | addr_buffer[1] = 'x'; |
| 84 | addr_buffer[10] = 0; |
| 85 | ssize_t bytes_read = read(fd, addr_buffer + 2, 8); |
| 86 | if (bytes_read < 8) break; |
| 87 | unsigned start = StringToLong(addr_buffer); |
| 88 | bytes_read = read(fd, addr_buffer + 2, 1); |
| 89 | if (bytes_read < 1) break; |
| 90 | if (addr_buffer[2] != '-') break; |
| 91 | bytes_read = read(fd, addr_buffer + 2, 8); |
| 92 | if (bytes_read < 8) break; |
| 93 | unsigned end = StringToLong(addr_buffer); |
| 94 | char buffer[MAP_LENGTH]; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 95 | bytes_read = -1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 96 | do { |
| 97 | bytes_read++; |
| 98 | if (bytes_read >= MAP_LENGTH - 1) |
| 99 | break; |
| 100 | bytes_read = read(fd, buffer + bytes_read, 1); |
| 101 | if (bytes_read < 1) break; |
| 102 | } while (buffer[bytes_read] != '\n'); |
| 103 | buffer[bytes_read] = 0; |
| 104 | // Ignore mappings that are not executable. |
| 105 | if (buffer[3] != 'x') continue; |
| 106 | char* start_of_path = index(buffer, '/'); |
| 107 | // There may be no filename in this line. Skip to next. |
| 108 | if (start_of_path == NULL) continue; |
| 109 | buffer[bytes_read] = 0; |
| 110 | result.push_back(SharedLibraryAddress(start_of_path, start, end)); |
| 111 | } |
| 112 | close(fd); |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | void OS::SignalCodeMovingGC() { |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | // Constants used for mmap. |
| 123 | static const int kMmapFd = -1; |
| 124 | static const int kMmapFdOffset = 0; |
| 125 | |
| 126 | |
| 127 | VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } |
| 128 | |
| 129 | |
| 130 | VirtualMemory::VirtualMemory(size_t size) |
| 131 | : address_(ReserveRegion(size)), size_(size) { } |
| 132 | |
| 133 | |
| 134 | VirtualMemory::VirtualMemory(size_t size, size_t alignment) |
| 135 | : address_(NULL), size_(0) { |
| 136 | DCHECK((alignment % OS::AllocateAlignment()) == 0); |
| 137 | size_t request_size = RoundUp(size + alignment, |
| 138 | static_cast<intptr_t>(OS::AllocateAlignment())); |
| 139 | void* reservation = mmap(OS::GetRandomMmapAddr(), |
| 140 | request_size, |
| 141 | PROT_NONE, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 142 | MAP_PRIVATE | MAP_ANON, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 143 | kMmapFd, |
| 144 | kMmapFdOffset); |
| 145 | if (reservation == MAP_FAILED) return; |
| 146 | |
| 147 | uint8_t* base = static_cast<uint8_t*>(reservation); |
| 148 | uint8_t* aligned_base = RoundUp(base, alignment); |
| 149 | DCHECK_LE(base, aligned_base); |
| 150 | |
| 151 | // Unmap extra memory reserved before and after the desired block. |
| 152 | if (aligned_base != base) { |
| 153 | size_t prefix_size = static_cast<size_t>(aligned_base - base); |
| 154 | OS::Free(base, prefix_size); |
| 155 | request_size -= prefix_size; |
| 156 | } |
| 157 | |
| 158 | size_t aligned_size = RoundUp(size, OS::AllocateAlignment()); |
| 159 | DCHECK_LE(aligned_size, request_size); |
| 160 | |
| 161 | if (aligned_size != request_size) { |
| 162 | size_t suffix_size = request_size - aligned_size; |
| 163 | OS::Free(aligned_base + aligned_size, suffix_size); |
| 164 | request_size -= suffix_size; |
| 165 | } |
| 166 | |
| 167 | DCHECK(aligned_size == request_size); |
| 168 | |
| 169 | address_ = static_cast<void*>(aligned_base); |
| 170 | size_ = aligned_size; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | VirtualMemory::~VirtualMemory() { |
| 175 | if (IsReserved()) { |
| 176 | bool result = ReleaseRegion(address(), size()); |
| 177 | DCHECK(result); |
| 178 | USE(result); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
| 183 | bool VirtualMemory::IsReserved() { |
| 184 | return address_ != NULL; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | void VirtualMemory::Reset() { |
| 189 | address_ = NULL; |
| 190 | size_ = 0; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
| 195 | return CommitRegion(address, size, is_executable); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | bool VirtualMemory::Uncommit(void* address, size_t size) { |
| 200 | return UncommitRegion(address, size); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | bool VirtualMemory::Guard(void* address) { |
| 205 | OS::Guard(address, OS::CommitPageSize()); |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | void* VirtualMemory::ReserveRegion(size_t size) { |
| 211 | void* result = mmap(OS::GetRandomMmapAddr(), |
| 212 | size, |
| 213 | PROT_NONE, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 214 | MAP_PRIVATE | MAP_ANON, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 215 | kMmapFd, |
| 216 | kMmapFdOffset); |
| 217 | |
| 218 | if (result == MAP_FAILED) return NULL; |
| 219 | |
| 220 | return result; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { |
| 225 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| 226 | if (MAP_FAILED == mmap(base, |
| 227 | size, |
| 228 | prot, |
| 229 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
| 230 | kMmapFd, |
| 231 | kMmapFdOffset)) { |
| 232 | return false; |
| 233 | } |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | bool VirtualMemory::UncommitRegion(void* base, size_t size) { |
| 239 | return mmap(base, |
| 240 | size, |
| 241 | PROT_NONE, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 242 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 243 | kMmapFd, |
| 244 | kMmapFdOffset) != MAP_FAILED; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | bool VirtualMemory::ReleaseRegion(void* base, size_t size) { |
| 249 | return munmap(base, size) == 0; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | bool VirtualMemory::HasLazyCommits() { |
| 254 | // TODO(alph): implement for the platform. |
| 255 | return false; |
| 256 | } |
| 257 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 258 | } // namespace base |
| 259 | } // namespace v8 |