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)
 
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 37 of file cpuid.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ 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 44 of file cpuid.c.

44  {
45  unsigned long original;
46  unsigned long inverted;
47 
48  /* Check for instruction existence via flag modifiability */
49  __asm__ ( "pushf\n\t"
50  "pushf\n\t"
51  "pop %0\n\t"
52  "mov %0,%1\n\t"
53  "xor %2,%1\n\t"
54  "push %1\n\t"
55  "popf\n\t"
56  "pushf\n\t"
57  "pop %1\n\t"
58  "popf\n\t"
59  : "=&r" ( original ), "=&r" ( inverted )
60  : "ir" ( CPUID_FLAG ) );
61  if ( ! ( ( original ^ inverted ) & CPUID_FLAG ) ) {
62  DBGC ( colour, "CPUID instruction is not supported\n" );
63  return -ENOTSUP;
64  }
65 
66  return 0;
67 }
#define colour
Colour for debug messages.
Definition: cpuid.c:37
#define DBGC(...)
Definition: compiler.h:505
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
#define CPUID_FLAG
CPUID support flag.
Definition: cpuid.h:31
union interface::@570 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 75 of file cpuid.c.

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

References colour, CPUID_AMD_CHECK_MASK, CPUID_EXTENDED, 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 116 of file cpuid.c.

116  {
117  uint32_t discard_a;
118  uint32_t discard_b;
119  int rc;
120 
121  /* Check that features are available via CPUID */
122  if ( ( rc = cpuid_supported ( CPUID_FEATURES ) ) != 0 ) {
123  DBGC ( features, "CPUID has no Intel-defined features\n" );
124  return;
125  }
126 
127  /* Get features */
128  cpuid ( CPUID_FEATURES, 0, &discard_a, &discard_b,
129  &features->intel.ecx, &features->intel.edx );
130  DBGC ( features, "CPUID Intel features: %%ecx=%08x, %%edx=%08x\n",
131  features->intel.ecx, features->intel.edx );
132 
133 }
int cpuid_supported(uint32_t function)
Check whether or not CPUID function is supported.
Definition: cpuid.c:75
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define CPUID_FEATURES
Get standard features.
Definition: cpuid.h:40
#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 140 of file cpuid.c.

140  {
141  uint32_t discard_a;
142  uint32_t discard_b;
143  int rc;
144 
145  /* Check that features are available via CPUID */
146  if ( ( rc = cpuid_supported ( CPUID_AMD_FEATURES ) ) != 0 ) {
147  DBGC ( features, "CPUID has no AMD-defined features\n" );
148  return;
149  }
150 
151  /* Get features */
152  cpuid ( CPUID_AMD_FEATURES, 0, &discard_a, &discard_b,
153  &features->amd.ecx, &features->amd.edx );
154  DBGC ( features, "CPUID AMD features: %%ecx=%08x, %%edx=%08x\n",
155  features->amd.ecx, features->amd.edx );
156 }
int cpuid_supported(uint32_t function)
Check whether or not CPUID function is supported.
Definition: cpuid.c:75
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:64

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 163 of file cpuid.c.

163  {
164 
165  /* Clear all features */
166  memset ( features, 0, sizeof ( *features ) );
167 
168  /* Get Intel-defined features */
170 
171  /* Get AMD-defined features */
173 }
static void x86_amd_features(struct x86_features *features)
Get AMD-defined x86 CPU features.
Definition: cpuid.c:140
static void x86_intel_features(struct x86_features *features)
Get Intel-defined x86 CPU features.
Definition: cpuid.c:116
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().