iPXE
Macros | Functions
cpuid.c File Reference

x86 CPU feature detection More...

#include <string.h>
#include <errno.h>
#include <ipxe/cpuid.h>

Go to the source code of this file.

Macros

#define colour   0x861d
 Colour for debug messages. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
 FILE_SECBOOT (PERMITTED)
 
static int cpuid_instruction_supported (void)
 Check whether or not CPUID instruction is supported. More...
 
int cpuid_supported (uint32_t function)
 Check whether or not CPUID function is supported. More...
 
static void x86_intel_features (struct x86_features *features)
 Get Intel-defined x86 CPU features. More...
 
static void x86_amd_features (struct x86_features *features)
 Get AMD-defined x86 CPU features. More...
 
void x86_features (struct x86_features *features)
 Get x86 CPU features. More...
 

Detailed Description

x86 CPU feature detection

Definition in file cpuid.c.

Macro Definition Documentation

◆ colour

#define colour   0x861d

Colour for debug messages.

Definition at line 38 of file cpuid.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED  )

◆ cpuid_instruction_supported()

static int cpuid_instruction_supported ( void  )
static

Check whether or not CPUID instruction is supported.

Return values
rcReturn status code

Definition at line 45 of file cpuid.c.

45  {
46  unsigned long original;
47  unsigned long inverted;
48 
49  /* Check for instruction existence via flag modifiability */
50  __asm__ ( "pushf\n\t"
51  "pushf\n\t"
52  "pop %0\n\t"
53  "mov %0,%1\n\t"
54  "xor %2,%1\n\t"
55  "push %1\n\t"
56  "popf\n\t"
57  "pushf\n\t"
58  "pop %1\n\t"
59  "popf\n\t"
60  : "=&r" ( original ), "=&r" ( inverted )
61  : "ir" ( CPUID_FLAG ) );
62  if ( ! ( ( original ^ inverted ) & CPUID_FLAG ) ) {
63  DBGC ( colour, "CPUID instruction is not supported\n" );
64  return -ENOTSUP;
65  }
66 
67  return 0;
68 }
#define colour
Colour for debug messages.
Definition: cpuid.c:38
#define DBGC(...)
Definition: compiler.h:505
#define ENOTSUP
Operation not supported.
Definition: errno.h:590
#define CPUID_FLAG
CPUID support flag.
Definition: cpuid.h:32
union interface::@635 original
Original interface properties.
__asm__(".section \".rodata\", \"a\", " PROGBITS "\n\t" "\nprivate_key_data:\n\t" ".size private_key_data, ( . - private_key_data )\n\t" ".equ private_key_len, ( . - private_key_data )\n\t" ".previous\n\t")

References __asm__(), colour, CPUID_FLAG, DBGC, and ENOTSUP.

Referenced by cpuid_supported().

◆ cpuid_supported()

int cpuid_supported ( uint32_t  function)

Check whether or not CPUID function is supported.

Parameters
functionCPUID function
Return values
rcReturn status code

Definition at line 76 of file cpuid.c.

76  {
77  uint32_t max_function;
78  uint32_t discard_b;
80  uint32_t discard_d;
81  int rc;
82 
83  /* Check that CPUID instruction is available */
84  if ( ( rc = cpuid_instruction_supported() ) != 0 )
85  return rc;
86 
87  /* Find highest supported function number within this family */
88  cpuid ( ( function & ( CPUID_EXTENDED | CPUID_HYPERVISOR ) ), 0,
89  &max_function, &discard_b, &discard_c, &discard_d );
90 
91  /* Fail if maximum function number is meaningless (e.g. if we
92  * are attempting to call an extended function on a CPU which
93  * does not support them).
94  */
95  if ( ( max_function & CPUID_AMD_CHECK_MASK ) !=
96  ( function & CPUID_AMD_CHECK_MASK ) ) {
97  DBGC ( colour, "CPUID invalid maximum function %#08x\n",
98  max_function );
99  return -EINVAL;
100  }
101 
102  /* Fail if this function is not supported */
103  if ( function > max_function ) {
104  DBGC ( colour, "CPUID function %#08x not supported\n",
105  function );
106  return -ENOTTY;
107  }
108 
109  return 0;
110 }
#define EINVAL
Invalid argument.
Definition: errno.h:429
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define colour
Colour for debug messages.
Definition: cpuid.c:38
#define CPUID_HYPERVISOR
CPUID hypervisor function.
Definition: cpuid.h:38
#define CPUID_AMD_CHECK_MASK
Extended function existence check mask.
Definition: cpuid.h:65
#define DBGC(...)
Definition: compiler.h:505
static int cpuid_instruction_supported(void)
Check whether or not CPUID instruction is supported.
Definition: cpuid.c:45
unsigned int uint32_t
Definition: stdint.h:12
#define ENOTTY
Inappropriate I/O control operation.
Definition: errno.h:595
#define CPUID_EXTENDED
CPUID extended function.
Definition: cpuid.h:35
long discard_c
Definition: bigint.h:33

References colour, CPUID_AMD_CHECK_MASK, CPUID_EXTENDED, CPUID_HYPERVISOR, cpuid_instruction_supported(), DBGC, discard_c, EINVAL, ENOTTY, and rc.

Referenced by cpuid_settings_fetch(), rdtsc_probe(), x86_amd_features(), and x86_intel_features().

◆ x86_intel_features()

static void x86_intel_features ( struct x86_features features)
static

Get Intel-defined x86 CPU features.

Parameters
featuresx86 CPU features to fill in

Definition at line 117 of file cpuid.c.

117  {
118  uint32_t discard_a;
119  uint32_t discard_b;
120  int rc;
121 
122  /* Check that features are available via CPUID */
123  if ( ( rc = cpuid_supported ( CPUID_FEATURES ) ) != 0 ) {
124  DBGC ( features, "CPUID has no Intel-defined features\n" );
125  return;
126  }
127 
128  /* Get features */
129  cpuid ( CPUID_FEATURES, 0, &discard_a, &discard_b,
130  &features->intel.ecx, &features->intel.edx );
131  DBGC ( features, "CPUID Intel features: %%ecx=%08x, %%edx=%08x\n",
132  features->intel.ecx, features->intel.edx );
133 
134 }
int cpuid_supported(uint32_t function)
Check whether or not CPUID function is supported.
Definition: cpuid.c:76
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define CPUID_FEATURES
Get standard features.
Definition: cpuid.h:44
#define DBGC(...)
Definition: compiler.h:505
uint32_t features
Supported features.
Definition: ena.h:16
unsigned int uint32_t
Definition: stdint.h:12

References CPUID_FEATURES, cpuid_supported(), DBGC, features, and rc.

Referenced by x86_features().

◆ x86_amd_features()

static void x86_amd_features ( struct x86_features features)
static

Get AMD-defined x86 CPU features.

Parameters
featuresx86 CPU features to fill in

Definition at line 141 of file cpuid.c.

141  {
142  uint32_t discard_a;
143  uint32_t discard_b;
144  int rc;
145 
146  /* Check that features are available via CPUID */
147  if ( ( rc = cpuid_supported ( CPUID_AMD_FEATURES ) ) != 0 ) {
148  DBGC ( features, "CPUID has no AMD-defined features\n" );
149  return;
150  }
151 
152  /* Get features */
153  cpuid ( CPUID_AMD_FEATURES, 0, &discard_a, &discard_b,
154  &features->amd.ecx, &features->amd.edx );
155  DBGC ( features, "CPUID AMD features: %%ecx=%08x, %%edx=%08x\n",
156  features->amd.ecx, features->amd.edx );
157 }
int cpuid_supported(uint32_t function)
Check whether or not CPUID function is supported.
Definition: cpuid.c:76
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
uint32_t features
Supported features.
Definition: ena.h:16
unsigned int uint32_t
Definition: stdint.h:12
#define CPUID_AMD_FEATURES
Get extended features.
Definition: cpuid.h:68

References CPUID_AMD_FEATURES, cpuid_supported(), DBGC, features, and rc.

Referenced by x86_features().

◆ x86_features()

void x86_features ( struct x86_features features)

Get x86 CPU features.

Parameters
featuresx86 CPU features to fill in

Definition at line 164 of file cpuid.c.

164  {
165 
166  /* Clear all features */
167  memset ( features, 0, sizeof ( *features ) );
168 
169  /* Get Intel-defined features */
171 
172  /* Get AMD-defined features */
174 }
static void x86_amd_features(struct x86_features *features)
Get AMD-defined x86 CPU features.
Definition: cpuid.c:141
static void x86_intel_features(struct x86_features *features)
Get Intel-defined x86 CPU features.
Definition: cpuid.c:117
uint32_t features
Supported features.
Definition: ena.h:16
void * memset(void *dest, int character, size_t len) __nonnull

References features, memset(), x86_amd_features(), and x86_intel_features().

Referenced by check_fxsr(), cpuid_exec(), hv_check_hv(), rdrand_entropy_enable(), and rtc_entropy_enable().