From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: qemu-arm@nongnu.org, "Nicholas Piggin" <npiggin@gmail.com>,
"Edgar E. Iglesias" <edgar.iglesias@gmail.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Akihiko Odaki" <akihiko.odaki@daynix.com>,
qemu-ppc@nongnu.org,
"Richard Henderson" <richard.henderson@linaro.org>,
"Thomas Huth" <thuth@redhat.com>,
"David Gibson" <david@gibson.dropbear.id.au>,
"Daniel Henrique Barboza" <danielhb413@gmail.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Ilya Leoshkevich" <iii@linux.ibm.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Cédric Le Goater" <clg@kaod.org>,
"Peter Maydell" <peter.maydell@linaro.org>,
qemu-s390x@nongnu.org,
"Wainer dos Santos Moschetta" <wainersm@redhat.com>,
"David Hildenbrand" <david@redhat.com>,
"Pierrick Bouvier" <pierrick.bouvier@linaro.org>
Subject: Re: [PATCH v2 04/11] gdbstub: introduce target independent gdb register helper
Date: Mon, 24 Mar 2025 10:32:42 +0000 [thread overview]
Message-ID: <87ikny4t2t.fsf@draig.linaro.org> (raw)
In-Reply-To: <20250324102142.67022-5-alex.bennee@linaro.org> ("Alex Bennée"'s message of "Mon, 24 Mar 2025 10:21:35 +0000")
Alex Bennée <alex.bennee@linaro.org> writes:
> The current helper.h functions rely on hard coded assumptions about
> target endianess to use the tswap macros. We also end up double
> swapping a bunch of values if the target can run in multiple endianess
> modes. Avoid this by getting the target to pass the endianess and size
> via a MemOp and fixing up appropriately.
>
> Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
>
> ---
> v2
> - use unsigned consistently
> - fix some rouge whitespace
> - add typed reg32/64 wrappers
> - pass void * to underlying helper to avoid casting
> ---
> include/gdbstub/registers.h | 55 +++++++++++++++++++++++++++++++++++++
> gdbstub/gdbstub.c | 23 ++++++++++++++++
> 2 files changed, 78 insertions(+)
> create mode 100644 include/gdbstub/registers.h
>
> diff --git a/include/gdbstub/registers.h b/include/gdbstub/registers.h
> new file mode 100644
> index 0000000000..2220f58efe
> --- /dev/null
> +++ b/include/gdbstub/registers.h
> @@ -0,0 +1,55 @@
> +/*
> + * GDB Common Register Helpers
> + *
> + * Copyright (c) 2025 Linaro Ltd
> + *
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + */
> +
> +#ifndef GDB_REGISTERS_H
> +#define GDB_REGISTERS_H
> +
> +#include "exec/memop.h"
> +
> +/**
> + * gdb_get_register_value() - get register value for gdb
> + * mo: size and endian MemOp
> + * buf: GByteArray to store in target order
> + * val: pointer to value in host order
> + *
> + * This replaces the previous legacy read functions with a single
> + * function to handle all sizes. Passing @mo allows the target mode to
> + * be taken into account and avoids using hard coded tswap() macros.
> + *
> + * There are wrapper functions for the common sizes you can use to
> + * keep type checking.
> + *
> + * Returns the number of bytes written to the array.
> + */
> +int gdb_get_register_value(MemOp op, GByteArray *buf, void *val);
> +
> +/**
> + * gdb_get_reg32_value() - type checked wrapper for gdb_get_register_value()
> + * mo: size and endian MemOp
> + * buf: GByteArray to store in target order
> + * val: pointer to uint32_t value in host order
> + */
> +static inline int gdb_get_reg32_value(MemOp op, GByteArray *buf, uint32_t *val) {
> + g_assert((op & MO_SIZE) == MO_32);
> + return gdb_get_register_value(op, buf, val);
> +}
> +
> +/**
> + * gdb_get_reg64_value() - type checked wrapper for gdb_get_register_value()
> + * mo: size and endian MemOp
> + * buf: GByteArray to store in target order
> + * val: pointer to uint32_t value in host order
> + */
> +static inline int gdb_get_reg64_value(MemOp op, GByteArray *buf, uint64_t *val) {
> + g_assert((op & MO_SIZE) == MO_64);
> + return gdb_get_register_value(op, buf, val);
> +}
> +
> +#endif /* GDB_REGISTERS_H */
> +
> +
> diff --git a/gdbstub/gdbstub.c b/gdbstub/gdbstub.c
> index b6d5e11e03..e799fdc019 100644
> --- a/gdbstub/gdbstub.c
> +++ b/gdbstub/gdbstub.c
> @@ -32,6 +32,7 @@
> #include "exec/gdbstub.h"
> #include "gdbstub/commands.h"
> #include "gdbstub/syscalls.h"
> +#include "gdbstub/registers.h"
> #ifdef CONFIG_USER_ONLY
> #include "accel/tcg/vcpu-state.h"
> #include "gdbstub/user.h"
> @@ -45,6 +46,7 @@
> #include "system/runstate.h"
> #include "exec/replay-core.h"
> #include "exec/hwaddr.h"
> +#include "exec/memop.h"
>
> #include "internals.h"
>
> @@ -551,6 +553,27 @@ static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
> return 0;
> }
>
> +/*
> + * Target helper function to read value into GByteArray, target
> + * supplies the size and target endianess via the MemOp.
> + */
> +int gdb_get_register_value(MemOp op, GByteArray *buf, void *val)
> +{
> + unsigned bytes = memop_size(op);
> +
> + if (op & MO_BSWAP) {
> + uint8_t *ptr = &((uint8_t *) val)[bytes - 1];
> + for (unsigned i = bytes; i > 0; i--) {
> + g_byte_array_append(buf, ptr--, 1);
> + };
I forgot to fix this up. Hopefully the following seems a little less
like pointer abuse:
/*
* Target helper function to read value into GByteArray, target
* supplies the size and target endianess via the MemOp.
*/
int gdb_get_register_value(MemOp op, GByteArray *buf, void *val)
{
unsigned bytes = memop_size(op);
uint8_t *data = val;
if (op & MO_BSWAP) {
uint8_t *ptr = &data[bytes - 1];
do {
g_byte_array_append(buf, ptr--, 1);
} while (ptr >= data);
} else {
g_byte_array_append(buf, val, bytes);
}
return bytes;
}
> + } else {
> + g_byte_array_append(buf, val, bytes);
> + }
> +
> + return bytes;
> +}
> +
> +
> static void gdb_register_feature(CPUState *cpu, int base_reg,
> gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
> const GDBFeature *feature)
--
Alex Bennée
Virtualisation Tech Lead @ Linaro
next prev parent reply other threads:[~2025-03-24 10:32 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-24 10:21 [PATCH v2 00/11] gdbstub: conversion to runtime endianess helpers Alex Bennée
2025-03-24 10:21 ` [PATCH v2 01/11] include/exec: fix assert in size_memop Alex Bennée
2025-03-24 16:40 ` Philippe Mathieu-Daudé
2025-03-24 17:08 ` Richard Henderson
2025-03-24 19:08 ` Pierrick Bouvier
2025-03-29 7:51 ` Akihiko Odaki
2025-03-30 8:25 ` Philippe Mathieu-Daudé
2025-03-24 10:21 ` [PATCH v2 02/11] include/gdbstub: fix include guard in commands.h Alex Bennée
2025-03-24 19:11 ` Pierrick Bouvier
2025-03-24 10:21 ` [PATCH v2 03/11] gdbstub: assert earlier in handle_read_all_regs Alex Bennée
2025-03-24 19:11 ` Pierrick Bouvier
2025-03-24 10:21 ` [PATCH v2 04/11] gdbstub: introduce target independent gdb register helper Alex Bennée
2025-03-24 10:32 ` Alex Bennée [this message]
2025-03-29 8:15 ` Akihiko Odaki
2025-03-24 17:32 ` Richard Henderson
2025-03-24 19:07 ` Pierrick Bouvier
2025-03-24 19:21 ` Pierrick Bouvier
2025-03-24 19:34 ` Pierrick Bouvier
2025-03-29 7:58 ` Akihiko Odaki
2025-03-24 10:21 ` [PATCH v2 05/11] target/arm: convert 32 bit gdbstub to new helpers Alex Bennée
2025-03-24 19:16 ` Pierrick Bouvier
2025-03-24 10:21 ` [PATCH v2 06/11] target/arm: convert 64 " Alex Bennée
2025-03-24 19:18 ` Pierrick Bouvier
2025-03-24 10:21 ` [PATCH v2 07/11] target/ppc: expand comment on FP/VMX/VSX access functions Alex Bennée
2025-03-24 17:35 ` Richard Henderson
2025-03-24 19:24 ` Pierrick Bouvier
2025-03-24 10:21 ` [PATCH v2 08/11] target/ppc: make ppc_maybe_bswap_register static Alex Bennée
2025-03-24 17:34 ` Richard Henderson
2025-03-24 19:24 ` Pierrick Bouvier
2025-03-24 10:21 ` [PATCH v2 09/11] target/ppc: convert gdbstub to new helpers Alex Bennée
2025-03-24 17:39 ` Richard Henderson
2025-03-24 19:29 ` Pierrick Bouvier
2025-03-24 20:04 ` Richard Henderson
2025-03-24 20:49 ` Pierrick Bouvier
2025-03-25 9:22 ` Philippe Mathieu-Daudé
2025-03-25 14:21 ` Pierrick Bouvier
2025-03-24 10:21 ` [PATCH v2 10/11] target/microblaze: convert gdbstub to new helper Alex Bennée
2025-03-24 16:45 ` Philippe Mathieu-Daudé
2025-03-24 19:30 ` Pierrick Bouvier
2025-03-24 10:21 ` [PATCH v2 11/11] include/gdbstub: add note to helpers.h Alex Bennée
2025-03-24 16:46 ` Philippe Mathieu-Daudé
2025-03-24 17:29 ` Alex Bennée
2025-03-24 19:33 ` Pierrick Bouvier
2025-03-24 19:33 ` Pierrick Bouvier
2025-03-24 19:10 ` [PATCH v2 00/11] gdbstub: conversion to runtime endianess helpers Pierrick Bouvier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87ikny4t2t.fsf@draig.linaro.org \
--to=alex.bennee@linaro.org \
--cc=akihiko.odaki@daynix.com \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=clg@kaod.org \
--cc=danielhb413@gmail.com \
--cc=david@gibson.dropbear.id.au \
--cc=david@redhat.com \
--cc=edgar.iglesias@gmail.com \
--cc=iii@linux.ibm.com \
--cc=npiggin@gmail.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@linaro.org \
--cc=pierrick.bouvier@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-ppc@nongnu.org \
--cc=qemu-s390x@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=thuth@redhat.com \
--cc=wainersm@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.