2009-11-21 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2.git] / efiemu / i386 / coredetect.c
blob828508dee33471abf7142698ebd8fc255e32abc7
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2009 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/efiemu/efiemu.h>
20 #include <grub/machine/efiemu.h>
21 #include <grub/command.h>
23 #define cpuid(num,a,b,c,d) \
24 asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1" \
25 : "=a" (a), "=r" (b), "=c" (c), "=d" (d) \
26 : "0" (num))
28 #define bit_LM (1 << 29)
30 char *
31 grub_efiemu_get_default_core_name (void)
34 unsigned int eax, ebx, ecx, edx;
35 unsigned int max_level;
36 unsigned int ext_level;
38 /* See if we can use cpuid. */
39 asm volatile ("pushfl; pushfl; popl %0; movl %0,%1; xorl %2,%0;"
40 "pushl %0; popfl; pushfl; popl %0; popfl"
41 : "=&r" (eax), "=&r" (ebx)
42 : "i" (0x00200000));
43 if (((eax ^ ebx) & 0x00200000) == 0)
44 return "efiemu32.o";
46 /* Check the highest input value for eax. */
47 cpuid (0, eax, ebx, ecx, edx);
48 /* We only look at the first four characters. */
49 max_level = eax;
50 if (max_level == 0)
51 return "efiemu32.o";
53 cpuid (0x80000000, eax, ebx, ecx, edx);
54 ext_level = eax;
55 if (ext_level < 0x80000000)
56 return "efiemu32.o";
58 cpuid (0x80000001, eax, ebx, ecx, edx);
59 return (edx & bit_LM) ? "efiemu64.o" : "efiemu32.o";