platform-posix.cc 17 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6 7
// Platform-specific code for POSIX goes here. This is not a platform on its
// own, but contains the parts which are the same across the POSIX platforms
// Linux, MacOS, FreeBSD, OpenBSD, NetBSD and QNX.
8

9
#include <errno.h>
10
#include <limits.h>
11
#include <pthread.h>
12
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
13 14
#include <pthread_np.h>  // for pthread_set_name_np
#endif
15
#include <sched.h>  // for sched_yield
16
#include <time.h>
17
#include <unistd.h>
18

19
#include <sys/mman.h>
20
#include <sys/resource.h>
21
#include <sys/stat.h>
22
#include <sys/time.h>
23
#include <sys/types.h>
24 25
#if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || \
    defined(__NetBSD__) || defined(__OpenBSD__)
26
#include <sys/sysctl.h>  // NOLINT, for sysctl
27
#endif
28

29 30
#undef MAP_TYPE

31
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
32
#define LOG_TAG "v8"
33
#include <android/log.h>  // NOLINT
34 35
#endif

36 37
#include <cmath>
#include <cstdlib>
38

39
#include "src/base/lazy-instance.h"
40
#include "src/base/macros.h"
41 42 43
#include "src/base/platform/platform.h"
#include "src/base/platform/time.h"
#include "src/base/utils/random-number-generator.h"
44

45 46 47 48
#ifdef V8_FAST_TLS_SUPPORTED
#include "src/base/atomicops.h"
#endif

49 50 51 52
#if V8_OS_MACOSX
#include <dlfcn.h>
#endif

53 54 55 56 57 58 59 60
#if V8_OS_LINUX
#include <sys/prctl.h>  // NOLINT, for prctl
#endif

#if !V8_OS_NACL
#include <sys/syscall.h>
#endif

61
namespace v8 {
62
namespace base {
63

64 65
namespace {

66
// 0 is never a valid thread id.
67 68 69 70 71 72 73
const pthread_t kNoThread = (pthread_t) 0;

bool g_hard_abort = false;

const char* g_gc_fake_mmap = NULL;

}  // namespace
74

75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
int OS::ActivationFrameAlignment() {
#if V8_TARGET_ARCH_ARM
  // On EABI ARM targets this is required for fp correctness in the
  // runtime system.
  return 8;
#elif V8_TARGET_ARCH_MIPS
  return 8;
#else
  // Otherwise we just assume 16 byte alignment, i.e.:
  // - With gcc 4.4 the tree vectorization optimizer can generate code
  //   that requires 16 byte alignment such as movdqa on x86.
  // - Mac OS X and Solaris (64-bit) activation frames must be 16 byte-aligned;
  //   see "Mac OS X ABI Function Call Guide"
  return 16;
#endif
}


94 95 96 97 98 99 100 101 102 103
intptr_t OS::CommitPageSize() {
  static intptr_t page_size = getpagesize();
  return page_size;
}


void OS::Free(void* address, const size_t size) {
  // TODO(1240712): munmap has a return value which is ignored here.
  int result = munmap(address, size);
  USE(result);
104
  DCHECK(result == 0);
105 106 107 108 109
}


// Get rid of writable permission on code allocations.
void OS::ProtectCode(void* address, const size_t size) {
110
#if V8_OS_CYGWIN
111 112
  DWORD old_protect;
  VirtualProtect(address, size, PAGE_EXECUTE_READ, &old_protect);
113
#elif V8_OS_NACL
114 115 116 117 118 119 120 121 122 123 124
  // The Native Client port of V8 uses an interpreter, so
  // code pages don't need PROT_EXEC.
  mprotect(address, size, PROT_READ);
#else
  mprotect(address, size, PROT_READ | PROT_EXEC);
#endif
}


// Create guard pages.
void OS::Guard(void* address, const size_t size) {
125
#if V8_OS_CYGWIN
126 127 128 129 130 131 132 133
  DWORD oldprotect;
  VirtualProtect(address, size, PAGE_NOACCESS, &oldprotect);
#else
  mprotect(address, size, PROT_NONE);
#endif
}


134
static LazyInstance<RandomNumberGenerator>::type
135 136 137
    platform_random_number_generator = LAZY_INSTANCE_INITIALIZER;


138 139 140 141 142 143 144 145 146 147 148 149
void OS::Initialize(int64_t random_seed, bool hard_abort,
                    const char* const gc_fake_mmap) {
  if (random_seed) {
    platform_random_number_generator.Pointer()->SetSeed(random_seed);
  }
  g_hard_abort = hard_abort;
  g_gc_fake_mmap = gc_fake_mmap;
}


const char* OS::GetGCFakeMMapFile() {
  return g_gc_fake_mmap;
150 151 152
}


153
void* OS::GetRandomMmapAddr() {
154
#if V8_OS_NACL
155 156 157 158
  // TODO(bradchen): restore randomization once Native Client gets
  // smarter about using mmap address hints.
  // See http://code.google.com/p/nativeclient/issues/3341
  return NULL;
159 160 161 162 163
#endif
#if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
    defined(THREAD_SANITIZER)
  // Dynamic tools do not support custom mmap addresses.
  return NULL;
164
#endif
165 166 167
  uintptr_t raw_addr;
  platform_random_number_generator.Pointer()->NextBytes(&raw_addr,
                                                        sizeof(raw_addr));
168
#if V8_TARGET_ARCH_X64
169 170 171 172
  // Currently available CPUs have 48 bits of virtual addressing.  Truncate
  // the hint address to 46 bits to give the kernel a fighting chance of
  // fulfilling our placement request.
  raw_addr &= V8_UINT64_C(0x3ffffffff000);
173
#else
174
  raw_addr &= 0x3ffff000;
175 176

# ifdef __sun
177 178 179 180 181 182 183 184 185 186
  // For our Solaris/illumos mmap hint, we pick a random address in the bottom
  // half of the top half of the address space (that is, the third quarter).
  // Because we do not MAP_FIXED, this will be treated only as a hint -- the
  // system will not fail to mmap() because something else happens to already
  // be mapped at our random address. We deliberately set the hint high enough
  // to get well above the system's break (that is, the heap); Solaris and
  // illumos will try the hint and if that fails allocate as if there were
  // no hint at all. The high hint prevents the break from getting hemmed in
  // at low values, ceding half of the address space to the system heap.
  raw_addr += 0x80000000;
187
# else
188 189 190 191
  // The range 0x20000000 - 0x60000000 is relatively unpopulated across a
  // variety of ASLR modes (PAE kernel, NX compat mode, etc) and on macos
  // 10.6 and 10.7.
  raw_addr += 0x20000000;
192 193
# endif
#endif
194
  return reinterpret_cast<void*>(raw_addr);
195 196 197 198
}


size_t OS::AllocateAlignment() {
199
  return static_cast<size_t>(sysconf(_SC_PAGESIZE));
200 201 202
}


203 204 205 206 207 208 209
void OS::Sleep(int milliseconds) {
  useconds_t ms = static_cast<useconds_t>(milliseconds);
  usleep(1000 * ms);
}


void OS::Abort() {
210
  if (g_hard_abort) {
211
    V8_IMMEDIATE_CRASH();
212
  }
213
  // Redirect to std abort to signal abnormal program termination.
214
  abort();
215 216 217 218 219 220
}


void OS::DebugBreak() {
#if V8_HOST_ARCH_ARM
  asm("bkpt 0");
221
#elif V8_HOST_ARCH_ARM64
222
  asm("brk 0");
223 224
#elif V8_HOST_ARCH_MIPS
  asm("break");
225 226
#elif V8_HOST_ARCH_MIPS64
  asm("break");
227
#elif V8_HOST_ARCH_IA32
228
#if V8_OS_NACL
229 230 231
  asm("hlt");
#else
  asm("int $3");
232
#endif  // V8_OS_NACL
233 234 235 236 237 238 239 240
#elif V8_HOST_ARCH_X64
  asm("int $3");
#else
#error Unsupported host architecture.
#endif
}


241 242 243
// ----------------------------------------------------------------------------
// Math functions

244 245 246 247 248 249
double OS::nan_value() {
  // NAN from math.h is defined in C99 and not in POSIX.
  return NAN;
}


250 251 252 253 254
int OS::GetCurrentProcessId() {
  return static_cast<int>(getpid());
}


255
int OS::GetCurrentThreadId() {
256
#if V8_OS_MACOSX
257
  return static_cast<int>(pthread_mach_thread_np(pthread_self()));
258
#elif V8_OS_LINUX
259
  return static_cast<int>(syscall(__NR_gettid));
260 261
#elif V8_OS_ANDROID
  return static_cast<int>(gettid());
262
#else
263
  return reinterpret_cast<int>(pthread_self());
264
#endif
265 266 267
}


268 269 270 271
// ----------------------------------------------------------------------------
// POSIX date/time support.
//

272
int OS::GetUserTime(uint32_t* secs,  uint32_t* usecs) {
273 274 275 276
#if V8_OS_NACL
  // Optionally used in Logger::ResourceEvent.
  return -1;
#else
277 278 279 280 281 282
  struct rusage usage;

  if (getrusage(RUSAGE_SELF, &usage) < 0) return -1;
  *secs = usage.ru_utime.tv_sec;
  *usecs = usage.ru_utime.tv_usec;
  return 0;
283
#endif
284 285 286 287
}


double OS::TimeCurrentMillis() {
288
  return Time::Now().ToJsTime();
289 290 291
}


292 293 294 295 296 297 298 299 300
class TimezoneCache {};


TimezoneCache* OS::CreateTimezoneCache() {
  return NULL;
}


void OS::DisposeTimezoneCache(TimezoneCache* cache) {
301
  DCHECK(cache == NULL);
302 303 304 305
}


void OS::ClearTimezoneCache(TimezoneCache* cache) {
306
  DCHECK(cache == NULL);
307 308 309 310
}


double OS::DaylightSavingsOffset(double time, TimezoneCache*) {
311
  if (std::isnan(time)) return nan_value();
312
  time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
313
  struct tm* t = localtime(&tv);
314
  if (NULL == t) return nan_value();
315 316 317 318
  return t->tm_isdst > 0 ? 3600 * msPerSecond : 0;
}


319 320 321 322 323
int OS::GetLastError() {
  return errno;
}


324 325 326 327 328
// ----------------------------------------------------------------------------
// POSIX stdio support.
//

FILE* OS::FOpen(const char* path, const char* mode) {
329 330 331 332 333 334 335 336
  FILE* file = fopen(path, mode);
  if (file == NULL) return NULL;
  struct stat file_stat;
  if (fstat(fileno(file), &file_stat) != 0) return NULL;
  bool is_regular_file = ((file_stat.st_mode & S_IFREG) != 0);
  if (is_regular_file) return file;
  fclose(file);
  return NULL;
337 338 339
}


340 341 342 343 344
bool OS::Remove(const char* path) {
  return (remove(path) == 0);
}


345 346 347 348 349
FILE* OS::OpenTemporaryFile() {
  return tmpfile();
}


350
const char* const OS::LogFileOpenMode = "w";
351 352


353 354 355 356 357 358 359 360 361
void OS::Print(const char* format, ...) {
  va_list args;
  va_start(args, format);
  VPrint(format, args);
  va_end(args);
}


void OS::VPrint(const char* format, va_list args) {
362
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
363
  __android_log_vprint(ANDROID_LOG_INFO, LOG_TAG, format, args);
364
#else
365
  vprintf(format, args);
366
#endif
367 368 369
}


370 371 372 373 374 375 376 377 378
void OS::FPrint(FILE* out, const char* format, ...) {
  va_list args;
  va_start(args, format);
  VFPrint(out, format, args);
  va_end(args);
}


void OS::VFPrint(FILE* out, const char* format, va_list args) {
379
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
380
  __android_log_vprint(ANDROID_LOG_INFO, LOG_TAG, format, args);
381 382 383 384 385 386
#else
  vfprintf(out, format, args);
#endif
}


387 388 389 390 391 392 393 394 395
void OS::PrintError(const char* format, ...) {
  va_list args;
  va_start(args, format);
  VPrintError(format, args);
  va_end(args);
}


void OS::VPrintError(const char* format, va_list args) {
396
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
397
  __android_log_vprint(ANDROID_LOG_ERROR, LOG_TAG, format, args);
398
#else
399
  vfprintf(stderr, format, args);
400
#endif
401 402 403
}


404
int OS::SNPrintF(char* str, int length, const char* format, ...) {
405 406
  va_list args;
  va_start(args, format);
407
  int result = VSNPrintF(str, length, format, args);
408 409 410 411 412
  va_end(args);
  return result;
}


413 414
int OS::VSNPrintF(char* str,
                  int length,
415 416
                  const char* format,
                  va_list args) {
417 418
  int n = vsnprintf(str, length, format, args);
  if (n < 0 || n >= length) {
419
    // If the length is zero, the assignment fails.
420 421
    if (length > 0)
      str[length - 1] = '\0';
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
    return -1;
  } else {
    return n;
  }
}


// ----------------------------------------------------------------------------
// POSIX string support.
//

char* OS::StrChr(char* str, int c) {
  return strchr(str, c);
}


438 439
void OS::StrNCpy(char* dest, int length, const char* src, size_t n) {
  strncpy(dest, src, n);
440 441 442
}


443 444 445 446
// ----------------------------------------------------------------------------
// POSIX thread support.
//

447
class Thread::PlatformData {
448 449 450
 public:
  PlatformData() : thread_(kNoThread) {}
  pthread_t thread_;  // Thread handle for pthread.
451 452
  // Synchronizes thread creation
  Mutex thread_creation_mutex_;
453 454 455 456 457 458
};

Thread::Thread(const Options& options)
    : data_(new PlatformData),
      stack_size_(options.stack_size()),
      start_semaphore_(NULL) {
459
  if (stack_size_ > 0 && static_cast<size_t>(stack_size_) < PTHREAD_STACK_MIN) {
460 461
    stack_size_ = PTHREAD_STACK_MIN;
  }
462 463 464 465 466 467 468 469 470 471
  set_name(options.name());
}


Thread::~Thread() {
  delete data_;
}


static void SetThreadName(const char* name) {
472
#if V8_OS_DRAGONFLYBSD || V8_OS_FREEBSD || V8_OS_OPENBSD
473
  pthread_set_name_np(pthread_self(), name);
474
#elif V8_OS_NETBSD
475
  STATIC_ASSERT(Thread::kMaxThreadNameLength <= PTHREAD_MAX_NAMELEN_NP);
476
  pthread_setname_np(pthread_self(), "%s", name);
477
#elif V8_OS_MACOSX
478 479 480 481 482 483 484 485 486 487 488
  // pthread_setname_np is only available in 10.6 or later, so test
  // for it at runtime.
  int (*dynamic_pthread_setname_np)(const char*);
  *reinterpret_cast<void**>(&dynamic_pthread_setname_np) =
    dlsym(RTLD_DEFAULT, "pthread_setname_np");
  if (dynamic_pthread_setname_np == NULL)
    return;

  // Mac OS X does not expose the length limit of the name, so hardcode it.
  static const int kMaxNameLength = 63;
  STATIC_ASSERT(Thread::kMaxThreadNameLength <= kMaxNameLength);
489
  dynamic_pthread_setname_np(name);
490
#elif defined(PR_SET_NAME)
491 492 493
  prctl(PR_SET_NAME,
        reinterpret_cast<unsigned long>(name),  // NOLINT
        0, 0, 0);
494 495 496 497 498 499
#endif
}


static void* ThreadEntry(void* arg) {
  Thread* thread = reinterpret_cast<Thread*>(arg);
500 501 502
  // We take the lock here to make sure that pthread_create finished first since
  // we don't know which thread will run first (the original thread or the new
  // one).
503
  { LockGuard<Mutex> lock_guard(&thread->data()->thread_creation_mutex_); }
504
  SetThreadName(thread->name());
505
  DCHECK(thread->data()->thread_ != kNoThread);
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
  thread->NotifyStartedAndRun();
  return NULL;
}


void Thread::set_name(const char* name) {
  strncpy(name_, name, sizeof(name_));
  name_[sizeof(name_) - 1] = '\0';
}


void Thread::Start() {
  int result;
  pthread_attr_t attr;
  memset(&attr, 0, sizeof(attr));
  result = pthread_attr_init(&attr);
522
  DCHECK_EQ(0, result);
523
  // Native client uses default stack size.
524
#if !V8_OS_NACL
525 526
  if (stack_size_ > 0) {
    result = pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
527
    DCHECK_EQ(0, result);
528 529
  }
#endif
530
  {
531
    LockGuard<Mutex> lock_guard(&data_->thread_creation_mutex_);
532 533
    result = pthread_create(&data_->thread_, &attr, ThreadEntry, this);
  }
534
  DCHECK_EQ(0, result);
535
  result = pthread_attr_destroy(&attr);
536 537
  DCHECK_EQ(0, result);
  DCHECK(data_->thread_ != kNoThread);
538 539 540 541 542 543 544 545 546
  USE(result);
}


void Thread::Join() {
  pthread_join(data_->thread_, NULL);
}


547
void Thread::YieldCPU() {
548
  int result = sched_yield();
549
  DCHECK_EQ(0, result);
550 551 552 553 554
  USE(result);
}


static Thread::LocalStorageKey PthreadKeyToLocalKey(pthread_key_t pthread_key) {
555
#if V8_OS_CYGWIN
556 557 558 559 560 561 562 563 564 565 566 567 568
  // We need to cast pthread_key_t to Thread::LocalStorageKey in two steps
  // because pthread_key_t is a pointer type on Cygwin. This will probably not
  // work on 64-bit platforms, but Cygwin doesn't support 64-bit anyway.
  STATIC_ASSERT(sizeof(Thread::LocalStorageKey) == sizeof(pthread_key_t));
  intptr_t ptr_key = reinterpret_cast<intptr_t>(pthread_key);
  return static_cast<Thread::LocalStorageKey>(ptr_key);
#else
  return static_cast<Thread::LocalStorageKey>(pthread_key);
#endif
}


static pthread_key_t LocalKeyToPthreadKey(Thread::LocalStorageKey local_key) {
569
#if V8_OS_CYGWIN
570 571 572 573 574 575 576 577 578 579 580
  STATIC_ASSERT(sizeof(Thread::LocalStorageKey) == sizeof(pthread_key_t));
  intptr_t ptr_key = static_cast<intptr_t>(local_key);
  return reinterpret_cast<pthread_key_t>(ptr_key);
#else
  return static_cast<pthread_key_t>(local_key);
#endif
}


#ifdef V8_FAST_TLS_SUPPORTED

581
static Atomic32 tls_base_offset_initialized = 0;
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
intptr_t kMacTlsBaseOffset = 0;

// It's safe to do the initialization more that once, but it has to be
// done at least once.
static void InitializeTlsBaseOffset() {
  const size_t kBufferSize = 128;
  char buffer[kBufferSize];
  size_t buffer_size = kBufferSize;
  int ctl_name[] = { CTL_KERN , KERN_OSRELEASE };
  if (sysctl(ctl_name, 2, buffer, &buffer_size, NULL, 0) != 0) {
    V8_Fatal(__FILE__, __LINE__, "V8 failed to get kernel version");
  }
  // The buffer now contains a string of the form XX.YY.ZZ, where
  // XX is the major kernel version component.
  // Make sure the buffer is 0-terminated.
  buffer[kBufferSize - 1] = '\0';
  char* period_pos = strchr(buffer, '.');
  *period_pos = '\0';
  int kernel_version_major =
      static_cast<int>(strtol(buffer, NULL, 10));  // NOLINT
  // The constants below are taken from pthreads.s from the XNU kernel
  // sources archive at www.opensource.apple.com.
  if (kernel_version_major < 11) {
    // 8.x.x (Tiger), 9.x.x (Leopard), 10.x.x (Snow Leopard) have the
    // same offsets.
#if V8_HOST_ARCH_IA32
    kMacTlsBaseOffset = 0x48;
#else
    kMacTlsBaseOffset = 0x60;
#endif
  } else {
    // 11.x.x (Lion) changed the offset.
    kMacTlsBaseOffset = 0;
  }

617
  Release_Store(&tls_base_offset_initialized, 1);
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
}


static void CheckFastTls(Thread::LocalStorageKey key) {
  void* expected = reinterpret_cast<void*>(0x1234CAFE);
  Thread::SetThreadLocal(key, expected);
  void* actual = Thread::GetExistingThreadLocal(key);
  if (expected != actual) {
    V8_Fatal(__FILE__, __LINE__,
             "V8 failed to initialize fast TLS on current kernel");
  }
  Thread::SetThreadLocal(key, NULL);
}

#endif  // V8_FAST_TLS_SUPPORTED


Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
#ifdef V8_FAST_TLS_SUPPORTED
  bool check_fast_tls = false;
  if (tls_base_offset_initialized == 0) {
    check_fast_tls = true;
    InitializeTlsBaseOffset();
  }
#endif
  pthread_key_t key;
  int result = pthread_key_create(&key, NULL);
645
  DCHECK_EQ(0, result);
646 647 648 649 650 651 652 653 654 655 656 657 658
  USE(result);
  LocalStorageKey local_key = PthreadKeyToLocalKey(key);
#ifdef V8_FAST_TLS_SUPPORTED
  // If we just initialized fast TLS support, make sure it works.
  if (check_fast_tls) CheckFastTls(local_key);
#endif
  return local_key;
}


void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
  pthread_key_t pthread_key = LocalKeyToPthreadKey(key);
  int result = pthread_key_delete(pthread_key);
659
  DCHECK_EQ(0, result);
660 661 662 663 664 665 666 667 668 669 670 671 672
  USE(result);
}


void* Thread::GetThreadLocal(LocalStorageKey key) {
  pthread_key_t pthread_key = LocalKeyToPthreadKey(key);
  return pthread_getspecific(pthread_key);
}


void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
  pthread_key_t pthread_key = LocalKeyToPthreadKey(key);
  int result = pthread_setspecific(pthread_key, value);
673
  DCHECK_EQ(0, result);
674
  USE(result);
675 676 677
}


678
} }  // namespace v8::base