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