1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Platform-specific code shared between macOS and iOS goes here. The macOS
6 // specific part is in platform-macos.cc, the POSIX-compatible parts in
7 // platform-posix.cc.
8
9 #include <AvailabilityMacros.h>
10 #include <dlfcn.h>
11 #include <errno.h>
12 #include <libkern/OSAtomic.h>
13 #include <mach-o/dyld.h>
14 #include <mach-o/getsect.h>
15 #include <mach/mach.h>
16 #include <mach/mach_init.h>
17 #include <mach/semaphore.h>
18 #include <mach/task.h>
19 #include <mach/vm_statistics.h>
20 #include <pthread.h>
21 #include <semaphore.h>
22 #include <signal.h>
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/mman.h>
27 #include <sys/resource.h>
28 #include <sys/sysctl.h>
29 #include <sys/time.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32
33 #include <cmath>
34
35 #undef MAP_TYPE
36
37 #include "src/base/macros.h"
38 #include "src/base/platform/platform-posix-time.h"
39 #include "src/base/platform/platform-posix.h"
40 #include "src/base/platform/platform.h"
41
42 namespace v8 {
43 namespace base {
44
GetSharedLibraryAddresses()45 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
46 std::vector<SharedLibraryAddress> result;
47 unsigned int images_count = _dyld_image_count();
48 for (unsigned int i = 0; i < images_count; ++i) {
49 const mach_header* header = _dyld_get_image_header(i);
50 if (header == nullptr) continue;
51 #if V8_HOST_ARCH_I32
52 unsigned int size;
53 char* code_ptr = getsectdatafromheader(header, SEG_TEXT, SECT_TEXT, &size);
54 #else
55 uint64_t size;
56 char* code_ptr = getsectdatafromheader_64(
57 reinterpret_cast<const mach_header_64*>(header), SEG_TEXT, SECT_TEXT,
58 &size);
59 #endif
60 if (code_ptr == nullptr) continue;
61 const intptr_t slide = _dyld_get_image_vmaddr_slide(i);
62 const uintptr_t start = reinterpret_cast<uintptr_t>(code_ptr) + slide;
63 result.push_back(SharedLibraryAddress(_dyld_get_image_name(i), start,
64 start + size, slide));
65 }
66 return result;
67 }
68
SignalCodeMovingGC()69 void OS::SignalCodeMovingGC() {}
70
CreateTimezoneCache()71 TimezoneCache* OS::CreateTimezoneCache() {
72 return new PosixDefaultTimezoneCache();
73 }
74
AdjustSchedulingParams()75 void OS::AdjustSchedulingParams() {
76 #if V8_TARGET_ARCH_X64 || V8_TARGET_ARCH_IA32
77 {
78 // Check availability of scheduling params.
79 uint32_t val = 0;
80 size_t valSize = sizeof(val);
81 int rc = sysctlbyname("kern.tcsm_available", &val, &valSize, NULL, 0);
82 if (rc < 0 || !val) return;
83 }
84
85 {
86 // Adjust scheduling params.
87 uint32_t val = 1;
88 int rc = sysctlbyname("kern.tcsm_enable", NULL, NULL, &val, sizeof(val));
89 DCHECK_GE(rc, 0);
90 USE(rc);
91 }
92 #endif
93 }
94
GetFreeMemoryRangesWithin(OS::Address boundary_start,OS::Address boundary_end,size_t minimum_size,size_t alignment)95 std::vector<OS::MemoryRange> OS::GetFreeMemoryRangesWithin(
96 OS::Address boundary_start, OS::Address boundary_end, size_t minimum_size,
97 size_t alignment) {
98 return {};
99 }
100
101 // static
GetStackStart()102 Stack::StackSlot Stack::GetStackStart() {
103 return pthread_get_stackaddr_np(pthread_self());
104 }
105
106 } // namespace base
107 } // namespace v8
108