blob: a3cdc031ef1a5a820512cca804a754bb18688d43 [file] [log] [blame]
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001// Copyright 2011 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>
lrn@chromium.org5d00b602011-01-05 09:51:43 +000034#include <sys/prctl.h>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035#include <sys/time.h>
36#include <sys/resource.h>
lrn@chromium.org303ada72010-10-27 09:33:13 +000037#include <sys/syscall.h>
ager@chromium.org381abbb2009-02-25 13:23:22 +000038#include <sys/types.h>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039#include <stdlib.h>
40
41// Ubuntu Dapper requires memory pages to be marked as
42// executable. Otherwise, OS raises an exception when executing code
43// in that page.
44#include <sys/types.h> // mmap & munmap
ager@chromium.org236ad962008-09-25 09:45:57 +000045#include <sys/mman.h> // mmap & munmap
46#include <sys/stat.h> // open
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000047#include <fcntl.h> // open
48#include <unistd.h> // sysconf
49#ifdef __GLIBC__
ager@chromium.org236ad962008-09-25 09:45:57 +000050#include <execinfo.h> // backtrace, backtrace_symbols
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000051#endif // def __GLIBC__
ager@chromium.org236ad962008-09-25 09:45:57 +000052#include <strings.h> // index
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053#include <errno.h>
54#include <stdarg.h>
55
56#undef MAP_TYPE
57
58#include "v8.h"
59
60#include "platform.h"
ager@chromium.orga1645e22009-09-09 19:27:10 +000061#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
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000078static Mutex* limit_mutex = NULL;
79
80
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000081void OS::SetUp() {
ricow@chromium.org9fa09672011-07-25 11:05:35 +000082 // Seed the random number generator. We preserve microsecond resolution.
83 uint64_t seed = Ticks() ^ (getpid() << 16);
ager@chromium.org9258b6b2008-09-11 09:11:10 +000084 srandom(static_cast<unsigned int>(seed));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000085 limit_mutex = CreateMutex();
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +000086
87#ifdef __arm__
88 // When running on ARM hardware check that the EABI used by V8 and
89 // by the C code is the same.
90 bool hard_float = OS::ArmUsingHardFloat();
91 if (hard_float) {
92#if !USE_EABI_HARDFLOAT
93 PrintF("ERROR: Binary compiled with -mfloat-abi=hard but without "
94 "-DUSE_EABI_HARDFLOAT\n");
95 exit(1);
96#endif
97 } else {
98#if USE_EABI_HARDFLOAT
99 PrintF("ERROR: Binary not compiled with -mfloat-abi=hard but with "
100 "-DUSE_EABI_HARDFLOAT\n");
101 exit(1);
102#endif
103 }
104#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105}
106
107
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000108uint64_t OS::CpuFeaturesImpliedByPlatform() {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000109 return 0; // Linux runs on anything.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000110}
111
112
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000113#ifdef __arm__
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000114static bool CPUInfoContainsString(const char * search_string) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000115 const char* file_name = "/proc/cpuinfo";
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000116 // This is written as a straight shot one pass parser
117 // and not using STL string and ifstream because,
118 // on Linux, it's reading from a (non-mmap-able)
119 // character special device.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000120 FILE* f = NULL;
121 const char* what = search_string;
122
123 if (NULL == (f = fopen(file_name, "r")))
124 return false;
125
126 int k;
127 while (EOF != (k = fgetc(f))) {
128 if (k == *what) {
129 ++what;
130 while ((*what != '\0') && (*what == fgetc(f))) {
131 ++what;
132 }
133 if (*what == '\0') {
134 fclose(f);
135 return true;
136 } else {
137 what = search_string;
138 }
139 }
140 }
141 fclose(f);
142
143 // Did not find string in the proc file.
144 return false;
145}
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000146
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000147
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000148bool OS::ArmCpuHasFeature(CpuFeature feature) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000149 const char* search_string = NULL;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000150 // Simple detection of VFP at runtime for Linux.
151 // It is based on /proc/cpuinfo, which reveals hardware configuration
152 // to user-space applications. According to ARM (mid 2009), no similar
153 // facility is universally available on the ARM architectures,
154 // so it's up to individual OSes to provide such.
155 switch (feature) {
156 case VFP3:
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000157 search_string = "vfpv3";
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000158 break;
159 case ARMv7:
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000160 search_string = "ARMv7";
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000161 break;
162 default:
163 UNREACHABLE();
164 }
165
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000166 if (CPUInfoContainsString(search_string)) {
167 return true;
168 }
169
170 if (feature == VFP3) {
171 // Some old kernels will report vfp not vfpv3. Here we make a last attempt
172 // to detect vfpv3 by checking for vfp *and* neon, since neon is only
173 // available on architectures with vfpv3.
174 // Checking neon on its own is not enough as it is possible to have neon
175 // without vfp.
176 if (CPUInfoContainsString("vfp") && CPUInfoContainsString("neon")) {
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000177 return true;
178 }
179 }
180
181 return false;
182}
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000183
184
185// Simple helper function to detect whether the C code is compiled with
186// option -mfloat-abi=hard. The register d0 is loaded with 1.0 and the register
187// pair r0, r1 is loaded with 0.0. If -mfloat-abi=hard is pased to GCC then
188// calling this will return 1.0 and otherwise 0.0.
189static void ArmUsingHardFloatHelper() {
190 asm("mov r0, #0");
191#if defined(__VFP_FP__) && !defined(__SOFTFP__)
192 // Load 0x3ff00000 into r1 using instructions available in both ARM
193 // and Thumb mode.
194 asm("mov r1, #3");
195 asm("mov r2, #255");
196 asm("lsl r1, r1, #8");
197 asm("orr r1, r1, r2");
lrn@chromium.org1c092762011-05-09 09:42:16 +0000198 asm("lsl r1, r1, #20");
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000199 // For vmov d0, r0, r1 use ARM mode.
200#ifdef __thumb__
201 asm volatile(
202 "@ Enter ARM Mode \n\t"
203 " adr r3, 1f \n\t"
204 " bx r3 \n\t"
205 " .ALIGN 4 \n\t"
206 " .ARM \n"
207 "1: vmov d0, r0, r1 \n\t"
208 "@ Enter THUMB Mode\n\t"
209 " adr r3, 2f+1 \n\t"
210 " bx r3 \n\t"
211 " .THUMB \n"
212 "2: \n\t");
213#else
214 asm("vmov d0, r0, r1");
215#endif // __thumb__
216#endif // defined(__VFP_FP__) && !defined(__SOFTFP__)
217 asm("mov r1, #0");
218}
219
220
221bool OS::ArmUsingHardFloat() {
222 // Cast helper function from returning void to returning double.
223 typedef double (*F)();
224 F f = FUNCTION_CAST<F>(FUNCTION_ADDR(ArmUsingHardFloatHelper));
225 return f() == 1.0;
226}
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000227#endif // def __arm__
228
229
lrn@chromium.org7516f052011-03-30 08:52:27 +0000230#ifdef __mips__
231bool OS::MipsCpuHasFeature(CpuFeature feature) {
232 const char* search_string = NULL;
233 const char* file_name = "/proc/cpuinfo";
234 // Simple detection of FPU at runtime for Linux.
235 // It is based on /proc/cpuinfo, which reveals hardware configuration
236 // to user-space applications. According to MIPS (early 2010), no similar
237 // facility is universally available on the MIPS architectures,
238 // so it's up to individual OSes to provide such.
239 //
240 // This is written as a straight shot one pass parser
241 // and not using STL string and ifstream because,
242 // on Linux, it's reading from a (non-mmap-able)
243 // character special device.
244
245 switch (feature) {
246 case FPU:
247 search_string = "FPU";
248 break;
249 default:
250 UNREACHABLE();
251 }
252
253 FILE* f = NULL;
254 const char* what = search_string;
255
256 if (NULL == (f = fopen(file_name, "r")))
257 return false;
258
259 int k;
260 while (EOF != (k = fgetc(f))) {
261 if (k == *what) {
262 ++what;
263 while ((*what != '\0') && (*what == fgetc(f))) {
264 ++what;
265 }
266 if (*what == '\0') {
267 fclose(f);
268 return true;
269 } else {
270 what = search_string;
271 }
272 }
273 }
274 fclose(f);
275
276 // Did not find string in the proc file.
277 return false;
278}
279#endif // def __mips__
280
281
ager@chromium.org236ad962008-09-25 09:45:57 +0000282int OS::ActivationFrameAlignment() {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000283#ifdef V8_TARGET_ARCH_ARM
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000284 // On EABI ARM targets this is required for fp correctness in the
285 // runtime system.
ager@chromium.org3a6061e2009-03-12 14:24:36 +0000286 return 8;
ager@chromium.org5c838252010-02-19 08:53:10 +0000287#elif V8_TARGET_ARCH_MIPS
288 return 8;
289#endif
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000290 // With gcc 4.4 the tree vectorization optimizer can generate code
ager@chromium.orge2902be2009-06-08 12:21:35 +0000291 // that requires 16 byte alignment such as movdqa on x86.
292 return 16;
ager@chromium.org236ad962008-09-25 09:45:57 +0000293}
294
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000296void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000297#if (defined(V8_TARGET_ARCH_ARM) && defined(__arm__)) || \
298 (defined(V8_TARGET_ARCH_MIPS) && defined(__mips__))
299 // Only use on ARM or MIPS hardware.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000300 MemoryBarrier();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000301#else
302 __asm__ __volatile__("" : : : "memory");
303 // An x86 store acts as a release barrier.
304#endif
305 *ptr = value;
306}
307
308
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000309const char* OS::LocalTimezone(double time) {
310 if (isnan(time)) return "";
311 time_t tv = static_cast<time_t>(floor(time/msPerSecond));
312 struct tm* t = localtime(&tv);
313 if (NULL == t) return "";
314 return t->tm_zone;
315}
316
317
318double OS::LocalTimeOffset() {
319 time_t tv = time(NULL);
320 struct tm* t = localtime(&tv);
321 // tm_gmtoff includes any daylight savings offset, so subtract it.
322 return static_cast<double>(t->tm_gmtoff * msPerSecond -
323 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
324}
325
326
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327// We keep the lowest and highest addresses mapped as a quick way of
328// determining that pointers are outside the heap (used mostly in assertions
329// and verification). The estimate is conservative, ie, not all addresses in
330// 'allocated' space are actually allocated to our heap. The range is
331// [lowest, highest), inclusive on the low and and exclusive on the high end.
332static void* lowest_ever_allocated = reinterpret_cast<void*>(-1);
333static void* highest_ever_allocated = reinterpret_cast<void*>(0);
334
335
336static void UpdateAllocatedSpaceLimits(void* address, int size) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000337 ASSERT(limit_mutex != NULL);
338 ScopedLock lock(limit_mutex);
339
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000340 lowest_ever_allocated = Min(lowest_ever_allocated, address);
341 highest_ever_allocated =
342 Max(highest_ever_allocated,
343 reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size));
344}
345
346
347bool OS::IsOutsideAllocatedSpace(void* address) {
348 return address < lowest_ever_allocated || address >= highest_ever_allocated;
349}
350
351
352size_t OS::AllocateAlignment() {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000353 return sysconf(_SC_PAGESIZE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354}
355
356
kasper.lund7276f142008-07-30 08:49:36 +0000357void* OS::Allocate(const size_t requested,
358 size_t* allocated,
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000359 bool is_executable) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000360 const size_t msize = RoundUp(requested, AllocateAlignment());
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000361 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000362 void* addr = OS::GetRandomMmapAddr();
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000363 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 if (mbase == MAP_FAILED) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000365 LOG(i::Isolate::Current(),
366 StringEvent("OS::Allocate", "mmap failed"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 return NULL;
368 }
369 *allocated = msize;
370 UpdateAllocatedSpaceLimits(mbase, msize);
371 return mbase;
372}
373
374
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000375void OS::Free(void* address, const size_t size) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376 // TODO(1240712): munmap has a return value which is ignored here.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000377 int result = munmap(address, size);
378 USE(result);
379 ASSERT(result == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380}
381
382
383void OS::Sleep(int milliseconds) {
384 unsigned int ms = static_cast<unsigned int>(milliseconds);
385 usleep(1000 * ms);
386}
387
388
389void OS::Abort() {
390 // Redirect to std abort to signal abnormal program termination.
391 abort();
392}
393
394
kasper.lund7276f142008-07-30 08:49:36 +0000395void OS::DebugBreak() {
ager@chromium.org5ec48922009-05-05 07:25:34 +0000396// TODO(lrn): Introduce processor define for runtime system (!= V8_ARCH_x,
397// which is the architecture of generated code).
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000398#if (defined(__arm__) || defined(__thumb__))
399# if defined(CAN_USE_ARMV5_INSTRUCTIONS)
kasper.lund7276f142008-07-30 08:49:36 +0000400 asm("bkpt 0");
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000401# endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000402#elif defined(__mips__)
403 asm("break");
kasper.lund7276f142008-07-30 08:49:36 +0000404#else
405 asm("int $3");
406#endif
407}
408
409
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000410class PosixMemoryMappedFile : public OS::MemoryMappedFile {
411 public:
412 PosixMemoryMappedFile(FILE* file, void* memory, int size)
413 : file_(file), memory_(memory), size_(size) { }
414 virtual ~PosixMemoryMappedFile();
415 virtual void* memory() { return memory_; }
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000416 virtual int size() { return size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000417 private:
418 FILE* file_;
419 void* memory_;
420 int size_;
421};
422
423
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000424OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000425 FILE* file = fopen(name, "r+");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000426 if (file == NULL) return NULL;
427
428 fseek(file, 0, SEEK_END);
429 int size = ftell(file);
430
431 void* memory =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000432 mmap(OS::GetRandomMmapAddr(),
433 size,
434 PROT_READ | PROT_WRITE,
435 MAP_SHARED,
436 fileno(file),
437 0);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000438 return new PosixMemoryMappedFile(file, memory, size);
439}
440
441
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
443 void* initial) {
444 FILE* file = fopen(name, "w+");
445 if (file == NULL) return NULL;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000446 int result = fwrite(initial, size, 1, file);
447 if (result < 1) {
448 fclose(file);
449 return NULL;
450 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000451 void* memory =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000452 mmap(OS::GetRandomMmapAddr(),
453 size,
454 PROT_READ | PROT_WRITE,
455 MAP_SHARED,
456 fileno(file),
457 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458 return new PosixMemoryMappedFile(file, memory, size);
459}
460
461
462PosixMemoryMappedFile::~PosixMemoryMappedFile() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000463 if (memory_) OS::Free(memory_, size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464 fclose(file_);
465}
466
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468void OS::LogSharedLibraryAddresses() {
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000469 // This function assumes that the layout of the file is as follows:
470 // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
471 // If we encounter an unexpected situation we abort scanning further entries.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000472 FILE* fp = fopen("/proc/self/maps", "r");
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000473 if (fp == NULL) return;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000474
475 // Allocate enough room to be able to store a full file name.
476 const int kLibNameLen = FILENAME_MAX + 1;
477 char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
478
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000479 i::Isolate* isolate = ISOLATE;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000480 // This loop will terminate once the scanning hits an EOF.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481 while (true) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000482 uintptr_t start, end;
483 char attr_r, attr_w, attr_x, attr_p;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000484 // Parse the addresses and permission bits at the beginning of the line.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000485 if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
486 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 +0000487
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000488 int c;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000489 if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
490 // Found a read-only executable entry. Skip characters until we reach
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000491 // the beginning of the filename or the end of the line.
492 do {
493 c = getc(fp);
494 } while ((c != EOF) && (c != '\n') && (c != '/'));
495 if (c == EOF) break; // EOF: Was unexpected, just exit.
496
497 // Process the filename if found.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000498 if (c == '/') {
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000499 ungetc(c, fp); // Push the '/' back into the stream to be read below.
500
501 // Read to the end of the line. Exit if the read fails.
502 if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
503
504 // Drop the newline character read by fgets. We do not need to check
505 // for a zero-length string because we know that we at least read the
506 // '/' character.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000507 lib_name[strlen(lib_name) - 1] = '\0';
508 } else {
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000509 // No library name found, just record the raw address range.
510 snprintf(lib_name, kLibNameLen,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000511 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
512 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000513 LOG(isolate, SharedLibraryEvent(lib_name, start, end));
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000514 } else {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000515 // Entry not describing executable data. Skip to end of line to set up
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000516 // reading the next entry.
517 do {
518 c = getc(fp);
519 } while ((c != EOF) && (c != '\n'));
520 if (c == EOF) break;
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000521 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522 }
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000523 free(lib_name);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000524 fclose(fp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525}
526
527
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000528static const char kGCFakeMmap[] = "/tmp/__v8_gc__";
529
530
531void OS::SignalCodeMovingGC() {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000532 // Support for ll_prof.py.
533 //
534 // The Linux profiler built into the kernel logs all mmap's with
535 // PROT_EXEC so that analysis tools can properly attribute ticks. We
536 // do a mmap with a name known by ll_prof.py and immediately munmap
537 // it. This injects a GC marker into the stream of events generated
538 // by the kernel and allows us to synchronize V8 code log and the
539 // kernel log.
540 int size = sysconf(_SC_PAGESIZE);
541 FILE* f = fopen(kGCFakeMmap, "w+");
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000542 void* addr = mmap(OS::GetRandomMmapAddr(),
543 size,
544 PROT_READ | PROT_EXEC,
545 MAP_PRIVATE,
546 fileno(f),
547 0);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000548 ASSERT(addr != MAP_FAILED);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000549 OS::Free(addr, size);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000550 fclose(f);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000551}
552
553
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000554int OS::StackWalk(Vector<OS::StackFrame> frames) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000555 // backtrace is a glibc extension.
556#ifdef __GLIBC__
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000557 int frames_size = frames.length();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000558 ScopedVector<void*> addresses(frames_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000560 int frames_count = backtrace(addresses.start(), frames_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000561
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000562 char** symbols = backtrace_symbols(addresses.start(), frames_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563 if (symbols == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 return kStackWalkError;
565 }
566
567 for (int i = 0; i < frames_count; i++) {
568 frames[i].address = addresses[i];
569 // Format a text representation of the frame based on the information
570 // available.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000571 SNPrintF(MutableCStrVector(frames[i].text, kStackWalkMaxTextLen),
572 "%s",
573 symbols[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574 // Make sure line termination is in place.
575 frames[i].text[kStackWalkMaxTextLen - 1] = '\0';
576 }
577
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 free(symbols);
579
580 return frames_count;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000581#else // ndef __GLIBC__
582 return 0;
583#endif // ndef __GLIBC__
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584}
585
586
587// Constants used for mmap.
588static const int kMmapFd = -1;
589static const int kMmapFdOffset = 0;
590
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000591VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000593VirtualMemory::VirtualMemory(size_t size) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000594 address_ = ReserveRegion(size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595 size_ = size;
596}
597
598
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000599VirtualMemory::VirtualMemory(size_t size, size_t alignment)
600 : address_(NULL), size_(0) {
601 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
602 size_t request_size = RoundUp(size + alignment,
603 static_cast<intptr_t>(OS::AllocateAlignment()));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000604 void* reservation = mmap(OS::GetRandomMmapAddr(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000605 request_size,
606 PROT_NONE,
607 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
608 kMmapFd,
609 kMmapFdOffset);
610 if (reservation == MAP_FAILED) return;
611
612 Address base = static_cast<Address>(reservation);
613 Address aligned_base = RoundUp(base, alignment);
614 ASSERT_LE(base, aligned_base);
615
616 // Unmap extra memory reserved before and after the desired block.
617 if (aligned_base != base) {
618 size_t prefix_size = static_cast<size_t>(aligned_base - base);
619 OS::Free(base, prefix_size);
620 request_size -= prefix_size;
621 }
622
623 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
624 ASSERT_LE(aligned_size, request_size);
625
626 if (aligned_size != request_size) {
627 size_t suffix_size = request_size - aligned_size;
628 OS::Free(aligned_base + aligned_size, suffix_size);
629 request_size -= suffix_size;
630 }
631
632 ASSERT(aligned_size == request_size);
633
634 address_ = static_cast<void*>(aligned_base);
635 size_ = aligned_size;
636}
637
638
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639VirtualMemory::~VirtualMemory() {
640 if (IsReserved()) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000641 bool result = ReleaseRegion(address(), size());
642 ASSERT(result);
643 USE(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644 }
645}
646
647
648bool VirtualMemory::IsReserved() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000649 return address_ != NULL;
650}
651
652
653void VirtualMemory::Reset() {
654 address_ = NULL;
655 size_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656}
657
658
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000659bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000660 return CommitRegion(address, size, is_executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000661}
662
663
664bool VirtualMemory::Uncommit(void* address, size_t size) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000665 return UncommitRegion(address, size);
666}
667
668
669void* VirtualMemory::ReserveRegion(size_t size) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000670 void* result = mmap(OS::GetRandomMmapAddr(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000671 size,
672 PROT_NONE,
673 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
674 kMmapFd,
675 kMmapFdOffset);
676
677 if (result == MAP_FAILED) return NULL;
678
679 return result;
680}
681
682
683bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
684 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
685 if (MAP_FAILED == mmap(base,
686 size,
687 prot,
688 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
689 kMmapFd,
690 kMmapFdOffset)) {
691 return false;
692 }
693
694 UpdateAllocatedSpaceLimits(base, size);
695 return true;
696}
697
698
699bool VirtualMemory::UncommitRegion(void* base, size_t size) {
700 return mmap(base,
701 size,
702 PROT_NONE,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000703 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000704 kMmapFd,
705 kMmapFdOffset) != MAP_FAILED;
706}
707
708
709bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
710 return munmap(base, size) == 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711}
712
713
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000714class Thread::PlatformData : public Malloced {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715 public:
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000716 PlatformData() : thread_(kNoThread) {}
ager@chromium.org41826e72009-03-30 13:30:57 +0000717
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000718 pthread_t thread_; // Thread handle for pthread.
719};
720
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000721Thread::Thread(const Options& options)
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000722 : data_(new PlatformData()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000723 stack_size_(options.stack_size) {
724 set_name(options.name);
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000725}
726
727
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000728Thread::Thread(const char* name)
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000729 : data_(new PlatformData()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000730 stack_size_(0) {
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000731 set_name(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732}
733
734
735Thread::~Thread() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000736 delete data_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737}
738
739
740static void* ThreadEntry(void* arg) {
741 Thread* thread = reinterpret_cast<Thread*>(arg);
742 // This is also initialized by the first argument to pthread_create() but we
743 // don't know which thread will run first (the original thread or the new
744 // one) so we initialize it here too.
danno@chromium.orgb6451162011-08-17 14:33:23 +0000745#ifdef PR_SET_NAME
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000746 prctl(PR_SET_NAME,
747 reinterpret_cast<unsigned long>(thread->name()), // NOLINT
748 0, 0, 0);
danno@chromium.orgb6451162011-08-17 14:33:23 +0000749#endif
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000750 thread->data()->thread_ = pthread_self();
751 ASSERT(thread->data()->thread_ != kNoThread);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752 thread->Run();
753 return NULL;
754}
755
756
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000757void Thread::set_name(const char* name) {
758 strncpy(name_, name, sizeof(name_));
759 name_[sizeof(name_) - 1] = '\0';
760}
761
762
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763void Thread::Start() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000764 pthread_attr_t* attr_ptr = NULL;
765 pthread_attr_t attr;
766 if (stack_size_ > 0) {
767 pthread_attr_init(&attr);
768 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
769 attr_ptr = &attr;
770 }
danno@chromium.orgc612e022011-11-10 11:38:15 +0000771 int result = pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this);
772 CHECK_EQ(0, result);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000773 ASSERT(data_->thread_ != kNoThread);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000774}
775
776
777void Thread::Join() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000778 pthread_join(data_->thread_, NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779}
780
781
782Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
783 pthread_key_t key;
784 int result = pthread_key_create(&key, NULL);
785 USE(result);
786 ASSERT(result == 0);
787 return static_cast<LocalStorageKey>(key);
788}
789
790
791void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
792 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
793 int result = pthread_key_delete(pthread_key);
794 USE(result);
795 ASSERT(result == 0);
796}
797
798
799void* Thread::GetThreadLocal(LocalStorageKey key) {
800 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
801 return pthread_getspecific(pthread_key);
802}
803
804
805void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
806 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
807 pthread_setspecific(pthread_key, value);
808}
809
810
811void Thread::YieldCPU() {
812 sched_yield();
813}
814
815
816class LinuxMutex : public Mutex {
817 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000818 LinuxMutex() {
819 pthread_mutexattr_t attrs;
820 int result = pthread_mutexattr_init(&attrs);
821 ASSERT(result == 0);
822 result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE);
823 ASSERT(result == 0);
824 result = pthread_mutex_init(&mutex_, &attrs);
825 ASSERT(result == 0);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000826 USE(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000827 }
828
829 virtual ~LinuxMutex() { pthread_mutex_destroy(&mutex_); }
830
831 virtual int Lock() {
832 int result = pthread_mutex_lock(&mutex_);
833 return result;
834 }
835
836 virtual int Unlock() {
837 int result = pthread_mutex_unlock(&mutex_);
838 return result;
839 }
840
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000841 virtual bool TryLock() {
842 int result = pthread_mutex_trylock(&mutex_);
843 // Return false if the lock is busy and locking failed.
844 if (result == EBUSY) {
845 return false;
846 }
847 ASSERT(result == 0); // Verify no other errors.
848 return true;
849 }
850
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851 private:
852 pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.
853};
854
855
856Mutex* OS::CreateMutex() {
857 return new LinuxMutex();
858}
859
860
861class LinuxSemaphore : public Semaphore {
862 public:
863 explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); }
864 virtual ~LinuxSemaphore() { sem_destroy(&sem_); }
865
kasper.lund7276f142008-07-30 08:49:36 +0000866 virtual void Wait();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000867 virtual bool Wait(int timeout);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868 virtual void Signal() { sem_post(&sem_); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869 private:
870 sem_t sem_;
871};
872
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000873
kasper.lund7276f142008-07-30 08:49:36 +0000874void LinuxSemaphore::Wait() {
875 while (true) {
876 int result = sem_wait(&sem_);
877 if (result == 0) return; // Successfully got semaphore.
878 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
879 }
880}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000882
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000883#ifndef TIMEVAL_TO_TIMESPEC
884#define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
885 (ts)->tv_sec = (tv)->tv_sec; \
886 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
887} while (false)
888#endif
889
890
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000891bool LinuxSemaphore::Wait(int timeout) {
892 const long kOneSecondMicros = 1000000; // NOLINT
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000893
894 // Split timeout into second and nanosecond parts.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000895 struct timeval delta;
896 delta.tv_usec = timeout % kOneSecondMicros;
897 delta.tv_sec = timeout / kOneSecondMicros;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000898
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000899 struct timeval current_time;
900 // Get the current time.
901 if (gettimeofday(&current_time, NULL) == -1) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000902 return false;
903 }
904
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000905 // Calculate time for end of timeout.
906 struct timeval end_time;
907 timeradd(&current_time, &delta, &end_time);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000908
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000909 struct timespec ts;
910 TIMEVAL_TO_TIMESPEC(&end_time, &ts);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000911 // Wait for semaphore signalled or timeout.
912 while (true) {
913 int result = sem_timedwait(&sem_, &ts);
914 if (result == 0) return true; // Successfully got semaphore.
915 if (result > 0) {
916 // For glibc prior to 2.3.4 sem_timedwait returns the error instead of -1.
917 errno = result;
918 result = -1;
919 }
920 if (result == -1 && errno == ETIMEDOUT) return false; // Timeout.
921 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
922 }
923}
924
925
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926Semaphore* OS::CreateSemaphore(int count) {
927 return new LinuxSemaphore(count);
928}
929
ager@chromium.org381abbb2009-02-25 13:23:22 +0000930
kasperl@chromium.orgacae3782009-04-11 09:17:08 +0000931#if !defined(__GLIBC__) && (defined(__arm__) || defined(__thumb__))
932// Android runs a fairly new Linux kernel, so signal info is there,
933// but the C library doesn't have the structs defined.
934
935struct sigcontext {
936 uint32_t trap_no;
937 uint32_t error_code;
938 uint32_t oldmask;
939 uint32_t gregs[16];
940 uint32_t arm_cpsr;
941 uint32_t fault_address;
942};
943typedef uint32_t __sigset_t;
944typedef struct sigcontext mcontext_t;
945typedef struct ucontext {
946 uint32_t uc_flags;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000947 struct ucontext* uc_link;
kasperl@chromium.orgacae3782009-04-11 09:17:08 +0000948 stack_t uc_stack;
949 mcontext_t uc_mcontext;
950 __sigset_t uc_sigmask;
951} ucontext_t;
952enum ArmRegisters {R15 = 15, R13 = 13, R11 = 11};
953
954#endif
955
956
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000957static int GetThreadID() {
958 // Glibc doesn't provide a wrapper for gettid(2).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000959#if defined(ANDROID)
960 return syscall(__NR_gettid);
961#else
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000962 return syscall(SYS_gettid);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000963#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000964}
965
966
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000967static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) {
968 USE(info);
969 if (signal != SIGPROF) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000970 Isolate* isolate = Isolate::UncheckedCurrent();
971 if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) {
972 // We require a fully initialized and entered isolate.
973 return;
974 }
vitalyr@chromium.org0ec56d62011-04-15 22:22:08 +0000975 if (v8::Locker::IsActive() &&
976 !isolate->thread_manager()->IsLockedByCurrentThread()) {
977 return;
978 }
979
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000980 Sampler* sampler = isolate->logger()->sampler();
981 if (sampler == NULL || !sampler->IsActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982
lrn@chromium.org25156de2010-04-06 13:10:27 +0000983 TickSample sample_obj;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000984 TickSample* sample = CpuProfiler::TickSampleEvent(isolate);
ager@chromium.org357bf652010-04-12 11:30:10 +0000985 if (sample == NULL) sample = &sample_obj;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000986
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000987 // Extracting the sample from the context is extremely machine dependent.
988 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context);
989 mcontext_t& mcontext = ucontext->uc_mcontext;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000990 sample->state = isolate->current_vm_state();
ager@chromium.org9085a012009-05-11 19:22:57 +0000991#if V8_HOST_ARCH_IA32
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000992 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]);
993 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]);
994 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]);
ager@chromium.org9085a012009-05-11 19:22:57 +0000995#elif V8_HOST_ARCH_X64
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000996 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]);
997 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]);
998 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]);
ager@chromium.org9085a012009-05-11 19:22:57 +0000999#elif V8_HOST_ARCH_ARM
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00001000// An undefined macro evaluates to 0, so this applies to Android's Bionic also.
1001#if (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001002 sample->pc = reinterpret_cast<Address>(mcontext.gregs[R15]);
1003 sample->sp = reinterpret_cast<Address>(mcontext.gregs[R13]);
1004 sample->fp = reinterpret_cast<Address>(mcontext.gregs[R11]);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00001005#else
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001006 sample->pc = reinterpret_cast<Address>(mcontext.arm_pc);
1007 sample->sp = reinterpret_cast<Address>(mcontext.arm_sp);
1008 sample->fp = reinterpret_cast<Address>(mcontext.arm_fp);
danno@chromium.orgc612e022011-11-10 11:38:15 +00001009#endif // (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
ager@chromium.org5c838252010-02-19 08:53:10 +00001010#elif V8_HOST_ARCH_MIPS
danno@chromium.orgc612e022011-11-10 11:38:15 +00001011 sample->pc = reinterpret_cast<Address>(mcontext.pc);
1012 sample->sp = reinterpret_cast<Address>(mcontext.gregs[29]);
1013 sample->fp = reinterpret_cast<Address>(mcontext.gregs[30]);
1014#endif // V8_HOST_ARCH_*
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001015 sampler->SampleStack(sample);
1016 sampler->Tick(sample);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017}
1018
1019
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001020class Sampler::PlatformData : public Malloced {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001021 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001022 PlatformData() : vm_tid_(GetThreadID()) {}
1023
1024 int vm_tid() const { return vm_tid_; }
1025
1026 private:
1027 const int vm_tid_;
1028};
1029
1030
1031class SignalSender : public Thread {
1032 public:
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001033 enum SleepInterval {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001034 HALF_INTERVAL,
1035 FULL_INTERVAL
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001036 };
1037
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001038 explicit SignalSender(int interval)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001039 : Thread("SignalSender"),
lrn@chromium.org303ada72010-10-27 09:33:13 +00001040 vm_tgid_(getpid()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001041 interval_(interval) {}
1042
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001043 static void InstallSignalHandler() {
1044 struct sigaction sa;
1045 sa.sa_sigaction = ProfilerSignalHandler;
1046 sigemptyset(&sa.sa_mask);
1047 sa.sa_flags = SA_RESTART | SA_SIGINFO;
1048 signal_handler_installed_ =
1049 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0);
1050 }
1051
1052 static void RestoreSignalHandler() {
1053 if (signal_handler_installed_) {
1054 sigaction(SIGPROF, &old_signal_handler_, 0);
1055 signal_handler_installed_ = false;
1056 }
1057 }
1058
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001059 static void AddActiveSampler(Sampler* sampler) {
1060 ScopedLock lock(mutex_);
1061 SamplerRegistry::AddActiveSampler(sampler);
1062 if (instance_ == NULL) {
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001063 // Start a thread that will send SIGPROF signal to VM threads,
1064 // when CPU profiling will be enabled.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001065 instance_ = new SignalSender(sampler->interval());
1066 instance_->Start();
1067 } else {
1068 ASSERT(instance_->interval_ == sampler->interval());
1069 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070 }
1071
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001072 static void RemoveActiveSampler(Sampler* sampler) {
1073 ScopedLock lock(mutex_);
1074 SamplerRegistry::RemoveActiveSampler(sampler);
1075 if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) {
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001076 RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001077 delete instance_;
1078 instance_ = NULL;
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001079 RestoreSignalHandler();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001080 }
1081 }
1082
1083 // Implement Thread::Run().
1084 virtual void Run() {
1085 SamplerRegistry::State state;
1086 while ((state = SamplerRegistry::GetState()) !=
1087 SamplerRegistry::HAS_NO_SAMPLERS) {
1088 bool cpu_profiling_enabled =
1089 (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS);
1090 bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled();
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001091 if (cpu_profiling_enabled && !signal_handler_installed_) {
1092 InstallSignalHandler();
1093 } else if (!cpu_profiling_enabled && signal_handler_installed_) {
1094 RestoreSignalHandler();
1095 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001096 // When CPU profiling is enabled both JavaScript and C++ code is
1097 // profiled. We must not suspend.
1098 if (!cpu_profiling_enabled) {
1099 if (rate_limiter_.SuspendIfNecessary()) continue;
1100 }
1101 if (cpu_profiling_enabled && runtime_profiler_enabled) {
1102 if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) {
1103 return;
1104 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001105 Sleep(HALF_INTERVAL);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001106 if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, NULL)) {
1107 return;
1108 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001109 Sleep(HALF_INTERVAL);
1110 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001111 if (cpu_profiling_enabled) {
1112 if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile,
1113 this)) {
1114 return;
1115 }
1116 }
1117 if (runtime_profiler_enabled) {
1118 if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile,
1119 NULL)) {
1120 return;
1121 }
1122 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001123 Sleep(FULL_INTERVAL);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001124 }
lrn@chromium.org303ada72010-10-27 09:33:13 +00001125 }
1126 }
1127
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001128 static void DoCpuProfile(Sampler* sampler, void* raw_sender) {
1129 if (!sampler->IsProfiling()) return;
1130 SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender);
1131 sender->SendProfilingSignal(sampler->platform_data()->vm_tid());
1132 }
1133
1134 static void DoRuntimeProfile(Sampler* sampler, void* ignored) {
1135 if (!sampler->isolate()->IsInitialized()) return;
1136 sampler->isolate()->runtime_profiler()->NotifyTick();
1137 }
1138
1139 void SendProfilingSignal(int tid) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001140 if (!signal_handler_installed_) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001141 // Glibc doesn't provide a wrapper for tgkill(2).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001142#if defined(ANDROID)
1143 syscall(__NR_tgkill, vm_tgid_, tid, SIGPROF);
1144#else
1145 syscall(SYS_tgkill, vm_tgid_, tid, SIGPROF);
1146#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001147 }
1148
1149 void Sleep(SleepInterval full_or_half) {
1150 // Convert ms to us and subtract 100 us to compensate delays
1151 // occuring during signal delivery.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001152 useconds_t interval = interval_ * 1000 - 100;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001153 if (full_or_half == HALF_INTERVAL) interval /= 2;
1154 int result = usleep(interval);
1155#ifdef DEBUG
1156 if (result != 0 && errno != EINTR) {
1157 fprintf(stderr,
1158 "SignalSender usleep error; interval = %u, errno = %d\n",
1159 interval,
1160 errno);
1161 ASSERT(result == 0 || errno == EINTR);
1162 }
1163#endif
1164 USE(result);
1165 }
1166
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001167 const int vm_tgid_;
1168 const int interval_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001169 RuntimeProfilerRateLimiter rate_limiter_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001170
1171 // Protects the process wide state below.
1172 static Mutex* mutex_;
1173 static SignalSender* instance_;
1174 static bool signal_handler_installed_;
1175 static struct sigaction old_signal_handler_;
1176
1177 DISALLOW_COPY_AND_ASSIGN(SignalSender);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001178};
1179
1180
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001181Mutex* SignalSender::mutex_ = OS::CreateMutex();
1182SignalSender* SignalSender::instance_ = NULL;
1183struct sigaction SignalSender::old_signal_handler_;
1184bool SignalSender::signal_handler_installed_ = false;
lrn@chromium.org303ada72010-10-27 09:33:13 +00001185
1186
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001187Sampler::Sampler(Isolate* isolate, int interval)
1188 : isolate_(isolate),
1189 interval_(interval),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001190 profiling_(false),
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001191 active_(false),
1192 samples_taken_(0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001193 data_ = new PlatformData;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001194}
1195
1196
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001197Sampler::~Sampler() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001198 ASSERT(!IsActive());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 delete data_;
1200}
1201
1202
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001203void Sampler::Start() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001204 ASSERT(!IsActive());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001205 SetActive(true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001206 SignalSender::AddActiveSampler(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001207}
1208
1209
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001210void Sampler::Stop() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001211 ASSERT(IsActive());
1212 SignalSender::RemoveActiveSampler(this);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001213 SetActive(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001214}
1215
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001216
1217} } // namespace v8::internal