cpuflags.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <linux/types.h>
  2. #include "bitops.h"
  3. #include <asm/processor-flags.h>
  4. #include <asm/required-features.h>
  5. #include <asm/msr-index.h>
  6. #include "cpuflags.h"
  7. struct cpu_features cpu;
  8. u32 cpu_vendor[3];
  9. static bool loaded_flags;
  10. static int has_fpu(void)
  11. {
  12. u16 fcw = -1, fsw = -1;
  13. unsigned long cr0;
  14. asm volatile("mov %%cr0,%0" : "=r" (cr0));
  15. if (cr0 & (X86_CR0_EM|X86_CR0_TS)) {
  16. cr0 &= ~(X86_CR0_EM|X86_CR0_TS);
  17. asm volatile("mov %0,%%cr0" : : "r" (cr0));
  18. }
  19. asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
  20. : "+m" (fsw), "+m" (fcw));
  21. return fsw == 0 && (fcw & 0x103f) == 0x003f;
  22. }
  23. /*
  24. * For building the 16-bit code we want to explicitly specify 32-bit
  25. * push/pop operations, rather than just saying 'pushf' or 'popf' and
  26. * letting the compiler choose. But this is also included from the
  27. * compressed/ directory where it may be 64-bit code, and thus needs
  28. * to be 'pushfq' or 'popfq' in that case.
  29. */
  30. #ifdef __x86_64__
  31. #define PUSHF "pushfq"
  32. #define POPF "popfq"
  33. #else
  34. #define PUSHF "pushfl"
  35. #define POPF "popfl"
  36. #endif
  37. int has_eflag(unsigned long mask)
  38. {
  39. unsigned long f0, f1;
  40. asm volatile(PUSHF " \n\t"
  41. PUSHF " \n\t"
  42. "pop %0 \n\t"
  43. "mov %0,%1 \n\t"
  44. "xor %2,%1 \n\t"
  45. "push %1 \n\t"
  46. POPF " \n\t"
  47. PUSHF " \n\t"
  48. "pop %1 \n\t"
  49. POPF
  50. : "=&r" (f0), "=&r" (f1)
  51. : "ri" (mask));
  52. return !!((f0^f1) & mask);
  53. }
  54. /* Handle x86_32 PIC using ebx. */
  55. #if defined(__i386__) && defined(__PIC__)
  56. # define EBX_REG "=r"
  57. #else
  58. # define EBX_REG "=b"
  59. #endif
  60. static inline void cpuid(u32 id, u32 *a, u32 *b, u32 *c, u32 *d)
  61. {
  62. asm volatile(".ifnc %%ebx,%3 ; movl %%ebx,%3 ; .endif \n\t"
  63. "cpuid \n\t"
  64. ".ifnc %%ebx,%3 ; xchgl %%ebx,%3 ; .endif \n\t"
  65. : "=a" (*a), "=c" (*c), "=d" (*d), EBX_REG (*b)
  66. : "a" (id)
  67. );
  68. }
  69. void get_cpuflags(void)
  70. {
  71. u32 max_intel_level, max_amd_level;
  72. u32 tfms;
  73. u32 ignored;
  74. if (loaded_flags)
  75. return;
  76. loaded_flags = true;
  77. if (has_fpu())
  78. set_bit(X86_FEATURE_FPU, cpu.flags);
  79. if (has_eflag(X86_EFLAGS_ID)) {
  80. cpuid(0x0, &max_intel_level, &cpu_vendor[0], &cpu_vendor[2],
  81. &cpu_vendor[1]);
  82. if (max_intel_level >= 0x00000001 &&
  83. max_intel_level <= 0x0000ffff) {
  84. cpuid(0x1, &tfms, &ignored, &cpu.flags[4],
  85. &cpu.flags[0]);
  86. cpu.level = (tfms >> 8) & 15;
  87. cpu.family = cpu.level;
  88. cpu.model = (tfms >> 4) & 15;
  89. if (cpu.level >= 6)
  90. cpu.model += ((tfms >> 16) & 0xf) << 4;
  91. }
  92. cpuid(0x80000000, &max_amd_level, &ignored, &ignored,
  93. &ignored);
  94. if (max_amd_level >= 0x80000001 &&
  95. max_amd_level <= 0x8000ffff) {
  96. cpuid(0x80000001, &ignored, &ignored, &cpu.flags[6],
  97. &cpu.flags[1]);
  98. }
  99. }
  100. }