Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / i386 / cpuid.c
blob3b895cb937202027781d6a53236f1d69ed64363e
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>
28 #include <grub/i18n.h>
30 GRUB_MOD_LICENSE ("GPLv3+");
32 #define cpuid(num,a,b,c,d) \
33 asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1" \
34 : "=a" (a), "=r" (b), "=c" (c), "=d" (d) \
35 : "0" (num))
37 static const struct grub_arg_option options[] =
39 /* TRANSLATORS: "(default)" at the end means that this option is used if
40 no argument is specified. */
41 {"long-mode", 'l', 0, N_("Check if CPU supports 64-bit (long) mode (default)."), 0, 0},
42 {0, 0, 0, 0, 0, 0}
45 #define bit_LM (1 << 29)
47 unsigned char grub_cpuid_has_longmode = 0;
49 static grub_err_t
50 grub_cmd_cpuid (grub_extcmd_context_t ctxt __attribute__ ((unused)),
51 int argc __attribute__ ((unused)),
52 char **args __attribute__ ((unused)))
54 return grub_cpuid_has_longmode ? GRUB_ERR_NONE
55 /* TRANSLATORS: it's a standalone boolean value,
56 opposite of "true". */
57 : grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
60 static grub_extcmd_t cmd;
62 GRUB_MOD_INIT(cpuid)
64 #ifdef __x86_64__
65 /* grub-emu */
66 grub_cpuid_has_longmode = 1;
67 #else
68 unsigned int eax, ebx, ecx, edx;
69 unsigned int max_level;
70 unsigned int ext_level;
72 /* See if we can use cpuid. */
73 asm volatile ("pushfl; pushfl; popl %0; movl %0,%1; xorl %2,%0;"
74 "pushl %0; popfl; pushfl; popl %0; popfl"
75 : "=&r" (eax), "=&r" (ebx)
76 : "i" (0x00200000));
77 if (((eax ^ ebx) & 0x00200000) == 0)
78 goto done;
80 /* Check the highest input value for eax. */
81 cpuid (0, eax, ebx, ecx, edx);
82 /* We only look at the first four characters. */
83 max_level = eax;
84 if (max_level == 0)
85 goto done;
87 cpuid (0x80000000, eax, ebx, ecx, edx);
88 ext_level = eax;
89 if (ext_level < 0x80000000)
90 goto done;
92 cpuid (0x80000001, eax, ebx, ecx, edx);
93 grub_cpuid_has_longmode = !!(edx & bit_LM);
94 done:
95 #endif
97 cmd = grub_register_extcmd ("cpuid", grub_cmd_cpuid, 0,
98 "[-l]", N_("Check for CPU features."), options);
101 GRUB_MOD_FINI(cpuid)
103 grub_unregister_extcmd (cmd);