21 #ifndef V8_BASE_PLATFORM_PLATFORM_H_ 22 #define V8_BASE_PLATFORM_PLATFORM_H_ 28 #include "src/base/base-export.h" 29 #include "src/base/build_config.h" 30 #include "src/base/compiler-specific.h" 31 #include "src/base/platform/mutex.h" 32 #include "src/base/platform/semaphore.h" 35 #include "src/base/qnx-math.h" 45 #ifndef V8_NO_FAST_TLS 47 #if V8_CC_MSVC && V8_HOST_ARCH_IA32 49 #define V8_FAST_TLS_SUPPORTED 1 51 V8_INLINE intptr_t InternalGetExistingThreadLocal(intptr_t index);
53 inline intptr_t InternalGetExistingThreadLocal(intptr_t index) {
54 const intptr_t kTibInlineTlsOffset = 0xE10;
55 const intptr_t kTibExtraTlsOffset = 0xF94;
56 const intptr_t kMaxInlineSlots = 64;
57 const intptr_t kMaxSlots = kMaxInlineSlots + 1024;
58 const intptr_t kPointerSize =
sizeof(
void*);
59 DCHECK(0 <= index && index < kMaxSlots);
61 if (index < kMaxInlineSlots) {
62 return static_cast<intptr_t
>(__readfsdword(kTibInlineTlsOffset +
63 kPointerSize * index));
65 intptr_t extra =
static_cast<intptr_t
>(__readfsdword(kTibExtraTlsOffset));
67 return *
reinterpret_cast<intptr_t*
>(extra +
68 kPointerSize * (index - kMaxInlineSlots));
71 #elif defined(__APPLE__) && (V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64) 73 #define V8_FAST_TLS_SUPPORTED 1 75 extern V8_BASE_EXPORT intptr_t kMacTlsBaseOffset;
77 V8_INLINE intptr_t InternalGetExistingThreadLocal(intptr_t index);
79 inline intptr_t InternalGetExistingThreadLocal(intptr_t index) {
82 asm(
"movl %%gs:(%1,%2,4), %0;" 84 :
"r"(kMacTlsBaseOffset),
"r"(index));
86 asm(
"movq %%gs:(%1,%2,8), %0;" 88 :
"r"(kMacTlsBaseOffset),
"r"(index));
95 #endif // V8_NO_FAST_TLS 107 class V8_BASE_EXPORT
OS {
112 static void Initialize(
bool hard_abort,
const char*
const gc_fake_mmap);
122 static double TimeCurrentMillis();
127 static int GetLastError();
129 static FILE* FOpen(
const char* path,
const char* mode);
130 static bool Remove(
const char* path);
132 static char DirectorySeparator();
133 static bool isDirectorySeparator(
const char ch);
136 static FILE* OpenTemporaryFile();
139 static const char*
const LogFileOpenMode;
144 static PRINTF_FORMAT(1, 2)
void Print(
const char* format, ...);
145 static PRINTF_FORMAT(1, 0)
void VPrint(
const char* format, va_list args);
148 static PRINTF_FORMAT(2, 3)
void FPrint(FILE* out,
const char* format, ...);
149 static PRINTF_FORMAT(2, 0)
void VFPrint(FILE* out,
const char* format,
155 static PRINTF_FORMAT(1, 2)
void PrintError(
const char* format, ...);
156 static PRINTF_FORMAT(1, 0)
void VPrintError(
const char* format, va_list args);
160 enum class MemoryPermission {
169 static bool HasLazyCommits();
175 [[noreturn]]
static void Abort();
178 static void DebugBreak();
181 static const int kStackWalkError = -1;
182 static const int kStackWalkMaxNameLen = 256;
183 static const int kStackWalkMaxTextLen = 256;
186 char text[kStackWalkMaxTextLen];
192 virtual void* memory()
const = 0;
193 virtual size_t size()
const = 0;
202 static PRINTF_FORMAT(3, 4)
int SNPrintF(
char* str,
int length,
203 const
char* format, ...);
204 static PRINTF_FORMAT(3, 0)
int VSNPrintF(
char* str,
int length,
205 const
char* format, va_list args);
207 static
char* StrChr(
char* str,
int c);
208 static
void StrNCpy(
char* dest,
int length, const
char* src,
size_t n);
215 : library_path(library_path), start(start), end(end), aslr_slide(0) {}
218 : library_path(library_path),
221 aslr_slide(aslr_slide) {}
223 std::string library_path;
229 static std::vector<SharedLibraryAddress> GetSharedLibraryAddresses();
235 static void SignalCodeMovingGC();
239 static bool ArmUsingHardFloat();
243 static int ActivationFrameAlignment();
245 static int GetCurrentProcessId();
247 static int GetCurrentThreadId();
249 static void ExitProcess(
int exit_code);
257 static size_t AllocatePageSize();
259 static size_t CommitPageSize();
261 static void SetRandomMmapSeed(
int64_t seed);
263 static void* GetRandomMmapAddr();
265 V8_WARN_UNUSED_RESULT
static void* Allocate(
void* address,
size_t size,
267 MemoryPermission access);
269 V8_WARN_UNUSED_RESULT
static bool Free(
void* address,
const size_t size);
271 V8_WARN_UNUSED_RESULT
static bool Release(
void* address,
size_t size);
273 V8_WARN_UNUSED_RESULT
static bool SetPermissions(
void* address,
size_t size,
274 MemoryPermission access);
276 V8_WARN_UNUSED_RESULT
static bool DiscardSystemPages(
void* address,
279 static const int msPerSecond = 1000;
282 static const char* GetGCFakeMMapFile();
285 DISALLOW_IMPLICIT_CONSTRUCTORS(
OS);
288 #if (defined(_WIN32) || defined(_WIN64)) 289 V8_BASE_EXPORT
void EnsureConsoleOutputWin32();
290 #endif // (defined(_WIN32) || defined(_WIN64)) 292 inline void EnsureConsoleOutput() {
293 #if (defined(_WIN32) || defined(_WIN64)) 296 EnsureConsoleOutputWin32();
297 #endif // (defined(_WIN32) || defined(_WIN64)) 311 typedef int32_t LocalStorageKey;
315 Options() : name_(
"v8:<unknown>"), stack_size_(0) {}
316 explicit Options(
const char* name,
int stack_size = 0)
317 : name_(name), stack_size_(stack_size) {}
319 const char* name()
const {
return name_; }
320 int stack_size()
const {
return stack_size_; }
335 void StartSynchronously() {
338 start_semaphore_->Wait();
339 delete start_semaphore_;
340 start_semaphore_ =
nullptr;
346 inline const char* name()
const {
351 virtual void Run() = 0;
354 static LocalStorageKey CreateThreadLocalKey();
355 static void DeleteThreadLocalKey(LocalStorageKey key);
356 static void* GetThreadLocal(LocalStorageKey key);
357 static int GetThreadLocalInt(LocalStorageKey key) {
358 return static_cast<int>(
reinterpret_cast<intptr_t
>(GetThreadLocal(key)));
360 static void SetThreadLocal(LocalStorageKey key,
void* value);
361 static void SetThreadLocalInt(LocalStorageKey key,
int value) {
362 SetThreadLocal(key, reinterpret_cast<void*>(static_cast<intptr_t>(value)));
364 static bool HasThreadLocal(LocalStorageKey key) {
365 return GetThreadLocal(key) !=
nullptr;
368 #ifdef V8_FAST_TLS_SUPPORTED 369 static inline void* GetExistingThreadLocal(LocalStorageKey key) {
370 void* result =
reinterpret_cast<void*
>(
371 InternalGetExistingThreadLocal(static_cast<intptr_t>(key)));
372 DCHECK(result == GetThreadLocal(key));
376 static inline void* GetExistingThreadLocal(LocalStorageKey key) {
377 return GetThreadLocal(key);
383 static const int kMaxThreadNameLength = 16;
386 PlatformData* data() {
return data_; }
388 void NotifyStartedAndRun() {
389 if (start_semaphore_) start_semaphore_->Signal();
394 void set_name(
const char* name);
398 char name_[kMaxThreadNameLength];
400 Semaphore* start_semaphore_;
402 DISALLOW_COPY_AND_ASSIGN(Thread);
408 #endif // V8_BASE_PLATFORM_PLATFORM_H_