diff mbox

[v2,04/22] KVM: x86: introduce vcpu_gva_to_gpa to cleanup the code

Message ID 4E01FC5B.9040809@cn.fujitsu.com (mailing list archive)
State New, archived
Headers show

Commit Message

Xiao Guangrong June 22, 2011, 2:29 p.m. UTC
Introduce vcpu_gva_to_gpa to translate the gva to gpa, we can use it
to cleanup the code between read emulation and write emulation

Signed-off-by: Xiao Guangrong <xiaoguangrong@cn.fujitsu.com>
---
 arch/x86/kvm/x86.c |   38 +++++++++++++++++++++++++++++---------
 1 files changed, 29 insertions(+), 9 deletions(-)

Comments

Avi Kivity June 29, 2011, 8:24 a.m. UTC | #1
On 06/22/2011 05:29 PM, Xiao Guangrong wrote:
> Introduce vcpu_gva_to_gpa to translate the gva to gpa, we can use it
> to cleanup the code between read emulation and write emulation
>
> Signed-off-by: Xiao Guangrong<xiaoguangrong@cn.fujitsu.com>
> ---
>   arch/x86/kvm/x86.c |   38 +++++++++++++++++++++++++++++---------
>   1 files changed, 29 insertions(+), 9 deletions(-)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index eb27be4..c29ef96 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -3944,6 +3944,27 @@ out:
>   }
>   EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
>
> +static int vcpu_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
> +			   gpa_t *gpa, struct x86_exception *exception,
> +			   bool write)
> +{
> +	u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
> +
> +	if (write)
> +		access |= PFERR_WRITE_MASK;

Needs fetch as well so NX/SMEP can work.

> +
> +	*gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Xiao Guangrong June 29, 2011, 10:56 a.m. UTC | #2
On 06/29/2011 04:24 PM, Avi Kivity wrote:

>> +static int vcpu_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
>> +               gpa_t *gpa, struct x86_exception *exception,
>> +               bool write)
>> +{
>> +    u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
>> +
>> +    if (write)
>> +        access |= PFERR_WRITE_MASK;
> 
> Needs fetch as well so NX/SMEP can work.
> 

This function is only used by read/write emulator, execute permission is
not needed for read/write, no?
--
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
Avi Kivity June 29, 2011, 11:09 a.m. UTC | #3
On 06/29/2011 01:56 PM, Xiao Guangrong wrote:
> On 06/29/2011 04:24 PM, Avi Kivity wrote:
>
> >>  +static int vcpu_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
> >>  +               gpa_t *gpa, struct x86_exception *exception,
> >>  +               bool write)
> >>  +{
> >>  +    u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
> >>  +
> >>  +    if (write)
> >>  +        access |= PFERR_WRITE_MASK;
> >
> >  Needs fetch as well so NX/SMEP can work.
> >
>
> This function is only used by read/write emulator, execute permission is
> not needed for read/write, no?

It's not good to have a function which only implements the functionality 
partially.  It can later be misused.

You can pass the page-fault-error-code instead of the write parameter, I 
think it will be simpler.
Xiao Guangrong June 29, 2011, 11:26 a.m. UTC | #4
On 06/29/2011 07:09 PM, Avi Kivity wrote:
> On 06/29/2011 01:56 PM, Xiao Guangrong wrote:
>> On 06/29/2011 04:24 PM, Avi Kivity wrote:
>>
>> >>  +static int vcpu_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
>> >>  +               gpa_t *gpa, struct x86_exception *exception,
>> >>  +               bool write)
>> >>  +{
>> >>  +    u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
>> >>  +
>> >>  +    if (write)
>> >>  +        access |= PFERR_WRITE_MASK;
>> >
>> >  Needs fetch as well so NX/SMEP can work.
>> >
>>
>> This function is only used by read/write emulator, execute permission is
>> not needed for read/write, no?
> 
> It's not good to have a function which only implements the functionality partially.  It can later be misused.
> 
> You can pass the page-fault-error-code instead of the write parameter, I think it will be simpler.
> 

Actually, we will get the cache mmio info in this function, i think it is pure waste for other
access execpt mmio, what about change the function name to vcpu_gva_to_gpa_mmio?
--
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
Avi Kivity June 29, 2011, 11:26 a.m. UTC | #5
On 06/29/2011 02:26 PM, Xiao Guangrong wrote:
> On 06/29/2011 07:09 PM, Avi Kivity wrote:
> >  On 06/29/2011 01:56 PM, Xiao Guangrong wrote:
> >>  On 06/29/2011 04:24 PM, Avi Kivity wrote:
> >>
> >>  >>   +static int vcpu_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
> >>  >>   +               gpa_t *gpa, struct x86_exception *exception,
> >>  >>   +               bool write)
> >>  >>   +{
> >>  >>   +    u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
> >>  >>   +
> >>  >>   +    if (write)
> >>  >>   +        access |= PFERR_WRITE_MASK;
> >>  >
> >>  >   Needs fetch as well so NX/SMEP can work.
> >>  >
> >>
> >>  This function is only used by read/write emulator, execute permission is
> >>  not needed for read/write, no?
> >
> >  It's not good to have a function which only implements the functionality partially.  It can later be misused.
> >
> >  You can pass the page-fault-error-code instead of the write parameter, I think it will be simpler.
> >
>
> Actually, we will get the cache mmio info in this function, i think it is pure waste for other
> access execpt mmio, what about change the function name to vcpu_gva_to_gpa_mmio?

Not too happy, but ok.
Gleb Natapov June 29, 2011, 11:48 a.m. UTC | #6
On Wed, Jun 29, 2011 at 02:26:14PM +0300, Avi Kivity wrote:
> On 06/29/2011 02:26 PM, Xiao Guangrong wrote:
> >On 06/29/2011 07:09 PM, Avi Kivity wrote:
> >>  On 06/29/2011 01:56 PM, Xiao Guangrong wrote:
> >>>  On 06/29/2011 04:24 PM, Avi Kivity wrote:
> >>>
> >>>  >>   +static int vcpu_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
> >>>  >>   +               gpa_t *gpa, struct x86_exception *exception,
> >>>  >>   +               bool write)
> >>>  >>   +{
> >>>  >>   +    u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
> >>>  >>   +
> >>>  >>   +    if (write)
> >>>  >>   +        access |= PFERR_WRITE_MASK;
> >>>  >
> >>>  >   Needs fetch as well so NX/SMEP can work.
> >>>  >
> >>>
> >>>  This function is only used by read/write emulator, execute permission is
> >>>  not needed for read/write, no?
> >>
> >>  It's not good to have a function which only implements the functionality partially.  It can later be misused.
> >>
> >>  You can pass the page-fault-error-code instead of the write parameter, I think it will be simpler.
> >>
> >
> >Actually, we will get the cache mmio info in this function, i think it is pure waste for other
> >access execpt mmio, what about change the function name to vcpu_gva_to_gpa_mmio?
> 
> Not too happy, but ok.
> 
I do plan to add fetching from MMIO.

--
			Gleb.
--
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
diff mbox

Patch

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index eb27be4..c29ef96 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3944,6 +3944,27 @@  out:
 }
 EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
 
+static int vcpu_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
+			   gpa_t *gpa, struct x86_exception *exception,
+			   bool write)
+{
+	u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
+
+	if (write)
+		access |= PFERR_WRITE_MASK;
+
+	*gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
+
+	if (*gpa == UNMAPPED_GVA)
+		return -1;
+
+	/* For APIC access vmexit */
+	if ((*gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
+		return 1;
+
+	return 0;
+}
+
 static int emulator_read_emulated_onepage(unsigned long addr,
 					  void *val,
 					  unsigned int bytes,
@@ -3951,7 +3972,7 @@  static int emulator_read_emulated_onepage(unsigned long addr,
 					  struct kvm_vcpu *vcpu)
 {
 	gpa_t gpa;
-	int handled;
+	int handled, ret;
 
 	if (vcpu->mmio_read_completed) {
 		memcpy(val, vcpu->mmio_data, bytes);
@@ -3961,13 +3982,12 @@  static int emulator_read_emulated_onepage(unsigned long addr,
 		return X86EMUL_CONTINUE;
 	}
 
-	gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, exception);
+	ret = vcpu_gva_to_gpa(vcpu, addr, &gpa, exception, false);
 
-	if (gpa == UNMAPPED_GVA)
+	if (ret < 0)
 		return X86EMUL_PROPAGATE_FAULT;
 
-	/* For APIC access vmexit */
-	if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
+	if (ret)
 		goto mmio;
 
 	if (!kvm_read_guest(vcpu->kvm, gpa, val, bytes))
@@ -4043,15 +4063,15 @@  static int emulator_write_emulated_onepage(unsigned long addr,
 					   struct kvm_vcpu *vcpu)
 {
 	gpa_t                 gpa;
-	int handled;
+	int handled, ret;
 
-	gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, exception);
+	ret = vcpu_gva_to_gpa(vcpu, addr, &gpa, exception, true);
 
-	if (gpa == UNMAPPED_GVA)
+	if (ret < 0)
 		return X86EMUL_PROPAGATE_FAULT;
 
 	/* For APIC access vmexit */
-	if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
+	if (ret)
 		goto mmio;
 
 	if (emulator_write_phys(vcpu, gpa, val, bytes))