public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Christoffer Dall <christoffer.dall@linaro.org>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: Scott Wood <scottwood@freescale.com>,
	kvm-devel <kvm@vger.kernel.org>,
	kvm-ppc <kvm-ppc@vger.kernel.org>,
	"kvmarm@lists.cs.columbia.edu" <kvmarm@lists.cs.columbia.edu>,
	Patch Tracking <patches@linaro.org>,
	Marc Zyngier <marc.zyngier@arm.com>,
	Alexander Graf <agraf@suse.de>
Subject: Re: [PATCH v2] KVM: Specify byte order for KVM_EXIT_MMIO
Date: Fri, 24 Jan 2014 17:56:41 -0800	[thread overview]
Message-ID: <20140125015641.GA3750@cbox> (raw)
In-Reply-To: <CAFEAcA_YLwfcgcomXd7cmx5auyH48aZEFdD1UPHgXgVBGhX23g@mail.gmail.com>

On Sat, Jan 25, 2014 at 12:24:08AM +0000, Peter Maydell wrote:
> On 24 January 2014 23:51, Scott Wood <scottwood@freescale.com> wrote:
> > On Fri, 2014-01-24 at 15:39 -0800, Christoffer Dall wrote:
> >> The KVM API documentation is not clear about the semantics of the data
> >> field on the mmio struct on the kvm_run struct.
> >>
> >> This has become problematic when supporting ARM guests on big-endian
> >> host systems with guests of both endianness types, because it is unclear
> >> how the data should be exported to user space.
> >>
> >> This should not break with existing implementations as all supported
> >> existing implementations of known user space applications (QEMU and
> >> kvmtools for virtio) only support default endianness of the
> >> architectures on the host side.
> >>
> >> Cc: Marc Zyngier <marc.zyngier@arm.com>
> >> Cc: Peter Maydell <peter.maydell@linaro.org>
> >> Cc: Alexander Graf <agraf@suse.de>
> >> Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
> >> ---
> >> Changes [v1 - v2]:
> >>  - s/host kernel should/host user space should/
> >>
> >>  Documentation/virtual/kvm/api.txt | 5 +++++
> >>  1 file changed, 5 insertions(+)
> >>
> >> diff --git a/Documentation/virtual/kvm/api.txt b/Documentation/virtual/kvm/api.txt
> >> index 366bf4b..6dbd68c 100644
> >> --- a/Documentation/virtual/kvm/api.txt
> >> +++ b/Documentation/virtual/kvm/api.txt
> >> @@ -2565,6 +2565,11 @@ executed a memory-mapped I/O instruction which could not be satisfied
> >>  by kvm.  The 'data' member contains the written data if 'is_write' is
> >>  true, and should be filled by application code otherwise.
> >>
> >> +The 'data' member byte order is host kernel native endianness, regardless of
> >> +the endianness of the guest, and represents the the value as it would go on the
> >> +bus in real hardware.  The host user space should always be able to do:
> >> +<type> val = *((<type> *)mmio.data).
> >
> > Host userspace should be able to do that with what results?  It would
> > only produce a directly usable value if host endianness is the same as
> > the emulated device's endianness.
> 
> With the result that it gets the value the CPU has sent out on
> the bus as the memory transaction. Obviously if what userspace
> is emulating is a bus which has a byteswapping bridge or if it's
> being helpful to device emulation by providing "here's the value
> even though you think you're wired up backwards" then it needs
> to byteswap.
> 

Yes, because KVM emulates the CPU, it's interface should emulate the
external interface of the CPU, not CPU-internal bits.

The value is directly usable to emulate the bus to attached devices in
user space.

> > I'm not sure that "host kernel native endianness" is an accurate way of
> > describing what currently happens.  Regardless of host or guest
> > endianness, the guest should be swapping the value as necessary to
> > ensure that the value that goes on the (real or emulated) bus is the
> > same.
> 
> I don't know why you're bringing the guest in here. Whether
> the guest chooses to byteswap or not is IMHO not relevant.
> What KVM and userspace need to combine to achieve is that
> whatever the guest happens to have done causes the same result
> it would on the real hardware. Whether the guest sends out a
> write of 0x12345678 because it wrote 0x12345678 directly or
> because it started with 0x87654321 and issued a byte-reverse
> instruction doesn't matter.
> 
> > Plus, if it were really "as it would go on the bus" the value wouldn't
> > necessarily be left justified within data[], depending on how the bus
> > works.
> 
> The point is that the value in data[] is not "as it would go on the bus",
> but the value you get out by treating it as a host-native-endianness
> value of the relevant size left-justified within data[] is the value as
> it would go on the bus.
> 
> > How about a wording like this:
> >
> >   The 'data' member contains, in its first 'len' bytes, the value as it
> >   would appear if the guest had accessed memory rather than I/O.
> 
> I think this is confusing, because now userspace authors have
> to figure out how to get back to "value X of size Y at address Z"
> by interpreting this text... Can you write out the equivalent of
> Christoffer's text "here's how you get the memory transaction
> value" for what you want?
> 
> (Also, value as it would appear to who?)
> 
> I think your wording implies that the order of bytes in data[] depend
> on the guest CPU "usual byte order", ie the order which the CPU
> does not do a byte-lane-swap for (LE for ARM, BE for PPC),
> and it would mean it would come out differently from
> my/Alex/Christoffer's proposal if the host kernel was the opposite
> endianness from that "usual" order.
> 
> Finally, I think it's a bit confusing in that "as if the guest had
> accessed memory" is assigning implicit semantics to memory
> in the emulated system, when memory is actually kind of outside
> KVM's purview because it's not part of the CPU.
> 

The "as if the guest had accessed memory", would imply that user space
needs to look at the settings of the CPU (in the case of ARM the E-bit)
to understand which value the CPU intended for the memory transaction.

However, I'm fine with rewording the patch to something like:

  The 'data' member contains, in its first 'len' bytes, the value of the
  memory transaction in host kernel native endianness, regardless of the
  endianness of the guest.  Host user space should always be able to
  directly do the following to emulate the bus and attached peripherals:
  <type> val = *((<type> *)mmio.data)

if people find this more clear.

-Christoffer

  reply	other threads:[~2014-01-25  1:56 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-24 23:39 [PATCH v2] KVM: Specify byte order for KVM_EXIT_MMIO Christoffer Dall
2014-01-24 23:51 ` Scott Wood
2014-01-25  0:05   ` Victor Kamensky
2014-01-25  0:24   ` Peter Maydell
2014-01-25  1:56     ` Christoffer Dall [this message]
2014-01-25  2:04       ` Scott Wood
2014-01-25  2:16         ` Alexander Graf
2014-01-25  1:58     ` Scott Wood
2014-01-25  2:15       ` Alexander Graf
2014-01-25  2:34         ` Christoffer Dall
2014-01-25  9:13           ` Alexander Graf
2014-01-25  2:37         ` Victor Kamensky
2014-01-25  9:20           ` Alexander Graf
2014-01-25 15:36             ` Victor Kamensky
2014-01-25 16:12               ` Alexander Graf
2014-01-25 16:23         ` Peter Maydell
2014-01-25 18:31           ` Christoffer Dall
2014-01-26  3:46             ` Victor Kamensky
2014-01-26  5:43               ` Victor Kamensky
2014-01-27  7:52                 ` Alexander Graf
2014-01-27  9:42                   ` Peter Maydell
2014-01-27  7:41               ` Alexander Graf
2014-01-28  1:59         ` Scott Wood
2014-01-28  8:55           ` Peter Maydell

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=20140125015641.GA3750@cbox \
    --to=christoffer.dall@linaro.org \
    --cc=agraf@suse.de \
    --cc=kvm-ppc@vger.kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.cs.columbia.edu \
    --cc=marc.zyngier@arm.com \
    --cc=patches@linaro.org \
    --cc=peter.maydell@linaro.org \
    --cc=scottwood@freescale.com \
    /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