xref: /qemu/target/i386/kvm/tdx.c (revision 0dd5fe5ebeabefc7b3d7f043991b1edfe6b8eda9)
1756e12e7SXiaoyao Li /*
2756e12e7SXiaoyao Li  * QEMU TDX support
3756e12e7SXiaoyao Li  *
4756e12e7SXiaoyao Li  * Copyright (c) 2025 Intel Corporation
5756e12e7SXiaoyao Li  *
6756e12e7SXiaoyao Li  * Author:
7756e12e7SXiaoyao Li  *      Xiaoyao Li <xiaoyao.li@intel.com>
8756e12e7SXiaoyao Li  *
9756e12e7SXiaoyao Li  * SPDX-License-Identifier: GPL-2.0-or-later
10756e12e7SXiaoyao Li  */
11756e12e7SXiaoyao Li 
12756e12e7SXiaoyao Li #include "qemu/osdep.h"
138eddedc3SXiaoyao Li #include "qemu/error-report.h"
14d05a0858SIsaku Yamahata #include "qemu/base64.h"
158eddedc3SXiaoyao Li #include "qapi/error.h"
16756e12e7SXiaoyao Li #include "qom/object_interfaces.h"
17d05a0858SIsaku Yamahata #include "crypto/hash.h"
18756e12e7SXiaoyao Li 
19631a2ac5SXiaoyao Li #include "hw/i386/x86.h"
20b455880eSXiaoyao Li #include "kvm_i386.h"
21756e12e7SXiaoyao Li #include "tdx.h"
22756e12e7SXiaoyao Li 
230e73b843SXiaoyao Li #define TDX_MIN_TSC_FREQUENCY_KHZ   (100 * 1000)
240e73b843SXiaoyao Li #define TDX_MAX_TSC_FREQUENCY_KHZ   (10 * 1000 * 1000)
250e73b843SXiaoyao Li 
2653b6f406SXiaoyao Li #define TDX_TD_ATTRIBUTES_DEBUG             BIT_ULL(0)
276016e297SXiaoyao Li #define TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE   BIT_ULL(28)
28bb3be394SXiaoyao Li #define TDX_TD_ATTRIBUTES_PKS               BIT_ULL(30)
29bb3be394SXiaoyao Li #define TDX_TD_ATTRIBUTES_PERFMON           BIT_ULL(63)
306016e297SXiaoyao Li 
3153b6f406SXiaoyao Li #define TDX_SUPPORTED_TD_ATTRS  (TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE |\
3253b6f406SXiaoyao Li                                  TDX_TD_ATTRIBUTES_PKS | \
3353b6f406SXiaoyao Li                                  TDX_TD_ATTRIBUTES_PERFMON)
3453b6f406SXiaoyao Li 
351619d0e4SXiaoyao Li static TdxGuest *tdx_guest;
361619d0e4SXiaoyao Li 
378eddedc3SXiaoyao Li static struct kvm_tdx_capabilities *tdx_caps;
388eddedc3SXiaoyao Li 
391619d0e4SXiaoyao Li /* Valid after kvm_arch_init()->confidential_guest_kvm_init()->tdx_kvm_init() */
401619d0e4SXiaoyao Li bool is_tdx_vm(void)
411619d0e4SXiaoyao Li {
421619d0e4SXiaoyao Li     return !!tdx_guest;
431619d0e4SXiaoyao Li }
441619d0e4SXiaoyao Li 
458eddedc3SXiaoyao Li enum tdx_ioctl_level {
468eddedc3SXiaoyao Li     TDX_VM_IOCTL,
478eddedc3SXiaoyao Li     TDX_VCPU_IOCTL,
488eddedc3SXiaoyao Li };
498eddedc3SXiaoyao Li 
508eddedc3SXiaoyao Li static int tdx_ioctl_internal(enum tdx_ioctl_level level, void *state,
518eddedc3SXiaoyao Li                               int cmd_id, __u32 flags, void *data,
528eddedc3SXiaoyao Li                               Error **errp)
53631a2ac5SXiaoyao Li {
548eddedc3SXiaoyao Li     struct kvm_tdx_cmd tdx_cmd = {};
558eddedc3SXiaoyao Li     int r;
568eddedc3SXiaoyao Li 
578eddedc3SXiaoyao Li     const char *tdx_ioctl_name[] = {
588eddedc3SXiaoyao Li         [KVM_TDX_CAPABILITIES] = "KVM_TDX_CAPABILITIES",
598eddedc3SXiaoyao Li         [KVM_TDX_INIT_VM] = "KVM_TDX_INIT_VM",
608eddedc3SXiaoyao Li         [KVM_TDX_INIT_VCPU] = "KVM_TDX_INIT_VCPU",
618eddedc3SXiaoyao Li         [KVM_TDX_INIT_MEM_REGION] = "KVM_TDX_INIT_MEM_REGION",
628eddedc3SXiaoyao Li         [KVM_TDX_FINALIZE_VM] = "KVM_TDX_FINALIZE_VM",
638eddedc3SXiaoyao Li         [KVM_TDX_GET_CPUID] = "KVM_TDX_GET_CPUID",
648eddedc3SXiaoyao Li     };
658eddedc3SXiaoyao Li 
668eddedc3SXiaoyao Li     tdx_cmd.id = cmd_id;
678eddedc3SXiaoyao Li     tdx_cmd.flags = flags;
688eddedc3SXiaoyao Li     tdx_cmd.data = (__u64)(unsigned long)data;
698eddedc3SXiaoyao Li 
708eddedc3SXiaoyao Li     switch (level) {
718eddedc3SXiaoyao Li     case TDX_VM_IOCTL:
728eddedc3SXiaoyao Li         r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd);
738eddedc3SXiaoyao Li         break;
748eddedc3SXiaoyao Li     case TDX_VCPU_IOCTL:
758eddedc3SXiaoyao Li         r = kvm_vcpu_ioctl(state, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd);
768eddedc3SXiaoyao Li         break;
778eddedc3SXiaoyao Li     default:
788eddedc3SXiaoyao Li         error_setg(errp, "Invalid tdx_ioctl_level %d", level);
798eddedc3SXiaoyao Li         return -EINVAL;
808eddedc3SXiaoyao Li     }
818eddedc3SXiaoyao Li 
828eddedc3SXiaoyao Li     if (r < 0) {
838eddedc3SXiaoyao Li         error_setg_errno(errp, -r, "TDX ioctl %s failed, hw_errors: 0x%llx",
848eddedc3SXiaoyao Li                          tdx_ioctl_name[cmd_id], tdx_cmd.hw_error);
858eddedc3SXiaoyao Li     }
868eddedc3SXiaoyao Li     return r;
878eddedc3SXiaoyao Li }
888eddedc3SXiaoyao Li 
898eddedc3SXiaoyao Li static inline int tdx_vm_ioctl(int cmd_id, __u32 flags, void *data,
908eddedc3SXiaoyao Li                                Error **errp)
918eddedc3SXiaoyao Li {
928eddedc3SXiaoyao Li     return tdx_ioctl_internal(TDX_VM_IOCTL, NULL, cmd_id, flags, data, errp);
938eddedc3SXiaoyao Li }
948eddedc3SXiaoyao Li 
958eddedc3SXiaoyao Li static inline int tdx_vcpu_ioctl(CPUState *cpu, int cmd_id, __u32 flags,
968eddedc3SXiaoyao Li                                  void *data, Error **errp)
978eddedc3SXiaoyao Li {
988eddedc3SXiaoyao Li     return  tdx_ioctl_internal(TDX_VCPU_IOCTL, cpu, cmd_id, flags, data, errp);
998eddedc3SXiaoyao Li }
1008eddedc3SXiaoyao Li 
1018eddedc3SXiaoyao Li static int get_tdx_capabilities(Error **errp)
1028eddedc3SXiaoyao Li {
1038eddedc3SXiaoyao Li     struct kvm_tdx_capabilities *caps;
1048eddedc3SXiaoyao Li     /* 1st generation of TDX reports 6 cpuid configs */
1058eddedc3SXiaoyao Li     int nr_cpuid_configs = 6;
1068eddedc3SXiaoyao Li     size_t size;
1078eddedc3SXiaoyao Li     int r;
1088eddedc3SXiaoyao Li 
1098eddedc3SXiaoyao Li     do {
1108eddedc3SXiaoyao Li         Error *local_err = NULL;
1118eddedc3SXiaoyao Li         size = sizeof(struct kvm_tdx_capabilities) +
1128eddedc3SXiaoyao Li                       nr_cpuid_configs * sizeof(struct kvm_cpuid_entry2);
1138eddedc3SXiaoyao Li         caps = g_malloc0(size);
1148eddedc3SXiaoyao Li         caps->cpuid.nent = nr_cpuid_configs;
1158eddedc3SXiaoyao Li 
1168eddedc3SXiaoyao Li         r = tdx_vm_ioctl(KVM_TDX_CAPABILITIES, 0, caps, &local_err);
1178eddedc3SXiaoyao Li         if (r == -E2BIG) {
1188eddedc3SXiaoyao Li             g_free(caps);
1198eddedc3SXiaoyao Li             nr_cpuid_configs *= 2;
1208eddedc3SXiaoyao Li             if (nr_cpuid_configs > KVM_MAX_CPUID_ENTRIES) {
1218eddedc3SXiaoyao Li                 error_report("KVM TDX seems broken that number of CPUID entries"
1228eddedc3SXiaoyao Li                              " in kvm_tdx_capabilities exceeds limit: %d",
1238eddedc3SXiaoyao Li                              KVM_MAX_CPUID_ENTRIES);
1248eddedc3SXiaoyao Li                 error_propagate(errp, local_err);
1258eddedc3SXiaoyao Li                 return r;
1268eddedc3SXiaoyao Li             }
1278eddedc3SXiaoyao Li             error_free(local_err);
1288eddedc3SXiaoyao Li         } else if (r < 0) {
1298eddedc3SXiaoyao Li             g_free(caps);
1308eddedc3SXiaoyao Li             error_propagate(errp, local_err);
1318eddedc3SXiaoyao Li             return r;
1328eddedc3SXiaoyao Li         }
1338eddedc3SXiaoyao Li     } while (r == -E2BIG);
1348eddedc3SXiaoyao Li 
1358eddedc3SXiaoyao Li     tdx_caps = caps;
136631a2ac5SXiaoyao Li 
137631a2ac5SXiaoyao Li     return 0;
138631a2ac5SXiaoyao Li }
139631a2ac5SXiaoyao Li 
140*0dd5fe5eSChao Peng void tdx_set_tdvf_region(MemoryRegion *tdvf_mr)
141*0dd5fe5eSChao Peng {
142*0dd5fe5eSChao Peng     assert(!tdx_guest->tdvf_mr);
143*0dd5fe5eSChao Peng     tdx_guest->tdvf_mr = tdvf_mr;
144*0dd5fe5eSChao Peng }
145*0dd5fe5eSChao Peng 
1468eddedc3SXiaoyao Li static int tdx_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
1478eddedc3SXiaoyao Li {
1481619d0e4SXiaoyao Li     TdxGuest *tdx = TDX_GUEST(cgs);
1498eddedc3SXiaoyao Li     int r = 0;
1508eddedc3SXiaoyao Li 
1518eddedc3SXiaoyao Li     kvm_mark_guest_state_protected();
1528eddedc3SXiaoyao Li 
1538eddedc3SXiaoyao Li     if (!tdx_caps) {
1548eddedc3SXiaoyao Li         r = get_tdx_capabilities(errp);
1551619d0e4SXiaoyao Li         if (r) {
1561619d0e4SXiaoyao Li             return r;
1571619d0e4SXiaoyao Li         }
1588eddedc3SXiaoyao Li     }
1598eddedc3SXiaoyao Li 
1601619d0e4SXiaoyao Li     tdx_guest = tdx;
1611619d0e4SXiaoyao Li     return 0;
1628eddedc3SXiaoyao Li }
1638eddedc3SXiaoyao Li 
164b455880eSXiaoyao Li static int tdx_kvm_type(X86ConfidentialGuest *cg)
165b455880eSXiaoyao Li {
166b455880eSXiaoyao Li     /* Do the object check */
167b455880eSXiaoyao Li     TDX_GUEST(cg);
168b455880eSXiaoyao Li 
169b455880eSXiaoyao Li     return KVM_X86_TDX_VM;
170b455880eSXiaoyao Li }
171b455880eSXiaoyao Li 
17253b6f406SXiaoyao Li static int tdx_validate_attributes(TdxGuest *tdx, Error **errp)
17353b6f406SXiaoyao Li {
17453b6f406SXiaoyao Li     if ((tdx->attributes & ~tdx_caps->supported_attrs)) {
17553b6f406SXiaoyao Li         error_setg(errp, "Invalid attributes 0x%lx for TDX VM "
17653b6f406SXiaoyao Li                    "(KVM supported: 0x%llx)", tdx->attributes,
17753b6f406SXiaoyao Li                    tdx_caps->supported_attrs);
17853b6f406SXiaoyao Li         return -1;
17953b6f406SXiaoyao Li     }
18053b6f406SXiaoyao Li 
18153b6f406SXiaoyao Li     if (tdx->attributes & ~TDX_SUPPORTED_TD_ATTRS) {
18253b6f406SXiaoyao Li         error_setg(errp, "Some QEMU unsupported TD attribute bits being "
18353b6f406SXiaoyao Li                     "requested: 0x%lx (QEMU supported: 0x%llx)",
18453b6f406SXiaoyao Li                     tdx->attributes, TDX_SUPPORTED_TD_ATTRS);
18553b6f406SXiaoyao Li         return -1;
18653b6f406SXiaoyao Li     }
18753b6f406SXiaoyao Li 
18853b6f406SXiaoyao Li     return 0;
18953b6f406SXiaoyao Li }
19053b6f406SXiaoyao Li 
19153b6f406SXiaoyao Li static int setup_td_guest_attributes(X86CPU *x86cpu, Error **errp)
192bb3be394SXiaoyao Li {
193bb3be394SXiaoyao Li     CPUX86State *env = &x86cpu->env;
194bb3be394SXiaoyao Li 
195bb3be394SXiaoyao Li     tdx_guest->attributes |= (env->features[FEAT_7_0_ECX] & CPUID_7_0_ECX_PKS) ?
196bb3be394SXiaoyao Li                              TDX_TD_ATTRIBUTES_PKS : 0;
197bb3be394SXiaoyao Li     tdx_guest->attributes |= x86cpu->enable_pmu ? TDX_TD_ATTRIBUTES_PERFMON : 0;
19853b6f406SXiaoyao Li 
19953b6f406SXiaoyao Li     return tdx_validate_attributes(tdx_guest, errp);
200bb3be394SXiaoyao Li }
201bb3be394SXiaoyao Li 
202f15898b0SXiaoyao Li static int setup_td_xfam(X86CPU *x86cpu, Error **errp)
203f15898b0SXiaoyao Li {
204f15898b0SXiaoyao Li     CPUX86State *env = &x86cpu->env;
205f15898b0SXiaoyao Li     uint64_t xfam;
206f15898b0SXiaoyao Li 
207f15898b0SXiaoyao Li     xfam = env->features[FEAT_XSAVE_XCR0_LO] |
208f15898b0SXiaoyao Li            env->features[FEAT_XSAVE_XCR0_HI] |
209f15898b0SXiaoyao Li            env->features[FEAT_XSAVE_XSS_LO] |
210f15898b0SXiaoyao Li            env->features[FEAT_XSAVE_XSS_HI];
211f15898b0SXiaoyao Li 
212f15898b0SXiaoyao Li     if (xfam & ~tdx_caps->supported_xfam) {
213f15898b0SXiaoyao Li         error_setg(errp, "Invalid XFAM 0x%lx for TDX VM (supported: 0x%llx))",
214f15898b0SXiaoyao Li                    xfam, tdx_caps->supported_xfam);
215f15898b0SXiaoyao Li         return -1;
216f15898b0SXiaoyao Li     }
217f15898b0SXiaoyao Li 
218f15898b0SXiaoyao Li     tdx_guest->xfam = xfam;
219f15898b0SXiaoyao Li     return 0;
220f15898b0SXiaoyao Li }
221f15898b0SXiaoyao Li 
222f15898b0SXiaoyao Li static void tdx_filter_cpuid(struct kvm_cpuid2 *cpuids)
223f15898b0SXiaoyao Li {
224f15898b0SXiaoyao Li     int i, dest_cnt = 0;
225f15898b0SXiaoyao Li     struct kvm_cpuid_entry2 *src, *dest, *conf;
226f15898b0SXiaoyao Li 
227f15898b0SXiaoyao Li     for (i = 0; i < cpuids->nent; i++) {
228f15898b0SXiaoyao Li         src = cpuids->entries + i;
229f15898b0SXiaoyao Li         conf = cpuid_find_entry(&tdx_caps->cpuid, src->function, src->index);
230f15898b0SXiaoyao Li         if (!conf) {
231f15898b0SXiaoyao Li             continue;
232f15898b0SXiaoyao Li         }
233f15898b0SXiaoyao Li         dest = cpuids->entries + dest_cnt;
234f15898b0SXiaoyao Li 
235f15898b0SXiaoyao Li         dest->function = src->function;
236f15898b0SXiaoyao Li         dest->index = src->index;
237f15898b0SXiaoyao Li         dest->flags = src->flags;
238f15898b0SXiaoyao Li         dest->eax = src->eax & conf->eax;
239f15898b0SXiaoyao Li         dest->ebx = src->ebx & conf->ebx;
240f15898b0SXiaoyao Li         dest->ecx = src->ecx & conf->ecx;
241f15898b0SXiaoyao Li         dest->edx = src->edx & conf->edx;
242f15898b0SXiaoyao Li 
243f15898b0SXiaoyao Li         dest_cnt++;
244f15898b0SXiaoyao Li     }
245f15898b0SXiaoyao Li     cpuids->nent = dest_cnt++;
246f15898b0SXiaoyao Li }
247f15898b0SXiaoyao Li 
248f15898b0SXiaoyao Li int tdx_pre_create_vcpu(CPUState *cpu, Error **errp)
249f15898b0SXiaoyao Li {
250f15898b0SXiaoyao Li     X86CPU *x86cpu = X86_CPU(cpu);
251f15898b0SXiaoyao Li     CPUX86State *env = &x86cpu->env;
252f15898b0SXiaoyao Li     g_autofree struct kvm_tdx_init_vm *init_vm = NULL;
253f15898b0SXiaoyao Li     Error *local_err = NULL;
254d05a0858SIsaku Yamahata     size_t data_len;
255f15898b0SXiaoyao Li     int retry = 10000;
256f15898b0SXiaoyao Li     int r = 0;
257f15898b0SXiaoyao Li 
258f15898b0SXiaoyao Li     QEMU_LOCK_GUARD(&tdx_guest->lock);
259f15898b0SXiaoyao Li     if (tdx_guest->initialized) {
260f15898b0SXiaoyao Li         return r;
261f15898b0SXiaoyao Li     }
262f15898b0SXiaoyao Li 
263f15898b0SXiaoyao Li     init_vm = g_malloc0(sizeof(struct kvm_tdx_init_vm) +
264f15898b0SXiaoyao Li                         sizeof(struct kvm_cpuid_entry2) * KVM_MAX_CPUID_ENTRIES);
265f15898b0SXiaoyao Li 
266d529a2acSXiaoyao Li     if (!kvm_check_extension(kvm_state, KVM_CAP_X86_APIC_BUS_CYCLES_NS)) {
267d529a2acSXiaoyao Li         error_setg(errp, "KVM doesn't support KVM_CAP_X86_APIC_BUS_CYCLES_NS");
268d529a2acSXiaoyao Li         return -EOPNOTSUPP;
269d529a2acSXiaoyao Li     }
270d529a2acSXiaoyao Li 
271d529a2acSXiaoyao Li     r = kvm_vm_enable_cap(kvm_state, KVM_CAP_X86_APIC_BUS_CYCLES_NS,
272d529a2acSXiaoyao Li                           0, TDX_APIC_BUS_CYCLES_NS);
273d529a2acSXiaoyao Li     if (r < 0) {
274d529a2acSXiaoyao Li         error_setg_errno(errp, -r,
275d529a2acSXiaoyao Li                          "Unable to set core crystal clock frequency to 25MHz");
276d529a2acSXiaoyao Li         return r;
277d529a2acSXiaoyao Li     }
278d529a2acSXiaoyao Li 
2790e73b843SXiaoyao Li     if (env->tsc_khz && (env->tsc_khz < TDX_MIN_TSC_FREQUENCY_KHZ ||
2800e73b843SXiaoyao Li                          env->tsc_khz > TDX_MAX_TSC_FREQUENCY_KHZ)) {
2810e73b843SXiaoyao Li         error_setg(errp, "Invalid TSC %ld KHz, must specify cpu_frequency "
2820e73b843SXiaoyao Li                          "between [%d, %d] kHz", env->tsc_khz,
2830e73b843SXiaoyao Li                          TDX_MIN_TSC_FREQUENCY_KHZ, TDX_MAX_TSC_FREQUENCY_KHZ);
2840e73b843SXiaoyao Li        return -EINVAL;
2850e73b843SXiaoyao Li     }
2860e73b843SXiaoyao Li 
2870e73b843SXiaoyao Li     if (env->tsc_khz % (25 * 1000)) {
2880e73b843SXiaoyao Li         error_setg(errp, "Invalid TSC %ld KHz, it must be multiple of 25MHz",
2890e73b843SXiaoyao Li                    env->tsc_khz);
2900e73b843SXiaoyao Li         return -EINVAL;
2910e73b843SXiaoyao Li     }
2920e73b843SXiaoyao Li 
2930e73b843SXiaoyao Li     /* it's safe even env->tsc_khz is 0. KVM uses host's tsc_khz in this case */
2940e73b843SXiaoyao Li     r = kvm_vm_ioctl(kvm_state, KVM_SET_TSC_KHZ, env->tsc_khz);
2950e73b843SXiaoyao Li     if (r < 0) {
2960e73b843SXiaoyao Li         error_setg_errno(errp, -r, "Unable to set TSC frequency to %ld kHz",
2970e73b843SXiaoyao Li                          env->tsc_khz);
2980e73b843SXiaoyao Li         return r;
2990e73b843SXiaoyao Li     }
3000e73b843SXiaoyao Li 
301d05a0858SIsaku Yamahata     if (tdx_guest->mrconfigid) {
302d05a0858SIsaku Yamahata         g_autofree uint8_t *data = qbase64_decode(tdx_guest->mrconfigid,
303d05a0858SIsaku Yamahata                               strlen(tdx_guest->mrconfigid), &data_len, errp);
304d05a0858SIsaku Yamahata         if (!data) {
305d05a0858SIsaku Yamahata             return -1;
306d05a0858SIsaku Yamahata         }
307d05a0858SIsaku Yamahata         if (data_len != QCRYPTO_HASH_DIGEST_LEN_SHA384) {
308d05a0858SIsaku Yamahata             error_setg(errp, "TDX: failed to decode mrconfigid");
309d05a0858SIsaku Yamahata             return -1;
310d05a0858SIsaku Yamahata         }
311d05a0858SIsaku Yamahata         memcpy(init_vm->mrconfigid, data, data_len);
312d05a0858SIsaku Yamahata     }
313d05a0858SIsaku Yamahata 
314d05a0858SIsaku Yamahata     if (tdx_guest->mrowner) {
315d05a0858SIsaku Yamahata         g_autofree uint8_t *data = qbase64_decode(tdx_guest->mrowner,
316d05a0858SIsaku Yamahata                               strlen(tdx_guest->mrowner), &data_len, errp);
317d05a0858SIsaku Yamahata         if (!data) {
318d05a0858SIsaku Yamahata             return -1;
319d05a0858SIsaku Yamahata         }
320d05a0858SIsaku Yamahata         if (data_len != QCRYPTO_HASH_DIGEST_LEN_SHA384) {
321d05a0858SIsaku Yamahata             error_setg(errp, "TDX: failed to decode mrowner");
322d05a0858SIsaku Yamahata             return -1;
323d05a0858SIsaku Yamahata         }
324d05a0858SIsaku Yamahata         memcpy(init_vm->mrowner, data, data_len);
325d05a0858SIsaku Yamahata     }
326d05a0858SIsaku Yamahata 
327d05a0858SIsaku Yamahata     if (tdx_guest->mrownerconfig) {
328d05a0858SIsaku Yamahata         g_autofree uint8_t *data = qbase64_decode(tdx_guest->mrownerconfig,
329d05a0858SIsaku Yamahata                             strlen(tdx_guest->mrownerconfig), &data_len, errp);
330d05a0858SIsaku Yamahata         if (!data) {
331d05a0858SIsaku Yamahata             return -1;
332d05a0858SIsaku Yamahata         }
333d05a0858SIsaku Yamahata         if (data_len != QCRYPTO_HASH_DIGEST_LEN_SHA384) {
334d05a0858SIsaku Yamahata             error_setg(errp, "TDX: failed to decode mrownerconfig");
335d05a0858SIsaku Yamahata             return -1;
336d05a0858SIsaku Yamahata         }
337d05a0858SIsaku Yamahata         memcpy(init_vm->mrownerconfig, data, data_len);
338d05a0858SIsaku Yamahata     }
339d05a0858SIsaku Yamahata 
34053b6f406SXiaoyao Li     r = setup_td_guest_attributes(x86cpu, errp);
34153b6f406SXiaoyao Li     if (r) {
34253b6f406SXiaoyao Li         return r;
34353b6f406SXiaoyao Li     }
344bb3be394SXiaoyao Li 
345f15898b0SXiaoyao Li     r = setup_td_xfam(x86cpu, errp);
346f15898b0SXiaoyao Li     if (r) {
347f15898b0SXiaoyao Li         return r;
348f15898b0SXiaoyao Li     }
349f15898b0SXiaoyao Li 
350f15898b0SXiaoyao Li     init_vm->cpuid.nent = kvm_x86_build_cpuid(env, init_vm->cpuid.entries, 0);
351f15898b0SXiaoyao Li     tdx_filter_cpuid(&init_vm->cpuid);
352f15898b0SXiaoyao Li 
353f15898b0SXiaoyao Li     init_vm->attributes = tdx_guest->attributes;
354f15898b0SXiaoyao Li     init_vm->xfam = tdx_guest->xfam;
355f15898b0SXiaoyao Li 
356f15898b0SXiaoyao Li     /*
357f15898b0SXiaoyao Li      * KVM_TDX_INIT_VM gets -EAGAIN when KVM side SEAMCALL(TDH_MNG_CREATE)
358f15898b0SXiaoyao Li      * gets TDX_RND_NO_ENTROPY due to Random number generation (e.g., RDRAND or
359f15898b0SXiaoyao Li      * RDSEED) is busy.
360f15898b0SXiaoyao Li      *
361f15898b0SXiaoyao Li      * Retry for the case.
362f15898b0SXiaoyao Li      */
363f15898b0SXiaoyao Li     do {
364f15898b0SXiaoyao Li         error_free(local_err);
365f15898b0SXiaoyao Li         local_err = NULL;
366f15898b0SXiaoyao Li         r = tdx_vm_ioctl(KVM_TDX_INIT_VM, 0, init_vm, &local_err);
367f15898b0SXiaoyao Li     } while (r == -EAGAIN && --retry);
368f15898b0SXiaoyao Li 
369f15898b0SXiaoyao Li     if (r < 0) {
370f15898b0SXiaoyao Li         if (!retry) {
371f15898b0SXiaoyao Li             error_append_hint(&local_err, "Hardware RNG (Random Number "
372f15898b0SXiaoyao Li             "Generator) is busy occupied by someone (via RDRAND/RDSEED) "
373f15898b0SXiaoyao Li             "maliciously, which leads to KVM_TDX_INIT_VM keeping failure "
374f15898b0SXiaoyao Li             "due to lack of entropy.\n");
375f15898b0SXiaoyao Li         }
376f15898b0SXiaoyao Li         error_propagate(errp, local_err);
377f15898b0SXiaoyao Li         return r;
378f15898b0SXiaoyao Li     }
379f15898b0SXiaoyao Li 
380f15898b0SXiaoyao Li     tdx_guest->initialized = true;
381f15898b0SXiaoyao Li 
382f15898b0SXiaoyao Li     return 0;
383f15898b0SXiaoyao Li }
384f15898b0SXiaoyao Li 
3856016e297SXiaoyao Li static bool tdx_guest_get_sept_ve_disable(Object *obj, Error **errp)
3866016e297SXiaoyao Li {
3876016e297SXiaoyao Li     TdxGuest *tdx = TDX_GUEST(obj);
3886016e297SXiaoyao Li 
3896016e297SXiaoyao Li     return !!(tdx->attributes & TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE);
3906016e297SXiaoyao Li }
3916016e297SXiaoyao Li 
3926016e297SXiaoyao Li static void tdx_guest_set_sept_ve_disable(Object *obj, bool value, Error **errp)
3936016e297SXiaoyao Li {
3946016e297SXiaoyao Li     TdxGuest *tdx = TDX_GUEST(obj);
3956016e297SXiaoyao Li 
3966016e297SXiaoyao Li     if (value) {
3976016e297SXiaoyao Li         tdx->attributes |= TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE;
3986016e297SXiaoyao Li     } else {
3996016e297SXiaoyao Li         tdx->attributes &= ~TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE;
4006016e297SXiaoyao Li     }
4016016e297SXiaoyao Li }
4026016e297SXiaoyao Li 
403d05a0858SIsaku Yamahata static char *tdx_guest_get_mrconfigid(Object *obj, Error **errp)
404d05a0858SIsaku Yamahata {
405d05a0858SIsaku Yamahata     TdxGuest *tdx = TDX_GUEST(obj);
406d05a0858SIsaku Yamahata 
407d05a0858SIsaku Yamahata     return g_strdup(tdx->mrconfigid);
408d05a0858SIsaku Yamahata }
409d05a0858SIsaku Yamahata 
410d05a0858SIsaku Yamahata static void tdx_guest_set_mrconfigid(Object *obj, const char *value, Error **errp)
411d05a0858SIsaku Yamahata {
412d05a0858SIsaku Yamahata     TdxGuest *tdx = TDX_GUEST(obj);
413d05a0858SIsaku Yamahata 
414d05a0858SIsaku Yamahata     g_free(tdx->mrconfigid);
415d05a0858SIsaku Yamahata     tdx->mrconfigid = g_strdup(value);
416d05a0858SIsaku Yamahata }
417d05a0858SIsaku Yamahata 
418d05a0858SIsaku Yamahata static char *tdx_guest_get_mrowner(Object *obj, Error **errp)
419d05a0858SIsaku Yamahata {
420d05a0858SIsaku Yamahata     TdxGuest *tdx = TDX_GUEST(obj);
421d05a0858SIsaku Yamahata 
422d05a0858SIsaku Yamahata     return g_strdup(tdx->mrowner);
423d05a0858SIsaku Yamahata }
424d05a0858SIsaku Yamahata 
425d05a0858SIsaku Yamahata static void tdx_guest_set_mrowner(Object *obj, const char *value, Error **errp)
426d05a0858SIsaku Yamahata {
427d05a0858SIsaku Yamahata     TdxGuest *tdx = TDX_GUEST(obj);
428d05a0858SIsaku Yamahata 
429d05a0858SIsaku Yamahata     g_free(tdx->mrowner);
430d05a0858SIsaku Yamahata     tdx->mrowner = g_strdup(value);
431d05a0858SIsaku Yamahata }
432d05a0858SIsaku Yamahata 
433d05a0858SIsaku Yamahata static char *tdx_guest_get_mrownerconfig(Object *obj, Error **errp)
434d05a0858SIsaku Yamahata {
435d05a0858SIsaku Yamahata     TdxGuest *tdx = TDX_GUEST(obj);
436d05a0858SIsaku Yamahata 
437d05a0858SIsaku Yamahata     return g_strdup(tdx->mrownerconfig);
438d05a0858SIsaku Yamahata }
439d05a0858SIsaku Yamahata 
440d05a0858SIsaku Yamahata static void tdx_guest_set_mrownerconfig(Object *obj, const char *value, Error **errp)
441d05a0858SIsaku Yamahata {
442d05a0858SIsaku Yamahata     TdxGuest *tdx = TDX_GUEST(obj);
443d05a0858SIsaku Yamahata 
444d05a0858SIsaku Yamahata     g_free(tdx->mrownerconfig);
445d05a0858SIsaku Yamahata     tdx->mrownerconfig = g_strdup(value);
446d05a0858SIsaku Yamahata }
447d05a0858SIsaku Yamahata 
448756e12e7SXiaoyao Li /* tdx guest */
449756e12e7SXiaoyao Li OBJECT_DEFINE_TYPE_WITH_INTERFACES(TdxGuest,
450756e12e7SXiaoyao Li                                    tdx_guest,
451756e12e7SXiaoyao Li                                    TDX_GUEST,
452756e12e7SXiaoyao Li                                    X86_CONFIDENTIAL_GUEST,
453756e12e7SXiaoyao Li                                    { TYPE_USER_CREATABLE },
454756e12e7SXiaoyao Li                                    { NULL })
455756e12e7SXiaoyao Li 
456756e12e7SXiaoyao Li static void tdx_guest_init(Object *obj)
457756e12e7SXiaoyao Li {
458756e12e7SXiaoyao Li     ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
459756e12e7SXiaoyao Li     TdxGuest *tdx = TDX_GUEST(obj);
460756e12e7SXiaoyao Li 
461f15898b0SXiaoyao Li     qemu_mutex_init(&tdx->lock);
462f15898b0SXiaoyao Li 
463756e12e7SXiaoyao Li     cgs->require_guest_memfd = true;
464714af522SIsaku Yamahata     tdx->attributes = TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE;
465756e12e7SXiaoyao Li 
466756e12e7SXiaoyao Li     object_property_add_uint64_ptr(obj, "attributes", &tdx->attributes,
467756e12e7SXiaoyao Li                                    OBJ_PROP_FLAG_READWRITE);
4686016e297SXiaoyao Li     object_property_add_bool(obj, "sept-ve-disable",
4696016e297SXiaoyao Li                              tdx_guest_get_sept_ve_disable,
4706016e297SXiaoyao Li                              tdx_guest_set_sept_ve_disable);
471d05a0858SIsaku Yamahata     object_property_add_str(obj, "mrconfigid",
472d05a0858SIsaku Yamahata                             tdx_guest_get_mrconfigid,
473d05a0858SIsaku Yamahata                             tdx_guest_set_mrconfigid);
474d05a0858SIsaku Yamahata     object_property_add_str(obj, "mrowner",
475d05a0858SIsaku Yamahata                             tdx_guest_get_mrowner, tdx_guest_set_mrowner);
476d05a0858SIsaku Yamahata     object_property_add_str(obj, "mrownerconfig",
477d05a0858SIsaku Yamahata                             tdx_guest_get_mrownerconfig,
478d05a0858SIsaku Yamahata                             tdx_guest_set_mrownerconfig);
479756e12e7SXiaoyao Li }
480756e12e7SXiaoyao Li 
481756e12e7SXiaoyao Li static void tdx_guest_finalize(Object *obj)
482756e12e7SXiaoyao Li {
483756e12e7SXiaoyao Li }
484756e12e7SXiaoyao Li 
485756e12e7SXiaoyao Li static void tdx_guest_class_init(ObjectClass *oc, const void *data)
486756e12e7SXiaoyao Li {
487631a2ac5SXiaoyao Li     ConfidentialGuestSupportClass *klass = CONFIDENTIAL_GUEST_SUPPORT_CLASS(oc);
488b455880eSXiaoyao Li     X86ConfidentialGuestClass *x86_klass = X86_CONFIDENTIAL_GUEST_CLASS(oc);
489b455880eSXiaoyao Li 
490631a2ac5SXiaoyao Li     klass->kvm_init = tdx_kvm_init;
491b455880eSXiaoyao Li     x86_klass->kvm_type = tdx_kvm_type;
492756e12e7SXiaoyao Li }
493