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 <sys/types.h> // mmap & munmap
21 #include <unistd.h> // getpagesize
22 // If you don't have execinfo.h then you need devel/libexecinfo from ports.
26 #include <strings.h> // index
32 #include "src/base/macros.h"
33 #include "src/base/platform/platform.h"
40 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
41 if (std::isnan(time)) return "";
42 time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
43 struct tm* t = localtime(&tv);
44 if (NULL == t) return "";
49 double OS::LocalTimeOffset(TimezoneCache* cache) {
50 time_t tv = time(NULL);
51 struct tm* t = localtime(&tv);
52 // tm_gmtoff includes any daylight savings offset, so subtract it.
53 return static_cast<double>(t->tm_gmtoff * msPerSecond -
54 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
58 void* OS::Allocate(const size_t requested,
61 const size_t msize = RoundUp(requested, getpagesize());
62 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0);
63 void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
65 if (mbase == MAP_FAILED) return NULL;
71 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
73 PosixMemoryMappedFile(FILE* file, void* memory, int size)
74 : file_(file), memory_(memory), size_(size) { }
75 virtual ~PosixMemoryMappedFile();
76 virtual void* memory() { return memory_; }
77 virtual int size() { return size_; }
85 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
86 FILE* file = fopen(name, "r+");
87 if (file == NULL) return NULL;
89 fseek(file, 0, SEEK_END);
90 int size = ftell(file);
93 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
94 return new PosixMemoryMappedFile(file, memory, size);
98 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
100 FILE* file = fopen(name, "w+");
101 if (file == NULL) return NULL;
102 int result = fwrite(initial, size, 1, file);
108 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
109 return new PosixMemoryMappedFile(file, memory, size);
113 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
114 if (memory_) munmap(memory_, size_);
119 static unsigned StringToLong(char* buffer) {
120 return static_cast<unsigned>(strtol(buffer, NULL, 16)); // NOLINT
124 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
125 std::vector<SharedLibraryAddress> result;
126 static const int MAP_LENGTH = 1024;
127 int fd = open("/proc/self/maps", O_RDONLY);
128 if (fd < 0) return result;
130 char addr_buffer[11];
131 addr_buffer[0] = '0';
132 addr_buffer[1] = 'x';
134 ssize_t bytes_read = read(fd, addr_buffer + 2, 8);
135 if (bytes_read < 8) break;
136 unsigned start = StringToLong(addr_buffer);
137 bytes_read = read(fd, addr_buffer + 2, 1);
138 if (bytes_read < 1) break;
139 if (addr_buffer[2] != '-') break;
140 bytes_read = read(fd, addr_buffer + 2, 8);
141 if (bytes_read < 8) break;
142 unsigned end = StringToLong(addr_buffer);
143 char buffer[MAP_LENGTH];
147 if (bytes_read >= MAP_LENGTH - 1)
149 bytes_read = read(fd, buffer + bytes_read, 1);
150 if (bytes_read < 1) break;
151 } while (buffer[bytes_read] != '\n');
152 buffer[bytes_read] = 0;
153 // Ignore mappings that are not executable.
154 if (buffer[3] != 'x') continue;
155 char* start_of_path = index(buffer, '/');
156 // There may be no filename in this line. Skip to next.
157 if (start_of_path == NULL) continue;
158 buffer[bytes_read] = 0;
159 result.push_back(SharedLibraryAddress(start_of_path, start, end));
166 void OS::SignalCodeMovingGC() {
171 // Constants used for mmap.
172 static const int kMmapFd = -1;
173 static const int kMmapFdOffset = 0;
176 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
179 VirtualMemory::VirtualMemory(size_t size)
180 : address_(ReserveRegion(size)), size_(size) { }
183 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
184 : address_(NULL), size_(0) {
185 DCHECK((alignment % OS::AllocateAlignment()) == 0);
186 size_t request_size = RoundUp(size + alignment,
187 static_cast<intptr_t>(OS::AllocateAlignment()));
188 void* reservation = mmap(OS::GetRandomMmapAddr(),
191 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
194 if (reservation == MAP_FAILED) return;
196 uint8_t* base = static_cast<uint8_t*>(reservation);
197 uint8_t* aligned_base = RoundUp(base, alignment);
198 DCHECK_LE(base, aligned_base);
200 // Unmap extra memory reserved before and after the desired block.
201 if (aligned_base != base) {
202 size_t prefix_size = static_cast<size_t>(aligned_base - base);
203 OS::Free(base, prefix_size);
204 request_size -= prefix_size;
207 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
208 DCHECK_LE(aligned_size, request_size);
210 if (aligned_size != request_size) {
211 size_t suffix_size = request_size - aligned_size;
212 OS::Free(aligned_base + aligned_size, suffix_size);
213 request_size -= suffix_size;
216 DCHECK(aligned_size == request_size);
218 address_ = static_cast<void*>(aligned_base);
219 size_ = aligned_size;
223 VirtualMemory::~VirtualMemory() {
225 bool result = ReleaseRegion(address(), size());
232 bool VirtualMemory::IsReserved() {
233 return address_ != NULL;
237 void VirtualMemory::Reset() {
243 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
244 return CommitRegion(address, size, is_executable);
248 bool VirtualMemory::Uncommit(void* address, size_t size) {
249 return UncommitRegion(address, size);
253 bool VirtualMemory::Guard(void* address) {
254 OS::Guard(address, OS::CommitPageSize());
259 void* VirtualMemory::ReserveRegion(size_t size) {
260 void* result = mmap(OS::GetRandomMmapAddr(),
263 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
267 if (result == MAP_FAILED) return NULL;
273 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
274 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
275 if (MAP_FAILED == mmap(base,
278 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
287 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
291 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
293 kMmapFdOffset) != MAP_FAILED;
297 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
298 return munmap(base, size) == 0;
302 bool VirtualMemory::HasLazyCommits() {
303 // TODO(alph): implement for the platform.
307 } } // namespace v8::base