Add additional plugin API functions to read and write memory and registers
v3 cover Add additional plugin API functions to read and write memory and registers
This patch series adds several new API functions focused on enabling use cases around reading and writing guest memory from QEMU plugins. To support these new APIs, some utility functionality around retrieving information about address spaces is added as well.
The new qemu_plugin_write_register utilizes gdb_write_register, which is now declared in gdbstub.h for this purpose instead of being static.
qemu_plugin_write_memory_vaddr utilizes cpu_memory_rw_debug much the same as the existing read_memory_vaddr function does.
The read and write_hwaddr functions are the most different. These functions use address_space_rw, which works well in most cases. There is an important caveat that for writes, the page being written will be set dirty by the write operation. This dirty setting requires locking the page range, which can contend with an already held lock in page_collection_lock when called in a tb translate callback with a write to the instruction memory in the tb. The doc comments warn against doing this, and it's unlikely anyone would want to do this.
I've also added two test plugins: one that implements a simple hypercall interface that guest code can use to communicate with the plugin in a structured way with a test to ensure that this hypercall works and writing virtual memory works. And one that implements a simple patch utility to patch memory at runtime. The test for the second plugin ensures the patch applies successfully to instruction memory, and can use both hw and vaddr methods.
novafacing (8):
Expose gdb_write_register function to consumers of gdbstub Add register write API
Add address space API
Add memory virtual address write API
Add memory hardware address read/write API
Add patcher plugin and test
Add hypercalls plugin and test
Update plugin version and add notes
gdbstub/gdbstub.c | 2 +- include/exec/gdbstub.h | 14 + include/qemu/plugin.h | 6 + include/qemu/qemu-plugin.h | 217 ++++++- plugins/api.c | 213 ++++++- tests/tcg/Makefile.target | 2 + tests/tcg/plugins/hypercalls.c | 552 ++++++++++++++++++ tests/tcg/plugins/meson.build | 2 +- tests/tcg/plugins/patch.c | 324 ++++++++++ tests/tcg/x86_64/Makefile.softmmu-target | 36 +- tests/tcg/x86_64/system/hypercalls-target.c | 45 ++ tests/tcg/x86_64/system/patch-target.c | 32 + .../tcg/x86_64/system/validate-hypercalls.py | 40 ++ tests/tcg/x86_64/system/validate-patch.py | 39 ++ 14 files changed, 1501 insertions(+), 23 deletions(-) create mode 100644 tests/tcg/plugins/hypercalls.c create mode 100644 tests/tcg/plugins/patch.c create mode 100644 tests/tcg/x86_64/system/hypercalls-target.c create mode 100644 tests/tcg/x86_64/system/patch-target.c create mode 100755 tests/tcg/x86_64/system/validate-hypercalls.py create mode 100755 tests/tcg/x86_64/system/validate-patch.py
Signature · 1 line
2.49.0
PATCH v3 1/8 Expose gdb_write_register function to consumers of gdbstub
From: novafacing <[email protected]>
--- gdbstub/gdbstub.c | 2 +- include/exec/gdbstub.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)
gdbstub/gdbstub.c +1 -1
@@ -534,7 +534,7 @@ int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)return 0 ; } -static int gdb_write_register (CPUState *cpu,uint8_t *mem_buf,int reg) +int gdb_write_register (CPUState *cpu,uint8_t *mem_buf,int reg) {GDBRegisterState *r;
include/exec/gdbstub.h +14 -0
@@ -124,6 +124,20 @@ const GDBFeature *gdb_find_static_feature(const char *xmlname);*/ int gdb_read_register (CPUState *cpu,GByteArray *buf,int reg); +/** +* gdb_write_register() - Write a register associated with a CPU. +* @cpu: The CPU associated with the register. +* @buf: The buffer that the register contents will be set to. +* @reg: The register's number returned by gdb_find_feature_register(). +* +* The size of @buf must be at least the size of the register being +* written. +* +* Return: The number of written bytes, or 0 if an error occurred (for +* example, an unknown register was provided). +*/ +int gdb_write_register (CPUState *cpu,uint8_t *mem_buf,int reg); +/** * typedef GDBRegDesc - a register description from gdbstub */
Signature · 1 line
2.49.0
Re: [PATCH v3 1/8] Expose gdb_write_register function to consumers of gdbstub
Quoted 8 lines · Rowan Hart
From: novafacing <[email protected]>
Signed-off-by novafacing <[email protected]>Signed-off-by Rowan Hart <[email protected]>--- gdbstub/gdbstub.c | 2 +- include/exec/gdbstub.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)
Re: [PATCH v3 1/8] Expose gdb_write_register function to consumers of gdbstub
In reply to Rowan HartQuoted 49 lines · Rowan Hart
From: novafacing <[email protected]>
Signed-off-by novafacing <[email protected]>Signed-off-by Rowan Hart <[email protected]>--- gdbstub/gdbstub.c | 2 +- include/exec/gdbstub.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)gdbstub/gdbstub.c +1
-1@@ -534,7 +534,7 @@ int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)return 0 ; } -static int gdb_write_register (CPUState *cpu,uint8_t *mem_buf,int reg) +int gdb_write_register (CPUState *cpu,uint8_t *mem_buf,int reg) {GDBRegisterState *r;include/exec/gdbstub.h +14
-1@@ -124,6 +124,20 @@ const GDBFeature *gdb_find_static_feature(const char *xmlname);*/ int gdb_read_register (CPUState *cpu,GByteArray *buf,int reg); +/** +* gdb_write_register() - Write a register associated with a CPU. +* @cpu: The CPU associated with the register. +* @buf: The buffer that the register contents will be set to. +* @reg: The register's number returned by gdb_find_feature_register(). +* +* The size of @buf must be at least the size of the register being +* written. +* +* Return: The number of written bytes, or 0 if an error occurred (for +* example, an unknown register was provided). +*/ +int gdb_write_register (CPUState *cpu,uint8_t *mem_buf,int reg); +/** * typedef GDBRegDesc - a register description from gdbstub */ --
Quoted 3 lines
2.49.0
FYI there's another patch in the list that does this
https://lore.kernel.org/qemu-devel/[email protected]/
Letting you know so you can track each other's series if need be.
Signature · 2 lines
Manos Pitsidianakis Emulation and Virtualization Engineer at Linaro Ltd
Re: [PATCH v3 1/8] Expose gdb_write_register function to consumers of gdbstub
In reply to Rowan HartHi Rowan,
Quoted 4 lines
From: novafacing <[email protected]>
Signed-off-by novafacing <[email protected]>Signed-off-by Rowan Hart <[email protected]>
As I understand it, the commit subject should be prefixed with the subsystem the patch (mainly) touches. In this case "plugins".
Commit message bodies would also be appreciated. In this case maybe one motivating the change in one or two sentences. It's obvious enough from context here, but you want at least some context when stumbling accross a commit in isolation (e.g. after consulting git blame).
Regards,
Julian
PATCH v3 2/8 Add register write API
In reply to Rowan HartFrom: novafacing <[email protected]>
--- include/qemu/qemu-plugin.h | 57 +++++++++++++++++++++++++------------- plugins/api.c | 26 ++++++++++++----- 2 files changed, 56 insertions(+), 27 deletions(-)
include/qemu/qemu-plugin.h +37 -20
@@ -254,9 +254,6 @@ typedef struct {* @QEMU_PLUGIN_CB_NO_REGS: callback does not access the CPU's regs * @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs * @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs -* -* Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change -* system register state. */ enum qemu_plugin_cb_flags {QEMU_PLUGIN_CB_NO_REGS ,
@@ -871,7 +868,8 @@ struct qemu_plugin_register;/** * typedef qemu_plugin_reg_descriptor - register descriptions * -* @handle: opaque handle for retrieving value with qemu_plugin_read_register +* @handle: opaque handle for retrieving value with qemu_plugin_read_register or +* writing value with qemu_plugin_write_register * @name: register name * @feature: optional feature descriptor, can be NULL */
@@ -893,6 +891,41 @@ typedef struct {QEMU_PLUGIN_API GArray *qemu_plugin_get_registers(void); +/** +* qemu_plugin_read_register() - read register for current vCPU +* +* @handle: a @qemu_plugin_reg_handle handle +* @buf: A GByteArray for the data owned by the plugin +* +* This function is only available in a context that register read access is +* explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag. +* +* Returns the size of the read register. The content of @buf is in target byte +* order. On failure returns -1. +*/ +QEMU_PLUGIN_API +int qemu_plugin_read_register(struct qemu_plugin_register *handle, +GByteArray *buf); + +/** +* qemu_plugin_write_register() - write register for current vCPU +* +* @handle: a @qemu_plugin_reg_handle handle +* @buf: A GByteArray for the data owned by the plugin +* +* This function is only available in a context that register write access is +* explicitly requested via the QEMU_PLUGIN_CB_W_REGS flag. +* +* The size of @buf must be at least the size of the requested register. +* Attempting to write a register with @buf smaller than the register size +* will result in a crash or other undesired behavior. +* +* Returns the number of bytes written. On failure returns 0. +*/ +QEMU_PLUGIN_API +int qemu_plugin_write_register(struct qemu_plugin_register *handle, +GByteArray *buf); +/** * qemu_plugin_read_memory_vaddr() - read from memory using a virtual address *
@@ -915,22 +948,6 @@ QEMU_PLUGIN_APIbool qemu_plugin_read_memory_vaddr (uint64_t addr,GByteArray *data,size_t len); -/** -* qemu_plugin_read_register() - read register for current vCPU -* -* @handle: a @qemu_plugin_reg_handle handle -* @buf: A GByteArray for the data owned by the plugin -* -* This function is only available in a context that register read access is -* explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag. -* -* Returns the size of the read register. The content of @buf is in target byte -* order. On failure returns -1. -*/ -QEMU_PLUGIN_API -int qemu_plugin_read_register(struct qemu_plugin_register *handle, -GByteArray *buf); -/** * qemu_plugin_scoreboard_new() - alloc a new scoreboard *
plugins/api.c +19 -7
@@ -433,6 +433,25 @@ GArray *qemu_plugin_get_registers(void)return create_register_handles (regs); } +int qemu_plugin_read_register (struct qemu_plugin_register *reg,GByteArray *buf) +{ +g_assert (current_cpu); + +return gdb_read_register (current_cpu, buf,GPOINTER_TO_INT (reg) -1 ); +} + +int qemu_plugin_write_register (struct qemu_plugin_register *reg, +GByteArray *buf) +{ +g_assert (current_cpu); + +if (buf->len ==0 ) { +return 0 ; + } + +return gdb_write_register (current_cpu, buf->data,GPOINTER_TO_INT (reg) -1 ); +} +bool qemu_plugin_read_memory_vaddr (uint64_t addr,GByteArray *data,size_t len) {g_assert (current_cpu);
@@ -453,13 +472,6 @@ bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t len)return true; } -int qemu_plugin_read_register (struct qemu_plugin_register *reg,GByteArray *buf) -{ -g_assert (current_cpu); - -return gdb_read_register (current_cpu, buf,GPOINTER_TO_INT (reg) -1 ); -} -struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new (size_t element_size) {return plugin_scoreboard_new (element_size);
Signature · 1 line
2.49.0
Re: [PATCH v3 2/8] Add register write API
Quoted 8 lines · Rowan Hart
From: novafacing <[email protected]>
Signed-off-by novafacing <[email protected]>Signed-off-by Rowan Hart <[email protected]>--- include/qemu/qemu-plugin.h | 57 +++++++++++++++++++++++++------------- plugins/api.c | 26 ++++++++++++----- 2 files changed, 56 insertions(+), 27 deletions(-)
Re: [PATCH v3 2/8] Add register write API
In reply to Rowan HartHi Rowan,
Quoted 56 lines
include/qemu/qemu-plugin.h +35
-6@@ -893,6 +891,41 @@ typedef struct {QEMU_PLUGIN_API GArray *qemu_plugin_get_registers(void); +/** +* qemu_plugin_read_register() - read register for current vCPU +* +* @handle: a @qemu_plugin_reg_handle handle +* @buf: A GByteArray for the data owned by the plugin +* +* This function is only available in a context that register read access is +* explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag. +* +* Returns the size of the read register. The content of @buf is in target byte +* order. On failure returns -1. +*/ +QEMU_PLUGIN_API +int qemu_plugin_read_register(struct qemu_plugin_register *handle, +GByteArray *buf); + +/** +* qemu_plugin_write_register() - write register for current vCPU +* +* @handle: a @qemu_plugin_reg_handle handle +* @buf: A GByteArray for the data owned by the plugin +* +* This function is only available in a context that register write access is +* explicitly requested via the QEMU_PLUGIN_CB_W_REGS flag. +* +* The size of @buf must be at least the size of the requested register. +* Attempting to write a register with @buf smaller than the register size +* will result in a crash or other undesired behavior. +* +* Returns the number of bytes written. On failure returns 0. +*/ +QEMU_PLUGIN_API +int qemu_plugin_write_register(struct qemu_plugin_register *handle, +GByteArray *buf); +/** * qemu_plugin_read_memory_vaddr() - read from memory using a virtual address * @@ -915,22 +948,6 @@ QEMU_PLUGIN_APIbool qemu_plugin_read_memory_vaddr (uint64_t addr,GByteArray *data,size_t len); -/** -* qemu_plugin_read_register() - read register for current vCPU -* -* @handle: a @qemu_plugin_reg_handle handle -* @buf: A GByteArray for the data owned by the plugin -*
include/qemu/qemu-plugin.h QEMU_PLUGIN_API · line 933
Quoted 10 lines
include/qemu/qemu-plugin.h +0
-10-* This function is only available in a context that register read access is -* explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag. -* -* Returns the size of the read register. The content of @buf is in target byte -* order. On failure returns -1. -*/ -QEMU_PLUGIN_API -int qemu_plugin_read_register(struct qemu_plugin_register *handle, -GByteArray *buf); -
Why the code move?
Quoted 31 lines
plugins/api.c +19
-0@@ -433,6 +433,25 @@ GArray *qemu_plugin_get_registers(void)return create_register_handles (regs); } +int qemu_plugin_read_register (struct qemu_plugin_register *reg,GByteArray *buf) +{ +g_assert (current_cpu); + +return gdb_read_register (current_cpu, buf,GPOINTER_TO_INT (reg) -1 ); +} + +int qemu_plugin_write_register (struct qemu_plugin_register *reg, +GByteArray *buf) +{ +g_assert (current_cpu); + +if (buf->len ==0 ) { +return 0 ; + } + +return gdb_write_register (current_cpu, buf->data,GPOINTER_TO_INT (reg) -1 ); +} +bool qemu_plugin_read_memory_vaddr (uint64_t addr,GByteArray *data,size_t len) {g_assert (current_cpu);
plugins/api.c bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t len) · line 462
Quoted 10 lines
plugins/api.c +0
-7@@ -453,13 +472,6 @@ bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t len)return true; } -int qemu_plugin_read_register (struct qemu_plugin_register *reg,GByteArray *buf) -{ -g_assert (current_cpu); - -return gdb_read_register (current_cpu, buf,GPOINTER_TO_INT (reg) -1 ); -} -
Again, what was the reason for moving qemu_plugin_read_register?
Regards,
Julian
Re: [PATCH v3 2/8] Add register write API
Hi Julian,
Quoted 1 line
Again, what was the reason for moving
qemu_plugin_read_register?
I moved it so it's grouped with get_registers above instead of being separated below the memory functions. I can move it back, just seemed nicer that way.
-Rowan
Quoted 134 lines · Julian Ganz
Hi Rowan,
Quoted 5 lines
include/qemu/qemu-plugin.h +0
-0include/qemu/qemu-plugin.h typedef struct { · line 900
Quoted 10 lines
include/qemu/qemu-plugin.h +7
-0@@ -893,6 +891,41 @@ typedef struct {QEMU_PLUGIN_API GArray *qemu_plugin_get_registers(void); +/** +* qemu_plugin_read_register() - read register for current vCPU +* +* @handle: a @qemu_plugin_reg_handle handle +* @buf: A GByteArray for the data owned by the plugin +* +* This function is only available in a context that register read access is
include/qemu/qemu-plugin.h typedef struct {
Quoted 3 lines
include/qemu/qemu-plugin.h +3
-0+* explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag. +* +* Returns the size of the read register. The content of @buf is in target byte
include/qemu/qemu-plugin.h typedef struct {
Quoted 13 lines
include/qemu/qemu-plugin.h +13
-0+* order. On failure returns -1. +*/ +QEMU_PLUGIN_API +int qemu_plugin_read_register(struct qemu_plugin_register *handle, +GByteArray *buf); + +/** +* qemu_plugin_write_register() - write register for current vCPU +* +* @handle: a @qemu_plugin_reg_handle handle +* @buf: A GByteArray for the data owned by the plugin +* +* This function is only available in a context that register write access is
include/qemu/qemu-plugin.h typedef struct {
Quoted 4 lines
include/qemu/qemu-plugin.h +4
-0+* explicitly requested via the QEMU_PLUGIN_CB_W_REGS flag. +* +* The size of @buf must be at least the size of the requested register. +* Attempting to write a register with @buf smaller than the register size
include/qemu/qemu-plugin.h typedef struct {
Quoted 10 lines
include/qemu/qemu-plugin.h +8
-0+* will result in a crash or other undesired behavior. +* +* Returns the number of bytes written. On failure returns 0. +*/ +QEMU_PLUGIN_API +int qemu_plugin_write_register(struct qemu_plugin_register *handle, +GByteArray *buf); +/** * qemu_plugin_read_memory_vaddr() - read from memory using a virtual address
include/qemu/qemu-plugin.h QEMU_PLUGIN_API · line 924
Quoted 12 lines
include/qemu/qemu-plugin.h +0
-7* @@ -915,22 +948,6 @@ QEMU_PLUGIN_APIbool qemu_plugin_read_memory_vaddr (uint64_t addr,GByteArray *data,size_t len); -/** -* qemu_plugin_read_register() - read register for current vCPU -* -* @handle: a @qemu_plugin_reg_handle handle -* @buf: A GByteArray for the data owned by the plugin -* -* This function is only available in a context that register read access is
include/qemu/qemu-plugin.h QEMU_PLUGIN_API
Quoted 3 lines
include/qemu/qemu-plugin.h +0
-3-* explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag. -* -* Returns the size of the read register. The content of @buf is in target byte
include/qemu/qemu-plugin.h QEMU_PLUGIN_API · line 933
Quoted 6 lines
include/qemu/qemu-plugin.h +0
-6-* order. On failure returns -1. -*/ -QEMU_PLUGIN_API -int qemu_plugin_read_register(struct qemu_plugin_register *handle, -GByteArray *buf); -Why the code move?
plugins/api.c GArray *qemu_plugin_get_registers(void) · line 436
Quoted 9 lines
plugins/api.c +1
-0@@ -433,6 +433,25 @@ GArray *qemu_plugin_get_registers(void)return create_register_handles (regs); } +int qemu_plugin_read_register(struct qemu_plugin_register *reg,GByteArray *buf)
plugins/api.c GArray *qemu_plugin_get_registers(void)
Quoted 4 lines
plugins/api.c +4
-0+{ +g_assert (current_cpu); + + return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg) -1);
plugins/api.c GArray *qemu_plugin_get_registers(void)
Quoted 12 lines
plugins/api.c +12
-0+} + +int qemu_plugin_write_register (struct qemu_plugin_register *reg, +GByteArray *buf) +{ +g_assert (current_cpu); + +if (buf->len ==0 ) { +return 0 ; + } + + return gdb_write_register(current_cpu, buf->data,
GPOINTER_TO_INT(reg)- 1);plugins/api.c GArray *qemu_plugin_get_registers(void)
Quoted 3 lines
plugins/api.c +2
-0+} + bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data,
size_tlen)plugins/api.c bool qemu_plugin_read_memory_vaddr(uint64_t addr,
Quoted 3 lines
plugins/api.c +0
-0{g_assert (current_cpu);@@ -453,13 +472,6 @@ bool qemu_plugin_read_memory_vaddr(uint64_t addr,GByteArray *data,
size_tlen)plugins/api.c bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t len)
Quoted 4 lines
plugins/api.c +0
-1return true; } -int qemu_plugin_read_register(struct qemu_plugin_register *reg,GByteArray *buf)
plugins/api.c bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t len)
Quoted 4 lines
plugins/api.c +0
-4-{ -g_assert (current_cpu); - - return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg) -1);
plugins/api.c bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t len) · line 462
Quoted 2 lines
plugins/api.c +0
-2-} -Again, what was the reason for moving
qemu_plugin_read_register?Reviewed-by Julian Ganz <[email protected]>Regards,
Julian
Re: [PATCH v3 2/8] Add register write API
Hi Rowan,
Quoted 3 lines · Rowan Hart
Quoted 1 line
Again, what was the reason for moving
qemu_plugin_read_register?I moved it so it's grouped with
get_registersabove instead of being separated below the memory functions. I can move it back, just seemed nicer that way.
The move itself is totally acceptable. However, the commit message should, in my opinion, state why you did so.
Regards,
Julian
Re: [PATCH v3 2/8] Add register write API
In reply to Rowan HartQuoted 14 lines · Rowan Hart
From: novafacing <[email protected]>
Signed-off-by novafacing <[email protected]>Signed-off-by Rowan Hart <[email protected]>--- include/qemu/qemu-plugin.h | 57 +++++++++++++++++++++++++------------- plugins/api.c | 26 ++++++++++++----- 2 files changed, 56 insertions(+), 27 deletions(-)include/qemu/qemu-plugin.h +0
-0
include/qemu/qemu-plugin.h typedef struct { · line 259
Quoted 6 lines
include/qemu/qemu-plugin.h +0
-3@@ -254,9 +254,6 @@ typedef struct {* @QEMU_PLUGIN_CB_NO_REGS: callback does not access the CPU's regs * @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs * @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs -* -* Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change -* system register state.
I would expect us to:
a) handle the QEMU_PLUGIN_CB_RW_REGS
b) try and enforce we are only being called from such callbacks
Otherwise TCG won't know to restore register state from what has been written to.
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Re: [PATCH v3 2/8] Add register write API
Quoted 1 line
a) handle the
QEMU_PLUGIN_CB_RW_REGS
I missed that this was not already handled. I'll fix that.
Quoted 1 line
b) try and enforce we are only being called from such callbacks
Sure, beyond documentation I suppose we can add and check a flag to ensure this. I think it's a good idea to reduce foot guns from just calling it from any old place.
-Rowan
PATCH v3 3/8 Add address space API
In reply to Rowan HartFrom: novafacing <[email protected]>
--- include/qemu/plugin.h | 6 +++ include/qemu/qemu-plugin.h | 45 ++++++++++++++++++++++ plugins/api.c | 79 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+)
include/qemu/plugin.h +6 -0
@@ -139,6 +139,12 @@ struct qemu_plugin_tb {GArray *cbs; }; +/* Internal context for address space information */ +struct qemu_plugin_address_space_info { +CPUState *cpu; +GPtrArray *names; +}; +/** * struct CPUPluginState - per-CPU state for plugins * @event_mask: plugin event bitmap. Modified only via async work.
include/qemu/qemu-plugin.h +45 -0
51 lines not on this page · raw message
plugins/api.c +79 -0
91 lines not on this page · raw message
Signature · 1 line
2.49.0
PATCH v3 4/8 Add memory virtual address write API
In reply to Rowan HartFrom: novafacing <[email protected]>
--- include/qemu/qemu-plugin.h | 21 +++++++++++++++++++++ plugins/api.c | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+)
include/qemu/qemu-plugin.h +21 -0
@@ -993,6 +993,27 @@ QEMU_PLUGIN_APIbool qemu_plugin_read_memory_vaddr (uint64_t addr,GByteArray *data,size_t len); +/** +* qemu_plugin_write_memory_vaddr() - write to memory using a virtual address +* +* @addr: A virtual address to write to +* @data: A byte array containing the data to write +* +* The contents of @data will be written to memory starting at the virtual +* address @addr. +* +* This function does not guarantee consistency of writes, nor does it ensure +* that pending writes are flushed either before or after the write takes place, +* so callers should take care to only call this function in vCPU context (i.e. +* in callbacks) and avoid depending on the existence of data written using this +* function which may be overwritten afterward. +* +* Returns true on success and false on failure. +*/ +QEMU_PLUGIN_API +bool qemu_plugin_write_memory_vaddr(uint64_t addr, +GByteArray *data); +/** * qemu_plugin_scoreboard_new() - alloc a new scoreboard *
plugins/api.c +18 -0
@@ -551,6 +551,24 @@ bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t len)return true; } +bool qemu_plugin_write_memory_vaddr (uint64_t addr,GByteArray *data) +{ +g_assert (current_cpu); + +if (data->len ==0 ) { +return false; + } + +int result =cpu_memory_rw_debug (current_cpu, addr, data->data, + data->len, true); + +if (result <0 ) { +return false; + } + +return true; +} +struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new (size_t element_size) {return plugin_scoreboard_new (element_size);
Signature · 1 line
2.49.0
Re: [PATCH v3 4/8] Add memory virtual address write API
Quoted 8 lines · Rowan Hart
From: novafacing <[email protected]>
Signed-off-by novafacing <[email protected]>Signed-off-by Rowan Hart <[email protected]>--- include/qemu/qemu-plugin.h | 21 +++++++++++++++++++++ plugins/api.c | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+)
PATCH v3 5/8 Add memory hardware address read/write API
In reply to Rowan HartFrom: novafacing <[email protected]>
--- include/qemu/qemu-plugin.h | 96 +++++++++++++++++++++++++++++++++++ plugins/api.c | 100 +++++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+)
include/qemu/qemu-plugin.h +96 -0
@@ -1014,6 +1014,102 @@ QEMU_PLUGIN_APIbool qemu_plugin_write_memory_vaddr (uint64_t addr,GByteArray *data); +/** +* enum qemu_plugin_hwaddr_operation_result - result of a memory operation +* +* @QEMU_PLUGIN_HWADDR_OPERATION_OK: hwaddr operation succeeded +* @QEMU_PLUGIN_HWADDR_OPERATION_ERROR: unexpected error occurred +* @QEMU_PLUGIN_HWADDR_OPERATION_DEVICE_ERROR: error in memory device +* @QEMU_PLUGIN_HWADDR_OPERATION_ACCESS_DENIED: permission error +* @QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS: address was invalid +* @QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS_SPACE: invalid address space +*/ +enum qemu_plugin_hwaddr_operation_result { +QEMU_PLUGIN_HWADDR_OPERATION_OK , +QEMU_PLUGIN_HWADDR_OPERATION_ERROR , +QEMU_PLUGIN_HWADDR_OPERATION_DEVICE_ERROR , +QEMU_PLUGIN_HWADDR_OPERATION_ACCESS_DENIED , +QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS , +QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS_SPACE , +}; + +/** +* qemu_plugin_read_memory_hwaddr() - read from memory using a hardware address +* +* @as_idx: The index of the address space to read from +* @addr: A physical 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. +* +* This function is only valid for softmmu targets. +* +* Returns a qemu_plugin_hwaddr_operation_result indicating the result of the +* operation. +*/ +QEMU_PLUGIN_API +enum qemu_plugin_hwaddr_operation_result +qemu_plugin_read_memory_hwaddr (unsigned int as_idx,uint64_t addr, +GByteArray *data,size_t len); + +/** +* qemu_plugin_write_memory_hwaddr() - write to memory using a hardware address +* +* @as_idx: The index of the address space to write to +* @addr: A physical address to write to +* @data: A byte array containing the data to write +* +* The contents of @data will be written to memory starting at the hardware +* address @addr. +* +* This function does not guarantee consistency of writes, nor does it ensure +* that pending writes are flushed either before or after the write takes place, +* so callers should take care when calling this function in plugin callbacks to +* avoid depending on the existence of data written using this function which +* may be overwritten afterward. In addition, this function requires that the +* pages containing the address are not locked. Practically, this means that you +* should not write instruction memory in a current translation block inside a +* callback registered with qemu_plugin_register_vcpu_tb_trans_cb. +* +* You can, for example, write instruction memory in a current translation block +* in a callback registered with qemu_plugin_register_vcpu_tb_exec_cb, although +* be aware that the write will not be flushed until after the translation block +* has finished executing. In general, this function should be used to write +* data memory or to patch code at a known address, not in a current translation +* block. +* +* This function is only valid for softmmu targets. +* +* Returns a qemu_plugin_hwaddr_operation_result indicating the result of the +* operation. +*/ +QEMU_PLUGIN_API +enum qemu_plugin_hwaddr_operation_result +qemu_plugin_write_memory_hwaddr (unsigned int as_idx,uint64_t addr, +GByteArray *data); + +/** +* qemu_plugin_translate_vaddr() - translate a virtual address to a physical one +* +* @vaddr: virtual address to translate +* @hwaddr: pointer to store the physical address +* +* This function is only valid in vCPU context (i.e. in callbacks) and is only +* valid for softmmu targets. +* +* Returns true on success and false on failure. +*/ +QEMU_PLUGIN_API +bool qemu_plugin_translate_vaddr(uint64_t vaddr, uint64_t *hwaddr); +/** * qemu_plugin_scoreboard_new() - alloc a new scoreboard *
plugins/api.c +100 -0
106 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH v3 5/8] Add memory hardware address read/write API
Quoted 8 lines · Rowan Hart
From: novafacing <[email protected]>
Signed-off-by novafacing <[email protected]>Signed-off-by Rowan Hart <[email protected]>--- include/qemu/qemu-plugin.h | 96 +++++++++++++++++++++++++++++++++++ plugins/api.c | 100 +++++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+)
Reading this patch, and patch 3 (Add address space API), I am not sure AddressSpace is something we want to leak in plugins interface.
It is a concept *very* internal to QEMU, and not reflecting directly something concerning the emulated architecture (it is related, but not officially described for it).
The same way qemu_plugin_write_memory_vaddr is only valid in the current page table setup, we could assume the same for current address space, and return an error if memory is not mapped with current AS. Eventually, we could read/write a given hwaddr in all existing address spaces (starting with current mapped one), if it makes sense to do this, which I'm not sure about.
What are your thoughts on this?
qemu_plugin_translate_vaddr is fine for me.
Re: [PATCH v3 5/8] Add memory hardware address read/write API
Well, first I just noticed that I left a debug print in this function! So I'll fix that.
Quoted 7 lines
Reading this patch, and patch 3 (Add address space API), I am not sure AddressSpace is something we want to leak in plugins interface.
It is a concept *very* internal to QEMU, and not reflecting directly something concerning the emulated architecture (it is related, but not officially described for it).The same way
qemu_plugin_write_memory_vaddris only valid in the current page table setup, we could assume the same for current address space, and return an error if memory is not mapped with current AS. Eventually, we could read/write a given hwaddr in all existing address spaces (starting with current mapped one), if it makes sense to do this, which I'm not sure about.What are your thoughts on this?
I definitely see the arguments for not exposing it even as an opaque struct, internality not withstanding it also adds some complexity for plugin authors.
My thought with exposing it is kind of twofold. First, there are specific address spaces like the secure address space on ARM or I/O memory on x86 that plugins might like to access and I think it's easiest to facilitate that if we just let them choose which one they want to r/w to. Second, I wanted to lean towards being less restrictive now to avoid having to go back and remove restrictions later since even though it's very internal, it doesn't seem very likely to change.
That said, if you think it's more trouble than it's worth I'm totally fine with just defaulting to writing to the current AS (or to cpu-memory, whichever's more reasonable). Your call, just let me know which way you think is best for v4 :)
Quoted 1 line
qemu_plugin_translate_vaddris fine for me.
I did have a question about this -- one of the test plugins prints out vaddr, haddr from qemu_plugin_insn_haddr, and the translated haddr from qemu_plugin_translate_vaddr. When running with direct memory mappings in a system test, the vaddr = translated haddr, which is correct, but the haddr from qemu_plugin_insn_haddr was incorrect (it was 0x7f....f<actual address>). Is this expected behavior?
Thanks for the feedback!
Re: [PATCH v3 5/8] Add memory hardware address read/write API
Quoted 30 lines · Rowan Hart
Well, first I just noticed that I left a debug print in this function! So I'll fix that.
Quoted 6 lines
Reading this patch, and patch 3 (Add address space API), I am not sure AddressSpace is something we want to leak in plugins interface. It is a concept *very* internal to QEMU, and not reflecting directly something concerning the emulated architecture (it is related, but not officially described for it).
Quoted 8 lines
The same way
qemu_plugin_write_memory_vaddris only valid in the current page table setup, we could assume the same for current address space, and return an error if memory is not mapped with current AS. Eventually, we could read/write a given hwaddr in all existing address spaces (starting with current mapped one), if it makes sense to do this, which I'm not sure about.What are your thoughts on this?
I definitely see the arguments for not exposing it even as an opaque struct, internality not withstanding it also adds some complexity for plugin authors.
My thought with exposing it is kind of twofold. First, there are specific address spaces like the secure address space on ARM or I/O memory on x86 that plugins might like to access and I think it's easiest to facilitate that if we just let them choose which one they want to r/w to. Second, I wanted to lean towards being less restrictive now to avoid having to go back and remove restrictions later since even though it's very internal, it doesn't seem very likely to change.
Quoted 4 lines
That said, if you think it's more trouble than it's worth I'm totally fine with just defaulting to writing to the current AS (or to cpu-memory, whichever's more reasonable). Your call, just let me know which way you think is best for v4 :)
I understand your point, but to the opposite of registers, I think we should refrain from exposing all this. For now, we can just use the current AS.
Later, we could consider to add a new architecture specific parameter for that need (being a union, with fields per base architecture). So people writing architecture specific plugins can have a solution.
AddressSpace as = {.arm = ADDRESS_SPACE_ARM_SECURE}; qemu_plugin_read_memory_hwaddr_as(addr, data, as);
Quoted 8 lines
Quoted 1 line
qemu_plugin_translate_vaddris fine for me.I did have a question about this -- one of the test plugins prints out vaddr, haddr from
qemu_plugin_insn_haddr, and the translated haddr fromqemu_plugin_translate_vaddr. When running with direct memory mappings in a system test, the vaddr = translated haddr, which is correct, but the haddr fromqemu_plugin_insn_haddrwas incorrect (it was 0x7f....f<actual address>). Is this expected behavior?
qemu_plugin_insn_haddr returns directly a pointer to instruction in (host) memory, which is different from hwaddr, thus the void* signature. Name is pretty confusing though, qemu_plugin_insn_ptr could have been a better name.
Quoted 3 lines
Thanks for the feedback!
Re: [PATCH v3 5/8] Add memory hardware address read/write API
In reply to Rowan HartHi Rowan,
Quoted 12 lines
include/qemu/qemu-plugin.h +3
-0@@ -1014,6 +1014,102 @@ QEMU_PLUGIN_APIbool qemu_plugin_write_memory_vaddr (uint64_t addr,GByteArray *data); +/** +* qemu_plugin_translate_vaddr() - translate a virtual address to a physical one +*
include/qemu/qemu-plugin.h QEMU_PLUGIN_API · line 1111
Quoted 10 lines
include/qemu/qemu-plugin.h +10
-0+* @vaddr: virtual address to translate +* @hwaddr: pointer to store the physical address +* +* This function is only valid in vCPU context (i.e. in callbacks) and is only +* valid for softmmu targets. +* +* Returns true on success and false on failure. +*/ +QEMU_PLUGIN_API +bool qemu_plugin_translate_vaddr(uint64_t vaddr, uint64_t *hwaddr);
As a (testing) plugin author, I'm quite interested in this function in particular. For the next iteration, would you be willing to split this one out into its own patch? This would enhance the chance of it getting picked up even if other parts of the series should not make it.
Quoted 16 lines
plugins/api.c +10
-0@@ -569,6 +569,106 @@ bool qemu_plugin_write_memory_vaddr(uint64_t addr, GByteArray *data) +bool qemu_plugin_translate_vaddr (uint64_t vaddr,uint64_t *hwaddr) +{ +#ifdef CONFIG_SOFTMMU +g_assert (current_cpu); + +CPUState *cpu = current_cpu; + +uint64_t res =cpu_get_phys_page_debug (cpu, vaddr); + +if (res == (uint64_t )-1 ) {
plugins/api.c bool qemu_plugin_write_memory_vaddr(uint64_t addr, GByteArray *data) · line 670
Quoted 10 lines
plugins/api.c +10
-0+ return false; + } + + *hwaddr = res | (vaddr & ~TARGET_PAGE_MASK ); + +return true; +#else +return false; +#endif +}
This definition strikes me as odd. What was your reason to assert current_cpu here, but not in the other two functions? Also a bit surprising is the declaration of cpu if you use it in just one place (rather than just use current_cpu directly as for the assertion).
And there is no reason in particular why the vCPU could not be a function parameter of qemu_plugin_translate_vaddr, right? You don't have the same restrictions as in qemu_plugin_read_memory_hwaddr or qemu_plugin_hwaddr_operation_result where you actually touch memory?
Regards,
Julian
Re: [PATCH v3 5/8] Add memory hardware address read/write API
Quoted 10 lines · Julian Ganz
This definition strikes me as odd. What was your reason to assert
current_cpuhere, but not in the other two functions? Also a bit surprising is the declaration ofcpuif you use it in just one place (rather than just usecurrent_cpudirectly as for the assertion).And there is no reason in particular why the vCPU could not be a function parameter of
qemu_plugin_translate_vaddr, right? You don't have the same restrictions as inqemu_plugin_read_memory_hwaddrorqemu_plugin_hwaddr_operation_resultwhere you actually touch memory?
That's a good point, adding a "unsigned int vcpu_index" to the signature should be enough to query current or any other vcpu easily.
Quoted 2 lines
Regards,
Julian
Re: [PATCH v3 5/8] Add memory hardware address read/write API
Quoted 7 lines
Quoted 7 lines
This definition strikes me as odd. What was your reason to assert
current_cpuhere, but not in the other two functions? Also a bit surprising is the declaration ofcpuif you use it in just one place (rather than just usecurrent_cpudirectly as for the assertion).
Quoted 9 lines
Quoted 5 lines
And there is no reason in particular why the vCPU could not be a function parameter of
qemu_plugin_translate_vaddr, right? You don't have the same restrictions as inqemu_plugin_read_memory_hwaddrorqemu_plugin_hwaddr_operation_resultwhere you actually touch memory?That's a good point, adding a "unsigned int
vcpu_index" to the signature should be enough to query current or any other vcpu easily.
This is a really nice idea, it might be nice to make a vcpu version of read/write register too. For memory, I'd think going with the current memory is probably fine, I don't see any configs with different memory per vcpu?
Quoted 1 line
Re: [PATCH v3 5/8] Add memory hardware address read/write API
Quoted 14 lines · Rowan Hart
> This definition strikes me as odd. What was your reason to assert >
current_cpuhere, but not in the other two functions? Also a bit > surprising is the declaration ofcpuif you use it in just one
place
> (rather than just usecurrent_cpudirectly as for the assertion). >
> And there is no reason in particular why the vCPU could not be a > function parameter ofqemu_plugin_translate_vaddr, right? You don't > have the same restrictions as inqemu_plugin_read_memory_hwaddror >qemu_plugin_hwaddr_operation_resultwhere you actually touch
memory?
>
Quoted 6 lines
That's a good point, adding a "unsigned int
vcpu_index" to the signature
should be enough to query current or any other vcpu easily.This is a really nice idea, it might be nice to make a vcpu version of read/write register too. For memory, I'd think going with the current memory is probably fine, I don't see any configs with different memory per vcpu?
Thinking about it twice, and after reading Alex comments for writing registers, it's probably not a good idea to allow such side effects on other vcpus (on registers and memory). In case of registers, there is nothing ensuring they will be written correctly, so it only makes sense for current_cpu, as we are in a callback running on it. Safer to have the same semantic for memory read/write also, so please forget my idea.
PATCH v3 6/8 Add patcher plugin and test
In reply to Rowan HartFrom: novafacing <[email protected]>
--- tests/tcg/Makefile.target | 1 + tests/tcg/plugins/meson.build | 2 +- tests/tcg/plugins/patch.c | 324 ++++++++++++++++++++++ tests/tcg/x86_64/Makefile.softmmu-target | 32 ++- tests/tcg/x86_64/system/patch-target.c | 32 +++ tests/tcg/x86_64/system/validate-patch.py | 39 +++ 6 files changed, 424 insertions(+), 6 deletions(-) create mode 100644 tests/tcg/plugins/patch.c create mode 100644 tests/tcg/x86_64/system/patch-target.c create mode 100755 tests/tcg/x86_64/system/validate-patch.py
tests/tcg/Makefile.target +1 -0
@@ -176,6 +176,7 @@ RUN_TESTS+=$(EXTRA_RUNS) # Some plugins need additional arguments above the default to fully # exercise things. We can define them on a per-test basis here. run-plugin-%-with-libmem.so: PLUGIN_ARGS=$(COMMA)inline=true +run-plugin-%-with-libpatch.so: PLUGIN_ARGS=$(COMMA)target=ffffffff$(COMMA)patch=00000000 ifeq ($(filter %-softmmu, $(TARGET)),) run-%: %
tests/tcg/plugins/meson.build +1 -1
@@ -1,6 +1,6 @@ t = [] if get_option('plugins') - foreach i : ['bb', 'empty', 'inline', 'insn', 'mem', 'reset', 'syscall'] + foreach i : ['bb', 'empty', 'inline', 'insn', 'mem', 'reset', 'syscall', 'patch'] if host_os == 'windows' t += shared_module(i, files(i + '.c') + '../../../contrib/plugins/win32_linker.c', include_directories: '../../../include/qemu',
tests/tcg/plugins/patch.c new file +324 -0
324 lines not on this page · raw message
tests/tcg/x86_64/Makefile.softmmu-target +27 -5
@@ -7,18 +7,27 @@ # I386_SYSTEM_SRC=$(SRC_PATH)/tests/tcg/i386/system -X64_SYSTEM_SRC=$(SRC_PATH)/tests/tcg/x86_64/system +X86_64_SYSTEM_SRC=$(SRC_PATH)/tests/tcg/x86_64/system # These objects provide the basic boot code and helper functions for all tests CRT_OBJS=boot.o -CRT_PATH=$(X64_SYSTEM_SRC) -LINK_SCRIPT=$(X64_SYSTEM_SRC)/kernel.ld +X86_64_TEST_C_SRCS=$(wildcard $(X86_64_SYSTEM_SRC)/*.c) +X86_64_TEST_S_SRCS= + +X86_64_C_TESTS = $(patsubst $(X86_64_SYSTEM_SRC)/%.c, %, $(X86_64_TEST_C_SRCS)) +X86_64_S_TESTS = $(patsubst $(X86_64_SYSTEM_SRC)/%.S, %, $(X86_64_TEST_S_SRCS)) + +X86_64_TESTS = $(X86_64_C_TESTS) +X86_64_TESTS += $(X86_64_S_TESTS) + +CRT_PATH=$(X86_64_SYSTEM_SRC) +LINK_SCRIPT=$(X86_64_SYSTEM_SRC)/kernel.ld LDFLAGS=-Wl,-T$(LINK_SCRIPT) -Wl,-melf_x86_64 CFLAGS+=-nostdlib -ggdb -O0 $(MINILIB_INC) LDFLAGS+=-static -nostdlib $(CRT_OBJS) $(MINILIB_OBJS) -lgcc -TESTS+=$(MULTIARCH_TESTS) +TESTS+=$(X86_64_TESTS) $(MULTIARCH_TESTS) EXTRA_RUNS+=$(MULTIARCH_RUNS) # building head blobs
@@ -27,11 +36,24 @@ EXTRA_RUNS+=$(MULTIARCH_RUNS) %.o: $(CRT_PATH)/%.S $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -Wa,--noexecstack -c $< -o $@ -# Build and link the tests +# Build and link the multiarch tests %: %.c $(LINK_SCRIPT) $(CRT_OBJS) $(MINILIB_OBJS) $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< -o $@ $(LDFLAGS) +# Build and link the arch tests +%: $(X86_64_SYSTEM_SRC)/%.c $(LINK_SCRIPT) $(CRT_OBJS) $(MINILIB_OBJS) + $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< -o $@ $(LDFLAGS) + memory: CFLAGS+=-DCHECK_UNALIGNED=1 +patch-target: CFLAGS+=-O0 # Running QEMU_OPTS+=-device isa-debugcon,chardev=output -device isa-debug-exit,iobase=0xf4,iosize=0x4 -kernel + +# Add patch-target to ADDITIONAL_PLUGINS_TESTS +ADDITIONAL_PLUGINS_TESTS += patch-target + +run-plugin-patch-target-with-libpatch.so: \ + PLUGIN_ARGS=$(COMMA)target=ffc0$(COMMA)patch=9090$(COMMA)use_hwaddr=true$(COMMA)debug_insns=false +run-plugin-patch-target-with-libpatch.so: \ + CHECK_PLUGIN_OUTPUT_COMMAND=$(X86_64_SYSTEM_SRC)/validate-patch.py [email protected]
tests/tcg/x86_64/system/patch-target.c new file +32 -0
@@ -0,0 +1,32 @@ +/* +* SPDX-License-Identifier: GPL-2.0-or-later +* +* Copyright (C) 2025, Rowan Hart <[email protected]> +* +* License: GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +* This test target increments a value 100 times. The patcher converts the +* inc instruction to a nop, so it only increments the value once. +* +*/ +#include <minilib.h> + +int main (void ) +{ +ml_printf ("Running test...\n" ); +#if defined(__x86_64__) +ml_printf ("Testing insn memory read/write...\n" ); +unsigned int x =0 ; +for (int i =0 ; i <100 ; i++) { + asmvolatile ( +"inc %[x]" + : [x]"+a" (x) + ); + } +ml_printf ("Value: %d\n" , x); +#else +#error "This test is only valid for x86_64 architecture." +#endif +return 0 ; +}
tests/tcg/x86_64/system/validate-patch.py new file +39 -0
@@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# +# validate-patch.py: check the patch applies +# +# This program takes two inputs: +# - the plugin output +# - the binary output +# +# Copyright (C) 2024 +# +# SPDX-License-Identifier: GPL-2.0-or-later + +import sys +from argparse import ArgumentParser + +def main() -> None: + """ + Process the arguments, injest the program and plugin out and + verify they match up and report if they do not. + """ + parser = ArgumentParser(description="Validate patch") + parser.add_argument('test_output', + help="The output from the test itself") + parser.add_argument('plugin_output', + help="The output from plugin") + args = parser.parse_args() + + with open(args.test_output, 'r') as f: + test_data = f.read() + with open(args.plugin_output, 'r') as f: + plugin_data = f.read() + if "Value: 1" in test_data: + sys.exit(0) + else: + sys.exit(1) + +if __name__ == "__main__": + main() +
Signature · 1 line
2.49.0
PATCH v3 7/8 Add hypercalls plugin and test
In reply to Rowan HartFrom: novafacing <[email protected]>
--- tests/tcg/Makefile.target | 1 + tests/tcg/plugins/hypercalls.c | 552 ++++++++++++++++++ tests/tcg/plugins/meson.build | 2 +- tests/tcg/x86_64/Makefile.softmmu-target | 6 +- tests/tcg/x86_64/system/hypercalls-target.c | 45 ++ .../tcg/x86_64/system/validate-hypercalls.py | 40 ++ 6 files changed, 644 insertions(+), 2 deletions(-) create mode 100644 tests/tcg/plugins/hypercalls.c create mode 100644 tests/tcg/x86_64/system/hypercalls-target.c create mode 100755 tests/tcg/x86_64/system/validate-hypercalls.py
tests/tcg/Makefile.target +1 -0
@@ -177,6 +177,7 @@ RUN_TESTS+=$(EXTRA_RUNS) # exercise things. We can define them on a per-test basis here. run-plugin-%-with-libmem.so: PLUGIN_ARGS=$(COMMA)inline=true run-plugin-%-with-libpatch.so: PLUGIN_ARGS=$(COMMA)target=ffffffff$(COMMA)patch=00000000 +run-plugin-%-with-libhypercalls.so: PLUGIN_ARGS=$(COMMA)ignore_unsupported=true ifeq ($(filter %-softmmu, $(TARGET)),) run-%: %
tests/tcg/plugins/hypercalls.c new file +552 -0
552 lines not on this page · raw message
tests/tcg/plugins/meson.build +1 -1
@@ -1,6 +1,6 @@ t = [] if get_option('plugins') - foreach i : ['bb', 'empty', 'inline', 'insn', 'mem', 'reset', 'syscall', 'patch'] + foreach i : ['bb', 'empty', 'inline', 'insn', 'mem', 'reset', 'syscall', 'hypercalls', 'patch'] if host_os == 'windows' t += shared_module(i, files(i + '.c') + '../../../contrib/plugins/win32_linker.c', include_directories: '../../../include/qemu',
tests/tcg/x86_64/Makefile.softmmu-target +5 -1
@@ -46,14 +46,18 @@ EXTRA_RUNS+=$(MULTIARCH_RUNS) memory: CFLAGS+=-DCHECK_UNALIGNED=1 patch-target: CFLAGS+=-O0 +hypercalls-target: CFLAGS+=-O0 # Running QEMU_OPTS+=-device isa-debugcon,chardev=output -device isa-debug-exit,iobase=0xf4,iosize=0x4 -kernel # Add patch-target to ADDITIONAL_PLUGINS_TESTS ADDITIONAL_PLUGINS_TESTS += patch-target +ADDITIONAL_PLUGINS_TESTS += hypercalls-target run-plugin-patch-target-with-libpatch.so: \ PLUGIN_ARGS=$(COMMA)target=ffc0$(COMMA)patch=9090$(COMMA)use_hwaddr=true$(COMMA)debug_insns=false run-plugin-patch-target-with-libpatch.so: \ - CHECK_PLUGIN_OUTPUT_COMMAND=$(X86_64_SYSTEM_SRC)/validate-patch.py [email protected] + CHECK_PLUGIN_OUTPUT_COMMAND=$(X86_64_SYSTEM_SRC)/validate-patch.py [email protected] +run-plugin-hypercalls-target-with-libhypercalls.so: \ + CHECK_PLUGIN_OUTPUT_COMMAND=$(X86_64_SYSTEM_SRC)/validate-hypercalls.py [email protected]
tests/tcg/x86_64/system/hypercalls-target.c new file +45 -0
45 lines not on this page · raw message
tests/tcg/x86_64/system/validate-hypercalls.py new file +40 -0
@@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# +# validate-patch.py: check the patch applies +# +# This program takes two inputs: +# - the plugin output +# - the binary output +# +# Copyright (C) 2024 +# +# SPDX-License-Identifier: GPL-2.0-or-later + +import sys +from argparse import ArgumentParser + +def main() -> None: + """ + Process the arguments, injest the program and plugin out and + verify they match up and report if they do not. + """ + parser = ArgumentParser(description="Validate patch") + parser.add_argument('test_output', + help="The output from the test itself") + parser.add_argument('plugin_output', + help="The output from plugin") + args = parser.parse_args() + + with open(args.test_output, 'r') as f: + test_data = f.read() + with open(args.plugin_output, 'r') as f: + plugin_data = f.read() + + if "Victory" in test_data: + sys.exit(0) + else: + sys.exit(1) + +if __name__ == "__main__": + main() +
Signature · 1 line
2.49.0
PATCH v3 8/8 Update plugin version and add notes
In reply to Rowan HartFrom: novafacing <[email protected]>
--- include/qemu/qemu-plugin.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
include/qemu/qemu-plugin.h +11 -1
@@ -65,11 +65,21 @@ typedef uint64_t qemu_plugin_id_t;* * version 4: * - added qemu_plugin_read_memory_vaddr +* +* version 5: +* - added qemu_plugin_write_memory_vaddr +* - added qemu_plugin_read_memory_hwaddr +* - added qemu_plugin_write_memory_hwaddr +* - added qemu_plugin_write_register +* - added qemu_plugin_get_current_vcpu_address_spaces +* - added qemu_plugin_n_address_spaces +* - added qemu_plugin_address_space_name +* - added qemu_plugin_translate_vaddr */ extern QEMU_PLUGIN_EXPORT int qemu_plugin_version; -#define QEMU_PLUGIN_VERSION 4 +#define QEMU_PLUGIN_VERSION 5/** * struct qemu_info_t - system information for plugins
Signature · 1 line
2.49.0