cpu.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. int cpu_has_cpuid(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. unsigned int cpu_family_model(void)
  15. {
  16. unsigned int a, b, c, d, family, model;
  17. if (!cpu_has_cpuid())
  18. return 0;
  19. a = 0;
  20. __asm__ volatile ("cpuid" : "+a"(a), "=b"(b), "=c"(c), "=d"(d));
  21. if (a < 1)
  22. return 0;
  23. a = 1;
  24. __asm__ volatile ("cpuid" : "+a"(a), "=b"(b), "=c"(c), "=d"(d));
  25. family = (a >> 8) & 0xFu;
  26. model = (a >> 4) & 0xFu;
  27. if (family == 0xFu)
  28. family += (a >> 20) & 0xFFu;
  29. if (family == 0x6u || family == 0xFu)
  30. model |= ((a >> 16) & 0xFu) << 4;
  31. return (family << 8) | model;
  32. }
  33. /* An AP leaves INIT with CR0.NE clear and an x87 control word of 0x0040:
  34. * every exception unmasked and 24-bit precision. With NE clear an
  35. * unmasked exception (an inexact result, i.e. most floating-point
  36. * operations) freezes the CPU at the next FPU instruction until an external
  37. * interrupt -- the next 1 ms timer tick. QuickJS computes with doubles, so
  38. * the browser's scripts ran several hundred times slower whenever it was
  39. * scheduled on an AP, at 100% CPU, with single-precision results. QEMU
  40. * models neither the freeze nor the precision. Native #MF reporting (NE),
  41. * MP for WAIT, no emulation or task-switch trap, and FNINIT's 0x037F. */
  42. void cpu_init_fpu(void)
  43. {
  44. unsigned int cr0;
  45. __asm__ volatile ("mov %%cr0, %0" : "=r"(cr0));
  46. cr0 |= (1u << 5) | (1u << 1); /* NE | MP */
  47. cr0 &= ~((1u << 2) | (1u << 3)); /* EM | TS */
  48. __asm__ volatile ("mov %0, %%cr0" :: "r"(cr0) : "memory");
  49. __asm__ volatile ("fninit");
  50. }
  51. void cpu_enable_sse(void)
  52. {
  53. unsigned int eax, ebx, ecx, edx, cr4;
  54. if (!cpu_has_cpuid())
  55. return;
  56. eax = 1;
  57. __asm__ volatile ("cpuid"
  58. : "+a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx));
  59. if (!(edx & (1u << 24))) /* FXSR */
  60. return;
  61. __asm__ volatile ("mov %%cr4, %0" : "=r"(cr4));
  62. cr4 |= (1u << 9) | (1u << 10); /* OSFXSR | OSXMMEXCPT */
  63. __asm__ volatile ("mov %0, %%cr4" :: "r"(cr4) : "memory");
  64. }
  65. int cpu_has_sse2(void)
  66. {
  67. unsigned int eax, ebx, ecx, edx;
  68. if (sse2 >= 0)
  69. return sse2;
  70. sse2 = 0;
  71. if (cpu_has_cpuid()) {
  72. eax = 1;
  73. __asm__ volatile ("cpuid"
  74. : "+a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx));
  75. if (edx & (1u << 26)) {
  76. /* SSE instructions #UD unless CR4 enables the state
  77. * (OSFXSR). The kernel is built -mno-sse, so the
  78. * registers are only used inside a single helper and
  79. * need no per-process save. */
  80. if (edx & (1u << 24)) { /* FXSR */
  81. unsigned int cr4;
  82. __asm__ volatile ("mov %%cr4, %0" : "=r"(cr4));
  83. cr4 |= (1u << 9) | (1u << 10); /* OSFXSR | OSXMMEXCPT */
  84. __asm__ volatile ("mov %0, %%cr4" :: "r"(cr4)
  85. : "memory");
  86. }
  87. sse2 = 1;
  88. }
  89. }
  90. return sse2;
  91. }