core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 "exec/exec-all.h"
  26. #include "exec/tb-flush.h"
  27. #include "exec/helper-proto.h"
  28. #include "tcg/tcg.h"
  29. #include "tcg/tcg-op.h"
  30. #include "plugin.h"
  31. #include "qemu/compiler.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. tcg_flush_jmp_cache(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. /*
  82. * Disable CFI checks.
  83. * The callback function has been loaded from an external library so we do not
  84. * have type information
  85. */
  86. QEMU_DISABLE_CFI
  87. static void plugin_vcpu_cb__simple(CPUState *cpu, enum qemu_plugin_event ev)
  88. {
  89. struct qemu_plugin_cb *cb, *next;
  90. switch (ev) {
  91. case QEMU_PLUGIN_EV_VCPU_INIT:
  92. case QEMU_PLUGIN_EV_VCPU_EXIT:
  93. case QEMU_PLUGIN_EV_VCPU_IDLE:
  94. case QEMU_PLUGIN_EV_VCPU_RESUME:
  95. /* iterate safely; plugins might uninstall themselves at any time */
  96. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  97. qemu_plugin_vcpu_simple_cb_t func = cb->f.vcpu_simple;
  98. func(cb->ctx->id, cpu->cpu_index);
  99. }
  100. break;
  101. default:
  102. g_assert_not_reached();
  103. }
  104. }
  105. /*
  106. * Disable CFI checks.
  107. * The callback function has been loaded from an external library so we do not
  108. * have type information
  109. */
  110. QEMU_DISABLE_CFI
  111. static void plugin_cb__simple(enum qemu_plugin_event ev)
  112. {
  113. struct qemu_plugin_cb *cb, *next;
  114. switch (ev) {
  115. case QEMU_PLUGIN_EV_FLUSH:
  116. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  117. qemu_plugin_simple_cb_t func = cb->f.simple;
  118. func(cb->ctx->id);
  119. }
  120. break;
  121. default:
  122. g_assert_not_reached();
  123. }
  124. }
  125. /*
  126. * Disable CFI checks.
  127. * The callback function has been loaded from an external library so we do not
  128. * have type information
  129. */
  130. QEMU_DISABLE_CFI
  131. static void plugin_cb__udata(enum qemu_plugin_event ev)
  132. {
  133. struct qemu_plugin_cb *cb, *next;
  134. switch (ev) {
  135. case QEMU_PLUGIN_EV_ATEXIT:
  136. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  137. qemu_plugin_udata_cb_t func = cb->f.udata;
  138. func(cb->ctx->id, cb->udata);
  139. }
  140. break;
  141. default:
  142. g_assert_not_reached();
  143. }
  144. }
  145. static void
  146. do_plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
  147. void *func, void *udata)
  148. {
  149. struct qemu_plugin_ctx *ctx;
  150. QEMU_LOCK_GUARD(&plugin.lock);
  151. ctx = plugin_id_to_ctx_locked(id);
  152. /* if the plugin is on its way out, ignore this request */
  153. if (unlikely(ctx->uninstalling)) {
  154. return;
  155. }
  156. if (func) {
  157. struct qemu_plugin_cb *cb = ctx->callbacks[ev];
  158. if (cb) {
  159. cb->f.generic = func;
  160. cb->udata = udata;
  161. } else {
  162. cb = g_new(struct qemu_plugin_cb, 1);
  163. cb->ctx = ctx;
  164. cb->f.generic = func;
  165. cb->udata = udata;
  166. ctx->callbacks[ev] = cb;
  167. QLIST_INSERT_HEAD_RCU(&plugin.cb_lists[ev], cb, entry);
  168. if (!test_bit(ev, plugin.mask)) {
  169. set_bit(ev, plugin.mask);
  170. g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked,
  171. NULL);
  172. }
  173. }
  174. } else {
  175. plugin_unregister_cb__locked(ctx, ev);
  176. }
  177. }
  178. void plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
  179. void *func)
  180. {
  181. do_plugin_register_cb(id, ev, func, NULL);
  182. }
  183. void
  184. plugin_register_cb_udata(qemu_plugin_id_t id, enum qemu_plugin_event ev,
  185. void *func, void *udata)
  186. {
  187. do_plugin_register_cb(id, ev, func, udata);
  188. }
  189. void qemu_plugin_vcpu_init_hook(CPUState *cpu)
  190. {
  191. bool success;
  192. qemu_rec_mutex_lock(&plugin.lock);
  193. plugin_cpu_update__locked(&cpu->cpu_index, NULL, NULL);
  194. success = g_hash_table_insert(plugin.cpu_ht, &cpu->cpu_index,
  195. &cpu->cpu_index);
  196. g_assert(success);
  197. qemu_rec_mutex_unlock(&plugin.lock);
  198. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_INIT);
  199. }
  200. void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
  201. {
  202. bool success;
  203. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_EXIT);
  204. qemu_rec_mutex_lock(&plugin.lock);
  205. success = g_hash_table_remove(plugin.cpu_ht, &cpu->cpu_index);
  206. g_assert(success);
  207. qemu_rec_mutex_unlock(&plugin.lock);
  208. }
  209. struct plugin_for_each_args {
  210. struct qemu_plugin_ctx *ctx;
  211. qemu_plugin_vcpu_simple_cb_t cb;
  212. };
  213. static void plugin_vcpu_for_each(gpointer k, gpointer v, gpointer udata)
  214. {
  215. struct plugin_for_each_args *args = udata;
  216. int cpu_index = *(int *)k;
  217. args->cb(args->ctx->id, cpu_index);
  218. }
  219. void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id,
  220. qemu_plugin_vcpu_simple_cb_t cb)
  221. {
  222. struct plugin_for_each_args args;
  223. if (cb == NULL) {
  224. return;
  225. }
  226. qemu_rec_mutex_lock(&plugin.lock);
  227. args.ctx = plugin_id_to_ctx_locked(id);
  228. args.cb = cb;
  229. g_hash_table_foreach(plugin.cpu_ht, plugin_vcpu_for_each, &args);
  230. qemu_rec_mutex_unlock(&plugin.lock);
  231. }
  232. /* Allocate and return a callback record */
  233. static struct qemu_plugin_dyn_cb *plugin_get_dyn_cb(GArray **arr)
  234. {
  235. GArray *cbs = *arr;
  236. if (!cbs) {
  237. cbs = g_array_sized_new(false, false,
  238. sizeof(struct qemu_plugin_dyn_cb), 1);
  239. *arr = cbs;
  240. }
  241. g_array_set_size(cbs, cbs->len + 1);
  242. return &g_array_index(cbs, struct qemu_plugin_dyn_cb, cbs->len - 1);
  243. }
  244. void plugin_register_inline_op(GArray **arr,
  245. enum qemu_plugin_mem_rw rw,
  246. enum qemu_plugin_op op, void *ptr,
  247. uint64_t imm)
  248. {
  249. struct qemu_plugin_dyn_cb *dyn_cb;
  250. dyn_cb = plugin_get_dyn_cb(arr);
  251. dyn_cb->userp = ptr;
  252. dyn_cb->type = PLUGIN_CB_INLINE;
  253. dyn_cb->rw = rw;
  254. dyn_cb->inline_insn.op = op;
  255. dyn_cb->inline_insn.imm = imm;
  256. }
  257. void plugin_register_dyn_cb__udata(GArray **arr,
  258. qemu_plugin_vcpu_udata_cb_t cb,
  259. enum qemu_plugin_cb_flags flags,
  260. void *udata)
  261. {
  262. struct qemu_plugin_dyn_cb *dyn_cb = plugin_get_dyn_cb(arr);
  263. dyn_cb->userp = udata;
  264. /* Note flags are discarded as unused. */
  265. dyn_cb->f.vcpu_udata = cb;
  266. dyn_cb->type = PLUGIN_CB_REGULAR;
  267. }
  268. void plugin_register_vcpu_mem_cb(GArray **arr,
  269. void *cb,
  270. enum qemu_plugin_cb_flags flags,
  271. enum qemu_plugin_mem_rw rw,
  272. void *udata)
  273. {
  274. struct qemu_plugin_dyn_cb *dyn_cb;
  275. dyn_cb = plugin_get_dyn_cb(arr);
  276. dyn_cb->userp = udata;
  277. /* Note flags are discarded as unused. */
  278. dyn_cb->type = PLUGIN_CB_REGULAR;
  279. dyn_cb->rw = rw;
  280. dyn_cb->f.generic = cb;
  281. }
  282. /*
  283. * Disable CFI checks.
  284. * The callback function has been loaded from an external library so we do not
  285. * have type information
  286. */
  287. QEMU_DISABLE_CFI
  288. void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb)
  289. {
  290. struct qemu_plugin_cb *cb, *next;
  291. enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_TB_TRANS;
  292. /* no plugin_mask check here; caller should have checked */
  293. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  294. qemu_plugin_vcpu_tb_trans_cb_t func = cb->f.vcpu_tb_trans;
  295. func(cb->ctx->id, tb);
  296. }
  297. }
  298. /*
  299. * Disable CFI checks.
  300. * The callback function has been loaded from an external library so we do not
  301. * have type information
  302. */
  303. QEMU_DISABLE_CFI
  304. void
  305. qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,
  306. uint64_t a3, uint64_t a4, uint64_t a5,
  307. uint64_t a6, uint64_t a7, uint64_t a8)
  308. {
  309. struct qemu_plugin_cb *cb, *next;
  310. enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL;
  311. if (!test_bit(ev, cpu->plugin_mask)) {
  312. return;
  313. }
  314. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  315. qemu_plugin_vcpu_syscall_cb_t func = cb->f.vcpu_syscall;
  316. func(cb->ctx->id, cpu->cpu_index, num, a1, a2, a3, a4, a5, a6, a7, a8);
  317. }
  318. }
  319. /*
  320. * Disable CFI checks.
  321. * The callback function has been loaded from an external library so we do not
  322. * have type information
  323. */
  324. QEMU_DISABLE_CFI
  325. void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret)
  326. {
  327. struct qemu_plugin_cb *cb, *next;
  328. enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL_RET;
  329. if (!test_bit(ev, cpu->plugin_mask)) {
  330. return;
  331. }
  332. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  333. qemu_plugin_vcpu_syscall_ret_cb_t func = cb->f.vcpu_syscall_ret;
  334. func(cb->ctx->id, cpu->cpu_index, num, ret);
  335. }
  336. }
  337. void qemu_plugin_vcpu_idle_cb(CPUState *cpu)
  338. {
  339. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_IDLE);
  340. }
  341. void qemu_plugin_vcpu_resume_cb(CPUState *cpu)
  342. {
  343. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_RESUME);
  344. }
  345. void qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id,
  346. qemu_plugin_vcpu_simple_cb_t cb)
  347. {
  348. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_IDLE, cb);
  349. }
  350. void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id,
  351. qemu_plugin_vcpu_simple_cb_t cb)
  352. {
  353. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_RESUME, cb);
  354. }
  355. void qemu_plugin_register_flush_cb(qemu_plugin_id_t id,
  356. qemu_plugin_simple_cb_t cb)
  357. {
  358. plugin_register_cb(id, QEMU_PLUGIN_EV_FLUSH, cb);
  359. }
  360. static bool free_dyn_cb_arr(void *p, uint32_t h, void *userp)
  361. {
  362. g_array_free((GArray *) p, true);
  363. return true;
  364. }
  365. void qemu_plugin_flush_cb(void)
  366. {
  367. qht_iter_remove(&plugin.dyn_cb_arr_ht, free_dyn_cb_arr, NULL);
  368. qht_reset(&plugin.dyn_cb_arr_ht);
  369. plugin_cb__simple(QEMU_PLUGIN_EV_FLUSH);
  370. }
  371. void exec_inline_op(struct qemu_plugin_dyn_cb *cb)
  372. {
  373. uint64_t *val = cb->userp;
  374. switch (cb->inline_insn.op) {
  375. case QEMU_PLUGIN_INLINE_ADD_U64:
  376. *val += cb->inline_insn.imm;
  377. break;
  378. default:
  379. g_assert_not_reached();
  380. }
  381. }
  382. void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
  383. MemOpIdx oi, enum qemu_plugin_mem_rw rw)
  384. {
  385. GArray *arr = cpu->plugin_mem_cbs;
  386. size_t i;
  387. if (arr == NULL) {
  388. return;
  389. }
  390. for (i = 0; i < arr->len; i++) {
  391. struct qemu_plugin_dyn_cb *cb =
  392. &g_array_index(arr, struct qemu_plugin_dyn_cb, i);
  393. if (!(rw & cb->rw)) {
  394. break;
  395. }
  396. switch (cb->type) {
  397. case PLUGIN_CB_REGULAR:
  398. cb->f.vcpu_mem(cpu->cpu_index, make_plugin_meminfo(oi, rw),
  399. vaddr, cb->userp);
  400. break;
  401. case PLUGIN_CB_INLINE:
  402. exec_inline_op(cb);
  403. break;
  404. default:
  405. g_assert_not_reached();
  406. }
  407. }
  408. }
  409. void qemu_plugin_atexit_cb(void)
  410. {
  411. plugin_cb__udata(QEMU_PLUGIN_EV_ATEXIT);
  412. }
  413. void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id,
  414. qemu_plugin_udata_cb_t cb,
  415. void *udata)
  416. {
  417. plugin_register_cb_udata(id, QEMU_PLUGIN_EV_ATEXIT, cb, udata);
  418. }
  419. /*
  420. * Handle exit from linux-user. Unlike the normal atexit() mechanism
  421. * we need to handle the clean-up manually as it's possible threads
  422. * are still running. We need to remove all callbacks from code
  423. * generation, flush the current translations and then we can safely
  424. * trigger the exit callbacks.
  425. */
  426. void qemu_plugin_user_exit(void)
  427. {
  428. enum qemu_plugin_event ev;
  429. CPUState *cpu;
  430. /*
  431. * Locking order: we must acquire locks in an order that is consistent
  432. * with the one in fork_start(). That is:
  433. * - start_exclusive(), which acquires qemu_cpu_list_lock,
  434. * must be called before acquiring plugin.lock.
  435. * - tb_flush(), which acquires mmap_lock(), must be called
  436. * while plugin.lock is not held.
  437. */
  438. start_exclusive();
  439. qemu_rec_mutex_lock(&plugin.lock);
  440. /* un-register all callbacks except the final AT_EXIT one */
  441. for (ev = 0; ev < QEMU_PLUGIN_EV_MAX; ev++) {
  442. if (ev != QEMU_PLUGIN_EV_ATEXIT) {
  443. struct qemu_plugin_cb *cb, *next;
  444. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  445. plugin_unregister_cb__locked(cb->ctx, ev);
  446. }
  447. }
  448. }
  449. CPU_FOREACH(cpu) {
  450. qemu_plugin_disable_mem_helpers(cpu);
  451. }
  452. qemu_rec_mutex_unlock(&plugin.lock);
  453. tb_flush(current_cpu);
  454. end_exclusive();
  455. /* now it's safe to handle the exit case */
  456. qemu_plugin_atexit_cb();
  457. }
  458. /*
  459. * Helpers for *-user to ensure locks are sane across fork() events.
  460. */
  461. void qemu_plugin_user_prefork_lock(void)
  462. {
  463. qemu_rec_mutex_lock(&plugin.lock);
  464. }
  465. void qemu_plugin_user_postfork(bool is_child)
  466. {
  467. if (is_child) {
  468. /* should we just reset via plugin_init? */
  469. qemu_rec_mutex_init(&plugin.lock);
  470. } else {
  471. qemu_rec_mutex_unlock(&plugin.lock);
  472. }
  473. }
  474. static bool plugin_dyn_cb_arr_cmp(const void *ap, const void *bp)
  475. {
  476. return ap == bp;
  477. }
  478. static void __attribute__((__constructor__)) plugin_init(void)
  479. {
  480. int i;
  481. for (i = 0; i < QEMU_PLUGIN_EV_MAX; i++) {
  482. QLIST_INIT(&plugin.cb_lists[i]);
  483. }
  484. qemu_rec_mutex_init(&plugin.lock);
  485. plugin.id_ht = g_hash_table_new(g_int64_hash, g_int64_equal);
  486. plugin.cpu_ht = g_hash_table_new(g_int_hash, g_int_equal);
  487. QTAILQ_INIT(&plugin.ctxs);
  488. qht_init(&plugin.dyn_cb_arr_ht, plugin_dyn_cb_arr_cmp, 16,
  489. QHT_MODE_AUTO_RESIZE);
  490. atexit(qemu_plugin_atexit_cb);
  491. }