Support more than 8 MMU modes, speedup PPC by 10%
v2 cover Support more than 8 MMU modes, speedup PPC by 10%
Patches 1 and 2 enable support from more than 8 MMU modes in TCG (patch 1 is in the targets, patch 2 is in cpu-defs.h). The TLB size is reduced proportionally on targets where that is necessary.
Patch 3 uses the new support in the PPC target.
Paolo
Paolo Bonzini (3):
tcg: add TCG_TARGET_TLB_DISPLACEMENT_BITS
softmmu: support up to 12 MMU modes
target-ppc: use separate indices for various translation modes
include/exec/cpu-defs.h | 34 +++++++++++++++- include/exec/cpu_ldst.h | 104 ++++++++++++++++++++++++++++++++++++++++++++--- target-ppc/cpu.h | 12 +++--- target-ppc/excp_helper.c | 3 -- target-ppc/helper_regs.h | 15 ++++--- tcg/aarch64/tcg-target.h | 1 + tcg/arm/tcg-target.h | 1 + tcg/i386/tcg-target.h | 1 + tcg/ia64/tcg-target.h | 2 + tcg/mips/tcg-target.h | 1 + tcg/ppc/tcg-target.h | 1 + tcg/s390/tcg-target.h | 1 + tcg/sparc/tcg-target.h | 1 + tcg/tci/tcg-target.h | 1 + 14 files changed, 156 insertions(+), 22 deletions(-)
Signature · 1 line
2.3.0
PATCH v2 1/3 tcg: add TCG_TARGET_TLB_DISPLACEMENT_BITS
This will be used to size the TLB when more than 8 MMU modes are used by the target. Limitations come from the limited size of the immediate fields (which sometimes, as in the case of Aarch64, extend to instructions that shift the immediate).
--- tcg/aarch64/tcg-target.h | 1 + tcg/arm/tcg-target.h | 1 + tcg/i386/tcg-target.h | 1 + tcg/ia64/tcg-target.h | 2 ++ tcg/mips/tcg-target.h | 1 + tcg/ppc/tcg-target.h | 1 + tcg/s390/tcg-target.h | 1 + tcg/sparc/tcg-target.h | 1 + tcg/tci/tcg-target.h | 1 + 9 files changed, 10 insertions(+)
tcg/aarch64/tcg-target.h +1 -0
@@ -14,6 +14,7 @@#define TCG_TARGET_AARCH64 1#define TCG_TARGET_INSN_UNIT_SIZE 4 +#define TCG_TARGET_TLB_DISPLACEMENT_BITS 24#undef TCG_TARGET_STACK_GROWSUP typedef enum {
tcg/arm/tcg-target.h +1 -0
@@ -27,6 +27,7 @@#undef TCG_TARGET_STACK_GROWSUP#define TCG_TARGET_INSN_UNIT_SIZE 4 +#define TCG_TARGET_TLB_DISPLACEMENT_BITS 16 typedef enum { TCG_REG_R0 = 0,
tcg/i386/tcg-target.h +1 -0
@@ -25,6 +25,7 @@#define TCG_TARGET_I386 1#define TCG_TARGET_INSN_UNIT_SIZE 1 +#define TCG_TARGET_TLB_DISPLACEMENT_BITS 32#ifdef __x86_64__# define TCG_TARGET_REG_BITS 64
tcg/ia64/tcg-target.h +2 -0
@@ -26,6 +26,8 @@#define TCG_TARGET_IA64 1#define TCG_TARGET_INSN_UNIT_SIZE 16 +#define TCG_TARGET_TLB_DISPLACEMENT_BITS 21 + typedef struct { uint64_t lo __attribute__((aligned(16))); uint64_t hi;
tcg/mips/tcg-target.h +1 -0
@@ -27,6 +27,7 @@#define TCG_TARGET_MIPS 1#define TCG_TARGET_INSN_UNIT_SIZE 4 +#define TCG_TARGET_TLB_DISPLACEMENT_BITS 16#define TCG_TARGET_NB_REGS 32 typedef enum {
tcg/ppc/tcg-target.h +1 -0
@@ -32,6 +32,7 @@#define TCG_TARGET_NB_REGS 32#define TCG_TARGET_INSN_UNIT_SIZE 4 +#define TCG_TARGET_TLB_DISPLACEMENT_BITS 16 typedef enum { TCG_REG_R0, TCG_REG_R1, TCG_REG_R2, TCG_REG_R3,
tcg/s390/tcg-target.h +1 -0
@@ -25,6 +25,7 @@#define TCG_TARGET_S390 1#define TCG_TARGET_INSN_UNIT_SIZE 2 +#define TCG_TARGET_TLB_DISPLACEMENT_BITS 19 typedef enum TCGReg { TCG_REG_R0 = 0,
tcg/sparc/tcg-target.h +1 -0
@@ -27,6 +27,7 @@#define TCG_TARGET_REG_BITS 64#define TCG_TARGET_INSN_UNIT_SIZE 4 +#define TCG_TARGET_TLB_DISPLACEMENT_BITS 32#define TCG_TARGET_NB_REGS 32 typedef enum {
tcg/tci/tcg-target.h +1 -0
@@ -44,6 +44,7 @@#define TCG_TARGET_INTERPRETER 1#define TCG_TARGET_INSN_UNIT_SIZE 1 +#define TCG_TARGET_TLB_DISPLACEMENT_BITS 32#if UINTPTR_MAX ==UINT32_MAX # define TCG_TARGET_REG_BITS 32
Signature · 1 line
2.3.0
Re: [Qemu-devel] [PATCH 1/3] tcg: add TCG_TARGET_TLB_DISPLACEMENT_BITS
tcg/i386/tcg-target.h line 28
Quoted 9 lines · Paolo Bonzini
tcg/i386/tcg-target.h +1
-0@@ -25,6 +25,7 @@#define TCG_TARGET_I386 1#define TCG_TARGET_INSN_UNIT_SIZE 1 +#define TCG_TARGET_TLB_DISPLACEMENT_BITS 32
31.
Positive displacements only in 64-bit mode. I don't think it's worth conditionalizing this for 32-bit, since we can't actually allocate 2G of TLBs and then actually accomplish anything. ;-)
Quoted 17 lines
tcg/mips/tcg-target.h +1
-0@@ -27,6 +27,7 @@#define TCG_TARGET_MIPS 1#define TCG_TARGET_INSN_UNIT_SIZE 4 +#define TCG_TARGET_TLB_DISPLACEMENT_BITS 16#define TCG_TARGET_NB_REGS 32 typedef enum {tcg/ppc/tcg-target.h +0
-0
tcg/ppc/tcg-target.h line 35
Quoted 4 lines
tcg/ppc/tcg-target.h +1
-0@@ -32,6 +32,7 @@#define TCG_TARGET_NB_REGS 32#define TCG_TARGET_INSN_UNIT_SIZE 4 +#define TCG_TARGET_TLB_DISPLACEMENT_BITS 16
Close enough, since the BUILD_BUG_ON should still catch this out if somehow the size is within that last 16 bytes of 64k.
r~
Re: [Qemu-devel] [PATCH 1/3] tcg: add TCG_TARGET_TLB_DISPLACEMENT_BITS
Quoted 8 lines · Richard Henderson
Quoted 7 lines · Paolo Bonzini
tcg/i386/tcg-target.h +0
-0@@ -25,6 +25,7 @@#define TCG_TARGET_I386 1
Quoted 8 lines
tcg/i386/tcg-target.h line 28
Quoted 2 lines
tcg/i386/tcg-target.h +1
-0#define TCG_TARGET_INSN_UNIT_SIZE 1 +#define TCG_TARGET_TLB_DISPLACEMENT_BITS 3231.
Positive displacements only in 64-bit mode. I don't think it's worth conditionalizing this for 32-bit, since we can't actually allocate 2G of TLBs and then actually accomplish anything. ;-)
I suppose Paolo is already off to the hospital ;). Richard, if I just s/32/31/ in the hunk above, does that mean you ack the patch?
Alex
Quoted 29 lines
Quoted 18 lines
tcg/mips/tcg-target.h +1
-0@@ -27,6 +27,7 @@#define TCG_TARGET_MIPS 1#define TCG_TARGET_INSN_UNIT_SIZE 4 +#define TCG_TARGET_TLB_DISPLACEMENT_BITS 16#define TCG_TARGET_NB_REGS 32 typedef enum {tcg/ppc/tcg-target.h +0
-0tcg/ppc/tcg-target.h line 35
Quoted 4 lines
tcg/ppc/tcg-target.h +1
-0@@ -32,6 +32,7 @@#define TCG_TARGET_NB_REGS 32#define TCG_TARGET_INSN_UNIT_SIZE 4 +#define TCG_TARGET_TLB_DISPLACEMENT_BITS 16Close enough, since the
BUILD_BUG_ONshould still catch this out if somehow the size is within that last 16 bytes of 64k.r~
Re: [Qemu-devel] [PATCH 1/3] tcg: add TCG_TARGET_TLB_DISPLACEMENT_BITS
Quoted 2 lines · Alexander Graf
I suppose Paolo is already off to the hospital ;). Richard, if I just s/32/31/ in the hunk above, does that mean you ack the patch?
Yep.
r~
PATCH v2 2/3 softmmu: support up to 12 MMU modes
In reply to Paolo BonziniAt 8k per TLB (for 64-bit host or target), 8 or more modes make the TLBs bigger than 64k, and some RISC TCG backends do not like that. On the affected hosts, cut the TLB size in half---there is still a measurable speedup on PPC with the next patch.
--- include/exec/cpu-defs.h | 34 +++++++++++++++- include/exec/cpu_ldst.h | 104 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 130 insertions(+), 8 deletions(-)
include/exec/cpu-defs.h +32 -2
@@ -27,6 +27,7 @@#include <inttypes.h> #include "qemu/osdep.h" #include "qemu/queue.h" +#include "tcg-target.h" #ifndef CONFIG_USER_ONLY #include "exec/hwaddr.h" #endif
@@ -69,8 +70,6 @@ typedef uint64_t target_ulong;#define TB_JMP_PAGE_MASK (TB_JMP_CACHE_SIZE - TB_JMP_PAGE_SIZE)#if !defined(CONFIG_USER_ONLY ) -#define CPU_TLB_BITS 8 -#define CPU_TLB_SIZE (1 << CPU_TLB_BITS)/* use a fully associative victim tlb of 8 entries */ #define CPU_VTLB_SIZE 8
@@ -80,6 +79,37 @@ typedef uint64_t target_ulong;#define CPU_TLB_ENTRY_BITS 5#endif +/* TCG_TARGET_TLB_DISPLACEMENT_BITS is used in CPU_TLB_BITS to ensure that +* the TLB is not unnecessarily small, but still small enough for the +* TLB lookup instruction sequence used by the TCG target. +* +* TCG will have to generate an operand as large as the distance between +* tlb_table[0][0] and the tlb_table[NB_MMU_MODES - 1][0].addend. For +* simplicity, the TCG targets just round everything up to the next +* power of two, and count bits. This works because: 1) the size of each +* TLB is a largish power of two, 2) and because the limit of the +* displacement is really close to a power of two. +* +* For example, the maximum displacement 0xFFF0 on PPC and MIPS, but TCG +* just says "the displacement is 16 bits". TCG_TARGET_TLB_DISPLACEMENT_BITS +* then ensures that tlb_table at least 0x8000 bytes large ("not unnecessarily +* small": 2^15). The operand then will come up smaller than 0xFFF0 without +* any particular care, because the TLB for a single MMU mode is larger than +* 0x10000-0xFFF0=16 bytes. In the end, the maximum value of the operand +* will be something like 0xC018; here, 0xC000 is the offset of the last TLB +* table (tlb_table[NB_MMU_MODES - 1][0]), and 0x18 is the offset of the +* addend field in each TLB entry. +*/ +#define CPU_TLB_BITS \ + MIN(8, \ + TCG_TARGET_TLB_DISPLACEMENT_BITS - CPU_TLB_ENTRY_BITS - \ + (NB_MMU_MODES <= 1 ? 0 : \ + NB_MMU_MODES <= 2 ? 1 : \ + NB_MMU_MODES <= 4 ? 2 : \ + NB_MMU_MODES <= 8 ? 3 : 4)) + +#define CPU_TLB_SIZE (1 << CPU_TLB_BITS) + typedef struct CPUTLBEntry { /* bit TARGET_LONG_BITS to TARGET_PAGE_BITS : virtual address bit TARGET_PAGE_BITS-1..4 : Nonzero for accesses that should not
include/exec/cpu_ldst.h +98 -6
110 lines not on this page · raw message
Signature · 1 line
2.3.0
Re: [Qemu-devel] [PATCH 2/3] softmmu: support up to 12 MMU modes
Quoted 2 lines · Paolo Bonzini
+ * TCG will have to generate an operand as large as the distance between
+ * tlb_table[0][0] and the tlb_table[NB_MMU_MODES - 1][0].addend. For
Nit: the distance is from the start of env, i.e.
offsetof(CPUArchState, tlb_table[mem_index][0].addend)
not from the start of the table. Not that this particularly invalidates the rest of your reasoning, but the start of the tlb_table is usually at a non-trivial (though not huge) offset from the start of CPUArchState.
Otherwise, it looks good.
r~
PATCH v2 3/3 target-ppc: use separate indices for various translation modes
In reply to Paolo BonziniPowerPC TCG flushes the TLB on every IR/DR change, which basically means on every user<->kernel context switch. Encode IR/DR in the MMU index.
This brings the number of TLB flushes down from ~900000 to ~50000 for starting up the Debian installer, which is in line with x86 and gives a ~10% performance improvement.
--- target-ppc/cpu.h | 12 +++++++----- target-ppc/excp_helper.c | 3 --- target-ppc/helper_regs.h | 15 +++++++++------ 3 files changed, 16 insertions(+), 14 deletions(-)
target-ppc/cpu.h +7 -5
@@ -943,7 +943,13 @@ struct ppc_segment_page_sizes {/*****************************************************************************/ /* The whole PowerPC CPU context */ -#define NB_MMU_MODES 3 +#define NB_MMU_MODES 12 +#define MMU_IDX_IR 1 +#define MMU_IDX_DR 2 +#define MMU_IDX_PR 0 +#define MMU_IDX_SUP 4 +#define MMU_IDX_HV 8 +#define MMU_USER_IDX (MMU_IDX_PR|MMU_IDX_IR|MMU_IDX_DR)#define PPC_CPU_OPCODES_LEN 0x40#define PPC_CPU_INDIRECT_OPCODES_LEN 0x20
@@ -1252,10 +1258,6 @@ static inline CPUPPCState *cpu_init(const char *cpu_model)#define cpu_list ppc_cpu_list/* MMU modes definitions */ -#define MMU_MODE0_SUFFIX _user -#define MMU_MODE1_SUFFIX _kernel -#define MMU_MODE2_SUFFIX _hypv -#define MMU_USER_IDX 0static inline int cpu_mmu_index (CPUPPCState *env) {return env->mmu_idx;
target-ppc/excp_helper.c +0 -3
@@ -623,9 +623,6 @@ static inline void powerpc_excp(PowerPCCPU *cpu, int excp_model, int excp)if (env->spr[SPR_LPCR ] &LPCR_AIL ) { new_msr |= (1 <<MSR_IR ) | (1 <<MSR_DR ); - }else if (msr & ((1 <<MSR_IR ) | (1 <<MSR_DR ))) { -/* If we disactivated any translation, flush TLBs */ -tlb_flush (cs,1 ); }#ifdef TARGET_PPC64
target-ppc/helper_regs.h +9 -6
@@ -41,12 +41,17 @@ static inline void hreg_swap_gpr_tgpr(CPUPPCState *env)static inline void hreg_compute_mem_idx (CPUPPCState *env) { +int mmu_idx; +/* Precompute MMU index */ -if (msr_pr ==0 && msr_hv !=0 ) { - env->mmu_idx =2 ; +if (msr_pr ==1 ) { + mmu_idx =MMU_IDX_PR ; }else { - env->mmu_idx =1 - msr_pr; + mmu_idx = msr_hv ?MMU_IDX_HV :MMU_IDX_SUP ; } + mmu_idx |= msr_ir ?MMU_IDX_IR :0 ; + mmu_idx |= msr_dr ?MMU_IDX_DR :0 ; + env->mmu_idx = mmu_idx; }static inline void hreg_compute_hflags (CPUPPCState *env)
@@ -56,7 +61,7 @@ static inline void hreg_compute_hflags(CPUPPCState *env)/* We 'forget' FE0 & FE1: we'll never generate imprecise exceptions */ hflags_mask = (1 <<MSR_VR ) | (1 <<MSR_AP ) | (1 <<MSR_SA ) | (1 <<MSR_PR ) | (1 <<MSR_FP ) | (1 <<MSR_SE ) | (1 <<MSR_BE ) | - (1 <<MSR_LE ) | (1 <<MSR_VSX ); + (1 <<MSR_LE ) | (1 <<MSR_VSX ) | (1 <<MSR_IR ) | (1 <<MSR_DR ); hflags_mask |= (1ULL <<MSR_CM ) | (1ULL <<MSR_SF ) |MSR_HVB ;hreg_compute_mem_idx (env); env->hflags = env->msr & hflags_mask;
@@ -82,8 +87,6 @@ static inline int hreg_store_msr(CPUPPCState *env, target_ulong value, }if (((value >>MSR_IR ) &1 ) != msr_ir || ((value >>MSR_DR ) &1 ) != msr_dr) { -/* Flush all tlb when changing translation mode */ -tlb_flush (cs,1 ); excp =POWERPC_EXCP_NONE ; cs->interrupt_request |=CPU_INTERRUPT_EXITTB ; }
Signature · 1 line
2.3.0
Re: [Qemu-devel] [PATCH 3/3] target-ppc: use separate indices for various translation modes
Quoted 8 lines · Paolo Bonzini
PowerPC TCG flushes the TLB on every IR/DR change, which basically means on every user<->kernel context switch. Encode IR/DR in the MMU index.
This brings the number of TLB flushes down from ~900000 to ~50000 for starting up the Debian installer, which is in line with x86 and gives a ~10% performance improvement.
Quoted 6 lines
Signed-off-by Paolo Bonzini <[email protected]>--- target-ppc/cpu.h | 12 +++++++----- target-ppc/excp_helper.c | 3 --- target-ppc/helper_regs.h | 15 +++++++++------ 3 files changed, 16 insertions(+), 14 deletions(-)
I'm not an expert on ppc memory management, but it certainly LGTM.
r~