cpuinfo.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. static void print_cpuid_frequency(unsigned int max_leaf)
  138. {
  139. unsigned int eax, ebx, ecx, edx;
  140. if (max_leaf >= 0x15u) {
  141. cpuid(0x15u, 0, &eax, &ebx, &ecx, &edx);
  142. if (eax != 0 && ebx != 0) {
  143. io_putstr("cpuid15 ratio ");
  144. put_uint(ebx);
  145. io_putchar('/');
  146. put_uint(eax);
  147. if (ecx != 0) {
  148. io_putstr(" crystal ");
  149. put_uint(ecx / 1000000u);
  150. io_putstr(" MHz");
  151. }
  152. io_putchar('\n');
  153. }
  154. }
  155. if (max_leaf >= 0x16u) {
  156. cpuid(0x16u, 0, &eax, &ebx, &ecx, &edx);
  157. if (eax != 0 || ebx != 0 || ecx != 0) {
  158. io_putstr("cpuid16 base ");
  159. put_uint(eax);
  160. io_putstr(" max ");
  161. put_uint(ebx);
  162. io_putstr(" bus ");
  163. put_uint(ecx);
  164. io_putstr(" MHz\n");
  165. }
  166. }
  167. }
  168. static void parse_family_model(unsigned int eax1,
  169. unsigned int *family_out,
  170. unsigned int *model_out,
  171. unsigned int *stepping_out)
  172. {
  173. unsigned int family = (eax1 >> 8) & 0xF;
  174. unsigned int model = (eax1 >> 4) & 0xF;
  175. unsigned int stepping = eax1 & 0xF;
  176. if (family == 0xF)
  177. family += (eax1 >> 20) & 0xFF;
  178. if (family == 0x6 || family == 0xF)
  179. model += ((eax1 >> 16) & 0xF) << 4;
  180. *family_out = family;
  181. *model_out = model;
  182. *stepping_out = stepping;
  183. }
  184. void program_cpuinfo_main(void)
  185. {
  186. unsigned int eax, ebx, ecx, edx;
  187. unsigned int ecx1, edx1, ebx1;
  188. char vendor[13];
  189. char brand[49];
  190. unsigned int max_leaf;
  191. unsigned int max_ext_leaf;
  192. unsigned int family, model, stepping;
  193. unsigned int logical_cpus = 1;
  194. unsigned int cores = 1;
  195. unsigned int threads_per_core = 1;
  196. unsigned int start_ms, end_ms, delta_ms;
  197. unsigned int t0, t1, delta_cycles;
  198. unsigned int mhz_x10;
  199. unsigned int has_brand = 0;
  200. unsigned int i;
  201. cpuid(0, 0, &max_leaf, &ebx, &ecx, &edx);
  202. ((unsigned int *)vendor)[0] = ebx;
  203. ((unsigned int *)vendor)[1] = edx;
  204. ((unsigned int *)vendor)[2] = ecx;
  205. vendor[12] = '\0';
  206. cpuid(0x80000000u, 0, &max_ext_leaf, &ebx, &ecx, &edx);
  207. io_putstr("vendor ");
  208. io_putstr(vendor);
  209. io_putchar('\n');
  210. io_putstr("cpuid max ");
  211. put_uint(max_leaf);
  212. io_putstr(" ext max ");
  213. put_uint(max_ext_leaf & 0xFFFFu);
  214. io_putchar('\n');
  215. cpuid(1, 0, &eax, &ebx, &ecx, &edx);
  216. ebx1 = ebx;
  217. ecx1 = ecx;
  218. edx1 = edx;
  219. parse_family_model(eax, &family, &model, &stepping);
  220. io_putstr("family ");
  221. put_uint(family);
  222. io_putstr(" model ");
  223. put_uint(model);
  224. io_putstr(" stepping ");
  225. put_uint(stepping);
  226. io_putchar('\n');
  227. if (max_ext_leaf >= 0x80000004u) {
  228. for (i = 0; i < 48; i++)
  229. brand[i] = '\0';
  230. for (i = 0; i < 3; i++) {
  231. unsigned int *dst = (unsigned int *)(brand + i * 16);
  232. cpuid(0x80000002u + i, 0, &eax, &ebx, &ecx, &edx);
  233. dst[0] = eax;
  234. dst[1] = ebx;
  235. dst[2] = ecx;
  236. dst[3] = edx;
  237. }
  238. brand[48] = '\0';
  239. trim_right_spaces(brand);
  240. if (brand[0] != '\0')
  241. has_brand = 1;
  242. }
  243. if (has_brand) {
  244. io_putstr("brand ");
  245. io_putstr(brand);
  246. io_putchar('\n');
  247. }
  248. /* Logical processors package-wide from leaf 1 EBX[23:16]. */
  249. logical_cpus = (ebx1 >> 16) & 0xFF;
  250. if (logical_cpus == 0)
  251. logical_cpus = 1;
  252. /* Physical core count (best effort across vendors/generations). */
  253. if (max_leaf >= 4) {
  254. cpuid(4, 0, &eax, &ebx, &ecx, &edx);
  255. cores = ((eax >> 26) & 0x3F) + 1;
  256. }
  257. if (max_ext_leaf >= 0x80000008u) {
  258. cpuid(0x80000008u, 0, &eax, &ebx, &ecx, &edx);
  259. if ((ecx & 0xFFu) != 0)
  260. cores = (ecx & 0xFFu) + 1;
  261. }
  262. if (cores == 0)
  263. cores = 1;
  264. if (logical_cpus >= cores && cores != 0)
  265. threads_per_core = logical_cpus / cores;
  266. if (threads_per_core == 0)
  267. threads_per_core = 1;
  268. io_putstr("topology ");
  269. put_uint(logical_cpus);
  270. io_putstr(" logical, ");
  271. put_uint(cores);
  272. io_putstr(" cores, ");
  273. put_uint(threads_per_core);
  274. io_putstr(" threads/core\n");
  275. print_cpuid_frequency(max_leaf);
  276. start_ms = get_ticks_ms();
  277. t0 = read_tsc_low();
  278. do {
  279. __asm__ volatile ("hlt");
  280. end_ms = get_ticks_ms();
  281. } while (end_ms - start_ms < 200);
  282. t1 = read_tsc_low();
  283. delta_ms = end_ms - start_ms;
  284. delta_cycles = t1 - t0;
  285. if (delta_ms == 0)
  286. delta_ms = 1;
  287. /* MHz with one decimal: cycles / (ms * 100). */
  288. mhz_x10 = delta_cycles / (delta_ms * 100);
  289. io_putstr("clock tsc ");
  290. put_uint(mhz_x10 / 10);
  291. io_putchar('.');
  292. put_uint(mhz_x10 % 10);
  293. io_putstr(" MHz (sampled)\n");
  294. print_features_leaf1(ecx1, edx1);
  295. print_features_leaf7(max_leaf);
  296. print_features_ext(max_ext_leaf);
  297. }