From: "Pavel Dovgalyuk" <dovgaluk@ispras.ru>
To: 'Pavel Dovgalyuk' <dovgaluk@ispras.ru>,
'Stefan Hajnoczi' <stefanha@gmail.com>
Cc: "'Peter Maydell'" <peter.maydell@linaro.org>,
"'Pavel Dovgalyuk'" <Pavel.Dovgaluk@ispras.ru>,
"'Paolo Bonzini'" <pbonzini@redhat.com>,
maria.klimushenkova@ispras.ru,
"'QEMU Developers'" <qemu-devel@nongnu.org>,
"'Lluís Vilanova'" <vilanova@ac.upc.edu>
Subject: Re: [Qemu-devel] [RFC PATCH v2 0/7] QEMU binary instrumentation prototype
Date: Mon, 30 Jul 2018 16:26:30 +0300 [thread overview]
Message-ID: <002d01d42808$edcb9aa0$c962cfe0$@ru> (raw)
In-Reply-To: <000601d418dc$cbd606a0$638213e0$@ru>
Ping?
Pavel Dovgalyuk
> -----Original Message-----
> From: Pavel Dovgalyuk [mailto:dovgaluk@ispras.ru]
> Sent: Wednesday, July 11, 2018 9:03 AM
> To: 'Stefan Hajnoczi'
> Cc: 'Peter Maydell'; 'Pavel Dovgalyuk'; 'Paolo Bonzini'; maria.klimushenkova@ispras.ru; 'QEMU
> Developers'; 'Lluís Vilanova'
> Subject: RE: [Qemu-devel] [RFC PATCH v2 0/7] QEMU binary instrumentation prototype
>
> > From: Stefan Hajnoczi [mailto:stefanha@gmail.com]
> > On Tue, Jun 05, 2018 at 02:56:29PM +0300, Pavel Dovgalyuk wrote:
> > > > From: Peter Maydell [mailto:peter.maydell@linaro.org]
> > > >
> > > > This series doesn't seem to add anything to Documentation/ that
> > > > describes the API we make available to plugins. I'm a lot more
> > > > interested in reviewing the API that will be used by plugins
> > > > than I am in the implementation at this stage. Can you provide
> > > > a description/documentation of the API for review, please?
> > >
> > >
> > > Here is the draft:
> >
> > I like the minimal interface that you are proposing and that it is
> > completely separate from QEMU-internal APIs. This will make it easy to
> > keep this public API cleanly separated from private internal APIs.
> >
> > > Introduction
> > > ============
> > >
> > > This document describes an API for creating the QEMU
> > > instrumentation plugins.
> > >
> > > It is based on the following prior sources:
> > > - KVM Forum 2017 talk "Instrumenting, Introspection, and Debugging with QEMU"
> > > https://www.linux-kvm.org/images/3/3d/Introspect.pdf
> > > - Discussion on Lluis Vilanova instrumentation patch series
> > > https://lists.gnu.org/archive/html/qemu-devel/2017-09/msg03357.html
> > >
> > > The aim of the instrumentation is implementing different runtime
> > > tracers that can track the executed instructions, memory and
> > > hardware operations.
> > >
> > > Instrumenting the code
> > > ======================
> > >
> > > Instrumentation subsystem exploits TCG helper mechanism to embed
> > > callbacks into the translation blocks. These callbacks may be inserted
> > > before the specific instructions, when the plugins require such filtering.
> > >
> > > Translator uses two functions for embedding the callbacks:
> > > - first function checks whether the current instruction should be
> > > instrumented
> > > - second function embeds the callback for executing the plugin-specific
> > > code before that instruction
> > >
> > > The similar method may be used for memory access instrumentation.
> > >
> > > QEMU->Plugin API
> > > ================
> > >
> > > Instrumentation layer passes the requests from the translator
> > > to the dynamically loaded plugins. Every plugin may provide
> > > the following functions to perform the instrumentation:
> > >
> > > 1. bool plugin_init(const char *args);
> > > Initialization function. May return false if the plugin
> > > can't work in the current environment.
> >
> > Please document how plugin loading and argument handling works.
> >
> > Do you think unloading is necessary?
>
> Yes, I think we'll need it, but I omitted it in the draft.
> Will fix in the next version.
>
> > > 2. bool plugin_needs_before_insn(uint64_t pc, void *cpu);
> > > Returns true if the plugin needs to instrument the current instruction.
> > > It may use the address (pc) for making the decision or the guest
> > > CPU state (cpu), which can be passed back to QEMU core API
> > > (e.g., for reading the guest memory).
> > > This function is called at both translation and execution phases.
> >
> > What type of address is 'pc' - guest virtual or guest physical?
>
> It is a guest virtual address. We can expose a function for address translation
> to let the plugin obtain physical address.
>
> > Is the guest CPU state well-defined when this function is called? For
> > example, is reading CPU registers meaningful in this function since it
> > could be called at pretty much any time?
>
> It is not. The guest state is the same as in translate.c.
> Therefore such functions are limited in accessing the registers.
>
> > Why is this function called during execution? I expected this to be
> > called at translation time only. If a plugin decides at runtime to
> > instrument instructions that were previously not instrumented, then it
> > could flush the relevant TB(s) - that seems a lot more efficient than
> > calling this function for every instruction that gets executed. But
> > maybe I am missing a use case for calling this at execution time...?
>
> It isn't called for every executed instruction. It is called for instructions
> that was marked by the same function to instrument.
> I had a choice of two options:
> - insert many callbacks in case of multiple plugins, but call 'needs' only once.
> - insert one callback to dispatch function and call 'needs' for every plugin again,
> because there is no information which plugin caused the instrumentation.
>
> Anyway, we can switch to the first option, if you don't see any other pitfalls
> except the larger generated code.
>
> > > 3. void plugin_before_insn(uint64_t pc, void *cpu);
> > > If the previous function returned true for some instruction,
> > > then this function will be called. This process is repeated before
> > > every execution of the instruction, if it was instrumented.
> >
> > Plugins that instrument multiple kinds of instructions will have to
> > first look up pc and decide which kind of instruction it is. The plugin
> > could keep a list or hash table, or it could read memory to check the
> > guest code again. This will be very repetitive - many plugins will need
> > to do this.
>
> Right.
>
> > A slightly different take on this API is:
> >
> > /* Plugin->QEMU API */
> >
> > /* Called by QEMU before translating an instruction
> > * @pc: guest virtual address of instruction
> > */
> > void plugin_pre_translate(void *cpu, uint64_t pc);
> >
> > /* QEMU->Plugin API */
> >
> > /* A callback invoked by QEMU before executing an instrumented
> > * instruction
> > * @opaque: plugin-specific data
> > */
> > typedef void (*InstrumentCallback)(void *cpu, void *opaque);
> >
> > /* Register a callback @cb each time the instruction at @pc is about
> > * to be executed
> > * @cpu: the cpu to instrument or NULL to instrument all cpus
>
> This is interesting point I missed.
> As far, as I understand, current version translates TB only once for all CPUs.
> And we could need to instrument only some of them, which means that translation
> should be somehow extended for that.
>
> > * @opaque: plugin-specific data that is passed to @cb
> > */
> > void instrument(void *cpu, uint64_t pc,
> > InstrumentCallback cb,
> > void *opaque);
> >
> > /* Unregister a callback @cb previously registered using instrument()
> > */
> > void uninstrument(void *cpu, uint64_t pc,
> > InstrumentCallback cb,
> > void *opaque);
> >
> > Here plugin_pre_translate() is similar to plugin_needs_before_insn(),
> > but note it has no return value. Instead of telling QEMU whether or not
> > to instrument an instruction, it must call instrument() if it wishes to
> > receive a callback immediately before a particular instruction is
> > executed.
>
> This is similar to our current approach here: https://github.com/ispras/qemu/tree/plugins
>
> But it has some issues:
> - You can't switch QEMU core from translation to interpretation, because this interface
> is targeted to translation.
> - Callbacks can't be inserted without registering them in tcg
> (this in needed at least for debug output).
> Therefore someone should maintain already registered callbacks, or we should
> expose the registration interface to the plugins (as we do in our projects now),
> which also nail them to the translation-based emulation.
>
> Pavel Dovgalyuk
next prev parent reply other threads:[~2018-07-30 13:26 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-05 10:39 [Qemu-devel] [RFC PATCH v2 0/7] QEMU binary instrumentation prototype Pavel Dovgalyuk
2018-06-05 10:39 ` [Qemu-devel] [RFC PATCH v2 1/7] tcg: add headers for non-target helpers Pavel Dovgalyuk
2018-06-05 13:07 ` Thomas Huth
2018-06-06 7:30 ` Pavel Dovgalyuk
2018-09-07 12:16 ` Alex Bennée
2018-06-05 10:39 ` [Qemu-devel] [RFC PATCH v2 2/7] Add plugin support Pavel Dovgalyuk
2018-09-07 10:11 ` Alex Bennée
2018-09-13 6:40 ` Pavel Dovgalyuk
2018-09-07 12:34 ` Alex Bennée
2018-09-10 8:30 ` Pavel Dovgalyuk
2018-09-07 14:14 ` Alex Bennée
2018-09-10 11:41 ` Pavel Dovgalyuk
2018-06-05 10:39 ` [Qemu-devel] [RFC PATCH v2 3/7] plugins: provide helper functions for plugins Pavel Dovgalyuk
2018-09-07 13:06 ` Alex Bennée
2018-06-05 10:39 ` [Qemu-devel] [RFC PATCH v2 4/7] tcg: add instrumenting module Pavel Dovgalyuk
2018-09-07 13:36 ` Alex Bennée
2018-09-13 6:55 ` Pavel Dovgalyuk
2018-06-05 10:39 ` [Qemu-devel] [RFC PATCH v2 5/7] plugins: add plugin template Pavel Dovgalyuk
2018-09-07 13:41 ` Alex Bennée
2018-06-05 10:39 ` [Qemu-devel] [RFC PATCH v2 6/7] plugin: add instruction execution logger Pavel Dovgalyuk
2018-09-07 13:59 ` Alex Bennée
2018-06-05 10:39 ` [Qemu-devel] [RFC PATCH v2 7/7] plugins: add syscall logging plugin sample Pavel Dovgalyuk
2018-09-07 14:06 ` Alex Bennée
2018-09-10 9:18 ` Pavel Dovgalyuk
2018-09-10 13:58 ` Alex Bennée
2018-06-05 10:49 ` [Qemu-devel] [RFC PATCH v2 0/7] QEMU binary instrumentation prototype Peter Maydell
2018-06-05 11:56 ` Pavel Dovgalyuk
2018-06-25 5:46 ` Pavel Dovgalyuk
2018-06-25 9:06 ` Peter Maydell
2018-09-07 14:10 ` Alex Bennée
2018-07-10 13:06 ` Stefan Hajnoczi
2018-07-11 6:02 ` Pavel Dovgalyuk
2018-07-30 13:26 ` Pavel Dovgalyuk [this message]
2018-08-29 5:39 ` Pavel Dovgalyuk
2018-08-29 19:57 ` Peter Maydell
2018-08-30 4:03 ` Alex Bennée
2018-06-06 8:52 ` no-reply
2018-06-06 9:21 ` no-reply
2018-06-06 10:45 ` no-reply
2018-09-07 14:39 ` Alex Bennée
2018-09-08 0:57 ` Peter Maydell
2018-09-10 9:01 ` Alex Bennée
2018-09-10 11:44 ` Pavel Dovgalyuk
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='002d01d42808$edcb9aa0$c962cfe0$@ru' \
--to=dovgaluk@ispras.ru \
--cc=Pavel.Dovgaluk@ispras.ru \
--cc=maria.klimushenkova@ispras.ru \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@gmail.com \
--cc=vilanova@ac.upc.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).