2
0

api.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 "qemu/log.h"
  39. #include "tcg/tcg.h"
  40. #include "exec/exec-all.h"
  41. #include "exec/ram_addr.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. #else
  48. #include "qemu.h"
  49. #ifdef CONFIG_LINUX
  50. #include "loader.h"
  51. #endif
  52. #endif
  53. /* Uninstall and Reset handlers */
  54. void qemu_plugin_uninstall(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
  55. {
  56. plugin_reset_uninstall(id, cb, false);
  57. }
  58. void qemu_plugin_reset(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
  59. {
  60. plugin_reset_uninstall(id, cb, true);
  61. }
  62. /*
  63. * Plugin Register Functions
  64. *
  65. * This allows the plugin to register callbacks for various events
  66. * during the translation.
  67. */
  68. void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id,
  69. qemu_plugin_vcpu_simple_cb_t cb)
  70. {
  71. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_INIT, cb);
  72. }
  73. void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id,
  74. qemu_plugin_vcpu_simple_cb_t cb)
  75. {
  76. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_EXIT, cb);
  77. }
  78. void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb *tb,
  79. qemu_plugin_vcpu_udata_cb_t cb,
  80. enum qemu_plugin_cb_flags flags,
  81. void *udata)
  82. {
  83. if (!tb->mem_only) {
  84. plugin_register_dyn_cb__udata(&tb->cbs[PLUGIN_CB_REGULAR],
  85. cb, flags, udata);
  86. }
  87. }
  88. void qemu_plugin_register_vcpu_tb_exec_inline(struct qemu_plugin_tb *tb,
  89. enum qemu_plugin_op op,
  90. void *ptr, uint64_t imm)
  91. {
  92. if (!tb->mem_only) {
  93. plugin_register_inline_op(&tb->cbs[PLUGIN_CB_INLINE], 0, op, ptr, imm);
  94. }
  95. }
  96. void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn *insn,
  97. qemu_plugin_vcpu_udata_cb_t cb,
  98. enum qemu_plugin_cb_flags flags,
  99. void *udata)
  100. {
  101. if (!insn->mem_only) {
  102. plugin_register_dyn_cb__udata(&insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR],
  103. cb, flags, udata);
  104. }
  105. }
  106. void qemu_plugin_register_vcpu_insn_exec_inline(struct qemu_plugin_insn *insn,
  107. enum qemu_plugin_op op,
  108. void *ptr, uint64_t imm)
  109. {
  110. if (!insn->mem_only) {
  111. plugin_register_inline_op(&insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_INLINE],
  112. 0, op, ptr, imm);
  113. }
  114. }
  115. /*
  116. * We always plant memory instrumentation because they don't finalise until
  117. * after the operation has complete.
  118. */
  119. void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn,
  120. qemu_plugin_vcpu_mem_cb_t cb,
  121. enum qemu_plugin_cb_flags flags,
  122. enum qemu_plugin_mem_rw rw,
  123. void *udata)
  124. {
  125. plugin_register_vcpu_mem_cb(&insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR],
  126. cb, flags, rw, udata);
  127. }
  128. void qemu_plugin_register_vcpu_mem_inline(struct qemu_plugin_insn *insn,
  129. enum qemu_plugin_mem_rw rw,
  130. enum qemu_plugin_op op, void *ptr,
  131. uint64_t imm)
  132. {
  133. plugin_register_inline_op(&insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE],
  134. rw, op, ptr, imm);
  135. }
  136. void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id,
  137. qemu_plugin_vcpu_tb_trans_cb_t cb)
  138. {
  139. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_TB_TRANS, cb);
  140. }
  141. void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id,
  142. qemu_plugin_vcpu_syscall_cb_t cb)
  143. {
  144. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL, cb);
  145. }
  146. void
  147. qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id,
  148. qemu_plugin_vcpu_syscall_ret_cb_t cb)
  149. {
  150. plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL_RET, cb);
  151. }
  152. /*
  153. * Plugin Queries
  154. *
  155. * These are queries that the plugin can make to gauge information
  156. * from our opaque data types. We do not want to leak internal details
  157. * here just information useful to the plugin.
  158. */
  159. /*
  160. * Translation block information:
  161. *
  162. * A plugin can query the virtual address of the start of the block
  163. * and the number of instructions in it. It can also get access to
  164. * each translated instruction.
  165. */
  166. size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb)
  167. {
  168. return tb->n;
  169. }
  170. uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb)
  171. {
  172. return tb->vaddr;
  173. }
  174. struct qemu_plugin_insn *
  175. qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx)
  176. {
  177. struct qemu_plugin_insn *insn;
  178. if (unlikely(idx >= tb->n)) {
  179. return NULL;
  180. }
  181. insn = g_ptr_array_index(tb->insns, idx);
  182. insn->mem_only = tb->mem_only;
  183. return insn;
  184. }
  185. /*
  186. * Instruction information
  187. *
  188. * These queries allow the plugin to retrieve information about each
  189. * instruction being translated.
  190. */
  191. const void *qemu_plugin_insn_data(const struct qemu_plugin_insn *insn)
  192. {
  193. return insn->data->data;
  194. }
  195. size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn)
  196. {
  197. return insn->data->len;
  198. }
  199. uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn)
  200. {
  201. return insn->vaddr;
  202. }
  203. void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn)
  204. {
  205. return insn->haddr;
  206. }
  207. char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn)
  208. {
  209. CPUState *cpu = current_cpu;
  210. return plugin_disas(cpu, insn->vaddr, insn->data->len);
  211. }
  212. const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn *insn)
  213. {
  214. const char *sym = lookup_symbol(insn->vaddr);
  215. return sym[0] != 0 ? sym : NULL;
  216. }
  217. /*
  218. * The memory queries allow the plugin to query information about a
  219. * memory access.
  220. */
  221. unsigned qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info)
  222. {
  223. MemOp op = get_memop(info);
  224. return op & MO_SIZE;
  225. }
  226. bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info)
  227. {
  228. MemOp op = get_memop(info);
  229. return op & MO_SIGN;
  230. }
  231. bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info)
  232. {
  233. MemOp op = get_memop(info);
  234. return (op & MO_BSWAP) == MO_BE;
  235. }
  236. bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info)
  237. {
  238. return get_plugin_meminfo_rw(info) & QEMU_PLUGIN_MEM_W;
  239. }
  240. /*
  241. * Virtual Memory queries
  242. */
  243. #ifdef CONFIG_SOFTMMU
  244. static __thread struct qemu_plugin_hwaddr hwaddr_info;
  245. #endif
  246. struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info,
  247. uint64_t vaddr)
  248. {
  249. #ifdef CONFIG_SOFTMMU
  250. CPUState *cpu = current_cpu;
  251. unsigned int mmu_idx = get_mmuidx(info);
  252. enum qemu_plugin_mem_rw rw = get_plugin_meminfo_rw(info);
  253. hwaddr_info.is_store = (rw & QEMU_PLUGIN_MEM_W) != 0;
  254. assert(mmu_idx < NB_MMU_MODES);
  255. if (!tlb_plugin_lookup(cpu, vaddr, mmu_idx,
  256. hwaddr_info.is_store, &hwaddr_info)) {
  257. error_report("invalid use of qemu_plugin_get_hwaddr");
  258. return NULL;
  259. }
  260. return &hwaddr_info;
  261. #else
  262. return NULL;
  263. #endif
  264. }
  265. bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr *haddr)
  266. {
  267. #ifdef CONFIG_SOFTMMU
  268. return haddr->is_io;
  269. #else
  270. return false;
  271. #endif
  272. }
  273. uint64_t qemu_plugin_hwaddr_phys_addr(const struct qemu_plugin_hwaddr *haddr)
  274. {
  275. #ifdef CONFIG_SOFTMMU
  276. if (haddr) {
  277. if (!haddr->is_io) {
  278. RAMBlock *block;
  279. ram_addr_t offset;
  280. void *hostaddr = haddr->v.ram.hostaddr;
  281. block = qemu_ram_block_from_host(hostaddr, false, &offset);
  282. if (!block) {
  283. error_report("Bad host ram pointer %p", haddr->v.ram.hostaddr);
  284. abort();
  285. }
  286. return block->offset + offset + block->mr->addr;
  287. } else {
  288. MemoryRegionSection *mrs = haddr->v.io.section;
  289. return mrs->offset_within_address_space + haddr->v.io.offset;
  290. }
  291. }
  292. #endif
  293. return 0;
  294. }
  295. const char *qemu_plugin_hwaddr_device_name(const struct qemu_plugin_hwaddr *h)
  296. {
  297. #ifdef CONFIG_SOFTMMU
  298. if (h && h->is_io) {
  299. MemoryRegionSection *mrs = h->v.io.section;
  300. if (!mrs->mr->name) {
  301. unsigned long maddr = 0xffffffff & (uintptr_t) mrs->mr;
  302. g_autofree char *temp = g_strdup_printf("anon%08lx", maddr);
  303. return g_intern_string(temp);
  304. } else {
  305. return g_intern_string(mrs->mr->name);
  306. }
  307. } else {
  308. return g_intern_static_string("RAM");
  309. }
  310. #else
  311. return g_intern_static_string("Invalid");
  312. #endif
  313. }
  314. /*
  315. * Queries to the number and potential maximum number of vCPUs there
  316. * will be. This helps the plugin dimension per-vcpu arrays.
  317. */
  318. #ifndef CONFIG_USER_ONLY
  319. static MachineState * get_ms(void)
  320. {
  321. return MACHINE(qdev_get_machine());
  322. }
  323. #endif
  324. int qemu_plugin_n_vcpus(void)
  325. {
  326. #ifdef CONFIG_USER_ONLY
  327. return -1;
  328. #else
  329. return get_ms()->smp.cpus;
  330. #endif
  331. }
  332. int qemu_plugin_n_max_vcpus(void)
  333. {
  334. #ifdef CONFIG_USER_ONLY
  335. return -1;
  336. #else
  337. return get_ms()->smp.max_cpus;
  338. #endif
  339. }
  340. /*
  341. * Plugin output
  342. */
  343. void qemu_plugin_outs(const char *string)
  344. {
  345. qemu_log_mask(CPU_LOG_PLUGIN, "%s", string);
  346. }
  347. bool qemu_plugin_bool_parse(const char *name, const char *value, bool *ret)
  348. {
  349. return name && value && qapi_bool_parse(name, value, ret, NULL);
  350. }
  351. /*
  352. * Binary path, start and end locations
  353. */
  354. const char *qemu_plugin_path_to_binary(void)
  355. {
  356. char *path = NULL;
  357. #ifdef CONFIG_USER_ONLY
  358. TaskState *ts = (TaskState *) current_cpu->opaque;
  359. path = g_strdup(ts->bprm->filename);
  360. #endif
  361. return path;
  362. }
  363. uint64_t qemu_plugin_start_code(void)
  364. {
  365. uint64_t start = 0;
  366. #ifdef CONFIG_USER_ONLY
  367. TaskState *ts = (TaskState *) current_cpu->opaque;
  368. start = ts->info->start_code;
  369. #endif
  370. return start;
  371. }
  372. uint64_t qemu_plugin_end_code(void)
  373. {
  374. uint64_t end = 0;
  375. #ifdef CONFIG_USER_ONLY
  376. TaskState *ts = (TaskState *) current_cpu->opaque;
  377. end = ts->info->end_code;
  378. #endif
  379. return end;
  380. }
  381. uint64_t qemu_plugin_entry_code(void)
  382. {
  383. uint64_t entry = 0;
  384. #ifdef CONFIG_USER_ONLY
  385. TaskState *ts = (TaskState *) current_cpu->opaque;
  386. entry = ts->info->entry;
  387. #endif
  388. return entry;
  389. }