You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cpu.c 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <stdint.h>
  2. #include <string.h>
  3. #include <cpu.h>
  4. /** @file
  5. *
  6. * CPU identification
  7. *
  8. */
  9. /**
  10. * Test to see if CPU flag is changeable
  11. *
  12. * @v flag Flag to test
  13. * @ret can_change Flag is changeable
  14. */
  15. static inline int flag_is_changeable ( unsigned int flag ) {
  16. uint32_t f1, f2;
  17. __asm__ ( "pushfl\n\t"
  18. "pushfl\n\t"
  19. "popl %0\n\t"
  20. "movl %0,%1\n\t"
  21. "xorl %2,%0\n\t"
  22. "pushl %0\n\t"
  23. "popfl\n\t"
  24. "pushfl\n\t"
  25. "popl %0\n\t"
  26. "popfl\n\t"
  27. : "=&r" ( f1 ), "=&r" ( f2 )
  28. : "ir" ( flag ) );
  29. return ( ( ( f1 ^ f2 ) & flag ) != 0 );
  30. }
  31. /**
  32. * Get CPU information
  33. *
  34. * @v cpu CPU information structure to fill in
  35. */
  36. void get_cpuinfo ( struct cpuinfo_x86 *cpu ) {
  37. unsigned int cpuid_level;
  38. unsigned int cpuid_extlevel;
  39. unsigned int discard_1, discard_2, discard_3;
  40. memset ( cpu, 0, sizeof ( *cpu ) );
  41. /* Check for CPUID instruction */
  42. if ( ! flag_is_changeable ( X86_EFLAGS_ID ) ) {
  43. DBG ( "CPUID not supported\n" );
  44. return;
  45. }
  46. /* Get features, if present */
  47. cpuid ( 0x00000000, &cpuid_level, &discard_1,
  48. &discard_2, &discard_3 );
  49. if ( cpuid_level >= 0x00000001 ) {
  50. cpuid ( 0x00000001, &discard_1, &discard_2,
  51. &discard_3, &cpu->features );
  52. } else {
  53. DBG ( "CPUID cannot return capabilities\n" );
  54. }
  55. /* Get 64-bit features, if present */
  56. cpuid ( 0x80000000, &cpuid_extlevel, &discard_1,
  57. &discard_2, &discard_3 );
  58. if ( ( cpuid_extlevel & 0xffff0000 ) == 0x80000000 ) {
  59. if ( cpuid_extlevel >= 0x80000001 ) {
  60. cpuid ( 0x80000001, &discard_1, &discard_2,
  61. &discard_3, &cpu->amd_features );
  62. }
  63. }
  64. }