core.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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/plugin.h"
  21. #include "qemu/queue.h"
  22. #include "qemu/rcu_queue.h"
  23. #include "qemu/xxhash.h"
  24. #include "qemu/rcu.h"
  25. #include "hw/core/cpu.h"
  26. #include "exec/exec-all.h"
  27. #include "exec/tb-flush.h"
  28. #include "tcg/tcg.h"
  29. #include "tcg/tcg-op.h"
  30. #include "plugin.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_state->event_mask,
  53. &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 (DEVICE(cpu)->realized) {
  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. CPUPluginState *qemu_plugin_create_vcpu_state(void)
  190. {
  191. return g_new0(CPUPluginState, 1);
  192. }
  193. static void plugin_grow_scoreboards__locked(CPUState *cpu)
  194. {
  195. if (cpu->cpu_index < plugin.scoreboard_alloc_size) {
  196. return;
  197. }
  198. bool need_realloc = FALSE;
  199. while (cpu->cpu_index >= plugin.scoreboard_alloc_size) {
  200. plugin.scoreboard_alloc_size *= 2;
  201. need_realloc = TRUE;
  202. }
  203. if (!need_realloc || QLIST_EMPTY(&plugin.scoreboards)) {
  204. /* nothing to do, we just updated sizes for future scoreboards */
  205. return;
  206. }
  207. /* cpus must be stopped, as tb might still use an existing scoreboard. */
  208. start_exclusive();
  209. struct qemu_plugin_scoreboard *score;
  210. QLIST_FOREACH(score, &plugin.scoreboards, entry) {
  211. g_array_set_size(score->data, plugin.scoreboard_alloc_size);
  212. }
  213. /* force all tb to be flushed, as scoreboard pointers were changed. */
  214. tb_flush(cpu);
  215. end_exclusive();
  216. }
  217. void qemu_plugin_vcpu_init_hook(CPUState *cpu)
  218. {
  219. bool success;
  220. qemu_rec_mutex_lock(&plugin.lock);
  221. plugin.num_vcpus = MAX(plugin.num_vcpus, cpu->cpu_index + 1);
  222. plugin_cpu_update__locked(&cpu->cpu_index, NULL, NULL);
  223. success = g_hash_table_insert(plugin.cpu_ht, &cpu->cpu_index,
  224. &cpu->cpu_index);
  225. g_assert(success);
  226. plugin_grow_scoreboards__locked(cpu);
  227. qemu_rec_mutex_unlock(&plugin.lock);
  228. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_INIT);
  229. }
  230. void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
  231. {
  232. bool success;
  233. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_EXIT);
  234. qemu_rec_mutex_lock(&plugin.lock);
  235. success = g_hash_table_remove(plugin.cpu_ht, &cpu->cpu_index);
  236. g_assert(success);
  237. qemu_rec_mutex_unlock(&plugin.lock);
  238. }
  239. struct plugin_for_each_args {
  240. struct qemu_plugin_ctx *ctx;
  241. qemu_plugin_vcpu_simple_cb_t cb;
  242. };
  243. static void plugin_vcpu_for_each(gpointer k, gpointer v, gpointer udata)
  244. {
  245. struct plugin_for_each_args *args = udata;
  246. int cpu_index = *(int *)k;
  247. args->cb(args->ctx->id, cpu_index);
  248. }
  249. void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id,
  250. qemu_plugin_vcpu_simple_cb_t cb)
  251. {
  252. struct plugin_for_each_args args;
  253. if (cb == NULL) {
  254. return;
  255. }
  256. qemu_rec_mutex_lock(&plugin.lock);
  257. args.ctx = plugin_id_to_ctx_locked(id);
  258. args.cb = cb;
  259. g_hash_table_foreach(plugin.cpu_ht, plugin_vcpu_for_each, &args);
  260. qemu_rec_mutex_unlock(&plugin.lock);
  261. }
  262. /* Allocate and return a callback record */
  263. static struct qemu_plugin_dyn_cb *plugin_get_dyn_cb(GArray **arr)
  264. {
  265. GArray *cbs = *arr;
  266. if (!cbs) {
  267. cbs = g_array_sized_new(false, true,
  268. sizeof(struct qemu_plugin_dyn_cb), 1);
  269. *arr = cbs;
  270. }
  271. g_array_set_size(cbs, cbs->len + 1);
  272. return &g_array_index(cbs, struct qemu_plugin_dyn_cb, cbs->len - 1);
  273. }
  274. static enum plugin_dyn_cb_type op_to_cb_type(enum qemu_plugin_op op)
  275. {
  276. switch (op) {
  277. case QEMU_PLUGIN_INLINE_ADD_U64:
  278. return PLUGIN_CB_INLINE_ADD_U64;
  279. case QEMU_PLUGIN_INLINE_STORE_U64:
  280. return PLUGIN_CB_INLINE_STORE_U64;
  281. default:
  282. g_assert_not_reached();
  283. }
  284. }
  285. void plugin_register_inline_op_on_entry(GArray **arr,
  286. enum qemu_plugin_mem_rw rw,
  287. enum qemu_plugin_op op,
  288. qemu_plugin_u64 entry,
  289. uint64_t imm)
  290. {
  291. struct qemu_plugin_dyn_cb *dyn_cb;
  292. dyn_cb = plugin_get_dyn_cb(arr);
  293. dyn_cb->userp = NULL;
  294. dyn_cb->type = op_to_cb_type(op);
  295. dyn_cb->rw = rw;
  296. dyn_cb->inline_insn.entry = entry;
  297. dyn_cb->inline_insn.op = op;
  298. dyn_cb->inline_insn.imm = imm;
  299. }
  300. void plugin_register_dyn_cb__udata(GArray **arr,
  301. qemu_plugin_vcpu_udata_cb_t cb,
  302. enum qemu_plugin_cb_flags flags,
  303. void *udata)
  304. {
  305. static TCGHelperInfo info[3] = {
  306. [QEMU_PLUGIN_CB_NO_REGS].flags = TCG_CALL_NO_RWG,
  307. [QEMU_PLUGIN_CB_R_REGS].flags = TCG_CALL_NO_WG,
  308. /*
  309. * Match qemu_plugin_vcpu_udata_cb_t:
  310. * void (*)(uint32_t, void *)
  311. */
  312. [0 ... 2].typemask = (dh_typemask(void, 0) |
  313. dh_typemask(i32, 1) |
  314. dh_typemask(ptr, 2))
  315. };
  316. struct qemu_plugin_dyn_cb *dyn_cb = plugin_get_dyn_cb(arr);
  317. dyn_cb->userp = udata;
  318. dyn_cb->type = PLUGIN_CB_REGULAR;
  319. dyn_cb->regular.f.vcpu_udata = cb;
  320. assert((unsigned)flags < ARRAY_SIZE(info));
  321. dyn_cb->regular.info = &info[flags];
  322. }
  323. void plugin_register_dyn_cond_cb__udata(GArray **arr,
  324. qemu_plugin_vcpu_udata_cb_t cb,
  325. enum qemu_plugin_cb_flags flags,
  326. enum qemu_plugin_cond cond,
  327. qemu_plugin_u64 entry,
  328. uint64_t imm,
  329. void *udata)
  330. {
  331. static TCGHelperInfo info[3] = {
  332. [QEMU_PLUGIN_CB_NO_REGS].flags = TCG_CALL_NO_RWG,
  333. [QEMU_PLUGIN_CB_R_REGS].flags = TCG_CALL_NO_WG,
  334. /*
  335. * Match qemu_plugin_vcpu_udata_cb_t:
  336. * void (*)(uint32_t, void *)
  337. */
  338. [0 ... 2].typemask = (dh_typemask(void, 0) |
  339. dh_typemask(i32, 1) |
  340. dh_typemask(ptr, 2))
  341. };
  342. struct qemu_plugin_dyn_cb *dyn_cb = plugin_get_dyn_cb(arr);
  343. dyn_cb->userp = udata;
  344. dyn_cb->type = PLUGIN_CB_COND;
  345. dyn_cb->cond.f.vcpu_udata = cb;
  346. dyn_cb->cond.cond = cond;
  347. dyn_cb->cond.entry = entry;
  348. dyn_cb->cond.imm = imm;
  349. assert((unsigned)flags < ARRAY_SIZE(info));
  350. dyn_cb->cond.info = &info[flags];
  351. }
  352. void plugin_register_vcpu_mem_cb(GArray **arr,
  353. void *cb,
  354. enum qemu_plugin_cb_flags flags,
  355. enum qemu_plugin_mem_rw rw,
  356. void *udata)
  357. {
  358. /*
  359. * Expect that the underlying type for enum qemu_plugin_meminfo_t
  360. * is either int32_t or uint32_t, aka int or unsigned int.
  361. */
  362. QEMU_BUILD_BUG_ON(
  363. !__builtin_types_compatible_p(qemu_plugin_meminfo_t, uint32_t) &&
  364. !__builtin_types_compatible_p(qemu_plugin_meminfo_t, int32_t));
  365. static TCGHelperInfo info[3] = {
  366. [QEMU_PLUGIN_CB_NO_REGS].flags = TCG_CALL_NO_RWG,
  367. [QEMU_PLUGIN_CB_R_REGS].flags = TCG_CALL_NO_WG,
  368. /*
  369. * Match qemu_plugin_vcpu_mem_cb_t:
  370. * void (*)(uint32_t, qemu_plugin_meminfo_t, uint64_t, void *)
  371. */
  372. [0 ... 2].typemask =
  373. (dh_typemask(void, 0) |
  374. dh_typemask(i32, 1) |
  375. (__builtin_types_compatible_p(qemu_plugin_meminfo_t, uint32_t)
  376. ? dh_typemask(i32, 2) : dh_typemask(s32, 2)) |
  377. dh_typemask(i64, 3) |
  378. dh_typemask(ptr, 4))
  379. };
  380. struct qemu_plugin_dyn_cb *dyn_cb = plugin_get_dyn_cb(arr);
  381. dyn_cb->userp = udata;
  382. dyn_cb->type = PLUGIN_CB_MEM_REGULAR;
  383. dyn_cb->rw = rw;
  384. dyn_cb->regular.f.vcpu_mem = cb;
  385. assert((unsigned)flags < ARRAY_SIZE(info));
  386. dyn_cb->regular.info = &info[flags];
  387. }
  388. /*
  389. * Disable CFI checks.
  390. * The callback function has been loaded from an external library so we do not
  391. * have type information
  392. */
  393. QEMU_DISABLE_CFI
  394. void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb)
  395. {
  396. struct qemu_plugin_cb *cb, *next;
  397. enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_TB_TRANS;
  398. /* no plugin_state->event_mask check here; caller should have checked */
  399. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  400. qemu_plugin_vcpu_tb_trans_cb_t func = cb->f.vcpu_tb_trans;
  401. func(cb->ctx->id, tb);
  402. }
  403. }
  404. /*
  405. * Disable CFI checks.
  406. * The callback function has been loaded from an external library so we do not
  407. * have type information
  408. */
  409. QEMU_DISABLE_CFI
  410. void
  411. qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,
  412. uint64_t a3, uint64_t a4, uint64_t a5,
  413. uint64_t a6, uint64_t a7, uint64_t a8)
  414. {
  415. struct qemu_plugin_cb *cb, *next;
  416. enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL;
  417. if (!test_bit(ev, cpu->plugin_state->event_mask)) {
  418. return;
  419. }
  420. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  421. qemu_plugin_vcpu_syscall_cb_t func = cb->f.vcpu_syscall;
  422. func(cb->ctx->id, cpu->cpu_index, num, a1, a2, a3, a4, a5, a6, a7, a8);
  423. }
  424. }
  425. /*
  426. * Disable CFI checks.
  427. * The callback function has been loaded from an external library so we do not
  428. * have type information
  429. */
  430. QEMU_DISABLE_CFI
  431. void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret)
  432. {
  433. struct qemu_plugin_cb *cb, *next;
  434. enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL_RET;
  435. if (!test_bit(ev, cpu->plugin_state->event_mask)) {
  436. return;
  437. }
  438. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  439. qemu_plugin_vcpu_syscall_ret_cb_t func = cb->f.vcpu_syscall_ret;
  440. func(cb->ctx->id, cpu->cpu_index, num, ret);
  441. }
  442. }
  443. void qemu_plugin_vcpu_idle_cb(CPUState *cpu)
  444. {
  445. /* idle and resume cb may be called before init, ignore in this case */
  446. if (cpu->cpu_index < plugin.num_vcpus) {
  447. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_IDLE);
  448. }
  449. }
  450. void qemu_plugin_vcpu_resume_cb(CPUState *cpu)
  451. {
  452. if (cpu->cpu_index < plugin.num_vcpus) {
  453. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_RESUME);
  454. }
  455. }
  456. void qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id,
  457. qemu_plugin_vcpu_simple_cb_t cb)
  458. {
  459. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_IDLE, cb);
  460. }
  461. void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id,
  462. qemu_plugin_vcpu_simple_cb_t cb)
  463. {
  464. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_RESUME, cb);
  465. }
  466. void qemu_plugin_register_flush_cb(qemu_plugin_id_t id,
  467. qemu_plugin_simple_cb_t cb)
  468. {
  469. plugin_register_cb(id, QEMU_PLUGIN_EV_FLUSH, cb);
  470. }
  471. static bool free_dyn_cb_arr(void *p, uint32_t h, void *userp)
  472. {
  473. g_array_free((GArray *) p, true);
  474. return true;
  475. }
  476. void qemu_plugin_flush_cb(void)
  477. {
  478. qht_iter_remove(&plugin.dyn_cb_arr_ht, free_dyn_cb_arr, NULL);
  479. qht_reset(&plugin.dyn_cb_arr_ht);
  480. plugin_cb__simple(QEMU_PLUGIN_EV_FLUSH);
  481. }
  482. void exec_inline_op(struct qemu_plugin_dyn_cb *cb, int cpu_index)
  483. {
  484. char *ptr = cb->inline_insn.entry.score->data->data;
  485. size_t elem_size = g_array_get_element_size(
  486. cb->inline_insn.entry.score->data);
  487. size_t offset = cb->inline_insn.entry.offset;
  488. uint64_t *val = (uint64_t *)(ptr + offset + cpu_index * elem_size);
  489. switch (cb->inline_insn.op) {
  490. case QEMU_PLUGIN_INLINE_ADD_U64:
  491. *val += cb->inline_insn.imm;
  492. break;
  493. case QEMU_PLUGIN_INLINE_STORE_U64:
  494. *val = cb->inline_insn.imm;
  495. break;
  496. default:
  497. g_assert_not_reached();
  498. }
  499. }
  500. void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
  501. MemOpIdx oi, enum qemu_plugin_mem_rw rw)
  502. {
  503. GArray *arr = cpu->neg.plugin_mem_cbs;
  504. size_t i;
  505. if (arr == NULL) {
  506. return;
  507. }
  508. for (i = 0; i < arr->len; i++) {
  509. struct qemu_plugin_dyn_cb *cb =
  510. &g_array_index(arr, struct qemu_plugin_dyn_cb, i);
  511. if (!(rw & cb->rw)) {
  512. break;
  513. }
  514. switch (cb->type) {
  515. case PLUGIN_CB_MEM_REGULAR:
  516. cb->regular.f.vcpu_mem(cpu->cpu_index, make_plugin_meminfo(oi, rw),
  517. vaddr, cb->userp);
  518. break;
  519. case PLUGIN_CB_INLINE_ADD_U64:
  520. case PLUGIN_CB_INLINE_STORE_U64:
  521. exec_inline_op(cb, cpu->cpu_index);
  522. break;
  523. default:
  524. g_assert_not_reached();
  525. }
  526. }
  527. }
  528. void qemu_plugin_atexit_cb(void)
  529. {
  530. plugin_cb__udata(QEMU_PLUGIN_EV_ATEXIT);
  531. }
  532. void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id,
  533. qemu_plugin_udata_cb_t cb,
  534. void *udata)
  535. {
  536. plugin_register_cb_udata(id, QEMU_PLUGIN_EV_ATEXIT, cb, udata);
  537. }
  538. /*
  539. * Handle exit from linux-user. Unlike the normal atexit() mechanism
  540. * we need to handle the clean-up manually as it's possible threads
  541. * are still running. We need to remove all callbacks from code
  542. * generation, flush the current translations and then we can safely
  543. * trigger the exit callbacks.
  544. */
  545. void qemu_plugin_user_exit(void)
  546. {
  547. enum qemu_plugin_event ev;
  548. CPUState *cpu;
  549. /*
  550. * Locking order: we must acquire locks in an order that is consistent
  551. * with the one in fork_start(). That is:
  552. * - start_exclusive(), which acquires qemu_cpu_list_lock,
  553. * must be called before acquiring plugin.lock.
  554. * - tb_flush(), which acquires mmap_lock(), must be called
  555. * while plugin.lock is not held.
  556. */
  557. start_exclusive();
  558. qemu_rec_mutex_lock(&plugin.lock);
  559. /* un-register all callbacks except the final AT_EXIT one */
  560. for (ev = 0; ev < QEMU_PLUGIN_EV_MAX; ev++) {
  561. if (ev != QEMU_PLUGIN_EV_ATEXIT) {
  562. struct qemu_plugin_cb *cb, *next;
  563. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  564. plugin_unregister_cb__locked(cb->ctx, ev);
  565. }
  566. }
  567. }
  568. CPU_FOREACH(cpu) {
  569. qemu_plugin_disable_mem_helpers(cpu);
  570. }
  571. qemu_rec_mutex_unlock(&plugin.lock);
  572. tb_flush(current_cpu);
  573. end_exclusive();
  574. /* now it's safe to handle the exit case */
  575. qemu_plugin_atexit_cb();
  576. }
  577. /*
  578. * Helpers for *-user to ensure locks are sane across fork() events.
  579. */
  580. void qemu_plugin_user_prefork_lock(void)
  581. {
  582. qemu_rec_mutex_lock(&plugin.lock);
  583. }
  584. void qemu_plugin_user_postfork(bool is_child)
  585. {
  586. if (is_child) {
  587. /* should we just reset via plugin_init? */
  588. qemu_rec_mutex_init(&plugin.lock);
  589. } else {
  590. qemu_rec_mutex_unlock(&plugin.lock);
  591. }
  592. }
  593. static bool plugin_dyn_cb_arr_cmp(const void *ap, const void *bp)
  594. {
  595. return ap == bp;
  596. }
  597. static void __attribute__((__constructor__)) plugin_init(void)
  598. {
  599. int i;
  600. for (i = 0; i < QEMU_PLUGIN_EV_MAX; i++) {
  601. QLIST_INIT(&plugin.cb_lists[i]);
  602. }
  603. qemu_rec_mutex_init(&plugin.lock);
  604. plugin.id_ht = g_hash_table_new(g_int64_hash, g_int64_equal);
  605. plugin.cpu_ht = g_hash_table_new(g_int_hash, g_int_equal);
  606. QLIST_INIT(&plugin.scoreboards);
  607. plugin.scoreboard_alloc_size = 16; /* avoid frequent reallocation */
  608. QTAILQ_INIT(&plugin.ctxs);
  609. qht_init(&plugin.dyn_cb_arr_ht, plugin_dyn_cb_arr_cmp, 16,
  610. QHT_MODE_AUTO_RESIZE);
  611. atexit(qemu_plugin_atexit_cb);
  612. }
  613. int plugin_num_vcpus(void)
  614. {
  615. return plugin.num_vcpus;
  616. }
  617. struct qemu_plugin_scoreboard *plugin_scoreboard_new(size_t element_size)
  618. {
  619. struct qemu_plugin_scoreboard *score =
  620. g_malloc0(sizeof(struct qemu_plugin_scoreboard));
  621. score->data = g_array_new(FALSE, TRUE, element_size);
  622. g_array_set_size(score->data, plugin.scoreboard_alloc_size);
  623. qemu_rec_mutex_lock(&plugin.lock);
  624. QLIST_INSERT_HEAD(&plugin.scoreboards, score, entry);
  625. qemu_rec_mutex_unlock(&plugin.lock);
  626. return score;
  627. }
  628. void plugin_scoreboard_free(struct qemu_plugin_scoreboard *score)
  629. {
  630. qemu_rec_mutex_lock(&plugin.lock);
  631. QLIST_REMOVE(score, entry);
  632. qemu_rec_mutex_unlock(&plugin.lock);
  633. g_array_free(score->data, TRUE);
  634. g_free(score);
  635. }