[PATCH v8 14/55] i386/tdx: Set APIC bus rate to match with what TDX module enforces

Xiaoyao Li posted 55 patches 10 months, 2 weeks ago
[PATCH v8 14/55] i386/tdx: Set APIC bus rate to match with what TDX module enforces
Posted by Xiaoyao Li 10 months, 2 weeks ago
TDX advertises core crystal clock with cpuid[0x15] as 25MHz for TD
guests and it's unchangeable from VMM. As a result, TDX guest reads
the APIC timer as the same frequency, 25MHz.

While KVM's default emulated frequency for APIC bus is 1GHz, set the
APIC bus rate to match with TDX explicitly to ensure KVM provide correct
emulated APIC timer for TD guest.

Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
---
Changes in v6:
 - new patch;
---
 target/i386/kvm/tdx.c | 13 +++++++++++++
 target/i386/kvm/tdx.h |  3 +++
 2 files changed, 16 insertions(+)

diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c
index 77ddb2655c53..1a99b677686e 100644
--- a/target/i386/kvm/tdx.c
+++ b/target/i386/kvm/tdx.c
@@ -253,6 +253,19 @@ int tdx_pre_create_vcpu(CPUState *cpu, Error **errp)
     init_vm = g_malloc0(sizeof(struct kvm_tdx_init_vm) +
                         sizeof(struct kvm_cpuid_entry2) * KVM_MAX_CPUID_ENTRIES);
 
+    if (!kvm_check_extension(kvm_state, KVM_CAP_X86_APIC_BUS_CYCLES_NS)) {
+        error_setg(errp, "KVM doesn't support KVM_CAP_X86_APIC_BUS_CYCLES_NS");
+        return -EOPNOTSUPP;
+    }
+
+    r = kvm_vm_enable_cap(kvm_state, KVM_CAP_X86_APIC_BUS_CYCLES_NS,
+                          0, TDX_APIC_BUS_CYCLES_NS);
+    if (r < 0) {
+        error_setg_errno(errp, -r,
+                         "Unable to set core crystal clock frequency to 25MHz");
+        return r;
+    }
+
     if (tdx_guest->mrconfigid) {
         g_autofree uint8_t *data = qbase64_decode(tdx_guest->mrconfigid,
                               strlen(tdx_guest->mrconfigid), &data_len, errp);
diff --git a/target/i386/kvm/tdx.h b/target/i386/kvm/tdx.h
index e472b11fb0dd..d39e733d9fcc 100644
--- a/target/i386/kvm/tdx.h
+++ b/target/i386/kvm/tdx.h
@@ -16,6 +16,9 @@ typedef struct TdxGuestClass {
     X86ConfidentialGuestClass parent_class;
 } TdxGuestClass;
 
+/* TDX requires bus frequency 25MHz */
+#define TDX_APIC_BUS_CYCLES_NS 40
+
 typedef struct TdxGuest {
     X86ConfidentialGuest parent_obj;
 
-- 
2.34.1
Re: [PATCH v8 14/55] i386/tdx: Set APIC bus rate to match with what TDX module enforces
Posted by Zhao Liu 9 months, 3 weeks ago
On Tue, Apr 01, 2025 at 09:01:24AM -0400, Xiaoyao Li wrote:
> Date: Tue,  1 Apr 2025 09:01:24 -0400
> From: Xiaoyao Li <xiaoyao.li@intel.com>
> Subject: [PATCH v8 14/55] i386/tdx: Set APIC bus rate to match with what
>  TDX module enforces
> X-Mailer: git-send-email 2.34.1
> 
> TDX advertises core crystal clock with cpuid[0x15] as 25MHz for TD
> guests and it's unchangeable from VMM. As a result, TDX guest reads
> the APIC timer as the same frequency, 25MHz.
> 
> While KVM's default emulated frequency for APIC bus is 1GHz, set the
> APIC bus rate to match with TDX explicitly to ensure KVM provide correct
> emulated APIC timer for TD guest.
> 
> Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
> ---
> Changes in v6:
>  - new patch;
> ---
>  target/i386/kvm/tdx.c | 13 +++++++++++++
>  target/i386/kvm/tdx.h |  3 +++
>  2 files changed, 16 insertions(+)

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Re: [PATCH v8 14/55] i386/tdx: Set APIC bus rate to match with what TDX module enforces
Posted by Daniel P. Berrangé 10 months, 1 week ago
On Tue, Apr 01, 2025 at 09:01:24AM -0400, Xiaoyao Li wrote:
> TDX advertises core crystal clock with cpuid[0x15] as 25MHz for TD
> guests and it's unchangeable from VMM. As a result, TDX guest reads
> the APIC timer as the same frequency, 25MHz.

Did you mean    s/as/at/  ?

> While KVM's default emulated frequency for APIC bus is 1GHz, set the
> APIC bus rate to match with TDX explicitly to ensure KVM provide correct
> emulated APIC timer for TD guest.
> 
> Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
> ---
> Changes in v6:
>  - new patch;
> ---
>  target/i386/kvm/tdx.c | 13 +++++++++++++
>  target/i386/kvm/tdx.h |  3 +++
>  2 files changed, 16 insertions(+)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


Re: [PATCH v8 14/55] i386/tdx: Set APIC bus rate to match with what TDX module enforces
Posted by Xiaoyao Li 10 months, 1 week ago
On 4/2/2025 7:56 PM, Daniel P. Berrangé wrote:
> On Tue, Apr 01, 2025 at 09:01:24AM -0400, Xiaoyao Li wrote:
>> TDX advertises core crystal clock with cpuid[0x15] as 25MHz for TD
>> guests and it's unchangeable from VMM. As a result, TDX guest reads
>> the APIC timer as the same frequency, 25MHz.
> 
> Did you mean    s/as/at/  ?

yes, thanks for catching it!

>> While KVM's default emulated frequency for APIC bus is 1GHz, set the
>> APIC bus rate to match with TDX explicitly to ensure KVM provide correct
>> emulated APIC timer for TD guest.
>>
>> Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
>> ---
>> Changes in v6:
>>   - new patch;
>> ---
>>   target/i386/kvm/tdx.c | 13 +++++++++++++
>>   target/i386/kvm/tdx.h |  3 +++
>>   2 files changed, 16 insertions(+)
> 
> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
> 
> 
> With regards,
> Daniel