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 <semaphore.h>
10 #include <signal.h>
11 #include <stdlib.h>
12 #include <sys/resource.h>
13 #include <sys/time.h>
14 #include <sys/types.h>
15 #include <sys/ucontext.h>
16 
17 #include <sys/fcntl.h>  // open
18 #include <sys/mman.h>   // mmap & munmap
19 #include <sys/stat.h>   // open
20 #include <unistd.h>     // getpagesize
21 // If you don't have execinfo.h then you need devel/libexecinfo from ports.
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdarg.h>
25 #include <strings.h>    // index
26 
27 #include <cmath>
28 
29 #undef MAP_TYPE
30 
31 #include "src/base/macros.h"
32 #include "src/base/platform/platform-posix-time.h"
33 #include "src/base/platform/platform-posix.h"
34 #include "src/base/platform/platform.h"
35 
36 namespace v8 {
37 namespace base {
38 
CreateTimezoneCache()39 TimezoneCache* OS::CreateTimezoneCache() {
40   return new PosixDefaultTimezoneCache();
41 }
42 
StringToLong(char * buffer)43 static unsigned StringToLong(char* buffer) {
44   return static_cast<unsigned>(strtol(buffer, nullptr, 16));  // NOLINT
45 }
46 
GetSharedLibraryAddresses()47 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
48   std::vector<SharedLibraryAddress> result;
49   static const int MAP_LENGTH = 1024;
50   int fd = open("/proc/self/maps", O_RDONLY);
51   if (fd < 0) return result;
52   while (true) {
53     char addr_buffer[11];
54     addr_buffer[0] = '0';
55     addr_buffer[1] = 'x';
56     addr_buffer[10] = 0;
57     ssize_t bytes_read = read(fd, addr_buffer + 2, 8);
58     if (bytes_read < 8) break;
59     unsigned start = StringToLong(addr_buffer);
60     bytes_read = read(fd, addr_buffer + 2, 1);
61     if (bytes_read < 1) break;
62     if (addr_buffer[2] != '-') break;
63     bytes_read = read(fd, addr_buffer + 2, 8);
64     if (bytes_read < 8) break;
65     unsigned end = StringToLong(addr_buffer);
66     char buffer[MAP_LENGTH];
67     bytes_read = -1;
68     do {
69       bytes_read++;
70       if (bytes_read >= MAP_LENGTH - 1) break;
71       bytes_read = read(fd, buffer + bytes_read, 1);
72       if (bytes_read < 1) break;
73     } while (buffer[bytes_read] != '\n');
74     buffer[bytes_read] = 0;
75     // Ignore mappings that are not executable.
76     if (buffer[3] != 'x') continue;
77     char* start_of_path = index(buffer, '/');
78     // There may be no filename in this line.  Skip to next.
79     if (start_of_path == nullptr) continue;
80     buffer[bytes_read] = 0;
81     result.push_back(SharedLibraryAddress(start_of_path, start, end));
82   }
83   close(fd);
84   return result;
85 }
86 
SignalCodeMovingGC()87 void OS::SignalCodeMovingGC() {}
88 
89 #ifdef __arm__
90 
ArmUsingHardFloat()91 bool OS::ArmUsingHardFloat() {
92 // GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify
93 // the Floating Point ABI used (PCS stands for Procedure Call Standard).
94 // We use these as well as a couple of other defines to statically determine
95 // what FP ABI used.
96 // GCC versions 4.4 and below don't support hard-fp.
97 // GCC versions 4.5 may support hard-fp without defining __ARM_PCS or
98 // __ARM_PCS_VFP.
99 
100 #define GCC_VERSION \
101   (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
102 #if GCC_VERSION >= 40600 && !defined(__clang__)
103 #if defined(__ARM_PCS_VFP)
104   return true;
105 #else
106   return false;
107 #endif
108 
109 #elif GCC_VERSION < 40500 && !defined(__clang__)
110   return false;
111 
112 #else
113 #if defined(__ARM_PCS_VFP)
114   return true;
115 #elif defined(__ARM_PCS) || defined(__SOFTFP__) || defined(__SOFTFP) || \
116     !defined(__VFP_FP__)
117   return false;
118 #else
119 #error \
120     "Your version of compiler does not report the FP ABI compiled for."     \
121        "Please report it on this issue"                                        \
122        "http://code.google.com/p/v8/issues/detail?id=2140"
123 
124 #endif
125 #endif
126 #undef GCC_VERSION
127 }
128 
129 #endif // def __arm__
130 
131 }  // namespace base
132 }  // namespace v8
133