cpu.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * cpu.c
  3. *
  4. * Copyright (C) 2018 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <cpu.h>
  20. #include <cpuid.h>
  21. #define CPUID_GET_FEATURE_FLAGS 0x00000001
  22. #define CPUID_GET_MISC_INFORMATION 0x80000008
  23. #define CPUID_FEATURE_FLAG_ECX_SSE3 (1 << 0)
  24. #define CPUID_FEATURE_FLAG_ECX_PCLMUL (1 << 1)
  25. #define CPUID_FEATURE_FLAG_ECX_DTES64 (1 << 2)
  26. #define CPUID_FEATURE_FLAG_ECX_MON (1 << 3)
  27. #define CPUID_FEATURE_FLAG_ECX_DSCPL (1 << 4)
  28. #define CPUID_FEATURE_FLAG_ECX_VMX (1 << 5)
  29. #define CPUID_FEATURE_FLAG_ECX_SMX (1 << 6)
  30. #define CPUID_FEATURE_FLAG_ECX_EST (1 << 7)
  31. #define CPUID_FEATURE_FLAG_ECX_TM2 (1 << 8)
  32. #define CPUID_FEATURE_FLAG_ECX_SSSE3 (1 << 9)
  33. #define CPUID_FEATURE_FLAG_ECX_CID (1 << 10)
  34. #define CPUID_FEATURE_FLAG_ECX_SDBG (1 << 11)
  35. #define CPUID_FEATURE_FLAG_ECX_FMA (1 << 12)
  36. #define CPUID_FEATURE_FLAG_ECX_CX16 (1 << 13)
  37. #define CPUID_FEATURE_FLAG_ECX_ETPRD (1 << 14)
  38. #define CPUID_FEATURE_FLAG_ECX_PDCM (1 << 15)
  39. #define CPUID_FEATURE_FLAG_ECX_PCID (1 << 17)
  40. #define CPUID_FEATURE_FLAG_ECX_DCA (1 << 18)
  41. #define CPUID_FEATURE_FLAG_ECX_SSE41 (1 << 19)
  42. #define CPUID_FEATURE_FLAG_ECX_SSE42 (1 << 20)
  43. #define CPUID_FEATURE_FLAG_ECX_X2APIC (1 << 21)
  44. #define CPUID_FEATURE_FLAG_ECX_MOVBE (1 << 22)
  45. #define CPUID_FEATURE_FLAG_ECX_POPCNT (1 << 23)
  46. #define CPUID_FEATURE_FLAG_ECX_TSCD (1 << 24)
  47. #define CPUID_FEATURE_FLAG_ECX_AES (1 << 25)
  48. #define CPUID_FEATURE_FLAG_ECX_XSAVE (1 << 26)
  49. #define CPUID_FEATURE_FLAG_ECX_OSXSAVE (1 << 27)
  50. #define CPUID_FEATURE_FLAG_ECX_AVX (1 << 28)
  51. #define CPUID_FEATURE_FLAG_ECX_F16C (1 << 29)
  52. #define CPUID_FEATURE_FLAG_ECX_RDRAND (1 << 30)
  53. #define CPUID_FEATURE_FLAG_ECX_HV (1 << 31)
  54. #define CPUID_FEATURE_FLAG_EDX_FPU (1 << 0)
  55. #define CPUID_FEATURE_FLAG_EDX_VME (1 << 1)
  56. #define CPUID_FEATURE_FLAG_EDX_DE (1 << 2)
  57. #define CPUID_FEATURE_FLAG_EDX_PSE (1 << 3)
  58. #define CPUID_FEATURE_FLAG_EDX_TSC (1 << 4)
  59. #define CPUID_FEATURE_FLAG_EDX_MSR (1 << 5)
  60. #define CPUID_FEATURE_FLAG_EDX_PAE (1 << 6)
  61. #define CPUID_FEATURE_FLAG_EDX_MCE (1 << 7)
  62. #define CPUID_FEATURE_FLAG_EDX_CX8 (1 << 8)
  63. #define CPUID_FEATURE_FLAG_EDX_APIC (1 << 9)
  64. #define CPUID_FEATURE_FLAG_EDX_SEP (1 << 11)
  65. #define CPUID_FEATURE_FLAG_EDX_MTRR (1 << 12)
  66. #define CPUID_FEATURE_FLAG_EDX_PGE (1 << 13)
  67. #define CPUID_FEATURE_FLAG_EDX_MCA (1 << 14)
  68. #define CPUID_FEATURE_FLAG_EDX_CMOV (1 << 15)
  69. #define CPUID_FEATURE_FLAG_EDX_PAT (1 << 16)
  70. #define CPUID_FEATURE_FLAG_EDX_PSE36 (1 << 17)
  71. #define CPUID_FEATURE_FLAG_EDX_PSN (1 << 18)
  72. #define CPUID_FEATURE_FLAG_EDX_CLFL (1 << 19)
  73. #define CPUID_FEATURE_FLAG_EDX_DTES (1 << 21)
  74. #define CPUID_FEATURE_FLAG_EDX_ACPI (1 << 22)
  75. #define CPUID_FEATURE_FLAG_EDX_MMX (1 << 23)
  76. #define CPUID_FEATURE_FLAG_EDX_FXSR (1 << 24)
  77. #define CPUID_FEATURE_FLAG_EDX_SSE (1 << 25)
  78. #define CPUID_FEATURE_FLAG_EDX_SSE2 (1 << 26)
  79. #define CPUID_FEATURE_FLAG_EDX_SS (1 << 27)
  80. #define CPUID_FEATURE_FLAG_EDX_HTT (1 << 28)
  81. #define CPUID_FEATURE_FLAG_EDX_TM1 (1 << 29)
  82. #define CPUID_FEATURE_FLAG_EDX_IA64 (1 << 30)
  83. #define CPUID_FEATURE_FLAG_EDX_PBE (1 << 31)
  84. int cpu_fpu_present = FPU_NOT_PRESENT;
  85. byte_t cpu_max_physical_bits = 32;
  86. static void fpu_init(void)
  87. {
  88. cpu_write_master_control_register(cpu_read_master_control_register() & ~(CPU_CONTROL_FLAG_EM | CPU_CONTROL_FLAG_TS));
  89. __asm__ volatile ("fninit");
  90. }
  91. static void fpu_probe(void)
  92. {
  93. word_t fpu_status = 0xCCCC;
  94. __asm__ volatile ("fnstsw %0"
  95. : "=m" (fpu_status) /* output */
  96. : /* input */
  97. : /* clobber */);
  98. if (fpu_status == 0)
  99. {
  100. cpu_fpu_present = FPU_LEGACY;
  101. }
  102. else
  103. {
  104. cpu_fpu_present = FPU_NOT_PRESENT;
  105. cpu_write_master_control_register(cpu_read_master_control_register() | CPU_CONTROL_FLAG_EM);
  106. }
  107. }
  108. void cpu_init(void)
  109. {
  110. unsigned int features[5], address_size_info, cpu_count_info, dummy;
  111. if (!__get_cpuid(CPUID_GET_FEATURE_FLAGS, &features[0], &features[1], &features[2], &features[3]))
  112. {
  113. fpu_init();
  114. fpu_probe();
  115. return;
  116. }
  117. if (features[3] & CPUID_FEATURE_FLAG_EDX_FPU)
  118. {
  119. fpu_init();
  120. if (features[3] & CPUID_FEATURE_FLAG_EDX_FXSR)
  121. {
  122. cpu_write_feature_register(cpu_read_feature_register() | CPU_FEATURE_FLAG_OSXFSR);
  123. cpu_fpu_present = FPU_XFSR;
  124. }
  125. else
  126. {
  127. cpu_fpu_present = FPU_LEGACY;
  128. }
  129. }
  130. if (features[3] & CPUID_FEATURE_FLAG_EDX_PGE)
  131. {
  132. cpu_write_feature_register(cpu_read_feature_register() | CPU_FEATURE_FLAG_PGE);
  133. }
  134. if (features[3] & CPUID_FEATURE_FLAG_EDX_PSE)
  135. {
  136. cpu_write_feature_register(cpu_read_feature_register() | CPU_FEATURE_FLAG_PSE);
  137. }
  138. if ((features[3] & CPUID_FEATURE_FLAG_EDX_PAE) || (features[3] & CPUID_FEATURE_FLAG_EDX_PSE36))
  139. {
  140. /* Don't enable PAE here, it might be disabled on the user's request */
  141. cpu_max_physical_bits = 36;
  142. }
  143. if (__get_cpuid(CPUID_GET_MISC_INFORMATION, &address_size_info, &features[4], &cpu_count_info, &dummy))
  144. {
  145. byte_t physical_bits = address_size_info & 0xFF;
  146. if (physical_bits > 36) cpu_max_physical_bits = physical_bits;
  147. }
  148. }