Skip to contentSkip to conversation

RFC: Code fetch optimisation]

Fwd: Re: J. Mayer 3 participants 8 messages Oct 12, 2007 – Oct 13, 2007

lore
Share & tools
lore

J. Mayer

author
#0 lore raw

-------- Forwarded Message --------

Quoted 40 lines

From: Jocelyn Mayer <[email protected]>
Reply-To: [email protected], [email protected]
To: [email protected]
Subject: Re: [Qemu-devel] RFC: Code fetch optimisation
Date: Fri, 12 Oct 2007 20:24:44 +0200

Quoted 14 lines · Blue Swirl
Quoted 13 lines · J. Mayer

Here's a small patch that allow an optimisation for code fetch, at least for RISC CPU targets, as suggested by Fabrice Bellard.
The main idea is that a translated block is never to span over a page boundary. As the tb_find_slow routine already gets the physical address of the page of code to be translated, the code translator could then fetch the code using raw host memory accesses instead of doing it through the softmmu routines.
This patch could also be adapted to RISC CPU targets, with care for the last instruction of a page. For now, I did implement it for alpha, arm, mips, PowerPC and SH4.
I don't actually know if the optimsation would bring a sensible speed gain or if it will be absolutelly marginal.

Quoted 4 lines
Quoted 1 line

Please comment.

This will not work correctly for execution of MMIO registers, but maybe that won't work on real hardware either. Who cares.

I wonder if this is important or not... But maybe, when retrieving the physical address we could check if it is inside ROM/RAM or an I/O area and in the last case do not give the phys_addr information to the translator. In that case, it would go on using the ldxx_code. I guess if we want to do that, a set of helpers would be appreciated to avoid adding code like:
if (phys_pc == 0)
opc = ldul_code(virt_pc)
else
opc = ldul_raw(phys_pc)
everywhere... I could also add another check so this set of macro would automatically use ldxx_code if we reach a page boundary, which would then make easy to use this optimisation for CISC/VLE architectures too.

Quoted 3 lines

I'm not sure of the proper solution to allow executing code from mmio devices. But adding specific accessors to handle the CISC/VLE case is to be done.

[...]

I did update my patch following this way and it's now able to run x86 and PowerPC targets.
PowerPC is the easy case, x86 is maybe the worst... Well, I'm not really sure of what I've done for Sparc, but other targets should be safe.

Please comment.

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.119

cpu-exec.c +1 -0
diff -u -d -d -p -r1.119 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.17

softmmu_header.h +54 -0

60 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.5

translate.c +6 -1
diff -u -d -d -p -r1.5 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/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/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/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/translate.c
===================================================================
RCS file: /sources/qemu/qemu/target-ppc/translate.c,v retrieving revision 1.92

translate.c +6 -1
diff -u -d -d -p -r1.92 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;
@@ -6763,7 +6767,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, env->nip);
         if (msr_le) {
             ctx.opcode = ((ctx.opcode & 0xFF000000) >> 24) |
                 ((ctx.opcode & 0x00FF0000) >> 8) |
@@ -6778,6 +6782,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.74

translate.c +8 -1
diff -u -d -d -p -r1.74 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);
@@ -3319,6 +3321,7 @@ static void disas_sparc_insn(DisasContex
     }
     /* default case for non jump instructions */
     if (dc->npc == DYNAMIC_PC) {
+        dc->phys_pc += DYNAMIC_PC - dc->pc;
         dc->pc = DYNAMIC_PC;
         gen_op_next_insn();
     } else if (dc->npc == JUMP_PC) {
@@ -3326,6 +3329,7 @@ static void disas_sparc_insn(DisasContex
         gen_branch2(dc, dc->jump_pc[0], dc->jump_pc[1]);
         dc->is_br = 1;
     } else {
+        dc->phys_pc += dc->npc - dc->pc;
         dc->pc = dc->npc;
         dc->npc = dc->npc + 4;
     }
@@ -3376,6 +3380,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)
#1 lore raw
Quoted 45 lines · J. Mayer

-------- Forwarded Message --------

Quoted 40 lines

From: Jocelyn Mayer <[email protected]>
Reply-To: [email protected], [email protected]
To: [email protected]
Subject: Re: [Qemu-devel] RFC: Code fetch optimisation
Date: Fri, 12 Oct 2007 20:24:44 +0200

Quoted 14 lines · Blue Swirl
Quoted 13 lines · J. Mayer

Here's a small patch that allow an optimisation for code fetch, at least for RISC CPU targets, as suggested by Fabrice Bellard.
The main idea is that a translated block is never to span over a page boundary. As the tb_find_slow routine already gets the physical address of the page of code to be translated, the code translator could then fetch the code using raw host memory accesses instead of doing it through the softmmu routines.
This patch could also be adapted to RISC CPU targets, with care for the last instruction of a page. For now, I did implement it for alpha, arm, mips, PowerPC and SH4.
I don't actually know if the optimsation would bring a sensible speed gain or if it will be absolutelly marginal.

Quoted 4 lines
Quoted 1 line

Please comment.

This will not work correctly for execution of MMIO registers, but maybe that won't work on real hardware either. Who cares.

I wonder if this is important or not... But maybe, when retrieving the physical address we could check if it is inside ROM/RAM or an I/O area and in the last case do not give the phys_addr information to the translator. In that case, it would go on using the ldxx_code. I guess if we want to do that, a set of helpers would be appreciated to avoid adding code like:
if (phys_pc == 0)
opc = ldul_code(virt_pc)
else
opc = ldul_raw(phys_pc)
everywhere... I could also add another check so this set of macro would automatically use ldxx_code if we reach a page boundary, which would then make easy to use this optimisation for CISC/VLE architectures too.

Quoted 3 lines

I'm not sure of the proper solution to allow executing code from mmio devices. But adding specific accessors to handle the CISC/VLE case is to be done.

Quoted 6 lines

[...]

I did update my patch following this way and it's now able to run x86 and PowerPC targets.
PowerPC is the easy case, x86 is maybe the worst... Well, I'm not really sure of what I've done for Sparc, but other targets should be safe.

It broke Sparc, delay slot handling makes things complicated. The updated patch passes my tests.

For extra performance, I bypassed the ldl_code_p. On Sparc, instructions can't be split between two pages. Isn't translation always contained to the same page for all targets like Sparc?

cpu-all.h +7 -0
Index: qemu/cpu-all.h
===================================================================
@@ -646,6 +646,13 @@
 #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)
cpu-exec.c +1 -0
Index: qemu/cpu-exec.c
===================================================================
@@ -133,6 +133,7 @@
     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));
 
softmmu_header.h +54 -0

60 lines not on this page · raw message

target-alpha/translate.c +6 -1
Index: qemu/target-alpha/translate.c
===================================================================
@@ -1965,6 +1965,7 @@
     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 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 @@
                     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 @@
         }
 #endif
         ctx.pc += 4;
+        phys_pc += 4;
         ret = translate_one(ctxp, insn);
         if (ret != 0)
             break;
target-arm/translate.c +10 -3
Index: qemu/target-arm/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 @@
 {
     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 @@
     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 @@
             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 @@
 
     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;
target-cris/translate.c +14 -8
Index: qemu/target-cris/translate.c
===================================================================
@@ -100,6 +100,7 @@
 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 @@
 		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 @@
 	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 @@
 {
 	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 @@
 {
 	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 @@
 	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 @@
 	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_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 @@
 	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 @@
 	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 @@
 		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))) {
target-i386/translate.c +101 -74

651 lines not on this page · raw message

target-m68k/translate.c +70 -30

294 lines not on this page · raw message

target-mips/translate.c +8 -1
Index: qemu/target-mips/translate.c
===================================================================
@@ -536,6 +536,7 @@
 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 @@
             /* 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 @@
             /* 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_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_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;
target-ppc/translate.c +6 -1
Index: qemu/target-ppc/translate.c
===================================================================
@@ -6678,6 +6678,7 @@
 {
     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 @@
     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;
@@ -6763,7 +6767,7 @@
                     ctx.nip, 1 - msr_pr, msr_ir);
         }
 #endif
-        ctx.opcode = ldl_code(ctx.nip);
+        ctx.opcode = ldl_code_p(&phys_pc_start, phys_pc, env->nip);
         if (msr_le) {
             ctx.opcode = ((ctx.opcode & 0xFF000000) >> 24) |
                 ((ctx.opcode & 0x00FF0000) >> 8) |
@@ -6778,6 +6782,7 @@
         }
 #endif
         ctx.nip += 4;
+        phys_pc += 4;
         table = env->opcodes;
         handler = table[opc1(ctx.opcode)];
         if (is_indirect_opcode(handler)) {
target-sh4/translate.c +6 -1
Index: qemu/target-sh4/translate.c
===================================================================
@@ -1150,11 +1150,15 @@
 {
     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 @@
 	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)
target-sparc/translate.c +10 -1
Index: qemu/target-sparc/translate.c
===================================================================
@@ -48,6 +48,8 @@
     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,11 @@
 {
     unsigned int insn, opc, rs1, rs2, rd;
 
-    insn = ldl_code(dc->pc);
+#if defined(CONFIG_USER_ONLY)
+    insn = ldl_raw(dc->pc);
+#else
+    insn = ldl_raw(dc->phys_pc);
+#endif
     opc = GET_FIELD(insn, 0, 1);
 
     rd = GET_FIELD(insn, 2, 6);
@@ -3376,6 +3382,8 @@
     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);
     last_pc = dc->pc;
     dc->npc = (target_ulong) tb->cs_base;
 #if defined(CONFIG_USER_ONLY)
@@ -3422,6 +3430,7 @@
             }
         }
         last_pc = dc->pc;
+        dc->phys_pc = dc->phys_pc_start + dc->pc - pc_start;
         disas_sparc_insn(dc);
 
         if (dc->is_br)
author
#2 lore raw
Quoted 48 lines · Blue Swirl
Quoted 47 lines · J. Mayer

-------- Forwarded Message --------

Quoted 43 lines

From: Jocelyn Mayer <[email protected]>
Reply-To: [email protected], [email protected]
To: [email protected]
Subject: Re: [Qemu-devel] RFC: Code fetch optimisation
Date: Fri, 12 Oct 2007 20:24:44 +0200

Quoted 14 lines · Blue Swirl
Quoted 13 lines · J. Mayer

Here's a small patch that allow an optimisation for code fetch, at least for RISC CPU targets, as suggested by Fabrice Bellard.
The main idea is that a translated block is never to span over a page boundary. As the tb_find_slow routine already gets the physical address of the page of code to be translated, the code translator could then fetch the code using raw host memory accesses instead of doing it through the softmmu routines.
This patch could also be adapted to RISC CPU targets, with care for the last instruction of a page. For now, I did implement it for alpha, arm, mips, PowerPC and SH4.
I don't actually know if the optimsation would bring a sensible speed gain or if it will be absolutelly marginal.

Quoted 4 lines
Quoted 1 line

Please comment.

This will not work correctly for execution of MMIO registers, but maybe that won't work on real hardware either. Who cares.

I wonder if this is important or not... But maybe, when retrieving the physical address we could check if it is inside ROM/RAM or an I/O area and in the last case do not give the phys_addr information to the translator. In that case, it would go on using the ldxx_code. I guess if we want to do that, a set of helpers would be appreciated to avoid adding code like:
if (phys_pc == 0)
opc = ldul_code(virt_pc)
else
opc = ldul_raw(phys_pc)
everywhere... I could also add another check so this set of macro would automatically use ldxx_code if we reach a page boundary, which would then make easy to use this optimisation for CISC/VLE architectures too.

I'm not sure of the proper solution to allow executing code from mmio devices. But adding specific accessors to handle the CISC/VLE case is to be done.

[...]

Quoted 7 lines
Quoted 4 lines

I did update my patch following this way and it's now able to run x86 and PowerPC targets.
PowerPC is the easy case, x86 is maybe the worst... Well, I'm not really sure of what I've done for Sparc, but other targets should be safe.

It broke Sparc, delay slot handling makes things complicated. The updated patch passes my tests.

OK. I will take a look of how you solved this issue.

Quoted 3 lines

For extra performance, I bypassed the ldl_code_p. On Sparc, instructions can't be split between two pages. Isn't translation always contained to the same page for all targets like Sparc?

Yes, for RISC targets running 32 bits mode, we always stop translation when we reach the end of a code page. The problem comes with CISC architectures, like x86 or m68k, or RISC architecture running 16/32 bits code, like ARM in thumb mode or PowerPC in VLE mode. In all those case, there can be instructions spanning on 2 pages, then we need the ldx_code_p functions.
My idea of always using the ldx_code_p function is that we may have the occasion to make it more cleaver and make the slow case handle code execution in mmio areas, when it will be possible.

Signature · 2 lines
J. Mayer <[email protected]>
Never organized
author
#3 lore raw
Quoted 63 lines · J. Mayer
Quoted 48 lines · Blue Swirl
Quoted 47 lines · J. Mayer

-------- Forwarded Message --------

Quoted 43 lines

From: Jocelyn Mayer <[email protected]>
Reply-To: [email protected], [email protected]
To: [email protected]
Subject: Re: [Qemu-devel] RFC: Code fetch optimisation
Date: Fri, 12 Oct 2007 20:24:44 +0200

Quoted 14 lines · Blue Swirl
Quoted 13 lines · J. Mayer

Here's a small patch that allow an optimisation for code fetch, at least for RISC CPU targets, as suggested by Fabrice Bellard.
The main idea is that a translated block is never to span over a page boundary. As the tb_find_slow routine already gets the physical address of the page of code to be translated, the code translator could then fetch the code using raw host memory accesses instead of doing it through the softmmu routines.
This patch could also be adapted to RISC CPU targets, with care for the last instruction of a page. For now, I did implement it for alpha, arm, mips, PowerPC and SH4.
I don't actually know if the optimsation would bring a sensible speed gain or if it will be absolutelly marginal.

Quoted 4 lines
Quoted 1 line

Please comment.

This will not work correctly for execution of MMIO registers, but maybe that won't work on real hardware either. Who cares.

I wonder if this is important or not... But maybe, when retrieving the physical address we could check if it is inside ROM/RAM or an I/O area and in the last case do not give the phys_addr information to the translator. In that case, it would go on using the ldxx_code. I guess if we want to do that, a set of helpers would be appreciated to avoid adding code like:
if (phys_pc == 0)
opc = ldul_code(virt_pc)
else
opc = ldul_raw(phys_pc)
everywhere... I could also add another check so this set of macro would automatically use ldxx_code if we reach a page boundary, which would then make easy to use this optimisation for CISC/VLE architectures too.

I'm not sure of the proper solution to allow executing code from mmio devices. But adding specific accessors to handle the CISC/VLE case is to be done.

[...]

Quoted 7 lines
Quoted 4 lines

I did update my patch following this way and it's now able to run x86 and PowerPC targets.
PowerPC is the easy case, x86 is maybe the worst... Well, I'm not really sure of what I've done for Sparc, but other targets should be safe.

It broke Sparc, delay slot handling makes things complicated. The updated patch passes my tests.

OK. I will take a look of how you solved this issue.

Quoted 3 lines

For extra performance, I bypassed the ldl_code_p. On Sparc, instructions can't be split between two pages. Isn't translation always contained to the same page for all targets like Sparc?

Quoted 9 lines

Yes, for RISC targets running 32 bits mode, we always stop translation when we reach the end of a code page. The problem comes with CISC architectures, like x86 or m68k, or RISC architecture running 16/32 bits code, like ARM in thumb mode or PowerPC in VLE mode. In all those case, there can be instructions spanning on 2 pages, then we need the ldx_code_p functions.
My idea of always using the ldx_code_p function is that we may have the occasion to make it more cleaver and make the slow case handle code execution in mmio areas, when it will be possible.

Here's an updated patch. I added a definition TARGET_HAS_VLE_INSNS which is defined is the cris, i386, m68k and ppcemb cases. Arm already has an explicit support for 32 bits thumb instructions spanning 2 pages, so it should not need this define. When this define is not set, the ldxxx_code_p function just does ldxxx_raw(phys_pc) in the softmmu case and ldxxx_raw(pc) in the user-mode only case. This is optimal for pure RISC architectures and does not need the #ifdef CONFIG_USER_ONLY you added for Sparc in your patch version. I also added a provision for a TARGET_MMIO_CODE define which may be used later when this will really be supported by Qemu.
I also took your fixes for Sparc phys_pc computation, but reversed your patch to use ldl_raw as it should not be needed anymore.
I did test PowerPC in user-mode only and softmmu mode and i386 in softmmu successfully using this new version of the patch.

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.119

cpu-exec.c +1 -0
diff -u -d -d -p -r1.119 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.17

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.5

translate.c +6 -1
diff -u -d -d -p -r1.5 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 +2018,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 +2026,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.1

cpu.h +2 -0
diff -u -d -d -p -r1.1 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 +2356,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.50

cpu.h +2 -0
diff -u -d -d -p -r1.50 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.13

cpu.h +2 -0
diff -u -d -d -p -r1.13 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 +6554,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.79

cpu.h +2 -0
diff -u -d -d -p -r1.79 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.92

translate.c +6 -1
diff -u -d -d -p -r1.92 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;
@@ -6763,7 +6771,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) |
@@ -6778,6 +6786,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 +1218,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.74

translate.c +6 -1
diff -u -d -d -p -r1.74 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,8 @@ 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);
     last_pc = dc->pc;
     dc->npc = (target_ulong) tb->cs_base;
 #if defined(CONFIG_USER_ONLY)
@@ -3422,6 +3431,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)
#4 lore raw
Quoted 74 lines · J. Mayer
Quoted 63 lines · J. Mayer
Quoted 48 lines · Blue Swirl
Quoted 47 lines · J. Mayer

-------- Forwarded Message --------

Quoted 43 lines

From: Jocelyn Mayer <[email protected]>
Reply-To: [email protected], [email protected]
To: [email protected]
Subject: Re: [Qemu-devel] RFC: Code fetch optimisation
Date: Fri, 12 Oct 2007 20:24:44 +0200

Quoted 14 lines · Blue Swirl
Quoted 13 lines · J. Mayer

Here's a small patch that allow an optimisation for code fetch, at least for RISC CPU targets, as suggested by Fabrice Bellard.
The main idea is that a translated block is never to span over a page boundary. As the tb_find_slow routine already gets the physical address of the page of code to be translated, the code translator could then fetch the code using raw host memory accesses instead of doing it through the softmmu routines.
This patch could also be adapted to RISC CPU targets, with care for the last instruction of a page. For now, I did implement it for alpha, arm, mips, PowerPC and SH4.
I don't actually know if the optimsation would bring a sensible speed gain or if it will be absolutelly marginal.

Quoted 4 lines
Quoted 1 line

Please comment.

This will not work correctly for execution of MMIO registers, but maybe that won't work on real hardware either. Who cares.

I wonder if this is important or not... But maybe, when retrieving the physical address we could check if it is inside ROM/RAM or an I/O area and in the last case do not give the phys_addr information to the translator. In that case, it would go on using the ldxx_code. I guess if we want to do that, a set of helpers would be appreciated to avoid adding code like:
if (phys_pc == 0)
opc = ldul_code(virt_pc)
else
opc = ldul_raw(phys_pc)
everywhere... I could also add another check so this set of macro would automatically use ldxx_code if we reach a page boundary, which would then make easy to use this optimisation for CISC/VLE architectures too.

I'm not sure of the proper solution to allow executing code from mmio devices. But adding specific accessors to handle the CISC/VLE case is to be done.

[...]

Quoted 7 lines
Quoted 4 lines

I did update my patch following this way and it's now able to run x86 and PowerPC targets.
PowerPC is the easy case, x86 is maybe the worst... Well, I'm not really sure of what I've done for Sparc, but other targets should be safe.

It broke Sparc, delay slot handling makes things complicated. The updated patch passes my tests.

OK. I will take a look of how you solved this issue.

Quoted 3 lines

For extra performance, I bypassed the ldl_code_p. On Sparc, instructions can't be split between two pages. Isn't translation always contained to the same page for all targets like Sparc?

Quoted 9 lines

Yes, for RISC targets running 32 bits mode, we always stop translation when we reach the end of a code page. The problem comes with CISC architectures, like x86 or m68k, or RISC architecture running 16/32 bits code, like ARM in thumb mode or PowerPC in VLE mode. In all those case, there can be instructions spanning on 2 pages, then we need the ldx_code_p functions.
My idea of always using the ldx_code_p function is that we may have the occasion to make it more cleaver and make the slow case handle code execution in mmio areas, when it will be possible.

Quoted 14 lines

Here's an updated patch. I added a definition TARGET_HAS_VLE_INSNS which is defined is the cris, i386, m68k and ppcemb cases. Arm already has an explicit support for 32 bits thumb instructions spanning 2 pages, so it should not need this define. When this define is not set, the ldxxx_code_p function just does ldxxx_raw(phys_pc) in the softmmu case and ldxxx_raw(pc) in the user-mode only case. This is optimal for pure RISC architectures and does not need the #ifdef CONFIG_USER_ONLY you added for Sparc in your patch version. I also added a provision for a TARGET_MMIO_CODE define which may be used later when this will really be supported by Qemu.
I also took your fixes for Sparc phys_pc computation, but reversed your patch to use ldl_raw as it should not be needed anymore.
I did test PowerPC in user-mode only and softmmu mode and i386 in softmmu successfully using this new version of the patch.

OK for Sparc.

#5 lore raw
In reply to J. Mayer

J. Mayer wrote:
[snip]

Quoted 4 lines
Quoted 3 lines

My idea of always using the ldx_code_p function is that we may have the occasion to make it more cleaver and make the slow case handle code execution in mmio areas, when it will be possible.

Quoted 14 lines

Here's an updated patch. I added a definition TARGET_HAS_VLE_INSNS which is defined is the cris, i386, m68k and ppcemb cases. Arm already has an explicit support for 32 bits thumb instructions spanning 2 pages, so it should not need this define. When this define is not set, the ldxxx_code_p function just does ldxxx_raw(phys_pc) in the softmmu case and ldxxx_raw(pc) in the user-mode only case. This is optimal for pure RISC architectures and does not need the #ifdef CONFIG_USER_ONLY you added for Sparc in your patch version. I also added a provision for a TARGET_MMIO_CODE define which may be used later when this will really be supported by Qemu.
I also took your fixes for Sparc phys_pc computation, but reversed your patch to use ldl_raw as it should not be needed anymore.
I did test PowerPC in user-mode only and softmmu mode and i386 in softmmu successfully using this new version of the patch.

Works ok for MIPS. There's no obvious change in performance, I guess the slow TLB emulation drowns out any possible improvement.

Thiemo

author
#6 lore raw
Quoted 21 lines · Thiemo Seufer

J. Mayer wrote:
[snip]

Quoted 4 lines
Quoted 3 lines

My idea of always using the ldx_code_p function is that we may have the occasion to make it more cleaver and make the slow case handle code execution in mmio areas, when it will be possible.

Quoted 14 lines

Here's an updated patch. I added a definition TARGET_HAS_VLE_INSNS which is defined is the cris, i386, m68k and ppcemb cases. Arm already has an explicit support for 32 bits thumb instructions spanning 2 pages, so it should not need this define. When this define is not set, the ldxxx_code_p function just does ldxxx_raw(phys_pc) in the softmmu case and ldxxx_raw(pc) in the user-mode only case. This is optimal for pure RISC architectures and does not need the #ifdef CONFIG_USER_ONLY you added for Sparc in your patch version. I also added a provision for a TARGET_MMIO_CODE define which may be used later when this will really be supported by Qemu.
I also took your fixes for Sparc phys_pc computation, but reversed your patch to use ldl_raw as it should not be needed anymore.
I did test PowerPC in user-mode only and softmmu mode and i386 in softmmu successfully using this new version of the patch.

Quoted 2 lines

Works ok for MIPS. There's no obvious change in performance, I guess the slow TLB emulation drowns out any possible improvement.

Great !
Yes, the optimisation we got here is more a 'don't waste our time doing unneeded things' than a great performance boost. Running a long test program in linux user mode seems to indicate it spend a little less time in user-mode when the patch is applied, but this is not very significant compared to the total time spent in execution. Maybe gprof would show us if we really spend less time in the translation process...

Signature · 2 lines
J. Mayer <[email protected]>
Never organized
#7 lore raw
In reply to J. Mayer
Quoted 63 lines · J. Mayer
Quoted 48 lines · Blue Swirl
Quoted 47 lines · J. Mayer

-------- Forwarded Message --------

Quoted 43 lines

From: Jocelyn Mayer <[email protected]>
Reply-To: [email protected], [email protected]
To: [email protected]
Subject: Re: [Qemu-devel] RFC: Code fetch optimisation
Date: Fri, 12 Oct 2007 20:24:44 +0200

Quoted 14 lines · Blue Swirl
Quoted 13 lines · J. Mayer

Here's a small patch that allow an optimisation for code fetch, at least for RISC CPU targets, as suggested by Fabrice Bellard.
The main idea is that a translated block is never to span over a page boundary. As the tb_find_slow routine already gets the physical address of the page of code to be translated, the code translator could then fetch the code using raw host memory accesses instead of doing it through the softmmu routines.
This patch could also be adapted to RISC CPU targets, with care for the last instruction of a page. For now, I did implement it for alpha, arm, mips, PowerPC and SH4.
I don't actually know if the optimsation would bring a sensible speed gain or if it will be absolutelly marginal.

Quoted 4 lines
Quoted 1 line

Please comment.

This will not work correctly for execution of MMIO registers, but maybe that won't work on real hardware either. Who cares.

I wonder if this is important or not... But maybe, when retrieving the physical address we could check if it is inside ROM/RAM or an I/O area and in the last case do not give the phys_addr information to the translator. In that case, it would go on using the ldxx_code. I guess if we want to do that, a set of helpers would be appreciated to avoid adding code like:
if (phys_pc == 0)
opc = ldul_code(virt_pc)
else
opc = ldul_raw(phys_pc)
everywhere... I could also add another check so this set of macro would automatically use ldxx_code if we reach a page boundary, which would then make easy to use this optimisation for CISC/VLE architectures too.

I'm not sure of the proper solution to allow executing code from mmio devices. But adding specific accessors to handle the CISC/VLE case is to be done.

[...]

Quoted 7 lines
Quoted 4 lines

I did update my patch following this way and it's now able to run x86 and PowerPC targets.
PowerPC is the easy case, x86 is maybe the worst... Well, I'm not really sure of what I've done for Sparc, but other targets should be safe.

It broke Sparc, delay slot handling makes things complicated. The updated patch passes my tests.

OK. I will take a look of how you solved this issue.

Quoted 3 lines

For extra performance, I bypassed the ldl_code_p. On Sparc, instructions can't be split between two pages. Isn't translation always contained to the same page for all targets like Sparc?

Quoted 6 lines

Yes, for RISC targets running 32 bits mode, we always stop translation when we reach the end of a code page. The problem comes with CISC architectures, like x86 or m68k, or RISC architecture running 16/32 bits code, like ARM in thumb mode or PowerPC in VLE mode. In all those case, there can be instructions spanning on 2 pages, then we need the ldx_code_p functions.

I see.

Quoted 3 lines

My idea of always using the ldx_code_p function is that we may have the occasion to make it more cleaver and make the slow case handle code execution in mmio areas, when it will be possible.

The fast path could be provided for the RISC targets conditional to, for example #define TARGET_CODE_ALIGNED
or something.