From 67a8e57c784ccbce4989c39dd44c70ae7f5aeda4 Mon Sep 17 00:00:00 2001 From: jianchunfu Date: Thu, 4 Aug 2022 15:37:47 +0800 Subject: [PATCH 1/4] target/ppc: add error report when fopen fails in kvmppc_read_int_dt() Use an Error pointer to report the error back to the caller. While we're at it, return '0' instead of '-1' on error since the function is supposed to return an uint64_t. Signed-off-by: jianchunfu --- target/ppc/kvm.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c index dc93b99189..63382256c3 100644 --- a/target/ppc/kvm.c +++ b/target/ppc/kvm.c @@ -1895,7 +1895,7 @@ static int kvmppc_find_cpu_dt(char *buf, int buf_len) return 0; } -static uint64_t kvmppc_read_int_dt(const char *filename) +static uint64_t kvmppc_read_int_dt(const char *filename, Error **errp) { union { uint32_t v32; @@ -1906,7 +1906,8 @@ static uint64_t kvmppc_read_int_dt(const char *filename) f = fopen(filename, "rb"); if (!f) { - return -1; + error_setg_errno(errp, errno, "error opening %s", filename); + return 0; } len = fread(&u, 1, sizeof(u), f); @@ -1937,7 +1938,7 @@ static uint64_t kvmppc_read_int_cpu_dt(const char *propname) } tmp = g_strdup_printf("%s/%s", buf, propname); - val = kvmppc_read_int_dt(tmp); + val = kvmppc_read_int_dt(tmp, NULL); g_free(tmp); return val; -- Gitee From b167d664414e7d4a5a7a7058bc4c7699f6e66d48 Mon Sep 17 00:00:00 2001 From: jianchunfu Date: Thu, 4 Aug 2022 15:57:46 +0800 Subject: [PATCH 2/4] target/ppc: enhance error report in kvmppc_read_int_cpu_dt() First and foremost, the function can't return '-1' when an error occurs because the return type is set to uint64_t. Let's fix that. After that, the function can't simply return 0 whether an error happened and call it a day. We must provide a way of letting callers know if the zero return is legitimate or due to an error. Add an Error pointer to kvmppc_read_int_cpu_dt() that will be filled with an appropriate error, if one occurs. Callers are then free to pass an Error pointer and handle it. Signed-off-by: jianchunfu --- target/ppc/kvm.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c index 63382256c3..d64d7c5b4a 100644 --- a/target/ppc/kvm.c +++ b/target/ppc/kvm.c @@ -1925,20 +1925,22 @@ static uint64_t kvmppc_read_int_dt(const char *filename, Error **errp) /* * Read a CPU node property from the host device tree that's a single - * integer (32-bit or 64-bit). Returns 0 if anything goes wrong - * (can't find or open the property, or doesn't understand the format) + * integer (32-bit or 64-bit). Returns 0 and set errp if anything goes + * wrong (can't find or open the property, or doesn't understand the + * format) */ -static uint64_t kvmppc_read_int_cpu_dt(const char *propname) +static uint64_t kvmppc_read_int_cpu_dt(const char *propname, Error **errp) { char buf[PATH_MAX], *tmp; uint64_t val; if (kvmppc_find_cpu_dt(buf, sizeof(buf))) { - return -1; + error_setg(errp, "Failed to read CPU property %s", propname); + return 0; } tmp = g_strdup_printf("%s/%s", buf, propname); - val = kvmppc_read_int_dt(tmp, NULL); + val = kvmppc_read_int_dt(tmp, errp); g_free(tmp); return val; @@ -1946,12 +1948,12 @@ static uint64_t kvmppc_read_int_cpu_dt(const char *propname) uint64_t kvmppc_get_clockfreq(void) { - return kvmppc_read_int_cpu_dt("clock-frequency"); + return kvmppc_read_int_cpu_dt("clock-frequency", NULL); } static int kvmppc_get_dec_bits(void) { - int nr_bits = kvmppc_read_int_cpu_dt("ibm,dec-bits"); + int nr_bits = kvmppc_read_int_cpu_dt("ibm,dec-bits", NULL); if (nr_bits > 0) { return nr_bits; @@ -2336,8 +2338,8 @@ static void alter_insns(uint64_t *word, uint64_t flags, bool on) static void kvmppc_host_cpu_class_init(ObjectClass *oc, void *data) { PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(oc); - uint32_t dcache_size = kvmppc_read_int_cpu_dt("d-cache-size"); - uint32_t icache_size = kvmppc_read_int_cpu_dt("i-cache-size"); + uint32_t dcache_size = kvmppc_read_int_cpu_dt("d-cache-size", NULL); + uint32_t icache_size = kvmppc_read_int_cpu_dt("i-cache-size", NULL); /* Now fix up the class with information we can query from the host */ pcc->pvr = mfpvr(); -- Gitee From 02a15861235d29dcf89b61bf88fed2ec4ccee9dc Mon Sep 17 00:00:00 2001 From: jianchunfu Date: Thu, 4 Aug 2022 16:41:35 +0800 Subject: [PATCH 3/4] target/ppc: use g_autofree in kvmppc_read_int_cpu_dt() This spares us a g_free() call. Let's also not use 'val' and return the value of kvmppc_read_int_dt() directly. Signed-off-by: jianchunfu --- target/ppc/kvm.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c index d64d7c5b4a..1a6c6b6fa0 100644 --- a/target/ppc/kvm.c +++ b/target/ppc/kvm.c @@ -1931,8 +1931,8 @@ static uint64_t kvmppc_read_int_dt(const char *filename, Error **errp) */ static uint64_t kvmppc_read_int_cpu_dt(const char *propname, Error **errp) { - char buf[PATH_MAX], *tmp; - uint64_t val; + g_autofree char *tmp = NULL; + char buf[PATH_MAX]; if (kvmppc_find_cpu_dt(buf, sizeof(buf))) { error_setg(errp, "Failed to read CPU property %s", propname); @@ -1940,10 +1940,8 @@ static uint64_t kvmppc_read_int_cpu_dt(const char *propname, Error **errp) } tmp = g_strdup_printf("%s/%s", buf, propname); - val = kvmppc_read_int_dt(tmp, errp); - g_free(tmp); - return val; + return kvmppc_read_int_dt(tmp, errp); } uint64_t kvmppc_get_clockfreq(void) -- Gitee From 584a648e0de0b0bda49e87349f2db3b2f0f87c33 Mon Sep 17 00:00:00 2001 From: jianchunfu Date: Thu, 4 Aug 2022 16:47:26 +0800 Subject: [PATCH 4/4] target/ppc: exit(1) on failure in kvmppc_get_clockfreq() When running under KVM accel it is expected to have 'clock-frequency' in the DT. Not having this attribute is too risky for both the machine emulation and userspace. We have a way of telling whether this error scenario might happen or not via kvmppc_read_int_cpu_dt() now being able to report errors. From now on, when running KVM, we will assume that 'clock-frequency' will always be present in the DT. Signed-off-by: jianchunfu --- target/ppc/kvm.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c index 1a6c6b6fa0..d73563045b 100644 --- a/target/ppc/kvm.c +++ b/target/ppc/kvm.c @@ -1944,9 +1944,24 @@ static uint64_t kvmppc_read_int_cpu_dt(const char *propname, Error **errp) return kvmppc_read_int_dt(tmp, errp); } +/* + * Read the clock-frequency from the DT. On error (e.g. + * 'clock-frequency' is not present in the DT) will + * report an error and exit(1). + */ uint64_t kvmppc_get_clockfreq(void) { - return kvmppc_read_int_cpu_dt("clock-frequency", NULL); + Error *local_err = NULL; + int ret; + + ret = kvmppc_read_int_cpu_dt("clock-frequency", &local_err); + + if (local_err) { + error_report_err(local_err); + exit(1); + } + + return ret; } static int kvmppc_get_dec_bits(void) -- Gitee