From: Vijaya Kumar K <vijaya.ku...@caviumnetworks.com> utils cannot read target cpu information to fetch cpu information to implement cpu specific features or erratas. For this parse /proc/cpuinfo and fetch cpu information.
For now this helper only fetches cpu information for arm architectures. Signed-off-by: Vijaya Kumar K <vija...@caviumnetworks.com> Signed-off-by: Suresh <ksur...@caviumnetworks.com> --- include/qemu-common.h | 11 ++++++ util/Makefile.objs | 1 + util/cpuinfo.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) diff --git a/include/qemu-common.h b/include/qemu-common.h index 163bcbb..364aa0a 100644 --- a/include/qemu-common.h +++ b/include/qemu-common.h @@ -170,4 +170,15 @@ void page_size_init(void); * returned. */ bool dump_in_progress(void); +/* + * cpu info structure read from /proc/cpuinfo + */ + +struct cpu_info { + uint32_t imp; + uint32_t arch; + uint32_t part; +}; + +void qemu_read_cpu_info(struct cpu_info *cinf); #endif diff --git a/util/Makefile.objs b/util/Makefile.objs index a8a777e..59cde64 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -32,3 +32,4 @@ util-obj-y += buffer.o util-obj-y += timed-average.o util-obj-y += base64.o util-obj-y += log.o +util-obj-y += cpuinfo.o diff --git a/util/cpuinfo.c b/util/cpuinfo.c new file mode 100644 index 0000000..e049672 --- /dev/null +++ b/util/cpuinfo.c @@ -0,0 +1,94 @@ +/* + * Dealing with /proc/cpuinfo + * + * Copyright (C) 2016 Cavium, Inc. + * + * Authors: + * Vijaya Kumar K <vija...@caviumnetworks.com> + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 + * or later. See the COPYING.LIB file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "qemu-common.h" +#include <string.h> + +#if defined(__arm__) || defined(__aarch64__) +static uint32_t read_arm_cpu_implementer(char *str) +{ + char *match; + uint32_t imp = 0; + + match = strstr(str, "CPU implementer"); + if (match != NULL) { + sscanf(match, "CPU implementer : 0x%x", &imp); + } + + return imp; +} + +static uint32_t read_arm_cpu_architecture(char *str) +{ + char *match; + uint32_t arch = 0; + + match = strstr(str, "CPU architecture"); + if (match != NULL) { + sscanf(match, "CPU architecture: %d", &arch); + } + + return arch; +} + +static uint32_t read_arm_cpu_part(char *str) +{ + char *match; + uint32_t part = 0; + + match = strstr(str, "CPU part"); + if (match != NULL) { + sscanf(match, "CPU part : 0x%x", &part); + } + + return part; +} +#endif + +void qemu_read_cpu_info(struct cpu_info *cinf) +{ + FILE *fp; + char *buf; +#define BUF_SIZE 1024 + size_t bytes_read; + + cinf->imp = cinf->arch = cinf->part = 0; + fp = fopen("/proc/cpuinfo", "r"); + if (!fp) { + return; + } + + buf = g_malloc0(BUF_SIZE); + if (!buf) { + fclose(fp); + return; + } + + /* Read the contents of /proc/cpuinfo into the buffer. */ + bytes_read = fread(buf, 1, BUF_SIZE, fp); + fclose(fp); + + if (bytes_read == 0) { + g_free(buf); + return; + } + + buf[bytes_read] = '\0'; + +#if defined(__arm__) || defined(__aarch64__) + cinf->imp = read_arm_cpu_implementer(buf); + cinf->arch = read_arm_cpu_architecture(buf); + cinf->part = read_arm_cpu_part(buf); +#endif + g_free(buf); +} -- 1.7.9.5