Skip to contentSkip to conversation

plugins: add plugin API to read guest memory

PATCH Applied Rowan Hart 2 participants 4 messages Aug 27, 2024 – Sep 5, 2024

lore
Share & tools
lore

Rowan Hart

author
#0 lore raw

v3 cover plugins: add plugin API to read guest memory

This patch adds one API function to the QEMU plugin API

bool qemu_plugin_read_memory_vaddr(vaddr, GByteArray *, size_t);

The API allows reading memory from an arbitrary guest virtual address, which is useful for many things but the motivating examples are:

* Virtual Machine Introspection (VMI)
* Accurate and easier execution trace extraction
* Debugging and logging tools

An example of its use is added to the existing syscalls plugin, which now has an option to hexdump the buf argument to any write(2) syscalls which occur.

For v3, fixed a missing '*' in a comment which caused a doc build issue.

Rowan Hart (2):
plugins: add plugin API to read guest memory
plugins: add option to dump write argument to syscall plugin

 docs/about/emulation.rst     |  14 ++++-
 include/qemu/qemu-plugin.h   |  32 +++++++++-
 plugins/api.c                |  20 ++++++
 plugins/qemu-plugins.symbols |   1 +
 tests/tcg/plugins/syscall.c  | 117 +++++++++++++++++++++++++++++++++++
 5 files changed, 182 insertions(+), 2 deletions(-)
Signature · 1 line
2.46.0
author
#1 lore raw

PATCH v3 1/2 plugins: add plugin API to read guest memory

---
 include/qemu/qemu-plugin.h   | 32 +++++++++++++++++++++++++++++++-
 plugins/api.c                | 20 ++++++++++++++++++++
 plugins/qemu-plugins.symbols |  1 +
 3 files changed, 52 insertions(+), 1 deletion(-)
include/qemu/qemu-plugin.h +31 -1
index c71c705b69..e4068f823b 100644
@@ -57,11 +57,19 @@ typedef uint64_t qemu_plugin_id_t;
  * - Remove qemu_plugin_register_vcpu_{tb, insn, mem}_exec_inline.
  *   Those functions are replaced by *_per_vcpu variants, which guarantee
  *   thread-safety for operations.
+ *
+ * version 3:
+ * - modified arguments and return value of qemu_plugin_insn_data to copy
+ *   the data into a user-provided buffer instead of returning a pointer
+ *   to the data.
+ *
+ * version 4:
+ * - added qemu_plugin_read_memory_vaddr
  */
 
 extern QEMU_PLUGIN_EXPORT int qemu_plugin_version;
 
-#define QEMU_PLUGIN_VERSION 3
+#define QEMU_PLUGIN_VERSION 4
 
 /**
  * struct qemu_info_t - system information for plugins
@@ -852,6 +860,28 @@ typedef struct {
 QEMU_PLUGIN_API
 GArray *qemu_plugin_get_registers(void);
 
+/**
+ * qemu_plugin_read_memory_vaddr() - read from memory using a virtual address
+ *
+ * @addr: A virtual address to read from
+ * @data: A byte array to store data into
+ * @len: The number of bytes to read, starting from @addr
+ *
+ * @len bytes of data is read starting at @addr and stored into @data. If @data
+ * is not large enough to hold @len bytes, it will be expanded to the necessary
+ * size, reallocating if necessary. @len must be greater than 0.
+ *
+ * This function does not ensure writes are flushed prior to reading, so
+ * callers should take care when calling this function in plugin callbacks to
+ * avoid attempting to read data which may not yet be written and should use
+ * the memory callback API instead.
+ *
+ * Returns true on success and false on failure.
+ */
+QEMU_PLUGIN_API
+bool qemu_plugin_read_memory_vaddr(uint64_t addr,
+                                          GByteArray *data, size_t len);
+
 /**
  * qemu_plugin_read_register() - read register for current vCPU
  *
plugins/api.c +20 -0
index 2ff13d09de..cb7d818918 100644
@@ -527,6 +527,26 @@ GArray *qemu_plugin_get_registers(void)
     return create_register_handles(regs);
 }
 
+bool qemu_plugin_read_memory_vaddr(vaddr addr, GByteArray *data, size_t len)
+{
+    g_assert(current_cpu);
+
+    if (len == 0) {
+        return false;
+    }
+
+    g_byte_array_set_size(data, len);
+
+    int result = cpu_memory_rw_debug(current_cpu, addr, data->data,
+                                data->len, 0);
+
+    if (result < 0) {
+        return false;
+    }
+
+    return true;
+}
+
 int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf)
 {
     g_assert(current_cpu);
plugins/qemu-plugins.symbols +1 -0
index ca773d8d9f..3ad479a924 100644
@@ -20,6 +20,7 @@
   qemu_plugin_num_vcpus;
   qemu_plugin_outs;
   qemu_plugin_path_to_binary;
+  qemu_plugin_read_memory_vaddr;
   qemu_plugin_read_register;
   qemu_plugin_register_atexit_cb;
   qemu_plugin_register_flush_cb;
Signature · 1 line
2.46.0
author
#2 lore raw

PATCH v3 2/2 plugins: add option to dump write argument to syscall plugin

In reply to Rowan Hart
---
 docs/about/emulation.rst    |  14 ++++-
 tests/tcg/plugins/syscall.c | 117 ++++++++++++++++++++++++++++++++++++
 2 files changed, 130 insertions(+), 1 deletion(-)
docs/about/emulation.rst +13 -1
index eea1261baa..e85d494ff0 100644
@@ -388,6 +388,19 @@ run::
   160          1      0
   135          1      0
 
+Behaviour can be tweaked with the following arguments:
+
+.. list-table:: Syscall plugin arguments
+  :widths: 20 80
+  :header-rows: 1
+
+  * - Option
+    - Description
+  * - print=true|false
+    - Print the number of times each syscall is called
+  * - log_writes=true|false
+    - Log the buffer of each write syscall in hexdump format
+
 Test inline operations
 ......................
 
@@ -777,4 +790,3 @@ Other emulation features
 When running system emulation you can also enable deterministic
 execution which allows for repeatable record/replay debugging. See
 :ref:`Record/Replay<replay>` for more details.
-
tests/tcg/plugins/syscall.c +117 -0

149 lines not on this page · raw message

Signature · 1 line
2.46.0
Applied
#3 lore raw
In reply to Rowan Hart
Quoted 7 lines · Rowan Hart

This patch adds one API function to the QEMU plugin API

bool qemu_plugin_read_memory_vaddr(vaddr, GByteArray *, size_t);

The API allows reading memory from an arbitrary guest virtual address, which is useful for many things but the motivating examples are:

Queued to plugins/next with a minor tweak to the cpu_memory_rw_debug call.

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro