Make softmmu/qtest.c target independent
cover Make softmmu/qtest.c target independent
For being able to build universal binaries one day, we need certain files to be independent from the emulated target. qtest.c is one of these files. Rework the target specific code in there so we can finally move it from "specific_ss" to "softmmu_ss".
Thomas Huth (3):
softmmu/qtest: Move the target-specific pseries RTAS code out of
qtest.c
include/exec: Provide the tswap() functions for target independent
code, too
softmmu: Make qtest.c target independent
include/exec/cpu-all.h | 64 +------------------------------------ include/exec/tswap.h | 72 ++++++++++++++++++++++++++++++++++++++++++ include/sysemu/qtest.h | 4 +++ hw/ppc/spapr_rtas.c | 29 +++++++++++++++++ softmmu/qtest.c | 51 +++++++++++------------------- softmmu/meson.build | 2 +- 6 files changed, 126 insertions(+), 96 deletions(-) create mode 100644 include/exec/tswap.h
Signature · 1 line
2.31.1
PATCH 1/3 softmmu/qtest: Move the target-specific pseries RTAS code out of qtest.c
Ideally, qtest.c should be independent from target specific code, so we only have to compile it once for all targets. Thus start improving the situation by moving the pseries related code to hw/ppc/spapr_rtas.c instead and allow target code to register a callback handler for such target specific commands.
--- include/sysemu/qtest.h | 4 ++++ hw/ppc/spapr_rtas.c | 29 +++++++++++++++++++++++++++++ softmmu/qtest.c | 39 +++++++++++++-------------------------- 3 files changed, 46 insertions(+), 26 deletions(-)
include/sysemu/qtest.h +4 -0
@@ -14,6 +14,7 @@#ifndef QTEST_H #define QTEST_H +#include "chardev/char.h" extern bool qtest_allowed;
@@ -22,6 +23,9 @@ static inline bool qtest_enabled(void)return qtest_allowed; } +void qtest_send_prefix (CharBackend *chr); +void G_GNUC_PRINTF(2, 3) qtest_sendf(CharBackend *chr, const char *fmt, ...); +void qtest_set_command_cb(bool (*pc_cb)(CharBackend *chr, gchar **words));bool qtest_driver (void );void qtest_server_init (const char *qtest_chrdev,const char *qtest_log,Error **errp);
hw/ppc/spapr_rtas.c +29 -0
@@ -33,6 +33,7 @@#include "sysemu/cpus.h" #include "sysemu/hw_accel.h" #include "sysemu/runstate.h" +#include "sysemu/qtest.h" #include "kvm_ppc.h" #include "hw/ppc/spapr.h"
@@ -548,6 +549,32 @@ uint64_t qtest_rtas_call(char *cmd, uint32_t nargs, uint64_t args,return H_PARAMETER ; } +static bool spapr_qtest_callback (CharBackend *chr,gchar **words) +{ +if (strcmp (words[0 ],"rtas" ) ==0 ) { +uint64_t res, args, ret; +unsigned long nargs, nret; +int rc; + + rc =qemu_strtoul (words[2 ],NULL ,0 , &nargs); +g_assert (rc ==0 ); + rc =qemu_strtou64 (words[3 ],NULL ,0 , &args); +g_assert (rc ==0 ); + rc =qemu_strtoul (words[4 ],NULL ,0 , &nret); +g_assert (rc ==0 ); + rc =qemu_strtou64 (words[5 ],NULL ,0 , &ret); +g_assert (rc ==0 ); + res =qtest_rtas_call (words[1 ], nargs, args, nret, ret); + +qtest_send_prefix (chr); +qtest_sendf (chr,"OK %" PRIu64"\n" , res); + +return true; + } + +return false; +} +void spapr_rtas_register (int token,const char *name,spapr_rtas_fn fn) {assert ((token >=RTAS_TOKEN_BASE ) && (token <RTAS_TOKEN_MAX ));
@@ -630,6 +657,8 @@ static void core_rtas_register_types(void) rtas_ibm_nmi_register);spapr_rtas_register (RTAS_IBM_NMI_INTERLOCK ,"ibm,nmi-interlock" , rtas_ibm_nmi_interlock); + +qtest_set_command_cb (spapr_qtest_callback); }type_init (core_rtas_register_types)
softmmu/qtest.c +13 -26
@@ -29,10 +29,6 @@#include "qemu/module.h" #include "qemu/cutils.h" #include "qom/object_interfaces.h" -#include CONFIG_DEVICES -#ifdef CONFIG_PSERIES -#include "hw/ppc/spapr_rtas.h" -#endif #define MAX_IRQ 256
@@ -263,7 +259,7 @@ static int hex2nib(char ch) } } -static void qtest_send_prefix (CharBackend *chr) +void qtest_send_prefix (CharBackend *chr) {if (!qtest_log_fp || !qtest_opened) {return ;
@@ -302,8 +298,7 @@ static void qtest_send(CharBackend *chr, const char *str)qtest_server_send (qtest_server_send_opaque, str); } -static void G_GNUC_PRINTF(2, 3) qtest_sendf(CharBackend *chr, - const char *fmt, ...) +void qtest_sendf (CharBackend *chr,const char *fmt, ...) {va_list ap;gchar *buffer;
@@ -361,6 +356,15 @@ static void qtest_clock_warp(int64_t dest)qemu_clock_notify (QEMU_CLOCK_VIRTUAL ); } +static bool (*process_command_cb)(CharBackend *chr,gchar **words); + +void qtest_set_command_cb (bool (*pc_cb)(CharBackend *chr,gchar **words)) +{ +assert (!process_command_cb);/* Switch to a list if we need more than one */ + + process_command_cb = pc_cb; +} +static void qtest_process_command (CharBackend *chr,gchar **words) {const gchar *command;
@@ -717,25 +721,6 @@ static void qtest_process_command(CharBackend *chr, gchar **words)qtest_sendf (chr,"OK big\n" );#else qtest_sendf (chr,"OK little\n" ); -#endif -#ifdef CONFIG_PSERIES - }else if (strcmp (words[0 ],"rtas" ) ==0 ) { -uint64_t res, args, ret; -unsigned long nargs, nret; -int rc; - - rc =qemu_strtoul (words[2 ],NULL ,0 , &nargs); -g_assert (rc ==0 ); - rc =qemu_strtou64 (words[3 ],NULL ,0 , &args); -g_assert (rc ==0 ); - rc =qemu_strtoul (words[4 ],NULL ,0 , &nret); -g_assert (rc ==0 ); - rc =qemu_strtou64 (words[5 ],NULL ,0 , &ret); -g_assert (rc ==0 ); - res =qtest_rtas_call (words[1 ], nargs, args, nret, ret); - -qtest_send_prefix (chr); -qtest_sendf (chr,"OK %" PRIu64"\n" , res);#endif }else if (qtest_enabled () &&strcmp (words[0 ],"clock_step" ) ==0 ) {int64_t ns;
@@ -777,6 +762,8 @@ static void qtest_process_command(CharBackend *chr, gchar **words)qtest_send_prefix (chr);qtest_sendf (chr,"OK %" PRIi64"\n" , (int64_t )qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL )); + }else if (process_command_cb &&process_command_cb (chr, words)) { +/* Command got consumed by the callback handler */ }else {qtest_send_prefix (chr);qtest_sendf (chr,"FAIL Unknown command '%s'\n" , words[0 ]);
Signature · 1 line
2.31.1
Re: [PATCH 1/3] softmmu/qtest: Move the target-specific pseries RTAS code out of qtest.c
Quoted 7 lines · Thomas Huth
Ideally,
qtest.cshould be independent from target specific code, so we only have to compile it once for all targets. Thus start improving the situation by moving the pseries related code tohw/ppc/spapr_rtas.cinstead and allow target code to register a callback handler for such target specific commands.Signed-off-by Thomas Huth <[email protected]>
That #ifdef CONFIG_PSERIES was really ugly.
Thanks,
C.
Quoted 167 lines
--- include/sysemu/qtest.h | 4 ++++ hw/ppc/spapr_rtas.c | 29 +++++++++++++++++++++++++++++ softmmu/qtest.c | 39 +++++++++++++-------------------------- 3 files changed, 46 insertions(+), 26 deletions(-)include/sysemu/qtest.h +4
-0@@ -14,6 +14,7 @@#ifndef QTEST_H #define QTEST_H +#include "chardev/char.h" extern bool qtest_allowed;@@ -22,6 +23,9 @@ static inline bool qtest_enabled(void)return qtest_allowed; } +void qtest_send_prefix (CharBackend *chr); +void G_GNUC_PRINTF(2, 3) qtest_sendf(CharBackend *chr, const char *fmt, ...); +void qtest_set_command_cb(bool (*pc_cb)(CharBackend *chr, gchar **words));bool qtest_driver (void );void qtest_server_init (const char *qtest_chrdev,const char *qtest_log,Error **errp);hw/ppc/spapr_rtas.c +29
-0@@ -33,6 +33,7 @@#include "sysemu/cpus.h" #include "sysemu/hw_accel.h" #include "sysemu/runstate.h" +#include "sysemu/qtest.h" #include "kvm_ppc.h" #include "hw/ppc/spapr.h" @@ -548,6 +549,32 @@ uint64_t qtest_rtas_call(char *cmd, uint32_t nargs, uint64_t args,return H_PARAMETER ; } +static bool spapr_qtest_callback (CharBackend *chr,gchar **words) +{ +if (strcmp (words[0 ],"rtas" ) ==0 ) { +uint64_t res, args, ret; +unsigned long nargs, nret; +int rc; + + rc =qemu_strtoul (words[2 ],NULL ,0 , &nargs); +g_assert (rc ==0 ); + rc =qemu_strtou64 (words[3 ],NULL ,0 , &args); +g_assert (rc ==0 ); + rc =qemu_strtoul (words[4 ],NULL ,0 , &nret); +g_assert (rc ==0 ); + rc =qemu_strtou64 (words[5 ],NULL ,0 , &ret); +g_assert (rc ==0 ); + res =qtest_rtas_call (words[1 ], nargs, args, nret, ret); + +qtest_send_prefix (chr); +qtest_sendf (chr,"OK %" PRIu64"\n" , res); + +return true; + } + +return false; +} +void spapr_rtas_register (int token,const char *name,spapr_rtas_fn fn) {assert ((token >=RTAS_TOKEN_BASE ) && (token <RTAS_TOKEN_MAX ));@@ -630,6 +657,8 @@ static void core_rtas_register_types(void) rtas_ibm_nmi_register);spapr_rtas_register (RTAS_IBM_NMI_INTERLOCK ,"ibm,nmi-interlock" , rtas_ibm_nmi_interlock); + +qtest_set_command_cb (spapr_qtest_callback); }type_init (core_rtas_register_types)softmmu/qtest.c +13
-26@@ -29,10 +29,6 @@#include "qemu/module.h" #include "qemu/cutils.h" #include "qom/object_interfaces.h" -#include CONFIG_DEVICES -#ifdef CONFIG_PSERIES -#include "hw/ppc/spapr_rtas.h" -#endif #define MAX_IRQ 256@@ -263,7 +259,7 @@ static int hex2nib(char ch) } } -static void qtest_send_prefix (CharBackend *chr) +void qtest_send_prefix (CharBackend *chr) {if (!qtest_log_fp || !qtest_opened) {return ;@@ -302,8 +298,7 @@ static void qtest_send(CharBackend *chr, const char *str)qtest_server_send (qtest_server_send_opaque, str); } -static void G_GNUC_PRINTF(2, 3) qtest_sendf(CharBackend *chr, - const char *fmt, ...) +void qtest_sendf (CharBackend *chr,const char *fmt, ...) {va_list ap;gchar *buffer;@@ -361,6 +356,15 @@ static void qtest_clock_warp(int64_t dest)qemu_clock_notify (QEMU_CLOCK_VIRTUAL ); } +static bool (*process_command_cb)(CharBackend *chr,gchar **words); + +void qtest_set_command_cb (bool (*pc_cb)(CharBackend *chr,gchar **words)) +{ +assert (!process_command_cb);/* Switch to a list if we need more than one */ + + process_command_cb = pc_cb; +} +static void qtest_process_command (CharBackend *chr,gchar **words) {const gchar *command;@@ -717,25 +721,6 @@ static void qtest_process_command(CharBackend *chr, gchar **words)qtest_sendf (chr,"OK big\n" );#else qtest_sendf (chr,"OK little\n" ); -#endif -#ifdef CONFIG_PSERIES - }else if (strcmp (words[0 ],"rtas" ) ==0 ) { -uint64_t res, args, ret; -unsigned long nargs, nret; -int rc; - - rc =qemu_strtoul (words[2 ],NULL ,0 , &nargs); -g_assert (rc ==0 ); - rc =qemu_strtou64 (words[3 ],NULL ,0 , &args); -g_assert (rc ==0 ); - rc =qemu_strtoul (words[4 ],NULL ,0 , &nret); -g_assert (rc ==0 ); - rc =qemu_strtou64 (words[5 ],NULL ,0 , &ret); -g_assert (rc ==0 ); - res =qtest_rtas_call (words[1 ], nargs, args, nret, ret); - -qtest_send_prefix (chr); -qtest_sendf (chr,"OK %" PRIu64"\n" , res);#endif }else if (qtest_enabled () &&strcmp (words[0 ],"clock_step" ) ==0 ) {int64_t ns;@@ -777,6 +762,8 @@ static void qtest_process_command(CharBackend *chr, gchar **words)qtest_send_prefix (chr);qtest_sendf (chr,"OK %" PRIi64"\n" , (int64_t )qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL )); + }else if (process_command_cb &&process_command_cb (chr, words)) { +/* Command got consumed by the callback handler */ }else {qtest_send_prefix (chr);qtest_sendf (chr,"FAIL Unknown command '%s'\n" , words[0 ]);
PATCH 2/3 include/exec: Provide the tswap() functions for target independent code, too
In reply to Thomas HuthIn some cases of target independent code, it would be useful to have access to the functions that swap endianess in case it differs between guest and host. Thus re-implement the tswapXX() functions in a new header that can be included separately. The check whether the swapping is needed continues to be done at compile-time for target specific code, while it is done at run-time in target-independent code.
--- include/exec/cpu-all.h | 64 +------------------------------------ include/exec/tswap.h | 72 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 63 deletions(-) create mode 100644 include/exec/tswap.h
include/exec/cpu-all.h +1 -63
@@ -21,6 +21,7 @@#include "exec/cpu-common.h" #include "exec/memory.h" +#include "exec/tswap.h" #include "qemu/thread.h" #include "hw/core/cpu.h" #include "qemu/rcu.h"
@@ -44,69 +45,6 @@#define BSWAP_NEEDED #endif -#ifdef BSWAP_NEEDED - -static inline uint16_t tswap16 (uint16_t s) -{ -return bswap16 (s); -} - -static inline uint32_t tswap32 (uint32_t s) -{ -return bswap32 (s); -} - -static inline uint64_t tswap64 (uint64_t s) -{ -return bswap64 (s); -} - -static inline void tswap16s (uint16_t *s) -{ - *s =bswap16 (*s); -} - -static inline void tswap32s (uint32_t *s) -{ - *s =bswap32 (*s); -} - -static inline void tswap64s (uint64_t *s) -{ - *s =bswap64 (*s); -} - -#else - -static inline uint16_t tswap16 (uint16_t s) -{ -return s; -} - -static inline uint32_t tswap32 (uint32_t s) -{ -return s; -} - -static inline uint64_t tswap64 (uint64_t s) -{ -return s; -} - -static inline void tswap16s (uint16_t *s) -{ -} - -static inline void tswap32s (uint32_t *s) -{ -} - -static inline void tswap64s (uint64_t *s) -{ -} - -#endif -#if TARGET_LONG_SIZE ==4 #define tswapl (s) tswap32(s)#define tswapls (s) tswap32s((uint32_t *)(s))
include/exec/tswap.h new file +72 -0
72 lines not on this page · raw message
Signature · 1 line
2.31.1
Re: [PATCH 2/3] include/exec: Provide the tswap() functions for target independent code, too
Quoted 6 lines · Thomas Huth
In some cases of target independent code, it would be useful to have access to the functions that swap endianess in case it differs between guest and host. Thus re-implement the
tswapXX()functions in a new header that can be included separately. The check whether the swapping is needed continues to be done at compile-time for target specific code, while it is done at run-time in target-independent code.
Quoted 21 lines
Signed-off-by Thomas Huth <[email protected]>--- include/exec/cpu-all.h | 64 +------------------------------------ include/exec/tswap.h | 72 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 63 deletions(-)create mode 100644
include/exec/tswap.hinclude/exec/cpu-all.h +1
-0@@ -21,6 +21,7 @@#include "exec/cpu-common.h" #include "exec/memory.h" +#include "exec/tswap.h" #include "qemu/thread.h" #include "hw/core/cpu.h" #include "qemu/rcu.h"
include/exec/cpu-all.h line 45
Quoted 1 line
include/exec/cpu-all.h +0
-0@@ -44,69 +45,6 @@#define BSWAP_NEEDED
I wonder if we could get rid of the BSWAP_NEEDED define also. (used load_aout() and ELF loaders)
Thanks,
C.
Quoted 146 lines
include/exec/cpu-all.h +0
-63#endif -#ifdef BSWAP_NEEDED - -static inline uint16_t tswap16 (uint16_t s) -{ -return bswap16 (s); -} - -static inline uint32_t tswap32 (uint32_t s) -{ -return bswap32 (s); -} - -static inline uint64_t tswap64 (uint64_t s) -{ -return bswap64 (s); -} - -static inline void tswap16s (uint16_t *s) -{ - *s =bswap16 (*s); -} - -static inline void tswap32s (uint32_t *s) -{ - *s =bswap32 (*s); -} - -static inline void tswap64s (uint64_t *s) -{ - *s =bswap64 (*s); -} - -#else - -static inline uint16_t tswap16 (uint16_t s) -{ -return s; -} - -static inline uint32_t tswap32 (uint32_t s) -{ -return s; -} - -static inline uint64_t tswap64 (uint64_t s) -{ -return s; -} - -static inline void tswap16s (uint16_t *s) -{ -} - -static inline void tswap32s (uint32_t *s) -{ -} - -static inline void tswap64s (uint64_t *s) -{ -} - -#endif -#if TARGET_LONG_SIZE ==4 #define tswapl (s) tswap32(s)#define tswapls (s) tswap32s((uint32_t *)(s))include/exec/tswap.h new file +72
-0@@ -0,0 +1,72 @@ +/* +* Macros for swapping a value if the endianness is different +* between the target and the host. +* +* SPDX-License-Identifier: LGPL-2.1-or-later +*/ + +#ifndef TSWAP_H +#define TSWAP_H + +#include "hw/core/cpu.h" +#include "qemu/bswap.h" + +/* +* If we're in target-specific code, we can hard-code the swapping +* condition, otherwise we have to do (slower) run-time checks. +*/ +#ifdef NEED_CPU_H +#define target_needs_bswap () (HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN) +#else +#define target_needs_bswap () (target_words_bigendian() != HOST_BIG_ENDIAN) +#endif + +static inline uint16_t tswap16 (uint16_t s) +{ +if (target_needs_bswap ()) { +return bswap16 (s); + }else { +return s; + } +} + +static inline uint32_t tswap32 (uint32_t s) +{ +if (target_needs_bswap ()) { +return bswap32 (s); + }else { +return s; + } +} + +static inline uint64_t tswap64 (uint64_t s) +{ +if (target_needs_bswap ()) { +return bswap64 (s); + }else { +return s; + } +} + +static inline void tswap16s (uint16_t *s) +{ +if (target_needs_bswap ()) { + *s =bswap16 (*s); + } +} + +static inline void tswap32s (uint32_t *s) +{ +if (target_needs_bswap ()) { + *s =bswap32 (*s); + } +} + +static inline void tswap64s (uint64_t *s) +{ +if (target_needs_bswap ()) { + *s =bswap64 (*s); + } +} + +#endif /* TSWAP_H */
PATCH 3/3 softmmu: Make qtest.c target independent
In reply to Thomas HuthThe code in this file is not performance critical, so we can use the target independent endianess functions to only compile this file once for all targets.
--- softmmu/qtest.c | 12 ++++++------ softmmu/meson.build | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-)
softmmu/qtest.c +6 -6
@@ -13,12 +13,12 @@#include "qemu/osdep.h" #include "qapi/error.h" -#include "cpu.h" #include "sysemu/qtest.h" #include "sysemu/runstate.h" #include "chardev/char-fe.h" #include "exec/ioport.h" #include "exec/memory.h" +#include "exec/tswap.h" #include "hw/qdev-core.h" #include "hw/irq.h" #include "qemu/accel.h"
@@ -717,11 +717,11 @@ static void qtest_process_command(CharBackend *chr, gchar **words)qtest_send (chr,"OK\n" ); }else if (strcmp (words[0 ],"endianness" ) ==0 ) {qtest_send_prefix (chr); -#if TARGET_BIG_ENDIAN -qtest_sendf (chr,"OK big\n" ); -#else -qtest_sendf (chr,"OK little\n" ); -#endif +if (target_words_bigendian ()) { +qtest_sendf (chr,"OK big\n" ); + }else { +qtest_sendf (chr,"OK little\n" ); + } }else if (qtest_enabled () &&strcmp (words[0 ],"clock_step" ) ==0 ) {int64_t ns;
softmmu/meson.build +1 -1
@@ -3,7 +3,6 @@ specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: [files( 'ioport.c', 'memory.c', 'physmem.c', - 'qtest.c', 'dirtylimit.c', 'watchpoint.c', )])
@@ -23,6 +22,7 @@ softmmu_ss.add(files( 'globals.c', 'memory_mapping.c', 'qdev-monitor.c', + 'qtest.c', 'rtc.c', 'runstate-action.c', 'runstate-hmp-cmds.c',
Signature · 1 line
2.31.1
Re: [PATCH 3/3] softmmu: Make qtest.c target independent
Quoted 5 lines · Thomas Huth
The code in this file is not performance critical, so we can use the target independent endianess functions to only compile this file once for all targets.
Signed-off-by Thomas Huth <[email protected]>
Thanks,
C.
Quoted 60 lines
--- softmmu/qtest.c | 12 ++++++------ softmmu/meson.build | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-)softmmu/qtest.c +6
-6@@ -13,12 +13,12 @@#include "qemu/osdep.h" #include "qapi/error.h" -#include "cpu.h" #include "sysemu/qtest.h" #include "sysemu/runstate.h" #include "chardev/char-fe.h" #include "exec/ioport.h" #include "exec/memory.h" +#include "exec/tswap.h" #include "hw/qdev-core.h" #include "hw/irq.h" #include "qemu/accel.h" @@ -717,11 +717,11 @@ static void qtest_process_command(CharBackend *chr, gchar **words)qtest_send (chr,"OK\n" ); }else if (strcmp (words[0 ],"endianness" ) ==0 ) {qtest_send_prefix (chr); -#if TARGET_BIG_ENDIAN -qtest_sendf (chr,"OK big\n" ); -#else -qtest_sendf (chr,"OK little\n" ); -#endif +if (target_words_bigendian ()) { +qtest_sendf (chr,"OK big\n" ); + }else { +qtest_sendf (chr,"OK little\n" ); + } }else if (qtest_enabled () &&strcmp (words[0 ],"clock_step" ) ==0 ) {int64_t ns;softmmu/meson.build +1
-1@@ -3,7 +3,6 @@ specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: [files( 'ioport.c', 'memory.c', 'physmem.c', - 'qtest.c', 'dirtylimit.c', 'watchpoint.c', )])@@ -23,6 +22,7 @@ softmmu_ss.add(files( 'globals.c', 'memory_mapping.c', 'qdev-monitor.c', + 'qtest.c', 'rtc.c', 'runstate-action.c', 'runstate-hmp-cmds.c',
Quoted 11 lines · Thomas Huth
For being able to build universal binaries one day, we need certain files to be independent from the emulated target.
qtest.cis one of these files. Rework the target specific code in there so we can finally move it from "specific_ss" to "softmmu_ss".Thomas Huth (3):
softmmu/qtest: Move the target-specific pseries RTAS code out of
qtest.c
include/exec: Provide thetswap()functions for target independent
code, too
softmmu: Makeqtest.ctarget independent
r~