Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | // This module contains the platform-specific code. This make the rest of the |
| 29 | // code less dependent on operating system, compilers and runtime libraries. |
| 30 | // This module does specifically not deal with differences between different |
| 31 | // processor architecture. |
| 32 | // The platform classes have the same definition for all platforms. The |
| 33 | // implementation for a particular platform is put in platform_<os>.cc. |
| 34 | // The build system then uses the implementation for the target platform. |
| 35 | // |
| 36 | // This design has been chosen because it is simple and fast. Alternatively, |
| 37 | // the platform dependent classes could have been implemented using abstract |
| 38 | // superclasses with virtual methods and having specializations for each |
| 39 | // platform. This design was rejected because it was more complicated and |
| 40 | // slower. It would require factory methods for selecting the right |
| 41 | // implementation and the overhead of virtual methods for performance |
| 42 | // sensitive like mutex locking/unlocking. |
| 43 | |
| 44 | #ifndef V8_PLATFORM_H_ |
| 45 | #define V8_PLATFORM_H_ |
| 46 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 47 | #ifdef __sun |
| 48 | # ifndef signbit |
| 49 | int signbit(double x); |
| 50 | # endif |
| 51 | #endif |
| 52 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 53 | // GCC specific stuff |
| 54 | #ifdef __GNUC__ |
| 55 | |
| 56 | // Needed for va_list on at least MinGW and Android. |
| 57 | #include <stdarg.h> |
| 58 | |
| 59 | #define __GNUC_VERSION__ (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) |
| 60 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | #endif // __GNUC__ |
| 62 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 63 | |
| 64 | // Windows specific stuff. |
| 65 | #ifdef WIN32 |
| 66 | |
| 67 | // Microsoft Visual C++ specific stuff. |
| 68 | #ifdef _MSC_VER |
| 69 | |
| 70 | #include "win32-math.h" |
| 71 | |
| 72 | int strncasecmp(const char* s1, const char* s2, int n); |
| 73 | |
| 74 | #endif // _MSC_VER |
| 75 | |
| 76 | // Random is missing on both Visual Studio and MinGW. |
| 77 | int random(); |
| 78 | |
| 79 | #endif // WIN32 |
| 80 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 81 | #include "atomicops.h" |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 82 | #include "platform-tls.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 83 | #include "utils.h" |
| 84 | #include "v8globals.h" |
| 85 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | namespace v8 { |
| 87 | namespace internal { |
| 88 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 89 | // Use AtomicWord for a machine-sized pointer. It is assumed that |
| 90 | // reads and writes of naturally aligned values of this type are atomic. |
| 91 | typedef intptr_t AtomicWord; |
| 92 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 93 | class Semaphore; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 94 | class Mutex; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | |
| 96 | double ceiling(double x); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 97 | double modulo(double x, double y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | |
| 99 | // Forward declarations. |
| 100 | class Socket; |
| 101 | |
| 102 | // ---------------------------------------------------------------------------- |
| 103 | // OS |
| 104 | // |
| 105 | // This class has static methods for the different platform specific |
| 106 | // functions. Add methods here to cope with differences between the |
| 107 | // supported platforms. |
| 108 | |
| 109 | class OS { |
| 110 | public: |
| 111 | // Initializes the platform OS support. Called once at VM startup. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 112 | static void SetUp(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | |
| 114 | // Returns the accumulated user time for thread. This routine |
| 115 | // can be used for profiling. The implementation should |
| 116 | // strive for high-precision timer resolution, preferable |
| 117 | // micro-second resolution. |
| 118 | static int GetUserTime(uint32_t* secs, uint32_t* usecs); |
| 119 | |
| 120 | // Get a tick counter normalized to one tick per microsecond. |
| 121 | // Used for calculating time intervals. |
| 122 | static int64_t Ticks(); |
| 123 | |
| 124 | // Returns current time as the number of milliseconds since |
| 125 | // 00:00:00 UTC, January 1, 1970. |
| 126 | static double TimeCurrentMillis(); |
| 127 | |
| 128 | // Returns a string identifying the current time zone. The |
| 129 | // timestamp is used for determining if DST is in effect. |
| 130 | static const char* LocalTimezone(double time); |
| 131 | |
| 132 | // Returns the local time offset in milliseconds east of UTC without |
| 133 | // taking daylight savings time into account. |
| 134 | static double LocalTimeOffset(); |
| 135 | |
| 136 | // Returns the daylight savings offset for the given time. |
| 137 | static double DaylightSavingsOffset(double time); |
| 138 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 139 | // Returns last OS error. |
| 140 | static int GetLastError(); |
| 141 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 142 | static FILE* FOpen(const char* path, const char* mode); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 143 | static bool Remove(const char* path); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 144 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 145 | // Opens a temporary file, the file is auto removed on close. |
| 146 | static FILE* OpenTemporaryFile(); |
| 147 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 148 | // Log file open mode is platform-dependent due to line ends issues. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 149 | static const char* const LogFileOpenMode; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 150 | |
| 151 | // Print output to console. This is mostly used for debugging output. |
| 152 | // On platforms that has standard terminal output, the output |
| 153 | // should go to stdout. |
| 154 | static void Print(const char* format, ...); |
| 155 | static void VPrint(const char* format, va_list args); |
| 156 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 157 | // Print output to a file. This is mostly used for debugging output. |
| 158 | static void FPrint(FILE* out, const char* format, ...); |
| 159 | static void VFPrint(FILE* out, const char* format, va_list args); |
| 160 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | // Print error output to console. This is mostly used for error message |
| 162 | // output. On platforms that has standard terminal output, the output |
| 163 | // should go to stderr. |
| 164 | static void PrintError(const char* format, ...); |
| 165 | static void VPrintError(const char* format, va_list args); |
| 166 | |
| 167 | // Allocate/Free memory used by JS heap. Pages are readable/writable, but |
| 168 | // they are not guaranteed to be executable unless 'executable' is true. |
| 169 | // Returns the address of allocated memory, or NULL if failed. |
| 170 | static void* Allocate(const size_t requested, |
| 171 | size_t* allocated, |
| 172 | bool is_executable); |
| 173 | static void Free(void* address, const size_t size); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 174 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 175 | // This is the granularity at which the ProtectCode(...) call can set page |
| 176 | // permissions. |
| 177 | static intptr_t CommitPageSize(); |
| 178 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 179 | // Mark code segments non-writable. |
| 180 | static void ProtectCode(void* address, const size_t size); |
| 181 | |
| 182 | // Assign memory as a guard page so that access will cause an exception. |
| 183 | static void Guard(void* address, const size_t size); |
| 184 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 185 | // Generate a random address to be used for hinting mmap(). |
| 186 | static void* GetRandomMmapAddr(); |
| 187 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 188 | // Get the Alignment guaranteed by Allocate(). |
| 189 | static size_t AllocateAlignment(); |
| 190 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 191 | // Returns an indication of whether a pointer is in a space that |
| 192 | // has been allocated by Allocate(). This method may conservatively |
| 193 | // always return false, but giving more accurate information may |
| 194 | // improve the robustness of the stack dump code in the presence of |
| 195 | // heap corruption. |
| 196 | static bool IsOutsideAllocatedSpace(void* pointer); |
| 197 | |
| 198 | // Sleep for a number of milliseconds. |
| 199 | static void Sleep(const int milliseconds); |
| 200 | |
| 201 | // Abort the current process. |
| 202 | static void Abort(); |
| 203 | |
| 204 | // Debug break. |
| 205 | static void DebugBreak(); |
| 206 | |
| 207 | // Walk the stack. |
| 208 | static const int kStackWalkError = -1; |
| 209 | static const int kStackWalkMaxNameLen = 256; |
| 210 | static const int kStackWalkMaxTextLen = 256; |
| 211 | struct StackFrame { |
| 212 | void* address; |
| 213 | char text[kStackWalkMaxTextLen]; |
| 214 | }; |
| 215 | |
| 216 | static int StackWalk(Vector<StackFrame> frames); |
| 217 | |
| 218 | // Factory method for creating platform dependent Mutex. |
| 219 | // Please use delete to reclaim the storage for the returned Mutex. |
| 220 | static Mutex* CreateMutex(); |
| 221 | |
| 222 | // Factory method for creating platform dependent Semaphore. |
| 223 | // Please use delete to reclaim the storage for the returned Semaphore. |
| 224 | static Semaphore* CreateSemaphore(int count); |
| 225 | |
| 226 | // Factory method for creating platform dependent Socket. |
| 227 | // Please use delete to reclaim the storage for the returned Socket. |
| 228 | static Socket* CreateSocket(); |
| 229 | |
| 230 | class MemoryMappedFile { |
| 231 | public: |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 232 | static MemoryMappedFile* open(const char* name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 233 | static MemoryMappedFile* create(const char* name, int size, void* initial); |
| 234 | virtual ~MemoryMappedFile() { } |
| 235 | virtual void* memory() = 0; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 236 | virtual int size() = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | // Safe formatting print. Ensures that str is always null-terminated. |
| 240 | // Returns the number of chars written, or -1 if output was truncated. |
| 241 | static int SNPrintF(Vector<char> str, const char* format, ...); |
| 242 | static int VSNPrintF(Vector<char> str, |
| 243 | const char* format, |
| 244 | va_list args); |
| 245 | |
| 246 | static char* StrChr(char* str, int c); |
| 247 | static void StrNCpy(Vector<char> dest, const char* src, size_t n); |
| 248 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 249 | // Support for the profiler. Can do nothing, in which case ticks |
| 250 | // occuring in shared libraries will not be properly accounted for. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 251 | static void LogSharedLibraryAddresses(); |
| 252 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 253 | // Support for the profiler. Notifies the external profiling |
| 254 | // process that a code moving garbage collection starts. Can do |
| 255 | // nothing, in which case the code objects must not move (e.g., by |
| 256 | // using --never-compact) if accurate profiling is desired. |
| 257 | static void SignalCodeMovingGC(); |
| 258 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 259 | // The return value indicates the CPU features we are sure of because of the |
| 260 | // OS. For example MacOSX doesn't run on any x86 CPUs that don't have SSE2 |
| 261 | // instructions. |
| 262 | // This is a little messy because the interpretation is subject to the cross |
| 263 | // of the CPU and the OS. The bits in the answer correspond to the bit |
| 264 | // positions indicated by the members of the CpuFeature enum from globals.h |
| 265 | static uint64_t CpuFeaturesImpliedByPlatform(); |
| 266 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 267 | // Maximum size of the virtual memory. 0 means there is no artificial |
| 268 | // limit. |
| 269 | static intptr_t MaxVirtualMemory(); |
| 270 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 271 | // Returns the double constant NAN |
| 272 | static double nan_value(); |
| 273 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 274 | // Support runtime detection of VFP3 on ARM CPUs. |
| 275 | static bool ArmCpuHasFeature(CpuFeature feature); |
| 276 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 277 | // Support runtime detection of whether the hard float option of the |
| 278 | // EABI is used. |
| 279 | static bool ArmUsingHardFloat(); |
| 280 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 281 | // Support runtime detection of FPU on MIPS CPUs. |
| 282 | static bool MipsCpuHasFeature(CpuFeature feature); |
| 283 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 284 | // Returns the activation frame alignment constraint or zero if |
| 285 | // the platform doesn't care. Guaranteed to be a power of two. |
| 286 | static int ActivationFrameAlignment(); |
| 287 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 288 | static void ReleaseStore(volatile AtomicWord* ptr, AtomicWord value); |
| 289 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 290 | #if defined(V8_TARGET_ARCH_IA32) |
| 291 | // Copy memory area to disjoint memory area. |
| 292 | static void MemCopy(void* dest, const void* src, size_t size); |
| 293 | // Limit below which the extra overhead of the MemCopy function is likely |
| 294 | // to outweigh the benefits of faster copying. |
| 295 | static const int kMinComplexMemCopy = 64; |
| 296 | typedef void (*MemCopyFunction)(void* dest, const void* src, size_t size); |
| 297 | |
| 298 | #else // V8_TARGET_ARCH_IA32 |
| 299 | static void MemCopy(void* dest, const void* src, size_t size) { |
| 300 | memcpy(dest, src, size); |
| 301 | } |
| 302 | static const int kMinComplexMemCopy = 256; |
| 303 | #endif // V8_TARGET_ARCH_IA32 |
| 304 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 305 | private: |
| 306 | static const int msPerSecond = 1000; |
| 307 | |
| 308 | DISALLOW_IMPLICIT_CONSTRUCTORS(OS); |
| 309 | }; |
| 310 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 311 | // Represents and controls an area of reserved memory. |
| 312 | // Control of the reserved memory can be assigned to another VirtualMemory |
| 313 | // object by assignment or copy-contructing. This removes the reserved memory |
| 314 | // from the original object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 315 | class VirtualMemory { |
| 316 | public: |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 317 | // Empty VirtualMemory object, controlling no reserved memory. |
| 318 | VirtualMemory(); |
| 319 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 320 | // Reserves virtual memory with size. |
| 321 | explicit VirtualMemory(size_t size); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 322 | |
| 323 | // Reserves virtual memory containing an area of the given size that |
| 324 | // is aligned per alignment. This may not be at the position returned |
| 325 | // by address(). |
| 326 | VirtualMemory(size_t size, size_t alignment); |
| 327 | |
| 328 | // Releases the reserved memory, if any, controlled by this VirtualMemory |
| 329 | // object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 330 | ~VirtualMemory(); |
| 331 | |
| 332 | // Returns whether the memory has been reserved. |
| 333 | bool IsReserved(); |
| 334 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 335 | // Initialize or resets an embedded VirtualMemory object. |
| 336 | void Reset(); |
| 337 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 338 | // Returns the start address of the reserved memory. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 339 | // If the memory was reserved with an alignment, this address is not |
| 340 | // necessarily aligned. The user might need to round it up to a multiple of |
| 341 | // the alignment to get the start of the aligned block. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 342 | void* address() { |
| 343 | ASSERT(IsReserved()); |
| 344 | return address_; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 345 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 346 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 347 | // Returns the size of the reserved memory. The returned value is only |
| 348 | // meaningful when IsReserved() returns true. |
| 349 | // If the memory was reserved with an alignment, this size may be larger |
| 350 | // than the requested size. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 351 | size_t size() { return size_; } |
| 352 | |
| 353 | // Commits real memory. Returns whether the operation succeeded. |
| 354 | bool Commit(void* address, size_t size, bool is_executable); |
| 355 | |
| 356 | // Uncommit real memory. Returns whether the operation succeeded. |
| 357 | bool Uncommit(void* address, size_t size); |
| 358 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 359 | // Creates a single guard page at the given address. |
| 360 | bool Guard(void* address); |
| 361 | |
| 362 | void Release() { |
| 363 | ASSERT(IsReserved()); |
| 364 | // Notice: Order is important here. The VirtualMemory object might live |
| 365 | // inside the allocated region. |
| 366 | void* address = address_; |
| 367 | size_t size = size_; |
| 368 | Reset(); |
| 369 | bool result = ReleaseRegion(address, size); |
| 370 | USE(result); |
| 371 | ASSERT(result); |
| 372 | } |
| 373 | |
| 374 | // Assign control of the reserved region to a different VirtualMemory object. |
| 375 | // The old object is no longer functional (IsReserved() returns false). |
| 376 | void TakeControl(VirtualMemory* from) { |
| 377 | ASSERT(!IsReserved()); |
| 378 | address_ = from->address_; |
| 379 | size_ = from->size_; |
| 380 | from->Reset(); |
| 381 | } |
| 382 | |
| 383 | static void* ReserveRegion(size_t size); |
| 384 | |
| 385 | static bool CommitRegion(void* base, size_t size, bool is_executable); |
| 386 | |
| 387 | static bool UncommitRegion(void* base, size_t size); |
| 388 | |
| 389 | // Must be called with a base pointer that has been returned by ReserveRegion |
| 390 | // and the same size it was reserved with. |
| 391 | static bool ReleaseRegion(void* base, size_t size); |
| 392 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 393 | private: |
| 394 | void* address_; // Start address of the virtual memory. |
| 395 | size_t size_; // Size of the virtual memory. |
| 396 | }; |
| 397 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 398 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 399 | // ---------------------------------------------------------------------------- |
| 400 | // Thread |
| 401 | // |
| 402 | // Thread objects are used for creating and running threads. When the start() |
| 403 | // method is called the new thread starts running the run() method in the new |
| 404 | // thread. The Thread object should not be deallocated before the thread has |
| 405 | // terminated. |
| 406 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 407 | class Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 408 | public: |
| 409 | // Opaque data type for thread-local storage keys. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 410 | // LOCAL_STORAGE_KEY_MIN_VALUE and LOCAL_STORAGE_KEY_MAX_VALUE are specified |
| 411 | // to ensure that enumeration type has correct value range (see Issue 830 for |
| 412 | // more details). |
| 413 | enum LocalStorageKey { |
| 414 | LOCAL_STORAGE_KEY_MIN_VALUE = kMinInt, |
| 415 | LOCAL_STORAGE_KEY_MAX_VALUE = kMaxInt |
| 416 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 417 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 418 | class Options { |
| 419 | public: |
| 420 | Options() : name_("v8:<unknown>"), stack_size_(0) {} |
| 421 | Options(const char* name, int stack_size = 0) |
| 422 | : name_(name), stack_size_(stack_size) {} |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 423 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 424 | const char* name() const { return name_; } |
| 425 | int stack_size() const { return stack_size_; } |
| 426 | |
| 427 | private: |
| 428 | const char* name_; |
| 429 | int stack_size_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 430 | }; |
| 431 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 432 | // Create new thread. |
| 433 | explicit Thread(const Options& options); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 434 | virtual ~Thread(); |
| 435 | |
| 436 | // Start new thread by calling the Run() method in the new thread. |
| 437 | void Start(); |
| 438 | |
| 439 | // Wait until thread terminates. |
| 440 | void Join(); |
| 441 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 442 | inline const char* name() const { |
| 443 | return name_; |
| 444 | } |
| 445 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 446 | // Abstract method for run handler. |
| 447 | virtual void Run() = 0; |
| 448 | |
| 449 | // Thread-local storage. |
| 450 | static LocalStorageKey CreateThreadLocalKey(); |
| 451 | static void DeleteThreadLocalKey(LocalStorageKey key); |
| 452 | static void* GetThreadLocal(LocalStorageKey key); |
| 453 | static int GetThreadLocalInt(LocalStorageKey key) { |
| 454 | return static_cast<int>(reinterpret_cast<intptr_t>(GetThreadLocal(key))); |
| 455 | } |
| 456 | static void SetThreadLocal(LocalStorageKey key, void* value); |
| 457 | static void SetThreadLocalInt(LocalStorageKey key, int value) { |
| 458 | SetThreadLocal(key, reinterpret_cast<void*>(static_cast<intptr_t>(value))); |
| 459 | } |
| 460 | static bool HasThreadLocal(LocalStorageKey key) { |
| 461 | return GetThreadLocal(key) != NULL; |
| 462 | } |
| 463 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 464 | #ifdef V8_FAST_TLS_SUPPORTED |
| 465 | static inline void* GetExistingThreadLocal(LocalStorageKey key) { |
| 466 | void* result = reinterpret_cast<void*>( |
| 467 | InternalGetExistingThreadLocal(static_cast<intptr_t>(key))); |
| 468 | ASSERT(result == GetThreadLocal(key)); |
| 469 | return result; |
| 470 | } |
| 471 | #else |
| 472 | static inline void* GetExistingThreadLocal(LocalStorageKey key) { |
| 473 | return GetThreadLocal(key); |
| 474 | } |
| 475 | #endif |
| 476 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 477 | // A hint to the scheduler to let another thread run. |
| 478 | static void YieldCPU(); |
| 479 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 480 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 481 | // The thread name length is limited to 16 based on Linux's implementation of |
| 482 | // prctl(). |
| 483 | static const int kMaxThreadNameLength = 16; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 484 | |
| 485 | class PlatformData; |
| 486 | PlatformData* data() { return data_; } |
| 487 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 488 | private: |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 489 | void set_name(const char* name); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 490 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 491 | PlatformData* data_; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 492 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 493 | char name_[kMaxThreadNameLength]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 494 | int stack_size_; |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 495 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 496 | DISALLOW_COPY_AND_ASSIGN(Thread); |
| 497 | }; |
| 498 | |
| 499 | |
| 500 | // ---------------------------------------------------------------------------- |
| 501 | // Mutex |
| 502 | // |
| 503 | // Mutexes are used for serializing access to non-reentrant sections of code. |
| 504 | // The implementations of mutex should allow for nested/recursive locking. |
| 505 | |
| 506 | class Mutex { |
| 507 | public: |
| 508 | virtual ~Mutex() {} |
| 509 | |
| 510 | // Locks the given mutex. If the mutex is currently unlocked, it becomes |
| 511 | // locked and owned by the calling thread, and immediately. If the mutex |
| 512 | // is already locked by another thread, suspends the calling thread until |
| 513 | // the mutex is unlocked. |
| 514 | virtual int Lock() = 0; |
| 515 | |
| 516 | // Unlocks the given mutex. The mutex is assumed to be locked and owned by |
| 517 | // the calling thread on entrance. |
| 518 | virtual int Unlock() = 0; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 519 | |
| 520 | // Tries to lock the given mutex. Returns whether the mutex was |
| 521 | // successfully locked. |
| 522 | virtual bool TryLock() = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 523 | }; |
| 524 | |
| 525 | |
| 526 | // ---------------------------------------------------------------------------- |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 527 | // ScopedLock |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 528 | // |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 529 | // Stack-allocated ScopedLocks provide block-scoped locking and |
| 530 | // unlocking of a mutex. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 531 | class ScopedLock { |
| 532 | public: |
| 533 | explicit ScopedLock(Mutex* mutex): mutex_(mutex) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 534 | ASSERT(mutex_ != NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 535 | mutex_->Lock(); |
| 536 | } |
| 537 | ~ScopedLock() { |
| 538 | mutex_->Unlock(); |
| 539 | } |
| 540 | |
| 541 | private: |
| 542 | Mutex* mutex_; |
| 543 | DISALLOW_COPY_AND_ASSIGN(ScopedLock); |
| 544 | }; |
| 545 | |
| 546 | |
| 547 | // ---------------------------------------------------------------------------- |
| 548 | // Semaphore |
| 549 | // |
| 550 | // A semaphore object is a synchronization object that maintains a count. The |
| 551 | // count is decremented each time a thread completes a wait for the semaphore |
| 552 | // object and incremented each time a thread signals the semaphore. When the |
| 553 | // count reaches zero, threads waiting for the semaphore blocks until the |
| 554 | // count becomes non-zero. |
| 555 | |
| 556 | class Semaphore { |
| 557 | public: |
| 558 | virtual ~Semaphore() {} |
| 559 | |
| 560 | // Suspends the calling thread until the semaphore counter is non zero |
| 561 | // and then decrements the semaphore counter. |
| 562 | virtual void Wait() = 0; |
| 563 | |
| 564 | // Suspends the calling thread until the counter is non zero or the timeout |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 565 | // time has passed. If timeout happens the return value is false and the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 566 | // counter is unchanged. Otherwise the semaphore counter is decremented and |
| 567 | // true is returned. The timeout value is specified in microseconds. |
| 568 | virtual bool Wait(int timeout) = 0; |
| 569 | |
| 570 | // Increments the semaphore counter. |
| 571 | virtual void Signal() = 0; |
| 572 | }; |
| 573 | |
| 574 | |
| 575 | // ---------------------------------------------------------------------------- |
| 576 | // Socket |
| 577 | // |
| 578 | |
| 579 | class Socket { |
| 580 | public: |
| 581 | virtual ~Socket() {} |
| 582 | |
| 583 | // Server initialization. |
| 584 | virtual bool Bind(const int port) = 0; |
| 585 | virtual bool Listen(int backlog) const = 0; |
| 586 | virtual Socket* Accept() const = 0; |
| 587 | |
| 588 | // Client initialization. |
| 589 | virtual bool Connect(const char* host, const char* port) = 0; |
| 590 | |
| 591 | // Shutdown socket for both read and write. This causes blocking Send and |
| 592 | // Receive calls to exit. After Shutdown the Socket object cannot be used for |
| 593 | // any communication. |
| 594 | virtual bool Shutdown() = 0; |
| 595 | |
| 596 | // Data Transimission |
| 597 | virtual int Send(const char* data, int len) const = 0; |
| 598 | virtual int Receive(char* data, int len) const = 0; |
| 599 | |
| 600 | // Set the value of the SO_REUSEADDR socket option. |
| 601 | virtual bool SetReuseAddress(bool reuse_address) = 0; |
| 602 | |
| 603 | virtual bool IsValid() const = 0; |
| 604 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 605 | static bool SetUp(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 606 | static int LastError(); |
| 607 | static uint16_t HToN(uint16_t value); |
| 608 | static uint16_t NToH(uint16_t value); |
| 609 | static uint32_t HToN(uint32_t value); |
| 610 | static uint32_t NToH(uint32_t value); |
| 611 | }; |
| 612 | |
| 613 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 614 | // ---------------------------------------------------------------------------- |
| 615 | // Sampler |
| 616 | // |
| 617 | // A sampler periodically samples the state of the VM and optionally |
| 618 | // (if used for profiling) the program counter and stack pointer for |
| 619 | // the thread that created it. |
| 620 | |
| 621 | // TickSample captures the information collected for each sample. |
| 622 | class TickSample { |
| 623 | public: |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 624 | TickSample() |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 625 | : state(OTHER), |
| 626 | pc(NULL), |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 627 | sp(NULL), |
| 628 | fp(NULL), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 629 | tos(NULL), |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 630 | frames_count(0), |
| 631 | has_external_callback(false) {} |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 632 | StateTag state; // The state of the VM. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 633 | Address pc; // Instruction pointer. |
| 634 | Address sp; // Stack pointer. |
| 635 | Address fp; // Frame pointer. |
| 636 | union { |
| 637 | Address tos; // Top stack value (*sp). |
| 638 | Address external_callback; |
| 639 | }; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 640 | static const int kMaxFramesCount = 64; |
| 641 | Address stack[kMaxFramesCount]; // Call stack. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 642 | int frames_count : 8; // Number of captured frames. |
| 643 | bool has_external_callback : 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 644 | }; |
| 645 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 646 | class Sampler { |
| 647 | public: |
| 648 | // Initialize sampler. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 649 | Sampler(Isolate* isolate, int interval); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 650 | virtual ~Sampler(); |
| 651 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 652 | int interval() const { return interval_; } |
| 653 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 654 | // Performs stack sampling. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 655 | void SampleStack(TickSample* sample) { |
| 656 | DoSampleStack(sample); |
| 657 | IncSamplesTaken(); |
| 658 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 659 | |
| 660 | // This method is called for each sampling period with the current |
| 661 | // program counter. |
| 662 | virtual void Tick(TickSample* sample) = 0; |
| 663 | |
| 664 | // Start and stop sampler. |
| 665 | void Start(); |
| 666 | void Stop(); |
| 667 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 668 | // Is the sampler used for profiling? |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 669 | bool IsProfiling() const { return NoBarrier_Load(&profiling_) > 0; } |
| 670 | void IncreaseProfilingDepth() { NoBarrier_AtomicIncrement(&profiling_, 1); } |
| 671 | void DecreaseProfilingDepth() { NoBarrier_AtomicIncrement(&profiling_, -1); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 672 | |
| 673 | // Whether the sampler is running (that is, consumes resources). |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 674 | bool IsActive() const { return NoBarrier_Load(&active_); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 675 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 676 | Isolate* isolate() { return isolate_; } |
| 677 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 678 | // Used in tests to make sure that stack sampling is performed. |
| 679 | int samples_taken() const { return samples_taken_; } |
| 680 | void ResetSamplesTaken() { samples_taken_ = 0; } |
| 681 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 682 | class PlatformData; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 683 | PlatformData* data() { return data_; } |
| 684 | |
| 685 | PlatformData* platform_data() { return data_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 686 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 687 | protected: |
| 688 | virtual void DoSampleStack(TickSample* sample) = 0; |
| 689 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 690 | private: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 691 | void SetActive(bool value) { NoBarrier_Store(&active_, value); } |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 692 | void IncSamplesTaken() { if (++samples_taken_ < 0) samples_taken_ = 0; } |
| 693 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 694 | Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 695 | const int interval_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 696 | Atomic32 profiling_; |
| 697 | Atomic32 active_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 698 | PlatformData* data_; // Platform specific data. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 699 | int samples_taken_; // Counts stack samples taken. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 700 | DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); |
| 701 | }; |
| 702 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 703 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 704 | } } // namespace v8::internal |
| 705 | |
| 706 | #endif // V8_PLATFORM_H_ |