~ubuntu-branches/ubuntu/jaunty/google-perftools/jaunty

« back to all changes in this revision

Viewing changes to src/base/atomicops-internals-x86.cc

  • Committer: Bazaar Package Importer
  • Author(s): Daigo Moriwaki
  • Date: 2008-06-15 23:41:36 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20080615234136-al5gawvdvt5vhdtz
Tags: 0.98-1
* New upstream release. (Closes: #425147)
* Compiled with GCC 4.3. (Closes: #454841)
* debian/watch: can now report upstream's version (Closes: #450294)
* Because of a file conflict between tau and libgoogle-perftools the
  binary pprof is renamed as google-pprof. (Closes: #404001)
  Great thanks to Michael Mende.
* debian/rules: autoconf files are now generated at the build time.
* Bumped up Standards-Version to 3.7.3, no changes are required.
* Split a new package, libtcmallc_minimal0. The upstream supports
  this module for wider platforms. So I leave its architecture to be
  `any'.
* libgoogle-perftools0's architecture is now i386. The upstream
  supports this module for x86 and x86_64. However, x86_64 requires
  libunwind's development head, which Debian does not have yet.
* Removed an unnecessary patch, debian/patches/02_profiler.cc_alpha.diff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2007, Google Inc.
 
2
 * All rights reserved.
 
3
 * 
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are
 
6
 * met:
 
7
 * 
 
8
 *     * Redistributions of source code must retain the above copyright
 
9
 * notice, this list of conditions and the following disclaimer.
 
10
 *     * Redistributions in binary form must reproduce the above
 
11
 * copyright notice, this list of conditions and the following disclaimer
 
12
 * in the documentation and/or other materials provided with the
 
13
 * distribution.
 
14
 *     * Neither the name of Google Inc. nor the names of its
 
15
 * contributors may be used to endorse or promote products derived from
 
16
 * this software without specific prior written permission.
 
17
 * 
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
21
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
22
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
23
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
24
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
28
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 *
 
30
 * ---
 
31
 * Author: Mike Burrows
 
32
 *
 
33
 * This module gets enough CPU information to optimize the
 
34
 * atomicops module on x86.
 
35
 */
 
36
 
 
37
#include "base/atomicops.h"
 
38
#include "base/basictypes.h"
 
39
#include "base/googleinit.h"
 
40
#include "base/logging.h"
 
41
#include <string.h>
 
42
 
 
43
// This file only makes sense with atomicops-internals-x86.h -- it
 
44
// depends on structs that are defined in that file.  If atomicops.h
 
45
// doesn't sub-include that file, then we aren't needed, and shouldn't
 
46
// try to do anything.
 
47
#ifdef BASE_ATOMICOPS_INTERNALS_X86_H_
 
48
 
 
49
// Inline cpuid instruction.  In PIC compilations, %ebx contains the address
 
50
// of the global offset table.  To avoid breaking such executables, this code
 
51
// must preserve that register's value across cpuid instructions.
 
52
#if defined(__i386__)
 
53
#define cpuid(a, b, c, d, inp) \
 
54
  asm ("mov %%ebx, %%edi\n"    \
 
55
       "cpuid\n"               \
 
56
       "xchg %%edi, %%ebx\n"   \
 
57
       : "=a" (a), "=D" (b), "=c" (c), "=d" (d) : "a" (inp))
 
58
#elif defined (__x86_64__)
 
59
#define cpuid(a, b, c, d, inp) \
 
60
  asm ("mov %%rbx, %%rdi\n"    \
 
61
       "cpuid\n"               \
 
62
       "xchg %%rdi, %%rbx\n"   \
 
63
       : "=a" (a), "=D" (b), "=c" (c), "=d" (d) : "a" (inp))
 
64
#endif
 
65
 
 
66
#if defined(cpuid)        // initialize the struct only on x86
 
67
 
 
68
// Set the flags so that code will run correctly and conservatively
 
69
// until InitGoogle() is called.
 
70
struct AtomicOps_x86CPUFeatureStruct AtomicOps_Internalx86CPUFeatures = {
 
71
  false,          // bug can't exist before process spawns multiple threads
 
72
  false,          // no SSE2
 
73
  false,          // no cmpxchg16b
 
74
};
 
75
 
 
76
// Initialize the AtomicOps_Internalx86CPUFeatures struct.
 
77
static void AtomicOps_Internalx86CPUFeaturesInit() {
 
78
  uint32 eax;
 
79
  uint32 ebx;
 
80
  uint32 ecx;
 
81
  uint32 edx;
 
82
 
 
83
  // Get vendor string (issue CPUID with eax = 0)
 
84
  cpuid(eax, ebx, ecx, edx, 0);
 
85
  char vendor[13];
 
86
  memcpy(vendor, &ebx, 4);
 
87
  memcpy(vendor + 4, &edx, 4);
 
88
  memcpy(vendor + 8, &ecx, 4);
 
89
  vendor[12] = 0;
 
90
 
 
91
  // get feature flags in ecx/edx, and family/model in eax
 
92
  cpuid(eax, ebx, ecx, edx, 1);
 
93
 
 
94
  int family = (eax >> 8) & 0xf;        // family and model fields
 
95
  int model = (eax >> 4) & 0xf;
 
96
  if (family == 0xf) {                  // use extended family and model fields
 
97
    family += (eax >> 20) & 0xff;
 
98
    model += ((eax >> 16) & 0xf) << 4;
 
99
  }
 
100
 
 
101
  // Opteron Rev E has a bug in which on very rare occasions a locked
 
102
  // instruction doesn't act as a read-acquire barrier if followed by a
 
103
  // non-locked read-modify-write instruction.  Rev F has this bug in 
 
104
  // pre-release versions, but not in versions released to customers,
 
105
  // so we test only for Rev E, which is family 15, model 32..63 inclusive.
 
106
  if (strcmp(vendor, "AuthenticAMD") == 0 &&       // AMD
 
107
      family == 15 &&
 
108
      32 <= model && model <= 63) {
 
109
    AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug = true;
 
110
  } else {
 
111
    AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug = false;
 
112
  }
 
113
 
 
114
  // edx bit 26 is SSE2 which we use to tell use whether we can use mfence
 
115
  AtomicOps_Internalx86CPUFeatures.has_sse2 = ((edx >> 26) & 1);
 
116
 
 
117
  // ecx bit 13 indicates whether the cmpxchg16b instruction is supported
 
118
  AtomicOps_Internalx86CPUFeatures.has_cmpxchg16b = ((ecx >> 13) & 1);
 
119
}
 
120
 
 
121
REGISTER_MODULE_INITIALIZER(atomicops_x86, {
 
122
  AtomicOps_Internalx86CPUFeaturesInit();
 
123
});
 
124
 
 
125
#endif
 
126
 
 
127
#endif  /* ifdef BASE_ATOMICOPS_INTERNALS_X86_H_ */