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 for FreeBSD goes here. For the POSIX-compatible
6 // parts, the implementation is in platform-posix.cc.
7
8 #include <pthread.h>
9 #include <pthread_np.h>
10 #include <semaphore.h>
11 #include <signal.h>
12 #include <stdlib.h>
13 #include <sys/resource.h>
14 #include <sys/time.h>
15 #include <sys/types.h>
16 #include <sys/ucontext.h>
17 #include <sys/user.h>
18
19 #include <sys/fcntl.h> // open
20 #include <sys/mman.h> // mmap & munmap
21 #include <sys/stat.h> // open
22 #include <sys/sysctl.h>
23 #include <unistd.h> // getpagesize
24 // If you don't have execinfo.h then you need devel/libexecinfo from ports.
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdarg.h>
28 #include <strings.h> // index
29
30 #include <cmath>
31
32 #undef MAP_TYPE
33
34 #include "src/base/macros.h"
35 #include "src/base/platform/platform-posix-time.h"
36 #include "src/base/platform/platform-posix.h"
37 #include "src/base/platform/platform.h"
38
39 namespace v8 {
40 namespace base {
41
CreateTimezoneCache()42 TimezoneCache* OS::CreateTimezoneCache() {
43 return new PosixDefaultTimezoneCache();
44 }
45
StringToLong(char * buffer)46 static unsigned StringToLong(char* buffer) {
47 return static_cast<unsigned>(strtol(buffer, nullptr, 16));
48 }
49
GetSharedLibraryAddresses()50 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
51 std::vector<SharedLibraryAddress> result;
52 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_VMMAP, getpid()};
53 size_t miblen = sizeof(mib) / sizeof(mib[0]);
54 size_t buffer_size;
55 if (sysctl(mib, miblen, nullptr, &buffer_size, nullptr, 0) == 0) {
56 // Overallocate the buffer by 1/3 to account for concurrent
57 // kinfo_vmentry change. 1/3 is an arbitrary constant that
58 // works in practice.
59 buffer_size = buffer_size * 4 / 3;
60 std::vector<char> buffer(buffer_size);
61 int ret = sysctl(mib, miblen, buffer.data(), &buffer_size, nullptr, 0);
62
63 if (ret == 0 || (ret == -1 && errno == ENOMEM)) {
64 char* start = buffer.data();
65 char* end = start + buffer_size;
66
67 while (start < end) {
68 struct kinfo_vmentry* map =
69 reinterpret_cast<struct kinfo_vmentry*>(start);
70 const size_t ssize = map->kve_structsize;
71 char* path = map->kve_path;
72
73 CHECK_NE(0, ssize);
74
75 if ((map->kve_protection & KVME_PROT_READ) != 0 &&
76 (map->kve_protection & KVME_PROT_EXEC) != 0 && path[0] != '\0') {
77 char* sep = strrchr(path, '/');
78 std::string lib_name;
79 if (sep != nullptr) {
80 lib_name = std::string(++sep);
81 } else {
82 lib_name = std::string(path);
83 }
84 result.push_back(SharedLibraryAddress(
85 lib_name, reinterpret_cast<uintptr_t>(map->kve_start),
86 reinterpret_cast<uintptr_t>(map->kve_end)));
87 }
88
89 start += ssize;
90 }
91 }
92 }
93 return result;
94 }
95
SignalCodeMovingGC()96 void OS::SignalCodeMovingGC() {}
97
AdjustSchedulingParams()98 void OS::AdjustSchedulingParams() {}
99
GetFreeMemoryRangesWithin(OS::Address boundary_start,OS::Address boundary_end,size_t minimum_size,size_t alignment)100 std::vector<OS::MemoryRange> OS::GetFreeMemoryRangesWithin(
101 OS::Address boundary_start, OS::Address boundary_end, size_t minimum_size,
102 size_t alignment) {
103 return {};
104 }
105
106 // static
GetStackStart()107 Stack::StackSlot Stack::GetStackStart() {
108 pthread_attr_t attr;
109 int error;
110 pthread_attr_init(&attr);
111 error = pthread_attr_get_np(pthread_self(), &attr);
112 if (!error) {
113 void* base;
114 size_t size;
115 error = pthread_attr_getstack(&attr, &base, &size);
116 CHECK(!error);
117 pthread_attr_destroy(&attr);
118 return reinterpret_cast<uint8_t*>(base) + size;
119 }
120 pthread_attr_destroy(&attr);
121 return nullptr;
122 }
123
124 } // namespace base
125 } // namespace v8
126