Plugin support
cover Plugin support
For those of you who need some context: "plugins" are dynamic libraries that are loaded at run-time. These plugins can subscribe to interesting events (e.g. instruction execution) via an API, to then do something interesting with them. This functionality is similar to what other instrumentation tools (e.g. Pin and DynamoRIO) provide, although since QEMU is full-system we have some additional features.
As an example application, I've been using this plugin implementation for the last year or so to implement a parallel computer simulator that uses QEMU as its execution frontend.
The key features of this plugin implementation are:
- Support for an arbitrary number of plugins
- Focus on speed. "Dynamic" callbacks are used for frequent events, such as memory callbacks, to call the plugin code directly, i.e. without going through an intermediate helper. This provides an average 1.33x speedup for SPEC06 over using helpers with a list of subscribers, and it becomes more important as more subscribers are added. I can share more detailed numbers if you want them.
- Instruction-granularity instrumentation. Getting callbacks on *all* TBs/mem accesses/instructions is not flexible. Consider a plugin that just wants to get callbacks on the specific memory accesses of a set of instructions (e.g. cmpxchg); the API must provide a way for the plugin to subscribe to those events *only*, instead of giving it all events (e.g. all mem accesses) for the plugin to then discard 99.9% of them.
- 2-pass translation. Once a "TB translation" callback is called, the plugin must know the span of the TB. We should not force plugins to guess where the TB will end; that is strictly QEMU's job, and can change any time. A TB is thus a sequence of instructions of whatever length the particular QEMU implementation decides. Thus, for each TB, a 3-step process is followed: (1) the plugin layer keeps a copy of the contents of the current TB, (2) once the TB is well-defined, its descriptor and contents are passed to plugins, which then register their desired instrumentation (e.g. "call me back on this particular instruction", or "call me back when the whole TB executes"); note that plugins can use a disassembler like capstone to decide what to do with each instruction; they can also allocate memory and then get a pointer to it passed back from the callbacks. And finally, (3) the target translator is called again to generate the final instrumented translated TB. This is what I called the "2-pass translation", since we go twice over the translation loop in translator.c. Note that the 2-pass approach has virtually no overhead (0.40% for SPEC06int); translation is much cheaper than execution. But anyway, if no plugins have subscribed to TB translation, we only do one pass.
- Support for inlining instrumentation. This is done via an explicit API, i.e. we do not export TCG ops, which are internal to QEMU. For now, I just have support for incrementing a u64 with an immediate, e.g. to increment a counter.
- Treating the plugins as "malicious", in that we don't export any pointers to key QEMU data structures (CPUState, TB). I implemented this after a comment from Stefan, but maybe it is a bit overkill.
- Other features that go beyond passively getting callbacks (I need these for the simulator):
+ Control of the virtual clock from plugins
+ CPU lockstep execution, where plugins decide when CPUs must synchronize to reduce their execution skew. This can be understood as a "parallel icount" mode, although plugins can decide to synchronize whenever they want, not whenever a certain amount of instructions have execution. For instance, I am using this to synchronize CPUs every X number of simulated cycles, thereby having the ability to limit skew while maintaining parallelism. When a CPU is idle, then we assume its "execution window" (aka "time slice") has expired.
+ Guest hooks. Instead of using "magic" instructions, export a PCI device and let plugins determine what encoding to follow. I'm using this to mark regions of interest in guest programs, so that in the simulator I start/stop recording simulation events.
- Things I haven't included here:
+ Ability to emulate devices from plugins. I'm using this to simulate peripherals. These are devices whose timing is important to overall performance (e.g. 'accelerators' to which the main CPU offloads computation, e.g. a JPEG encoder).
The design I'm showing here shares nothing with the tracing infrastructure. While it is true that some features (e.g. syscall callbacks) are identical, some others (instruction-granularity instrumentation, 2-pass translation, lockstep execution) are not. So I'm open to discussing where we could save code (e.g. having a single trace+plugin generator, e.g. for syscalls), as long as performance and/or the ability to instrument aren't compromise.
Peter: I remember you asked for an API first. I am including that as a single patch in patch 14; see also patches 40, 45 and 47.
The first 10 or so patches in the series are preliminary work, including the support of runtime TCG helpers. I think a subset of this could be in a proper patch series, particularly the xxhash patches. Then I've added plugin-related patches, trying to break this down my original 80-or-so patches into something a little easier to review. The "core" plugin code is perhaps the last place to look, because when it is added nothing is calling it yet. The last patch in the series adds some example plugins just for discussion's sake.
This series applies on top of my cpu-lock-v4 series. You can fetch it from:
https://github.com/cota/qemu/tree/plugin
Cheers,
Emilio
RFC 1/48 cpu: introduce run_on_cpu_no_bql
This allows us to queue synchronous CPU work without the BQL.
Will gain a user soon.
--- include/qom/cpu.h | 13 +++++++++++++ cpus-common.c | 28 ++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 6 deletions(-)
include/qom/cpu.h +13 -0
@@ -877,6 +877,19 @@ bool cpu_is_stopped(CPUState *cpu);*/ void run_on_cpu (CPUState *cpu,run_on_cpu_func func,run_on_cpu_data data); +/** +* run_on_cpu_no_bql +* @cpu: The vCPU to run on. +* @func: The function to be executed. +* @data: Data to pass to the function. +* +* Schedules the function @func for execution on the vCPU @cpu. +* This function is run outside the BQL. +* See also: run_on_cpu() +*/ +void run_on_cpu_no_bql (CPUState *cpu,run_on_cpu_func func, +run_on_cpu_data data); +/** * async_run_on_cpu: * @cpu: The vCPU to run on.
cpus-common.c +22 -6
@@ -144,7 +144,8 @@ static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi)cpu_mutex_unlock (cpu); } -void run_on_cpu (CPUState *cpu,run_on_cpu_func func,run_on_cpu_data data) +static void do_run_on_cpu (CPUState *cpu,run_on_cpu_func func, +run_on_cpu_data data,bool bql) {struct qemu_work_item wi;bool has_bql =qemu_mutex_iothread_locked ();
@@ -152,12 +153,16 @@ void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)g_assert (no_cpu_mutex_locked ());if (qemu_cpu_is_self (cpu)) { -if (has_bql) { -func (cpu, data); +if (bql) { +if (has_bql) { +func (cpu, data); + }else { +qemu_mutex_lock_iothread (); +func (cpu, data); +qemu_mutex_unlock_iothread (); + } }else { -qemu_mutex_lock_iothread ();func (cpu, data); -qemu_mutex_unlock_iothread (); }return ; }
@@ -172,7 +177,7 @@ void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data) wi.done = false; wi.free = false; wi.exclusive = false; - wi.bql = true; + wi.bql = bql;cpu_mutex_lock (cpu);queue_work_on_cpu_locked (cpu, &wi);
@@ -189,6 +194,17 @@ void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data) } } +void run_on_cpu (CPUState *cpu,run_on_cpu_func func,run_on_cpu_data data) +{ +do_run_on_cpu (cpu, func, data, true); +} + +void +run_on_cpu_no_bql (CPUState *cpu,run_on_cpu_func func,run_on_cpu_data data) +{ +do_run_on_cpu (cpu, func, data, false); +} +void async_run_on_cpu (CPUState *cpu,run_on_cpu_func func,run_on_cpu_data data) {struct qemu_work_item *wi;
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 01/48] cpu: introduce run_on_cpu_no_bql
Quoted 3 lines · Emilio G. Cota
This allows us to queue synchronous CPU work without the BQL.
Will gain a user soon.
This is also in the cpu-lock series right?
Quoted 83 lines
Signed-off-by Emilio G. Cota <[email protected]>--- include/qom/cpu.h | 13 +++++++++++++ cpus-common.c | 28 ++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 6 deletions(-)include/qom/cpu.h +13
-0@@ -877,6 +877,19 @@ bool cpu_is_stopped(CPUState *cpu);*/ void run_on_cpu (CPUState *cpu,run_on_cpu_func func,run_on_cpu_data data); +/** +* run_on_cpu_no_bql +* @cpu: The vCPU to run on. +* @func: The function to be executed. +* @data: Data to pass to the function. +* +* Schedules the function @func for execution on the vCPU @cpu. +* This function is run outside the BQL. +* See also: run_on_cpu() +*/ +void run_on_cpu_no_bql (CPUState *cpu,run_on_cpu_func func, +run_on_cpu_data data); +/** * async_run_on_cpu: * @cpu: The vCPU to run on. cpus-common.c +15
-6@@ -144,7 +144,8 @@ static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi)cpu_mutex_unlock (cpu); } -void run_on_cpu (CPUState *cpu,run_on_cpu_func func,run_on_cpu_data data) +static void do_run_on_cpu (CPUState *cpu,run_on_cpu_func func, +run_on_cpu_data data,bool bql) {struct qemu_work_item wi;bool has_bql =qemu_mutex_iothread_locked ();@@ -152,12 +153,16 @@ void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)g_assert (no_cpu_mutex_locked ());if (qemu_cpu_is_self (cpu)) { -if (has_bql) { -func (cpu, data); +if (bql) { +if (has_bql) { +func (cpu, data); + }else { +qemu_mutex_lock_iothread (); +func (cpu, data); +qemu_mutex_unlock_iothread (); + } }else { -qemu_mutex_lock_iothread ();func (cpu, data); -qemu_mutex_unlock_iothread (); }return ; }@@ -172,7 +177,7 @@ void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data) wi.done = false; wi.free = false; wi.exclusive = false; - wi.bql = true; + wi.bql = bql;cpu_mutex_lock (cpu);queue_work_on_cpu_locked (cpu, &wi);@@ -189,6 +194,17 @@ void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data) } } +void run_on_cpu (CPUState *cpu,run_on_cpu_func func,run_on_cpu_data data) +{ +do_run_on_cpu (cpu, func, data, true); +}
cpus-common.c void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data) · line 210
Quoted 10 lines
cpus-common.c +7
-0+ +void +run_on_cpu_no_bql (CPUState *cpu,run_on_cpu_func func,run_on_cpu_data data) +{ +do_run_on_cpu (cpu, func, data, false); +} +void async_run_on_cpu (CPUState *cpu,run_on_cpu_func func,run_on_cpu_data data) {struct qemu_work_item *wi;
--
Alex Bennée
Re: [Qemu-devel] [RFC 01/48] cpu: introduce run_on_cpu_no_bql
Quoted 8 lines · Alex Bennée
Quoted 3 lines · Emilio G. Cota
This allows us to queue synchronous CPU work without the BQL.
Will gain a user soon.
This is also in the cpu-lock series right?
No, in the cpu-lock series we add async_run_on_cpu_no_bql; here we add the sync version.
E.
Alex Bennée
Reviewed-byRe: [Qemu-devel] [RFC 01/48] cpu: introduce run_on_cpu_no_bql
Quoted 4 lines · Emilio G. Cota
Quoted 3 lines · Alex Bennée
Emilio G. Cota <[email protected]> writes:
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
This allows us to queue synchronous CPU work without the BQL.
Will gain a user soon.
This is also in the cpu-lock series right?
No, in the cpu-lock series we add
async_run_on_cpu_no_bql; here we add the sync version.
My mistake, being word blind:
--
Alex Bennée
RFC 2/48 trace: expand mem_info:size_shift to 3 bits
In reply to Emilio G. CotaThis will allow us to trace 16B-long memory accesses.
While at it, add some defines for the mem_info bits and simplify trace_mem_get_info by making it a wrapper around trace_mem_build_info.
--- trace-events | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
trace-events +1 -1
@@ -151,7 +151,7 @@ vcpu guest_cpu_reset(void) # Access information can be parsed as: # # struct mem_info { -# uint8_t size_shift : 2; /* interpreted as "1 << size_shift" bytes */ +# uint8_t size_shift : 3; /* interpreted as "1 << size_shift" bytes */ # bool sign_extend: 1; /* sign-extended */ # uint8_t endianness : 1; /* 0: little, 1: big */ # bool store : 1; /* wheter it's a store operation */
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 02/48] trace: expand mem_info:size_shift to 3 bits
Quoted 6 lines · Emilio G. Cota
This will allow us to trace 16B-long memory accesses.
While at it, add some defines for the
mem_infobits and simplifytrace_mem_get_infoby making it a wrapper aroundtrace_mem_build_info.Signed-off-by Emilio G. Cota <[email protected]>
Currently we ignore atomic operation but I assume we'll want to represent them at some point here. I don't know if memory barrier behaviour would also be worth exporting?
Quoted 9 lines
--- trace-events | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)trace-events +0
-0
trace-events vcpu guest_cpu_reset(void) · line 157
Quoted 8 lines
trace-events +1
-1@@ -151,7 +151,7 @@ vcpu guest_cpu_reset(void) # Access information can be parsed as: # # struct mem_info { -# uint8_t size_shift : 2; /* interpreted as "1 << size_shift" bytes */ +# uint8_t size_shift : 3; /* interpreted as "1 << size_shift" bytes */ # bool sign_extend: 1; /* sign-extended */ # uint8_t endianness : 1; /* 0: little, 1: big */ # bool store : 1; /* wheter it's a store operation */
--
Alex Bennée
Re: [Qemu-devel] [RFC 02/48] trace: expand mem_info:size_shift to 3 bits
Quoted 5 lines · Alex Bennée
Quoted 2 lines · Emilio G. Cota
This will allow us to trace 16B-long memory accesses.
Quoted 7 lines
Quoted 4 lines
While at it, add some defines for the
mem_infobits and simplifytrace_mem_get_infoby making it a wrapper aroundtrace_mem_build_info.Signed-off-by Emilio G. Cota <[email protected]>Currently we ignore atomic operation but I assume we'll want to represent them at some point here.
We "ignore" them in that we just trace them without marking them as atomic. We do trace them since d071f4cd55 ("trace: enable tracing of TCG atomics", 2018-06-27), which BTW is a spin-off of early iterations of this series.
Quoted 1 line
I don't know if memory barrier behaviour would also be worth exporting?
I don't see much value in that from a tracing viewpoint; if users need such instruction-specific details they can just use a plugin, where they can disassemble TB's.
Thanks,
E.
RFC 3/48 tcg/README: fix typo s/afterwise/afterwards/
In reply to Emilio G. CotaAfterwise is "wise after the fact", as in "hindsight". Here we meant "afterwards" (as in "subsequently"). Fix it.
--- tcg/README | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tcg/README +1 -1
@@ -101,7 +101,7 @@ This can be overridden using the following function modifiers: canonical locations before calling the helper. - TCG_CALL_NO_WRITE_GLOBALS means that the helper does not modify any globals. They will only be saved to their canonical location before calling helpers, - but they won't be reloaded afterwise. + but they won't be reloaded afterwards. - TCG_CALL_NO_SIDE_EFFECTS means that the call to the function is removed if the return value is not used.
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 03/48] tcg/README: fix typo s/afterwise/afterwards/
Quoted 4 lines · Emilio G. Cota
Afterwise is "wise after the fact", as in "hindsight". Here we meant "afterwards" (as in "subsequently"). Fix it.
Signed-off-by Emilio G. Cota <[email protected]>
Quoted 9 lines
--- tcg/README | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tcg/README +0
-0
tcg/README This can be overridden using the following function modifiers: · line 106
Quoted 7 lines
tcg/README +1
-1@@ -101,7 +101,7 @@ This can be overridden using the following function modifiers: canonical locations before calling the helper. - TCG_CALL_NO_WRITE_GLOBALS means that the helper does not modify any globals. They will only be saved to their canonical location before calling helpers, - but they won't be reloaded afterwise. + but they won't be reloaded afterwards. - TCG_CALL_NO_SIDE_EFFECTS means that the call to the function is removed if the return value is not used.
--
Alex Bennée
RFC 4/48 exec: introduce qemu_xxhash{2,4,5,6,7}
In reply to Emilio G. CotaBefore moving them all to include/qemu/xxhash.h.
--- include/exec/tb-hash-xx.h | 41 +++++++++++++++++++++++++++++---------- include/exec/tb-hash.h | 2 +- tests/qht-bench.c | 2 +- util/qsp.c | 12 ++++++------ 4 files changed, 39 insertions(+), 18 deletions(-)
include/exec/tb-hash-xx.h +31 -10
@@ -42,23 +42,23 @@#define PRIME32_4 668265263U#define PRIME32_5 374761393U -#define TB_HASH_XX_SEED 1 +#define QEMU_XXHASH_SEED 1/* * xxhash32, customized for input variables that are not guaranteed to be * contiguous in memory. */ static inline uint32_t -tb_hash_func7 (uint64_t a0,uint64_t b0,uint32_t e,uint32_t f,uint32_t g) +qemu_xxhash7 (uint64_t ab,uint64_t cd,uint32_t e,uint32_t f,uint32_t g) { -uint32_t v1 =TB_HASH_XX_SEED +PRIME32_1 +PRIME32_2 ; -uint32_t v2 =TB_HASH_XX_SEED +PRIME32_2 ; -uint32_t v3 =TB_HASH_XX_SEED +0 ; -uint32_t v4 =TB_HASH_XX_SEED -PRIME32_1 ; -uint32_t a = a0 >>32 ; -uint32_t b = a0; -uint32_t c = b0 >>32 ; -uint32_t d = b0; +uint32_t v1 =QEMU_XXHASH_SEED +PRIME32_1 +PRIME32_2 ; +uint32_t v2 =QEMU_XXHASH_SEED +PRIME32_2 ; +uint32_t v3 =QEMU_XXHASH_SEED +0 ; +uint32_t v4 =QEMU_XXHASH_SEED -PRIME32_1 ; +uint32_t a = ab >>32 ; +uint32_t b = ab; +uint32_t c = cd >>32 ; +uint32_t d = cd;uint32_t h32; v1 += a *PRIME32_2 ;
@@ -98,4 +98,25 @@ tb_hash_func7(uint64_t a0, uint64_t b0, uint32_t e, uint32_t f, uint32_t g) return h32; } +static inline uint32_t qemu_xxhash2 (uint64_t ab) +{ +return qemu_xxhash7 (ab,0 ,0 ,0 ,0 ); +} + +static inline uint32_t qemu_xxhash4 (uint64_t ab,uint64_t cd) +{ +return qemu_xxhash7 (ab, cd,0 ,0 ,0 ); +} + +static inline uint32_t qemu_xxhash5 (uint64_t ab,uint64_t cd,uint32_t e) +{ +return qemu_xxhash7 (ab, cd, e,0 ,0 ); +} + +static inline uint32_t qemu_xxhash6 (uint64_t ab,uint64_t cd,uint32_t e, +uint32_t f) +{ +return qemu_xxhash7 (ab, cd, e, f,0 ); +} +#endif /* EXEC_TB_HASH_XX_H */
include/exec/tb-hash.h +1 -1
@@ -61,7 +61,7 @@ static inlineuint32_t tb_hash_func (tb_page_addr_t phys_pc,target_ulong pc,uint32_t flags,uint32_t cf_mask,uint32_t trace_vcpu_dstate) { -return tb_hash_func7 (phys_pc, pc, flags, cf_mask, trace_vcpu_dstate); +return qemu_xxhash7 (phys_pc, pc, flags, cf_mask, trace_vcpu_dstate); }#endif
tests/qht-bench.c +1 -1
@@ -104,7 +104,7 @@ static bool is_equal(const void *ap, const void *bp)static uint32_t h (unsigned long v) { -return tb_hash_func7 (v,0 ,0 ,0 ,0 ); +return qemu_xxhash2 (v); }static uint32_t hval (unsigned long v)
util/qsp.c +6 -6
@@ -135,13 +135,13 @@ QemuCondWaitFunc qemu_cond_wait_func = qemu_cond_wait_impl;* without it we still get a pretty unique hash. */ static inline -uint32_t do_qsp_callsite_hash (const QSPCallSite *callsite,uint64_t a) +uint32_t do_qsp_callsite_hash (const QSPCallSite *callsite,uint64_t ab) { -uint64_t b = (uint64_t )(uintptr_t )callsite->obj; +uint64_t cd = (uint64_t )(uintptr_t )callsite->obj;uint32_t e = callsite->line;uint32_t f = callsite->type; -return tb_hash_func7 (a, b, e, f,0 ); +return qemu_xxhash6 (ab, cd, e, f); } static inline
@@ -169,11 +169,11 @@ static uint32_t qsp_entry_no_thread_hash(const QSPEntry *entry)static uint32_t qsp_entry_no_thread_obj_hash (const QSPEntry *entry) {const QSPCallSite *callsite = entry->callsite; -uint64_t a =g_str_hash (callsite->file); -uint64_t b = callsite->line; +uint64_t ab =g_str_hash (callsite->file); +uint64_t cd = callsite->line;uint32_t e = callsite->type; -return tb_hash_func7 (a, b, e,0 ,0 ); +return qemu_xxhash5 (ab, cd, e); }static bool qsp_callsite_cmp (const void *ap,const void *bp)
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 04/48] exec: introduce qemu_xxhash{2, 4, 5, 6, 7}
Quoted 3 lines · Emilio G. Cota
Before moving them all to
include/qemu/xxhash.h.Signed-off-by Emilio G. Cota <[email protected]>
Quoted 123 lines
--- include/exec/tb-hash-xx.h | 41 +++++++++++++++++++++++++++++---------- include/exec/tb-hash.h | 2 +- tests/qht-bench.c | 2 +- util/qsp.c | 12 ++++++------ 4 files changed, 39 insertions(+), 18 deletions(-)include/exec/tb-hash-xx.h +31
-10@@ -42,23 +42,23 @@#define PRIME32_4 668265263U#define PRIME32_5 374761393U -#define TB_HASH_XX_SEED 1 +#define QEMU_XXHASH_SEED 1/* * xxhash32, customized for input variables that are not guaranteed to be * contiguous in memory. */ static inline uint32_t -tb_hash_func7 (uint64_t a0,uint64_t b0,uint32_t e,uint32_t f,uint32_t g) +qemu_xxhash7 (uint64_t ab,uint64_t cd,uint32_t e,uint32_t f,uint32_t g) { -uint32_t v1 =TB_HASH_XX_SEED +PRIME32_1 +PRIME32_2 ; -uint32_t v2 =TB_HASH_XX_SEED +PRIME32_2 ; -uint32_t v3 =TB_HASH_XX_SEED +0 ; -uint32_t v4 =TB_HASH_XX_SEED -PRIME32_1 ; -uint32_t a = a0 >>32 ; -uint32_t b = a0; -uint32_t c = b0 >>32 ; -uint32_t d = b0; +uint32_t v1 =QEMU_XXHASH_SEED +PRIME32_1 +PRIME32_2 ; +uint32_t v2 =QEMU_XXHASH_SEED +PRIME32_2 ; +uint32_t v3 =QEMU_XXHASH_SEED +0 ; +uint32_t v4 =QEMU_XXHASH_SEED -PRIME32_1 ; +uint32_t a = ab >>32 ; +uint32_t b = ab; +uint32_t c = cd >>32 ; +uint32_t d = cd;uint32_t h32; v1 += a *PRIME32_2 ;@@ -98,4 +98,25 @@ tb_hash_func7(uint64_t a0, uint64_t b0, uint32_t e, uint32_t f, uint32_t g) return h32; } +static inline uint32_t qemu_xxhash2 (uint64_t ab) +{ +return qemu_xxhash7 (ab,0 ,0 ,0 ,0 ); +} + +static inline uint32_t qemu_xxhash4 (uint64_t ab,uint64_t cd) +{ +return qemu_xxhash7 (ab, cd,0 ,0 ,0 ); +} + +static inline uint32_t qemu_xxhash5 (uint64_t ab,uint64_t cd,uint32_t e) +{ +return qemu_xxhash7 (ab, cd, e,0 ,0 ); +} + +static inline uint32_t qemu_xxhash6 (uint64_t ab,uint64_t cd,uint32_t e, +uint32_t f) +{ +return qemu_xxhash7 (ab, cd, e, f,0 ); +} +#endif /* EXEC_TB_HASH_XX_H */ include/exec/tb-hash.h +1
-1@@ -61,7 +61,7 @@ static inlineuint32_t tb_hash_func (tb_page_addr_t phys_pc,target_ulong pc,uint32_t flags,uint32_t cf_mask,uint32_t trace_vcpu_dstate) { -return tb_hash_func7 (phys_pc, pc, flags, cf_mask, trace_vcpu_dstate); +return qemu_xxhash7 (phys_pc, pc, flags, cf_mask, trace_vcpu_dstate); }#endif tests/qht-bench.c +1
-1@@ -104,7 +104,7 @@ static bool is_equal(const void *ap, const void *bp)static uint32_t h (unsigned long v) { -return tb_hash_func7 (v,0 ,0 ,0 ,0 ); +return qemu_xxhash2 (v); }static uint32_t hval (unsigned long v)util/qsp.c +3
-4@@ -135,13 +135,13 @@ QemuCondWaitFunc qemu_cond_wait_func = qemu_cond_wait_impl;* without it we still get a pretty unique hash. */ static inline -uint32_t do_qsp_callsite_hash (const QSPCallSite *callsite,uint64_t a) +uint32_t do_qsp_callsite_hash (const QSPCallSite *callsite,uint64_t ab) { -uint64_t b = (uint64_t )(uintptr_t )callsite->obj; +uint64_t cd = (uint64_t )(uintptr_t )callsite->obj;uint32_t e = callsite->line;uint32_t f = callsite->type; -return tb_hash_func7 (a, b, e, f,0 ); +return qemu_xxhash6 (ab, cd, e, f); } static inline@@ -169,11 +169,11 @@ static uint32_t qsp_entry_no_thread_hash(const QSPEntry *entry)static uint32_t qsp_entry_no_thread_obj_hash (const QSPEntry *entry) {const QSPCallSite *callsite = entry->callsite; -uint64_t a =g_str_hash (callsite->file);
util/qsp.c static uint32_t qsp_entry_no_thread_hash(const QSPEntry *entry) · line 179
Quoted 10 lines
util/qsp.c +3
-2-uint64_t b = callsite->line; +uint64_t ab =g_str_hash (callsite->file); +uint64_t cd = callsite->line;uint32_t e = callsite->type; -return tb_hash_func7 (a, b, e,0 ,0 ); +return qemu_xxhash5 (ab, cd, e); }static bool qsp_callsite_cmp (const void *ap,const void *bp)
--
Alex Bennée
RFC 5/48 include: move exec/tb-hash-xx.h to qemu/xxhash.h
In reply to Emilio G. Cota---
include/exec/tb-hash.h | 2 +-
include/{exec/tb-hash-xx.h => qemu/xxhash.h} | 6 +++---
tests/qht-bench.c | 2 +-
util/qsp.c | 2 +-
4 files changed, 6 insertions(+), 6 deletions(-)
rename include/{exec/tb-hash-xx.h => qemu/xxhash.h} (97%)include/exec/tb-hash.h +1 -1
@@ -20,7 +20,7 @@#ifndef EXEC_TB_HASH_H #define EXEC_TB_HASH_H -#include "exec/tb-hash-xx.h" +#include "qemu/xxhash.h" #ifdef CONFIG_SOFTMMU
include/qemu/xxhash.h renamed from include/exec/tb-hash-xx.h +3 -3
@@ -31,8 +31,8 @@* - xxHash source repository : https://github.com/Cyan4973/xxHash */ -#ifndef EXEC_TB_HASH_XX_H -#define EXEC_TB_HASH_XX_H +#ifndef QEMU_XXHASH_H +#define QEMU_XXHASH_H #include "qemu/bitops.h"
@@ -119,4 +119,4 @@ static inline uint32_t qemu_xxhash6(uint64_t ab, uint64_t cd, uint32_t e, return qemu_xxhash7(ab, cd, e, f, 0); } -#endif /* EXEC_TB_HASH_XX_H */ +#endif /* QEMU_XXHASH_H */
tests/qht-bench.c +1 -1
@@ -9,7 +9,7 @@#include "qemu/atomic.h" #include "qemu/qht.h" #include "qemu/rcu.h" -#include "exec/tb-hash-xx.h" +#include "qemu/xxhash.h" struct thread_stats {size_t rd;
util/qsp.c +1 -1
@@ -61,7 +61,7 @@#include "qemu/timer.h" #include "qemu/qht.h" #include "qemu/rcu.h" -#include "exec/tb-hash-xx.h" +#include "qemu/xxhash.h" enum QSPType {QSP_MUTEX ,
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 05/48] include: move exec/tb-hash-xx.h to qemu/xxhash.h
Quoted 1 line · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>
Quoted 63 lines
--- include/exec/tb-hash.h | 2 +- include/{exec/tb-hash-xx.h => qemu/xxhash.h} | 6 +++--- tests/qht-bench.c | 2 +- util/qsp.c | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) rename include/{exec/tb-hash-xx.h => qemu/xxhash.h} (97%)include/exec/tb-hash.h +1
-1@@ -20,7 +20,7 @@#ifndef EXEC_TB_HASH_H #define EXEC_TB_HASH_H -#include "exec/tb-hash-xx.h" +#include "qemu/xxhash.h" #ifdef CONFIG_SOFTMMU include/qemu/xxhash.h renamed from include/exec/tb-hash-xx.h +3
-3@@ -31,8 +31,8 @@* - xxHash source repository : https://github.com/Cyan4973/xxHash */ -#ifndef EXEC_TB_HASH_XX_H -#define EXEC_TB_HASH_XX_H +#ifndef QEMU_XXHASH_H +#define QEMU_XXHASH_H #include "qemu/bitops.h" @@ -119,4 +119,4 @@ static inline uint32_t qemu_xxhash6(uint64_t ab, uint64_t cd, uint32_t e, return qemu_xxhash7(ab, cd, e, f, 0); } -#endif /* EXEC_TB_HASH_XX_H */ +#endif /* QEMU_XXHASH_H */ tests/qht-bench.c +1
-1@@ -9,7 +9,7 @@#include "qemu/atomic.h" #include "qemu/qht.h" #include "qemu/rcu.h" -#include "exec/tb-hash-xx.h" +#include "qemu/xxhash.h" struct thread_stats {size_t rd;util/qsp.c +0
-0
util/qsp.c line 67
Quoted 8 lines
util/qsp.c +1
-1@@ -61,7 +61,7 @@#include "qemu/timer.h" #include "qemu/qht.h" #include "qemu/rcu.h" -#include "exec/tb-hash-xx.h" +#include "qemu/xxhash.h" enum QSPType {QSP_MUTEX ,
--
Alex Bennée
RFC 6/48 tcg: use QHT for helper_table
In reply to Emilio G. CotaThis will allow us to add TCG helpers at run-time.
While at it, rename tcg_find_helper to tcg_helper_find for consistency with the added tcg_helper_foo functions.
--- tcg/tcg.c | 59 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 10 deletions(-)
tcg/tcg.c +49 -10
@@ -33,6 +33,7 @@#include "qemu/error-report.h" #include "qemu/cutils.h" #include "qemu/host-utils.h" +#include "qemu/xxhash.h" #include "qemu/timer.h" /* Note: the long term plan is to reduce the dependencies on the QEMU
@@ -879,13 +880,46 @@ typedef struct TCGHelperInfo {static const TCGHelperInfo all_helpers[] = { #include "exec/helper-tcg.h" }; -static GHashTable *helper_table; +static struct qht helper_table; +static bool helper_table_inited;static int indirect_reg_alloc_order[ARRAY_SIZE (tcg_target_reg_alloc_order)];static void process_op_defs (TCGContext *s);static TCGTemp *tcg_global_reg_new_internal (TCGContext *s,TCGType type,TCGReg reg,const char *name); +static inline uint32_t tcg_helper_func_hash (const void *func) +{ +return qemu_xxhash2 ((uint64_t )func); +} + +static bool tcg_helper_cmp (const void *ap,const void *bp) +{ +const TCGHelperInfo *a = ap; +const TCGHelperInfo *b = bp; + +return a->func == b->func && + a->flags == b->flags && + a->sizemask == b->sizemask && + !strcmp (a->name, b->name); +} + +static bool tcg_helper_lookup_cmp (const void *obj,const void *func) +{ +const TCGHelperInfo *info = obj; + +return info->func == func; +} + +static void tcg_helper_insert (const TCGHelperInfo *info) +{ +uint32_t hash =tcg_helper_func_hash (info->func); +bool inserted; + + inserted =qht_insert (&helper_table, (void *)info, hash,NULL ); +g_assert (inserted); +} +void tcg_context_init (TCGContext *s) {int op, total_args, n, i;
@@ -919,13 +953,13 @@ void tcg_context_init(TCGContext *s) }/* Register helpers. */ -/* Use g_direct_hash/equal for direct pointer comparisons on func. */ - helper_table =g_hash_table_new (NULL ,NULL ); +qht_init (&helper_table, tcg_helper_cmp,ARRAY_SIZE (all_helpers), +QHT_MODE_AUTO_RESIZE );for (i =0 ; i <ARRAY_SIZE (all_helpers); ++i) { -g_hash_table_insert (helper_table, (gpointer )all_helpers[i].func, - (gpointer)&all_helpers[i]); +tcg_helper_insert (&all_helpers[i]); } + helper_table_inited = true;tcg_target_init (s);process_op_defs (s);
@@ -1620,9 +1654,10 @@ void tcg_gen_callN(void *func, TCGTemp *ret, int nargs, TCGTemp **args)int i, real_args, nb_rets, pi;unsigned sizemask, flags;TCGHelperInfo *info; +uint32_t hash =tcg_helper_func_hash (func);TCGOp *op; - info =g_hash_table_lookup (helper_table, (gpointer )func); + info =qht_lookup_custom (&helper_table, func, hash, tcg_helper_lookup_cmp); flags = info->flags; sizemask = info->sizemask;
@@ -1825,11 +1860,15 @@ static char *tcg_get_arg_str(TCGContext *s, char *buf, }/* Find helper name. */ -static inline const char *tcg_find_helper (TCGContext *s,uintptr_t val) +static inline const char *tcg_helper_find (TCGContext *s,uintptr_t val) {const char *ret =NULL ; -if (helper_table) { -TCGHelperInfo *info =g_hash_table_lookup (helper_table, (gpointer )val); +if (helper_table_inited) { +uint32_t hash =tcg_helper_func_hash ((void *)val); +TCGHelperInfo *info; + + info =qht_lookup_custom (&helper_table, (void *)val, hash, + tcg_helper_lookup_cmp);if (info) { ret = info->name; }
@@ -1919,7 +1958,7 @@ void tcg_dump_ops(TCGContext *s)/* function name, flags, out args */ col +=qemu_log (" %s %s,$0x%" TCG_PRIlx",$%d" , def->name, -tcg_find_helper (s, op->args[nb_oargs + nb_iargs]), +tcg_helper_find (s, op->args[nb_oargs + nb_iargs]), op->args[nb_oargs + nb_iargs +1 ], nb_oargs);for (i =0 ; i < nb_oargs; i++) { col += qemu_log(",%s", tcg_get_arg_str(s, buf, sizeof(buf),
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 06/48] tcg: use QHT for helper_table
Quoted 23 lines · Emilio G. Cota
This will allow us to add TCG helpers at run-time.
While at it, rename
tcg_find_helpertotcg_helper_findfor consistency with the addedtcg_helper_foofunctions.Signed-off-by Emilio G. Cota <[email protected]>--- tcg/tcg.c | 59 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 10 deletions(-)tcg/tcg.c +1
-0@@ -33,6 +33,7 @@#include "qemu/error-report.h" #include "qemu/cutils.h" #include "qemu/host-utils.h" +#include "qemu/xxhash.h" #include "qemu/timer.h" /* Note: the long term plan is to reduce the dependencies on the QEMU
tcg/tcg.c typedef struct TCGHelperInfo { · line 884
Quoted 6 lines
tcg/tcg.c +2
-1@@ -879,13 +880,46 @@ typedef struct TCGHelperInfo {static const TCGHelperInfo all_helpers[] = { #include "exec/helper-tcg.h" }; -static GHashTable *helper_table; +static struct qht helper_table; +static bool helper_table_inited;
Having a flag for initialisation seems a little excessive considering we've moved that initialisation into tcg_context_init() which has to be called before we do anything TCG related.
Quoted 45 lines
tcg/tcg.c +32
-1static int indirect_reg_alloc_order[ARRAY_SIZE (tcg_target_reg_alloc_order)];static void process_op_defs (TCGContext *s);static TCGTemp *tcg_global_reg_new_internal (TCGContext *s,TCGType type,TCGReg reg,const char *name); +static inline uint32_t tcg_helper_func_hash (const void *func) +{ +return qemu_xxhash2 ((uint64_t )func); +} + +static bool tcg_helper_cmp (const void *ap,const void *bp) +{ +const TCGHelperInfo *a = ap; +const TCGHelperInfo *b = bp; + +return a->func == b->func && + a->flags == b->flags && + a->sizemask == b->sizemask && + !strcmp (a->name, b->name); +} + +static bool tcg_helper_lookup_cmp (const void *obj,const void *func) +{ +const TCGHelperInfo *info = obj; + +return info->func == func; +} + +static void tcg_helper_insert (const TCGHelperInfo *info) +{ +uint32_t hash =tcg_helper_func_hash (info->func); +bool inserted; + + inserted =qht_insert (&helper_table, (void *)info, hash,NULL ); +g_assert (inserted); +} +void tcg_context_init (TCGContext *s) {int op, total_args, n, i;@@ -919,13 +953,13 @@ void tcg_context_init(TCGContext *s) }/* Register helpers. */ -/* Use g_direct_hash/equal for direct pointer comparisons on func. */
tcg/tcg.c void tcg_context_init(TCGContext *s) · line 962
Quoted 10 lines
tcg/tcg.c +4
-3- helper_table =g_hash_table_new (NULL ,NULL ); +qht_init (&helper_table, tcg_helper_cmp,ARRAY_SIZE (all_helpers), +QHT_MODE_AUTO_RESIZE );for (i =0 ; i <ARRAY_SIZE (all_helpers); ++i) { -g_hash_table_insert (helper_table, (gpointer )all_helpers[i].func, - (gpointer)&all_helpers[i]); +tcg_helper_insert (&all_helpers[i]); } + helper_table_inited = true;
so I think we can drop this and...
Quoted 16 lines
tcg/tcg.c +2
-1tcg_target_init (s);process_op_defs (s);@@ -1620,9 +1654,10 @@ void tcg_gen_callN(void *func, TCGTemp *ret, int nargs, TCGTemp **args)int i, real_args, nb_rets, pi;unsigned sizemask, flags;TCGHelperInfo *info; +uint32_t hash =tcg_helper_func_hash (func);TCGOp *op; - info =g_hash_table_lookup (helper_table, (gpointer )func); + info =qht_lookup_custom (&helper_table, func, hash, tcg_helper_lookup_cmp); flags = info->flags; sizemask = info->sizemask;
tcg/tcg.c static char *tcg_get_arg_str(TCGContext *s, char *buf, · line 1866
Quoted 10 lines
tcg/tcg.c +2
-3@@ -1825,11 +1860,15 @@ static char *tcg_get_arg_str(TCGContext *s, char *buf, }/* Find helper name. */ -static inline const char *tcg_find_helper (TCGContext *s,uintptr_t val) +static inline const char *tcg_helper_find (TCGContext *s,uintptr_t val) {const char *ret =NULL ; -if (helper_table) { -TCGHelperInfo *info =g_hash_table_lookup (helper_table, (gpointer )val); +if (helper_table_inited) {
change this to a assert(helper_table.cmp) if you really want to.
Quoted 9 lines
tcg/tcg.c +5
-0+uint32_t hash =tcg_helper_func_hash ((void *)val); +TCGHelperInfo *info; + + info =qht_lookup_custom (&helper_table, (void *)val, hash, + tcg_helper_lookup_cmp);if (info) { ret = info->name; }
tcg/tcg.c void tcg_dump_ops(TCGContext *s) · line 1964
Quoted 8 lines
tcg/tcg.c +1
-1@@ -1919,7 +1958,7 @@ void tcg_dump_ops(TCGContext *s)/* function name, flags, out args */ col +=qemu_log (" %s %s,$0x%" TCG_PRIlx",$%d" , def->name, -tcg_find_helper (s, op->args[nb_oargs + nb_iargs]), +tcg_helper_find (s, op->args[nb_oargs + nb_iargs]), op->args[nb_oargs + nb_iargs +1 ], nb_oargs);for (i =0 ; i < nb_oargs; i++) { col += qemu_log(",%s", tcg_get_arg_str(s, buf, sizeof(buf),
Otherwise:
--
Alex Bennée
Re: [Qemu-devel] [RFC 06/48] tcg: use QHT for helper_table
Quoted 1 line · Alex Bennée
Emilio G. Cota <[email protected]> writes:
(snip)
Quoted 7 lines
tcg/tcg.c typedef struct TCGHelperInfo { · line 884
Quoted 3 lines
tcg/tcg.c +2
-1-static GHashTable *helper_table; +static struct qht helper_table; +static bool helper_table_inited;Having a flag for initialisation seems a little excessive considering we've moved that initialisation into
tcg_context_init()which has to be called before we do anything TCG related.
(snip)
Quoted 3 lines
Quoted 1 line
+
helper_table_inited= true;so I think we can drop this and...
(snip)
Quoted 8 lines
tcg/tcg.c static char *tcg_get_arg_str(TCGContext *s, char *buf, · line 1866
Quoted 6 lines
tcg/tcg.c +2
-2+static inline const char *tcg_helper_find (TCGContext *s,uintptr_t val) {const char *ret =NULL ; -if (helper_table) { -TCGHelperInfo *info =g_hash_table_lookup (helper_table, (gpointer )val); +if (helper_table_inited) {change this to a
assert(helper_table.cmp)if you really want to.
I like this suggestion. The only caller of tcg_helper_find is tcg_dump_ops, which is unlikely to be called on an uninitialized TCGContext.
I have added this to v2, without the assert.
(snip)
Quoted 3 lines
Otherwise:
Reviewed-by Alex Bennée <[email protected]>
Thanks!
E.
Re: [Qemu-devel] [RFC 06/48] tcg: use QHT for helper_table
In reply to Emilio G. CotaQuoted 29 lines · Emilio G. Cota
This will allow us to add TCG helpers at run-time.
While at it, rename
tcg_find_helpertotcg_helper_findfor consistency with the addedtcg_helper_foofunctions.Signed-off-by Emilio G. Cota <[email protected]>--- tcg/tcg.c | 59 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 10 deletions(-)tcg/tcg.c +3
-1@@ -33,6 +33,7 @@#include "qemu/error-report.h" #include "qemu/cutils.h" #include "qemu/host-utils.h" +#include "qemu/xxhash.h" #include "qemu/timer.h" /* Note: the long term plan is to reduce the dependencies on the QEMU @@ -879,13 +880,46 @@ typedef struct TCGHelperInfo {static const TCGHelperInfo all_helpers[] = { #include "exec/helper-tcg.h" }; -static GHashTable *helper_table; +static struct qht helper_table; +static bool helper_table_inited;
tcg/tcg.c typedef struct TCGHelperInfo { · line 894
Quoted 10 lines
tcg/tcg.c +4
-0static int indirect_reg_alloc_order[ARRAY_SIZE (tcg_target_reg_alloc_order)];static void process_op_defs (TCGContext *s);static TCGTemp *tcg_global_reg_new_internal (TCGContext *s,TCGType type,TCGReg reg,const char *name); +static inline uint32_t tcg_helper_func_hash (const void *func) +{ +return qemu_xxhash2 ((uint64_t )func); +}
I needed to do this:
modified tcg/tcg.c
@@ -884,7 +884,7 @@ static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
static inline uint32_t tcg_helper_func_hash(const void *func) {
- return qemu_xxhash2((uint64_t)func);
+ return qemu_xxhash2((uint64_t)(uintptr_t)func);
}
To prevent the compiler complaining about:
tcg.c:887:25: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]
Quoted 81 lines
tcg/tcg.c +41
-8+ +static bool tcg_helper_cmp (const void *ap,const void *bp) +{ +const TCGHelperInfo *a = ap; +const TCGHelperInfo *b = bp; + +return a->func == b->func && + a->flags == b->flags && + a->sizemask == b->sizemask && + !strcmp (a->name, b->name); +} + +static bool tcg_helper_lookup_cmp (const void *obj,const void *func) +{ +const TCGHelperInfo *info = obj; + +return info->func == func; +} + +static void tcg_helper_insert (const TCGHelperInfo *info) +{ +uint32_t hash =tcg_helper_func_hash (info->func); +bool inserted; + + inserted =qht_insert (&helper_table, (void *)info, hash,NULL ); +g_assert (inserted); +} +void tcg_context_init (TCGContext *s) {int op, total_args, n, i;@@ -919,13 +953,13 @@ void tcg_context_init(TCGContext *s) }/* Register helpers. */ -/* Use g_direct_hash/equal for direct pointer comparisons on func. */ - helper_table =g_hash_table_new (NULL ,NULL ); +qht_init (&helper_table, tcg_helper_cmp,ARRAY_SIZE (all_helpers), +QHT_MODE_AUTO_RESIZE );for (i =0 ; i <ARRAY_SIZE (all_helpers); ++i) { -g_hash_table_insert (helper_table, (gpointer )all_helpers[i].func, - (gpointer)&all_helpers[i]); +tcg_helper_insert (&all_helpers[i]); } + helper_table_inited = true;tcg_target_init (s);process_op_defs (s);@@ -1620,9 +1654,10 @@ void tcg_gen_callN(void *func, TCGTemp *ret, int nargs, TCGTemp **args)int i, real_args, nb_rets, pi;unsigned sizemask, flags;TCGHelperInfo *info; +uint32_t hash =tcg_helper_func_hash (func);TCGOp *op; - info =g_hash_table_lookup (helper_table, (gpointer )func); + info =qht_lookup_custom (&helper_table, func, hash, tcg_helper_lookup_cmp); flags = info->flags; sizemask = info->sizemask;@@ -1825,11 +1860,15 @@ static char *tcg_get_arg_str(TCGContext *s, char *buf, }/* Find helper name. */ -static inline const char *tcg_find_helper (TCGContext *s,uintptr_t val) +static inline const char *tcg_helper_find (TCGContext *s,uintptr_t val) {const char *ret =NULL ; -if (helper_table) { -TCGHelperInfo *info =g_hash_table_lookup (helper_table, (gpointer )val); +if (helper_table_inited) { +uint32_t hash =tcg_helper_func_hash ((void *)val); +TCGHelperInfo *info; + + info =qht_lookup_custom (&helper_table, (void *)val, hash, + tcg_helper_lookup_cmp);if (info) { ret = info->name; }
tcg/tcg.c void tcg_dump_ops(TCGContext *s) · line 1964
Quoted 8 lines
tcg/tcg.c +1
-1@@ -1919,7 +1958,7 @@ void tcg_dump_ops(TCGContext *s)/* function name, flags, out args */ col +=qemu_log (" %s %s,$0x%" TCG_PRIlx",$%d" , def->name, -tcg_find_helper (s, op->args[nb_oargs + nb_iargs]), +tcg_helper_find (s, op->args[nb_oargs + nb_iargs]), op->args[nb_oargs + nb_iargs +1 ], nb_oargs);for (i =0 ; i < nb_oargs; i++) { col += qemu_log(",%s", tcg_get_arg_str(s, buf, sizeof(buf),
--
Alex Bennée
Re: [Qemu-devel] [RFC 06/48] tcg: use QHT for helper_table
Quoted 2 lines · Alex Bennée
Emilio G. Cota <[email protected]> writes:
(snip)
Quoted 13 lines
I needed to do this:
modified
tcg/tcg.c(quoted diff) +1
-1@@ -884,7 +884,7 @@ static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type, static inline uint32_t tcg_helper_func_hash(const void *func) { - return qemu_xxhash2((uint64_t)func); + return qemu_xxhash2((uint64_t)(uintptr_t)func); }To prevent the compiler complaining about:
Quoted 1 line
tcg.c:887:25: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]
Fixed, thanks.
E.
RFC 7/48 tcg: export TCGHelperInfo
In reply to Emilio G. Cota--- tcg/tcg.h | 7 +++++++ tcg/tcg.c | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-)
tcg/tcg.h +7 -0
@@ -480,6 +480,13 @@ typedef TCGv_ptr TCGv_env;/* Used to align parameters. See the comment before tcgv_i32_temp. */ #define TCG_CALL_DUMMY_ARG ((TCGArg)0) +typedef struct TCGHelperInfo { +void *func; +const char *name; +unsigned flags; +unsigned sizemask; +}TCGHelperInfo ; +/* Conditions. Note that these are laid out for easy manipulation by the functions below: bit 0 is used for inverting;
tcg/tcg.c +0 -7
@@ -868,13 +868,6 @@ void tcg_pool_reset(TCGContext *s) s->pool_current =NULL ; } -typedef struct TCGHelperInfo { -void *func; -const char *name; -unsigned flags; -unsigned sizemask; -}TCGHelperInfo ; -#include "exec/helper-proto.h" static const TCGHelperInfo all_helpers[] = {
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 07/48] tcg: export TCGHelperInfo
Quoted 1 line · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>
Quoted 31 lines
--- tcg/tcg.h | 7 +++++++ tcg/tcg.c | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-)tcg/tcg.h +7
-0@@ -480,6 +480,13 @@ typedef TCGv_ptr TCGv_env;/* Used to align parameters. See the comment before tcgv_i32_temp. */ #define TCG_CALL_DUMMY_ARG ((TCGArg)0) +typedef struct TCGHelperInfo { +void *func; +const char *name; +unsigned flags; +unsigned sizemask; +}TCGHelperInfo ; +/* Conditions. Note that these are laid out for easy manipulation by the functions below: bit 0 is used for inverting; tcg/tcg.c +0
-0@@ -868,13 +868,6 @@ void tcg_pool_reset(TCGContext *s) s->pool_current =NULL ; }
tcg/tcg.c void tcg_pool_reset(TCGContext *s) · line 873
Quoted 10 lines
tcg/tcg.c +0
-7-typedef struct TCGHelperInfo { -void *func; -const char *name; -unsigned flags; -unsigned sizemask; -}TCGHelperInfo ; -#include "exec/helper-proto.h" static const TCGHelperInfo all_helpers[] = {
--
Alex Bennée
RFC 8/48 tcg: export tcg_gen_runtime_helper
In reply to Emilio G. CotaThis takes the TCGHelperInfo directly, which will allow us to generate helpers at run-time.
--- tcg/tcg.h | 2 ++ tcg/tcg.c | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 5 deletions(-)
tcg/tcg.h +2 -0
@@ -1077,6 +1077,8 @@ do {\bool tcg_op_supported (TCGOpcode op);void tcg_gen_callN (void *func,TCGTemp *ret,int nargs,TCGTemp **args); +void tcg_gen_runtime_helper (const TCGHelperInfo *orig,TCGTemp *ret,int nargs, +TCGTemp **args);TCGOp *tcg_emit_op (TCGOpcode opc);void tcg_op_remove (TCGContext *s,TCGOp *op);
tcg/tcg.c +45 -5
73 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 08/48] tcg: export tcg_gen_runtime_helper
Quoted 4 lines · Emilio G. Cota
This takes the TCGHelperInfo directly, which will allow us to generate helpers at run-time.
Signed-off-by Emilio G. Cota <[email protected]>
Quoted 88 lines
--- tcg/tcg.h | 2 ++ tcg/tcg.c | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 5 deletions(-)tcg/tcg.h +2
-0@@ -1077,6 +1077,8 @@ do {\bool tcg_op_supported (TCGOpcode op);void tcg_gen_callN (void *func,TCGTemp *ret,int nargs,TCGTemp **args); +void tcg_gen_runtime_helper (const TCGHelperInfo *orig,TCGTemp *ret,int nargs, +TCGTemp **args);TCGOp *tcg_emit_op (TCGOpcode opc);void tcg_op_remove (TCGContext *s,TCGOp *op);tcg/tcg.c +38
-5@@ -1642,15 +1642,13 @@ bool tcg_op_supported(TCGOpcode op)/* Note: we convert the 64 bit args to 32 bit and do some alignment and endian swap. Maybe it would be better to do the alignment and endian swap in tcg_reg_alloc_call(). */ -void tcg_gen_callN (void *func,TCGTemp *ret,int nargs,TCGTemp **args) +static void do_tcg_gen_callN (TCGHelperInfo *info,TCGTemp *ret,int nargs, +TCGTemp **args) {int i, real_args, nb_rets, pi;unsigned sizemask, flags; -TCGHelperInfo *info; -uint32_t hash =tcg_helper_func_hash (func);TCGOp *op; - info =qht_lookup_custom (&helper_table, func, hash, tcg_helper_lookup_cmp); flags = info->flags; sizemask = info->sizemask;@@ -1774,7 +1772,7 @@ void tcg_gen_callN(void *func, TCGTemp *ret, int nargs, TCGTemp **args) op->args[pi++] =temp_arg (args[i]); real_args++; } - op->args[pi++] = (uintptr_t )func; + op->args[pi++] = (uintptr_t )info->func; op->args[pi++] = flags;TCGOP_CALLI (op) = real_args;@@ -1812,6 +1810,48 @@ void tcg_gen_callN(void *func, TCGTemp *ret, int nargs, TCGTemp **args)#endif /* TCG_TARGET_EXTEND_ARGS */ } +void tcg_gen_callN (void *func,TCGTemp *ret,int nargs,TCGTemp **args) +{ +TCGHelperInfo *info; +uint32_t hash =tcg_helper_func_hash (func); + +/* +* Here we can get away with tcg_helper_lookup_cmp, which only looks +* at the function pointer, since we have the compile-time guarantee +* that @func can only be in one TCGHelperInfo. +*/ + info =qht_lookup_custom (&helper_table, func, hash, tcg_helper_lookup_cmp); +do_tcg_gen_callN (info, ret, nargs, args); +} + +void tcg_gen_runtime_helper (const TCGHelperInfo *orig,TCGTemp *ret,int nargs, +TCGTemp **args) +{ +TCGHelperInfo *info; +uint32_t hash =tcg_helper_func_hash (orig->func); + +/* +* Use the full TCGHelperInfo lookup, since there is no guarantee that func +* will be unique to each TCGHelperInfo. For instance, we could have the +* same helper function registered in several TCGHelperInfo's, each of them +* with different flags. +*/ + info =qht_lookup (&helper_table, orig, hash); +if (info ==NULL ) { +void *existing =NULL ; + +/* @orig might be in the stack, so we need to allocate a new struct */ + info =g_new (TCGHelperInfo,1 ); +memcpy (info, orig,sizeof (TCGHelperInfo)); +qht_insert (&helper_table, info, hash, &existing); +if (unlikely (existing)) {
tcg/tcg.c void tcg_gen_callN(void *func, TCGTemp *ret, int nargs, TCGTemp **args) · line 1857
Quoted 10 lines
tcg/tcg.c +7
-0+g_free (info); + info = existing; + } + } +do_tcg_gen_callN (info, ret, nargs, args); +} +static void tcg_reg_alloc_start (TCGContext *s) {int i, n;
--
Alex Bennée
RFC 9/48 tcg: reset runtime helpers when flushing the code cache
In reply to Emilio G. CotaIn preparation for adding plugin support. One of the clean-up actions when uninstalling plugins will be to flush the code cache. We'll also have to clear the runtime helpers, since some of those runtime helpers may belong to the plugin being uninstalled.
--- tcg/tcg.h | 1 + accel/tcg/translate-all.c | 1 + tcg/tcg.c | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+)
tcg/tcg.h +1 -0
@@ -1079,6 +1079,7 @@ bool tcg_op_supported(TCGOpcode op);void tcg_gen_callN (void *func,TCGTemp *ret,int nargs,TCGTemp **args);void tcg_gen_runtime_helper (const TCGHelperInfo *orig,TCGTemp *ret,int nargs,TCGTemp **args); +void tcg_reset_runtime_helpers (void );TCGOp *tcg_emit_op (TCGOpcode opc);void tcg_op_remove (TCGContext *s,TCGOp *op);
accel/tcg/translate-all.c +1 -0
@@ -1255,6 +1255,7 @@ static void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count)qht_reset_size (&tb_ctx.htable,CODE_GEN_HTABLE_SIZE );page_flush_tb (); +tcg_reset_runtime_helpers ();tcg_region_reset_all ();/* XXX: flush processor icache at this point if cache flush is
tcg/tcg.c +21 -0
@@ -874,6 +874,7 @@ static const TCGHelperInfo all_helpers[] = {#include "exec/helper-tcg.h" };static struct qht helper_table; +static struct qht runtime_helper_table;static bool helper_table_inited;static int indirect_reg_alloc_order[ARRAY_SIZE (tcg_target_reg_alloc_order)];
@@ -913,6 +914,22 @@ static void tcg_helper_insert(const TCGHelperInfo *info)g_assert (inserted); } +static void +rm_from_helper_table_and_free (void *p,uint32_t h,void *userp) +{ +bool success; + + success =qht_remove (&helper_table, p, h); +g_assert (success); +g_free (p); +} + +void tcg_reset_runtime_helpers (void ) +{ +qht_iter (&runtime_helper_table, rm_from_helper_table_and_free,NULL ); +qht_reset (&runtime_helper_table); +} +void tcg_context_init (TCGContext *s) {int op, total_args, n, i;
@@ -948,6 +965,7 @@ void tcg_context_init(TCGContext *s)/* Register helpers. */ qht_init (&helper_table, tcg_helper_cmp,ARRAY_SIZE (all_helpers),QHT_MODE_AUTO_RESIZE ); +qht_init (&runtime_helper_table, tcg_helper_cmp,1 ,QHT_MODE_AUTO_RESIZE );for (i =0 ; i <ARRAY_SIZE (all_helpers); ++i) {tcg_helper_insert (&all_helpers[i]);
@@ -1847,6 +1865,9 @@ void tcg_gen_runtime_helper(const TCGHelperInfo *orig, TCGTemp *ret, int nargs,if (unlikely (existing)) {g_free (info); info = existing; + }else { +qht_insert (&runtime_helper_table, info, hash, &existing); +g_assert (existing ==NULL ); } }do_tcg_gen_callN (info, ret, nargs, args);
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 09/48] tcg: reset runtime helpers when flushing the code cache
Quoted 18 lines · Emilio G. Cota
In preparation for adding plugin support. One of the clean-up actions when uninstalling plugins will be to flush the code cache. We'll also have to clear the runtime helpers, since some of those runtime helpers may belong to the plugin being uninstalled.
Signed-off-by Emilio G. Cota <[email protected]>--- tcg/tcg.h | 1 + accel/tcg/translate-all.c | 1 + tcg/tcg.c | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+)tcg/tcg.h +0
-0
tcg/tcg.h bool tcg_op_supported(TCGOpcode op); · line 1082
Quoted 4 lines
tcg/tcg.h +1
-0@@ -1079,6 +1079,7 @@ bool tcg_op_supported(TCGOpcode op);void tcg_gen_callN (void *func,TCGTemp *ret,int nargs,TCGTemp **args);void tcg_gen_runtime_helper (const TCGHelperInfo *orig,TCGTemp *ret,int nargs,TCGTemp **args); +void tcg_reset_runtime_helpers (void );
I know tcg.h doesn't have and API doc blocks but perhaps we should start?
/**
* tcg_reset_runtime_helpers:
*
* Remove all runtime registered helpers. Should only be called from a
* quiescent system while flushing old code.
*/
Quoted 59 lines
tcg/tcg.h +0
-0TCGOp *tcg_emit_op (TCGOpcode opc);void tcg_op_remove (TCGContext *s,TCGOp *op);accel/tcg/translate-all.c +1
-0@@ -1255,6 +1255,7 @@ static void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count)qht_reset_size (&tb_ctx.htable,CODE_GEN_HTABLE_SIZE );page_flush_tb (); +tcg_reset_runtime_helpers ();tcg_region_reset_all ();/* XXX: flush processor icache at this point if cache flush is tcg/tcg.c +18
-0@@ -874,6 +874,7 @@ static const TCGHelperInfo all_helpers[] = {#include "exec/helper-tcg.h" };static struct qht helper_table; +static struct qht runtime_helper_table;static bool helper_table_inited;static int indirect_reg_alloc_order[ARRAY_SIZE (tcg_target_reg_alloc_order)];@@ -913,6 +914,22 @@ static void tcg_helper_insert(const TCGHelperInfo *info)g_assert (inserted); } +static void +rm_from_helper_table_and_free (void *p,uint32_t h,void *userp) +{ +bool success; + + success =qht_remove (&helper_table, p, h); +g_assert (success); +g_free (p); +} + +void tcg_reset_runtime_helpers (void ) +{ +qht_iter (&runtime_helper_table, rm_from_helper_table_and_free,NULL ); +qht_reset (&runtime_helper_table); +} +void tcg_context_init (TCGContext *s) {int op, total_args, n, i;@@ -948,6 +965,7 @@ void tcg_context_init(TCGContext *s)/* Register helpers. */ qht_init (&helper_table, tcg_helper_cmp,ARRAY_SIZE (all_helpers),QHT_MODE_AUTO_RESIZE ); +qht_init (&runtime_helper_table, tcg_helper_cmp,1 ,QHT_MODE_AUTO_RESIZE );for (i =0 ; i <ARRAY_SIZE (all_helpers); ++i) {tcg_helper_insert (&all_helpers[i]);
tcg/tcg.c void tcg_gen_runtime_helper(const TCGHelperInfo *orig, TCGTemp *ret, int nargs, · line 1873
Quoted 9 lines
tcg/tcg.c +3
-0@@ -1847,6 +1865,9 @@ void tcg_gen_runtime_helper(const TCGHelperInfo *orig, TCGTemp *ret, int nargs,if (unlikely (existing)) {g_free (info); info = existing; + }else { +qht_insert (&runtime_helper_table, info, hash, &existing); +g_assert (existing ==NULL ); } }do_tcg_gen_callN (info, ret, nargs, args);
Otherwise:
--
Alex Bennée
Re: [Qemu-devel] [RFC 09/48] tcg: reset runtime helpers when flushing the code cache
Quoted 11 lines · Alex Bennée
Quoted 8 lines · Emilio G. Cota
In preparation for adding plugin support. One of the clean-up actions when uninstalling plugins will be to flush the code cache. We'll also have to clear the runtime helpers, since some of those runtime helpers may belong to the plugin being uninstalled.
Signed-off-by Emilio G. Cota <[email protected]>---
(snip)
Quoted 11 lines
Quoted 1 line
+void
tcg_reset_runtime_helpers(void);I know
tcg.hdoesn't have and API doc blocks but perhaps we should start?/**
*tcg_reset_runtime_helpers:
*
* Remove all runtime registered helpers. Should only be called from a
* quiescent system while flushing old code.
*/
Better late than never. Added to v2.
(snip)
Quoted 3 lines
Otherwise:
Reviewed-by Alex Bennée <[email protected]>
Thanks,
E.
RFC 10/48 exec: export do_tb_flush
In reply to Emilio G. CotaThis will be used by plugin code to flush the code cache as well as doing other bookkeeping in a safe work environment.
--- include/exec/exec-all.h | 1 + accel/tcg/translate-all.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)
include/exec/exec-all.h +1 -0
@@ -427,6 +427,7 @@ void tb_invalidate_phys_range(target_ulong start, target_ulong end);void tb_invalidate_phys_addr (AddressSpace *as,hwaddr addr,MemTxAttrs attrs);#endif void tb_flush (CPUState *cpu); +void do_tb_flush (CPUState *cpu,run_on_cpu_data tb_flush_count);void tb_phys_invalidate (TranslationBlock *tb,tb_page_addr_t page_addr); TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc, target_ulong cs_base, uint32_t flags,
accel/tcg/translate-all.c +1 -1
@@ -1230,7 +1230,7 @@ static gboolean tb_host_size_iter(gpointer key, gpointer value, gpointer data) }/* flush all the translation blocks */ -static void do_tb_flush (CPUState *cpu,run_on_cpu_data tb_flush_count) +void do_tb_flush (CPUState *cpu,run_on_cpu_data tb_flush_count) {mmap_lock ();/* If it is already been done on request of another CPU,
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 10/48] exec: export do_tb_flush
Quoted 2 lines · Emilio G. Cota
This will be used by plugin code to flush the code cache as well as doing other bookkeeping in a safe work environment.
This seems a little excessive given the plugin code could just call tb_flush() directly. Wouldn't calling tb_flush after scheduling the plugin_destroy be enough?
If there is a race condition here maybe we could build some sort of awareness into tb_flush as to the current run state. But having two entry points to this rather fundamental action seems likely to either be misused or misunderstood.
Quoted 24 lines
Signed-off-by Emilio G. Cota <[email protected]>--- include/exec/exec-all.h | 1 + accel/tcg/translate-all.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)include/exec/exec-all.h +1
-0@@ -427,6 +427,7 @@ void tb_invalidate_phys_range(target_ulong start, target_ulong end);void tb_invalidate_phys_addr (AddressSpace *as,hwaddr addr,MemTxAttrs attrs);#endif void tb_flush (CPUState *cpu); +void do_tb_flush (CPUState *cpu,run_on_cpu_data tb_flush_count);void tb_phys_invalidate (TranslationBlock *tb,tb_page_addr_t page_addr); TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc, target_ulong cs_base, uint32_t flags,accel/tcg/translate-all.c +0
-0
accel/tcg/translate-all.c static gboolean tb_host_size_iter(gpointer key, gpointer value, gpointer data) · line 1236
Quoted 8 lines
accel/tcg/translate-all.c +1
-1@@ -1230,7 +1230,7 @@ static gboolean tb_host_size_iter(gpointer key, gpointer value, gpointer data) }/* flush all the translation blocks */ -static void do_tb_flush (CPUState *cpu,run_on_cpu_data tb_flush_count) +void do_tb_flush (CPUState *cpu,run_on_cpu_data tb_flush_count) {mmap_lock ();/* If it is already been done on request of another CPU,
--
Alex Bennée
Re: [Qemu-devel] [RFC 10/48] exec: export do_tb_flush
Quoted 6 lines · Alex Bennée
Quoted 2 lines · Emilio G. Cota
This will be used by plugin code to flush the code cache as well as doing other bookkeeping in a safe work environment.
Quoted 8 lines
This seems a little excessive given the plugin code could just call
tb_flush()directly. Wouldn't callingtb_flushafter scheduling theplugin_destroybe enough?If there is a race condition here maybe we could build some sort of awareness into
tb_flushas to the current run state. But having two entry points to this rather fundamental action seems likely to either be misused or misunderstood.
We have to make sure that no callback left in the generated code is called once a plugin has been uninstalled. To me, using the same safe work window to both flush the TB and uninstall the plugin seems the simplest way to do this.
Thanks,
Emilio
Re: [Qemu-devel] [RFC 10/48] exec: export do_tb_flush
Quoted 16 lines · Emilio G. Cota
Quoted 6 lines · Alex Bennée
Quoted 2 lines · Emilio G. Cota
This will be used by plugin code to flush the code cache as well as doing other bookkeeping in a safe work environment.
Quoted 8 lines
This seems a little excessive given the plugin code could just call
tb_flush()directly. Wouldn't callingtb_flushafter scheduling theplugin_destroybe enough?If there is a race condition here maybe we could build some sort of awareness into
tb_flushas to the current run state. But having two entry points to this rather fundamental action seems likely to either be misused or misunderstood.
Quoted 4 lines
We have to make sure that no callback left in the generated code is called once a plugin has been uninstalled. To me, using the same safe work window to both flush the TB and uninstall the plugin seems the simplest way to do this.
I still think making tb_flush() aware that it can run in an exclusive period would be a better solution than exposing two functions for the operation. So tb_flush could be something like:
void tb_flush(CPUState *cpu)
{
if (tcg_enabled()) {
unsigned tb_flush_count = atomic_mb_read(&tb_ctx.tb_flush_count);
if (cpu_current_and_exclusive(cpu)) {
do_tb_flush(RUN_ON_CPU_HOST_INT(tb_flush_count))
} else {
async_safe_run_on_cpu(cpu, do_tb_flush,
RUN_ON_CPU_HOST_INT(tb_flush_count));
}
}
}Or possibly push that logic down into async_safe_run_on_cpu()?
--
Alex Bennée
Re: [Qemu-devel] [RFC 10/48] exec: export do_tb_flush
Quoted 41 lines · Alex Bennée
Quoted 16 lines · Emilio G. Cota
Quoted 6 lines · Alex Bennée
Quoted 2 lines · Emilio G. Cota
This will be used by plugin code to flush the code cache as well as doing other bookkeeping in a safe work environment.
Quoted 8 lines
This seems a little excessive given the plugin code could just call
tb_flush()directly. Wouldn't callingtb_flushafter scheduling theplugin_destroybe enough?If there is a race condition here maybe we could build some sort of awareness into
tb_flushas to the current run state. But having two entry points to this rather fundamental action seems likely to either be misused or misunderstood.Quoted 4 lines
We have to make sure that no callback left in the generated code is called once a plugin has been uninstalled. To me, using the same safe work window to both flush the TB and uninstall the plugin seems the simplest way to do this.
I still think making
tb_flush()aware that it can run in an exclusive period would be a better solution than exposing two functions for the operation. Sotb_flushcould be something like:void tb_flush(CPUState *cpu) { if (tcg_enabled()) { unsigned tb_flush_count = atomic_mb_read(&tb_ctx.tb_flush_count); if (cpu_current_and_exclusive(cpu)) { do_tb_flush(RUN_ON_CPU_HOST_INT(tb_flush_count)) } else { async_safe_run_on_cpu(cpu, do_tb_flush, RUN_ON_CPU_HOST_INT(tb_flush_count)); } } }
Quoted 1 line
Or possibly push that logic down into
async_safe_run_on_cpu()?
The latter option would be much harder, because in async_safe_run_on_cpu we always queue the work and kick the CPU (which could be ourselves). IOW the job is always asynchronous, as the name implies.
I've thus implemented the former in v2, as follows (I'm using a hole in struct CPUState to add the bool):
@@ -1277,8 +1277,13 @@ void tb_flush(CPUState *cpu)
{
if (tcg_enabled()) {
unsigned tb_flush_count = atomic_mb_read(&tb_ctx.tb_flush_count);- async_safe_run_on_cpu(cpu, do_tb_flush,
- RUN_ON_CPU_HOST_INT(tb_flush_count));
+
+ if (cpu_in_exclusive_work_context(cpu)) {
+ do_tb_flush(cpu, RUN_ON_CPU_HOST_INT(tb_flush_count));
+ } else {
+ async_safe_run_on_cpu(cpu, do_tb_flush,
+ RUN_ON_CPU_HOST_INT(tb_flush_count));
+ }
}
}
+++ b/cpus-common.c
@@ -386,7 +386,9 @@ static void process_queued_cpu_work_locked(CPUState *cpu)
qemu_mutex_unlock_iothread();
}
start_exclusive();
+ cpu->in_exclusive_work_context = true;
wi->func(cpu, wi->data);
+ cpu->in_exclusive_work_context = false;
end_exclusive();I've also fixed a couple of unrelated bugs when uninstalling a plugin with memory callbacks enabled.
Thanks,
Emilio
RFC 11/48 atomic_template: fix indentation in GEN_ATOMIC_HELPER
In reply to Emilio G. Cota--- accel/tcg/atomic_template.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
accel/tcg/atomic_template.h +1 -1
@@ -284,7 +284,7 @@ ABI_TYPE ATOMIC_NAME(xchg)(CPUArchState *env, target_ulong addr,#define GEN_ATOMIC_HELPER (X ) \ ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr, \ - ABI_TYPE val EXTRA_ARGS) \ + ABI_TYPE val EXTRA_ARGS) \ { \ ATOMIC_MMU_DECLS; \ DATA_TYPE *haddr = ATOMIC_MMU_LOOKUP; \
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 11/48] atomic_template: fix indentation in GEN_ATOMIC_HELPER
Quoted 1 line · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>
Quoted 9 lines
--- accel/tcg/atomic_template.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)accel/tcg/atomic_template.h +0
-0
accel/tcg/atomic_template.h ABI_TYPE ATOMIC_NAME(xchg)(CPUArchState *env, target_ulong addr, · line 290
Quoted 8 lines
accel/tcg/atomic_template.h +1
-1@@ -284,7 +284,7 @@ ABI_TYPE ATOMIC_NAME(xchg)(CPUArchState *env, target_ulong addr,#define GEN_ATOMIC_HELPER (X ) \ ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr, \ - ABI_TYPE val EXTRA_ARGS) \ + ABI_TYPE val EXTRA_ARGS) \ { \ ATOMIC_MMU_DECLS; \ DATA_TYPE *haddr = ATOMIC_MMU_LOOKUP; \
--
Alex Bennée
RFC 12/48 atomic_template: define pre/post macros
In reply to Emilio G. CotaIn preparation for plugin support.
--- accel/tcg/atomic_template.h | 92 +++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 35 deletions(-)
accel/tcg/atomic_template.h +57 -35
@@ -59,25 +59,26 @@# define ABI_TYPE uint32_t#endif -#define ATOMIC_TRACE_RMW do { \ - uint8_t info = glue(trace_mem_build_info_no_se, MEND)(SHIFT, false); \ - \ - trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info); \ - trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, \ - info | TRACE_MEM_ST); \ - } while (0) - -#define ATOMIC_TRACE_LD do { \ - uint8_t info = glue(trace_mem_build_info_no_se, MEND)(SHIFT, false); \ - \ - trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info); \ - } while (0) - -# define ATOMIC_TRACE_ST do { \ - uint8_t info = glue(trace_mem_build_info_no_se, MEND)(SHIFT, true); \ - \ - trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info); \ - } while (0) +/* these don't depend on MEND/SHIFT, so we just define them once */ +#ifndef ATOMIC_TRACE_RMW_PRE +# define ATOMIC_TRACE_RMW_PRE do { \ + trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info); \ + trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info | TRACE_MEM_ST); \ +} while (0) + +# define ATOMIC_TRACE_RMW_POST \ + +# define ATOMIC_TRACE_LD_PRE \ + trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info) + +# define ATOMIC_TRACE_LD_POST \ + +# define ATOMIC_TRACE_ST_PRE \ + trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info) + +# define ATOMIC_TRACE_ST_POST \ + +#endif/* ATOMIC_TRACE_RMW_PRE */ /* Define host-endian atomic operations. Note that END is used within the ATOMIC_NAME macro, and redefined below. */
@@ -98,14 +99,16 @@ ABI_TYPE ATOMIC_NAME(cmpxchg)(CPUArchState *env, target_ulong addr,ATOMIC_MMU_DECLS ;DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ;DATA_TYPE ret; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); -ATOMIC_TRACE_RMW ; +ATOMIC_TRACE_RMW_PRE ;#if DATA_SIZE ==16 ret =atomic16_cmpxchg (haddr, cmpv, newv);#else ret =atomic_cmpxchg__nocheck (haddr, cmpv, newv);#endif ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_RMW_POST ;return ret; }
@@ -115,10 +118,12 @@ ABI_TYPE ATOMIC_NAME(ld)(CPUArchState *env, target_ulong addr EXTRA_ARGS) {ATOMIC_MMU_DECLS ;DATA_TYPE val, *haddr =ATOMIC_MMU_LOOKUP ; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); -ATOMIC_TRACE_LD ; +ATOMIC_TRACE_LD_PRE ; val =atomic16_read (haddr);ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_LD_POST ;return val; }
@@ -127,10 +132,12 @@ void ATOMIC_NAME(st)(CPUArchState *env, target_ulong addr, {ATOMIC_MMU_DECLS ;DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , true); -ATOMIC_TRACE_ST ; +ATOMIC_TRACE_ST_PRE ;atomic16_set (haddr, val);ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_ST_POST ; }#endif #else
@@ -140,10 +147,12 @@ ABI_TYPE ATOMIC_NAME(xchg)(CPUArchState *env, target_ulong addr,ATOMIC_MMU_DECLS ;DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ;DATA_TYPE ret; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); -ATOMIC_TRACE_RMW ; +ATOMIC_TRACE_RMW_PRE ; ret =atomic_xchg__nocheck (haddr, val);ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_RMW_POST ;return ret; }
@@ -154,10 +163,12 @@ ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr, \ATOMIC_MMU_DECLS ; \DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ; \DATA_TYPE ret; \ +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); \ \ -ATOMIC_TRACE_RMW ; \ +ATOMIC_TRACE_RMW_PRE ; \ ret = atomic_##X(haddr, val); \ ATOMIC_MMU_CLEANUP; \ + ATOMIC_TRACE_RMW_POST; \ return ret; \ }
@@ -186,8 +197,9 @@ ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr, \ATOMIC_MMU_DECLS ; \XDATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ; \XDATA_TYPE cmp, old, new, val = xval; \ +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); \ \ -ATOMIC_TRACE_RMW ; \ +ATOMIC_TRACE_RMW_PRE ; \smp_mb (); \ cmp =atomic_read__nocheck (haddr); \ do { \
@@ -195,6 +207,7 @@ ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr, \ cmp =atomic_cmpxchg__nocheck (haddr, old, new); \ }while (cmp != old); \ATOMIC_MMU_CLEANUP ; \ +ATOMIC_TRACE_RMW_POST ; \return RET ; \ }
@@ -232,14 +245,16 @@ ABI_TYPE ATOMIC_NAME(cmpxchg)(CPUArchState *env, target_ulong addr,ATOMIC_MMU_DECLS ;DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ;DATA_TYPE ret; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); -ATOMIC_TRACE_RMW ; +ATOMIC_TRACE_RMW_PRE ;#if DATA_SIZE ==16 ret =atomic16_cmpxchg (haddr,BSWAP (cmpv),BSWAP (newv));#else ret =atomic_cmpxchg__nocheck (haddr,BSWAP (cmpv),BSWAP (newv));#endif ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_RMW_POST ;return BSWAP (ret); }
@@ -249,10 +264,12 @@ ABI_TYPE ATOMIC_NAME(ld)(CPUArchState *env, target_ulong addr EXTRA_ARGS) {ATOMIC_MMU_DECLS ;DATA_TYPE val, *haddr =ATOMIC_MMU_LOOKUP ; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); -ATOMIC_TRACE_LD ; +ATOMIC_TRACE_LD_PRE ; val =atomic16_read (haddr);ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_LD_POST ;return BSWAP (val); }
@@ -261,11 +278,14 @@ void ATOMIC_NAME(st)(CPUArchState *env, target_ulong addr, {ATOMIC_MMU_DECLS ;DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , true); -ATOMIC_TRACE_ST ; + val =BSWAP (val); +ATOMIC_TRACE_ST_PRE ; val =BSWAP (val);atomic16_set (haddr, val);ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_ST_POST ; }#endif #else
@@ -275,10 +295,12 @@ ABI_TYPE ATOMIC_NAME(xchg)(CPUArchState *env, target_ulong addr,ATOMIC_MMU_DECLS ;DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ;ABI_TYPE ret; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); -ATOMIC_TRACE_RMW ; +ATOMIC_TRACE_RMW_PRE ; ret =atomic_xchg__nocheck (haddr,BSWAP (val));ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_RMW_POST ;return BSWAP (ret); }
@@ -289,10 +311,12 @@ ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr, \ATOMIC_MMU_DECLS ; \DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ; \DATA_TYPE ret; \ +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); \ \ -ATOMIC_TRACE_RMW ; \ +ATOMIC_TRACE_RMW_PRE ; \ ret = atomic_##X(haddr, BSWAP(val)); \ ATOMIC_MMU_CLEANUP; \ + ATOMIC_TRACE_RMW_POST; \ return BSWAP(ret); \ }
@@ -319,8 +343,9 @@ ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr, \ATOMIC_MMU_DECLS ; \XDATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ; \XDATA_TYPE ldo, ldn, old, new, val = xval; \ +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); \ \ -ATOMIC_TRACE_RMW ; \ +ATOMIC_TRACE_RMW_PRE ; \smp_mb (); \ ldn =atomic_read__nocheck (haddr); \ do { \
@@ -328,6 +353,7 @@ ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr, \ ldn =atomic_cmpxchg__nocheck (haddr, ldo,BSWAP (new)); \ }while (ldo != ldn); \ATOMIC_MMU_CLEANUP ; \ +ATOMIC_TRACE_RMW_POST ; \return RET ; \ }
@@ -355,10 +381,6 @@ GEN_ATOMIC_HELPER_FN(add_fetch, ADD, DATA_TYPE, new)#undef MEND#endif /* DATA_SIZE > 1 */ -#undef ATOMIC_TRACE_ST -#undef ATOMIC_TRACE_LD -#undef ATOMIC_TRACE_RMW -#undef BSWAP#undef ABI_TYPE#undef DATA_TYPE
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 12/48] atomic_template: define pre/post macros
Quoted 3 lines · Emilio G. Cota
In preparation for plugin support.
Signed-off-by Emilio G. Cota <[email protected]>
More macros for the macro-god. I guess this works but I wonder if it's possible to do a clean-up ala softfloat and the experimental softmmu re-factor that makes this less a mess of macros?
Quoted 243 lines
--- accel/tcg/atomic_template.h | 92 +++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 35 deletions(-)accel/tcg/atomic_template.h +57
-31@@ -59,25 +59,26 @@# define ABI_TYPE uint32_t#endif -#define ATOMIC_TRACE_RMW do { \ - uint8_t info = glue(trace_mem_build_info_no_se, MEND)(SHIFT, false); \ - \ - trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info); \ - trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, \ - info | TRACE_MEM_ST); \ - } while (0) - -#define ATOMIC_TRACE_LD do { \ - uint8_t info = glue(trace_mem_build_info_no_se, MEND)(SHIFT, false); \ - \ - trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info); \ - } while (0) - -# define ATOMIC_TRACE_ST do { \ - uint8_t info = glue(trace_mem_build_info_no_se, MEND)(SHIFT, true); \ - \ - trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info); \ - } while (0) +/* these don't depend on MEND/SHIFT, so we just define them once */ +#ifndef ATOMIC_TRACE_RMW_PRE +# define ATOMIC_TRACE_RMW_PRE do { \ + trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info); \ + trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info | TRACE_MEM_ST); \ +} while (0) + +# define ATOMIC_TRACE_RMW_POST \ + +# define ATOMIC_TRACE_LD_PRE \ + trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info) + +# define ATOMIC_TRACE_LD_POST \ + +# define ATOMIC_TRACE_ST_PRE \ + trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info) + +# define ATOMIC_TRACE_ST_POST \ + +#endif/* ATOMIC_TRACE_RMW_PRE */ /* Define host-endian atomic operations. Note that END is used within the ATOMIC_NAME macro, and redefined below. */ @@ -98,14 +99,16 @@ ABI_TYPE ATOMIC_NAME(cmpxchg)(CPUArchState *env, target_ulong addr,ATOMIC_MMU_DECLS ;DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ;DATA_TYPE ret; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); -ATOMIC_TRACE_RMW ; +ATOMIC_TRACE_RMW_PRE ;#if DATA_SIZE ==16 ret =atomic16_cmpxchg (haddr, cmpv, newv);#else ret =atomic_cmpxchg__nocheck (haddr, cmpv, newv);#endif ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_RMW_POST ;return ret; }@@ -115,10 +118,12 @@ ABI_TYPE ATOMIC_NAME(ld)(CPUArchState *env, target_ulong addr EXTRA_ARGS) {ATOMIC_MMU_DECLS ;DATA_TYPE val, *haddr =ATOMIC_MMU_LOOKUP ; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); -ATOMIC_TRACE_LD ; +ATOMIC_TRACE_LD_PRE ; val =atomic16_read (haddr);ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_LD_POST ;return val; }@@ -127,10 +132,12 @@ void ATOMIC_NAME(st)(CPUArchState *env, target_ulong addr, {ATOMIC_MMU_DECLS ;DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , true); -ATOMIC_TRACE_ST ; +ATOMIC_TRACE_ST_PRE ;atomic16_set (haddr, val);ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_ST_POST ; }#endif #else @@ -140,10 +147,12 @@ ABI_TYPE ATOMIC_NAME(xchg)(CPUArchState *env, target_ulong addr,ATOMIC_MMU_DECLS ;DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ;DATA_TYPE ret; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); -ATOMIC_TRACE_RMW ; +ATOMIC_TRACE_RMW_PRE ; ret =atomic_xchg__nocheck (haddr, val);ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_RMW_POST ;return ret; }@@ -154,10 +163,12 @@ ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr, \ATOMIC_MMU_DECLS ; \DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ; \DATA_TYPE ret; \ +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); \ \ -ATOMIC_TRACE_RMW ; \ +ATOMIC_TRACE_RMW_PRE ; \ ret = atomic_##X(haddr, val); \ ATOMIC_MMU_CLEANUP; \ + ATOMIC_TRACE_RMW_POST; \ return ret; \ }@@ -186,8 +197,9 @@ ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr, \ATOMIC_MMU_DECLS ; \XDATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ; \XDATA_TYPE cmp, old, new, val = xval; \ +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); \ \ -ATOMIC_TRACE_RMW ; \ +ATOMIC_TRACE_RMW_PRE ; \smp_mb (); \ cmp =atomic_read__nocheck (haddr); \ do { \@@ -195,6 +207,7 @@ ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr, \ cmp =atomic_cmpxchg__nocheck (haddr, old, new); \ }while (cmp != old); \ATOMIC_MMU_CLEANUP ; \ +ATOMIC_TRACE_RMW_POST ; \return RET ; \ }@@ -232,14 +245,16 @@ ABI_TYPE ATOMIC_NAME(cmpxchg)(CPUArchState *env, target_ulong addr,ATOMIC_MMU_DECLS ;DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ;DATA_TYPE ret; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); -ATOMIC_TRACE_RMW ; +ATOMIC_TRACE_RMW_PRE ;#if DATA_SIZE ==16 ret =atomic16_cmpxchg (haddr,BSWAP (cmpv),BSWAP (newv));#else ret =atomic_cmpxchg__nocheck (haddr,BSWAP (cmpv),BSWAP (newv));#endif ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_RMW_POST ;return BSWAP (ret); }@@ -249,10 +264,12 @@ ABI_TYPE ATOMIC_NAME(ld)(CPUArchState *env, target_ulong addr EXTRA_ARGS) {ATOMIC_MMU_DECLS ;DATA_TYPE val, *haddr =ATOMIC_MMU_LOOKUP ; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); -ATOMIC_TRACE_LD ; +ATOMIC_TRACE_LD_PRE ; val =atomic16_read (haddr);ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_LD_POST ;return BSWAP (val); }@@ -261,11 +278,14 @@ void ATOMIC_NAME(st)(CPUArchState *env, target_ulong addr, {ATOMIC_MMU_DECLS ;DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , true); -ATOMIC_TRACE_ST ; + val =BSWAP (val); +ATOMIC_TRACE_ST_PRE ; val =BSWAP (val);atomic16_set (haddr, val);ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_ST_POST ; }#endif #else @@ -275,10 +295,12 @@ ABI_TYPE ATOMIC_NAME(xchg)(CPUArchState *env, target_ulong addr,ATOMIC_MMU_DECLS ;DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ;ABI_TYPE ret; +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); -ATOMIC_TRACE_RMW ; +ATOMIC_TRACE_RMW_PRE ; ret =atomic_xchg__nocheck (haddr,BSWAP (val));ATOMIC_MMU_CLEANUP ; +ATOMIC_TRACE_RMW_POST ;return BSWAP (ret); }@@ -289,10 +311,12 @@ ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr, \ATOMIC_MMU_DECLS ; \DATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ; \DATA_TYPE ret; \ +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); \ \ -ATOMIC_TRACE_RMW ; \ +ATOMIC_TRACE_RMW_PRE ; \ ret = atomic_##X(haddr, BSWAP(val)); \ ATOMIC_MMU_CLEANUP; \ + ATOMIC_TRACE_RMW_POST; \ return BSWAP(ret); \ }@@ -319,8 +343,9 @@ ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr, \ATOMIC_MMU_DECLS ; \XDATA_TYPE *haddr =ATOMIC_MMU_LOOKUP ; \XDATA_TYPE ldo, ldn, old, new, val = xval; \ +uint8_t info =glue (trace_mem_build_info_no_se,MEND )(SHIFT , false); \ \ -ATOMIC_TRACE_RMW ; \ +ATOMIC_TRACE_RMW_PRE ; \smp_mb (); \ ldn =atomic_read__nocheck (haddr); \ do { \@@ -328,6 +353,7 @@ ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr, \ ldn =atomic_cmpxchg__nocheck (haddr, ldo,BSWAP (new)); \ }while (ldo != ldn); \ATOMIC_MMU_CLEANUP ; \ +ATOMIC_TRACE_RMW_POST ; \return RET ; \ }
accel/tcg/atomic_template.h GEN_ATOMIC_HELPER_FN(add_fetch, ADD, DATA_TYPE, new) · line 386
Quoted 10 lines
accel/tcg/atomic_template.h +0
-4@@ -355,10 +381,6 @@ GEN_ATOMIC_HELPER_FN(add_fetch, ADD, DATA_TYPE, new)#undef MEND#endif /* DATA_SIZE > 1 */ -#undef ATOMIC_TRACE_ST -#undef ATOMIC_TRACE_LD -#undef ATOMIC_TRACE_RMW -#undef BSWAP#undef ABI_TYPE#undef DATA_TYPE
--
Alex Bennée
Re: [Qemu-devel] [RFC 12/48] atomic_template: define pre/post macros
Quoted 10 lines · Alex Bennée
Quoted 3 lines · Emilio G. Cota
In preparation for plugin support.
Signed-off-by Emilio G. Cota <[email protected]>More macros for the macro-god. I guess this works but I wonder if it's possible to do a clean-up ala softfloat and the experimental softmmu re-factor that makes this less a mess of macros?
Heh, point taken.
I'll try to fix this once (and if!) the series graduates from RFC to proper patchset. BTW I'm planning to pick rth's softfloat cleanup soon, so that should help.
Thanks,
Emilio
Re: [Qemu-devel] [RFC 12/48] atomic_template: define pre/post macros
Quoted 12 lines · Emilio G. Cota
Quoted 10 lines · Alex Bennée
Quoted 3 lines · Emilio G. Cota
In preparation for plugin support.
Signed-off-by Emilio G. Cota <[email protected]>More macros for the macro-god. I guess this works but I wonder if it's possible to do a clean-up ala softfloat and the experimental softmmu re-factor that makes this less a mess of macros?
Quoted 5 lines
Heh, point taken.
I'll try to fix this once (and if!) the series graduates from RFC to proper patchset. BTW I'm planning to pick rth's softfloat cleanup soon, so that should help.
Yeah I was in the middle of re-spinning the softmmu demacro patches but ran into a bug I didn't get time to track down. I'll get it re-spun once I've finished going through this.
I assume the rth's softfloat cleanup is for hardfloat support? I stand ready to review the next version. We should get it in well before the 4.0 soft freeze (next release).
Quoted 4 lines
Thanks,
Emilio
--
Alex Bennée
Re: [Qemu-devel] [RFC 12/48] atomic_template: define pre/post macros
Quoted 17 lines · Alex Bennée
Quoted 14 lines · Emilio G. Cota
Quoted 10 lines · Alex Bennée
Quoted 3 lines · Emilio G. Cota
In preparation for plugin support.
Signed-off-by Emilio G. Cota <[email protected]>More macros for the macro-god. I guess this works but I wonder if it's possible to do a clean-up ala softfloat and the experimental softmmu re-factor that makes this less a mess of macros?
Heh, point taken.
Quoted 7 lines
Quoted 3 lines
I'll try to fix this once (and if!) the series graduates from RFC to proper patchset. BTW I'm planning to pick rth's softfloat cleanup soon, so that should help.
Yeah I was in the middle of re-spinning the softmmu demacro patches but ran into a bug I didn't get time to track down. I'll get it re-spun once I've finished going through this.
Nice!
Quoted 3 lines
I assume the rth's softfloat cleanup is for hardfloat support? I stand ready to review the next version. We should get it in well before the 4.0 soft freeze (next release).
Yes I meant hardfloat, not softfloat :P
I submitted v6 over the weekend. You'll be happy to hear that after applying rth's cleanups, the series only has four macros left, the largest of which only takes 8 lines :-)
Emilio
RFC 13/48 xxhash: add qemu_xxhash8
In reply to Emilio G. CotaIt will be used for TB hashing soon.
--- include/qemu/xxhash.h | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-)
include/qemu/xxhash.h +27 -13
@@ -49,7 +49,8 @@* contiguous in memory. */ static inline uint32_t -qemu_xxhash7 (uint64_t ab,uint64_t cd,uint32_t e,uint32_t f,uint32_t g) +qemu_xxhash8 (uint64_t ab,uint64_t cd,uint32_t e,uint32_t f,uint32_t g, +uint32_t h) {uint32_t v1 =QEMU_XXHASH_SEED +PRIME32_1 +PRIME32_2 ;uint32_t v2 =QEMU_XXHASH_SEED +PRIME32_2 ;
@@ -77,17 +78,24 @@ qemu_xxhash7(uint64_t ab, uint64_t cd, uint32_t e, uint32_t f, uint32_t g) v4 =rol32 (v4,13 ); v4 *=PRIME32_1 ; - h32 =rol32 (v1,1 ) +rol32 (v2,7 ) +rol32 (v3,12 ) +rol32 (v4,18 ); - h32 +=28 ; + v1 += e *PRIME32_2 ; + v1 =rol32 (v1,13 ); + v1 *=PRIME32_1 ; - h32 += e *PRIME32_3 ; - h32 =rol32 (h32,17 ) *PRIME32_4 ; + v2 += f *PRIME32_2 ; + v2 =rol32 (v2,13 ); + v2 *=PRIME32_1 ; + + v3 += g *PRIME32_2 ; + v3 =rol32 (v3,13 ); + v3 *=PRIME32_1 ; - h32 += f *PRIME32_3 ; - h32 =rol32 (h32,17 ) *PRIME32_4 ; + v4 += h *PRIME32_2 ; + v4 =rol32 (v4,13 ); + v4 *=PRIME32_1 ; - h32 += g *PRIME32_3 ; - h32 =rol32 (h32,17 ) *PRIME32_4 ; + h32 =rol32 (v1,1 ) +rol32 (v2,7 ) +rol32 (v3,12 ) +rol32 (v4,18 ); + h32 +=32 ; h32 ^= h32 >>15 ; h32 *=PRIME32_2 ;
@@ -100,23 +108,29 @@ qemu_xxhash7(uint64_t ab, uint64_t cd, uint32_t e, uint32_t f, uint32_t g)static inline uint32_t qemu_xxhash2 (uint64_t ab) { -return qemu_xxhash7 (ab,0 ,0 ,0 ,0 ); +return qemu_xxhash8 (ab,0 ,0 ,0 ,0 ,0 ); }static inline uint32_t qemu_xxhash4 (uint64_t ab,uint64_t cd) { -return qemu_xxhash7 (ab, cd,0 ,0 ,0 ); +return qemu_xxhash8 (ab, cd,0 ,0 ,0 ,0 ); }static inline uint32_t qemu_xxhash5 (uint64_t ab,uint64_t cd,uint32_t e) { -return qemu_xxhash7 (ab, cd, e,0 ,0 ); +return qemu_xxhash8 (ab, cd, e,0 ,0 ,0 ); }static inline uint32_t qemu_xxhash6 (uint64_t ab,uint64_t cd,uint32_t e,uint32_t f) { -return qemu_xxhash7 (ab, cd, e, f,0 ); +return qemu_xxhash8 (ab, cd, e, f,0 ,0 ); +} + +static inline uint32_t qemu_xxhash7 (uint64_t ab,uint64_t cd,uint32_t e, +uint32_t f,uint32_t g) +{ +return qemu_xxhash8 (ab, cd, e, f, g,0 ); }#endif /* QEMU_XXHASH_H */
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 13/48] xxhash: add qemu_xxhash8
Quoted 12 lines · Emilio G. Cota
It will be used for TB hashing soon.
Signed-off-by Emilio G. Cota <[email protected]>--- include/qemu/xxhash.h | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-)include/qemu/xxhash.h +0
-0
include/qemu/xxhash.h line 53
Quoted 6 lines
include/qemu/xxhash.h +2
-1@@ -49,7 +49,8 @@* contiguous in memory. */ static inline uint32_t -qemu_xxhash7 (uint64_t ab,uint64_t cd,uint32_t e,uint32_t f,uint32_t g) +qemu_xxhash8 (uint64_t ab,uint64_t cd,uint32_t e,uint32_t f,uint32_t g, +uint32_t h)
As we've expanded to bigger and bigger hashes why are everything after cd passed as 32 bit values? Isn't this just generating extra register pressure or is the compiler smart enough to figure it out?
Quoted 23 lines
include/qemu/xxhash.h +10
-4{uint32_t v1 =QEMU_XXHASH_SEED +PRIME32_1 +PRIME32_2 ;uint32_t v2 =QEMU_XXHASH_SEED +PRIME32_2 ;@@ -77,17 +78,24 @@ qemu_xxhash7(uint64_t ab, uint64_t cd, uint32_t e, uint32_t f, uint32_t g) v4 =rol32 (v4,13 ); v4 *=PRIME32_1 ; - h32 =rol32 (v1,1 ) +rol32 (v2,7 ) +rol32 (v3,12 ) +rol32 (v4,18 ); - h32 +=28 ; + v1 += e *PRIME32_2 ; + v1 =rol32 (v1,13 ); + v1 *=PRIME32_1 ; - h32 += e *PRIME32_3 ; - h32 =rol32 (h32,17 ) *PRIME32_4 ; + v2 += f *PRIME32_2 ; + v2 =rol32 (v2,13 ); + v2 *=PRIME32_1 ; + + v3 += g *PRIME32_2 ; + v3 =rol32 (v3,13 ); + v3 *=PRIME32_1 ;
include/qemu/xxhash.h qemu_xxhash7(uint64_t ab, uint64_t cd, uint32_t e, uint32_t f, uint32_t g) · line 98
Quoted 10 lines
include/qemu/xxhash.h +5
-4- h32 += f *PRIME32_3 ; - h32 =rol32 (h32,17 ) *PRIME32_4 ; + v4 += h *PRIME32_2 ; + v4 =rol32 (v4,13 ); + v4 *=PRIME32_1 ; - h32 += g *PRIME32_3 ; - h32 =rol32 (h32,17 ) *PRIME32_4 ; + h32 =rol32 (v1,1 ) +rol32 (v2,7 ) +rol32 (v3,12 ) +rol32 (v4,18 ); + h32 +=32 ;
How do we validate we haven't broken the distribution of the original xxhash as we've extended it?
Quoted 27 lines
include/qemu/xxhash.h +3
-4h32 ^= h32 >>15 ; h32 *=PRIME32_2 ;@@ -100,23 +108,29 @@ qemu_xxhash7(uint64_t ab, uint64_t cd, uint32_t e, uint32_t f, uint32_t g)static inline uint32_t qemu_xxhash2 (uint64_t ab) { -return qemu_xxhash7 (ab,0 ,0 ,0 ,0 ); +return qemu_xxhash8 (ab,0 ,0 ,0 ,0 ,0 ); }static inline uint32_t qemu_xxhash4 (uint64_t ab,uint64_t cd) { -return qemu_xxhash7 (ab, cd,0 ,0 ,0 ); +return qemu_xxhash8 (ab, cd,0 ,0 ,0 ,0 ); }static inline uint32_t qemu_xxhash5 (uint64_t ab,uint64_t cd,uint32_t e) { -return qemu_xxhash7 (ab, cd, e,0 ,0 ); +return qemu_xxhash8 (ab, cd, e,0 ,0 ,0 ); }static inline uint32_t qemu_xxhash6 (uint64_t ab,uint64_t cd,uint32_t e,uint32_t f) { -return qemu_xxhash7 (ab, cd, e, f,0 );
include/qemu/xxhash.h qemu_xxhash7(uint64_t ab, uint64_t cd, uint32_t e, uint32_t f, uint32_t g) · line 136
Quoted 10 lines
include/qemu/xxhash.h +7
-0+ return qemu_xxhash8(ab, cd, e, f, 0, 0); +} + +static inline uint32_t qemu_xxhash7 (uint64_t ab,uint64_t cd,uint32_t e, +uint32_t f,uint32_t g) +{ +return qemu_xxhash8 (ab, cd, e, f, g,0 ); }#endif /* QEMU_XXHASH_H */
--
Alex Bennée
Re: [Qemu-devel] [RFC 13/48] xxhash: add qemu_xxhash8
Quoted 10 lines · Alex Bennée
Quoted 7 lines · Emilio G. Cota
It will be used for TB hashing soon.
Signed-off-by Emilio G. Cota <[email protected]>--- include/qemu/xxhash.h | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-)
Quoted 15 lines
include/qemu/xxhash.h line 53
Quoted 11 lines
include/qemu/xxhash.h +2
-1@@ -49,7 +49,8 @@* contiguous in memory. */ static inline uint32_t -qemu_xxhash7 (uint64_t ab,uint64_t cd,uint32_t e,uint32_t f,uint32_t g) +qemu_xxhash8 (uint64_t ab,uint64_t cd,uint32_t e,uint32_t f,uint32_t g, +uint32_t h)As we've expanded to bigger and bigger hashes why are everything after cd passed as 32 bit values? Isn't this just generating extra register pressure or is the compiler smart enough to figure it out?
The latter -- the compiler does a good job with constant propagation.
Quoted 12 lines
Quoted 12 lines
include/qemu/xxhash.h +3
-2{uint32_t v1 =QEMU_XXHASH_SEED +PRIME32_1 +PRIME32_2 ;uint32_t v2 =QEMU_XXHASH_SEED +PRIME32_2 ;@@ -77,17 +78,24 @@ qemu_xxhash7(uint64_t ab, uint64_t cd, uint32_t e, uint32_t f, uint32_t g) v4 =rol32 (v4,13 ); v4 *=PRIME32_1 ; - h32 =rol32 (v1,1 ) +rol32 (v2,7 ) +rol32 (v3,12 ) +rol32 (v4,18 ); - h32 +=28 ; + v1 += e *PRIME32_2 ; + v1 =rol32 (v1,13 ); + v1 *=PRIME32_1 ;
(snip)
Quoted 2 lines
How do we validate we haven't broken the distribution of the original xxhash as we've extended it?
We weren't testing for that, so this is a valid concern.
I just added a test to my xxhash repo to compare our version against the original:
https://github.com/cota/xxhash/tree/qemu
Turns out that to exactly match the original we just have to swap our a/b and c/d pairs. I don't see how this could cause a loss of randomness, but just to be safe I'll send a for-4.0 patch so that our output matches the original one.
Thanks,
Emilio
RFC 14/48 plugin: preliminary user-facing API
In reply to Emilio G. CotaAdd the API first to ease review.
--- include/qemu/plugin-api.h | 227 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100644 include/qemu/plugin-api.h
include/qemu/plugin-api.h new file +227 -0
@@ -0,0 +1,227 @@ +/* +* Copyright (C) 2017, Emilio G. Cota <[email protected]> +* +* License: GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ +#ifndef QEMU_PLUGIN_API_H +#define QEMU_PLUGIN_API_H + +#include <inttypes.h> +#include <stdbool.h> + +/* +* For best performance, build the plugin with -fvisibility=hidden so that +* QEMU_PLUGIN_LOCAL is implicit. Then, just mark qemu_plugin_install with +* QEMU_PLUGIN_EXPORT. For more info, see +* https://gcc.gnu.org/wiki/Visibility +*/ +#if defined _WIN32 || defined __CYGWIN__ +#ifdef BUILDING_DLL +#define QEMU_PLUGIN_EXPORT __declspec(dllexport) +#else +#define QEMU_PLUGIN_EXPORT __declspec(dllimport) +#endif +#define QEMU_PLUGIN_LOCAL +#else +#if __GNUC__ >=4 +#define QEMU_PLUGIN_EXPORT __attribute__((visibility("default"))) +#define QEMU_PLUGIN_LOCAL __attribute__((visibility("hidden"))) +#else +#define QEMU_PLUGIN_EXPORT +#define QEMU_PLUGIN_LOCAL +#endif +#endif + +typedef uint64_t qemu_plugin_id_t ; + +/** +* qemu_plugin_install - Install a plugin +* @id: this plugin's opaque ID +* @argc: number of arguments +* @argv: array of arguments (@argc elements) +* +* All plugins must export this symbol. +* +* Note: @argv is freed after this function returns. +*/ +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, int argc, +char **argv); + +typedef void (*qemu_plugin_uninstall_cb_t )(qemu_plugin_id_t id); + +/** +* qemu_plugin_uninstall - Uninstall a plugin +* @id: this plugin's opaque ID +* @cb: callback to be called once the plugin has been removed +* +* Do NOT assume that the plugin has been uninstalled once this +* function returns. Plugins are uninstalled asynchronously, +* and therefore the given plugin might still receive callbacks +* from prior subscriptions _until_ @cb is called. +*/ +void qemu_plugin_uninstall (qemu_plugin_id_t id,qemu_plugin_uninstall_cb_t cb); + +typedef void (*qemu_plugin_simple_cb_t )(qemu_plugin_id_t id); + +typedef void (*qemu_plugin_udata_cb_t )(qemu_plugin_id_t id,void *userdata); + +typedef void (*qemu_plugin_vcpu_simple_cb_t )(qemu_plugin_id_t id, +unsigned int vcpu_index); + +typedef void (*qemu_plugin_vcpu_udata_cb_t )(unsigned int vcpu_index, +void *userdata); + +/** +* qemu_plugin_register_vcpu_init_cb - register a vCPU initialization callback +* @id: plugin ID +* @cb: callback function +* +* The @cb function is called every time a vCPU is initialized. +* +* See also: qemu_plugin_register_vcpu_exit_cb() +*/ +void qemu_plugin_register_vcpu_init_cb (qemu_plugin_id_t id, +qemu_plugin_vcpu_simple_cb_t cb); + +/** +* qemu_plugin_register_vcpu_exit_cb - register a vCPU exit callback +* @id: plugin ID +* @cb: callback function +* +* The @cb function is called every time a vCPU exits. +* +* See also: qemu_plugin_register_vcpu_init_cb() +*/ +void qemu_plugin_register_vcpu_exit_cb (qemu_plugin_id_t id, +qemu_plugin_vcpu_simple_cb_t cb); + +void qemu_plugin_register_vcpu_idle_cb (qemu_plugin_id_t id, +qemu_plugin_vcpu_simple_cb_t cb); + +void qemu_plugin_register_vcpu_resume_cb (qemu_plugin_id_t id, +qemu_plugin_vcpu_simple_cb_t cb); + +struct qemu_plugin_tb ; +struct qemu_plugin_insn ; + +enum qemu_plugin_cb_flags { +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 */ +}; + +typedef void (*qemu_plugin_vcpu_tb_trans_cb_t )(qemu_plugin_id_t id, +unsigned int vcpu_index, +struct qemu_plugin_tb *tb); + +void qemu_plugin_register_vcpu_tb_trans_cb (qemu_plugin_id_t id, +qemu_plugin_vcpu_tb_trans_cb_t cb); + +/* can only call from tb_trans_cb callback */ +void qemu_plugin_register_vcpu_tb_exec_cb (struct qemu_plugin_tb *tb, +qemu_plugin_vcpu_udata_cb_t cb, +enum qemu_plugin_cb_flags flags, +void *userdata); + +enum qemu_plugin_op { +QEMU_PLUGIN_INLINE_ADD_U64 , +}; + +void qemu_plugin_register_vcpu_tb_exec_inline (struct qemu_plugin_tb *tb, +enum qemu_plugin_op op, +void *ptr,uint64_t imm); + +void qemu_plugin_register_vcpu_insn_exec_cb (struct qemu_plugin_insn *insn, +qemu_plugin_vcpu_udata_cb_t cb, +enum qemu_plugin_cb_flags flags, +void *userdata); + +typedef uint32_t qemu_plugin_meminfo_t ; + +unsigned qemu_plugin_mem_size_shift (qemu_plugin_meminfo_t info); +bool qemu_plugin_mem_is_sign_extended (qemu_plugin_meminfo_t info); +bool qemu_plugin_mem_is_big_endian (qemu_plugin_meminfo_t info); +bool qemu_plugin_mem_is_store (qemu_plugin_meminfo_t info); + +typedef void +(*qemu_plugin_vcpu_mem_cb_t )(unsigned int vcpu_index, +qemu_plugin_meminfo_t info,uint64_t vaddr, +void *userdata); + +typedef void +(*qemu_plugin_vcpu_mem_haddr_cb_t )(unsigned int vcpu_index, +qemu_plugin_meminfo_t info,uint64_t vaddr, +void *haddr,void *userdata); + +void qemu_plugin_register_vcpu_mem_cb (struct qemu_plugin_insn *insn, +qemu_plugin_vcpu_mem_cb_t cb, +enum qemu_plugin_cb_flags flags, +void *userdata); + +void qemu_plugin_register_vcpu_mem_haddr_cb (struct qemu_plugin_insn *insn, +qemu_plugin_vcpu_mem_haddr_cb_t cb, +enum qemu_plugin_cb_flags flags, +void *userdata); + +void qemu_plugin_register_vcpu_mem_inline (struct qemu_plugin_insn *insn, +enum qemu_plugin_op op,void *ptr, +uint64_t imm); + +uint64_t qemu_plugin_ram_addr_from_host (void *haddr); + +typedef void +(*qemu_plugin_vcpu_syscall_cb_t )(qemu_plugin_id_t id,unsigned int vcpu_index, +int64_t num,uint64_t a1,uint64_t a2, +uint64_t a3,uint64_t a4,uint64_t a5, +uint64_t a6,uint64_t a7,uint64_t a8); + +void qemu_plugin_register_vcpu_syscall_cb (qemu_plugin_id_t id, +qemu_plugin_vcpu_syscall_cb_t cb); + +typedef void +(*qemu_plugin_vcpu_syscall_ret_cb_t )(qemu_plugin_id_t id,unsigned int vcpu_idx, +int64_t num,int64_t ret); + +void +qemu_plugin_register_vcpu_syscall_ret_cb (qemu_plugin_id_t id, +qemu_plugin_vcpu_syscall_ret_cb_t cb); + +size_t qemu_plugin_tb_n_insns (const struct qemu_plugin_tb *tb); + +uint64_t qemu_plugin_tb_vaddr (const struct qemu_plugin_tb *tb); + +struct qemu_plugin_insn * +qemu_plugin_tb_get_insn (const struct qemu_plugin_tb *tb,size_t idx); + +const void *qemu_plugin_insn_data (const struct qemu_plugin_insn *insn); + +size_t qemu_plugin_insn_size (const struct qemu_plugin_insn *insn); + +uint64_t qemu_plugin_insn_vaddr (const struct qemu_plugin_insn *insn); + +/** +* qemu_plugin_vcpu_for_each - iterate over the existing vCPU +* @id: plugin ID +* @cb: callback function +* +* The @cb function is called once for each existing vCPU. +* +* See also: qemu_plugin_register_vcpu_init_cb() +*/ +void qemu_plugin_vcpu_for_each (qemu_plugin_id_t id, +qemu_plugin_vcpu_simple_cb_t cb); + +void qemu_plugin_register_flush_cb (qemu_plugin_id_t id, +qemu_plugin_simple_cb_t cb); + +void qemu_plugin_register_atexit_cb (qemu_plugin_id_t id, +qemu_plugin_udata_cb_t cb,void *userdata); + +/* returns -1 in user-mode */ +int qemu_plugin_n_vcpus (void ); + +/* returns -1 in user-mode */ +int qemu_plugin_n_max_vcpus (void ); + +#endif /* QEMU_PLUGIN_API_H */
Signature · 1 line
2.17.1
RFC 15/48 plugin: add core code
In reply to Emilio G. CotaThe goals are to:
- Have a simple implementation that shares nothing with tracing code.
- Make sure we cannot deadlock, particularly under MTTCG. For this, we acquire a lock when called from plugin code, and keep
RCU lists of callbacks so that we do not have to hold the lock when calling the callbacks. This is also for performance, since some callbacks (e.g. memory access callbacks) might be called very frequently.
* A consequence of this is that we keep our own list of CPUs, so that we do not have to worry about locking order wrt cpu_list_lock.
* Some functions such as vcpu_for_each will call plugin code with the lock held. Instead of making the lock recursive, just document that the function called by vcpu_for_each cannot call any other plugin API.
- Support as many plugins as the user wants (e.g. -plugin foo -plugin bar), just like other tools (e.g. dynamorio) do.
- Support the installation/uninstallation of a plugin any time (i.e. from any callback from the guest, or from QAPI/QMP).
- Avoid malicious plugins from abusing the API. This is done by:
* Adding a qemu_plugin_id_t that all calls need to use. This is a unique id per plugin.
* Hiding CPUState * under cpu_index. Plugin code can keep per-vcpu data by using said index (say to index an array).
* Only exporting the relevant qemu_plugin symbols to the plugins by passing --dynamic-file to the linker (when supported), instead of exporting all symbols with -rdynamic.
- Performance: registering/unregistering callbacks is slow. But this is very infrequent; we want performance when calling (or not) callbacks. Using RCU is great for this. The only difficulty is when uninstalling a plugin, where some callbacks might still be called after the uninstall returns. An alternative would be to use r/w locks, but that would complicate code quite a bit for very little gain; I suspect most plugins will just run until QEMU exits.
Some design decisions:
- I considered registering callbacks per-vcpu, but really I don't see the use case for it (would complicate the API and 99% of plugins won't care, so I'd rather make that 1% slower by letting them discard unwanted callbacks).
- Using a per-vcpu mask is key to allow for maximum performance/scalability. With RCU callbacks and async work we can ensure no events are missed, and we keep maximum scalability (again, we assume callbacks are a lot more common than callback registration).
- Last, 'plugin' vs. 'instrumentation' naming: I think instrumentation is a subset of the functionality that plugins can provide. IOW, in the future not all plugins might be considered instrumentation, even if currently my goal is to use them for that purpose.
--- Makefile | 7 +- Makefile.target | 2 + include/qemu/plugin.h | 239 ++++++++++ include/qom/cpu.h | 6 + plugin.c | 981 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 1234 insertions(+), 1 deletion(-) create mode 100644 include/qemu/plugin.h create mode 100644 plugin.c
Makefile +6 -1
@@ -862,8 +862,10 @@ ifneq (,$(findstring qemu-ga,$(TOOLS))) endif endif +install-includedir: + $(INSTALL_DIR) "$(DESTDIR)$(includedir)" -install: all $(if $(BUILD_DOCS),install-doc) install-datadir install-localstatedir +install: all $(if $(BUILD_DOCS),install-doc) install-datadir install-localstatedir install-includedir ifneq ($(TOOLS),) $(call install-prog,$(subst qemu-ga,qemu-ga$(EXESUF),$(TOOLS)),$(DESTDIR)$(bindir)) endif
@@ -885,6 +887,9 @@ ifneq ($(BLOBS),) endif ifdef CONFIG_GTK $(MAKE) -C po $@ +endif +ifeq ($(CONFIG_PLUGINS),y) + $(INSTALL_DATA) $(SRC_PATH)/include/qemu/plugin-api.h "$(DESTDIR)$(includedir)/qemu-plugin.h" endif $(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)/keymaps" set -e; for x in $(KEYMAPS); do \
Makefile.target +2 -0
@@ -107,6 +107,8 @@ obj-y += target/$(TARGET_BASE_ARCH)/ obj-y += disas.o obj-$(call notempty,$(TARGET_XML_FILES)) += gdbstub-xml.o +obj-$(CONFIG_PLUGINS) += plugin.o + ######################################################### # Linux user emulator target
include/qemu/plugin.h new file +239 -0
239 lines not on this page · raw message
include/qom/cpu.h +6 -0
@@ -30,6 +30,7 @@#include "qemu/rcu_queue.h" #include "qemu/queue.h" #include "qemu/thread.h" +#include "qemu/plugin.h" typedef int (*WriteCoreDumpFunction )(const void *buf,size_t size,void *opaque);
@@ -326,6 +327,7 @@ struct qemu_work_item;* @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes * to @trace_dstate). * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask). +* @plugin_mask: Plugin event bitmap. Modified only via async work. * @ignore_memory_transaction_failures: Cached copy of the MachineState * flag of the same name: allows the board to suppress calling of the * CPU do_transaction_failed hook function.
@@ -413,6 +415,10 @@ struct CPUState {DECLARE_BITMAP (trace_dstate_delayed,CPU_TRACE_DSTATE_MAX_EVENTS );DECLARE_BITMAP (trace_dstate,CPU_TRACE_DSTATE_MAX_EVENTS ); +DECLARE_BITMAP (plugin_mask,QEMU_PLUGIN_EV_MAX ); + +struct qemu_plugin_dyn_cb_arr *plugin_mem_cbs; +/* TODO Move common fields from CPUArchState here. */ int cpu_index;uint32_t can_do_io;
plugin.c new file +981 -0
981 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC 16/48 tcg: add plugin_mask to TB hash
In reply to Emilio G. Cota--- include/exec/exec-all.h | 2 ++ include/exec/tb-hash.h | 6 ++++-- include/exec/tb-lookup.h | 1 + accel/tcg/cpu-exec.c | 6 +++++- accel/tcg/translate-all.c | 6 ++++-- 5 files changed, 16 insertions(+), 5 deletions(-)
include/exec/exec-all.h +2 -0
@@ -358,6 +358,8 @@ struct TranslationBlock {/* Per-vCPU dynamic tracing state used to generate this TB */ uint32_t trace_vcpu_dstate; +uint32_t plugin_mask; +struct tb_tc tc;/* original tb when cflags has CF_NOCACHE */
include/exec/tb-hash.h +4 -2
@@ -59,9 +59,11 @@ static inline unsigned int tb_jmp_cache_hash_func(target_ulong pc)static inline uint32_t tb_hash_func (tb_page_addr_t phys_pc,target_ulong pc,uint32_t flags, -uint32_t cf_mask,uint32_t trace_vcpu_dstate) +uint32_t cf_mask,uint32_t trace_vcpu_dstate, +uint32_t plugin_mask) { -return qemu_xxhash7 (phys_pc, pc, flags, cf_mask, trace_vcpu_dstate); +return qemu_xxhash8 (phys_pc, pc, flags, cf_mask, trace_vcpu_dstate, + plugin_mask); }#endif
include/exec/tb-lookup.h +1 -0
@@ -33,6 +33,7 @@ tb_lookup__cpu_state(CPUState *cpu, target_ulong *pc, target_ulong *cs_base, tb->cs_base == *cs_base && tb->flags == *flags && tb->trace_vcpu_dstate == *cpu->trace_dstate && + tb->plugin_mask == *cpu->plugin_mask && (tb_cflags(tb) & (CF_HASH_MASK | CF_INVALID)) == cf_mask)) {return tb; }
accel/tcg/cpu-exec.c +5 -1
@@ -287,6 +287,7 @@ struct tb_desc {uint32_t flags;uint32_t cf_mask;uint32_t trace_vcpu_dstate; +uint32_t plugin_mask; };static bool tb_lookup_cmp (const void *p,const void *d)
@@ -299,6 +300,7 @@ static bool tb_lookup_cmp(const void *p, const void *d) tb->cs_base == desc->cs_base && tb->flags == desc->flags && tb->trace_vcpu_dstate == desc->trace_vcpu_dstate && + tb->plugin_mask == desc->plugin_mask && (tb_cflags(tb) & (CF_HASH_MASK | CF_INVALID)) == desc->cf_mask) { /* check next page if needed */ if (tb->page_addr[1] == -1) {
@@ -330,13 +332,15 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc, desc.flags = flags; desc.cf_mask = cf_mask; desc.trace_vcpu_dstate = *cpu->trace_dstate; + desc.plugin_mask = *cpu->plugin_mask; desc.pc = pc; phys_pc =get_page_addr_code (desc.env, pc);if (phys_pc ==-1 ) {return NULL ; } desc.phys_page1 = phys_pc &TARGET_PAGE_MASK ; - h =tb_hash_func (phys_pc, pc, flags, cf_mask, *cpu->trace_dstate); + h =tb_hash_func (phys_pc, pc, flags, cf_mask, *cpu->trace_dstate, + *cpu->plugin_mask);return qht_lookup_custom (&tb_ctx.htable, &desc, h, tb_lookup_cmp); }
accel/tcg/translate-all.c +4 -2
@@ -1129,6 +1129,7 @@ static bool tb_cmp(const void *ap, const void *bp) a->flags == b->flags && (tb_cflags (a) &CF_HASH_MASK ) == (tb_cflags (b) &CF_HASH_MASK ) && a->trace_vcpu_dstate == b->trace_vcpu_dstate && + a->plugin_mask == b->plugin_mask && a->page_addr[0 ] == b->page_addr[0 ] && a->page_addr[1 ] == b->page_addr[1 ]; }
@@ -1444,7 +1445,7 @@ static void do_tb_phys_invalidate(TranslationBlock *tb, bool rm_from_page_list)/* remove the TB from the hash list */ phys_pc = tb->page_addr[0 ] + (tb->pc & ~TARGET_PAGE_MASK ); h =tb_hash_func (phys_pc, tb->pc, tb->flags,tb_cflags (tb) &CF_HASH_MASK , - tb->trace_vcpu_dstate); + tb->trace_vcpu_dstate, tb->plugin_mask);if (!(tb->cflags &CF_NOCACHE ) && !qht_remove (&tb_ctx.htable, tb, h)) {return ;
@@ -1640,7 +1641,7 @@ tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc,/* add in the hash table */ h =tb_hash_func (phys_pc, tb->pc, tb->flags, tb->cflags &CF_HASH_MASK , - tb->trace_vcpu_dstate); + tb->trace_vcpu_dstate, tb->plugin_mask);qht_insert (&tb_ctx.htable, tb, h, &existing_tb);/* remove TB from the page(s) if we couldn't insert it */
@@ -1712,6 +1713,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu, tb->cflags = cflags; tb->trace_vcpu_dstate = *cpu->trace_dstate; tcg_ctx->tb_cflags = cflags; + tb->plugin_mask = *cpu->plugin_mask;#ifdef CONFIG_PROFILER /* includes aborted translations because of exceptions */
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 16/48] tcg: add plugin_mask to TB hash
Quoted 1 line · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>
This commit message needs more description. What is this plugin mask for? Are we extending the TB flags with a mask of loaded plugins so we can correctly identify what code was generated under what plugins?
Quoted 119 lines
--- include/exec/exec-all.h | 2 ++ include/exec/tb-hash.h | 6 ++++-- include/exec/tb-lookup.h | 1 + accel/tcg/cpu-exec.c | 6 +++++- accel/tcg/translate-all.c | 6 ++++-- 5 files changed, 16 insertions(+), 5 deletions(-)include/exec/exec-all.h +2
-0@@ -358,6 +358,8 @@ struct TranslationBlock {/* Per-vCPU dynamic tracing state used to generate this TB */ uint32_t trace_vcpu_dstate; +uint32_t plugin_mask; +struct tb_tc tc;/* original tb when cflags has CF_NOCACHE */ include/exec/tb-hash.h +4
-2@@ -59,9 +59,11 @@ static inline unsigned int tb_jmp_cache_hash_func(target_ulong pc)static inline uint32_t tb_hash_func (tb_page_addr_t phys_pc,target_ulong pc,uint32_t flags, -uint32_t cf_mask,uint32_t trace_vcpu_dstate) +uint32_t cf_mask,uint32_t trace_vcpu_dstate, +uint32_t plugin_mask) { -return qemu_xxhash7 (phys_pc, pc, flags, cf_mask, trace_vcpu_dstate); +return qemu_xxhash8 (phys_pc, pc, flags, cf_mask, trace_vcpu_dstate, + plugin_mask); }#endif include/exec/tb-lookup.h +1
-0@@ -33,6 +33,7 @@ tb_lookup__cpu_state(CPUState *cpu, target_ulong *pc, target_ulong *cs_base, tb->cs_base == *cs_base && tb->flags == *flags && tb->trace_vcpu_dstate == *cpu->trace_dstate && + tb->plugin_mask == *cpu->plugin_mask && (tb_cflags(tb) & (CF_HASH_MASK | CF_INVALID)) == cf_mask)) {return tb; }accel/tcg/cpu-exec.c +5
-1@@ -287,6 +287,7 @@ struct tb_desc {uint32_t flags;uint32_t cf_mask;uint32_t trace_vcpu_dstate; +uint32_t plugin_mask; };static bool tb_lookup_cmp (const void *p,const void *d)@@ -299,6 +300,7 @@ static bool tb_lookup_cmp(const void *p, const void *d) tb->cs_base == desc->cs_base && tb->flags == desc->flags && tb->trace_vcpu_dstate == desc->trace_vcpu_dstate && + tb->plugin_mask == desc->plugin_mask && (tb_cflags(tb) & (CF_HASH_MASK | CF_INVALID)) == desc->cf_mask) { /* check next page if needed */ if (tb->page_addr[1] == -1) {@@ -330,13 +332,15 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc, desc.flags = flags; desc.cf_mask = cf_mask; desc.trace_vcpu_dstate = *cpu->trace_dstate; + desc.plugin_mask = *cpu->plugin_mask; desc.pc = pc; phys_pc =get_page_addr_code (desc.env, pc);if (phys_pc ==-1 ) {return NULL ; } desc.phys_page1 = phys_pc &TARGET_PAGE_MASK ; - h =tb_hash_func (phys_pc, pc, flags, cf_mask, *cpu->trace_dstate); + h =tb_hash_func (phys_pc, pc, flags, cf_mask, *cpu->trace_dstate, + *cpu->plugin_mask);return qht_lookup_custom (&tb_ctx.htable, &desc, h, tb_lookup_cmp); }accel/tcg/translate-all.c +3
-2@@ -1129,6 +1129,7 @@ static bool tb_cmp(const void *ap, const void *bp) a->flags == b->flags && (tb_cflags (a) &CF_HASH_MASK ) == (tb_cflags (b) &CF_HASH_MASK ) && a->trace_vcpu_dstate == b->trace_vcpu_dstate && + a->plugin_mask == b->plugin_mask && a->page_addr[0 ] == b->page_addr[0 ] && a->page_addr[1 ] == b->page_addr[1 ]; }@@ -1444,7 +1445,7 @@ static void do_tb_phys_invalidate(TranslationBlock *tb, bool rm_from_page_list)/* remove the TB from the hash list */ phys_pc = tb->page_addr[0 ] + (tb->pc & ~TARGET_PAGE_MASK ); h =tb_hash_func (phys_pc, tb->pc, tb->flags,tb_cflags (tb) &CF_HASH_MASK , - tb->trace_vcpu_dstate); + tb->trace_vcpu_dstate, tb->plugin_mask);if (!(tb->cflags &CF_NOCACHE ) && !qht_remove (&tb_ctx.htable, tb, h)) {return ;@@ -1640,7 +1641,7 @@ tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc,/* add in the hash table */ h =tb_hash_func (phys_pc, tb->pc, tb->flags, tb->cflags &CF_HASH_MASK , - tb->trace_vcpu_dstate); + tb->trace_vcpu_dstate, tb->plugin_mask);qht_insert (&tb_ctx.htable, tb, h, &existing_tb);/* remove TB from the page(s) if we couldn't insert it */
accel/tcg/translate-all.c TranslationBlock *tb_gen_code(CPUState *cpu, · line 1719
Quoted 7 lines
accel/tcg/translate-all.c +1
-0@@ -1712,6 +1713,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu, tb->cflags = cflags; tb->trace_vcpu_dstate = *cpu->trace_dstate; tcg_ctx->tb_cflags = cflags; + tb->plugin_mask = *cpu->plugin_mask;#ifdef CONFIG_PROFILER /* includes aborted translations because of exceptions */
--
Alex Bennée
Re: [Qemu-devel] [RFC 16/48] tcg: add plugin_mask to TB hash
Quoted 8 lines · Alex Bennée
Quoted 1 line · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>This commit message needs more description. What is this plugin mask for? Are we extending the TB flags with a mask of loaded plugins so we can correctly identify what code was generated under what plugins?
This is similar to the bitmap for trace events, which we added to the TB hash so that when the TCG events have no subscribers left, we just regenerate the TBs.
It's true though that in this RFC there are no good use cases for this, because the callbacks that affect performance are all unconditionally embedded in the generated code. So at least for now we could drop this patch.
Thanks,
Emilio
RFC 17/48 plugin-gen: add TCG code generation helpers
In reply to Emilio G. CotaHere we implement several features:
- Inlining TCG code for simple operations. Crucially, we do not export TCG ops to plugins. Instead, we give them a C API to insert inlined ops. So far we only support adding an immediate to a u64, e.g. to count events.
- "Direct" callbacks. These are callbacks that do not go via a helper. Instead, the helper is defined at run-time, so that the plugin code is directly called from TCG. This makes direct callbacks as efficient as possible; they are therefore used for very frequent events, e.g. memory callbacks.
- Passing the host address to memory callbacks. Most of this is implemented in a later patch though.
- Instrumentation of memory accesses performed from helpers. See comment.
--- include/exec/plugin-gen.h | 51 +++++++++ accel/tcg/plugin-gen.c | 230 ++++++++++++++++++++++++++++++++++++++ accel/tcg/Makefile.objs | 1 + 3 files changed, 282 insertions(+) create mode 100644 include/exec/plugin-gen.h create mode 100644 accel/tcg/plugin-gen.c
include/exec/plugin-gen.h new file +51 -0
@@ -0,0 +1,51 @@ +/* +* Copyright (C) 2017, Emilio G. Cota <[email protected]> +* +* License: GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +* plugin-gen.h - TCG-dependent definitions for generating plugin code +* +* This header should be included only from plugin.c and C files that emit +* TCG code. +*/ +#ifndef QEMU_PLUGIN_GEN_H +#define QEMU_PLUGIN_GEN_H + +#include "qemu/plugin.h" +#include "tcg/tcg.h" + +#ifdef CONFIG_PLUGINS + +void qemu_plugin_gen_vcpu_mem_callbacks (struct qemu_plugin_dyn_cb_arr *arr, +TCGv vaddr,uint8_t info); + +void qemu_plugin_gen_vcpu_udata_callbacks (struct qemu_plugin_dyn_cb_arr *arr); + +void qemu_plugin_gen_disable_mem_helpers (void ); + +void +qemu_plugin_gen_enable_mem_helpers (const struct qemu_plugin_dyn_cb_arr *orig); + +#else /* !CONFIG_PLUGINS */ + +static inline void +qemu_plugin_gen_vcpu_mem_callbacks (struct qemu_plugin_dyn_cb_arr *arr, +TCGv vaddr,uint8_t info) +{ } + +static inline void +qemu_plugin_gen_vcpu_udata_callbacks (struct qemu_plugin_dyn_cb_arr *arr) +{ } + +static inline void qemu_plugin_gen_disable_mem_helpers (void ) +{ } + +static inline void +qemu_plugin_gen_enable_mem_helpers (const struct qemu_plugin_dyn_cb_arr *orig) +{ } + +#endif /* CONFIG_PLUGINS */ + +#endif /* QEMU_PLUGIN_GEN_H */ +
accel/tcg/plugin-gen.c new file +230 -0
230 lines not on this page · raw message
accel/tcg/Makefile.objs +1 -0
@@ -6,3 +6,4 @@ obj-y += translator.o obj-$(CONFIG_USER_ONLY) += user-exec.o obj-$(call lnot,$(CONFIG_SOFTMMU)) += user-exec-stub.o +obj-$(CONFIG_PLUGINS) += plugin-gen.o
Signature · 1 line
2.17.1
RFC 18/48 tcg: add memory callbacks for plugins (WIP)
In reply to Emilio G. CotaXXX: store hostaddr from non-i386 TCG backends
XXX: what hostaddr to return for I/O accesses?
XXX: what hostaddr to return for cross-page accesses?
Here the trickiest feature is passing the host address to memory callbacks that request it. Perhaps it would be more appropriate to pass a "physical" address to plugins, but since in QEMU host addr ~= guest physical, I'm going with that for simplicity.
To keep the implementation simple we piggy-back on the TLB fast path, and thus can only provide the host address _after_ memory accesses have occurred. For the slow path, it's a bit tedious because there are many places to update, but it's fairly simple.
However, note that cross-page accesses are tricky, since the access might be to non-contiguous host addresses. So I'm punting on that and just passing NULL.
--- accel/tcg/atomic_template.h | 8 ++++- accel/tcg/softmmu_template.h | 39 ++++++++++++++++++++ include/exec/cpu-defs.h | 2 ++ include/exec/cpu_ldst_template.h | 43 +++++++++++++++-------- include/exec/cpu_ldst_useronly_template.h | 42 +++++++++++++++------- tcg/tcg-op.h | 5 +++ tcg/tcg.h | 4 +++ tcg/i386/tcg-target.inc.c | 5 +++ tcg/tcg-op.c | 37 ++++++++++++++----- tcg/tcg.c | 3 ++ 10 files changed, 152 insertions(+), 36 deletions(-)
accel/tcg/atomic_template.h +7 -1
@@ -18,6 +18,7 @@* License along with this library; if not, see <http://www.gnu.org/licenses/>. */ +#include "qemu/plugin.h" #include "trace/mem.h" #if DATA_SIZE ==16
@@ -66,17 +67,22 @@trace_guest_mem_before_exec (ENV_GET_CPU (env), addr, info |TRACE_MEM_ST ); \ } while (0) -# define ATOMIC_TRACE_RMW_POST \ +# define ATOMIC_TRACE_RMW_POST do { \ + qemu_plugin_vcpu_mem_cb(ENV_GET_CPU(env), addr, haddr, info); \ + qemu_plugin_vcpu_mem_cb(ENV_GET_CPU(env), addr, haddr, info | TRACE_MEM_ST); \ +} while (0)# define ATOMIC_TRACE_LD_PRE \ trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info)# define ATOMIC_TRACE_LD_POST \ + qemu_plugin_vcpu_mem_cb(ENV_GET_CPU(env), addr, haddr, info)# define ATOMIC_TRACE_ST_PRE \ trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info)# define ATOMIC_TRACE_ST_POST \ + qemu_plugin_vcpu_mem_cb(ENV_GET_CPU(env), addr, haddr, info)#endif /* ATOMIC_TRACE_RMW_PRE */
accel/tcg/softmmu_template.h +39 -0
@@ -103,6 +103,11 @@ static inline DATA_TYPE glue(io_read, SUFFIX)(CPUArchState *env, MMUAccessType access_type) {CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index]; + +/* XXX Any sensible choice other than NULL? */ +if (tcg_ctx->plugin_mem_cb) { + env->hostaddr =NULL ; + }return io_readx (env, iotlbentry, mmu_idx, addr, retaddr, recheck, access_type,DATA_SIZE ); }
@@ -162,12 +167,23 @@ WORD_TYPE helper_le_ld_name(CPUArchState *env, target_ulong addr, res2 =helper_le_ld_name (env, addr2, oi, retaddr); shift = (addr & (DATA_SIZE -1 )) *8 ; +/* +* XXX cross-page accesses would have to be split into separate accesses +* for the host address to make sense. For now, just return NULL. +*/ +if (tcg_ctx->plugin_mem_cb) { + env->hostaddr =NULL ; + } +/* Little-endian combine. */ res = (res1 >> shift) | (res2 << ((DATA_SIZE *8 ) - shift));return res; } haddr = addr + entry->addend; +if (tcg_ctx->plugin_mem_cb) { + env->hostaddr = (void *)haddr; + }#if DATA_SIZE ==1 res =glue (glue (ld,LSUFFIX ), _p)((uint8_t *)haddr);#else
@@ -231,12 +247,19 @@ WORD_TYPE helper_be_ld_name(CPUArchState *env, target_ulong addr, res2 =helper_be_ld_name (env, addr2, oi, retaddr); shift = (addr & (DATA_SIZE -1 )) *8 ; +if (tcg_ctx->plugin_mem_cb) { + env->hostaddr =NULL ; + } +/* Big-endian combine. */ res = (res1 << shift) | (res2 >> ((DATA_SIZE *8 ) - shift));return res; } haddr = addr + entry->addend; +if (tcg_ctx->plugin_mem_cb) { + env->hostaddr = (void *)haddr; + } res =glue (glue (ld,LSUFFIX ), _be_p)((uint8_t *)haddr);return res; }
@@ -270,6 +293,10 @@ static inline void glue(io_write, SUFFIX)(CPUArchState *env, bool recheck) {CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index]; + +if (tcg_ctx->plugin_mem_cb) { + env->hostaddr =NULL ; + }return io_writex (env, iotlbentry, mmu_idx, val, addr, retaddr, recheck,DATA_SIZE ); }
@@ -340,10 +367,16 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,glue (helper_ret_stb,MMUSUFFIX )(env, addr + i, val8, oi, retaddr); } +if (tcg_ctx->plugin_mem_cb) { + env->hostaddr =NULL ; + }return ; } haddr = addr + entry->addend; +if (tcg_ctx->plugin_mem_cb) { + env->hostaddr = (void *)haddr; + }#if DATA_SIZE ==1 glue (glue (st,SUFFIX ), _p)((uint8_t *)haddr, val);#else
@@ -418,10 +451,16 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,glue (helper_ret_stb,MMUSUFFIX )(env, addr + i, val8, oi, retaddr); } +if (tcg_ctx->plugin_mem_cb) { + env->hostaddr =NULL ; + }return ; } haddr = addr + entry->addend; +if (tcg_ctx->plugin_mem_cb) { + env->hostaddr = (void *)haddr; + }glue (glue (st,SUFFIX ), _be_p)((uint8_t *)haddr, val); }#endif /* DATA_SIZE > 1 */
include/exec/cpu-defs.h +2 -0
@@ -186,6 +186,8 @@ typedef struct CPUTLBDesc {CPUTLBEntry tlb_v_table[NB_MMU_MODES ][CPU_VTLB_SIZE ]; \CPU_IOTLB \ CPUIOTLBEntry iotlb_v[NB_MMU_MODES][CPU_VTLB_SIZE]; \ +/* stores the host address of a guest access, if needed for plugins */ \ +void *hostaddr; \size_t tlb_flush_count; \target_ulong tlb_flush_addr; \target_ulong tlb_flush_mask; \
include/exec/cpu_ldst_template.h +28 -15
97 lines not on this page · raw message
include/exec/cpu_ldst_useronly_template.h +30 -12
71 lines not on this page · raw message
tcg/tcg-op.h +5 -0
@@ -1208,6 +1208,11 @@ static inline void tcg_gen_ld_ptr(TCGv_ptr r, TCGv_ptr a, intptr_t o)glue (tcg_gen_ld_,PTR )((NAT )r, a, o); } +static inline void tcg_gen_st_ptr (TCGv_ptr r,TCGv_ptr a,intptr_t o) +{ +glue (tcg_gen_st_,PTR )((NAT )r, a, o); +} +static inline void tcg_gen_discard_ptr (TCGv_ptr a) {glue (tcg_gen_discard_,PTR )((NAT )a);
tcg/tcg.h +4 -0
@@ -29,6 +29,7 @@#include "cpu.h" #include "exec/tb-context.h" #include "qemu/bitops.h" +#include "qemu/plugin.h" #include "qemu/queue.h" #include "tcg-mo.h" #include "tcg-target.h"
@@ -719,6 +720,9 @@ struct TCGContext {TCGLabel *exitreq_label; +struct qemu_plugin_dyn_cb_arr *plugin_mem_cb; +struct qemu_plugin_insn *plugin_insn; +TCGTempSet free_temps[TCG_TYPE_COUNT *2 ];TCGTemp temps[TCG_MAX_TEMPS ];/* globals first, temps after */
tcg/i386/tcg-target.inc.c +5 -0
@@ -1685,6 +1685,11 @@ static inline void tcg_out_tlb_load(TCGContext *s, TCGReg addrlo, TCGReg addrhi,/* add addend(r0), r1 */ tcg_out_modrm_offset (s, OPC_ADD_GvEv + hrexw, r1, r0, offsetof(CPUTLBEntry , addend)); + +if (s->plugin_mem_cb) { +tcg_out_st (s,TCG_TYPE_PTR , r1,TCG_AREG0 , + offsetof(CPUArchState , hostaddr)); + } }/*
tcg/tcg-op.c +29 -8
@@ -31,6 +31,7 @@#include "tcg-mo.h" #include "trace-tcg.h" #include "trace/mem.h" +#include "exec/plugin-gen.h" /* Reduce the number of ifdefs below. This assumes that all uses of TCGV_HIGH and TCGV_LOW are properly protected by a conditional that
@@ -2699,26 +2700,42 @@ static void tcg_gen_req_mo(TCGBar type) } } +static inline void plugin_gen_mem_callbacks (TCGv vaddr,uint8_t info) +{ +struct qemu_plugin_dyn_cb_arr *arr = tcg_ctx->plugin_mem_cb; + +if (arr ==NULL ) { +return ; + } +qemu_plugin_gen_vcpu_mem_callbacks (arr, vaddr, info); +} +void tcg_gen_qemu_ld_i32 (TCGv_i32 val,TCGv addr,TCGArg idx,TCGMemOp memop) { +uint8_t info =trace_mem_get_info (memop,0 ); +tcg_gen_req_mo (TCG_MO_LD_LD |TCG_MO_ST_LD ); memop =tcg_canonicalize_memop (memop,0 ,0 ); -trace_guest_mem_before_tcg (tcg_ctx->cpu, cpu_env, - addr,trace_mem_get_info (memop,0 )); +trace_guest_mem_before_tcg (tcg_ctx->cpu, cpu_env, addr, info);gen_ldst_i32 (INDEX_op_qemu_ld_i32, val, addr, memop, idx); +plugin_gen_mem_callbacks (addr, info); }void tcg_gen_qemu_st_i32 (TCGv_i32 val,TCGv addr,TCGArg idx,TCGMemOp memop) { +uint8_t info =trace_mem_get_info (memop,1 ); +tcg_gen_req_mo (TCG_MO_LD_ST |TCG_MO_ST_ST ); memop =tcg_canonicalize_memop (memop,0 ,1 ); -trace_guest_mem_before_tcg (tcg_ctx->cpu, cpu_env, - addr,trace_mem_get_info (memop,1 )); +trace_guest_mem_before_tcg (tcg_ctx->cpu, cpu_env, addr, info);gen_ldst_i32 (INDEX_op_qemu_st_i32, val, addr, memop, idx); +plugin_gen_mem_callbacks (addr, info); }void tcg_gen_qemu_ld_i64 (TCGv_i64 val,TCGv addr,TCGArg idx,TCGMemOp memop) { +uint8_t info; +tcg_gen_req_mo (TCG_MO_LD_LD |TCG_MO_ST_LD );if (TCG_TARGET_REG_BITS ==32 && (memop &MO_SIZE ) <MO_64 ) {tcg_gen_qemu_ld_i32 (TCGV_LOW (val), addr, idx, memop);
@@ -2731,13 +2748,16 @@ void tcg_gen_qemu_ld_i64(TCGv_i64 val, TCGv addr, TCGArg idx, TCGMemOp memop) } memop =tcg_canonicalize_memop (memop,1 ,0 ); -trace_guest_mem_before_tcg (tcg_ctx->cpu, cpu_env, - addr,trace_mem_get_info (memop,0 )); + info =trace_mem_get_info (memop,0 ); +trace_guest_mem_before_tcg (tcg_ctx->cpu, cpu_env, addr, info);gen_ldst_i64 (INDEX_op_qemu_ld_i64, val, addr, memop, idx); +plugin_gen_mem_callbacks (addr, info); }void tcg_gen_qemu_st_i64 (TCGv_i64 val,TCGv addr,TCGArg idx,TCGMemOp memop) { +uint8_t info; +tcg_gen_req_mo (TCG_MO_LD_ST |TCG_MO_ST_ST );if (TCG_TARGET_REG_BITS ==32 && (memop &MO_SIZE ) <MO_64 ) {tcg_gen_qemu_st_i32 (TCGV_LOW (val), addr, idx, memop);
@@ -2745,9 +2765,10 @@ void tcg_gen_qemu_st_i64(TCGv_i64 val, TCGv addr, TCGArg idx, TCGMemOp memop) } memop =tcg_canonicalize_memop (memop,1 ,1 ); -trace_guest_mem_before_tcg (tcg_ctx->cpu, cpu_env, - addr,trace_mem_get_info (memop,1 )); + info =trace_mem_get_info (memop,1 ); +trace_guest_mem_before_tcg (tcg_ctx->cpu, cpu_env, addr, info);gen_ldst_i64 (INDEX_op_qemu_st_i64, val, addr, memop, idx); +plugin_gen_mem_callbacks (addr, info); }static void tcg_gen_ext_i32 (TCGv_i32 ret,TCGv_i32 val,TCGMemOp opc)
tcg/tcg.c +3 -0
@@ -1839,6 +1839,9 @@ void tcg_gen_callN(void *func, TCGTemp *ret, int nargs, TCGTemp **args)* that @func can only be in one TCGHelperInfo. */ info =qht_lookup_custom (&helper_table, func, hash, tcg_helper_lookup_cmp); +if (unlikely (tcg_ctx->plugin_insn)) { + tcg_ctx->plugin_insn->calls_helpers = true; + }do_tcg_gen_callN (info, ret, nargs, args); }
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 18/48] tcg: add memory callbacks for plugins (WIP)
Quoted 74 lines · Emilio G. Cota
XXX: store hostaddr from non-i386 TCG backends
XXX: what hostaddr to return for I/O accesses?
XXX: what hostaddr to return for cross-page accesses?Here the trickiest feature is passing the host address to memory callbacks that request it. Perhaps it would be more appropriate to pass a "physical" address to plugins, but since in QEMU host addr ~= guest physical, I'm going with that for simplicity.
To keep the implementation simple we piggy-back on the TLB fast path, and thus can only provide the host address _after_ memory accesses have occurred. For the slow path, it's a bit tedious because there are many places to update, but it's fairly simple.
However, note that cross-page accesses are tricky, since the access might be to non-contiguous host addresses. So I'm punting on that and just passing NULL.
Signed-off-by Emilio G. Cota <[email protected]>--- accel/tcg/atomic_template.h | 8 ++++- accel/tcg/softmmu_template.h | 39 ++++++++++++++++++++ include/exec/cpu-defs.h | 2 ++ include/exec/cpu_ldst_template.h | 43 +++++++++++++++-------- include/exec/cpu_ldst_useronly_template.h | 42 +++++++++++++++------- tcg/tcg-op.h | 5 +++ tcg/tcg.h | 4 +++ tcg/i386/tcg-target.inc.c | 5 +++ tcg/tcg-op.c | 37 ++++++++++++++----- tcg/tcg.c | 3 ++ 10 files changed, 152 insertions(+), 36 deletions(-)accel/tcg/atomic_template.h +7
-1@@ -18,6 +18,7 @@* License along with this library; if not, see <http://www.gnu.org/licenses/>. */ +#include "qemu/plugin.h" #include "trace/mem.h" #if DATA_SIZE ==16 @@ -66,17 +67,22 @@trace_guest_mem_before_exec (ENV_GET_CPU (env), addr, info |TRACE_MEM_ST ); \ } while (0) -# define ATOMIC_TRACE_RMW_POST \ +# define ATOMIC_TRACE_RMW_POST do { \ + qemu_plugin_vcpu_mem_cb(ENV_GET_CPU(env), addr, haddr, info); \ + qemu_plugin_vcpu_mem_cb(ENV_GET_CPU(env), addr, haddr, info | TRACE_MEM_ST); \ +} while (0)# define ATOMIC_TRACE_LD_PRE \ trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info)# define ATOMIC_TRACE_LD_POST \ + qemu_plugin_vcpu_mem_cb(ENV_GET_CPU(env), addr, haddr, info)# define ATOMIC_TRACE_ST_PRE \ trace_guest_mem_before_exec(ENV_GET_CPU(env), addr, info)# define ATOMIC_TRACE_ST_POST \ + qemu_plugin_vcpu_mem_cb(ENV_GET_CPU(env), addr, haddr, info)#endif /* ATOMIC_TRACE_RMW_PRE */ accel/tcg/softmmu_template.h +0
-0
accel/tcg/softmmu_template.h static inline DATA_TYPE glue(io_read, SUFFIX)(CPUArchState *env, · line 110
Quoted 8 lines
accel/tcg/softmmu_template.h +5
-0@@ -103,6 +103,11 @@ static inline DATA_TYPE glue(io_read, SUFFIX)(CPUArchState *env, MMUAccessType access_type) {CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index]; + +/* XXX Any sensible choice other than NULL? */ +if (tcg_ctx->plugin_mem_cb) { + env->hostaddr =NULL ; + }
This is more argument for getting the softmmu de-macrofiction in first.
--
Alex Bennée
RFC 19/48 translate-all: notify plugin code of tb_flush
In reply to Emilio G. Cota--- accel/tcg/translate-all.c | 6 ++++++ 1 file changed, 6 insertions(+)
accel/tcg/translate-all.c +6 -0
@@ -1233,6 +1233,8 @@ static gboolean tb_host_size_iter(gpointer key, gpointer value, gpointer data)/* flush all the translation blocks */ void do_tb_flush (CPUState *cpu,run_on_cpu_data tb_flush_count) { +bool did_flush = false; +mmap_lock ();/* If it is already been done on request of another CPU, * just retry.
@@ -1240,6 +1242,7 @@ void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count)if (tb_ctx.tb_flush_count != tb_flush_count.host_int) { goto done; } + did_flush = true;if (DEBUG_TB_FLUSH_GATE ) {size_t nb_tbs =tcg_nb_tbs ();
@@ -1265,6 +1268,9 @@ void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count) done:mmap_unlock (); +if (did_flush) { +qemu_plugin_flush_cb (); + } }void tb_flush (CPUState *cpu)
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 19/48] translate-all: notify plugin code of tb_flush
Quoted 27 lines · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>--- accel/tcg/translate-all.c | 6 ++++++ 1 file changed, 6 insertions(+)accel/tcg/translate-all.c +3
-0@@ -1233,6 +1233,8 @@ static gboolean tb_host_size_iter(gpointer key, gpointer value, gpointer data)/* flush all the translation blocks */ void do_tb_flush (CPUState *cpu,run_on_cpu_data tb_flush_count) { +bool did_flush = false; +mmap_lock ();/* If it is already been done on request of another CPU, * just retry. @@ -1240,6 +1242,7 @@ void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count)if (tb_ctx.tb_flush_count != tb_flush_count.host_int) { goto done; } + did_flush = true;if (DEBUG_TB_FLUSH_GATE ) {size_t nb_tbs =tcg_nb_tbs ();
accel/tcg/translate-all.c void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count) · line 1273
Quoted 6 lines
accel/tcg/translate-all.c +3
-0@@ -1265,6 +1268,9 @@ void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count) done:mmap_unlock (); +if (did_flush) { +qemu_plugin_flush_cb (); + }
Are we introducing a race here?
What is the purpose of letting the plugin know a flush has occurred?
It shouldn't have any knowledge of the details of liveliness of the translated code and if it still exits or not. If all it wants to do is look at the counts then I think we can provide a simpler less abuse-able way to do this.
accel/tcg/translate-all.c void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count) · line 1276
Quoted 3 lines
accel/tcg/translate-all.c +0
-0}void tb_flush (CPUState *cpu)
--
Alex Bennée
Re: [Qemu-devel] [RFC 19/48] translate-all: notify plugin code of tb_flush
Quoted 31 lines · Alex Bennée
Quoted 28 lines · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>--- accel/tcg/translate-all.c | 6 ++++++ 1 file changed, 6 insertions(+)accel/tcg/translate-all.c +3
-0@@ -1233,6 +1233,8 @@ static gboolean tb_host_size_iter(gpointer key, gpointer value, gpointer data)/* flush all the translation blocks */ void do_tb_flush (CPUState *cpu,run_on_cpu_data tb_flush_count) { +bool did_flush = false; +mmap_lock ();/* If it is already been done on request of another CPU, * just retry. @@ -1240,6 +1242,7 @@ void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count)if (tb_ctx.tb_flush_count != tb_flush_count.host_int) { goto done; } + did_flush = true;if (DEBUG_TB_FLUSH_GATE ) {size_t nb_tbs =tcg_nb_tbs ();@@ -1265,6 +1268,9 @@ void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count)
Quoted 7 lines
accel/tcg/translate-all.c void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count) · line 1273
Quoted 5 lines
accel/tcg/translate-all.c +3
-0done:mmap_unlock (); +if (did_flush) { +qemu_plugin_flush_cb (); + }Are we introducing a race here?
A race, how? We're in an async safe environment here, i.e. no other vCPU is running.
Quoted 1 line
What is the purpose of letting the plugin know a flush has occurred?
Plugins might allocate per-TB data that then they get passed each time the TB is executed (via the *userdata pointer). For example, in a simulator we'd allocate a per-TB struct that describes the guest instructions, after having disassembled them at translate time.
It is therefore useful for plugins to know when all TB's have been flushed, so that they can then free that per-TB data.
Quoted 4 lines
It shouldn't have any knowledge of the details of liveliness of the translated code and if it still exits or not. If all it wants to do is look at the counts then I think we can provide a simpler less abuse-able way to do this.
I'm confused. What does "look at the counts" mean here?
To reiterate, plugins should have a way to know when a TB doesn't exist any longer, so that they can reclaim memory.
Thanks,
Emilio
Alex Bennée
Reviewed-byRe: [Qemu-devel] [RFC 19/48] translate-all: notify plugin code of tb_flush
Quoted 40 lines · Emilio G. Cota
Quoted 31 lines · Alex Bennée
Quoted 28 lines · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>--- accel/tcg/translate-all.c | 6 ++++++ 1 file changed, 6 insertions(+)accel/tcg/translate-all.c +3
-0@@ -1233,6 +1233,8 @@ static gboolean tb_host_size_iter(gpointer key, gpointer value, gpointer data)/* flush all the translation blocks */ void do_tb_flush (CPUState *cpu,run_on_cpu_data tb_flush_count) { +bool did_flush = false; +mmap_lock ();/* If it is already been done on request of another CPU, * just retry. @@ -1240,6 +1242,7 @@ void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count)if (tb_ctx.tb_flush_count != tb_flush_count.host_int) { goto done; } + did_flush = true;if (DEBUG_TB_FLUSH_GATE ) {size_t nb_tbs =tcg_nb_tbs ();@@ -1265,6 +1268,9 @@ void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count)Quoted 7 lines
accel/tcg/translate-all.c void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count) · line 1273
Quoted 5 lines
accel/tcg/translate-all.c +3
-0done:mmap_unlock (); +if (did_flush) { +qemu_plugin_flush_cb (); + }Are we introducing a race here?
Quoted 2 lines
A race, how? We're in an async safe environment here, i.e. no other vCPU is running.
You are right, I was thrown by the mmap_lock/unlocks - but I guess even they aren't needed now if tb flush is always in an exclusive context.
Quoted 10 lines
Quoted 2 lines
What is the purpose of letting the plugin know a flush has occurred?
Plugins might allocate per-TB data that then they get passed each time the TB is executed (via the *userdata pointer). For example, in a simulator we'd allocate a per-TB struct that describes the guest instructions, after having disassembled them at translate time.
It is therefore useful for plugins to know when all TB's have been flushed, so that they can then free that per-TB data.
Would they need a generation count propagated here or would it just be flush anything translation related at this point?
Quoted 9 lines
Quoted 4 lines
It shouldn't have any knowledge of the details of liveliness of the translated code and if it still exits or not. If all it wants to do is look at the counts then I think we can provide a simpler less abuse-able way to do this.
I'm confused. What does "look at the counts" mean here?
To reiterate, plugins should have a way to know when a TB doesn't exist any longer, so that they can reclaim memory.
OK that makes sense. Could you expand the commit message just to explain the use case?
--
Alex Bennée
Re: [Qemu-devel] [RFC 19/48] translate-all: notify plugin code of tb_flush
Quoted 9 lines · Alex Bennée
Quoted 8 lines · Emilio G. Cota
Quoted 1 line · Alex Bennée
What is the purpose of letting the plugin know a flush has occurred?
Plugins might allocate per-TB data that then they get passed each time the TB is executed (via the *userdata pointer). For example, in a simulator we'd allocate a per-TB struct that describes the guest instructions, after having disassembled them at translate time.
Quoted 5 lines
Quoted 2 lines
It is therefore useful for plugins to know when all TB's have been flushed, so that they can then free that per-TB data.
Would they need a generation count propagated here or would it just be flush anything translation related at this point?
The callback is guaranteed to be called in an exclusive context, so plugins can just rely on that; since no code execution can happen at that time, there's no need to pass further info, since the plugins can free that data right from the callback.
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
It shouldn't have any knowledge of the details of liveliness of the translated code and if it still exits or not. If all it wants to do is look at the counts then I think we can provide a simpler less abuse-able way to do this.
I'm confused. What does "look at the counts" mean here?
Quoted 7 lines
Quoted 2 lines
To reiterate, plugins should have a way to know when a TB doesn't exist any longer, so that they can reclaim memory.
OK that makes sense. Could you expand the commit message just to explain the use case?
Reviewed-by Alex Bennée <[email protected]>
Updated the commit message with:
translate-all: notify plugin code of tb_flush
Plugins might allocate per-TB data that then they get passed each time a TB is executed (via the *userdata pointer).
Notify plugin code every time a code cache flush occurs, so that plugins can then reclaim the memory of the per-TB data.
Thanks,
Emilio
RFC 20/48 *-user: notify plugin of exit
In reply to Emilio G. Cota--- bsd-user/syscall.c | 3 +++ linux-user/exit.c | 1 + 2 files changed, 4 insertions(+)
bsd-user/syscall.c +3 -0
@@ -332,6 +332,7 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,_mcleanup ();#endif gdb_exit (cpu_env, arg1); +qemu_plugin_atexit_cb ();/* XXX: should free thread stack and CPU env */ _exit (arg1); ret =0 ;/* avoid warning */
@@ -430,6 +431,7 @@ abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,_mcleanup ();#endif gdb_exit (cpu_env, arg1); +qemu_plugin_atexit_cb ();/* XXX: should free thread stack and CPU env */ _exit (arg1); ret =0 ;/* avoid warning */
@@ -505,6 +507,7 @@ abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,_mcleanup ();#endif gdb_exit (cpu_env, arg1); +qemu_plugin_atexit_cb ();/* XXX: should free thread stack and CPU env */ _exit (arg1); ret =0 ;/* avoid warning */
linux-user/exit.c +1 -0
@@ -32,4 +32,5 @@ void preexit_cleanup(CPUArchState *env, int code)__gcov_dump ();#endif gdb_exit (env, code); +qemu_plugin_atexit_cb (); }
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 20/48] *-user: notify plugin of exit
Quoted 1 line · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>
Quoted 38 lines
--- bsd-user/syscall.c | 3 +++ linux-user/exit.c | 1 + 2 files changed, 4 insertions(+)bsd-user/syscall.c +3
-0@@ -332,6 +332,7 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,_mcleanup ();#endif gdb_exit (cpu_env, arg1); +qemu_plugin_atexit_cb ();/* XXX: should free thread stack and CPU env */ _exit (arg1); ret =0 ;/* avoid warning */ @@ -430,6 +431,7 @@ abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,_mcleanup ();#endif gdb_exit (cpu_env, arg1); +qemu_plugin_atexit_cb ();/* XXX: should free thread stack and CPU env */ _exit (arg1); ret =0 ;/* avoid warning */ @@ -505,6 +507,7 @@ abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,_mcleanup ();#endif gdb_exit (cpu_env, arg1); +qemu_plugin_atexit_cb ();/* XXX: should free thread stack and CPU env */ _exit (arg1); ret =0 ;/* avoid warning */ linux-user/exit.c +0
-0
linux-user/exit.c void preexit_cleanup(CPUArchState *env, int code) · line 36
Quoted 5 lines
linux-user/exit.c +1
-0@@ -32,4 +32,5 @@ void preexit_cleanup(CPUArchState *env, int code)__gcov_dump ();#endif gdb_exit (env, code); +qemu_plugin_atexit_cb (); }
--
Alex Bennée
RFC 21/48 *-user: plugin syscalls
In reply to Emilio G. Cota--- bsd-user/syscall.c | 9 +++++++++ linux-user/syscall.c | 3 +++ 2 files changed, 12 insertions(+)
bsd-user/syscall.c +9 -0
@@ -323,6 +323,8 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,gemu_log ("freebsd syscall %d\n" , num);#endif trace_guest_user_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); +qemu_plugin_vcpu_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, + arg8);if (do_strace)print_freebsd_syscall (num, arg1, arg2, arg3, arg4, arg5, arg6);
@@ -404,6 +406,7 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,if (do_strace)print_freebsd_syscall_ret (num, ret);trace_guest_user_syscall_ret (cpu, num, ret); +qemu_plugin_vcpu_syscall_ret (cpu, num, ret);return ret; efault: ret = -TARGET_EFAULT ;
@@ -422,6 +425,8 @@ abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,gemu_log ("netbsd syscall %d\n" , num);#endif trace_guest_user_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6,0 ,0 ); +qemu_plugin_vcpu_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6,0 , +0 );if (do_strace)print_netbsd_syscall (num, arg1, arg2, arg3, arg4, arg5, arg6);
@@ -480,6 +485,7 @@ abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,if (do_strace)print_netbsd_syscall_ret (num, ret);trace_guest_user_syscall_ret (cpu, num, ret); +qemu_plugin_vcpu_syscall_ret (cpu, num, ret);return ret; efault: ret = -TARGET_EFAULT ;
@@ -498,6 +504,8 @@ abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,gemu_log ("openbsd syscall %d\n" , num);#endif trace_guest_user_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6,0 ,0 ); +qemu_plugin_vcpu_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6,0 , +0 );if (do_strace)print_openbsd_syscall (num, arg1, arg2, arg3, arg4, arg5, arg6);
@@ -556,6 +564,7 @@ abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,if (do_strace)print_openbsd_syscall_ret (num, ret);trace_guest_user_syscall_ret (cpu, num, ret); +qemu_plugin_vcpu_syscall_ret (cpu, num, ret);return ret; efault: ret = -TARGET_EFAULT ;
linux-user/syscall.c +3 -0
@@ -11232,6 +11232,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,trace_guest_user_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); +qemu_plugin_vcpu_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, + arg8);if (unlikely (do_strace)) {print_syscall (num, arg1, arg2, arg3, arg4, arg5, arg6);
@@ -11244,5 +11246,6 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, }trace_guest_user_syscall_ret (cpu, num, ret); +qemu_plugin_vcpu_syscall_ret (cpu, num, ret);return ret; }
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 21/48] *-user: plugin syscalls
Quoted 11 lines · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>--- bsd-user/syscall.c | 9 +++++++++ linux-user/syscall.c | 3 +++ 2 files changed, 12 insertions(+)bsd-user/syscall.c +0
-0
bsd-user/syscall.c abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1, · line 327
Quoted 5 lines
bsd-user/syscall.c +2
-0@@ -323,6 +323,8 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,gemu_log ("freebsd syscall %d\n" , num);#endif trace_guest_user_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); +qemu_plugin_vcpu_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, + arg8);
I think we discussed this on my series about avoiding this sort of duplication by providing a wrapper for trace points that are also plugin hooks. So something like:
trace_and_plugin_guest_user_syscall(...)
Although it's probably worth keeping the trace names grep-able so maybe:
plug_trace_guest_user_syscall(...)
?
Quoted 59 lines
bsd-user/syscall.c +7
-0if (do_strace)print_freebsd_syscall (num, arg1, arg2, arg3, arg4, arg5, arg6);@@ -404,6 +406,7 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,if (do_strace)print_freebsd_syscall_ret (num, ret);trace_guest_user_syscall_ret (cpu, num, ret); +qemu_plugin_vcpu_syscall_ret (cpu, num, ret);return ret; efault: ret = -TARGET_EFAULT ;@@ -422,6 +425,8 @@ abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,gemu_log ("netbsd syscall %d\n" , num);#endif trace_guest_user_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6,0 ,0 ); +qemu_plugin_vcpu_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6,0 , +0 );if (do_strace)print_netbsd_syscall (num, arg1, arg2, arg3, arg4, arg5, arg6);@@ -480,6 +485,7 @@ abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,if (do_strace)print_netbsd_syscall_ret (num, ret);trace_guest_user_syscall_ret (cpu, num, ret); +qemu_plugin_vcpu_syscall_ret (cpu, num, ret);return ret; efault: ret = -TARGET_EFAULT ;@@ -498,6 +504,8 @@ abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,gemu_log ("openbsd syscall %d\n" , num);#endif trace_guest_user_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6,0 ,0 ); +qemu_plugin_vcpu_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6,0 , +0 );if (do_strace)print_openbsd_syscall (num, arg1, arg2, arg3, arg4, arg5, arg6);@@ -556,6 +564,7 @@ abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,if (do_strace)print_openbsd_syscall_ret (num, ret);trace_guest_user_syscall_ret (cpu, num, ret); +qemu_plugin_vcpu_syscall_ret (cpu, num, ret);return ret; efault: ret = -TARGET_EFAULT ;linux-user/syscall.c +2
-0@@ -11232,6 +11232,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,trace_guest_user_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); +qemu_plugin_vcpu_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, + arg8);if (unlikely (do_strace)) {print_syscall (num, arg1, arg2, arg3, arg4, arg5, arg6);
linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 11251
Quoted 6 lines
linux-user/syscall.c +1
-0@@ -11244,5 +11246,6 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, }trace_guest_user_syscall_ret (cpu, num, ret); +qemu_plugin_vcpu_syscall_ret (cpu, num, ret);return ret; }
--
Alex Bennée
Re: [Qemu-devel] [RFC 21/48] *-user: plugin syscalls
Quoted 24 lines · Alex Bennée
Quoted 11 lines · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>--- bsd-user/syscall.c | 9 +++++++++ linux-user/syscall.c | 3 +++ 2 files changed, 12 insertions(+)bsd-user/syscall.c +0
-0bsd-user/syscall.c abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1, · line 327
Quoted 5 lines
bsd-user/syscall.c +2
-0@@ -323,6 +323,8 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,gemu_log ("freebsd syscall %d\n" , num);#endif trace_guest_user_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); +qemu_plugin_vcpu_syscall (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, + arg8);I think we discussed this on my series about avoiding this sort of duplication by providing a wrapper for trace points that are also plugin hooks. So something like:
Quoted 7 lines
trace_and_plugin_guest_user_syscall(...)Although it's probably worth keeping the trace names grep-able so maybe:
plug_trace_guest_user_syscall(...)?
Yes, I remember that discussion and agree that a common function here would be best.
Emilio
RFC 22/48 cpu: hook plugin vcpu events
In reply to Emilio G. Cota--- cpus.c | 10 ++++++++++ exec.c | 2 ++ qom/cpu.c | 2 ++ 3 files changed, 14 insertions(+)
cpus.c +10 -0
@@ -43,6 +43,7 @@#include "exec/exec-all.h" #include "qemu/thread.h" +#include "qemu/plugin.h" #include "sysemu/cpus.h" #include "sysemu/qtest.h" #include "qemu/main-loop.h"
@@ -1322,12 +1323,21 @@ static void qemu_tcg_rr_wait_io_event(CPUState *cpu)static void qemu_wait_io_event (CPUState *cpu) { +bool asleep = false; +g_assert (cpu_mutex_locked (cpu));g_assert (!qemu_mutex_iothread_locked ());while (cpu_thread_is_idle (cpu)) { +if (!asleep) { + asleep = true; +qemu_plugin_vcpu_idle_cb (cpu); + }qemu_cond_wait (&cpu->halt_cond, &cpu->lock); } +if (asleep) { +qemu_plugin_vcpu_resume_cb (cpu); + }#ifdef _WIN32/* Eat dummy APC queued by qemu_cpu_kick_thread. */
exec.c +2 -0
@@ -967,6 +967,8 @@ void cpu_exec_realizefn(CPUState *cpu, Error **errp) }tlb_init (cpu); +qemu_plugin_vcpu_init_hook (cpu); +#ifndef CONFIG_USER_ONLY if (qdev_get_vmsd (DEVICE (cpu)) ==NULL ) {vmstate_register (NULL , cpu->cpu_index, &vmstate_cpu_common, cpu);
qom/cpu.c +2 -0
@@ -32,6 +32,7 @@#include "hw/boards.h" #include "hw/qdev-properties.h" #include "trace-root.h" +#include "qemu/plugin.h" CPUInterruptHandler cpu_interrupt_handler;
@@ -353,6 +354,7 @@ static void cpu_common_unrealizefn(DeviceState *dev, Error **errp)CPUState *cpu =CPU (dev);/* NOTE: latest generic point before the cpu is fully unrealized */ trace_fini_vcpu (cpu); +qemu_plugin_vcpu_exit_hook (cpu);cpu_exec_unrealizefn (cpu); }
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 22/48] cpu: hook plugin vcpu events
Quoted 20 lines · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>--- cpus.c | 10 ++++++++++ exec.c | 2 ++ qom/cpu.c | 2 ++ 3 files changed, 14 insertions(+)cpus.c +1
-0@@ -43,6 +43,7 @@#include "exec/exec-all.h" #include "qemu/thread.h" +#include "qemu/plugin.h" #include "sysemu/cpus.h" #include "sysemu/qtest.h" #include "qemu/main-loop.h"
cpus.c static void qemu_tcg_rr_wait_io_event(CPUState *cpu) · line 1327
Quoted 5 lines
cpus.c +2
-0@@ -1322,12 +1323,21 @@ static void qemu_tcg_rr_wait_io_event(CPUState *cpu)static void qemu_wait_io_event (CPUState *cpu) { +bool asleep = false; +
slept?
cpus.c static void qemu_tcg_rr_wait_io_event(CPUState *cpu) · line 1340
Quoted 13 lines
cpus.c +7
-0g_assert (cpu_mutex_locked (cpu));g_assert (!qemu_mutex_iothread_locked ());while (cpu_thread_is_idle (cpu)) { +if (!asleep) { + asleep = true; +qemu_plugin_vcpu_idle_cb (cpu); + }qemu_cond_wait (&cpu->halt_cond, &cpu->lock); } +if (asleep) { +qemu_plugin_vcpu_resume_cb (cpu); + }
I wonder if having two hooks is too much? What might a plugin want to do before we go into idle sleep?
It feels like we are exposing too much of the guts of TCG to the plugin here as wait_io could be for any number of internal reasons other than the actual emulation blocking for IO through a WFI or something. If a plugin really wants to track such things shouldn't it be hooking to the guest sleep points?
If idle sleeps really are that important maybe we could just report our sleep time on resume - so a single hook but passing a bit more information?
Quoted 29 lines
cpus.c +0
-0#ifdef _WIN32/* Eat dummy APC queued by qemu_cpu_kick_thread. */ exec.c +2
-0@@ -967,6 +967,8 @@ void cpu_exec_realizefn(CPUState *cpu, Error **errp) }tlb_init (cpu); +qemu_plugin_vcpu_init_hook (cpu); +#ifndef CONFIG_USER_ONLY if (qdev_get_vmsd (DEVICE (cpu)) ==NULL ) {vmstate_register (NULL , cpu->cpu_index, &vmstate_cpu_common, cpu);qom/cpu.c +1
-0@@ -32,6 +32,7 @@#include "hw/boards.h" #include "hw/qdev-properties.h" #include "trace-root.h" +#include "qemu/plugin.h" CPUInterruptHandler cpu_interrupt_handler;
qom/cpu.c static void cpu_common_unrealizefn(DeviceState *dev, Error **errp) · line 359
Quoted 6 lines
qom/cpu.c +1
-0@@ -353,6 +354,7 @@ static void cpu_common_unrealizefn(DeviceState *dev, Error **errp)CPUState *cpu =CPU (dev);/* NOTE: latest generic point before the cpu is fully unrealized */ trace_fini_vcpu (cpu); +qemu_plugin_vcpu_exit_hook (cpu);cpu_exec_unrealizefn (cpu); }
--
Alex Bennée
Re: [Qemu-devel] [RFC 22/48] cpu: hook plugin vcpu events
Quoted 1 line · Alex Bennée
Emilio G. Cota <[email protected]> writes:
(snip)
Quoted 8 lines
cpus.c static void qemu_tcg_rr_wait_io_event(CPUState *cpu) · line 1327
Quoted 6 lines
cpus.c +2
-0@@ -1322,12 +1323,21 @@ static void qemu_tcg_rr_wait_io_event(CPUState *cpu)static void qemu_wait_io_event (CPUState *cpu) { +bool asleep = false; +slept?
Changed to slept, thanks.
Quoted 23 lines
cpus.c static void qemu_tcg_rr_wait_io_event(CPUState *cpu) · line 1340
Quoted 13 lines
cpus.c +7
-0g_assert (cpu_mutex_locked (cpu));g_assert (!qemu_mutex_iothread_locked ());while (cpu_thread_is_idle (cpu)) { +if (!asleep) { + asleep = true; +qemu_plugin_vcpu_idle_cb (cpu); + }qemu_cond_wait (&cpu->halt_cond, &cpu->lock); } +if (asleep) { +qemu_plugin_vcpu_resume_cb (cpu); + }I wonder if having two hooks is too much? What might a plugin want to do before we go into idle sleep?
It feels like we are exposing too much of the guts of TCG to the plugin here as
wait_iocould be for any number of internal reasons other than the actual emulation blocking for IO through a WFI or something. If a plugin really wants to track such things shouldn't it be hooking to the guest sleep points?
Quoted 3 lines
If idle sleeps really are that important maybe we could just report our sleep time on resume - so a single hook but passing a bit more information?
I don't have all the use cases for this figured out. For now I'm using this in plugins as a way to know when a vCPU is for sure idle, which is used in memory reclamation schemes such as RCU.
What are the "guest sleep points" you mention? Are they target-agnostic?
Thanks,
Emilio
Re: [Qemu-devel] [RFC 22/48] cpu: hook plugin vcpu events
Quoted 37 lines · Emilio G. Cota
Quoted 1 line · Alex Bennée
Emilio G. Cota <[email protected]> writes:
(snip)
Quoted 8 lines
cpus.c static void qemu_tcg_rr_wait_io_event(CPUState *cpu) · line 1327
Quoted 6 lines
cpus.c +2
-0@@ -1322,12 +1323,21 @@ static void qemu_tcg_rr_wait_io_event(CPUState *cpu)static void qemu_wait_io_event (CPUState *cpu) { +bool asleep = false; +slept?
Changed to slept, thanks.
Quoted 23 lines
cpus.c static void qemu_tcg_rr_wait_io_event(CPUState *cpu) · line 1340
Quoted 13 lines
cpus.c +7
-0g_assert (cpu_mutex_locked (cpu));g_assert (!qemu_mutex_iothread_locked ());while (cpu_thread_is_idle (cpu)) { +if (!asleep) { + asleep = true; +qemu_plugin_vcpu_idle_cb (cpu); + }qemu_cond_wait (&cpu->halt_cond, &cpu->lock); } +if (asleep) { +qemu_plugin_vcpu_resume_cb (cpu); + }I wonder if having two hooks is too much? What might a plugin want to do before we go into idle sleep?
It feels like we are exposing too much of the guts of TCG to the plugin here as
wait_iocould be for any number of internal reasons other than the actual emulation blocking for IO through a WFI or something. If a plugin really wants to track such things shouldn't it be hooking to the guest sleep points?
Quoted 7 lines
Quoted 3 lines
If idle sleeps really are that important maybe we could just report our sleep time on resume - so a single hook but passing a bit more information?
I don't have all the use cases for this figured out. For now I'm using this in plugins as a way to know when a vCPU is for sure idle, which is used in memory reclamation schemes such as RCU.
Couldn't we achieve the same by scheduling async or safe async work on the vCPU? Maybe we would expose this to the plugin as a "run callback when idle" function.
Quoted 2 lines
What are the "guest sleep points" you mention? Are they target-agnostic?
I mean we can arrive here for a variety of reasons - not all of which are triggered by the guest going to sleep. But that isn't your current use case.
They aren't target agnostic as not all guests try to fully model their "sleeping" instructions. Some just end up busy spinning until the event they should have been sleeping until happens.
Quoted 4 lines
Thanks,
Emilio
--
Alex Bennée
Re: [Qemu-devel] [RFC 22/48] cpu: hook plugin vcpu events
Quoted 44 lines · Alex Bennée
Quoted 41 lines · Emilio G. Cota
Quoted 1 line · Alex Bennée
Emilio G. Cota <[email protected]> writes:
(snip)
Quoted 8 lines
cpus.c static void qemu_tcg_rr_wait_io_event(CPUState *cpu) · line 1327
Quoted 6 lines
cpus.c +2
-0@@ -1322,12 +1323,21 @@ static void qemu_tcg_rr_wait_io_event(CPUState *cpu)static void qemu_wait_io_event (CPUState *cpu) { +bool asleep = false; +slept?
Changed to slept, thanks.
Quoted 26 lines
cpus.c static void qemu_tcg_rr_wait_io_event(CPUState *cpu) · line 1340
Quoted 13 lines
cpus.c +7
-0g_assert (cpu_mutex_locked (cpu));g_assert (!qemu_mutex_iothread_locked ());while (cpu_thread_is_idle (cpu)) { +if (!asleep) { + asleep = true; +qemu_plugin_vcpu_idle_cb (cpu); + }qemu_cond_wait (&cpu->halt_cond, &cpu->lock); } +if (asleep) { +qemu_plugin_vcpu_resume_cb (cpu); + }I wonder if having two hooks is too much? What might a plugin want to do before we go into idle sleep?
It feels like we are exposing too much of the guts of TCG to the plugin here as
wait_iocould be for any number of internal reasons other than the actual emulation blocking for IO through a WFI or something. If a plugin really wants to track such things shouldn't it be hooking to the guest sleep points?If idle sleeps really are that important maybe we could just report our sleep time on resume - so a single hook but passing a bit more information?
Quoted 7 lines
Quoted 3 lines
I don't have all the use cases for this figured out. For now I'm using this in plugins as a way to know when a vCPU is for sure idle, which is used in memory reclamation schemes such as RCU.
Couldn't we achieve the same by scheduling async or safe async work on the vCPU? Maybe we would expose this to the plugin as a "run callback when idle" function.
I'm not sure I understand the first question. Do you mean to schedule regular work just to make the CPU idle every now and then? Letting the CPU idle whenever it wants to is fine, so I don't see the need to force those transitions.
BTW I just thought of a different use case for this. Imagine a plugin is estimating how much power a CPU is consuming; if the CPU is idle, the power model would bring down the voltage/freq and account for that. Of course the model would also want to track the instructions being executed, which is a nice segue to your point below.
Quoted 10 lines
Quoted 2 lines
What are the "guest sleep points" you mention? Are they target-agnostic?
I mean we can arrive here for a variety of reasons - not all of which are triggered by the guest going to sleep. But that isn't your current use case.
They aren't target agnostic as not all guests try to fully model their "sleeping" instructions. Some just end up busy spinning until the event they should have been sleeping until happens.
I see, so you mean something like a "pause" instruction in the guest. Plugins that needed such level of detail (like the imaginary power model plugin I mentioned above) would have to track these instructions as well. IOW, this would be orthogonal to the "idle" callback as implemented in this series.
Emilio
RFC 23/48 translator: add plugin_insn argument to translate_insn
In reply to Emilio G. Cota--- include/exec/translator.h | 4 +++- accel/tcg/translator.c | 4 ++-- target/alpha/translate.c | 3 ++- target/arm/translate-a64.c | 3 ++- target/arm/translate.c | 6 ++++-- target/hppa/translate.c | 3 ++- target/i386/translate.c | 3 ++- target/m68k/translate.c | 3 ++- target/mips/translate.c | 3 ++- target/openrisc/translate.c | 3 ++- target/ppc/translate.c | 3 ++- target/riscv/translate.c | 3 ++- target/s390x/translate.c | 3 ++- target/sh4/translate.c | 3 ++- target/sparc/translate.c | 3 ++- target/xtensa/translate.c | 3 ++- 16 files changed, 35 insertions(+), 18 deletions(-)
include/exec/translator.h +3 -1
@@ -20,6 +20,7 @@#include "exec/exec-all.h" +#include "qemu/plugin.h" #include "tcg/tcg.h"
@@ -112,7 +113,8 @@ typedef struct TranslatorOps {void (*insn_start)(DisasContextBase *db,CPUState *cpu);bool (*breakpoint_check)(DisasContextBase *db,CPUState *cpu,const CPUBreakpoint *bp); -void (*translate_insn)(DisasContextBase *db,CPUState *cpu); +void (*translate_insn)(DisasContextBase *db,CPUState *cpu, +struct qemu_plugin_insn *plugin_insn);void (*tb_stop)(DisasContextBase *db,CPUState *cpu);void (*disas_log)(const DisasContextBase *db,CPUState *cpu); } TranslatorOps;
accel/tcg/translator.c +2 -2
@@ -101,10 +101,10 @@ void translator_loop(const TranslatorOps *ops, DisasContextBase *db, && (tb_cflags(db->tb) & CF_LAST_IO)) { /* Accept I/O on the last instruction. */ gen_io_start(); - ops->translate_insn(db, cpu); + ops->translate_insn(db, cpu, NULL); gen_io_end(); } else { - ops->translate_insn (db, cpu); + ops->translate_insn (db, cpu,NULL ); }/* Stop translation if translate_insn so indicated. */
target/alpha/translate.c +2 -1
@@ -2983,7 +2983,8 @@ static bool alpha_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,return true; } -static void alpha_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu) +static void alpha_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu, +struct qemu_plugin_insn *plugin_insn) {DisasContext *ctx =container_of (dcbase, DisasContext, base);CPUAlphaState *env = cpu->env_ptr;
target/arm/translate-a64.c +2 -1
@@ -13937,7 +13937,8 @@ static bool aarch64_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,return true; } -static void aarch64_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu) +static void aarch64_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu, +struct qemu_plugin_insn *plugin_insn) {DisasContext *dc =container_of (dcbase, DisasContext, base);CPUARMState *env = cpu->env_ptr;
target/arm/translate.c +4 -2
@@ -12787,7 +12787,8 @@ static void arm_post_translate_insn(DisasContext *dc)translator_loop_temp_check (&dc->base); } -static void arm_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu) +static void arm_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu, +struct qemu_plugin_insn *plugin_insn) {DisasContext *dc =container_of (dcbase, DisasContext, base);CPUARMState *env = cpu->env_ptr;
@@ -12854,7 +12855,8 @@ static bool thumb_insn_is_unconditional(DisasContext *s, uint32_t insn)return false; } -static void thumb_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu) +static void thumb_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu, +struct qemu_plugin_insn *plugin_insn) {DisasContext *dc =container_of (dcbase, DisasContext, base);CPUARMState *env = cpu->env_ptr;
target/hppa/translate.c +2 -1
@@ -4737,7 +4737,8 @@ static bool hppa_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cs,return true; } -static void hppa_tr_translate_insn (DisasContextBase *dcbase,CPUState *cs) +static void hppa_tr_translate_insn (DisasContextBase *dcbase,CPUState *cs, +struct qemu_plugin_insn *plugin_insn) {DisasContext *ctx =container_of (dcbase, DisasContext, base);CPUHPPAState *env = cs->env_ptr;
target/i386/translate.c +2 -1
@@ -8525,7 +8525,8 @@ static bool i386_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu, } } -static void i386_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu) +static void i386_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu, +struct qemu_plugin_insn *plugin_insn) {DisasContext *dc =container_of (dcbase, DisasContext, base);target_ulong pc_next =disas_insn (dc, cpu);
target/m68k/translate.c +2 -1
@@ -6087,7 +6087,8 @@ static bool m68k_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,return true; } -static void m68k_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu) +static void m68k_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu, +struct qemu_plugin_insn *plugin_insn) {DisasContext *dc =container_of (dcbase, DisasContext, base);CPUM68KState *env = cpu->env_ptr;
target/mips/translate.c +2 -1
@@ -25349,7 +25349,8 @@ static bool mips_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cs,return true; } -static void mips_tr_translate_insn (DisasContextBase *dcbase,CPUState *cs) +static void mips_tr_translate_insn (DisasContextBase *dcbase,CPUState *cs, +struct qemu_plugin_insn *plugin_insn) {CPUMIPSState *env = cs->env_ptr;DisasContext *ctx =container_of (dcbase, DisasContext, base);
target/openrisc/translate.c +2 -1
@@ -1301,7 +1301,8 @@ static bool openrisc_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cs,return true; } -static void openrisc_tr_translate_insn (DisasContextBase *dcbase,CPUState *cs) +static void openrisc_tr_translate_insn (DisasContextBase *dcbase,CPUState *cs, +struct qemu_plugin_insn *plugin_insn) {DisasContext *dc =container_of (dcbase, DisasContext, base);OpenRISCCPU *cpu =OPENRISC_CPU (cs);
target/ppc/translate.c +2 -1
@@ -7545,7 +7545,8 @@ static bool ppc_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cs,return true; } -static void ppc_tr_translate_insn (DisasContextBase *dcbase,CPUState *cs) +static void ppc_tr_translate_insn (DisasContextBase *dcbase,CPUState *cs, +struct qemu_plugin_insn *plugin_insn) {DisasContext *ctx =container_of (dcbase, DisasContext, base);CPUPPCState *env = cs->env_ptr;
target/riscv/translate.c +2 -1
@@ -1843,7 +1843,8 @@ static bool riscv_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu, } -static void riscv_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu) +static void riscv_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu, +struct qemu_plugin_insn *plugin_insn) {DisasContext *ctx =container_of (dcbase, DisasContext, base);CPURISCVState *env = cpu->env_ptr;
target/s390x/translate.c +2 -1
@@ -6228,7 +6228,8 @@ static bool s390x_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cs,return true; } -static void s390x_tr_translate_insn (DisasContextBase *dcbase,CPUState *cs) +static void s390x_tr_translate_insn (DisasContextBase *dcbase,CPUState *cs, +struct qemu_plugin_insn *plugin_insn) {CPUS390XState *env = cs->env_ptr;DisasContext *dc =container_of (dcbase, DisasContext, base);
target/sh4/translate.c +2 -1
@@ -2313,7 +2313,8 @@ static bool sh4_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cs,return true; } -static void sh4_tr_translate_insn (DisasContextBase *dcbase,CPUState *cs) +static void sh4_tr_translate_insn (DisasContextBase *dcbase,CPUState *cs, +struct qemu_plugin_insn *plugin_insn) {CPUSH4State *env = cs->env_ptr;DisasContext *ctx =container_of (dcbase, DisasContext, base);
target/sparc/translate.c +2 -1
@@ -5894,7 +5894,8 @@ static bool sparc_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cs,return true; } -static void sparc_tr_translate_insn (DisasContextBase *dcbase,CPUState *cs) +static void sparc_tr_translate_insn (DisasContextBase *dcbase,CPUState *cs, +struct qemu_plugin_insn *plugin_insn) {DisasContext *dc =container_of (dcbase, DisasContext, base);CPUSPARCState *env = cs->env_ptr;
target/xtensa/translate.c +2 -1
@@ -1146,7 +1146,8 @@ static bool xtensa_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,return true; } -static void xtensa_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu) +static void xtensa_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu, +struct qemu_plugin_insn *plugin_insn) {DisasContext *dc =container_of (dcbase, DisasContext, base);CPUXtensaState *env = cpu->env_ptr;
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 23/48] translator: add plugin_insn argument to translate_insn
Quoted 33 lines · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>--- include/exec/translator.h | 4 +++- accel/tcg/translator.c | 4 ++-- target/alpha/translate.c | 3 ++- target/arm/translate-a64.c | 3 ++- target/arm/translate.c | 6 ++++-- target/hppa/translate.c | 3 ++- target/i386/translate.c | 3 ++- target/m68k/translate.c | 3 ++- target/mips/translate.c | 3 ++- target/openrisc/translate.c | 3 ++- target/ppc/translate.c | 3 ++- target/riscv/translate.c | 3 ++- target/s390x/translate.c | 3 ++- target/sh4/translate.c | 3 ++- target/sparc/translate.c | 3 ++- target/xtensa/translate.c | 3 ++- 16 files changed, 35 insertions(+), 18 deletions(-)include/exec/translator.h +1
-0@@ -20,6 +20,7 @@#include "exec/exec-all.h" +#include "qemu/plugin.h" #include "tcg/tcg.h"
include/exec/translator.h typedef struct TranslatorOps { · line 117
Quoted 6 lines
include/exec/translator.h +2
-1@@ -112,7 +113,8 @@ typedef struct TranslatorOps {void (*insn_start)(DisasContextBase *db,CPUState *cpu);bool (*breakpoint_check)(DisasContextBase *db,CPUState *cpu,const CPUBreakpoint *bp); -void (*translate_insn)(DisasContextBase *db,CPUState *cpu); +void (*translate_insn)(DisasContextBase *db,CPUState *cpu, +struct qemu_plugin_insn *plugin_insn);
I'm not convinced this is the best way to go about it. We end up having to sprinkle the plugin calls into each decoder rather than keeping all the infrastructure in the common main loop. However the common loop will need to know the total number of bytes decoded so we could change the declaration to:
int (*translate_insn)(DisasContextBase *db, CPUState *cpu);
and return the number of bytes decoded. It would mean a minor inefficiency in having to re-read the instruction bytes into a buffer in preparation for passing to the plugin but it would all at least be in one place.
--
Alex Bennée
Re: [Qemu-devel] [RFC 23/48] translator: add plugin_insn argument to translate_insn
Quoted 9 lines · Alex Bennée
I'm not convinced this is the best way to go about it. We end up having to sprinkle the plugin calls into each decoder rather than keeping all the infrastructure in the common main loop. However the common loop will need to know the total number of bytes decoded so we could change the declaration to:
int (*translate_insn)(DisasContextBase *db, CPUState *cpu);and return the number of bytes decoded.
Returning the number of bytes is more difficult than simply just
old_pc = db->pc_next;
opc->translate_insn(db, cpu);
bytes = db->pc_next - old_pc;requiring no target changes at all.
r~
Re: [Qemu-devel] [RFC 23/48] translator: add plugin_insn argument to translate_insn
Quoted 11 lines · On Mon, 26 Nov 2018, 18:27 Richard Henderson <[email protected]…
Quoted 9 lines · Alex Bennée
I'm not convinced this is the best way to go about it. We end up having to sprinkle the plugin calls into each decoder rather than keeping all the infrastructure in the common main loop. However the common loop will need to know the total number of bytes decoded so we could change the declaration to:
int (*translate_insn)(DisasContextBase *db, CPUState *cpu);and return the number of bytes decoded.
Quoted 8 lines
Returning the number of bytes is more difficult than simply just
old_pc = db->pc_next; opc->translate_insn(db, cpu); bytes = db->pc_next - old_pc;requiring no target changes at all.
If that's always true then great, but what happens with direct branches?
Quoted 3 lines
r~
Re: [Qemu-devel] [RFC 23/48] translator: add plugin_insn argument to translate_insn
Quoted 18 lines · Alex Bennée
On Mon, 26 Nov 2018, 18:27 Richard Henderson <[email protected] <mailto:[email protected]> wrote:
On 11/26/18 6:52 AM, Alex Bennée wrote:
> I'm not convinced this is the best way to go about it. We end up having > to sprinkle the plugin calls into each decoder rather than keeping all > the infrastructure in the common main loop. However the common loop will > need to know the total number of bytes decoded so we could change the > declaration to:
>
> int (*translate_insn)(DisasContextBase *db, CPUState *cpu); >
> and return the number of bytes decoded.Returning the number of bytes is more difficult than simply just
Quoted 8 lines
old_pc=db->pc_next;
opc->translate_insn(db, cpu);
bytes =db->pc_next-old_pc;requiring no target changes at all.
If that's always true then great, but what happens with direct branches?
pc_next is still updated by the size of the branch, not it's destination; db->is_jmp will be != DISAS_NEXT, ending the TB.
r~
Re: [Qemu-devel] [RFC 23/48] translator: add plugin_insn argument to translate_insn
In reply to Richard HendersonQuoted 11 lines · Richard Henderson
Quoted 9 lines · Alex Bennée
I'm not convinced this is the best way to go about it. We end up having to sprinkle the plugin calls into each decoder rather than keeping all the infrastructure in the common main loop. However the common loop will need to know the total number of bytes decoded so we could change the declaration to:
int (*translate_insn)(DisasContextBase *db, CPUState *cpu);and return the number of bytes decoded.
Quoted 7 lines
Returning the number of bytes is more difficult than simply just
old_pc = db->pc_next; opc->translate_insn(db, cpu); bytes = db->pc_next - old_pc;requiring no target changes at all.
The main reason why I added the qemu_plugin_insn_append calls was to avoid reading the instructions twice from guest memory, because I was worried that doing so might somehow alter the guest's execution, e.g. what if we read a cross-page instruction, and both pages mapped to the same TLB entry? We'd end up having more TLB misses because instrumentation was enabled.
If you think that's not really a concern, we could just re-do the reads in the translator loop and get the size as above.
Thanks,
Emilio
Re: [Qemu-devel] [RFC 23/48] translator: add plugin_insn argument to translate_insn
Quoted 6 lines · Emilio G. Cota
The main reason why I added the
qemu_plugin_insn_appendcalls was to avoid reading the instructions twice from guest memory, because I was worried that doing so might somehow alter the guest's execution, e.g. what if we read a cross-page instruction, and both pages mapped to the same TLB entry? We'd end up having more TLB misses because instrumentation was enabled.
A better solution for this, I think is to change direct calls from
cpu_ldl_code(env, pc);
to
translator_ldl_code(dc_base, env, pc);
instead of passing around a new argument separate from DisasContextBase?
r~
Re: [Qemu-devel] [RFC 23/48] translator: add plugin_insn argument to translate_insn
Quoted 8 lines · Richard Henderson
Quoted 6 lines · Emilio G. Cota
The main reason why I added the
qemu_plugin_insn_appendcalls was to avoid reading the instructions twice from guest memory, because I was worried that doing so might somehow alter the guest's execution, e.g. what if we read a cross-page instruction, and both pages mapped to the same TLB entry? We'd end up having more TLB misses because instrumentation was enabled.
Quoted 7 lines
A better solution for this, I think is to change direct calls from
cpu_ldl_code(env, pc);
to
translator_ldl_code(dc_base, env, pc);instead of passing around a new argument separate from DisasContextBase?
I think this + diff'ing pc_next should work to figure out the contents and size of each instruction.
I'll do it this way in v2.
Thanks,
Emilio
Re: [Qemu-devel] [RFC 23/48] translator: add plugin_insn argument to translate_insn
Quoted 11 lines · Emilio G. Cota
Quoted 10 lines · Richard Henderson
Quoted 6 lines · Emilio G. Cota
The main reason why I added the
qemu_plugin_insn_appendcalls was to avoid reading the instructions twice from guest memory, because I was worried that doing so might somehow alter the guest's execution, e.g. what if we read a cross-page instruction, and both pages mapped to the same TLB entry? We'd end up having more TLB misses because instrumentation was enabled.A better solution for this, I think is to change direct calls from
Quoted 8 lines
Quoted 5 lines
cpu_ldl_code(env, pc);
to
translator_ldl_code(dc_base, env, pc);instead of passing around a new argument separate from DisasContextBase?
I think this + diff'ing
pc_nextshould work to figure out the contents and size of each instruction.
I just tried doing things this way.
For some targets like i386, the translator_ld* helpers work great; the instruction contents are copied, and through the helpers we get the sizes of the instructions as well.
For ARM though (and maybe others, I haven't gone through all of them yet), arm_ldl_code does the following:
/* Load an instruction and return it in the standard little-endian order */ static inline uint32_t arm_ldl_code(CPUARMState *env, target_ulong addr,
bool sctlr_b)
{
uint32_t insn = cpu_ldl_code(env, addr);
if (bswap_code(sctlr_b)) {
return bswap32(insn);
}
return insn;
}To avoid altering the signature of .translate_insn, I've modified arm_ldl_code directly, as follows:
uint32_t insn = cpu_ldl_code(env, addr);
+
if (bswap_code(sctlr_b)) {- return bswap32(insn);
+ insn = bswap32(insn);
+ }
+ if (tcg_ctx->plugin_insn) {
+ qemu_plugin_insn_append(tcg_ctx->plugin_insn, &insn, sizeof(insn));
}
return insn;}
(NB. tcg_ctx->plugin_insn is updated by translator_loop on every iteration.)
Let me know if you think I should do this differently.
Thanks,
Emilio
Re: [Qemu-devel] [RFC 23/48] translator: add plugin_insn argument to translate_insn
Quoted 4 lines · Emilio G. Cota
To avoid altering the signature of .translate_insn, I've modified
arm_ldl_codedirectly, as follows:uint32_t insn = cpu_ldl_code(env, addr);
Review
Quoted 10 lines
(quoted diff) +5
-1+ if (bswap_code(sctlr_b)) { - return bswap32(insn); + insn = bswap32(insn); + } + if (tcg_ctx->plugin_insn) { + qemu_plugin_insn_append(tcg_ctx->plugin_insn, &insn, sizeof(insn)); } return insn; }
Turns out it got even more complicated with thumb, since instructions can be 16 or 32 bits.
I ended up with the appended (qemu_plugin_insn_append() returns when the first argument is NULL).
Emilio
target/arm/translate-a64.c +2 -0
@@ -38,6 +38,7 @@#include "trace-tcg.h" #include "translate-a64.h" #include "qemu/atomic128.h" +#include "qemu/plugin.h" static TCGv_i64 cpu_X[32 ];static TCGv_i64 cpu_pc;
@@ -13321,6 +13322,7 @@ static void disas_a64_insn(CPUARMState *env, DisasContext *s)uint32_t insn; insn =arm_ldl_code (env, s->pc, s->sctlr_b); +qemu_plugin_insn_append (tcg_ctx->plugin_insn, &insn,sizeof (insn)); s->insn = insn; s->pc +=4 ;
target/arm/translate.c +8 -1
@@ -36,6 +36,7 @@#include "trace-tcg.h" #include "exec/log.h" +#include "qemu/plugin.h" #define ENABLE_ARCH_4T arm_dc_feature(s, ARM_FEATURE_V4T)
@@ -13234,6 +13235,7 @@ static void arm_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu) } insn =arm_ldl_code (env, dc->pc, dc->sctlr_b); +qemu_plugin_insn_append (tcg_ctx->plugin_insn, &insn,sizeof (insn)); dc->insn = insn; dc->pc +=4 ;disas_arm_insn (dc, insn);
@@ -13304,11 +13306,16 @@ static void thumb_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu) insn =arm_lduw_code (env, dc->pc, dc->sctlr_b); is_16bit =thumb_insn_is_16bit (dc, insn); dc->pc +=2 ; -if (!is_16bit) { +if (is_16bit) { +uint16_t insn16 = insn; + +qemu_plugin_insn_append (tcg_ctx->plugin_insn, &insn16,sizeof (insn16)); + }else {uint32_t insn2 =arm_lduw_code (env, dc->pc, dc->sctlr_b); insn = insn <<16 | insn2; dc->pc +=2 ; +qemu_plugin_insn_append (tcg_ctx->plugin_insn, &insn,sizeof (insn)); } dc->insn = insn;
Re: [Qemu-devel] [RFC 23/48] translator: add plugin_insn argument to translate_insn
In reply to Emilio G. CotaQuoted 56 lines · Emilio G. Cota
Quoted 11 lines · Emilio G. Cota
Quoted 10 lines · Richard Henderson
Quoted 6 lines · Emilio G. Cota
The main reason why I added the
qemu_plugin_insn_appendcalls was to avoid reading the instructions twice from guest memory, because I was worried that doing so might somehow alter the guest's execution, e.g. what if we read a cross-page instruction, and both pages mapped to the same TLB entry? We'd end up having more TLB misses because instrumentation was enabled.A better solution for this, I think is to change direct calls from
Quoted 8 lines
Quoted 5 lines
cpu_ldl_code(env, pc);
to
translator_ldl_code(dc_base, env, pc);instead of passing around a new argument separate from DisasContextBase?
I think this + diff'ing
pc_nextshould work to figure out the contents and size of each instruction.I just tried doing things this way.
For some targets like i386, the translator_ld* helpers work great; the instruction contents are copied, and through the helpers we get the sizes of the instructions as well.
For ARM though (and maybe others, I haven't gone through all of them yet),
arm_ldl_codedoes the following:/* Load an instruction and return it in the standard little-endian order */ static inline
uint32_tarm_ldl_code(CPUARMState *env,target_ulongaddr,
boolsctlr_b){ uint32_t insn = cpu_ldl_code(env, addr); if (bswap_code(sctlr_b)) { return bswap32(insn); } return insn; }To avoid altering the signature of .translate_insn, I've modified
arm_ldl_codedirectly, as follows:uint32_t insn = cpu_ldl_code(env, addr);(quoted diff) +5
-1+ if (bswap_code(sctlr_b)) { - return bswap32(insn); + insn = bswap32(insn); + } + if (tcg_ctx->plugin_insn) { + qemu_plugin_insn_append(tcg_ctx->plugin_insn, &insn, sizeof(insn)); } return insn; }
Quoted 4 lines
(NB.
tcg_ctx->plugin_insnis updated bytranslator_loopon every iteration.)Let me know if you think I should do this differently.
I was envisioning something more like the following so all the plugin gubins could be kept in the core code:
accel/tcg/translator.c | 25 +++++++++++++++++++++++++ include/exec/translator.h | 10 ++++++++++ target/arm/arm_ldst.h | 16 +++-------------
modified accel/tcg/translator.c
@@ -10,6 +10,7 @@
#include "qemu/osdep.h"
#include "qemu-common.h"
#include "qemu/error-report.h"
+#include "qemu/bswap.h"
#include "cpu.h"
#include "tcg/tcg.h"
#include "tcg/tcg-op.h"
@@ -18,6 +19,30 @@
#include "exec/log.h"
#include "exec/translator.h"
#include "exec/plugin-gen.h"
+#include "exec/cpu_ldst.h"
+
+/*
+ * Translator Code Load Helpers
+ */
+
+uint16_t translator_ld16(CPUArchState *env, target_ulong ptr, bool bswap) +{
+ uint16_t insn = cpu_lduw_code(env, ptr);
+ if (bswap) {
+ insn = bswap16(insn);
+ }
+ return insn;
+}
+
+uint32_t translator_ld32(CPUArchState *env, target_ulong ptr, bool bswap) +{
+ uint32_t insn = cpu_ldl_code(env, ptr);
+ if (bswap) {
+ insn = bswap32(insn);
+ }
+ return insn;
+}
+
/* Pairs with tcg_clear_temp_count.
To be called by #TranslatorOps.{translate_insn,tb_stop} if
modified include/exec/translator.h
@@ -147,4 +147,14 @@ void translator_loop(const TranslatorOps *ops, DisasContextBase *db,
void translator_loop_temp_check(DisasContextBase *db);
+/*
+ * Translator Code Load Helpers
+ *
+ * These allow the core translator code to track where code is being
+ * loaded from.
+ */
+
+uint16_t translator_ld16(CPUArchState *env, target_ulong ptr, bool bswap); +uint32_t translator_ld32(CPUArchState *env, target_ulong ptr, bool bswap); +
#endif /* EXEC__TRANSLATOR_H */
modified target/arm/arm_ldst.h
@@ -20,25 +20,19 @@
#ifndef ARM_LDST_H
#define ARM_LDST_H
-#include "exec/cpu_ldst.h"
-#include "qemu/bswap.h"
+#include "exec/translator.h"
/* Load an instruction and return it in the standard little-endian order */ static inline uint32_t arm_ldl_code(CPUARMState *env, target_ulong addr,
bool sctlr_b)
{
- uint32_t insn = cpu_ldl_code(env, addr);
- if (bswap_code(sctlr_b)) {
- return bswap32(insn);
- } - return insn; + return translator_ld32(env, addr, bswap_code(sctlr_b));
}
/* Ditto, for a halfword (Thumb) instruction */
static inline uint16_t arm_lduw_code(CPUARMState *env, target_ulong addr,
bool sctlr_b)
{
- uint16_t insn;
#ifndef CONFIG_USER_ONLY
/* In big-endian (BE32) mode, adjacent Thumb instructions have been swapped
within each word. Undo that now. */
@@ -46,11 +40,7 @@ static inline uint16_t arm_lduw_code(CPUARMState *env, target_ulong addr,
addr ^= 2;
} #endif
- insn = cpu_lduw_code(env, addr);
- if (bswap_code(sctlr_b)) {
- return bswap16(insn);
- } - return insn; + return translator_ld16(env, addr, bswap_code(sctlr_b));
}
#endif
Re: [Qemu-devel] [RFC 23/48] translator: add plugin_insn argument to translate_insn
Quoted 2 lines · Alex Bennée
I was envisioning something more like the following so all the plugin gubins could be kept in the core code:
(snip)
Quoted 20 lines
static inline
uint32_tarm_ldl_code(CPUARMState *env,target_ulongaddr,
boolsctlr_b)
{(quoted diff) +1
-6- uint32_t insn = cpu_ldl_code(env, addr); - if (bswap_code(sctlr_b)) { - return bswap32(insn); - } - return insn; + return translator_ld32(env, addr, bswap_code(sctlr_b)); } /* Ditto, for a halfword (Thumb) instruction */ static inline uint16_t arm_lduw_code(CPUARMState *env, target_ulong addr, bool sctlr_b) { - uint16_t insn; #ifndef CONFIG_USER_ONLY /* In big-endian (BE32) mode, adjacent Thumb instructions have been swapped within each word. Undo that now. */
Review static inline uint16_t arm_lduw_code(CPUARMState *env, target_ulong addr, · line 44
Quoted 10 lines
(quoted diff) +1
-5@@ -46,11 +40,7 @@ static inline uint16_t arm_lduw_code(CPUARMState *env, target_ulong addr, addr ^= 2; } #endif - insn = cpu_lduw_code(env, addr); - if (bswap_code(sctlr_b)) { - return bswap16(insn); - } - return insn; + return translator_ld16(env, addr, bswap_code(sctlr_b)); }
I like this, thanks.
However, for Thumb I think we still need to call qemu_plugin_insn_append directly:
@@ -13304,11 +13306,16 @@ static void thumb_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
insn = arm_lduw_code(env, dc->pc, dc->sctlr_b);
is_16bit = thumb_insn_is_16bit(dc, insn);
dc->pc += 2;- if (!is_16bit) {
+ if (is_16bit) {
+ uint16_t insn16 = insn;
+
+ qemu_plugin_insn_append(tcg_ctx->plugin_insn, &insn16, sizeof(insn16));
+ } else {
uint32_t insn2 = arm_lduw_code(env, dc->pc, dc->sctlr_b);
insn = insn << 16 | insn2;
dc->pc += 2;
+ qemu_plugin_insn_append(tcg_ctx->plugin_insn, &insn, sizeof(insn));
}Otherwise we might mess up the contents of 32-bit insns.
Thanks,
E.
Re: [Qemu-devel] [RFC 23/48] translator: add plugin_insn argument to translate_insn
In reply to Emilio G. CotaQuoted 19 lines
From: Emilio G. Cota [mailto:[email protected]]
Quoted 17 lines · Richard Henderson
Quoted 9 lines · Alex Bennée
I'm not convinced this is the best way to go about it. We end up having to sprinkle the plugin calls into each decoder rather than keeping all the infrastructure in the common main loop. However the common loop will need to know the total number of bytes decoded so we could change the declaration to:
int (*translate_insn)(DisasContextBase *db, CPUState *cpu);and return the number of bytes decoded.
Returning the number of bytes is more difficult than simply just
old_pc = db->pc_next; opc->translate_insn(db, cpu); bytes = db->pc_next - old_pc;
Quoted 8 lines
Quoted 1 line
requiring no target changes at all.
The main reason why I added the
qemu_plugin_insn_appendcalls was to avoid reading the instructions twice from guest memory, because I was worried that doing so might somehow alter the guest's execution, e.g. what if we read a cross-page instruction, and both pages mapped to the same TLB entry? We'd end up having more TLB misses because instrumentation was enabled.
In our plugins we use cpu_debug_rw function.
But I think that your example with mapping of the page and simultaneous unmapping of the previous is impossible, because both pages should be available to the translator for creating the TB.
The translation immediately interrupted with TLB miss and repeated again after mapping. It means that the cross-page instruction should not be unmapped until it completely executes.
Pavel Dovgalyuk
RFC 24/48 translator: add .ctx_base_offset and .ctx_size to TranslatorOps
In reply to Emilio G. Cota--- include/exec/translator.h | 4 ++++ 1 file changed, 4 insertions(+)
include/exec/translator.h +4 -0
@@ -106,6 +106,8 @@ typedef struct DisasContextBase {* * @disas_log: * Print instruction disassembly to log. +* @ctx_base_offset: offset of DisasContextBase within DisasContext. +* @ctx_size: size of DisasContext. */ typedef struct TranslatorOps { void (*init_disas_context)(DisasContextBase *db, CPUState *cpu);
@@ -117,6 +119,8 @@ typedef struct TranslatorOps { struct qemu_plugin_insn *plugin_insn);void (*tb_stop)(DisasContextBase *db,CPUState *cpu);void (*disas_log)(const DisasContextBase *db,CPUState *cpu); +size_t ctx_base_offset; +size_t ctx_size; } TranslatorOps;/**
Signature · 1 line
2.17.1
RFC 25/48 target/arm: prepare for 2-pass translation
In reply to Emilio G. Cota--- target/arm/translate-a64.c | 8 ++++++-- target/arm/translate.c | 25 +++++++++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-)
target/arm/translate-a64.c +6 -2
@@ -13783,11 +13783,13 @@ static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn) }/* C3.1 A64 instruction index by encoding */ -static void disas_a64_insn (CPUARMState *env,DisasContext *s) +static void disas_a64_insn (CPUARMState *env,DisasContext *s, +struct qemu_plugin_insn *plugin_insn) {uint32_t insn; insn =arm_ldl_code (env, s->pc, s->sctlr_b); +qemu_plugin_insn_append (plugin_insn, &insn,sizeof (insn)); s->insn = insn; s->pc +=4 ;
@@ -13959,7 +13961,7 @@ static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu, default_exception_el(dc)); dc->base.is_jmp =DISAS_NORETURN ; }else { -disas_a64_insn (env, dc); +disas_a64_insn (env, dc, plugin_insn); } dc->base.pc_next = dc->pc;
@@ -14058,4 +14060,6 @@ const TranslatorOps aarch64_translator_ops = { .translate_insn = aarch64_tr_translate_insn, .tb_stop = aarch64_tr_tb_stop, .disas_log = aarch64_tr_disas_log, + .ctx_base_offset = offsetof(DisasContext, base), + .ctx_size = sizeof(DisasContext), };
target/arm/translate.c +21 -4
@@ -10198,7 +10198,8 @@ gen_thumb2_data_op(DisasContext *s, int op, int conds, uint32_t shifter_out, }/* Translate a 32-bit thumb instruction. */ -static void disas_thumb2_insn (DisasContext *s,uint32_t insn) +static void disas_thumb2_insn (DisasContext *s,uint32_t insn, +struct qemu_plugin_insn *plugin_insn) {uint32_t imm, shift, offset;uint32_t rd, rn, rm, rs;
@@ -11736,7 +11737,8 @@ illegal_op: default_exception_el(s)); } -static void disas_thumb_insn (DisasContext *s,uint32_t insn) +static void disas_thumb_insn (DisasContext *s,uint32_t insn, +struct qemu_plugin_insn *plugin_insn) {uint32_t val, op, rm, rn, rd, shift, cond;int32_t offset;
@@ -12800,6 +12802,7 @@ static void arm_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu, insn =arm_ldl_code (env, dc->pc, dc->sctlr_b); dc->insn = insn; +qemu_plugin_insn_append (plugin_insn, &insn,sizeof (insn)); dc->pc +=4 ;disas_arm_insn (dc, insn);
@@ -12870,11 +12873,21 @@ static void thumb_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu, insn =arm_lduw_code (env, dc->pc, dc->sctlr_b); is_16bit =thumb_insn_is_16bit (dc, insn); dc->pc +=2 ; +if (plugin_insn) { +uint16_t insn16 = insn; + +qemu_plugin_insn_append (plugin_insn, &insn16,sizeof (insn16)); + }if (!is_16bit) {uint32_t insn2 =arm_lduw_code (env, dc->pc, dc->sctlr_b); insn = insn <<16 | insn2; dc->pc +=2 ; +if (plugin_insn) { +uint16_t insn16 = insn2; + +qemu_plugin_insn_append (plugin_insn, &insn16,sizeof (insn16)); + } } dc->insn = insn;
@@ -12887,9 +12900,9 @@ static void thumb_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu, }if (is_16bit) { -disas_thumb_insn (dc, insn); +disas_thumb_insn (dc, insn, plugin_insn); }else { -disas_thumb2_insn (dc, insn); +disas_thumb2_insn (dc, insn, plugin_insn); }/* Advance the Thumb condexec condition. */
@@ -13064,6 +13077,8 @@ static const TranslatorOps arm_translator_ops = { .translate_insn = arm_tr_translate_insn, .tb_stop = arm_tr_tb_stop, .disas_log = arm_tr_disas_log, + .ctx_base_offset = offsetof(DisasContext, base), + .ctx_size = sizeof(DisasContext), };static const TranslatorOps thumb_translator_ops = {
@@ -13074,6 +13089,8 @@ static const TranslatorOps thumb_translator_ops = { .translate_insn = thumb_tr_translate_insn, .tb_stop = arm_tr_tb_stop, .disas_log = arm_tr_disas_log, + .ctx_base_offset = offsetof(DisasContext, base), + .ctx_size = sizeof(DisasContext), };/* generate intermediate code for basic block 'tb'. */
Signature · 1 line
2.17.1
RFC 26/48 target/ppc: prepare for 2-pass translation
In reply to Emilio G. Cota--- target/ppc/translate.c | 5 +++++ 1 file changed, 5 insertions(+)
target/ppc/translate.c +5 -0
@@ -7565,6 +7565,9 @@ static void ppc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs, ctx->opcode,opc1 (ctx->opcode),opc2 (ctx->opcode),opc3 (ctx->opcode),opc4 (ctx->opcode), ctx->le_mode ? "little" : "big"); + +qemu_plugin_insn_append (plugin_insn, &ctx->opcode,sizeof (ctx->opcode)); + ctx->base.pc_next +=4 ; table = env->opcodes; handler = table[opc1 (ctx->opcode)];
@@ -7664,6 +7667,8 @@ static const TranslatorOps ppc_tr_ops = { .translate_insn = ppc_tr_translate_insn, .tb_stop = ppc_tr_tb_stop, .disas_log = ppc_tr_disas_log, + .ctx_base_offset = offsetof(DisasContext, base), + .ctx_size = sizeof(DisasContext), };void gen_intermediate_code (CPUState *cs,struct TranslationBlock *tb)
Signature · 1 line
2.17.1
RFC 27/48 target/sh4: prepare for 2-pass translation (WIP)
In reply to Emilio G. CotaXXX: cleanly get the gUSA instructions
--- target/sh4/translate.c | 3 +++ 1 file changed, 3 insertions(+)
target/sh4/translate.c +3 -0
@@ -2333,6 +2333,7 @@ static void sh4_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs,#endif ctx->opcode =cpu_lduw_code (env, ctx->base.pc_next); +qemu_plugin_insn_append (plugin_insn, &ctx->opcode,sizeof (ctx->opcode));decode_opc (ctx); ctx->base.pc_next +=2 ; }
@@ -2381,6 +2382,8 @@ static const TranslatorOps sh4_tr_ops = { .translate_insn = sh4_tr_translate_insn, .tb_stop = sh4_tr_tb_stop, .disas_log = sh4_tr_disas_log, + .ctx_base_offset = offsetof(DisasContext, base), + .ctx_size = sizeof(DisasContext), };void gen_intermediate_code (CPUState *cs,TranslationBlock *tb)
Signature · 1 line
2.17.1
RFC 28/48 target/i386: prepare for 2-pass translation
In reply to Emilio G. Cota--- target/i386/translate.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-)
target/i386/translate.c +28 -7
@@ -142,6 +142,7 @@ typedef struct DisasContext {TCGv_i32 tmp3_i32;TCGv_i64 tmp1_i64; +struct qemu_plugin_insn *plugin_insn;sigjmp_buf jmpbuf; } DisasContext;
@@ -1900,28 +1901,43 @@ static uint64_t advance_pc(CPUX86State *env, DisasContext *s, int num_bytes)static inline uint8_t x86_ldub_code (CPUX86State *env,DisasContext *s) { -return cpu_ldub_code (env,advance_pc (env, s,1 )); +uint8_t ret =cpu_ldub_code (env,advance_pc (env, s,1 )); + +qemu_plugin_insn_append (s->plugin_insn, &ret,sizeof (ret)); +return ret; }static inline int16_t x86_ldsw_code (CPUX86State *env,DisasContext *s) { -return cpu_ldsw_code (env,advance_pc (env, s,2 )); +int16_t ret =cpu_ldsw_code (env,advance_pc (env, s,2 )); + +qemu_plugin_insn_append (s->plugin_insn, &ret,sizeof (ret)); +return ret; }static inline uint16_t x86_lduw_code (CPUX86State *env,DisasContext *s) { -return cpu_lduw_code (env,advance_pc (env, s,2 )); +uint16_t ret =cpu_lduw_code (env,advance_pc (env, s,2 )); + +qemu_plugin_insn_append (s->plugin_insn, &ret,sizeof (ret)); +return ret; }static inline uint32_t x86_ldl_code (CPUX86State *env,DisasContext *s) { -return cpu_ldl_code (env,advance_pc (env, s,4 )); +uint32_t ret =cpu_ldl_code (env,advance_pc (env, s,4 )); + +qemu_plugin_insn_append (s->plugin_insn, &ret,sizeof (ret)); +return ret; }#ifdef TARGET_X86_64 static inline uint64_t x86_ldq_code (CPUX86State *env,DisasContext *s) { -return cpu_ldq_code (env,advance_pc (env, s,8 )); +uint64_t ret =cpu_ldq_code (env,advance_pc (env, s,8 )); + +qemu_plugin_insn_append (s->plugin_insn, &ret,sizeof (ret)); +return ret; }#endif
@@ -4473,7 +4489,8 @@ static void gen_sse(CPUX86State *env, DisasContext *s, int b,/* convert one instruction. s->base.is_jmp is set if the translation must be stopped. Return the next pc value */ -static target_ulong disas_insn (DisasContext *s,CPUState *cpu) +static target_ulong disas_insn (DisasContext *s,CPUState *cpu, +struct qemu_plugin_insn *plugin_insn) {CPUX86State *env = cpu->env_ptr;int b, prefixes;
@@ -4484,6 +4501,8 @@ static target_ulong disas_insn(DisasContext *s, CPUState *cpu)int rex_w, rex_r;target_ulong pc_start = s->base.pc_next; + s->plugin_insn = plugin_insn; + s->pc_start = s->pc = pc_start; s->override =-1 ;#ifdef TARGET_X86_64
@@ -8529,7 +8548,7 @@ static void i386_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu, struct qemu_plugin_insn *plugin_insn) {DisasContext *dc =container_of (dcbase, DisasContext, base); -target_ulong pc_next =disas_insn (dc, cpu); +target_ulong pc_next =disas_insn (dc, cpu, plugin_insn);if (dc->tf || (dc->base.tb->flags &HF_INHIBIT_IRQ_MASK )) {/* if single step mode, we generate only one instruction and
@@ -8584,6 +8603,8 @@ static const TranslatorOps i386_tr_ops = { .translate_insn = i386_tr_translate_insn, .tb_stop = i386_tr_tb_stop, .disas_log = i386_tr_disas_log, + .ctx_base_offset = offsetof(DisasContext, base), + .ctx_size = sizeof(DisasContext), };/* generate intermediate code for basic block 'tb'. */
Signature · 1 line
2.17.1
RFC 29/48 target/hppa: prepare for 2-pass translation
In reply to Emilio G. Cota--- target/hppa/translate.c | 4 ++++ 1 file changed, 4 insertions(+)
target/hppa/translate.c +4 -0
@@ -4757,6 +4757,8 @@ static void hppa_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs,the page permissions for execute. */ uint32_t insn =cpu_ldl_code (env, ctx->base.pc_next); +qemu_plugin_insn_append (plugin_insn, &insn,sizeof (insn)); +/* Set up the IA queue for the next insn. This will be overwritten by a branch. */ if (ctx->iaoq_b ==-1 ) {
@@ -4887,6 +4889,8 @@ static const TranslatorOps hppa_tr_ops = { .translate_insn = hppa_tr_translate_insn, .tb_stop = hppa_tr_tb_stop, .disas_log = hppa_tr_disas_log, + .ctx_base_offset = offsetof(DisasContext, base), + .ctx_size = sizeof(DisasContext), };void gen_intermediate_code (CPUState *cs,struct TranslationBlock *tb)
Signature · 1 line
2.17.1
RFC 30/48 target/m68k: prepare for 2-pass translation
In reply to Emilio G. Cota--- target/m68k/translate.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
target/m68k/translate.c +8 -1
@@ -116,6 +116,7 @@ typedef struct DisasContext {int done_mac;int writeback_mask;TCGv writeback[8 ]; +struct qemu_plugin_insn *plugin_insn;#define MAX_TO_RELEASE 8int release_count;TCGv release[MAX_TO_RELEASE ];
@@ -375,6 +376,7 @@ static inline uint16_t read_im16(CPUM68KState *env, DisasContext *s)uint16_t im; im =cpu_lduw_code (env, s->pc); s->pc +=2 ; +qemu_plugin_insn_append (s->plugin_insn, &im,sizeof (im));return im; }
@@ -6092,7 +6094,10 @@ static void m68k_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu, {DisasContext *dc =container_of (dcbase, DisasContext, base);CPUM68KState *env = cpu->env_ptr; -uint16_t insn =read_im16 (env, dc); +uint16_t insn; + + dc->plugin_insn = plugin_insn; + insn =read_im16 (env, dc); opcode_table[insn](env, dc, insn);do_writebacks (dc);
@@ -6167,6 +6172,8 @@ static const TranslatorOps m68k_tr_ops = { .translate_insn = m68k_tr_translate_insn, .tb_stop = m68k_tr_tb_stop, .disas_log = m68k_tr_disas_log, + .ctx_base_offset = offsetof(DisasContext, base), + .ctx_size = sizeof(DisasContext), };void gen_intermediate_code (CPUState *cpu,TranslationBlock *tb)
Signature · 1 line
2.17.1
RFC 31/48 target/mips: prepare for 2-pass translation (WIP)
In reply to Emilio G. CotaXXX: fill in plugin_insn
--- target/mips/translate.c | 2 ++ 1 file changed, 2 insertions(+)
target/mips/translate.c +2 -0
@@ -25455,6 +25455,8 @@ static const TranslatorOps mips_tr_ops = { .translate_insn = mips_tr_translate_insn, .tb_stop = mips_tr_tb_stop, .disas_log = mips_tr_disas_log, + .ctx_base_offset = offsetof(DisasContext, base), + .ctx_size = sizeof(DisasContext), };void gen_intermediate_code (CPUState *cs,struct TranslationBlock *tb)
Signature · 1 line
2.17.1
RFC 32/48 target/alpha: prepare for 2-pass translation
In reply to Emilio G. Cota--- target/alpha/translate.c | 3 +++ 1 file changed, 3 insertions(+)
target/alpha/translate.c +3 -0
@@ -2990,6 +2990,7 @@ static void alpha_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu,CPUAlphaState *env = cpu->env_ptr;uint32_t insn =cpu_ldl_code (env, ctx->base.pc_next); +qemu_plugin_insn_append (plugin_insn, &insn,sizeof (insn)); ctx->base.pc_next +=4 ; ctx->base.is_jmp =translate_one (ctx, insn);
@@ -3046,6 +3047,8 @@ static const TranslatorOps alpha_tr_ops = { .translate_insn = alpha_tr_translate_insn, .tb_stop = alpha_tr_tb_stop, .disas_log = alpha_tr_disas_log, + .ctx_base_offset = offsetof(DisasContext, base), + .ctx_size = sizeof(DisasContext), };void gen_intermediate_code (CPUState *cpu,TranslationBlock *tb)
Signature · 1 line
2.17.1
RFC 33/48 target/riscv: prepare for 2-pass translation
In reply to Emilio G. Cota--- target/riscv/translate.c | 3 +++ 1 file changed, 3 insertions(+)
target/riscv/translate.c +3 -0
@@ -1851,6 +1851,7 @@ static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu, ctx->opcode =cpu_ldl_code (env, ctx->base.pc_next);decode_opc (env, ctx); +qemu_plugin_insn_append (plugin_insn, &ctx->opcode,sizeof (ctx->opcode)); ctx->base.pc_next = ctx->pc_succ_insn;if (ctx->base.is_jmp ==DISAS_NEXT ) {
@@ -1892,6 +1893,8 @@ static const TranslatorOps riscv_tr_ops = { .translate_insn = riscv_tr_translate_insn, .tb_stop = riscv_tr_tb_stop, .disas_log = riscv_tr_disas_log, + .ctx_base_offset = offsetof(DisasContext, base), + .ctx_size = sizeof(DisasContext), };void gen_intermediate_code (CPUState *cs,TranslationBlock *tb)
Signature · 1 line
2.17.1
RFC 34/48 target/s390x: prepare for 2-pass translation
In reply to Emilio G. Cota--- target/s390x/translate.c | 49 ++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 7 deletions(-)
target/s390x/translate.c +42 -7
@@ -5934,13 +5934,39 @@ static void extract_field(DisasFields *o, const DisasField *f, uint64_t insn) o->c[f->indexC] = r; } +static inline void +s390_plugin_insn (uint64_t insn,struct qemu_plugin_insn *plugin_insn,int len) +{ +uint16_t insn2; +uint32_t insn4; + +if (plugin_insn ==NULL ) { +return ; + } + +switch (len) { +case 2 : + insn2 = insn; +qemu_plugin_insn_append (plugin_insn, &insn2,sizeof (insn2)); +break ; +case 4 : + insn4 = insn; +qemu_plugin_insn_append (plugin_insn, &insn4,sizeof (insn4)); +break ; +default : +g_assert_not_reached (); + } +} +/* Lookup the insn at the current PC, extracting the operands into O and returning the info struct for the insn. Returns NULL for invalid insn. */ static const DisasInsn *extract_insn (CPUS390XState *env,DisasContext *s, -DisasFields *f) +DisasFields *f, +struct qemu_plugin_insn *plugin_insn) {uint64_t insn, pc = s->base.pc_next; +uint64_t insn4;int op, op2, ilen;const DisasInsn *info;
@@ -5960,13 +5986,19 @@ static const DisasInsn *extract_insn(CPUS390XState *env, DisasContext *s, ilen =get_ilen (op);switch (ilen) {case 2 : - insn = insn <<48 ; +s390_plugin_insn (insn, plugin_insn,2 ); + insn <<=48 ;break ;case 4 : - insn =ld_code4 (env, pc) <<32 ; + insn =ld_code4 (env, pc); +s390_plugin_insn (insn, plugin_insn,4 ); + insn <<=32 ;break ;case 6 : - insn = (insn <<48 ) | (ld_code4 (env, pc +2 ) <<16 ); +s390_plugin_insn (insn, plugin_insn,2 ); + insn4 =ld_code4 (env, pc +2 ); +s390_plugin_insn (insn4, plugin_insn,4 ); + insn = (insn <<48 ) | (insn4 <<16 );break ;default :g_assert_not_reached ();
@@ -6049,7 +6081,8 @@ static bool is_fp_pair(int reg)return !(reg &0x2 ); } -static DisasJumpType translate_one (CPUS390XState *env,DisasContext *s) +static DisasJumpType translate_one (CPUS390XState *env,DisasContext *s, +struct qemu_plugin_insn *plugin_insn) {const DisasInsn *insn;DisasJumpType ret =DISAS_NEXT ;
@@ -6057,7 +6090,7 @@ static DisasJumpType translate_one(CPUS390XState *env, DisasContext *s)DisasOps o;/* Search for the insn in the table. */ - insn =extract_insn (env, s, &f); + insn =extract_insn (env, s, &f, plugin_insn);/* Not found means unimplemented/illegal opcode. */ if (insn ==NULL ) {
@@ -6234,7 +6267,7 @@ static void s390x_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs,CPUS390XState *env = cs->env_ptr;DisasContext *dc =container_of (dcbase, DisasContext, base); - dc->base.is_jmp =translate_one (env, dc); + dc->base.is_jmp =translate_one (env, dc, plugin_insn);if (dc->base.is_jmp ==DISAS_NEXT ) {uint64_t page_start;
@@ -6300,6 +6333,8 @@ static const TranslatorOps s390x_tr_ops = { .translate_insn = s390x_tr_translate_insn, .tb_stop = s390x_tr_tb_stop, .disas_log = s390x_tr_disas_log, + .ctx_base_offset = offsetof(DisasContext, base), + .ctx_size = sizeof(DisasContext), };void gen_intermediate_code (CPUState *cs,TranslationBlock *tb)
Signature · 1 line
2.17.1
RFC 35/48 target/sparc: prepare for 2-pass translation
In reply to Emilio G. Cota--- target/sparc/translate.c | 3 +++ 1 file changed, 3 insertions(+)
target/sparc/translate.c +3 -0
@@ -5902,6 +5902,7 @@ static void sparc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs,unsigned int insn; insn =cpu_ldl_code (env, dc->pc); +qemu_plugin_insn_append (plugin_insn, &insn,sizeof (insn)); dc->base.pc_next +=4 ;disas_sparc_insn (dc, insn);
@@ -5961,6 +5962,8 @@ static const TranslatorOps sparc_tr_ops = { .translate_insn = sparc_tr_translate_insn, .tb_stop = sparc_tr_tb_stop, .disas_log = sparc_tr_disas_log, + .ctx_base_offset = offsetof(DisasContext, base), + .ctx_size = sizeof(DisasContext), };void gen_intermediate_code (CPUState *cs,TranslationBlock *tb)
Signature · 1 line
2.17.1
RFC 36/48 target/xtensa: prepare for 2-pass translation
In reply to Emilio G. Cota--- target/xtensa/translate.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
target/xtensa/translate.c +6 -2
@@ -879,7 +879,8 @@ static inline unsigned xtensa_op0_insn_len(DisasContext *dc, uint8_t op0)return xtensa_isa_length_from_chars (dc->config->isa, &op0); } -static void disas_xtensa_insn (CPUXtensaState *env,DisasContext *dc) +static void disas_xtensa_insn (CPUXtensaState *env,DisasContext *dc, +struct qemu_plugin_insn *plugin_insn) {xtensa_isa isa = dc->config->isa;unsigned char b[MAX_INSN_LENGTH ] = {cpu_ldub_code (env, dc->pc)};
@@ -916,6 +917,7 @@ static void disas_xtensa_insn(CPUXtensaState *env, DisasContext *dc)for (i =1 ; i < len; ++i) { b[i] =cpu_ldub_code (env, dc->pc + i); } +qemu_plugin_insn_append (plugin_insn, b, len);xtensa_insnbuf_from_chars (isa, dc->insnbuf, b, len); fmt =xtensa_format_decode (isa, dc->insnbuf);if (fmt ==XTENSA_UNDEFINED ) {
@@ -1183,7 +1185,7 @@ static void xtensa_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu,gen_ibreak_check (env, dc); } -disas_xtensa_insn (env, dc); +disas_xtensa_insn (env, dc, plugin_insn);if (dc->icount) {tcg_gen_mov_i32 (cpu_SR[ICOUNT ], dc->next_icount);
@@ -1241,6 +1243,8 @@ static const TranslatorOps xtensa_translator_ops = { .translate_insn = xtensa_tr_translate_insn, .tb_stop = xtensa_tr_tb_stop, .disas_log = xtensa_tr_disas_log, + .ctx_base_offset = offsetof(DisasContext, base), + .ctx_size = sizeof(DisasContext), };void gen_intermediate_code (CPUState *cpu,TranslationBlock *tb)
Signature · 1 line
2.17.1
RFC 37/48 target/openrisc: prepare for 2-pass translation
In reply to Emilio G. Cota--- target/openrisc/translate.c | 4 ++++ 1 file changed, 4 insertions(+)
target/openrisc/translate.c +4 -0
@@ -1308,6 +1308,8 @@ static void openrisc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs,OpenRISCCPU *cpu =OPENRISC_CPU (cs);uint32_t insn =cpu_ldl_code (&cpu->env, dc->base.pc_next); +qemu_plugin_insn_append (plugin_insn, &insn,sizeof (insn)); +if (!decode (dc, insn)) {gen_illegal_exception (dc); }
@@ -1407,6 +1409,8 @@ static const TranslatorOps openrisc_tr_ops = { .translate_insn = openrisc_tr_translate_insn, .tb_stop = openrisc_tr_tb_stop, .disas_log = openrisc_tr_disas_log, + .ctx_base_offset = offsetof(DisasContext, base), + .ctx_size = sizeof(DisasContext), };void gen_intermediate_code (CPUState *cs,struct TranslationBlock *tb)
Signature · 1 line
2.17.1
RFC 38/48 translator: implement 2-pass translation
In reply to Emilio G. CotaThe second pass only occurs when a plugin has subscribed to TB translation events.
--- tcg/tcg.h | 8 ++++ accel/tcg/translator.c | 91 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 2 deletions(-)
tcg/tcg.h +8 -0
@@ -720,6 +720,14 @@ struct TCGContext {TCGLabel *exitreq_label; +/* +* We keep one plugin_tb struct per TCGContext. Note that on every TB +* translation we clear but do not free its contents; this way we +* avoid a lot of malloc/free churn, since after a few TB's it's +* unlikely that we'll need to allocate either more instructions or more +* space for instructions (for variable-instruction-length ISAs). +*/ +struct qemu_plugin_tb plugin_tb;struct qemu_plugin_dyn_cb_arr *plugin_mem_cb;struct qemu_plugin_insn *plugin_insn;
accel/tcg/translator.c +89 -2
130 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 38/48] translator: implement 2-pass translation
Quoted 17 lines · Emilio G. Cota
The second pass only occurs when a plugin has subscribed to TB translation events.
Signed-off-by Emilio G. Cota <[email protected]>--- tcg/tcg.h | 8 ++++ accel/tcg/translator.c | 91 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 2 deletions(-)tcg/tcg.h +0
-0@@ -720,6 +720,14 @@ struct TCGContext {TCGLabel *exitreq_label;
<snip>
tcg/tcg.h
Quoted 8 lines
}tcg/tcg.h +6
-0+if (tb_trans_cb && first_pass) { +qemu_plugin_tb_trans_cb (cpu, plugin_tb); + first_pass = false; + goto translate; + } +
So the only reason we are doing this two pass tango is to ensure the plugin can insert TCG ops before the actual translation has occurred?
I think we can do better, especially as the internal structures of TCGops are implemented as a list so ops and be inserted before and after other ops. This is currently only done by the optimiser at the moment, see:
TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *op, TCGOpcode opc, int narg); TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *op, TCGOpcode opc, int narg);
and all the base tcg ops end up going to tcg_emit_op which just appends to the tail. But if we can come up with a neater way to track the op used before the current translated expression we could do away with two phases translation completely.
Quoted 3 lines
/* Emit code to exit the TB, as indicated by db->is_jmp. */
ops->tb_stop(db, cpu);
gen_tb_end(db->tb, db->num_insns - bp_insn);--
Alex Bennée
Re: [Qemu-devel] [RFC 38/48] translator: implement 2-pass translation
Quoted 1 line · Alex Bennée
Emilio G. Cota <[email protected]> writes:
(snip)
Quoted 8 lines
accel/tcg/translator.c void translator_loop(const TranslatorOps *ops, DisasContextBase *db, · line 208
Quoted 5 lines
accel/tcg/translator.c +5
-0+if (tb_trans_cb && first_pass) { +qemu_plugin_tb_trans_cb (cpu, plugin_tb); + first_pass = false; + goto translate; + }So the only reason we are doing this two pass tango is to ensure the plugin can insert TCG ops before the actual translation has occurred?
Not only. The idea is to provide plugins with well-defined TBs, i.e. the instruction sizes and contents can be queried by the plugin before the plugin decides how/where to instrument the TB.
Since in the targets we generate TCG code and also generate host code in a single shot (TranslatorOps.translate_insn), the 2-pass approach is a workaround to first get the well-defined TB, and in the second pass inject the instrumentation in the appropriate places.
This is a bit of a waste but given that it only happens at translate time, it can have negligible performance impact -- I measured a 0.13% gmean slowdown for SPEC06int.
Quoted 5 lines
I think we can do better, especially as the internal structures of TCGops are implemented as a list so ops and be inserted before and after other ops. This is currently only done by the optimiser at the moment, see:
Quoted 7 lines
TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *op, TCGOpcode opc, int narg); TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *op, TCGOpcode opc, int narg);
and all the base tcg ops end up going to
tcg_emit_opwhich just appends to the tail. But if we can come up with a neater way to track the op used before the current translated expression we could do away with two phases translation completely.
This list of ops is generated via TranslatorOps.translate_insn. Unfortunately, this function also defines the guest insns that form the TB. Decoupling the two actions ("define the TB" and "translate to TCG ops") would be ideal, but I didn't want to rewrite all the target translators in QEMU, and opted instead for the 2-pass approach as a compromise.
Thanks,
Emilio
Re: [Qemu-devel] [RFC 38/48] translator: implement 2-pass translation
Quoted 9 lines · Emilio G. Cota
Quoted 1 line · Alex Bennée
Emilio G. Cota <[email protected]> writes:
(snip)
Quoted 6 lines
Quoted 5 lines
accel/tcg/translator.c +5
-0+if (tb_trans_cb && first_pass) { +qemu_plugin_tb_trans_cb (cpu, plugin_tb); + first_pass = false; + goto translate; + }
Quoted 6 lines
Quoted 2 lines
So the only reason we are doing this two pass tango is to ensure the plugin can insert TCG ops before the actual translation has occurred?
Not only. The idea is to provide plugins with well-defined TBs, i.e. the instruction sizes and contents can be queried by the plugin before the plugin decides how/where to instrument the TB.
Hmmm, this seems a little to close to internal knowledge of the TCG. Is the idea that a plugin might make a different decision based on the number of a particular type of instruction in the translation block?
This seems like it would get broken if we wanted to implement other types of TranslationBlock (e.g. hot-blocks with multiple exits for the non-hot case).
That said looking at the examples using it so far it doesn't seem to be doing more than looking at the total number of instructions for a given translation block.
Quoted 5 lines
Since in the targets we generate TCG code and also generate host code in a single shot (TranslatorOps.translate_insn), the 2-pass approach is a workaround to first get the well-defined TB, and in the second pass inject the instrumentation in the appropriate places.
Richard's suggestion of providing a central translator_ldl_code could keep the book keeping of each instruction location and contents in the core translator. With a little tweaking to the TCG we could then insert our instrumentation at the end of the pass with all the knowledge we want to export to the plugin.
Inserting instrumentation after instructions have executed will be trickier though due to reasons Peter mentioned on IRC.
Quoted 3 lines
This is a bit of a waste but given that it only happens at translate time, it can have negligible performance impact -- I measured a 0.13% gmean slowdown for SPEC06int.
I'm less concerned about efficiency as complicating the code, especially if we are baking in concepts that restrict our freedom to change code generation around going forward.
Quoted 14 lines
Quoted 6 lines
I think we can do better, especially as the internal structures of TCGops are implemented as a list so ops and be inserted before and after other ops. This is currently only done by the optimiser at the moment, see:
Quoted 7 lines
TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *op, TCGOpcode opc, int narg); TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *op, TCGOpcode opc, int narg);
and all the base tcg ops end up going to
tcg_emit_opwhich just appends to the tail. But if we can come up with a neater way to track the op used before the current translated expression we could do away with two phases translation completely.
Quoted 5 lines
This list of ops is generated via TranslatorOps.translate_insn. Unfortunately, this function also defines the guest insns that form the TB. Decoupling the two actions ("define the TB" and "translate to TCG ops") would be ideal, but I didn't want to rewrite all the target translators in QEMU, and opted instead for the 2-pass approach as a compromise.
I don't quite follow. When we've done all our translation into TCG ops haven't we by definition defined the TB?
Maybe the interface shouldn't be per-insn and per-TB but just an arbitrary chunk of instructions. We could call the plugin with a list of instructions with some opaque data that can be passed back to the plugin APIs to allow insertion of instrumentation at the appropriate points. The initial implementation would be a single-pass and called after the TCG op generation. An instruction counter plugin would then be free to insert counter instrumentation as frequently or infrequently as it wants. These chunks wouldn't have to be tied to the internals of TCG and in the worst case we could just inform the plugin in 1 insn chunks without having to change the API?
What do you think?
--
Alex Bennée
Re: [Qemu-devel] [RFC 38/48] translator: implement 2-pass translation
Quoted 10 lines · Alex Bennée
Quoted 9 lines · Emilio G. Cota
Quoted 1 line · Alex Bennée
Emilio G. Cota <[email protected]> writes:
(snip)
Quoted 6 lines
Quoted 5 lines
accel/tcg/translator.c +5
-0+if (tb_trans_cb && first_pass) { +qemu_plugin_tb_trans_cb (cpu, plugin_tb); + first_pass = false; + goto translate; + }
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
So the only reason we are doing this two pass tango is to ensure the plugin can insert TCG ops before the actual translation has occurred?
Not only. The idea is to provide plugins with well-defined TBs, i.e. the instruction sizes and contents can be queried by the plugin before the plugin decides how/where to instrument the TB.
Hmmm, this seems a little to close to internal knowledge of the TCG.
As far as plugins are concerned, a "TB" is a sequence of instructions that will (unless there are exceptions) execute in sequence. That is, single-entry and single-exit.
QEMU is free to cut those in any way it wants, and there's no need for a 1:1 mapping between the "TBs" exported to plugins and the "TranslationBlock"s we manage internally in QEMU.
I thought about calling them "basic blocks", but then that could confuse users because not all TB's meet the definition of basic blocks, that is TBs might end in a non-branch instruction, whereas basic blocks don't.
So I kept the TB name, but note that all that plugins can assume about TBs, is that they are single-entry and single-exit, that's it. Different QEMU releases will cut TB's differently, and plugins will cope with that perfectly fine. IOW, this imposes no restrictions on TCG's implementation.
Quoted 2 lines
Is the idea that a plugin might make a different decision based on the number of a particular type of instruction in the translation block?
Plugins will make their decisions based on the TB's contents. For that, they need to know what instructions form the TB, and be able to disassemble them.
Quoted 3 lines
This seems like it would get broken if we wanted to implement other types of TranslationBlock (e.g. hot-blocks with multiple exits for the non-hot case).
Again, let's dissociate struct TranslationBlock vs. what we export; let's call the latter "plugin TB's" for discussion's sake.
If we implemented single-entry, multiple-exit traces, we could implement that in any way we wanted (e.g. expanding TranslationBlock, or grouping them into TranslationTraces or whatever we called them). Plugins would them be exposed to an interface similar to what Pin/DynamoRIO offer, that is, plugins can subscribe to "Trace" translation events, where Traces are lists of "plugin TB's".
Besides, I'm OK with having an API that we can break in the future. (Pin/DynamoRIO do it all the time.)
Quoted 3 lines
That said looking at the examples using it so far it doesn't seem to be doing more than looking at the total number of instructions for a given translation block.
OK, so I'm appending a more complicated example, where we use capstone to look at the instructions in a TB at translation time. (Just for illustration purposes, we then register an empty callback)
Quoted 9 lines
Quoted 5 lines
Since in the targets we generate TCG code and also generate host code in a single shot (TranslatorOps.translate_insn), the 2-pass approach is a workaround to first get the well-defined TB, and in the second pass inject the instrumentation in the appropriate places.
Richard's suggestion of providing a central
translator_ldl_codecould keep the book keeping of each instruction location and contents in the core translator.
Yes, at least for most targets I think that will work.
Quoted 3 lines
With a little tweaking to the TCG we could then insert our instrumentation at the end of the pass with all the knowledge we want to export to the plugin.
After .translate_insn has returned for the last instruction, how do we insert the instrumentation that the plugin wants--say, a TB callback at the beginning of the TB, memory callbacks for the 2nd instruction, and an insn callback before the 3rd instruction executes?
I don't see how we could achieve that with "a little tweaking" instead of a 2nd pass, but I'd love to be wrong =)
Quoted 2 lines
Inserting instrumentation after instructions have executed will be trickier though due to reasons Peter mentioned on IRC.
Particularly the last instruction in a TB; by the time we return from .translate_insn, all code we insert will most likely be dead.
Quoted 7 lines
Quoted 3 lines
This is a bit of a waste but given that it only happens at translate time, it can have negligible performance impact -- I measured a 0.13% gmean slowdown for SPEC06int.
I'm less concerned about efficiency as complicating the code, especially if we are baking in concepts that restrict our freedom to change code generation around going forward.
Agreed. I don't think exposing for now "plugin TB"s, i.e. single-entry, single-exit blocks of insns restricts our future designs. And if all else fails, we should reserve the right to break the API (e.g. via new version numbers).
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
I think we can do better, especially as the internal structures of TCGops are implemented as a list so ops and be inserted before and after other ops. This is currently only done by the optimiser at the moment, see:
TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *op, TCGOpcode opc, int narg); TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *op, TCGOpcode opc, int narg);
and all the base tcg ops end up going to
tcg_emit_opwhich just appends to the tail. But if we can come up with a neater way to track the op used before the current translated expression we could do away with two phases translation completely.
Quoted 8 lines
Quoted 5 lines
This list of ops is generated via TranslatorOps.translate_insn. Unfortunately, this function also defines the guest insns that form the TB. Decoupling the two actions ("define the TB" and "translate to TCG ops") would be ideal, but I didn't want to rewrite all the target translators in QEMU, and opted instead for the 2-pass approach as a compromise.
I don't quite follow. When we've done all our translation into TCG ops haven't we by definition defined the TB?
Yes, that's precisely my point.
The part that's missing is that once the TB is defined, we want to insert instrumentation. Unfortunately, the "TCG ops" we get after the 1st pass (no instrumentation), are very different from the list of "TCG ops" that we get after the 2nd pass (after having injected instrumentation). Could we get the same output of the 2nd pass, just by using the output of the 1st and the list of injection points? It's probably possible, but it seems very hard to do. (Think for instance of memory callbacks, and further the complication of when they use helpers).
The only reasonable way to do this I think would be to leave behind "placeholder" TCG ops, that then we could scan to add further TCG ops. But you'll agree with me that the 2nd pass is simpler :P
Quoted 4 lines
Maybe the interface shouldn't be per-insn and per-TB but just an arbitrary chunk of instructions. We could call the plugin with a list of instructions with some opaque data that can be passed back to the plugin APIs to allow insertion of instrumentation at the appropriate points.
This is what this series implements. It just happens that these chunks match our internal translation blocks, but there's no need for that (and for now, no good reason for them not to match.)
Quoted 8 lines
The initial implementation would be a single-pass and called after the TCG op generation. An instruction counter plugin would then be free to insert counter instrumentation as frequently or infrequently as it wants. These chunks wouldn't have to be tied to the internals of TCG and in the worst case we could just inform the plugin in 1 insn chunks without having to change the API?
What do you think?
With a single pass, all you can do is to add a callback with a descriptor of what just executed. So the "instruction counter" example would work OK.
But what about a plugin that needed only memory accesses performed by, say, xchg instructions? It'd have to subscribe to *all* memory accesses, because after a TB is generated we cannot go back to instrument something (due to the single pass), and then somehow figure out a way to discard non-xchg-generated memory accesses at run-time.
So having instruction-grained injection is very valuable, and it's not surprising that Pin/DynamoRIO provide that in their API. With this series I'm trying to provide something similar.
Thanks,
Emilio
---
#include <inttypes.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <capstone/capstone.h>
#include <qemu-plugin.h>
struct tb {
size_t n_insns;
};static csh cap_handle;
static cs_insn *cap_insn;
static void vcpu_tb_exec(unsigned int cpu_index, void *udata) { }
static void vcpu_tb_trans(qemu_plugin_id_t id, unsigned int cpu_index, struct qemu_plugin_tb *tb)
{
struct tb *desc;
size_t n = qemu_plugin_tb_n_insns(tb);
size_t i;
for (i = 0; i < n; i++) {
struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
size_t size = qemu_plugin_insn_size(insn);
const uint8_t *code = qemu_plugin_insn_data(insn);
uint64_t offset = 0;
bool success;
success = cs_disasm_iter(cap_handle, &code, &size, &offset, cap_insn);
assert(success);
}
desc = malloc(sizeof(*desc));
assert(desc);
desc->n_insns = n;
qemu_plugin_register_vcpu_tb_exec_cb(tb, vcpu_tb_exec,
QEMU_PLUGIN_CB_NO_REGS, desc);
}QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, int argc, char **argv)
{
if (cs_open(CS_ARCH_X86, CS_MODE_64, &cap_handle) != CS_ERR_OK) {
return -1;
}
cap_insn = cs_malloc(cap_handle);
if (cap_insn == NULL) {
return -1;
}
qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
return 0;
}Re: [Qemu-devel] [RFC 38/48] translator: implement 2-pass translation
Quoted 13 lines · Emilio G. Cota
Quoted 3 lines · Alex Bennée
With a little tweaking to the TCG we could then insert our instrumentation at the end of the pass with all the knowledge we want to export to the plugin.
After .translate_insn has returned for the last instruction, how do we insert the instrumentation that the plugin wants--say, a TB callback at the beginning of the TB, memory callbacks for the 2nd instruction, and an insn callback before the 3rd instruction executes?
I don't see how we could achieve that with "a little tweaking" instead of a 2nd pass, but I'd love to be wrong =)
(snip)
Quoted 15 lines
Quoted 2 lines
I don't quite follow. When we've done all our translation into TCG ops haven't we by definition defined the TB?
Yes, that's precisely my point.
The part that's missing is that once the TB is defined, we want to insert instrumentation. Unfortunately, the "TCG ops" we get after the 1st pass (no instrumentation), are very different from the list of "TCG ops" that we get after the 2nd pass (after having injected instrumentation). Could we get the same output of the 2nd pass, just by using the output of the 1st and the list of injection points? It's probably possible, but it seems very hard to do. (Think for instance of memory callbacks, and further the complication of when they use helpers).
Quoted 3 lines
The only reasonable way to do this I think would be to leave behind "placeholder" TCG ops, that then we could scan to add further TCG ops. But you'll agree with me that the 2nd pass is simpler :P
It might not be that much simpler after all!
I am exploring the approach you suggested, that is IIUC to do a single pass and then walk the list of Ops, adding (and reordering) Ops based on what the plugins have requested.
Contrary to what I wrote earlier today (quoted above), this approach seems quite promising, and certainly less ugly than doing the 2 passes.
I just wrote some code to go over the list and add TB callbacks, which go right before the first insn_start Op. The code is hack-ish in that we first generate the TCG ops we need, which get added to the end of the ops list, and then we go over those and move them to where we want them to be (before insn_start, in this case). But it works and it's less of a hack than doing the whole 2nd pass.
Insn callbacks will be trivial to implement this way; memory callbacks should be harder because there are several qemu_ld/st opcodes, but it should be doable; last, memory instrumentation of helpers might actually be easier than with the 2 passes, because here we just have to look for a Call TCG op to know whether a guest instruction uses helpers, and if it does we can wrap the call with the helpers to generate the setting/resetting of CPUState.plugin_mem_cbs.
I'll try to do what's in the previous paragraph tomorrow -- I'm appending what I did so far.
Thanks,
Emilio
---
accel/tcg/translator.c +6 -0
@@ -18,6 +18,7 @@#include "exec/gen-icount.h" #include "exec/log.h" #include "exec/translator.h" +#include "exec/plugin-gen.h" /* Pairs with tcg_clear_temp_count. To be called by #TranslatorOps.{translate_insn,tb_stop} if
@@ -142,6 +143,11 @@ void translator_loop(const TranslatorOps *ops, DisasContextBase *db,gen_tb_end (db->tb, db->num_insns - bp_insn);if (plugin_enabled) { +/* collect the plugins' instrumentation */ +qemu_plugin_tb_trans_cb (cpu, &tcg_ctx->plugin_tb); +/* inject instrumentation */ +qemu_plugin_gen_inject (); +/* done */ tcg_ctx->plugin_insn =NULL ; }
accel/tcg/plugin-gen.c +82 -1
103 lines not on this page · raw message
Re: [Qemu-devel] [RFC 38/48] translator: implement 2-pass translation
Quoted 45 lines · Emilio G. Cota
Quoted 13 lines · Emilio G. Cota
Quoted 3 lines · Alex Bennée
With a little tweaking to the TCG we could then insert our instrumentation at the end of the pass with all the knowledge we want to export to the plugin.
After .translate_insn has returned for the last instruction, how do we insert the instrumentation that the plugin wants--say, a TB callback at the beginning of the TB, memory callbacks for the 2nd instruction, and an insn callback before the 3rd instruction executes?
I don't see how we could achieve that with "a little tweaking" instead of a 2nd pass, but I'd love to be wrong =)
(snip)
Quoted 15 lines
Quoted 2 lines
I don't quite follow. When we've done all our translation into TCG ops haven't we by definition defined the TB?
Yes, that's precisely my point.
The part that's missing is that once the TB is defined, we want to insert instrumentation. Unfortunately, the "TCG ops" we get after the 1st pass (no instrumentation), are very different from the list of "TCG ops" that we get after the 2nd pass (after having injected instrumentation). Could we get the same output of the 2nd pass, just by using the output of the 1st and the list of injection points? It's probably possible, but it seems very hard to do. (Think for instance of memory callbacks, and further the complication of when they use helpers).
Quoted 3 lines
The only reasonable way to do this I think would be to leave behind "placeholder" TCG ops, that then we could scan to add further TCG ops. But you'll agree with me that the 2nd pass is simpler :P
It might not be that much simpler after all!
I am exploring the approach you suggested, that is IIUC to do a single pass and then walk the list of Ops, adding (and reordering) Ops based on what the plugins have requested.
Contrary to what I wrote earlier today (quoted above), this approach seems quite promising, and certainly less ugly than doing the 2 passes.
Quoted 6 lines
I just wrote some code to go over the list and add TB callbacks, which go right before the first
insn_startOp. The code is hack-ish in that we first generate the TCG ops we need, which get added to the end of the ops list, and then we go over those and move them to where we want them to be (beforeinsn_start, in this case). But it works and it's less of a hack than doing the whole 2nd pass.
But we should be able to insert the ops directly in the right place. That is the whole point of being a list right ;-)
Quoted 3 lines
Insn callbacks will be trivial to implement this way; memory callbacks should be harder because there are several qemu_ld/st opcodes, but it should be doable;
I was thinking about this last night. I wonder if we need to tag the memory tcg ops so we can find them afterwards during the insertion phase - but maybe the opcode itself provides enough information.
Quoted 6 lines
last, memory instrumentation
of helpers might actually be easier than with the 2 passes, because here we just have to look for a Call TCG op to know whether a guest instruction uses helpers, and if it does we can wrap the call with the helpers to generate the setting/resetting of CPUState.plugin_mem_cbs.
So merging the two helper calls into one from the target code?
Quoted 133 lines
I'll try to do what's in the previous paragraph tomorrow -- I'm appending what I did so far.
Thanks,
Emilio
---accel/tcg/translator.c +6
-0@@ -18,6 +18,7 @@#include "exec/gen-icount.h" #include "exec/log.h" #include "exec/translator.h" +#include "exec/plugin-gen.h" /* Pairs with tcg_clear_temp_count. To be called by #TranslatorOps.{translate_insn,tb_stop} if @@ -142,6 +143,11 @@ void translator_loop(const TranslatorOps *ops, DisasContextBase *db,gen_tb_end (db->tb, db->num_insns - bp_insn);if (plugin_enabled) { +/* collect the plugins' instrumentation */ +qemu_plugin_tb_trans_cb (cpu, &tcg_ctx->plugin_tb); +/* inject instrumentation */ +qemu_plugin_gen_inject (); +/* done */ tcg_ctx->plugin_insn =NULL ; }accel/tcg/plugin-gen.c +72
-1@@ -1,4 +1,5 @@#include "qemu/osdep.h" +#include "qemu/queue.h" #include "cpu.h" #include "tcg/tcg.h" #include "tcg/tcg-op.h" @@ -169,8 +170,61 @@ static void gen_vcpu_udata_cb(struct qemu_plugin_dyn_cb *cb)tcg_temp_free_i32 (cpu_index); } -void qemu_plugin_gen_vcpu_udata_callbacks (struct qemu_plugin_dyn_cb_arr *arr) +/* check that @a comes before @b */ +static inline void ops_check (const TCGOp *a,const TCGOp *b) { +const TCGOp *op; +bool seen_a = false; +bool seen_b = false; + +tcg_debug_assert (a != b); + QTAILQ_FOREACH(op, &tcg_ctx->ops, link) { +if (op == a) { +tcg_debug_assert (!seen_b); + seen_a = true; + }else if (op == b) { +tcg_debug_assert (seen_a); + seen_b = true; +break ; + } + } +} + +/* +* Move ops from @from to @dest. +* @from must come after @dest in the list. +*/ +static void ops_move (TCGOp *dest,TCGOp *from) +{ +TCGOp *curr; + +#ifdef CONFIG_DEBUG_TCG +ops_check (dest, from); +#endif + +if (QTAILQ_NEXT (dest, link) == from) { +/* nothing to do */ +return ; + } + curr = from; +do { +TCGOp *next =QTAILQ_NEXT (curr, link); + +/* +* This could be done more efficiently since (@from,end) will +* remain linked together, but most likely we just have a few +* elements, so this is simple enough. +*/ +QTAILQ_REMOVE (&tcg_ctx->ops, curr, link); +QTAILQ_INSERT_AFTER (&tcg_ctx->ops, dest, curr, link); + dest = curr; + curr = next; + }while (curr); +} + +static void inject_vcpu_udata_callbacks (struct qemu_plugin_dyn_cb_arr *arr,TCGOp *dest) +{ +TCGOp *last_op =tcg_last_op ();size_t i;for (i =0 ; i < arr->n; i++) {@@ -187,6 +241,10 @@ void qemu_plugin_gen_vcpu_udata_callbacks(struct qemu_plugin_dyn_cb_arr *arr)g_assert_not_reached (); } } +g_assert (tcg_last_op () != last_op); + last_op =QTAILQ_NEXT (last_op, link); +g_assert (last_op); +ops_move (dest, last_op); }/* @@ -228,3 +286,26 @@ void qemu_plugin_gen_disable_mem_helpers(void) plugin_mem_cbs));tcg_temp_free_ptr (ptr); } + +void qemu_plugin_gen_inject (void ) +{ +struct qemu_plugin_tb *plugin_tb = &tcg_ctx->plugin_tb; + +/* TB exec callbacks */ +if (plugin_tb->cbs.n) { +TCGOp *op; + +/* Insert the callbacks right before the first insn_start */ + QTAILQ_FOREACH(op, &tcg_ctx->ops, link) { +if (op->opc == INDEX_op_insn_start) { +break ;
accel/tcg/plugin-gen.c void qemu_plugin_gen_disable_mem_helpers(void) · line 311
Quoted 10 lines
accel/tcg/plugin-gen.c +10
-0+ } + } +/* a TB without insn_start? Something went wrong */ +g_assert (op); + op =QTAILQ_PREV (op, TCGOpHead, link); +/* we should have called gen_tb_start before the 1st insn */ +g_assert (op); +inject_vcpu_udata_callbacks (&plugin_tb->cbs, op); + } +}
--
Alex Bennée
Re: [Qemu-devel] [RFC 38/48] translator: implement 2-pass translation
Quoted 10 lines · Alex Bennée
Quoted 6 lines · Emilio G. Cota
I just wrote some code to go over the list and add TB callbacks, which go right before the first
insn_startOp. The code is hack-ish in that we first generate the TCG ops we need, which get added to the end of the ops list, and then we go over those and move them to where we want them to be (beforeinsn_start, in this case). But it works and it's less of a hack than doing the whole 2nd pass.But we should be able to insert the ops directly in the right place. That is the whole point of being a list right ;-)
Right, it's just hard sometimes to know exactly where to insert.
Quoted 7 lines
Quoted 3 lines
Insn callbacks will be trivial to implement this way; memory callbacks should be harder because there are several qemu_ld/st opcodes, but it should be doable;
I was thinking about this last night. I wonder if we need to tag the memory tcg ops so we can find them afterwards during the insertion phase - but maybe the opcode itself provides enough information.
We should be able to extract the info from the memop argument, I think.
Quoted 8 lines
Quoted 6 lines
last, memory instrumentation
of helpers might actually be easier than with the 2 passes, because here we just have to look for a Call TCG op to know whether a guest instruction uses helpers, and if it does we can wrap the call with the helpers to generate the setting/resetting of CPUState.plugin_mem_cbs.So merging the two helper calls into one from the target code?
Actually we don't need helpers to set/reset CPUState.plugin_mem_cbs; we do that in TCG directly. So here we could just add the "set" code right after "insn_start", and the "reset" code at the very end of the translation (right before tb_exit/goto_tb etc). The "reset" might still be dead code, but that is unavoidable because the helper might do a longjmp. But we can fix that by resetting the variable when returning from the jump.
Thanks,
E.
RFC 39/48 plugin: add API symbols to qemu-plugins.symbols
In reply to Emilio G. Cota--- qemu-plugins.symbols | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 qemu-plugins.symbols
qemu-plugins.symbols new file +32 -0
@@ -0,0 +1,32 @@ +{ + qemu_plugin_uninstall; + qemu_plugin_register_vcpu_init_cb; + qemu_plugin_register_vcpu_exit_cb; + qemu_plugin_register_vcpu_idle_cb; + qemu_plugin_register_vcpu_resume_cb; + qemu_plugin_register_vcpu_insn_exec_cb; + qemu_plugin_register_vcpu_mem_cb; + qemu_plugin_register_vcpu_mem_haddr_cb; + qemu_plugin_register_vcpu_mem_inline; + qemu_plugin_ram_addr_from_host; + qemu_plugin_register_vcpu_tb_trans_cb; + qemu_plugin_register_vcpu_tb_exec_cb; + qemu_plugin_register_vcpu_tb_exec_inline; + qemu_plugin_register_flush_cb; + qemu_plugin_register_vcpu_syscall_cb; + qemu_plugin_register_vcpu_syscall_ret_cb; + qemu_plugin_register_atexit_cb; + qemu_plugin_tb_n_insns; + qemu_plugin_tb_get_insn; + qemu_plugin_tb_vaddr; + qemu_plugin_insn_data; + qemu_plugin_insn_size; + qemu_plugin_insn_vaddr; + qemu_plugin_mem_size_shift; + qemu_plugin_mem_is_sign_extended; + qemu_plugin_mem_is_big_endian; + qemu_plugin_mem_is_store; + qemu_plugin_vcpu_for_each; + qemu_plugin_n_vcpus; + qemu_plugin_n_max_vcpus; +};
Signature · 1 line
2.17.1
RFC 40/48 plugin: let plugins control the virtual clock
In reply to Emilio G. Cota--- include/qemu/plugin-api.h | 9 +++++ include/qemu/plugin.h | 5 +++ plugin.c | 79 +++++++++++++++++++++++++++++++++++++++ stubs/plugin.c | 9 +++++ util/qemu-timer.c | 3 ++ qemu-plugins.symbols | 1 + stubs/Makefile.objs | 1 + 7 files changed, 107 insertions(+) create mode 100644 stubs/plugin.c
include/qemu/plugin-api.h +9 -0
@@ -218,6 +218,15 @@ void qemu_plugin_register_flush_cb(qemu_plugin_id_t id,void qemu_plugin_register_atexit_cb (qemu_plugin_id_t id,qemu_plugin_udata_cb_t cb,void *userdata); +typedef int64_t (*qemu_plugin_clock_func_t )(void ); + +/* +* Can only be called from plugin_init. +* Returns true on success +*/ +bool qemu_plugin_register_virtual_clock (qemu_plugin_id_t id, +qemu_plugin_clock_func_t clock); +/* returns -1 in user-mode */ int qemu_plugin_n_vcpus (void );
include/qemu/plugin.h +5 -0
@@ -127,6 +127,8 @@ struct qemu_plugin_tb {struct qemu_plugin_dyn_cb_arr cbs; }; +extern bool use_plugin_clock; +static inline void qemu_plugin_insn_append (struct qemu_plugin_insn *insn,const void *from,size_t size) {
@@ -191,6 +193,7 @@ void qemu_plugin_flush_cb(void);void qemu_plugin_atexit_cb (void );void qemu_plugin_add_dyn_cb_arr (struct qemu_plugin_dyn_cb_arr *arr); +int64_t plugin_get_clock (void );#else /* !CONFIG_PLUGINS */
@@ -234,6 +237,8 @@ static inlinevoid qemu_plugin_add_dyn_cb_arr (struct qemu_plugin_dyn_cb_arr *arr) { } +int64_t plugin_get_clock (void ); +#endif /* !CONFIG_PLUGINS */ #endif /* QEMU_PLUGIN_H */
plugin.c +79 -0
110 lines not on this page · raw message
stubs/plugin.c new file +9 -0
@@ -0,0 +1,9 @@ +#include "qemu/osdep.h" +#include "qemu/plugin.h" + +bool use_plugin_clock; + +int64_t plugin_get_clock (void ) +{ +abort (); +}
util/qemu-timer.c +3 -0
@@ -24,6 +24,7 @@#include "qemu/osdep.h" #include "qemu/main-loop.h" +#include "qemu/plugin.h" #include "qemu/timer.h" #include "sysemu/replay.h" #include "sysemu/sysemu.h"
@@ -601,6 +602,8 @@ int64_t qemu_clock_get_ns(QEMUClockType type)case QEMU_CLOCK_VIRTUAL_EXT :if (use_icount) {return cpu_get_icount (); + }else if (use_plugin_clock) { +return plugin_get_clock (); }else {return cpu_get_clock (); }
qemu-plugins.symbols +1 -0
@@ -16,6 +16,7 @@ qemu_plugin_register_vcpu_syscall_cb; qemu_plugin_register_vcpu_syscall_ret_cb; qemu_plugin_register_atexit_cb; + qemu_plugin_register_virtual_clock; qemu_plugin_tb_n_insns; qemu_plugin_tb_get_insn; qemu_plugin_tb_vaddr;
stubs/Makefile.objs +1 -0
@@ -23,6 +23,7 @@ stub-obj-y += migr-blocker.o stub-obj-y += change-state-handler.o stub-obj-y += monitor.o stub-obj-y += notify-event.o +stub-obj-y += plugin.o stub-obj-y += qtest.o stub-obj-y += replay.o stub-obj-y += runstate-check.o
Signature · 1 line
2.17.1
RFC 41/48 configure: add --enable-plugins
In reply to Emilio G. CotaFor now only add it for ELF platforms, since we rely on the linker's --dynamic-list flag to pass a list of symbols to be exported to the executable. An alternative would be to use -rdynamic, but that would expose all of QEMU's objects to plugins.
I have no experience with non-ELF systems but I suspect adding support for those should be pretty easy.
--- configure | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+)
configure +51 -0
@@ -30,6 +30,7 @@ TMPO="${TMPDIR1}/${TMPB}.o" TMPCXX="${TMPDIR1}/${TMPB}.cxx" TMPE="${TMPDIR1}/${TMPB}.exe" TMPMO="${TMPDIR1}/${TMPB}.mo" +TMPTXT="${TMPDIR1}/${TMPB}.txt" rm -f config.log
@@ -477,6 +478,7 @@ libxml2="" docker="no" debug_mutex="no" libpmem="" +plugins="no" # cross compilers defaults, can be overridden with --cross-cc-ARCH cross_cc_aarch64="aarch64-linux-gnu-gcc"
@@ -1443,6 +1445,10 @@ for opt do ;; --disable-libpmem) libpmem=no ;; + --enable-plugins) plugins="yes" + ;; + --disable-plugins) plugins="no" + ;; *) echo "ERROR: unknown option $opt" echo "Try '$0 --help' for more information"
@@ -1633,6 +1639,8 @@ Advanced options (experts only): xen pv domain builder --enable-debug-stack-usage track the maximum stack usage of stacks created by qemu_alloc_stack + --enable-plugins + enable plugins via shared library loading Optional features, enabled with --enable-FEATURE and disabled with --disable-FEATURE, default is enabled if available:
@@ -5204,6 +5212,42 @@ if compile_prog "" "" ; then atomic64=yes fi +######################################### +# See if --dynamic-list is supported by the linker + +cat > $TMPTXT <<EOF +{ + foo; +}; +EOF + +cat > $TMPC <<EOF +#include <stdio.h> +void foo(void); + +void foo(void) +{ + printf("foo\n"); +} + +int main(void) +{ + foo(); + return 0; +} +EOF + +if compile_prog "" "-Wl,--dynamic-list=$TMPTXT" ; then + ld_dynamic_list="yes" +else + if test "$plugins" = "yes" ; then + error_exit \ + "Plugin support requires specifying a set of symbols that " \ + "are exported to plugins. Unfortunately your linker doesn't " \ + "support the flag (--dynamic-list) used for this purpose." + fi +fi + ######################################## # See if 16-byte vector operations are supported. # Even without a vector unit the compiler may expand these.
@@ -6091,6 +6135,7 @@ echo "VxHS block device $vxhs" echo "capstone $capstone" echo "docker $docker" echo "libpmem support $libpmem" +echo "plugin support $plugins" if test "$sdl_too_old" = "yes"; then echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -6849,6 +6894,12 @@ if test "$libpmem" = "yes" ; then echo "CONFIG_LIBPMEM=y" >> $config_host_mak fi +if test "$plugins" = "yes" ; then + echo "CONFIG_PLUGINS=y" >> $config_host_mak + LIBS="-ldl $LIBS" + LDFLAGS="-Wl,--dynamic-list=\$(SRC_PATH)/qemu-plugins.symbols $LDFLAGS" +fi + if test "$tcg_interpreter" = "yes"; then QEMU_INCLUDES="-iquote \$(SRC_PATH)/tcg/tci $QEMU_INCLUDES" elif test "$ARCH" = "sparc64" ; then
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 41/48] configure: add --enable-plugins
Quoted 9 lines · Emilio G. Cota
For now only add it for ELF platforms, since we rely on the linker's --dynamic-list flag to pass a list of symbols to be exported to the executable. An alternative would be to use -rdynamic, but that would expose all of QEMU's objects to plugins.
I have no experience with non-ELF systems but I suspect adding support for those should be pretty easy.
Signed-off-by Emilio G. Cota <[email protected]>
As far as the configure logic is concerned:
I'm not quite following what is so special about the dynamic-list symbols compared to the rest of the symbols in the binary. when I do readelf -s they are all specified as GLOBAL DEFAULT.
Perhaps this will become clearer to me once I get to the implementation of the plugins later in the series?
Quoted 98 lines
--- configure | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+)configure +45
-0@@ -30,6 +30,7 @@ TMPO="${TMPDIR1}/${TMPB}.o" TMPCXX="${TMPDIR1}/${TMPB}.cxx" TMPE="${TMPDIR1}/${TMPB}.exe" TMPMO="${TMPDIR1}/${TMPB}.mo" +TMPTXT="${TMPDIR1}/${TMPB}.txt" rm -f config.log@@ -477,6 +478,7 @@ libxml2="" docker="no" debug_mutex="no" libpmem="" +plugins="no" # cross compilers defaults, can be overridden with --cross-cc-ARCH cross_cc_aarch64="aarch64-linux-gnu-gcc"@@ -1443,6 +1445,10 @@ for opt do ;; --disable-libpmem) libpmem=no ;; + --enable-plugins) plugins="yes" + ;; + --disable-plugins) plugins="no" + ;; *) echo "ERROR: unknown option $opt" echo "Try '$0 --help' for more information"@@ -1633,6 +1639,8 @@ Advanced options (experts only): xen pv domain builder --enable-debug-stack-usage track the maximum stack usage of stacks created by qemu_alloc_stack + --enable-plugins + enable plugins via shared library loading Optional features, enabled with --enable-FEATURE and disabled with --disable-FEATURE, default is enabled if available:@@ -5204,6 +5212,42 @@ if compile_prog "" "" ; then atomic64=yes fi +######################################### +# See if --dynamic-list is supported by the linker + +cat > $TMPTXT <<EOF +{ + foo; +}; +EOF + +cat > $TMPC <<EOF +#include <stdio.h> +void foo(void); + +void foo(void) +{ + printf("foo\n"); +} + +int main(void) +{ + foo(); + return 0; +} +EOF + +if compile_prog "" "-Wl,--dynamic-list=$TMPTXT" ; then + ld_dynamic_list="yes" +else + if test "$plugins" = "yes" ; then + error_exit \ + "Plugin support requires specifying a set of symbols that " \ + "are exported to plugins. Unfortunately your linker doesn't " \ + "support the flag (--dynamic-list) used for this purpose." + fi +fi + ######################################## # See if 16-byte vector operations are supported. # Even without a vector unit the compiler may expand these.@@ -6091,6 +6135,7 @@ echo "VxHS block device $vxhs" echo "capstone $capstone" echo "docker $docker" echo "libpmem support $libpmem" +echo "plugin support $plugins" if test "$sdl_too_old" = "yes"; then echo "-> Your SDL version is too old - please upgrade to have SDL support"@@ -6849,6 +6894,12 @@ if test "$libpmem" = "yes" ; then echo "CONFIG_LIBPMEM=y" >> $config_host_mak fi
configure if test "$libpmem" = "yes" ; then · line 6905
Quoted 10 lines
configure +6
-0+if test "$plugins" = "yes" ; then + echo "CONFIG_PLUGINS=y" >> $config_host_mak + LIBS="-ldl $LIBS" + LDFLAGS="-Wl,--dynamic-list=\$(SRC_PATH)/qemu-plugins.symbols $LDFLAGS" +fi + if test "$tcg_interpreter" = "yes"; then QEMU_INCLUDES="-iquote \$(SRC_PATH)/tcg/tci $QEMU_INCLUDES" elif test "$ARCH" = "sparc64" ; then
--
Alex Bennée
Re: [Qemu-devel] [RFC 41/48] configure: add --enable-plugins
Quoted 15 lines · Alex Bennée
Quoted 9 lines · Emilio G. Cota
For now only add it for ELF platforms, since we rely on the linker's --dynamic-list flag to pass a list of symbols to be exported to the executable. An alternative would be to use -rdynamic, but that would expose all of QEMU's objects to plugins.
I have no experience with non-ELF systems but I suspect adding support for those should be pretty easy.
Signed-off-by Emilio G. Cota <[email protected]>As far as the configure logic is concerned:
Quoted 8 lines
Reviewed-by Alex Bennée <[email protected]>I'm not quite following what is so special about the dynamic-list symbols compared to the rest of the symbols in the binary. when I do readelf -s they are all specified as GLOBAL DEFAULT.
Perhaps this will become clearer to me once I get to the implementation of the plugins later in the series?
You should look at `readelf --dyn-syms's output. There you'll see that the only defined function symbols are qemu_plugin_* ones (plus _init and _fini).
The goal is to prevent plugins from accessing symbols outside of the plugin API. For instance, if from plugin 'foo' I try to call qht_init (after having added the necessary declarations in foo.c), at dlopen time I get:
plugin_load: libfoo.so: undefined symbol: qht_init
The alternative is to export all symbols; we can do this by passing --export-dynamic to the linker instead of an explicit --dynamic-list. We can then access *any* global symbol in the QEMU executable. The previous example now works past dlopen, i.e. I can call qht_init from my plugin:
ERROR:/data/src/qemu/util/qht.c:401:qht_init: assertion failed: (cmp)
Thanks,
Emilio
Re: [Qemu-devel] [RFC 41/48] configure: add --enable-plugins
In reply to Emilio G. CotaQuoted 121 lines · Emilio G. Cota
For now only add it for ELF platforms, since we rely on the linker's --dynamic-list flag to pass a list of symbols to be exported to the executable. An alternative would be to use -rdynamic, but that would expose all of QEMU's objects to plugins.
I have no experience with non-ELF systems but I suspect adding support for those should be pretty easy.
Signed-off-by Emilio G. Cota <[email protected]>--- configure | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+)configure +51
-0@@ -30,6 +30,7 @@ TMPO="${TMPDIR1}/${TMPB}.o" TMPCXX="${TMPDIR1}/${TMPB}.cxx" TMPE="${TMPDIR1}/${TMPB}.exe" TMPMO="${TMPDIR1}/${TMPB}.mo" +TMPTXT="${TMPDIR1}/${TMPB}.txt" rm -f config.log@@ -477,6 +478,7 @@ libxml2="" docker="no" debug_mutex="no" libpmem="" +plugins="no" # cross compilers defaults, can be overridden with --cross-cc-ARCH cross_cc_aarch64="aarch64-linux-gnu-gcc"@@ -1443,6 +1445,10 @@ for opt do ;; --disable-libpmem) libpmem=no ;; + --enable-plugins) plugins="yes" + ;; + --disable-plugins) plugins="no" + ;; *) echo "ERROR: unknown option $opt" echo "Try '$0 --help' for more information"@@ -1633,6 +1639,8 @@ Advanced options (experts only): xen pv domain builder --enable-debug-stack-usage track the maximum stack usage of stacks created by qemu_alloc_stack + --enable-plugins + enable plugins via shared library loading Optional features, enabled with --enable-FEATURE and disabled with --disable-FEATURE, default is enabled if available:@@ -5204,6 +5212,42 @@ if compile_prog "" "" ; then atomic64=yes fi +######################################### +# See if --dynamic-list is supported by the linker + +cat > $TMPTXT <<EOF +{ + foo; +}; +EOF + +cat > $TMPC <<EOF +#include <stdio.h> +void foo(void); + +void foo(void) +{ + printf("foo\n"); +} + +int main(void) +{ + foo(); + return 0; +} +EOF + +if compile_prog "" "-Wl,--dynamic-list=$TMPTXT" ; then + ld_dynamic_list="yes" +else + if test "$plugins" = "yes" ; then + error_exit \ + "Plugin support requires specifying a set of symbols that " \ + "are exported to plugins. Unfortunately your linker doesn't " \ + "support the flag (--dynamic-list) used for this purpose." + fi +fi + ######################################## # See if 16-byte vector operations are supported. # Even without a vector unit the compiler may expand these.@@ -6091,6 +6135,7 @@ echo "VxHS block device $vxhs" echo "capstone $capstone" echo "docker $docker" echo "libpmem support $libpmem" +echo "plugin support $plugins" if test "$sdl_too_old" = "yes"; then echo "-> Your SDL version is too old - please upgrade to have SDL support"@@ -6849,6 +6894,12 @@ if test "$libpmem" = "yes" ; then echo "CONFIG_LIBPMEM=y" >> $config_host_mak fi +if test "$plugins" = "yes" ; then + echo "CONFIG_PLUGINS=y" >> $config_host_mak + LIBS="-ldl $LIBS" + LDFLAGS="-Wl,--dynamic-list=\$(SRC_PATH)/qemu-plugins.symbols $LDFLAGS" +fi + if test "$tcg_interpreter" = "yes"; then QEMU_INCLUDES="-iquote \$(SRC_PATH)/tcg/tci $QEMU_INCLUDES" elif test "$ARCH" = "sparc64" ; thenSignature · 1 line
2.17.1
ld64 on macOS has similar -exported_symbols_list option. Here's the reference:
-exported_symbols_list filename
The specified filename contains a list of global symbol names that will remain as global symbols in the output file. All other global symbols will be treated as if they were marked as __private_extern__ (aka visibility=hidden) and will not be global in the output file. The symbol names listed in filename must be one per line. Leading and trailing white space are not part of the symbol name. Lines starting with # are ignored, as are lines with only white space. Some wildcards (similar to shell file matching) are supported. The * matches zero or more characters. The ? matches one character. [abc] matches one character which must be an 'a', 'b', or 'c'. [a-z] matches any single lower case letter from 'a' to 'z'.
I can try your branch if you add support of the linker flag or send required changes via GitHub.
Best regards,
Roman
Re: [Qemu-devel] [RFC 41/48] configure: add --enable-plugins
Quoted 16 lines · Roman Bolshakov
ld64 on macOS has similar -exported_symbols_list option. Here's the reference:
-exported_symbols_list filename
The specified filename contains a list of global symbol names that will remain as global symbols in the output file. All other global symbols will be treated as if they were marked as__private_extern__(aka visibility=hidden) and will not be global in the output file. The symbol names listed in filename must be one per line. Leading and trailing white space are not part of the symbol name. Lines starting with # are ignored, as are lines with only white space. Some wildcards (similar to shell file matching) are supported. The * matches zero or more characters. The ? matches one character. [abc] matches one character which must be an 'a', 'b', or 'c'. [a-z] matches any single lower case letter from 'a' to 'z'.
Quoted 2 lines
I can try your branch if you add support of the linker flag or send required changes via GitHub.
Can you please try this branch? I added a commit with the appended.
https://github.com/cota/qemu/tree/plugin-v2
You can inspect the symbols in the final binary with readelf --dyn-syms' or nm -D'.
Thanks,
Emilio
---
configure +35 -8
@@ -5176,15 +5176,31 @@ int main(void) } EOF +ld_dynamic_list="no" if compile_prog "" "-Wl,--dynamic-list=$TMPTXT" ; then ld_dynamic_list="yes" -else - if test "$plugins" = "yes" ; then - error_exit \ - "Plugin support requires specifying a set of symbols that " \ - "are exported to plugins. Unfortunately your linker doesn't " \ - "support the flag (--dynamic-list) used for this purpose." - fi +fi + +######################################### +# See if -exported_symbols_list is supported by the linker + +cat > $TMPTXT <<EOF + foo +EOF + +ld_exported_symbols_list="no" +if compile_prog "" "-Wl,-exported_symbols_list,$TMPTXT" ; then + ld_exported_symbols_list="yes" +fi + +if test "$plugins" = "yes" && + test "$ld_dynamic_list" = "no" && + test "$ld_exported_symbols_list" = "no" ; then + error_exit \ + "Plugin support requires specifying a set of symbols that " \ + "are exported to plugins. Unfortunately your linker doesn't " \ + "support the flag (--dynamic-list or -exported_symbols_list) used " \ + "for this purpose." fi ########################################
@@ -6827,7 +6843,18 @@ fi if test "$plugins" = "yes" ; then echo "CONFIG_PLUGINS=y" >> $config_host_mak LIBS="-ldl $LIBS" - LDFLAGS="-Wl,--dynamic-list=\$(SRC_PATH)/qemu-plugins.symbols $LDFLAGS" + if test "$ld_dynamic_list" = "yes" ; then + LDFLAGS="-Wl,--dynamic-list=\$(SRC_PATH)/qemu-plugins.symbols $LDFLAGS" + elif test "$ld_exported_symbols_list" = "yes" ; then + ld64_symbols=qemu-plugins-ld64.symbols + echo "# Automatically generated by configure - do not modify" > $ld64_symbols + cat "$source_path/qemu-plugins.symbols" | grep qemu_ | sed 's/;//g' >> $ld64_symbols + LDFLAGS="-Wl,-exported_symbols_list,\$(BUILD_DIR)/$ld64_symbols $LDFLAGS" + else + error_exit \ + "If \$plugins=yes, either \$ld_dynamic_list or " \ + "\$ld_exported_symbols_list should have been set to 'yes'." + fi fi if test "$tcg_interpreter" = "yes"; then
Re: [Qemu-devel] [RFC 41/48] configure: add --enable-plugins
Quoted 85 lines · Emilio G. Cota
Quoted 16 lines · Roman Bolshakov
ld64 on macOS has similar -exported_symbols_list option. Here's the reference:
-exported_symbols_list filename
The specified filename contains a list of global symbol names that will remain as global symbols in the output file. All other global symbols will be treated as if they were marked as__private_extern__(aka visibility=hidden) and will not be global in the output file. The symbol names listed in filename must be one per line. Leading and trailing white space are not part of the symbol name. Lines starting with # are ignored, as are lines with only white space. Some wildcards (similar to shell file matching) are supported. The * matches zero or more characters. The ? matches one character. [abc] matches one character which must be an 'a', 'b', or 'c'. [a-z] matches any single lower case letter from 'a' to 'z'.Quoted 2 lines
I can try your branch if you add support of the linker flag or send required changes via GitHub.
Can you please try this branch? I added a commit with the appended.
https://github.com/cota/qemu/tree/plugin-v2You can inspect the symbols in the final binary with
readelf --dyn-syms' ornm -D'.Thanks,
Emilio
---
configure +28
-8@@ -5176,15 +5176,31 @@ int main(void) } EOF +ld_dynamic_list="no" if compile_prog "" "-Wl,--dynamic-list=$TMPTXT" ; then ld_dynamic_list="yes" -else - if test "$plugins" = "yes" ; then - error_exit \ - "Plugin support requires specifying a set of symbols that " \ - "are exported to plugins. Unfortunately your linker doesn't " \ - "support the flag (--dynamic-list) used for this purpose." - fi +fi + +######################################### +# See if -exported_symbols_list is supported by the linker + +cat > $TMPTXT <<EOF + foo +EOF + +ld_exported_symbols_list="no" +if compile_prog "" "-Wl,-exported_symbols_list,$TMPTXT" ; then + ld_exported_symbols_list="yes" +fi + +if test "$plugins" = "yes" && + test "$ld_dynamic_list" = "no" && + test "$ld_exported_symbols_list" = "no" ; then + error_exit \ + "Plugin support requires specifying a set of symbols that " \ + "are exported to plugins. Unfortunately your linker doesn't " \ + "support the flag (--dynamic-list or -exported_symbols_list) used " \ + "for this purpose." fi ########################################@@ -6827,7 +6843,18 @@ fi if test "$plugins" = "yes" ; then echo "CONFIG_PLUGINS=y" >> $config_host_mak LIBS="-ldl $LIBS" - LDFLAGS="-Wl,--dynamic-list=\$(SRC_PATH)/qemu-plugins.symbols $LDFLAGS" + if test "$ld_dynamic_list" = "yes" ; then + LDFLAGS="-Wl,--dynamic-list=\$(SRC_PATH)/qemu-plugins.symbols $LDFLAGS" + elif test "$ld_exported_symbols_list" = "yes" ; then + ld64_symbols=qemu-plugins-ld64.symbols + echo "# Automatically generated by configure - do not modify" > $ld64_symbols
configure fi · line 6860
Quoted 10 lines
configure +7
-0+ cat "$source_path/qemu-plugins.symbols" | grep qemu_ | sed 's/;//g' >> $ld64_symbols + LDFLAGS="-Wl,-exported_symbols_list,\$(BUILD_DIR)/$ld64_symbols $LDFLAGS" + else + error_exit \ + "If \$plugins=yes, either \$ld_dynamic_list or " \ + "\$ld_exported_symbols_list should have been set to 'yes'." + fi fi if test "$tcg_interpreter" = "yes"; then
Hi Emilio,
The test of -exported_symbols_list fails:
Undefined symbols for architecture x86_64:
"foo", referenced from:
-exported_symbol[s_list] command line option
(maybe you meant: _foo)
All functions on macOS are prefixed with underscore [1]:
"The name of a symbol representing a function that conforms to standard C calling conventions is the name of the function with an underscore prefix. Thus, the name of the symbol representing the function main would be _main."
So it can be fixed by prepending foo and qemu-plugins-ld64.symbols with underscore:
configure +2 -2
@@ -5185,7 +5185,7 @@ fi # See if -exported_symbols_list is supported by the linker cat > $TMPTXT <<EOF - foo + _foo EOF ld_exported_symbols_list="no"
@@ -6848,7 +6848,7 @@ if test "$plugins" = "yes" ; then elif test "$ld_exported_symbols_list" = "yes" ; then ld64_symbols=qemu-plugins-ld64.symbols echo "# Automatically generated by configure - do not modify" > $ld64_symbols - cat "$source_path/qemu-plugins.symbols" | grep qemu_ | sed 's/;//g' >> $ld64_symbols + cat "$source_path/qemu-plugins.symbols" | sed -nE 's/^([[:space:]]+)(.+);/\1_\2/p' >> $ld64_symbols LDFLAGS="-Wl,-exported_symbols_list,\$(BUILD_DIR)/$ld64_symbols $LDFLAGS" else error_exit \
--
Then if I print globally defined functions I can see it in config-temp/qemu-conf.exe:
nm -g -U config-temp/qemu-conf.exe0000000100000f50 T _foo
qemu-ga fails to link because it doesn't have symbols declared in qemu-plugins-ld64.symbols. Perhaps "-Wl,-exported_symbols_list" should be applied only to qemu-system?
Best regards,
Roman
Re: [Qemu-devel] [RFC 41/48] configure: add --enable-plugins
Quoted 6 lines · Roman Bolshakov
The test of -exported_symbols_list fails:
Undefined symbols for architecturex86_64:
"foo", referenced from:
-exported_symbol[s_list] command line option
(maybe you meant:_foo)
Quoted 7 lines
All functions on macOS are prefixed with underscore [1]:
"The name of a symbol representing a function that conforms to standard C calling conventions is the name of the function with an underscore prefix. Thus, the name of the symbol representing the function main would be_main."So it can be fixed by prepending foo and qemu-plugins-ld64.symbols with underscore:
Ah I see, thanks!
(snip)
Quoted 3 lines
qemu-ga fails to link because it doesn't have symbols declared in qemu-plugins-ld64.symbols. Perhaps "-Wl,-exported_symbols_list" should be applied only to qemu-system?
I just pushed to the same github branch the appended fixup. The idea is to only add the linker flags when the output binary links plugin.o.
Can you please test?
Thanks,
Emilio
---
commit 8f45416b79765b66e5ce0fca7db93b97bbcfcfbb
Author: Emilio G. Cota <[email protected]> Date: Wed Nov 28 12:11:23 2018 -0500
configure: ld64 fixup
And copy the file to the build dir also for !ld64.
Makefile.target +17 -1
@@ -107,7 +107,23 @@ obj-y += target/$(TARGET_BASE_ARCH)/ obj-y += disas.o obj-$(call notempty,$(TARGET_XML_FILES)) += gdbstub-xml.o -obj-$(CONFIG_PLUGINS) += plugin.o +ifdef CONFIG_PLUGINS +obj-y += plugin.o +# Abuse -libs suffix to only link with --dynamic-list/-exported_symbols_list +# when the final binary includes the plugin object. +# +# Note that simply setting LDFLAGS is not enough: we build binaries that +# never link plugin.o, and the linker might fail (at least ld64 does) +# if the symbols in the list are not in the output binary. + ifdef CONFIG_HAS_LD_DYNAMIC_LIST + plugin.o-libs := -Wl,--dynamic-list=$(BUILD_DIR)/qemu-plugins-ld.symbols + else + ifdef CONFIG_HAS_LD_EXPORTED_SYMBOLS_LIST + plugin.o-libs := \ + -Wl,-exported_symbols_list,$(BUILD_DIR)/qemu-plugins-ld64.symbols + endif + endif +endif ######################################################### # Linux user emulator target
configure +8 -4
@@ -5185,7 +5185,7 @@ fi # See if -exported_symbols_list is supported by the linker cat > $TMPTXT <<EOF - foo + _foo EOF ld_exported_symbols_list="no"
@@ -6843,13 +6843,17 @@ fi if test "$plugins" = "yes" ; then echo "CONFIG_PLUGINS=y" >> $config_host_mak LIBS="-ldl $LIBS" + # Copy the export object list to the build dir if test "$ld_dynamic_list" = "yes" ; then - LDFLAGS="-Wl,--dynamic-list=\$(SRC_PATH)/qemu-plugins.symbols $LDFLAGS" + echo "CONFIG_HAS_LD_DYNAMIC_LIST=yes" >> $config_host_mak + ld_symbols=qemu-plugins-ld.symbols + cp "$source_path/qemu-plugins.symbols" $ld_symbols elif test "$ld_exported_symbols_list" = "yes" ; then + echo "CONFIG_HAS_LD_EXPORTED_SYMBOLS_LIST=yes" >> $config_host_mak ld64_symbols=qemu-plugins-ld64.symbols echo "# Automatically generated by configure - do not modify" > $ld64_symbols - cat "$source_path/qemu-plugins.symbols" | grep qemu_ | sed 's/;//g' >> $ld64_symbols - LDFLAGS="-Wl,-exported_symbols_list,\$(BUILD_DIR)/$ld64_symbols $LDFLAGS" + grep 'qemu_' "$source_path/qemu-plugins.symbols" | sed 's/;//g' | \ + sed -E 's/^\s*(.*)/_\1/' >> $ld64_symbols else error_exit \ "If \$plugins=yes, either \$ld_dynamic_list or " \
Re: [Qemu-devel] [RFC 41/48] configure: add --enable-plugins
Quoted 80 lines · Emilio G. Cota
Quoted 3 lines · Roman Bolshakov
qemu-ga fails to link because it doesn't have symbols declared in qemu-plugins-ld64.symbols. Perhaps "-Wl,-exported_symbols_list" should be applied only to qemu-system?
I just pushed to the same github branch the appended fixup. The idea is to only add the linker flags when the output binary links plugin.o.
Can you please test?
Thanks,
Emilio
---
commit8f45416b79765b66e5ce0fca7db93b97bbcfcfbbAuthor: Emilio G. Cota <[email protected]> Date: Wed Nov 28 12:11:23 2018 -0500configure: ld64 fixup
And copy the file to the build dir also for !ld64.
Signed-off-by Emilio G. Cota <[email protected]>Makefile.target +17
-1@@ -107,7 +107,23 @@ obj-y += target/$(TARGET_BASE_ARCH)/ obj-y += disas.o obj-$(call notempty,$(TARGET_XML_FILES)) += gdbstub-xml.o -obj-$(CONFIG_PLUGINS) += plugin.o +ifdef CONFIG_PLUGINS +obj-y += plugin.o +# Abuse -libs suffix to only link with --dynamic-list/-exported_symbols_list +# when the final binary includes the plugin object. +# +# Note that simply setting LDFLAGS is not enough: we build binaries that +# never link plugin.o, and the linker might fail (at least ld64 does) +# if the symbols in the list are not in the output binary. + ifdef CONFIG_HAS_LD_DYNAMIC_LIST + plugin.o-libs := -Wl,--dynamic-list=$(BUILD_DIR)/qemu-plugins-ld.symbols + else + ifdef CONFIG_HAS_LD_EXPORTED_SYMBOLS_LIST + plugin.o-libs := \ + -Wl,-exported_symbols_list,$(BUILD_DIR)/qemu-plugins-ld64.symbols + endif + endif +endif ######################################################### # Linux user emulator targetconfigure +5
-2@@ -5185,7 +5185,7 @@ fi # See if -exported_symbols_list is supported by the linker cat > $TMPTXT <<EOF - foo + _foo EOF ld_exported_symbols_list="no"@@ -6843,13 +6843,17 @@ fi if test "$plugins" = "yes" ; then echo "CONFIG_PLUGINS=y" >> $config_host_mak LIBS="-ldl $LIBS" + # Copy the export object list to the build dir if test "$ld_dynamic_list" = "yes" ; then - LDFLAGS="-Wl,--dynamic-list=\$(SRC_PATH)/qemu-plugins.symbols $LDFLAGS" + echo "CONFIG_HAS_LD_DYNAMIC_LIST=yes" >> $config_host_mak + ld_symbols=qemu-plugins-ld.symbols + cp "$source_path/qemu-plugins.symbols" $ld_symbols elif test "$ld_exported_symbols_list" = "yes" ; then
configure fi · line 6859
Quoted 10 lines
configure +3
-2+ echo "CONFIG_HAS_LD_EXPORTED_SYMBOLS_LIST=yes" >> $config_host_mak ld64_symbols=qemu-plugins-ld64.symbols echo "# Automatically generated by configure - do not modify" > $ld64_symbols - cat "$source_path/qemu-plugins.symbols" | grep qemu_ | sed 's/;//g' >> $ld64_symbols - LDFLAGS="-Wl,-exported_symbols_list,\$(BUILD_DIR)/$ld64_symbols $LDFLAGS" + grep 'qemu_' "$source_path/qemu-plugins.symbols" | sed 's/;//g' | \ + sed -E 's/^\s*(.*)/_\1/' >> $ld64_symbols else error_exit \ "If \$plugins=yes, either \$ld_dynamic_list or " \
Hi Emilio,
I think there's an issue with "\s" character class, it's not recognized by macOS sed and I'm getting incorrect lines in qemu-plugins-ld64.symbols:
_ qemu_xxx
_ qemu_xyz
After I replaced "\s" with "[[:space:]]", linking proceeds further, but doesn't succeed because of an unresolved reference for qemu-system cris, lm32, m68k, microblaze, microblazeel, moxie, nios2, or1k, riscv32, riscv64, sparc, unicore32, tricore, xtensa, xtensaeb:
Undefined symbols for architecture x86_64:
"_pci_register_bar", referenced from:
_plugin_chan_realize in plugin-chan.o
It probably has nothing to do with macOS per-se and shouldn't link on Linux as well. If I disable the aforementioned targets the build succeeds and I can see the symbols from qemu-plugins-ld64.symbols in compiled qemu-system binaries.
Best regards,
Roman
Re: [Qemu-devel] [RFC 41/48] configure: add --enable-plugins
Quoted 9 lines · Roman Bolshakov
Hi Emilio,
I think there's an issue with "\s" character class, it's not recognized by macOS sed and I'm getting incorrect lines in qemu-plugins-ld64.symbols:
_qemu_xxx
_qemu_xyzAfter I replaced "\s" with "[[:space:]]", linking proceeds further
Nice, thanks. Will update.
Quoted 4 lines
, but doesn't succeed because of an unresolved reference for qemu-system cris, lm32, m68k, microblaze, microblazeel, moxie, nios2, or1k, riscv32, riscv64, sparc, unicore32, tricore, xtensa, xtensaeb:
Quoted 8 lines
Undefined symbols for architecture
x86_64:
"_pci_register_bar", referenced from:
_plugin_chan_realizein plugin-chan.oIt probably has nothing to do with macOS per-se and shouldn't link on Linux as well. If I disable the aforementioned targets the build succeeds and I can see the symbols from qemu-plugins-ld64.symbols in compiled qemu-system binaries.
Yes, that's because plugin-chan should only be built if the guest has PCI support. Will fix.
Thanks,
Emilio
Re: [Qemu-devel] [RFC 41/48] configure: add --enable-plugins
Quoted 21 lines · Emilio G. Cota
Quoted 9 lines · Roman Bolshakov
Hi Emilio,
I think there's an issue with "\s" character class, it's not recognized by macOS sed and I'm getting incorrect lines in qemu-plugins-ld64.symbols:
_qemu_xxx
_qemu_xyzAfter I replaced "\s" with "[[:space:]]", linking proceeds further
Nice, thanks. Will update.
Quoted 8 lines
, but doesn't succeed because of an unresolved reference for qemu-system cris, lm32, m68k, microblaze, microblazeel, moxie, nios2, or1k, riscv32, riscv64, sparc, unicore32, tricore, xtensa, xtensaeb:
Undefined symbols for architecture
x86_64:
"_pci_register_bar", referenced from:
_plugin_chan_realizein plugin-chan.o
Quoted 7 lines
Quoted 4 lines
It probably has nothing to do with macOS per-se and shouldn't link on Linux as well. If I disable the aforementioned targets the build succeeds and I can see the symbols from qemu-plugins-ld64.symbols in compiled qemu-system binaries.
Yes, that's because plugin-chan should only be built if the guest has PCI support. Will fix.
Pushed the fixes to the github branch. Hope it works for you now!
Thanks,
Emilio
Re: [Qemu-devel] [RFC 41/48] configure: add --enable-plugins
Quoted 27 lines · Emilio G. Cota
Quoted 26 lines · Emilio G. Cota
Quoted 9 lines · Roman Bolshakov
Hi Emilio,
I think there's an issue with "\s" character class, it's not recognized by macOS sed and I'm getting incorrect lines in qemu-plugins-ld64.symbols:
_qemu_xxx
_qemu_xyzAfter I replaced "\s" with "[[:space:]]", linking proceeds further
Nice, thanks. Will update.
Quoted 12 lines
, but doesn't succeed because of an unresolved reference for qemu-system cris, lm32, m68k, microblaze, microblazeel, moxie, nios2, or1k, riscv32, riscv64, sparc, unicore32, tricore, xtensa, xtensaeb:
Undefined symbols for architecture
x86_64:
"_pci_register_bar", referenced from:
_plugin_chan_realizein plugin-chan.oIt probably has nothing to do with macOS per-se and shouldn't link on Linux as well. If I disable the aforementioned targets the build succeeds and I can see the symbols from qemu-plugins-ld64.symbols in compiled qemu-system binaries.
Quoted 5 lines
Quoted 2 lines
Yes, that's because plugin-chan should only be built if the guest has PCI support. Will fix.
Pushed the fixes to the github branch. Hope it works for you now!
Thank you Emilio,
the build succeded with the set of declared symbols exposed in qemu-system.
I've just noticed qemu-plugins-ld64.symbols should be added to .gitignore.
Best regards,
Roman
RFC 42/48 vl: support -plugin option
In reply to Emilio G. CotaFrom: Lluís Vilanova <[email protected]>
--- vl.c | 11 +++++++++++ qemu-options.hx | 17 +++++++++++++++++ 2 files changed, 28 insertions(+)
vl.c +11 -0
@@ -111,6 +111,7 @@ int main(int argc, char **argv)#include "trace-root.h" #include "trace/control.h" +#include "qemu/plugin.h" #include "qemu/queue.h" #include "sysemu/arch_init.h"
@@ -2962,6 +2963,7 @@ int main(int argc, char **argv, char **envp) } BlockdevOptions_queue; QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue =QSIMPLEQ_HEAD_INITIALIZER (bdo_queue); +struct qemu_plugin_list plugin_list =QTAILQ_HEAD_INITIALIZER (plugin_list);module_call_init (MODULE_INIT_TRACE );
@@ -2990,6 +2992,7 @@ int main(int argc, char **argv, char **envp)qemu_add_opts (&qemu_global_opts);qemu_add_opts (&qemu_mon_opts);qemu_add_opts (&qemu_trace_opts); +qemu_plugin_add_opts ();qemu_add_opts (&qemu_option_rom_opts);qemu_add_opts (&qemu_machine_opts);qemu_add_opts (&qemu_accel_opts);
@@ -3806,6 +3809,9 @@ int main(int argc, char **argv, char **envp)g_free (trace_file); trace_file =trace_opt_parse (optarg);break ; +case QEMU_OPTION_plugin: +qemu_plugin_opt_parse (optarg, &plugin_list); +break ;case QEMU_OPTION_readconfig: {int ret =qemu_read_config_file (optarg);
@@ -4107,6 +4113,11 @@ int main(int argc, char **argv, char **envp) machine_class->default_machine_opts, 0); } +/* process plugin before CPUs are created, but once -smp has been parsed */ +if (qemu_plugin_load_list (&plugin_list)) { +exit (1 ); + } +qemu_opts_foreach (qemu_find_opts ("device" ), default_driver_check,NULL ,NULL ); qemu_opts_foreach(qemu_find_opts("global"),
qemu-options.hx +17 -0
@@ -3838,6 +3838,23 @@ HXCOMM HX does not support conditional compilation of text. @findex -trace @include qemu-option-trace.texi ETEXI +DEF("plugin", HAS_ARG, QEMU_OPTION_plugin, + "-plugin [file=]<file>[,arg=<string>]\n" + " load a plugin\n", + QEMU_ARCH_ALL) +STEXI +@item -plugin file=@var{file}[,arg=@var{string}] +@findex -plugin + +Load a plugin. + +@table @option +@item file=@var{file} +Load the given plugin from a shared library file. +@item arg=@var{string} +Argument string passed to the plugin. (Can be given multiple times.) +@end table +ETEXI HXCOMM Internal use DEF("qtest", HAS_ARG, QEMU_OPTION_qtest, "", QEMU_ARCH_ALL)
Signature · 1 line
2.17.1
RFC 43/48 linux-user: support -plugin option
In reply to Emilio G. CotaFrom: Lluís Vilanova <[email protected]>
--- linux-user/main.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
linux-user/main.c +18 -0
@@ -28,6 +28,7 @@#include "qemu/config-file.h" #include "qemu/cutils.h" #include "qemu/help_option.h" +#include "qemu/plugin.h" #include "cpu.h" #include "exec/exec-all.h" #include "tcg.h"
@@ -385,6 +386,15 @@ static void handle_arg_trace(const char *arg) trace_file =trace_opt_parse (arg); } +static struct qemu_plugin_list plugins =QTAILQ_HEAD_INITIALIZER (plugins); + +#ifdef CONFIG_PLUGINS +static void handle_arg_plugin (const char *arg) +{ +qemu_plugin_opt_parse (arg, &plugins); +} +#endif +struct qemu_argument {const char *argv;const char *env;
@@ -436,6 +446,10 @@ static const struct qemu_argument arg_table[] = { "", "Seed for pseudo-random number generator"}, {"trace" ,"QEMU_TRACE" , true, handle_arg_trace, "", "[[enable=]<pattern>][,events=<file>][,file=<file>]"}, +#ifdef CONFIG_PLUGINS + {"plugin", "QEMU_PLUGIN", true, handle_arg_plugin, + "", "[file=]<file>[,arg=<string>]"}, +#endif {"version", "QEMU_VERSION", false, handle_arg_version, "", "display version information and exit"}, {NULL, NULL, false, NULL, NULL, NULL}
@@ -627,6 +641,7 @@ int main(int argc, char **argv, char **envp)srand (time (NULL ));qemu_add_opts (&qemu_trace_opts); +qemu_plugin_add_opts (); optind =parse_args (argc, argv);
@@ -634,6 +649,9 @@ int main(int argc, char **argv, char **envp)exit (1 ); }trace_init_file (trace_file); +if (qemu_plugin_load_list (&plugins)) { +exit (1 ); + }/* Zero out regs */ memset (regs,0 ,sizeof (struct target_pt_regs ));
Signature · 1 line
2.17.1
RFC 44/48 cpus: lockstep execution support
In reply to Emilio G. Cota--- include/qom/cpu.h | 27 +++++++++++ cpus.c | 113 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 139 insertions(+), 1 deletion(-)
include/qom/cpu.h +27 -0
@@ -279,6 +279,12 @@ typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data);struct qemu_work_item ; +enum cpu_lockstep { +CPU_LOCKSTEP_RUN , +CPU_LOCKSTEP_STOP_REQUEST , +CPU_LOCKSTEP_WAIT , +}; +#define CPU_UNSET_NUMA_NODE_ID -1#define CPU_TRACE_DSTATE_MAX_EVENTS 32
@@ -364,6 +370,7 @@ struct CPUState {QemuCond halt_cond; QSIMPLEQ_HEAD(, qemu_work_item) work_list;uint32_t halted; +enum cpu_lockstep lockstep;bool created;bool stop;bool stopped;
@@ -1019,6 +1026,26 @@ static inline void cpu_interrupt(CPUState *cpu, int mask) cpu_interrupt_handler(cpu, mask); } +/** +* cpu_lockstep_enable - Enable execution of CPUs in lockstep +* +* Note: this feature is MTTCG-only. +* Lockstep execution allows CPUs to partition their execution into windows +* whose start is synchronized with that of other CPUs. This can have many +* uses, e.g. limiting execution skew in the guest. +* +* See also: cpu_lockstep_request_stop() +*/ +void cpu_lockstep_enable (void ); + +/** +* cpu_lockstep_request_stop - Finish the CPU's execution window +* @cpu: the CPU of interest +* +* See also: cpu_lockstep_enable() +*/ +void cpu_lockstep_request_stop (CPUState *cpu); +#else /* USER_ONLY */ void cpu_interrupt (CPUState *cpu,int mask);
cpus.c +112 -1
155 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 44/48] cpus: lockstep execution support
Quoted 2 lines · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>---
<snip>
Quoted 6 lines
void
cpu_interrupt(CPUState *cpu, int mask);cpus.c +0
-0
<snip>
Quoted 4 lines
cpus.c +4
-0+ +static void cpu_lockstep_init (CPUState *cpu) +{ +if (!lockstep_enabled) {
cpus.c static void qemu_wait_io_event_common(CPUState *cpu) · line 1388
Quoted 10 lines
cpus.c +10
-0+return ; + } +qemu_mutex_lock (&lockstep_lock); +/* +* HACK: avoid racing with a wakeup, which would miss the addition +* of this CPU; just wait until no wakeup is ongoing. +*/ +while (unlikely (lockstep_ongoing_wakeup)) { +qemu_mutex_unlock (&lockstep_lock); +sched_yield ();
This breaks Windows builds. I suspect if we do want to this sort of functionality we'll need to expose a utility function in oslib-posix/oslib-win32
--
Alex Bennée
Re: [Qemu-devel] [RFC 44/48] cpus: lockstep execution support
Quoted 23 lines · Alex Bennée
Quoted 2 lines · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>---
<snip>
Quoted 6 lines
void
cpu_interrupt(CPUState *cpu, int mask);cpus.c +0
-0<snip>
Quoted 10 lines
cpus.c +10
-0+ +static void cpu_lockstep_init (CPUState *cpu) +{ +if (!lockstep_enabled) { +return ; + } +qemu_mutex_lock (&lockstep_lock); +/* +* HACK: avoid racing with a wakeup, which would miss the addition +* of this CPU; just wait until no wakeup is ongoing.
Quoted 8 lines
cpus.c
Quoted 4 lines
cpus.c +4
-0+*/ +while (unlikely (lockstep_ongoing_wakeup)) { +qemu_mutex_unlock (&lockstep_lock); +sched_yield ();This breaks Windows builds. I suspect if we do want to this sort of functionality we'll need to expose a utility function in oslib-posix/oslib-win32
This was just a quick hack to avoid adding a condvar to the wake-up fast path. Fixed in v2 with the below, which only calls cond_broadcast if needed (i.e. very rarely).
Thanks,
Emilio
cpus.c +17 -8
@@ -84,8 +84,10 @@ static unsigned int throttle_percentage;static bool lockstep_enabled;static bool lockstep_ongoing_wakeup;static QemuMutex lockstep_lock; +static QemuCond lockstep_cond;static int n_lockstep_running_cpus;static int n_lockstep_cpus; +static int n_lockstep_initializing_cpus;static CPUState **lockstep_cpus;#define CPU_THROTTLE_PCT_MIN 1
@@ -1260,6 +1262,7 @@ void qemu_init_cpu_loop(void)qemu_init_sigbus ();qemu_mutex_init (&qemu_global_mutex);qemu_mutex_init (&lockstep_lock); +qemu_cond_init (&lockstep_cond);qemu_thread_get_self (&io_thread); }
@@ -1369,6 +1372,13 @@ static void lockstep_check_stop(CPUState *cpu)cpu_mutex_lock (cpu);qemu_mutex_lock (&lockstep_lock); lockstep_ongoing_wakeup = false; +/* +* If newly spawned CPUs are waiting to be added to the wait list, +* let them do so now. +*/ +if (unlikely (n_lockstep_initializing_cpus)) { +qemu_cond_broadcast (&lockstep_cond); + } }qemu_mutex_unlock (&lockstep_lock); }
@@ -1379,16 +1389,15 @@ static void cpu_lockstep_init(CPUState *cpu)if (!lockstep_enabled) {return ; } +qemu_mutex_lock (&lockstep_lock); -/* -* HACK: avoid racing with a wakeup, which would miss the addition -* of this CPU; just wait until no wakeup is ongoing. -*/ -while (unlikely (lockstep_ongoing_wakeup)) { -qemu_mutex_unlock (&lockstep_lock); -sched_yield (); -qemu_mutex_lock (&lockstep_lock); +/* avoid racing with a wakeup, which would miss the addition of this CPU */ + n_lockstep_initializing_cpus++; +while (lockstep_ongoing_wakeup) { +qemu_cond_wait (&lockstep_cond, &lockstep_lock); } + n_lockstep_initializing_cpus--; + lockstep_cpus =g_realloc (lockstep_cpus, (n_lockstep_cpus +1 ) *sizeof (CPUState *)); lockstep_cpus[n_lockstep_cpus++] = cpu;
RFC 45/48 plugin: lockstep execution support
In reply to Emilio G. Cota--- include/qemu/plugin-api.h | 7 +++++++ include/qemu/plugin.h | 5 +++++ cpus.c | 1 + plugin.c | 35 +++++++++++++++++++++++++++++++++++ qemu-plugins.symbols | 3 +++ 5 files changed, 51 insertions(+)
include/qemu/plugin-api.h +7 -0
@@ -227,6 +227,13 @@ typedef int64_t (*qemu_plugin_clock_func_t)(void);bool qemu_plugin_register_virtual_clock (qemu_plugin_id_t id,qemu_plugin_clock_func_t clock); +void qemu_plugin_enable_lockstep_execution (void ); + +void qemu_plugin_register_lockstep_cb (qemu_plugin_id_t id, +qemu_plugin_simple_cb_t cb); + +void qemu_plugin_end_time_slice (void ); +/* returns -1 in user-mode */ int qemu_plugin_n_vcpus (void );
include/qemu/plugin.h +5 -0
@@ -58,6 +58,7 @@ enum qemu_plugin_event {QEMU_PLUGIN_EV_VCPU_RESUME ,QEMU_PLUGIN_EV_VCPU_SYSCALL ,QEMU_PLUGIN_EV_VCPU_SYSCALL_RET , +QEMU_PLUGIN_EV_LOCKSTEP ,QEMU_PLUGIN_EV_FLUSH ,QEMU_PLUGIN_EV_ATEXIT , QEMU_PLUGIN_EV_MAX,
@@ -194,6 +195,7 @@ void qemu_plugin_atexit_cb(void);void qemu_plugin_add_dyn_cb_arr (struct qemu_plugin_dyn_cb_arr *arr);int64_t plugin_get_clock (void ); +void plugin_lockstep_cb (void );#else /* !CONFIG_PLUGINS */
@@ -237,6 +239,9 @@ static inlinevoid qemu_plugin_add_dyn_cb_arr (struct qemu_plugin_dyn_cb_arr *arr) { } +static inline void plugin_lockstep_cb (void ) +{ } +int64_t plugin_get_clock (void );#endif /* !CONFIG_PLUGINS */
cpus.c +1 -0
@@ -1359,6 +1359,7 @@ static void lockstep_check_stop(CPUState *cpu)/* wake up all waiting cpus */ lockstep_ongoing_wakeup = true; n_lockstep_running_cpus = n_lockstep_cpus; +plugin_lockstep_cb ();qemu_mutex_unlock (&lockstep_lock);cpu_mutex_unlock (cpu);for (i =0 ; i < n_lockstep_cpus; i++) {
plugin.c +35 -0
@@ -472,6 +472,7 @@ static void plugin_cb__simple(enum qemu_plugin_event ev)switch (ev) {case QEMU_PLUGIN_EV_FLUSH : +case QEMU_PLUGIN_EV_LOCKSTEP : QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {qemu_plugin_simple_cb_t func = cb->f.simple;
@@ -1043,6 +1044,40 @@ int64_t plugin_get_clock(void) }#endif +/* +* We manage the CPU state changes; the plugin will control the length of the +* execution windows. +*/ +void qemu_plugin_enable_lockstep_execution (void ) +{ +#ifdef CONFIG_USER_ONLY +abort (); +#else +cpu_lockstep_enable (); +#endif +} + +void qemu_plugin_end_time_slice (void ) +{ +#ifdef CONFIG_USER_ONLY +abort (); +#else +g_assert (current_cpu); +cpu_lockstep_request_stop (current_cpu); +#endif +} + +void qemu_plugin_register_lockstep_cb (qemu_plugin_id_t id, +qemu_plugin_simple_cb_t cb) +{ +plugin_register_cb (id,QEMU_PLUGIN_EV_LOCKSTEP , cb); +} + +void plugin_lockstep_cb (void ) +{ +plugin_cb__simple (QEMU_PLUGIN_EV_LOCKSTEP ); +} +static void __attribute__((__constructor__))plugin_init (void ) {int i;
qemu-plugins.symbols +3 -0
@@ -1,5 +1,8 @@ { qemu_plugin_uninstall; + qemu_plugin_enable_lockstep_execution; + qemu_plugin_end_time_slice; + qemu_plugin_register_lockstep_cb; qemu_plugin_register_vcpu_init_cb; qemu_plugin_register_vcpu_exit_cb; qemu_plugin_register_vcpu_idle_cb;
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 45/48] plugin: lockstep execution support
Quoted 1 line · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>
There are no users of this for now so I don't think this qualifies for a first cut of the plugin API. Is the lockstep support only their for plugins? Is there any practical use that isn't handled by non-MTTCG round-robin and icount type scenarios?
Quoted 126 lines
--- include/qemu/plugin-api.h | 7 +++++++ include/qemu/plugin.h | 5 +++++ cpus.c | 1 + plugin.c | 35 +++++++++++++++++++++++++++++++++++ qemu-plugins.symbols | 3 +++ 5 files changed, 51 insertions(+)include/qemu/plugin-api.h +7
-0@@ -227,6 +227,13 @@ typedef int64_t (*qemu_plugin_clock_func_t)(void);bool qemu_plugin_register_virtual_clock (qemu_plugin_id_t id,qemu_plugin_clock_func_t clock); +void qemu_plugin_enable_lockstep_execution (void ); + +void qemu_plugin_register_lockstep_cb (qemu_plugin_id_t id, +qemu_plugin_simple_cb_t cb); + +void qemu_plugin_end_time_slice (void ); +/* returns -1 in user-mode */ int qemu_plugin_n_vcpus (void );include/qemu/plugin.h +5
-0@@ -58,6 +58,7 @@ enum qemu_plugin_event {QEMU_PLUGIN_EV_VCPU_RESUME ,QEMU_PLUGIN_EV_VCPU_SYSCALL ,QEMU_PLUGIN_EV_VCPU_SYSCALL_RET , +QEMU_PLUGIN_EV_LOCKSTEP ,QEMU_PLUGIN_EV_FLUSH ,QEMU_PLUGIN_EV_ATEXIT , QEMU_PLUGIN_EV_MAX,@@ -194,6 +195,7 @@ void qemu_plugin_atexit_cb(void);void qemu_plugin_add_dyn_cb_arr (struct qemu_plugin_dyn_cb_arr *arr);int64_t plugin_get_clock (void ); +void plugin_lockstep_cb (void );#else /* !CONFIG_PLUGINS */ @@ -237,6 +239,9 @@ static inlinevoid qemu_plugin_add_dyn_cb_arr (struct qemu_plugin_dyn_cb_arr *arr) { } +static inline void plugin_lockstep_cb (void ) +{ } +int64_t plugin_get_clock (void );#endif /* !CONFIG_PLUGINS */ cpus.c +1
-0@@ -1359,6 +1359,7 @@ static void lockstep_check_stop(CPUState *cpu)/* wake up all waiting cpus */ lockstep_ongoing_wakeup = true; n_lockstep_running_cpus = n_lockstep_cpus; +plugin_lockstep_cb ();qemu_mutex_unlock (&lockstep_lock);cpu_mutex_unlock (cpu);for (i =0 ; i < n_lockstep_cpus; i++) {plugin.c +35
-0@@ -472,6 +472,7 @@ static void plugin_cb__simple(enum qemu_plugin_event ev)switch (ev) {case QEMU_PLUGIN_EV_FLUSH : +case QEMU_PLUGIN_EV_LOCKSTEP : QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {qemu_plugin_simple_cb_t func = cb->f.simple;@@ -1043,6 +1044,40 @@ int64_t plugin_get_clock(void) }#endif +/* +* We manage the CPU state changes; the plugin will control the length of the +* execution windows. +*/ +void qemu_plugin_enable_lockstep_execution (void ) +{ +#ifdef CONFIG_USER_ONLY +abort (); +#else +cpu_lockstep_enable (); +#endif +} + +void qemu_plugin_end_time_slice (void ) +{ +#ifdef CONFIG_USER_ONLY +abort (); +#else +g_assert (current_cpu); +cpu_lockstep_request_stop (current_cpu); +#endif +} + +void qemu_plugin_register_lockstep_cb (qemu_plugin_id_t id, +qemu_plugin_simple_cb_t cb) +{ +plugin_register_cb (id,QEMU_PLUGIN_EV_LOCKSTEP , cb); +} + +void plugin_lockstep_cb (void ) +{ +plugin_cb__simple (QEMU_PLUGIN_EV_LOCKSTEP ); +} +static void __attribute__((__constructor__))plugin_init (void ) {int i;qemu-plugins.symbols +0
-0
qemu-plugins.symbols line 8
Quoted 8 lines
qemu-plugins.symbols +3
-0@@ -1,5 +1,8 @@ { qemu_plugin_uninstall; + qemu_plugin_enable_lockstep_execution; + qemu_plugin_end_time_slice; + qemu_plugin_register_lockstep_cb; qemu_plugin_register_vcpu_init_cb; qemu_plugin_register_vcpu_exit_cb; qemu_plugin_register_vcpu_idle_cb;
--
Alex Bennée
Re: [Qemu-devel] [RFC 45/48] plugin: lockstep execution support
Quoted 7 lines · Alex Bennée
Quoted 1 line · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>There are no users of this for now so I don't think this qualifies for a first cut of the plugin API.
Fair enough. It was more as an example that plugins are not just for instrumentation purposes. Another example of this is the use of plugins to control the guest's clock -- for instance, there was this series
https://lists.gnu.org/archive/html/qemu-devel/2017-02/msg03028.html
that implemented a module to control the guest's clock over sockets. Instead, a plugin can just be loaded to take control, and the plugin is free to interact with the outer world in whatever way it wants (sockets, pipes, etc.). So having a plugin infrastructure can make adding those features much easier to implement (FWIW, that patch never landed on master).
Quoted 3 lines
Is the lockstep support only their for
plugins? Is there any practical use that isn't handled by non-MTTCG round-robin and icount type scenarios?
This is a compromise between icount and MTTCG by limiting the latter's skew among vCPUs. So you don't get full determinism, but get closer to it without giving up parallelism.
I think this feature could be added without plugins, by for instance defaulting to an "MTTCG icount-like" behaviour. That is, the time window of each CPU would expire after N instructions executed.
But I didn't have a use case for that; my use case is to control the time windows from the plugins, since in my simulator the plugin controls the guest clock, and instructions have different latencies.
So yes, feel free to skip this patch!
Thanks,
Emilio
RFC 46/48 plugin: add plugin-chan PCI device
In reply to Emilio G. CotaThis will allow communication between guest and plugins.
--- Makefile.target | 2 + include/hw/pci/pci.h | 2 + include/qemu/plugin.h | 1 + hw/plugin/plugin-chan.c | 136 ++++++++++++++++++++++++++++++++++++++++ plugin.c | 4 ++ 5 files changed, 145 insertions(+) create mode 100644 hw/plugin/plugin-chan.c
Makefile.target +2 -0
@@ -156,6 +156,8 @@ else obj-y += hw/$(TARGET_BASE_ARCH)/ endif +obj-$(CONFIG_PLUGINS) += hw/plugin/plugin-chan.o + GENERATED_FILES += hmp-commands.h hmp-commands-info.h endif # CONFIG_SOFTMMU
include/hw/pci/pci.h +2 -0
@@ -86,6 +86,8 @@ extern bool pci_available;#define PCI_DEVICE_ID_VIRTIO_9P 0x1009#define PCI_DEVICE_ID_VIRTIO_VSOCK 0x1012 +#define PCI_DEVICE_ID_QEMU_PLUGIN_CHAN 0x10f0 +#define PCI_VENDOR_ID_REDHAT 0x1b36#define PCI_DEVICE_ID_REDHAT_BRIDGE 0x0001#define PCI_DEVICE_ID_REDHAT_SERIAL 0x0002
include/qemu/plugin.h +1 -0
@@ -196,6 +196,7 @@ void qemu_plugin_atexit_cb(void);void qemu_plugin_add_dyn_cb_arr (struct qemu_plugin_dyn_cb_arr *arr);int64_t plugin_get_clock (void );void plugin_lockstep_cb (void ); +void plugin_chan_xmit (uint32_t cmd,const void *data,size_t size);#else /* !CONFIG_PLUGINS */
hw/plugin/plugin-chan.c new file +136 -0
136 lines not on this page · raw message
plugin.c +4 -0
@@ -1078,6 +1078,10 @@ void plugin_lockstep_cb(void)plugin_cb__simple (QEMU_PLUGIN_EV_LOCKSTEP ); } +void plugin_chan_xmit (uint32_t cmd,const void *data,size_t size) +{ +} +static void __attribute__((__constructor__))plugin_init (void ) {int i;
Signature · 1 line
2.17.1
RFC 47/48 plugin: support guest hooks
In reply to Emilio G. CotaThese "hooks" are callbacks from the guest to plugins. This is useful when we need guest-host communication, for instance to signal the beginning/end of a certain "region of interest" in the guest program. Simulators typically would use "magic" instructions for this, but that is painful to maintain across ISAs. Instead, we use plugin-chan PCI device through which we can relay guest messages to the host.
--- include/qemu/plugin-api.h | 6 ++++++ include/qemu/plugin.h | 2 ++ plugin.c | 13 +++++++++++++ qemu-plugins.symbols | 1 + 4 files changed, 22 insertions(+)
include/qemu/plugin-api.h +6 -0
@@ -234,6 +234,12 @@ void qemu_plugin_register_lockstep_cb(qemu_plugin_id_t id,void qemu_plugin_end_time_slice (void ); +typedef void (*qemu_plugin_hook_cb_t )(uint32_t cmd,const void *data, +size_t size); + +void qemu_plugin_register_hook_cb (qemu_plugin_id_t id, +qemu_plugin_hook_cb_t cb); +/* returns -1 in user-mode */ int qemu_plugin_n_vcpus (void );
include/qemu/plugin.h +2 -0
@@ -61,12 +61,14 @@ enum qemu_plugin_event {QEMU_PLUGIN_EV_LOCKSTEP ,QEMU_PLUGIN_EV_FLUSH ,QEMU_PLUGIN_EV_ATEXIT , +QEMU_PLUGIN_EV_HOOK , QEMU_PLUGIN_EV_MAX, };union qemu_plugin_cb_sig {qemu_plugin_simple_cb_t simple;qemu_plugin_udata_cb_t udata; +qemu_plugin_hook_cb_t hook;qemu_plugin_vcpu_simple_cb_t vcpu_simple;qemu_plugin_vcpu_udata_cb_t vcpu_udata;qemu_plugin_vcpu_tb_trans_cb_t vcpu_tb_trans;
plugin.c +13 -0
@@ -1078,8 +1078,21 @@ void plugin_lockstep_cb(void)plugin_cb__simple (QEMU_PLUGIN_EV_LOCKSTEP ); } +void qemu_plugin_register_hook_cb (qemu_plugin_id_t id,qemu_plugin_hook_cb_t cb) +{ +plugin_register_cb (id,QEMU_PLUGIN_EV_HOOK , cb); +} +void plugin_chan_xmit (uint32_t cmd,const void *data,size_t size) { +struct qemu_plugin_cb *cb, *next; +enum qemu_plugin_event ev =QEMU_PLUGIN_EV_HOOK ; + + QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) { +qemu_plugin_hook_cb_t func = cb->f.hook; + +func (cmd, data, size); + } }static void __attribute__((__constructor__))plugin_init (void )
qemu-plugins.symbols +1 -0
@@ -31,6 +31,7 @@ qemu_plugin_mem_is_big_endian; qemu_plugin_mem_is_store; qemu_plugin_vcpu_for_each; + qemu_plugin_register_hook_cb; qemu_plugin_n_vcpus; qemu_plugin_n_max_vcpus; };
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 47/48] plugin: support guest hooks
Quoted 7 lines · Emilio G. Cota
These "hooks" are callbacks from the guest to plugins. This is useful when we need guest-host communication, for instance to signal the beginning/end of a certain "region of interest" in the guest program. Simulators typically would use "magic" instructions for this, but that is painful to maintain across ISAs. Instead, we use plugin-chan PCI device through which we can relay guest messages to the host.
I suspect the solution for this is either a virtio socket that may (or may not) be located on a PCI bus. However definitely out of scope for a first cut of the plugin API, especially without an example usage.
Android has a similar guest-aware channel although the host side was outside of QEMU and QEMU's part was just to expose a socket on the host side that adb could talk to.
Quoted 76 lines
Signed-off-by Emilio G. Cota <[email protected]>--- include/qemu/plugin-api.h | 6 ++++++ include/qemu/plugin.h | 2 ++ plugin.c | 13 +++++++++++++ qemu-plugins.symbols | 1 + 4 files changed, 22 insertions(+)include/qemu/plugin-api.h +6
-0@@ -234,6 +234,12 @@ void qemu_plugin_register_lockstep_cb(qemu_plugin_id_t id,void qemu_plugin_end_time_slice (void ); +typedef void (*qemu_plugin_hook_cb_t )(uint32_t cmd,const void *data, +size_t size); + +void qemu_plugin_register_hook_cb (qemu_plugin_id_t id, +qemu_plugin_hook_cb_t cb); +/* returns -1 in user-mode */ int qemu_plugin_n_vcpus (void );include/qemu/plugin.h +2
-0@@ -61,12 +61,14 @@ enum qemu_plugin_event {QEMU_PLUGIN_EV_LOCKSTEP ,QEMU_PLUGIN_EV_FLUSH ,QEMU_PLUGIN_EV_ATEXIT , +QEMU_PLUGIN_EV_HOOK , QEMU_PLUGIN_EV_MAX, };union qemu_plugin_cb_sig {qemu_plugin_simple_cb_t simple;qemu_plugin_udata_cb_t udata; +qemu_plugin_hook_cb_t hook;qemu_plugin_vcpu_simple_cb_t vcpu_simple;qemu_plugin_vcpu_udata_cb_t vcpu_udata;qemu_plugin_vcpu_tb_trans_cb_t vcpu_tb_trans;plugin.c +13
-0@@ -1078,8 +1078,21 @@ void plugin_lockstep_cb(void)plugin_cb__simple (QEMU_PLUGIN_EV_LOCKSTEP ); } +void qemu_plugin_register_hook_cb (qemu_plugin_id_t id,qemu_plugin_hook_cb_t cb) +{ +plugin_register_cb (id,QEMU_PLUGIN_EV_HOOK , cb); +} +void plugin_chan_xmit (uint32_t cmd,const void *data,size_t size) { +struct qemu_plugin_cb *cb, *next; +enum qemu_plugin_event ev =QEMU_PLUGIN_EV_HOOK ; + + QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) { +qemu_plugin_hook_cb_t func = cb->f.hook; + +func (cmd, data, size); + } }static void __attribute__((__constructor__))plugin_init (void )qemu-plugins.symbols +0
-0
qemu-plugins.symbols line 37
Quoted 7 lines
qemu-plugins.symbols +1
-0@@ -31,6 +31,7 @@ qemu_plugin_mem_is_big_endian; qemu_plugin_mem_is_store; qemu_plugin_vcpu_for_each; + qemu_plugin_register_hook_cb; qemu_plugin_n_vcpus; qemu_plugin_n_max_vcpus; };
--
Alex Bennée
RFC 48/48 plugin: add a couple of very simple examples
In reply to Emilio G. Cota--- plugin-examples/bbcount_avgsize_racy.c | 50 ++++++++++++++++++++++ plugin-examples/mem_count_racy_both.c | 58 ++++++++++++++++++++++++++ plugin-examples/Makefile | 31 ++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 plugin-examples/bbcount_avgsize_racy.c create mode 100644 plugin-examples/mem_count_racy_both.c create mode 100644 plugin-examples/Makefile
plugin-examples/bbcount_avgsize_racy.c new file +50 -0
@@ -0,0 +1,50 @@ +#include <inttypes.h> +#include <assert.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <stdio.h> + +#include <qemu-plugin.h> + +static uint64_t bb_count; +static uint64_t insn_count; +const char *filename; +static int stdout_fd; + +static void plugin_exit (qemu_plugin_id_t id,void *p) +{ +dprintf (stdout_fd,"insns: %" PRIu64", bb: %" PRIu64", " +"avg insns/bb: %.2f\n" , + insn_count, bb_count, (double )insn_count / bb_count); +} + +static void vcpu_tb_exec (unsigned int cpu_index,void *udata) +{ +unsigned long n_insns = (unsigned long )udata; + + insn_count += n_insns; + bb_count++; +} + +static void vcpu_tb_trans (qemu_plugin_id_t id,unsigned int cpu_index, +struct qemu_plugin_tb *tb) +{ +unsigned long n_insns =qemu_plugin_tb_n_insns (tb); + +qemu_plugin_register_vcpu_tb_exec_cb (tb, vcpu_tb_exec, +QEMU_PLUGIN_CB_NO_REGS , + (void *)n_insns); +} + +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, int argc, +char **argv) +{ +/* plugin_exit might write to stdout after stdout has been closed */ + stdout_fd =dup (STDOUT_FILENO ); +assert (stdout_fd); + +qemu_plugin_register_vcpu_tb_trans_cb (id, vcpu_tb_trans); +qemu_plugin_register_atexit_cb (id, plugin_exit,NULL ); +return 0 ; +}
plugin-examples/mem_count_racy_both.c new file +58 -0
58 lines not on this page · raw message
plugin-examples/Makefile new file +31 -0
@@ -0,0 +1,31 @@ +NAMES := +NAMES += bbcount_avgsize_racy +NAMES += mem_count_racy_both + +SONAMES := $(addsuffix .so,$(addprefix lib,$(NAMES))) + +# QEMU installed path, set by --prefix during configure +QEMU_PATH ?= /data/src/qemu-inst/plugin-test + +CC := gcc +CFLAGS := +CFLAGS += -O2 -Werror -Wall +CFLAGS += -Wundef -Wwrite-strings -Wmissing-prototypes +CFLAGS += -Wstrict-prototypes -Wredundant-decls +CFLAGS += -fno-strict-aliasing -fno-common -fwrapv +CFLAGS += -I$(QEMU_PATH)/include +LDLIBS := -lc + +all: $(SONAMES) + +%.o: %.c + $(CC) $(CFLAGS) -fPIC -c $< -o $@ + +lib%.so: %.o + $(CC) -shared -Wl,-soname,$@ -o $@ $^ $(LDLIBS) + +clean: + $(RM) -f *.o *.so + $(RM) -Rf .libs + +.PHONY: all clean
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC 48/48] plugin: add a couple of very simple examples
Quoted 11 lines
From: Emilio G. Cota [mailto:[email protected]]
Signed-off-by Emilio G. Cota <[email protected]>--- plugin-examples/bbcount_avgsize_racy.c | 50 ++++++++++++++++++++++ plugin-examples/mem_count_racy_both.c | 58 ++++++++++++++++++++++++++ plugin-examples/Makefile | 31 ++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 plugin-examples/bbcount_avgsize_racy.c create mode 100644 plugin-examples/mem_count_racy_both.c create mode 100644 plugin-examples/Makefile
<snip>
Quoted 54 lines
plugin-examples/mem_count_racy_both.c new file +48
-0@@ -0,0 +1,58 @@ +#include <inttypes.h> +#include <assert.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <stdio.h> + +#include <qemu-plugin.h> + +static uint64_t mem_count; +static int stdout_fd; +static bool do_inline; + +static void plugin_exit (qemu_plugin_id_t id,void *p) +{ +dprintf (stdout_fd,"accesses: %" PRIu64"\n" , mem_count); +} + +static void vcpu_mem (unsigned int cpu_index,qemu_plugin_meminfo_t meminfo, +uint64_t vaddr,void *udata) +{ + mem_count++; +} + +static void vcpu_tb_trans (qemu_plugin_id_t id,unsigned int cpu_index, +struct qemu_plugin_tb *tb) +{ +size_t n =qemu_plugin_tb_n_insns (tb); +size_t i; + +for (i =0 ; i < n; i++) { +struct qemu_plugin_insn *insn =qemu_plugin_tb_get_insn (tb, i); + +if (do_inline) { +qemu_plugin_register_vcpu_mem_inline (insn, +QEMU_PLUGIN_INLINE_ADD_U64 , + &mem_count,1 ); + }else { +qemu_plugin_register_vcpu_mem_cb (insn, vcpu_mem, +QEMU_PLUGIN_CB_NO_REGS ,NULL ); + } + } +} + +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, int argc, +char **argv) +{ +if (argc &&strcmp (argv[0 ],"inline" ) ==0 ) {
plugin-examples/mem_count_racy_both.c line 58
Quoted 10 lines
plugin-examples/mem_count_racy_both.c new file +10
-0+ do_inline = true; + } +/* plugin_exit might write to stdout after stdout has been closed */ + stdout_fd =dup (STDOUT_FILENO ); +assert (stdout_fd); + +qemu_plugin_register_vcpu_tb_trans_cb (id, vcpu_tb_trans); +qemu_plugin_register_atexit_cb (id, plugin_exit,NULL ); +return 0 ; +}
Thanks for the series.
Can you provide more plugin examples for better understanding of double-translate idea? E.g., plugins that hook specific instructions or addresses.
Pavel Dovgalyuk
Re: [Qemu-devel] [RFC 48/48] plugin: add a couple of very simple examples
Quoted 2 lines · Pavel Dovgalyuk
Quoted 2 lines
From: Emilio G. Cota [mailto:[email protected]]
Signed-off-by Emilio G. Cota <[email protected]>
(snip)
Quoted 3 lines
Thanks for the series.
Can you provide more plugin examples for better understanding of double-translate idea? E.g., plugins that hook specific instructions or addresses.
Here's a plugin that at TB translation time, disassembles the instructions provided via capstone. It doesn't do anything with that info (vcpu_tb_exec is empty), but say a simulator would pass via the *udata pointer some descriptor (allocated at translation time) based on the instructions in the TB.
Note that the disassembly happens because we already have a fully formed TB thanks to the 2-pass translation. Without it, we'd have to (1) perform additional loads in the guest to read instructions given the PC, and (2) guess when the TB would end (recall that when to finish a TB is a decision internal to QEMU).
Thanks,
Emilio
PS. Compile with -lcapstone
---
#include <inttypes.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <capstone/capstone.h>
#include <qemu-plugin.h>
struct tb {
size_t n_insns;
};static csh cap_handle;
static cs_insn *cap_insn;
static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo,
uint64_t vaddr, void *udata)
{ }
static void vcpu_tb_exec(unsigned int cpu_index, void *udata) { }
static void vcpu_tb_trans(qemu_plugin_id_t id, unsigned int cpu_index, struct qemu_plugin_tb *tb)
{
struct tb *desc;
size_t n = qemu_plugin_tb_n_insns(tb);
size_t i;
for (i = 0; i < n; i++) {
struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
size_t size = qemu_plugin_insn_size(insn);
const uint8_t *code = qemu_plugin_insn_data(insn);
uint64_t offset = 0;
bool success;
success = cs_disasm_iter(cap_handle, &code, &size, &offset, cap_insn);
assert(success);
qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem,
QEMU_PLUGIN_CB_NO_REGS, NULL);
}
desc = malloc(sizeof(*desc));
assert(desc);
desc->n_insns = n;
qemu_plugin_register_vcpu_tb_exec_cb(tb, vcpu_tb_exec,
QEMU_PLUGIN_CB_NO_REGS, desc);
}QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, int argc, char **argv)
{
if (cs_open(CS_ARCH_X86, CS_MODE_64, &cap_handle) != CS_ERR_OK) {
return -1;
}
cap_insn = cs_malloc(cap_handle);
if (cap_insn == NULL) {
return -1;
}
qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
return 0;
}Re: [Qemu-devel] [RFC 48/48] plugin: add a couple of very simple examples
In reply to Emilio G. CotaQuoted 5 lines · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>--- plugin-examples/bbcount_avgsize_racy.c | 50 ++++++++++++++++++++++ plugin-examples/mem_count_racy_both.c | 58 ++++++++++++++++++++++++++ plugin-examples/Makefile | 31 ++++++++++++++
So I think we need to be putting these somewhere else and also building the examples by default. As plugins only make sense with tcg guests maybe a layout like:
tcg/plugins/plugin.c
tcg/plugins/examples/
Quoted 11 lines
3 files changed, 139 insertions(+) create mode 100644 plugin-examples/bbcount_avgsize_racy.c create mode 100644 plugin-examples/mem_count_racy_both.c create mode 100644 plugin-examples/Makefileplugin-examples/bbcount_avgsize_racy.c new file +0
-0
plugin-examples/bbcount_avgsize_racy.c line 8
Quoted 8 lines
plugin-examples/bbcount_avgsize_racy.c new file +8
-0@@ -0,0 +1,50 @@ +#include <inttypes.h> +#include <assert.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <stdio.h> + +#include <qemu-plugin.h>
-#include <qemu-plugin.h>
+#include <plugin-api.h>
Quoted 48 lines
plugin-examples/bbcount_avgsize_racy.c new file +42
-0+ +static uint64_t bb_count; +static uint64_t insn_count; +const char *filename; +static int stdout_fd; + +static void plugin_exit (qemu_plugin_id_t id,void *p) +{ +dprintf (stdout_fd,"insns: %" PRIu64", bb: %" PRIu64", " +"avg insns/bb: %.2f\n" , + insn_count, bb_count, (double )insn_count / bb_count); +} + +static void vcpu_tb_exec (unsigned int cpu_index,void *udata) +{ +unsigned long n_insns = (unsigned long )udata; + + insn_count += n_insns; + bb_count++; +} + +static void vcpu_tb_trans (qemu_plugin_id_t id,unsigned int cpu_index, +struct qemu_plugin_tb *tb) +{ +unsigned long n_insns =qemu_plugin_tb_n_insns (tb); + +qemu_plugin_register_vcpu_tb_exec_cb (tb, vcpu_tb_exec, +QEMU_PLUGIN_CB_NO_REGS , + (void *)n_insns); +} + +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, int argc, +char **argv) +{ +/* plugin_exit might write to stdout after stdout has been closed */ + stdout_fd =dup (STDOUT_FILENO ); +assert (stdout_fd); + +qemu_plugin_register_vcpu_tb_trans_cb (id, vcpu_tb_trans); +qemu_plugin_register_atexit_cb (id, plugin_exit,NULL ); +return 0 ; +}plugin-examples/mem_count_racy_both.c new file +0
-0
plugin-examples/mem_count_racy_both.c line 8
Quoted 8 lines
plugin-examples/mem_count_racy_both.c new file +8
-0@@ -0,0 +1,58 @@ +#include <inttypes.h> +#include <assert.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <stdio.h> + +#include <qemu-plugin.h>
-#include <qemu-plugin.h>
+#include <plugin-api.h>
Quoted 56 lines
plugin-examples/mem_count_racy_both.c new file +50
-0+ +static uint64_t mem_count; +static int stdout_fd; +static bool do_inline; + +static void plugin_exit (qemu_plugin_id_t id,void *p) +{ +dprintf (stdout_fd,"accesses: %" PRIu64"\n" , mem_count); +} + +static void vcpu_mem (unsigned int cpu_index,qemu_plugin_meminfo_t meminfo, +uint64_t vaddr,void *udata) +{ + mem_count++; +} + +static void vcpu_tb_trans (qemu_plugin_id_t id,unsigned int cpu_index, +struct qemu_plugin_tb *tb) +{ +size_t n =qemu_plugin_tb_n_insns (tb); +size_t i; + +for (i =0 ; i < n; i++) { +struct qemu_plugin_insn *insn =qemu_plugin_tb_get_insn (tb, i); + +if (do_inline) { +qemu_plugin_register_vcpu_mem_inline (insn, +QEMU_PLUGIN_INLINE_ADD_U64 , + &mem_count,1 ); + }else { +qemu_plugin_register_vcpu_mem_cb (insn, vcpu_mem, +QEMU_PLUGIN_CB_NO_REGS ,NULL ); + } + } +} + +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, int argc, +char **argv) +{ +if (argc &&strcmp (argv[0 ],"inline" ) ==0 ) { + do_inline = true; + } +/* plugin_exit might write to stdout after stdout has been closed */ + stdout_fd =dup (STDOUT_FILENO ); +assert (stdout_fd); + +qemu_plugin_register_vcpu_tb_trans_cb (id, vcpu_tb_trans); +qemu_plugin_register_atexit_cb (id, plugin_exit,NULL ); +return 0 ; +}plugin-examples/Makefile new file +0
-0
plugin-examples/Makefile line 8
Quoted 8 lines
plugin-examples/Makefile new file +8
-0@@ -0,0 +1,31 @@ +NAMES := +NAMES += bbcount_avgsize_racy +NAMES += mem_count_racy_both + +SONAMES := $(addsuffix .so,$(addprefix lib,$(NAMES))) + +# QEMU installed path, set by --prefix during configure +QEMU_PATH ?= /data/src/qemu-inst/plugin-test
I guess we should use SRC_PATH (or maybe QEMU_SRC_PATH) if we want the Makefile to be both invoked as a sub-make from a normal QEMU build and to be used as an example for out-of-tree builds.
plugin-examples/Makefile line 16
Quoted 8 lines
plugin-examples/Makefile new file +8
-0+ +CC := gcc +CFLAGS := +CFLAGS += -O2 -Werror -Wall +CFLAGS += -Wundef -Wwrite-strings -Wmissing-prototypes +CFLAGS += -Wstrict-prototypes -Wredundant-decls +CFLAGS += -fno-strict-aliasing -fno-common -fwrapv +CFLAGS += -I$(QEMU_PATH)/include
-CFLAGS += -I$(QEMU_PATH)/include
+CFLAGS += -I$(QEMU_PATH)/include/qemu
--
Alex Bennée
Re: [Qemu-devel] [RFC 48/48] plugin: add a couple of very simple examples
Quoted 11 lines · Alex Bennée
Quoted 5 lines · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>--- plugin-examples/bbcount_avgsize_racy.c | 50 ++++++++++++++++++++++ plugin-examples/mem_count_racy_both.c | 58 ++++++++++++++++++++++++++ plugin-examples/Makefile | 31 ++++++++++++++So I think we need to be putting these somewhere else and also building the examples by default. As plugins only make sense with tcg guests
Most of plugin functionality is related to TCG, but some of it is not. For instance, one could use a plugin to just control the guest clock, and nothing else. This would work with KVM.
This is why I think $SRC_PATH/plugin.c makes sense.
Wrt the examples, I just included them here for reviewing purposes. If we end up adding them, I think tests/plugin[s] would be an appropriate place for them.
Quoted 6 lines
plugin-examples/Makefile line 8
Quoted 2 lines
plugin-examples/Makefile new file +2
-0+# QEMU installed path, set by --prefix during configure +QEMU_PATH ?= /data/src/qemu-inst/plugin-testI guess we should use
SRC_PATH(or maybeQEMU_SRC_PATH) if we want theMakefileto be both invoked as a sub-make from a normal QEMU build and to be used as an example for out-of-tree builds.
(As I said, this was a quick hack just to get the RFC out :P)
Thanks,
Emilio
Re: [Qemu-devel] [RFC 48/48] plugin: add a couple of very simple examples
In reply to Emilio G. Cotaplugin-examples/Makefile line 25
Quoted 3 lines · Emilio G. Cota
plugin-examples/Makefile new file +3
-0+ +lib%.so: %.o + $(CC) -shared -Wl,-soname,$@ -o $@ $^ $(LDLIBS)
The rule should be a bit different for macOS:
%.bundle: %.o
$(CC) -bundle -Wl,-bundle_loader,PATH_TO_QEMU_EXE -o $@ $^ $(LDLIBS)
"-bundle" flag is needed because macOS has two kinds of Mach-O dynamically loaded executables:
- dylib (MH_DYLIB) - it's like an ELF shared object used for dynamic linking. Can be loaded, preloaded for sake of function interposing but cannot be explicitly unloaded.
- bundle (MH_BUNDLE) - similar to an ELF shared object used in plugin dlopen/dlclose scenario.
We can pick any (enabled in configure) qemu-system executable as bundle_loader. We specify it to check if all symbols in a bundle, including the ones coming from the executable are defined. FWIW, here's the reference for bundle_loader flag:
-bundle_loader executable
This specifies the executable that will be loading the bundle output file being linked. Undefined symbols from the bundle are checked against the specified executable like it was one of the dynamic libraries the bundle was linked with
The ".bundle" extension is not required but IMO it feels more native to the system than ".so".
I've checked both plugins can be loaded and print a line when QEMU exits.
Best regards,
Roman
Re: [Qemu-devel] [RFC 48/48] plugin: add a couple of very simple examples
Quoted 28 lines · Roman Bolshakov
plugin-examples/Makefile line 25
Quoted 3 lines · Emilio G. Cota
plugin-examples/Makefile new file +3
-0+ +lib%.so: %.o + $(CC) -shared -Wl,-soname,$@ -o $@ $^ $(LDLIBS)The rule should be a bit different for macOS:
%.bundle: %.o$(CC) -bundle -Wl,-bundle_loader,PATH_TO_QEMU_EXE -o $@ $^ $(LDLIBS)"-bundle" flag is needed because macOS has two kinds of Mach-O dynamically loaded executables:
- dylib (MH_DYLIB) - it's like an ELF shared object used for dynamic linking. Can be loaded, preloaded for sake of function interposing but cannot be explicitly unloaded.
- bundle (MH_BUNDLE) - similar to an ELF shared object used in plugin dlopen/dlclose scenario.We can pick any (enabled in configure) qemu-system executable as
bundle_loader. We specify it to check if all symbols in a bundle, including the ones coming from the executable are defined. FWIW, here's the reference forbundle_loaderflag:
-bundle_loader executable
This specifies the executable that will be loading the bundle output file being linked. Undefined symbols from the bundle are checked against the specified executable like it was one of the dynamic libraries the bundle was linked with
Quoted 2 lines
The ".bundle" extension is not required but IMO it feels more native to the system than ".so".
The goal of the examples is to be target-independent, so I'm not convinced that we want to bury $PATH_TO_QEMU_EXE in the build recipe (or get configure involved in this).
Since you say the "bundle" business isn't a requirement, I'll leave just the .so rule in v2.
Thanks,
Emilio
Quoted 14 lines
From: Emilio G. Cota [mailto:[email protected]]
- 2-pass translation. Once a "TB translation" callback is called, the plugin must know the span of the TB. We should not force plugins to guess where the TB will end; that is strictly QEMU's job, and can change any time. A TB is thus a sequence of instructions of whatever length the particular QEMU implementation decides. Thus, for each TB, a 3-step process is followed: (1) the plugin layer keeps a copy of the contents of the current TB, (2) once the TB is well-defined, its descriptor and contents are passed to plugins, which then register their desired instrumentation (e.g. "call me back on this particular instruction", or "call me back when the whole TB executes"); note that plugins can use a disassembler like capstone to decide what to do with each instruction; they
Quoted 8 lines
can also allocate memory and then get a pointer to it passed back from the callbacks. And finally, (3) the target translator is called again to generate the final instrumented translated TB. This is what I called the "2-pass translation", since we go twice over the translation loop in
translator.c. Note that the 2-pass approach has virtually no overhead (0.40% for SPEC06int); translation is much cheaper than execution. But anyway, if no plugins have subscribed to TB translation, we only do one pass.
Can plugin affect the translation somehow to force flushing cached registers? E.g. callback may need correct EFLAGS which usually does not updated until the end of the block.
Quoted 4 lines
- Support for inlining instrumentation. This is done via an explicit API, i.e. we do not export TCG ops, which are internal to QEMU. For now, I just have support for incrementing a u64 with an immediate, e.g. to increment a counter.
It means that we'll have "yet another one TCG"?
Pavel Dovgalyuk
Quoted 23 lines · Pavel Dovgalyuk
Quoted 14 lines
From: Emilio G. Cota [mailto:[email protected]]
- 2-pass translation. Once a "TB translation" callback is called, the plugin must know the span of the TB. We should not force plugins to guess where the TB will end; that is strictly QEMU's job, and can change any time. A TB is thus a sequence of instructions of whatever length the particular QEMU implementation decides. Thus, for each TB, a 3-step process is followed: (1) the plugin layer keeps a copy of the contents of the current TB, (2) once the TB is well-defined, its descriptor and contents are passed to plugins, which then register their desired instrumentation (e.g. "call me back on this particular instruction", or "call me back when the whole TB executes"); note that plugins can use a disassembler like capstone to decide what to do with each instruction; theyQuoted 8 lines
can also allocate memory and then get a pointer to it passed back from the callbacks. And finally, (3) the target translator is called again to generate the final instrumented translated TB. This is what I called the "2-pass translation", since we go twice over the translation loop in
translator.c. Note that the 2-pass approach has virtually no overhead (0.40% for SPEC06int); translation is much cheaper than execution. But anyway, if no plugins have subscribed to TB translation, we only do one pass.
Quoted 3 lines
Can plugin affect the translation somehow to force flushing cached registers? E.g. callback may need correct EFLAGS which usually does not updated until the end of the block.
I'd provide an API call to get those up to date, since the common case is that callbacks won't require those to be up to date.
Quoted 6 lines
Quoted 4 lines
- Support for inlining instrumentation. This is done via an explicit API, i.e. we do not export TCG ops, which are internal to QEMU. For now, I just have support for incrementing a u64 with an immediate, e.g. to increment a counter.
It means that we'll have "yet another one TCG"?
That's certainly not my intention.
I'd only export common ops that are trivial to implement, whether with TCG or whatever we use in 100 years time. I think incrementing a counter is a good use of this; letting plugins go wild by exporting a "TCG-like API" is not at all the point. Users that demand that can be just told to fork QEMU.
Thanks,
Emilio