qemu/include/exec/gen-icount.h
<<
>>
Prefs
   1#ifndef GEN_ICOUNT_H
   2#define GEN_ICOUNT_H
   3
   4#include "qemu/timer.h"
   5
   6/* Helpers for instruction counting code generation.  */
   7
   8static TCGOp *icount_start_insn;
   9
  10static inline void gen_io_start(void)
  11{
  12    TCGv_i32 tmp = tcg_const_i32(1);
  13    tcg_gen_st_i32(tmp, cpu_env,
  14                   offsetof(ArchCPU, parent_obj.can_do_io) -
  15                   offsetof(ArchCPU, env));
  16    tcg_temp_free_i32(tmp);
  17}
  18
  19/*
  20 * cpu->can_do_io is cleared automatically at the beginning of
  21 * each translation block.  The cost is minimal and only paid
  22 * for -icount, plus it would be very easy to forget doing it
  23 * in the translator.  Therefore, backends only need to call
  24 * gen_io_start.
  25 */
  26static inline void gen_io_end(void)
  27{
  28    TCGv_i32 tmp = tcg_const_i32(0);
  29    tcg_gen_st_i32(tmp, cpu_env,
  30                   offsetof(ArchCPU, parent_obj.can_do_io) -
  31                   offsetof(ArchCPU, env));
  32    tcg_temp_free_i32(tmp);
  33}
  34
  35static inline void gen_tb_start(const TranslationBlock *tb)
  36{
  37    TCGv_i32 count;
  38
  39    tcg_ctx->exitreq_label = gen_new_label();
  40    if (tb_cflags(tb) & CF_USE_ICOUNT) {
  41        count = tcg_temp_local_new_i32();
  42    } else {
  43        count = tcg_temp_new_i32();
  44    }
  45
  46    tcg_gen_ld_i32(count, cpu_env,
  47                   offsetof(ArchCPU, neg.icount_decr.u32) -
  48                   offsetof(ArchCPU, env));
  49
  50    if (tb_cflags(tb) & CF_USE_ICOUNT) {
  51        /*
  52         * We emit a sub with a dummy immediate argument. Keep the insn index
  53         * of the sub so that we later (when we know the actual insn count)
  54         * can update the argument with the actual insn count.
  55         */
  56        tcg_gen_sub_i32(count, count, tcg_constant_i32(0));
  57        icount_start_insn = tcg_last_op();
  58    }
  59
  60    tcg_gen_brcondi_i32(TCG_COND_LT, count, 0, tcg_ctx->exitreq_label);
  61
  62    if (tb_cflags(tb) & CF_USE_ICOUNT) {
  63        tcg_gen_st16_i32(count, cpu_env,
  64                         offsetof(ArchCPU, neg.icount_decr.u16.low) -
  65                         offsetof(ArchCPU, env));
  66        gen_io_end();
  67    }
  68
  69    tcg_temp_free_i32(count);
  70}
  71
  72static inline void gen_tb_end(const TranslationBlock *tb, int num_insns)
  73{
  74    if (tb_cflags(tb) & CF_USE_ICOUNT) {
  75        /*
  76         * Update the num_insn immediate parameter now that we know
  77         * the actual insn count.
  78         */
  79        tcg_set_insn_param(icount_start_insn, 2,
  80                           tcgv_i32_arg(tcg_constant_i32(num_insns)));
  81    }
  82
  83    gen_set_label(tcg_ctx->exitreq_label);
  84    tcg_gen_exit_tb(tb, TB_EXIT_REQUESTED);
  85}
  86
  87#endif
  88