Skip to contentSkip to conversation

RFC: Code fetch optimisation

Qemu-devel J. Mayer 3 participants 5 messages Oct 12, 2007

lore
Share & tools
lore

J. Mayer

author
#0 lore raw

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.

Please comment.

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

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: target-alpha/translate.c
===================================================================
RCS file: /sources/qemu/qemu/target-alpha/translate.c,v retrieving revision 1.5

translate.c +5 -1
diff -u -d -d -p -r1.5 translate.c
@@ -1966,12 +1966,15 @@ int gen_intermediate_code_internal (CPUS
 #endif
     DisasContext ctx, *ctxp = &ctx;
     target_ulong pc_start;
+    unsigned long phys_pc;
     uint32_t insn;
     uint16_t *gen_opc_end;
     int j, lj = -1;
     int ret;
 
     pc_start = tb->pc;
+    phys_pc = (unsigned long)phys_ram_base + tb->page_addr[0] +
+        (pc_start & ~TARGET_PAGE_MASK);
     gen_opc_ptr = gen_opc_buf;
     gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
     gen_opparam_ptr = gen_opparam_buf;
@@ -2010,7 +2013,7 @@ int gen_intermediate_code_internal (CPUS
                     ctx.pc, ctx.mem_idx);
         }
 #endif
-        insn = ldl_code(ctx.pc);
+        insn = ldl_raw(phys_pc);
 #if defined ALPHA_DEBUG_DISAS
         insn_count++;
         if (logfile != NULL) {
@@ -2018,6 +2021,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 +8 -3
diff -u -d -d -p -r1.57 translate.c
@@ -38,6 +38,7 @@
 /* internal defines */
 typedef struct DisasContext {
     target_ulong pc;
+    unsigned long phys_pc;
     int is_jmp;
     /* Nonzero if this instruction has been conditionally skipped.  */
     int condjmp;
@@ -2206,8 +2207,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_raw(s->phys_pc);
     s->pc += 4;
+    s->phys_pc += 4;
 
     cond = insn >> 28;
     if (cond == 0xf){
@@ -2971,8 +2973,9 @@ static void disas_thumb_insn(DisasContex
     int32_t offset;
     int i;
 
-    insn = lduw_code(s->pc);
+    insn = lduw_raw(s->phys_pc);
     s->pc += 2;
+    s->phys_pc += 2;
 
     switch (insn >> 12) {
     case 0: case 1:
@@ -3494,7 +3497,7 @@ static void disas_thumb_insn(DisasContex
             break;
         }
         offset = ((int32_t)insn << 21) >> 10;
-        insn = lduw_code(s->pc);
+        insn = lduw_raw(s->phys_pc);
         offset |= insn & 0x7ff;
 
         val = (uint32_t)s->pc + 2;
@@ -3544,6 +3547,8 @@ static inline int gen_intermediate_code_
 
     dc->is_jmp = DISAS_NEXT;
     dc->pc = pc_start;
+    dc->phys_pc = (unsigned long)phys_ram_base + tb->page_addr[0] +
+        (pc_start & ~TARGET_PAGE_MASK);
     dc->singlestep_enabled = env->singlestep_enabled;
     dc->condjmp = 0;
     dc->thumb = env->thumb;

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

translate.c +5 -1
diff -u -d -d -p -r1.106 translate.c
@@ -6483,6 +6483,7 @@ gen_intermediate_code_internal (CPUState
 {
     DisasContext ctx;
     target_ulong pc_start;
+    unsigned long phys_pc;
     uint16_t *gen_opc_end;
     int j, lj = -1;
 
@@ -6490,6 +6491,8 @@ gen_intermediate_code_internal (CPUState
         fprintf (logfile, "search pc %d\n", search_pc);
 
     pc_start = tb->pc;
+    phys_pc = (unsigned long)phys_ram_base + tb->page_addr[0] +
+        (pc_start & ~TARGET_PAGE_MASK);
     gen_opc_ptr = gen_opc_buf;
     gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
     gen_opparam_ptr = gen_opparam_buf;
@@ -6544,9 +6547,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_raw(phys_pc);
         decode_opc(env, &ctx);
         ctx.pc += 4;
+        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 +5 -1
diff -u -d -d -p -r1.92 translate.c
@@ -6679,12 +7569,15 @@ static always_inline int gen_intermediat
     DisasContext ctx, *ctxp = &ctx;
     opc_handler_t **table, *handler;
     target_ulong pc_start;
+    unsigned long phys_pc;
     uint16_t *gen_opc_end;
     int supervisor;
     int single_step, branch_step;
     int j, lj = -1;
 
     pc_start = tb->pc;
+    phys_pc = (unsigned long)phys_ram_base + tb->page_addr[0] +
+        (pc_start & ~TARGET_PAGE_MASK);
     gen_opc_ptr = gen_opc_buf;
     gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
     gen_opparam_ptr = gen_opparam_buf;
@@ -6763,7 +7649,7 @@ static always_inline int gen_intermediat
                     ctx.nip, 1 - msr_pr, msr_ir);
         }
 #endif
-        ctx.opcode = ldl_code(ctx.nip);
+        ctx.opcode = ldl_raw(phys_pc);
         if (msr_le) {
             ctx.opcode = ((ctx.opcode & 0xFF000000) >> 24) |
                 ((ctx.opcode & 0x00FF0000) >> 8) |
@@ -6778,6 +7664,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 +5 -1
diff -u -d -d -p -r1.18 translate.c
@@ -1150,11 +1150,14 @@ gen_intermediate_code_internal(CPUState 
 {
     DisasContext ctx;
     target_ulong pc_start;
+    unsigned long phys_pc;
     static uint16_t *gen_opc_end;
     uint32_t old_flags;
     int i, ii;
 
     pc_start = tb->pc;
+    phys_pc = (unsigned long)phys_ram_base + tb->page_addr[0] +
+        (pc_start & ~TARGET_PAGE_MASK);
     gen_opc_ptr = gen_opc_buf;
     gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
     gen_opparam_ptr = gen_opparam_buf;
@@ -1210,9 +1213,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_raw(phys_pc);
 	decode_opc(&ctx);
 	ctx.pc += 2;
+        phys_pc += 2;
 	if ((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0)
 	    break;
 	if (env->singlestep_enabled)
#1 lore raw
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 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.

Wouldn't it be even more efficient if you moved most of this calculation:
+ phys_pc = (unsigned long)phys_ram_base + tb->page_addr[0] +
+ (pc_start & ~TARGET_PAGE_MASK);
here:
+ tb->page_addr[0] = phys_page1;
?

#2 lore raw
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. Something like this might be OK:

static inline target_ulong ldl_code_p(unsigned long *start_pc, unsigned long *phys_pc, target_ulong *virt_pc)

{
    target_ulong opc;
    if ((*start_pc ^ *phys_pc) & TARGET_PAGE_MASK) {
        /* slow path that may raise an exception */
        opc = ldul_code(virt_pc);
	*start_pc = phys_pc; /* Avoid softmmu call on next load */
    } else {
	/* Optimized path */
        opc = ldul_raw(phys_pc);
    }
    *phys_pc += sizeof(target_ulong);
    *virt_pc += sizeof(target_ulong);
    return opc;
}

Of course, 8, 16 and 64 (why not ?) bits accessors would be also provided the same way.

Quoted 6 lines

Wouldn't it be even more efficient if you moved most of this calculation:

(quoted diff) +2 -0
+    phys_pc = (unsigned long)phys_ram_base + tb->page_addr[0] +
+        (pc_start & ~TARGET_PAGE_MASK);

here:
+ tb->page_addr[0] = phys_page1;
?

Maybe. I choosed to do this way because it's exactly the same assignment that is done in tb_link_phys after the gen_intermediate_code function returns. I then though that the safer thing to do was to store the same value so, whatever might happen, the value in the tb structure is never inconsistent. I also guess that it's not so important as the tb is not linked at this point...

#3 lore raw
In reply to Blue Swirl
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.

It can never happen because QEMU currently does not support it (see get_phys_addr_code()). I started to implement it but never really finished it (real hardware can do it so QEMU should support it). The idea consist in using a reserved ram page to store the code. Another point is that the TB must be discarded once executed as the MMIO data can change.

Regards,

Fabrice.

#4 lore raw
In reply to Blue Swirl
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.

It can never happen because QEMU currently does not support it (see get_phys_addr_code()). I started to implement it but never really finished it (real hardware can do it so QEMU should support it). The idea consist in using a reserved ram page to store the code. Another point is that the TB must be discarded once executed as the MMIO data can change.

Regards,

Fabrice.