2
0

api.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. *
  12. * Which can then be passed back into the API to do additional things.
  13. * As such all the public functions in here are exported in
  14. * qemu-plugin.h.
  15. *
  16. * The general life-cycle of a plugin is:
  17. *
  18. * - plugin is loaded, public qemu_plugin_install called
  19. * - the install func registers callbacks for events
  20. * - usually an atexit_cb is registered to dump info at the end
  21. * - when a registered event occurs the plugin is called
  22. * - some events pass additional info
  23. * - during translation the plugin can decide to instrument any
  24. * instruction
  25. * - when QEMU exits all the registered atexit callbacks are called
  26. *
  27. * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
  28. * Copyright (C) 2019, Linaro
  29. *
  30. * License: GNU GPL, version 2 or later.
  31. * See the COPYING file in the top-level directory.
  32. *
  33. * SPDX-License-Identifier: GPL-2.0-or-later
  34. *
  35. */
  36. #include "qemu/osdep.h"
  37. #include "qemu/plugin.h"
  38. #include "cpu.h"
  39. #include "sysemu/sysemu.h"
  40. #include "tcg/tcg.h"
  41. #include "exec/exec-all.h"
  42. #include "disas/disas.h"
  43. #include "plugin.h"
  44. #ifndef CONFIG_USER_ONLY
  45. #include "qemu/plugin-memory.h"
  46. #include "hw/boards.h"
  47. #endif
  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. void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb *tb,
  74. qemu_plugin_vcpu_udata_cb_t cb,
  75. enum qemu_plugin_cb_flags flags,
  76. void *udata)
  77. {
  78. plugin_register_dyn_cb__udata(&tb->cbs[PLUGIN_CB_REGULAR],
  79. cb, flags, udata);
  80. }
  81. void qemu_plugin_register_vcpu_tb_exec_inline(struct qemu_plugin_tb *tb,
  82. enum qemu_plugin_op op,
  83. void *ptr, uint64_t imm)
  84. {
  85. plugin_register_inline_op(&tb->cbs[PLUGIN_CB_INLINE], 0, op, ptr, imm);
  86. }
  87. void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn *insn,
  88. qemu_plugin_vcpu_udata_cb_t cb,
  89. enum qemu_plugin_cb_flags flags,
  90. void *udata)
  91. {
  92. plugin_register_dyn_cb__udata(&insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR],
  93. cb, flags, udata);
  94. }
  95. void qemu_plugin_register_vcpu_insn_exec_inline(struct qemu_plugin_insn *insn,
  96. enum qemu_plugin_op op,
  97. void *ptr, uint64_t imm)
  98. {
  99. plugin_register_inline_op(&insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_INLINE],
  100. 0, op, ptr, imm);
  101. }
  102. void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn,
  103. qemu_plugin_vcpu_mem_cb_t cb,
  104. enum qemu_plugin_cb_flags flags,
  105. enum qemu_plugin_mem_rw rw,
  106. void *udata)
  107. {
  108. plugin_register_vcpu_mem_cb(&insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR],
  109. cb, flags, rw, udata);
  110. }
  111. void qemu_plugin_register_vcpu_mem_inline(struct qemu_plugin_insn *insn,
  112. enum qemu_plugin_mem_rw rw,
  113. enum qemu_plugin_op op, void *ptr,
  114. uint64_t imm)
  115. {
  116. plugin_register_inline_op(&insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE],
  117. rw, op, ptr, imm);
  118. }
  119. void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id,
  120. qemu_plugin_vcpu_tb_trans_cb_t cb)
  121. {
  122. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_TB_TRANS, cb);
  123. }
  124. void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id,
  125. qemu_plugin_vcpu_syscall_cb_t cb)
  126. {
  127. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL, cb);
  128. }
  129. void
  130. qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id,
  131. qemu_plugin_vcpu_syscall_ret_cb_t cb)
  132. {
  133. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL_RET, cb);
  134. }
  135. /*
  136. * Plugin Queries
  137. *
  138. * These are queries that the plugin can make to gauge information
  139. * from our opaque data types. We do not want to leak internal details
  140. * here just information useful to the plugin.
  141. */
  142. /*
  143. * Translation block information:
  144. *
  145. * A plugin can query the virtual address of the start of the block
  146. * and the number of instructions in it. It can also get access to
  147. * each translated instruction.
  148. */
  149. size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb)
  150. {
  151. return tb->n;
  152. }
  153. uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb)
  154. {
  155. return tb->vaddr;
  156. }
  157. struct qemu_plugin_insn *
  158. qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx)
  159. {
  160. if (unlikely(idx >= tb->n)) {
  161. return NULL;
  162. }
  163. return g_ptr_array_index(tb->insns, idx);
  164. }
  165. /*
  166. * Instruction information
  167. *
  168. * These queries allow the plugin to retrieve information about each
  169. * instruction being translated.
  170. */
  171. const void *qemu_plugin_insn_data(const struct qemu_plugin_insn *insn)
  172. {
  173. return insn->data->data;
  174. }
  175. size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn)
  176. {
  177. return insn->data->len;
  178. }
  179. uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn)
  180. {
  181. return insn->vaddr;
  182. }
  183. void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn)
  184. {
  185. return insn->haddr;
  186. }
  187. char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn)
  188. {
  189. CPUState *cpu = current_cpu;
  190. return plugin_disas(cpu, insn->vaddr, insn->data->len);
  191. }
  192. /*
  193. * The memory queries allow the plugin to query information about a
  194. * memory access.
  195. */
  196. unsigned qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info)
  197. {
  198. return info & TRACE_MEM_SZ_SHIFT_MASK;
  199. }
  200. bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info)
  201. {
  202. return !!(info & TRACE_MEM_SE);
  203. }
  204. bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info)
  205. {
  206. return !!(info & TRACE_MEM_BE);
  207. }
  208. bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info)
  209. {
  210. return !!(info & TRACE_MEM_ST);
  211. }
  212. /*
  213. * Virtual Memory queries
  214. */
  215. #ifdef CONFIG_SOFTMMU
  216. static __thread struct qemu_plugin_hwaddr hwaddr_info;
  217. struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info,
  218. uint64_t vaddr)
  219. {
  220. CPUState *cpu = current_cpu;
  221. unsigned int mmu_idx = info >> TRACE_MEM_MMU_SHIFT;
  222. hwaddr_info.is_store = info & TRACE_MEM_ST;
  223. if (!tlb_plugin_lookup(cpu, vaddr, mmu_idx,
  224. info & TRACE_MEM_ST, &hwaddr_info)) {
  225. error_report("invalid use of qemu_plugin_get_hwaddr");
  226. return NULL;
  227. }
  228. return &hwaddr_info;
  229. }
  230. #else
  231. struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info,
  232. uint64_t vaddr)
  233. {
  234. return NULL;
  235. }
  236. #endif
  237. bool qemu_plugin_hwaddr_is_io(struct qemu_plugin_hwaddr *hwaddr)
  238. {
  239. #ifdef CONFIG_SOFTMMU
  240. return hwaddr->is_io;
  241. #else
  242. return false;
  243. #endif
  244. }
  245. uint64_t qemu_plugin_hwaddr_device_offset(const struct qemu_plugin_hwaddr *haddr)
  246. {
  247. #ifdef CONFIG_SOFTMMU
  248. if (haddr) {
  249. if (!haddr->is_io) {
  250. ram_addr_t ram_addr = qemu_ram_addr_from_host((void *) haddr->v.ram.hostaddr);
  251. if (ram_addr == RAM_ADDR_INVALID) {
  252. error_report("Bad ram pointer %"PRIx64"", haddr->v.ram.hostaddr);
  253. abort();
  254. }
  255. return ram_addr;
  256. } else {
  257. return haddr->v.io.offset;
  258. }
  259. }
  260. #endif
  261. return 0;
  262. }
  263. /*
  264. * Queries to the number and potential maximum number of vCPUs there
  265. * will be. This helps the plugin dimension per-vcpu arrays.
  266. */
  267. #ifndef CONFIG_USER_ONLY
  268. static MachineState * get_ms(void)
  269. {
  270. return MACHINE(qdev_get_machine());
  271. }
  272. #endif
  273. int qemu_plugin_n_vcpus(void)
  274. {
  275. #ifdef CONFIG_USER_ONLY
  276. return -1;
  277. #else
  278. return get_ms()->smp.cpus;
  279. #endif
  280. }
  281. int qemu_plugin_n_max_vcpus(void)
  282. {
  283. #ifdef CONFIG_USER_ONLY
  284. return -1;
  285. #else
  286. return get_ms()->smp.max_cpus;
  287. #endif
  288. }
  289. /*
  290. * Plugin output
  291. */
  292. void qemu_plugin_outs(const char *string)
  293. {
  294. qemu_log_mask(CPU_LOG_PLUGIN, "%s", string);
  295. }