api.c 17 KB

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