api.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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/exec-all.h"
  43. #include "exec/gdbstub.h"
  44. #include "exec/translator.h"
  45. #include "disas/disas.h"
  46. #include "plugin.h"
  47. #ifndef CONFIG_USER_ONLY
  48. #include "exec/ram_addr.h"
  49. #include "qemu/plugin-memory.h"
  50. #include "hw/boards.h"
  51. #else
  52. #include "qemu.h"
  53. #ifdef CONFIG_LINUX
  54. #include "loader.h"
  55. #endif
  56. #endif
  57. /* Uninstall and Reset handlers */
  58. void qemu_plugin_uninstall(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
  59. {
  60. plugin_reset_uninstall(id, cb, false);
  61. }
  62. void qemu_plugin_reset(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
  63. {
  64. plugin_reset_uninstall(id, cb, true);
  65. }
  66. /*
  67. * Plugin Register Functions
  68. *
  69. * This allows the plugin to register callbacks for various events
  70. * during the translation.
  71. */
  72. void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id,
  73. qemu_plugin_vcpu_simple_cb_t cb)
  74. {
  75. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_INIT, cb);
  76. }
  77. void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id,
  78. qemu_plugin_vcpu_simple_cb_t cb)
  79. {
  80. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_EXIT, cb);
  81. }
  82. static bool tb_is_mem_only(void)
  83. {
  84. return tb_cflags(tcg_ctx->gen_tb) & CF_MEMI_ONLY;
  85. }
  86. void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb *tb,
  87. qemu_plugin_vcpu_udata_cb_t cb,
  88. enum qemu_plugin_cb_flags flags,
  89. void *udata)
  90. {
  91. if (!tb_is_mem_only()) {
  92. plugin_register_dyn_cb__udata(&tb->cbs, cb, flags, udata);
  93. }
  94. }
  95. void qemu_plugin_register_vcpu_tb_exec_cond_cb(struct qemu_plugin_tb *tb,
  96. qemu_plugin_vcpu_udata_cb_t cb,
  97. enum qemu_plugin_cb_flags flags,
  98. enum qemu_plugin_cond cond,
  99. qemu_plugin_u64 entry,
  100. uint64_t imm,
  101. void *udata)
  102. {
  103. if (cond == QEMU_PLUGIN_COND_NEVER || tb_is_mem_only()) {
  104. return;
  105. }
  106. if (cond == QEMU_PLUGIN_COND_ALWAYS) {
  107. qemu_plugin_register_vcpu_tb_exec_cb(tb, cb, flags, udata);
  108. return;
  109. }
  110. plugin_register_dyn_cond_cb__udata(&tb->cbs, cb, flags,
  111. cond, entry, imm, udata);
  112. }
  113. void qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
  114. struct qemu_plugin_tb *tb,
  115. enum qemu_plugin_op op,
  116. qemu_plugin_u64 entry,
  117. uint64_t imm)
  118. {
  119. if (!tb_is_mem_only()) {
  120. plugin_register_inline_op_on_entry(&tb->cbs, 0, op, entry, imm);
  121. }
  122. }
  123. void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn *insn,
  124. qemu_plugin_vcpu_udata_cb_t cb,
  125. enum qemu_plugin_cb_flags flags,
  126. void *udata)
  127. {
  128. if (!tb_is_mem_only()) {
  129. plugin_register_dyn_cb__udata(&insn->insn_cbs, cb, flags, udata);
  130. }
  131. }
  132. void qemu_plugin_register_vcpu_insn_exec_cond_cb(
  133. struct qemu_plugin_insn *insn,
  134. qemu_plugin_vcpu_udata_cb_t cb,
  135. enum qemu_plugin_cb_flags flags,
  136. enum qemu_plugin_cond cond,
  137. qemu_plugin_u64 entry,
  138. uint64_t imm,
  139. void *udata)
  140. {
  141. if (cond == QEMU_PLUGIN_COND_NEVER || tb_is_mem_only()) {
  142. return;
  143. }
  144. if (cond == QEMU_PLUGIN_COND_ALWAYS) {
  145. qemu_plugin_register_vcpu_insn_exec_cb(insn, cb, flags, udata);
  146. return;
  147. }
  148. plugin_register_dyn_cond_cb__udata(&insn->insn_cbs, cb, flags,
  149. cond, entry, imm, udata);
  150. }
  151. void qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(
  152. struct qemu_plugin_insn *insn,
  153. enum qemu_plugin_op op,
  154. qemu_plugin_u64 entry,
  155. uint64_t imm)
  156. {
  157. if (!tb_is_mem_only()) {
  158. plugin_register_inline_op_on_entry(&insn->insn_cbs, 0, op, entry, imm);
  159. }
  160. }
  161. /*
  162. * We always plant memory instrumentation because they don't finalise until
  163. * after the operation has complete.
  164. */
  165. void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn,
  166. qemu_plugin_vcpu_mem_cb_t cb,
  167. enum qemu_plugin_cb_flags flags,
  168. enum qemu_plugin_mem_rw rw,
  169. void *udata)
  170. {
  171. plugin_register_vcpu_mem_cb(&insn->mem_cbs, cb, flags, rw, udata);
  172. }
  173. void qemu_plugin_register_vcpu_mem_inline_per_vcpu(
  174. struct qemu_plugin_insn *insn,
  175. enum qemu_plugin_mem_rw rw,
  176. enum qemu_plugin_op op,
  177. qemu_plugin_u64 entry,
  178. uint64_t imm)
  179. {
  180. plugin_register_inline_op_on_entry(&insn->mem_cbs, rw, op, entry, imm);
  181. }
  182. void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id,
  183. qemu_plugin_vcpu_tb_trans_cb_t cb)
  184. {
  185. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_TB_TRANS, cb);
  186. }
  187. void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id,
  188. qemu_plugin_vcpu_syscall_cb_t cb)
  189. {
  190. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL, cb);
  191. }
  192. void
  193. qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id,
  194. qemu_plugin_vcpu_syscall_ret_cb_t cb)
  195. {
  196. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL_RET, cb);
  197. }
  198. /*
  199. * Plugin Queries
  200. *
  201. * These are queries that the plugin can make to gauge information
  202. * from our opaque data types. We do not want to leak internal details
  203. * here just information useful to the plugin.
  204. */
  205. /*
  206. * Translation block information:
  207. *
  208. * A plugin can query the virtual address of the start of the block
  209. * and the number of instructions in it. It can also get access to
  210. * each translated instruction.
  211. */
  212. size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb)
  213. {
  214. return tb->n;
  215. }
  216. uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb)
  217. {
  218. const DisasContextBase *db = tcg_ctx->plugin_db;
  219. return db->pc_first;
  220. }
  221. struct qemu_plugin_insn *
  222. qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx)
  223. {
  224. struct qemu_plugin_insn *insn;
  225. if (unlikely(idx >= tb->n)) {
  226. return NULL;
  227. }
  228. insn = g_ptr_array_index(tb->insns, idx);
  229. return insn;
  230. }
  231. /*
  232. * Instruction information
  233. *
  234. * These queries allow the plugin to retrieve information about each
  235. * instruction being translated.
  236. */
  237. size_t qemu_plugin_insn_data(const struct qemu_plugin_insn *insn,
  238. void *dest, size_t len)
  239. {
  240. const DisasContextBase *db = tcg_ctx->plugin_db;
  241. len = MIN(len, insn->len);
  242. return translator_st(db, dest, insn->vaddr, len) ? len : 0;
  243. }
  244. size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn)
  245. {
  246. return insn->len;
  247. }
  248. uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn)
  249. {
  250. return insn->vaddr;
  251. }
  252. void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn)
  253. {
  254. const DisasContextBase *db = tcg_ctx->plugin_db;
  255. vaddr page0_last = db->pc_first | ~TARGET_PAGE_MASK;
  256. if (db->fake_insn) {
  257. return NULL;
  258. }
  259. /*
  260. * ??? The return value is not intended for use of host memory,
  261. * but as a proxy for address space and physical address.
  262. * Thus we are only interested in the first byte and do not
  263. * care about spanning pages.
  264. */
  265. if (insn->vaddr <= page0_last) {
  266. if (db->host_addr[0] == NULL) {
  267. return NULL;
  268. }
  269. return db->host_addr[0] + insn->vaddr - db->pc_first;
  270. } else {
  271. if (db->host_addr[1] == NULL) {
  272. return NULL;
  273. }
  274. return db->host_addr[1] + insn->vaddr - (page0_last + 1);
  275. }
  276. }
  277. char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn)
  278. {
  279. return plugin_disas(tcg_ctx->cpu, tcg_ctx->plugin_db,
  280. insn->vaddr, insn->len);
  281. }
  282. const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn *insn)
  283. {
  284. const char *sym = lookup_symbol(insn->vaddr);
  285. return sym[0] != 0 ? sym : NULL;
  286. }
  287. /*
  288. * The memory queries allow the plugin to query information about a
  289. * memory access.
  290. */
  291. unsigned qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info)
  292. {
  293. MemOp op = get_memop(info);
  294. return op & MO_SIZE;
  295. }
  296. bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info)
  297. {
  298. MemOp op = get_memop(info);
  299. return op & MO_SIGN;
  300. }
  301. bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info)
  302. {
  303. MemOp op = get_memop(info);
  304. return (op & MO_BSWAP) == MO_BE;
  305. }
  306. bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info)
  307. {
  308. return get_plugin_meminfo_rw(info) & QEMU_PLUGIN_MEM_W;
  309. }
  310. /*
  311. * Virtual Memory queries
  312. */
  313. #ifdef CONFIG_SOFTMMU
  314. static __thread struct qemu_plugin_hwaddr hwaddr_info;
  315. #endif
  316. struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info,
  317. uint64_t vaddr)
  318. {
  319. #ifdef CONFIG_SOFTMMU
  320. CPUState *cpu = current_cpu;
  321. unsigned int mmu_idx = get_mmuidx(info);
  322. enum qemu_plugin_mem_rw rw = get_plugin_meminfo_rw(info);
  323. hwaddr_info.is_store = (rw & QEMU_PLUGIN_MEM_W) != 0;
  324. assert(mmu_idx < NB_MMU_MODES);
  325. if (!tlb_plugin_lookup(cpu, vaddr, mmu_idx,
  326. hwaddr_info.is_store, &hwaddr_info)) {
  327. error_report("invalid use of qemu_plugin_get_hwaddr");
  328. return NULL;
  329. }
  330. return &hwaddr_info;
  331. #else
  332. return NULL;
  333. #endif
  334. }
  335. bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr *haddr)
  336. {
  337. #ifdef CONFIG_SOFTMMU
  338. return haddr->is_io;
  339. #else
  340. return false;
  341. #endif
  342. }
  343. uint64_t qemu_plugin_hwaddr_phys_addr(const struct qemu_plugin_hwaddr *haddr)
  344. {
  345. #ifdef CONFIG_SOFTMMU
  346. if (haddr) {
  347. return haddr->phys_addr;
  348. }
  349. #endif
  350. return 0;
  351. }
  352. const char *qemu_plugin_hwaddr_device_name(const struct qemu_plugin_hwaddr *h)
  353. {
  354. #ifdef CONFIG_SOFTMMU
  355. if (h && h->is_io) {
  356. MemoryRegion *mr = h->mr;
  357. if (!mr->name) {
  358. unsigned maddr = (uintptr_t)mr;
  359. g_autofree char *temp = g_strdup_printf("anon%08x", maddr);
  360. return g_intern_string(temp);
  361. } else {
  362. return g_intern_string(mr->name);
  363. }
  364. } else {
  365. return g_intern_static_string("RAM");
  366. }
  367. #else
  368. return g_intern_static_string("Invalid");
  369. #endif
  370. }
  371. int qemu_plugin_num_vcpus(void)
  372. {
  373. return plugin_num_vcpus();
  374. }
  375. /*
  376. * Plugin output
  377. */
  378. void qemu_plugin_outs(const char *string)
  379. {
  380. qemu_log_mask(CPU_LOG_PLUGIN, "%s", string);
  381. }
  382. bool qemu_plugin_bool_parse(const char *name, const char *value, bool *ret)
  383. {
  384. return name && value && qapi_bool_parse(name, value, ret, NULL);
  385. }
  386. /*
  387. * Binary path, start and end locations
  388. */
  389. const char *qemu_plugin_path_to_binary(void)
  390. {
  391. char *path = NULL;
  392. #ifdef CONFIG_USER_ONLY
  393. TaskState *ts = get_task_state(current_cpu);
  394. path = g_strdup(ts->bprm->filename);
  395. #endif
  396. return path;
  397. }
  398. uint64_t qemu_plugin_start_code(void)
  399. {
  400. uint64_t start = 0;
  401. #ifdef CONFIG_USER_ONLY
  402. TaskState *ts = get_task_state(current_cpu);
  403. start = ts->info->start_code;
  404. #endif
  405. return start;
  406. }
  407. uint64_t qemu_plugin_end_code(void)
  408. {
  409. uint64_t end = 0;
  410. #ifdef CONFIG_USER_ONLY
  411. TaskState *ts = get_task_state(current_cpu);
  412. end = ts->info->end_code;
  413. #endif
  414. return end;
  415. }
  416. uint64_t qemu_plugin_entry_code(void)
  417. {
  418. uint64_t entry = 0;
  419. #ifdef CONFIG_USER_ONLY
  420. TaskState *ts = get_task_state(current_cpu);
  421. entry = ts->info->entry;
  422. #endif
  423. return entry;
  424. }
  425. /*
  426. * Create register handles.
  427. *
  428. * We need to create a handle for each register so the plugin
  429. * infrastructure can call gdbstub to read a register. They are
  430. * currently just a pointer encapsulation of the gdb_reg but in
  431. * future may hold internal plugin state so its important plugin
  432. * authors are not tempted to treat them as numbers.
  433. *
  434. * We also construct a result array with those handles and some
  435. * ancillary data the plugin might find useful.
  436. */
  437. static GArray *create_register_handles(GArray *gdbstub_regs)
  438. {
  439. GArray *find_data = g_array_new(true, true,
  440. sizeof(qemu_plugin_reg_descriptor));
  441. for (int i = 0; i < gdbstub_regs->len; i++) {
  442. GDBRegDesc *grd = &g_array_index(gdbstub_regs, GDBRegDesc, i);
  443. qemu_plugin_reg_descriptor desc;
  444. /* skip "un-named" regs */
  445. if (!grd->name) {
  446. continue;
  447. }
  448. /* Create a record for the plugin */
  449. desc.handle = GINT_TO_POINTER(grd->gdb_reg);
  450. desc.name = g_intern_string(grd->name);
  451. desc.feature = g_intern_string(grd->feature_name);
  452. g_array_append_val(find_data, desc);
  453. }
  454. return find_data;
  455. }
  456. GArray *qemu_plugin_get_registers(void)
  457. {
  458. g_assert(current_cpu);
  459. g_autoptr(GArray) regs = gdb_get_register_list(current_cpu);
  460. return create_register_handles(regs);
  461. }
  462. int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf)
  463. {
  464. g_assert(current_cpu);
  465. return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg));
  466. }
  467. struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new(size_t element_size)
  468. {
  469. return plugin_scoreboard_new(element_size);
  470. }
  471. void qemu_plugin_scoreboard_free(struct qemu_plugin_scoreboard *score)
  472. {
  473. plugin_scoreboard_free(score);
  474. }
  475. void *qemu_plugin_scoreboard_find(struct qemu_plugin_scoreboard *score,
  476. unsigned int vcpu_index)
  477. {
  478. g_assert(vcpu_index < qemu_plugin_num_vcpus());
  479. /* we can't use g_array_index since entry size is not statically known */
  480. char *base_ptr = score->data->data;
  481. return base_ptr + vcpu_index * g_array_get_element_size(score->data);
  482. }
  483. static uint64_t *plugin_u64_address(qemu_plugin_u64 entry,
  484. unsigned int vcpu_index)
  485. {
  486. char *ptr = qemu_plugin_scoreboard_find(entry.score, vcpu_index);
  487. return (uint64_t *)(ptr + entry.offset);
  488. }
  489. void qemu_plugin_u64_add(qemu_plugin_u64 entry, unsigned int vcpu_index,
  490. uint64_t added)
  491. {
  492. *plugin_u64_address(entry, vcpu_index) += added;
  493. }
  494. uint64_t qemu_plugin_u64_get(qemu_plugin_u64 entry,
  495. unsigned int vcpu_index)
  496. {
  497. return *plugin_u64_address(entry, vcpu_index);
  498. }
  499. void qemu_plugin_u64_set(qemu_plugin_u64 entry, unsigned int vcpu_index,
  500. uint64_t val)
  501. {
  502. *plugin_u64_address(entry, vcpu_index) = val;
  503. }
  504. uint64_t qemu_plugin_u64_sum(qemu_plugin_u64 entry)
  505. {
  506. uint64_t total = 0;
  507. for (int i = 0, n = qemu_plugin_num_vcpus(); i < n; ++i) {
  508. total += qemu_plugin_u64_get(entry, i);
  509. }
  510. return total;
  511. }