| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 | 53 |
| 54 #include "platform-posix.h" | 54 #include "platform-posix.h" |
| 55 #include "platform.h" | 55 #include "platform.h" |
| 56 #include "v8threads.h" | 56 #include "v8threads.h" |
| 57 #include "vm-state-inl.h" | 57 #include "vm-state-inl.h" |
| 58 | 58 |
| 59 | 59 |
| 60 namespace v8 { | 60 namespace v8 { |
| 61 namespace internal { | 61 namespace internal { |
| 62 | 62 |
| 63 // 0 is never a valid thread id on Linux and OpenBSD since tids and pids share a | |
| 64 // name space and pid 0 is reserved (see man 2 kill). | |
| 65 static const pthread_t kNoThread = (pthread_t) 0; | |
| 66 | |
| 67 | |
| 68 double ceiling(double x) { | |
| 69 return ceil(x); | |
| 70 } | |
| 71 | |
| 72 | 63 |
| 73 static Mutex* limit_mutex = NULL; | 64 static Mutex* limit_mutex = NULL; |
| 74 | 65 |
| 75 | 66 |
| 76 static void* GetRandomMmapAddr() { | |
| 77 Isolate* isolate = Isolate::UncheckedCurrent(); | |
| 78 // Note that the current isolate isn't set up in a call path via | |
| 79 // CpuFeatures::Probe. We don't care about randomization in this case because | |
| 80 // the code page is immediately freed. | |
| 81 if (isolate != NULL) { | |
| 82 #if V8_TARGET_ARCH_X64 | |
| 83 uint64_t rnd1 = V8::RandomPrivate(isolate); | |
| 84 uint64_t rnd2 = V8::RandomPrivate(isolate); | |
| 85 uint64_t raw_addr = (rnd1 << 32) ^ rnd2; | |
| 86 // Currently available CPUs have 48 bits of virtual addressing. Truncate | |
| 87 // the hint address to 46 bits to give the kernel a fighting chance of | |
| 88 // fulfilling our placement request. | |
| 89 raw_addr &= V8_UINT64_C(0x3ffffffff000); | |
| 90 #else | |
| 91 uint32_t raw_addr = V8::RandomPrivate(isolate); | |
| 92 // The range 0x20000000 - 0x60000000 is relatively unpopulated across a | |
| 93 // variety of ASLR modes (PAE kernel, NX compat mode, etc). | |
| 94 raw_addr &= 0x3ffff000; | |
| 95 raw_addr += 0x20000000; | |
| 96 #endif | |
| 97 return reinterpret_cast<void*>(raw_addr); | |
| 98 } | |
| 99 return NULL; | |
| 100 } | |
| 101 | |
| 102 | |
| 103 void OS::PostSetUp() { | |
| 104 POSIXPostSetUp(); | |
| 105 } | |
| 106 | |
| 107 | |
| 108 uint64_t OS::CpuFeaturesImpliedByPlatform() { | |
| 109 return 0; | |
| 110 } | |
| 111 | |
| 112 | |
| 113 int OS::ActivationFrameAlignment() { | |
| 114 // With gcc 4.4 the tree vectorization optimizer can generate code | |
| 115 // that requires 16 byte alignment such as movdqa on x86. | |
| 116 return 16; | |
| 117 } | |
| 118 | |
| 119 | |
| 120 const char* OS::LocalTimezone(double time) { | 67 const char* OS::LocalTimezone(double time) { |
| 121 if (std::isnan(time)) return ""; | 68 if (std::isnan(time)) return ""; |
| 122 time_t tv = static_cast<time_t>(floor(time/msPerSecond)); | 69 time_t tv = static_cast<time_t>(floor(time/msPerSecond)); |
| 123 struct tm* t = localtime(&tv); | 70 struct tm* t = localtime(&tv); |
| 124 if (NULL == t) return ""; | 71 if (NULL == t) return ""; |
| 125 return t->tm_zone; | 72 return t->tm_zone; |
| 126 } | 73 } |
| 127 | 74 |
| 128 | 75 |
| 129 double OS::LocalTimeOffset() { | 76 double OS::LocalTimeOffset() { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 153 Max(highest_ever_allocated, | 100 Max(highest_ever_allocated, |
| 154 reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size)); | 101 reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size)); |
| 155 } | 102 } |
| 156 | 103 |
| 157 | 104 |
| 158 bool OS::IsOutsideAllocatedSpace(void* address) { | 105 bool OS::IsOutsideAllocatedSpace(void* address) { |
| 159 return address < lowest_ever_allocated || address >= highest_ever_allocated; | 106 return address < lowest_ever_allocated || address >= highest_ever_allocated; |
| 160 } | 107 } |
| 161 | 108 |
| 162 | 109 |
| 163 size_t OS::AllocateAlignment() { | |
| 164 return sysconf(_SC_PAGESIZE); | |
| 165 } | |
| 166 | |
| 167 | |
| 168 void* OS::Allocate(const size_t requested, | 110 void* OS::Allocate(const size_t requested, |
| 169 size_t* allocated, | 111 size_t* allocated, |
| 170 bool is_executable) { | 112 bool is_executable) { |
| 171 const size_t msize = RoundUp(requested, AllocateAlignment()); | 113 const size_t msize = RoundUp(requested, AllocateAlignment()); |
| 172 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); | 114 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| 173 void* addr = GetRandomMmapAddr(); | 115 void* addr = OS::GetRandomMmapAddr(); |
| 174 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0); | 116 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0); |
| 175 if (mbase == MAP_FAILED) { | 117 if (mbase == MAP_FAILED) { |
| 176 LOG(i::Isolate::Current(), | 118 LOG(i::Isolate::Current(), |
| 177 StringEvent("OS::Allocate", "mmap failed")); | 119 StringEvent("OS::Allocate", "mmap failed")); |
| 178 return NULL; | 120 return NULL; |
| 179 } | 121 } |
| 180 *allocated = msize; | 122 *allocated = msize; |
| 181 UpdateAllocatedSpaceLimits(mbase, msize); | 123 UpdateAllocatedSpaceLimits(mbase, msize); |
| 182 return mbase; | 124 return mbase; |
| 183 } | 125 } |
| 184 | 126 |
| 185 | 127 |
| 186 void OS::Free(void* address, const size_t size) { | |
| 187 // TODO(1240712): munmap has a return value which is ignored here. | |
| 188 int result = munmap(address, size); | |
| 189 USE(result); | |
| 190 ASSERT(result == 0); | |
| 191 } | |
| 192 | |
| 193 | |
| 194 void OS::Sleep(int milliseconds) { | |
| 195 unsigned int ms = static_cast<unsigned int>(milliseconds); | |
| 196 usleep(1000 * ms); | |
| 197 } | |
| 198 | |
| 199 | |
| 200 int OS::NumberOfCores() { | |
| 201 return sysconf(_SC_NPROCESSORS_ONLN); | |
| 202 } | |
| 203 | |
| 204 | |
| 205 void OS::Abort() { | |
| 206 // Redirect to std abort to signal abnormal program termination. | |
| 207 abort(); | |
| 208 } | |
| 209 | |
| 210 | |
| 211 void OS::DebugBreak() { | |
| 212 asm("int $3"); | |
| 213 } | |
| 214 | |
| 215 | |
| 216 void OS::DumpBacktrace() { | 128 void OS::DumpBacktrace() { |
| 217 // Currently unsupported. | 129 // Currently unsupported. |
| 218 } | 130 } |
| 219 | 131 |
| 220 | 132 |
| 221 class PosixMemoryMappedFile : public OS::MemoryMappedFile { | 133 class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
| 222 public: | 134 public: |
| 223 PosixMemoryMappedFile(FILE* file, void* memory, int size) | 135 PosixMemoryMappedFile(FILE* file, void* memory, int size) |
| 224 : file_(file), memory_(memory), size_(size) { } | 136 : file_(file), memory_(memory), size_(size) { } |
| 225 virtual ~PosixMemoryMappedFile(); | 137 virtual ~PosixMemoryMappedFile(); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 | 300 |
| 389 VirtualMemory::VirtualMemory(size_t size) | 301 VirtualMemory::VirtualMemory(size_t size) |
| 390 : address_(ReserveRegion(size)), size_(size) { } | 302 : address_(ReserveRegion(size)), size_(size) { } |
| 391 | 303 |
| 392 | 304 |
| 393 VirtualMemory::VirtualMemory(size_t size, size_t alignment) | 305 VirtualMemory::VirtualMemory(size_t size, size_t alignment) |
| 394 : address_(NULL), size_(0) { | 306 : address_(NULL), size_(0) { |
| 395 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment()))); | 307 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment()))); |
| 396 size_t request_size = RoundUp(size + alignment, | 308 size_t request_size = RoundUp(size + alignment, |
| 397 static_cast<intptr_t>(OS::AllocateAlignment())); | 309 static_cast<intptr_t>(OS::AllocateAlignment())); |
| 398 void* reservation = mmap(GetRandomMmapAddr(), | 310 void* reservation = mmap(OS::GetRandomMmapAddr(), |
| 399 request_size, | 311 request_size, |
| 400 PROT_NONE, | 312 PROT_NONE, |
| 401 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, | 313 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
| 402 kMmapFd, | 314 kMmapFd, |
| 403 kMmapFdOffset); | 315 kMmapFdOffset); |
| 404 if (reservation == MAP_FAILED) return; | 316 if (reservation == MAP_FAILED) return; |
| 405 | 317 |
| 406 Address base = static_cast<Address>(reservation); | 318 Address base = static_cast<Address>(reservation); |
| 407 Address aligned_base = RoundUp(base, alignment); | 319 Address aligned_base = RoundUp(base, alignment); |
| 408 ASSERT_LE(base, aligned_base); | 320 ASSERT_LE(base, aligned_base); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 } | 372 } |
| 461 | 373 |
| 462 | 374 |
| 463 bool VirtualMemory::Guard(void* address) { | 375 bool VirtualMemory::Guard(void* address) { |
| 464 OS::Guard(address, OS::CommitPageSize()); | 376 OS::Guard(address, OS::CommitPageSize()); |
| 465 return true; | 377 return true; |
| 466 } | 378 } |
| 467 | 379 |
| 468 | 380 |
| 469 void* VirtualMemory::ReserveRegion(size_t size) { | 381 void* VirtualMemory::ReserveRegion(size_t size) { |
| 470 void* result = mmap(GetRandomMmapAddr(), | 382 void* result = mmap(OS::GetRandomMmapAddr(), |
| 471 size, | 383 size, |
| 472 PROT_NONE, | 384 PROT_NONE, |
| 473 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, | 385 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
| 474 kMmapFd, | 386 kMmapFd, |
| 475 kMmapFdOffset); | 387 kMmapFdOffset); |
| 476 | 388 |
| 477 if (result == MAP_FAILED) return NULL; | 389 if (result == MAP_FAILED) return NULL; |
| 478 | 390 |
| 479 return result; | 391 return result; |
| 480 } | 392 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 510 return munmap(base, size) == 0; | 422 return munmap(base, size) == 0; |
| 511 } | 423 } |
| 512 | 424 |
| 513 | 425 |
| 514 bool VirtualMemory::HasLazyCommits() { | 426 bool VirtualMemory::HasLazyCommits() { |
| 515 // TODO(alph): implement for the platform. | 427 // TODO(alph): implement for the platform. |
| 516 return false; | 428 return false; |
| 517 } | 429 } |
| 518 | 430 |
| 519 | 431 |
| 520 class Thread::PlatformData : public Malloced { | |
| 521 public: | |
| 522 PlatformData() : thread_(kNoThread) {} | |
| 523 | |
| 524 pthread_t thread_; // Thread handle for pthread. | |
| 525 }; | |
| 526 | |
| 527 Thread::Thread(const Options& options) | |
| 528 : data_(new PlatformData()), | |
| 529 stack_size_(options.stack_size()), | |
| 530 start_semaphore_(NULL) { | |
| 531 set_name(options.name()); | |
| 532 } | |
| 533 | |
| 534 | |
| 535 Thread::~Thread() { | |
| 536 delete data_; | |
| 537 } | |
| 538 | |
| 539 | |
| 540 static void* ThreadEntry(void* arg) { | |
| 541 Thread* thread = reinterpret_cast<Thread*>(arg); | |
| 542 // This is also initialized by the first argument to pthread_create() but we | |
| 543 // don't know which thread will run first (the original thread or the new | |
| 544 // one) so we initialize it here too. | |
| 545 #ifdef PR_SET_NAME | |
| 546 prctl(PR_SET_NAME, | |
| 547 reinterpret_cast<unsigned long>(thread->name()), // NOLINT | |
| 548 0, 0, 0); | |
| 549 #endif | |
| 550 thread->data()->thread_ = pthread_self(); | |
| 551 ASSERT(thread->data()->thread_ != kNoThread); | |
| 552 thread->NotifyStartedAndRun(); | |
| 553 return NULL; | |
| 554 } | |
| 555 | |
| 556 | |
| 557 void Thread::set_name(const char* name) { | |
| 558 strncpy(name_, name, sizeof(name_)); | |
| 559 name_[sizeof(name_) - 1] = '\0'; | |
| 560 } | |
| 561 | |
| 562 | |
| 563 void Thread::Start() { | |
| 564 pthread_attr_t* attr_ptr = NULL; | |
| 565 pthread_attr_t attr; | |
| 566 if (stack_size_ > 0) { | |
| 567 pthread_attr_init(&attr); | |
| 568 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); | |
| 569 attr_ptr = &attr; | |
| 570 } | |
| 571 pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this); | |
| 572 ASSERT(data_->thread_ != kNoThread); | |
| 573 } | |
| 574 | |
| 575 | |
| 576 void Thread::Join() { | |
| 577 pthread_join(data_->thread_, NULL); | |
| 578 } | |
| 579 | |
| 580 | |
| 581 Thread::LocalStorageKey Thread::CreateThreadLocalKey() { | |
| 582 pthread_key_t key; | |
| 583 int result = pthread_key_create(&key, NULL); | |
| 584 USE(result); | |
| 585 ASSERT(result == 0); | |
| 586 return static_cast<LocalStorageKey>(key); | |
| 587 } | |
| 588 | |
| 589 | |
| 590 void Thread::DeleteThreadLocalKey(LocalStorageKey key) { | |
| 591 pthread_key_t pthread_key = static_cast<pthread_key_t>(key); | |
| 592 int result = pthread_key_delete(pthread_key); | |
| 593 USE(result); | |
| 594 ASSERT(result == 0); | |
| 595 } | |
| 596 | |
| 597 | |
| 598 void* Thread::GetThreadLocal(LocalStorageKey key) { | |
| 599 pthread_key_t pthread_key = static_cast<pthread_key_t>(key); | |
| 600 return pthread_getspecific(pthread_key); | |
| 601 } | |
| 602 | |
| 603 | |
| 604 void Thread::SetThreadLocal(LocalStorageKey key, void* value) { | |
| 605 pthread_key_t pthread_key = static_cast<pthread_key_t>(key); | |
| 606 pthread_setspecific(pthread_key, value); | |
| 607 } | |
| 608 | |
| 609 | |
| 610 class OpenBSDSemaphore : public Semaphore { | 432 class OpenBSDSemaphore : public Semaphore { |
| 611 public: | 433 public: |
| 612 explicit OpenBSDSemaphore(int count) { sem_init(&sem_, 0, count); } | 434 explicit OpenBSDSemaphore(int count) { sem_init(&sem_, 0, count); } |
| 613 virtual ~OpenBSDSemaphore() { sem_destroy(&sem_); } | 435 virtual ~OpenBSDSemaphore() { sem_destroy(&sem_); } |
| 614 | 436 |
| 615 virtual void Wait(); | 437 virtual void Wait(); |
| 616 virtual bool Wait(int timeout); | 438 virtual bool Wait(int timeout); |
| 617 virtual void Signal() { sem_post(&sem_); } | 439 virtual void Signal() { sem_post(&sem_); } |
| 618 private: | 440 private: |
| 619 sem_t sem_; | 441 sem_t sem_; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 683 limit_mutex = CreateMutex(); | 505 limit_mutex = CreateMutex(); |
| 684 } | 506 } |
| 685 | 507 |
| 686 | 508 |
| 687 void OS::TearDown() { | 509 void OS::TearDown() { |
| 688 delete limit_mutex; | 510 delete limit_mutex; |
| 689 } | 511 } |
| 690 | 512 |
| 691 | 513 |
| 692 } } // namespace v8::internal | 514 } } // namespace v8::internal |
| OLD | NEW |