tcg: Direct block chaining clean-up
v4 cover tcg: Direct block chaining clean-up
From: Sergey Fedorov <[email protected]>
This series combines a set of patches which is meant to improve overall code structure and readability of the direct block chaining mechanism. The other point is to make a step towards thread safety of TB chainig.
The series' tree can be found in a public git repository [1].
[1] https://github.com/sergefdrv/qemu/tree/tb-chaining-cleanup-v4
Summary of changes:
Changes in v4:
* Removed assert from tb_add_jump() [PATCH v4 02/10]
* Added comment on TB stuff synchronization [PATCH v4 04/10]
* Documented tcg_gen_goto_tb() and moved its usage notes there
[PATCH v4 09/10] and [PATCH v4 10/10]
* Cc'ed usermode maintainers in commit message [PATCH v4 10/10]
Changes in v3:
* New patch to clean up safety checks [PATCH v3 09/10]
* New patch to eliminate unneeded checks in user-mode [PATCH v3 10/10]
Changes in v2:
* Eliminated duplicate dereference of 'ptb' in tb_jmp_remove() [PATCH v2 2/8]
* Tweaked a comment [PATCH v2 4/8]
* Complete rewrite [PATCH v2 5/8]
* Tweaked a comment; eliminated duplicate dereference of 'ptb' in
tb_jmp_unlink() [PATCH v2 8/8]Sergey Fedorov (10):
tcg: Clean up direct block chaining data fields
tcg: Use uintptr_t type for jmp_list_{next|first} fields of TB
tcg: Rearrange tb_link_page() to avoid forward declaration
tcg: Init TB's direct jumps before making it visible
tcg: Clarify thread safety check in tb_add_jump()
tcg: Rename tb_jmp_remove() to tb_remove_from_jmp_list()
tcg: Extract removing of jumps to TB from tb_phys_invalidate()
tcg: Clean up tb_jmp_unlink()
tcg: Clean up direct block chaining safety checks
tcg: Allow goto_tb to any target PC in user mode
cpu-exec.c | 7 +- include/exec/exec-all.h | 69 ++++++---- target-alpha/translate.c | 4 + target-arm/translate-a64.c | 2 + target-arm/translate.c | 17 ++- target-cris/translate.c | 16 ++- target-i386/translate.c | 23 ++-- target-lm32/translate.c | 21 ++- target-m68k/translate.c | 18 ++- target-microblaze/translate.c | 15 ++- target-mips/translate.c | 20 ++- target-moxie/translate.c | 21 ++- target-openrisc/translate.c | 20 ++- target-ppc/translate.c | 20 ++- target-s390x/translate.c | 17 ++- target-sh4/translate.c | 21 ++- target-sparc/translate.c | 24 +++- target-tricore/translate.c | 20 ++- target-unicore32/translate.c | 16 ++- target-xtensa/translate.c | 4 + tcg/aarch64/tcg-target.inc.c | 7 +- tcg/arm/tcg-target.inc.c | 8 +- tcg/i386/tcg-target.inc.c | 8 +- tcg/ia64/tcg-target.inc.c | 6 +- tcg/mips/tcg-target.inc.c | 8 +- tcg/ppc/tcg-target.inc.c | 6 +- tcg/s390/tcg-target.inc.c | 11 +- tcg/sparc/tcg-target.inc.c | 9 +- tcg/tcg-op.h | 13 ++ tcg/tcg.h | 6 +- tcg/tci/tcg-target.inc.c | 10 +- translate-all.c | 297 ++++++++++++++++++++++-------------------- 32 files changed, 470 insertions(+), 294 deletions(-)
Signature · 1 line
2.8.1
PATCH v4 1/10 tcg: Clean up direct block chaining data fields
From: Sergey Fedorov <[email protected]>
Briefly describe in a comment how direct block chaining is done. It should help in understanding of the following data fields.
Rename some fields in TranslationBlock and TCGContext structures to better reflect their purpose (dropping excessive 'tb_' prefix in TranslationBlock but keeping it in TCGContext):
tb_next_offset => jmp_reset_offset tb_jmp_offset => jmp_insn_offset tb_next => jmp_target_addr jmp_next => jmp_list_next jmp_first => jmp_list_first
Avoid using a magic constant as an invalid offset which is used to indicate that there's no n-th jump generated.
--- include/exec/exec-all.h | 44 ++++++++++++++++++++++++-------------- tcg/aarch64/tcg-target.inc.c | 7 +++--- tcg/arm/tcg-target.inc.c | 8 +++---- tcg/i386/tcg-target.inc.c | 8 +++---- tcg/ia64/tcg-target.inc.c | 6 +++--- tcg/mips/tcg-target.inc.c | 8 +++---- tcg/ppc/tcg-target.inc.c | 6 +++--- tcg/s390/tcg-target.inc.c | 11 +++++----- tcg/sparc/tcg-target.inc.c | 9 ++++---- tcg/tcg.h | 6 +++--- tcg/tci/tcg-target.inc.c | 10 ++++----- translate-all.c | 51 +++++++++++++++++++++++--------------------- 12 files changed, 96 insertions(+), 78 deletions(-)
include/exec/exec-all.h +28 -16
@@ -257,20 +257,32 @@ struct TranslationBlock {struct TranslationBlock *page_next[2 ];tb_page_addr_t page_addr[2 ]; -/* the following data are used to directly call another TB from -the code of this one. */ -uint16_t tb_next_offset[2 ];/* offset of original jump target */ +/* The following data are used to directly call another TB from +* the code of this one. This can be done either by emitting direct or +* indirect native jump instructions. These jumps are reset so that the TB +* just continue its execution. The TB can be linked to another one by +* setting one of the jump targets (or patching the jump instruction). Only +* two of such jumps are supported. +*/ +uint16_t jmp_reset_offset[2 ];/* offset of original jump target */ +#define TB_JMP_RESET_OFFSET_INVALID 0xffff/* indicates no jump generated */ #ifdef USE_DIRECT_JUMP -uint16_t tb_jmp_offset[2 ];/* offset of jump instruction */ +uint16_t jmp_insn_offset[2 ];/* offset of native jump instruction */ #else -uintptr_t tb_next[2 ];/* address of jump generated code */ +uintptr_t jmp_target_addr[2 ];/* target address for indirect jump */ #endif -/* list of TBs jumping to this one. This is a circular list using -the two least significant bits of the pointers to tell what is -the next pointer: 0 = jmp_next[0], 1 = jmp_next[1], 2 = -jmp_first */ -struct TranslationBlock *jmp_next[2 ]; -struct TranslationBlock *jmp_first; +/* Each TB has an assosiated circular list of TBs jumping to this one. +* jmp_list_first points to the first TB jumping to this one. +* jmp_list_next is used to point to the next TB in a list. +* Since each TB can have two jumps, it can participate in two lists. +* The two least significant bits of a pointer are used to choose which +* data field holds a pointer to the next TB: +* 0 => jmp_list_next[0], 1 => jmp_list_next[1], 2 => jmp_list_first. +* In other words, 0/1 tells which jump is used in the pointed TB, +* and 2 means that this is a pointer back to the target TB of this list. +*/ +struct TranslationBlock *jmp_list_next[2 ]; +struct TranslationBlock *jmp_list_first; };#include "qemu/thread.h"
@@ -359,7 +371,7 @@ void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr);static inline void tb_set_jmp_target (TranslationBlock *tb,int n,uintptr_t addr) { -uint16_t offset = tb->tb_jmp_offset[n]; +uint16_t offset = tb->jmp_insn_offset[n];tb_set_jmp_target1 ((uintptr_t )(tb->tc_ptr + offset), addr); }
@@ -369,7 +381,7 @@ static inline void tb_set_jmp_target(TranslationBlock *tb,static inline void tb_set_jmp_target (TranslationBlock *tb,int n,uintptr_t addr) { - tb->tb_next[n] = addr; + tb->jmp_target_addr[n] = addr; }#endif
@@ -378,7 +390,7 @@ static inline void tb_add_jump(TranslationBlock *tb, int n, TranslationBlock *tb_next) {/* NOTE: this test is only needed for thread safety */ -if (!tb->jmp_next[n]) { +if (!tb->jmp_list_next[n]) { qemu_log_mask_and_addr(CPU_LOG_EXEC, tb->pc, "Linking TBs %p [" TARGET_FMT_lx "] index %d -> %p [" TARGET_FMT_lx "]\n",
@@ -388,8 +400,8 @@ static inline void tb_add_jump(TranslationBlock *tb, int n,tb_set_jmp_target (tb, n, (uintptr_t )tb_next->tc_ptr);/* add in TB jmp circular list */ - tb->jmp_next[n] = tb_next->jmp_first; - tb_next->jmp_first = (TranslationBlock *)((uintptr_t )(tb) | (n)); + tb->jmp_list_next[n] = tb_next->jmp_list_first; + tb_next->jmp_list_first = (TranslationBlock *)((uintptr_t )tb | n); } }
tcg/aarch64/tcg-target.inc.c +4 -3
@@ -1294,12 +1294,13 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc,#ifndef USE_DIRECT_JUMP #error "USE_DIRECT_JUMP required for aarch64"#endif -assert (s->tb_jmp_offset !=NULL );/* consistency for USE_DIRECT_JUMP */ - s->tb_jmp_offset[a0] =tcg_current_code_size (s); +/* consistency for USE_DIRECT_JUMP */ +assert (s->tb_jmp_insn_offset !=NULL ); + s->tb_jmp_insn_offset[a0] =tcg_current_code_size (s);/* actual branch destination will be patched by aarch64_tb_set_jmp_target later, beware retranslation. */ tcg_out_goto_noaddr (s); - s->tb_next_offset[a0] =tcg_current_code_size (s); + s->tb_jmp_reset_offset[a0] =tcg_current_code_size (s);break ;case INDEX_op_br:
tcg/arm/tcg-target.inc.c +4 -4
@@ -1647,17 +1647,17 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,tcg_out_goto (s,COND_AL , tb_ret_addr);break ;case INDEX_op_goto_tb: -if (s->tb_jmp_offset) { +if (s->tb_jmp_insn_offset) {/* Direct jump method */ - s->tb_jmp_offset[args[0 ]] =tcg_current_code_size (s); + s->tb_jmp_insn_offset[args[0 ]] =tcg_current_code_size (s);tcg_out_b_noaddr (s,COND_AL ); }else {/* Indirect jump method */ -intptr_t ptr = (intptr_t )(s->tb_next + args[0 ]); +intptr_t ptr = (intptr_t )(s->tb_jmp_target_addr + args[0 ]);tcg_out_movi32 (s,COND_AL ,TCG_REG_R0 , ptr & ~0xfff );tcg_out_ld32_12 (s,COND_AL ,TCG_REG_PC ,TCG_REG_R0 , ptr &0xfff ); } - s->tb_next_offset[args[0 ]] =tcg_current_code_size (s); + s->tb_jmp_reset_offset[args[0 ]] =tcg_current_code_size (s);break ;case INDEX_op_br:tcg_out_goto_label (s,COND_AL ,arg_label (args[0 ]));
tcg/i386/tcg-target.inc.c +4 -4
@@ -1775,17 +1775,17 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,tcg_out_jmp (s, tb_ret_addr);break ;case INDEX_op_goto_tb: -if (s->tb_jmp_offset) { +if (s->tb_jmp_insn_offset) {/* direct jump method */ tcg_out8 (s, OPC_JMP_long);/* jmp im */ - s->tb_jmp_offset[args[0 ]] =tcg_current_code_size (s); + s->tb_jmp_insn_offset[args[0 ]] =tcg_current_code_size (s);tcg_out32 (s,0 ); }else {/* indirect jump method */ tcg_out_modrm_offset (s,OPC_GRP5 , EXT5_JMPN_Ev,-1 , - (intptr_t )(s->tb_next + args[0 ])); + (intptr_t )(s->tb_jmp_target_addr + args[0 ])); } - s->tb_next_offset[args[0 ]] =tcg_current_code_size (s); + s->tb_jmp_reset_offset[args[0 ]] =tcg_current_code_size (s);break ;case INDEX_op_br:tcg_out_jxx (s,JCC_JMP ,arg_label (args[0 ]),0 );
tcg/ia64/tcg-target.inc.c +3 -3
@@ -881,13 +881,13 @@ static void tcg_out_exit_tb(TCGContext *s, tcg_target_long arg)static inline void tcg_out_goto_tb (TCGContext *s,TCGArg arg) { -if (s->tb_jmp_offset) { +if (s->tb_jmp_insn_offset) {/* direct jump method */ tcg_abort (); }else {/* indirect jump method */ tcg_out_movi (s,TCG_TYPE_PTR ,TCG_REG_R2 , - (tcg_target_long)(s->tb_next + arg)); + (tcg_target_long)(s->tb_jmp_target_addr + arg)); tcg_out_bundle(s, MmI, tcg_opc_m1 (TCG_REG_P0, OPC_LD8_M1, TCG_REG_R2, TCG_REG_R2),
@@ -900,7 +900,7 @@ static inline void tcg_out_goto_tb(TCGContext *s, TCGArg arg)tcg_opc_b4 (TCG_REG_P0 ,OPC_BR_SPTK_MANY_B4 , TCG_REG_B6)); } - s->tb_next_offset[arg] =tcg_current_code_size (s); + s->tb_jmp_reset_offset[arg] =tcg_current_code_size (s); }static inline void tcg_out_jmp (TCGContext *s,TCGArg addr)
tcg/mips/tcg-target.inc.c +4 -4
@@ -1397,19 +1397,19 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc, }break ;case INDEX_op_goto_tb: -if (s->tb_jmp_offset) { +if (s->tb_jmp_insn_offset) {/* direct jump method */ - s->tb_jmp_offset[a0] =tcg_current_code_size (s); + s->tb_jmp_insn_offset[a0] =tcg_current_code_size (s);/* Avoid clobbering the address during retranslation. */ tcg_out32 (s,OPC_J | (*(uint32_t *)s->code_ptr &0x3ffffff )); }else {/* indirect jump method */ tcg_out_ld (s,TCG_TYPE_PTR ,TCG_TMP0 ,TCG_REG_ZERO , - (uintptr_t )(s->tb_next + a0)); + (uintptr_t )(s->tb_jmp_target_addr + a0));tcg_out_opc_reg (s,OPC_JR ,0 ,TCG_TMP0 ,0 ); }tcg_out_nop (s); - s->tb_next_offset[a0] =tcg_current_code_size (s); + s->tb_jmp_reset_offset[a0] =tcg_current_code_size (s);break ;case INDEX_op_br: tcg_out_brcond(s, TCG_COND_EQ, TCG_REG_ZERO, TCG_REG_ZERO,
tcg/ppc/tcg-target.inc.c +3 -3
@@ -1894,17 +1894,17 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,tcg_out_b (s,0 , tb_ret_addr);break ;case INDEX_op_goto_tb: -tcg_debug_assert (s->tb_jmp_offset); +tcg_debug_assert (s->tb_jmp_insn_offset);/* Direct jump. Ensure the next insns are 8-byte aligned. */ if ((uintptr_t )s->code_ptr &7 ) {tcg_out32 (s,NOP ); } - s->tb_jmp_offset[args[0 ]] =tcg_current_code_size (s); + s->tb_jmp_insn_offset[args[0 ]] =tcg_current_code_size (s);/* To be replaced by either a branch+nop or a load into TMP1. */ s->code_ptr +=2 ;tcg_out32 (s,MTSPR |RS (TCG_REG_TMP1 ) |CTR );tcg_out32 (s,BCCTR |BO_ALWAYS ); - s->tb_next_offset[args[0 ]] =tcg_current_code_size (s); + s->tb_jmp_reset_offset[args[0 ]] =tcg_current_code_size (s);break ;case INDEX_op_br: {
tcg/s390/tcg-target.inc.c +6 -5
@@ -1715,17 +1715,18 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,break ;case INDEX_op_goto_tb: -if (s->tb_jmp_offset) { +if (s->tb_jmp_insn_offset) {tcg_out16 (s,RIL_BRCL | (S390_CC_ALWAYS <<4 )); - s->tb_jmp_offset[args[0 ]] =tcg_current_code_size (s); + s->tb_jmp_insn_offset[args[0 ]] =tcg_current_code_size (s); s->code_ptr +=2 ; }else { -/* load address stored at s->tb_next + args[0] */ -tcg_out_ld_abs (s,TCG_TYPE_PTR ,TCG_TMP0 , s->tb_next + args[0 ]); +/* load address stored at s->tb_jmp_target_addr + args[0] */ +tcg_out_ld_abs (s,TCG_TYPE_PTR ,TCG_TMP0 , + s->tb_jmp_target_addr + args[0 ]);/* and go there */ tcg_out_insn (s,RR ,BCR ,S390_CC_ALWAYS ,TCG_TMP0 ); } - s->tb_next_offset[args[0 ]] =tcg_current_code_size (s); + s->tb_jmp_reset_offset[args[0 ]] =tcg_current_code_size (s);break ; OP_32_64(ld8u):
tcg/sparc/tcg-target.inc.c +5 -4
@@ -1229,18 +1229,19 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, }break ;case INDEX_op_goto_tb: -if (s->tb_jmp_offset) { +if (s->tb_jmp_insn_offset) {/* direct jump method */ - s->tb_jmp_offset[a0] =tcg_current_code_size (s); + s->tb_jmp_insn_offset[a0] =tcg_current_code_size (s);/* Make sure to preserve links during retranslation. */ tcg_out32 (s,CALL | (*s->code_ptr & ~INSN_OP (-1 ))); }else {/* indirect jump method */ -tcg_out_ld_ptr (s,TCG_REG_T1 , (uintptr_t )(s->tb_next + a0)); +tcg_out_ld_ptr (s,TCG_REG_T1 , + (uintptr_t )(s->tb_jmp_target_addr + a0));tcg_out_arithi (s,TCG_REG_G0 ,TCG_REG_T1 ,0 ,JMPL ); }tcg_out_nop (s); - s->tb_next_offset[a0] =tcg_current_code_size (s); + s->tb_jmp_reset_offset[a0] =tcg_current_code_size (s);break ;case INDEX_op_br:tcg_out_bpcc (s,COND_A ,BPCC_PT ,arg_label (a0));
tcg/tcg.h +3 -3
@@ -510,9 +510,9 @@ struct TCGContext {/* goto_tb support */ tcg_insn_unit *code_buf; -uintptr_t *tb_next; -uint16_t *tb_next_offset; -uint16_t *tb_jmp_offset;/* != NULL if USE_DIRECT_JUMP */ +uint16_t *tb_jmp_reset_offset;/* tb->jmp_reset_offset */ +uint16_t *tb_jmp_insn_offset;/* tb->jmp_insn_offset if USE_DIRECT_JUMP */ +uintptr_t *tb_jmp_target_addr;/* tb->jmp_target_addr if !USE_DIRECT_JUMP */ /* liveness analysis */ uint16_t *op_dead_args;/* for each operation, each bit tells if the
tcg/tci/tcg-target.inc.c +5 -5
@@ -553,17 +553,17 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,tcg_out64 (s, args[0 ]);break ;case INDEX_op_goto_tb: -if (s->tb_jmp_offset) { +if (s->tb_jmp_insn_offset) {/* Direct jump method. */ -assert (args[0 ] <ARRAY_SIZE (s->tb_jmp_offset)); - s->tb_jmp_offset[args[0 ]] =tcg_current_code_size (s); +assert (args[0 ] <ARRAY_SIZE (s->tb_jmp_insn_offset)); + s->tb_jmp_insn_offset[args[0 ]] =tcg_current_code_size (s);tcg_out32 (s,0 ); }else {/* Indirect jump method. */ TODO (); } -assert (args[0 ] <ARRAY_SIZE (s->tb_next_offset)); - s->tb_next_offset[args[0 ]] =tcg_current_code_size (s); +assert (args[0 ] <ARRAY_SIZE (s->tb_jmp_reset_offset)); + s->tb_jmp_reset_offset[args[0 ]] =tcg_current_code_size (s);break ;case INDEX_op_br:tci_out_label (s,arg_label (args[0 ]));
translate-all.c +27 -24
114 lines not on this page · raw message
Signature · 1 line
2.8.1
PATCH v4 2/10 tcg: Use uintptr_t type for jmp_list_{next|first} fields of TB
In reply to Sergey FedorovFrom: Sergey Fedorov <[email protected]>
These fields do not contain pure pointers to a TranslationBlock structure. So uintptr_t is the most appropriate type for them. Also put some asserts to assure that the two least significant bits of the pointer are always zero before assigning it to jmp_list_first.
---
Changes in v4:
* Removed assert from tb_add_jump()
* Minor commit message rewording
Changes in v2:
* Eliminated duplicate dereference of 'ptb' in tb_jmp_remove()
include/exec/exec-all.h | 12 +++++++----- translate-all.c | 38 ++++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 23 deletions(-)
include/exec/exec-all.h +7 -5
@@ -275,14 +275,16 @@ struct TranslationBlock {* jmp_list_first points to the first TB jumping to this one. * jmp_list_next is used to point to the next TB in a list. * Since each TB can have two jumps, it can participate in two lists. -* The two least significant bits of a pointer are used to choose which -* data field holds a pointer to the next TB: +* jmp_list_first and jmp_list_next are 4-byte aligned pointers to a +* TranslationBlock structure, but the two least significant bits of +* them are used to encode which data field of the pointed TB should +* be used to traverse the list further from that TB: * 0 => jmp_list_next[0], 1 => jmp_list_next[1], 2 => jmp_list_first. * In other words, 0/1 tells which jump is used in the pointed TB, * and 2 means that this is a pointer back to the target TB of this list. */ -struct TranslationBlock *jmp_list_next[2 ]; -struct TranslationBlock *jmp_list_first; +uintptr_t jmp_list_next[2 ]; +uintptr_t jmp_list_first; };#include "qemu/thread.h"
@@ -401,7 +403,7 @@ static inline void tb_add_jump(TranslationBlock *tb, int n,/* add in TB jmp circular list */ tb->jmp_list_next[n] = tb_next->jmp_list_first; - tb_next->jmp_list_first = (TranslationBlock *)((uintptr_t )tb | n); + tb_next->jmp_list_first = (uintptr_t )tb | n; } }
translate-all.c +20 -18
@@ -927,17 +927,17 @@ static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)static inline void tb_jmp_remove (TranslationBlock *tb,int n) { -TranslationBlock *tb1, **ptb; +TranslationBlock *tb1; +uintptr_t *ptb, ntb;unsigned int n1; ptb = &tb->jmp_list_next[n]; - tb1 = *ptb; -if (tb1) { +if (*ptb) {/* find tb(n) in circular list */ for (;;) { - tb1 = *ptb; - n1 = (uintptr_t )tb1 &3 ; - tb1 = (TranslationBlock *)((uintptr_t )tb1 & ~3 ); + ntb = *ptb; + n1 = ntb &3 ; + tb1 = (TranslationBlock *)(ntb & ~3 );if (n1 == n && tb1 == tb) {break ; }
@@ -950,7 +950,7 @@ static inline void tb_jmp_remove(TranslationBlock *tb, int n)/* now we can suppress tb(n) from the list */ *ptb = tb->jmp_list_next[n]; - tb->jmp_list_next[n] =NULL ; + tb->jmp_list_next[n] = (uintptr_t )NULL ; } }
@@ -969,7 +969,7 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)PageDesc *p;unsigned int h, n1;tb_page_addr_t phys_pc; -TranslationBlock *tb1, *tb2; +uintptr_t tb1, tb2;/* remove the TB from the hash list */ phys_pc = tb->page_addr[0 ] + (tb->pc & ~TARGET_PAGE_MASK );
@@ -1005,19 +1005,20 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)/* suppress any remaining jumps to this TB */ tb1 = tb->jmp_list_first;for (;;) { - n1 = (uintptr_t )tb1 &3 ; +TranslationBlock *tmp_tb; + n1 = tb1 &3 ;if (n1 ==2 ) {break ; } - tb1 = (TranslationBlock *)((uintptr_t )tb1 & ~3 ); - tb2 = tb1->jmp_list_next[n1]; -tb_reset_jump (tb1, n1); - tb1->jmp_list_next[n1] =NULL ; + tmp_tb = (TranslationBlock *)(tb1 & ~3 ); + tb2 = tmp_tb->jmp_list_next[n1]; +tb_reset_jump (tmp_tb, n1); + tmp_tb->jmp_list_next[n1] = (uintptr_t )NULL ; tb1 = tb2; } -/* fail safe */ - tb->jmp_list_first = (TranslationBlock *)((uintptr_t )tb |2 ); +assert (((uintptr_t )tb &3 ) ==0 ); + tb->jmp_list_first = (uintptr_t )tb |2 ;/* fail safe */ tcg_ctx.tb_ctx.tb_phys_invalidate_count++; }
@@ -1491,9 +1492,10 @@ static void tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc, tb->page_addr[1 ] =-1 ; } - tb->jmp_list_first = (TranslationBlock *)((uintptr_t )tb |2 ); - tb->jmp_list_next[0 ] =NULL ; - tb->jmp_list_next[1 ] =NULL ; +assert (((uintptr_t )tb &3 ) ==0 ); + tb->jmp_list_first = (uintptr_t )tb |2 ; + tb->jmp_list_next[0 ] = (uintptr_t )NULL ; + tb->jmp_list_next[1 ] = (uintptr_t )NULL ;/* init original jump addresses */ if (tb->jmp_reset_offset[0 ] !=TB_JMP_RESET_OFFSET_INVALID ) {
Signature · 1 line
2.8.1
PATCH v4 3/10 tcg: Rearrange tb_link_page() to avoid forward declaration
In reply to Sergey FedorovFrom: Sergey Fedorov <[email protected]>
--- translate-all.c | 204 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 101 insertions(+), 103 deletions(-)
translate-all.c +101 -103
@@ -153,8 +153,6 @@ void tb_lock_reset(void)#endif } -static void tb_link_page (TranslationBlock *tb,tb_page_addr_t phys_pc, -tb_page_addr_t phys_page2);static TranslationBlock *tb_find_pc (uintptr_t tc_ptr);void cpu_gen_init (void )
@@ -1052,6 +1050,107 @@ static void build_page_bitmap(PageDesc *p) } } +/* add the tb in the target page and protect it if necessary +* +* Called with mmap_lock held for user-mode emulation. +*/ +static inline void tb_alloc_page (TranslationBlock *tb, +unsigned int n,tb_page_addr_t page_addr) +{ +PageDesc *p; +#ifndef CONFIG_USER_ONLY +bool page_already_protected; +#endif + + tb->page_addr[n] = page_addr; + p =page_find_alloc (page_addr >>TARGET_PAGE_BITS ,1 ); + tb->page_next[n] = p->first_tb; +#ifndef CONFIG_USER_ONLY + page_already_protected = p->first_tb !=NULL ; +#endif + p->first_tb = (TranslationBlock *)((uintptr_t )tb | n); +invalidate_page_bitmap (p); + +#if defined(CONFIG_USER_ONLY ) +if (p->flags &PAGE_WRITE ) { +target_ulong addr; +PageDesc *p2; +int prot; + +/* force the host page as non writable (writes will have a +page fault + mprotect overhead) */ + page_addr &= qemu_host_page_mask; + prot =0 ; +for (addr = page_addr; addr < page_addr + qemu_host_page_size; + addr +=TARGET_PAGE_SIZE ) { + + p2 =page_find (addr >>TARGET_PAGE_BITS ); +if (!p2) { +continue ; + } + prot |= p2->flags; + p2->flags &= ~PAGE_WRITE ; + } +mprotect (g2h (page_addr), qemu_host_page_size, + (prot &PAGE_BITS ) & ~PAGE_WRITE ); +#ifdef DEBUG_TB_INVALIDATE +printf ("protecting code page: 0x" TARGET_FMT_lx"\n" , + page_addr); +#endif + } +#else +/* if some code is already present, then the pages are already +protected. So we handle the case where only the first TB is +allocated in a physical page */ +if (!page_already_protected) { +tlb_protect_code (page_addr); + } +#endif +} + +/* 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. +*/ +static void tb_link_page (TranslationBlock *tb,tb_page_addr_t phys_pc, +tb_page_addr_t phys_page2) +{ +unsigned int h; +TranslationBlock **ptb; + +/* add in the physical hash table */ + h =tb_phys_hash_func (phys_pc); + ptb = &tcg_ctx.tb_ctx.tb_phys_hash[h]; + tb->phys_hash_next = *ptb; + *ptb = tb; + +/* add in the page list */ +tb_alloc_page (tb,0 , phys_pc &TARGET_PAGE_MASK ); +if (phys_page2 !=-1 ) { +tb_alloc_page (tb,1 , phys_page2); + }else { + tb->page_addr[1 ] =-1 ; + } + +assert (((uintptr_t )tb &3 ) ==0 ); + tb->jmp_list_first = (uintptr_t )tb |2 ; + tb->jmp_list_next[0 ] = (uintptr_t )NULL ; + tb->jmp_list_next[1 ] = (uintptr_t )NULL ; + +/* init original jump addresses */ +if (tb->jmp_reset_offset[0 ] !=TB_JMP_RESET_OFFSET_INVALID ) { +tb_reset_jump (tb,0 ); + } +if (tb->jmp_reset_offset[1 ] !=TB_JMP_RESET_OFFSET_INVALID ) { +tb_reset_jump (tb,1 ); + } + +#ifdef DEBUG_TB_CHECK +tb_page_check (); +#endif +} +/* Called with mmap_lock held for user mode emulation. */ TranslationBlock *tb_gen_code(CPUState *cpu, target_ulong pc, target_ulong cs_base,
@@ -1409,107 +1508,6 @@ static void tb_invalidate_phys_page(tb_page_addr_t addr, }#endif -/* add the tb in the target page and protect it if necessary -* -* Called with mmap_lock held for user-mode emulation. -*/ -static inline void tb_alloc_page (TranslationBlock *tb, -unsigned int n,tb_page_addr_t page_addr) -{ -PageDesc *p; -#ifndef CONFIG_USER_ONLY -bool page_already_protected; -#endif - - tb->page_addr[n] = page_addr; - p =page_find_alloc (page_addr >>TARGET_PAGE_BITS ,1 ); - tb->page_next[n] = p->first_tb; -#ifndef CONFIG_USER_ONLY - page_already_protected = p->first_tb !=NULL ; -#endif - p->first_tb = (TranslationBlock *)((uintptr_t )tb | n); -invalidate_page_bitmap (p); - -#if defined(CONFIG_USER_ONLY ) -if (p->flags &PAGE_WRITE ) { -target_ulong addr; -PageDesc *p2; -int prot; - -/* force the host page as non writable (writes will have a -page fault + mprotect overhead) */ - page_addr &= qemu_host_page_mask; - prot =0 ; -for (addr = page_addr; addr < page_addr + qemu_host_page_size; - addr +=TARGET_PAGE_SIZE ) { - - p2 =page_find (addr >>TARGET_PAGE_BITS ); -if (!p2) { -continue ; - } - prot |= p2->flags; - p2->flags &= ~PAGE_WRITE ; - } -mprotect (g2h (page_addr), qemu_host_page_size, - (prot &PAGE_BITS ) & ~PAGE_WRITE ); -#ifdef DEBUG_TB_INVALIDATE -printf ("protecting code page: 0x" TARGET_FMT_lx"\n" , - page_addr); -#endif - } -#else -/* if some code is already present, then the pages are already -protected. So we handle the case where only the first TB is -allocated in a physical page */ -if (!page_already_protected) { -tlb_protect_code (page_addr); - } -#endif -} - -/* 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. -*/ -static void tb_link_page (TranslationBlock *tb,tb_page_addr_t phys_pc, -tb_page_addr_t phys_page2) -{ -unsigned int h; -TranslationBlock **ptb; - -/* add in the physical hash table */ - h =tb_phys_hash_func (phys_pc); - ptb = &tcg_ctx.tb_ctx.tb_phys_hash[h]; - tb->phys_hash_next = *ptb; - *ptb = tb; - -/* add in the page list */ -tb_alloc_page (tb,0 , phys_pc &TARGET_PAGE_MASK ); -if (phys_page2 !=-1 ) { -tb_alloc_page (tb,1 , phys_page2); - }else { - tb->page_addr[1 ] =-1 ; - } - -assert (((uintptr_t )tb &3 ) ==0 ); - tb->jmp_list_first = (uintptr_t )tb |2 ; - tb->jmp_list_next[0 ] = (uintptr_t )NULL ; - tb->jmp_list_next[1 ] = (uintptr_t )NULL ; - -/* init original jump addresses */ -if (tb->jmp_reset_offset[0 ] !=TB_JMP_RESET_OFFSET_INVALID ) { -tb_reset_jump (tb,0 ); - } -if (tb->jmp_reset_offset[1 ] !=TB_JMP_RESET_OFFSET_INVALID ) { -tb_reset_jump (tb,1 ); - } - -#ifdef DEBUG_TB_CHECK -tb_page_check (); -#endif -} -/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr < tb[1].tc_ptr. Return NULL if not found */ static TranslationBlock *tb_find_pc (uintptr_t tc_ptr)
Signature · 1 line
2.8.1
PATCH v4 4/10 tcg: Init TB's direct jumps before making it visible
In reply to Sergey FedorovFrom: Sergey Fedorov <[email protected]>
Initialize TB's direct jump list data fields and reset the jumps before tb_link_page() puts it into the physical hash table and the physical page list. So TB is completely initialized before it becomes visible.
This is pure rearrangement of code to a more suitable place, though it could be a preparation for relaxing the locking scheme in future.
---
Changes in v4:
* Added comment on TB stuff synchronization
* Expanded commit message
Changes in v2:
* Tweaked a comment
translate-all.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-)
translate-all.c +19 -13
@@ -1133,19 +1133,6 @@ static void tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc, tb->page_addr[1 ] =-1 ; } -assert (((uintptr_t )tb &3 ) ==0 ); - tb->jmp_list_first = (uintptr_t )tb |2 ; - tb->jmp_list_next[0 ] = (uintptr_t )NULL ; - tb->jmp_list_next[1 ] = (uintptr_t )NULL ; - -/* init original jump addresses */ -if (tb->jmp_reset_offset[0 ] !=TB_JMP_RESET_OFFSET_INVALID ) { -tb_reset_jump (tb,0 ); - } -if (tb->jmp_reset_offset[1 ] !=TB_JMP_RESET_OFFSET_INVALID ) { -tb_reset_jump (tb,1 ); - } -#ifdef DEBUG_TB_CHECK tb_page_check ();#endif
@@ -1254,12 +1241,31 @@ TranslationBlock *tb_gen_code(CPUState *cpu,ROUND_UP ((uintptr_t )gen_code_buf + gen_code_size + search_size,CODE_GEN_ALIGN ); +/* init jump list */ +assert (((uintptr_t )tb &3 ) ==0 ); + tb->jmp_list_first = (uintptr_t )tb |2 ; + tb->jmp_list_next[0 ] = (uintptr_t )NULL ; + tb->jmp_list_next[1 ] = (uintptr_t )NULL ; + +/* init original jump addresses wich has been set during tcg_gen_code() */ +if (tb->jmp_reset_offset[0 ] !=TB_JMP_RESET_OFFSET_INVALID ) { +tb_reset_jump (tb,0 ); + } +if (tb->jmp_reset_offset[1 ] !=TB_JMP_RESET_OFFSET_INVALID ) { +tb_reset_jump (tb,1 ); + } +/* check next page if needed */ virt_page2 = (pc + tb->size -1 ) &TARGET_PAGE_MASK ; phys_page2 =-1 ;if ((pc &TARGET_PAGE_MASK ) != virt_page2) { phys_page2 =get_page_addr_code (env, virt_page2); } +/* As long as consistency of the TB stuff is provided by tb_lock in user +* mode and is implicit in single-threaded softmmu emulation, no explicit +* memory barrier is required before tb_link_page() makes the TB visible +* through the physical hash table and physical page list. +*/ tb_link_page (tb, phys_pc, phys_page2);return tb; }
Signature · 1 line
2.8.1
PATCH v4 5/10 tcg: Clarify thread safety check in tb_add_jump()
In reply to Sergey FedorovFrom: Sergey Fedorov <[email protected]>
The check is to make sure that another thread hasn't already done the same while we were outside of tb_lock. Mention this in a comment.
---
Changes in v2:
* Typo fixed in the commit title
* Complete rewrite of the commit body and the patch based on Paolo's comments
include/exec/exec-all.h | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-)
include/exec/exec-all.h +16 -13
@@ -391,20 +391,23 @@ static inline void tb_set_jmp_target(TranslationBlock *tb,static inline void tb_add_jump (TranslationBlock *tb,int n,TranslationBlock *tb_next) { -/* NOTE: this test is only needed for thread safety */ -if (!tb->jmp_list_next[n]) { -qemu_log_mask_and_addr (CPU_LOG_EXEC , tb->pc, -"Linking TBs %p [" TARGET_FMT_lx -"] index %d -> %p [" TARGET_FMT_lx"]\n" , - tb->tc_ptr, tb->pc, n, - tb_next->tc_ptr, tb_next->pc); -/* patch the native jump address */ -tb_set_jmp_target (tb, n, (uintptr_t )tb_next->tc_ptr); - -/* add in TB jmp circular list */ - tb->jmp_list_next[n] = tb_next->jmp_list_first; - tb_next->jmp_list_first = (uintptr_t )tb | n; +if (tb->jmp_list_next[n]) { +/* Another thread has already done this while we were +* outside of the lock; nothing to do in this case */ +return ; } +qemu_log_mask_and_addr (CPU_LOG_EXEC , tb->pc, +"Linking TBs %p [" TARGET_FMT_lx +"] index %d -> %p [" TARGET_FMT_lx"]\n" , + tb->tc_ptr, tb->pc, n, + tb_next->tc_ptr, tb_next->pc); + +/* patch the native jump address */ +tb_set_jmp_target (tb, n, (uintptr_t )tb_next->tc_ptr); + +/* add in TB jmp circular list */ + tb->jmp_list_next[n] = tb_next->jmp_list_first; + tb_next->jmp_list_first = (uintptr_t )tb | n; }/* GETRA is the true target of the return instruction that we'll execute,
Signature · 1 line
2.8.1
PATCH v4 6/10 tcg: Rename tb_jmp_remove() to tb_remove_from_jmp_list()
In reply to Sergey FedorovFrom: Sergey Fedorov <[email protected]>
tb_jmp_remove() was only used to remove the TB from a list of all TBs jumping to the same TB which is n-th jump destination of the given TB. Put a comment briefly describing the function behavior and rename it to better reflect its purpose.
--- translate-all.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
translate-all.c +4 -3
@@ -923,7 +923,8 @@ static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb) } } -static inline void tb_jmp_remove (TranslationBlock *tb,int n) +/* remove the TB from a list of TBs jumping to the n-th jump target of the TB */ +static inline void tb_remove_from_jmp_list (TranslationBlock *tb,int n) {TranslationBlock *tb1;uintptr_t *ptb, ntb;
@@ -997,8 +998,8 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) }/* suppress this TB from the two jump lists */ -tb_jmp_remove (tb,0 ); -tb_jmp_remove (tb,1 ); +tb_remove_from_jmp_list (tb,0 ); +tb_remove_from_jmp_list (tb,1 );/* suppress any remaining jumps to this TB */ tb1 = tb->jmp_list_first;
Signature · 1 line
2.8.1
PATCH v4 7/10 tcg: Extract removing of jumps to TB from tb_phys_invalidate()
In reply to Sergey FedorovFrom: Sergey Fedorov <[email protected]>
Move the code for removing jumps to a TB out of tb_phys_invalidate() to a separate static inline function tb_jmp_unlink(). This simplifies tb_phys_invalidate() and improves code structure.
--- translate-all.c | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-)
translate-all.c +26 -18
@@ -961,14 +961,37 @@ static inline void tb_reset_jump(TranslationBlock *tb, int n)tb_set_jmp_target (tb, n, addr); } +/* remove any jumps to the TB */ +static inline void tb_jmp_unlink (TranslationBlock *tb) +{ +uintptr_t tb1, tb2; +unsigned int n1; + + tb1 = tb->jmp_list_first; +for (;;) { +TranslationBlock *tmp_tb; + n1 = tb1 &3 ; +if (n1 ==2 ) { +break ; + } + tmp_tb = (TranslationBlock *)(tb1 & ~3 ); + tb2 = tmp_tb->jmp_list_next[n1]; +tb_reset_jump (tmp_tb, n1); + tmp_tb->jmp_list_next[n1] = (uintptr_t )NULL ; + tb1 = tb2; + } + +assert (((uintptr_t )tb &3 ) ==0 ); + tb->jmp_list_first = (uintptr_t )tb |2 ;/* fail safe */ +} +/* invalidate one TB */ void tb_phys_invalidate (TranslationBlock *tb,tb_page_addr_t page_addr) {CPUState *cpu;PageDesc *p; -unsigned int h, n1; +unsigned int h;tb_page_addr_t phys_pc; -uintptr_t tb1, tb2;/* remove the TB from the hash list */ phys_pc = tb->page_addr[0 ] + (tb->pc & ~TARGET_PAGE_MASK );
@@ -1002,22 +1025,7 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)tb_remove_from_jmp_list (tb,1 );/* suppress any remaining jumps to this TB */ - tb1 = tb->jmp_list_first; -for (;;) { -TranslationBlock *tmp_tb; - n1 = tb1 &3 ; -if (n1 ==2 ) { -break ; - } - tmp_tb = (TranslationBlock *)(tb1 & ~3 ); - tb2 = tmp_tb->jmp_list_next[n1]; -tb_reset_jump (tmp_tb, n1); - tmp_tb->jmp_list_next[n1] = (uintptr_t )NULL ; - tb1 = tb2; - } - -assert (((uintptr_t )tb &3 ) ==0 ); - tb->jmp_list_first = (uintptr_t )tb |2 ;/* fail safe */ +tb_jmp_unlink (tb); tcg_ctx.tb_ctx.tb_phys_invalidate_count++; }
Signature · 1 line
2.8.1
PATCH v4 8/10 tcg: Clean up tb_jmp_unlink()
In reply to Sergey FedorovFrom: Sergey Fedorov <[email protected]>
Unify the code of this function with tb_jmp_remove_from_list(). Making these functions similar improves their readability. Also this could be a step towards making this function thread-safe.
---
Changes in v2:
* Tweaked a comment to better describe jmp_list_{next|first} usage
* Eliminated duplicate dereference of 'ptb' in tb_jmp_unlink()
translate-all.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-)
translate-all.c +9 -12
@@ -964,25 +964,22 @@ static inline void tb_reset_jump(TranslationBlock *tb, int n)/* remove any jumps to the TB */ static inline void tb_jmp_unlink (TranslationBlock *tb) { -uintptr_t tb1, tb2; +TranslationBlock *tb1; +uintptr_t *ptb, ntb;unsigned int n1; - tb1 = tb->jmp_list_first; + ptb = &tb->jmp_list_first;for (;;) { -TranslationBlock *tmp_tb; - n1 = tb1 &3 ; + ntb = *ptb; + n1 = ntb &3 ; + tb1 = (TranslationBlock *)(ntb & ~3 );if (n1 ==2 ) {break ; } - tmp_tb = (TranslationBlock *)(tb1 & ~3 ); - tb2 = tmp_tb->jmp_list_next[n1]; -tb_reset_jump (tmp_tb, n1); - tmp_tb->jmp_list_next[n1] = (uintptr_t )NULL ; - tb1 = tb2; +tb_reset_jump (tb1, n1); + *ptb = tb1->jmp_list_next[n1]; + tb1->jmp_list_next[n1] = (uintptr_t )NULL ; } - -assert (((uintptr_t )tb &3 ) ==0 ); - tb->jmp_list_first = (uintptr_t )tb |2 ;/* fail safe */ }/* invalidate one TB */
Signature · 1 line
2.8.1
PATCH v4 9/10 tcg: Clean up direct block chaining safety checks
In reply to Sergey FedorovFrom: Sergey Fedorov <[email protected]>
We don't take care of direct jumps when address mapping changes. Thus we must be sure to generate direct jumps so that they always keep valid even if address mapping changes. Luckily, we can only allow to execute a TB if it was generated from the pages which match with current mapping.
Document tcg_gen_goto_tb() declaration and note the reason for destination PC limitations.
Some targets with variable length instructions allow TB to straddle a page boundary. However, we make sure that both of TB pages match the current address mapping when looking up TBs. So it is safe to do direct jumps into the both pages. Correct the checks for some of those targets.
Given that, we can safely patch a TB which spans two pages. Remove the unnecessary check in cpu_exec() and allow such TBs to be patched.
---
Changes in v4:
* Documented tcg_gen_goto_tb() declaration
* Destination PC check explanatory comments moved to tcg_gen_goto_tb() declaration comment
* Updated commit message
cpu-exec.c | 7 ++----- target-arm/translate.c | 3 ++- target-cris/translate.c | 4 +++- target-i386/translate.c | 2 +- target-m68k/translate.c | 2 +- target-s390x/translate.c | 2 +- tcg/tcg-op.h | 10 ++++++++++ 7 files changed, 20 insertions(+), 10 deletions(-)
cpu-exec.c +2 -5
@@ -508,11 +508,8 @@ int cpu_exec(CPUState *cpu) next_tb =0 ; tcg_ctx.tb_ctx.tb_invalidated_flag =0 ; } -/* see if we can patch the calling TB. When the TB -spans two pages, we cannot safely do a direct -jump. */ -if (next_tb !=0 && tb->page_addr[1 ] ==-1 - && !qemu_loglevel_mask (CPU_LOG_TB_NOCHAIN )) { +/* See if we can patch the calling TB. */ +if (next_tb !=0 && !qemu_loglevel_mask (CPU_LOG_TB_NOCHAIN )) {tb_add_jump ((TranslationBlock *)(next_tb & ~TB_EXIT_MASK ), next_tb &TB_EXIT_MASK , tb); }
target-arm/translate.c +2 -1
@@ -4054,7 +4054,8 @@ static inline void gen_goto_tb(DisasContext *s, int n, target_ulong dest)TranslationBlock *tb; tb = s->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { +if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || + ((s->pc -1 ) &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) {tcg_gen_goto_tb (n);gen_set_pc_im (s, dest);tcg_gen_exit_tb ((uintptr_t )tb + n);
target-cris/translate.c +3 -1
@@ -524,7 +524,9 @@ static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest) {TranslationBlock *tb; tb = dc->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { + +if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || + (dc->ppc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) {tcg_gen_goto_tb (n);tcg_gen_movi_tl (env_pc, dest);tcg_gen_exit_tb ((uintptr_t )tb + n);
target-i386/translate.c +1 -1
@@ -2094,7 +2094,7 @@ static inline void gen_goto_tb(DisasContext *s, int tb_num, target_ulong eip) tb = s->tb;/* NOTE: we handle the case where the TB spans two pages here */ if ((pc &TARGET_PAGE_MASK ) == (tb->pc &TARGET_PAGE_MASK ) || - (pc &TARGET_PAGE_MASK ) == ((s->pc -1 ) &TARGET_PAGE_MASK )) { + (pc &TARGET_PAGE_MASK ) == (s->pc_start &TARGET_PAGE_MASK )) {/* jump to same page: we can use a direct jump */ tcg_gen_goto_tb (tb_num);gen_jmp_im (eip);
target-m68k/translate.c +1 -1
@@ -861,7 +861,7 @@ static void gen_jmp_tb(DisasContext *s, int n, uint32_t dest)if (unlikely (s->singlestep_enabled)) {gen_exception (s, dest,EXCP_DEBUG ); }else if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || - (s->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { + (s->insn_pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) {tcg_gen_goto_tb (n);tcg_gen_movi_i32 (QREG_PC , dest);tcg_gen_exit_tb ((uintptr_t )tb + n);
target-s390x/translate.c +1 -1
@@ -610,7 +610,7 @@ static int use_goto_tb(DisasContext *s, uint64_t dest) {/* NOTE: we handle the case where the TB spans two pages here */ return (((dest &TARGET_PAGE_MASK ) == (s->tb->pc &TARGET_PAGE_MASK ) - || (dest &TARGET_PAGE_MASK ) == ((s->pc -1 ) &TARGET_PAGE_MASK )) + || (dest &TARGET_PAGE_MASK ) == (s->pc &TARGET_PAGE_MASK )) && !s->singlestep_enabled && !(s->tb->cflags &CF_LAST_IO ) && !(s->tb->flags &FLAG_MASK_PER ));
tcg/tcg-op.h +10 -0
@@ -753,6 +753,16 @@ static inline void tcg_gen_exit_tb(uintptr_t val)tcg_gen_op1i (INDEX_op_exit_tb, val); } +/** +* tcg_gen_goto_tb() - output goto_tb TCG operation +* @idx: Direct jump slot index (0 or 1) +* +* See tcg/README for more info about this TCG operation. +* +* NOTE: Direct jumps with goto_tb are only safe within the pages this TB +* resides in because we don't take care of direct jumps when address mapping +* changes, e.g. in tlb_flush(). +*/ void tcg_gen_goto_tb (unsigned idx);#if TARGET_LONG_BITS ==32
Signature · 1 line
2.8.1
Re: [PATCH v4 09/10] tcg: Clean up direct block chaining safety checks
Quoted 43 lines · Sergey Fedorov
From: Sergey Fedorov <[email protected]>
We don't take care of direct jumps when address mapping changes. Thus we must be sure to generate direct jumps so that they always keep valid even if address mapping changes. Luckily, we can only allow to execute a TB if it was generated from the pages which match with current mapping.
Document
tcg_gen_goto_tb()declaration and note the reason for destination PC limitations.Some targets with variable length instructions allow TB to straddle a page boundary. However, we make sure that both of TB pages match the current address mapping when looking up TBs. So it is safe to do direct jumps into the both pages. Correct the checks for some of those targets.
Given that, we can safely patch a TB which spans two pages. Remove the unnecessary check in
cpu_exec()and allow such TBs to be patched.Signed-off-by Sergey Fedorov <[email protected]>Signed-off-by Sergey Fedorov <[email protected]>---
Changes in v4:
* Documentedtcg_gen_goto_tb()declaration
* Destination PC check explanatory comments moved totcg_gen_goto_tb()declaration comment
* Updated commit messagecpu-exec.c | 7 ++----- target-arm/translate.c | 3 ++- target-cris/translate.c | 4 +++- target-i386/translate.c | 2 +- target-m68k/translate.c | 2 +- target-s390x/translate.c | 2 +- tcg/tcg-op.h | 10 ++++++++++ 7 files changed, 20 insertions(+), 10 deletions(-)cpu-exec.c +0
-0@@ -508,11 +508,8 @@ int cpu_exec(CPUState *cpu) next_tb =0 ;
cpu-exec.c int cpu_exec(CPUState *cpu) · line 513
Quoted 10 lines
cpu-exec.c +2
-5tcg_ctx.tb_ctx.tb_invalidated_flag =0 ; } -/* see if we can patch the calling TB. When the TB -spans two pages, we cannot safely do a direct -jump. */ -if (next_tb !=0 && tb->page_addr[1 ] ==-1 - && !qemu_loglevel_mask (CPU_LOG_TB_NOCHAIN )) { +/* See if we can patch the calling TB. */ +if (next_tb !=0 && !qemu_loglevel_mask (CPU_LOG_TB_NOCHAIN )) {
A pointer to the definitive comment helps ;-)
/* See if we can patch the calling TB, see tcg_gen_goto_tb */
Quoted 82 lines
cpu-exec.c +0
-0tb_add_jump ((TranslationBlock *)(next_tb & ~TB_EXIT_MASK ), next_tb &TB_EXIT_MASK , tb);}target-arm/translate.c +2
-1@@ -4054,7 +4054,8 @@ static inline void gen_goto_tb(DisasContext *s, int n, target_ulong dest)TranslationBlock *tb; tb = s->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { +if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || + ((s->pc -1 ) &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) {tcg_gen_goto_tb (n);gen_set_pc_im (s, dest);tcg_gen_exit_tb ((uintptr_t )tb + n);target-cris/translate.c +3
-1@@ -524,7 +524,9 @@ static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest) {TranslationBlock *tb; tb = dc->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { + +if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || + (dc->ppc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) {tcg_gen_goto_tb (n);tcg_gen_movi_tl (env_pc, dest);tcg_gen_exit_tb ((uintptr_t )tb + n);target-i386/translate.c +1
-1@@ -2094,7 +2094,7 @@ static inline void gen_goto_tb(DisasContext *s, int tb_num, target_ulong eip) tb = s->tb;/* NOTE: we handle the case where the TB spans two pages here */ if ((pc &TARGET_PAGE_MASK ) == (tb->pc &TARGET_PAGE_MASK ) || - (pc &TARGET_PAGE_MASK ) == ((s->pc -1 ) &TARGET_PAGE_MASK )) { + (pc &TARGET_PAGE_MASK ) == (s->pc_start &TARGET_PAGE_MASK )) {/* jump to same page: we can use a direct jump */ tcg_gen_goto_tb (tb_num);gen_jmp_im (eip);target-m68k/translate.c +1
-1@@ -861,7 +861,7 @@ static void gen_jmp_tb(DisasContext *s, int n, uint32_t dest)if (unlikely (s->singlestep_enabled)) {gen_exception (s, dest,EXCP_DEBUG ); }else if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || - (s->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { + (s->insn_pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) {tcg_gen_goto_tb (n);tcg_gen_movi_i32 (QREG_PC , dest);tcg_gen_exit_tb ((uintptr_t )tb + n);target-s390x/translate.c +1
-1@@ -610,7 +610,7 @@ static int use_goto_tb(DisasContext *s, uint64_t dest) {/* NOTE: we handle the case where the TB spans two pages here */ return (((dest &TARGET_PAGE_MASK ) == (s->tb->pc &TARGET_PAGE_MASK ) - || (dest &TARGET_PAGE_MASK ) == ((s->pc -1 ) &TARGET_PAGE_MASK )) + || (dest &TARGET_PAGE_MASK ) == (s->pc &TARGET_PAGE_MASK )) && !s->singlestep_enabled && !(s->tb->cflags &CF_LAST_IO ) && !(s->tb->flags &FLAG_MASK_PER ));tcg/tcg-op.h +3
-0@@ -753,6 +753,16 @@ static inline void tcg_gen_exit_tb(uintptr_t val)tcg_gen_op1i (INDEX_op_exit_tb, val); } +/** +* tcg_gen_goto_tb() - output goto_tb TCG operation +* @idx: Direct jump slot index (0 or 1)
tcg/tcg-op.h static inline void tcg_gen_exit_tb(uintptr_t val) · line 768
Quoted 10 lines
tcg/tcg-op.h +7
-0+* +* See tcg/README for more info about this TCG operation. +* +* NOTE: Direct jumps with goto_tb are only safe within the pages this TB +* resides in because we don't take care of direct jumps when address mapping +* changes, e.g. in tlb_flush(). +*/ void tcg_gen_goto_tb (unsigned idx);#if TARGET_LONG_BITS ==32
--
Alex Bennée
Re: [PATCH v4 09/10] tcg: Clean up direct block chaining safety checks
Quoted 19 lines · Alex Bennée
Quoted 6 lines · Sergey Fedorov
cpu-exec.c +0
-0@@ -508,11 +508,8 @@ int cpu_exec(CPUState *cpu) next_tb =0 ;cpu-exec.c int cpu_exec(CPUState *cpu) · line 513
Quoted 10 lines
cpu-exec.c +2
-5tcg_ctx.tb_ctx.tb_invalidated_flag =0 ; } -/* see if we can patch the calling TB. When the TB -spans two pages, we cannot safely do a direct -jump. */ -if (next_tb !=0 && tb->page_addr[1 ] ==-1 - && !qemu_loglevel_mask (CPU_LOG_TB_NOCHAIN )) { +/* See if we can patch the calling TB. */ +if (next_tb !=0 && !qemu_loglevel_mask (CPU_LOG_TB_NOCHAIN )) {A pointer to the definitive comment helps ;-)
Quoted 1 line
/* See if we can patch the calling TB, see tcg_gen_goto_tb */
I'm not so sure that the comment for tcg_gen_goto_tb() would be of much use here. Actually, what we check here is if we know the calling TB (what is called 'next_tb' here so far) and if logging settings don't forbid us to chain TBs. The note in the comment for tcg_gen_goto_tb() is all about when goto_tb TCG ops can be emitted by the target translation code, not so relevant here, I suppose.
Kind regards,
Sergey
Quoted 4 lines
Quoted 4 lines
cpu-exec.c +0
-0tb_add_jump ((TranslationBlock *)(next_tb & ~TB_EXIT_MASK ), next_tb &TB_EXIT_MASK , tb);}
(snip)
Quoted 21 lines
Quoted 21 lines
tcg/tcg-op.h +10
-0@@ -753,6 +753,16 @@ static inline void tcg_gen_exit_tb(uintptr_t val)tcg_gen_op1i (INDEX_op_exit_tb, val); } +/** +* tcg_gen_goto_tb() - output goto_tb TCG operation +* @idx: Direct jump slot index (0 or 1) +* +* See tcg/README for more info about this TCG operation. +* +* NOTE: Direct jumps with goto_tb are only safe within the pages this TB +* resides in because we don't take care of direct jumps when address mapping +* changes, e.g. in tlb_flush(). +*/ void tcg_gen_goto_tb (unsigned idx);#if TARGET_LONG_BITS ==32
Re: [PATCH v4 09/10] tcg: Clean up direct block chaining safety checks
Quoted 20 lines · Sergey Fedorov
Quoted 19 lines · Alex Bennée
Quoted 6 lines · Sergey Fedorov
cpu-exec.c +0
-0@@ -508,11 +508,8 @@ int cpu_exec(CPUState *cpu) next_tb =0 ;cpu-exec.c int cpu_exec(CPUState *cpu) · line 513
Quoted 10 lines
cpu-exec.c +2
-5tcg_ctx.tb_ctx.tb_invalidated_flag =0 ; } -/* see if we can patch the calling TB. When the TB -spans two pages, we cannot safely do a direct -jump. */ -if (next_tb !=0 && tb->page_addr[1 ] ==-1 - && !qemu_loglevel_mask (CPU_LOG_TB_NOCHAIN )) { +/* See if we can patch the calling TB. */ +if (next_tb !=0 && !qemu_loglevel_mask (CPU_LOG_TB_NOCHAIN )) {A pointer to the definitive comment helps ;-)
Quoted 8 lines
Quoted 1 line
/* See if we can patch the calling TB, see tcg_gen_goto_tb */I'm not so sure that the comment for
tcg_gen_goto_tb()would be of much use here. Actually, what we check here is if we know the calling TB (what is called 'next_tb' here so far) and if logging settings don't forbid us to chain TBs. The note in the comment fortcg_gen_goto_tb()is all about whengoto_tbTCG ops can be emitted by the target translation code, not so relevant here, I suppose.
True, it makes more sense on the following patches. It's not a major thing.
Quoted 29 lines
Kind regards,
SergeyQuoted 4 lines
Quoted 4 lines
cpu-exec.c +0
-0tb_add_jump ((TranslationBlock *)(next_tb & ~TB_EXIT_MASK ), next_tb &TB_EXIT_MASK , tb);}(snip)
Quoted 20 lines
Quoted 20 lines
tcg/tcg-op.h +10
-0@@ -753,6 +753,16 @@ static inline void tcg_gen_exit_tb(uintptr_t val)tcg_gen_op1i (INDEX_op_exit_tb, val); } +/** +* tcg_gen_goto_tb() - output goto_tb TCG operation +* @idx: Direct jump slot index (0 or 1) +* +* See tcg/README for more info about this TCG operation. +* +* NOTE: Direct jumps with goto_tb are only safe within the pages this TB +* resides in because we don't take care of direct jumps when address mapping +* changes, e.g. in tlb_flush(). +*/ void tcg_gen_goto_tb (unsigned idx);
Quoted 1 line
Quoted 1 line
tcg/tcg-op.h static inline void tcg_gen_exit_tb(uintptr_t val) · line 768
Quoted 1 line
tcg/tcg-op.h +0
-0#if TARGET_LONG_BITS ==32
--
Alex Bennée
PATCH v4 10/10 tcg: Allow goto_tb to any target PC in user mode
In reply to Sergey FedorovFrom: Sergey Fedorov <[email protected]>
In user mode, there's only a static address translation, TBs are always invalidated properly and direct jumps are reset when mapping change. Thus the destination address is always valid for direct jumps and there's no need to restrict it to the pages the TB resides in.
---
Changes in v4:
* Explanatory comments moved to tcg_gen_goto_tb() declaration comment
* Cc'ed usermode maintainers in commit message excplicitly
target-alpha/translate.c | 4 ++++ target-arm/translate-a64.c | 2 ++ target-arm/translate.c | 18 ++++++++++++------ target-cris/translate.c | 18 ++++++++++++------ target-i386/translate.c | 23 ++++++++++++++--------- target-lm32/translate.c | 21 +++++++++++++++------ target-m68k/translate.c | 18 ++++++++++++------ target-microblaze/translate.c | 15 +++++++++++---- target-mips/translate.c | 20 +++++++++++++++----- target-moxie/translate.c | 21 +++++++++++++++------ target-openrisc/translate.c | 20 +++++++++++++++----- target-ppc/translate.c | 20 +++++++++++++++----- target-s390x/translate.c | 17 +++++++++++------ target-sh4/translate.c | 21 +++++++++++++++------ target-sparc/translate.c | 24 +++++++++++++++++------- target-tricore/translate.c | 20 +++++++++++++++----- target-unicore32/translate.c | 16 +++++++++++----- target-xtensa/translate.c | 4 ++++ tcg/tcg-op.h | 9 ++++++--- 19 files changed, 221 insertions(+), 90 deletions(-)
target-alpha/translate.c +4 -0
@@ -464,8 +464,12 @@ static bool use_goto_tb(DisasContext *ctx, uint64_t dest)if (in_superpage (ctx, dest)) {return true; } +#ifndef CONFIG_USER_ONLY /* Check for the dest on the same page as the start of the TB. */ return ((ctx->tb->pc ^ dest) &TARGET_PAGE_MASK ) ==0 ; +#else +return true; +#endif }static ExitStatus gen_bdirect (DisasContext *ctx,int ra,int32_t disp)
target-arm/translate-a64.c +2 -0
@@ -274,10 +274,12 @@ static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)return false; } +#ifndef CONFIG_USER_ONLY /* Only link tbs from inside the same guest page */ if ((s->tb->pc &TARGET_PAGE_MASK ) != (dest &TARGET_PAGE_MASK )) {return false; } +#endif return true; }
target-arm/translate.c +12 -6
@@ -4049,16 +4049,22 @@ static int disas_vfp_insn(DisasContext *s, uint32_t insn)return 0 ; } -static inline void gen_goto_tb (DisasContext *s,int n,target_ulong dest) +static inline bool use_goto_tb (DisasContext *s,target_ulong dest) { -TranslationBlock *tb; +#ifndef CONFIG_USER_ONLY +return (s->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || + ((s->pc -1 ) &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} - tb = s->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || - ((s->pc -1 ) &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { +static inline void gen_goto_tb (DisasContext *s,int n,target_ulong dest) +{ +if (use_goto_tb (s, dest)) {tcg_gen_goto_tb (n);gen_set_pc_im (s, dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )s->tb + n); }else {gen_set_pc_im (s, dest);tcg_gen_exit_tb (0 );
target-cris/translate.c +12 -6
@@ -520,16 +520,22 @@ static void t_gen_cc_jmp(TCGv pc_true, TCGv pc_false)gen_set_label (l1); } -static void gen_goto_tb (DisasContext *dc,int n,target_ulong dest) +static inline bool use_goto_tb (DisasContext *dc,target_ulong dest) { -TranslationBlock *tb; - tb = dc->tb; +#ifndef CONFIG_USER_ONLY +return (dc->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || + (dc->ppc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || - (dc->ppc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { +static void gen_goto_tb (DisasContext *dc,int n,target_ulong dest) +{ +if (use_goto_tb (dc, dest)) {tcg_gen_goto_tb (n);tcg_gen_movi_tl (env_pc, dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )dc->tb + n); }else {tcg_gen_movi_tl (env_pc, dest);tcg_gen_exit_tb (0 );
target-i386/translate.c +14 -9
@@ -2085,20 +2085,25 @@ static inline int insn_const_size(TCGMemOp ot) } } +static inline bool use_goto_tb (DisasContext *s,target_ulong pc) +{ +#ifndef CONFIG_USER_ONLY +return (pc &TARGET_PAGE_MASK ) == (s->tb->pc &TARGET_PAGE_MASK ) || + (pc &TARGET_PAGE_MASK ) == (s->pc_start &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +static inline void gen_goto_tb (DisasContext *s,int tb_num,target_ulong eip) { -TranslationBlock *tb; -target_ulong pc; - - pc = s->cs_base + eip; - tb = s->tb; -/* NOTE: we handle the case where the TB spans two pages here */ -if ((pc &TARGET_PAGE_MASK ) == (tb->pc &TARGET_PAGE_MASK ) || - (pc &TARGET_PAGE_MASK ) == (s->pc_start &TARGET_PAGE_MASK )) { +target_ulong pc = s->cs_base + eip; + +if (use_goto_tb (s, pc)) {/* jump to same page: we can use a direct jump */ tcg_gen_goto_tb (tb_num);gen_jmp_im (eip); -tcg_gen_exit_tb ((uintptr_t )tb + tb_num); +tcg_gen_exit_tb ((uintptr_t )s->tb + tb_num); }else {/* jump to another page: currently not optimized */ gen_jmp_im (eip);
target-lm32/translate.c +15 -6
@@ -133,16 +133,25 @@ static inline void t_gen_illegal_insn(DisasContext *dc)gen_helper_ill (cpu_env); } -static void gen_goto_tb (DisasContext *dc,int n,target_ulong dest) +static inline bool use_goto_tb (DisasContext *dc,target_ulong dest) { -TranslationBlock *tb; +if (unlikely (dc->singlestep_enabled)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (dc->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} - tb = dc->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) && -likely (!dc->singlestep_enabled)) { +static void gen_goto_tb (DisasContext *dc,int n,target_ulong dest) +{ +if (use_goto_tb (dc, dest)) {tcg_gen_goto_tb (n);tcg_gen_movi_tl (cpu_pc, dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )dc->tb + n); }else {tcg_gen_movi_tl (cpu_pc, dest);if (dc->singlestep_enabled) {
target-m68k/translate.c +12 -6
@@ -852,19 +852,25 @@ static inline void gen_addr_fault(DisasContext *s) } \ }while (0 ) +static inline bool use_goto_tb(DisasContext *s, uint32_t dest) +{ +#ifndef CONFIG_USER_ONLY +return (s->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || + (s->insn_pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +/* Generate a jump to an immediate address. */ static void gen_jmp_tb (DisasContext *s,int n,uint32_t dest) { -TranslationBlock *tb; - - tb = s->tb;if (unlikely (s->singlestep_enabled)) {gen_exception (s, dest,EXCP_DEBUG ); - }else if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || - (s->insn_pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { + }else if (use_goto_tb (s, dest)) {tcg_gen_goto_tb (n);tcg_gen_movi_i32 (QREG_PC , dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )s->tb + n); }else {gen_jmp_im (s, dest);tcg_gen_exit_tb (0 );
target-microblaze/translate.c +11 -4
@@ -124,14 +124,21 @@ static inline void t_gen_raise_exception(DisasContext *dc, uint32_t index) dc->is_jmp =DISAS_UPDATE ; } +static inline bool use_goto_tb (DisasContext *dc,target_ulong dest) +{ +#ifndef CONFIG_USER_ONLY +return (dc->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +static void gen_goto_tb (DisasContext *dc,int n,target_ulong dest) { -TranslationBlock *tb; - tb = dc->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { +if (use_goto_tb (dc, dest)) {tcg_gen_goto_tb (n);tcg_gen_movi_tl (cpu_SR[SR_PC ], dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )dc->tb + n); }else {tcg_gen_movi_tl (cpu_SR[SR_PC ], dest);tcg_gen_exit_tb (0 );
target-mips/translate.c +15 -5
@@ -4191,15 +4191,25 @@ static void gen_trap (DisasContext *ctx, uint32_t opc,tcg_temp_free (t1); } +static inline bool use_goto_tb (DisasContext *ctx,target_ulong dest) +{ +if (unlikely (ctx->singlestep_enabled)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (ctx->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +static inline void gen_goto_tb (DisasContext *ctx,int n,target_ulong dest) { -TranslationBlock *tb; - tb = ctx->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) && -likely (!ctx->singlestep_enabled)) { +if (use_goto_tb (ctx, dest)) {tcg_gen_goto_tb (n);gen_save_pc (dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )ctx->tb + n); }else {gen_save_pc (dest);if (ctx->singlestep_enabled) {
target-moxie/translate.c +15 -6
@@ -121,17 +121,26 @@ void moxie_translate_init(void) done_init =1 ; } +static inline bool use_goto_tb (DisasContext *ctx,target_ulong dest) +{ +if (unlikely (ctx->singlestep_enabled)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (ctx->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +static inline void gen_goto_tb (CPUMoxieState *env,DisasContext *ctx,int n,target_ulong dest) { -TranslationBlock *tb; - tb = ctx->tb; - -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) && - !ctx->singlestep_enabled) { +if (use_goto_tb (ctx, dest)) {tcg_gen_goto_tb (n);tcg_gen_movi_i32 (cpu_pc, dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )ctx->tb + n); }else {tcg_gen_movi_i32 (cpu_pc, dest);if (ctx->singlestep_enabled) {
target-openrisc/translate.c +15 -5
@@ -190,15 +190,25 @@ static void check_ov64s(DisasContext *dc)} #endif*/ +static inline bool use_goto_tb (DisasContext *dc,target_ulong dest) +{ +if (unlikely (dc->singlestep_enabled)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (dc->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +static void gen_goto_tb (DisasContext *dc,int n,target_ulong dest) { -TranslationBlock *tb; - tb = dc->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) && -likely (!dc->singlestep_enabled)) { +if (use_goto_tb (dc, dest)) {tcg_gen_movi_tl (cpu_pc, dest);tcg_gen_goto_tb (n); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )dc->tb + n); }else {tcg_gen_movi_tl (cpu_pc, dest);if (dc->singlestep_enabled) {
target-ppc/translate.c +15 -5
@@ -3822,19 +3822,29 @@ static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)#endif } +static inline bool use_goto_tb (DisasContext *ctx,target_ulong dest) +{ +if (unlikely (ctx->singlestep_enabled)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (ctx->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +/*** Branch ***/ static inline void gen_goto_tb (DisasContext *ctx,int n,target_ulong dest) { -TranslationBlock *tb; - tb = ctx->tb;if (NARROW_MODE (ctx)) { dest = (uint32_t ) dest; } -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) && -likely (!ctx->singlestep_enabled)) { +if (use_goto_tb (ctx, dest)) {tcg_gen_goto_tb (n);tcg_gen_movi_tl (cpu_nip, dest & ~3 ); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )ctx->tb + n); }else {tcg_gen_movi_tl (cpu_nip, dest & ~3 );if (unlikely (ctx->singlestep_enabled)) {
target-s390x/translate.c +11 -6
@@ -608,12 +608,17 @@ static void gen_op_calc_cc(DisasContext *s)static int use_goto_tb (DisasContext *s,uint64_t dest) { -/* NOTE: we handle the case where the TB spans two pages here */ -return (((dest &TARGET_PAGE_MASK ) == (s->tb->pc &TARGET_PAGE_MASK ) - || (dest &TARGET_PAGE_MASK ) == (s->pc &TARGET_PAGE_MASK )) - && !s->singlestep_enabled - && !(s->tb->cflags &CF_LAST_IO ) - && !(s->tb->flags &FLAG_MASK_PER )); +if (unlikely (s->singlestep_enabled) || + (s->tb->cflags &CF_LAST_IO ) || + (s->tb->flags &FLAG_MASK_PER )) { +return false; + } +#ifndef CONFIG_USER_ONLY +return (dest &TARGET_PAGE_MASK ) == (s->tb->pc &TARGET_PAGE_MASK ) || + (dest &TARGET_PAGE_MASK ) == (s->pc &TARGET_PAGE_MASK ); +#else +return true; +#endif }static void account_noninline_branch (DisasContext *s,int cc_op)
target-sh4/translate.c +15 -6
@@ -205,17 +205,26 @@ static void gen_write_sr(TCGv src)tcg_gen_andi_i32 (cpu_sr_t, cpu_sr_t,1 ); } -static void gen_goto_tb (DisasContext * ctx,int n,target_ulong dest) +static inline bool use_goto_tb (DisasContext *ctx,target_ulong dest) { -TranslationBlock *tb; - tb = ctx->tb; +if (unlikely (ctx->singlestep_enabled)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (ctx->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) && - !ctx->singlestep_enabled) { +static void gen_goto_tb (DisasContext *ctx,int n,target_ulong dest) +{ +if (use_goto_tb (ctx, dest)) {/* Use a direct jump if in same page and singlestep not enabled */ tcg_gen_goto_tb (n);tcg_gen_movi_i32 (cpu_pc, dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )ctx->tb + n); }else {tcg_gen_movi_i32 (cpu_pc, dest);if (ctx->singlestep_enabled)
target-sparc/translate.c +17 -7
@@ -303,20 +303,30 @@ static inline TCGv gen_dest_gpr(DisasContext *dc, int reg) } } +static inline bool use_goto_tb (DisasContext *s,target_ulong pc, +target_ulong npc) +{ +if (unlikely (s->singlestep)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (pc &TARGET_PAGE_MASK ) == (s->tb->pc &TARGET_PAGE_MASK ) && + (npc &TARGET_PAGE_MASK ) == (s->tb->pc &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +static inline void gen_goto_tb (DisasContext *s,int tb_num,target_ulong pc,target_ulong npc) { -TranslationBlock *tb; - - tb = s->tb; -if ((pc &TARGET_PAGE_MASK ) == (tb->pc &TARGET_PAGE_MASK ) && - (npc &TARGET_PAGE_MASK ) == (tb->pc &TARGET_PAGE_MASK ) && - !s->singlestep) { +if (use_goto_tb (s, pc, npc)) {/* jump to same page: we can use a direct jump */ tcg_gen_goto_tb (tb_num);tcg_gen_movi_tl (cpu_pc, pc);tcg_gen_movi_tl (cpu_npc, npc); -tcg_gen_exit_tb ((uintptr_t )tb + tb_num); +tcg_gen_exit_tb ((uintptr_t )s->tb + tb_num); }else {/* jump to another page: currently not optimized */ tcg_gen_movi_tl (cpu_pc, pc);
target-tricore/translate.c +15 -5
@@ -3236,15 +3236,25 @@ static inline void gen_save_pc(target_ulong pc)tcg_gen_movi_tl (cpu_PC, pc); } +static inline bool use_goto_tb (DisasContext *ctx,target_ulong dest) +{ +if (unlikely (ctx->singlestep_enabled)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (ctx->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +static inline void gen_goto_tb (DisasContext *ctx,int n,target_ulong dest) { -TranslationBlock *tb; - tb = ctx->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) && -likely (!ctx->singlestep_enabled)) { +if (use_goto_tb (ctx, dest)) {tcg_gen_goto_tb (n);gen_save_pc (dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )ctx->tb + n); }else {gen_save_pc (dest);if (ctx->singlestep_enabled) {
target-unicore32/translate.c +11 -5
@@ -1089,15 +1089,21 @@ static void disas_ucf64_insn(CPUUniCore32State *env, DisasContext *s, uint32_t i } } -static inline void gen_goto_tb (DisasContext *s,int n,uint32_t dest) +static inline bool use_goto_tb (DisasContext *s,uint32_t dest) { -TranslationBlock *tb; +#ifndef CONFIG_USER_ONLY +return (s->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} - tb = s->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { +static inline void gen_goto_tb (DisasContext *s,int n,uint32_t dest) +{ +if (use_goto_tb (s, dest)) {tcg_gen_goto_tb (n);gen_set_pc_im (dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )s->tb + n); }else {gen_set_pc_im (dest);tcg_gen_exit_tb (0 );
target-xtensa/translate.c +4 -0
@@ -418,9 +418,11 @@ static void gen_jump(DisasContext *dc, TCGv dest)static void gen_jumpi (DisasContext *dc,uint32_t dest,int slot) {TCGv_i32 tmp =tcg_const_i32 (dest); +#ifndef CONFIG_USER_ONLY if (((dc->tb->pc ^ dest) &TARGET_PAGE_MASK ) !=0 ) { slot =-1 ; } +#endif gen_jump_slot (dc, tmp, slot);tcg_temp_free (tmp); }
@@ -446,9 +448,11 @@ static void gen_callw(DisasContext *dc, int callinc, TCGv_i32 dest)static void gen_callwi (DisasContext *dc,int callinc,uint32_t dest,int slot) {TCGv_i32 tmp =tcg_const_i32 (dest); +#ifndef CONFIG_USER_ONLY if (((dc->tb->pc ^ dest) &TARGET_PAGE_MASK ) !=0 ) { slot =-1 ; } +#endif gen_callw_slot (dc, callinc, tmp, slot);tcg_temp_free (tmp); }
tcg/tcg-op.h +6 -3
@@ -759,9 +759,12 @@ static inline void tcg_gen_exit_tb(uintptr_t val)* * See tcg/README for more info about this TCG operation. * -* NOTE: Direct jumps with goto_tb are only safe within the pages this TB -* resides in because we don't take care of direct jumps when address mapping -* changes, e.g. in tlb_flush(). +* NOTE: In softmmu emulation, direct jumps with goto_tb are only safe within +* the pages this TB resides in because we don't take care of direct jumps when +* address mapping changes, e.g. in tlb_flush(). In user mode, there's only a +* static address translation, so the destination address is always valid, TBs +* are always invalidated properly, and direct jumps are reset when mapping +* changes. */ void tcg_gen_goto_tb (unsigned idx);
Signature · 1 line
2.8.1
Re: [PATCH v4 10/10] tcg: Allow goto_tb to any target PC in user mode
Quoted 11 lines · Sergey Fedorov
From: Sergey Fedorov <[email protected]>
In user mode, there's only a static address translation, TBs are always invalidated properly and direct jumps are reset when mapping change. Thus the destination address is always valid for direct jumps and there's no need to restrict it to the pages the TB resides in.
Signed-off-by Sergey Fedorov <[email protected]>Signed-off-by Sergey Fedorov <[email protected]>Cc Riku Voipio <[email protected]>Cc Blue Swirl <[email protected]>
Quoted 625 lines
---
Changes in v4:
* Explanatory comments moved totcg_gen_goto_tb()declaration comment
* Cc'ed usermode maintainers in commit message excplicitlytarget-alpha/translate.c | 4 ++++ target-arm/translate-a64.c | 2 ++ target-arm/translate.c | 18 ++++++++++++------ target-cris/translate.c | 18 ++++++++++++------ target-i386/translate.c | 23 ++++++++++++++--------- target-lm32/translate.c | 21 +++++++++++++++------ target-m68k/translate.c | 18 ++++++++++++------ target-microblaze/translate.c | 15 +++++++++++---- target-mips/translate.c | 20 +++++++++++++++----- target-moxie/translate.c | 21 +++++++++++++++------ target-openrisc/translate.c | 20 +++++++++++++++----- target-ppc/translate.c | 20 +++++++++++++++----- target-s390x/translate.c | 17 +++++++++++------ target-sh4/translate.c | 21 +++++++++++++++------ target-sparc/translate.c | 24 +++++++++++++++++------- target-tricore/translate.c | 20 +++++++++++++++----- target-unicore32/translate.c | 16 +++++++++++----- target-xtensa/translate.c | 4 ++++ tcg/tcg-op.h | 9 ++++++--- 19 files changed, 221 insertions(+), 90 deletions(-)target-alpha/translate.c +4
-0@@ -464,8 +464,12 @@ static bool use_goto_tb(DisasContext *ctx, uint64_t dest)if (in_superpage (ctx, dest)) {return true; } +#ifndef CONFIG_USER_ONLY /* Check for the dest on the same page as the start of the TB. */ return ((ctx->tb->pc ^ dest) &TARGET_PAGE_MASK ) ==0 ; +#else +return true; +#endif }static ExitStatus gen_bdirect (DisasContext *ctx,int ra,int32_t disp)target-arm/translate-a64.c +2
-0@@ -274,10 +274,12 @@ static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)return false; } +#ifndef CONFIG_USER_ONLY /* Only link tbs from inside the same guest page */ if ((s->tb->pc &TARGET_PAGE_MASK ) != (dest &TARGET_PAGE_MASK )) {return false; } +#endif return true; }target-arm/translate.c +12
-6@@ -4049,16 +4049,22 @@ static int disas_vfp_insn(DisasContext *s, uint32_t insn)return 0 ; } -static inline void gen_goto_tb (DisasContext *s,int n,target_ulong dest) +static inline bool use_goto_tb (DisasContext *s,target_ulong dest) { -TranslationBlock *tb; +#ifndef CONFIG_USER_ONLY +return (s->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || + ((s->pc -1 ) &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} - tb = s->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || - ((s->pc -1 ) &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { +static inline void gen_goto_tb (DisasContext *s,int n,target_ulong dest) +{ +if (use_goto_tb (s, dest)) {tcg_gen_goto_tb (n);gen_set_pc_im (s, dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )s->tb + n); }else {gen_set_pc_im (s, dest);tcg_gen_exit_tb (0 );target-cris/translate.c +12
-6@@ -520,16 +520,22 @@ static void t_gen_cc_jmp(TCGv pc_true, TCGv pc_false)gen_set_label (l1); } -static void gen_goto_tb (DisasContext *dc,int n,target_ulong dest) +static inline bool use_goto_tb (DisasContext *dc,target_ulong dest) { -TranslationBlock *tb; - tb = dc->tb; +#ifndef CONFIG_USER_ONLY +return (dc->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || + (dc->ppc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || - (dc->ppc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { +static void gen_goto_tb (DisasContext *dc,int n,target_ulong dest) +{ +if (use_goto_tb (dc, dest)) {tcg_gen_goto_tb (n);tcg_gen_movi_tl (env_pc, dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )dc->tb + n); }else {tcg_gen_movi_tl (env_pc, dest);tcg_gen_exit_tb (0 );target-i386/translate.c +14
-9@@ -2085,20 +2085,25 @@ static inline int insn_const_size(TCGMemOp ot) } } +static inline bool use_goto_tb (DisasContext *s,target_ulong pc) +{ +#ifndef CONFIG_USER_ONLY +return (pc &TARGET_PAGE_MASK ) == (s->tb->pc &TARGET_PAGE_MASK ) || + (pc &TARGET_PAGE_MASK ) == (s->pc_start &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +static inline void gen_goto_tb (DisasContext *s,int tb_num,target_ulong eip) { -TranslationBlock *tb; -target_ulong pc; - - pc = s->cs_base + eip; - tb = s->tb; -/* NOTE: we handle the case where the TB spans two pages here */ -if ((pc &TARGET_PAGE_MASK ) == (tb->pc &TARGET_PAGE_MASK ) || - (pc &TARGET_PAGE_MASK ) == (s->pc_start &TARGET_PAGE_MASK )) { +target_ulong pc = s->cs_base + eip; + +if (use_goto_tb (s, pc)) {/* jump to same page: we can use a direct jump */ tcg_gen_goto_tb (tb_num);gen_jmp_im (eip); -tcg_gen_exit_tb ((uintptr_t )tb + tb_num); +tcg_gen_exit_tb ((uintptr_t )s->tb + tb_num); }else {/* jump to another page: currently not optimized */ gen_jmp_im (eip);target-lm32/translate.c +15
-6@@ -133,16 +133,25 @@ static inline void t_gen_illegal_insn(DisasContext *dc)gen_helper_ill (cpu_env); } -static void gen_goto_tb (DisasContext *dc,int n,target_ulong dest) +static inline bool use_goto_tb (DisasContext *dc,target_ulong dest) { -TranslationBlock *tb; +if (unlikely (dc->singlestep_enabled)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (dc->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} - tb = dc->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) && -likely (!dc->singlestep_enabled)) { +static void gen_goto_tb (DisasContext *dc,int n,target_ulong dest) +{ +if (use_goto_tb (dc, dest)) {tcg_gen_goto_tb (n);tcg_gen_movi_tl (cpu_pc, dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )dc->tb + n); }else {tcg_gen_movi_tl (cpu_pc, dest);if (dc->singlestep_enabled) {target-m68k/translate.c +12
-6@@ -852,19 +852,25 @@ static inline void gen_addr_fault(DisasContext *s) } \ }while (0 ) +static inline bool use_goto_tb(DisasContext *s, uint32_t dest) +{ +#ifndef CONFIG_USER_ONLY +return (s->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || + (s->insn_pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +/* Generate a jump to an immediate address. */ static void gen_jmp_tb (DisasContext *s,int n,uint32_t dest) { -TranslationBlock *tb; - - tb = s->tb;if (unlikely (s->singlestep_enabled)) {gen_exception (s, dest,EXCP_DEBUG ); - }else if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) || - (s->insn_pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { + }else if (use_goto_tb (s, dest)) {tcg_gen_goto_tb (n);tcg_gen_movi_i32 (QREG_PC , dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )s->tb + n); }else {gen_jmp_im (s, dest);tcg_gen_exit_tb (0 );target-microblaze/translate.c +11
-4@@ -124,14 +124,21 @@ static inline void t_gen_raise_exception(DisasContext *dc, uint32_t index) dc->is_jmp =DISAS_UPDATE ; } +static inline bool use_goto_tb (DisasContext *dc,target_ulong dest) +{ +#ifndef CONFIG_USER_ONLY +return (dc->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +static void gen_goto_tb (DisasContext *dc,int n,target_ulong dest) { -TranslationBlock *tb; - tb = dc->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { +if (use_goto_tb (dc, dest)) {tcg_gen_goto_tb (n);tcg_gen_movi_tl (cpu_SR[SR_PC ], dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )dc->tb + n); }else {tcg_gen_movi_tl (cpu_SR[SR_PC ], dest);tcg_gen_exit_tb (0 );target-mips/translate.c +15
-5@@ -4191,15 +4191,25 @@ static void gen_trap (DisasContext *ctx, uint32_t opc,tcg_temp_free (t1); } +static inline bool use_goto_tb (DisasContext *ctx,target_ulong dest) +{ +if (unlikely (ctx->singlestep_enabled)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (ctx->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +static inline void gen_goto_tb (DisasContext *ctx,int n,target_ulong dest) { -TranslationBlock *tb; - tb = ctx->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) && -likely (!ctx->singlestep_enabled)) { +if (use_goto_tb (ctx, dest)) {tcg_gen_goto_tb (n);gen_save_pc (dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )ctx->tb + n); }else {gen_save_pc (dest);if (ctx->singlestep_enabled) {target-moxie/translate.c +15
-6@@ -121,17 +121,26 @@ void moxie_translate_init(void) done_init =1 ; } +static inline bool use_goto_tb (DisasContext *ctx,target_ulong dest) +{ +if (unlikely (ctx->singlestep_enabled)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (ctx->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +static inline void gen_goto_tb (CPUMoxieState *env,DisasContext *ctx,int n,target_ulong dest) { -TranslationBlock *tb; - tb = ctx->tb; - -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) && - !ctx->singlestep_enabled) { +if (use_goto_tb (ctx, dest)) {tcg_gen_goto_tb (n);tcg_gen_movi_i32 (cpu_pc, dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )ctx->tb + n); }else {tcg_gen_movi_i32 (cpu_pc, dest);if (ctx->singlestep_enabled) {target-openrisc/translate.c +15
-5@@ -190,15 +190,25 @@ static void check_ov64s(DisasContext *dc)} #endif*/ +static inline bool use_goto_tb (DisasContext *dc,target_ulong dest) +{ +if (unlikely (dc->singlestep_enabled)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (dc->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +static void gen_goto_tb (DisasContext *dc,int n,target_ulong dest) { -TranslationBlock *tb; - tb = dc->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) && -likely (!dc->singlestep_enabled)) { +if (use_goto_tb (dc, dest)) {tcg_gen_movi_tl (cpu_pc, dest);tcg_gen_goto_tb (n); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )dc->tb + n); }else {tcg_gen_movi_tl (cpu_pc, dest);if (dc->singlestep_enabled) {target-ppc/translate.c +15
-5@@ -3822,19 +3822,29 @@ static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)#endif } +static inline bool use_goto_tb (DisasContext *ctx,target_ulong dest) +{ +if (unlikely (ctx->singlestep_enabled)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (ctx->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +/*** Branch ***/ static inline void gen_goto_tb (DisasContext *ctx,int n,target_ulong dest) { -TranslationBlock *tb; - tb = ctx->tb;if (NARROW_MODE (ctx)) { dest = (uint32_t ) dest; } -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) && -likely (!ctx->singlestep_enabled)) { +if (use_goto_tb (ctx, dest)) {tcg_gen_goto_tb (n);tcg_gen_movi_tl (cpu_nip, dest & ~3 ); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )ctx->tb + n); }else {tcg_gen_movi_tl (cpu_nip, dest & ~3 );if (unlikely (ctx->singlestep_enabled)) {target-s390x/translate.c +11
-6@@ -608,12 +608,17 @@ static void gen_op_calc_cc(DisasContext *s)static int use_goto_tb (DisasContext *s,uint64_t dest) { -/* NOTE: we handle the case where the TB spans two pages here */ -return (((dest &TARGET_PAGE_MASK ) == (s->tb->pc &TARGET_PAGE_MASK ) - || (dest &TARGET_PAGE_MASK ) == (s->pc &TARGET_PAGE_MASK )) - && !s->singlestep_enabled - && !(s->tb->cflags &CF_LAST_IO ) - && !(s->tb->flags &FLAG_MASK_PER )); +if (unlikely (s->singlestep_enabled) || + (s->tb->cflags &CF_LAST_IO ) || + (s->tb->flags &FLAG_MASK_PER )) { +return false; + } +#ifndef CONFIG_USER_ONLY +return (dest &TARGET_PAGE_MASK ) == (s->tb->pc &TARGET_PAGE_MASK ) || + (dest &TARGET_PAGE_MASK ) == (s->pc &TARGET_PAGE_MASK ); +#else +return true; +#endif }static void account_noninline_branch (DisasContext *s,int cc_op)target-sh4/translate.c +15
-6@@ -205,17 +205,26 @@ static void gen_write_sr(TCGv src)tcg_gen_andi_i32 (cpu_sr_t, cpu_sr_t,1 ); } -static void gen_goto_tb (DisasContext * ctx,int n,target_ulong dest) +static inline bool use_goto_tb (DisasContext *ctx,target_ulong dest) { -TranslationBlock *tb; - tb = ctx->tb; +if (unlikely (ctx->singlestep_enabled)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (ctx->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) && - !ctx->singlestep_enabled) { +static void gen_goto_tb (DisasContext *ctx,int n,target_ulong dest) +{ +if (use_goto_tb (ctx, dest)) {/* Use a direct jump if in same page and singlestep not enabled */ tcg_gen_goto_tb (n);tcg_gen_movi_i32 (cpu_pc, dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )ctx->tb + n); }else {tcg_gen_movi_i32 (cpu_pc, dest);if (ctx->singlestep_enabled)target-sparc/translate.c +17
-7@@ -303,20 +303,30 @@ static inline TCGv gen_dest_gpr(DisasContext *dc, int reg) } } +static inline bool use_goto_tb (DisasContext *s,target_ulong pc, +target_ulong npc) +{ +if (unlikely (s->singlestep)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (pc &TARGET_PAGE_MASK ) == (s->tb->pc &TARGET_PAGE_MASK ) && + (npc &TARGET_PAGE_MASK ) == (s->tb->pc &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +static inline void gen_goto_tb (DisasContext *s,int tb_num,target_ulong pc,target_ulong npc) { -TranslationBlock *tb; - - tb = s->tb; -if ((pc &TARGET_PAGE_MASK ) == (tb->pc &TARGET_PAGE_MASK ) && - (npc &TARGET_PAGE_MASK ) == (tb->pc &TARGET_PAGE_MASK ) && - !s->singlestep) { +if (use_goto_tb (s, pc, npc)) {/* jump to same page: we can use a direct jump */ tcg_gen_goto_tb (tb_num);tcg_gen_movi_tl (cpu_pc, pc);tcg_gen_movi_tl (cpu_npc, npc); -tcg_gen_exit_tb ((uintptr_t )tb + tb_num); +tcg_gen_exit_tb ((uintptr_t )s->tb + tb_num); }else {/* jump to another page: currently not optimized */ tcg_gen_movi_tl (cpu_pc, pc);target-tricore/translate.c +15
-5@@ -3236,15 +3236,25 @@ static inline void gen_save_pc(target_ulong pc)tcg_gen_movi_tl (cpu_PC, pc); } +static inline bool use_goto_tb (DisasContext *ctx,target_ulong dest) +{ +if (unlikely (ctx->singlestep_enabled)) { +return false; + } + +#ifndef CONFIG_USER_ONLY +return (ctx->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} +static inline void gen_goto_tb (DisasContext *ctx,int n,target_ulong dest) { -TranslationBlock *tb; - tb = ctx->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ) && -likely (!ctx->singlestep_enabled)) { +if (use_goto_tb (ctx, dest)) {tcg_gen_goto_tb (n);gen_save_pc (dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )ctx->tb + n); }else {gen_save_pc (dest);if (ctx->singlestep_enabled) {target-unicore32/translate.c +11
-5@@ -1089,15 +1089,21 @@ static void disas_ucf64_insn(CPUUniCore32State *env, DisasContext *s, uint32_t i } } -static inline void gen_goto_tb (DisasContext *s,int n,uint32_t dest) +static inline bool use_goto_tb (DisasContext *s,uint32_t dest) { -TranslationBlock *tb; +#ifndef CONFIG_USER_ONLY +return (s->tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK ); +#else +return true; +#endif +} - tb = s->tb; -if ((tb->pc &TARGET_PAGE_MASK ) == (dest &TARGET_PAGE_MASK )) { +static inline void gen_goto_tb (DisasContext *s,int n,uint32_t dest) +{ +if (use_goto_tb (s, dest)) {tcg_gen_goto_tb (n);gen_set_pc_im (dest); -tcg_gen_exit_tb ((uintptr_t )tb + n); +tcg_gen_exit_tb ((uintptr_t )s->tb + n); }else {gen_set_pc_im (dest);tcg_gen_exit_tb (0 );target-xtensa/translate.c +4
-0@@ -418,9 +418,11 @@ static void gen_jump(DisasContext *dc, TCGv dest)static void gen_jumpi (DisasContext *dc,uint32_t dest,int slot) {TCGv_i32 tmp =tcg_const_i32 (dest); +#ifndef CONFIG_USER_ONLY if (((dc->tb->pc ^ dest) &TARGET_PAGE_MASK ) !=0 ) { slot =-1 ; } +#endif gen_jump_slot (dc, tmp, slot);tcg_temp_free (tmp); }@@ -446,9 +448,11 @@ static void gen_callw(DisasContext *dc, int callinc, TCGv_i32 dest)static void gen_callwi (DisasContext *dc,int callinc,uint32_t dest,int slot) {TCGv_i32 tmp =tcg_const_i32 (dest); +#ifndef CONFIG_USER_ONLY if (((dc->tb->pc ^ dest) &TARGET_PAGE_MASK ) !=0 ) { slot =-1 ; } +#endif gen_callw_slot (dc, callinc, tmp, slot);tcg_temp_free (tmp); }tcg/tcg-op.h +0
-1@@ -759,9 +759,12 @@ static inline void tcg_gen_exit_tb(uintptr_t val)* * See tcg/README for more info about this TCG operation. * -* NOTE: Direct jumps with goto_tb are only safe within the pages this TB
tcg/tcg-op.h static inline void tcg_gen_exit_tb(uintptr_t val) · line 769
Quoted 10 lines
tcg/tcg-op.h +6
-2-* resides in because we don't take care of direct jumps when address mapping -* changes, e.g. in tlb_flush(). +* NOTE: In softmmu emulation, direct jumps with goto_tb are only safe within +* the pages this TB resides in because we don't take care of direct jumps when +* address mapping changes, e.g. in tlb_flush(). In user mode, there's only a +* static address translation, so the destination address is always valid, TBs +* are always invalidated properly, and direct jumps are reset when mapping +* changes. */ void tcg_gen_goto_tb (unsigned idx);
--
Alex Bennée
Re: [Qemu-devel] [PATCH v4 10/10] tcg: Allow goto_tb to any target PC in user mode
In reply to Sergey FedorovQuoted 4 lines · Sergey Fedorov
target-alpha/translate.c +0
-0@@ -464,8 +464,12 @@ static bool use_goto_tb(DisasContext *ctx, uint64_t dest)if (in_superpage (ctx, dest)) {
target-alpha/translate.c static bool use_goto_tb(DisasContext *ctx, uint64_t dest) · line 474
Quoted 10 lines
target-alpha/translate.c +4
-0return true; } +#ifndef CONFIG_USER_ONLY /* Check for the dest on the same page as the start of the TB. */ return ((ctx->tb->pc ^ dest) &TARGET_PAGE_MASK ) ==0 ; +#else +return true; +#endif }
Nit: The in_superpage test just above is for kernel mode and need not be tested in user-only.
r~
Quoted 9 lines · Sergey Fedorov
From: Sergey Fedorov <[email protected]>
This series combines a set of patches which is meant to improve overall code structure and readability of the direct block chaining mechanism. The other point is to make a step towards thread safety of TB chainig.
The series' tree can be found in a public git repository [1].
[1] https://github.com/sergefdrv/qemu/tree/tb-chaining-cleanup-v4
I've run into a bunch of merge failures whilst trying to build a combined tree. A bunch in the tcg/* files due to atomic patching fixes now in rth/tcg-next and the final one due to the code motion in cpu-exec from the misc clean-ups.
It might be worth re-basing at least on tcg-next?
Quoted 55 lines
Summary of changes:
Changes in v4:
* Removed assert fromtb_add_jump()[PATCH v4 02/10]
* Added comment on TB stuff synchronization [PATCH v4 04/10]
* Documentedtcg_gen_goto_tb()and moved its usage notes there[PATCH v4 09/10] and [PATCH v4 10/10] * Cc'ed usermode maintainers in commit message [PATCH v4 10/10] Changes in v3: * New patch to clean up safety checks [PATCH v3 09/10] * New patch to eliminate unneeded checks in user-mode [PATCH v3 10/10] Changes in v2: * Eliminated duplicate dereference of 'ptb' in tb_jmp_remove() [PATCH v2 2/8] * Tweaked a comment [PATCH v2 4/8] * Complete rewrite [PATCH v2 5/8] * Tweaked a comment; eliminated duplicate dereference of 'ptb' in tb_jmp_unlink() [PATCH v2 8/8]Sergey Fedorov (10):
tcg: Clean up direct block chaining data fields
tcg: Useuintptr_ttype for jmp_list_{next|first} fields of TB
tcg: Rearrangetb_link_page()to avoid forward declaration
tcg: Init TB's direct jumps before making it visible
tcg: Clarify thread safety check intb_add_jump()
tcg: Renametb_jmp_remove()totb_remove_from_jmp_list()
tcg: Extract removing of jumps to TB fromtb_phys_invalidate()
tcg: Clean uptb_jmp_unlink()
tcg: Clean up direct block chaining safety checks
tcg: Allowgoto_tbto any target PC in user modecpu-exec.c | 7 +- include/exec/exec-all.h | 69 ++++++---- target-alpha/translate.c | 4 + target-arm/translate-a64.c | 2 + target-arm/translate.c | 17 ++- target-cris/translate.c | 16 ++- target-i386/translate.c | 23 ++-- target-lm32/translate.c | 21 ++- target-m68k/translate.c | 18 ++- target-microblaze/translate.c | 15 ++- target-mips/translate.c | 20 ++- target-moxie/translate.c | 21 ++- target-openrisc/translate.c | 20 ++- target-ppc/translate.c | 20 ++- target-s390x/translate.c | 17 ++- target-sh4/translate.c | 21 ++- target-sparc/translate.c | 24 +++- target-tricore/translate.c | 20 ++- target-unicore32/translate.c | 16 ++- target-xtensa/translate.c | 4 + tcg/aarch64/tcg-target.inc.c | 7 +- tcg/arm/tcg-target.inc.c | 8 +- tcg/i386/tcg-target.inc.c | 8 +- tcg/ia64/tcg-target.inc.c | 6 +- tcg/mips/tcg-target.inc.c | 8 +-
Quoted 8 lines
tcg/ppc/tcg-target.inc.c | 6 +- tcg/s390/tcg-target.inc.c | 11 +- tcg/sparc/tcg-target.inc.c | 9 +- tcg/tcg-op.h | 13 ++ tcg/tcg.h | 6 +- tcg/tci/tcg-target.inc.c | 10 +- translate-all.c | 297 ++++++++++++++++++++++-------------------- 32 files changed, 470 insertions(+), 294 deletions(-)
--
Alex Bennée
Quoted 10 lines · Alex Bennée
Quoted 8 lines · Sergey Fedorov
From: Sergey Fedorov <[email protected]>
This series combines a set of patches which is meant to improve overall code structure and readability of the direct block chaining mechanism. The other point is to make a step towards thread safety of TB chainig.
The series' tree can be found in a public git repository [1].
Quoted 7 lines
Quoted 1 line
[1] https://github.com/sergefdrv/qemu/tree/tb-chaining-cleanup-v4
I've run into a bunch of merge failures whilst trying to build a combined tree. A bunch in the tcg/* files due to atomic patching fixes now in rth/tcg-next and the final one due to the code motion in cpu-exec from the misc clean-ups.
It might be worth re-basing at least on tcg-next?
Okay, I'll do a re-base and send v5 today.
Kind regards,
Sergey
Quoted 3 lines · Sergey Fedorov
Quoted 1 line
It might be worth re-basing at least on tcg-next?
Okay, I'll do a re-base and send v5 today.
Thanks. I noticed similar conflicts with the misc cleanups patchset last night. I believe both series are ready to merge with a rebase.
r~