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 Linux 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 <stdio.h>
12 #include <stdlib.h>
13 #include <sys/prctl.h>
14 #include <sys/resource.h>
15 #include <sys/syscall.h>
16 #include <sys/time.h>
17 
18 // Ubuntu Dapper requires memory pages to be marked as
19 // executable. Otherwise, OS raises an exception when executing code
20 // in that page.
21 #include <errno.h>
22 #include <fcntl.h>  // open
23 #include <stdarg.h>
24 #include <strings.h>    // index
25 #include <sys/mman.h>   // mmap & munmap
26 #include <sys/stat.h>   // open
27 #include <sys/types.h>  // mmap & munmap
28 #include <unistd.h>     // sysconf
29 
30 // GLibc on ARM defines mcontext_t has a typedef for 'struct sigcontext'.
31 // Old versions of the C library <signal.h> didn't define the type.
32 #if defined(__ANDROID__) && !defined(__BIONIC_HAVE_UCONTEXT_T) && \
33     (defined(__arm__) || defined(__aarch64__)) &&                 \
34     !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT)
35 #include <asm/sigcontext.h>  // NOLINT
36 #endif
37 
38 #include <cmath>
39 
40 #undef MAP_TYPE
41 
42 #include "src/base/macros.h"
43 #include "src/base/platform/platform-posix-time.h"
44 #include "src/base/platform/platform-posix.h"
45 #include "src/base/platform/platform.h"
46 
47 namespace v8 {
48 namespace base {
49 
50 #ifdef __arm__
51 
ArmUsingHardFloat()52 bool OS::ArmUsingHardFloat() {
53 // GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify
54 // the Floating Point ABI used (PCS stands for Procedure Call Standard).
55 // We use these as well as a couple of other defines to statically determine
56 // what FP ABI used.
57 // GCC versions 4.4 and below don't support hard-fp.
58 // GCC versions 4.5 may support hard-fp without defining __ARM_PCS or
59 // __ARM_PCS_VFP.
60 
61 #define GCC_VERSION \
62   (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
63 #if GCC_VERSION >= 40600 && !defined(__clang__)
64 #if defined(__ARM_PCS_VFP)
65   return true;
66 #else
67   return false;
68 #endif
69 
70 #elif GCC_VERSION < 40500 && !defined(__clang__)
71   return false;
72 
73 #else
74 #if defined(__ARM_PCS_VFP)
75   return true;
76 #elif defined(__ARM_PCS) || defined(__SOFTFP__) || defined(__SOFTFP) || \
77     !defined(__VFP_FP__)
78   return false;
79 #else
80 #error \
81     "Your version of compiler does not report the FP ABI compiled for."     \
82        "Please report it on this issue"                                        \
83        "http://code.google.com/p/v8/issues/detail?id=2140"
84 
85 #endif
86 #endif
87 #undef GCC_VERSION
88 }
89 
90 #endif  // def __arm__
91 
CreateTimezoneCache()92 TimezoneCache* OS::CreateTimezoneCache() {
93   return new PosixDefaultTimezoneCache();
94 }
95 
GetSharedLibraryAddresses()96 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
97   std::vector<SharedLibraryAddress> result;
98   // This function assumes that the layout of the file is as follows:
99   // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
100   // If we encounter an unexpected situation we abort scanning further entries.
101   FILE* fp = fopen("/proc/self/maps", "r");
102   if (fp == nullptr) return result;
103 
104   // Allocate enough room to be able to store a full file name.
105   const int kLibNameLen = FILENAME_MAX + 1;
106   char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
107 
108   // This loop will terminate once the scanning hits an EOF.
109   while (true) {
110     uintptr_t start, end, offset;
111     char attr_r, attr_w, attr_x, attr_p;
112     // Parse the addresses and permission bits at the beginning of the line.
113     if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
114     if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
115     if (fscanf(fp, "%" V8PRIxPTR, &offset) != 1) break;
116 
117     // Adjust {start} based on {offset}.
118     start -= offset;
119 
120     int c;
121     if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
122       // Found a read-only executable entry. Skip characters until we reach
123       // the beginning of the filename or the end of the line.
124       do {
125         c = getc(fp);
126       } while ((c != EOF) && (c != '\n') && (c != '/') && (c != '['));
127       if (c == EOF) break;  // EOF: Was unexpected, just exit.
128 
129       // Process the filename if found.
130       if ((c == '/') || (c == '[')) {
131         // Push the '/' or '[' back into the stream to be read below.
132         ungetc(c, fp);
133 
134         // Read to the end of the line. Exit if the read fails.
135         if (fgets(lib_name, kLibNameLen, fp) == nullptr) break;
136 
137         // Drop the newline character read by fgets. We do not need to check
138         // for a zero-length string because we know that we at least read the
139         // '/' or '[' character.
140         lib_name[strlen(lib_name) - 1] = '\0';
141       } else {
142         // No library name found, just record the raw address range.
143         snprintf(lib_name, kLibNameLen, "%08" V8PRIxPTR "-%08" V8PRIxPTR, start,
144                  end);
145       }
146       result.push_back(SharedLibraryAddress(lib_name, start, end));
147     } else {
148       // Entry not describing executable data. Skip to end of line to set up
149       // reading the next entry.
150       do {
151         c = getc(fp);
152       } while ((c != EOF) && (c != '\n'));
153       if (c == EOF) break;
154     }
155   }
156   free(lib_name);
157   fclose(fp);
158   return result;
159 }
160 
SignalCodeMovingGC()161 void OS::SignalCodeMovingGC() {
162   // Support for ll_prof.py.
163   //
164   // The Linux profiler built into the kernel logs all mmap's with
165   // PROT_EXEC so that analysis tools can properly attribute ticks. We
166   // do a mmap with a name known by ll_prof.py and immediately munmap
167   // it. This injects a GC marker into the stream of events generated
168   // by the kernel and allows us to synchronize V8 code log and the
169   // kernel log.
170   long size = sysconf(_SC_PAGESIZE);  // NOLINT(runtime/int)
171   FILE* f = fopen(OS::GetGCFakeMMapFile(), "w+");
172   if (f == nullptr) {
173     OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile());
174     OS::Abort();
175   }
176   void* addr = mmap(OS::GetRandomMmapAddr(), size, PROT_READ | PROT_EXEC,
177                     MAP_PRIVATE, fileno(f), 0);
178   DCHECK_NE(MAP_FAILED, addr);
179   CHECK(Free(addr, size));
180   fclose(f);
181 }
182 
183 }  // namespace base
184 }  // namespace v8
185