cpu.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* CPU feature detection (see cpu.h). */
  2. #include "cpu.h"
  3. static int sse2 = -1; /* -1 = not probed yet, 0 = no, 1 = yes */
  4. /* CPUID exists only if the ID bit in EFLAGS can be toggled (i586+). */
  5. static int cpuid_supported(void)
  6. {
  7. unsigned int a, b;
  8. __asm__ volatile ("pushfl; popl %0" : "=r"(a));
  9. b = a ^ (1u << 21);
  10. __asm__ volatile ("pushl %0; popfl" :: "r"(b));
  11. __asm__ volatile ("pushfl; popl %0" : "=r"(b));
  12. return ((a ^ b) & (1u << 21)) != 0;
  13. }
  14. int cpu_has_sse2(void)
  15. {
  16. unsigned int eax, ebx, ecx, edx;
  17. if (sse2 >= 0)
  18. return sse2;
  19. sse2 = 0;
  20. if (cpuid_supported()) {
  21. eax = 1;
  22. __asm__ volatile ("cpuid"
  23. : "+a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx));
  24. if (edx & (1u << 26)) {
  25. /* SSE instructions #UD unless CR4 enables the state
  26. * (OSFXSR). The kernel is built -mno-sse, so the
  27. * registers are only used inside a single helper and
  28. * need no per-process save. */
  29. if (edx & (1u << 24)) { /* FXSR */
  30. unsigned int cr4;
  31. __asm__ volatile ("mov %%cr4, %0" : "=r"(cr4));
  32. cr4 |= (1u << 9) | (1u << 10); /* OSFXSR | OSXMMEXCPT */
  33. __asm__ volatile ("mov %0, %%cr4" :: "r"(cr4)
  34. : "memory");
  35. }
  36. sse2 = 1;
  37. }
  38. }
  39. return sse2;
  40. }