All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Han, Huaitong" <huaitong.han@intel.com>
To: "pbonzini@redhat.com" <pbonzini@redhat.com>
Cc: "gleb@kernel.org" <gleb@kernel.org>,
	"kvm@vger.kernel.org" <kvm@vger.kernel.org>
Subject: Re: [PATCH 6/9] KVM, pkeys: add pkeys support for permission_fault logic
Date: Tue, 10 Nov 2015 09:28:08 +0000	[thread overview]
Message-ID: <1447147692.30613.12.camel@intel.com> (raw)
In-Reply-To: <56409CDF.3020406@redhat.com>

On Mon, 2015-11-09 at 14:17 +0100, Paolo Bonzini wrote:

> > >  static inline bool permission_fault(struct kvm_vcpu *vcpu,
> > > struct kvm_mmu *mmu,
> > > -				    unsigned pte_access,
> > > unsigned pfec)
> > > +		unsigned pte_access, unsigned pte_pkeys,
> > > unsigned pfec)
> > >  {
> > > -	int cpl = kvm_x86_ops->get_cpl(vcpu);
> > > -	unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
> > > +	unsigned long smap, rflags;
> > > +	u32 pkru;
> > > +	int cpl, index;
> > > +	bool wf, uf, pk, pkru_ad, pkru_wd;
> > > +
> > > +	cpl = kvm_x86_ops->get_cpl(vcpu);
> > > +	rflags = kvm_x86_ops->get_rflags(vcpu);
> > > +
> > > +	pkru = read_pkru();
> > > +
> > > +	/*
> > > +	* PKRU defines 32 bits, there are 16 domains and 2
> > > attribute bits per
> > > +	* domain in pkru, pkey is index to a defined domain, so
> > > the value of
> > > +	* pkey * PKRU_ATTRS + R/W is offset of a defined domain
> > > attribute.
> > > +	*/
> > > +	pkru_ad = (pkru >> (pte_pkeys * PKRU_ATTRS + PKRU_READ))
> > > & 1;
> > > +	pkru_wd = (pkru >> (pte_pkeys * PKRU_ATTRS +
> > > PKRU_WRITE)) & 1;
> > > +
> > > +	wf = pfec & PFERR_WRITE_MASK;
> > > +	uf = pfec & PFERR_USER_MASK;
> > > +
> > > +	/*
> > > +	* PKeys 2nd and 6th conditions:
> > > +	* 2.EFER_LMA=1
> > > +	* 6.PKRU.AD=1
> > > +	*	or The access is a data write and PKRU.WD=1 and
> > > +	*		either CR0.WP=1 or it is a user mode
> > > access
> > > +	*/
> > > +	pk = is_long_mode(vcpu) && (pkru_ad ||
> > > +			(pkru_wd && wf &&
> > > (is_write_protection(vcpu) || uf)));
> 
> A little more optimized:
> 
> 	pkru_bits = (pkru >> (pte_pkeys * PKRU_ATTRS)) & 3;
> 
> 	/*
> 	 * Ignore PKRU.WD if not relevant to this access (a read,
> 	 * or a supervisor mode access if CR0.WP=0).
> 	 */
> 	if (!wf || (!uf && !is_write_protection(vcpu)))
> 		pkru_bits &= ~(1 << PKRU_WRITE);
> 
> ... and then just check pkru_bits != 0.
> 
> > > +	/*
> > > +	* PK bit right value in pfec equal to
> > > +	* PK bit current value in pfec and pk value.
> > > +	*/
> > > +	pfec &= (pk << PFERR_PK_BIT) + ~PFERR_PK_MASK;
> > 
> > PK is only applicable to guest page tables, but if you do not
> > support
> > PKRU without EPT (patch 9), none of this is necessary, is it?
> 
> Doh. :(  Sorry, this is of course needed for the emulation case.
> 
> I think you should optimize this for the common case where pkru is
> zero,
> hence pk will always be zero.  So something like
> 
> 	pkru = is_long_mode(vcpu) ? read_pkru() : 0;
> 	if (unlikely(pkru) && (pfec & PFERR_PK_MASK)) {
> 		... from above ... */
> 
> 		/* Flip PFERR_PK_MASK if pkru_bits is non-zero */
> 		pfec ^= -pkru_bits & PFERR_PK_MASK;

If pkru_bits is zero, it means dynamically conditions is not met for
protection-key violations, so pfec on PK bit should be flipped. So I
guess it should be:
	pfec ^= pkru_bits ? 0 : PFERR_PK_MASK;

> 	}
> 
> Paolo
> 

  reply	other threads:[~2015-11-10  9:29 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-09 11:54 [PATCH 0/9] KVM, pkeys: add memory protection-key support Huaitong Han
2015-11-09 11:54 ` [PATCH 1/9] KVM, pkeys: expose CPUID:PKU to guest Huaitong Han
2015-11-09 11:54 ` [PATCH 2/9] KVM, pkeys: add pkeys support when setting CR4 Huaitong Han
2015-11-09 11:54 ` [PATCH 3/9] KVM, pkeys: expose CPUID:OSPKE to guest Huaitong Han
2015-11-09 12:32   ` Paolo Bonzini
2015-11-09 11:54 ` [PATCH 4/9] KVM, pkeys: disable pkeys for guests in non-paging mode Huaitong Han
2015-11-09 11:54 ` [PATCH 5/9] KVM, pkeys: update memeory permission bitmask for pkeys Huaitong Han
2015-11-09 13:24   ` Paolo Bonzini
2015-11-09 11:54 ` [PATCH 6/9] KVM, pkeys: add pkeys support for permission_fault logic Huaitong Han
2015-11-09 12:43   ` Paolo Bonzini
2015-11-09 13:17     ` Paolo Bonzini
2015-11-10  9:28       ` Han, Huaitong [this message]
2015-11-10  9:35         ` Paolo Bonzini
2015-11-09 11:54 ` [PATCH 7/9] KVM, pkeys: Add pkeys support for gva_to_gpa funcions Huaitong Han
2015-11-09 13:23   ` Paolo Bonzini
2015-11-09 11:54 ` [PATCH 8/9] KVM, pkeys: add pkeys support for xsave state Huaitong Han
2015-11-09 11:54 ` [PATCH 9/9] KVM, pkeys: disable PKU feature without ept Huaitong Han
2015-11-09 13:26 ` [PATCH 0/9] KVM, pkeys: add memory protection-key support Paolo Bonzini

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=1447147692.30613.12.camel@intel.com \
    --to=huaitong.han@intel.com \
    --cc=gleb@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.