Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / v8 / src / atomicops_internals_x86_gcc.cc
1 // Copyright 2010 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 // This module gets enough CPU information to optimize the
6 // atomicops module on x86.
7
8 #include <string.h>
9
10 #include "atomicops.h"
11 #include "platform.h"
12
13 // This file only makes sense with atomicops_internals_x86_gcc.h -- it
14 // depends on structs that are defined in that file.  If atomicops.h
15 // doesn't sub-include that file, then we aren't needed, and shouldn't
16 // try to do anything.
17 #ifdef V8_ATOMICOPS_INTERNALS_X86_GCC_H_
18
19 // Inline cpuid instruction.  In PIC compilations, %ebx contains the address
20 // of the global offset table.  To avoid breaking such executables, this code
21 // must preserve that register's value across cpuid instructions.
22 #if defined(__i386__)
23 #define cpuid(a, b, c, d, inp) \
24   asm("mov %%ebx, %%edi\n"     \
25       "cpuid\n"                \
26       "xchg %%edi, %%ebx\n"    \
27       : "=a" (a), "=D" (b), "=c" (c), "=d" (d) : "a" (inp))
28 #elif defined(__x86_64__)
29 #define cpuid(a, b, c, d, inp) \
30   asm("mov %%rbx, %%rdi\n"     \
31       "cpuid\n"                \
32       "xchg %%rdi, %%rbx\n"    \
33       : "=a" (a), "=D" (b), "=c" (c), "=d" (d) : "a" (inp))
34 #endif
35
36 #if defined(cpuid)        // initialize the struct only on x86
37
38 namespace v8 {
39 namespace internal {
40
41 // Set the flags so that code will run correctly and conservatively, so even
42 // if we haven't been initialized yet, we're probably single threaded, and our
43 // default values should hopefully be pretty safe.
44 struct AtomicOps_x86CPUFeatureStruct AtomicOps_Internalx86CPUFeatures = {
45   false,          // bug can't exist before process spawns multiple threads
46   false,          // no SSE2
47 };
48
49 } }  // namespace v8::internal
50
51 namespace {
52
53 // Initialize the AtomicOps_Internalx86CPUFeatures struct.
54 void AtomicOps_Internalx86CPUFeaturesInit() {
55   using v8::internal::AtomicOps_Internalx86CPUFeatures;
56
57   uint32_t eax = 0;
58   uint32_t ebx = 0;
59   uint32_t ecx = 0;
60   uint32_t edx = 0;
61
62   // Get vendor string (issue CPUID with eax = 0)
63   cpuid(eax, ebx, ecx, edx, 0);
64   char vendor[13];
65   v8::internal::OS::MemCopy(vendor, &ebx, 4);
66   v8::internal::OS::MemCopy(vendor + 4, &edx, 4);
67   v8::internal::OS::MemCopy(vendor + 8, &ecx, 4);
68   vendor[12] = 0;
69
70   // get feature flags in ecx/edx, and family/model in eax
71   cpuid(eax, ebx, ecx, edx, 1);
72
73   int family = (eax >> 8) & 0xf;        // family and model fields
74   int model = (eax >> 4) & 0xf;
75   if (family == 0xf) {                  // use extended family and model fields
76     family += (eax >> 20) & 0xff;
77     model += ((eax >> 16) & 0xf) << 4;
78   }
79
80   // Opteron Rev E has a bug in which on very rare occasions a locked
81   // instruction doesn't act as a read-acquire barrier if followed by a
82   // non-locked read-modify-write instruction.  Rev F has this bug in
83   // pre-release versions, but not in versions released to customers,
84   // so we test only for Rev E, which is family 15, model 32..63 inclusive.
85   if (strcmp(vendor, "AuthenticAMD") == 0 &&       // AMD
86       family == 15 &&
87       32 <= model && model <= 63) {
88     AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug = true;
89   } else {
90     AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug = false;
91   }
92
93   // edx bit 26 is SSE2 which we use to tell use whether we can use mfence
94   AtomicOps_Internalx86CPUFeatures.has_sse2 = ((edx >> 26) & 1);
95 }
96
97 class AtomicOpsx86Initializer {
98  public:
99   AtomicOpsx86Initializer() {
100     AtomicOps_Internalx86CPUFeaturesInit();
101   }
102 };
103
104
105 // A global to get use initialized on startup via static initialization :/
106 AtomicOpsx86Initializer g_initer;
107
108 }  // namespace
109
110 #endif  // if x86
111
112 #endif  // ifdef V8_ATOMICOPS_INTERNALS_X86_GCC_H_