2 * plugin-gen.c - TCG-related bits of plugin infrastructure
4 * Copyright (C) 2018, Emilio G. Cota <cota@braap.org>
5 * License: GNU GPL, version 2 or later.
6 * See the COPYING file in the top-level directory.
8 * We support instrumentation at an instruction granularity. That is,
9 * if a plugin wants to instrument the memory accesses performed by a
10 * particular instruction, it can just do that instead of instrumenting
11 * all memory accesses. Thus, in order to do this we first have to
12 * translate a TB, so that plugins can decide what/where to instrument.
14 * Injecting the desired instrumentation could be done with a second
15 * translation pass that combined the instrumentation requests, but that
16 * would be ugly and inefficient since we would decode the guest code twice.
17 * Instead, during TB translation we add "empty" instrumentation calls for all
18 * possible instrumentation events, and then once we collect the instrumentation
19 * requests from plugins, we either "fill in" those empty events or remove them
20 * if they have no requests.
22 * When "filling in" an event we first copy the empty callback's TCG ops. This
23 * might seem unnecessary, but it is done to support an arbitrary number
24 * of callbacks per event. Take for example a regular instruction callback.
25 * We first generate a callback to an empty helper function. Then, if two
26 * plugins register one callback each for this instruction, we make two copies
27 * of the TCG ops generated for the empty callback, substituting the function
28 * pointer that points to the empty helper function with the plugins' desired
29 * callback functions. After that we remove the empty callback's ops.
31 * Note that the location in TCGOp.args[] of the pointer to a helper function
32 * varies across different guest and host architectures. Instead of duplicating
33 * the logic that figures this out, we rely on the fact that the empty
34 * callbacks point to empty functions that are unique pointers in the program.
35 * Thus, to find the right location we just have to look for a match in
36 * TCGOp.args[]. This is the main reason why we first copy an empty callback's
37 * TCG ops and then fill them in; regardless of whether we have one or many
38 * callbacks for that event, the logic to add all of them is the same.
40 * When generating more than one callback per event, we make a small
41 * optimization to avoid generating redundant operations. For instance, for the
42 * second and all subsequent callbacks of an event, we do not need to reload the
43 * CPU's index into a TCG temp, since the first callback did it already.
45 #include "qemu/osdep.h"
46 #include "qemu/plugin.h"
49 #include "tcg/tcg-temp-internal.h"
50 #include "tcg/tcg-op.h"
51 #include "exec/exec-all.h"
52 #include "exec/plugin-gen.h"
53 #include "exec/translator.h"
55 enum plugin_gen_from
{
58 PLUGIN_GEN_AFTER_INSN
,
62 static void plugin_gen_empty_callback(enum plugin_gen_from from
)
65 case PLUGIN_GEN_AFTER_INSN
:
66 case PLUGIN_GEN_FROM_TB
:
67 case PLUGIN_GEN_FROM_INSN
:
68 tcg_gen_plugin_cb(from
);
71 g_assert_not_reached();
75 /* called before finishing a TB with exit_tb, goto_tb or goto_ptr */
76 void plugin_gen_disable_mem_helpers(void)
78 if (tcg_ctx
->plugin_insn
) {
79 tcg_gen_plugin_cb(PLUGIN_GEN_AFTER_TB
);
83 static void gen_enable_mem_helper(struct qemu_plugin_tb
*ptb
,
84 struct qemu_plugin_insn
*insn
)
91 * Tracking memory accesses performed from helpers requires extra work.
92 * If an instruction is emulated with helpers, we do two things:
93 * (1) copy the CB descriptors, and keep track of it so that they can be
94 * freed later on, and (2) point CPUState.plugin_mem_cbs to the
95 * descriptors, so that we can read them at run-time
96 * (i.e. when the helper executes).
97 * This run-time access is performed from qemu_plugin_vcpu_mem_cb.
99 * Note that plugin_gen_disable_mem_helpers undoes (2). Since it
100 * is possible that the code we generate after the instruction is
101 * dead, we also add checks before generating tb_exit etc.
103 if (!insn
->calls_helpers
) {
107 cbs
[0] = insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_REGULAR
];
108 cbs
[1] = insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_INLINE
];
109 n_cbs
= cbs
[0]->len
+ cbs
[1]->len
;
112 insn
->mem_helper
= false;
115 insn
->mem_helper
= true;
116 ptb
->mem_helper
= true;
118 arr
= g_array_sized_new(false, false,
119 sizeof(struct qemu_plugin_dyn_cb
), n_cbs
);
120 g_array_append_vals(arr
, cbs
[0]->data
, cbs
[0]->len
);
121 g_array_append_vals(arr
, cbs
[1]->data
, cbs
[1]->len
);
123 qemu_plugin_add_dyn_cb_arr(arr
);
125 tcg_gen_st_ptr(tcg_constant_ptr((intptr_t)arr
), tcg_env
,
126 offsetof(CPUState
, plugin_mem_cbs
) -
127 offsetof(ArchCPU
, env
));
130 static void gen_disable_mem_helper(void)
132 tcg_gen_st_ptr(tcg_constant_ptr(0), tcg_env
,
133 offsetof(CPUState
, plugin_mem_cbs
) -
134 offsetof(ArchCPU
, env
));
137 static void gen_udata_cb(struct qemu_plugin_dyn_cb
*cb
)
139 TCGv_i32 cpu_index
= tcg_temp_ebb_new_i32();
141 tcg_gen_ld_i32(cpu_index
, tcg_env
,
142 -offsetof(ArchCPU
, env
) + offsetof(CPUState
, cpu_index
));
143 tcg_gen_call2(cb
->regular
.f
.vcpu_udata
, cb
->regular
.info
, NULL
,
144 tcgv_i32_temp(cpu_index
),
145 tcgv_ptr_temp(tcg_constant_ptr(cb
->userp
)));
146 tcg_temp_free_i32(cpu_index
);
149 static void gen_inline_cb(struct qemu_plugin_dyn_cb
*cb
)
151 GArray
*arr
= cb
->inline_insn
.entry
.score
->data
;
152 size_t offset
= cb
->inline_insn
.entry
.offset
;
153 TCGv_i32 cpu_index
= tcg_temp_ebb_new_i32();
154 TCGv_i64 val
= tcg_temp_ebb_new_i64();
155 TCGv_ptr ptr
= tcg_temp_ebb_new_ptr();
157 tcg_gen_ld_i32(cpu_index
, tcg_env
,
158 -offsetof(ArchCPU
, env
) + offsetof(CPUState
, cpu_index
));
159 tcg_gen_muli_i32(cpu_index
, cpu_index
, g_array_get_element_size(arr
));
160 tcg_gen_ext_i32_ptr(ptr
, cpu_index
);
161 tcg_temp_free_i32(cpu_index
);
163 tcg_gen_addi_ptr(ptr
, ptr
, (intptr_t)arr
->data
);
164 tcg_gen_ld_i64(val
, ptr
, offset
);
165 tcg_gen_addi_i64(val
, val
, cb
->inline_insn
.imm
);
166 tcg_gen_st_i64(val
, ptr
, offset
);
168 tcg_temp_free_i64(val
);
169 tcg_temp_free_ptr(ptr
);
172 static void gen_mem_cb(struct qemu_plugin_dyn_cb
*cb
,
173 qemu_plugin_meminfo_t meminfo
, TCGv_i64 addr
)
175 TCGv_i32 cpu_index
= tcg_temp_ebb_new_i32();
177 tcg_gen_ld_i32(cpu_index
, tcg_env
,
178 -offsetof(ArchCPU
, env
) + offsetof(CPUState
, cpu_index
));
179 tcg_gen_call4(cb
->regular
.f
.vcpu_mem
, cb
->regular
.info
, NULL
,
180 tcgv_i32_temp(cpu_index
),
181 tcgv_i32_temp(tcg_constant_i32(meminfo
)),
183 tcgv_ptr_temp(tcg_constant_ptr(cb
->userp
)));
184 tcg_temp_free_i32(cpu_index
);
187 /* #define DEBUG_PLUGIN_GEN_OPS */
188 static void pr_ops(void)
190 #ifdef DEBUG_PLUGIN_GEN_OPS
194 QTAILQ_FOREACH(op
, &tcg_ctx
->ops
, link
) {
195 const char *name
= "";
196 const char *type
= "";
198 if (op
->opc
== INDEX_op_plugin_cb_start
) {
199 switch (op
->args
[0]) {
200 case PLUGIN_GEN_FROM_TB
:
203 case PLUGIN_GEN_FROM_INSN
:
206 case PLUGIN_GEN_FROM_MEM
:
209 case PLUGIN_GEN_AFTER_INSN
:
215 switch (op
->args
[1]) {
216 case PLUGIN_GEN_CB_UDATA
:
219 case PLUGIN_GEN_CB_INLINE
:
222 case PLUGIN_GEN_CB_MEM
:
225 case PLUGIN_GEN_ENABLE_MEM_HELPER
:
226 type
= "enable mem helper";
228 case PLUGIN_GEN_DISABLE_MEM_HELPER
:
229 type
= "disable mem helper";
235 printf("op[%2i]: %s %s %s\n", i
, tcg_op_defs
[op
->opc
].name
, name
, type
);
241 static void plugin_gen_inject(struct qemu_plugin_tb
*plugin_tb
)
249 * While injecting code, we cannot afford to reuse any ebb temps
250 * that might be live within the existing opcode stream.
251 * The simplest solution is to release them all and create new.
253 memset(tcg_ctx
->free_temps
, 0, sizeof(tcg_ctx
->free_temps
));
255 QTAILQ_FOREACH_SAFE(op
, &tcg_ctx
->ops
, link
, next
) {
257 case INDEX_op_insn_start
:
261 case INDEX_op_plugin_cb
:
263 enum plugin_gen_from from
= op
->args
[0];
264 struct qemu_plugin_insn
*insn
= NULL
;
269 insn
= g_ptr_array_index(plugin_tb
->insns
, insn_idx
);
272 tcg_ctx
->emit_before_op
= op
;
275 case PLUGIN_GEN_AFTER_TB
:
276 if (plugin_tb
->mem_helper
) {
277 gen_disable_mem_helper();
281 case PLUGIN_GEN_AFTER_INSN
:
282 assert(insn
!= NULL
);
283 if (insn
->mem_helper
) {
284 gen_disable_mem_helper();
288 case PLUGIN_GEN_FROM_TB
:
289 assert(insn
== NULL
);
291 cbs
= plugin_tb
->cbs
[PLUGIN_CB_REGULAR
];
292 for (i
= 0, n
= (cbs
? cbs
->len
: 0); i
< n
; i
++) {
293 struct qemu_plugin_dyn_cb
*cb
=
294 &g_array_index(cbs
, struct qemu_plugin_dyn_cb
, i
);
298 cbs
= plugin_tb
->cbs
[PLUGIN_CB_INLINE
];
299 for (i
= 0, n
= (cbs
? cbs
->len
: 0); i
< n
; i
++) {
300 struct qemu_plugin_dyn_cb
*cb
=
301 &g_array_index(cbs
, struct qemu_plugin_dyn_cb
, i
);
306 case PLUGIN_GEN_FROM_INSN
:
307 assert(insn
!= NULL
);
309 gen_enable_mem_helper(plugin_tb
, insn
);
311 cbs
= insn
->cbs
[PLUGIN_CB_INSN
][PLUGIN_CB_REGULAR
];
312 for (i
= 0, n
= (cbs
? cbs
->len
: 0); i
< n
; i
++) {
313 struct qemu_plugin_dyn_cb
*cb
=
314 &g_array_index(cbs
, struct qemu_plugin_dyn_cb
, i
);
318 cbs
= insn
->cbs
[PLUGIN_CB_INSN
][PLUGIN_CB_INLINE
];
319 for (i
= 0, n
= (cbs
? cbs
->len
: 0); i
< n
; i
++) {
320 struct qemu_plugin_dyn_cb
*cb
=
321 &g_array_index(cbs
, struct qemu_plugin_dyn_cb
, i
);
327 g_assert_not_reached();
330 tcg_ctx
->emit_before_op
= NULL
;
331 tcg_op_remove(tcg_ctx
, op
);
335 case INDEX_op_plugin_mem_cb
:
337 TCGv_i64 addr
= temp_tcgv_i64(arg_temp(op
->args
[0]));
338 qemu_plugin_meminfo_t meminfo
= op
->args
[1];
339 struct qemu_plugin_insn
*insn
;
343 assert(insn_idx
>= 0);
344 insn
= g_ptr_array_index(plugin_tb
->insns
, insn_idx
);
345 rw
= qemu_plugin_mem_is_store(meminfo
) ? 2 : 1;
347 tcg_ctx
->emit_before_op
= op
;
349 cbs
= insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_REGULAR
];
350 for (i
= 0, n
= (cbs
? cbs
->len
: 0); i
< n
; i
++) {
351 struct qemu_plugin_dyn_cb
*cb
=
352 &g_array_index(cbs
, struct qemu_plugin_dyn_cb
, i
);
354 gen_mem_cb(cb
, meminfo
, addr
);
358 cbs
= insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_INLINE
];
359 for (i
= 0, n
= (cbs
? cbs
->len
: 0); i
< n
; i
++) {
360 struct qemu_plugin_dyn_cb
*cb
=
361 &g_array_index(cbs
, struct qemu_plugin_dyn_cb
, i
);
367 tcg_ctx
->emit_before_op
= NULL
;
368 tcg_op_remove(tcg_ctx
, op
);
373 /* plugins don't care about any other ops */
380 bool plugin_gen_tb_start(CPUState
*cpu
, const DisasContextBase
*db
,
385 if (test_bit(QEMU_PLUGIN_EV_VCPU_TB_TRANS
, cpu
->plugin_state
->event_mask
)) {
386 struct qemu_plugin_tb
*ptb
= tcg_ctx
->plugin_tb
;
389 /* reset callbacks */
390 for (i
= 0; i
< PLUGIN_N_CB_SUBTYPES
; i
++) {
392 g_array_set_size(ptb
->cbs
[i
], 0);
399 ptb
->vaddr
= db
->pc_first
;
401 ptb
->haddr1
= db
->host_addr
[0];
403 ptb
->mem_only
= mem_only
;
404 ptb
->mem_helper
= false;
406 plugin_gen_empty_callback(PLUGIN_GEN_FROM_TB
);
409 tcg_ctx
->plugin_insn
= NULL
;
414 void plugin_gen_insn_start(CPUState
*cpu
, const DisasContextBase
*db
)
416 struct qemu_plugin_tb
*ptb
= tcg_ctx
->plugin_tb
;
417 struct qemu_plugin_insn
*pinsn
;
419 pinsn
= qemu_plugin_tb_insn_get(ptb
, db
->pc_next
);
420 tcg_ctx
->plugin_insn
= pinsn
;
421 plugin_gen_empty_callback(PLUGIN_GEN_FROM_INSN
);
424 * Detect page crossing to get the new host address.
425 * Note that we skip this when haddr1 == NULL, e.g. when we're
426 * fetching instructions from a region not backed by RAM.
428 if (ptb
->haddr1
== NULL
) {
430 } else if (is_same_page(db
, db
->pc_next
)) {
431 pinsn
->haddr
= ptb
->haddr1
+ pinsn
->vaddr
- ptb
->vaddr
;
433 if (ptb
->vaddr2
== -1) {
434 ptb
->vaddr2
= TARGET_PAGE_ALIGN(db
->pc_first
);
435 get_page_addr_code_hostp(cpu_env(cpu
), ptb
->vaddr2
, &ptb
->haddr2
);
437 pinsn
->haddr
= ptb
->haddr2
+ pinsn
->vaddr
- ptb
->vaddr2
;
441 void plugin_gen_insn_end(void)
443 plugin_gen_empty_callback(PLUGIN_GEN_AFTER_INSN
);
447 * There are cases where we never get to finalise a translation - for
448 * example a page fault during translation. As a result we shouldn't
449 * do any clean-up here and make sure things are reset in
450 * plugin_gen_tb_start.
452 void plugin_gen_tb_end(CPUState
*cpu
, size_t num_insns
)
454 struct qemu_plugin_tb
*ptb
= tcg_ctx
->plugin_tb
;
456 /* translator may have removed instructions, update final count */
457 g_assert(num_insns
<= ptb
->n
);
460 /* collect instrumentation requests */
461 qemu_plugin_tb_trans_cb(cpu
, ptb
);
463 /* inject the instrumentation at the appropriate places */
464 plugin_gen_inject(ptb
);