Add additional plugin API functions to read and write memory and registers
v5 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.
For v3, I've had a few comments from the last submission that I've addressed, and some that I haven't for one reason or another:
- Enforce QEMU_PLUGIN_CB_ flags in register read/write operations: done!
- Fix my commit messages and add long messages describing commits: done!
- Un-expose AS internals: done! Functions operate on current vCPU, current AS.
- Clean up use of current_cpu: done!
- Make functions take a vcpu_idx: not done. May revisit but it allows footguns. Even for translation, seems best to not do this now. We can easily add _vcpu versions of these functions in the future if we change our minds!
For v4, I've just updated the enforcement of the QEMU_PLUGIN_CB_ flags to just use immediate stores, which simplifies the implementation quite a lot and should be more efficient too. Thanks Pierrick for the suggestion!
Rowan Hart (2):
plugins: Add enforcement of QEMU_PLUGIN_CB flags in register R/W
callbacks
plugins: Remove use of qemu_plugin_read_register where it is not
permitted
novafacing (7):
gdbstub: Expose gdb_write_register function to consumers of gdbstub
plugins: Add register write API
plugins: Add memory virtual address write API
plugins: Add memory hardware address read/write API
plugins: Add patcher plugin and test
plugins: Add hypercalls plugin and test
plugins: Update plugin version and add notes
accel/tcg/plugin-gen.c | 30 + gdbstub/gdbstub.c | 2 +- include/exec/gdbstub.h | 14 + include/hw/core/cpu.h | 1 + include/qemu/plugin.h | 4 + include/qemu/qemu-plugin.h | 166 +++++- plugins/api.c | 135 ++++- plugins/core.c | 32 +- tests/tcg/Makefile.target | 2 + tests/tcg/plugins/hypercalls.c | 552 ++++++++++++++++++ tests/tcg/plugins/insn.c | 22 +- tests/tcg/plugins/meson.build | 2 +- tests/tcg/plugins/patch.c | 302 ++++++++++ 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 ++ 18 files changed, 1406 insertions(+), 50 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 v5 1/9 gdbstub: Expose gdb_write_register function to consumers of gdbstub
From: novafacing <[email protected]>
This patch exposes the gdb_write_register function from gdbstub/gdbstub.c via the exec/gdbstub.h header file to support use in plugins to write register contents.
--- 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
PATCH v5 2/9 plugins: Add register write API
In reply to Rowan HartFrom: novafacing <[email protected]>
This patch adds a function to the plugins API to allow plugins to write register contents. It also moves the qemu_plugin_read_register function so all the register-related functions are grouped together in the file.
--- include/qemu/qemu-plugin.h | 54 ++++++++++++++++++++++++++------------ plugins/api.c | 22 +++++++++++----- 2 files changed, 52 insertions(+), 24 deletions(-)
include/qemu/qemu-plugin.h +37 -17
@@ -871,7 +871,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 +894,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_RW_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 +951,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 +15 -7
@@ -433,6 +433,21 @@ 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); + +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 +468,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
PATCH v5 3/9 plugins: Add enforcement of QEMU_PLUGIN_CB flags in register R/W callbacks
In reply to Rowan HartThis patch adds functionality to enforce the requested QEMU_PLUGIN_CB_ flags level passed when registering a callback function using the plugins API. Each time a callback is about to be invoked, a thread-local variable will be updated with the level that callback requested. Then, called API functions (in particular, the register read and write API) will call qemu_plugin_get_cb_flags() to check the level is at least the level they require.
--- accel/tcg/plugin-gen.c | 30 ++++++++++++++++++++++++++++++ include/hw/core/cpu.h | 1 + include/qemu/plugin.h | 4 ++++ include/qemu/qemu-plugin.h | 3 --- plugins/api.c | 8 ++++++++ plugins/core.c | 32 ++++++++++++++++++++++++++------ 6 files changed, 69 insertions(+), 9 deletions(-)
accel/tcg/plugin-gen.c +30 -0
@@ -117,10 +117,20 @@ static TCGv_i32 gen_cpu_index(void)static void gen_udata_cb (struct qemu_plugin_regular_cb *cb) {TCGv_i32 cpu_index =gen_cpu_index (); +enum qemu_plugin_cb_flags cb_flags = +tcg_call_to_qemu_plugin_cb_flags (cb->info->flags); +TCGv_i32 flags =tcg_constant_i32 (cb_flags); +TCGv_i32 clear_flags =tcg_constant_i32 (QEMU_PLUGIN_CB_NO_REGS ); +tcg_gen_st_i32 (flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState));tcg_gen_call2 (cb->f.vcpu_udata, cb->info,NULL ,tcgv_i32_temp (cpu_index),tcgv_ptr_temp (tcg_constant_ptr (cb->userp))); +tcg_gen_st_i32 (clear_flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState));tcg_temp_free_i32 (cpu_index); +tcg_temp_free_i32 (flags); +tcg_temp_free_i32 (clear_flags); }static TCGv_ptr gen_plugin_u64_ptr (qemu_plugin_u64 entry)
@@ -173,10 +183,20 @@ static void gen_udata_cond_cb(struct qemu_plugin_conditional_cb *cb)tcg_gen_ld_i64 (val, ptr,0 );tcg_gen_brcondi_i64 (cond, val, cb->imm, after_cb);TCGv_i32 cpu_index =gen_cpu_index (); +enum qemu_plugin_cb_flags cb_flags = +tcg_call_to_qemu_plugin_cb_flags (cb->info->flags); +TCGv_i32 flags =tcg_constant_i32 (cb_flags); +TCGv_i32 clear_flags =tcg_constant_i32 (QEMU_PLUGIN_CB_NO_REGS ); +tcg_gen_st_i32 (flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState));tcg_gen_call2 (cb->f.vcpu_udata, cb->info,NULL ,tcgv_i32_temp (cpu_index),tcgv_ptr_temp (tcg_constant_ptr (cb->userp))); +tcg_gen_st_i32 (clear_flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState));tcg_temp_free_i32 (cpu_index); +tcg_temp_free_i32 (flags); +tcg_temp_free_i32 (clear_flags);gen_set_label (after_cb);tcg_temp_free_i64 (val);
@@ -210,12 +230,22 @@ static void gen_mem_cb(struct qemu_plugin_regular_cb *cb, qemu_plugin_meminfo_t meminfo, TCGv_i64 addr) {TCGv_i32 cpu_index =gen_cpu_index (); +enum qemu_plugin_cb_flags cb_flags = +tcg_call_to_qemu_plugin_cb_flags (cb->info->flags); +TCGv_i32 flags =tcg_constant_i32 (cb_flags); +TCGv_i32 clear_flags =tcg_constant_i32 (QEMU_PLUGIN_CB_NO_REGS ); +tcg_gen_st_i32 (flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState));tcg_gen_call4 (cb->f.vcpu_mem, cb->info,NULL ,tcgv_i32_temp (cpu_index),tcgv_i32_temp (tcg_constant_i32 (meminfo)),tcgv_i64_temp (addr),tcgv_ptr_temp (tcg_constant_ptr (cb->userp))); +tcg_gen_st_i32 (clear_flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState));tcg_temp_free_i32 (cpu_index); +tcg_temp_free_i32 (flags); +tcg_temp_free_i32 (clear_flags); }static void inject_cb (struct qemu_plugin_dyn_cb *cb)
include/hw/core/cpu.h +1 -0
@@ -368,6 +368,7 @@ typedef struct CPUNegativeOffsetState {GArray *plugin_mem_cbs;uint64_t plugin_mem_value_low;uint64_t plugin_mem_value_high; +int32_t plugin_cb_flags;#endif IcountDecr icount_decr;bool can_do_io;
include/qemu/plugin.h +4 -0
@@ -209,6 +209,10 @@ void qemu_plugin_user_prefork_lock(void);*/ void qemu_plugin_user_postfork (bool is_child); +enum qemu_plugin_cb_flags tcg_call_to_qemu_plugin_cb_flags (int flags); + +enum qemu_plugin_cb_flags qemu_plugin_get_cb_flags (void ); +#else /* !CONFIG_PLUGIN */ static inline void qemu_plugin_add_opts (void )
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. */ enum qemu_plugin_cb_flags {QEMU_PLUGIN_CB_NO_REGS ,
plugins/api.c +8 -0
@@ -437,6 +437,10 @@ int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf) {g_assert (current_cpu); +if (qemu_plugin_get_cb_flags () ==QEMU_PLUGIN_CB_NO_REGS ) { +return -1 ; + } +return gdb_read_register (current_cpu, buf,GPOINTER_TO_INT (reg) -1 ); }
@@ -445,6 +449,10 @@ int qemu_plugin_write_register(struct qemu_plugin_register *reg, {g_assert (current_cpu); +if (buf->len ==0 ||qemu_plugin_get_cb_flags () !=QEMU_PLUGIN_CB_RW_REGS ) { +return 0 ; + } +return gdb_write_register (current_cpu, buf->data,GPOINTER_TO_INT (reg) -1 ); }
plugins/core.c +26 -6
@@ -364,14 +364,15 @@ void plugin_register_dyn_cb__udata(GArray **arr,enum qemu_plugin_cb_flags flags, void *udata) { - static TCGHelperInfo info[3] = { + static TCGHelperInfo info[4] = { [QEMU_PLUGIN_CB_NO_REGS ].flags =TCG_CALL_NO_RWG , [QEMU_PLUGIN_CB_R_REGS ].flags =TCG_CALL_NO_WG , + [QEMU_PLUGIN_CB_RW_REGS ].flags =0 ,/* * Match qemu_plugin_vcpu_udata_cb_t: * void (*)(uint32_t, void *) */ - [0 ...2 ].typemask = (dh_typemask (void,0 ) | + [0 ...3 ].typemask = (dh_typemask (void,0 ) |dh_typemask (i32,1 ) |dh_typemask (ptr,2 )) };
@@ -393,14 +394,15 @@ void plugin_register_dyn_cond_cb__udata(GArray **arr,uint64_t imm, void *udata) { - static TCGHelperInfo info[3] = { + static TCGHelperInfo info[4] = { [QEMU_PLUGIN_CB_NO_REGS ].flags =TCG_CALL_NO_RWG , [QEMU_PLUGIN_CB_R_REGS ].flags =TCG_CALL_NO_WG , + [QEMU_PLUGIN_CB_RW_REGS ].flags =0 ,/* * Match qemu_plugin_vcpu_udata_cb_t: * void (*)(uint32_t, void *) */ - [0 ...2 ].typemask = (dh_typemask (void,0 ) | + [0 ...3 ].typemask = (dh_typemask (void,0 ) |dh_typemask (i32,1 ) |dh_typemask (ptr,2 )) };
@@ -431,14 +433,15 @@ void plugin_register_vcpu_mem_cb(GArray **arr, !__builtin_types_compatible_p(qemu_plugin_meminfo_t, uint32_t) && !__builtin_types_compatible_p(qemu_plugin_meminfo_t, int32_t)); -static TCGHelperInfo info[3 ] = { +static TCGHelperInfo info[4 ] = { [QEMU_PLUGIN_CB_NO_REGS ].flags =TCG_CALL_NO_RWG , [QEMU_PLUGIN_CB_R_REGS ].flags =TCG_CALL_NO_WG , + [QEMU_PLUGIN_CB_RW_REGS ].flags =0 ,/* * Match qemu_plugin_vcpu_mem_cb_t: * void (*)(uint32_t, qemu_plugin_meminfo_t, uint64_t, void *) */ - [0 ... 2].typemask = + [0 ... 3].typemask = (dh_typemask(void, 0) | dh_typemask(i32, 1) | (__builtin_types_compatible_p(qemu_plugin_meminfo_t, uint32_t)
@@ -760,3 +763,20 @@ void plugin_scoreboard_free(struct qemu_plugin_scoreboard *score)g_array_free (score->data, TRUE);g_free (score); } + +enum qemu_plugin_cb_flags tcg_call_to_qemu_plugin_cb_flags (int flags) +{ +if (flags &TCG_CALL_NO_RWG ) { +return QEMU_PLUGIN_CB_NO_REGS ; + }else if (flags &TCG_CALL_NO_WG ) { +return QEMU_PLUGIN_CB_R_REGS ; + }else { +return QEMU_PLUGIN_CB_RW_REGS ; + } +} + +enum qemu_plugin_cb_flags qemu_plugin_get_cb_flags (void ) +{ +assert (current_cpu); +return current_cpu->neg.plugin_cb_flags; +}
Signature · 1 line
2.49.0
PATCH v5 4/9 plugins: Add memory virtual address write API
In reply to Rowan HartFrom: novafacing <[email protected]>
This patch adds functions to the plugins API to allow reading and writing memory via virtual addresses. These functions only permit doing so on the current CPU, because there is no way to ensure consistency if plugins are allowed to read or write to other CPUs that aren't currently in the context of the plugin.
--- include/qemu/qemu-plugin.h | 21 +++++++++++++++++++++ plugins/api.c | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+)
include/qemu/qemu-plugin.h +21 -0
@@ -948,6 +948,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
@@ -476,6 +476,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
PATCH v5 5/9 plugins: Add memory hardware address read/write API
In reply to Rowan HartFrom: novafacing <[email protected]>
This patch adds functions to the plugins API to allow plugins to read and write memory via hardware addresses. The functions use the current address space of the current CPU in order to avoid exposing address space information to users. A later patch may want to add a function to permit a specified address space, for example to facilitate architecture-specific plugins that want to operate on them, for example reading ARM secure memory.
--- include/qemu/qemu-plugin.h | 93 ++++++++++++++++++++++++++++++++++++ plugins/api.c | 97 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 190 insertions(+)
include/qemu/qemu-plugin.h +93 -0
@@ -969,6 +969,99 @@ 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 +* +* @addr: The 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 from the current memory space for the current +* vCPU 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 (uint64_t addr,GByteArray *data,size_t len); + +/** +* qemu_plugin_write_memory_hwaddr() - write to memory using a hardware address +* +* @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 in the current address space for the current vCPU. +* +* 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 (uint64_t addr,GByteArray *data); + +/** +* qemu_plugin_translate_vaddr() - translate virtual address for current vCPU +* +* @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 +97 -0
109 lines not on this page · raw message
Signature · 1 line
2.49.0
PATCH v5 6/9 plugins: Add patcher plugin and test
In reply to Rowan HartFrom: novafacing <[email protected]>
This patch adds a plugin that exercises the virtual and hardware memory read-write API functions added in a previous patch. The plugin takes a target and patch byte sequence, and will overwrite any instruction matching the target byte sequence with the patch.
--- tests/tcg/Makefile.target | 1 + tests/tcg/plugins/meson.build | 2 +- tests/tcg/plugins/patch.c | 302 ++++++++++++++++++++++ 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, 402 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 +302 -0
302 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 v5 7/9 plugins: Add hypercalls plugin and test
In reply to Rowan HartFrom: novafacing <[email protected]>
This patch adds a plugin that implements a simple form of hypercalls from guest code to the plugin by using the register read API. It accepts only one hypercall, which writes a magic value to guest memory.
--- 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 v5 8/9 plugins: Remove use of qemu_plugin_read_register where it is not permitted
In reply to Rowan HartThis patch is required to make the insn plugin work after adding enforcement of QEMU_PLUGIN_CB_ flags in calls to read or write registers. Previously, these flags were not enforced and the API could be called from anywhere, but this was not intended as described by the documentation. Now, the flags are enforced and qemu_plugin_read_register can no longer be called from a vcpu_init callback because it does not request the QEMU_PLUGIN_CB_ flag (nor does it have a mechanism to do so).
--- tests/tcg/plugins/insn.c | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-)
tests/tcg/plugins/insn.c +1 -21
@@ -81,25 +81,6 @@ static Instruction * get_insn_record(const char *disas, uint64_t vaddr, Match *mreturn record; } -/* -* Initialise a new vcpu with reading the register list -*/ -static void vcpu_init (qemu_plugin_id_t id,unsigned int vcpu_index) -{ -g_autoptr (GArray) reg_list =qemu_plugin_get_registers (); -g_autoptr (GByteArray) reg_value =g_byte_array_new (); - -if (reg_list) { -for (int i =0 ; i < reg_list->len; i++) { -qemu_plugin_reg_descriptor *rd = &g_array_index ( - reg_list, qemu_plugin_reg_descriptor, i); -int count =qemu_plugin_read_register (rd->handle, reg_value); -g_assert (count >0 ); - } - } -} - -static void vcpu_insn_exec_before (unsigned int cpu_index,void *udata) {qemu_plugin_u64_add (insn_count, cpu_index,1 );
@@ -295,8 +276,7 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, insn_count =qemu_plugin_scoreboard_u64 (qemu_plugin_scoreboard_new (sizeof (uint64_t ))); -/* Register init, translation block and exit callbacks */ -qemu_plugin_register_vcpu_init_cb (id, vcpu_init); +/* Register translation block and exit callbacks */ qemu_plugin_register_vcpu_tb_trans_cb (id, vcpu_tb_trans);qemu_plugin_register_atexit_cb (id, plugin_exit,NULL );return 0 ;
Signature · 1 line
2.49.0
PATCH v5 9/9 plugins: Update plugin version and add notes
In reply to Rowan HartFrom: novafacing <[email protected]>
This patch updates the plugin version to gate new APIs and adds notes describing what has been added.
--- include/qemu/qemu-plugin.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
include/qemu/qemu-plugin.h +8 -1
@@ -65,11 +65,18 @@ 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_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