cpuid.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_MOD_LICENSE ("GPLv3+");
  29. static const struct grub_arg_option options[] =
  30. {
  31. /* TRANSLATORS: "(default)" at the end means that this option is used if
  32. no argument is specified. */
  33. {"long-mode", 'l', 0, N_("Check if CPU supports 64-bit (long) mode (default)."), 0, 0},
  34. {"pae", 'p', 0, N_("Check if CPU supports Physical Address Extension."), 0, 0},
  35. {0, 0, 0, 0, 0, 0}
  36. };
  37. enum
  38. {
  39. MODE_LM = 0,
  40. MODE_PAE = 1
  41. };
  42. enum
  43. {
  44. bit_PAE = (1 << 6),
  45. };
  46. enum
  47. {
  48. bit_LM = (1 << 29)
  49. };
  50. unsigned char grub_cpuid_has_longmode = 0, grub_cpuid_has_pae = 0;
  51. static grub_err_t
  52. grub_cmd_cpuid (grub_extcmd_context_t ctxt,
  53. int argc __attribute__ ((unused)),
  54. char **args __attribute__ ((unused)))
  55. {
  56. int val = 0;
  57. if (ctxt->state[MODE_PAE].set)
  58. val = grub_cpuid_has_pae;
  59. else
  60. val = grub_cpuid_has_longmode;
  61. return val ? GRUB_ERR_NONE
  62. /* TRANSLATORS: it's a standalone boolean value,
  63. opposite of "true". */
  64. : grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
  65. }
  66. static grub_extcmd_t cmd;
  67. GRUB_MOD_INIT(cpuid)
  68. {
  69. #ifdef __x86_64__
  70. /* grub-emu */
  71. grub_cpuid_has_longmode = 1;
  72. grub_cpuid_has_pae = 1;
  73. #else
  74. unsigned int eax, ebx, ecx, edx;
  75. unsigned int max_level;
  76. unsigned int ext_level;
  77. /* See if we can use cpuid. */
  78. asm volatile ("pushfl; pushfl; popl %0; movl %0,%1; xorl %2,%0;"
  79. "pushl %0; popfl; pushfl; popl %0; popfl"
  80. : "=&r" (eax), "=&r" (ebx)
  81. : "i" (0x00200000));
  82. if (((eax ^ ebx) & 0x00200000) == 0)
  83. goto done;
  84. /* Check the highest input value for eax. */
  85. grub_cpuid (0, eax, ebx, ecx, edx);
  86. /* We only look at the first four characters. */
  87. max_level = eax;
  88. if (max_level == 0)
  89. goto done;
  90. if (max_level >= 1)
  91. {
  92. grub_cpuid (1, eax, ebx, ecx, edx);
  93. grub_cpuid_has_pae = !!(edx & bit_PAE);
  94. }
  95. grub_cpuid (0x80000000, eax, ebx, ecx, edx);
  96. ext_level = eax;
  97. if (ext_level < 0x80000000)
  98. goto done;
  99. grub_cpuid (0x80000001, eax, ebx, ecx, edx);
  100. grub_cpuid_has_longmode = !!(edx & bit_LM);
  101. done:
  102. #endif
  103. cmd = grub_register_extcmd ("cpuid", grub_cmd_cpuid, 0,
  104. "[-l]", N_("Check for CPU features."), options);
  105. }
  106. GRUB_MOD_FINI(cpuid)
  107. {
  108. grub_unregister_extcmd (cmd);
  109. }