On Thu, Apr 09, 2020 at 11:28:04 +0100, Daniel P. Berrangé wrote: > On Thu, Apr 02, 2020 at 05:03:59PM +0800, Zhenyu Zheng wrote: > > Introduce getHost support for ARM CPU driver, read > > CPU vendor_id, part_id and flags from registers > > directly. > > > > Signed-off-by: Zhenyu Zheng <zhengzhenyulixi@xxxxxxxxx> > > --- > > src/cpu/cpu_arm.c | 194 +++++++++++++++++++++++++++++++++++++++++++++- > > 1 file changed, 193 insertions(+), 1 deletion(-) > > > > diff --git a/src/cpu/cpu_arm.c b/src/cpu/cpu_arm.c > > index 88b4d91946..c8f5ce7e8a 100644 > > --- a/src/cpu/cpu_arm.c > > +++ b/src/cpu/cpu_arm.c > > @@ -21,6 +21,8 @@ > > */ > > > > #include <config.h> > > +#include <asm/hwcap.h> > > +#include <sys/auxv.h> > > > > #include "viralloc.h" > > #include "cpu.h" > > @@ -31,6 +33,10 @@ > > #include "virxml.h" > > > > #define VIR_FROM_THIS VIR_FROM_CPU > > +/* Shift bit mask for parsing cpu flags */ > > +#define BIT_SHIFTS(n) (1UL << (n)) > > +/* The current max number of cpu flags on ARM is 32 */ > > +#define MAX_CPU_FLAGS 32 > > > > VIR_LOG_INIT("cpu.cpu_arm"); > > > > @@ -498,14 +504,200 @@ virCPUarmValidateFeatures(virCPUDefPtr cpu) > > return 0; > > } > > > > +/** > > + * armCpuDataFromRegs: > > + * > > + * @data: 64-bit arm CPU specific data > > + * > > + * Fetches CPU vendor_id and part_id from MIDR_EL1 register, parse CPU > > + * flags from AT_HWCAP. There are currently 32 valid flags on ARM arch > > + * represented by each bit. > > + */ > > +static int > > +armCpuDataFromRegs(virCPUarmData *data) > > virCPUarmDataFromRegs() > > > +{ > > + /* Generate human readable flag list according to the order of */ > > + /* AT_HWCAP bit map */ > > + const char *flag_list[MAX_CPU_FLAGS] = { > > + "fp", "asimd", "evtstrm", "aes", "pmull", "sha1", "sha2", > > + "crc32", "atomics", "fphp", "asimdhp", "cpuid", "asimdrdm", > > + "jscvt", "fcma", "lrcpc", "dcpop", "sha3", "sm3", "sm4", > > + "asimddp", "sha512", "sve", "asimdfhm", "dit", "uscat", > > + "ilrcpc", "flagm", "ssbs", "sb", "paca", "pacg"}; > > + unsigned long cpuid, hwcaps; > > + char **features = NULL; > > + char *cpu_feature_str = NULL; > > g_autofree > > > + int cpu_feature_index = 0; > > + size_t i; > > + > > + if (!(getauxval(AT_HWCAP) & HWCAP_CPUID)) { > > + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", > > + _("CPUID registers unavailable")); > > + return -1; > > + } > > Indent messed up here > > > + > > + /* read the cpuid data from MIDR_EL1 register */ > > + asm("mrs %0, MIDR_EL1" : "=r" (cpuid)); > > + VIR_DEBUG("CPUID read from register: 0x%016lx", cpuid); > > + > > + /* parse the coresponding part_id bits */ > > + data->pvr = cpuid>>4&0xFFF; > > + /* parse the coresponding vendor_id bits */ > > + data->vendor_id = cpuid>>24&0xFF; > > + > > + hwcaps = getauxval(AT_HWCAP); > > + VIR_DEBUG("CPU flags read from register: 0x%016lx", hwcaps); > > + > > + if (VIR_ALLOC_N(features, MAX_CPU_FLAGS) < 0) > > + return -1; > > + > > + /* shift bit map mask to parse for CPU flags */ > > + for (i = 0; i< MAX_CPU_FLAGS; i++) { > > + if (hwcaps & BIT_SHIFTS(i)) { > > + features[cpu_feature_index] = g_strdup(flag_list[i]); > > + cpu_feature_index++; > > + } > > + } > > Indent of } messed up again. > > > + > > + if (cpu_feature_index > 1) { > > + cpu_feature_str = virStringListJoin((const char **)features, " "); > > + if (!cpu_feature_str) > > + goto cleanup; > > + } > > + data->features = g_strdup(cpu_feature_str); > > + > > + return 0; > > + > > + cleanup: > > This should be called "error", since this label is only > visited for fatal errors. > > > + virStringListFree(features); > > + VIR_FREE(cpu_feature_str); > > Can avoid the VIR_FREE with g_autofree. virStringListFree can be avoided too if features is declared with VIR_AUTOSTRINGLIST. Jirka