blob: 46c74b017ff0181bd8b2b6ccedd5f51def079cd0 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000028// Platform specific code for Linux goes here. For the POSIX comaptible parts
29// the implementation is in platform-posix.cc.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000030
31#include <pthread.h>
32#include <semaphore.h>
33#include <signal.h>
34#include <sys/time.h>
35#include <sys/resource.h>
lrn@chromium.org303ada72010-10-27 09:33:13 +000036#include <sys/syscall.h>
ager@chromium.org381abbb2009-02-25 13:23:22 +000037#include <sys/types.h>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038#include <stdlib.h>
39
40// Ubuntu Dapper requires memory pages to be marked as
41// executable. Otherwise, OS raises an exception when executing code
42// in that page.
43#include <sys/types.h> // mmap & munmap
ager@chromium.org236ad962008-09-25 09:45:57 +000044#include <sys/mman.h> // mmap & munmap
45#include <sys/stat.h> // open
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000046#include <fcntl.h> // open
47#include <unistd.h> // sysconf
48#ifdef __GLIBC__
ager@chromium.org236ad962008-09-25 09:45:57 +000049#include <execinfo.h> // backtrace, backtrace_symbols
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000050#endif // def __GLIBC__
ager@chromium.org236ad962008-09-25 09:45:57 +000051#include <strings.h> // index
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052#include <errno.h>
53#include <stdarg.h>
54
55#undef MAP_TYPE
56
57#include "v8.h"
58
59#include "platform.h"
ager@chromium.orga1645e22009-09-09 19:27:10 +000060#include "top.h"
61#include "v8threads.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000062#include "vm-state-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000063
64
kasperl@chromium.org71affb52009-05-26 05:44:31 +000065namespace v8 {
66namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000067
68// 0 is never a valid thread id on Linux since tids and pids share a
69// name space and pid 0 is reserved (see man 2 kill).
70static const pthread_t kNoThread = (pthread_t) 0;
71
72
73double ceiling(double x) {
74 return ceil(x);
75}
76
77
78void OS::Setup() {
79 // Seed the random number generator.
ager@chromium.org9258b6b2008-09-11 09:11:10 +000080 // Convert the current time to a 64-bit integer first, before converting it
81 // to an unsigned. Going directly can cause an overflow and the seed to be
82 // set to all ones. The seed will be identical for different instances that
83 // call this setup code within the same millisecond.
84 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis());
85 srandom(static_cast<unsigned int>(seed));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000086}
87
88
ager@chromium.orgc4c92722009-11-18 14:12:51 +000089uint64_t OS::CpuFeaturesImpliedByPlatform() {
90#if (defined(__VFP_FP__) && !defined(__SOFTFP__))
91 // Here gcc is telling us that we are on an ARM and gcc is assuming that we
92 // have VFP3 instructions. If gcc can assume it then so can we.
93 return 1u << VFP3;
ager@chromium.org5c838252010-02-19 08:53:10 +000094#elif CAN_USE_ARMV7_INSTRUCTIONS
95 return 1u << ARMv7;
ager@chromium.orgc4c92722009-11-18 14:12:51 +000096#else
97 return 0; // Linux runs on anything.
98#endif
99}
100
101
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000102#ifdef __arm__
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000103static bool CPUInfoContainsString(const char * search_string) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000104 const char* file_name = "/proc/cpuinfo";
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000105 // This is written as a straight shot one pass parser
106 // and not using STL string and ifstream because,
107 // on Linux, it's reading from a (non-mmap-able)
108 // character special device.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000109 FILE* f = NULL;
110 const char* what = search_string;
111
112 if (NULL == (f = fopen(file_name, "r")))
113 return false;
114
115 int k;
116 while (EOF != (k = fgetc(f))) {
117 if (k == *what) {
118 ++what;
119 while ((*what != '\0') && (*what == fgetc(f))) {
120 ++what;
121 }
122 if (*what == '\0') {
123 fclose(f);
124 return true;
125 } else {
126 what = search_string;
127 }
128 }
129 }
130 fclose(f);
131
132 // Did not find string in the proc file.
133 return false;
134}
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000135
136bool OS::ArmCpuHasFeature(CpuFeature feature) {
137 const int max_items = 2;
138 const char* search_strings[max_items] = { NULL, NULL };
139 int search_items = 0;
140 // Simple detection of VFP at runtime for Linux.
141 // It is based on /proc/cpuinfo, which reveals hardware configuration
142 // to user-space applications. According to ARM (mid 2009), no similar
143 // facility is universally available on the ARM architectures,
144 // so it's up to individual OSes to provide such.
145 switch (feature) {
146 case VFP3:
147 search_strings[0] = "vfpv3";
148 // Some old kernels will report vfp for A8, not vfpv3, so we check for
149 // A8 explicitely. The cpuinfo file report the CPU Part which for Cortex
150 // A8 is 0xc08.
151 search_strings[1] = "0xc08";
152 search_items = 2;
153 ASSERT(search_items <= max_items);
154 break;
155 case ARMv7:
156 search_strings[0] = "ARMv7" ;
157 search_items = 1;
158 ASSERT(search_items <= max_items);
159 break;
160 default:
161 UNREACHABLE();
162 }
163
164 for (int i = 0; i < search_items; ++i) {
165 if (CPUInfoContainsString(search_strings[i])) {
166 return true;
167 }
168 }
169
170 return false;
171}
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000172#endif // def __arm__
173
174
ager@chromium.org236ad962008-09-25 09:45:57 +0000175int OS::ActivationFrameAlignment() {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000176#ifdef V8_TARGET_ARCH_ARM
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000177 // On EABI ARM targets this is required for fp correctness in the
178 // runtime system.
ager@chromium.org3a6061e2009-03-12 14:24:36 +0000179 return 8;
ager@chromium.org5c838252010-02-19 08:53:10 +0000180#elif V8_TARGET_ARCH_MIPS
181 return 8;
182#endif
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000183 // With gcc 4.4 the tree vectorization optimizer can generate code
ager@chromium.orge2902be2009-06-08 12:21:35 +0000184 // that requires 16 byte alignment such as movdqa on x86.
185 return 16;
ager@chromium.org236ad962008-09-25 09:45:57 +0000186}
187
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000189void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000190#if defined(V8_TARGET_ARCH_ARM) && defined(__arm__)
191 // Only use on ARM hardware.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000192 MemoryBarrier();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000193#else
194 __asm__ __volatile__("" : : : "memory");
195 // An x86 store acts as a release barrier.
196#endif
197 *ptr = value;
198}
199
200
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000201const char* OS::LocalTimezone(double time) {
202 if (isnan(time)) return "";
203 time_t tv = static_cast<time_t>(floor(time/msPerSecond));
204 struct tm* t = localtime(&tv);
205 if (NULL == t) return "";
206 return t->tm_zone;
207}
208
209
210double OS::LocalTimeOffset() {
211 time_t tv = time(NULL);
212 struct tm* t = localtime(&tv);
213 // tm_gmtoff includes any daylight savings offset, so subtract it.
214 return static_cast<double>(t->tm_gmtoff * msPerSecond -
215 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
216}
217
218
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219// We keep the lowest and highest addresses mapped as a quick way of
220// determining that pointers are outside the heap (used mostly in assertions
221// and verification). The estimate is conservative, ie, not all addresses in
222// 'allocated' space are actually allocated to our heap. The range is
223// [lowest, highest), inclusive on the low and and exclusive on the high end.
224static void* lowest_ever_allocated = reinterpret_cast<void*>(-1);
225static void* highest_ever_allocated = reinterpret_cast<void*>(0);
226
227
228static void UpdateAllocatedSpaceLimits(void* address, int size) {
229 lowest_ever_allocated = Min(lowest_ever_allocated, address);
230 highest_ever_allocated =
231 Max(highest_ever_allocated,
232 reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size));
233}
234
235
236bool OS::IsOutsideAllocatedSpace(void* address) {
237 return address < lowest_ever_allocated || address >= highest_ever_allocated;
238}
239
240
241size_t OS::AllocateAlignment() {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000242 return sysconf(_SC_PAGESIZE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243}
244
245
kasper.lund7276f142008-07-30 08:49:36 +0000246void* OS::Allocate(const size_t requested,
247 size_t* allocated,
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000248 bool is_executable) {
sgjesse@chromium.orgc3a01972010-08-04 09:46:24 +0000249 // TODO(805): Port randomization of allocated executable memory to Linux.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000250 const size_t msize = RoundUp(requested, sysconf(_SC_PAGESIZE));
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000251 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
kasper.lund7276f142008-07-30 08:49:36 +0000252 void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000253 if (mbase == MAP_FAILED) {
254 LOG(StringEvent("OS::Allocate", "mmap failed"));
255 return NULL;
256 }
257 *allocated = msize;
258 UpdateAllocatedSpaceLimits(mbase, msize);
259 return mbase;
260}
261
262
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000263void OS::Free(void* address, const size_t size) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264 // TODO(1240712): munmap has a return value which is ignored here.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000265 int result = munmap(address, size);
266 USE(result);
267 ASSERT(result == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268}
269
270
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000271#ifdef ENABLE_HEAP_PROTECTION
272
273void OS::Protect(void* address, size_t size) {
274 // TODO(1240712): mprotect has a return value which is ignored here.
275 mprotect(address, size, PROT_READ);
276}
277
278
279void OS::Unprotect(void* address, size_t size, bool is_executable) {
280 // TODO(1240712): mprotect has a return value which is ignored here.
281 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
282 mprotect(address, size, prot);
283}
284
285#endif
286
287
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288void OS::Sleep(int milliseconds) {
289 unsigned int ms = static_cast<unsigned int>(milliseconds);
290 usleep(1000 * ms);
291}
292
293
294void OS::Abort() {
295 // Redirect to std abort to signal abnormal program termination.
296 abort();
297}
298
299
kasper.lund7276f142008-07-30 08:49:36 +0000300void OS::DebugBreak() {
ager@chromium.org5ec48922009-05-05 07:25:34 +0000301// TODO(lrn): Introduce processor define for runtime system (!= V8_ARCH_x,
302// which is the architecture of generated code).
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000303#if (defined(__arm__) || defined(__thumb__))
304# if defined(CAN_USE_ARMV5_INSTRUCTIONS)
kasper.lund7276f142008-07-30 08:49:36 +0000305 asm("bkpt 0");
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000306# endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000307#elif defined(__mips__)
308 asm("break");
kasper.lund7276f142008-07-30 08:49:36 +0000309#else
310 asm("int $3");
311#endif
312}
313
314
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315class PosixMemoryMappedFile : public OS::MemoryMappedFile {
316 public:
317 PosixMemoryMappedFile(FILE* file, void* memory, int size)
318 : file_(file), memory_(memory), size_(size) { }
319 virtual ~PosixMemoryMappedFile();
320 virtual void* memory() { return memory_; }
321 private:
322 FILE* file_;
323 void* memory_;
324 int size_;
325};
326
327
328OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
329 void* initial) {
330 FILE* file = fopen(name, "w+");
331 if (file == NULL) return NULL;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000332 int result = fwrite(initial, size, 1, file);
333 if (result < 1) {
334 fclose(file);
335 return NULL;
336 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 void* memory =
338 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
339 return new PosixMemoryMappedFile(file, memory, size);
340}
341
342
343PosixMemoryMappedFile::~PosixMemoryMappedFile() {
344 if (memory_) munmap(memory_, size_);
345 fclose(file_);
346}
347
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349void OS::LogSharedLibraryAddresses() {
350#ifdef ENABLE_LOGGING_AND_PROFILING
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000351 // This function assumes that the layout of the file is as follows:
352 // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
353 // If we encounter an unexpected situation we abort scanning further entries.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000354 FILE* fp = fopen("/proc/self/maps", "r");
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000355 if (fp == NULL) return;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000356
357 // Allocate enough room to be able to store a full file name.
358 const int kLibNameLen = FILENAME_MAX + 1;
359 char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
360
361 // This loop will terminate once the scanning hits an EOF.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 while (true) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000363 uintptr_t start, end;
364 char attr_r, attr_w, attr_x, attr_p;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000365 // Parse the addresses and permission bits at the beginning of the line.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000366 if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
367 if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000368
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000369 int c;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000370 if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
371 // Found a read-only executable entry. Skip characters until we reach
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000372 // the beginning of the filename or the end of the line.
373 do {
374 c = getc(fp);
375 } while ((c != EOF) && (c != '\n') && (c != '/'));
376 if (c == EOF) break; // EOF: Was unexpected, just exit.
377
378 // Process the filename if found.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000379 if (c == '/') {
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000380 ungetc(c, fp); // Push the '/' back into the stream to be read below.
381
382 // Read to the end of the line. Exit if the read fails.
383 if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
384
385 // Drop the newline character read by fgets. We do not need to check
386 // for a zero-length string because we know that we at least read the
387 // '/' character.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000388 lib_name[strlen(lib_name) - 1] = '\0';
389 } else {
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000390 // No library name found, just record the raw address range.
391 snprintf(lib_name, kLibNameLen,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000392 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
393 }
394 LOG(SharedLibraryEvent(lib_name, start, end));
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000395 } else {
396 // Entry not describing executable data. Skip to end of line to setup
397 // reading the next entry.
398 do {
399 c = getc(fp);
400 } while ((c != EOF) && (c != '\n'));
401 if (c == EOF) break;
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000402 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403 }
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000404 free(lib_name);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000405 fclose(fp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406#endif
407}
408
409
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000410static const char kGCFakeMmap[] = "/tmp/__v8_gc__";
411
412
413void OS::SignalCodeMovingGC() {
414#ifdef ENABLE_LOGGING_AND_PROFILING
415 // Support for ll_prof.py.
416 //
417 // The Linux profiler built into the kernel logs all mmap's with
418 // PROT_EXEC so that analysis tools can properly attribute ticks. We
419 // do a mmap with a name known by ll_prof.py and immediately munmap
420 // it. This injects a GC marker into the stream of events generated
421 // by the kernel and allows us to synchronize V8 code log and the
422 // kernel log.
423 int size = sysconf(_SC_PAGESIZE);
424 FILE* f = fopen(kGCFakeMmap, "w+");
425 void* addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE,
426 fileno(f), 0);
427 ASSERT(addr != MAP_FAILED);
428 munmap(addr, size);
429 fclose(f);
430#endif
431}
432
433
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000434int OS::StackWalk(Vector<OS::StackFrame> frames) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000435 // backtrace is a glibc extension.
436#ifdef __GLIBC__
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000437 int frames_size = frames.length();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000438 ScopedVector<void*> addresses(frames_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000440 int frames_count = backtrace(addresses.start(), frames_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000442 char** symbols = backtrace_symbols(addresses.start(), frames_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443 if (symbols == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444 return kStackWalkError;
445 }
446
447 for (int i = 0; i < frames_count; i++) {
448 frames[i].address = addresses[i];
449 // Format a text representation of the frame based on the information
450 // available.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000451 SNPrintF(MutableCStrVector(frames[i].text, kStackWalkMaxTextLen),
452 "%s",
453 symbols[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454 // Make sure line termination is in place.
455 frames[i].text[kStackWalkMaxTextLen - 1] = '\0';
456 }
457
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458 free(symbols);
459
460 return frames_count;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000461#else // ndef __GLIBC__
462 return 0;
463#endif // ndef __GLIBC__
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464}
465
466
467// Constants used for mmap.
468static const int kMmapFd = -1;
469static const int kMmapFdOffset = 0;
470
471
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000472VirtualMemory::VirtualMemory(size_t size) {
473 address_ = mmap(NULL, size, PROT_NONE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
475 kMmapFd, kMmapFdOffset);
476 size_ = size;
477}
478
479
480VirtualMemory::~VirtualMemory() {
481 if (IsReserved()) {
482 if (0 == munmap(address(), size())) address_ = MAP_FAILED;
483 }
484}
485
486
487bool VirtualMemory::IsReserved() {
488 return address_ != MAP_FAILED;
489}
490
491
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000492bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
493 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
kasper.lund7276f142008-07-30 08:49:36 +0000494 if (MAP_FAILED == mmap(address, size, prot,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
496 kMmapFd, kMmapFdOffset)) {
497 return false;
498 }
499
500 UpdateAllocatedSpaceLimits(address, size);
501 return true;
502}
503
504
505bool VirtualMemory::Uncommit(void* address, size_t size) {
506 return mmap(address, size, PROT_NONE,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000507 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508 kMmapFd, kMmapFdOffset) != MAP_FAILED;
509}
510
511
512class ThreadHandle::PlatformData : public Malloced {
513 public:
514 explicit PlatformData(ThreadHandle::Kind kind) {
515 Initialize(kind);
516 }
517
518 void Initialize(ThreadHandle::Kind kind) {
519 switch (kind) {
520 case ThreadHandle::SELF: thread_ = pthread_self(); break;
521 case ThreadHandle::INVALID: thread_ = kNoThread; break;
522 }
523 }
ager@chromium.org41826e72009-03-30 13:30:57 +0000524
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 pthread_t thread_; // Thread handle for pthread.
526};
527
528
529ThreadHandle::ThreadHandle(Kind kind) {
530 data_ = new PlatformData(kind);
531}
532
533
534void ThreadHandle::Initialize(ThreadHandle::Kind kind) {
535 data_->Initialize(kind);
536}
537
538
539ThreadHandle::~ThreadHandle() {
540 delete data_;
541}
542
543
544bool ThreadHandle::IsSelf() const {
545 return pthread_equal(data_->thread_, pthread_self());
546}
547
548
549bool ThreadHandle::IsValid() const {
550 return data_->thread_ != kNoThread;
551}
552
553
554Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) {
555}
556
557
558Thread::~Thread() {
559}
560
561
562static void* ThreadEntry(void* arg) {
563 Thread* thread = reinterpret_cast<Thread*>(arg);
564 // This is also initialized by the first argument to pthread_create() but we
565 // don't know which thread will run first (the original thread or the new
566 // one) so we initialize it here too.
567 thread->thread_handle_data()->thread_ = pthread_self();
568 ASSERT(thread->IsValid());
569 thread->Run();
570 return NULL;
571}
572
573
574void Thread::Start() {
575 pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this);
576 ASSERT(IsValid());
577}
578
579
580void Thread::Join() {
581 pthread_join(thread_handle_data()->thread_, NULL);
582}
583
584
585Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
586 pthread_key_t key;
587 int result = pthread_key_create(&key, NULL);
588 USE(result);
589 ASSERT(result == 0);
590 return static_cast<LocalStorageKey>(key);
591}
592
593
594void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
595 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
596 int result = pthread_key_delete(pthread_key);
597 USE(result);
598 ASSERT(result == 0);
599}
600
601
602void* Thread::GetThreadLocal(LocalStorageKey key) {
603 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
604 return pthread_getspecific(pthread_key);
605}
606
607
608void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
609 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
610 pthread_setspecific(pthread_key, value);
611}
612
613
614void Thread::YieldCPU() {
615 sched_yield();
616}
617
618
619class LinuxMutex : public Mutex {
620 public:
621
622 LinuxMutex() {
623 pthread_mutexattr_t attrs;
624 int result = pthread_mutexattr_init(&attrs);
625 ASSERT(result == 0);
626 result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE);
627 ASSERT(result == 0);
628 result = pthread_mutex_init(&mutex_, &attrs);
629 ASSERT(result == 0);
630 }
631
632 virtual ~LinuxMutex() { pthread_mutex_destroy(&mutex_); }
633
634 virtual int Lock() {
635 int result = pthread_mutex_lock(&mutex_);
636 return result;
637 }
638
639 virtual int Unlock() {
640 int result = pthread_mutex_unlock(&mutex_);
641 return result;
642 }
643
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000644 virtual bool TryLock() {
645 int result = pthread_mutex_trylock(&mutex_);
646 // Return false if the lock is busy and locking failed.
647 if (result == EBUSY) {
648 return false;
649 }
650 ASSERT(result == 0); // Verify no other errors.
651 return true;
652 }
653
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000654 private:
655 pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.
656};
657
658
659Mutex* OS::CreateMutex() {
660 return new LinuxMutex();
661}
662
663
664class LinuxSemaphore : public Semaphore {
665 public:
666 explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); }
667 virtual ~LinuxSemaphore() { sem_destroy(&sem_); }
668
kasper.lund7276f142008-07-30 08:49:36 +0000669 virtual void Wait();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000670 virtual bool Wait(int timeout);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671 virtual void Signal() { sem_post(&sem_); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672 private:
673 sem_t sem_;
674};
675
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000676
kasper.lund7276f142008-07-30 08:49:36 +0000677void LinuxSemaphore::Wait() {
678 while (true) {
679 int result = sem_wait(&sem_);
680 if (result == 0) return; // Successfully got semaphore.
681 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
682 }
683}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000684
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000685
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000686#ifndef TIMEVAL_TO_TIMESPEC
687#define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
688 (ts)->tv_sec = (tv)->tv_sec; \
689 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
690} while (false)
691#endif
692
693
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000694bool LinuxSemaphore::Wait(int timeout) {
695 const long kOneSecondMicros = 1000000; // NOLINT
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000696
697 // Split timeout into second and nanosecond parts.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000698 struct timeval delta;
699 delta.tv_usec = timeout % kOneSecondMicros;
700 delta.tv_sec = timeout / kOneSecondMicros;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000701
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000702 struct timeval current_time;
703 // Get the current time.
704 if (gettimeofday(&current_time, NULL) == -1) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000705 return false;
706 }
707
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000708 // Calculate time for end of timeout.
709 struct timeval end_time;
710 timeradd(&current_time, &delta, &end_time);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000711
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000712 struct timespec ts;
713 TIMEVAL_TO_TIMESPEC(&end_time, &ts);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000714 // Wait for semaphore signalled or timeout.
715 while (true) {
716 int result = sem_timedwait(&sem_, &ts);
717 if (result == 0) return true; // Successfully got semaphore.
718 if (result > 0) {
719 // For glibc prior to 2.3.4 sem_timedwait returns the error instead of -1.
720 errno = result;
721 result = -1;
722 }
723 if (result == -1 && errno == ETIMEDOUT) return false; // Timeout.
724 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
725 }
726}
727
728
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000729Semaphore* OS::CreateSemaphore(int count) {
730 return new LinuxSemaphore(count);
731}
732
ager@chromium.org381abbb2009-02-25 13:23:22 +0000733
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000734#ifdef ENABLE_LOGGING_AND_PROFILING
735
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000736static Sampler* active_sampler_ = NULL;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000737static int vm_tid_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000738
kasperl@chromium.orgacae3782009-04-11 09:17:08 +0000739
740#if !defined(__GLIBC__) && (defined(__arm__) || defined(__thumb__))
741// Android runs a fairly new Linux kernel, so signal info is there,
742// but the C library doesn't have the structs defined.
743
744struct sigcontext {
745 uint32_t trap_no;
746 uint32_t error_code;
747 uint32_t oldmask;
748 uint32_t gregs[16];
749 uint32_t arm_cpsr;
750 uint32_t fault_address;
751};
752typedef uint32_t __sigset_t;
753typedef struct sigcontext mcontext_t;
754typedef struct ucontext {
755 uint32_t uc_flags;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000756 struct ucontext* uc_link;
kasperl@chromium.orgacae3782009-04-11 09:17:08 +0000757 stack_t uc_stack;
758 mcontext_t uc_mcontext;
759 __sigset_t uc_sigmask;
760} ucontext_t;
761enum ArmRegisters {R15 = 15, R13 = 13, R11 = 11};
762
763#endif
764
765
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000766static int GetThreadID() {
767 // Glibc doesn't provide a wrapper for gettid(2).
768 return syscall(SYS_gettid);
769}
770
771
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000773#ifndef V8_HOST_ARCH_MIPS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000774 USE(info);
775 if (signal != SIGPROF) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000776 if (active_sampler_ == NULL || !active_sampler_->IsActive()) return;
777 if (vm_tid_ != GetThreadID()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000778
lrn@chromium.org25156de2010-04-06 13:10:27 +0000779 TickSample sample_obj;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000780 TickSample* sample = CpuProfiler::TickSampleEvent();
ager@chromium.org357bf652010-04-12 11:30:10 +0000781 if (sample == NULL) sample = &sample_obj;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000782
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000783 // Extracting the sample from the context is extremely machine dependent.
784 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context);
785 mcontext_t& mcontext = ucontext->uc_mcontext;
786 sample->state = Top::current_vm_state();
ager@chromium.org9085a012009-05-11 19:22:57 +0000787#if V8_HOST_ARCH_IA32
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000788 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]);
789 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]);
790 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]);
ager@chromium.org9085a012009-05-11 19:22:57 +0000791#elif V8_HOST_ARCH_X64
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000792 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]);
793 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]);
794 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]);
ager@chromium.org9085a012009-05-11 19:22:57 +0000795#elif V8_HOST_ARCH_ARM
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000796// An undefined macro evaluates to 0, so this applies to Android's Bionic also.
797#if (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000798 sample->pc = reinterpret_cast<Address>(mcontext.gregs[R15]);
799 sample->sp = reinterpret_cast<Address>(mcontext.gregs[R13]);
800 sample->fp = reinterpret_cast<Address>(mcontext.gregs[R11]);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000801#else
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000802 sample->pc = reinterpret_cast<Address>(mcontext.arm_pc);
803 sample->sp = reinterpret_cast<Address>(mcontext.arm_sp);
804 sample->fp = reinterpret_cast<Address>(mcontext.arm_fp);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000805#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000806#elif V8_HOST_ARCH_MIPS
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000807 // Implement this on MIPS.
808 UNIMPLEMENTED();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000809#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000810 active_sampler_->SampleStack(sample);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000811 active_sampler_->Tick(sample);
812#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813}
814
815
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000816class Sampler::PlatformData : public Malloced {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000817 public:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000818 enum SleepInterval {
819 FULL_INTERVAL,
820 HALF_INTERVAL
821 };
822
lrn@chromium.org303ada72010-10-27 09:33:13 +0000823 explicit PlatformData(Sampler* sampler)
824 : sampler_(sampler),
825 signal_handler_installed_(false),
826 vm_tgid_(getpid()),
lrn@chromium.org303ada72010-10-27 09:33:13 +0000827 signal_sender_launched_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 }
829
lrn@chromium.org303ada72010-10-27 09:33:13 +0000830 void SignalSender() {
831 while (sampler_->IsActive()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000832 if (rate_limiter_.SuspendIfNecessary()) continue;
833 if (sampler_->IsProfiling() && RuntimeProfiler::IsEnabled()) {
834 SendProfilingSignal();
835 Sleep(HALF_INTERVAL);
836 RuntimeProfiler::NotifyTick();
837 Sleep(HALF_INTERVAL);
838 } else {
839 if (sampler_->IsProfiling()) SendProfilingSignal();
840 if (RuntimeProfiler::IsEnabled()) RuntimeProfiler::NotifyTick();
841 Sleep(FULL_INTERVAL);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000842 }
lrn@chromium.org303ada72010-10-27 09:33:13 +0000843 }
844 }
845
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000846 void SendProfilingSignal() {
847 // Glibc doesn't provide a wrapper for tgkill(2).
848 syscall(SYS_tgkill, vm_tgid_, vm_tid_, SIGPROF);
849 }
850
851 void Sleep(SleepInterval full_or_half) {
852 // Convert ms to us and subtract 100 us to compensate delays
853 // occuring during signal delivery.
854 useconds_t interval = sampler_->interval_ * 1000 - 100;
855 if (full_or_half == HALF_INTERVAL) interval /= 2;
856 int result = usleep(interval);
857#ifdef DEBUG
858 if (result != 0 && errno != EINTR) {
859 fprintf(stderr,
860 "SignalSender usleep error; interval = %u, errno = %d\n",
861 interval,
862 errno);
863 ASSERT(result == 0 || errno == EINTR);
864 }
865#endif
866 USE(result);
867 }
868
lrn@chromium.org303ada72010-10-27 09:33:13 +0000869 Sampler* sampler_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870 bool signal_handler_installed_;
871 struct sigaction old_signal_handler_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000872 int vm_tgid_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000873 bool signal_sender_launched_;
874 pthread_t signal_sender_thread_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000875 RuntimeProfilerRateLimiter rate_limiter_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876};
877
878
lrn@chromium.org303ada72010-10-27 09:33:13 +0000879static void* SenderEntry(void* arg) {
880 Sampler::PlatformData* data =
881 reinterpret_cast<Sampler::PlatformData*>(arg);
882 data->SignalSender();
883 return 0;
884}
885
886
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000887Sampler::Sampler(int interval)
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000888 : interval_(interval),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000889 profiling_(false),
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000890 active_(false),
891 samples_taken_(0) {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000892 data_ = new PlatformData(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893}
894
895
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000896Sampler::~Sampler() {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000897 ASSERT(!data_->signal_sender_launched_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898 delete data_;
899}
900
901
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000902void Sampler::Start() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 // There can only be one active sampler at the time on POSIX
904 // platforms.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000905 ASSERT(!IsActive());
906 vm_tid_ = GetThreadID();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907
908 // Request profiling signals.
909 struct sigaction sa;
910 sa.sa_sigaction = ProfilerSignalHandler;
911 sigemptyset(&sa.sa_mask);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000912 sa.sa_flags = SA_RESTART | SA_SIGINFO;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 if (sigaction(SIGPROF, &sa, &data_->old_signal_handler_) != 0) return;
914 data_->signal_handler_installed_ = true;
915
lrn@chromium.org303ada72010-10-27 09:33:13 +0000916 // Start a thread that sends SIGPROF signal to VM thread.
917 // Sending the signal ourselves instead of relying on itimer provides
918 // much better accuracy.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000919 SetActive(true);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000920 if (pthread_create(
921 &data_->signal_sender_thread_, NULL, SenderEntry, data_) == 0) {
922 data_->signal_sender_launched_ = true;
923 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000924
925 // Set this sampler as the active sampler.
926 active_sampler_ = this;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927}
928
929
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000930void Sampler::Stop() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000931 SetActive(false);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000932
933 // Wait for signal sender termination (it will exit after setting
934 // active_ to false).
935 if (data_->signal_sender_launched_) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000936 Top::WakeUpRuntimeProfilerThreadBeforeShutdown();
lrn@chromium.org303ada72010-10-27 09:33:13 +0000937 pthread_join(data_->signal_sender_thread_, NULL);
938 data_->signal_sender_launched_ = false;
939 }
940
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941 // Restore old signal handler
942 if (data_->signal_handler_installed_) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000943 sigaction(SIGPROF, &data_->old_signal_handler_, 0);
944 data_->signal_handler_installed_ = false;
945 }
946
947 // This sampler is no longer the active sampler.
948 active_sampler_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949}
950
ager@chromium.orga1645e22009-09-09 19:27:10 +0000951
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952#endif // ENABLE_LOGGING_AND_PROFILING
953
954} } // namespace v8::internal