summaryrefslogtreecommitdiffstats
path: root/third_party/rust/raw-cpuid/src/cpuid.c
blob: c014da437418932d12a5155796048a2c721c1a95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdint.h>

void cpuid(uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) {
#ifdef _MSC_VER
  uint32_t regs[4];
  __cpuidex((int *)regs, *eax, *ecx);
  *eax = regs[0], *ebx = regs[1], *ecx = regs[2], *edx = regs[3];
#else
  asm volatile(
#if defined(__i386__) && defined(__PIC__)
      // The reason for this ebx juggling is the -fPIC rust compilation mode.
      // On 32-bit to locate variables it uses a global offset table whose
      // pointer is stored in ebx. Without temporary storing ebx in edi, the
      // compiler will complain about inconsistent operand constraints in an
      // 'asm'.
      // Also note that this is only an issue on older compiler versions.
      "mov %%ebx, %%edi;"
      "cpuid;"
      "xchgl %%ebx, %%edi;"
      :
      "+a" (*eax),
      "=D" (*ebx),
#else
      "cpuid"
      :
      "+a"(*eax),
      "=b"(*ebx),
#endif
      "+c"(*ecx),
      "=d"(*edx));
#endif
}