1/* ----------------------------------------------------------------------- *
2 *
3 *   Copyright 2009 Erwan Velu - All Rights Reserved
4 *
5 *   Permission is hereby granted, free of charge, to any person
6 *   obtaining a copy of this software and associated documentation
7 *   files (the "Software"), to deal in the Software without
8 *   restriction, including without limitation the rights to use,
9 *   copy, modify, merge, publish, distribute, sublicense, and/or
10 *   sell copies of the Software, and to permit persons to whom
11 *   the Software is furnished to do so, subject to the following
12 *   conditions:
13 *
14 *   The above copyright notice and this permission notice shall
15 *   be included in all copies or substantial portions of the Software.
16 *
17 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 *   OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * -----------------------------------------------------------------------
27 */
28
29#include <stdio.h>
30#include <string.h>
31#include <stdlib.h>
32#include <errno.h>
33
34#include "hdt-cli.h"
35#include "hdt-common.h"
36
37void main_show_cpu(int argc __unused, char **argv __unused,
38		   struct s_hardware *hardware)
39{
40    char features[81];
41    /* We know the total number of logical cores and we
42     * know the number of cores of the first CPU. Let's consider
43     * the system as symetrical, and so compute the number of
44     * physical CPUs. This is only possible if ACPI is present */
45    if (hardware->acpi.madt.processor_local_apic_count > 0) {
46	more_printf("CPU (%d logical / %d phys)\n",
47		    hardware->acpi.madt.processor_local_apic_count,
48		    hardware->physical_cpu_count);
49    } else
50	more_printf("CPU\n");
51    more_printf(" Manufacturer : %s \n", hardware->cpu.vendor);
52    more_printf(" Product      : %s \n", hardware->cpu.model);
53    more_printf(" CPU Cores    : %d \n", hardware->cpu.num_cores);
54    if (hardware->dmi.processor.thread_count != 0)
55        more_printf(" CPU Threads  : %d \n", hardware->dmi.processor.thread_count);
56    more_printf(" L2 Cache     : %dK\n", hardware->cpu.l2_cache_size);
57
58    memset(features, 0, sizeof(features));
59    snprintf(features, sizeof(features), " Features     : %d Mhz : ",
60	     hardware->dmi.processor.current_speed);
61    if (hardware->cpu.flags.lm)
62	strcat(features, "x86_64 64bit ");
63    else
64	strcat(features, "x86 32bit ");
65    if (hardware->cpu.flags.smp)
66	strcat(features, "SMP ");
67
68    /* This CPU is featuring Intel or AMD Virtualisation Technology */
69    if (hardware->cpu.flags.vmx || hardware->cpu.flags.svm)
70	strcat(features, "HwVIRT ");
71
72    more_printf("%s\n", features);
73}
74
75/* Let's compute the cpu flags display
76 * We have to maximize the number of flags per line */
77static void show_flag(char *buffer, bool flag, char *flag_name, bool flush)
78{
79    char output_buffer[81];
80    /* Flush is only set when no more flags are present
81     * When it's set, or if the line is complete,
82     * we have to end the string computation and display the line.
83     * Before adding the flag into the buffer, let's check that adding it
84     * will not overflow the rendering.*/
85    if ((((strlen(buffer) + strlen(flag_name)) > 66) && flag) || flush) {
86	snprintf(output_buffer, sizeof output_buffer, "Flags     : %s\n",
87		 buffer);
88	more_printf("%s", output_buffer);
89	memset(buffer, 0, sizeof(buffer));
90	if (flush)
91	    return;
92    }
93    /* Let's add the flag name only if the flag is present */
94    if (flag)
95	strcat(buffer, flag_name);
96}
97
98static void show_cpu(int argc __unused, char **argv __unused,
99		     struct s_hardware *hardware)
100{
101    char buffer[81];
102    reset_more_printf();
103    /* We know the total number of logical cores and we
104     * know the number of cores of the first CPU. Let's consider
105     * the system as symetrical, and so compute the number of
106     * physical CPUs. This is only possible if ACPI is present*/
107    if (hardware->acpi.madt.processor_local_apic_count > 0) {
108	more_printf("CPU (%d logical / %d phys)\n",
109		    hardware->acpi.madt.processor_local_apic_count,
110		    hardware->acpi.madt.processor_local_apic_count /
111		    hardware->cpu.num_cores);
112    } else
113	more_printf("CPU\n");
114    more_printf("Vendor    : %s\n", hardware->cpu.vendor);
115    more_printf("Model     : %s\n", hardware->cpu.model);
116    more_printf("CPU Cores : %d\n", hardware->cpu.num_cores);
117    if (hardware->dmi.processor.core_enabled != 0)
118        more_printf("CPU Enable: %d\n", hardware->dmi.processor.core_enabled);
119    if (hardware->dmi.processor.thread_count != 0)
120        more_printf("CPU Thread: %d \n", hardware->dmi.processor.thread_count);
121    more_printf("L1 Cache  : %dK + %dK (I + D) \n",
122		hardware->cpu.l1_instruction_cache_size,
123		hardware->cpu.l1_data_cache_size);
124    more_printf("L2 Cache  : %dK\n", hardware->cpu.l2_cache_size);
125    more_printf("Family ID : %d\n", hardware->cpu.family);
126    more_printf("Model  ID : %d\n", hardware->cpu.model_id);
127    more_printf("Stepping  : %d\n", hardware->cpu.stepping);
128    if (hardware->is_dmi_valid) {
129	more_printf("FSB       : %d MHz\n",
130		    hardware->dmi.processor.external_clock);
131	more_printf("Cur. Speed: %d MHz\n",
132		    hardware->dmi.processor.current_speed);
133	more_printf("Max Speed : %d MHz\n", hardware->dmi.processor.max_speed);
134	more_printf("Upgrade   : %s\n", hardware->dmi.processor.upgrade);
135	more_printf("Voltage   : %d.%02d\n",
136		    hardware->dmi.processor.voltage_mv / 1000,
137		    hardware->dmi.processor.voltage_mv -
138		    ((hardware->dmi.processor.voltage_mv / 1000) * 1000));
139    }
140
141    if (hardware->cpu.flags.smp) {
142	more_printf("SMP       : yes\n");
143    } else {
144	more_printf("SMP       : no\n");
145    }
146
147    if (hardware->cpu.flags.lm) {
148	more_printf("x86_64    : yes\n");
149    } else {
150	more_printf("x86_64    : no\n");
151    }
152
153    if (hardware->cpu.flags.vmx || hardware->cpu.flags.svm) {
154	more_printf("HwVirt    : yes\n");
155    } else {
156	more_printf("HwVirt    : no\n");
157    }
158
159    /* Let's display the supported cpu flags */
160    memset(buffer, 0, sizeof(buffer));
161    show_flag(buffer, hardware->cpu.flags.fpu, "fpu ", false);
162    show_flag(buffer, hardware->cpu.flags.vme, "vme ", false);
163    show_flag(buffer, hardware->cpu.flags.de, "de ", false);
164    show_flag(buffer, hardware->cpu.flags.pse, "pse ", false);
165    show_flag(buffer, hardware->cpu.flags.tsc, "tsc ", false);
166    show_flag(buffer, hardware->cpu.flags.msr, "msr ", false);
167    show_flag(buffer, hardware->cpu.flags.pae, "pae ", false);
168    show_flag(buffer, hardware->cpu.flags.mce, "mce ", false);
169    show_flag(buffer, hardware->cpu.flags.cx8, "cx8 ", false);
170    show_flag(buffer, hardware->cpu.flags.apic, "apic ", false);
171    show_flag(buffer, hardware->cpu.flags.sep, "sep ", false);
172    show_flag(buffer, hardware->cpu.flags.mtrr, "mtrr ", false);
173    show_flag(buffer, hardware->cpu.flags.pge, "pge ", false);
174    show_flag(buffer, hardware->cpu.flags.mca, "mca ", false);
175    show_flag(buffer, hardware->cpu.flags.cmov, "cmov ", false);
176    show_flag(buffer, hardware->cpu.flags.pat, "pat ", false);
177    show_flag(buffer, hardware->cpu.flags.pse_36, "pse_36 ", false);
178    show_flag(buffer, hardware->cpu.flags.psn, "psn ", false);
179    show_flag(buffer, hardware->cpu.flags.clflsh, "clflsh ", false);
180    show_flag(buffer, hardware->cpu.flags.dts, "dts ", false);
181    show_flag(buffer, hardware->cpu.flags.acpi, "acpi ", false);
182    show_flag(buffer, hardware->cpu.flags.mmx, "mmx ", false);
183    show_flag(buffer, hardware->cpu.flags.sse, "sse ", false);
184    show_flag(buffer, hardware->cpu.flags.sse2, "sse2 ", false);
185    show_flag(buffer, hardware->cpu.flags.ss, "ss ", false);
186    show_flag(buffer, hardware->cpu.flags.htt, "ht ", false);
187    show_flag(buffer, hardware->cpu.flags.acc, "acc ", false);
188    show_flag(buffer, hardware->cpu.flags.syscall, "syscall ", false);
189    show_flag(buffer, hardware->cpu.flags.mp, "mp ", false);
190    show_flag(buffer, hardware->cpu.flags.nx, "nx ", false);
191    show_flag(buffer, hardware->cpu.flags.mmxext, "mmxext ", false);
192    show_flag(buffer, hardware->cpu.flags.lm, "lm ", false);
193    show_flag(buffer, hardware->cpu.flags.nowext, "3dnowext ", false);
194    show_flag(buffer, hardware->cpu.flags.now, "3dnow! ", false);
195    show_flag(buffer, hardware->cpu.flags.svm, "svm ", false);
196    show_flag(buffer, hardware->cpu.flags.vmx, "vmx ", false);
197    show_flag(buffer, hardware->cpu.flags.pbe, "pbe ", false);
198    show_flag(buffer, hardware->cpu.flags.fxsr_opt, "fxsr_opt ", false);
199    show_flag(buffer, hardware->cpu.flags.gbpages, "gbpages ", false);
200    show_flag(buffer, hardware->cpu.flags.rdtscp, "rdtscp ", false);
201    show_flag(buffer, hardware->cpu.flags.pni, "pni ", false);
202    show_flag(buffer, hardware->cpu.flags.pclmulqd, "pclmulqd ", false);
203    show_flag(buffer, hardware->cpu.flags.dtes64, "dtes64 ", false);
204    show_flag(buffer, hardware->cpu.flags.smx, "smx ", false);
205    show_flag(buffer, hardware->cpu.flags.est, "est ", false);
206    show_flag(buffer, hardware->cpu.flags.tm2, "tm2 ", false);
207    show_flag(buffer, hardware->cpu.flags.sse3, "sse3 ", false);
208    show_flag(buffer, hardware->cpu.flags.fma, "fma ", false);
209    show_flag(buffer, hardware->cpu.flags.cx16, "cx16 ", false);
210    show_flag(buffer, hardware->cpu.flags.xtpr, "xtpr ", false);
211    show_flag(buffer, hardware->cpu.flags.pdcm, "pdcm ", false);
212    show_flag(buffer, hardware->cpu.flags.dca, "dca ", false);
213    show_flag(buffer, hardware->cpu.flags.xmm4_1, "xmm4_1 ", false);
214    show_flag(buffer, hardware->cpu.flags.xmm4_2, "xmm4_2 ", false);
215    show_flag(buffer, hardware->cpu.flags.x2apic, "x2apic ", false);
216    show_flag(buffer, hardware->cpu.flags.movbe, "movbe ", false);
217    show_flag(buffer, hardware->cpu.flags.popcnt, "popcnt ", false);
218    show_flag(buffer, hardware->cpu.flags.aes, "aes ", false);
219    show_flag(buffer, hardware->cpu.flags.xsave, "xsave ", false);
220    show_flag(buffer, hardware->cpu.flags.osxsave, "osxsave ", false);
221    show_flag(buffer, hardware->cpu.flags.avx, "avx ", false);
222    show_flag(buffer, hardware->cpu.flags.hypervisor, "hypervisor ", false);
223    show_flag(buffer, hardware->cpu.flags.ace2, "ace2 ", false);
224    show_flag(buffer, hardware->cpu.flags.ace2_en, "ace2_en ", false);
225    show_flag(buffer, hardware->cpu.flags.phe, "phe ", false);
226    show_flag(buffer, hardware->cpu.flags.phe_en, "phe_en ", false);
227    show_flag(buffer, hardware->cpu.flags.pmm, "pmm ", false);
228    show_flag(buffer, hardware->cpu.flags.pmm_en, "pmm_en ", false);
229    show_flag(buffer, hardware->cpu.flags.extapic, "extapic ", false);
230    show_flag(buffer, hardware->cpu.flags.cr8_legacy, "cr8_legacy ", false);
231    show_flag(buffer, hardware->cpu.flags.abm, "abm ", false);
232    show_flag(buffer, hardware->cpu.flags.sse4a, "sse4a ", false);
233    show_flag(buffer, hardware->cpu.flags.misalignsse, "misalignsse ", false);
234    show_flag(buffer, hardware->cpu.flags.nowprefetch, "3dnowprefetch ", false);
235    show_flag(buffer, hardware->cpu.flags.osvw, "osvw ", false);
236    show_flag(buffer, hardware->cpu.flags.ibs, "ibs ", false);
237    show_flag(buffer, hardware->cpu.flags.sse5, "sse5 ", false);
238    show_flag(buffer, hardware->cpu.flags.skinit, "skinit ", false);
239    show_flag(buffer, hardware->cpu.flags.wdt, "wdt ", false);
240    show_flag(buffer, hardware->cpu.flags.ida, "ida ", false);
241    show_flag(buffer, hardware->cpu.flags.arat, "arat ", false);
242    show_flag(buffer, hardware->cpu.flags.tpr_shadow, "tpr_shadow ", false);
243    show_flag(buffer, hardware->cpu.flags.vnmi, "vnmi ", false);
244    show_flag(buffer, hardware->cpu.flags.flexpriority, "flexpriority ", false);
245    show_flag(buffer, hardware->cpu.flags.ept, "ept ", false);
246    show_flag(buffer, hardware->cpu.flags.vpid, "vpid ", false);
247
248    /* No more flags, let's display the remaining flags */
249    show_flag(buffer, false, "", true);
250}
251
252struct cli_module_descr cpu_show_modules = {
253    .modules = NULL,
254    .default_callback = show_cpu,
255};
256
257struct cli_mode_descr cpu_mode = {
258    .mode = CPU_MODE,
259    .name = CLI_CPU,
260    .default_modules = NULL,
261    .show_modules = &cpu_show_modules,
262    .set_modules = NULL,
263};
264