From: Paul Mackerras <paulus@samba.org>
To: Alexander Graf <agraf@suse.de>
Cc: kvm-ppc@vger.kernel.org, KVM list <kvm@vger.kernel.org>,
Rusty Russell <rusty@rustcorp.com.au>
Subject: Re: [PATCH v3 1/2] KVM: PPC: Book3S: Get/set guest SPRs using the GET/SET_ONE_REG interface
Date: Fri, 21 Sep 2012 19:52:34 +1000 [thread overview]
Message-ID: <20120921095234.GA12974@bloggs.ozlabs.ibm.com> (raw)
In-Reply-To: <0906175A-396F-4647-9CDF-BEF0B906DF67@suse.de>
On Fri, Sep 21, 2012 at 11:15:51AM +0200, Alexander Graf wrote:
>
> On 21.09.2012, at 07:44, Paul Mackerras wrote:
>
> > +union kvmppc_one_reg {
> > + u32 wval;
> > + u64 dval;
>
> Phew. Is this guaranteed to always pad on the right, rather than left?
Absolutely (for big-endian targets). A union is basically a struct
with all the members at offset 0. So we read N bytes into the union
and then access the N-byte member.
> > +static int one_reg_size(u64 id)
> > +{
> > + id = (id & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT;
> > + return 1 << id;
> > +}
>
> Rusty has a patch to export this as a generic function for all kvm targets. Check out
>
> http://permalink.gmane.org/gmane.comp.emulators.kvm.devel/97553
>
> I actually like static inlines better, but we really should keep this code on top level :).
Once Rusty's patch goes in I'd be happy to use it.
> > + switch (reg->id) {
> > + case KVM_REG_PPC_DAR:
> > + val.dval = vcpu->arch.shared->dar;
>
> Hmm. I would like to keep the amount of places who need to manually know about the size of a register as low as possible. We do know the size of the register already on in the REG id and in sizeof(register), right?
This code doesn't "manually" know about the size of the register. It
could be that vcpu->arch.shared->dar is 4 bytes, or 2 bytes, instead
of 8 bytes, and that wouldn't affect this code. All this code "knows"
is that the definition of KVM_REG_PPC_DAR includes the "8 byte"
encoding. And since that definition is -- or will be -- part of the
userspace ABI, it's not something that can be changed later, so I
don't see any problem with just using val.dval here.
> I think there will be valid scenarios for them to be of different size. For example when a register suddenly increases in size. But the assignment should always happen on the size the register id tells us.
It doesn't matter if a register changes in size, that doesn't affect
this code. We may want to define an extra KVM_REG_PPC_* symbol if it
increases in size, but that would be a different symbol to the ones we
already have. As I said, we couldn't just arbitrarily change the
existing symbol because that would break existing userspace programs.
In other words the assignment already does happen on the size the
register id tells us, with my code.
> So how about something like
>
> #define kvmppc_set_reg(id, val, reg) { \
> switch (one_reg_size(id)) { \
> case 4: val.wval = reg; break; \
> case 8: val.dval = reg; break; \
> default: BUG(); \
> } \
> }
>
> case KVM_REG_PPC_DAR:
> kvmppc_set_reg(reg->id, &val, vcpu->arch.shared->dar);
> break;
>
> Maybe you can come up with something even easier though :).
Seems unnecessarily complex to me. If we did something like that we
should do
kvmppc_set_reg(KVM_REG_PPC_DAR, &val, vcpu->arch.shared->dar);
and use a macro instead of one_reg_size() to give the compiler a
better chance to do constant folding and only compile in the branch of
the switch statement that we need.
> Btw, with such a scheme in place, we wouldn't even need the union. We could just reserve a char array and cast it in the setter/getter functions.
I'm always wary of accessing a variable of one type as a different
type using a cast on its address, because of the C99 type-punning
rules. It's probably OK with a char array, but I think a union is
cleaner, because you're explicitly stating that you want some storage
that can be accessed as different types.
Paul.
next prev parent reply other threads:[~2012-09-21 9:52 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-09-21 5:44 [PATCH v3 1/2] KVM: PPC: Book3S: Get/set guest SPRs using the GET/SET_ONE_REG interface Paul Mackerras
2012-09-21 5:45 ` [PATCH v3 2/2] KVM: PPC: Book3S: Get/set guest FP regs " Paul Mackerras
2012-09-21 9:15 ` [PATCH v3 1/2] KVM: PPC: Book3S: Get/set guest SPRs " Alexander Graf
2012-09-21 9:52 ` Paul Mackerras [this message]
2012-09-21 10:07 ` Alexander Graf
2012-09-24 12:16 ` Paul Mackerras
2012-09-24 12:29 ` Alexander Graf
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=20120921095234.GA12974@bloggs.ozlabs.ibm.com \
--to=paulus@samba.org \
--cc=agraf@suse.de \
--cc=kvm-ppc@vger.kernel.org \
--cc=kvm@vger.kernel.org \
--cc=rusty@rustcorp.com.au \
/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