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 FreeBSD goes here. For the POSIX-compatible
6 // parts, the implementation is in platform-posix.cc.
12 #include <sys/resource.h>
14 #include <sys/types.h>
15 #include <sys/ucontext.h>
17 #include <sys/fcntl.h> // open
18 #include <sys/mman.h> // mmap & munmap
19 #include <sys/stat.h> // open
20 #include <unistd.h> // getpagesize
21 // If you don't have execinfo.h then you need devel/libexecinfo from ports.
25 #include <strings.h> // index
31 #include "src/base/macros.h"
32 #include "src/base/platform/platform.h"
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));
42 struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn)
43 if (NULL == t) return "";
48 double OS::LocalTimeOffset(TimezoneCache* cache) {
49 time_t tv = time(NULL);
50 struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn)
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));
57 void* OS::Allocate(const size_t requested,
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);
64 if (mbase == MAP_FAILED) return NULL;
70 static unsigned StringToLong(char* buffer) {
71 return static_cast<unsigned>(strtol(buffer, NULL, 16)); // NOLINT
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;
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];
98 if (bytes_read >= MAP_LENGTH - 1)
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));
117 void OS::SignalCodeMovingGC() {
122 // Constants used for mmap.
123 static const int kMmapFd = -1;
124 static const int kMmapFdOffset = 0;
127 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
130 VirtualMemory::VirtualMemory(size_t size)
131 : address_(ReserveRegion(size)), size_(size) { }
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(),
142 MAP_PRIVATE | MAP_ANON,
145 if (reservation == MAP_FAILED) return;
147 uint8_t* base = static_cast<uint8_t*>(reservation);
148 uint8_t* aligned_base = RoundUp(base, alignment);
149 DCHECK_LE(base, aligned_base);
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;
158 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
159 DCHECK_LE(aligned_size, request_size);
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;
167 DCHECK(aligned_size == request_size);
169 address_ = static_cast<void*>(aligned_base);
170 size_ = aligned_size;
174 VirtualMemory::~VirtualMemory() {
176 bool result = ReleaseRegion(address(), size());
183 bool VirtualMemory::IsReserved() {
184 return address_ != NULL;
188 void VirtualMemory::Reset() {
194 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
195 return CommitRegion(address, size, is_executable);
199 bool VirtualMemory::Uncommit(void* address, size_t size) {
200 return UncommitRegion(address, size);
204 bool VirtualMemory::Guard(void* address) {
205 OS::Guard(address, OS::CommitPageSize());
210 void* VirtualMemory::ReserveRegion(size_t size) {
211 void* result = mmap(OS::GetRandomMmapAddr(),
214 MAP_PRIVATE | MAP_ANON,
218 if (result == MAP_FAILED) return NULL;
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,
229 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
238 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
242 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
244 kMmapFdOffset) != MAP_FAILED;
248 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
249 return munmap(base, size) == 0;
253 bool VirtualMemory::HasLazyCommits() {
254 // TODO(alph): implement for the platform.
258 } } // namespace v8::base