Skip to contentSkip to conversation

RFC: Code fetch optimisation

Qemu-devel J. Mayer 3 participants 12 messages Oct 14, 2007 – Oct 17, 2007

lore
Share & tools
lore

J. Mayer

author
#0 lore raw

Here's an updated version of the code fetch optimisation patch against current CVS.
As a remainder, this patch avoid use of softmmu helpers to fetch the code in most case. A new target define TARGET_HAS_VLE_INSNS has been added which is used to handle the case of an instruction that span 2 pages, when the target CPU uses a variable-length instructions encoding. For pure RISC, the code fetch is done using raw access routines.

Signature · 2 lines
J. Mayer <[email protected]>
Never organized

Index: cpu-all.h
===================================================================
RCS file: /sources/qemu/qemu/cpu-all.h,v
retrieving revision 1.76

cpu-all.h +7 -0
diff -u -d -d -p -r1.76 cpu-all.h
@@ -646,6 +646,13 @@ static inline void stfq_be_p(void *ptr, 
 #define ldl_code(p) ldl_raw(p)
 #define ldq_code(p) ldq_raw(p)
 
+#define ldub_code_p(sp, pp, p) ldub_raw(p)
+#define ldsb_code_p(sp, pp, p) ldsb_raw(p)
+#define lduw_code_p(sp, pp, p) lduw_raw(p)
+#define ldsw_code_p(sp, pp, p) ldsw_raw(p)
+#define ldl_code_p(sp, pp, p) ldl_raw(p)
+#define ldq_code_p(sp, pp, p) ldq_raw(p)
+
 #define ldub_kernel(p) ldub_raw(p)
 #define ldsb_kernel(p) ldsb_raw(p)
 #define lduw_kernel(p) lduw_raw(p)

Index: cpu-exec.c
===================================================================
RCS file: /sources/qemu/qemu/cpu-exec.c,v
retrieving revision 1.120

cpu-exec.c +1 -0
diff -u -d -d -p -r1.120 cpu-exec.c
@@ -133,6 +133,7 @@ static TranslationBlock *tb_find_slow(ta
     tb->tc_ptr = tc_ptr;
     tb->cs_base = cs_base;
     tb->flags = flags;
+    tb->page_addr[0] = phys_page1;
     cpu_gen_code(env, tb, CODE_GEN_MAX_SIZE, &code_gen_size);
     code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
 

Index: softmmu_header.h
===================================================================
RCS file: /sources/qemu/qemu/softmmu_header.h,v
retrieving revision 1.18

softmmu_header.h +62 -0

68 lines not on this page · raw message

Index: target-alpha/translate.c
===================================================================
RCS file: /sources/qemu/qemu/target-alpha/translate.c,v retrieving revision 1.6

translate.c +6 -1
diff -u -d -d -p -r1.6 translate.c
@@ -1965,6 +1965,7 @@ int gen_intermediate_code_internal (CPUS
     static int insn_count;
 #endif
     DisasContext ctx, *ctxp = &ctx;
+    unsigned long phys_pc, phys_pc_start;
     target_ulong pc_start;
     uint32_t insn;
     uint16_t *gen_opc_end;
@@ -1972,6 +1973,9 @@ int gen_intermediate_code_internal (CPUS
     int ret;
 
     pc_start = tb->pc;
+    phys_pc_start = (unsigned long)phys_ram_base + tb->page_addr[0] +
+        (pc_start & ~TARGET_PAGE_MASK);
+    phys_pc = phys_pc_start;
     gen_opc_ptr = gen_opc_buf;
     gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
     gen_opparam_ptr = gen_opparam_buf;
@@ -2010,7 +2014,7 @@ int gen_intermediate_code_internal (CPUS
                     ctx.pc, ctx.mem_idx);
         }
 #endif
-        insn = ldl_code(ctx.pc);
+        insn = ldl_code_p(&phys_pc_start, phys_pc, ctx.pc);
 #if defined ALPHA_DEBUG_DISAS
         insn_count++;
         if (logfile != NULL) {
@@ -2018,6 +2022,7 @@ int gen_intermediate_code_internal (CPUS
         }
 #endif
         ctx.pc += 4;
+        phys_pc += 4;
         ret = translate_one(ctxp, insn);
         if (ret != 0)
             break;

Index: target-arm/translate.c
===================================================================
RCS file: /sources/qemu/qemu/target-arm/translate.c,v retrieving revision 1.57

translate.c +10 -3
diff -u -d -d -p -r1.57 translate.c
@@ -38,6 +38,8 @@
 /* internal defines */
 typedef struct DisasContext {
     target_ulong pc;
+    unsigned long phys_pc;
+    unsigned long phys_pc_start;
     int is_jmp;
     /* Nonzero if this instruction has been conditionally skipped.  */
     int condjmp;
@@ -2206,8 +2208,9 @@ static void disas_arm_insn(CPUState * en
 {
     unsigned int cond, insn, val, op1, i, shift, rm, rs, rn, rd, sh;
 
-    insn = ldl_code(s->pc);
+    insn = ldl_code_p(&s->phys_pc_start, s->phys_pc, s->pc);
     s->pc += 4;
+    s->phys_pc += 4;
 
     cond = insn >> 28;
     if (cond == 0xf){
@@ -2971,8 +2974,9 @@ static void disas_thumb_insn(DisasContex
     int32_t offset;
     int i;
 
-    insn = lduw_code(s->pc);
+    insn = lduw_code_p(&s->phys_pc_start, s->phys_pc, s->pc);
     s->pc += 2;
+    s->phys_pc += 2;
 
     switch (insn >> 12) {
     case 0: case 1:
@@ -3494,7 +3498,7 @@ static void disas_thumb_insn(DisasContex
             break;
         }
         offset = ((int32_t)insn << 21) >> 10;
-        insn = lduw_code(s->pc);
+        insn = lduw_code_p(&s->phys_pc_start, s->phys_pc, s->pc);
         offset |= insn & 0x7ff;
 
         val = (uint32_t)s->pc + 2;
@@ -3544,6 +3548,9 @@ static inline int gen_intermediate_code_
 
     dc->is_jmp = DISAS_NEXT;
     dc->pc = pc_start;
+    dc->phys_pc_start = (unsigned long)phys_ram_base + tb->page_addr[0] +
+        (pc_start & ~TARGET_PAGE_MASK);
+    dc->phys_pc = dc->phys_pc_start;
     dc->singlestep_enabled = env->singlestep_enabled;
     dc->condjmp = 0;
     dc->thumb = env->thumb;

Index: target-cris/cpu.h
===================================================================
RCS file: /sources/qemu/qemu/target-cris/cpu.h,v
retrieving revision 1.2

cpu.h +2 -0
diff -u -d -d -p -r1.2 cpu.h
@@ -22,6 +22,8 @@
 #define CPU_CRIS_H
 
 #define TARGET_LONG_BITS 32
+/* need explicit support for instructions spanning 2 pages */
+#define TARGET_HAS_VLE_INSNS 1
 
 #include "cpu-defs.h"
 

Index: target-cris/translate.c
===================================================================
RCS file: /sources/qemu/qemu/target-cris/translate.c,v retrieving revision 1.1

translate.c +14 -8
diff -u -d -d -p -r1.1 translate.c
@@ -100,6 +100,7 @@ enum {
 typedef struct DisasContext {
 	CPUState *env;
 	target_ulong pc, insn_pc;
+        unsigned long phys_pc, phys_pc_start;
 
 	/* Decoder.  */
 	uint32_t ir;
@@ -828,7 +829,8 @@ static int dec_prep_alu_m(DisasContext *
 		if (memsize == 1)
 			insn_len++;
 
-		imm = ldl_code(dc->pc + 2);
+                imm = ldl_code_p(&dc->phys_pc_start, dc->phys_pc + 2,
+                                 dc->pc + 2);
 		if (memsize != 4) {
 			if (s_ext) {
 				imm = sign_extend(imm, (memsize * 8) - 1);
@@ -1962,7 +1964,7 @@ static unsigned int dec_lapc_im(DisasCon
 	rd = dc->op2;
 
 	cris_cc_mask(dc, 0);
-	imm = ldl_code(dc->pc + 2);
+	imm = ldl_code_p(&dc->phys_pc_start, dc->phys_pc + 2, dc->pc + 2);
 	DIS(fprintf (logfile, "lapc 0x%x, $r%u\n", imm + dc->pc, dc->op2));
 	gen_op_movl_T0_im (dc->pc + imm);
 	gen_movl_reg_T0[rd] ();
@@ -1999,7 +2001,7 @@ static unsigned int dec_jas_im(DisasCont
 {
 	uint32_t imm;
 
-	imm = ldl_code(dc->pc + 2);
+	imm = ldl_code_p(&dc->phys_pc_start, dc->phys_pc + 2, dc->pc + 2);
 
 	DIS(fprintf (logfile, "jas 0x%x\n", imm));
 	cris_cc_mask(dc, 0);
@@ -2016,7 +2018,7 @@ static unsigned int dec_jasc_im(DisasCon
 {
 	uint32_t imm;
 
-	imm = ldl_code(dc->pc + 2);
+	imm = ldl_code_p(&dc->phys_pc_start, dc->phys_pc + 2, dc->pc + 2);
 
 	DIS(fprintf (logfile, "jasc 0x%x\n", imm));
 	cris_cc_mask(dc, 0);
@@ -2047,7 +2049,7 @@ static unsigned int dec_bcc_im(DisasCont
 	int32_t offset;
 	uint32_t cond = dc->op2;
 
-	offset = ldl_code(dc->pc + 2);
+	offset = ldl_code_p(&dc->phys_pc_start, dc->phys_pc + 2, dc->pc + 2);
 	offset = sign_extend(offset, 15);
 
 	DIS(fprintf (logfile, "b%s %d pc=%x dst=%x\n",
@@ -2065,7 +2067,7 @@ static unsigned int dec_bas_im(DisasCont
 	int32_t simm;
 
 
-	simm = ldl_code(dc->pc + 2);
+	simm = ldl_code_p(&dc->phys_pc_start, dc->phys_pc + 2, dc->pc + 2);
 
 	DIS(fprintf (logfile, "bas 0x%x, $p%u\n", dc->pc + simm, dc->op2));
 	cris_cc_mask(dc, 0);
@@ -2081,7 +2083,7 @@ static unsigned int dec_bas_im(DisasCont
 static unsigned int dec_basc_im(DisasContext *dc)
 {
 	int32_t simm;
-	simm = ldl_code(dc->pc + 2);
+	simm = ldl_code_p(&dc->phys_pc_start, dc->phys_pc + 2, dc->pc + 2);
 
 	DIS(fprintf (logfile, "basc 0x%x, $p%u\n", dc->pc + simm, dc->op2));
 	cris_cc_mask(dc, 0);
@@ -2259,7 +2261,7 @@ cris_decoder(DisasContext *dc)
 	int i;
 
 	/* Load a halfword onto the instruction register.  */
-	tmp = ldl_code(dc->pc);
+	tmp = ldl_code_p(&dc->phys_pc_start, dc->phys_pc, dc->pc);
 	dc->ir = tmp & 0xffff;
 
 	/* Now decode it.  */
@@ -2313,6 +2315,9 @@ gen_intermediate_code_internal(CPUState 
 	uint32_t next_page_start;
 
 	pc_start = tb->pc;
+        dc->phys_pc_start = (unsigned long)phys_ram_base + tb->page_addr[0] +
+            (pc_start & ~TARGET_PAGE_MASK);
+        dc->phys_pc = dc->phys_pc_start;
 	dc->env = env;
 	dc->tb = tb;
 
@@ -2347,6 +2352,7 @@ gen_intermediate_code_internal(CPUState 
 		insn_len = cris_decoder(dc);
 		STATS(gen_op_exec_insn());
 		dc->pc += insn_len;
+                dc->phys_pc += insn_len;
 		if (!dc->flagx_live
 		    || (dc->flagx_live &&
 			!(dc->cc_op == CC_OP_FLAGS && dc->flags_x))) {

Index: target-i386/cpu.h
===================================================================
RCS file: /sources/qemu/qemu/target-i386/cpu.h,v
retrieving revision 1.51

cpu.h +2 -0
diff -u -d -d -p -r1.51 cpu.h
@@ -33,6 +33,8 @@
 /* support for self modifying code even if the modified instruction is
    close to the modifying instruction */
 #define TARGET_HAS_PRECISE_SMC
+/* need explicit support for instructions spanning 2 pages */
+#define TARGET_HAS_VLE_INSNS 1
 
 #define TARGET_HAS_ICE 1
 

Index: target-i386/translate.c
===================================================================
RCS file: /sources/qemu/qemu/target-i386/translate.c,v retrieving revision 1.72

translate.c +101 -74

651 lines not on this page · raw message

Index: target-m68k/cpu.h
===================================================================
RCS file: /sources/qemu/qemu/target-m68k/cpu.h,v
retrieving revision 1.14

cpu.h +2 -0
diff -u -d -d -p -r1.14 cpu.h
@@ -22,6 +22,8 @@
 #define CPU_M68K_H
 
 #define TARGET_LONG_BITS 32
+/* need explicit support for instructions spanning 2 pages */
+#define TARGET_HAS_VLE_INSNS 1
 
 #include "cpu-defs.h"
 

Index: target-m68k/translate.c
===================================================================
RCS file: /sources/qemu/qemu/target-m68k/translate.c,v retrieving revision 1.20

translate.c +70 -30

294 lines not on this page · raw message

Index: target-mips/translate.c
===================================================================
RCS file: /sources/qemu/qemu/target-mips/translate.c,v retrieving revision 1.106

translate.c +8 -1
diff -u -d -d -p -r1.106 translate.c
@@ -536,6 +536,7 @@ FOP_CONDS(abs, ps)
 typedef struct DisasContext {
     struct TranslationBlock *tb;
     target_ulong pc, saved_pc;
+    unsigned long phys_pc, phys_pc_start;
     uint32_t opcode;
     uint32_t fp_status;
     /* Routine used to access memory */
@@ -1764,6 +1765,7 @@ static void gen_compute_branch (DisasCon
             /* Skip the instruction in the delay slot */
             MIPS_DEBUG("bnever, link and skip");
             ctx->pc += 4;
+            ctx->phys_pc += 4;
             return;
         case OPC_BNEL:    /* rx != rx likely */
         case OPC_BGTZL:   /* 0 > 0 likely */
@@ -1771,6 +1773,7 @@ static void gen_compute_branch (DisasCon
             /* Skip the instruction in the delay slot */
             MIPS_DEBUG("bnever and skip");
             ctx->pc += 4;
+            ctx->phys_pc += 4;
             return;
         case OPC_J:
             ctx->hflags |= MIPS_HFLAG_B;
@@ -6495,6 +6498,9 @@ gen_intermediate_code_internal (CPUState
     gen_opparam_ptr = gen_opparam_buf;
     nb_gen_labels = 0;
     ctx.pc = pc_start;
+    ctx.phys_pc_start = (unsigned long)phys_ram_base + tb->page_addr[0] +
+        (pc_start & ~TARGET_PAGE_MASK);
+    ctx.phys_pc = ctx.phys_pc_start;
     ctx.saved_pc = -1;
     ctx.tb = tb;
     ctx.bstate = BS_NONE;
@@ -6544,9 +6550,10 @@ gen_intermediate_code_internal (CPUState
             gen_opc_hflags[lj] = ctx.hflags & MIPS_HFLAG_BMASK;
             gen_opc_instr_start[lj] = 1;
         }
-        ctx.opcode = ldl_code(ctx.pc);
+        ctx.opcode = ldl_code_p(&ctx.phys_pc_start, ctx.phys_pc, ctx.pc);
         decode_opc(env, &ctx);
         ctx.pc += 4;
+        ctx.phys_pc += 4;
 
         if (env->singlestep_enabled)
             break;

Index: target-ppc/cpu.h
===================================================================
RCS file: /sources/qemu/qemu/target-ppc/cpu.h,v
retrieving revision 1.83

cpu.h +2 -0
diff -u -d -d -p -r1.83 cpu.h
@@ -37,6 +37,8 @@ typedef uint64_t ppc_gpr_t;
 #define TARGET_GPR_BITS  64
 #define TARGET_LONG_BITS 32
 #define REGX "%016" PRIx64
+/* need explicit support for instructions spanning 2 pages for VLE code */
+#define TARGET_HAS_VLE_INSNS 1
 #if defined(CONFIG_USER_ONLY)
 /* It looks like a lot of Linux programs assume page size
  * is 4kB long. This is evil, but we have to deal with it...

Index: target-ppc/translate.c
===================================================================
RCS file: /sources/qemu/qemu/target-ppc/translate.c,v retrieving revision 1.93

translate.c +6 -1
diff -u -d -d -p -r1.93 translate.c
@@ -6678,6 +6678,7 @@ static always_inline int gen_intermediat
 {
     DisasContext ctx, *ctxp = &ctx;
     opc_handler_t **table, *handler;
+    unsigned long phys_pc, phys_pc_start;
     target_ulong pc_start;
     uint16_t *gen_opc_end;
     int supervisor;
@@ -6685,6 +6686,9 @@ static always_inline int gen_intermediat
     int j, lj = -1;
 
     pc_start = tb->pc;
+    phys_pc_start = (unsigned long)phys_ram_base + tb->page_addr[0] +
+        (pc_start & ~TARGET_PAGE_MASK);
+    phys_pc = phys_pc_start;
     gen_opc_ptr = gen_opc_buf;
     gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
     gen_opparam_ptr = gen_opparam_buf;
@@ -6756,7 +6760,7 @@ static always_inline int gen_intermediat
                     ctx.nip, 1 - msr_pr, msr_ir);
         }
 #endif
-        ctx.opcode = ldl_code(ctx.nip);
+        ctx.opcode = ldl_code_p(&phys_pc_start, phys_pc, ctx.nip);
         if (msr_le) {
             ctx.opcode = ((ctx.opcode & 0xFF000000) >> 24) |
                 ((ctx.opcode & 0x00FF0000) >> 8) |
@@ -6771,6 +6775,7 @@ static always_inline int gen_intermediat
         }
 #endif
         ctx.nip += 4;
+        phys_pc += 4;
         table = env->opcodes;
         handler = table[opc1(ctx.opcode)];
         if (is_indirect_opcode(handler)) {

Index: target-sh4/translate.c
===================================================================
RCS file: /sources/qemu/qemu/target-sh4/translate.c,v retrieving revision 1.18

translate.c +6 -1
diff -u -d -d -p -r1.18 translate.c
@@ -1150,11 +1150,15 @@ gen_intermediate_code_internal(CPUState 
 {
     DisasContext ctx;
     target_ulong pc_start;
+    unsigned long phys_pc, phys_pc_start;
     static uint16_t *gen_opc_end;
     uint32_t old_flags;
     int i, ii;
 
     pc_start = tb->pc;
+    phys_pc_start = (unsigned long)phys_ram_base + tb->page_addr[0] +
+        (pc_start & ~TARGET_PAGE_MASK);
+    phys_pc = phys_pc_start;
     gen_opc_ptr = gen_opc_buf;
     gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
     gen_opparam_ptr = gen_opparam_buf;
@@ -1210,9 +1214,10 @@ gen_intermediate_code_internal(CPUState 
 	fprintf(stderr, "Loading opcode at address 0x%08x\n", ctx.pc);
 	fflush(stderr);
 #endif
-	ctx.opcode = lduw_code(ctx.pc);
+	ctx.opcode = lduw_code_p(&phys_pc_start, phys_pc, ctx.pc);
 	decode_opc(&ctx);
 	ctx.pc += 2;
+        phys_pc += 2;
 	if ((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0)
 	    break;
 	if (env->singlestep_enabled)

Index: target-sparc/translate.c
===================================================================
RCS file: /sources/qemu/qemu/target-sparc/translate.c,v retrieving revision 1.75

translate.c +7 -1
diff -u -d -d -p -r1.75 translate.c
@@ -48,6 +48,8 @@ typedef struct DisasContext {
     target_ulong pc;    /* current Program Counter: integer or DYNAMIC_PC */
     target_ulong npc;   /* next PC: integer or DYNAMIC_PC or JUMP_PC */
     target_ulong jump_pc[2]; /* used when JUMP_PC pc value is used */
+    unsigned long phys_pc;
+    unsigned long phys_pc_start;
     int is_br;
     int mem_idx;
     int fpu_enabled;
@@ -1089,7 +1091,7 @@ static void disas_sparc_insn(DisasContex
 {
     unsigned int insn, opc, rs1, rs2, rd;
 
-    insn = ldl_code(dc->pc);
+    insn = ldl_code_p(&dc->phys_pc_start, dc->phys_pc, dc->pc);
     opc = GET_FIELD(insn, 0, 1);
 
     rd = GET_FIELD(insn, 2, 6);
@@ -3376,6 +3378,9 @@ static inline int gen_intermediate_code_
     dc->tb = tb;
     pc_start = tb->pc;
     dc->pc = pc_start;
+    dc->phys_pc_start = (unsigned long)phys_ram_base + tb->page_addr[0] +
+        (pc_start & ~TARGET_PAGE_MASK);
+    dc->phys_pc = dc->phys_pc_start;
     last_pc = dc->pc;
     dc->npc = (target_ulong) tb->cs_base;
 #if defined(CONFIG_USER_ONLY)
@@ -3422,6 +3427,7 @@ static inline int gen_intermediate_code_
             }
         }
         last_pc = dc->pc;
+        dc->phys_pc = dc->phys_pc_start + dc->pc - pc_start;
         disas_sparc_insn(dc);
 
         if (dc->is_br)
#1 lore raw

Review

Quoted 10 lines · J. Mayer

Here's an updated version of the code fetch optimisation patch against current CVS.
As a remainder, this patch avoid use of softmmu helpers to fetch the code in most case. A new target define TARGET_HAS_VLE_INSNS has been added which is used to handle the case of an instruction that span 2 pages, when the target CPU uses a variable-length instructions encoding. For pure RISC, the code fetch is done using raw access routines.

(quoted diff) +2 -0
+    unsigned long phys_pc;
+    unsigned long phys_pc_start;

These are ram offsets, not physical addresses. I recommend naming them as such to avoid confusion.

softmmu_header.h static inline void glue(glue(st, SUFFIX) · line 314

Quoted 7 lines
softmmu_header.h +7 -0
+    opc = glue(glue(lds,SUFFIX),MEMSUFFIX)(virt_pc);
+    /* Avoid softmmu access on next load */
+    /* XXX: dont: phys PC is not correct anymore
+     *      We could call get_phys_addr_code(env, pc); and remove the else
+     *      condition, here. 
+     */
+    //*start_pc = phys_pc;

The commented out code is completely bogus, please remove it. The comment is also somewhat misleading/incorrect. The else would still be required for accesses that span a page boundary.

The code itself looks ok, though I'd be surprised if it made a significant difference. We're always going to hit the fast-path TLB lookup case anyway.

Paul

author
#2 lore raw
Quoted 9 lines · Paul Brook
Quoted 7 lines · J. Mayer

Here's an updated version of the code fetch optimisation patch against current CVS.
As a remainder, this patch avoid use of softmmu helpers to fetch the code in most case. A new target define TARGET_HAS_VLE_INSNS has been added which is used to handle the case of an instruction that span 2 pages, when the target CPU uses a variable-length instructions encoding. For pure RISC, the code fetch is done using raw access routines.

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+    unsigned long phys_pc;
+    unsigned long phys_pc_start;

These are ram offsets, not physical addresses. I recommend naming them as such to avoid confusion.

Well, those are host addresses. Fabrice even suggested me to replace them with void * to prevent confusion, but I kept using unsigned long because the _p functions API do not use pointers. As those values are defined as phys_ram_base + offset, those are likely to be host address, not RAM offset, and are used directly to dereference host pointers in the ldxxx_p functions. Did I miss something ?

Quoted 11 lines

softmmu_header.h static inline void glue(glue(st, SUFFIX) · line 314

Quoted 7 lines
softmmu_header.h +7 -0
+    opc = glue(glue(lds,SUFFIX),MEMSUFFIX)(virt_pc);
+    /* Avoid softmmu access on next load */
+    /* XXX: dont: phys PC is not correct anymore
+     *      We could call get_phys_addr_code(env, pc); and remove the else
+     *      condition, here. 
+     */
+    //*start_pc = phys_pc;

The commented out code is completely bogus, please remove it. The comment is also somewhat misleading/incorrect. The else would still be required for accesses that span a page boundary.

I guess trying to optimize this case retrieving the physical address would not bring any optimization as in fact only the last translated instruction of a TB (then only a few code loads) may hit this case. I'd like to keep a comment here to show that it may not be a good idea (or may not be as simple as it seems at first sight) to try to do more optimisation here, but you're right this comment is not correct.

Quoted 2 lines

The code itself looks ok, though I'd be surprised if it made a significant difference. We're always going to hit the fast-path TLB lookup case anyway.

It seems that the generated code for the code fetch is much more efficient than the one generated when we get when using the softmmu routines. But it's true we do not get any significant performance boost. As it was previously mentioned, the idea of the patch is more a 'don't do unneeded things during code translation' than a great performance improvment.

Signature · 2 lines
J. Mayer <[email protected]>
Never organized
#3 lore raw
Quoted 6 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+    unsigned long phys_pc;
+    unsigned long phys_pc_start;

These are ram offsets, not physical addresses. I recommend naming them as such to avoid confusion.

Quoted 6 lines

Well, those are host addresses. Fabrice even suggested me to replace them with void * to prevent confusion, but I kept using unsigned long because the _p functions API do not use pointers. As those values are defined as phys_ram_base + offset, those are likely to be host address, not RAM offset, and are used directly to dereference host pointers in the ldxxx_p functions. Did I miss something ?

You are correct, they are host addresses. I still think calling them phys_pc is confusing. It took me a while to convince myself that "unsigned long" was an appropriate type (ignoring 64-bit windows hosts for now).

How about host_pc?

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

+    /* Avoid softmmu access on next load */
+    /* XXX: dont: phys PC is not correct anymore
+     *      We could call get_phys_addr_code(env, pc); and remove the else +     *      condition, here.
+     */
+    //*start_pc = phys_pc;

Quoted 7 lines
Quoted 3 lines

The commented out code is completely bogus, please remove it. The comment is also somewhat misleading/incorrect. The else would still be required for accesses that span a page boundary.

I guess trying to optimize this case retrieving the physical address would not bring any optimization as in fact only the last translated instruction of a TB (then only a few code loads) may hit this case.

VLE targets (x86, m68k) can translate almost a full page of instructions, and a page boundary can be anywhere within that block. Once we've spanned multiple pages there's not point stopping translation immediately. We may as well translate as many instructions as we can on the second page.

I'd guess most TB are much smaller than a page, so on average only a few instructions are going to come after the page boundary.

Quoted 3 lines

I'd like to keep a comment here to show that it may not be a good idea (or may not be as simple as it seems at first sight) to try to do more optimisation here, but you're right this comment is not correct.

Agreed.

Quoted 10 lines
Quoted 3 lines

The code itself looks ok, though I'd be surprised if it made a significant difference. We're always going to hit the fast-path TLB lookup case anyway.

It seems that the generated code for the code fetch is much more efficient than the one generated when we get when using the softmmu routines. But it's true we do not get any significant performance boost. As it was previously mentioned, the idea of the patch is more a 'don't do unneeded things during code translation' than a great performance improvment.

OTOH it does make the the code more complicated. I'm agnostic about whether this patch should be applied.

Paul

#4 lore raw
Quoted 13 lines · Paul Brook

[...]

Quoted 10 lines
Quoted 3 lines

The code itself looks ok, though I'd be surprised if it made a significant difference. We're always going to hit the fast-path TLB lookup case anyway.

It seems that the generated code for the code fetch is much more efficient than the one generated when we get when using the softmmu routines. But it's true we do not get any significant performance boost. As it was previously mentioned, the idea of the patch is more a 'don't do unneeded things during code translation' than a great performance improvment.

Quoted 1 line

OTOH it does make the the code more complicated. I'm agnostic about whether this patch should be applied.

If it does not correct the existing x86 issues (no code segment limit tests and no explicit handling of code fetch exceptions in the translation phase in VLE case) I see no advantage in commiting it in its current form.

Regards,

Fabrice.

author
#5 lore raw
In reply to Paul Brook
Quoted 13 lines · Paul Brook
Quoted 6 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+    unsigned long phys_pc;
+    unsigned long phys_pc_start;

These are ram offsets, not physical addresses. I recommend naming them as such to avoid confusion.

Quoted 6 lines

Well, those are host addresses. Fabrice even suggested me to replace them with void * to prevent confusion, but I kept using unsigned long because the _p functions API do not use pointers. As those values are defined as phys_ram_base + offset, those are likely to be host address, not RAM offset, and are used directly to dereference host pointers in the ldxxx_p functions. Did I miss something ?

Quoted 5 lines

You are correct, they are host addresses. I still think calling them phys_pc is confusing. It took me a while to convince myself that "unsigned long" was an appropriate type (ignoring 64-bit windows hosts for now).

How about host_pc?

It's OK for me.

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
softmmu_header.h +3 -0
+    /* Avoid softmmu access on next load */
+    /* XXX: dont: phys PC is not correct anymore
+     *      We could call get_phys_addr_code(env, pc); and remove the

else + * condition, here.

(quoted diff) +2 -0
+     */
+    //*start_pc = phys_pc;
Quoted 7 lines
Quoted 3 lines

The commented out code is completely bogus, please remove it. The comment is also somewhat misleading/incorrect. The else would still be required for accesses that span a page boundary.

I guess trying to optimize this case retrieving the physical address would not bring any optimization as in fact only the last translated instruction of a TB (then only a few code loads) may hit this case.

Quoted 7 lines

VLE targets (x86, m68k) can translate almost a full page of instructions, and a page boundary can be anywhere within that block. Once we've spanned multiple pages there's not point stopping translation immediately. We may as well translate as many instructions as we can on the second page.

I'd guess most TB are much smaller than a page, so on average only a few instructions are going to come after the page boundary.

This leads me to another reflexion. For fixed length encoding targets, we always stop translation when reaching a page boundary. If we keep using the current model and we optimize the slow case, it would be possible to stop only if we cross 2 pages boundary during code translation, and it seems that this case is not likely to happen. If we keep the current behavior, we could remove the second page_addr element in the tb structure and maybe optimize parts of the tb management and invalidation code.

Quoted 17 lines
Quoted 3 lines

I'd like to keep a comment here to show that it may not be a good idea (or may not be as simple as it seems at first sight) to try to do more optimisation here, but you're right this comment is not correct.

Agreed.

Quoted 10 lines
Quoted 3 lines

The code itself looks ok, though I'd be surprised if it made a significant difference. We're always going to hit the fast-path TLB lookup case anyway.

It seems that the generated code for the code fetch is much more efficient than the one generated when we get when using the softmmu routines. But it's true we do not get any significant performance boost. As it was previously mentioned, the idea of the patch is more a 'don't do unneeded things during code translation' than a great performance improvment.

Quoted 2 lines

OTOH it does make the the code more complicated. I'm agnostic about whether this patch should be applied.

I agree that this proposal was an answer to a challenging idea that I received more than a real need.
The worst thing in this patch, imho, is that you need to increase 2 values each time you want to change the PC. This is likely to bring some bug when one will forgot to increase one of the two. I was thinking of hiding the pc, host_pc and host_pc_start (and maybe also pc_start) in a structure and add inline helpers:
* get_pc would return the current virtual PC, as needed by the jump and relative memory accesses functions.
* get_tb_len would return the difference between the virtual PC and the virtual pc_start, as it is done at the end of the gen_intermediate_code functions
* move_pc would add an offset to the virtual and the physical PC. This has to be target dependant, due to the special case for Sparc
* update_phys_pc would be void for most targets, except for Sparc where the phys_pc needs to be adjusted after the translation of each target instruction.
and maybe more, if needed.
This structure could also contain target specific information. To address the problem of segment limit check reported by Fabrice Bellard, we could for example add the address of the next segment limit for x86 target and add a target specific check at the start of the ldx_code_p function. But I don't know much about segmentation "subtilities" on x86, then this idea may not be appropriate to solve this problem.

Signature · 2 lines
J. Mayer <[email protected]>
Never organized
#6 lore raw
Quoted 8 lines
Quoted 7 lines

VLE targets (x86, m68k) can translate almost a full page of instructions, and a page boundary can be anywhere within that block. Once we've spanned multiple pages there's not point stopping translation immediately. We may as well translate as many instructions as we can on the second page.

I'd guess most TB are much smaller than a page, so on average only a few instructions are going to come after the page boundary.

Quoted 8 lines

This leads me to another reflexion. For fixed length encoding targets, we always stop translation when reaching a page boundary. If we keep using the current model and we optimize the slow case, it would be possible to stop only if we cross 2 pages boundary during code translation, and it seems that this case is not likely to happen. If we keep the current behavior, we could remove the second page_addr element in the tb structure and maybe optimize parts of the tb management and invalidation code.

The latter may be the only feasible option.

Some targets (ARMv5, maybe others) do not have an explicit fault address for MMU instruction faults. The faulting address is the address of the current instruction when the fault occurs. Prefetch aborts are generated at translation time, which effectively means the faulting instruction must be at the start of a TB. Terminating the TB on a page boundary guarantees this behavior.

For VLE targets we already get this wrong (the prefetch abort occurs some time before the faulting instruction executes). I don't know if this behavior is permitted by the ISA, but it's definitely possible to construct cases where it has visible effect.

Paul

author
#7 lore raw
Quoted 17 lines · Paul Brook
Quoted 8 lines
Quoted 7 lines

VLE targets (x86, m68k) can translate almost a full page of instructions, and a page boundary can be anywhere within that block. Once we've spanned multiple pages there's not point stopping translation immediately. We may as well translate as many instructions as we can on the second page.

I'd guess most TB are much smaller than a page, so on average only a few instructions are going to come after the page boundary.

Quoted 8 lines

This leads me to another reflexion. For fixed length encoding targets, we always stop translation when reaching a page boundary. If we keep using the current model and we optimize the slow case, it would be possible to stop only if we cross 2 pages boundary during code translation, and it seems that this case is not likely to happen. If we keep the current behavior, we could remove the second page_addr element in the tb structure and maybe optimize parts of the tb management and invalidation code.

Quoted 8 lines

The latter may be the only feasible option.

Some targets (ARMv5, maybe others) do not have an explicit fault address for MMU instruction faults. The faulting address is the address of the current instruction when the fault occurs. Prefetch aborts are generated at translation time, which effectively means the faulting instruction must be at the start of a TB. Terminating the TB on a page boundary guarantees this behavior.

Well, we got the same behavior on PowerPC. What I was thinking of is that if we fix the VLE problems, the fix, if done in a proper way, could also allow benefit to RISC targets. What I don't know is; would we really have a benefit not stopping translation on page boundaries ?

Quoted 4 lines

For VLE targets we already get this wrong (the prefetch abort occurs some time before the faulting instruction executes). I don't know if this behavior is permitted by the ISA, but it's definitely possible to construct cases where it has visible effect.

I think that it would be possible to do things properly. I'm not really sure what is the best solution to implement it but if, in the "slow case" path of the code fetch low-level routine, we call the get_physical_address or the cpu_get_phys_page_debug function, we then have a way to know if the code fetch is allowed. If it, we would just have to adjust our host_pc and host_pc_start for the next fetch to be optimized. If not, we could stop the translation and generate a "gen_op_raise_excp_error" to raise the exception at the right place, respecting the ISA insns execution ordering. Generating the exception from inside the TB won't be OK, as it may not be necessary on a second execution of the same TB, then the solution may be to link the TB with a special other TB that would just raise this exception and would be unlink once the exception has been treated. Or maybe the solution would just be to stop the translation knowing that the exception will be raised when trying to translate the first instruction in the next page. There still may be specific problems for instructions spanning 2 pages, using those solutions...

Signature · 2 lines
J. Mayer <[email protected]>
Never organized
#8 lore raw
Quoted 10 lines

Well, we got the same behavior on PowerPC. What I was thinking of is that if we fix the VLE problems, the fix, if done in a proper way, could also allow benefit to RISC targets. What I don't know is; would we really have a benefit not stopping translation on page boundaries ?

Quoted 4 lines

For VLE targets we already get this wrong (the prefetch abort occurs some time before the faulting instruction executes). I don't know if this behavior is permitted by the ISA, but it's definitely possible to construct cases where it has visible effect.

Quoted 4 lines

I think that it would be possible to do things properly. [...] Or maybe the solution would
just be to stop the translation knowing that the exception will be raised when trying to translate the first instruction in the next page.

I'd go for this one. It's approximately the same method currently used for RISC targets.

In general think this will require target specific support. For RISC targets this is trivial. For x86/m68k figuring out the length of an insn is trickier.

Detecting crossing a page boundary on subsequent insns in the load/mmu routines is problematic because it happens relatively late. In particular it may theoretically happen after we've output ops that change CPU state.

I suspect the best solution is to backtrack (remove the generated ops) after decoding the insn if we discover we've passed a page boundary. The ld*_code routines can simply return garbage (e.g. zero) if the read is not on the first page.

Trying to generate prefetch aborts at runtime sounds too hairy for my liking.

Paul

#11 lore raw
In reply to J. Mayer
Quoted 4 lines

Well, we got the same behavior on PowerPC. What I was thinking of is that if we fix the VLE problems, the fix, if done in a proper way, could also allow benefit to RISC targets. What I don't know is; would we really have a benefit not stopping translation on page boundaries ?

[ I meant to say in my previous mail, but got cut during editing ]

I suspect that we're going to want/need to break the TB to get the exception semantics right, so for RISC targets there's no point having TBs that span a page boundary.

Paul

author
#9 lore raw
In reply to Paul Brook
Quoted 9 lines · Paul Brook
Quoted 4 lines

Well, we got the same behavior on PowerPC. What I was thinking of is that if we fix the VLE problems, the fix, if done in a proper way, could also allow benefit to RISC targets. What I don't know is; would we really have a benefit not stopping translation on page boundaries ?

I suspect that we're going to want/need to break the TB to get the exception semantics right, so for RISC targets there's no point having TBs that span a
page boundary.

My opinion is that this an optimisation that may be tried later, if it really give an advantage in terms of translation efficiency, which is far from being evident. Then, let's keep what works well and just try to solve the VLE problems for now...

Quoted 16 lines
Quoted 9 lines
Quoted 4 lines

For VLE targets we already get this wrong (the prefetch abort occurs some time before the faulting instruction executes). I don't know if this behavior is permitted by the ISA, but it's definitely possible to construct cases where it has visible effect.

I think that it would be possible to do things properly. [...] Or maybe the solution would
just be to stop the translation knowing that the exception will be raised when trying to translate the first instruction in the next page.

I'd go for this one. It's approximately the same method currently used for RISC targets.

In general think this will require target specific support. For RISC targets this is trivial. For x86/m68k figuring out the length of an insn is trickier.

Quoted 8 lines

Detecting crossing a page boundary on subsequent insns in the load/mmu routines is problematic because it happens relatively late. In particular it may theoretically happen after we've output ops that change CPU state.

I suspect the best solution is to backtrack (remove the generated ops) after decoding the insn if we discover we've passed a page boundary. The ld*_code routines can simply return garbage (e.g. zero) if the read is not on the first page.

The "incorrect" returned value may be target specific to be sure it's always an invalid opcode.
Backtracking should not be hard if we register the last cc pointer each time we finish translating an insn. I'll think about this solution, which really seems feasible to me.

Quoted 1 line

Trying to generate prefetch aborts at runtime sounds too hairy for my liking.

It might be really tricky and is likely to be bugged, I agree.

Signature · 2 lines
J. Mayer <[email protected]>
Never organized
#10 lore raw
Quoted 7 lines
Quoted 4 lines

I suspect the best solution is to backtrack (remove the generated ops) after decoding the insn if we discover we've passed a page boundary. The ld*_code routines can simply return garbage (e.g. zero) if the read is not on the first page.

The "incorrect" returned value may be target specific to be sure it's always an invalid opcode.

It doesn't matter whether it's valid or not, and we've no way of guaranteeing that anyway. We just have to make sure we don't generate an infinitely long instruction.

On a related note, I notice that we don't enforce x86 instruction length limits.

Quoted 3 lines

Backtracking should not be hard if we register the last cc pointer each time we finish translating an insn. I'll think about this solution, which really seems feasible to me.

Right. You only have to worry about backtracking the state that's lives across insns and is not constant within a TB. For x86 I think this is dc->pc, dc->cc_op, gen_opc_ptr and nb_gen_labels. Plus you need to reset dc->is_jmp to zero. gen_opparam_ptr is not used after disassembly, so can be ignored.

Paul