diff mbox series

[v1,4/6] KVM: x86: Add emulator helper for LASS violation check

Message ID 20230601142309.6307-5-guang.zeng@intel.com (mailing list archive)
State New, archived
Headers show
Series LASS KVM virtualization support | expand

Commit Message

Zeng Guang June 1, 2023, 2:23 p.m. UTC
When LASS is enabled, KVM need apply LASS violation check to instruction
emulations. Add helper for the usage of x86 emulator to perform LASS
protection.

Signed-off-by: Zeng Guang <guang.zeng@intel.com>
Tested-by: Xuelian Guo <xuelian.guo@intel.com>
---
 arch/x86/kvm/kvm_emulate.h |  1 +
 arch/x86/kvm/x86.c         | 12 ++++++++++++
 2 files changed, 13 insertions(+)

Comments

Sean Christopherson June 27, 2023, 6:28 p.m. UTC | #1
On Thu, Jun 01, 2023, Zeng Guang wrote:
> When LASS is enabled, KVM need apply LASS violation check to instruction
> emulations. Add helper for the usage of x86 emulator to perform LASS
> protection.
> 
> Signed-off-by: Zeng Guang <guang.zeng@intel.com>
> Tested-by: Xuelian Guo <xuelian.guo@intel.com>
> ---
>  arch/x86/kvm/kvm_emulate.h |  1 +
>  arch/x86/kvm/x86.c         | 12 ++++++++++++
>  2 files changed, 13 insertions(+)
> 
> diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h
> index f1439ab7c14b..fd1c2b22867e 100644
> --- a/arch/x86/kvm/kvm_emulate.h
> +++ b/arch/x86/kvm/kvm_emulate.h
> @@ -230,6 +230,7 @@ struct x86_emulate_ops {
>  	int (*leave_smm)(struct x86_emulate_ctxt *ctxt);
>  	void (*triple_fault)(struct x86_emulate_ctxt *ctxt);
>  	int (*set_xcr)(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr);
> +	bool (*check_lass)(struct x86_emulate_ctxt *ctxt, u64 access, u64 la, u32 flags);
>  };
>  
>  /* Type, address-of, and value of an instruction's operand. */
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index c0778ca39650..faf01fecc4ca 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -8287,6 +8287,17 @@ static void emulator_vm_bugged(struct x86_emulate_ctxt *ctxt)
>  		kvm_vm_bugged(kvm);
>  }
>  
> +static bool emulator_check_lass(struct x86_emulate_ctxt *ctxt,
> +				u64 access, u64 la, u32 flags)
> +{
> +	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
> +
> +	if (!is_long_mode(vcpu))
> +		return false;

Likely a moot point if we wrap ->gva_to_gpa(), but most this into the vendor
implementation.

And if we keep these emulator hooks, massage the patch ordering:

  1. Add plumbing to emulator to pass new flags
  2. Add kvm_x86_ops definition and invocation from emulator
  3. Implement and wire up vmx_is_lass_violation()

That way the changes to each area of KVM are better isolated.

> +	return static_call(kvm_x86_check_lass)(vcpu, access, la, flags);
> +}
> +
>  static const struct x86_emulate_ops emulate_ops = {
>  	.vm_bugged           = emulator_vm_bugged,
>  	.read_gpr            = emulator_read_gpr,
> @@ -8332,6 +8343,7 @@ static const struct x86_emulate_ops emulate_ops = {
>  	.leave_smm           = emulator_leave_smm,
>  	.triple_fault        = emulator_triple_fault,
>  	.set_xcr             = emulator_set_xcr,
> +	.check_lass          = emulator_check_lass,
>  };
>  
>  static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
> -- 
> 2.27.0
>
Zeng Guang June 29, 2023, 3:06 p.m. UTC | #2
On 6/28/2023 2:28 AM, Sean Christopherson wrote:
> On Thu, Jun 01, 2023, Zeng Guang wrote:
>> When LASS is enabled, KVM need apply LASS violation check to instruction
>> emulations. Add helper for the usage of x86 emulator to perform LASS
>> protection.
>>
>> Signed-off-by: Zeng Guang <guang.zeng@intel.com>
>> Tested-by: Xuelian Guo <xuelian.guo@intel.com>
>> ---
>>   arch/x86/kvm/kvm_emulate.h |  1 +
>>   arch/x86/kvm/x86.c         | 12 ++++++++++++
>>   2 files changed, 13 insertions(+)
>>
>> diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h
>> index f1439ab7c14b..fd1c2b22867e 100644
>> --- a/arch/x86/kvm/kvm_emulate.h
>> +++ b/arch/x86/kvm/kvm_emulate.h
>> @@ -230,6 +230,7 @@ struct x86_emulate_ops {
>>   	int (*leave_smm)(struct x86_emulate_ctxt *ctxt);
>>   	void (*triple_fault)(struct x86_emulate_ctxt *ctxt);
>>   	int (*set_xcr)(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr);
>> +	bool (*check_lass)(struct x86_emulate_ctxt *ctxt, u64 access, u64 la, u32 flags);
>>   };
>>   
>>   /* Type, address-of, and value of an instruction's operand. */
>> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
>> index c0778ca39650..faf01fecc4ca 100644
>> --- a/arch/x86/kvm/x86.c
>> +++ b/arch/x86/kvm/x86.c
>> @@ -8287,6 +8287,17 @@ static void emulator_vm_bugged(struct x86_emulate_ctxt *ctxt)
>>   		kvm_vm_bugged(kvm);
>>   }
>>   
>> +static bool emulator_check_lass(struct x86_emulate_ctxt *ctxt,
>> +				u64 access, u64 la, u32 flags)
>> +{
>> +	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
>> +
>> +	if (!is_long_mode(vcpu))
>> +		return false;
> Likely a moot point if we wrap ->gva_to_gpa(), but most this into the vendor
> implementation.
It's right way to move cpu mode check into is_lass_violation(). 
Previously I was struggling
to expect getting benefit from separate invocation. But it doesn't help 
much indeed.

>
> And if we keep these emulator hooks, massage the patch ordering:
>
>    1. Add plumbing to emulator to pass new flags
>    2. Add kvm_x86_ops definition and invocation from emulator
>    3. Implement and wire up vmx_is_lass_violation()
>
> That way the changes to each area of KVM are better isolated.
OK. Will reorganize the patch accordingly.
Thanks.
>> +	return static_call(kvm_x86_check_lass)(vcpu, access, la, flags);
>> +}
>> +
>>   static const struct x86_emulate_ops emulate_ops = {
>>   	.vm_bugged           = emulator_vm_bugged,
>>   	.read_gpr            = emulator_read_gpr,
>> @@ -8332,6 +8343,7 @@ static const struct x86_emulate_ops emulate_ops = {
>>   	.leave_smm           = emulator_leave_smm,
>>   	.triple_fault        = emulator_triple_fault,
>>   	.set_xcr             = emulator_set_xcr,
>> +	.check_lass          = emulator_check_lass,
>>   };
>>   
>>   static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
>> -- 
>> 2.27.0
>>
diff mbox series

Patch

diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h
index f1439ab7c14b..fd1c2b22867e 100644
--- a/arch/x86/kvm/kvm_emulate.h
+++ b/arch/x86/kvm/kvm_emulate.h
@@ -230,6 +230,7 @@  struct x86_emulate_ops {
 	int (*leave_smm)(struct x86_emulate_ctxt *ctxt);
 	void (*triple_fault)(struct x86_emulate_ctxt *ctxt);
 	int (*set_xcr)(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr);
+	bool (*check_lass)(struct x86_emulate_ctxt *ctxt, u64 access, u64 la, u32 flags);
 };
 
 /* Type, address-of, and value of an instruction's operand. */
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index c0778ca39650..faf01fecc4ca 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -8287,6 +8287,17 @@  static void emulator_vm_bugged(struct x86_emulate_ctxt *ctxt)
 		kvm_vm_bugged(kvm);
 }
 
+static bool emulator_check_lass(struct x86_emulate_ctxt *ctxt,
+				u64 access, u64 la, u32 flags)
+{
+	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
+
+	if (!is_long_mode(vcpu))
+		return false;
+
+	return static_call(kvm_x86_check_lass)(vcpu, access, la, flags);
+}
+
 static const struct x86_emulate_ops emulate_ops = {
 	.vm_bugged           = emulator_vm_bugged,
 	.read_gpr            = emulator_read_gpr,
@@ -8332,6 +8343,7 @@  static const struct x86_emulate_ops emulate_ops = {
 	.leave_smm           = emulator_leave_smm,
 	.triple_fault        = emulator_triple_fault,
 	.set_xcr             = emulator_set_xcr,
+	.check_lass          = emulator_check_lass,
 };
 
 static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)