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