Re: [PATCH v3 5/5] KVM: selftests: KVM: SVM: Add Idle HLT intercept test

2024-05-31 Thread Chao Gao
On Thu, May 30, 2024 at 06:49:56PM +0530, Manali Shukla wrote: >Hi Chao, >Thank you for reviewing my patches. > >On 5/28/2024 1:16 PM, Chao Gao wrote: >>> +static void guest_code(void) >>> +{ >>> + uint32_t icr_val; >>> + int i; >

Re: [PATCH v3 5/5] KVM: selftests: KVM: SVM: Add Idle HLT intercept test

2024-05-28 Thread Chao Gao
>+static void guest_code(void) >+{ >+ uint32_t icr_val; >+ int i; >+ >+ xapic_enable(); >+ >+ icr_val = (APIC_DEST_SELF | APIC_INT_ASSERT | VINTR_VECTOR); >+ >+ for (i = 0; i < NUM_ITERATIONS; i++) { >+ cli(); >+ xapic_write_reg(APIC_ICR,

Re: [PATCH v2 12/25] KVM: VMX: Handle FRED event data

2024-05-10 Thread Chao Gao
On Fri, May 10, 2024 at 05:36:03PM +0800, Li, Xin3 wrote: >> >+ if (kvm_is_fred_enabled(vcpu)) { >> >+ u64 event_data = 0; >> >+ >> >+ if (is_debug(intr_info)) >> >+ /* >> >+*

Re: [PATCH v2 11/25] KVM: x86: Add kvm_is_fred_enabled()

2024-05-10 Thread Chao Gao
On Sat, May 11, 2024 at 09:24:12AM +0800, Li, Xin3 wrote: >> >+/* >> >+ * It's enough to check just CR4.FRED (X86_CR4_FRED) to tell if >> >+ * a vCPU is running with FRED enabled, because: >> >+ * 1) CR4.FRED can be set to 1 only _after_ IA32_EFER.LMA = 1. >> >+ * 2) To leave IA-32e mode, CR4.FRED

Re: [PATCH v2 15/25] KVM: VMX: Dump FRED context in dump_vmcs()

2024-04-30 Thread Chao Gao
On Thu, Feb 08, 2024 at 01:26:35AM +0800, Xin Li wrote: >Add FRED related VMCS fields to dump_vmcs() to have it dump FRED context. > >Signed-off-by: Xin Li >Tested-by: Shan Kang >--- > >Change since v1: >* Use kvm_cpu_cap_has() instead of cpu_feature_enabled() (Chao

Re: [PATCH v2 14/25] KVM: VMX: Disable FRED if FRED consistency checks fail

2024-04-30 Thread Chao Gao
On Thu, Feb 08, 2024 at 01:26:34AM +0800, Xin Li wrote: >Refuse to virtualize FRED if FRED consistency checks fail. After reading this, I realize some consistency checks are missing in setup_vmcs_config(). Actually Sean requested some infrastructure for vmcs_entry_exit_pairs to deal with

Re: [PATCH v2 13/25] KVM: VMX: Handle VMX nested exception for FRED

2024-04-30 Thread Chao Gao
s the correct event stack level is >chosen when a VM entry injects a nested exception. > >Signed-off-by: Xin Li >Tested-by: Shan Kang >--- > >Changes since v1: >* Set the nested flag when there is an original interrupt (Chao Gao). >--- > arch/x86/include/asm/kv

Re: [PATCH v2 12/25] KVM: VMX: Handle FRED event data

2024-04-29 Thread Chao Gao
>index ee61d2c25cb0..f622fb90a098 100644 >--- a/arch/x86/kvm/vmx/vmx.c >+++ b/arch/x86/kvm/vmx/vmx.c >@@ -1871,9 +1871,29 @@ static void vmx_inject_exception(struct kvm_vcpu *vcpu) >vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, >

Re: [PATCH v2 11/25] KVM: x86: Add kvm_is_fred_enabled()

2024-04-29 Thread Chao Gao
On Thu, Feb 08, 2024 at 01:26:31AM +0800, Xin Li wrote: >Add kvm_is_fred_enabled() to get if FRED is enabled on a vCPU. > >Signed-off-by: Xin Li >Tested-by: Shan Kang >--- > >Change since v1: >* Explain why it is ok to only check CR4.FRED (Chao Gao). >--- > arch

Re: [PATCH v2 10/25] KVM: VMX: Add support for FRED context save/restore

2024-04-29 Thread Chao Gao
On Thu, Feb 08, 2024 at 01:26:30AM +0800, Xin Li wrote: >Handle host initiated FRED MSR access requests to allow FRED context >to be set/get from user level. > The changelog isn't accurate because guest accesses are also handled by this patch, specifically in the "else" branch. >+

Re: [PATCH v2 09/25] KVM: VMX: Switch FRED RSP0 between host and guest

2024-04-19 Thread Chao Gao
t;Changes since v1: >* Don't use guest_cpuid_has() in vmx_prepare_switch_to_{host,guest}(), > which are called from IRQ-disabled context (Chao Gao). >* Reset msr_guest_fred_rsp0 in __vmx_vcpu_reset() (Chao Gao). >--- > arch/x86/kvm/vmx/vmx.c | 17 + > arch/x86/kvm/v

Re: [PATCH v2 08/25] KVM: VMX: Initialize VMCS FRED fields

2024-04-19 Thread Chao Gao
kvm_cpu_cap_has() instead of cpu_feature_enabled() to decouple > KVM's capability to virtualize a feature and host's enabling of a > feature (Chao Gao). >* Move guest FRED states init into __vmx_vcpu_reset() (Chao Gao). >--- > arch/x86/include/asm/vmx.h | 16 > arch/x8

Re: [PATCH v2 07/25] KVM: VMX: Set intercept for FRED MSRs

2024-04-19 Thread Chao Gao
On Wed, Feb 07, 2024 at 09:26:27AM -0800, Xin Li wrote: >Add FRED MSRs to the valid passthrough MSR list and set FRED MSRs intercept >based on FRED enumeration. > >Signed-off-by: Xin Li >Tested-by: Shan Kang Reviewed-by: Chao Gao two nits below. >--- > >Change since

Re: [PATCH v2 06/25] KVM: VMX: Defer enabling FRED MSRs save/load until after set CPUID

2024-04-19 Thread Chao Gao
event delivery immediately after VM entry >and exit. However it is not required to save/load FRED MSRs for >a non-FRED guest, which aren't supposed to access FRED MSRs. > >Signed-off-by: Xin Li >Tested-by: Shan Kang Reviewed-by: Chao Gao

Re: [PATCH v2 05/25] KVM: VMX: Initialize FRED VM entry/exit controls in vmcs_config

2024-04-19 Thread Chao Gao
-off-by: Xin Li >Tested-by: Shan Kang Reviewed-by: Chao Gao

Re: [PATCH v2 04/25] KVM: x86: Mark CR4.FRED as not reserved

2024-04-19 Thread Chao Gao
On Wed, Feb 07, 2024 at 09:26:24AM -0800, Xin Li wrote: >The CR4.FRED bit, i.e., CR4[32], is no longer a reserved bit when a guest >enumerates FRED, otherwise it is still a reserved bit. > >Signed-off-by: Xin Li >Tested-by: Shan Kang Reviewed-by: Chao Gao

Re: [PATCH v2 03/25] KVM: VMX: Add support for the secondary VM exit controls

2024-04-19 Thread Chao Gao
y: Xin Li >Tested-by: Shan Kang Reviewed-by: Chao Gao

Re: [PATCH v1 13/23] KVM: VMX: Handle VMX nested exception for FRED

2023-12-07 Thread Chao Gao
On Thu, Dec 07, 2023 at 06:09:46PM +0800, Li, Xin3 wrote: >> >> > Exiting-event identification can also have bit 13 set, indicating a >> >> > nested exception encountered and caused VM-exit. when reinjecting the >> >> > exception to guests, kvm needs to set the "nested" bit, right? I >> >> >

Re: [PATCH v1 13/23] KVM: VMX: Handle VMX nested exception for FRED

2023-12-07 Thread Chao Gao
On Wed, Dec 06, 2023 at 04:37:39PM +0800, Li, Xin3 wrote: >> Subject: RE: [PATCH v1 13/23] KVM: VMX: Handle VMX nested exception for FRED >> >> > >+ if (idt_vectoring_info & >> VECTORING_INFO_DELIVER_CODE_MASK) >> > >+ kvm_requeue_exception_e(vcpu, vector, >> >

Re: [PATCH v1 12/23] KVM: VMX: Handle FRED event data

2023-11-15 Thread Chao Gao
>> >> >+default: >> >> >+WARN_ON(event_data != 0); >> >> >> >> I am not sure if this WARN_ON() can be triggeded by nested VMX. It is >> >> legitimate for L1 VMM to inject any event w/ an event_data. >> >> >> >> FRED spec says: >> >> >> >>

Re: [PATCH v1 12/23] KVM: VMX: Handle FRED event data

2023-11-14 Thread Chao Gao
On Tue, Nov 14, 2023 at 12:34:02PM +0800, Li, Xin3 wrote: >> >+ else if (is_nm_fault(intr_info) && >> >+vcpu->arch.guest_fpu.fpstate->xfd) >> >> does this necessarily mean the #NM is caused by XFD? > >Then the event data should be 0. Or I missed

Re: [PATCH v1 11/23] KVM: x86: Add kvm_is_fred_enabled()

2023-11-14 Thread Chao Gao
On Tue, Nov 14, 2023 at 12:42:13PM +0800, Li, Xin3 wrote: >> >+ return cpu_feature_enabled(X86_FEATURE_FRED) && >> >+ kvm_is_cr4_bit_set(vcpu, X86_CR4_FRED); >> >> FRED is enabled when CR4.FRED = IA32_EFER.LMA = 1. Any reason to omit the >> check about long mode? > >It won' t allow

Re: [PATCH v1 09/23] KVM: VMX: Switch FRED RSP0 between host and guest

2023-11-13 Thread Chao Gao
>> >diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index >> >f8c02bd37069..328a3447f064 100644 >> >--- a/arch/x86/kvm/vmx/vmx.h >> >+++ b/arch/x86/kvm/vmx/vmx.h >> >@@ -276,6 +276,8 @@ struct vcpu_vmx { >> > #ifdef CONFIG_X86_64 >> >u64 msr_host_kernel_gs_base;

Re: [PATCH v1 13/23] KVM: VMX: Handle VMX nested exception for FRED

2023-11-13 Thread Chao Gao
> /* Require Write-Back (WB) memory type for VMCS accesses. */ >@@ -7313,11 +7328,12 @@ static void __vmx_complete_interrupts(struct kvm_vcpu >*vcpu, > } > } > >- if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { >-

Re: [PATCH v1 12/23] KVM: VMX: Handle FRED event data

2023-11-13 Thread Chao Gao
On Wed, Nov 08, 2023 at 10:29:52AM -0800, Xin Li wrote: >Set injected-event data when injecting a #PF, #DB, or #NM caused >by extended feature disable using FRED event delivery, and save >original-event data for being used as injected-event data. > >Unlike IDT using some extra CPU register as part

Re: [PATCH v1 11/23] KVM: x86: Add kvm_is_fred_enabled()

2023-11-12 Thread Chao Gao
On Wed, Nov 08, 2023 at 10:29:51AM -0800, Xin Li wrote: >Add kvm_is_fred_enabled() to get if FRED is enabled on a vCPU. > >Tested-by: Shan Kang >Signed-off-by: Xin Li >--- > arch/x86/kvm/kvm_cache_regs.h | 10 ++ > 1 file changed, 10 insertions(+) > >diff --git

Re: [PATCH v1 10/23] KVM: VMX: Add support for FRED context save/restore

2023-11-12 Thread Chao Gao
>diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c >index 2c924075f6f1..c5a55810647f 100644 >--- a/arch/x86/kvm/x86.c >+++ b/arch/x86/kvm/x86.c >@@ -1451,6 +1451,9 @@ static const u32 msrs_to_save_base[] = { > MSR_STAR, > #ifdef CONFIG_X86_64 > MSR_CSTAR, MSR_KERNEL_GS_BASE,

Re: [PATCH v1 09/23] KVM: VMX: Switch FRED RSP0 between host and guest

2023-11-12 Thread Chao Gao
On Wed, Nov 08, 2023 at 10:29:49AM -0800, Xin Li wrote: >Switch MSR_IA32_FRED_RSP0 between host and guest in >vmx_prepare_switch_to_{host,guest}(). > >MSR_IA32_FRED_RSP0 is used during ring 3 event delivery only, thus >KVM, running on ring 0, can run safely with guest FRED RSP0, i.e., >no need to

Re: [PATCH v1 08/23] KVM: VMX: Initialize VMCS FRED fields

2023-11-12 Thread Chao Gao
On Wed, Nov 08, 2023 at 10:29:48AM -0800, Xin Li wrote: >Initialize host VMCS FRED fields with host FRED MSRs' value and >guest VMCS FRED fields to 0. > >FRED CPU states are managed in 9 new FRED MSRs, as well as a few >existing CPU registers and MSRs, e.g., CR4.FRED. To support FRED >context

Re: [PATCH v1 07/23] KVM: VMX: Disable intercepting FRED MSRs

2023-11-09 Thread Chao Gao
On Wed, Nov 08, 2023 at 10:29:47AM -0800, Xin Li wrote: >Add FRED MSRs to the valid passthrough MSR list and disable intercepting >FRED MSRs only if FRED is enumerated after set CPUID. > >Tested-by: Shan Kang >Signed-off-by: Xin Li >--- > arch/x86/kvm/vmx/vmx.c | 13 + > 1 file

Re: [PATCH v1 06/23] KVM: VMX: Defer enabling FRED MSRs save/load until after set CPUID

2023-11-09 Thread Chao Gao
On Wed, Nov 08, 2023 at 10:29:46AM -0800, Xin Li wrote: >Clear FRED VM entry/exit controls when initializing a vCPU, and set >these controls only if FRED is enumerated after set CPUID. > >FRED VM entry/exit controls need to be set to establish context >sufficient to support FRED event delivery

Re: [PATCH v1 05/23] KVM: VMX: Initialize FRED VM entry/exit controls in vmcs_config

2023-11-09 Thread Chao Gao
On Wed, Nov 08, 2023 at 10:29:45AM -0800, Xin Li wrote: >Setup the global vmcs_config for FRED: >1) Add VM_ENTRY_LOAD_IA32_FRED to KVM_OPTIONAL_VMX_VM_ENTRY_CONTROLS to > have a FRED CPU load guest FRED MSRs from VMCS upon VM entry. >2) Add SECONDARY_VM_EXIT_SAVE_IA32_FRED to >