Enable PC diversion via the plugin API
Subsystems (2)
v7 cover Enable PC diversion via the plugin API
Hi,
This patch series builds on top of the discussion from the thread at
https://lore.kernel.org/qemu-devel/[email protected]/
and adds a plugin API function to set the program counter of the guest, as just writing to it via qemu_plugin_write_register() has no direct effect.
This version v7 of the patch series addresses the requested changes from the previous v6 submission (details below).
Note: checkpatch.pl still reports a warning about line length violations in patch nr. 7/8 but I did not fix this, as the line was already > 80 characters long previously, the change added only a single character, and I think the readability of the code is better as it is now. Please let me know if you disagree and would like me to fix this!
@Pierrick: you already reviewed patch 3/7 (now 4/8) previously, but I had to change the regex in scripts/qemu-plugin-symbols.py to account for attributes, so please re-review!
Best regards,
Florian
Changes:
v7:
- Add Pierrick's patch to clamp 32-bit registers to prevent accidental
sign extension
- Fix documentation build by using __attribute__((__noreturn__)) instead
of G_NORETURN (required adjusting scripts/qemu-plugin-symbols.py).
- Rework test-plugin-set-pc test to get rid of volatile guard and make control flow more linear
- Link to v6: https://lore.kernel.org/qemu-devel/[email protected]
v6:
- update commit message for patch 4/7
v5:
- make QEMU abort via asserts instead of just returning an error from
the plugin API if preconditions are violated
- extend tests for qemu_plugin_set_pc() to different contexts
- fix issues highlighted by checkpatch.pl
v4:
- switch strcmp out in favor of g_strcmp0
- split the patch introducing the qemu_plugin_set_pc() API into three patches, two for preparing the plugin infrastructure and the syscall handling code and a third introducing the actual plugin API
v3:
- make PC registers read-only across architectures
- add tests for read-only registers
- adjust test structure for qemu_plugin_set_pc() by moving architecture-specific tests into corresponding directories
v2:
- add setjmp() in syscall handling path to allow PC redirection from
syscall callbacks (via longjmp(), the cpu_loop()'s setjmp() for
exiting a TB would not be live anymore in syscall handlers)
- add flags to ensure the qemu_plugin_set_pc() API is only called from contexts where the CPU is live
- add test for qemu_plugin_set_pc() API
v1:
- initial version
---
Florian Hofhammer (7):
plugins: add flag to specify whether PC is rw
linux-user: make syscall emulation interruptible
plugins: add PC diversion API function tests/tcg: add tests for qemu_plugin_set_pc API
plugins: add read-only property for registers
plugins: prohibit writing to read-only registers tests/tcg/plugins: test register accesses
Pierrick Bouvier (1):
plugins/core: clamp syscall arguments if target is 32-bit
MAINTAINERS | 1 +
include/plugins/qemu-plugin.h | 19 +++
linux-user/aarch64/cpu_loop.c | 2 +-
linux-user/alpha/cpu_loop.c | 2 +-
linux-user/arm/cpu_loop.c | 2 +-
linux-user/hexagon/cpu_loop.c | 2 +-
linux-user/hppa/cpu_loop.c | 1 +
linux-user/i386/cpu_loop.c | 8 +-
linux-user/include/special-errno.h | 8 ++
linux-user/loongarch64/cpu_loop.c | 5 +-
linux-user/m68k/cpu_loop.c | 2 +-
linux-user/microblaze/cpu_loop.c | 2 +-
linux-user/mips/cpu_loop.c | 9 +-
linux-user/or1k/cpu_loop.c | 2 +-
linux-user/ppc/cpu_loop.c | 10 +-
linux-user/riscv/cpu_loop.c | 2 +-
linux-user/s390x/cpu_loop.c | 2 +-
linux-user/sh4/cpu_loop.c | 2 +-
linux-user/sparc/cpu_loop.c | 4 +-
linux-user/syscall.c | 16 +++
linux-user/xtensa/cpu_loop.c | 1 +
plugins/api.c | 42 ++++++-
plugins/core.c | 50 ++++++--
scripts/qemu-plugin-symbols.py | 9 +-
tests/tcg/arm/Makefile.target | 6 +
tests/tcg/hexagon/Makefile.target | 8 ++
tests/tcg/multiarch/Makefile.target | 17 ++-
.../multiarch/{ => plugin}/check-plugin-output.sh | 0
.../{ => plugin}/test-plugin-mem-access.c | 0
tests/tcg/multiarch/plugin/test-plugin-set-pc.c | 134 +++++++++++++++++++++
tests/tcg/plugins/meson.build | 2 +
tests/tcg/plugins/registers.c | 79 ++++++++++++
tests/tcg/plugins/setpc.c | 109 +++++++++++++++++
33 files changed, 514 insertions(+), 44 deletions(-)---
base-commit: 3fb456e9a0e9eef6a71d9b49bfff596a0f0046e9
change-id: 20260303-setpc-v5-c1df30bad07f
PATCH v7 1/8 plugins/core: clamp syscall arguments if target is 32-bit
From: Pierrick Bouvier <[email protected]>
Syscall arguments are abi_long in user code, and plugin syscall interface works with uint64_t only.
According to C integer promotion rules, the value is sign extended before becoming unsigned, thus setting high bits when only 32-bit lower ones should have a significant value.
As a result, we need to clamp values we receive from user-code accordingly.
--- plugins/core.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
plugins/core.c +21 -0
@@ -513,6 +513,23 @@ void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb) } } +static void clamp_syscall_arguments (uint64_t *a1,uint64_t *a2,uint64_t *a3, +uint64_t *a4,uint64_t *a5,uint64_t *a6, +uint64_t *a7,uint64_t *a8) +{ +if (target_long_bits () ==32 ) { +const uint64_t mask =UINT32_MAX ; + *a1 &= mask; + *a2 &= mask; + *a3 &= mask; + *a4 &= mask; + *a5 &= mask; + *a6 &= mask; + *a7 &= mask; + *a8 &= mask; + } +} +/* * Disable CFI checks. * The callback function has been loaded from an external library so we do not
@@ -531,6 +548,8 @@ qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,return ; } +clamp_syscall_arguments (&a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8); + QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {qemu_plugin_vcpu_syscall_cb_t func = cb->f.vcpu_syscall;
@@ -584,6 +603,8 @@ qemu_plugin_vcpu_syscall_filter(CPUState *cpu, int64_t num, uint64_t a1,return false; } +clamp_syscall_arguments (&a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8); +qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_RW_REGS ); QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
Signature · 1 line
2.53.0
Re: [PATCH v7 1/8] plugins/core: clamp syscall arguments if target is 32-bit
Quoted 9 lines · Florian Hofhammer
From: Pierrick Bouvier <[email protected]>
Syscall arguments are
abi_longin user code, and plugin syscall interface works withuint64_tonly.According to C integer promotion rules, the value is sign extended before becoming unsigned, thus setting high bits when only 32-bit lower ones should have a significant value.
Quoted 8 lines
As a result, we need to clamp values we receive from user-code accordingly.
Signed-off-by Pierrick Bouvier <[email protected]>--- plugins/core.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
@Alex, Philippe: may I ask you a review on this one?
Regards,
Pierrick
Re: [PATCH v7 1/8] plugins/core: clamp syscall arguments if target is 32-bit
In reply to Florian HofhammerQuoted 5 lines · Florian Hofhammer
From: Pierrick Bouvier <[email protected]>
Syscall arguments are
abi_longin user code, and plugin syscall interface works withuint64_tonly.
Quoted 8 lines
According to C integer promotion rules, the value is sign extended before becoming unsigned, thus setting high bits when only 32-bit lower ones should have a significant value.
As a result, we need to clamp values we receive from user-code accordingly.
Signed-off-by Pierrick Bouvier <[email protected]>
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Re: [PATCH v7 1/8] plugins/core: clamp syscall arguments if target is 32-bit
In reply to Florian Hofhammer+Laurent
Quoted 22 lines · Florian Hofhammer
From: Pierrick Bouvier <[email protected]>
Syscall arguments are
abi_longin user code, and plugin syscall interface works withuint64_tonly.According to C integer promotion rules, the value is sign extended before becoming unsigned, thus setting high bits when only 32-bit lower ones should have a significant value.
As a result, we need to clamp values we receive from user-code accordingly.
Signed-off-by Pierrick Bouvier <[email protected]>--- plugins/core.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)plugins/core.c +0
-0
plugins/core.c void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb) · line 520
Quoted 8 lines
plugins/core.c +5
-0@@ -513,6 +513,23 @@ void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb) } } +static void clamp_syscall_arguments (uint64_t *a1,uint64_t *a2,uint64_t *a3, +uint64_t *a4,uint64_t *a5,uint64_t *a6, +uint64_t *a7,uint64_t *a8) +{ +if (target_long_bits () ==32 ) {
IIUC this is related to the target ABI, so maybe we want to unconditionally use:
const uint64_t mask = MAKE_64BIT_MASK(0, TARGET_ABI_BITS);
Maybe guarded with #if TARGET_ABI_BITS < 64?
Quoted 11 lines
plugins/core.c +11
-0+const uint64_t mask =UINT32_MAX ; + *a1 &= mask; + *a2 &= mask; + *a3 &= mask; + *a4 &= mask; + *a5 &= mask; + *a6 &= mask; + *a7 &= mask; + *a8 &= mask; + } +}
Re: [PATCH v7 1/8] plugins/core: clamp syscall arguments if target is 32-bit
Quoted 37 lines · Philippe Mathieu-Daudé
+Laurent
Quoted 22 lines · Florian Hofhammer
From: Pierrick Bouvier <[email protected]>
Syscall arguments are
abi_longin user code, and plugin syscall interface works withuint64_tonly.According to C integer promotion rules, the value is sign extended before becoming unsigned, thus setting high bits when only 32-bit lower ones should have a significant value.
As a result, we need to clamp values we receive from user-code accordingly.
Signed-off-by Pierrick Bouvier <[email protected]>--- plugins/core.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)plugins/core.c +0
-0plugins/core.c void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb) · line 520
Quoted 8 lines
plugins/core.c +5
-0@@ -513,6 +513,23 @@ void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb) } } +static void clamp_syscall_arguments (uint64_t *a1,uint64_t *a2,uint64_t *a3, +uint64_t *a4,uint64_t *a5,uint64_t *a6, +uint64_t *a7,uint64_t *a8) +{ +if (target_long_bits () ==32 ) {IIUC this is related to the target ABI, so maybe we want to unconditionally use:
Quoted 4 lines
const uint64_t mask = MAKE_64BIT_MASK(0, TARGET_ABI_BITS);Maybe guarded with #if
TARGET_ABI_BITS< 64?
More broadly, I really wonder why we use signed type for syscall arguments in linux-user, while I think they should always be unsigned. But since soft freeze is coming very soon, it's not the time for a refactoring solution, thus this pragmatic, but correct, patch.
Quoted 11 lines
Quoted 11 lines
plugins/core.c +11
-0+const uint64_t mask =UINT32_MAX ; + *a1 &= mask; + *a2 &= mask; + *a3 &= mask; + *a4 &= mask; + *a5 &= mask; + *a6 &= mask; + *a7 &= mask; + *a8 &= mask; + } +}
Re: [PATCH v7 1/8] plugins/core: clamp syscall arguments if target is 32-bit
Quoted 37 lines · Pierrick Bouvier
Quoted 36 lines · Philippe Mathieu-Daudé
+Laurent
Quoted 22 lines · Florian Hofhammer
From: Pierrick Bouvier <[email protected]>
Syscall arguments are
abi_longin user code, and plugin syscall interface works withuint64_tonly.According to C integer promotion rules, the value is sign extended before becoming unsigned, thus setting high bits when only 32-bit lower ones should have a significant value.
As a result, we need to clamp values we receive from user-code accordingly.
Signed-off-by Pierrick Bouvier <[email protected]>--- plugins/core.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)plugins/core.c +0
-0plugins/core.c void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb) · line 519
Quoted 7 lines
plugins/core.c +4
-0@@ -513,6 +513,23 @@ void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb) } } +static void clamp_syscall_arguments(uint64_t *a1, uint64_t *a2, uint64_t *a3, +uint64_t *a4,uint64_t *a5,uint64_t *a6, +uint64_t *a7,uint64_t *a8) +{ +if (target_long_bits () ==32 ) {IIUC this is related to the target ABI, so maybe we want to unconditionally use:
Quoted 7 lines
Quoted 4 lines
const uint64_t mask = MAKE_64BIT_MASK(0, TARGET_ABI_BITS);Maybe guarded with #if
TARGET_ABI_BITS< 64?More broadly, I really wonder why we use signed type for syscall arguments in linux-user, while I think they should always be unsigned. But since soft freeze is coming very soon, it's not the time for a refactoring solution, thus this pragmatic, but correct, patch.
If you think it makes sense to refactor this for the next development cycle/window, I'd happily give it a short and prepare an RFC patch for it.
Quoted 13 lines
Quoted 12 lines
plugins/core.c void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb) · line 531
Quoted 12 lines
plugins/core.c +11
-0+const uint64_t mask =UINT32_MAX ; + *a1 &= mask; + *a2 &= mask; + *a3 &= mask; + *a4 &= mask; + *a5 &= mask; + *a6 &= mask; + *a7 &= mask; + *a8 &= mask; + } +}
Best regards,
Florian
Re: [PATCH v7 1/8] plugins/core: clamp syscall arguments if target is 32-bit
Quoted 43 lines · Florian Hofhammer
Quoted 42 lines · Pierrick Bouvier
Quoted 40 lines · Philippe Mathieu-Daudé
+Laurent
Quoted 22 lines · Florian Hofhammer
From: Pierrick Bouvier <[email protected]>
Syscall arguments are
abi_longin user code, and plugin syscall interface works withuint64_tonly.According to C integer promotion rules, the value is sign extended before becoming unsigned, thus setting high bits when only 32-bit lower ones should have a significant value.
As a result, we need to clamp values we receive from user-code accordingly.
Signed-off-by Pierrick Bouvier <[email protected]>--- plugins/core.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)plugins/core.c +0
-0plugins/core.c void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb) · line 519
Quoted 7 lines
plugins/core.c +4
-0@@ -513,6 +513,23 @@ void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb) } } +static void clamp_syscall_arguments(uint64_t *a1, uint64_t *a2, uint64_t *a3, +uint64_t *a4,uint64_t *a5,uint64_t *a6, +uint64_t *a7,uint64_t *a8) +{ +if (target_long_bits () ==32 ) {IIUC this is related to the target ABI, so maybe we want to unconditionally use:
const uint64_t mask = MAKE_64BIT_MASK(0, TARGET_ABI_BITS);Maybe guarded with #if
TARGET_ABI_BITS< 64?
Quoted 7 lines
Quoted 2 lines
More broadly, I really wonder why we use signed type for syscall arguments in linux-user, while I think they should always be unsigned. But since soft freeze is coming very soon, it's not the time for a refactoring solution, thus this pragmatic, but correct, patch.
If you think it makes sense to refactor this for the next development cycle/window, I'd happily give it a short and prepare an RFC patch for it.
Sure, I would like to get feedback from Laurent and Richard first to see if we have a specific reason to have signed syscall arguments.
Regards,
Pierrick
Re: [PATCH v7 1/8] plugins/core: clamp syscall arguments if target is 32-bit
In reply to Philippe Mathieu-DaudéQuoted 37 lines · Philippe Mathieu-Daudé
+Laurent
Quoted 22 lines · Florian Hofhammer
From: Pierrick Bouvier <[email protected]>
Syscall arguments are
abi_longin user code, and plugin syscall interface works withuint64_tonly.According to C integer promotion rules, the value is sign extended before becoming unsigned, thus setting high bits when only 32-bit lower ones should have a significant value.
As a result, we need to clamp values we receive from user-code accordingly.
Signed-off-by Pierrick Bouvier <[email protected]>--- plugins/core.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)plugins/core.c +0
-0plugins/core.c void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb) · line 520
Quoted 8 lines
plugins/core.c +5
-0@@ -513,6 +513,23 @@ void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb) } } +static void clamp_syscall_arguments (uint64_t *a1,uint64_t *a2,uint64_t *a3, +uint64_t *a4,uint64_t *a5,uint64_t *a6, +uint64_t *a7,uint64_t *a8) +{ +if (target_long_bits () ==32 ) {IIUC this is related to the target ABI, so maybe we want to unconditionally use:
Quoted 4 lines
const uint64_t mask = MAKE_64BIT_MASK(0, TARGET_ABI_BITS);Maybe guarded with #if
TARGET_ABI_BITS< 64?
TARGET_ABI_BITS is a target specific define, but we are in common code here, so it's not usable.
As well, since this path is only exercised by linux-user (and not system), target_long_bits() should not be different from its runtime value.
Quoted 11 lines
Quoted 11 lines
plugins/core.c +11
-0+const uint64_t mask =UINT32_MAX ; + *a1 &= mask; + *a2 &= mask; + *a3 &= mask; + *a4 &= mask; + *a5 &= mask; + *a6 &= mask; + *a7 &= mask; + *a8 &= mask; + } +}
PATCH v7 2/8 plugins: add flag to specify whether PC is rw
In reply to Florian HofhammerIn addition to the flags specifying whether general-purpose registers are read-write (rw) during a plugin callback, we add an additional flag explicitly stating whether the PC is writable. This is in preparation of a patch that allows to explicitly set the PC to divert control flow from within a plugin callback, which is currently not possible.
--- include/plugins/qemu-plugin.h | 3 +++ plugins/api.c | 4 +++- plugins/core.c | 29 ++++++++++++++++------------- 3 files changed, 22 insertions(+), 14 deletions(-)
include/plugins/qemu-plugin.h +3 -0
@@ -325,11 +325,14 @@ typedef struct {* @QEMU_PLUGIN_CB_NO_REGS: callback does not access the CPU's regs * @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs * @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs +* @QEMU_PLUGIN_CB_RW_REGS_PC: callback reads and writes the CPU's +* regs and updates the PC */ enum qemu_plugin_cb_flags {QEMU_PLUGIN_CB_NO_REGS ,QEMU_PLUGIN_CB_R_REGS ,QEMU_PLUGIN_CB_RW_REGS , +QEMU_PLUGIN_CB_RW_REGS_PC , };enum qemu_plugin_mem_rw {
plugins/api.c +3 -1
@@ -458,7 +458,9 @@ bool qemu_plugin_write_register(struct qemu_plugin_register *reg, {g_assert (current_cpu); -if (buf->len ==0 ||qemu_plugin_get_cb_flags () !=QEMU_PLUGIN_CB_RW_REGS ) { +if (buf->len ==0 || + (qemu_plugin_get_cb_flags () !=QEMU_PLUGIN_CB_RW_REGS && +qemu_plugin_get_cb_flags () !=QEMU_PLUGIN_CB_RW_REGS_PC )) {return false; }
plugins/core.c +16 -13
@@ -119,7 +119,7 @@ static void plugin_vcpu_cb__discon(CPUState *cpu,struct qemu_plugin_cb *cb, *next;uint64_t to = cpu->cc->get_pc (cpu); -qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_RW_REGS ); +qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_RW_REGS_PC );if (cpu->cpu_index < plugin.num_vcpus) {/* iterate safely; plugins might uninstall themselves at any time */ QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
@@ -395,15 +395,16 @@ void plugin_register_dyn_cb__udata(GArray **arr,enum qemu_plugin_cb_flags flags, void *udata) { - static TCGHelperInfo info[3] = { + static TCGHelperInfo info[4] = { [QEMU_PLUGIN_CB_NO_REGS ].flags =TCG_CALL_NO_RWG , [QEMU_PLUGIN_CB_R_REGS ].flags =TCG_CALL_NO_WG , [QEMU_PLUGIN_CB_RW_REGS ].flags =0 , + [QEMU_PLUGIN_CB_RW_REGS_PC ].flags =0 ,/* * Match qemu_plugin_vcpu_udata_cb_t: * void (*)(uint32_t, void *) */ - [0 ...2 ].typemask = (dh_typemask (void,0 ) | + [0 ...3 ].typemask = (dh_typemask (void,0 ) |dh_typemask (i32,1 ) |dh_typemask (ptr,2 )) };
@@ -425,15 +426,16 @@ void plugin_register_dyn_cond_cb__udata(GArray **arr,uint64_t imm, void *udata) { - static TCGHelperInfo info[3] = { + static TCGHelperInfo info[4] = { [QEMU_PLUGIN_CB_NO_REGS ].flags =TCG_CALL_NO_RWG , [QEMU_PLUGIN_CB_R_REGS ].flags =TCG_CALL_NO_WG , [QEMU_PLUGIN_CB_RW_REGS ].flags =0 , + [QEMU_PLUGIN_CB_RW_REGS_PC ].flags =0 ,/* * Match qemu_plugin_vcpu_udata_cb_t: * void (*)(uint32_t, void *) */ - [0 ...2 ].typemask = (dh_typemask (void,0 ) | + [0 ...3 ].typemask = (dh_typemask (void,0 ) |dh_typemask (i32,1 ) |dh_typemask (ptr,2 )) };
@@ -464,15 +466,16 @@ void plugin_register_vcpu_mem_cb(GArray **arr, !__builtin_types_compatible_p(qemu_plugin_meminfo_t, uint32_t) && !__builtin_types_compatible_p(qemu_plugin_meminfo_t, int32_t)); -static TCGHelperInfo info[3 ] = { +static TCGHelperInfo info[4 ] = { [QEMU_PLUGIN_CB_NO_REGS ].flags =TCG_CALL_NO_RWG , [QEMU_PLUGIN_CB_R_REGS ].flags =TCG_CALL_NO_WG , [QEMU_PLUGIN_CB_RW_REGS ].flags =0 , + [QEMU_PLUGIN_CB_RW_REGS_PC ].flags =0 ,/* * Match qemu_plugin_vcpu_mem_cb_t: * void (*)(uint32_t, qemu_plugin_meminfo_t, uint64_t, void *) */ - [0 ... 2].typemask = + [0 ... 3].typemask = (dh_typemask(void, 0) | dh_typemask(i32, 1) | (__builtin_types_compatible_p(qemu_plugin_meminfo_t, uint32_t)
@@ -553,7 +556,7 @@ qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2, QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {qemu_plugin_vcpu_syscall_cb_t func = cb->f.vcpu_syscall; -qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_RW_REGS ); +qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_RW_REGS_PC );func (cb->ctx->id, cpu->cpu_index, num, a1, a2, a3, a4, a5, a6, a7, a8);qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_NO_REGS ); }
@@ -577,7 +580,7 @@ void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret) QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {qemu_plugin_vcpu_syscall_ret_cb_t func = cb->f.vcpu_syscall_ret; -qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_RW_REGS ); +qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_RW_REGS_PC );func (cb->ctx->id, cpu->cpu_index, num, ret);qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_NO_REGS ); }
@@ -605,7 +608,7 @@ qemu_plugin_vcpu_syscall_filter(CPUState *cpu, int64_t num, uint64_t a1,clamp_syscall_arguments (&a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8); -qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_RW_REGS ); +qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_RW_REGS_PC ); QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {qemu_plugin_vcpu_syscall_filter_cb_t func = cb->f.vcpu_syscall_filter;
@@ -626,7 +629,7 @@ void qemu_plugin_vcpu_idle_cb(CPUState *cpu) {/* idle and resume cb may be called before init, ignore in this case */ if (cpu->cpu_index < plugin.num_vcpus) { -qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_RW_REGS ); +qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_RW_REGS_PC );plugin_vcpu_cb__simple (cpu,QEMU_PLUGIN_EV_VCPU_IDLE );qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_NO_REGS ); }
@@ -635,7 +638,7 @@ void qemu_plugin_vcpu_idle_cb(CPUState *cpu)void qemu_plugin_vcpu_resume_cb (CPUState *cpu) {if (cpu->cpu_index < plugin.num_vcpus) { -qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_RW_REGS ); +qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_RW_REGS_PC );plugin_vcpu_cb__simple (cpu,QEMU_PLUGIN_EV_VCPU_RESUME );qemu_plugin_set_cb_flags (cpu,QEMU_PLUGIN_CB_NO_REGS ); }
@@ -906,6 +909,6 @@ enum qemu_plugin_cb_flags tcg_call_to_qemu_plugin_cb_flags(int flags) }else if (flags &TCG_CALL_NO_WG ) {return QEMU_PLUGIN_CB_R_REGS ; }else { -return QEMU_PLUGIN_CB_RW_REGS ; +return QEMU_PLUGIN_CB_RW_REGS_PC ; } }
Signature · 1 line
2.53.0
PATCH v7 3/8 linux-user: make syscall emulation interruptible
In reply to Florian HofhammerThe syscall emulation code previously wasn't interruptible via cpu_loop_exit(), as this construct relies on a longjmp target that is not live anymore in the syscall handling code. Consequently, longjmp() would operate on a (potentially overwritten) stale jump buffer. This patch adds an additional setjmp and the necessary handling around it to make longjmp() (and by proxy cpu_loop_exit() safe to call even within a syscall context.
--- linux-user/aarch64/cpu_loop.c | 2 +- linux-user/alpha/cpu_loop.c | 2 +- linux-user/arm/cpu_loop.c | 2 +- linux-user/hexagon/cpu_loop.c | 2 +- linux-user/hppa/cpu_loop.c | 1 + linux-user/i386/cpu_loop.c | 8 +++++--- linux-user/include/special-errno.h | 8 ++++++++ linux-user/loongarch64/cpu_loop.c | 5 +++-- linux-user/m68k/cpu_loop.c | 2 +- linux-user/microblaze/cpu_loop.c | 2 +- linux-user/mips/cpu_loop.c | 9 ++++++--- linux-user/or1k/cpu_loop.c | 2 +- linux-user/ppc/cpu_loop.c | 10 +++++++--- linux-user/riscv/cpu_loop.c | 2 +- linux-user/s390x/cpu_loop.c | 2 +- linux-user/sh4/cpu_loop.c | 2 +- linux-user/sparc/cpu_loop.c | 4 +++- linux-user/syscall.c | 16 ++++++++++++++++ linux-user/xtensa/cpu_loop.c | 1 + 19 files changed, 60 insertions(+), 22 deletions(-)
linux-user/aarch64/cpu_loop.c +1 -1
@@ -181,7 +181,7 @@ void cpu_loop(CPUARMState *env) 0, 0);if (ret == -QEMU_ERESTARTSYS ) { env->pc -=4 ; - }else if (ret != -QEMU_ESIGRETURN ) { + }else if (ret != -QEMU_ESIGRETURN && ret != -QEMU_ESETPC ) { env->xregs[0 ] = ret; }break ;
linux-user/alpha/cpu_loop.c +1 -1
@@ -82,7 +82,7 @@ void cpu_loop(CPUAlphaState *env) env->pc -=4 ;break ; } -if (sysret == -QEMU_ESIGRETURN ) { +if (sysret == -QEMU_ESIGRETURN || sysret == -QEMU_ESETPC ) {break ; }/* Syscall writes 0 to V0 to bypass error check, similar
linux-user/arm/cpu_loop.c +1 -1
@@ -399,7 +399,7 @@ void cpu_loop(CPUARMState *env) 0, 0);if (ret == -QEMU_ERESTARTSYS ) { env->regs[15 ] -= env->thumb ?2 :4 ; - }else if (ret != -QEMU_ESIGRETURN ) { + }else if (ret != -QEMU_ESIGRETURN && ret != -QEMU_ESETPC ) { env->regs[0 ] = ret; } }
linux-user/hexagon/cpu_loop.c +1 -1
@@ -56,7 +56,7 @@ void cpu_loop(CPUHexagonState *env) 0, 0);if (ret == -QEMU_ERESTARTSYS ) { env->gpr[HEX_REG_PC ] -=4 ; - }else if (ret != -QEMU_ESIGRETURN ) { + }else if (ret != -QEMU_ESIGRETURN && ret != -QEMU_ESETPC ) { env->gpr[0 ] = ret; }break ;
linux-user/hppa/cpu_loop.c +1 -0
@@ -124,6 +124,7 @@ void cpu_loop(CPUHPPAState *env)break ;case -QEMU_ERESTARTSYS :case -QEMU_ESIGRETURN : +case -QEMU_ESETPC :break ; }break ;
linux-user/i386/cpu_loop.c +5 -3
@@ -181,7 +181,9 @@ static void emulate_vsyscall(CPUX86State *env)if (ret == -TARGET_EFAULT ) { goto sigsegv; } - env->regs[R_EAX ] = ret; +if (ret != -QEMU_ESETPC ) { + env->regs[R_EAX ] = ret; + }/* Emulate a ret instruction to leave the vsyscall page. */ env->eip = caller;
@@ -234,7 +236,7 @@ void cpu_loop(CPUX86State *env) 0, 0);if (ret == -QEMU_ERESTARTSYS ) { env->eip -=2 ; - }else if (ret != -QEMU_ESIGRETURN ) { + }else if (ret != -QEMU_ESIGRETURN && ret != -QEMU_ESETPC ) { env->regs[R_EAX ] = ret; }break ;
@@ -253,7 +255,7 @@ void cpu_loop(CPUX86State *env) 0, 0);if (ret == -QEMU_ERESTARTSYS ) { env->eip -=2 ; - }else if (ret != -QEMU_ESIGRETURN ) { + }else if (ret != -QEMU_ESIGRETURN && ret != -QEMU_ESETPC ) { env->regs[R_EAX ] = ret; }break ;
linux-user/include/special-errno.h +8 -0
@@ -29,4 +29,12 @@*/ #define QEMU_ESIGRETURN 513 +/* +* This is returned after a plugin has used the qemu_plugin_set_pc API, to +* indicate that the plugin deliberately changed the PC and potentially +* modified the register values. The main loop should not touch the guest +* registers for this reason. +*/ +#define QEMU_ESETPC 514 +#endif /* SPECIAL_ERRNO_H */
linux-user/loongarch64/cpu_loop.c +3 -2
@@ -44,9 +44,10 @@ void cpu_loop(CPULoongArchState *env) env->pc -=4 ;break ; } -if (ret == -QEMU_ESIGRETURN ) { +if (ret == -QEMU_ESIGRETURN || ret == -QEMU_ESETPC ) {/* -* Returning from a successful sigreturn syscall. +* Returning from a successful sigreturn syscall or from +* control flow diversion in a plugin callback. * Avoid clobbering register state. */ break ;
linux-user/m68k/cpu_loop.c +1 -1
@@ -66,7 +66,7 @@ void cpu_loop(CPUM68KState *env) 0, 0);if (ret == -QEMU_ERESTARTSYS ) { env->pc -=2 ; - }else if (ret != -QEMU_ESIGRETURN ) { + }else if (ret != -QEMU_ESIGRETURN && ret != -QEMU_ESETPC ) { env->dregs[0 ] = ret; } }
linux-user/microblaze/cpu_loop.c +1 -1
@@ -54,7 +54,7 @@ void cpu_loop(CPUMBState *env)if (ret == -QEMU_ERESTARTSYS ) {/* Wind back to before the syscall. */ env->pc -=4 ; - }else if (ret != -QEMU_ESIGRETURN ) { + }else if (ret != -QEMU_ESIGRETURN && ret != -QEMU_ESETPC ) { env->regs[3 ] = ret; }/* All syscall exits result in guest r14 being equal to the
linux-user/mips/cpu_loop.c +6 -3
@@ -140,9 +140,12 @@ done_syscall: env->active_tc.PC -=4 ;break ; } -if (ret == -QEMU_ESIGRETURN ) { -/* Returning from a successful sigreturn syscall. -Avoid clobbering register state. */ +if (ret == -QEMU_ESIGRETURN || ret == -QEMU_ESETPC ) { +/* +* Returning from a successful sigreturn syscall or from +* control flow diversion in a plugin callback. +* Avoid clobbering register state. +*/ break ; }if ((abi_ulong )ret >= (abi_ulong)-1133 ) {
linux-user/or1k/cpu_loop.c +1 -1
@@ -48,7 +48,7 @@ void cpu_loop(CPUOpenRISCState *env) cpu_get_gpr(env, 8), 0, 0);if (ret == -QEMU_ERESTARTSYS ) { env->pc -=4 ; - }else if (ret != -QEMU_ESIGRETURN ) { + }else if (ret != -QEMU_ESIGRETURN && ret != -QEMU_ESETPC ) {cpu_set_gpr (env,11 , ret); }break ;
linux-user/ppc/cpu_loop.c +7 -3
@@ -340,9 +340,13 @@ void cpu_loop(CPUPPCState *env) env->nip -=4 ;break ; } -if (ret == (target_ulong)(-QEMU_ESIGRETURN )) { -/* Returning from a successful sigreturn syscall. -Avoid corrupting register state. */ +if (ret == (target_ulong)(-QEMU_ESIGRETURN ) || + ret == (target_ulong)(-QEMU_ESETPC )) { +/* +* Returning from a successful sigreturn syscall or from +* control flow diversion in a plugin callback. +* Avoid corrupting register state. +*/ break ; }if (ret > (target_ulong)(-515 )) {
linux-user/riscv/cpu_loop.c +1 -1
@@ -65,7 +65,7 @@ void cpu_loop(CPURISCVState *env) }if (ret == -QEMU_ERESTARTSYS ) { env->pc -=4 ; - }else if (ret != -QEMU_ESIGRETURN ) { + }else if (ret != -QEMU_ESIGRETURN && ret != -QEMU_ESETPC ) { env->gpr[xA0] = ret; }if (cs->singlestep_enabled) {
linux-user/s390x/cpu_loop.c +1 -1
@@ -83,7 +83,7 @@ void cpu_loop(CPUS390XState *env) env->regs[6], env->regs[7], 0, 0);if (ret == -QEMU_ERESTARTSYS ) { env->psw.addr -= env->int_svc_ilen; - }else if (ret != -QEMU_ESIGRETURN ) { + }else if (ret != -QEMU_ESIGRETURN && ret != -QEMU_ESETPC ) { env->regs[2 ] = ret; }
linux-user/sh4/cpu_loop.c +1 -1
@@ -50,7 +50,7 @@ void cpu_loop(CPUSH4State *env) 0, 0);if (ret == -QEMU_ERESTARTSYS ) { env->pc -=2 ; - }else if (ret != -QEMU_ESIGRETURN ) { + }else if (ret != -QEMU_ESIGRETURN && ret != -QEMU_ESETPC ) { env->gregs[0 ] = ret; }break ;
linux-user/sparc/cpu_loop.c +3 -1
@@ -229,7 +229,9 @@ void cpu_loop (CPUSPARCState *env) env->regwptr[2 ], env->regwptr[3 ], env->regwptr[4 ], env->regwptr[5 ], 0, 0); -if (ret == -QEMU_ERESTARTSYS || ret == -QEMU_ESIGRETURN ) { +if (ret == -QEMU_ERESTARTSYS || + ret == -QEMU_ESIGRETURN || + ret == -QEMU_ESETPC ) {break ; }if ((abi_ulong )ret >= (abi_ulong)(-515 )) {
linux-user/syscall.c +16 -0
@@ -43,6 +43,7 @@#include <linux/capability.h> #include <sched.h> #include <sys/timex.h> +#include <setjmp.h> #include <sys/socket.h> #include <linux/sockios.h> #include <sys/un.h>
@@ -600,6 +601,9 @@ const char *target_strerror(int err)if (err ==QEMU_ESIGRETURN ) {return "Successful exit from sigreturn" ; } +if (err ==QEMU_ESETPC ) { +return "Successfully redirected control flow" ; + }return strerror (target_to_host_errno (err)); }
@@ -14410,6 +14414,18 @@ abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,return -QEMU_ESIGRETURN ; } +/* +* Set up a longjmp target here so that we can call cpu_loop_exit to +* redirect control flow back to the main loop even from within +* syscall-related plugin callbacks. +* For other types of callbacks or longjmp call sites, the longjmp target +* is set up in the cpu loop itself but in syscalls the target is not live +* anymore. +*/ +if (unlikely (sigsetjmp (cpu->jmp_env,0 ) !=0 )) { +return -QEMU_ESETPC ; + } +record_syscall_start (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
linux-user/xtensa/cpu_loop.c +1 -0
@@ -186,6 +186,7 @@ void cpu_loop(CPUXtensaState *env)break ;case -QEMU_ESIGRETURN : +case -QEMU_ESETPC :break ; }break ;
Signature · 1 line
2.53.0
PATCH v7 4/8 plugins: add PC diversion API function
In reply to Florian HofhammerThis patch adds a plugin API function that allows diverting the program counter during execution. A potential use case for this functionality is to skip over parts of the code, e.g., by hooking into a specific instruction and setting the PC to the next instruction in the callback.
--- include/plugins/qemu-plugin.h | 13 +++++++++++++ plugins/api.c | 11 +++++++++++ scripts/qemu-plugin-symbols.py | 9 +++++++-- 3 files changed, 31 insertions(+), 2 deletions(-)
include/plugins/qemu-plugin.h +13 -0
@@ -76,6 +76,7 @@ typedef uint64_t qemu_plugin_id_t;* * version 6: * - changed return value of qemu_plugin_{read,write}_register from int to bool +* - added qemu_plugin_set_pc */ extern QEMU_PLUGIN_EXPORT int qemu_plugin_version;
@@ -1042,6 +1043,18 @@ QEMU_PLUGIN_APIbool qemu_plugin_write_register (struct qemu_plugin_register *handle,GByteArray *buf); +/** +* qemu_plugin_set_pc() - set the program counter for the current vCPU +* +* @vaddr: the new virtual (guest) address for the program counter +* +* This function sets the program counter for the current vCPU to @vaddr and +* resumes execution at that address. This function does not return. +*/ +QEMU_PLUGIN_API +__attribute__((__noreturn__)) +void qemu_plugin_set_pc(uint64_t vaddr); +/** * qemu_plugin_read_memory_vaddr() - read from memory using a virtual address *
plugins/api.c +11 -0
@@ -41,6 +41,7 @@#include "qemu/log.h" #include "system/memory.h" #include "tcg/tcg.h" +#include "exec/cpu-common.h" #include "exec/gdbstub.h" #include "exec/target_page.h" #include "exec/translation-block.h"
@@ -467,6 +468,16 @@ bool qemu_plugin_write_register(struct qemu_plugin_register *reg,return (gdb_write_register (current_cpu, buf->data,GPOINTER_TO_INT (reg) -1 ) >0 ); } +void qemu_plugin_set_pc (uint64_t vaddr) +{ +g_assert (current_cpu); + +g_assert (qemu_plugin_get_cb_flags () ==QEMU_PLUGIN_CB_RW_REGS_PC ); + +cpu_set_pc (current_cpu, vaddr); +cpu_loop_exit (current_cpu); +} +bool qemu_plugin_read_memory_vaddr (uint64_t addr,GByteArray *data,size_t len) {g_assert (current_cpu);
scripts/qemu-plugin-symbols.py +7 -2
@@ -20,9 +20,14 @@ def extract_symbols(plugin_header): # Remove QEMU_PLUGIN_API macro definition. content = content.replace('#define QEMU_PLUGIN_API', '') expected = content.count('QEMU_PLUGIN_API') - # Find last word between QEMU_PLUGIN_API and (, matching on several lines. + # Find last word between QEMU_PLUGIN_API and ( to get the function name, + # matching on several lines. Discard attributes, if any. # We use *? non-greedy quantifier. - syms = re.findall(r'QEMU_PLUGIN_API.*?(\w+)\s*\(', content, re.DOTALL) + syms = re.findall( + r'QEMU_PLUGIN_API\s+(?:__attribute__\(\(\S+\)\))?.*?(\w+)\s*\(', + content, + re.DOTALL, + ) syms.sort() # Ensure we found as many symbols as API markers. assert len(syms) == expected
Signature · 1 line
2.53.0
PATCH v7 5/8 tests/tcg: add tests for qemu_plugin_set_pc API
In reply to Florian HofhammerThe test plugin intercepts execution in different contexts. Without the plugin, any of the implemented test functions would trigger an assert and fail. With the plugin, control flow is redirected to skip the assert and return cleanly via the qemu_plugin_set_pc() API.
---
MAINTAINERS | 1 +
tests/tcg/arm/Makefile.target | 6 +
tests/tcg/hexagon/Makefile.target | 8 ++
tests/tcg/multiarch/Makefile.target | 17 ++-
.../multiarch/{ => plugin}/check-plugin-output.sh | 0
.../{ => plugin}/test-plugin-mem-access.c | 0
tests/tcg/multiarch/plugin/test-plugin-set-pc.c | 134 +++++++++++++++++++++
tests/tcg/plugins/meson.build | 1 +
tests/tcg/plugins/setpc.c | 109 +++++++++++++++++
9 files changed, 273 insertions(+), 3 deletions(-)MAINTAINERS +1 -0
@@ -4104,6 +4104,7 @@ S: Maintained F: docs/devel/tcg-plugins.rst F: plugins/ F: tests/tcg/plugins/ +F: tests/tcg/multiarch/plugin/ F: tests/functional/aarch64/test_tcg_plugins.py F: contrib/plugins/ F: scripts/qemu-plugin-symbols.py
tests/tcg/arm/Makefile.target +6 -0
@@ -78,4 +78,10 @@ sha512-vector: sha512.c ARM_TESTS += sha512-vector +ifeq ($(CONFIG_PLUGIN),y) +# Require emitting arm32 instructions, otherwise the vCPU might accidentally +# try to execute Thumb instructions in arm32 mode after qemu_plugin_set_pc() +test-plugin-set-pc: CFLAGS+=-marm +endif + TESTS += $(ARM_TESTS)
tests/tcg/hexagon/Makefile.target +8 -0
@@ -126,3 +126,11 @@ v73_scalar: CFLAGS += -Wno-unused-function hvx_histogram: hvx_histogram.c hvx_histogram_row.S $(CC) $(CFLAGS) $(CROSS_CC_GUEST_CFLAGS) $^ -o $@ $(LDFLAGS) + +ifeq ($(CONFIG_PLUGIN),y) +# LLVM is way too aggressive with inlining and dead code elimination even at +# -O0, which interferes with the test. What looks like dead code in this test +# to the compiler isn't actually dead code, so we need to disable all potential +# LLVM optimization passes. +test-plugin-set-pc: CFLAGS += -Xclang -disable-llvm-passes +endif
tests/tcg/multiarch/Makefile.target +14 -3
@@ -14,6 +14,10 @@ ifeq ($(filter %-linux-user, $(TARGET)),$(TARGET)) VPATH += $(MULTIARCH_SRC)/linux MULTIARCH_SRCS += $(notdir $(wildcard $(MULTIARCH_SRC)/linux/*.c)) endif +ifeq ($(CONFIG_PLUGIN),y) +VPATH += $(MULTIARCH_SRC)/plugin +MULTIARCH_SRCS += $(notdir $(wildcard $(MULTIARCH_SRC)/plugin/*.c)) +endif MULTIARCH_TESTS = $(MULTIARCH_SRCS:.c=) #
@@ -200,13 +204,20 @@ run-plugin-test-plugin-mem-access-with-libmem.so: \ PLUGIN_ARGS=$(COMMA)print-accesses=true run-plugin-test-plugin-mem-access-with-libmem.so: \ CHECK_PLUGIN_OUTPUT_COMMAND= \ - $(SRC_PATH)/tests/tcg/multiarch/check-plugin-output.sh \ + $(SRC_PATH)/tests/tcg/multiarch/plugin/check-plugin-output.sh \ $(QEMU) $< run-plugin-test-plugin-syscall-filter-with-libsyscall.so: +run-plugin-test-plugin-set-pc-with-libsetpc.so: EXTRA_RUNS_WITH_PLUGIN += run-plugin-test-plugin-mem-access-with-libmem.so \ - run-plugin-test-plugin-syscall-filter-with-libsyscall.so -else + run-plugin-test-plugin-syscall-filter-with-libsyscall.so \ + run-plugin-test-plugin-set-pc-with-libsetpc.so + +else # CONFIG_PLUGIN=n +# Do not build the syscall skipping test if it's not tested with the setpc +# plugin because it will simply fail the test. +MULTIARCH_TESTS := $(filter-out test-plugin-set-pc, $(MULTIARCH_TESTS)) + # test-plugin-syscall-filter needs syscall plugin to succeed test-plugin-syscall-filter: CFLAGS+=-DSKIP endif
tests/tcg/multiarch/plugin/check-plugin-output.sh renamed from tests/tcg/multiarch/check-plugin-output.sh +0 -0
tests/tcg/multiarch/plugin/test-plugin-mem-access.c renamed from tests/tcg/multiarch/test-plugin-mem-access.c +0 -0
tests/tcg/multiarch/plugin/test-plugin-set-pc.c new file +134 -0
134 lines not on this page · raw message
tests/tcg/plugins/meson.build +1 -0
@@ -7,6 +7,7 @@ test_plugins = [ 'mem.c', 'patch.c', 'reset.c', +'setpc.c', 'syscall.c', ]
tests/tcg/plugins/setpc.c new file +109 -0
109 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [PATCH v7 5/8] tests/tcg: add tests for qemu_plugin_set_pc API
Quoted 7 lines · Florian Hofhammer
The test plugin intercepts execution in different contexts. Without the plugin, any of the implemented test functions would trigger an assert and fail. With the plugin, control flow is redirected to skip the assert and return cleanly via the
qemu_plugin_set_pc()API.Signed-off-by Pierrick Bouvier <[email protected]>Signed-off-by Florian Hofhammer <[email protected]>
Quoted 12 lines
---
MAINTAINERS | 1 +
tests/tcg/arm/Makefile.target | 6 +
tests/tcg/hexagon/Makefile.target | 8 ++
tests/tcg/multiarch/Makefile.target | 17 ++-
.../multiarch/{ => plugin}/check-plugin-output.sh | 0
.../{ => plugin}/test-plugin-mem-access.c | 0
tests/tcg/multiarch/plugin/test-plugin-set-pc.c | 134 +++++++++++++++++++++
tests/tcg/plugins/meson.build | 1 +
tests/tcg/plugins/setpc.c | 109 +++++++++++++++++
9 files changed, 273 insertions(+), 3 deletions(-)Thanks, it looks good!
PATCH v7 6/8 plugins: add read-only property for registers
In reply to Florian HofhammerSome registers should be marked as read-only from a plugin API perspective, as writing to them via qemu_plugin_write_register has no effect. This includes the program counter, and we expose this fact to the plugins with this patch.
--- include/plugins/qemu-plugin.h | 3 +++ plugins/api.c | 16 ++++++++++++++++ 2 files changed, 19 insertions(+)
include/plugins/qemu-plugin.h +3 -0
@@ -979,11 +979,14 @@ struct qemu_plugin_register;* writing value with qemu_plugin_write_register * @name: register name * @feature: optional feature descriptor, can be NULL +* @is_readonly: true if the register cannot be written via +* qemu_plugin_write_register */ typedef struct {struct qemu_plugin_register *handle;const char *name;const char *feature; +bool is_readonly; }qemu_plugin_reg_descriptor ;/**
plugins/api.c +16 -0
@@ -410,6 +410,12 @@ bool qemu_plugin_bool_parse(const char *name, const char *value, bool *ret)* ancillary data the plugin might find useful. */ +static const char pc_str[] ="pc" ;/* generic name for program counter */ +static const char eip_str[] ="eip" ;/* x86-specific name for PC */ +static const char rip_str[] ="rip" ;/* x86_64-specific name for PC */ +static const char pswa_str[] ="pswa" ;/* s390x-specific name for PC */ +static const char iaoq_str[] ="iaoq" ;/* HP/PA-specific name for PC */ +static const char rpc_str[] ="rpc" ;/* microblaze-specific name for PC */ static GArray *create_register_handles (GArray *gdbstub_regs) { GArray *find_data = g_array_new(true, true,
@@ -427,6 +433,16 @@ static GArray *create_register_handles(GArray *gdbstub_regs)/* Create a record for the plugin */ desc.handle =GINT_TO_POINTER (grd->gdb_reg +1 ); desc.name =g_intern_string (grd->name); + desc.is_readonly = false; +if (g_strcmp0 (desc.name, pc_str) ==0 + ||g_strcmp0 (desc.name, eip_str) ==0 + ||g_strcmp0 (desc.name, rip_str) ==0 + ||g_strcmp0 (desc.name, pswa_str) ==0 + ||g_strcmp0 (desc.name, iaoq_str) ==0 + ||g_strcmp0 (desc.name, rpc_str) ==0 + ) { + desc.is_readonly = true; + } desc.feature =g_intern_string (grd->feature_name);g_array_append_val (find_data, desc); }
Signature · 1 line
2.53.0
PATCH v7 7/8 plugins: prohibit writing to read-only registers
In reply to Florian HofhammerThe opaque register handle encodes whether a register is read-only in the lowest bit and prevents writing to the register via the plugin API in this case.
--- plugins/api.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
plugins/api.c +8 -3
@@ -424,6 +424,7 @@ static GArray *create_register_handles(GArray *gdbstub_regs)for (int i =0 ; i < gdbstub_regs->len; i++) {GDBRegDesc *grd = &g_array_index (gdbstub_regs, GDBRegDesc, i);qemu_plugin_reg_descriptor desc; +gint plugin_ro_bit =0 ;/* skip "un-named" regs */ if (!grd->name) {
@@ -431,7 +432,6 @@ static GArray *create_register_handles(GArray *gdbstub_regs) }/* Create a record for the plugin */ - desc.handle =GINT_TO_POINTER (grd->gdb_reg +1 ); desc.name =g_intern_string (grd->name); desc.is_readonly = false; if (g_strcmp0(desc.name, pc_str) == 0
@@ -442,7 +442,9 @@ static GArray *create_register_handles(GArray *gdbstub_regs) || g_strcmp0(desc.name, rpc_str) == 0 ) { desc.is_readonly = true; + plugin_ro_bit =1 ; } + desc.handle =GINT_TO_POINTER ((grd->gdb_reg <<1 ) | plugin_ro_bit); desc.feature =g_intern_string (grd->feature_name);g_array_append_val (find_data, desc); }
@@ -467,7 +469,7 @@ bool qemu_plugin_read_register(struct qemu_plugin_register *reg,return false; } -return (gdb_read_register (current_cpu, buf,GPOINTER_TO_INT (reg) -1 ) >0 ); +return (gdb_read_register (current_cpu, buf,GPOINTER_TO_INT (reg) >>1 ) >0 ); } bool qemu_plugin_write_register(struct qemu_plugin_register *reg,
@@ -475,13 +477,16 @@ bool qemu_plugin_write_register(struct qemu_plugin_register *reg, {g_assert (current_cpu); +/* Read-only property is encoded in least significant bit */ +g_assert ((GPOINTER_TO_INT (reg) &1 ) ==0 ); +if (buf->len ==0 || (qemu_plugin_get_cb_flags () !=QEMU_PLUGIN_CB_RW_REGS &&qemu_plugin_get_cb_flags () !=QEMU_PLUGIN_CB_RW_REGS_PC )) {return false; } -return (gdb_write_register (current_cpu, buf->data,GPOINTER_TO_INT (reg) -1 ) >0 ); +return (gdb_write_register (current_cpu, buf->data,GPOINTER_TO_INT (reg) >>1 ) >0 ); }void qemu_plugin_set_pc (uint64_t vaddr)
Signature · 1 line
2.53.0
PATCH v7 8/8 tests/tcg/plugins: test register accesses
In reply to Florian HofhammerThe additional plugin tests register accesses, specifically both for read-only and read-write registers. Writing to a read-only register is currently not tested, as this would trigger an assertion and fail the test.
--- tests/tcg/plugins/meson.build | 1 + tests/tcg/plugins/registers.c | 79 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+)
tests/tcg/plugins/meson.build +1 -0
@@ -6,6 +6,7 @@ test_plugins = [ 'insn.c', 'mem.c', 'patch.c', +'registers.c', 'reset.c', 'setpc.c', 'syscall.c',
tests/tcg/plugins/registers.c new file +79 -0
79 lines not on this page · raw message
Signature · 1 line
2.53.0
Quoted 107 lines · Florian Hofhammer
Hi,
This patch series builds on top of the discussion from the thread at
https://lore.kernel.org/qemu-devel/[email protected]/
and adds a plugin API function to set the program counter of the guest, as just writing to it viaqemu_plugin_write_register()has no direct effect.This version v7 of the patch series addresses the requested changes from the previous v6 submission (details below).
Note: checkpatch.pl still reports a warning about line length violations in patch nr. 7/8 but I did not fix this, as the line was already > 80 characters long previously, the change added only a single character, and I think the readability of the code is better as it is now. Please let me know if you disagree and would like me to fix this!
@Pierrick: you already reviewed patch 3/7 (now 4/8) previously, but I had to change the regex in
scripts/qemu-plugin-symbols.pyto account for attributes, so please re-review!Best regards,
FlorianChanges:
v7:
- Add Pierrick's patch to clamp 32-bit registers to prevent accidental
sign extension
- Fix documentation build by using __attribute__((__noreturn__)) instead
ofG_NORETURN(required adjustingscripts/qemu-plugin-symbols.py).
- Rework test-plugin-set-pc test to get rid of volatile guard and make control flow more linear
- Link to v6: https://lore.kernel.org/qemu-devel/[email protected]
v6:
- update commit message for patch 4/7
v5:
- make QEMU abort via asserts instead of just returning an error from
the plugin API if preconditions are violated
- extend tests forqemu_plugin_set_pc()to different contexts
- fix issues highlighted by checkpatch.pl
v4:
- switch strcmp out in favor ofg_strcmp0
- split the patch introducing theqemu_plugin_set_pc()API into three patches, two for preparing the plugin infrastructure and the syscall handling code and a third introducing the actual plugin API
v3:
- make PC registers read-only across architectures
- add tests for read-only registers
- adjust test structure forqemu_plugin_set_pc()by moving architecture-specific tests into corresponding directories
v2:
- addsetjmp()in syscall handling path to allow PC redirection from
syscall callbacks (vialongjmp(), the cpu_loop()'ssetjmp()for
exiting a TB would not be live anymore in syscall handlers)
- add flags to ensure theqemu_plugin_set_pc()API is only called from contexts where the CPU is live
- add test forqemu_plugin_set_pc()API
v1:
- initial version---
Florian Hofhammer (7):
plugins: add flag to specify whether PC is rw
linux-user: make syscall emulation interruptible
plugins: add PC diversion API function tests/tcg: add tests forqemu_plugin_set_pcAPI
plugins: add read-only property for registers
plugins: prohibit writing to read-only registers tests/tcg/plugins: test register accessesPierrick Bouvier (1):
plugins/core: clamp syscall arguments if target is 32-bitMAINTAINERS | 1 + include/plugins/qemu-plugin.h | 19 +++ linux-user/aarch64/cpu_loop.c | 2 +- linux-user/alpha/cpu_loop.c | 2 +- linux-user/arm/cpu_loop.c | 2 +- linux-user/hexagon/cpu_loop.c | 2 +- linux-user/hppa/cpu_loop.c | 1 + linux-user/i386/cpu_loop.c | 8 +- linux-user/include/special-errno.h | 8 ++ linux-user/loongarch64/cpu_loop.c | 5 +- linux-user/m68k/cpu_loop.c | 2 +- linux-user/microblaze/cpu_loop.c | 2 +- linux-user/mips/cpu_loop.c | 9 +- linux-user/or1k/cpu_loop.c | 2 +- linux-user/ppc/cpu_loop.c | 10 +- linux-user/riscv/cpu_loop.c | 2 +- linux-user/s390x/cpu_loop.c | 2 +- linux-user/sh4/cpu_loop.c | 2 +- linux-user/sparc/cpu_loop.c | 4 +- linux-user/syscall.c | 16 +++ linux-user/xtensa/cpu_loop.c | 1 + plugins/api.c | 42 ++++++- plugins/core.c | 50 ++++++-- scripts/qemu-plugin-symbols.py | 9 +- tests/tcg/arm/Makefile.target | 6 + tests/tcg/hexagon/Makefile.target | 8 ++ tests/tcg/multiarch/Makefile.target | 17 ++- .../multiarch/{ => plugin}/check-plugin-output.sh | 0 .../{ => plugin}/test-plugin-mem-access.c | 0 tests/tcg/multiarch/plugin/test-plugin-set-pc.c | 134 +++++++++++++++++++++ tests/tcg/plugins/meson.build | 2 + tests/tcg/plugins/registers.c | 79 ++++++++++++ tests/tcg/plugins/setpc.c | 109 +++++++++++++++++ 33 files changed, 514 insertions(+), 44 deletions(-)
Quoted 3 lines
---
base-commit:3fb456e9a0e9eef6a71d9b49bfff596a0f0046e9
change-id: 20260303-setpc-v5-c1df30bad07f
This was merged into master (2d3f78269a14a389aefecba28decd16dd5151802). Thank you for your contribution!
Regards,
Pierrick