cpuinfo.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* cpuinfo command: print CPU vendor/model and estimated current clock speed. */
  2. #include "io.h"
  3. #include "timer.h"
  4. static void put_uint(unsigned int n)
  5. {
  6. char buf[12];
  7. char *p = buf + sizeof(buf) - 1;
  8. *p = '\0';
  9. if (n == 0) {
  10. io_putchar('0');
  11. return;
  12. }
  13. while (n) {
  14. *--p = (char)('0' + (n % 10));
  15. n /= 10;
  16. }
  17. while (*p)
  18. io_putchar(*p++);
  19. }
  20. static void cpuid(unsigned int leaf, unsigned int subleaf,
  21. unsigned int *eax, unsigned int *ebx,
  22. unsigned int *ecx, unsigned int *edx)
  23. {
  24. __asm__ volatile ("cpuid"
  25. : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
  26. : "a"(leaf), "c"(subleaf));
  27. }
  28. static unsigned int read_tsc_low(void)
  29. {
  30. unsigned int lo, hi;
  31. __asm__ volatile ("rdtsc" : "=a"(lo), "=d"(hi));
  32. (void)hi;
  33. return lo;
  34. }
  35. static void trim_right_spaces(char *s)
  36. {
  37. unsigned int len = 0;
  38. while (s[len])
  39. len++;
  40. while (len > 0 && s[len - 1] == ' ') {
  41. s[len - 1] = '\0';
  42. len--;
  43. }
  44. }
  45. static void print_flag(const char *name, int *printed_any)
  46. {
  47. if (*printed_any)
  48. io_putchar(' ');
  49. io_putstr(name);
  50. *printed_any = 1;
  51. }
  52. static void print_features_leaf1(unsigned int ecx1, unsigned int edx1)
  53. {
  54. int any = 0;
  55. io_putstr("ext l1 ");
  56. if (edx1 & (1u << 0)) print_flag("FPU", &any);
  57. if (edx1 & (1u << 4)) print_flag("TSC", &any);
  58. if (edx1 & (1u << 5)) print_flag("MSR", &any);
  59. if (edx1 & (1u << 6)) print_flag("PAE", &any);
  60. if (edx1 & (1u << 9)) print_flag("APIC", &any);
  61. if (edx1 & (1u << 12)) print_flag("MTRR", &any);
  62. if (edx1 & (1u << 15)) print_flag("CMOV", &any);
  63. if (edx1 & (1u << 16)) print_flag("PAT", &any);
  64. if (edx1 & (1u << 17)) print_flag("PSE36", &any);
  65. if (edx1 & (1u << 23)) print_flag("MMX", &any);
  66. if (edx1 & (1u << 24)) print_flag("FXSR", &any);
  67. if (edx1 & (1u << 25)) print_flag("SSE", &any);
  68. if (edx1 & (1u << 26)) print_flag("SSE2", &any);
  69. if (edx1 & (1u << 28)) print_flag("HTT", &any);
  70. if (ecx1 & (1u << 0)) print_flag("SSE3", &any);
  71. if (ecx1 & (1u << 1)) print_flag("PCLMULQDQ", &any);
  72. if (ecx1 & (1u << 9)) print_flag("SSSE3", &any);
  73. if (ecx1 & (1u << 12)) print_flag("FMA", &any);
  74. if (ecx1 & (1u << 13)) print_flag("CX16", &any);
  75. if (ecx1 & (1u << 19)) print_flag("SSE4.1", &any);
  76. if (ecx1 & (1u << 20)) print_flag("SSE4.2", &any);
  77. if (ecx1 & (1u << 22)) print_flag("MOVBE", &any);
  78. if (ecx1 & (1u << 23)) print_flag("POPCNT", &any);
  79. if (ecx1 & (1u << 25)) print_flag("AES", &any);
  80. if (ecx1 & (1u << 26)) print_flag("XSAVE", &any);
  81. if (ecx1 & (1u << 27)) print_flag("OSXSAVE", &any);
  82. if (ecx1 & (1u << 28)) print_flag("AVX", &any);
  83. if (ecx1 & (1u << 29)) print_flag("F16C", &any);
  84. if (ecx1 & (1u << 30)) print_flag("RDRAND", &any);
  85. if (!any)
  86. io_putstr("none");
  87. io_putchar('\n');
  88. }
  89. static void print_features_leaf7(unsigned int max_leaf)
  90. {
  91. unsigned int eax7, ebx7, ecx7, edx7;
  92. int any = 0;
  93. if (max_leaf < 7)
  94. return;
  95. cpuid(7, 0, &eax7, &ebx7, &ecx7, &edx7);
  96. (void)eax7;
  97. (void)ecx7;
  98. (void)edx7;
  99. io_putstr("ext l7 ");
  100. if (ebx7 & (1u << 0)) print_flag("FSGSBASE", &any);
  101. if (ebx7 & (1u << 3)) print_flag("BMI1", &any);
  102. if (ebx7 & (1u << 5)) print_flag("AVX2", &any);
  103. if (ebx7 & (1u << 8)) print_flag("BMI2", &any);
  104. if (ebx7 & (1u << 9)) print_flag("ERMS", &any);
  105. if (ebx7 & (1u << 18)) print_flag("RDSEED", &any);
  106. if (ebx7 & (1u << 19)) print_flag("ADX", &any);
  107. if (ebx7 & (1u << 20)) print_flag("SMAP", &any);
  108. if (ebx7 & (1u << 23)) print_flag("CLFLUSHOPT", &any);
  109. if (ebx7 & (1u << 29)) print_flag("SHA", &any);
  110. if (!any)
  111. io_putstr("none");
  112. io_putchar('\n');
  113. }
  114. static void print_features_ext(unsigned int max_ext_leaf)
  115. {
  116. unsigned int eax, ebx, ecx, edx;
  117. int any = 0;
  118. if (max_ext_leaf < 0x80000001u)
  119. return;
  120. cpuid(0x80000001u, 0, &eax, &ebx, &ecx, &edx);
  121. (void)eax;
  122. (void)ebx;
  123. io_putstr("ext x ");
  124. if (edx & (1u << 11)) print_flag("SYSCALL", &any);
  125. if (edx & (1u << 20)) print_flag("NX", &any);
  126. if (edx & (1u << 27)) print_flag("RDTSCP", &any);
  127. if (edx & (1u << 29)) print_flag("LM", &any);
  128. if (ecx & (1u << 0)) print_flag("LAHF_LM", &any);
  129. if (ecx & (1u << 2)) print_flag("SVM", &any);
  130. if (ecx & (1u << 5)) print_flag("ABM", &any);
  131. if (ecx & (1u << 6)) print_flag("SSE4A", &any);
  132. if (ecx & (1u << 8)) print_flag("PREFETCHW", &any);
  133. if (!any)
  134. io_putstr("none");
  135. io_putchar('\n');
  136. }
  137. void program_cpuinfo_main(void)
  138. {
  139. unsigned int eax, ebx, ecx, edx;
  140. unsigned int ecx1, edx1, ebx1;
  141. char vendor[13];
  142. char brand[49];
  143. unsigned int max_leaf;
  144. unsigned int max_ext_leaf;
  145. unsigned int family, model, stepping;
  146. unsigned int logical_cpus = 1;
  147. unsigned int cores = 1;
  148. unsigned int threads_per_core = 1;
  149. unsigned int start_ms, end_ms, delta_ms;
  150. unsigned int t0, t1, delta_cycles;
  151. unsigned int mhz_x10;
  152. unsigned int has_brand = 0;
  153. unsigned int i;
  154. cpuid(0, 0, &max_leaf, &ebx, &ecx, &edx);
  155. ((unsigned int *)vendor)[0] = ebx;
  156. ((unsigned int *)vendor)[1] = edx;
  157. ((unsigned int *)vendor)[2] = ecx;
  158. vendor[12] = '\0';
  159. cpuid(0x80000000u, 0, &max_ext_leaf, &ebx, &ecx, &edx);
  160. io_putstr("vendor ");
  161. io_putstr(vendor);
  162. io_putchar('\n');
  163. io_putstr("cpuid max ");
  164. put_uint(max_leaf);
  165. io_putstr(" ext max ");
  166. put_uint(max_ext_leaf & 0xFFFFu);
  167. io_putchar('\n');
  168. cpuid(1, 0, &eax, &ebx, &ecx, &edx);
  169. ebx1 = ebx;
  170. ecx1 = ecx;
  171. edx1 = edx;
  172. stepping = eax & 0xF;
  173. model = (eax >> 4) & 0xF;
  174. family = (eax >> 8) & 0xF;
  175. if (family == 0xF)
  176. family += (eax >> 20) & 0xFF;
  177. if (family == 0x6 || family == 0xF)
  178. model += ((eax >> 16) & 0xF) << 4;
  179. io_putstr("family ");
  180. put_uint(family);
  181. io_putstr(" model ");
  182. put_uint(model);
  183. io_putstr(" stepping ");
  184. put_uint(stepping);
  185. io_putchar('\n');
  186. if (max_ext_leaf >= 0x80000004u) {
  187. for (i = 0; i < 48; i++)
  188. brand[i] = '\0';
  189. for (i = 0; i < 3; i++) {
  190. unsigned int *dst = (unsigned int *)(brand + i * 16);
  191. cpuid(0x80000002u + i, 0, &eax, &ebx, &ecx, &edx);
  192. dst[0] = eax;
  193. dst[1] = ebx;
  194. dst[2] = ecx;
  195. dst[3] = edx;
  196. }
  197. brand[48] = '\0';
  198. trim_right_spaces(brand);
  199. if (brand[0] != '\0')
  200. has_brand = 1;
  201. }
  202. if (has_brand) {
  203. io_putstr("brand ");
  204. io_putstr(brand);
  205. io_putchar('\n');
  206. }
  207. /* Logical processors package-wide from leaf 1 EBX[23:16]. */
  208. logical_cpus = (ebx1 >> 16) & 0xFF;
  209. if (logical_cpus == 0)
  210. logical_cpus = 1;
  211. /* Physical core count (best effort across vendors/generations). */
  212. if (max_leaf >= 4) {
  213. cpuid(4, 0, &eax, &ebx, &ecx, &edx);
  214. cores = ((eax >> 26) & 0x3F) + 1;
  215. }
  216. if (max_ext_leaf >= 0x80000008u) {
  217. cpuid(0x80000008u, 0, &eax, &ebx, &ecx, &edx);
  218. if ((ecx & 0xFFu) != 0)
  219. cores = (ecx & 0xFFu) + 1;
  220. }
  221. if (cores == 0)
  222. cores = 1;
  223. if (logical_cpus >= cores && cores != 0)
  224. threads_per_core = logical_cpus / cores;
  225. if (threads_per_core == 0)
  226. threads_per_core = 1;
  227. io_putstr("topology ");
  228. put_uint(logical_cpus);
  229. io_putstr(" logical, ");
  230. put_uint(cores);
  231. io_putstr(" cores, ");
  232. put_uint(threads_per_core);
  233. io_putstr(" threads/core\n");
  234. start_ms = get_ticks_ms();
  235. t0 = read_tsc_low();
  236. do {
  237. __asm__ volatile ("hlt");
  238. end_ms = get_ticks_ms();
  239. } while (end_ms - start_ms < 200);
  240. t1 = read_tsc_low();
  241. delta_ms = end_ms - start_ms;
  242. delta_cycles = t1 - t0;
  243. if (delta_ms == 0)
  244. delta_ms = 1;
  245. /* MHz with one decimal: cycles / (ms * 100). */
  246. mhz_x10 = delta_cycles / (delta_ms * 100);
  247. io_putstr("clock ");
  248. put_uint(mhz_x10 / 10);
  249. io_putchar('.');
  250. put_uint(mhz_x10 % 10);
  251. io_putstr(" MHz (estimated)\n");
  252. print_features_leaf1(ecx1, edx1);
  253. print_features_leaf7(max_leaf);
  254. print_features_ext(max_ext_leaf);
  255. }