vaseboot/VasEBoot-core/commands/i386/cpuid.c

126 lines
3.3 KiB
C

/* cpuid.c - test for CPU features */
/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2006, 2007, 2009 Free Software Foundation, Inc.
* Based on gcc/gcc/config/i386/driver-i386.c
*
* VasEBoot 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 3 of the License, or
* (at your option) any later version.
*
* VasEBoot 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 VasEBoot. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/dl.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/env.h>
#include <VasEBoot/command.h>
#include <VasEBoot/extcmd.h>
#include <VasEBoot/i386/cpuid.h>
#include <VasEBoot/i18n.h>
VasEBoot_MOD_LICENSE ("GPLv3+");
static const struct VasEBoot_arg_option options[] =
{
/* TRANSLATORS: "(default)" at the end means that this option is used if
no argument is specified. */
{"long-mode", 'l', 0, N_("Check if CPU supports 64-bit (long) mode (default)."), 0, 0},
{"pae", 'p', 0, N_("Check if CPU supports Physical Address Extension."), 0, 0},
{0, 0, 0, 0, 0, 0}
};
enum
{
MODE_LM = 0,
MODE_PAE = 1
};
enum
{
bit_PAE = (1 << 6),
};
enum
{
bit_LM = (1 << 29)
};
unsigned char VasEBoot_cpuid_has_longmode = 0, VasEBoot_cpuid_has_pae = 0;
static VasEBoot_err_t
VasEBoot_cmd_cpuid (VasEBoot_extcmd_context_t ctxt,
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{
int val = 0;
if (ctxt->state[MODE_PAE].set)
val = VasEBoot_cpuid_has_pae;
else
val = VasEBoot_cpuid_has_longmode;
return val ? VasEBoot_ERR_NONE
/* TRANSLATORS: it's a standalone boolean value,
opposite of "true". */
: VasEBoot_error (VasEBoot_ERR_TEST_FAILURE, N_("false"));
}
static VasEBoot_extcmd_t cmd;
VasEBoot_MOD_INIT(cpuid)
{
#ifdef __x86_64__
/* VasEBoot-emu */
VasEBoot_cpuid_has_longmode = 1;
VasEBoot_cpuid_has_pae = 1;
#else
unsigned int eax, ebx, ecx, edx;
unsigned int max_level;
unsigned int ext_level;
/* See if we can use cpuid. */
asm volatile ("pushfl; pushfl; popl %0; movl %0,%1; xorl %2,%0;"
"pushl %0; popfl; pushfl; popl %0; popfl"
: "=&r" (eax), "=&r" (ebx)
: "i" (0x00200000));
if (((eax ^ ebx) & 0x00200000) == 0)
goto done;
/* Check the highest input value for eax. */
VasEBoot_cpuid (0, eax, ebx, ecx, edx);
/* We only look at the first four characters. */
max_level = eax;
if (max_level == 0)
goto done;
if (max_level >= 1)
{
VasEBoot_cpuid (1, eax, ebx, ecx, edx);
VasEBoot_cpuid_has_pae = !!(edx & bit_PAE);
}
VasEBoot_cpuid (0x80000000, eax, ebx, ecx, edx);
ext_level = eax;
if (ext_level < 0x80000000)
goto done;
VasEBoot_cpuid (0x80000001, eax, ebx, ecx, edx);
VasEBoot_cpuid_has_longmode = !!(edx & bit_LM);
done:
#endif
cmd = VasEBoot_register_extcmd ("cpuid", VasEBoot_cmd_cpuid, 0,
"[-l]", N_("Check for CPU features."), options);
}
VasEBoot_MOD_FINI(cpuid)
{
VasEBoot_unregister_extcmd (cmd);
}