mirror of
https://github.com/ipxe/ipxe
synced 2025-12-23 21:41:43 +03:00
[cmdline] Add "cpuid" command
Allow x86 CPU feature flags (such as support for 64-bit mode) to be checked using the "cpuid" command. Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
@@ -7,6 +7,7 @@ INCDIRS += arch/x86/include
|
||||
SRCDIRS += arch/x86/core
|
||||
SRCDIRS += arch/x86/interface/efi
|
||||
SRCDIRS += arch/x86/prefix
|
||||
SRCDIRS += arch/x86/hci/commands
|
||||
|
||||
# breaks building some of the linux-related objects
|
||||
CFLAGS += -Ulinux
|
||||
|
||||
154
src/arch/x86/core/cpuid.c
Normal file
154
src/arch/x86/core/cpuid.c
Normal file
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
FILE_LICENCE ( GPL2_OR_LATER );
|
||||
|
||||
#include <string.h>
|
||||
#include <ipxe/cpuid.h>
|
||||
|
||||
/** @file
|
||||
*
|
||||
* x86 CPU feature detection
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Check whether or not CPUID instruction is supported
|
||||
*
|
||||
* @ret is_supported CPUID instruction is supported
|
||||
*/
|
||||
static int cpuid_is_supported ( void ) {
|
||||
unsigned long original;
|
||||
unsigned long inverted;
|
||||
|
||||
__asm__ ( "pushf\n\t"
|
||||
"pushf\n\t"
|
||||
"pop %0\n\t"
|
||||
"mov %0,%1\n\t"
|
||||
"xor %2,%1\n\t"
|
||||
"push %1\n\t"
|
||||
"popf\n\t"
|
||||
"pushf\n\t"
|
||||
"pop %1\n\t"
|
||||
"popf\n\t"
|
||||
: "=&r" ( original ), "=&r" ( inverted )
|
||||
: "ir" ( CPUID_FLAG ) );
|
||||
return ( ( original ^ inverted ) & CPUID_FLAG );
|
||||
}
|
||||
|
||||
/**
|
||||
* Issue CPUID instruction
|
||||
*
|
||||
* @v operation CPUID operation
|
||||
* @v eax Output via %eax
|
||||
* @v ebx Output via %ebx
|
||||
* @v ecx Output via %ecx
|
||||
* @v edx Output via %edx
|
||||
*/
|
||||
static inline __attribute__ (( always_inline )) void
|
||||
cpuid ( uint32_t operation, uint32_t *eax, uint32_t *ebx, uint32_t *ecx,
|
||||
uint32_t *edx ) {
|
||||
|
||||
__asm__ ( "cpuid"
|
||||
: "=a" ( *eax ), "=b" ( *ebx ), "=c" ( *ecx ), "=d" ( *edx )
|
||||
: "0" ( operation ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Intel-defined x86 CPU features
|
||||
*
|
||||
* @v features x86 CPU features to fill in
|
||||
*/
|
||||
static void x86_intel_features ( struct x86_features *features ) {
|
||||
uint32_t max_level;
|
||||
uint32_t discard_a;
|
||||
uint32_t discard_b;
|
||||
uint32_t discard_c;
|
||||
uint32_t discard_d;
|
||||
|
||||
/* Check that features are available via CPUID */
|
||||
cpuid ( CPUID_VENDOR_ID, &max_level, &discard_b, &discard_c,
|
||||
&discard_d );
|
||||
if ( max_level < CPUID_FEATURES ) {
|
||||
DBGC ( features, "CPUID has no Intel-defined features (max "
|
||||
"level %08x)\n", max_level );
|
||||
return;
|
||||
}
|
||||
|
||||
/* Get features */
|
||||
cpuid ( CPUID_FEATURES, &discard_a, &discard_b,
|
||||
&features->intel.ecx, &features->intel.edx );
|
||||
DBGC ( features, "CPUID Intel features: %%ecx=%08x, %%edx=%08x\n",
|
||||
features->intel.ecx, features->intel.edx );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get AMD-defined x86 CPU features
|
||||
*
|
||||
* @v features x86 CPU features to fill in
|
||||
*/
|
||||
static void x86_amd_features ( struct x86_features *features ) {
|
||||
uint32_t max_level;
|
||||
uint32_t discard_a;
|
||||
uint32_t discard_b;
|
||||
uint32_t discard_c;
|
||||
uint32_t discard_d;
|
||||
|
||||
/* Check that features are available via CPUID */
|
||||
cpuid ( CPUID_AMD_MAX_FN, &max_level, &discard_b, &discard_c,
|
||||
&discard_d );
|
||||
if ( ( max_level & CPUID_AMD_CHECK_MASK ) != CPUID_AMD_CHECK ) {
|
||||
DBGC ( features, "CPUID has no extended functions\n" );
|
||||
return;
|
||||
}
|
||||
if ( max_level < CPUID_AMD_FEATURES ) {
|
||||
DBGC ( features, "CPUID has no AMD-defined features (max "
|
||||
"level %08x)\n", max_level );
|
||||
return;
|
||||
}
|
||||
|
||||
/* Get features */
|
||||
cpuid ( CPUID_AMD_FEATURES, &discard_a, &discard_b,
|
||||
&features->amd.ecx, &features->amd.edx );
|
||||
DBGC ( features, "CPUID AMD features: %%ecx=%08x, %%edx=%08x\n",
|
||||
features->amd.ecx, features->amd.edx );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get x86 CPU features
|
||||
*
|
||||
* @v features x86 CPU features to fill in
|
||||
*/
|
||||
void x86_features ( struct x86_features *features ) {
|
||||
|
||||
/* Clear all features */
|
||||
memset ( features, 0, sizeof ( *features ) );
|
||||
|
||||
/* Check that CPUID instruction is available */
|
||||
if ( ! cpuid_is_supported() ) {
|
||||
DBGC ( features, "CPUID instruction is not supported\n" );
|
||||
return;
|
||||
}
|
||||
|
||||
/* Get Intel-defined features */
|
||||
x86_intel_features ( features );
|
||||
|
||||
/* Get AMD-defined features */
|
||||
x86_amd_features ( features );
|
||||
}
|
||||
94
src/arch/x86/hci/commands/cpuid_cmd.c
Normal file
94
src/arch/x86/hci/commands/cpuid_cmd.c
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
FILE_LICENCE ( GPL2_OR_LATER );
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <getopt.h>
|
||||
#include <ipxe/cpuid.h>
|
||||
#include <ipxe/command.h>
|
||||
#include <ipxe/parseopt.h>
|
||||
|
||||
/** @file
|
||||
*
|
||||
* x86 CPU feature detection command
|
||||
*
|
||||
*/
|
||||
|
||||
/** "cpuid" options */
|
||||
struct cpuid_options {
|
||||
/** Check AMD-defined features (%eax=0x80000001) */
|
||||
int amd;
|
||||
/** Check features defined via %ecx */
|
||||
int ecx;
|
||||
};
|
||||
|
||||
/** "cpuid" option list */
|
||||
static struct option_descriptor cpuid_opts[] = {
|
||||
OPTION_DESC ( "amd", 'a', no_argument,
|
||||
struct cpuid_options, amd, parse_flag ),
|
||||
OPTION_DESC ( "ecx", 'c', no_argument,
|
||||
struct cpuid_options, ecx, parse_flag ),
|
||||
};
|
||||
|
||||
/** "cpuid" command descriptor */
|
||||
static struct command_descriptor cpuid_cmd =
|
||||
COMMAND_DESC ( struct cpuid_options, cpuid_opts, 1, 1,
|
||||
"[--amd] [--ecx] <bit>" );
|
||||
|
||||
/**
|
||||
* The "cpuid" command
|
||||
*
|
||||
* @v argc Argument count
|
||||
* @v argv Argument list
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int cpuid_exec ( int argc, char **argv ) {
|
||||
struct cpuid_options opts;
|
||||
struct x86_features features;
|
||||
struct x86_feature_registers *feature_regs;
|
||||
uint32_t feature_reg;
|
||||
unsigned int bit;
|
||||
int rc;
|
||||
|
||||
/* Parse options */
|
||||
if ( ( rc = parse_options ( argc, argv, &cpuid_cmd, &opts ) ) != 0 )
|
||||
return rc;
|
||||
|
||||
/* Parse bit number */
|
||||
if ( ( rc = parse_integer ( argv[optind], &bit ) ) != 0 )
|
||||
return rc;
|
||||
|
||||
/* Get CPU features */
|
||||
x86_features ( &features );
|
||||
|
||||
/* Extract relevant feature register */
|
||||
feature_regs = ( opts.amd ? &features.amd : &features.intel );
|
||||
feature_reg = ( opts.ecx ? feature_regs->ecx : feature_regs->edx );
|
||||
|
||||
/* Check presence of specified feature */
|
||||
return ( ( feature_reg & ( 1 << bit ) ) ? 0 : -ENOENT );
|
||||
}
|
||||
|
||||
/** x86 CPU feature detection command */
|
||||
struct command cpuid_command __command = {
|
||||
.name = "cpuid",
|
||||
.exec = cpuid_exec,
|
||||
};
|
||||
@@ -38,8 +38,10 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||
#define ERRFILE_undionly ( ERRFILE_ARCH | ERRFILE_NET | 0x00030000 )
|
||||
#define ERRFILE_undirom ( ERRFILE_ARCH | ERRFILE_NET | 0x00040000 )
|
||||
|
||||
#define ERRFILE_timer_rdtsc ( ERRFILE_ARCH | ERRFILE_DRIVER | 0x00000000 )
|
||||
#define ERRFILE_timer_bios ( ERRFILE_ARCH | ERRFILE_DRIVER | 0x00010000 )
|
||||
#define ERRFILE_timer_rdtsc ( ERRFILE_ARCH | ERRFILE_DRIVER | 0x00000000 )
|
||||
#define ERRFILE_timer_bios ( ERRFILE_ARCH | ERRFILE_DRIVER | 0x00010000 )
|
||||
|
||||
#define ERRFILE_cpuid_cmd ( ERRFILE_ARCH | ERRFILE_OTHER | 0x00000000 )
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
53
src/arch/x86/include/ipxe/cpuid.h
Normal file
53
src/arch/x86/include/ipxe/cpuid.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#ifndef _IPXE_CPUID_H
|
||||
#define _IPXE_CPUID_H
|
||||
|
||||
/** @file
|
||||
*
|
||||
* x86 CPU feature detection
|
||||
*
|
||||
*/
|
||||
|
||||
FILE_LICENCE ( GPL2_OR_LATER );
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/** An x86 CPU feature register set */
|
||||
struct x86_feature_registers {
|
||||
/** Features returned via %ecx */
|
||||
uint32_t ecx;
|
||||
/** Features returned via %edx */
|
||||
uint32_t edx;
|
||||
};
|
||||
|
||||
/** x86 CPU features */
|
||||
struct x86_features {
|
||||
/** Intel-defined features (%eax=0x00000001) */
|
||||
struct x86_feature_registers intel;
|
||||
/** AMD-defined features (%eax=0x80000001) */
|
||||
struct x86_feature_registers amd;
|
||||
};
|
||||
|
||||
/** CPUID support flag */
|
||||
#define CPUID_FLAG 0x00200000UL
|
||||
|
||||
/** Get vendor ID and largest standard function */
|
||||
#define CPUID_VENDOR_ID 0x00000000UL
|
||||
|
||||
/** Get standard features */
|
||||
#define CPUID_FEATURES 0x00000001UL
|
||||
|
||||
/** Get largest extended function */
|
||||
#define CPUID_AMD_MAX_FN 0x80000000UL
|
||||
|
||||
/** Extended function existence check */
|
||||
#define CPUID_AMD_CHECK 0x80000000UL
|
||||
|
||||
/** Extended function existence check mask */
|
||||
#define CPUID_AMD_CHECK_MASK 0xffff0000UL
|
||||
|
||||
/** Get extended features */
|
||||
#define CPUID_AMD_FEATURES 0x80000001UL
|
||||
|
||||
extern void x86_features ( struct x86_features *features );
|
||||
|
||||
#endif /* _IPXE_CPUID_H */
|
||||
Reference in New Issue
Block a user