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/option.h"
  19. #include "qemu/rcu_queue.h"
  20. #include "qemu/xxhash.h"
  21. #include "qemu/rcu.h"
  22. #include "hw/core/cpu.h"
  23. #include "exec/cpu-common.h"
  24. #include "cpu.h"
  25. #include "exec/exec-all.h"
  26. #include "exec/helper-proto.h"
  27. #include "sysemu/sysemu.h"
  28. #include "tcg/tcg.h"
  29. #include "tcg/tcg-op.h"
  30. #include "trace/mem-internal.h" /* mem_info macros */
  31. #include "plugin.h"
  32. struct qemu_plugin_cb {
  33. struct qemu_plugin_ctx *ctx;
  34. union qemu_plugin_cb_sig f;
  35. void *udata;
  36. QLIST_ENTRY(qemu_plugin_cb) entry;
  37. };
  38. struct qemu_plugin_state plugin;
  39. struct qemu_plugin_ctx *plugin_id_to_ctx_locked(qemu_plugin_id_t id)
  40. {
  41. struct qemu_plugin_ctx *ctx;
  42. qemu_plugin_id_t *id_p;
  43. id_p = g_hash_table_lookup(plugin.id_ht, &id);
  44. ctx = container_of(id_p, struct qemu_plugin_ctx, id);
  45. if (ctx == NULL) {
  46. error_report("plugin: invalid plugin id %" PRIu64, id);
  47. abort();
  48. }
  49. return ctx;
  50. }
  51. static void plugin_cpu_update__async(CPUState *cpu, run_on_cpu_data data)
  52. {
  53. bitmap_copy(cpu->plugin_mask, &data.host_ulong, QEMU_PLUGIN_EV_MAX);
  54. cpu_tb_jmp_cache_clear(cpu);
  55. }
  56. static void plugin_cpu_update__locked(gpointer k, gpointer v, gpointer udata)
  57. {
  58. CPUState *cpu = container_of(k, CPUState, cpu_index);
  59. run_on_cpu_data mask = RUN_ON_CPU_HOST_ULONG(*plugin.mask);
  60. if (cpu->created) {
  61. async_run_on_cpu(cpu, plugin_cpu_update__async, mask);
  62. } else {
  63. plugin_cpu_update__async(cpu, mask);
  64. }
  65. }
  66. void plugin_unregister_cb__locked(struct qemu_plugin_ctx *ctx,
  67. enum qemu_plugin_event ev)
  68. {
  69. struct qemu_plugin_cb *cb = ctx->callbacks[ev];
  70. if (cb == NULL) {
  71. return;
  72. }
  73. QLIST_REMOVE_RCU(cb, entry);
  74. g_free(cb);
  75. ctx->callbacks[ev] = NULL;
  76. if (QLIST_EMPTY_RCU(&plugin.cb_lists[ev])) {
  77. clear_bit(ev, plugin.mask);
  78. g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked, NULL);
  79. }
  80. }
  81. static void plugin_vcpu_cb__simple(CPUState *cpu, enum qemu_plugin_event ev)
  82. {
  83. struct qemu_plugin_cb *cb, *next;
  84. switch (ev) {
  85. case QEMU_PLUGIN_EV_VCPU_INIT:
  86. case QEMU_PLUGIN_EV_VCPU_EXIT:
  87. case QEMU_PLUGIN_EV_VCPU_IDLE:
  88. case QEMU_PLUGIN_EV_VCPU_RESUME:
  89. /* iterate safely; plugins might uninstall themselves at any time */
  90. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  91. qemu_plugin_vcpu_simple_cb_t func = cb->f.vcpu_simple;
  92. func(cb->ctx->id, cpu->cpu_index);
  93. }
  94. break;
  95. default:
  96. g_assert_not_reached();
  97. }
  98. }
  99. static void plugin_cb__simple(enum qemu_plugin_event ev)
  100. {
  101. struct qemu_plugin_cb *cb, *next;
  102. switch (ev) {
  103. case QEMU_PLUGIN_EV_FLUSH:
  104. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  105. qemu_plugin_simple_cb_t func = cb->f.simple;
  106. func(cb->ctx->id);
  107. }
  108. break;
  109. default:
  110. g_assert_not_reached();
  111. }
  112. }
  113. static void plugin_cb__udata(enum qemu_plugin_event ev)
  114. {
  115. struct qemu_plugin_cb *cb, *next;
  116. switch (ev) {
  117. case QEMU_PLUGIN_EV_ATEXIT:
  118. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  119. qemu_plugin_udata_cb_t func = cb->f.udata;
  120. func(cb->ctx->id, cb->udata);
  121. }
  122. break;
  123. default:
  124. g_assert_not_reached();
  125. }
  126. }
  127. static void
  128. do_plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
  129. void *func, void *udata)
  130. {
  131. struct qemu_plugin_ctx *ctx;
  132. qemu_rec_mutex_lock(&plugin.lock);
  133. ctx = plugin_id_to_ctx_locked(id);
  134. /* if the plugin is on its way out, ignore this request */
  135. if (unlikely(ctx->uninstalling)) {
  136. goto out_unlock;
  137. }
  138. if (func) {
  139. struct qemu_plugin_cb *cb = ctx->callbacks[ev];
  140. if (cb) {
  141. cb->f.generic = func;
  142. cb->udata = udata;
  143. } else {
  144. cb = g_new(struct qemu_plugin_cb, 1);
  145. cb->ctx = ctx;
  146. cb->f.generic = func;
  147. cb->udata = udata;
  148. ctx->callbacks[ev] = cb;
  149. QLIST_INSERT_HEAD_RCU(&plugin.cb_lists[ev], cb, entry);
  150. if (!test_bit(ev, plugin.mask)) {
  151. set_bit(ev, plugin.mask);
  152. g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked,
  153. NULL);
  154. }
  155. }
  156. } else {
  157. plugin_unregister_cb__locked(ctx, ev);
  158. }
  159. out_unlock:
  160. qemu_rec_mutex_unlock(&plugin.lock);
  161. }
  162. void plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
  163. void *func)
  164. {
  165. do_plugin_register_cb(id, ev, func, NULL);
  166. }
  167. void
  168. plugin_register_cb_udata(qemu_plugin_id_t id, enum qemu_plugin_event ev,
  169. void *func, void *udata)
  170. {
  171. do_plugin_register_cb(id, ev, func, udata);
  172. }
  173. void qemu_plugin_vcpu_init_hook(CPUState *cpu)
  174. {
  175. bool success;
  176. qemu_rec_mutex_lock(&plugin.lock);
  177. plugin_cpu_update__locked(&cpu->cpu_index, NULL, NULL);
  178. success = g_hash_table_insert(plugin.cpu_ht, &cpu->cpu_index,
  179. &cpu->cpu_index);
  180. g_assert(success);
  181. qemu_rec_mutex_unlock(&plugin.lock);
  182. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_INIT);
  183. }
  184. void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
  185. {
  186. bool success;
  187. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_EXIT);
  188. qemu_rec_mutex_lock(&plugin.lock);
  189. success = g_hash_table_remove(plugin.cpu_ht, &cpu->cpu_index);
  190. g_assert(success);
  191. qemu_rec_mutex_unlock(&plugin.lock);
  192. }
  193. struct plugin_for_each_args {
  194. struct qemu_plugin_ctx *ctx;
  195. qemu_plugin_vcpu_simple_cb_t cb;
  196. };
  197. static void plugin_vcpu_for_each(gpointer k, gpointer v, gpointer udata)
  198. {
  199. struct plugin_for_each_args *args = udata;
  200. int cpu_index = *(int *)k;
  201. args->cb(args->ctx->id, cpu_index);
  202. }
  203. void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id,
  204. qemu_plugin_vcpu_simple_cb_t cb)
  205. {
  206. struct plugin_for_each_args args;
  207. if (cb == NULL) {
  208. return;
  209. }
  210. qemu_rec_mutex_lock(&plugin.lock);
  211. args.ctx = plugin_id_to_ctx_locked(id);
  212. args.cb = cb;
  213. g_hash_table_foreach(plugin.cpu_ht, plugin_vcpu_for_each, &args);
  214. qemu_rec_mutex_unlock(&plugin.lock);
  215. }
  216. /* Allocate and return a callback record */
  217. static struct qemu_plugin_dyn_cb *plugin_get_dyn_cb(GArray **arr)
  218. {
  219. GArray *cbs = *arr;
  220. if (!cbs) {
  221. cbs = g_array_sized_new(false, false,
  222. sizeof(struct qemu_plugin_dyn_cb), 1);
  223. *arr = cbs;
  224. }
  225. g_array_set_size(cbs, cbs->len + 1);
  226. return &g_array_index(cbs, struct qemu_plugin_dyn_cb, cbs->len - 1);
  227. }
  228. void plugin_register_inline_op(GArray **arr,
  229. enum qemu_plugin_mem_rw rw,
  230. enum qemu_plugin_op op, void *ptr,
  231. uint64_t imm)
  232. {
  233. struct qemu_plugin_dyn_cb *dyn_cb;
  234. dyn_cb = plugin_get_dyn_cb(arr);
  235. dyn_cb->userp = ptr;
  236. dyn_cb->type = PLUGIN_CB_INLINE;
  237. dyn_cb->rw = rw;
  238. dyn_cb->inline_insn.op = op;
  239. dyn_cb->inline_insn.imm = imm;
  240. }
  241. static inline uint32_t cb_to_tcg_flags(enum qemu_plugin_cb_flags flags)
  242. {
  243. uint32_t ret;
  244. switch (flags) {
  245. case QEMU_PLUGIN_CB_RW_REGS:
  246. ret = 0;
  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. }