core.c 23 KB

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