core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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/lockable.h"
  16. #include "qemu/option.h"
  17. #include "qemu/plugin.h"
  18. #include "qemu/queue.h"
  19. #include "qemu/rcu_queue.h"
  20. #include "qemu/rcu.h"
  21. #include "exec/tb-flush.h"
  22. #include "tcg/tcg-op-common.h"
  23. #include "plugin.h"
  24. struct qemu_plugin_cb {
  25. struct qemu_plugin_ctx *ctx;
  26. union qemu_plugin_cb_sig f;
  27. void *udata;
  28. QLIST_ENTRY(qemu_plugin_cb) entry;
  29. };
  30. struct qemu_plugin_state plugin;
  31. struct qemu_plugin_ctx *plugin_id_to_ctx_locked(qemu_plugin_id_t id)
  32. {
  33. struct qemu_plugin_ctx *ctx;
  34. qemu_plugin_id_t *id_p;
  35. id_p = g_hash_table_lookup(plugin.id_ht, &id);
  36. ctx = container_of(id_p, struct qemu_plugin_ctx, id);
  37. if (ctx == NULL) {
  38. error_report("plugin: invalid plugin id %" PRIu64, id);
  39. abort();
  40. }
  41. return ctx;
  42. }
  43. static void plugin_cpu_update__async(CPUState *cpu, run_on_cpu_data data)
  44. {
  45. bitmap_copy(cpu->plugin_state->event_mask,
  46. &data.host_ulong, QEMU_PLUGIN_EV_MAX);
  47. tcg_flush_jmp_cache(cpu);
  48. }
  49. static void plugin_cpu_update__locked(gpointer k, gpointer v, gpointer udata)
  50. {
  51. CPUState *cpu = container_of(k, CPUState, cpu_index);
  52. run_on_cpu_data mask = RUN_ON_CPU_HOST_ULONG(*plugin.mask);
  53. async_run_on_cpu(cpu, plugin_cpu_update__async, mask);
  54. }
  55. void plugin_unregister_cb__locked(struct qemu_plugin_ctx *ctx,
  56. enum qemu_plugin_event ev)
  57. {
  58. struct qemu_plugin_cb *cb = ctx->callbacks[ev];
  59. if (cb == NULL) {
  60. return;
  61. }
  62. QLIST_REMOVE_RCU(cb, entry);
  63. g_free(cb);
  64. ctx->callbacks[ev] = NULL;
  65. if (QLIST_EMPTY_RCU(&plugin.cb_lists[ev])) {
  66. clear_bit(ev, plugin.mask);
  67. g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked, NULL);
  68. }
  69. }
  70. /*
  71. * Disable CFI checks.
  72. * The callback function has been loaded from an external library so we do not
  73. * have type information
  74. */
  75. QEMU_DISABLE_CFI
  76. static void plugin_vcpu_cb__simple(CPUState *cpu, enum qemu_plugin_event ev)
  77. {
  78. struct qemu_plugin_cb *cb, *next;
  79. switch (ev) {
  80. case QEMU_PLUGIN_EV_VCPU_INIT:
  81. case QEMU_PLUGIN_EV_VCPU_EXIT:
  82. case QEMU_PLUGIN_EV_VCPU_IDLE:
  83. case QEMU_PLUGIN_EV_VCPU_RESUME:
  84. /* iterate safely; plugins might uninstall themselves at any time */
  85. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  86. qemu_plugin_vcpu_simple_cb_t func = cb->f.vcpu_simple;
  87. func(cb->ctx->id, cpu->cpu_index);
  88. }
  89. break;
  90. default:
  91. g_assert_not_reached();
  92. }
  93. }
  94. /*
  95. * Disable CFI checks.
  96. * The callback function has been loaded from an external library so we do not
  97. * have type information
  98. */
  99. QEMU_DISABLE_CFI
  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. /*
  115. * Disable CFI checks.
  116. * The callback function has been loaded from an external library so we do not
  117. * have type information
  118. */
  119. QEMU_DISABLE_CFI
  120. static void plugin_cb__udata(enum qemu_plugin_event ev)
  121. {
  122. struct qemu_plugin_cb *cb, *next;
  123. switch (ev) {
  124. case QEMU_PLUGIN_EV_ATEXIT:
  125. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  126. qemu_plugin_udata_cb_t func = cb->f.udata;
  127. func(cb->ctx->id, cb->udata);
  128. }
  129. break;
  130. default:
  131. g_assert_not_reached();
  132. }
  133. }
  134. static void
  135. do_plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
  136. void *func, void *udata)
  137. {
  138. struct qemu_plugin_ctx *ctx;
  139. QEMU_LOCK_GUARD(&plugin.lock);
  140. ctx = plugin_id_to_ctx_locked(id);
  141. /* if the plugin is on its way out, ignore this request */
  142. if (unlikely(ctx->uninstalling)) {
  143. return;
  144. }
  145. if (func) {
  146. struct qemu_plugin_cb *cb = ctx->callbacks[ev];
  147. if (cb) {
  148. cb->f.generic = func;
  149. cb->udata = udata;
  150. } else {
  151. cb = g_new(struct qemu_plugin_cb, 1);
  152. cb->ctx = ctx;
  153. cb->f.generic = func;
  154. cb->udata = udata;
  155. ctx->callbacks[ev] = cb;
  156. QLIST_INSERT_HEAD_RCU(&plugin.cb_lists[ev], cb, entry);
  157. if (!test_bit(ev, plugin.mask)) {
  158. set_bit(ev, plugin.mask);
  159. g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked,
  160. NULL);
  161. }
  162. }
  163. } else {
  164. plugin_unregister_cb__locked(ctx, ev);
  165. }
  166. }
  167. void plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
  168. void *func)
  169. {
  170. do_plugin_register_cb(id, ev, func, NULL);
  171. }
  172. void
  173. plugin_register_cb_udata(qemu_plugin_id_t id, enum qemu_plugin_event ev,
  174. void *func, void *udata)
  175. {
  176. do_plugin_register_cb(id, ev, func, udata);
  177. }
  178. CPUPluginState *qemu_plugin_create_vcpu_state(void)
  179. {
  180. return g_new0(CPUPluginState, 1);
  181. }
  182. static void plugin_grow_scoreboards__locked(CPUState *cpu)
  183. {
  184. size_t scoreboard_size = plugin.scoreboard_alloc_size;
  185. bool need_realloc = false;
  186. if (cpu->cpu_index < scoreboard_size) {
  187. return;
  188. }
  189. while (cpu->cpu_index >= scoreboard_size) {
  190. scoreboard_size *= 2;
  191. need_realloc = true;
  192. }
  193. if (!need_realloc) {
  194. return;
  195. }
  196. if (QLIST_EMPTY(&plugin.scoreboards)) {
  197. /* just update size for future scoreboards */
  198. plugin.scoreboard_alloc_size = scoreboard_size;
  199. return;
  200. }
  201. /*
  202. * A scoreboard creation/deletion might be in progress. If a new vcpu is
  203. * initialized at the same time, we are safe, as the new
  204. * plugin.scoreboard_alloc_size was not yet written.
  205. */
  206. qemu_rec_mutex_unlock(&plugin.lock);
  207. /* cpus must be stopped, as tb might still use an existing scoreboard. */
  208. start_exclusive();
  209. /* re-acquire lock */
  210. qemu_rec_mutex_lock(&plugin.lock);
  211. /* in case another vcpu is created between unlock and exclusive section. */
  212. if (scoreboard_size > plugin.scoreboard_alloc_size) {
  213. struct qemu_plugin_scoreboard *score;
  214. QLIST_FOREACH(score, &plugin.scoreboards, entry) {
  215. g_array_set_size(score->data, scoreboard_size);
  216. }
  217. plugin.scoreboard_alloc_size = scoreboard_size;
  218. /* force all tb to be flushed, as scoreboard pointers were changed. */
  219. tb_flush(cpu);
  220. }
  221. end_exclusive();
  222. }
  223. static void qemu_plugin_vcpu_init__async(CPUState *cpu, run_on_cpu_data unused)
  224. {
  225. bool success;
  226. assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
  227. qemu_rec_mutex_lock(&plugin.lock);
  228. plugin.num_vcpus = MAX(plugin.num_vcpus, cpu->cpu_index + 1);
  229. plugin_cpu_update__locked(&cpu->cpu_index, NULL, NULL);
  230. success = g_hash_table_insert(plugin.cpu_ht, &cpu->cpu_index,
  231. &cpu->cpu_index);
  232. g_assert(success);
  233. plugin_grow_scoreboards__locked(cpu);
  234. qemu_rec_mutex_unlock(&plugin.lock);
  235. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_INIT);
  236. }
  237. void qemu_plugin_vcpu_init_hook(CPUState *cpu)
  238. {
  239. /* Plugin initialization must wait until the cpu start executing code */
  240. async_run_on_cpu(cpu, qemu_plugin_vcpu_init__async, RUN_ON_CPU_NULL);
  241. }
  242. void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
  243. {
  244. bool success;
  245. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_EXIT);
  246. assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
  247. qemu_rec_mutex_lock(&plugin.lock);
  248. success = g_hash_table_remove(plugin.cpu_ht, &cpu->cpu_index);
  249. g_assert(success);
  250. qemu_rec_mutex_unlock(&plugin.lock);
  251. }
  252. struct plugin_for_each_args {
  253. struct qemu_plugin_ctx *ctx;
  254. qemu_plugin_vcpu_simple_cb_t cb;
  255. };
  256. static void plugin_vcpu_for_each(gpointer k, gpointer v, gpointer udata)
  257. {
  258. struct plugin_for_each_args *args = udata;
  259. int cpu_index = *(int *)k;
  260. args->cb(args->ctx->id, cpu_index);
  261. }
  262. void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id,
  263. qemu_plugin_vcpu_simple_cb_t cb)
  264. {
  265. struct plugin_for_each_args args;
  266. if (cb == NULL) {
  267. return;
  268. }
  269. qemu_rec_mutex_lock(&plugin.lock);
  270. args.ctx = plugin_id_to_ctx_locked(id);
  271. args.cb = cb;
  272. g_hash_table_foreach(plugin.cpu_ht, plugin_vcpu_for_each, &args);
  273. qemu_rec_mutex_unlock(&plugin.lock);
  274. }
  275. /* Allocate and return a callback record */
  276. static struct qemu_plugin_dyn_cb *plugin_get_dyn_cb(GArray **arr)
  277. {
  278. GArray *cbs = *arr;
  279. if (!cbs) {
  280. cbs = g_array_sized_new(false, true,
  281. sizeof(struct qemu_plugin_dyn_cb), 1);
  282. *arr = cbs;
  283. }
  284. g_array_set_size(cbs, cbs->len + 1);
  285. return &g_array_index(cbs, struct qemu_plugin_dyn_cb, cbs->len - 1);
  286. }
  287. static enum plugin_dyn_cb_type op_to_cb_type(enum qemu_plugin_op op)
  288. {
  289. switch (op) {
  290. case QEMU_PLUGIN_INLINE_ADD_U64:
  291. return PLUGIN_CB_INLINE_ADD_U64;
  292. case QEMU_PLUGIN_INLINE_STORE_U64:
  293. return PLUGIN_CB_INLINE_STORE_U64;
  294. default:
  295. g_assert_not_reached();
  296. }
  297. }
  298. void plugin_register_inline_op_on_entry(GArray **arr,
  299. enum qemu_plugin_mem_rw rw,
  300. enum qemu_plugin_op op,
  301. qemu_plugin_u64 entry,
  302. uint64_t imm)
  303. {
  304. struct qemu_plugin_dyn_cb *dyn_cb;
  305. struct qemu_plugin_inline_cb inline_cb = { .rw = rw,
  306. .entry = entry,
  307. .imm = imm };
  308. dyn_cb = plugin_get_dyn_cb(arr);
  309. dyn_cb->type = op_to_cb_type(op);
  310. dyn_cb->inline_insn = inline_cb;
  311. }
  312. void plugin_register_dyn_cb__udata(GArray **arr,
  313. qemu_plugin_vcpu_udata_cb_t cb,
  314. enum qemu_plugin_cb_flags flags,
  315. void *udata)
  316. {
  317. static TCGHelperInfo info[3] = {
  318. [QEMU_PLUGIN_CB_NO_REGS].flags = TCG_CALL_NO_RWG,
  319. [QEMU_PLUGIN_CB_R_REGS].flags = TCG_CALL_NO_WG,
  320. /*
  321. * Match qemu_plugin_vcpu_udata_cb_t:
  322. * void (*)(uint32_t, void *)
  323. */
  324. [0 ... 2].typemask = (dh_typemask(void, 0) |
  325. dh_typemask(i32, 1) |
  326. dh_typemask(ptr, 2))
  327. };
  328. assert((unsigned)flags < ARRAY_SIZE(info));
  329. struct qemu_plugin_dyn_cb *dyn_cb = plugin_get_dyn_cb(arr);
  330. struct qemu_plugin_regular_cb regular_cb = { .f.vcpu_udata = cb,
  331. .userp = udata,
  332. .info = &info[flags] };
  333. dyn_cb->type = PLUGIN_CB_REGULAR;
  334. dyn_cb->regular = regular_cb;
  335. }
  336. void plugin_register_dyn_cond_cb__udata(GArray **arr,
  337. qemu_plugin_vcpu_udata_cb_t cb,
  338. enum qemu_plugin_cb_flags flags,
  339. enum qemu_plugin_cond cond,
  340. qemu_plugin_u64 entry,
  341. uint64_t imm,
  342. void *udata)
  343. {
  344. static TCGHelperInfo info[3] = {
  345. [QEMU_PLUGIN_CB_NO_REGS].flags = TCG_CALL_NO_RWG,
  346. [QEMU_PLUGIN_CB_R_REGS].flags = TCG_CALL_NO_WG,
  347. /*
  348. * Match qemu_plugin_vcpu_udata_cb_t:
  349. * void (*)(uint32_t, void *)
  350. */
  351. [0 ... 2].typemask = (dh_typemask(void, 0) |
  352. dh_typemask(i32, 1) |
  353. dh_typemask(ptr, 2))
  354. };
  355. assert((unsigned)flags < ARRAY_SIZE(info));
  356. struct qemu_plugin_dyn_cb *dyn_cb = plugin_get_dyn_cb(arr);
  357. struct qemu_plugin_conditional_cb cond_cb = { .userp = udata,
  358. .f.vcpu_udata = cb,
  359. .cond = cond,
  360. .entry = entry,
  361. .imm = imm,
  362. .info = &info[flags] };
  363. dyn_cb->type = PLUGIN_CB_COND;
  364. dyn_cb->cond = cond_cb;
  365. }
  366. void plugin_register_vcpu_mem_cb(GArray **arr,
  367. void *cb,
  368. enum qemu_plugin_cb_flags flags,
  369. enum qemu_plugin_mem_rw rw,
  370. void *udata)
  371. {
  372. /*
  373. * Expect that the underlying type for enum qemu_plugin_meminfo_t
  374. * is either int32_t or uint32_t, aka int or unsigned int.
  375. */
  376. QEMU_BUILD_BUG_ON(
  377. !__builtin_types_compatible_p(qemu_plugin_meminfo_t, uint32_t) &&
  378. !__builtin_types_compatible_p(qemu_plugin_meminfo_t, int32_t));
  379. static TCGHelperInfo info[3] = {
  380. [QEMU_PLUGIN_CB_NO_REGS].flags = TCG_CALL_NO_RWG,
  381. [QEMU_PLUGIN_CB_R_REGS].flags = TCG_CALL_NO_WG,
  382. /*
  383. * Match qemu_plugin_vcpu_mem_cb_t:
  384. * void (*)(uint32_t, qemu_plugin_meminfo_t, uint64_t, void *)
  385. */
  386. [0 ... 2].typemask =
  387. (dh_typemask(void, 0) |
  388. dh_typemask(i32, 1) |
  389. (__builtin_types_compatible_p(qemu_plugin_meminfo_t, uint32_t)
  390. ? dh_typemask(i32, 2) : dh_typemask(s32, 2)) |
  391. dh_typemask(i64, 3) |
  392. dh_typemask(ptr, 4))
  393. };
  394. assert((unsigned)flags < ARRAY_SIZE(info));
  395. struct qemu_plugin_dyn_cb *dyn_cb = plugin_get_dyn_cb(arr);
  396. struct qemu_plugin_regular_cb regular_cb = { .userp = udata,
  397. .rw = rw,
  398. .f.vcpu_mem = cb,
  399. .info = &info[flags] };
  400. dyn_cb->type = PLUGIN_CB_MEM_REGULAR;
  401. dyn_cb->regular = regular_cb;
  402. }
  403. /*
  404. * Disable CFI checks.
  405. * The callback function has been loaded from an external library so we do not
  406. * have type information
  407. */
  408. QEMU_DISABLE_CFI
  409. void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb)
  410. {
  411. struct qemu_plugin_cb *cb, *next;
  412. enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_TB_TRANS;
  413. /* no plugin_state->event_mask check here; caller should have checked */
  414. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  415. qemu_plugin_vcpu_tb_trans_cb_t func = cb->f.vcpu_tb_trans;
  416. func(cb->ctx->id, tb);
  417. }
  418. }
  419. /*
  420. * Disable CFI checks.
  421. * The callback function has been loaded from an external library so we do not
  422. * have type information
  423. */
  424. QEMU_DISABLE_CFI
  425. void
  426. qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,
  427. uint64_t a3, uint64_t a4, uint64_t a5,
  428. uint64_t a6, uint64_t a7, uint64_t a8)
  429. {
  430. struct qemu_plugin_cb *cb, *next;
  431. enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL;
  432. if (!test_bit(ev, cpu->plugin_state->event_mask)) {
  433. return;
  434. }
  435. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  436. qemu_plugin_vcpu_syscall_cb_t func = cb->f.vcpu_syscall;
  437. func(cb->ctx->id, cpu->cpu_index, num, a1, a2, a3, a4, a5, a6, a7, a8);
  438. }
  439. }
  440. /*
  441. * Disable CFI checks.
  442. * The callback function has been loaded from an external library so we do not
  443. * have type information
  444. */
  445. QEMU_DISABLE_CFI
  446. void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret)
  447. {
  448. struct qemu_plugin_cb *cb, *next;
  449. enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL_RET;
  450. if (!test_bit(ev, cpu->plugin_state->event_mask)) {
  451. return;
  452. }
  453. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  454. qemu_plugin_vcpu_syscall_ret_cb_t func = cb->f.vcpu_syscall_ret;
  455. func(cb->ctx->id, cpu->cpu_index, num, ret);
  456. }
  457. }
  458. void qemu_plugin_vcpu_idle_cb(CPUState *cpu)
  459. {
  460. /* idle and resume cb may be called before init, ignore in this case */
  461. if (cpu->cpu_index < plugin.num_vcpus) {
  462. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_IDLE);
  463. }
  464. }
  465. void qemu_plugin_vcpu_resume_cb(CPUState *cpu)
  466. {
  467. if (cpu->cpu_index < plugin.num_vcpus) {
  468. plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_RESUME);
  469. }
  470. }
  471. void qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id,
  472. qemu_plugin_vcpu_simple_cb_t cb)
  473. {
  474. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_IDLE, cb);
  475. }
  476. void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id,
  477. qemu_plugin_vcpu_simple_cb_t cb)
  478. {
  479. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_RESUME, cb);
  480. }
  481. void qemu_plugin_register_flush_cb(qemu_plugin_id_t id,
  482. qemu_plugin_simple_cb_t cb)
  483. {
  484. plugin_register_cb(id, QEMU_PLUGIN_EV_FLUSH, cb);
  485. }
  486. static bool free_dyn_cb_arr(void *p, uint32_t h, void *userp)
  487. {
  488. g_array_free((GArray *) p, true);
  489. return true;
  490. }
  491. void qemu_plugin_flush_cb(void)
  492. {
  493. qht_iter_remove(&plugin.dyn_cb_arr_ht, free_dyn_cb_arr, NULL);
  494. qht_reset(&plugin.dyn_cb_arr_ht);
  495. plugin_cb__simple(QEMU_PLUGIN_EV_FLUSH);
  496. }
  497. void exec_inline_op(enum plugin_dyn_cb_type type,
  498. struct qemu_plugin_inline_cb *cb,
  499. int cpu_index)
  500. {
  501. char *ptr = cb->entry.score->data->data;
  502. size_t elem_size = g_array_get_element_size(
  503. cb->entry.score->data);
  504. size_t offset = cb->entry.offset;
  505. uint64_t *val = (uint64_t *)(ptr + offset + cpu_index * elem_size);
  506. switch (type) {
  507. case PLUGIN_CB_INLINE_ADD_U64:
  508. *val += cb->imm;
  509. break;
  510. case PLUGIN_CB_INLINE_STORE_U64:
  511. *val = cb->imm;
  512. break;
  513. default:
  514. g_assert_not_reached();
  515. }
  516. }
  517. void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
  518. uint64_t value_low,
  519. uint64_t value_high,
  520. MemOpIdx oi, enum qemu_plugin_mem_rw rw)
  521. {
  522. GArray *arr = cpu->neg.plugin_mem_cbs;
  523. size_t i;
  524. if (arr == NULL) {
  525. return;
  526. }
  527. cpu->neg.plugin_mem_value_low = value_low;
  528. cpu->neg.plugin_mem_value_high = value_high;
  529. for (i = 0; i < arr->len; i++) {
  530. struct qemu_plugin_dyn_cb *cb =
  531. &g_array_index(arr, struct qemu_plugin_dyn_cb, i);
  532. switch (cb->type) {
  533. case PLUGIN_CB_MEM_REGULAR:
  534. if (rw & cb->regular.rw) {
  535. cb->regular.f.vcpu_mem(cpu->cpu_index,
  536. make_plugin_meminfo(oi, rw),
  537. vaddr, cb->regular.userp);
  538. }
  539. break;
  540. case PLUGIN_CB_INLINE_ADD_U64:
  541. case PLUGIN_CB_INLINE_STORE_U64:
  542. if (rw & cb->inline_insn.rw) {
  543. exec_inline_op(cb->type, &cb->inline_insn, cpu->cpu_index);
  544. }
  545. break;
  546. default:
  547. g_assert_not_reached();
  548. }
  549. }
  550. }
  551. void qemu_plugin_atexit_cb(void)
  552. {
  553. plugin_cb__udata(QEMU_PLUGIN_EV_ATEXIT);
  554. }
  555. void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id,
  556. qemu_plugin_udata_cb_t cb,
  557. void *udata)
  558. {
  559. plugin_register_cb_udata(id, QEMU_PLUGIN_EV_ATEXIT, cb, udata);
  560. }
  561. /*
  562. * Handle exit from linux-user. Unlike the normal atexit() mechanism
  563. * we need to handle the clean-up manually as it's possible threads
  564. * are still running. We need to remove all callbacks from code
  565. * generation, flush the current translations and then we can safely
  566. * trigger the exit callbacks.
  567. */
  568. void qemu_plugin_user_exit(void)
  569. {
  570. enum qemu_plugin_event ev;
  571. CPUState *cpu;
  572. /*
  573. * Locking order: we must acquire locks in an order that is consistent
  574. * with the one in fork_start(). That is:
  575. * - start_exclusive(), which acquires qemu_cpu_list_lock,
  576. * must be called before acquiring plugin.lock.
  577. * - tb_flush(), which acquires mmap_lock(), must be called
  578. * while plugin.lock is not held.
  579. */
  580. start_exclusive();
  581. qemu_rec_mutex_lock(&plugin.lock);
  582. /* un-register all callbacks except the final AT_EXIT one */
  583. for (ev = 0; ev < QEMU_PLUGIN_EV_MAX; ev++) {
  584. if (ev != QEMU_PLUGIN_EV_ATEXIT) {
  585. struct qemu_plugin_cb *cb, *next;
  586. QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
  587. plugin_unregister_cb__locked(cb->ctx, ev);
  588. }
  589. }
  590. }
  591. CPU_FOREACH(cpu) {
  592. qemu_plugin_disable_mem_helpers(cpu);
  593. }
  594. qemu_rec_mutex_unlock(&plugin.lock);
  595. tb_flush(current_cpu);
  596. end_exclusive();
  597. /* now it's safe to handle the exit case */
  598. qemu_plugin_atexit_cb();
  599. }
  600. /*
  601. * Helpers for *-user to ensure locks are sane across fork() events.
  602. */
  603. void qemu_plugin_user_prefork_lock(void)
  604. {
  605. qemu_rec_mutex_lock(&plugin.lock);
  606. }
  607. void qemu_plugin_user_postfork(bool is_child)
  608. {
  609. if (is_child) {
  610. /* should we just reset via plugin_init? */
  611. qemu_rec_mutex_init(&plugin.lock);
  612. } else {
  613. qemu_rec_mutex_unlock(&plugin.lock);
  614. }
  615. }
  616. static bool plugin_dyn_cb_arr_cmp(const void *ap, const void *bp)
  617. {
  618. return ap == bp;
  619. }
  620. static void __attribute__((__constructor__)) plugin_init(void)
  621. {
  622. int i;
  623. for (i = 0; i < QEMU_PLUGIN_EV_MAX; i++) {
  624. QLIST_INIT(&plugin.cb_lists[i]);
  625. }
  626. qemu_rec_mutex_init(&plugin.lock);
  627. plugin.id_ht = g_hash_table_new(g_int64_hash, g_int64_equal);
  628. plugin.cpu_ht = g_hash_table_new(g_int_hash, g_int_equal);
  629. QLIST_INIT(&plugin.scoreboards);
  630. plugin.scoreboard_alloc_size = 16; /* avoid frequent reallocation */
  631. QTAILQ_INIT(&plugin.ctxs);
  632. qht_init(&plugin.dyn_cb_arr_ht, plugin_dyn_cb_arr_cmp, 16,
  633. QHT_MODE_AUTO_RESIZE);
  634. atexit(qemu_plugin_atexit_cb);
  635. }
  636. int plugin_num_vcpus(void)
  637. {
  638. return plugin.num_vcpus;
  639. }
  640. struct qemu_plugin_scoreboard *plugin_scoreboard_new(size_t element_size)
  641. {
  642. struct qemu_plugin_scoreboard *score =
  643. g_malloc0(sizeof(struct qemu_plugin_scoreboard));
  644. score->data = g_array_new(FALSE, TRUE, element_size);
  645. g_array_set_size(score->data, plugin.scoreboard_alloc_size);
  646. qemu_rec_mutex_lock(&plugin.lock);
  647. QLIST_INSERT_HEAD(&plugin.scoreboards, score, entry);
  648. qemu_rec_mutex_unlock(&plugin.lock);
  649. return score;
  650. }
  651. void plugin_scoreboard_free(struct qemu_plugin_scoreboard *score)
  652. {
  653. qemu_rec_mutex_lock(&plugin.lock);
  654. QLIST_REMOVE(score, entry);
  655. qemu_rec_mutex_unlock(&plugin.lock);
  656. g_array_free(score->data, TRUE);
  657. g_free(score);
  658. }