]> git.proxmox.com Git - mirror_qemu.git/blob - accel/tcg/plugin-gen.c
tcg: Split helper-proto.h
[mirror_qemu.git] / accel / tcg / plugin-gen.c
1 /*
2 * plugin-gen.c - TCG-related bits of plugin infrastructure
3 *
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.
7 *
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.
13 *
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.
21 *
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.
30 *
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.
39 *
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.
44 */
45 #include "qemu/osdep.h"
46 #include "tcg/tcg.h"
47 #include "tcg/tcg-temp-internal.h"
48 #include "tcg/tcg-op.h"
49 #include "exec/exec-all.h"
50 #include "exec/plugin-gen.h"
51 #include "exec/translator.h"
52 #include "exec/helper-proto-common.h"
53
54 #define HELPER_H "accel/tcg/plugin-helpers.h"
55 #include "exec/helper-info.c.inc"
56 #undef HELPER_H
57
58 #ifdef CONFIG_SOFTMMU
59 # define CONFIG_SOFTMMU_GATE 1
60 #else
61 # define CONFIG_SOFTMMU_GATE 0
62 #endif
63
64 /*
65 * plugin_cb_start TCG op args[]:
66 * 0: enum plugin_gen_from
67 * 1: enum plugin_gen_cb
68 * 2: set to 1 for mem callback that is a write, 0 otherwise.
69 */
70
71 enum plugin_gen_from {
72 PLUGIN_GEN_FROM_TB,
73 PLUGIN_GEN_FROM_INSN,
74 PLUGIN_GEN_FROM_MEM,
75 PLUGIN_GEN_AFTER_INSN,
76 PLUGIN_GEN_N_FROMS,
77 };
78
79 enum plugin_gen_cb {
80 PLUGIN_GEN_CB_UDATA,
81 PLUGIN_GEN_CB_INLINE,
82 PLUGIN_GEN_CB_MEM,
83 PLUGIN_GEN_ENABLE_MEM_HELPER,
84 PLUGIN_GEN_DISABLE_MEM_HELPER,
85 PLUGIN_GEN_N_CBS,
86 };
87
88 /*
89 * These helpers are stubs that get dynamically switched out for calls
90 * direct to the plugin if they are subscribed to.
91 */
92 void HELPER(plugin_vcpu_udata_cb)(uint32_t cpu_index, void *udata)
93 { }
94
95 void HELPER(plugin_vcpu_mem_cb)(unsigned int vcpu_index,
96 qemu_plugin_meminfo_t info, uint64_t vaddr,
97 void *userdata)
98 { }
99
100 static void gen_empty_udata_cb(void)
101 {
102 TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
103 TCGv_ptr udata = tcg_temp_ebb_new_ptr();
104
105 tcg_gen_movi_ptr(udata, 0);
106 tcg_gen_ld_i32(cpu_index, cpu_env,
107 -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
108 gen_helper_plugin_vcpu_udata_cb(cpu_index, udata);
109
110 tcg_temp_free_ptr(udata);
111 tcg_temp_free_i32(cpu_index);
112 }
113
114 /*
115 * For now we only support addi_i64.
116 * When we support more ops, we can generate one empty inline cb for each.
117 */
118 static void gen_empty_inline_cb(void)
119 {
120 TCGv_i64 val = tcg_temp_ebb_new_i64();
121 TCGv_ptr ptr = tcg_temp_ebb_new_ptr();
122
123 tcg_gen_movi_ptr(ptr, 0);
124 tcg_gen_ld_i64(val, ptr, 0);
125 /* pass an immediate != 0 so that it doesn't get optimized away */
126 tcg_gen_addi_i64(val, val, 0xdeadface);
127 tcg_gen_st_i64(val, ptr, 0);
128 tcg_temp_free_ptr(ptr);
129 tcg_temp_free_i64(val);
130 }
131
132 static void gen_empty_mem_cb(TCGv_i64 addr, uint32_t info)
133 {
134 TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
135 TCGv_i32 meminfo = tcg_temp_ebb_new_i32();
136 TCGv_ptr udata = tcg_temp_ebb_new_ptr();
137
138 tcg_gen_movi_i32(meminfo, info);
139 tcg_gen_movi_ptr(udata, 0);
140 tcg_gen_ld_i32(cpu_index, cpu_env,
141 -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
142
143 gen_helper_plugin_vcpu_mem_cb(cpu_index, meminfo, addr, udata);
144
145 tcg_temp_free_ptr(udata);
146 tcg_temp_free_i32(meminfo);
147 tcg_temp_free_i32(cpu_index);
148 }
149
150 /*
151 * Share the same function for enable/disable. When enabling, the NULL
152 * pointer will be overwritten later.
153 */
154 static void gen_empty_mem_helper(void)
155 {
156 TCGv_ptr ptr = tcg_temp_ebb_new_ptr();
157
158 tcg_gen_movi_ptr(ptr, 0);
159 tcg_gen_st_ptr(ptr, cpu_env, offsetof(CPUState, plugin_mem_cbs) -
160 offsetof(ArchCPU, env));
161 tcg_temp_free_ptr(ptr);
162 }
163
164 static void gen_plugin_cb_start(enum plugin_gen_from from,
165 enum plugin_gen_cb type, unsigned wr)
166 {
167 tcg_gen_plugin_cb_start(from, type, wr);
168 }
169
170 static void gen_wrapped(enum plugin_gen_from from,
171 enum plugin_gen_cb type, void (*func)(void))
172 {
173 gen_plugin_cb_start(from, type, 0);
174 func();
175 tcg_gen_plugin_cb_end();
176 }
177
178 static void plugin_gen_empty_callback(enum plugin_gen_from from)
179 {
180 switch (from) {
181 case PLUGIN_GEN_AFTER_INSN:
182 gen_wrapped(from, PLUGIN_GEN_DISABLE_MEM_HELPER,
183 gen_empty_mem_helper);
184 break;
185 case PLUGIN_GEN_FROM_INSN:
186 /*
187 * Note: plugin_gen_inject() relies on ENABLE_MEM_HELPER being
188 * the first callback of an instruction
189 */
190 gen_wrapped(from, PLUGIN_GEN_ENABLE_MEM_HELPER,
191 gen_empty_mem_helper);
192 /* fall through */
193 case PLUGIN_GEN_FROM_TB:
194 gen_wrapped(from, PLUGIN_GEN_CB_UDATA, gen_empty_udata_cb);
195 gen_wrapped(from, PLUGIN_GEN_CB_INLINE, gen_empty_inline_cb);
196 break;
197 default:
198 g_assert_not_reached();
199 }
200 }
201
202 void plugin_gen_empty_mem_callback(TCGv_i64 addr, uint32_t info)
203 {
204 enum qemu_plugin_mem_rw rw = get_plugin_meminfo_rw(info);
205
206 gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM, PLUGIN_GEN_CB_MEM, rw);
207 gen_empty_mem_cb(addr, info);
208 tcg_gen_plugin_cb_end();
209
210 gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM, PLUGIN_GEN_CB_INLINE, rw);
211 gen_empty_inline_cb();
212 tcg_gen_plugin_cb_end();
213 }
214
215 static TCGOp *find_op(TCGOp *op, TCGOpcode opc)
216 {
217 while (op) {
218 if (op->opc == opc) {
219 return op;
220 }
221 op = QTAILQ_NEXT(op, link);
222 }
223 return NULL;
224 }
225
226 static TCGOp *rm_ops_range(TCGOp *begin, TCGOp *end)
227 {
228 TCGOp *ret = QTAILQ_NEXT(end, link);
229
230 QTAILQ_REMOVE_SEVERAL(&tcg_ctx->ops, begin, end, link);
231 return ret;
232 }
233
234 /* remove all ops until (and including) plugin_cb_end */
235 static TCGOp *rm_ops(TCGOp *op)
236 {
237 TCGOp *end_op = find_op(op, INDEX_op_plugin_cb_end);
238
239 tcg_debug_assert(end_op);
240 return rm_ops_range(op, end_op);
241 }
242
243 static TCGOp *copy_op_nocheck(TCGOp **begin_op, TCGOp *op)
244 {
245 TCGOp *old_op = QTAILQ_NEXT(*begin_op, link);
246 unsigned nargs = old_op->nargs;
247
248 *begin_op = old_op;
249 op = tcg_op_insert_after(tcg_ctx, op, old_op->opc, nargs);
250 memcpy(op->args, old_op->args, sizeof(op->args[0]) * nargs);
251
252 return op;
253 }
254
255 static TCGOp *copy_op(TCGOp **begin_op, TCGOp *op, TCGOpcode opc)
256 {
257 op = copy_op_nocheck(begin_op, op);
258 tcg_debug_assert((*begin_op)->opc == opc);
259 return op;
260 }
261
262 static TCGOp *copy_const_ptr(TCGOp **begin_op, TCGOp *op, void *ptr)
263 {
264 if (UINTPTR_MAX == UINT32_MAX) {
265 /* mov_i32 */
266 op = copy_op(begin_op, op, INDEX_op_mov_i32);
267 op->args[1] = tcgv_i32_arg(tcg_constant_i32((uintptr_t)ptr));
268 } else {
269 /* mov_i64 */
270 op = copy_op(begin_op, op, INDEX_op_mov_i64);
271 op->args[1] = tcgv_i64_arg(tcg_constant_i64((uintptr_t)ptr));
272 }
273 return op;
274 }
275
276 static TCGOp *copy_ld_i64(TCGOp **begin_op, TCGOp *op)
277 {
278 if (TCG_TARGET_REG_BITS == 32) {
279 /* 2x ld_i32 */
280 op = copy_op(begin_op, op, INDEX_op_ld_i32);
281 op = copy_op(begin_op, op, INDEX_op_ld_i32);
282 } else {
283 /* ld_i64 */
284 op = copy_op(begin_op, op, INDEX_op_ld_i64);
285 }
286 return op;
287 }
288
289 static TCGOp *copy_st_i64(TCGOp **begin_op, TCGOp *op)
290 {
291 if (TCG_TARGET_REG_BITS == 32) {
292 /* 2x st_i32 */
293 op = copy_op(begin_op, op, INDEX_op_st_i32);
294 op = copy_op(begin_op, op, INDEX_op_st_i32);
295 } else {
296 /* st_i64 */
297 op = copy_op(begin_op, op, INDEX_op_st_i64);
298 }
299 return op;
300 }
301
302 static TCGOp *copy_add_i64(TCGOp **begin_op, TCGOp *op, uint64_t v)
303 {
304 if (TCG_TARGET_REG_BITS == 32) {
305 /* all 32-bit backends must implement add2_i32 */
306 g_assert(TCG_TARGET_HAS_add2_i32);
307 op = copy_op(begin_op, op, INDEX_op_add2_i32);
308 op->args[4] = tcgv_i32_arg(tcg_constant_i32(v));
309 op->args[5] = tcgv_i32_arg(tcg_constant_i32(v >> 32));
310 } else {
311 op = copy_op(begin_op, op, INDEX_op_add_i64);
312 op->args[2] = tcgv_i64_arg(tcg_constant_i64(v));
313 }
314 return op;
315 }
316
317 static TCGOp *copy_st_ptr(TCGOp **begin_op, TCGOp *op)
318 {
319 if (UINTPTR_MAX == UINT32_MAX) {
320 /* st_i32 */
321 op = copy_op(begin_op, op, INDEX_op_st_i32);
322 } else {
323 /* st_i64 */
324 op = copy_st_i64(begin_op, op);
325 }
326 return op;
327 }
328
329 static TCGOp *copy_call(TCGOp **begin_op, TCGOp *op, void *empty_func,
330 void *func, int *cb_idx)
331 {
332 TCGOp *old_op;
333 int func_idx;
334
335 /* copy all ops until the call */
336 do {
337 op = copy_op_nocheck(begin_op, op);
338 } while (op->opc != INDEX_op_call);
339
340 /* fill in the op call */
341 old_op = *begin_op;
342 TCGOP_CALLI(op) = TCGOP_CALLI(old_op);
343 TCGOP_CALLO(op) = TCGOP_CALLO(old_op);
344 tcg_debug_assert(op->life == 0);
345
346 func_idx = TCGOP_CALLO(op) + TCGOP_CALLI(op);
347 *cb_idx = func_idx;
348 op->args[func_idx] = (uintptr_t)func;
349
350 return op;
351 }
352
353 /*
354 * When we append/replace ops here we are sensitive to changing patterns of
355 * TCGOps generated by the tcg_gen_FOO calls when we generated the
356 * empty callbacks. This will assert very quickly in a debug build as
357 * we assert the ops we are replacing are the correct ones.
358 */
359 static TCGOp *append_udata_cb(const struct qemu_plugin_dyn_cb *cb,
360 TCGOp *begin_op, TCGOp *op, int *cb_idx)
361 {
362 /* const_ptr */
363 op = copy_const_ptr(&begin_op, op, cb->userp);
364
365 /* copy the ld_i32, but note that we only have to copy it once */
366 if (*cb_idx == -1) {
367 op = copy_op(&begin_op, op, INDEX_op_ld_i32);
368 } else {
369 begin_op = QTAILQ_NEXT(begin_op, link);
370 tcg_debug_assert(begin_op && begin_op->opc == INDEX_op_ld_i32);
371 }
372
373 /* call */
374 op = copy_call(&begin_op, op, HELPER(plugin_vcpu_udata_cb),
375 cb->f.vcpu_udata, cb_idx);
376
377 return op;
378 }
379
380 static TCGOp *append_inline_cb(const struct qemu_plugin_dyn_cb *cb,
381 TCGOp *begin_op, TCGOp *op,
382 int *unused)
383 {
384 /* const_ptr */
385 op = copy_const_ptr(&begin_op, op, cb->userp);
386
387 /* ld_i64 */
388 op = copy_ld_i64(&begin_op, op);
389
390 /* add_i64 */
391 op = copy_add_i64(&begin_op, op, cb->inline_insn.imm);
392
393 /* st_i64 */
394 op = copy_st_i64(&begin_op, op);
395
396 return op;
397 }
398
399 static TCGOp *append_mem_cb(const struct qemu_plugin_dyn_cb *cb,
400 TCGOp *begin_op, TCGOp *op, int *cb_idx)
401 {
402 enum plugin_gen_cb type = begin_op->args[1];
403
404 tcg_debug_assert(type == PLUGIN_GEN_CB_MEM);
405
406 /* const_i32 == mov_i32 ("info", so it remains as is) */
407 op = copy_op(&begin_op, op, INDEX_op_mov_i32);
408
409 /* const_ptr */
410 op = copy_const_ptr(&begin_op, op, cb->userp);
411
412 /* copy the ld_i32, but note that we only have to copy it once */
413 if (*cb_idx == -1) {
414 op = copy_op(&begin_op, op, INDEX_op_ld_i32);
415 } else {
416 begin_op = QTAILQ_NEXT(begin_op, link);
417 tcg_debug_assert(begin_op && begin_op->opc == INDEX_op_ld_i32);
418 }
419
420 if (type == PLUGIN_GEN_CB_MEM) {
421 /* call */
422 op = copy_call(&begin_op, op, HELPER(plugin_vcpu_mem_cb),
423 cb->f.vcpu_udata, cb_idx);
424 }
425
426 return op;
427 }
428
429 typedef TCGOp *(*inject_fn)(const struct qemu_plugin_dyn_cb *cb,
430 TCGOp *begin_op, TCGOp *op, int *intp);
431 typedef bool (*op_ok_fn)(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb);
432
433 static bool op_ok(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb)
434 {
435 return true;
436 }
437
438 static bool op_rw(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb)
439 {
440 int w;
441
442 w = op->args[2];
443 return !!(cb->rw & (w + 1));
444 }
445
446 static void inject_cb_type(const GArray *cbs, TCGOp *begin_op,
447 inject_fn inject, op_ok_fn ok)
448 {
449 TCGOp *end_op;
450 TCGOp *op;
451 int cb_idx = -1;
452 int i;
453
454 if (!cbs || cbs->len == 0) {
455 rm_ops(begin_op);
456 return;
457 }
458
459 end_op = find_op(begin_op, INDEX_op_plugin_cb_end);
460 tcg_debug_assert(end_op);
461
462 op = end_op;
463 for (i = 0; i < cbs->len; i++) {
464 struct qemu_plugin_dyn_cb *cb =
465 &g_array_index(cbs, struct qemu_plugin_dyn_cb, i);
466
467 if (!ok(begin_op, cb)) {
468 continue;
469 }
470 op = inject(cb, begin_op, op, &cb_idx);
471 }
472 rm_ops_range(begin_op, end_op);
473 }
474
475 static void
476 inject_udata_cb(const GArray *cbs, TCGOp *begin_op)
477 {
478 inject_cb_type(cbs, begin_op, append_udata_cb, op_ok);
479 }
480
481 static void
482 inject_inline_cb(const GArray *cbs, TCGOp *begin_op, op_ok_fn ok)
483 {
484 inject_cb_type(cbs, begin_op, append_inline_cb, ok);
485 }
486
487 static void
488 inject_mem_cb(const GArray *cbs, TCGOp *begin_op)
489 {
490 inject_cb_type(cbs, begin_op, append_mem_cb, op_rw);
491 }
492
493 /* we could change the ops in place, but we can reuse more code by copying */
494 static void inject_mem_helper(TCGOp *begin_op, GArray *arr)
495 {
496 TCGOp *orig_op = begin_op;
497 TCGOp *end_op;
498 TCGOp *op;
499
500 end_op = find_op(begin_op, INDEX_op_plugin_cb_end);
501 tcg_debug_assert(end_op);
502
503 /* const ptr */
504 op = copy_const_ptr(&begin_op, end_op, arr);
505
506 /* st_ptr */
507 op = copy_st_ptr(&begin_op, op);
508
509 rm_ops_range(orig_op, end_op);
510 }
511
512 /*
513 * Tracking memory accesses performed from helpers requires extra work.
514 * If an instruction is emulated with helpers, we do two things:
515 * (1) copy the CB descriptors, and keep track of it so that they can be
516 * freed later on, and (2) point CPUState.plugin_mem_cbs to the descriptors, so
517 * that we can read them at run-time (i.e. when the helper executes).
518 * This run-time access is performed from qemu_plugin_vcpu_mem_cb.
519 *
520 * Note that plugin_gen_disable_mem_helpers undoes (2). Since it
521 * is possible that the code we generate after the instruction is
522 * dead, we also add checks before generating tb_exit etc.
523 */
524 static void inject_mem_enable_helper(struct qemu_plugin_tb *ptb,
525 struct qemu_plugin_insn *plugin_insn,
526 TCGOp *begin_op)
527 {
528 GArray *cbs[2];
529 GArray *arr;
530 size_t n_cbs, i;
531
532 cbs[0] = plugin_insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR];
533 cbs[1] = plugin_insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE];
534
535 n_cbs = 0;
536 for (i = 0; i < ARRAY_SIZE(cbs); i++) {
537 n_cbs += cbs[i]->len;
538 }
539
540 plugin_insn->mem_helper = plugin_insn->calls_helpers && n_cbs;
541 if (likely(!plugin_insn->mem_helper)) {
542 rm_ops(begin_op);
543 return;
544 }
545 ptb->mem_helper = true;
546
547 arr = g_array_sized_new(false, false,
548 sizeof(struct qemu_plugin_dyn_cb), n_cbs);
549
550 for (i = 0; i < ARRAY_SIZE(cbs); i++) {
551 g_array_append_vals(arr, cbs[i]->data, cbs[i]->len);
552 }
553
554 qemu_plugin_add_dyn_cb_arr(arr);
555 inject_mem_helper(begin_op, arr);
556 }
557
558 static void inject_mem_disable_helper(struct qemu_plugin_insn *plugin_insn,
559 TCGOp *begin_op)
560 {
561 if (likely(!plugin_insn->mem_helper)) {
562 rm_ops(begin_op);
563 return;
564 }
565 inject_mem_helper(begin_op, NULL);
566 }
567
568 /* called before finishing a TB with exit_tb, goto_tb or goto_ptr */
569 void plugin_gen_disable_mem_helpers(void)
570 {
571 /*
572 * We could emit the clearing unconditionally and be done. However, this can
573 * be wasteful if for instance plugins don't track memory accesses, or if
574 * most TBs don't use helpers. Instead, emit the clearing iff the TB calls
575 * helpers that might access guest memory.
576 *
577 * Note: we do not reset plugin_tb->mem_helper here; a TB might have several
578 * exit points, and we want to emit the clearing from all of them.
579 */
580 if (!tcg_ctx->plugin_tb->mem_helper) {
581 return;
582 }
583 tcg_gen_st_ptr(tcg_constant_ptr(NULL), cpu_env,
584 offsetof(CPUState, plugin_mem_cbs) - offsetof(ArchCPU, env));
585 }
586
587 static void plugin_gen_tb_udata(const struct qemu_plugin_tb *ptb,
588 TCGOp *begin_op)
589 {
590 inject_udata_cb(ptb->cbs[PLUGIN_CB_REGULAR], begin_op);
591 }
592
593 static void plugin_gen_tb_inline(const struct qemu_plugin_tb *ptb,
594 TCGOp *begin_op)
595 {
596 inject_inline_cb(ptb->cbs[PLUGIN_CB_INLINE], begin_op, op_ok);
597 }
598
599 static void plugin_gen_insn_udata(const struct qemu_plugin_tb *ptb,
600 TCGOp *begin_op, int insn_idx)
601 {
602 struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
603
604 inject_udata_cb(insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR], begin_op);
605 }
606
607 static void plugin_gen_insn_inline(const struct qemu_plugin_tb *ptb,
608 TCGOp *begin_op, int insn_idx)
609 {
610 struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
611 inject_inline_cb(insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_INLINE],
612 begin_op, op_ok);
613 }
614
615 static void plugin_gen_mem_regular(const struct qemu_plugin_tb *ptb,
616 TCGOp *begin_op, int insn_idx)
617 {
618 struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
619 inject_mem_cb(insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR], begin_op);
620 }
621
622 static void plugin_gen_mem_inline(const struct qemu_plugin_tb *ptb,
623 TCGOp *begin_op, int insn_idx)
624 {
625 const GArray *cbs;
626 struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
627
628 cbs = insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE];
629 inject_inline_cb(cbs, begin_op, op_rw);
630 }
631
632 static void plugin_gen_enable_mem_helper(struct qemu_plugin_tb *ptb,
633 TCGOp *begin_op, int insn_idx)
634 {
635 struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
636 inject_mem_enable_helper(ptb, insn, begin_op);
637 }
638
639 static void plugin_gen_disable_mem_helper(struct qemu_plugin_tb *ptb,
640 TCGOp *begin_op, int insn_idx)
641 {
642 struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
643 inject_mem_disable_helper(insn, begin_op);
644 }
645
646 /* #define DEBUG_PLUGIN_GEN_OPS */
647 static void pr_ops(void)
648 {
649 #ifdef DEBUG_PLUGIN_GEN_OPS
650 TCGOp *op;
651 int i = 0;
652
653 QTAILQ_FOREACH(op, &tcg_ctx->ops, link) {
654 const char *name = "";
655 const char *type = "";
656
657 if (op->opc == INDEX_op_plugin_cb_start) {
658 switch (op->args[0]) {
659 case PLUGIN_GEN_FROM_TB:
660 name = "tb";
661 break;
662 case PLUGIN_GEN_FROM_INSN:
663 name = "insn";
664 break;
665 case PLUGIN_GEN_FROM_MEM:
666 name = "mem";
667 break;
668 case PLUGIN_GEN_AFTER_INSN:
669 name = "after insn";
670 break;
671 default:
672 break;
673 }
674 switch (op->args[1]) {
675 case PLUGIN_GEN_CB_UDATA:
676 type = "udata";
677 break;
678 case PLUGIN_GEN_CB_INLINE:
679 type = "inline";
680 break;
681 case PLUGIN_GEN_CB_MEM:
682 type = "mem";
683 break;
684 case PLUGIN_GEN_ENABLE_MEM_HELPER:
685 type = "enable mem helper";
686 break;
687 case PLUGIN_GEN_DISABLE_MEM_HELPER:
688 type = "disable mem helper";
689 break;
690 default:
691 break;
692 }
693 }
694 printf("op[%2i]: %s %s %s\n", i, tcg_op_defs[op->opc].name, name, type);
695 i++;
696 }
697 #endif
698 }
699
700 static void plugin_gen_inject(struct qemu_plugin_tb *plugin_tb)
701 {
702 TCGOp *op;
703 int insn_idx = -1;
704
705 pr_ops();
706
707 QTAILQ_FOREACH(op, &tcg_ctx->ops, link) {
708 switch (op->opc) {
709 case INDEX_op_insn_start:
710 insn_idx++;
711 break;
712 case INDEX_op_plugin_cb_start:
713 {
714 enum plugin_gen_from from = op->args[0];
715 enum plugin_gen_cb type = op->args[1];
716
717 switch (from) {
718 case PLUGIN_GEN_FROM_TB:
719 {
720 g_assert(insn_idx == -1);
721
722 switch (type) {
723 case PLUGIN_GEN_CB_UDATA:
724 plugin_gen_tb_udata(plugin_tb, op);
725 break;
726 case PLUGIN_GEN_CB_INLINE:
727 plugin_gen_tb_inline(plugin_tb, op);
728 break;
729 default:
730 g_assert_not_reached();
731 }
732 break;
733 }
734 case PLUGIN_GEN_FROM_INSN:
735 {
736 g_assert(insn_idx >= 0);
737
738 switch (type) {
739 case PLUGIN_GEN_CB_UDATA:
740 plugin_gen_insn_udata(plugin_tb, op, insn_idx);
741 break;
742 case PLUGIN_GEN_CB_INLINE:
743 plugin_gen_insn_inline(plugin_tb, op, insn_idx);
744 break;
745 case PLUGIN_GEN_ENABLE_MEM_HELPER:
746 plugin_gen_enable_mem_helper(plugin_tb, op, insn_idx);
747 break;
748 default:
749 g_assert_not_reached();
750 }
751 break;
752 }
753 case PLUGIN_GEN_FROM_MEM:
754 {
755 g_assert(insn_idx >= 0);
756
757 switch (type) {
758 case PLUGIN_GEN_CB_MEM:
759 plugin_gen_mem_regular(plugin_tb, op, insn_idx);
760 break;
761 case PLUGIN_GEN_CB_INLINE:
762 plugin_gen_mem_inline(plugin_tb, op, insn_idx);
763 break;
764 default:
765 g_assert_not_reached();
766 }
767
768 break;
769 }
770 case PLUGIN_GEN_AFTER_INSN:
771 {
772 g_assert(insn_idx >= 0);
773
774 switch (type) {
775 case PLUGIN_GEN_DISABLE_MEM_HELPER:
776 plugin_gen_disable_mem_helper(plugin_tb, op, insn_idx);
777 break;
778 default:
779 g_assert_not_reached();
780 }
781 break;
782 }
783 default:
784 g_assert_not_reached();
785 }
786 break;
787 }
788 default:
789 /* plugins don't care about any other ops */
790 break;
791 }
792 }
793 pr_ops();
794 }
795
796 bool plugin_gen_tb_start(CPUState *cpu, const DisasContextBase *db,
797 bool mem_only)
798 {
799 bool ret = false;
800
801 if (test_bit(QEMU_PLUGIN_EV_VCPU_TB_TRANS, cpu->plugin_mask)) {
802 struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
803 int i;
804
805 /* reset callbacks */
806 for (i = 0; i < PLUGIN_N_CB_SUBTYPES; i++) {
807 if (ptb->cbs[i]) {
808 g_array_set_size(ptb->cbs[i], 0);
809 }
810 }
811 ptb->n = 0;
812
813 ret = true;
814
815 ptb->vaddr = db->pc_first;
816 ptb->vaddr2 = -1;
817 ptb->haddr1 = db->host_addr[0];
818 ptb->haddr2 = NULL;
819 ptb->mem_only = mem_only;
820 ptb->mem_helper = false;
821
822 plugin_gen_empty_callback(PLUGIN_GEN_FROM_TB);
823 }
824
825 tcg_ctx->plugin_insn = NULL;
826
827 return ret;
828 }
829
830 void plugin_gen_insn_start(CPUState *cpu, const DisasContextBase *db)
831 {
832 struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
833 struct qemu_plugin_insn *pinsn;
834
835 pinsn = qemu_plugin_tb_insn_get(ptb, db->pc_next);
836 tcg_ctx->plugin_insn = pinsn;
837 plugin_gen_empty_callback(PLUGIN_GEN_FROM_INSN);
838
839 /*
840 * Detect page crossing to get the new host address.
841 * Note that we skip this when haddr1 == NULL, e.g. when we're
842 * fetching instructions from a region not backed by RAM.
843 */
844 if (ptb->haddr1 == NULL) {
845 pinsn->haddr = NULL;
846 } else if (is_same_page(db, db->pc_next)) {
847 pinsn->haddr = ptb->haddr1 + pinsn->vaddr - ptb->vaddr;
848 } else {
849 if (ptb->vaddr2 == -1) {
850 ptb->vaddr2 = TARGET_PAGE_ALIGN(db->pc_first);
851 get_page_addr_code_hostp(cpu->env_ptr, ptb->vaddr2, &ptb->haddr2);
852 }
853 pinsn->haddr = ptb->haddr2 + pinsn->vaddr - ptb->vaddr2;
854 }
855 }
856
857 void plugin_gen_insn_end(void)
858 {
859 plugin_gen_empty_callback(PLUGIN_GEN_AFTER_INSN);
860 }
861
862 /*
863 * There are cases where we never get to finalise a translation - for
864 * example a page fault during translation. As a result we shouldn't
865 * do any clean-up here and make sure things are reset in
866 * plugin_gen_tb_start.
867 */
868 void plugin_gen_tb_end(CPUState *cpu)
869 {
870 struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
871
872 /* collect instrumentation requests */
873 qemu_plugin_tb_trans_cb(cpu, ptb);
874
875 /* inject the instrumentation at the appropriate places */
876 plugin_gen_inject(ptb);
877 }