2
0

core.c 15 KB

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