Skip to contentSkip to conversation

[4799] Add instruction counter.

Qemu-devel Paul Brook 10 participants 14 messages Jun 29, 2008 – Jul 11, 2008

lore
Share & tools
lore

Paul Brook

author
#0 lore raw

Revision: 4799

          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=4799
Author:   pbrook
Date:     2008-06-29 01:03:05 +0000 (Sun, 29 Jun 2008)

Log Message:
-----------
Add instruction counter.

Modified Paths:
--------------

    trunk/cpu-all.h
    trunk/cpu-defs.h
    trunk/cpu-exec.c
    trunk/exec-all.h
    trunk/exec.c
    trunk/hw/mips_timer.c
    trunk/qemu-doc.texi
    trunk/softmmu_template.h
    trunk/target-alpha/cpu.h
    trunk/target-alpha/translate.c
    trunk/target-arm/cpu.h
    trunk/target-arm/translate.c
    trunk/target-cris/cpu.h
    trunk/target-cris/translate.c
    trunk/target-i386/cpu.h
    trunk/target-i386/translate.c
    trunk/target-m68k/cpu.h
    trunk/target-m68k/translate.c
    trunk/target-mips/cpu.h
    trunk/target-mips/translate.c
    trunk/target-ppc/cpu.h
    trunk/target-ppc/helper.c
    trunk/target-ppc/translate.c
    trunk/target-sh4/cpu.h
    trunk/target-sh4/translate.c
    trunk/target-sparc/cpu.h
    trunk/target-sparc/translate.c
    trunk/translate-all.c
    trunk/vl.c

Modified: trunk/cpu-all.h
===================================================================

cpu-all.h +2 -0
@@ -782,6 +782,8 @@
     __attribute__ ((__noreturn__));
 extern CPUState *first_cpu;
 extern CPUState *cpu_single_env;
+extern int64_t qemu_icount;
+extern int use_icount;
 
 #define CPU_INTERRUPT_EXIT   0x01 /* wants exit from main loop */
 #define CPU_INTERRUPT_HARD   0x02 /* hardware interrupt pending */

Modified: trunk/cpu-defs.h
===================================================================

cpu-defs.h +28 -6
@@ -130,17 +130,29 @@
                    sizeof(target_phys_addr_t))];
 } CPUTLBEntry;
 
+#ifdef WORDS_BIGENDIAN
+typedef struct icount_decr_u16 {
+    uint16_t high;
+    uint16_t low;
+} icount_decr_u16;
+#else
+typedef struct icount_decr_u16 {
+    uint16_t low;
+    uint16_t high;
+} icount_decr_u16;
+#endif
+
 #define CPU_TEMP_BUF_NLONGS 128
 #define CPU_COMMON                                                      \
     struct TranslationBlock *current_tb; /* currently executing TB  */  \
     /* soft mmu support */                                              \
-    /* in order to avoid passing too many arguments to the memory       \
-       write helpers, we store some rarely used information in the CPU  \
+    /* in order to avoid passing too many arguments to the MMIO         \
+       helpers, we store some rarely used information in the CPU        \
        context) */                                                      \
-    unsigned long mem_write_pc; /* host pc at which the memory was      \
-                                   written */                           \
-    target_ulong mem_write_vaddr; /* target virtual addr at which the   \
-                                     memory was written */              \
+    unsigned long mem_io_pc; /* host pc at which the memory was         \
+                                accessed */                             \
+    target_ulong mem_io_vaddr; /* target virtual addr at which the      \
+                                     memory was accessed */             \
     int halted; /* TRUE if the CPU is in suspend state */               \
     /* The meaning of the MMU modes is defined in the target code. */   \
     CPUTLBEntry tlb_table[NB_MMU_MODES][CPU_TLB_SIZE];                  \
@@ -149,6 +161,16 @@
     /* buffer for temporaries in the code generator */                  \
     long temp_buf[CPU_TEMP_BUF_NLONGS];                                 \
                                                                         \
+    int64_t icount_extra; /* Instructions until next timer event.  */   \
+    /* Number of cycles left, with interrupt flag in high bit.          \
+       This allows a single read-compare-cbranch-write sequence to test \
+       for both decrementer underflow and exceptions.  */               \
+    union {                                                             \
+        uint32_t u32;                                                   \
+        icount_decr_u16 u16;                                            \
+    } icount_decr;                                                      \
+    uint32_t can_do_io; /* nonzero if memory mapped IO is safe.  */     \
+                                                                        \
     /* from this point: preserved by CPU reset */                       \
     /* ice debug support */                                             \
     target_ulong breakpoints[MAX_BREAKPOINTS];                          \

Modified: trunk/cpu-exec.c
===================================================================

cpu-exec.c +63 -30

131 lines not on this page · raw message

Modified: trunk/exec-all.h
===================================================================

exec-all.h +27 -6
@@ -27,7 +27,7 @@
 #define DISAS_UPDATE  2 /* cpu state was modified dynamically */
 #define DISAS_TB_JUMP 3 /* only pc was modified statically */
 
-struct TranslationBlock;
+typedef struct TranslationBlock TranslationBlock;
 
 /* XXX: make safe guess about sizes */
 #define MAX_OP_PER_INSTR 64
@@ -48,6 +48,7 @@
 extern target_ulong gen_opc_npc[OPC_BUF_SIZE];
 extern uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
 extern uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
+extern uint16_t gen_opc_icount[OPC_BUF_SIZE];
 extern target_ulong gen_opc_jump_pc[2];
 extern uint32_t gen_opc_hflags[OPC_BUF_SIZE];
 
@@ -75,6 +76,10 @@
                            CPUState *env, unsigned long searched_pc,
                            void *puc);
 void cpu_resume_from_signal(CPUState *env1, void *puc);
+void cpu_io_recompile(CPUState *env, void *retaddr);
+TranslationBlock *tb_gen_code(CPUState *env, 
+                              target_ulong pc, target_ulong cs_base, int flags,
+                              int cflags);
 void cpu_exec_init(CPUState *env);
 int page_unprotect(target_ulong address, unsigned long pc, void *puc);
 void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end,
@@ -117,16 +122,15 @@
 #define USE_DIRECT_JUMP
 #endif
 
-typedef struct TranslationBlock {
+struct TranslationBlock {
     target_ulong pc;   /* simulated PC corresponding to this block (EIP + CS base) */
     target_ulong cs_base; /* CS base for this block */
     uint64_t flags; /* flags defining in which context the code was generated */
     uint16_t size;      /* size of target code for this block (1 <=
                            size <= TARGET_PAGE_SIZE) */
     uint16_t cflags;    /* compile flags */
-#define CF_TB_FP_USED  0x0002 /* fp ops are used in the TB */
-#define CF_FP_USED     0x0004 /* fp ops are used in the TB or in a chained TB */
-#define CF_SINGLE_INSN 0x0008 /* compile only a single instruction */
+#define CF_COUNT_MASK  0x7fff
+#define CF_LAST_IO     0x8000 /* Last insn may be an IO access.  */
 
     uint8_t *tc_ptr;    /* pointer to the translated code */
     /* next matching tb for physical address. */
@@ -150,7 +154,8 @@
        jmp_first */
     struct TranslationBlock *jmp_next[2];
     struct TranslationBlock *jmp_first;
-} TranslationBlock;
+    uint32_t icount;
+};
 
 static inline unsigned int tb_jmp_cache_hash_page(target_ulong pc)
 {
@@ -173,9 +178,11 @@
 }
 
 TranslationBlock *tb_alloc(target_ulong pc);
+void tb_free(TranslationBlock *tb);
 void tb_flush(CPUState *env);
 void tb_link_phys(TranslationBlock *tb,
                   target_ulong phys_pc, target_ulong phys_page2);
+void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr);
 
 extern TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
 extern uint8_t *code_gen_ptr;
@@ -364,6 +371,20 @@
     }
     return addr + env1->tlb_table[mmu_idx][page_index].addend - (unsigned long)phys_ram_base;
 }
+
+/* Deterministic execution requires that IO only be performaed on the last
+   instruction of a TB so that interrupts take effect immediately.  */
+static inline int can_do_io(CPUState *env)
+{
+    if (!use_icount)
+        return 1;
+
+    /* If not executing code then assume we are ok.  */
+    if (!env->current_tb)
+        return 1;
+
+    return env->can_do_io != 0;
+}
 #endif
 
 #ifdef USE_KQEMU

Modified: trunk/exec.c
===================================================================

exec.c +118 -29

273 lines not on this page · raw message

Modified: trunk/hw/mips_timer.c
===================================================================

hw/mips_timer.c +5 -0
@@ -91,7 +91,12 @@
     if (env->CP0_Cause & (1 << CP0Ca_DC))
         return;
 
+    /* ??? This callback should occur when the counter is exactly equal to
+       the comparator value.  Offset the count by one to avoid immediately
+       retriggering the callback before any virtual time has passed.  */
+    env->CP0_Count++;
     cpu_mips_timer_update(env);
+    env->CP0_Count--;
     if (env->insn_flags & ISA_MIPS32R2)
         env->CP0_Cause |= 1 << CP0Ca_TI;
     qemu_irq_raise(env->irq[(env->CP0_IntCtl >> CP0IntCtl_IPTI) & 0x7]);

Modified: trunk/qemu-doc.texi
===================================================================

qemu-doc.texi +11 -0
@@ -965,6 +965,17 @@
 
 Note that this allows guest direct access to the host filesystem,
 so should only be used with trusted guest OS.
+
+@item -icount [N|auto]
+Enable virtual instruction counter.  The virtual cpu will execute one
+instruction every 2^N ns of virtual time.  If @code{auto} is specified
+then the virtual cpu speed will be automatically adjusted to keep virtual
+time within a few seconds of real time.
+
+Note that while this option can give deterministic behavior, it does not
+provide cycle accurate emulation.  Modern CPUs contain superscalar out of
+order cores with complex cache heirachies.  The number of instructions
+executed often has little or no correlation with actual performance.
 @end table
 
 @c man end

Modified: trunk/softmmu_template.h
===================================================================

softmmu_template.h +17 -5
@@ -51,12 +51,18 @@
                                                         int mmu_idx,
                                                         void *retaddr);
 static inline DATA_TYPE glue(io_read, SUFFIX)(target_phys_addr_t physaddr,
-                                              target_ulong addr)
+                                              target_ulong addr,
+                                              void *retaddr)
 {
     DATA_TYPE res;
     int index;
     index = (physaddr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
     physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
+    env->mem_io_pc = (unsigned long)retaddr;
+    if (index > (IO_MEM_NOTDIRTY >> IO_MEM_SHIFT)
+            && !can_do_io(env)) {
+        cpu_io_recompile(env, retaddr);
+    }
 
 #if SHIFT <= 2
     res = io_mem_read[index][SHIFT](io_mem_opaque[index], physaddr);
@@ -95,8 +101,9 @@
             /* IO access */
             if ((addr & (DATA_SIZE - 1)) != 0)
                 goto do_unaligned_access;
+            retaddr = GETPC();
             addend = env->iotlb[mmu_idx][index];
-            res = glue(io_read, SUFFIX)(addend, addr);
+            res = glue(io_read, SUFFIX)(addend, addr, retaddr);
         } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
             /* slow unaligned access (it spans two pages or IO) */
         do_unaligned_access:
@@ -148,8 +155,9 @@
             /* IO access */
             if ((addr & (DATA_SIZE - 1)) != 0)
                 goto do_unaligned_access;
+            retaddr = GETPC();
             addend = env->iotlb[mmu_idx][index];
-            res = glue(io_read, SUFFIX)(addend, addr);
+            res = glue(io_read, SUFFIX)(addend, addr, retaddr);
         } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
         do_unaligned_access:
             /* slow unaligned access (it spans two pages) */
@@ -194,9 +202,13 @@
     int index;
     index = (physaddr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
     physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
+    if (index > (IO_MEM_NOTDIRTY >> IO_MEM_SHIFT)
+            && !can_do_io(env)) {
+        cpu_io_recompile(env, retaddr);
+    }
 
-    env->mem_write_vaddr = addr;
-    env->mem_write_pc = (unsigned long)retaddr;
+    env->mem_io_vaddr = addr;
+    env->mem_io_pc = (unsigned long)retaddr;
 #if SHIFT <= 2
     io_mem_write[index][SHIFT](io_mem_opaque[index], physaddr, val);
 #else

Modified: trunk/target-alpha/cpu.h
===================================================================

target-alpha/cpu.h +2 -0
@@ -415,4 +415,6 @@
 void pal_init (CPUState *env);
 void call_pal (CPUState *env, int palcode);
 
+#define CPU_PC_FROM_TB(env, tb) env->pc = tb->pc
+
 #endif /* !defined (__CPU_ALPHA_H__) */

Modified: trunk/target-alpha/translate.c
===================================================================

target-alpha/translate.c +32 -1
@@ -43,6 +43,19 @@
     uint32_t amask;
 };
 
+TCGv cpu_env;
+
+#include "gen-icount.h"
+
+void alpha_translate_init()
+{
+    static int done_init = 0;
+    if (done_init)
+        return;
+    cpu_env = tcg_global_reg_new(TCG_TYPE_PTR, TCG_AREG0, "env");
+    done_init = 1;
+}
+
 static always_inline void gen_op_nop (void)
 {
 #if defined(GENERATE_NOP)
@@ -1970,6 +1983,8 @@
     uint16_t *gen_opc_end;
     int j, lj = -1;
     int ret;
+    int num_insns;
+    int max_insns;
 
     pc_start = tb->pc;
     gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
@@ -1981,6 +1996,12 @@
     ctx.mem_idx = ((env->ps >> 3) & 3);
     ctx.pal_mode = env->ipr[IPR_EXC_ADDR] & 1;
 #endif
+    num_insns = 0;
+    max_insns = tb->cflags & CF_COUNT_MASK;
+    if (max_insns == 0)
+        max_insns = CF_COUNT_MASK;
+
+    gen_icount_start();
     for (ret = 0; ret == 0;) {
         if (env->nb_breakpoints > 0) {
             for(j = 0; j < env->nb_breakpoints; j++) {
@@ -1998,8 +2019,11 @@
                     gen_opc_instr_start[lj++] = 0;
                 gen_opc_pc[lj] = ctx.pc;
                 gen_opc_instr_start[lj] = 1;
+                gen_opc_icount[lj] = num_insns;
             }
         }
+        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
+            gen_io_start();
 #if defined ALPHA_DEBUG_DISAS
         insn_count++;
         if (logfile != NULL) {
@@ -2014,6 +2038,7 @@
             fprintf(logfile, "opcode %08x %d\n", insn, insn_count);
         }
 #endif
+        num_insns++;
         ctx.pc += 4;
         ret = translate_one(ctxp, insn);
         if (ret != 0)
@@ -2022,7 +2047,8 @@
          * generation
          */
         if (((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0) ||
-            (env->singlestep_enabled)) {
+            (env->singlestep_enabled) ||
+            num_insns >= max_insns) {
             break;
         }
 #if defined (DO_SINGLE_STEP)
@@ -2035,8 +2061,11 @@
 #if defined (DO_TB_FLUSH)
     gen_op_tb_flush();
 #endif
+    if (tb->cflags & CF_LAST_IO)
+        gen_io_end();
     /* Generate the return instruction */
     tcg_gen_exit_tb(0);
+    gen_icount_end(tb, num_insns);
     *gen_opc_ptr = INDEX_op_end;
     if (search_pc) {
         j = gen_opc_ptr - gen_opc_buf;
@@ -2045,6 +2074,7 @@
             gen_opc_instr_start[lj++] = 0;
     } else {
         tb->size = ctx.pc - pc_start;
+        tb->icount = num_insns;
     }
 #if defined ALPHA_DEBUG_DISAS
     if (loglevel & CPU_LOG_TB_CPU) {
@@ -2079,6 +2109,7 @@
     if (!env)
         return NULL;
     cpu_exec_init(env);
+    alpha_translate_init();
     tlb_flush(env, 1);
     /* XXX: should not be hardcoded */
     env->implver = IMPLVER_2106x;

Modified: trunk/target-arm/cpu.h
===================================================================

target-arm/cpu.h +2 -0
@@ -417,6 +417,8 @@
 }
 #endif
 
+#define CPU_PC_FROM_TB(env, tb) env->regs[15] = tb->pc
+
 #include "cpu-all.h"
 
 #endif

Modified: trunk/target-arm/translate.c
===================================================================

target-arm/translate.c +30 -1
@@ -84,6 +84,9 @@
 static TCGv cpu_T[2];
 static TCGv cpu_F0s, cpu_F1s, cpu_F0d, cpu_F1d;
 
+#define ICOUNT_TEMP cpu_T[0]
+#include "gen-icount.h"
+
 /* initialize TCG globals.  */
 void arm_translate_init(void)
 {
@@ -8539,6 +8542,8 @@
     int j, lj;
     target_ulong pc_start;
     uint32_t next_page_start;
+    int num_insns;
+    int max_insns;
 
     /* generate intermediate code */
     num_temps = 0;
@@ -8575,6 +8580,12 @@
     cpu_M0 = tcg_temp_new(TCG_TYPE_I64);
     next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
     lj = -1;
+    num_insns = 0;
+    max_insns = tb->cflags & CF_COUNT_MASK;
+    if (max_insns == 0)
+        max_insns = CF_COUNT_MASK;
+
+    gen_icount_start();
     /* Reset the conditional execution bits immediately. This avoids
        complications trying to do it at the end of the block.  */
     if (env->condexec_bits)
@@ -8625,8 +8636,12 @@
             }
             gen_opc_pc[lj] = dc->pc;
             gen_opc_instr_start[lj] = 1;
+            gen_opc_icount[lj] = num_insns;
         }
 
+        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
+            gen_io_start();
+
         if (env->thumb) {
             disas_thumb_insn(env, dc);
             if (dc->condexec_mask) {
@@ -8659,10 +8674,21 @@
          * Otherwise the subsequent code could get translated several times.
          * Also stop translation when a page boundary is reached.  This
          * ensures prefech aborts occur at the right place.  */
+        num_insns ++;
     } while (!dc->is_jmp && gen_opc_ptr < gen_opc_end &&
              !env->singlestep_enabled &&
-             dc->pc < next_page_start);
+             dc->pc < next_page_start &&
+             num_insns < max_insns);
 
+    if (tb->cflags & CF_LAST_IO) {
+        if (dc->condjmp) {
+            /* FIXME:  This can theoretically happen with self-modifying
+               code.  */
+            cpu_abort(env, "IO on conditional branch instruction");
+        }
+        gen_io_end();
+    }
+
     /* At this stage dc->condjmp will only be set when the skipped
        instruction was a conditional branch or trap, and the PC has
        already been written.  */
@@ -8726,7 +8752,9 @@
             dc->condjmp = 0;
         }
     }
+
 done_generating:
+    gen_icount_end(tb, num_insns);
     *gen_opc_ptr = INDEX_op_end;
 
 #ifdef DEBUG_DISAS
@@ -8744,6 +8772,7 @@
             gen_opc_instr_start[lj++] = 0;
     } else {
         tb->size = dc->pc - pc_start;
+        tb->icount = num_insns;
     }
     return 0;
 }

Modified: trunk/target-cris/cpu.h
===================================================================

target-cris/cpu.h +2 -0
@@ -238,5 +238,7 @@
 #define SFR_RW_MM_TLB_LO   env->pregs[PR_SRS]][5
 #define SFR_RW_MM_TLB_HI   env->pregs[PR_SRS]][6
 
+#define CPU_PC_FROM_TB(env, tb) env->pc = tb->pc
+
 #include "cpu-all.h"
 #endif

Modified: trunk/target-cris/translate.c
===================================================================

target-cris/translate.c +20 -1
@@ -77,6 +77,8 @@
 TCGv env_btarget;
 TCGv env_pc;
 
+#include "gen-icount.h"
+
 /* This is the state at translation time.  */
 typedef struct DisasContext {
 	CPUState *env;
@@ -3032,6 +3034,8 @@
 	struct DisasContext *dc = &ctx;
 	uint32_t next_page_start;
 	target_ulong npc;
+        int num_insns;
+        int max_insns;
 
 	if (!logfile)
 		logfile = stderr;
@@ -3092,6 +3096,12 @@
 
 	next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
 	lj = -1;
+        num_insns = 0;
+        max_insns = tb->cflags & CF_COUNT_MASK;
+        if (max_insns == 0)
+            max_insns = CF_COUNT_MASK;
+
+        gen_icount_start();
 	do
 	{
 		check_breakpoint(env, dc);
@@ -3108,6 +3118,7 @@
 			else
 				gen_opc_pc[lj] = dc->pc;
 			gen_opc_instr_start[lj] = 1;
+                        gen_opc_icount[lj] = num_insns;
 		}
 
 		/* Pretty disas.  */
@@ -3116,6 +3127,8 @@
 			DIS(fprintf(logfile, "%x ", dc->pc));
 		}
 
+                if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
+                    gen_io_start();
 		dc->clear_x = 1;
 		if (unlikely(loglevel & CPU_LOG_TB_OP))
 			tcg_gen_debug_insn_start(dc->pc);
@@ -3125,6 +3138,7 @@
 		if (dc->clear_x)
 			cris_clear_x_flag(dc);
 
+                num_insns++;
 		/* Check for delayed branches here. If we do it before
 		   actually genereating any host code, the simulator will just
 		   loop doing nothing for on this program location.  */
@@ -3151,12 +3165,15 @@
 		if (!(tb->pc & 1) && env->singlestep_enabled)
 			break;
 	} while (!dc->is_jmp && gen_opc_ptr < gen_opc_end
-		 && (dc->pc < next_page_start));
+		 && (dc->pc < next_page_start)
+                 && num_insns < max_insns);
 
 	npc = dc->pc;
 	if (dc->jmp == JMP_DIRECT && !dc->delayed_branch)
 		npc = dc->jmp_pc;
 
+        if (tb->cflags & CF_LAST_IO)
+            gen_io_end();
 	/* Force an update if the per-tb cpu state has changed.  */
 	if (dc->is_jmp == DISAS_NEXT
 	    && (dc->cpustate_changed || !dc->flagx_known 
@@ -3194,6 +3211,7 @@
 				break;
 		}
 	}
+        gen_icount_end(tb, num_insns);
 	*gen_opc_ptr = INDEX_op_end;
 	if (search_pc) {
 		j = gen_opc_ptr - gen_opc_buf;
@@ -3202,6 +3220,7 @@
 			gen_opc_instr_start[lj++] = 0;
 	} else {
 		tb->size = dc->pc - pc_start;
+                tb->icount = num_insns;
 	}
 
 #ifdef DEBUG_DISAS

Modified: trunk/target-i386/cpu.h
===================================================================

target-i386/cpu.h +2 -0
@@ -753,6 +753,8 @@
 }
 #endif
 
+#define CPU_PC_FROM_TB(env, tb) env->eip = tb->pc - tb->cs_base
+
 #include "cpu-all.h"
 
 #include "svm.h"

Modified: trunk/target-i386/translate.c
===================================================================

target-i386/translate.c +61 -4

188 lines not on this page · raw message

Modified: trunk/target-m68k/cpu.h
===================================================================

target-m68k/cpu.h +2 -0
@@ -235,6 +235,8 @@
 }
 #endif
 
+#define CPU_PC_FROM_TB(env, tb) env->pc = tb->pc
+
 #include "cpu-all.h"
 
 #endif

Modified: trunk/target-m68k/translate.c
===================================================================

target-m68k/translate.c +20 -1
@@ -63,6 +63,8 @@
 /* Used to distinguish stores from bad addressing modes.  */
 static TCGv store_dummy;
 
+#include "gen-icount.h"
+
 void m68k_tcg_init(void)
 {
     char *p;
@@ -2919,6 +2921,8 @@
     target_ulong pc_start;
     int pc_offset;
     int last_cc_op;
+    int num_insns;
+    int max_insns;
 
     /* generate intermediate code */
     pc_start = tb->pc;
@@ -2937,6 +2941,12 @@
     dc->is_mem = 0;
     dc->mactmp = NULL_QREG;
     lj = -1;
+    num_insns = 0;
+    max_insns = tb->cflags & CF_COUNT_MASK;
+    if (max_insns == 0)
+        max_insns = CF_COUNT_MASK;
+
+    gen_icount_start();
     do {
         pc_offset = dc->pc - pc_start;
         gen_throws_exception = NULL;
@@ -2960,10 +2970,14 @@
             }
             gen_opc_pc[lj] = dc->pc;
             gen_opc_instr_start[lj] = 1;
+            gen_opc_icount[lj] = num_insns;
         }
+        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
+            gen_io_start();
         last_cc_op = dc->cc_op;
         dc->insn_pc = dc->pc;
 	disas_m68k_insn(env, dc);
+        num_insns++;
 
         /* Terminate the TB on memory ops if watchpoints are present.  */
         /* FIXME: This should be replacd by the deterministic execution
@@ -2972,8 +2986,11 @@
             break;
     } while (!dc->is_jmp && gen_opc_ptr < gen_opc_end &&
              !env->singlestep_enabled &&
-             (pc_offset) < (TARGET_PAGE_SIZE - 32));
+             (pc_offset) < (TARGET_PAGE_SIZE - 32) &&
+             num_insns < max_insns);
 
+    if (tb->cflags & CF_LAST_IO)
+        gen_io_end();
     if (__builtin_expect(env->singlestep_enabled, 0)) {
         /* Make sure the pc is updated, and raise a debug exception.  */
         if (!dc->is_jmp) {
@@ -2999,6 +3016,7 @@
             break;
         }
     }
+    gen_icount_end(tb, num_insns);
     *gen_opc_ptr = INDEX_op_end;
 
 #ifdef DEBUG_DISAS
@@ -3016,6 +3034,7 @@
             gen_opc_instr_start[lj++] = 0;
     } else {
         tb->size = dc->pc - pc_start;
+        tb->icount = num_insns;
     }
 
     //optimize_flags();

Modified: trunk/target-mips/cpu.h
===================================================================

target-mips/cpu.h +6 -0
@@ -572,4 +572,10 @@
 uint32_t cpu_mips_get_clock (void);
 int cpu_mips_signal_handler(int host_signum, void *pinfo, void *puc);
 
+#define CPU_PC_FROM_TB(env, tb) do { \
+    env->active_tc.PC = tb->pc; \
+    env->hflags &= ~MIPS_HFLAG_BMASK; \
+    env->hflags |= tb->flags & MIPS_HFLAG_BMASK; \
+    } while (0)
+
 #endif /* !defined (__MIPS_CPU_H__) */

Modified: trunk/target-mips/translate.c
===================================================================

target-mips/translate.c +51 -0

151 lines not on this page · raw message

Modified: trunk/target-ppc/cpu.h
===================================================================

target-ppc/cpu.h +3 -0
@@ -697,6 +697,7 @@
 
 /*****************************************************************************/
 CPUPPCState *cpu_ppc_init (const char *cpu_model);
+void ppc_translate_init(void);
 int cpu_ppc_exec (CPUPPCState *s);
 void cpu_ppc_close (CPUPPCState *s);
 /* you can call this signal handler from your SIGBUS and SIGSEGV
@@ -833,6 +834,8 @@
 }
 #endif
 
+#define CPU_PC_FROM_TB(env, tb) env->nip = tb->pc
+
 #include "cpu-all.h"
 
 /*****************************************************************************/

Modified: trunk/target-ppc/helper.c
===================================================================

target-ppc/helper.c +1 -0
@@ -2977,6 +2977,7 @@
     if (!env)
         return NULL;
     cpu_exec_init(env);
+    ppc_translate_init();
     env->cpu_model_str = cpu_model;
     cpu_ppc_register_internal(env, def);
     cpu_ppc_reset(env);

Modified: trunk/target-ppc/translate.c
===================================================================

target-ppc/translate.c +31 -1
@@ -43,6 +43,19 @@
 /*****************************************************************************/
 /* Code translation helpers                                                  */
 
+static TCGv cpu_env;
+
+#include "gen-icount.h"
+
+void ppc_translate_init(void)
+{
+    int done_init = 0;
+    if (done_init)
+        return;
+    cpu_env = tcg_global_reg_new(TCG_TYPE_PTR, TCG_AREG0, "env");
+    done_init = 1;
+}
+
 #if defined(OPTIMIZE_FPRF_UPDATE)
 static uint16_t *gen_fprf_buf[OPC_BUF_SIZE];
 static uint16_t **gen_fprf_ptr;
@@ -6168,6 +6181,8 @@
     uint16_t *gen_opc_end;
     int supervisor, little_endian;
     int j, lj = -1;
+    int num_insns;
+    int max_insns;
 
     pc_start = tb->pc;
     gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
@@ -6211,6 +6226,12 @@
     /* Single step trace mode */
     msr_se = 1;
 #endif
+    num_insns = 0;
+    max_insns = tb->cflags & CF_COUNT_MASK;
+    if (max_insns == 0)
+        max_insns = CF_COUNT_MASK;
+
+    gen_icount_start();
     /* Set env in case of segfault during code fetch */
     while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
         if (unlikely(env->nb_breakpoints > 0)) {
@@ -6230,6 +6251,7 @@
                     gen_opc_instr_start[lj++] = 0;
                 gen_opc_pc[lj] = ctx.nip;
                 gen_opc_instr_start[lj] = 1;
+                gen_opc_icount[lj] = num_insns;
             }
         }
 #if defined PPC_DEBUG_DISAS
@@ -6239,6 +6261,8 @@
                     ctx.nip, supervisor, (int)msr_ir);
         }
 #endif
+        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
+            gen_io_start();
         if (unlikely(little_endian)) {
             ctx.opcode = bswap32(ldl_code(ctx.nip));
         } else {
@@ -6253,6 +6277,7 @@
 #endif
         ctx.nip += 4;
         table = env->opcodes;
+        num_insns++;
         handler = table[opc1(ctx.opcode)];
         if (is_indirect_opcode(handler)) {
             table = ind_table(handler);
@@ -6306,7 +6331,8 @@
                      ctx.exception != POWERPC_EXCP_BRANCH)) {
             GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
         } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
-                            (env->singlestep_enabled))) {
+                            (env->singlestep_enabled) ||
+                            num_insns >= max_insns)) {
             /* if we reach a page boundary or are single stepping, stop
              * generation
              */
@@ -6316,6 +6342,8 @@
         break;
 #endif
     }
+    if (tb->cflags & CF_LAST_IO)
+        gen_io_end();
     if (ctx.exception == POWERPC_EXCP_NONE) {
         gen_goto_tb(&ctx, 0, ctx.nip);
     } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
@@ -6326,6 +6354,7 @@
         /* Generate the return instruction */
         tcg_gen_exit_tb(0);
     }
+    gen_icount_end(tb, num_insns);
     *gen_opc_ptr = INDEX_op_end;
     if (unlikely(search_pc)) {
         j = gen_opc_ptr - gen_opc_buf;
@@ -6334,6 +6363,7 @@
             gen_opc_instr_start[lj++] = 0;
     } else {
         tb->size = ctx.nip - pc_start;
+        tb->icount = num_insns;
     }
 #if defined(DEBUG_DISAS)
     if (loglevel & CPU_LOG_TB_CPU) {

Modified: trunk/target-sh4/cpu.h
===================================================================

target-sh4/cpu.h +5 -0
@@ -152,6 +152,11 @@
 }
 #endif
 
+#define CPU_PC_FROM_TB(env, tb) do { \
+    env->pc = tb->pc; \
+    env->flags = tb->flags; \
+    } while (0)
+
 #include "cpu-all.h"
 
 /* Memory access type */

Modified: trunk/target-sh4/translate.c
===================================================================

target-sh4/translate.c +31 -0
@@ -56,6 +56,19 @@
     BS_EXCP     = 3, /* We reached an exception condition */
 };
 
+static TCGv cpu_env;
+
+#include "gen-icount.h"
+
+void sh4_translate_init()
+{
+    static int done_init = 0;
+    if (done_init)
+        return;
+    cpu_env = tcg_global_reg_new(TCG_TYPE_PTR, TCG_AREG0, "env");
+    done_init = 1;
+}
+
 #ifdef CONFIG_USER_ONLY
 
 #define GEN_OP_LD(width, reg) \
@@ -143,6 +156,7 @@
     if (!env)
 	return NULL;
     cpu_exec_init(env);
+    sh4_translate_init();
     cpu_sh4_reset(env);
     tlb_flush(env, 1);
     return env;
@@ -1189,6 +1203,8 @@
     target_ulong pc_start;
     static uint16_t *gen_opc_end;
     int i, ii;
+    int num_insns;
+    int max_insns;
 
     pc_start = tb->pc;
     gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
@@ -1213,6 +1229,11 @@
 #endif
 
     ii = -1;
+    num_insns = 0;
+    max_insns = tb->cflags & CF_COUNT_MASK;
+    if (max_insns == 0)
+        max_insns = CF_COUNT_MASK;
+    gen_icount_start();
     while (ctx.bstate == BS_NONE && gen_opc_ptr < gen_opc_end) {
 	if (env->nb_breakpoints > 0) {
 	    for (i = 0; i < env->nb_breakpoints; i++) {
@@ -1235,22 +1256,30 @@
             gen_opc_pc[ii] = ctx.pc;
             gen_opc_hflags[ii] = ctx.flags;
             gen_opc_instr_start[ii] = 1;
+            gen_opc_icount[ii] = num_insns;
         }
+        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
+            gen_io_start();
 #if 0
 	fprintf(stderr, "Loading opcode at address 0x%08x\n", ctx.pc);
 	fflush(stderr);
 #endif
 	ctx.opcode = lduw_code(ctx.pc);
 	decode_opc(&ctx);
+        num_insns++;
 	ctx.pc += 2;
 	if ((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0)
 	    break;
 	if (env->singlestep_enabled)
 	    break;
+        if (num_insns >= max_insns)
+            break;
 #ifdef SH4_SINGLE_STEP
 	break;
 #endif
     }
+    if (tb->cflags & CF_LAST_IO)
+        gen_io_end();
     if (env->singlestep_enabled) {
         gen_op_debug();
     } else {
@@ -1274,6 +1303,7 @@
 	}
     }
 
+    gen_icount_end(tb, num_insns);
     *gen_opc_ptr = INDEX_op_end;
     if (search_pc) {
         i = gen_opc_ptr - gen_opc_buf;
@@ -1282,6 +1312,7 @@
             gen_opc_instr_start[ii++] = 0;
     } else {
         tb->size = ctx.pc - pc_start;
+        tb->icount = num_insns;
     }
 
 #ifdef DEBUG_DISAS

Modified: trunk/target-sparc/cpu.h
===================================================================

target-sparc/cpu.h +5 -0
@@ -437,6 +437,11 @@
 }
 #endif
 
+#define CPU_PC_FROM_TB(env, tb) do { \
+    env->pc = tb->pc; \
+    env->npc = tb->cs_base; \
+    } while(0)
+
 #include "cpu-all.h"
 
 #endif

Modified: trunk/target-sparc/translate.c
===================================================================

target-sparc/translate.c +19 -1
@@ -48,6 +48,8 @@
 /* local register indexes (only used inside old micro ops) */
 static TCGv cpu_tmp0, cpu_tmp32, cpu_tmp64;
 
+#include "gen-icount.h"
+
 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 */
@@ -4719,6 +4721,8 @@
     uint16_t *gen_opc_end;
     DisasContext dc1, *dc = &dc1;
     int j, lj = -1;
+    int num_insns;
+    int max_insns;
 
     memset(dc, 0, sizeof(DisasContext));
     dc->tb = tb;
@@ -4747,6 +4751,11 @@
     cpu_val = tcg_temp_local_new(TCG_TYPE_TL);
     cpu_addr = tcg_temp_local_new(TCG_TYPE_TL);
 
+    num_insns = 0;
+    max_insns = tb->cflags & CF_COUNT_MASK;
+    if (max_insns == 0)
+        max_insns = CF_COUNT_MASK;
+    gen_icount_start();
     do {
         if (env->nb_breakpoints > 0) {
             for(j = 0; j < env->nb_breakpoints; j++) {
@@ -4771,10 +4780,14 @@
                 gen_opc_pc[lj] = dc->pc;
                 gen_opc_npc[lj] = dc->npc;
                 gen_opc_instr_start[lj] = 1;
+                gen_opc_icount[lj] = num_insns;
             }
         }
+        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
+            gen_io_start();
         last_pc = dc->pc;
         disas_sparc_insn(dc);
+        num_insns++;
 
         if (dc->is_br)
             break;
@@ -4793,7 +4806,8 @@
             break;
         }
     } while ((gen_opc_ptr < gen_opc_end) &&
-             (dc->pc - pc_start) < (TARGET_PAGE_SIZE - 32));
+             (dc->pc - pc_start) < (TARGET_PAGE_SIZE - 32) &&
+             num_insns < max_insns);
 
  exit_gen_loop:
     tcg_temp_free(cpu_addr);
@@ -4802,6 +4816,8 @@
     tcg_temp_free(cpu_tmp64);
     tcg_temp_free(cpu_tmp32);
     tcg_temp_free(cpu_tmp0);
+    if (tb->cflags & CF_LAST_IO)
+        gen_io_end();
     if (!dc->is_br) {
         if (dc->pc != DYNAMIC_PC &&
             (dc->npc != DYNAMIC_PC && dc->npc != JUMP_PC)) {
@@ -4814,6 +4830,7 @@
             tcg_gen_exit_tb(0);
         }
     }
+    gen_icount_end(tb, num_insns);
     *gen_opc_ptr = INDEX_op_end;
     if (spc) {
         j = gen_opc_ptr - gen_opc_buf;
@@ -4829,6 +4846,7 @@
         gen_opc_jump_pc[1] = dc->jump_pc[1];
     } else {
         tb->size = last_pc + 4 - pc_start;
+        tb->icount = num_insns;
     }
 #ifdef DEBUG_DISAS
     if (loglevel & CPU_LOG_TB_IN_ASM) {

Modified: trunk/translate-all.c
===================================================================

translate-all.c +9 -0
@@ -38,6 +38,7 @@
 TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE];
 
 target_ulong gen_opc_pc[OPC_BUF_SIZE];
+uint16_t gen_opc_icount[OPC_BUF_SIZE];
 uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
 #if defined(TARGET_I386)
 uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
@@ -158,6 +159,13 @@
     if (gen_intermediate_code_pc(env, tb) < 0)
         return -1;
 
+    if (use_icount) {
+        /* Reset the cycle counter to the start of the block.  */
+        env->icount_decr.u16.low += tb->icount;
+        /* Clear the IO flag.  */
+        env->can_do_io = 0;
+    }
+
     /* find opc index corresponding to search_pc */
     tc_ptr = (unsigned long)tb->tc_ptr;
     if (searched_pc < tc_ptr)
@@ -177,6 +185,7 @@
     /* now find start of instruction before */
     while (gen_opc_instr_start[j] == 0)
         j--;
+    env->icount_decr.u16.low -= gen_opc_icount[j];
 
     gen_pc_load(env, tb, searched_pc, j, puc);
 

Modified: trunk/vl.c
===================================================================

vl.c +225 -31

391 lines not on this page · raw message

#1 lore raw
Quoted 4 lines · Paul Brook

Revision: 4799

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=4799
Author:   pbrook
Date:     2008-06-29 01:03:05 +0000 (Sun, 29 Jun 2008)

Some trivial comments:
- missing gen_icount.h file
- duplicate num_insns = 0; in
target_mips/translate.c/gen_intermediate_code_internal
- typo in vl.c/help "clock ticks per instructon"
- typo in qemu-doc.texi under icount: "cache heirachies"

Laurent

#2 lore raw
Quoted 6 lines · Laurent Desnogues
Quoted 4 lines · Paul Brook

Revision: 4799

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=4799
Author:   pbrook
Date:     2008-06-29 01:03:05 +0000 (Sun, 29 Jun 2008)
Quoted 6 lines

Some trivial comments:
- missing gen_icount.h file
- duplicate num_insns = 0; in
target_mips/translate.c/gen_intermediate_code_internal
- typo in vl.c/help "clock ticks per instructon"
- typo in qemu-doc.texi under icount: "cache heirachies"

A few more comments, taking a quick look at the PowerPC target changes:
+void ppc_translate_init(void)
+{
+ int done_init = 0;
+ if (done_init)
+ return;
+ cpu_env = tcg_global_reg_new(TCG_TYPE_PTR, TCG_AREG0, "env");
+ done_init = 1;
+}

- done_init is on the stack, thus will never be 1.
- why isn't this variable / code shared with other targets (can see the exact same code in Alpha target...) ?
- one detail: this function is more likely to be found in translate_init.c

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

target-ppc/translate.c

Quoted 8 lines
target-ppc/translate.c +6 -1
+    int done_init = 0;
+    if (done_init)
+        return;
+    cpu_env = tcg_global_reg_new(TCG_TYPE_PTR, TCG_AREG0, "env");
+    done_init = 1;
+}
 
- done_init is on the stack, thus will never be 1.

Fixed.

Quoted 2 lines

- why isn't this variable / code shared with other targets (can see the exact same code in Alpha target...) ?

Maybe. Most targets have many globals and cpu_env is the only one that's really target independent. alpha and ppc are only the same because they still haven't been converted to TCG.

Paul

#6 lore raw
In reply to Paul Brook
Quoted 8 lines · Paul Brook

Revision: 4799

          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=4799
Author:   pbrook
Date:     2008-06-29 01:03:05 +0000 (Sun, 29 Jun 2008)

Log Message:
-----------
Add instruction counter.

...

Quoted 11 lines
exec.c +11 -0
+/* in deterministic execution mode, instructions doing device I/Os
+   must be at the end of the TB */
+void cpu_io_recompile(CPUState *env, void *retaddr)
+{
+    TranslationBlock *tb;
+    uint32_t n, cflags;
+    target_ulong pc, cs_base;
+    uint64_t flags;
+
+    tb = tb_find_pc((unsigned long)retaddr);
+    if (!tb) {

exec.c line 3020

Quoted 10 lines
exec.c +10 -0
+        cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p", 
+                  retaddr);
+    }
+    n = env->icount_decr.u16.low + tb->icount;
+    cpu_restore_state(tb, env, (unsigned long)retaddr, NULL);
+    /* Calculate how many instructions had been executed before the fault
+       occured.  */
+    n = n - env->icount_decr.u16.low;
+    /* Generate a new TB ending on the I/O insn.  */
+    n++;

On the first glance this function looked like it could serve as an alternative to SSTEP_INTERNAL and provide the required roll-back on watchpoint hit. But looking closer I realized that icount_decr is only maintained if use_icount is set. But that appears to be optional and default off. Now I'm wondering if I should simply rebase my roll-back approach or if I should try to generalize yours in order to get the debugging series work again.

I do not yet get why you were forced to go a different path for cpu_io_recompile, ie. rebuilding and (re-executing?) the whole TB up to the instruction that caused the IO access instead of just regenerating a single-insn TB for that purpose. Is it more efficient? But if use_icount is off by default, I guess this doesn't come for free either...

Jan

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

target-ppc/translate.c

Quoted 8 lines
target-ppc/translate.c +6 -1
+    int done_init = 0;
+    if (done_init)
+        return;
+    cpu_env = tcg_global_reg_new(TCG_TYPE_PTR, TCG_AREG0, "env");
+    done_init = 1;
+}
 
- done_init is on the stack, thus will never be 1.

Fixed.

thanks

Quoted 6 lines
Quoted 2 lines

- why isn't this variable / code shared with other targets (can see the exact same code in Alpha target...) ?

Maybe. Most targets have many globals and cpu_env is the only one that's really target independent. alpha and ppc are only the same because they still haven't been converted to TCG.

OK, I see.

Signature · 2 lines
J. Mayer <[email protected]>
Never organized
#7 lore raw
In reply to Jan Kiszka
Quoted 4 lines

On the first glance this function looked like it could serve as an alternative to SSTEP_INTERNAL and provide the required roll-back on watchpoint hit. But looking closer I realized that icount_decr is only maintained if use_icount is set.

I'm fairly sure limiting the length of the TB and actual instruction counting are largely independent. IIUC you only need the former.

Quoted 4 lines

I do not yet get why you were forced to go a different path for cpu_io_recompile, ie. rebuilding and (re-executing?) the whole TB up to the instruction that caused the IO access instead of just regenerating a single-insn TB for that purpose. Is it more efficient?

Generating a single insn IO TB is a good idea for resolving the current fault. This is what the comment at the end of cpu_io_recompile is referring to.

Regenerating a truncated version of the original version of the TB is important for subsequent execution of that block. MMIO accesses occur frequently in loops when the guest is checking status bits or accessing a FIFO. Recompiling the TB means that subsequent accesses complete with minimal overhead. If we didn't recompile then every access would incur a (very expensive) trap+unwind+singlestep.

The type of access can't be determined statically (it's a property of the address being accesses, not the instruction itself). However I'd expect that most accesses always access wither RAM or MMIO spaces in practice, so recompiling when we see an IO access is a reasonable compromise.

Quoted 2 lines

But if use_icount is off by default, I guess this doesn't come for free either...

See above. cpu_io_recompile is used to get precise delivery of interrupts. This is required for but not dependent on having deterministic timing (i.e. use_icount).

Paul

#8 lore raw
Quoted 7 lines · Paul Brook
Quoted 4 lines

On the first glance this function looked like it could serve as an alternative to SSTEP_INTERNAL and provide the required roll-back on watchpoint hit. But looking closer I realized that icount_decr is only maintained if use_icount is set.

I'm fairly sure limiting the length of the TB and actual instruction counting are largely independent. IIUC you only need the former.

But to calculate the former, you need the latter again. I wonder if it wouldn't be more efficient and flexible to specify a terminating PC instead of an instruction count. Wouldn't that make cpu_io_recompile independent of icount_decr and, thus, use_icount?

Quoted 16 lines
Quoted 5 lines

I do not yet get why you were forced to go a different path for cpu_io_recompile, ie. rebuilding and (re-executing?) the whole TB up to the instruction that caused the IO access instead of just regenerating a single-insn TB for that purpose. Is it more efficient?

Generating a single insn IO TB is a good idea for resolving the current fault. This is what the comment at the end of cpu_io_recompile is referring to.

Regenerating a truncated version of the original version of the TB is important for subsequent execution of that block. MMIO accesses occur frequently in loops when the guest is checking status bits or accessing a FIFO. Recompiling the TB means that subsequent accesses complete with minimal overhead. If we didn't recompile then every access would incur a (very expensive) trap+unwind+singlestep.

Quoted 4 lines

The type of access can't be determined statically (it's a property of the address being accesses, not the instruction itself). However I'd expect that most accesses always access wither RAM or MMIO spaces in practice, so recompiling when we see an IO access is a reasonable compromise.

OK, understood.

Quoted 7 lines
Quoted 3 lines

But if use_icount is off by default, I guess this doesn't come for free either...

See above. cpu_io_recompile is used to get precise delivery of interrupts. This is required for but not dependent on having deterministic timing (i.e. use_icount).

Watchpoints, specifically guest-injected ones, require deterministic exception delivery as well. So I would like to reuse existing infrastructure that already solved a similar problem.

Jan

#9 lore raw
Quoted 10 lines · Jan Kiszka
Quoted 7 lines · Paul Brook
Quoted 4 lines

On the first glance this function looked like it could serve as an alternative to SSTEP_INTERNAL and provide the required roll-back on watchpoint hit. But looking closer I realized that icount_decr is only maintained if use_icount is set.

I'm fairly sure limiting the length of the TB and actual instruction counting are largely independent. IIUC you only need the former.

But to calculate the former, you need the latter again.

Not really. You only need to know how far through the TB you got before the trap occurred.

Quoted 4 lines

I wonder if it
wouldn't be more efficient and flexible to specify a terminating PC instead of an instruction count. Wouldn't that make cpu_io_recompile independent of icount_decr and, thus, use_icount?

Ah, I see what you're getting at. cpu_restore_state modifies icount_decr to indicate how far through the TB we got. That's can be independent of use_icount.

A terminating PC is much less useful. In general the only instruction you really know the location of is the one you're currently at.

Paul

#10 lore raw
In reply to Paul Brook
Quoted 33 lines · Paul Brook

Revision: 4799

        http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=4799
Author:   pbrook
Date:     2008-06-29 01:03:05 +0000 (Sun, 29 Jun 2008)

Log Message:
-----------
Add instruction counter.

Modified Paths:
--------------
trunk/cpu-all.h
trunk/cpu-defs.h
trunk/cpu-exec.c
trunk/exec-all.h
trunk/exec.c
trunk/hw/mips_timer.c
trunk/qemu-doc.texi
trunk/softmmu_template.h
trunk/target-alpha/cpu.h
trunk/target-alpha/translate.c
trunk/target-arm/cpu.h
trunk/target-arm/translate.c
trunk/target-cris/cpu.h
trunk/target-cris/translate.c
trunk/target-i386/cpu.h
trunk/target-i386/translate.c
trunk/target-m68k/cpu.h
trunk/target-m68k/translate.c
trunk/target-mips/cpu.h
trunk/target-mips/translate.c
trunk/target-ppc/cpu.h
trunk/target-ppc/helper.c

Quoted 8 lines

trunk/target-ppc/translate.c
trunk/target-sh4/cpu.h
trunk/target-sh4/translate.c
trunk/target-sparc/cpu.h
trunk/target-sparc/translate.c
trunk/translate-all.c
trunk/vl.c
[snip]

Since there isn't a gen-icount.h file, this revision fails to build. Maybe a forgotten patch wasn't applied?

#5 lore raw
In reply to Laurent Desnogues
Quoted 2 lines · Laurent Desnogues

- typo in vl.c/help "clock ticks per instructon"
- typo in qemu-doc.texi under icount: "cache heirachies"

A few more:

cpu-exec.c:
exired -> expired
exec-all.h:
performaed -> performed
exec.c:
Precice -> Precise
temorary -> temporary
mittle -> middle
occured -> occurred
preceeding -> preceding
failuting -> faulting?
gen-icount.h:
Helpewrs -> Helpers
genration -> generation
target-mips/translate.c:
simplicitly -> simplicity (x2)
vl.c:
instrctions -> instructions
alowable -> allowable
farly -> fairly
resonable -> reasonable

... and while we're at it:

target-arm/translate.c:
prefech -> prefetch
target-cris/translate.c:
genereating -> generating
target-m68k/translate.c:
replacd -> replaced

Cheers,

Signature · 1 line
Stuart Brady
#11 lore raw
In reply to Paul Brook
Quoted 10 lines · Paul Brook

Revision: 4799

          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=4799
Author:   pbrook
Date:     2008-06-29 01:03:05 +0000 (Sun, 29 Jun 2008)

Log Message:
-----------
Add instruction counter.

This patch breaks sparc32 for me.

A bisection shows:

4798   works
4799   doesn't build
4800   doesn't work (crashes with illegal instruction)

running qemu-g in sparc-test produces:

./qemu-g: line 4: 9859 Illegal instruction qemu-system-sparc -kernel vmlinux-2.6.11+tcx -initrd linux.img -append "root=/dev/ram"

running under valgrind produces this:

valgrind --trace-children=yes qemu-system-sparc -kernel vmlinux-2.6.11+tcx -initrd linux.img -append "root=/dev/ram" ==9861== Memcheck, a memory error detector.
==9861== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al.
==9861== Using LibVEX rev 1806, a library for dynamic binary translation.
==9861== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP.
==9861== Using valgrind-3.4.0.SVN, a dynamic binary instrumentation framework.
==9861== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al.
==9861== For more details, rerun with: -v
==9861==
==9861== Warning: set address range perms: large range 144179200 (undefined)
vex x86->IR: unhandled instruction bytes: 0xFF 0xFF 0xFF 0xFF
vex x86->IR: unhandled instruction bytes: 0xFF 0xFF 0xFF 0xFF
==9861== valgrind: Unrecognised instruction at address 0x114BA050.
==9861== Your program just tried to execute an instruction that Valgrind
==9861== did not recognise. There are two possible reasons for this.
==9861== 1. Your program has a bug and erroneously jumped to a non-code
==9861== location. If you are running Memcheck and you just saw a
==9861== warning about a bad jump, it's probably your program's fault.
==9861== 2. The instruction is legitimate but Valgrind doesn't handle it,
==9861== i.e. it's Valgrind's fault. If you think this is the case or
==9861== you are not sure, please let us know and we'll try to fix it.
==9861== Either way, Valgrind will now raise a SIGILL signal which will
==9861== probably kill your program.
==9861==
==9861== Process terminating with default action of signal 4 (SIGILL)
==9861== Illegal opcode at address 0x114BA050
==9861== at 0x114BA050: ???
==9861==

This is on a RedHat 9 system:

uname -a
Linux dell 2.4.20-31.9smp #1 SMP Tue Apr 13 17:40:10 EDT 2004 i686 i686 i386 GNU/Linux

gcc --version
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

#12 lore raw
Quoted 61 lines · Robert Reif
Quoted 2 lines · Paul Brook

Revision: 4799

http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=4799

Quoted 9 lines
Author:   pbrook
Date:     2008-06-29 01:03:05 +0000 (Sun, 29 Jun 2008)

Log Message:
-----------
Add instruction counter.

This patch breaks sparc32 for me.

A bisection shows:

 4798   works
 4799   doesn't build
 4800   doesn't work (crashes with illegal instruction)

running qemu-g in sparc-test produces:

./qemu-g: line 4: 9859 Illegal instruction qemu-system-sparc -kernel
vmlinux-2.6.11+tcx -initrd linux.img -append "root=/dev/ram"

running under valgrind produces this:

valgrind --trace-children=yes qemu-system-sparc -kernel vmlinux-2.6.11+tcx
-initrd linux.img -append "root=/dev/ram"
==9861== Memcheck, a memory error detector.
==9861== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al. ==9861== Using LibVEX rev 1806, a library for dynamic binary translation. ==9861== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP. ==9861== Using valgrind-3.4.0.SVN, a dynamic binary instrumentation
framework.
==9861== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al. ==9861== For more details, rerun with: -v
==9861==
==9861== Warning: set address range perms: large range 144179200
(undefined)
vex x86->IR: unhandled instruction bytes: 0xFF 0xFF 0xFF 0xFF vex x86->IR: unhandled instruction bytes: 0xFF 0xFF 0xFF 0xFF ==9861== valgrind: Unrecognised instruction at address 0x114BA050. ==9861== Your program just tried to execute an instruction that Valgrind ==9861== did not recognise. There are two possible reasons for this. ==9861== 1. Your program has a bug and erroneously jumped to a non-code
==9861== location. If you are running Memcheck and you just saw a
==9861== warning about a bad jump, it's probably your program's fault.
==9861== 2. The instruction is legitimate but Valgrind doesn't handle it,
==9861== i.e. it's Valgrind's fault. If you think this is the case or
==9861== you are not sure, please let us know and we'll try to fix it.
==9861== Either way, Valgrind will now raise a SIGILL signal which will ==9861== probably kill your program.
==9861==
==9861== Process terminating with default action of signal 4 (SIGILL) ==9861== Illegal opcode at address 0x114BA050
==9861== at 0x114BA050: ???
==9861==

Quoted 8 lines

This is on a RedHat 9 system:

uname -a
Linux dell 2.4.20-31.9smp #1 SMP Tue Apr 13 17:40:10 EDT 2004 i686 i686
i386 GNU/Linux

gcc --version
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

I can't reproduce the problem, with valgrind or without. I also tried recompiling with different gccs but still everything works fine. I have retired all real i386 machines so my tests were run inside an i386 chroot on an amd64 system, maybe that causes problems. Does anyone else see this error?

#13 lore raw
In reply to Robert Reif
Quoted 4 lines · Robert Reif

running under valgrind produces this:

valgrind --trace-children=yes qemu-system-sparc -kernel vmlinux-2.6.11+tcx -initrd linux.img -append "root=/dev/ram"

Minor side-note. When running qemu on valgrind, you should give valgrind the --smc-check=all flag, so that it correctly handles the situation where qemu generates new code into an area where previously there was other code (iow, after the TB is flushed). Otherwise all hell breaks loose (in Valgrind-land).

I have the impression that this became much more important after Paul Brook's recent changes to support a deterministic time base, since those cause qemu to much more frequently retranslate blocks that contain I/O instructions. (Or something like that ...)

J