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 Cygwin goes here. For the POSIX-compatible
6 // parts, the implementation is in platform-posix.cc.
10 #include <semaphore.h>
12 #include <strings.h> // index
13 #include <sys/mman.h> // mmap & munmap
15 #include <unistd.h> // sysconf
21 #include "src/base/macros.h"
22 #include "src/base/platform/platform.h"
23 #include "src/base/win32-headers.h"
29 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
30 if (std::isnan(time)) return "";
31 time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
32 struct tm* t = localtime(&tv);
33 if (NULL == t) return "";
34 return tzname[0]; // The location of the timezone string on Cygwin.
38 double OS::LocalTimeOffset(TimezoneCache* cache) {
39 // On Cygwin, struct tm does not contain a tm_gmtoff field.
40 time_t utc = time(NULL);
42 struct tm* loc = localtime(&utc);
44 // time - localtime includes any daylight savings offset, so subtract it.
45 return static_cast<double>((mktime(loc) - utc) * msPerSecond -
46 (loc->tm_isdst > 0 ? 3600 * msPerSecond : 0));
50 void* OS::Allocate(const size_t requested,
53 const size_t msize = RoundUp(requested, sysconf(_SC_PAGESIZE));
54 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
55 void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
56 if (mbase == MAP_FAILED) return NULL;
62 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
64 PosixMemoryMappedFile(FILE* file, void* memory, int size)
65 : file_(file), memory_(memory), size_(size) { }
66 virtual ~PosixMemoryMappedFile();
67 virtual void* memory() { return memory_; }
68 virtual int size() { return size_; }
76 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
77 FILE* file = fopen(name, "r+");
78 if (file == NULL) return NULL;
80 fseek(file, 0, SEEK_END);
81 int size = ftell(file);
84 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
85 return new PosixMemoryMappedFile(file, memory, size);
89 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
91 FILE* file = fopen(name, "w+");
92 if (file == NULL) return NULL;
93 int result = fwrite(initial, size, 1, file);
99 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
100 return new PosixMemoryMappedFile(file, memory, size);
104 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
105 if (memory_) munmap(memory_, size_);
110 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
111 std::vector<SharedLibraryAddresses> result;
112 // This function assumes that the layout of the file is as follows:
113 // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
114 // If we encounter an unexpected situation we abort scanning further entries.
115 FILE* fp = fopen("/proc/self/maps", "r");
116 if (fp == NULL) return result;
118 // Allocate enough room to be able to store a full file name.
119 const int kLibNameLen = FILENAME_MAX + 1;
120 char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
122 // This loop will terminate once the scanning hits an EOF.
124 uintptr_t start, end;
125 char attr_r, attr_w, attr_x, attr_p;
126 // Parse the addresses and permission bits at the beginning of the line.
127 if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
128 if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
131 if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
132 // Found a read-only executable entry. Skip characters until we reach
133 // the beginning of the filename or the end of the line.
136 } while ((c != EOF) && (c != '\n') && (c != '/'));
137 if (c == EOF) break; // EOF: Was unexpected, just exit.
139 // Process the filename if found.
141 ungetc(c, fp); // Push the '/' back into the stream to be read below.
143 // Read to the end of the line. Exit if the read fails.
144 if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
146 // Drop the newline character read by fgets. We do not need to check
147 // for a zero-length string because we know that we at least read the
149 lib_name[strlen(lib_name) - 1] = '\0';
151 // No library name found, just record the raw address range.
152 snprintf(lib_name, kLibNameLen,
153 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
155 result.push_back(SharedLibraryAddress(lib_name, start, end));
157 // Entry not describing executable data. Skip to end of line to set up
158 // reading the next entry.
161 } while ((c != EOF) && (c != '\n'));
171 void OS::SignalCodeMovingGC() {
172 // Nothing to do on Cygwin.
176 // The VirtualMemory implementation is taken from platform-win32.cc.
177 // The mmap-based virtual memory implementation as it is used on most posix
178 // platforms does not work well because Cygwin does not support MAP_FIXED.
179 // This causes VirtualMemory::Commit to not always commit the memory region
182 static void* RandomizedVirtualAlloc(size_t size, int action, int protection) {
185 if (protection == PAGE_EXECUTE_READWRITE || protection == PAGE_NOACCESS) {
186 // For exectutable pages try and randomize the allocation address
187 for (size_t attempts = 0; base == NULL && attempts < 3; ++attempts) {
188 base = VirtualAlloc(OS::GetRandomMmapAddr(), size, action, protection);
192 // After three attempts give up and let the OS find an address to use.
193 if (base == NULL) base = VirtualAlloc(NULL, size, action, protection);
199 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
202 VirtualMemory::VirtualMemory(size_t size)
203 : address_(ReserveRegion(size)), size_(size) { }
206 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
207 : address_(NULL), size_(0) {
208 DCHECK((alignment % OS::AllocateAlignment()) == 0);
209 size_t request_size = RoundUp(size + alignment,
210 static_cast<intptr_t>(OS::AllocateAlignment()));
211 void* address = ReserveRegion(request_size);
212 if (address == NULL) return;
213 uint8_t* base = RoundUp(static_cast<uint8_t*>(address), alignment);
214 // Try reducing the size by freeing and then reallocating a specific area.
215 bool result = ReleaseRegion(address, request_size);
218 address = VirtualAlloc(base, size, MEM_RESERVE, PAGE_NOACCESS);
219 if (address != NULL) {
221 DCHECK(base == static_cast<uint8_t*>(address));
223 // Resizing failed, just go with a bigger area.
224 address = ReserveRegion(request_size);
225 if (address == NULL) return;
228 size_ = request_size;
232 VirtualMemory::~VirtualMemory() {
234 bool result = ReleaseRegion(address_, size_);
241 bool VirtualMemory::IsReserved() {
242 return address_ != NULL;
246 void VirtualMemory::Reset() {
252 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
253 return CommitRegion(address, size, is_executable);
257 bool VirtualMemory::Uncommit(void* address, size_t size) {
258 DCHECK(IsReserved());
259 return UncommitRegion(address, size);
263 void* VirtualMemory::ReserveRegion(size_t size) {
264 return RandomizedVirtualAlloc(size, MEM_RESERVE, PAGE_NOACCESS);
268 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
269 int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
270 if (NULL == VirtualAlloc(base, size, MEM_COMMIT, prot)) {
277 bool VirtualMemory::Guard(void* address) {
278 if (NULL == VirtualAlloc(address,
279 OS::CommitPageSize(),
288 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
289 return VirtualFree(base, size, MEM_DECOMMIT) != 0;
293 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
294 return VirtualFree(base, 0, MEM_RELEASE) != 0;
298 bool VirtualMemory::HasLazyCommits() {
299 // TODO(alph): implement for the platform.
303 } } // namespace v8::base