diff mbox series

[RFC,v4,14/36] i386/tdx: Implement user specified tsc frequency

Message ID 20220512031803.3315890-15-xiaoyao.li@intel.com (mailing list archive)
State New, archived
Headers show
Series TDX QEMU support | expand

Commit Message

Xiaoyao Li May 12, 2022, 3:17 a.m. UTC
Reuse "-cpu,tsc-frequency=" to get user wanted tsc frequency and pass it
to KVM_TDX_INIT_VM.

Besides, sanity check the tsc frequency to be in the legal range and
legal granularity (required by TDX module).

Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
---
 target/i386/kvm/kvm.c |  8 ++++++++
 target/i386/kvm/tdx.c | 18 ++++++++++++++++++
 2 files changed, 26 insertions(+)

Comments

Isaku Yamahata May 12, 2022, 6:04 p.m. UTC | #1
On Thu, May 12, 2022 at 11:17:41AM +0800,
Xiaoyao Li <xiaoyao.li@intel.com> wrote:

> Reuse "-cpu,tsc-frequency=" to get user wanted tsc frequency and pass it
> to KVM_TDX_INIT_VM.
> 
> Besides, sanity check the tsc frequency to be in the legal range and
> legal granularity (required by TDX module).

Just to make it sure.
You didn't use VM-scoped KVM_SET_TSC_KHZ because KVM side patch is still in
kvm/queue?  Once the patch lands, we should use it.

Thanks,

> 
> Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
> ---
>  target/i386/kvm/kvm.c |  8 ++++++++
>  target/i386/kvm/tdx.c | 18 ++++++++++++++++++
>  2 files changed, 26 insertions(+)
> 
> diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
> index f2d7c3cf59ac..c51125ab200f 100644
> --- a/target/i386/kvm/kvm.c
> +++ b/target/i386/kvm/kvm.c
> @@ -818,6 +818,14 @@ static int kvm_arch_set_tsc_khz(CPUState *cs)
>      int r, cur_freq;
>      bool set_ioctl = false;
>  
> +    /*
> +     * TD guest's TSC is immutable, it cannot be set/changed via
> +     * KVM_SET_TSC_KHZ, but only be initialized via KVM_TDX_INIT_VM
> +     */
> +    if (is_tdx_vm()) {
> +        return 0;
> +    }
> +
>      if (!env->tsc_khz) {
>          return 0;
>      }
> diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c
> index 9f2cdf640b5c..622efc409438 100644
> --- a/target/i386/kvm/tdx.c
> +++ b/target/i386/kvm/tdx.c
> @@ -35,6 +35,9 @@
>  #define TDX_TD_ATTRIBUTES_PKS               BIT_ULL(30)
>  #define TDX_TD_ATTRIBUTES_PERFMON           BIT_ULL(63)
>  
> +#define TDX_MIN_TSC_FREQUENCY_KHZ   (100 * 1000)
> +#define TDX_MAX_TSC_FREQUENCY_KHZ   (10 * 1000 * 1000)
> +
>  static TdxGuest *tdx_guest;
>  
>  /* It's valid after kvm_confidential_guest_init()->kvm_tdx_init() */
> @@ -211,6 +214,20 @@ int tdx_pre_create_vcpu(CPUState *cpu)
>          goto out;
>      }
>  
> +    r = -EINVAL;
> +    if (env->tsc_khz && (env->tsc_khz < TDX_MIN_TSC_FREQUENCY_KHZ ||
> +                         env->tsc_khz > TDX_MAX_TSC_FREQUENCY_KHZ)) {
> +        error_report("Invalid TSC %ld KHz, must specify cpu_frequency between [%d, %d] kHz",
> +                      env->tsc_khz, TDX_MIN_TSC_FREQUENCY_KHZ,
> +                      TDX_MAX_TSC_FREQUENCY_KHZ);
> +        goto out;
> +    }
> +
> +    if (env->tsc_khz % (25 * 1000)) {
> +        error_report("Invalid TSC %ld KHz, it must be multiple of 25MHz", env->tsc_khz);
> +        goto out;
> +    }
> +
>      r = setup_td_guest_attributes(x86cpu);
>      if (r) {
>          goto out;
> @@ -221,6 +238,7 @@ int tdx_pre_create_vcpu(CPUState *cpu)
>  
>      init_vm.attributes = tdx_guest->attributes;
>      init_vm.max_vcpus = ms->smp.cpus;
> +    init_vm.tsc_khz = env->tsc_khz;
>  
>      r = tdx_vm_ioctl(KVM_TDX_INIT_VM, 0, &init_vm);
>      if (r < 0) {
> -- 
> 2.27.0
> 
>
Xiaoyao Li May 13, 2022, 12:46 a.m. UTC | #2
On 5/13/2022 2:04 AM, Isaku Yamahata wrote:
> On Thu, May 12, 2022 at 11:17:41AM +0800,
> Xiaoyao Li <xiaoyao.li@intel.com> wrote:
> 
>> Reuse "-cpu,tsc-frequency=" to get user wanted tsc frequency and pass it
>> to KVM_TDX_INIT_VM.
>>
>> Besides, sanity check the tsc frequency to be in the legal range and
>> legal granularity (required by TDX module).
> 
> Just to make it sure.
> You didn't use VM-scoped KVM_SET_TSC_KHZ because KVM side patch is still in
> kvm/queue?  Once the patch lands, we should use it.

I didn't use VM-scoped KVM_SET_TSC_KHZ is because

1) corresponding TDX KVM v6 series still provides tsc_khz in
    struct kvm_tdx_init_vm

2) Use KVM_SET_TSC_KHZ to set VM-scoped TSC seems possible to be applied 
to all VMs, not limited to TDs. It doesn't look like a small task.

I need more time to evaluate the efforts.

> Thanks,
> 
>>
>> Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
>> ---
>>   target/i386/kvm/kvm.c |  8 ++++++++
>>   target/i386/kvm/tdx.c | 18 ++++++++++++++++++
>>   2 files changed, 26 insertions(+)
>>
>> diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
>> index f2d7c3cf59ac..c51125ab200f 100644
>> --- a/target/i386/kvm/kvm.c
>> +++ b/target/i386/kvm/kvm.c
>> @@ -818,6 +818,14 @@ static int kvm_arch_set_tsc_khz(CPUState *cs)
>>       int r, cur_freq;
>>       bool set_ioctl = false;
>>   
>> +    /*
>> +     * TD guest's TSC is immutable, it cannot be set/changed via
>> +     * KVM_SET_TSC_KHZ, but only be initialized via KVM_TDX_INIT_VM
>> +     */
>> +    if (is_tdx_vm()) {
>> +        return 0;
>> +    }
>> +
>>       if (!env->tsc_khz) {
>>           return 0;
>>       }
>> diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c
>> index 9f2cdf640b5c..622efc409438 100644
>> --- a/target/i386/kvm/tdx.c
>> +++ b/target/i386/kvm/tdx.c
>> @@ -35,6 +35,9 @@
>>   #define TDX_TD_ATTRIBUTES_PKS               BIT_ULL(30)
>>   #define TDX_TD_ATTRIBUTES_PERFMON           BIT_ULL(63)
>>   
>> +#define TDX_MIN_TSC_FREQUENCY_KHZ   (100 * 1000)
>> +#define TDX_MAX_TSC_FREQUENCY_KHZ   (10 * 1000 * 1000)
>> +
>>   static TdxGuest *tdx_guest;
>>   
>>   /* It's valid after kvm_confidential_guest_init()->kvm_tdx_init() */
>> @@ -211,6 +214,20 @@ int tdx_pre_create_vcpu(CPUState *cpu)
>>           goto out;
>>       }
>>   
>> +    r = -EINVAL;
>> +    if (env->tsc_khz && (env->tsc_khz < TDX_MIN_TSC_FREQUENCY_KHZ ||
>> +                         env->tsc_khz > TDX_MAX_TSC_FREQUENCY_KHZ)) {
>> +        error_report("Invalid TSC %ld KHz, must specify cpu_frequency between [%d, %d] kHz",
>> +                      env->tsc_khz, TDX_MIN_TSC_FREQUENCY_KHZ,
>> +                      TDX_MAX_TSC_FREQUENCY_KHZ);
>> +        goto out;
>> +    }
>> +
>> +    if (env->tsc_khz % (25 * 1000)) {
>> +        error_report("Invalid TSC %ld KHz, it must be multiple of 25MHz", env->tsc_khz);
>> +        goto out;
>> +    }
>> +
>>       r = setup_td_guest_attributes(x86cpu);
>>       if (r) {
>>           goto out;
>> @@ -221,6 +238,7 @@ int tdx_pre_create_vcpu(CPUState *cpu)
>>   
>>       init_vm.attributes = tdx_guest->attributes;
>>       init_vm.max_vcpus = ms->smp.cpus;
>> +    init_vm.tsc_khz = env->tsc_khz;
>>   
>>       r = tdx_vm_ioctl(KVM_TDX_INIT_VM, 0, &init_vm);
>>       if (r < 0) {
>> -- 
>> 2.27.0
>>
>>
>
Gerd Hoffmann May 23, 2022, 9:43 a.m. UTC | #3
On Thu, May 12, 2022 at 11:17:41AM +0800, Xiaoyao Li wrote:
> Reuse "-cpu,tsc-frequency=" to get user wanted tsc frequency and pass it
> to KVM_TDX_INIT_VM.
> 
> Besides, sanity check the tsc frequency to be in the legal range and
> legal granularity (required by TDX module).

Acked-by: Gerd Hoffmann <kraxel@redhat.com>
diff mbox series

Patch

diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index f2d7c3cf59ac..c51125ab200f 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -818,6 +818,14 @@  static int kvm_arch_set_tsc_khz(CPUState *cs)
     int r, cur_freq;
     bool set_ioctl = false;
 
+    /*
+     * TD guest's TSC is immutable, it cannot be set/changed via
+     * KVM_SET_TSC_KHZ, but only be initialized via KVM_TDX_INIT_VM
+     */
+    if (is_tdx_vm()) {
+        return 0;
+    }
+
     if (!env->tsc_khz) {
         return 0;
     }
diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c
index 9f2cdf640b5c..622efc409438 100644
--- a/target/i386/kvm/tdx.c
+++ b/target/i386/kvm/tdx.c
@@ -35,6 +35,9 @@ 
 #define TDX_TD_ATTRIBUTES_PKS               BIT_ULL(30)
 #define TDX_TD_ATTRIBUTES_PERFMON           BIT_ULL(63)
 
+#define TDX_MIN_TSC_FREQUENCY_KHZ   (100 * 1000)
+#define TDX_MAX_TSC_FREQUENCY_KHZ   (10 * 1000 * 1000)
+
 static TdxGuest *tdx_guest;
 
 /* It's valid after kvm_confidential_guest_init()->kvm_tdx_init() */
@@ -211,6 +214,20 @@  int tdx_pre_create_vcpu(CPUState *cpu)
         goto out;
     }
 
+    r = -EINVAL;
+    if (env->tsc_khz && (env->tsc_khz < TDX_MIN_TSC_FREQUENCY_KHZ ||
+                         env->tsc_khz > TDX_MAX_TSC_FREQUENCY_KHZ)) {
+        error_report("Invalid TSC %ld KHz, must specify cpu_frequency between [%d, %d] kHz",
+                      env->tsc_khz, TDX_MIN_TSC_FREQUENCY_KHZ,
+                      TDX_MAX_TSC_FREQUENCY_KHZ);
+        goto out;
+    }
+
+    if (env->tsc_khz % (25 * 1000)) {
+        error_report("Invalid TSC %ld KHz, it must be multiple of 25MHz", env->tsc_khz);
+        goto out;
+    }
+
     r = setup_td_guest_attributes(x86cpu);
     if (r) {
         goto out;
@@ -221,6 +238,7 @@  int tdx_pre_create_vcpu(CPUState *cpu)
 
     init_vm.attributes = tdx_guest->attributes;
     init_vm.max_vcpus = ms->smp.cpus;
+    init_vm.tsc_khz = env->tsc_khz;
 
     r = tdx_vm_ioctl(KVM_TDX_INIT_VM, 0, &init_vm);
     if (r < 0) {