api.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. #include "trace/mem.h"
  49. /* Uninstall and Reset handlers */
  50. void qemu_plugin_uninstall(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
  51. {
  52. plugin_reset_uninstall(id, cb, false);
  53. }
  54. void qemu_plugin_reset(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
  55. {
  56. plugin_reset_uninstall(id, cb, true);
  57. }
  58. /*
  59. * Plugin Register Functions
  60. *
  61. * This allows the plugin to register callbacks for various events
  62. * during the translation.
  63. */
  64. void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id,
  65. qemu_plugin_vcpu_simple_cb_t cb)
  66. {
  67. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_INIT, cb);
  68. }
  69. void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id,
  70. qemu_plugin_vcpu_simple_cb_t cb)
  71. {
  72. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_EXIT, cb);
  73. }
  74. void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb *tb,
  75. qemu_plugin_vcpu_udata_cb_t cb,
  76. enum qemu_plugin_cb_flags flags,
  77. void *udata)
  78. {
  79. plugin_register_dyn_cb__udata(&tb->cbs[PLUGIN_CB_REGULAR],
  80. cb, flags, udata);
  81. }
  82. void qemu_plugin_register_vcpu_tb_exec_inline(struct qemu_plugin_tb *tb,
  83. enum qemu_plugin_op op,
  84. void *ptr, uint64_t imm)
  85. {
  86. plugin_register_inline_op(&tb->cbs[PLUGIN_CB_INLINE], 0, op, ptr, imm);
  87. }
  88. void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn *insn,
  89. qemu_plugin_vcpu_udata_cb_t cb,
  90. enum qemu_plugin_cb_flags flags,
  91. void *udata)
  92. {
  93. plugin_register_dyn_cb__udata(&insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR],
  94. cb, flags, udata);
  95. }
  96. void qemu_plugin_register_vcpu_insn_exec_inline(struct qemu_plugin_insn *insn,
  97. enum qemu_plugin_op op,
  98. void *ptr, uint64_t imm)
  99. {
  100. plugin_register_inline_op(&insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_INLINE],
  101. 0, op, ptr, imm);
  102. }
  103. void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn,
  104. qemu_plugin_vcpu_mem_cb_t cb,
  105. enum qemu_plugin_cb_flags flags,
  106. enum qemu_plugin_mem_rw rw,
  107. void *udata)
  108. {
  109. plugin_register_vcpu_mem_cb(&insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR],
  110. cb, flags, rw, udata);
  111. }
  112. void qemu_plugin_register_vcpu_mem_inline(struct qemu_plugin_insn *insn,
  113. enum qemu_plugin_mem_rw rw,
  114. enum qemu_plugin_op op, void *ptr,
  115. uint64_t imm)
  116. {
  117. plugin_register_inline_op(&insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE],
  118. rw, op, ptr, imm);
  119. }
  120. void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id,
  121. qemu_plugin_vcpu_tb_trans_cb_t cb)
  122. {
  123. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_TB_TRANS, cb);
  124. }
  125. void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id,
  126. qemu_plugin_vcpu_syscall_cb_t cb)
  127. {
  128. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL, cb);
  129. }
  130. void
  131. qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id,
  132. qemu_plugin_vcpu_syscall_ret_cb_t cb)
  133. {
  134. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL_RET, cb);
  135. }
  136. /*
  137. * Plugin Queries
  138. *
  139. * These are queries that the plugin can make to gauge information
  140. * from our opaque data types. We do not want to leak internal details
  141. * here just information useful to the plugin.
  142. */
  143. /*
  144. * Translation block information:
  145. *
  146. * A plugin can query the virtual address of the start of the block
  147. * and the number of instructions in it. It can also get access to
  148. * each translated instruction.
  149. */
  150. size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb)
  151. {
  152. return tb->n;
  153. }
  154. uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb)
  155. {
  156. return tb->vaddr;
  157. }
  158. struct qemu_plugin_insn *
  159. qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx)
  160. {
  161. if (unlikely(idx >= tb->n)) {
  162. return NULL;
  163. }
  164. return g_ptr_array_index(tb->insns, idx);
  165. }
  166. /*
  167. * Instruction information
  168. *
  169. * These queries allow the plugin to retrieve information about each
  170. * instruction being translated.
  171. */
  172. const void *qemu_plugin_insn_data(const struct qemu_plugin_insn *insn)
  173. {
  174. return insn->data->data;
  175. }
  176. size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn)
  177. {
  178. return insn->data->len;
  179. }
  180. uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn)
  181. {
  182. return insn->vaddr;
  183. }
  184. void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn)
  185. {
  186. return insn->haddr;
  187. }
  188. char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn)
  189. {
  190. CPUState *cpu = current_cpu;
  191. return plugin_disas(cpu, insn->vaddr, insn->data->len);
  192. }
  193. /*
  194. * The memory queries allow the plugin to query information about a
  195. * memory access.
  196. */
  197. unsigned qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info)
  198. {
  199. return info & TRACE_MEM_SZ_SHIFT_MASK;
  200. }
  201. bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info)
  202. {
  203. return !!(info & TRACE_MEM_SE);
  204. }
  205. bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info)
  206. {
  207. return !!(info & TRACE_MEM_BE);
  208. }
  209. bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info)
  210. {
  211. return !!(info & TRACE_MEM_ST);
  212. }
  213. /*
  214. * Virtual Memory queries
  215. */
  216. #ifdef CONFIG_SOFTMMU
  217. static __thread struct qemu_plugin_hwaddr hwaddr_info;
  218. struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info,
  219. uint64_t vaddr)
  220. {
  221. CPUState *cpu = current_cpu;
  222. unsigned int mmu_idx = info >> TRACE_MEM_MMU_SHIFT;
  223. hwaddr_info.is_store = info & TRACE_MEM_ST;
  224. if (!tlb_plugin_lookup(cpu, vaddr, mmu_idx,
  225. info & TRACE_MEM_ST, &hwaddr_info)) {
  226. error_report("invalid use of qemu_plugin_get_hwaddr");
  227. return NULL;
  228. }
  229. return &hwaddr_info;
  230. }
  231. #else
  232. struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info,
  233. uint64_t vaddr)
  234. {
  235. return NULL;
  236. }
  237. #endif
  238. bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr *haddr)
  239. {
  240. #ifdef CONFIG_SOFTMMU
  241. return haddr->is_io;
  242. #else
  243. return false;
  244. #endif
  245. }
  246. uint64_t qemu_plugin_hwaddr_device_offset(const struct qemu_plugin_hwaddr *haddr)
  247. {
  248. #ifdef CONFIG_SOFTMMU
  249. if (haddr) {
  250. if (!haddr->is_io) {
  251. ram_addr_t ram_addr = qemu_ram_addr_from_host((void *) haddr->v.ram.hostaddr);
  252. if (ram_addr == RAM_ADDR_INVALID) {
  253. error_report("Bad ram pointer %"PRIx64"", haddr->v.ram.hostaddr);
  254. abort();
  255. }
  256. return ram_addr;
  257. } else {
  258. return haddr->v.io.offset;
  259. }
  260. }
  261. #endif
  262. return 0;
  263. }
  264. /*
  265. * Queries to the number and potential maximum number of vCPUs there
  266. * will be. This helps the plugin dimension per-vcpu arrays.
  267. */
  268. #ifndef CONFIG_USER_ONLY
  269. static MachineState * get_ms(void)
  270. {
  271. return MACHINE(qdev_get_machine());
  272. }
  273. #endif
  274. int qemu_plugin_n_vcpus(void)
  275. {
  276. #ifdef CONFIG_USER_ONLY
  277. return -1;
  278. #else
  279. return get_ms()->smp.cpus;
  280. #endif
  281. }
  282. int qemu_plugin_n_max_vcpus(void)
  283. {
  284. #ifdef CONFIG_USER_ONLY
  285. return -1;
  286. #else
  287. return get_ms()->smp.max_cpus;
  288. #endif
  289. }
  290. /*
  291. * Plugin output
  292. */
  293. void qemu_plugin_outs(const char *string)
  294. {
  295. qemu_log_mask(CPU_LOG_PLUGIN, "%s", string);
  296. }