2009-10-28 Robert Millan <rmh.grub@aybabtu.com>
[grub2/jjazz.git] / commands / i386 / cpuid.c
bloba8bbfe69bb3e87ec301e8785a714af2bcea0fcfb
1 /* cpuid.c - test for CPU features */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2006, 2007, 2009 Free Software Foundation, Inc.
5 * Based on gcc/gcc/config/i386/driver-i386.c
7 * GRUB is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * GRUB is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/dl.h>
22 #include <grub/misc.h>
23 #include <grub/mm.h>
24 #include <grub/env.h>
25 #include <grub/command.h>
26 #include <grub/extcmd.h>
27 #include <grub/i386/cpuid.h>
29 #define cpuid(num,a,b,c,d) \
30 asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1" \
31 : "=a" (a), "=r" (b), "=c" (c), "=d" (d) \
32 : "0" (num))
34 static const struct grub_arg_option options[] =
36 {"long-mode", 'l', 0, "check for long mode flag (default)", 0, 0},
37 {0, 0, 0, 0, 0, 0}
40 #define bit_LM (1 << 29)
42 unsigned char grub_cpuid_has_longmode = 0;
44 static grub_err_t
45 grub_cmd_cpuid (grub_extcmd_t cmd __attribute__ ((unused)),
46 int argc __attribute__ ((unused)),
47 char **args __attribute__ ((unused)))
49 return grub_cpuid_has_longmode ? GRUB_ERR_NONE
50 : grub_error (GRUB_ERR_TEST_FAILURE, "false");
53 static grub_extcmd_t cmd;
55 GRUB_MOD_INIT(cpuid)
57 #ifdef __x86_64__
58 /* grub-emu */
59 grub_cpuid_has_longmode = 1;
60 #else
61 unsigned int eax, ebx, ecx, edx;
62 unsigned int max_level;
63 unsigned int ext_level;
65 /* See if we can use cpuid. */
66 asm volatile ("pushfl; pushfl; popl %0; movl %0,%1; xorl %2,%0;"
67 "pushl %0; popfl; pushfl; popl %0; popfl"
68 : "=&r" (eax), "=&r" (ebx)
69 : "i" (0x00200000));
70 if (((eax ^ ebx) & 0x00200000) == 0)
71 goto done;
73 /* Check the highest input value for eax. */
74 cpuid (0, eax, ebx, ecx, edx);
75 /* We only look at the first four characters. */
76 max_level = eax;
77 if (max_level == 0)
78 goto done;
80 cpuid (0x80000000, eax, ebx, ecx, edx);
81 ext_level = eax;
82 if (ext_level < 0x80000000)
83 goto done;
85 cpuid (0x80000001, eax, ebx, ecx, edx);
86 grub_cpuid_has_longmode = !!(edx & bit_LM);
87 done:
88 #endif
90 cmd = grub_register_extcmd ("cpuid", grub_cmd_cpuid, GRUB_COMMAND_FLAG_BOTH,
91 "cpuid [-l]", "Check for CPU features", options);
94 GRUB_MOD_FINI(cpuid)
96 grub_unregister_extcmd (cmd);