Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <ipxe/cpuid.h>
  27. /** @file
  28. *
  29. * x86 CPU feature detection
  30. *
  31. */
  32. /** Colour for debug messages */
  33. #define colour 0x861d
  34. /**
  35. * Check whether or not CPUID instruction is supported
  36. *
  37. * @ret rc Return status code
  38. */
  39. static int cpuid_instruction_supported ( void ) {
  40. unsigned long original;
  41. unsigned long inverted;
  42. /* Check for instruction existence via flag modifiability */
  43. __asm__ ( "pushf\n\t"
  44. "pushf\n\t"
  45. "pop %0\n\t"
  46. "mov %0,%1\n\t"
  47. "xor %2,%1\n\t"
  48. "push %1\n\t"
  49. "popf\n\t"
  50. "pushf\n\t"
  51. "pop %1\n\t"
  52. "popf\n\t"
  53. : "=&r" ( original ), "=&r" ( inverted )
  54. : "ir" ( CPUID_FLAG ) );
  55. if ( ! ( ( original ^ inverted ) & CPUID_FLAG ) ) {
  56. DBGC ( colour, "CPUID instruction is not supported\n" );
  57. return -ENOTSUP;
  58. }
  59. return 0;
  60. }
  61. /**
  62. * Check whether or not CPUID function is supported
  63. *
  64. * @v function CPUID function
  65. * @ret rc Return status code
  66. */
  67. int cpuid_supported ( uint32_t function ) {
  68. uint32_t max_function;
  69. uint32_t discard_b;
  70. uint32_t discard_c;
  71. uint32_t discard_d;
  72. int rc;
  73. /* Check that CPUID instruction is available */
  74. if ( ( rc = cpuid_instruction_supported() ) != 0 )
  75. return rc;
  76. /* Find highest supported function number within this family */
  77. cpuid ( ( function & CPUID_EXTENDED ), 0, &max_function, &discard_b,
  78. &discard_c, &discard_d );
  79. /* Fail if maximum function number is meaningless (e.g. if we
  80. * are attempting to call an extended function on a CPU which
  81. * does not support them).
  82. */
  83. if ( ( max_function & CPUID_AMD_CHECK_MASK ) !=
  84. ( function & CPUID_AMD_CHECK_MASK ) ) {
  85. DBGC ( colour, "CPUID invalid maximum function %#08x\n",
  86. max_function );
  87. return -EINVAL;
  88. }
  89. /* Fail if this function is not supported */
  90. if ( function > max_function ) {
  91. DBGC ( colour, "CPUID function %#08x not supported\n",
  92. function );
  93. return -ENOTTY;
  94. }
  95. return 0;
  96. }
  97. /**
  98. * Get Intel-defined x86 CPU features
  99. *
  100. * @v features x86 CPU features to fill in
  101. */
  102. static void x86_intel_features ( struct x86_features *features ) {
  103. uint32_t discard_a;
  104. uint32_t discard_b;
  105. int rc;
  106. /* Check that features are available via CPUID */
  107. if ( ( rc = cpuid_supported ( CPUID_FEATURES ) ) != 0 ) {
  108. DBGC ( features, "CPUID has no Intel-defined features\n" );
  109. return;
  110. }
  111. /* Get features */
  112. cpuid ( CPUID_FEATURES, 0, &discard_a, &discard_b,
  113. &features->intel.ecx, &features->intel.edx );
  114. DBGC ( features, "CPUID Intel features: %%ecx=%08x, %%edx=%08x\n",
  115. features->intel.ecx, features->intel.edx );
  116. }
  117. /**
  118. * Get AMD-defined x86 CPU features
  119. *
  120. * @v features x86 CPU features to fill in
  121. */
  122. static void x86_amd_features ( struct x86_features *features ) {
  123. uint32_t discard_a;
  124. uint32_t discard_b;
  125. int rc;
  126. /* Check that features are available via CPUID */
  127. if ( ( rc = cpuid_supported ( CPUID_AMD_FEATURES ) ) != 0 ) {
  128. DBGC ( features, "CPUID has no AMD-defined features\n" );
  129. return;
  130. }
  131. /* Get features */
  132. cpuid ( CPUID_AMD_FEATURES, 0, &discard_a, &discard_b,
  133. &features->amd.ecx, &features->amd.edx );
  134. DBGC ( features, "CPUID AMD features: %%ecx=%08x, %%edx=%08x\n",
  135. features->amd.ecx, features->amd.edx );
  136. }
  137. /**
  138. * Get x86 CPU features
  139. *
  140. * @v features x86 CPU features to fill in
  141. */
  142. void x86_features ( struct x86_features *features ) {
  143. /* Clear all features */
  144. memset ( features, 0, sizeof ( *features ) );
  145. /* Get Intel-defined features */
  146. x86_intel_features ( features );
  147. /* Get AMD-defined features */
  148. x86_amd_features ( features );
  149. }