ucmd_cpuinfo.c 8.2 KB

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