2
0

api.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * QEMU Plugin API
  3. *
  4. * This provides the API that is available to the plugins to interact
  5. * with QEMU. We have to be careful not to expose internal details of
  6. * how QEMU works so we abstract out things like translation and
  7. * instructions to anonymous data types:
  8. *
  9. * qemu_plugin_tb
  10. * qemu_plugin_insn
  11. * qemu_plugin_register
  12. *
  13. * Which can then be passed back into the API to do additional things.
  14. * As such all the public functions in here are exported in
  15. * qemu-plugin.h.
  16. *
  17. * The general life-cycle of a plugin is:
  18. *
  19. * - plugin is loaded, public qemu_plugin_install called
  20. * - the install func registers callbacks for events
  21. * - usually an atexit_cb is registered to dump info at the end
  22. * - when a registered event occurs the plugin is called
  23. * - some events pass additional info
  24. * - during translation the plugin can decide to instrument any
  25. * instruction
  26. * - when QEMU exits all the registered atexit callbacks are called
  27. *
  28. * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
  29. * Copyright (C) 2019, Linaro
  30. *
  31. * License: GNU GPL, version 2 or later.
  32. * See the COPYING file in the top-level directory.
  33. *
  34. * SPDX-License-Identifier: GPL-2.0-or-later
  35. *
  36. */
  37. #include "qemu/osdep.h"
  38. #include "qemu/main-loop.h"
  39. #include "qemu/plugin.h"
  40. #include "qemu/log.h"
  41. #include "tcg/tcg.h"
  42. #include "exec/gdbstub.h"
  43. #include "exec/target_page.h"
  44. #include "exec/translation-block.h"
  45. #include "exec/translator.h"
  46. #include "disas/disas.h"
  47. #include "plugin.h"
  48. /* Uninstall and Reset handlers */
  49. void qemu_plugin_uninstall(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
  50. {
  51. plugin_reset_uninstall(id, cb, false);
  52. }
  53. void qemu_plugin_reset(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
  54. {
  55. plugin_reset_uninstall(id, cb, true);
  56. }
  57. /*
  58. * Plugin Register Functions
  59. *
  60. * This allows the plugin to register callbacks for various events
  61. * during the translation.
  62. */
  63. void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id,
  64. qemu_plugin_vcpu_simple_cb_t cb)
  65. {
  66. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_INIT, cb);
  67. }
  68. void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id,
  69. qemu_plugin_vcpu_simple_cb_t cb)
  70. {
  71. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_EXIT, cb);
  72. }
  73. static bool tb_is_mem_only(void)
  74. {
  75. return tb_cflags(tcg_ctx->gen_tb) & CF_MEMI_ONLY;
  76. }
  77. void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb *tb,
  78. qemu_plugin_vcpu_udata_cb_t cb,
  79. enum qemu_plugin_cb_flags flags,
  80. void *udata)
  81. {
  82. if (!tb_is_mem_only()) {
  83. plugin_register_dyn_cb__udata(&tb->cbs, cb, flags, udata);
  84. }
  85. }
  86. void qemu_plugin_register_vcpu_tb_exec_cond_cb(struct qemu_plugin_tb *tb,
  87. qemu_plugin_vcpu_udata_cb_t cb,
  88. enum qemu_plugin_cb_flags flags,
  89. enum qemu_plugin_cond cond,
  90. qemu_plugin_u64 entry,
  91. uint64_t imm,
  92. void *udata)
  93. {
  94. if (cond == QEMU_PLUGIN_COND_NEVER || tb_is_mem_only()) {
  95. return;
  96. }
  97. if (cond == QEMU_PLUGIN_COND_ALWAYS) {
  98. qemu_plugin_register_vcpu_tb_exec_cb(tb, cb, flags, udata);
  99. return;
  100. }
  101. plugin_register_dyn_cond_cb__udata(&tb->cbs, cb, flags,
  102. cond, entry, imm, udata);
  103. }
  104. void qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
  105. struct qemu_plugin_tb *tb,
  106. enum qemu_plugin_op op,
  107. qemu_plugin_u64 entry,
  108. uint64_t imm)
  109. {
  110. if (!tb_is_mem_only()) {
  111. plugin_register_inline_op_on_entry(&tb->cbs, 0, op, entry, imm);
  112. }
  113. }
  114. void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn *insn,
  115. qemu_plugin_vcpu_udata_cb_t cb,
  116. enum qemu_plugin_cb_flags flags,
  117. void *udata)
  118. {
  119. if (!tb_is_mem_only()) {
  120. plugin_register_dyn_cb__udata(&insn->insn_cbs, cb, flags, udata);
  121. }
  122. }
  123. void qemu_plugin_register_vcpu_insn_exec_cond_cb(
  124. struct qemu_plugin_insn *insn,
  125. qemu_plugin_vcpu_udata_cb_t cb,
  126. enum qemu_plugin_cb_flags flags,
  127. enum qemu_plugin_cond cond,
  128. qemu_plugin_u64 entry,
  129. uint64_t imm,
  130. void *udata)
  131. {
  132. if (cond == QEMU_PLUGIN_COND_NEVER || tb_is_mem_only()) {
  133. return;
  134. }
  135. if (cond == QEMU_PLUGIN_COND_ALWAYS) {
  136. qemu_plugin_register_vcpu_insn_exec_cb(insn, cb, flags, udata);
  137. return;
  138. }
  139. plugin_register_dyn_cond_cb__udata(&insn->insn_cbs, cb, flags,
  140. cond, entry, imm, udata);
  141. }
  142. void qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(
  143. struct qemu_plugin_insn *insn,
  144. enum qemu_plugin_op op,
  145. qemu_plugin_u64 entry,
  146. uint64_t imm)
  147. {
  148. if (!tb_is_mem_only()) {
  149. plugin_register_inline_op_on_entry(&insn->insn_cbs, 0, op, entry, imm);
  150. }
  151. }
  152. /*
  153. * We always plant memory instrumentation because they don't finalise until
  154. * after the operation has complete.
  155. */
  156. void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn,
  157. qemu_plugin_vcpu_mem_cb_t cb,
  158. enum qemu_plugin_cb_flags flags,
  159. enum qemu_plugin_mem_rw rw,
  160. void *udata)
  161. {
  162. plugin_register_vcpu_mem_cb(&insn->mem_cbs, cb, flags, rw, udata);
  163. }
  164. void qemu_plugin_register_vcpu_mem_inline_per_vcpu(
  165. struct qemu_plugin_insn *insn,
  166. enum qemu_plugin_mem_rw rw,
  167. enum qemu_plugin_op op,
  168. qemu_plugin_u64 entry,
  169. uint64_t imm)
  170. {
  171. plugin_register_inline_op_on_entry(&insn->mem_cbs, rw, op, entry, imm);
  172. }
  173. void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id,
  174. qemu_plugin_vcpu_tb_trans_cb_t cb)
  175. {
  176. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_TB_TRANS, cb);
  177. }
  178. void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id,
  179. qemu_plugin_vcpu_syscall_cb_t cb)
  180. {
  181. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL, cb);
  182. }
  183. void
  184. qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id,
  185. qemu_plugin_vcpu_syscall_ret_cb_t cb)
  186. {
  187. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL_RET, cb);
  188. }
  189. /*
  190. * Plugin Queries
  191. *
  192. * These are queries that the plugin can make to gauge information
  193. * from our opaque data types. We do not want to leak internal details
  194. * here just information useful to the plugin.
  195. */
  196. /*
  197. * Translation block information:
  198. *
  199. * A plugin can query the virtual address of the start of the block
  200. * and the number of instructions in it. It can also get access to
  201. * each translated instruction.
  202. */
  203. size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb)
  204. {
  205. return tb->n;
  206. }
  207. uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb)
  208. {
  209. const DisasContextBase *db = tcg_ctx->plugin_db;
  210. return db->pc_first;
  211. }
  212. struct qemu_plugin_insn *
  213. qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx)
  214. {
  215. struct qemu_plugin_insn *insn;
  216. if (unlikely(idx >= tb->n)) {
  217. return NULL;
  218. }
  219. insn = g_ptr_array_index(tb->insns, idx);
  220. return insn;
  221. }
  222. /*
  223. * Instruction information
  224. *
  225. * These queries allow the plugin to retrieve information about each
  226. * instruction being translated.
  227. */
  228. size_t qemu_plugin_insn_data(const struct qemu_plugin_insn *insn,
  229. void *dest, size_t len)
  230. {
  231. const DisasContextBase *db = tcg_ctx->plugin_db;
  232. len = MIN(len, insn->len);
  233. return translator_st(db, dest, insn->vaddr, len) ? len : 0;
  234. }
  235. size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn)
  236. {
  237. return insn->len;
  238. }
  239. uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn)
  240. {
  241. return insn->vaddr;
  242. }
  243. void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn)
  244. {
  245. const DisasContextBase *db = tcg_ctx->plugin_db;
  246. vaddr page0_last = db->pc_first | ~qemu_target_page_mask();
  247. if (db->fake_insn) {
  248. return NULL;
  249. }
  250. /*
  251. * ??? The return value is not intended for use of host memory,
  252. * but as a proxy for address space and physical address.
  253. * Thus we are only interested in the first byte and do not
  254. * care about spanning pages.
  255. */
  256. if (insn->vaddr <= page0_last) {
  257. if (db->host_addr[0] == NULL) {
  258. return NULL;
  259. }
  260. return db->host_addr[0] + insn->vaddr - db->pc_first;
  261. } else {
  262. if (db->host_addr[1] == NULL) {
  263. return NULL;
  264. }
  265. return db->host_addr[1] + insn->vaddr - (page0_last + 1);
  266. }
  267. }
  268. char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn)
  269. {
  270. return plugin_disas(tcg_ctx->cpu, tcg_ctx->plugin_db,
  271. insn->vaddr, insn->len);
  272. }
  273. const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn *insn)
  274. {
  275. const char *sym = lookup_symbol(insn->vaddr);
  276. return sym[0] != 0 ? sym : NULL;
  277. }
  278. /*
  279. * The memory queries allow the plugin to query information about a
  280. * memory access.
  281. */
  282. unsigned qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info)
  283. {
  284. MemOp op = get_memop(info);
  285. return op & MO_SIZE;
  286. }
  287. bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info)
  288. {
  289. MemOp op = get_memop(info);
  290. return op & MO_SIGN;
  291. }
  292. bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info)
  293. {
  294. MemOp op = get_memop(info);
  295. return (op & MO_BSWAP) == MO_BE;
  296. }
  297. bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info)
  298. {
  299. return get_plugin_meminfo_rw(info) & QEMU_PLUGIN_MEM_W;
  300. }
  301. qemu_plugin_mem_value qemu_plugin_mem_get_value(qemu_plugin_meminfo_t info)
  302. {
  303. uint64_t low = current_cpu->neg.plugin_mem_value_low;
  304. qemu_plugin_mem_value value;
  305. switch (qemu_plugin_mem_size_shift(info)) {
  306. case 0:
  307. value.type = QEMU_PLUGIN_MEM_VALUE_U8;
  308. value.data.u8 = (uint8_t)low;
  309. break;
  310. case 1:
  311. value.type = QEMU_PLUGIN_MEM_VALUE_U16;
  312. value.data.u16 = (uint16_t)low;
  313. break;
  314. case 2:
  315. value.type = QEMU_PLUGIN_MEM_VALUE_U32;
  316. value.data.u32 = (uint32_t)low;
  317. break;
  318. case 3:
  319. value.type = QEMU_PLUGIN_MEM_VALUE_U64;
  320. value.data.u64 = low;
  321. break;
  322. case 4:
  323. value.type = QEMU_PLUGIN_MEM_VALUE_U128;
  324. value.data.u128.low = low;
  325. value.data.u128.high = current_cpu->neg.plugin_mem_value_high;
  326. break;
  327. default:
  328. g_assert_not_reached();
  329. }
  330. return value;
  331. }
  332. int qemu_plugin_num_vcpus(void)
  333. {
  334. return plugin_num_vcpus();
  335. }
  336. /*
  337. * Plugin output
  338. */
  339. void qemu_plugin_outs(const char *string)
  340. {
  341. qemu_log_mask(CPU_LOG_PLUGIN, "%s", string);
  342. }
  343. bool qemu_plugin_bool_parse(const char *name, const char *value, bool *ret)
  344. {
  345. return name && value && qapi_bool_parse(name, value, ret, NULL);
  346. }
  347. /*
  348. * Create register handles.
  349. *
  350. * We need to create a handle for each register so the plugin
  351. * infrastructure can call gdbstub to read a register. They are
  352. * currently just a pointer encapsulation of the gdb_reg but in
  353. * future may hold internal plugin state so its important plugin
  354. * authors are not tempted to treat them as numbers.
  355. *
  356. * We also construct a result array with those handles and some
  357. * ancillary data the plugin might find useful.
  358. */
  359. static GArray *create_register_handles(GArray *gdbstub_regs)
  360. {
  361. GArray *find_data = g_array_new(true, true,
  362. sizeof(qemu_plugin_reg_descriptor));
  363. for (int i = 0; i < gdbstub_regs->len; i++) {
  364. GDBRegDesc *grd = &g_array_index(gdbstub_regs, GDBRegDesc, i);
  365. qemu_plugin_reg_descriptor desc;
  366. /* skip "un-named" regs */
  367. if (!grd->name) {
  368. continue;
  369. }
  370. /* Create a record for the plugin */
  371. desc.handle = GINT_TO_POINTER(grd->gdb_reg + 1);
  372. desc.name = g_intern_string(grd->name);
  373. desc.feature = g_intern_string(grd->feature_name);
  374. g_array_append_val(find_data, desc);
  375. }
  376. return find_data;
  377. }
  378. GArray *qemu_plugin_get_registers(void)
  379. {
  380. g_assert(current_cpu);
  381. g_autoptr(GArray) regs = gdb_get_register_list(current_cpu);
  382. return create_register_handles(regs);
  383. }
  384. bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t len)
  385. {
  386. g_assert(current_cpu);
  387. if (len == 0) {
  388. return false;
  389. }
  390. g_byte_array_set_size(data, len);
  391. int result = cpu_memory_rw_debug(current_cpu, addr, data->data,
  392. data->len, false);
  393. if (result < 0) {
  394. return false;
  395. }
  396. return true;
  397. }
  398. int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf)
  399. {
  400. g_assert(current_cpu);
  401. return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg) - 1);
  402. }
  403. struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new(size_t element_size)
  404. {
  405. return plugin_scoreboard_new(element_size);
  406. }
  407. void qemu_plugin_scoreboard_free(struct qemu_plugin_scoreboard *score)
  408. {
  409. plugin_scoreboard_free(score);
  410. }
  411. void *qemu_plugin_scoreboard_find(struct qemu_plugin_scoreboard *score,
  412. unsigned int vcpu_index)
  413. {
  414. g_assert(vcpu_index < qemu_plugin_num_vcpus());
  415. /* we can't use g_array_index since entry size is not statically known */
  416. char *base_ptr = score->data->data;
  417. return base_ptr + vcpu_index * g_array_get_element_size(score->data);
  418. }
  419. static uint64_t *plugin_u64_address(qemu_plugin_u64 entry,
  420. unsigned int vcpu_index)
  421. {
  422. char *ptr = qemu_plugin_scoreboard_find(entry.score, vcpu_index);
  423. return (uint64_t *)(ptr + entry.offset);
  424. }
  425. void qemu_plugin_u64_add(qemu_plugin_u64 entry, unsigned int vcpu_index,
  426. uint64_t added)
  427. {
  428. *plugin_u64_address(entry, vcpu_index) += added;
  429. }
  430. uint64_t qemu_plugin_u64_get(qemu_plugin_u64 entry,
  431. unsigned int vcpu_index)
  432. {
  433. return *plugin_u64_address(entry, vcpu_index);
  434. }
  435. void qemu_plugin_u64_set(qemu_plugin_u64 entry, unsigned int vcpu_index,
  436. uint64_t val)
  437. {
  438. *plugin_u64_address(entry, vcpu_index) = val;
  439. }
  440. uint64_t qemu_plugin_u64_sum(qemu_plugin_u64 entry)
  441. {
  442. uint64_t total = 0;
  443. for (int i = 0, n = qemu_plugin_num_vcpus(); i < n; ++i) {
  444. total += qemu_plugin_u64_get(entry, i);
  445. }
  446. return total;
  447. }