core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * QEMU Plugin Core code
  3. *
  4. * This is the core code that deals with injecting instrumentation into the code
  5. *
  6. * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
  7. * Copyright (C) 2019, Linaro
  8. *
  9. * License: GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. *
  12. * SPDX-License-Identifier: GPL-2.0-or-later
  13. */
  14. #include "qemu/osdep.h"
  15. #include "qemu/error-report.h"
  16. #include "qemu/config-file.h"
  17. #include "qapi/error.h"
  18. #include "qemu/lockable.h"
  19. #include "qemu/option.h"
  20. #include "qemu/rcu_queue.h"
  21. #include "qemu/xxhash.h"
  22. #include "qemu/rcu.h"
  23. #include "hw/core/cpu.h"
  24. #include "exec/cpu-common.h"
  25. #include "cpu.h"
  26. #include "exec/exec-all.h"
  27. #include "exec/helper-proto.h"
  28. #include "sysemu/sysemu.h"
  29. #include "tcg/tcg.h"
  30. #include "tcg/tcg-op.h"
  31. #include "trace/mem-internal.h" /* mem_info macros */
  32. #include "plugin.h"
  33. struct qemu_plugin_cb {
  34. struct qemu_plugin_ctx *ctx;
  35. union qemu_plugin_cb_sig f;
  36. void *udata;
  37. QLIST_ENTRY(qemu_plugin_cb) entry;
  38. };
  39. struct qemu_plugin_state plugin;
  40. struct qemu_plugin_ctx *plugin_id_to_ctx_locked(qemu_plugin_id_t id)
  41. {
  42. struct qemu_plugin_ctx *ctx;
  43. qemu_plugin_id_t *id_p;
  44. id_p = g_hash_table_lookup(plugin.id_ht, &id);
  45. ctx = container_of(id_p, struct qemu_plugin_ctx, id);
  46. if (ctx == NULL) {
  47. error_report("plugin: invalid plugin id %" PRIu64, id);
  48. abort();
  49. }
  50. return ctx;
  51. }
  52. static void plugin_cpu_update__async(CPUState *cpu, run_on_cpu_data data)
  53. {
  54. bitmap_copy(cpu->plugin_mask, &data.host_ulong, QEMU_PLUGIN_EV_MAX);
  55. cpu_tb_jmp_cache_clear(cpu);
  56. }
  57. static void plugin_cpu_update__locked(gpointer k, gpointer v, gpointer udata)
  58. {
  59. CPUState *cpu = container_of(k, CPUState, cpu_index);
  60. run_on_cpu_data mask = RUN_ON_CPU_HOST_ULONG(*plugin.mask);
  61. if (cpu->created) {
  62. async_run_on_cpu(cpu, plugin_cpu_update__async, mask);
  63. } else {
  64. plugin_cpu_update__async(cpu, mask);
  65. }
  66. }
  67. void plugin_unregister_cb__locked(struct qemu_plugin_ctx *ctx,
  68. enum qemu_plugin_event ev)
  69. {
  70. struct qemu_plugin_cb *cb = ctx->callbacks[ev];
  71. if (cb == NULL) {
  72. return;
  73. }
  74. QLIST_REMOVE_RCU(cb, entry);
  75. g_free(cb);
  76. ctx->callbacks[ev] = NULL;
  77. if (QLIST_EMPTY_RCU(&plugin.cb_lists[ev])) {
  78. clear_bit(ev, plugin.mask);
  79. g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked, NULL);
  80. }
  81. }
  82. static void plugin_vcpu_cb__simple(CPUState *cpu, enum qemu_plugin_event ev)
  83. {
  84. struct qemu_plugin_cb *cb, *next;
  85. switch (ev) {
  86. case QEMU_PLUGIN_EV_VCPU_INIT:
  87. case QEMU_PLUGIN_EV_VCPU_EXIT:
  88. case QEMU_PLUGIN_EV_VCPU_IDLE:
  89. case QEMU_PLUGIN_EV_VCPU_RESUME:
  90. /* iterate safely; plugins might uninstall themselves at any time */
  91. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  92. qemu_plugin_vcpu_simple_cb_t func = cb->f.vcpu_simple;
  93. func(cb->ctx->id, cpu->cpu_index);
  94. }
  95. break;
  96. default:
  97. g_assert_not_reached();
  98. }
  99. }
  100. static void plugin_cb__simple(enum qemu_plugin_event ev)
  101. {
  102. struct qemu_plugin_cb *cb, *next;
  103. switch (ev) {
  104. case QEMU_PLUGIN_EV_FLUSH:
  105. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  106. qemu_plugin_simple_cb_t func = cb->f.simple;
  107. func(cb->ctx->id);
  108. }
  109. break;
  110. default:
  111. g_assert_not_reached();
  112. }
  113. }
  114. static void plugin_cb__udata(enum qemu_plugin_event ev)
  115. {
  116. struct qemu_plugin_cb *cb, *next;
  117. switch (ev) {
  118. case QEMU_PLUGIN_EV_ATEXIT:
  119. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  120. qemu_plugin_udata_cb_t func = cb->f.udata;
  121. func(cb->ctx->id, cb->udata);
  122. }
  123. break;
  124. default:
  125. g_assert_not_reached();
  126. }
  127. }
  128. static void
  129. do_plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
  130. void *func, void *udata)
  131. {
  132. struct qemu_plugin_ctx *ctx;
  133. QEMU_LOCK_GUARD(&plugin.lock);
  134. ctx = plugin_id_to_ctx_locked(id);
  135. /* if the plugin is on its way out, ignore this request */
  136. if (unlikely(ctx->uninstalling)) {
  137. return;
  138. }
  139. if (func) {
  140. struct qemu_plugin_cb *cb = ctx->callbacks[ev];
  141. if (cb) {
  142. cb->f.generic = func;
  143. cb->udata = udata;
  144. } else {
  145. cb = g_new(struct qemu_plugin_cb, 1);
  146. cb->ctx = ctx;
  147. cb->f.generic = func;
  148. cb->udata = udata;
  149. ctx->callbacks[ev] = cb;
  150. QLIST_INSERT_HEAD_RCU(&plugin.cb_lists[ev], cb, entry);
  151. if (!test_bit(ev, plugin.mask)) {
  152. set_bit(ev, plugin.mask);
  153. g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked,
  154. NULL);
  155. }
  156. }
  157. } else {
  158. plugin_unregister_cb__locked(ctx, ev);
  159. }
  160. }
  161. void plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
  162. void *func)
  163. {
  164. do_plugin_register_cb(id, ev, func, NULL);
  165. }
  166. void
  167. plugin_register_cb_udata(qemu_plugin_id_t id, enum qemu_plugin_event ev,
  168. void *func, void *udata)
  169. {
  170. do_plugin_register_cb(id, ev, func, udata);
  171. }
  172. void qemu_plugin_vcpu_init_hook(CPUState *cpu)
  173. {
  174. bool success;
  175. qemu_rec_mutex_lock(&plugin.lock);
  176. plugin_cpu_update__locked(&cpu->cpu_index, NULL, NULL);
  177. success = g_hash_table_insert(plugin.cpu_ht, &cpu->cpu_index,
  178. &cpu->cpu_index);
  179. g_assert(success);
  180. qemu_rec_mutex_unlock(&plugin.lock);
  181. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_INIT);
  182. }
  183. void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
  184. {
  185. bool success;
  186. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_EXIT);
  187. qemu_rec_mutex_lock(&plugin.lock);
  188. success = g_hash_table_remove(plugin.cpu_ht, &cpu->cpu_index);
  189. g_assert(success);
  190. qemu_rec_mutex_unlock(&plugin.lock);
  191. }
  192. struct plugin_for_each_args {
  193. struct qemu_plugin_ctx *ctx;
  194. qemu_plugin_vcpu_simple_cb_t cb;
  195. };
  196. static void plugin_vcpu_for_each(gpointer k, gpointer v, gpointer udata)
  197. {
  198. struct plugin_for_each_args *args = udata;
  199. int cpu_index = *(int *)k;
  200. args->cb(args->ctx->id, cpu_index);
  201. }
  202. void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id,
  203. qemu_plugin_vcpu_simple_cb_t cb)
  204. {
  205. struct plugin_for_each_args args;
  206. if (cb == NULL) {
  207. return;
  208. }
  209. qemu_rec_mutex_lock(&plugin.lock);
  210. args.ctx = plugin_id_to_ctx_locked(id);
  211. args.cb = cb;
  212. g_hash_table_foreach(plugin.cpu_ht, plugin_vcpu_for_each, &args);
  213. qemu_rec_mutex_unlock(&plugin.lock);
  214. }
  215. /* Allocate and return a callback record */
  216. static struct qemu_plugin_dyn_cb *plugin_get_dyn_cb(GArray **arr)
  217. {
  218. GArray *cbs = *arr;
  219. if (!cbs) {
  220. cbs = g_array_sized_new(false, false,
  221. sizeof(struct qemu_plugin_dyn_cb), 1);
  222. *arr = cbs;
  223. }
  224. g_array_set_size(cbs, cbs->len + 1);
  225. return &g_array_index(cbs, struct qemu_plugin_dyn_cb, cbs->len - 1);
  226. }
  227. void plugin_register_inline_op(GArray **arr,
  228. enum qemu_plugin_mem_rw rw,
  229. enum qemu_plugin_op op, void *ptr,
  230. uint64_t imm)
  231. {
  232. struct qemu_plugin_dyn_cb *dyn_cb;
  233. dyn_cb = plugin_get_dyn_cb(arr);
  234. dyn_cb->userp = ptr;
  235. dyn_cb->type = PLUGIN_CB_INLINE;
  236. dyn_cb->rw = rw;
  237. dyn_cb->inline_insn.op = op;
  238. dyn_cb->inline_insn.imm = imm;
  239. }
  240. static inline uint32_t cb_to_tcg_flags(enum qemu_plugin_cb_flags flags)
  241. {
  242. uint32_t ret;
  243. switch (flags) {
  244. case QEMU_PLUGIN_CB_RW_REGS:
  245. ret = 0;
  246. break;
  247. case QEMU_PLUGIN_CB_R_REGS:
  248. ret = TCG_CALL_NO_WG;
  249. break;
  250. case QEMU_PLUGIN_CB_NO_REGS:
  251. default:
  252. ret = TCG_CALL_NO_RWG;
  253. }
  254. return ret;
  255. }
  256. inline void
  257. plugin_register_dyn_cb__udata(GArray **arr,
  258. qemu_plugin_vcpu_udata_cb_t cb,
  259. enum qemu_plugin_cb_flags flags, void *udata)
  260. {
  261. struct qemu_plugin_dyn_cb *dyn_cb = plugin_get_dyn_cb(arr);
  262. dyn_cb->userp = udata;
  263. dyn_cb->tcg_flags = cb_to_tcg_flags(flags);
  264. dyn_cb->f.vcpu_udata = cb;
  265. dyn_cb->type = PLUGIN_CB_REGULAR;
  266. }
  267. void plugin_register_vcpu_mem_cb(GArray **arr,
  268. void *cb,
  269. enum qemu_plugin_cb_flags flags,
  270. enum qemu_plugin_mem_rw rw,
  271. void *udata)
  272. {
  273. struct qemu_plugin_dyn_cb *dyn_cb;
  274. dyn_cb = plugin_get_dyn_cb(arr);
  275. dyn_cb->userp = udata;
  276. dyn_cb->tcg_flags = cb_to_tcg_flags(flags);
  277. dyn_cb->type = PLUGIN_CB_REGULAR;
  278. dyn_cb->rw = rw;
  279. dyn_cb->f.generic = cb;
  280. }
  281. void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb)
  282. {
  283. struct qemu_plugin_cb *cb, *next;
  284. enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_TB_TRANS;
  285. /* no plugin_mask check here; caller should have checked */
  286. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  287. qemu_plugin_vcpu_tb_trans_cb_t func = cb->f.vcpu_tb_trans;
  288. func(cb->ctx->id, tb);
  289. }
  290. }
  291. void
  292. qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,
  293. uint64_t a3, uint64_t a4, uint64_t a5,
  294. uint64_t a6, uint64_t a7, uint64_t a8)
  295. {
  296. struct qemu_plugin_cb *cb, *next;
  297. enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL;
  298. if (!test_bit(ev, cpu->plugin_mask)) {
  299. return;
  300. }
  301. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  302. qemu_plugin_vcpu_syscall_cb_t func = cb->f.vcpu_syscall;
  303. func(cb->ctx->id, cpu->cpu_index, num, a1, a2, a3, a4, a5, a6, a7, a8);
  304. }
  305. }
  306. void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret)
  307. {
  308. struct qemu_plugin_cb *cb, *next;
  309. enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL_RET;
  310. if (!test_bit(ev, cpu->plugin_mask)) {
  311. return;
  312. }
  313. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  314. qemu_plugin_vcpu_syscall_ret_cb_t func = cb->f.vcpu_syscall_ret;
  315. func(cb->ctx->id, cpu->cpu_index, num, ret);
  316. }
  317. }
  318. void qemu_plugin_vcpu_idle_cb(CPUState *cpu)
  319. {
  320. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_IDLE);
  321. }
  322. void qemu_plugin_vcpu_resume_cb(CPUState *cpu)
  323. {
  324. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_RESUME);
  325. }
  326. void qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id,
  327. qemu_plugin_vcpu_simple_cb_t cb)
  328. {
  329. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_IDLE, cb);
  330. }
  331. void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id,
  332. qemu_plugin_vcpu_simple_cb_t cb)
  333. {
  334. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_RESUME, cb);
  335. }
  336. void qemu_plugin_register_flush_cb(qemu_plugin_id_t id,
  337. qemu_plugin_simple_cb_t cb)
  338. {
  339. plugin_register_cb(id, QEMU_PLUGIN_EV_FLUSH, cb);
  340. }
  341. static bool free_dyn_cb_arr(void *p, uint32_t h, void *userp)
  342. {
  343. g_array_free((GArray *) p, true);
  344. return true;
  345. }
  346. void qemu_plugin_flush_cb(void)
  347. {
  348. qht_iter_remove(&plugin.dyn_cb_arr_ht, free_dyn_cb_arr, NULL);
  349. qht_reset(&plugin.dyn_cb_arr_ht);
  350. plugin_cb__simple(QEMU_PLUGIN_EV_FLUSH);
  351. }
  352. void exec_inline_op(struct qemu_plugin_dyn_cb *cb)
  353. {
  354. uint64_t *val = cb->userp;
  355. switch (cb->inline_insn.op) {
  356. case QEMU_PLUGIN_INLINE_ADD_U64:
  357. *val += cb->inline_insn.imm;
  358. break;
  359. default:
  360. g_assert_not_reached();
  361. }
  362. }
  363. void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr, uint32_t info)
  364. {
  365. GArray *arr = cpu->plugin_mem_cbs;
  366. size_t i;
  367. if (arr == NULL) {
  368. return;
  369. }
  370. for (i = 0; i < arr->len; i++) {
  371. struct qemu_plugin_dyn_cb *cb =
  372. &g_array_index(arr, struct qemu_plugin_dyn_cb, i);
  373. int w = !!(info & TRACE_MEM_ST) + 1;
  374. if (!(w & cb->rw)) {
  375. break;
  376. }
  377. switch (cb->type) {
  378. case PLUGIN_CB_REGULAR:
  379. cb->f.vcpu_mem(cpu->cpu_index, info, vaddr, cb->userp);
  380. break;
  381. case PLUGIN_CB_INLINE:
  382. exec_inline_op(cb);
  383. break;
  384. default:
  385. g_assert_not_reached();
  386. }
  387. }
  388. }
  389. void qemu_plugin_atexit_cb(void)
  390. {
  391. plugin_cb__udata(QEMU_PLUGIN_EV_ATEXIT);
  392. }
  393. void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id,
  394. qemu_plugin_udata_cb_t cb,
  395. void *udata)
  396. {
  397. plugin_register_cb_udata(id, QEMU_PLUGIN_EV_ATEXIT, cb, udata);
  398. }
  399. /*
  400. * Call this function after longjmp'ing to the main loop. It's possible that the
  401. * last instruction of a TB might have used helpers, and therefore the
  402. * "disable" instruction will never execute because it ended up as dead code.
  403. */
  404. void qemu_plugin_disable_mem_helpers(CPUState *cpu)
  405. {
  406. cpu->plugin_mem_cbs = NULL;
  407. }
  408. static bool plugin_dyn_cb_arr_cmp(const void *ap, const void *bp)
  409. {
  410. return ap == bp;
  411. }
  412. static void __attribute__((__constructor__)) plugin_init(void)
  413. {
  414. int i;
  415. for (i = 0; i < QEMU_PLUGIN_EV_MAX; i++) {
  416. QLIST_INIT(&plugin.cb_lists[i]);
  417. }
  418. qemu_rec_mutex_init(&plugin.lock);
  419. plugin.id_ht = g_hash_table_new(g_int64_hash, g_int64_equal);
  420. plugin.cpu_ht = g_hash_table_new(g_int_hash, g_int_equal);
  421. QTAILQ_INIT(&plugin.ctxs);
  422. qht_init(&plugin.dyn_cb_arr_ht, plugin_dyn_cb_arr_cmp, 16,
  423. QHT_MODE_AUTO_RESIZE);
  424. atexit(qemu_plugin_atexit_cb);
  425. }