xref: /openbmc/qemu/accel/tcg/plugin-gen.c (revision f86fd4d8)
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 "plugin_cb" marker opcodes
18  * for all possible instrumentation events, and then once we collect the
19  * instrumentation requests from plugins, we generate code for those markers
20  * or remove them if they have no requests.
21  */
22 #include "qemu/osdep.h"
23 #include "qemu/plugin.h"
24 #include "qemu/log.h"
25 #include "cpu.h"
26 #include "tcg/tcg.h"
27 #include "tcg/tcg-temp-internal.h"
28 #include "tcg/tcg-op.h"
29 #include "exec/exec-all.h"
30 #include "exec/plugin-gen.h"
31 #include "exec/translator.h"
32 
33 enum plugin_gen_from {
34     PLUGIN_GEN_FROM_TB,
35     PLUGIN_GEN_FROM_INSN,
36     PLUGIN_GEN_AFTER_INSN,
37     PLUGIN_GEN_AFTER_TB,
38 };
39 
40 /* called before finishing a TB with exit_tb, goto_tb or goto_ptr */
41 void plugin_gen_disable_mem_helpers(void)
42 {
43     if (tcg_ctx->plugin_insn) {
44         tcg_gen_plugin_cb(PLUGIN_GEN_AFTER_TB);
45     }
46 }
47 
48 static void gen_enable_mem_helper(struct qemu_plugin_tb *ptb,
49                                   struct qemu_plugin_insn *insn)
50 {
51     GArray *arr;
52     size_t len;
53 
54     /*
55      * Tracking memory accesses performed from helpers requires extra work.
56      * If an instruction is emulated with helpers, we do two things:
57      * (1) copy the CB descriptors, and keep track of it so that they can be
58      * freed later on, and (2) point CPUState.neg.plugin_mem_cbs to the
59      * descriptors, so that we can read them at run-time
60      * (i.e. when the helper executes).
61      * This run-time access is performed from qemu_plugin_vcpu_mem_cb.
62      *
63      * Note that plugin_gen_disable_mem_helpers undoes (2). Since it
64      * is possible that the code we generate after the instruction is
65      * dead, we also add checks before generating tb_exit etc.
66      */
67     if (!insn->calls_helpers) {
68         return;
69     }
70 
71     if (!insn->mem_cbs || !insn->mem_cbs->len) {
72         insn->mem_helper = false;
73         return;
74     }
75     insn->mem_helper = true;
76     ptb->mem_helper = true;
77 
78     /*
79      * TODO: It seems like we should be able to use ref/unref
80      * to avoid needing to actually copy this array.
81      * Alternately, perhaps we could allocate new memory adjacent
82      * to the TranslationBlock itself, so that we do not have to
83      * actively manage the lifetime after this.
84      */
85     len = insn->mem_cbs->len;
86     arr = g_array_sized_new(false, false,
87                             sizeof(struct qemu_plugin_dyn_cb), len);
88     memcpy(arr->data, insn->mem_cbs->data,
89            len * sizeof(struct qemu_plugin_dyn_cb));
90     qemu_plugin_add_dyn_cb_arr(arr);
91 
92     tcg_gen_st_ptr(tcg_constant_ptr((intptr_t)arr), tcg_env,
93                    offsetof(CPUState, neg.plugin_mem_cbs) -
94                    offsetof(ArchCPU, env));
95 }
96 
97 static void gen_disable_mem_helper(void)
98 {
99     tcg_gen_st_ptr(tcg_constant_ptr(0), tcg_env,
100                    offsetof(CPUState, neg.plugin_mem_cbs) -
101                    offsetof(ArchCPU, env));
102 }
103 
104 static void gen_udata_cb(struct qemu_plugin_regular_cb *cb)
105 {
106     TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
107 
108     tcg_gen_ld_i32(cpu_index, tcg_env,
109                    -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
110     tcg_gen_call2(cb->f.vcpu_udata, cb->info, NULL,
111                   tcgv_i32_temp(cpu_index),
112                   tcgv_ptr_temp(tcg_constant_ptr(cb->userp)));
113     tcg_temp_free_i32(cpu_index);
114 }
115 
116 static TCGv_ptr gen_plugin_u64_ptr(qemu_plugin_u64 entry)
117 {
118     TCGv_ptr ptr = tcg_temp_ebb_new_ptr();
119 
120     GArray *arr = entry.score->data;
121     char *base_ptr = arr->data + entry.offset;
122     size_t entry_size = g_array_get_element_size(arr);
123 
124     TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
125     tcg_gen_ld_i32(cpu_index, tcg_env,
126                    -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
127     tcg_gen_muli_i32(cpu_index, cpu_index, entry_size);
128     tcg_gen_ext_i32_ptr(ptr, cpu_index);
129     tcg_temp_free_i32(cpu_index);
130     tcg_gen_addi_ptr(ptr, ptr, (intptr_t) base_ptr);
131 
132     return ptr;
133 }
134 
135 static TCGCond plugin_cond_to_tcgcond(enum qemu_plugin_cond cond)
136 {
137     switch (cond) {
138     case QEMU_PLUGIN_COND_EQ:
139         return TCG_COND_EQ;
140     case QEMU_PLUGIN_COND_NE:
141         return TCG_COND_NE;
142     case QEMU_PLUGIN_COND_LT:
143         return TCG_COND_LTU;
144     case QEMU_PLUGIN_COND_LE:
145         return TCG_COND_LEU;
146     case QEMU_PLUGIN_COND_GT:
147         return TCG_COND_GTU;
148     case QEMU_PLUGIN_COND_GE:
149         return TCG_COND_GEU;
150     default:
151         /* ALWAYS and NEVER conditions should never reach */
152         g_assert_not_reached();
153     }
154 }
155 
156 static void gen_udata_cond_cb(struct qemu_plugin_conditional_cb *cb)
157 {
158     TCGv_ptr ptr = gen_plugin_u64_ptr(cb->entry);
159     TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
160     TCGv_i64 val = tcg_temp_ebb_new_i64();
161     TCGLabel *after_cb = gen_new_label();
162 
163     /* Condition should be negated, as calling the cb is the "else" path */
164     TCGCond cond = tcg_invert_cond(plugin_cond_to_tcgcond(cb->cond));
165 
166     tcg_gen_ld_i64(val, ptr, 0);
167     tcg_gen_brcondi_i64(cond, val, cb->imm, after_cb);
168     tcg_gen_ld_i32(cpu_index, tcg_env,
169                    -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
170     tcg_gen_call2(cb->f.vcpu_udata, cb->info, NULL,
171                   tcgv_i32_temp(cpu_index),
172                   tcgv_ptr_temp(tcg_constant_ptr(cb->userp)));
173     gen_set_label(after_cb);
174 
175     tcg_temp_free_i64(val);
176     tcg_temp_free_i32(cpu_index);
177     tcg_temp_free_ptr(ptr);
178 }
179 
180 static void gen_inline_add_u64_cb(struct qemu_plugin_inline_cb *cb)
181 {
182     TCGv_ptr ptr = gen_plugin_u64_ptr(cb->entry);
183     TCGv_i64 val = tcg_temp_ebb_new_i64();
184 
185     tcg_gen_ld_i64(val, ptr, 0);
186     tcg_gen_addi_i64(val, val, cb->imm);
187     tcg_gen_st_i64(val, ptr, 0);
188 
189     tcg_temp_free_i64(val);
190     tcg_temp_free_ptr(ptr);
191 }
192 
193 static void gen_inline_store_u64_cb(struct qemu_plugin_inline_cb *cb)
194 {
195     TCGv_ptr ptr = gen_plugin_u64_ptr(cb->entry);
196     TCGv_i64 val = tcg_constant_i64(cb->imm);
197 
198     tcg_gen_st_i64(val, ptr, 0);
199 
200     tcg_temp_free_ptr(ptr);
201 }
202 
203 static void gen_mem_cb(struct qemu_plugin_regular_cb *cb,
204                        qemu_plugin_meminfo_t meminfo, TCGv_i64 addr)
205 {
206     TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
207 
208     tcg_gen_ld_i32(cpu_index, tcg_env,
209                    -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
210     tcg_gen_call4(cb->f.vcpu_mem, cb->info, NULL,
211                   tcgv_i32_temp(cpu_index),
212                   tcgv_i32_temp(tcg_constant_i32(meminfo)),
213                   tcgv_i64_temp(addr),
214                   tcgv_ptr_temp(tcg_constant_ptr(cb->userp)));
215     tcg_temp_free_i32(cpu_index);
216 }
217 
218 static void inject_cb(struct qemu_plugin_dyn_cb *cb)
219 
220 {
221     switch (cb->type) {
222     case PLUGIN_CB_REGULAR:
223         gen_udata_cb(&cb->regular);
224         break;
225     case PLUGIN_CB_COND:
226         gen_udata_cond_cb(&cb->cond);
227         break;
228     case PLUGIN_CB_INLINE_ADD_U64:
229         gen_inline_add_u64_cb(&cb->inline_insn);
230         break;
231     case PLUGIN_CB_INLINE_STORE_U64:
232         gen_inline_store_u64_cb(&cb->inline_insn);
233         break;
234     default:
235         g_assert_not_reached();
236     }
237 }
238 
239 static void inject_mem_cb(struct qemu_plugin_dyn_cb *cb,
240                           enum qemu_plugin_mem_rw rw,
241                           qemu_plugin_meminfo_t meminfo, TCGv_i64 addr)
242 {
243     switch (cb->type) {
244     case PLUGIN_CB_MEM_REGULAR:
245         if (rw && cb->regular.rw) {
246             gen_mem_cb(&cb->regular, meminfo, addr);
247         }
248         break;
249     case PLUGIN_CB_INLINE_ADD_U64:
250     case PLUGIN_CB_INLINE_STORE_U64:
251         if (rw && cb->inline_insn.rw) {
252             inject_cb(cb);
253         }
254         break;
255     default:
256         g_assert_not_reached();
257         break;
258     }
259 }
260 
261 static void plugin_gen_inject(struct qemu_plugin_tb *plugin_tb)
262 {
263     TCGOp *op, *next;
264     int insn_idx = -1;
265 
266     if (unlikely(qemu_loglevel_mask(LOG_TB_OP_PLUGIN)
267                  && qemu_log_in_addr_range(tcg_ctx->plugin_db->pc_first))) {
268         FILE *logfile = qemu_log_trylock();
269         if (logfile) {
270             fprintf(logfile, "OP before plugin injection:\n");
271             tcg_dump_ops(tcg_ctx, logfile, false);
272             fprintf(logfile, "\n");
273             qemu_log_unlock(logfile);
274         }
275     }
276 
277     /*
278      * While injecting code, we cannot afford to reuse any ebb temps
279      * that might be live within the existing opcode stream.
280      * The simplest solution is to release them all and create new.
281      */
282     memset(tcg_ctx->free_temps, 0, sizeof(tcg_ctx->free_temps));
283 
284     QTAILQ_FOREACH_SAFE(op, &tcg_ctx->ops, link, next) {
285         switch (op->opc) {
286         case INDEX_op_insn_start:
287             insn_idx++;
288             break;
289 
290         case INDEX_op_plugin_cb:
291         {
292             enum plugin_gen_from from = op->args[0];
293             struct qemu_plugin_insn *insn = NULL;
294             const GArray *cbs;
295             int i, n;
296 
297             if (insn_idx >= 0) {
298                 insn = g_ptr_array_index(plugin_tb->insns, insn_idx);
299             }
300 
301             tcg_ctx->emit_before_op = op;
302 
303             switch (from) {
304             case PLUGIN_GEN_AFTER_TB:
305                 if (plugin_tb->mem_helper) {
306                     gen_disable_mem_helper();
307                 }
308                 break;
309 
310             case PLUGIN_GEN_AFTER_INSN:
311                 assert(insn != NULL);
312                 if (insn->mem_helper) {
313                     gen_disable_mem_helper();
314                 }
315                 break;
316 
317             case PLUGIN_GEN_FROM_TB:
318                 assert(insn == NULL);
319 
320                 cbs = plugin_tb->cbs;
321                 for (i = 0, n = (cbs ? cbs->len : 0); i < n; i++) {
322                     inject_cb(
323                         &g_array_index(cbs, struct qemu_plugin_dyn_cb, i));
324                 }
325                 break;
326 
327             case PLUGIN_GEN_FROM_INSN:
328                 assert(insn != NULL);
329 
330                 gen_enable_mem_helper(plugin_tb, insn);
331 
332                 cbs = insn->insn_cbs;
333                 for (i = 0, n = (cbs ? cbs->len : 0); i < n; i++) {
334                     inject_cb(
335                         &g_array_index(cbs, struct qemu_plugin_dyn_cb, i));
336                 }
337                 break;
338 
339             default:
340                 g_assert_not_reached();
341             }
342 
343             tcg_ctx->emit_before_op = NULL;
344             tcg_op_remove(tcg_ctx, op);
345             break;
346         }
347 
348         case INDEX_op_plugin_mem_cb:
349         {
350             TCGv_i64 addr = temp_tcgv_i64(arg_temp(op->args[0]));
351             qemu_plugin_meminfo_t meminfo = op->args[1];
352             enum qemu_plugin_mem_rw rw =
353                 (qemu_plugin_mem_is_store(meminfo)
354                  ? QEMU_PLUGIN_MEM_W : QEMU_PLUGIN_MEM_R);
355             struct qemu_plugin_insn *insn;
356             const GArray *cbs;
357             int i, n;
358 
359             assert(insn_idx >= 0);
360             insn = g_ptr_array_index(plugin_tb->insns, insn_idx);
361 
362             tcg_ctx->emit_before_op = op;
363 
364             cbs = insn->mem_cbs;
365             for (i = 0, n = (cbs ? cbs->len : 0); i < n; i++) {
366                 inject_mem_cb(&g_array_index(cbs, struct qemu_plugin_dyn_cb, i),
367                               rw, meminfo, addr);
368             }
369 
370             tcg_ctx->emit_before_op = NULL;
371             tcg_op_remove(tcg_ctx, op);
372             break;
373         }
374 
375         default:
376             /* plugins don't care about any other ops */
377             break;
378         }
379     }
380 }
381 
382 bool plugin_gen_tb_start(CPUState *cpu, const DisasContextBase *db)
383 {
384     struct qemu_plugin_tb *ptb;
385 
386     if (!test_bit(QEMU_PLUGIN_EV_VCPU_TB_TRANS,
387                   cpu->plugin_state->event_mask)) {
388         return false;
389     }
390 
391     tcg_ctx->plugin_db = db;
392     tcg_ctx->plugin_insn = NULL;
393     ptb = tcg_ctx->plugin_tb;
394 
395     if (ptb) {
396         /* Reset callbacks */
397         if (ptb->cbs) {
398             g_array_set_size(ptb->cbs, 0);
399         }
400         ptb->n = 0;
401         ptb->mem_helper = false;
402     } else {
403         ptb = g_new0(struct qemu_plugin_tb, 1);
404         tcg_ctx->plugin_tb = ptb;
405         ptb->insns = g_ptr_array_new();
406     }
407 
408     tcg_gen_plugin_cb(PLUGIN_GEN_FROM_TB);
409     return true;
410 }
411 
412 void plugin_gen_insn_start(CPUState *cpu, const DisasContextBase *db)
413 {
414     struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
415     struct qemu_plugin_insn *insn;
416     size_t n = db->num_insns;
417     vaddr pc;
418 
419     assert(n >= 1);
420     ptb->n = n;
421     if (n <= ptb->insns->len) {
422         insn = g_ptr_array_index(ptb->insns, n - 1);
423     } else {
424         assert(n - 1 == ptb->insns->len);
425         insn = g_new0(struct qemu_plugin_insn, 1);
426         g_ptr_array_add(ptb->insns, insn);
427     }
428 
429     tcg_ctx->plugin_insn = insn;
430     insn->calls_helpers = false;
431     insn->mem_helper = false;
432     if (insn->insn_cbs) {
433         g_array_set_size(insn->insn_cbs, 0);
434     }
435     if (insn->mem_cbs) {
436         g_array_set_size(insn->mem_cbs, 0);
437     }
438 
439     pc = db->pc_next;
440     insn->vaddr = pc;
441 
442     tcg_gen_plugin_cb(PLUGIN_GEN_FROM_INSN);
443 }
444 
445 void plugin_gen_insn_end(void)
446 {
447     const DisasContextBase *db = tcg_ctx->plugin_db;
448     struct qemu_plugin_insn *pinsn = tcg_ctx->plugin_insn;
449 
450     pinsn->len = db->fake_insn ? db->record_len : db->pc_next - pinsn->vaddr;
451 
452     tcg_gen_plugin_cb(PLUGIN_GEN_AFTER_INSN);
453 }
454 
455 /*
456  * There are cases where we never get to finalise a translation - for
457  * example a page fault during translation. As a result we shouldn't
458  * do any clean-up here and make sure things are reset in
459  * plugin_gen_tb_start.
460  */
461 void plugin_gen_tb_end(CPUState *cpu, size_t num_insns)
462 {
463     struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
464 
465     /* translator may have removed instructions, update final count */
466     g_assert(num_insns <= ptb->n);
467     ptb->n = num_insns;
468 
469     /* collect instrumentation requests */
470     qemu_plugin_tb_trans_cb(cpu, ptb);
471 
472     /* inject the instrumentation at the appropriate places */
473     plugin_gen_inject(ptb);
474 }
475