cpuid.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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
  6. *
  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.
  11. *
  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.
  16. *
  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/>.
  19. */
  20. #include <grub/dl.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/env.h>
  24. #include <grub/command.h>
  25. #include <grub/extcmd.h>
  26. #include <grub/i386/cpuid.h>
  27. #include <grub/i18n.h>
  28. GRUB_EXPORT(grub_cpuid_has_longmode);
  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))
  33. static const struct grub_arg_option options[] =
  34. {
  35. {"long-mode", 'l', 0, N_("Check for long mode flag (default)."), 0, 0},
  36. {0, 0, 0, 0, 0, 0}
  37. };
  38. #define bit_LM (1 << 29)
  39. unsigned char grub_cpuid_has_longmode = 0;
  40. static grub_err_t
  41. grub_cmd_cpuid (grub_extcmd_t cmd __attribute__ ((unused)),
  42. int argc __attribute__ ((unused)),
  43. char **args __attribute__ ((unused)))
  44. {
  45. return grub_cpuid_has_longmode ? GRUB_ERR_NONE
  46. : grub_error (GRUB_ERR_TEST_FAILURE, "false");
  47. }
  48. static grub_extcmd_t cmd;
  49. GRUB_MOD_INIT(cpuid)
  50. {
  51. #ifdef __x86_64__
  52. /* grub-emu */
  53. grub_cpuid_has_longmode = 1;
  54. #else
  55. unsigned int eax, ebx, ecx, edx;
  56. unsigned int max_level;
  57. unsigned int ext_level;
  58. /* See if we can use cpuid. */
  59. asm volatile ("pushfl; pushfl; popl %0; movl %0,%1; xorl %2,%0;"
  60. "pushl %0; popfl; pushfl; popl %0; popfl"
  61. : "=&r" (eax), "=&r" (ebx)
  62. : "i" (0x00200000));
  63. if (((eax ^ ebx) & 0x00200000) == 0)
  64. goto done;
  65. /* Check the highest input value for eax. */
  66. cpuid (0, eax, ebx, ecx, edx);
  67. /* We only look at the first four characters. */
  68. max_level = eax;
  69. if (max_level == 0)
  70. goto done;
  71. cpuid (0x80000000, eax, ebx, ecx, edx);
  72. ext_level = eax;
  73. if (ext_level < 0x80000000)
  74. goto done;
  75. cpuid (0x80000001, eax, ebx, ecx, edx);
  76. grub_cpuid_has_longmode = !!(edx & bit_LM);
  77. done:
  78. #endif
  79. cmd = grub_register_extcmd ("cpuid", grub_cmd_cpuid, GRUB_COMMAND_FLAG_BOTH,
  80. "[-l]", N_("Check for CPU features."), options);
  81. }
  82. GRUB_MOD_FINI(cpuid)
  83. {
  84. grub_unregister_extcmd (cmd);
  85. }