fix plugins double counting with mmio, cleanup CF_ flags
cover fix plugins double counting with mmio, cleanup CF_ flags
Hi,
Aaron reported an issue with TCG plugins when interacting with the cpu_io_recompile code during icount. The ultimate fix was to avoid instrumenting the re-executed block but along the way we clean-up a bunch of the code by getting rid of CF_NOCACHE. I've also included Richard's recently posted recompile hook cleanups at the start of the series because it makes the improves the diffstat by pushing more arch specific black magic to the targets. In fact without the additional tests this removes more code than it adds ;-)
I've added some acceptance tests to detect the failure mode as well as manually testing with a test Peter had lying around that exercises the trixy "executing out of MMIO" code path which I've touched.
Please test and review.
Alex Bennée (8):
tests/plugin: expand insn test to detect duplicate instructions tests/acceptance: add a new set of tests to exercise plugins accel/tcg: actually cache our partial icount TB accel/tcg: cache single instruction TB on pending replay exception accel/tcg: re-factor non-RAM execution code
accel/tcg: remove CF_NOCACHE and special cases accel/tcg: allow plugin instrumentation to be disable via cflags tests/acceptance: add a new tests to detect counting errors
Richard Henderson (4):
exec: Move TranslationBlock typedef to qemu/typedefs.h accel/tcg: Create io_recompile_replay_branch hook target/mips: Create mips_io_recompile_replay_branch target/sh4: Create superh_io_recompile_replay_branch
include/exec/exec-all.h | 6 +- include/exec/tb-context.h | 1 - include/hw/core/cpu.h | 4 +- include/hw/core/tcg-cpu-ops.h | 13 +++- include/qemu/typedefs.h | 1 + target/arm/internals.h | 3 +- accel/tcg/cpu-exec.c | 62 ++++----------- accel/tcg/translate-all.c | 128 +++++++++++------------------- accel/tcg/translator.c | 2 +- target/cris/translate.c | 2 +- target/lm32/translate.c | 2 +- target/mips/cpu.c | 18 +++++ target/moxie/translate.c | 2 +- target/sh4/cpu.c | 18 +++++ target/unicore32/translate.c | 2 +- tests/plugin/insn.c | 12 ++- tests/acceptance/tcg_plugins.py | 134 ++++++++++++++++++++++++++++++++ 17 files changed, 263 insertions(+), 147 deletions(-) create mode 100644 tests/acceptance/tcg_plugins.py
Signature · 1 line
2.20.1
PATCH 1/12 exec: Move TranslationBlock typedef to qemu/typedefs.h
From: Richard Henderson <[email protected]>
This also means we don't need an extra declaration of the structure in hw/core/cpu.h.
Message-Id: <[email protected]>
--- include/exec/tb-context.h | 1 - include/hw/core/cpu.h | 4 +--- include/hw/core/tcg-cpu-ops.h | 3 +-- include/qemu/typedefs.h | 1 + target/arm/internals.h | 3 +-- target/cris/translate.c | 2 +- target/lm32/translate.c | 2 +- target/moxie/translate.c | 2 +- target/unicore32/translate.c | 2 +- 9 files changed, 8 insertions(+), 12 deletions(-)
include/exec/tb-context.h +0 -1
@@ -26,7 +26,6 @@#define CODE_GEN_HTABLE_BITS 15#define CODE_GEN_HTABLE_SIZE (1 << CODE_GEN_HTABLE_BITS) -typedef struct TranslationBlock TranslationBlock ;typedef struct TBContext TBContext ;struct TBContext {
include/hw/core/cpu.h +1 -3
@@ -74,8 +74,6 @@ typedef enum MMUAccessType {typedef struct CPUWatchpoint CPUWatchpoint ; -struct TranslationBlock ; -/* see tcg-cpu-ops.h */ struct TCGCPUOps ;
@@ -375,7 +373,7 @@ struct CPUState {IcountDecr *icount_decr_ptr;/* Accessed in parallel; all accesses must be atomic */ -struct TranslationBlock *tb_jmp_cache[TB_JMP_CACHE_SIZE ]; +TranslationBlock *tb_jmp_cache[TB_JMP_CACHE_SIZE ];struct GDBRegisterState *gdb_regs;int gdb_num_regs;
include/hw/core/tcg-cpu-ops.h +1 -2
@@ -30,8 +30,7 @@ struct TCGCPUOps {* If more state needs to be restored, the target must implement a * function to restore all the state, and register it here. */ -void (*synchronize_from_tb)(CPUState *cpu, -const struct TranslationBlock *tb); +void (*synchronize_from_tb)(CPUState *cpu,const TranslationBlock *tb);/** @cpu_exec_enter: Callback for cpu_exec preparation */ void (*cpu_exec_enter)(CPUState *cpu);/** @cpu_exec_exit: Callback for cpu_exec cleanup */
include/qemu/typedefs.h +1 -0
@@ -120,6 +120,7 @@ typedef struct ReservedRegion ReservedRegion;typedef struct SavedIOTLB SavedIOTLB ;typedef struct SHPCDevice SHPCDevice ;typedef struct SSIBus SSIBus ; +typedef struct TranslationBlock TranslationBlock ;typedef struct VirtIODevice VirtIODevice ;typedef struct Visitor Visitor ;typedef struct VMChangeStateEntry VMChangeStateEntry ;
target/arm/internals.h +1 -2
@@ -172,8 +172,7 @@ void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu);void arm_translate_init (void );#ifdef CONFIG_TCG -void arm_cpu_synchronize_from_tb (CPUState *cs, -const struct TranslationBlock *tb); +void arm_cpu_synchronize_from_tb (CPUState *cs,const TranslationBlock *tb);#endif /* CONFIG_TCG */
target/cris/translate.c +1 -1
@@ -132,7 +132,7 @@ typedef struct DisasContext {int delayed_branch; -struct TranslationBlock *tb; +TranslationBlock *tb;int singlestep_enabled; } DisasContext;
target/lm32/translate.c +1 -1
@@ -93,7 +93,7 @@ typedef struct DisasContext {unsigned int tb_flags, synced_flags;/* tb dependent flags. */ int is_jmp; -struct TranslationBlock *tb; +TranslationBlock *tb;int singlestep_enabled;uint32_t features;
target/moxie/translate.c +1 -1
@@ -36,7 +36,7 @@/* This is the state at translation time. */ typedef struct DisasContext { - struct TranslationBlock *tb; + TranslationBlock *tb; target_ulong pc, saved_pc; uint32_t opcode; uint32_t fp_status;
target/unicore32/translate.c +1 -1
@@ -34,7 +34,7 @@ typedef struct DisasContext {int condjmp;/* The label that will be jumped to when the instruction is skipped. */ TCGLabel *condlabel; -struct TranslationBlock *tb; +TranslationBlock *tb;int singlestep_enabled;#ifndef CONFIG_USER_ONLY int user;
Signature · 1 line
2.20.1
PATCH 2/12 accel/tcg: Create io_recompile_replay_branch hook
In reply to Alex BennéeFrom: Richard Henderson <[email protected]>
Create a hook in which to split out the mips and
sh4 ifdefs from cpu_io_recompile.
Message-Id: <[email protected]>
--- include/hw/core/tcg-cpu-ops.h | 10 ++++++++++ accel/tcg/translate-all.c | 17 +++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-)
include/hw/core/tcg-cpu-ops.h +10 -0
@@ -88,6 +88,16 @@ struct TCGCPUOps {*/ bool (*debug_check_watchpoint)(CPUState *cpu,CPUWatchpoint *wp); +/** +* @io_recompile_replay_branch: Callback for cpu_io_recompile. +* +* The cpu has been stopped, and cpu_restore_state_from_tb has been +* called. If the faulting instruction is in a delay slot, and the +* target architecture requires re-execution of the branch, then +* adjust the cpu state as required and return true. +*/ +bool (*io_recompile_replay_branch)(CPUState *cpu, +const TranslationBlock *tb);#endif /* CONFIG_SOFTMMU */ #endif /* NEED_CPU_H */
accel/tcg/translate-all.c +13 -4
@@ -60,6 +60,7 @@#include "sysemu/cpu-timers.h" #include "sysemu/tcg.h" #include "qapi/error.h" +#include "hw/core/tcg-cpu-ops.h" #include "internal.h" /* #define DEBUG_TB_INVALIDATE */
@@ -2420,6 +2421,7 @@ void cpu_io_recompile(CPUState *cpu, uintptr_t retaddr)CPUArchState *env = cpu->env_ptr;#endif TranslationBlock *tb; +CPUClass *cc;uint32_t n; tb =tcg_tb_lookup (retaddr);
@@ -2429,11 +2431,18 @@ void cpu_io_recompile(CPUState *cpu, uintptr_t retaddr) }cpu_restore_state_from_tb (cpu, tb, retaddr, true); -/* On MIPS and SH, delay slot instructions can only be restarted if -they were already the first instruction in the TB. If this is not -the first instruction in a TB then re-execute the preceding -branch. */ +/* +* Some guests must re-execute the branch when re-executing a delay +* slot instruction. When this is the case, adjust icount and N +* to account for the re-execution of the branch. +*/ n =1 ; + cc =CPU_GET_CLASS (cpu); +if (cc->tcg_ops->io_recompile_replay_branch && + cc->tcg_ops->io_recompile_replay_branch (cpu, tb)) { +cpu_neg (cpu)->icount_decr.u16.low++; + n =2 ; + }#if defined(TARGET_MIPS )if ((env->hflags &MIPS_HFLAG_BMASK ) !=0 && env->active_tc.PC != tb->pc) {
Signature · 1 line
2.20.1
PATCH 3/12 target/mips: Create mips_io_recompile_replay_branch
In reply to Alex BennéeFrom: Richard Henderson <[email protected]>
Move the code from accel/tcg/translate-all.c to target/mips/cpu.c.
Message-Id: <[email protected]>
--- accel/tcg/translate-all.c | 12 ++---------- target/mips/cpu.c | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 10 deletions(-)
accel/tcg/translate-all.c +2 -10
@@ -2417,7 +2417,7 @@ void tb_check_watchpoint(CPUState *cpu, uintptr_t retaddr)*/ void cpu_io_recompile (CPUState *cpu,uintptr_t retaddr) { -#if defined(TARGET_MIPS ) || defined(TARGET_SH4 ) +#if defined(TARGET_SH4 )CPUArchState *env = cpu->env_ptr;#endif TranslationBlock *tb;
@@ -2443,15 +2443,7 @@ void cpu_io_recompile(CPUState *cpu, uintptr_t retaddr)cpu_neg (cpu)->icount_decr.u16.low++; n =2 ; } -#if defined(TARGET_MIPS ) -if ((env->hflags &MIPS_HFLAG_BMASK ) !=0 - && env->active_tc.PC != tb->pc) { - env->active_tc.PC -= (env->hflags &MIPS_HFLAG_B16 ?2 :4 ); -cpu_neg (cpu)->icount_decr.u16.low++; - env->hflags &= ~MIPS_HFLAG_BMASK ; - n =2 ; - } -#elif defined(TARGET_SH4 ) +#if defined(TARGET_SH4 )if ((env->flags & ((DELAY_SLOT |DELAY_SLOT_CONDITIONAL ))) !=0 && env->pc != tb->pc) { env->pc -=2 ;
target/mips/cpu.c +18 -0
@@ -268,6 +268,23 @@ static void mips_cpu_synchronize_from_tb(CPUState *cs, env->hflags &= ~MIPS_HFLAG_BMASK ; env->hflags |= tb->flags & MIPS_HFLAG_BMASK; } + +# ifndef CONFIG_USER_ONLY +static bool mips_io_recompile_replay_branch (CPUState *cs, +const TranslationBlock *tb) +{ +MIPSCPU *cpu =MIPS_CPU (cs); +CPUMIPSState *env = &cpu->env; + +if ((env->hflags &MIPS_HFLAG_BMASK ) !=0 + && env->active_tc.PC != tb->pc) { + env->active_tc.PC -= (env->hflags &MIPS_HFLAG_B16 ?2 :4 ); + env->hflags &= ~MIPS_HFLAG_BMASK ; +return true; + } +return false; +} +# endif /* !CONFIG_USER_ONLY */ #endif /* CONFIG_TCG */ static bool mips_cpu_has_work (CPUState *cs)
@@ -679,6 +696,7 @@ static struct TCGCPUOps mips_tcg_ops = { .do_interrupt = mips_cpu_do_interrupt, .do_transaction_failed = mips_cpu_do_transaction_failed, .do_unaligned_access = mips_cpu_do_unaligned_access, + .io_recompile_replay_branch = mips_io_recompile_replay_branch, #endif /* !CONFIG_USER_ONLY */ };#endif /* CONFIG_TCG */
Signature · 1 line
2.20.1
PATCH 4/12 target/sh4: Create superh_io_recompile_replay_branch
In reply to Alex BennéeFrom: Richard Henderson <[email protected]>
Move the code from accel/tcg/translate-all.c to target/sh4/cpu.c.
Message-Id: <[email protected]>
--- accel/tcg/translate-all.c | 12 ------------ target/sh4/cpu.c | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 12 deletions(-)
accel/tcg/translate-all.c +0 -12
@@ -2417,9 +2417,6 @@ void tb_check_watchpoint(CPUState *cpu, uintptr_t retaddr)*/ void cpu_io_recompile (CPUState *cpu,uintptr_t retaddr) { -#if defined(TARGET_SH4 ) -CPUArchState *env = cpu->env_ptr; -#endif TranslationBlock *tb;CPUClass *cc;uint32_t n;
@@ -2443,15 +2440,6 @@ void cpu_io_recompile(CPUState *cpu, uintptr_t retaddr)cpu_neg (cpu)->icount_decr.u16.low++; n =2 ; } -#if defined(TARGET_SH4 ) -if ((env->flags & ((DELAY_SLOT |DELAY_SLOT_CONDITIONAL ))) !=0 - && env->pc != tb->pc) { - env->pc -=2 ; -cpu_neg (cpu)->icount_decr.u16.low++; - env->flags &= ~(DELAY_SLOT |DELAY_SLOT_CONDITIONAL ); - n =2 ; - } -#endif /* Generate a new TB executing the I/O insn. */ cpu->cflags_next_tb =curr_cflags () |CF_LAST_IO | n;
target/sh4/cpu.c +18 -0
@@ -43,6 +43,23 @@ static void superh_cpu_synchronize_from_tb(CPUState *cs, cpu->env.flags = tb->flags &TB_FLAG_ENVFLAGS_MASK ; } +#ifndef CONFIG_USER_ONLY +static bool superh_io_recompile_replay_branch (CPUState *cs, +const TranslationBlock *tb) +{ +SuperHCPU *cpu =SUPERH_CPU (cs); +CPUSH4State *env = &cpu->env; + +if ((env->flags & ((DELAY_SLOT |DELAY_SLOT_CONDITIONAL ))) !=0 + && env->pc != tb->pc) { + env->pc -=2 ; + env->flags &= ~(DELAY_SLOT |DELAY_SLOT_CONDITIONAL ); +return true; + } +return false; +} +#endif +static bool superh_cpu_has_work (CPUState *cs) {return cs->interrupt_request &CPU_INTERRUPT_HARD ;
@@ -217,6 +234,7 @@ static struct TCGCPUOps superh_tcg_ops = {#ifndef CONFIG_USER_ONLY .do_interrupt = superh_cpu_do_interrupt, .do_unaligned_access = superh_cpu_do_unaligned_access, + .io_recompile_replay_branch = superh_io_recompile_replay_branch,#endif /* !CONFIG_USER_ONLY */ };
Signature · 1 line
2.20.1
PATCH 5/12 tests/plugin: expand insn test to detect duplicate instructions
In reply to Alex BennéeA duplicate insn is one that is appears to be executed twice in a row. This is currently possible due to -icount and cpu_io_recompile() causing a re-translation of a block. On it's own this won't trigger any tests though.
---
[AJB: well not quite, the x86_64 test trips over this due to some weirdness in the way we handle rep insns, e.g. rep movsb (%esi), %es:(%edi) in the x86 bios code]
--- tests/plugin/insn.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
tests/plugin/insn.c +11 -1
@@ -21,6 +21,14 @@ static bool do_inline;static void vcpu_insn_exec_before (unsigned int cpu_index,void *udata) { +static uint64_t last_pc; +uint64_t this_pc =GPOINTER_TO_UINT (udata); +if (this_pc == last_pc) { + g_autofree gchar *out = g_strdup_printf("detected repeat execution @ 0x%" + PRIx64"\n" , this_pc); +qemu_plugin_outs (out); + } + last_pc = this_pc; insn_count++; }
@@ -36,8 +44,10 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)qemu_plugin_register_vcpu_insn_exec_inline ( insn,QEMU_PLUGIN_INLINE_ADD_U64 , &insn_count,1 ); }else { +uint64_t vaddr =qemu_plugin_insn_vaddr (insn);qemu_plugin_register_vcpu_insn_exec_cb ( - insn, vcpu_insn_exec_before,QEMU_PLUGIN_CB_NO_REGS ,NULL ); + insn, vcpu_insn_exec_before,QEMU_PLUGIN_CB_NO_REGS , +GUINT_TO_POINTER (vaddr)); } } }
Signature · 1 line
2.20.1
Re: [PATCH v1 05/12] tests/plugin: expand insn test to detect duplicate instructions
Quoted 11 lines · Alex Bennée
A duplicate insn is one that is appears to be executed twice in a row. This is currently possible due to -icount and
cpu_io_recompile()causing a re-translation of a block. On it's own this won't trigger any tests though.Signed-off-by Alex Bennée <[email protected]>---
[AJB: well not quite, thex86_64test trips over this due to some weirdness in the way we handle rep insns, e.g. rep movsb (%esi), %es:(%edi) in the x86 bios code]
Ah, but that's not tcg weirdness, that's architectural weirdness. Multiple executions is how "rep" is supposed to work.
r~
Re: [PATCH v1 05/12] tests/plugin: expand insn test to detect duplicate instructions
Quoted 8 lines · Richard Henderson
Quoted 7 lines · Alex Bennée
A duplicate insn is one that is appears to be executed twice in a row. This is currently possible due to -icount and
cpu_io_recompile()causing a re-translation of a block. On it's own this won't trigger any tests though.Signed-off-by Alex Bennée <[email protected]>
Quoted 7 lines
Quoted 4 lines
---
[AJB: well not quite, thex86_64test trips over this due to some weirdness in the way we handle rep insns, e.g. rep movsb (%esi), %es:(%edi) in the x86 bios code]Ah, but that's not tcg weirdness, that's architectural weirdness. Multiple executions is how "rep" is supposed to work.
As the plugin can know the arch I can just disable the test for x86. At the moment it doesn't matter because there is only a test for aarch64.
Signature · 1 line
Alex Bennée
PATCH 6/12 tests/acceptance: add a new set of tests to exercise plugins
In reply to Alex BennéeThis is just a simple test to count the instructions executed by a kernel. However a later test will detect a failure condition when icount is enabled.
--- tests/acceptance/tcg_plugins.py | 103 ++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 tests/acceptance/tcg_plugins.py
tests/acceptance/tcg_plugins.py new file +103 -0
@@ -0,0 +1,103 @@ +# TCG Plugins tests +# +# These are a little more involved than the basic tests run by check-tcg. +# +# Copyright (c) 2021 Linaro +# +# Author: +# Alex Bennée <[email protected]> +# +# SPDX-License-Identifier: GPL-2.0-or-later + +import logging +import time +import tempfile +import mmap +import re + +from boot_linux_console import LinuxKernelTest + + +class PluginKernelBase(LinuxKernelTest): + """ + Boots a Linux kernel with a TCG plugin enabled. + """ + + timeout = 120 + KERNEL_COMMON_COMMAND_LINE = 'printk.time=1 panic=-1 ' + + def run_vm(self, kernel_path, kernel_command_line, + plugin, plugin_log, console_pattern, args): + + logger = logging.getLogger('plugin') + start_time = time.time() + vm = self.get_vm() + vm.set_console() + vm.add_args('-kernel', kernel_path, + '-append', kernel_command_line, + '-plugin', plugin, + '-d', 'plugin', + '-D', plugin_log, + '-net', 'none', + '-no-reboot') + if args: + vm.add_args(*args) + + try: + vm.launch() + except: + # fails if plugins not enabled + self.cancel("TCG Plugins not enabled") + + self.wait_for_console_pattern(console_pattern, vm) + elapsed = time.time() - start_time + logger.info('elapsed time %.2f sec' % elapsed) + # ensure logs are flushed + vm.shutdown() + return elapsed + + +class PluginKernelNormal(PluginKernelBase): + + def _grab_aarch64_kernel(self): + kernel_url = ('http://security.debian.org/' + 'debian-security/pool/updates/main/l/linux-signed-arm64/' + 'linux-image-4.19.0-12-arm64_4.19.152-1_arm64.deb') + kernel_sha1 = '2036c2792f80ac9c4ccaae742b2e0a28385b6010' + kernel_deb = self.fetch_asset(kernel_url, asset_hash=kernel_sha1) + kernel_path = self.extract_from_deb(kernel_deb, + "/boot/vmlinuz-4.19.0-12-arm64") + return kernel_path + + def test_aarch64_virt_insn(self): + """ + :avocado: tags=accel:tcg + :avocado: tags=arch:aarch64 + :avocado: tags=machine:virt + :avocado: tags=cpu:cortex-a57 + """ + kernel_path = self._grab_aarch64_kernel() + kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + + 'console=ttyAMA0') + console_pattern = 'Kernel panic - not syncing: VFS:' + + plugin_log = tempfile.NamedTemporaryFile(mode="r+t", prefix="plugin", + suffix=".log") + + self.run_vm(kernel_path, kernel_command_line, + "tests/plugin/libinsn.so", plugin_log.name, + console_pattern, + args=('-cpu', 'cortex-a53')) + + logger = logging.getLogger() + + with plugin_log as lf, \ + mmap.mmap(lf.fileno(), 0, access=mmap.ACCESS_READ) as s: + + m = re.search(br"insns: (?P<count>\d+)", s) + if "count" in m.groupdict(): + logger.debug("reported %d instructions", + int(m.group("count"))) + else: + logger.debug("Failed to find instruction count") + self.fail
Signature · 1 line
2.20.1
PATCH 7/12 accel/tcg: actually cache our partial icount TB
In reply to Alex BennéeWhen we exit a block under icount with instructions left to execute we might need a shorter than normal block to take us to the next deterministic event. Instead of creating a throwaway block on demand we use the existing compile flags mechanism to ensure we fetch (or compile and fetch) a block with exactly the number of instructions we need.
--- accel/tcg/cpu-exec.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
accel/tcg/cpu-exec.c +10 -8
@@ -730,16 +730,18 @@ static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb,/* Ensure global icount has gone forward */ icount_update (cpu);/* Refill decrementer and continue execution. */ - insns_left =MIN (0xffff , cpu->icount_budget); + insns_left =MIN (CF_COUNT_MASK , cpu->icount_budget);cpu_neg (cpu)->icount_decr.u16.low = insns_left; cpu->icount_extra = cpu->icount_budget - insns_left; -if (!cpu->icount_extra && insns_left < tb->icount) { -/* Execute any remaining instructions, then let the main loop -* handle the next event. -*/ -if (insns_left >0 ) { -cpu_exec_nocache (cpu, insns_left, tb, false); - } + +/* +* If the next tb has more instructions than we have left to +* execute we need to ensure we find/generate a TB with exactly +* insns_left instructions in it. +*/ +if (!cpu->icount_extra && insns_left >0 && insns_left < tb->icount) { +g_assert (insns_left <CF_COUNT_MASK ); + cpu->cflags_next_tb = (tb->cflags & ~CF_COUNT_MASK ) | insns_left; }#endif }
Signature · 1 line
2.20.1
Re: [PATCH v1 07/12] accel/tcg: actually cache our partial icount TB
Quoted 3 lines · Alex Bennée
/* Refill decrementer and continue execution. */accel/tcg/cpu-exec.c +1
-1- insns_left =MIN (0xffff , cpu->icount_budget); + insns_left =MIN (CF_COUNT_MASK , cpu->icount_budget);
...
Quoted 1 line
+
g_assert(insns_left < CF_COUNT_MASK);
Why both the MIN and the assert?
r~
Re: [PATCH v1 07/12] accel/tcg: actually cache our partial icount TB
Quoted 8 lines · Richard Henderson
Quoted 3 lines · Alex Bennée
/* Refill decrementer and continue execution. */accel/tcg/cpu-exec.c +1
-1- insns_left =MIN (0xffff , cpu->icount_budget); + insns_left =MIN (CF_COUNT_MASK , cpu->icount_budget);...
Quoted 1 line
+
g_assert(insns_left < CF_COUNT_MASK);Why both the MIN and the assert?
Lack of faith in MIN I guess ;-)
I'll drop the assert.
Quoted 3 lines
r~
Signature · 1 line
Alex Bennée
PATCH 8/12 accel/tcg: cache single instruction TB on pending replay exception
In reply to Alex BennéeAgain there is no reason to jump through the nocache hoops to execute a single instruction block. We do have to add an additional wrinkle to the cpu_handle_interrupt case to ensure we let through a TB where we have specifically disabled icount for the block.
As the last user of cpu_exec_nocache we can now remove the function. Further clean-up will follow in subsequent patches.
--- accel/tcg/cpu-exec.c | 44 ++++---------------------------------------- 1 file changed, 4 insertions(+), 40 deletions(-)
accel/tcg/cpu-exec.c +4 -40
@@ -224,40 +224,6 @@ cpu_tb_exec(CPUState *cpu, TranslationBlock *itb, int *tb_exit)return last_tb; } -#ifndef CONFIG_USER_ONLY -/* Execute the code without caching the generated code. An interpreter -could be used if available. */ -static void cpu_exec_nocache (CPUState *cpu,int max_cycles, -TranslationBlock *orig_tb,bool ignore_icount) -{ -TranslationBlock *tb; -uint32_t cflags =curr_cflags () |CF_NOCACHE ; -int tb_exit; - -if (ignore_icount) { - cflags &= ~CF_USE_ICOUNT ; - } - -/* Should never happen. -We only end up here when an existing TB is too long. */ - cflags |=MIN (max_cycles,CF_COUNT_MASK ); - -mmap_lock (); - tb =tb_gen_code (cpu, orig_tb->pc, orig_tb->cs_base, - orig_tb->flags, cflags); - tb->orig_tb = orig_tb; -mmap_unlock (); - -/* execute the generated code */ -trace_exec_tb_nocache (tb, tb->pc); -cpu_tb_exec (cpu, tb, &tb_exit); - -mmap_lock (); -tb_phys_invalidate (tb,-1 ); -mmap_unlock (); -tcg_tb_remove (tb); -} -#endif static void cpu_exec_enter (CPUState *cpu) {
@@ -524,15 +490,12 @@ static inline bool cpu_handle_exception(CPUState *cpu, int *ret)#ifndef CONFIG_USER_ONLY if (replay_has_exception () &&cpu_neg (cpu)->icount_decr.u16.low + cpu->icount_extra ==0 ) { -/* try to cause an exception pending in the log */ -cpu_exec_nocache (cpu,1 ,tb_find (cpu,NULL ,0 ,curr_cflags ()), true); +/* Execute just one insn to trigger exception pending in the log */ + cpu->cflags_next_tb = (curr_cflags () & ~CF_USE_ICOUNT ) |1 ; }#endif -if (cpu->exception_index <0 ) { -return false; - } +return false; } -if (cpu->exception_index >=EXCP_INTERRUPT ) {/* exit request from the cpu execution loop */ *ret = cpu->exception_index;
@@ -688,6 +651,7 @@ static inline bool cpu_handle_interrupt(CPUState *cpu,/* Finally, check if we need to exit to the main loop. */ if (unlikely (qatomic_read (&cpu->exit_request)) || (icount_enabled () + && (cpu->cflags_next_tb ==-1 || cpu->cflags_next_tb &CF_USE_ICOUNT ) &&cpu_neg (cpu)->icount_decr.u16.low + cpu->icount_extra ==0 )) {qatomic_set (&cpu->exit_request,0 );if (cpu->exception_index ==-1 ) {
Signature · 1 line
2.20.1
PATCH 9/12 accel/tcg: re-factor non-RAM execution code
In reply to Alex BennéeThere is no real need to use CF_NOCACHE here. As long as the TB isn't linked to other TBs or included in the QHT or jump cache then it will only get executed once.
--- accel/tcg/translate-all.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-)
accel/tcg/translate-all.c +14 -15
@@ -1778,7 +1778,7 @@ static inline void tb_page_add(PageDesc *p, TranslationBlock *tb,#endif } -/* add a new TB and link it to the physical page tables. phys_page2 is +/* Add a new TB and link it to the physical page tables. phys_page2 is * (-1) to indicate that only one page contains the TB. * * Called with mmap_lock held for user-mode emulation.
@@ -1797,17 +1797,6 @@ tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc,assert_memory_lock (); -if (phys_pc ==-1 ) { -/* -* If the TB is not associated with a physical RAM page then -* it must be a temporary one-insn TB, and we have nothing to do -* except fill in the page_addr[] fields. -*/ -assert (tb->cflags &CF_NOCACHE ); - tb->page_addr[0 ] = tb->page_addr[1 ] =-1 ; -return tb; - } -/* * Add the TB to the page list, acquiring first the pages's locks. * We keep the locks held until after inserting the TB in the hash table,
@@ -1880,9 +1869,8 @@ TranslationBlock *tb_gen_code(CPUState *cpu, phys_pc =get_page_addr_code (env, pc);if (phys_pc ==-1 ) { -/* Generate a temporary TB with 1 insn in it */ - cflags &= ~CF_COUNT_MASK ; - cflags |=CF_NOCACHE |1 ; +/* Generate a one-shot TB with 1 insn in it */ + cflags = (cflags & ~CF_COUNT_MASK ) |1 ; } cflags &= ~CF_CLUSTER_MASK ;
@@ -2096,6 +2084,17 @@ TranslationBlock *tb_gen_code(CPUState *cpu,tb_reset_jump (tb,1 ); } +/* +* If the TB is not associated with a physical RAM page then +* it must be a temporary one-insn TB, and we have nothing to do +* except fill in the page_addr[] fields. Return early before +* attempting to link to other TBs or add to the lookup table. +*/ +if (phys_pc ==-1 ) { + tb->page_addr[0 ] = tb->page_addr[1 ] =-1 ; +return tb; + } +/* check next page if needed */ virt_page2 = (pc + tb->size -1 ) &TARGET_PAGE_MASK ; phys_page2 =-1 ;
Signature · 1 line
2.20.1
PATCH 10/12 accel/tcg: remove CF_NOCACHE and special cases
In reply to Alex BennéeNow we no longer generate CF_NOCACHE blocks we can remove a bunch of the special case handling for them. While we are at it we can remove the unused tb->orig_tb field and save a few bytes on the TB structure.
--- include/exec/exec-all.h | 3 --- accel/tcg/translate-all.c | 51 ++++++++++++--------------------------- 2 files changed, 15 insertions(+), 39 deletions(-)
include/exec/exec-all.h +0 -3
@@ -454,7 +454,6 @@ struct TranslationBlock {uint32_t cflags;/* compile flags */ #define CF_COUNT_MASK 0x00007fff#define CF_LAST_IO 0x00008000/* Last insn may be an IO access. */ -#define CF_NOCACHE 0x00010000/* To be freed after execution */ #define CF_USE_ICOUNT 0x00020000#define CF_INVALID 0x00040000/* TB is stale. Set with @jmp_lock held */ #define CF_PARALLEL 0x00080000/* Generate code for a parallel context */
@@ -469,8 +468,6 @@ struct TranslationBlock {struct tb_tc tc; -/* original tb when cflags has CF_NOCACHE */ -struct TranslationBlock *orig_tb;/* first and second physical page containing code. The lower bit of the pointer tells the index in page_next[]. The list is protected by the TB's page('s) lock(s) */
accel/tcg/translate-all.c +15 -36
89 lines not on this page · raw message
Signature · 1 line
2.20.1
Re: [PATCH v1 10/12] accel/tcg: remove CF_NOCACHE and special cases
Quoted 9 lines · Alex Bennée
Now we no longer generate
CF_NOCACHEblocks we can remove a bunch of the special case handling for them. While we are at it we can remove the unusedtb->orig_tbfield and save a few bytes on the TB structure.Signed-off-by Alex Bennée <[email protected]>--- include/exec/exec-all.h | 3 --- accel/tcg/translate-all.c | 51 ++++++++++++--------------------------- 2 files changed, 15 insertions(+), 39 deletions(-)
r~
PATCH 11/12 accel/tcg: allow plugin instrumentation to be disable via cflags
In reply to Alex BennéeWhen icount is enabled and we recompile an MMIO access we end up double counting the instruction execution. To avoid this we introduce the CF_NOINSTR cflag which disables instrumentation for the next TB. As this is part of the hashed compile flags we will only execute the generated TB while coming out of a cpu_io_recompile.
While we are at it delete the old TODO. We might as well keep the translation handy as it's likely you will repeatedly hit it on each MMIO access.
--- include/exec/exec-all.h | 3 ++- accel/tcg/translate-all.c | 17 ++++++++--------- accel/tcg/translator.c | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-)
include/exec/exec-all.h +2 -1
@@ -454,6 +454,7 @@ struct TranslationBlock {uint32_t cflags;/* compile flags */ #define CF_COUNT_MASK 0x00007fff#define CF_LAST_IO 0x00008000/* Last insn may be an IO access. */ +#define CF_NOINSTR 0x00010000/* Disable instrumentation of TB */ #define CF_USE_ICOUNT 0x00020000#define CF_INVALID 0x00040000/* TB is stale. Set with @jmp_lock held */ #define CF_PARALLEL 0x00080000/* Generate code for a parallel context */
@@ -461,7 +462,7 @@ struct TranslationBlock {#define CF_CLUSTER_SHIFT 24/* cflags' mask for hashing/comparison */ #define CF_HASH_MASK \ - (CF_COUNT_MASK | CF_LAST_IO | CF_USE_ICOUNT | CF_PARALLEL | CF_CLUSTER_MASK) + (CF_COUNT_MASK | CF_LAST_IO | CF_NOINSTR | CF_USE_ICOUNT | CF_PARALLEL | CF_CLUSTER_MASK)/* Per-vCPU dynamic tracing state used to generate this TB */ uint32_t trace_vcpu_dstate;
accel/tcg/translate-all.c +8 -9
@@ -2398,7 +2398,8 @@ void tb_check_watchpoint(CPUState *cpu, uintptr_t retaddr) }#ifndef CONFIG_USER_ONLY -/* in deterministic execution mode, instructions doing device I/Os +/* +* In deterministic execution mode, instructions doing device I/Os * must be at the end of the TB. * * Called by softmmu_template.h, with iothread mutex not held.
@@ -2429,19 +2430,17 @@ void cpu_io_recompile(CPUState *cpu, uintptr_t retaddr) n =2 ; } -/* Generate a new TB executing the I/O insn. */ - cpu->cflags_next_tb =curr_cflags () |CF_LAST_IO | n; +/* +* Exit the loop and potentially generate a new TB executing the +* just the I/O insns. We also disable instrumentation so we don't +* double count the instruction. +*/ + cpu->cflags_next_tb =curr_cflags () |CF_NOINSTR |CF_LAST_IO | n;qemu_log_mask_and_addr (CPU_LOG_EXEC , tb->pc,"cpu_io_recompile: rewound execution of TB to " TARGET_FMT_lx"\n" , tb->pc); -/* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not -* the first in the TB) then we end up generating a whole new TB and -* repeating the fault, which is horribly inefficient. -* Better would be to execute just this insn uncached, or generate a -* second new TB. -*/ cpu_loop_exit_noexc (cpu); }
accel/tcg/translator.c +1 -1
@@ -58,7 +58,7 @@ void translator_loop(const TranslatorOps *ops, DisasContextBase *db, ops->tb_start (db, cpu);tcg_debug_assert (db->is_jmp ==DISAS_NEXT );/* no early exit */ - plugin_enabled =plugin_gen_tb_start (cpu, tb); + plugin_enabled = !(tb_cflags (db->tb) &CF_NOINSTR ) &&plugin_gen_tb_start (cpu, tb);while (true) { db->num_insns++;
Signature · 1 line
2.20.1
Re: [PATCH v1 11/12] accel/tcg: allow plugin instrumentation to be disable via cflags
Quoted 10 lines · Alex Bennée
When icount is enabled and we recompile an MMIO access we end up double counting the instruction execution. To avoid this we introduce the
CF_NOINSTRcflag which disables instrumentation for the next TB. As this is part of the hashed compile flags we will only execute the generated TB while coming out of acpu_io_recompile.While we are at it delete the old TODO. We might as well keep the translation handy as it's likely you will repeatedly hit it on each MMIO access.
Quoted 7 lines
Reported-by Aaron Lindsay <[email protected]>Signed-off-by Alex Bennée <[email protected]>--- include/exec/exec-all.h | 3 ++- accel/tcg/translate-all.c | 17 ++++++++--------- accel/tcg/translator.c | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-)
r~
PATCH 12/12 tests/acceptance: add a new tests to detect counting errors
In reply to Alex BennéeThe insn plugin has a simple heuristic to detect if an instruction is detected running twice in a row. Check the plugin log after the run and pass accordingly.
--- tests/acceptance/tcg_plugins.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
tests/acceptance/tcg_plugins.py +31 -0
@@ -101,3 +101,34 @@ class PluginKernelNormal(PluginKernelBase): else: logger.debug("Failed to find instruction count") self.fail + + def test_aarch64_virt_insn_icount(self): + """ + :avocado: tags=accel:tcg + :avocado: tags=arch:aarch64 + :avocado: tags=machine:virt + :avocado: tags=cpu:cortex-a57 + """ + kernel_path = self._grab_aarch64_kernel() + kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + + 'console=ttyAMA0') + console_pattern = 'Kernel panic - not syncing: VFS:' + + plugin_log = tempfile.NamedTemporaryFile(mode="r+t", prefix="plugin", + suffix=".log", delete=False) + + self.run_vm(kernel_path, kernel_command_line, + "tests/plugin/libinsn.so", plugin_log.name, + console_pattern, + args=('-cpu', 'cortex-a53', '-icount', 'shift=1')) + + logger = logging.getLogger() + + with plugin_log as lf, \ + mmap.mmap(lf.fileno(), 0, access=mmap.ACCESS_READ) as s: + m = re.search(br"detected repeat execution @ (?P<addr>0x[0-9A-Fa-f]+)", s) + if m is not None and "addr" in m.groupdict(): + logger.debug("detected repeat instructions") + self.fail("detected repeated instructions") + else: + logger.debug("no repeats detected: %s", m)
Signature · 1 line
2.20.1
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series seems to have some coding style problems. See output below for more information:
Type: series
Message-id: [email protected]
Subject: [PATCH v1 00/12] fix plugins double counting with mmio, cleanup CF_ flags
=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
- [tag update] patchew/[email protected] -> patchew/[email protected]
- [tag update] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
a9637ea tests/acceptance: add a new tests to detect counting errors fa2e5c6 accel/tcg: allow plugin instrumentation to be disable via cflags feba470 accel/tcg: remove CF_NOCACHE and special cases
efc2b45 accel/tcg: re-factor non-RAM execution code
8fa939a accel/tcg: cache single instruction TB on pending replay exception 3950a33 accel/tcg: actually cache our partial icount TB 999a79a tests/acceptance: add a new set of tests to exercise plugins cd71497 tests/plugin: expand insn test to detect duplicate instructions be5dad9 target/sh4: Create superh_io_recompile_replay_branch ee10c4e target/mips: Create mips_io_recompile_replay_branch 69ecbdf accel/tcg: Create io_recompile_replay_branch hook 827fd40 exec: Move TranslationBlock typedef to qemu/typedefs.h
=== OUTPUT BEGIN ===
1/12 Checking commit 827fd4086c80 (exec: Move TranslationBlock typedef to qemu/typedefs.h) 2/12 Checking commit 69ecbdf64517 (accel/tcg: Create io_recompile_replay_branch hook) 3/12 Checking commit ee10c4e8c405 (target/mips: Create mips_io_recompile_replay_branch) 4/12 Checking commit be5dad9f333f (target/sh4: Create superh_io_recompile_replay_branch) 5/12 Checking commit cd71497cb2a2 (tests/plugin: expand insn test to detect duplicate instructions)
WARNING: line over 80 characters
#30: FILE: tests/plugin/insn.c:27:
+ g_autofree gchar *out = g_strdup_printf("detected repeat execution @ 0x%"
total: 0 errors, 1 warnings, 25 lines checked
Patch 5/12 has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
6/12 Checking commit 999a79a49265 (tests/acceptance: add a new set of tests to exercise plugins)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
#18:
new file mode 100644
total: 0 errors, 1 warnings, 103 lines checked
Patch 6/12 has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
7/12 Checking commit 3950a3391d8c (accel/tcg: actually cache our partial icount TB) 8/12 Checking commit 8fa939a360c2 (accel/tcg: cache single instruction TB on pending replay exception)
WARNING: line over 80 characters
#89: FILE: accel/tcg/cpu-exec.c:654:
+ && (cpu->cflags_next_tb == -1 || cpu->cflags_next_tb & CF_USE_ICOUNT)
total: 0 errors, 1 warnings, 65 lines checked
Patch 8/12 has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
9/12 Checking commit efc2b45722f4 (accel/tcg: re-factor non-RAM execution code)
WARNING: Block comments use a leading /* on a separate line
#26: FILE: accel/tcg/translate-all.c:1781:
+/* Add a new TB and link it to the physical page tables. phys_page2 is
total: 0 errors, 1 warnings, 53 lines checked
Patch 9/12 has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
10/12 Checking commit feba47079ea4 (accel/tcg: remove CF_NOCACHE and special cases) 11/12 Checking commit fa2e5c6fe34b (accel/tcg: allow plugin instrumentation to be disable via cflags)
WARNING: line over 80 characters
#75: FILE: accel/tcg/translator.c:61:
+ plugin_enabled = !(tb_cflags(db->tb) & CF_NOINSTR) && plugin_gen_tb_start(cpu, tb);
ERROR: line over 90 characters
#96: FILE: include/exec/exec-all.h:465:
+ (CF_COUNT_MASK | CF_LAST_IO | CF_NOINSTR | CF_USE_ICOUNT | CF_PARALLEL | CF_CLUSTER_MASK)
total: 1 errors, 1 warnings, 57 lines checked
Patch 11/12 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
12/12 Checking commit a9637eabc2c8 (tests/acceptance: add a new tests to detect counting errors)
WARNING: line over 80 characters
#50: FILE: tests/acceptance/tcg_plugins.py:129:
+ m = re.search(br"detected repeat execution @ (?P<addr>0x[0-9A-Fa-f]+)", s)
total: 0 errors, 1 warnings, 34 lines checked
Patch 12/12 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===
Test command exited with code: 1
The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]