public inbox for kvm@vger.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>,
	"guangrong.xiao@linux.intel.com" <guangrong.xiao@linux.intel.com>
Subject: Re: [PATCH V5 6/9] KVM, pkeys: add pkeys support for permission_fault logic
Date: Mon, 21 Mar 2016 12:41:46 +0000	[thread overview]
Message-ID: <1458564105.3453.4.camel@intel.com> (raw)
In-Reply-To: <56EFD325.7010903@redhat.com>

On Mon, 2016-03-21 at 11:55 +0100, Paolo Bonzini wrote:
> 
> On 21/03/2016 11:05, Huaitong Han wrote:
> >  static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
> > -				  unsigned pte_access, unsigned pfec)
> > +				  unsigned pte_access, unsigned pte_pkey,
> > +				  unsigned pfec)
> >  {
> >  	int cpl = kvm_x86_ops->get_cpl(vcpu);
> >  	unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
> > +	unsigned long smap = (cpl - 3) & (rflags & X86_EFLAGS_AC);
> > +	int index = (pfec >> 1) +
> > +		    (smap >> (X86_EFLAGS_AC_BIT - PFERR_RSVD_BIT + 1));
> > +
> > +	if (unlikely(mmu->pkru_mask)) {
> > +		u32 pkru_bits, offset;
> > +
> > +		WARN_ON(pfec & (PFERR_PK_MASK | PFERR_RSVD_MASK));
> > +
> > +		/*
> > +		* PKRU defines 32 bits, there are 16 domains and 2
> > +		* attribute bits per domain in pkru, pkey is the
> > +		* index to a defined domain, so the value of
> > +		* pkey * 2 is offset of a defined domain.
> > +		*/
> > +		pkru_bits = (kvm_read_pkru(vcpu) >> (pte_pkey * 2)) & 3;
> > +		/* replace PFEC.RSVD with ACC_USER_MASK. */
> > +		offset = pfec | ((pte_access & PT_USER_MASK) <<
> > +			(PFERR_RSVD_BIT - PT_USER_SHIFT));
> > +
> > +		pkru_bits &= mmu->pkru_mask >> (offset & ~1);
> > +		pfec |= -pkru_bits & PFERR_PK_MASK;
> > +	}
> >  
> >  	/*
> >  	 * If CPL < 3, SMAP prevention are disabled if EFLAGS.AC = 1.
> > @@ -167,14 +192,12 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
> >  	 * but it will be one in index if SMAP checks are being overridden.
> >  	 * It is important to keep this branchless.
> >  	 */
> > -	unsigned long smap = (cpl - 3) & (rflags & X86_EFLAGS_AC);
> > -	int index = (pfec >> 1) +
> > -		    (smap >> (X86_EFLAGS_AC_BIT - PFERR_RSVD_BIT + 1));
> >  
> >  	WARN_ON(pfec & PFERR_RSVD_MASK);
> >  
> >  	pfec |= PFERR_PRESENT_MASK;
> > -	return -((mmu->permissions[index] >> pte_access) & 1) & pfec;
> > +	return -(((pfec >> PFERR_PK_BIT) |
> > +		 (mmu->permissions[index] >> pte_access)) & 1) & pfec;
> >  }
> >  
> >  void kvm_mmu_invalidate_zap_all_pages(struct kvm *kvm);
> 
> Slightly cleaner:
> 
> 1) keep WARN_ON together
> 2) keep smap comment close to smap variable
> 3) build expression for final return a piece at a time
> 
> Does it look good?

Accepted.
> 
> Thanks,
> 
> Paolo
> 
> @@ -149,7 +150,8 @@ static inline bool is_write_protection(struct kvm_vcpu *vcpu)
>   * if the access faults.
>   */
>  static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
> -				  unsigned pte_access, unsigned pfec)
> +				  unsigned pte_access, unsigned pte_pkey,
> +				  unsigned pfec)
>  {
>  	int cpl = kvm_x86_ops->get_cpl(vcpu);
>  	unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
> @@ -170,11 +172,32 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
>  	unsigned long smap = (cpl - 3) & (rflags & X86_EFLAGS_AC);
>  	int index = (pfec >> 1) +
>  		    (smap >> (X86_EFLAGS_AC_BIT - PFERR_RSVD_BIT + 1));
> +	bool fault = (mmu->permissions[index] >> pte_access) & 1;
>  
> -	WARN_ON(pfec & PFERR_RSVD_MASK);
> -
> +	WARN_ON(pfec & (PFERR_PK_MASK | PFERR_RSVD_MASK));
>  	pfec |= PFERR_PRESENT_MASK;
> -	return -((mmu->permissions[index] >> pte_access) & 1) & pfec;
> +
> +	if (unlikely(mmu->pkru_mask)) {
> +		u32 pkru_bits, offset;
> +
> +		/*
> +		* PKRU defines 32 bits, there are 16 domains and 2
> +		* attribute bits per domain in pkru, pkey is the
> +		* index to a defined domain, so the value of
> +		* pkey * 2 is offset of a defined domain.
> +		*/
> +		pkru_bits = (kvm_read_pkru(vcpu) >> (pte_pkey * 2)) & 3;
> +
> +		/* clear present bit, replace PFEC.RSVD with ACC_USER_MASK. */
> +		offset = pfec - 1 +
> +			((pte_access & PT_USER_MASK) << (PFERR_RSVD_BIT - PT_USER_SHIFT));
> +
> +		pkru_bits &= mmu->pkru_mask >> offset;
> +		pfec |= -pkru_bits & PFERR_PK_MASK;
> +		fault |= (pkru_bits != 0);
> +	}
> +
> +	return -(uint32_t)fault & pfec;
>  }
>  
>  void kvm_mmu_invalidate_zap_all_pages(struct kvm *kvm);
> --
> To unsubscribe from this list: send the line "unsubscribe kvm" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html


  reply	other threads:[~2016-03-21 12:41 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-21 10:05 [PATCH V5 0/9] KVM, pkeys: add memory protection-key support Huaitong Han
2016-03-21 10:05 ` [PATCH V5 1/9] KVM, pkeys: disable pkeys for guests in non-paging mode Huaitong Han
2016-03-21 10:05 ` [PATCH V5 2/9] KVM, pkeys: add pkeys support for xsave state Huaitong Han
2016-03-21 10:05 ` [PATCH V5 3/9] x86: pkey: introduce write_pkru() for KVM Huaitong Han
2016-03-21 10:05 ` [PATCH V5 4/9] KVM, pkeys: save/restore PKRU when guest/host switches Huaitong Han
2016-03-21 10:28   ` Paolo Bonzini
2016-03-21 10:37     ` Han, Huaitong
2016-03-21 10:05 ` [PATCH V5 5/9] KVM, pkeys: introduce pkru_mask to cache conditions Huaitong Han
2016-03-21 17:43   ` Paolo Bonzini
2016-03-21 10:05 ` [PATCH V5 6/9] KVM, pkeys: add pkeys support for permission_fault logic Huaitong Han
2016-03-21 10:55   ` Paolo Bonzini
2016-03-21 12:41     ` Han, Huaitong [this message]
2016-03-21 10:05 ` [PATCH V5 7/9] KVM, pkeys: expose CPUID/CR4 to guest Huaitong Han
2016-03-21 11:36   ` Paolo Bonzini
2016-03-21 11:56     ` Han, Huaitong
2016-03-21 12:08       ` Paolo Bonzini
2016-03-21 10:05 ` [PATCH V5 8/9] KVM, pkeys: disable PKU feature without ept Huaitong Han
2016-03-21 11:01   ` Paolo Bonzini
2016-03-21 10:06 ` [PATCH V5 9/9] Revert "KVM: MMU: precompute page fault error code" Huaitong Han

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=1458564105.3453.4.camel@intel.com \
    --to=huaitong.han@intel.com \
    --cc=gleb@kernel.org \
    --cc=guangrong.xiao@linux.intel.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox