4 * Copyright Advanced Micro Devices 2016-2018
7 * Brijesh Singh <brijesh.singh@amd.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
16 #include <linux/kvm.h>
17 #include <linux/kvm_para.h>
18 #include <linux/psp-sev.h>
20 #include <sys/ioctl.h>
22 #include "qapi/error.h"
23 #include "qom/object_interfaces.h"
24 #include "qemu/base64.h"
25 #include "qemu/module.h"
26 #include "qemu/uuid.h"
27 #include "qemu/error-report.h"
28 #include "crypto/hash.h"
29 #include "sysemu/kvm.h"
30 #include "kvm/kvm_i386.h"
32 #include "sysemu/sysemu.h"
33 #include "sysemu/runstate.h"
35 #include "migration/blocker.h"
36 #include "qom/object.h"
37 #include "monitor/monitor.h"
38 #include "monitor/hmp-target.h"
39 #include "qapi/qapi-commands-misc-target.h"
40 #include "confidential-guest.h"
41 #include "hw/i386/pc.h"
42 #include "exec/address-spaces.h"
43 #include "qemu/queue.h"
45 OBJECT_DECLARE_TYPE(SevCommonState
, SevCommonStateClass
, SEV_COMMON
)
46 OBJECT_DECLARE_TYPE(SevGuestState
, SevCommonStateClass
, SEV_GUEST
)
47 OBJECT_DECLARE_TYPE(SevSnpGuestState
, SevCommonStateClass
, SEV_SNP_GUEST
)
49 /* hard code sha256 digest size */
52 typedef struct QEMU_PACKED SevHashTableEntry
{
55 uint8_t hash
[HASH_SIZE
];
58 typedef struct QEMU_PACKED SevHashTable
{
61 SevHashTableEntry cmdline
;
62 SevHashTableEntry initrd
;
63 SevHashTableEntry kernel
;
67 * Data encrypted by sev_encrypt_flash() must be padded to a multiple of
70 typedef struct QEMU_PACKED PaddedSevHashTable
{
72 uint8_t padding
[ROUND_UP(sizeof(SevHashTable
), 16) - sizeof(SevHashTable
)];
75 QEMU_BUILD_BUG_ON(sizeof(PaddedSevHashTable
) % 16 != 0);
77 #define SEV_INFO_BLOCK_GUID "00f771de-1a7e-4fcb-890e-68c77e2fb44e"
78 typedef struct __attribute__((__packed__
)) SevInfoBlock
{
79 /* SEV-ES Reset Vector Address */
83 #define SEV_HASH_TABLE_RV_GUID "7255371f-3a3b-4b04-927b-1da6efa8d454"
84 typedef struct QEMU_PACKED SevHashTableDescriptor
{
85 /* SEV hash table area guest address */
87 /* SEV hash table area size (in bytes) */
89 } SevHashTableDescriptor
;
91 struct SevCommonState
{
92 X86ConfidentialGuest parent_obj
;
96 /* configuration parameters */
99 uint32_t reduced_phys_bits
;
111 bool reset_data_valid
;
114 struct SevCommonStateClass
{
115 X86ConfidentialGuestClass parent_class
;
118 bool (*build_kernel_loader_hashes
)(SevCommonState
*sev_common
,
119 SevHashTableDescriptor
*area
,
120 SevKernelLoaderContext
*ctx
,
122 int (*launch_start
)(SevCommonState
*sev_common
);
123 void (*launch_finish
)(SevCommonState
*sev_common
);
124 int (*launch_update_data
)(SevCommonState
*sev_common
, hwaddr gpa
, uint8_t *ptr
, size_t len
);
125 int (*kvm_init
)(ConfidentialGuestSupport
*cgs
, Error
**errp
);
131 * The SevGuestState object is used for creating and managing a SEV
135 * -object sev-guest,id=sev0 \
136 * -machine ...,memory-encryption=sev0
138 struct SevGuestState
{
139 SevCommonState parent_obj
;
142 /* configuration parameters */
147 OnOffAuto legacy_vm_type
;
150 struct SevSnpGuestState
{
151 SevCommonState parent_obj
;
153 /* configuration parameters */
154 char *guest_visible_workarounds
;
155 char *id_block_base64
;
157 char *id_auth_base64
;
161 struct kvm_sev_snp_launch_start kvm_start_conf
;
162 struct kvm_sev_snp_launch_finish kvm_finish_conf
;
164 uint32_t kernel_hashes_offset
;
165 PaddedSevHashTable
*kernel_hashes_data
;
168 #define DEFAULT_GUEST_POLICY 0x1 /* disable debug */
169 #define DEFAULT_SEV_DEVICE "/dev/sev"
170 #define DEFAULT_SEV_SNP_POLICY 0x30000
172 typedef struct SevLaunchUpdateData
{
173 QTAILQ_ENTRY(SevLaunchUpdateData
) next
;
178 } SevLaunchUpdateData
;
180 static QTAILQ_HEAD(, SevLaunchUpdateData
) launch_update
;
182 static Error
*sev_mig_blocker
;
184 static const char *const sev_fw_errlist
[] = {
185 [SEV_RET_SUCCESS
] = "",
186 [SEV_RET_INVALID_PLATFORM_STATE
] = "Platform state is invalid",
187 [SEV_RET_INVALID_GUEST_STATE
] = "Guest state is invalid",
188 [SEV_RET_INAVLID_CONFIG
] = "Platform configuration is invalid",
189 [SEV_RET_INVALID_LEN
] = "Buffer too small",
190 [SEV_RET_ALREADY_OWNED
] = "Platform is already owned",
191 [SEV_RET_INVALID_CERTIFICATE
] = "Certificate is invalid",
192 [SEV_RET_POLICY_FAILURE
] = "Policy is not allowed",
193 [SEV_RET_INACTIVE
] = "Guest is not active",
194 [SEV_RET_INVALID_ADDRESS
] = "Invalid address",
195 [SEV_RET_BAD_SIGNATURE
] = "Bad signature",
196 [SEV_RET_BAD_MEASUREMENT
] = "Bad measurement",
197 [SEV_RET_ASID_OWNED
] = "ASID is already owned",
198 [SEV_RET_INVALID_ASID
] = "Invalid ASID",
199 [SEV_RET_WBINVD_REQUIRED
] = "WBINVD is required",
200 [SEV_RET_DFFLUSH_REQUIRED
] = "DF_FLUSH is required",
201 [SEV_RET_INVALID_GUEST
] = "Guest handle is invalid",
202 [SEV_RET_INVALID_COMMAND
] = "Invalid command",
203 [SEV_RET_ACTIVE
] = "Guest is active",
204 [SEV_RET_HWSEV_RET_PLATFORM
] = "Hardware error",
205 [SEV_RET_HWSEV_RET_UNSAFE
] = "Hardware unsafe",
206 [SEV_RET_UNSUPPORTED
] = "Feature not supported",
207 [SEV_RET_INVALID_PARAM
] = "Invalid parameter",
208 [SEV_RET_RESOURCE_LIMIT
] = "Required firmware resource depleted",
209 [SEV_RET_SECURE_DATA_INVALID
] = "Part-specific integrity check failure",
212 #define SEV_FW_MAX_ERROR ARRAY_SIZE(sev_fw_errlist)
214 /* <linux/kvm.h> doesn't expose this, so re-use the max from kvm.c */
215 #define KVM_MAX_CPUID_ENTRIES 100
217 typedef struct KvmCpuidInfo
{
218 struct kvm_cpuid2 cpuid
;
219 struct kvm_cpuid_entry2 entries
[KVM_MAX_CPUID_ENTRIES
];
222 #define SNP_CPUID_FUNCTION_MAXCOUNT 64
223 #define SNP_CPUID_FUNCTION_UNKNOWN 0xFFFFFFFF
235 } __attribute__((packed
)) SnpCpuidFunc
;
241 SnpCpuidFunc entries
[SNP_CPUID_FUNCTION_MAXCOUNT
];
242 } __attribute__((packed
)) SnpCpuidInfo
;
245 sev_ioctl(int fd
, int cmd
, void *data
, int *error
)
248 struct kvm_sev_cmd input
;
250 memset(&input
, 0x0, sizeof(input
));
254 input
.data
= (uintptr_t)data
;
256 r
= kvm_vm_ioctl(kvm_state
, KVM_MEMORY_ENCRYPT_OP
, &input
);
259 *error
= input
.error
;
266 sev_platform_ioctl(int fd
, int cmd
, void *data
, int *error
)
269 struct sev_issue_cmd arg
;
272 arg
.data
= (unsigned long)data
;
273 r
= ioctl(fd
, SEV_ISSUE_CMD
, &arg
);
282 fw_error_to_str(int code
)
284 if (code
< 0 || code
>= SEV_FW_MAX_ERROR
) {
285 return "unknown error";
288 return sev_fw_errlist
[code
];
292 sev_check_state(const SevCommonState
*sev_common
, SevState state
)
295 return sev_common
->state
== state
? true : false;
299 sev_set_guest_state(SevCommonState
*sev_common
, SevState new_state
)
301 assert(new_state
< SEV_STATE__MAX
);
304 trace_kvm_sev_change_state(SevState_str(sev_common
->state
),
305 SevState_str(new_state
));
306 sev_common
->state
= new_state
;
310 sev_ram_block_added(RAMBlockNotifier
*n
, void *host
, size_t size
,
314 struct kvm_enc_region range
;
319 * The RAM device presents a memory region that should be treated
320 * as IO region and should not be pinned.
322 mr
= memory_region_from_host(host
, &offset
);
323 if (mr
&& memory_region_is_ram_device(mr
)) {
327 range
.addr
= (uintptr_t)host
;
328 range
.size
= max_size
;
330 trace_kvm_memcrypt_register_region(host
, max_size
);
331 r
= kvm_vm_ioctl(kvm_state
, KVM_MEMORY_ENCRYPT_REG_REGION
, &range
);
333 error_report("%s: failed to register region (%p+%#zx) error '%s'",
334 __func__
, host
, max_size
, strerror(errno
));
340 sev_ram_block_removed(RAMBlockNotifier
*n
, void *host
, size_t size
,
344 struct kvm_enc_region range
;
349 * The RAM device presents a memory region that should be treated
350 * as IO region and should not have been pinned.
352 mr
= memory_region_from_host(host
, &offset
);
353 if (mr
&& memory_region_is_ram_device(mr
)) {
357 range
.addr
= (uintptr_t)host
;
358 range
.size
= max_size
;
360 trace_kvm_memcrypt_unregister_region(host
, max_size
);
361 r
= kvm_vm_ioctl(kvm_state
, KVM_MEMORY_ENCRYPT_UNREG_REGION
, &range
);
363 error_report("%s: failed to unregister region (%p+%#zx)",
364 __func__
, host
, max_size
);
368 static struct RAMBlockNotifier sev_ram_notifier
= {
369 .ram_block_added
= sev_ram_block_added
,
370 .ram_block_removed
= sev_ram_block_removed
,
376 ConfidentialGuestSupport
*cgs
= MACHINE(qdev_get_machine())->cgs
;
378 return !!object_dynamic_cast(OBJECT(cgs
), TYPE_SEV_COMMON
);
382 sev_snp_enabled(void)
384 ConfidentialGuestSupport
*cgs
= MACHINE(qdev_get_machine())->cgs
;
386 return !!object_dynamic_cast(OBJECT(cgs
), TYPE_SEV_SNP_GUEST
);
392 ConfidentialGuestSupport
*cgs
= MACHINE(qdev_get_machine())->cgs
;
394 return sev_snp_enabled() ||
395 (sev_enabled() && SEV_GUEST(cgs
)->policy
& SEV_POLICY_ES
);
399 sev_get_cbit_position(void)
401 SevCommonState
*sev_common
= SEV_COMMON(MACHINE(qdev_get_machine())->cgs
);
403 return sev_common
? sev_common
->cbitpos
: 0;
407 sev_get_reduced_phys_bits(void)
409 SevCommonState
*sev_common
= SEV_COMMON(MACHINE(qdev_get_machine())->cgs
);
411 return sev_common
? sev_common
->reduced_phys_bits
: 0;
414 static SevInfo
*sev_get_info(void)
417 SevCommonState
*sev_common
= SEV_COMMON(MACHINE(qdev_get_machine())->cgs
);
419 info
= g_new0(SevInfo
, 1);
420 info
->enabled
= sev_enabled();
423 info
->api_major
= sev_common
->api_major
;
424 info
->api_minor
= sev_common
->api_minor
;
425 info
->build_id
= sev_common
->build_id
;
426 info
->state
= sev_common
->state
;
428 if (sev_snp_enabled()) {
429 info
->sev_type
= SEV_GUEST_TYPE_SEV_SNP
;
430 info
->u
.sev_snp
.snp_policy
=
431 object_property_get_uint(OBJECT(sev_common
), "policy", NULL
);
433 info
->sev_type
= SEV_GUEST_TYPE_SEV
;
434 info
->u
.sev
.handle
= SEV_GUEST(sev_common
)->handle
;
436 (uint32_t)object_property_get_uint(OBJECT(sev_common
),
444 SevInfo
*qmp_query_sev(Error
**errp
)
448 info
= sev_get_info();
450 error_setg(errp
, "SEV feature is not available");
457 void hmp_info_sev(Monitor
*mon
, const QDict
*qdict
)
459 SevInfo
*info
= sev_get_info();
461 if (!info
|| !info
->enabled
) {
462 monitor_printf(mon
, "SEV is not enabled\n");
466 monitor_printf(mon
, "SEV type: %s\n", SevGuestType_str(info
->sev_type
));
467 monitor_printf(mon
, "state: %s\n", SevState_str(info
->state
));
468 monitor_printf(mon
, "build: %d\n", info
->build_id
);
469 monitor_printf(mon
, "api version: %d.%d\n", info
->api_major
,
472 if (sev_snp_enabled()) {
473 monitor_printf(mon
, "debug: %s\n",
474 info
->u
.sev_snp
.snp_policy
& SEV_SNP_POLICY_DBG
? "on"
476 monitor_printf(mon
, "SMT allowed: %s\n",
477 info
->u
.sev_snp
.snp_policy
& SEV_SNP_POLICY_SMT
? "on"
480 monitor_printf(mon
, "handle: %d\n", info
->u
.sev
.handle
);
481 monitor_printf(mon
, "debug: %s\n",
482 info
->u
.sev
.policy
& SEV_POLICY_NODBG
? "off" : "on");
483 monitor_printf(mon
, "key-sharing: %s\n",
484 info
->u
.sev
.policy
& SEV_POLICY_NOKS
? "off" : "on");
488 qapi_free_SevInfo(info
);
492 sev_get_pdh_info(int fd
, guchar
**pdh
, size_t *pdh_len
, guchar
**cert_chain
,
493 size_t *cert_chain_len
, Error
**errp
)
495 guchar
*pdh_data
= NULL
;
496 guchar
*cert_chain_data
= NULL
;
497 struct sev_user_data_pdh_cert_export export
= {};
500 /* query the certificate length */
501 r
= sev_platform_ioctl(fd
, SEV_PDH_CERT_EXPORT
, &export
, &err
);
503 if (err
!= SEV_RET_INVALID_LEN
) {
504 error_setg(errp
, "SEV: Failed to export PDH cert"
505 " ret=%d fw_err=%d (%s)",
506 r
, err
, fw_error_to_str(err
));
511 pdh_data
= g_new(guchar
, export
.pdh_cert_len
);
512 cert_chain_data
= g_new(guchar
, export
.cert_chain_len
);
513 export
.pdh_cert_address
= (unsigned long)pdh_data
;
514 export
.cert_chain_address
= (unsigned long)cert_chain_data
;
516 r
= sev_platform_ioctl(fd
, SEV_PDH_CERT_EXPORT
, &export
, &err
);
518 error_setg(errp
, "SEV: Failed to export PDH cert ret=%d fw_err=%d (%s)",
519 r
, err
, fw_error_to_str(err
));
524 *pdh_len
= export
.pdh_cert_len
;
525 *cert_chain
= cert_chain_data
;
526 *cert_chain_len
= export
.cert_chain_len
;
531 g_free(cert_chain_data
);
535 static int sev_get_cpu0_id(int fd
, guchar
**id
, size_t *id_len
, Error
**errp
)
538 struct sev_user_data_get_id2 get_id2
= {};
541 /* query the ID length */
542 r
= sev_platform_ioctl(fd
, SEV_GET_ID2
, &get_id2
, &err
);
543 if (r
< 0 && err
!= SEV_RET_INVALID_LEN
) {
544 error_setg(errp
, "SEV: Failed to get ID ret=%d fw_err=%d (%s)",
545 r
, err
, fw_error_to_str(err
));
549 id_data
= g_new(guchar
, get_id2
.length
);
550 get_id2
.address
= (unsigned long)id_data
;
552 r
= sev_platform_ioctl(fd
, SEV_GET_ID2
, &get_id2
, &err
);
554 error_setg(errp
, "SEV: Failed to get ID ret=%d fw_err=%d (%s)",
555 r
, err
, fw_error_to_str(err
));
560 *id_len
= get_id2
.length
;
568 static SevCapability
*sev_get_capabilities(Error
**errp
)
570 SevCapability
*cap
= NULL
;
571 guchar
*pdh_data
= NULL
;
572 guchar
*cert_chain_data
= NULL
;
573 guchar
*cpu0_id_data
= NULL
;
574 size_t pdh_len
= 0, cert_chain_len
= 0, cpu0_id_len
= 0;
577 SevCommonState
*sev_common
;
580 if (!kvm_enabled()) {
581 error_setg(errp
, "KVM not enabled");
584 if (kvm_vm_ioctl(kvm_state
, KVM_MEMORY_ENCRYPT_OP
, NULL
) < 0) {
585 error_setg(errp
, "SEV is not enabled in KVM");
589 sev_common
= SEV_COMMON(MACHINE(qdev_get_machine())->cgs
);
591 sev_device
= object_property_get_str(OBJECT(sev_common
), "sev-device",
594 sev_device
= g_strdup(DEFAULT_SEV_DEVICE
);
597 fd
= open(sev_device
, O_RDWR
);
599 error_setg_errno(errp
, errno
, "SEV: Failed to open %s",
606 if (sev_get_pdh_info(fd
, &pdh_data
, &pdh_len
,
607 &cert_chain_data
, &cert_chain_len
, errp
)) {
611 if (sev_get_cpu0_id(fd
, &cpu0_id_data
, &cpu0_id_len
, errp
)) {
615 cap
= g_new0(SevCapability
, 1);
616 cap
->pdh
= g_base64_encode(pdh_data
, pdh_len
);
617 cap
->cert_chain
= g_base64_encode(cert_chain_data
, cert_chain_len
);
618 cap
->cpu0_id
= g_base64_encode(cpu0_id_data
, cpu0_id_len
);
620 host_cpuid(0x8000001F, 0, NULL
, &ebx
, NULL
, NULL
);
621 cap
->cbitpos
= ebx
& 0x3f;
624 * When SEV feature is enabled, we loose one bit in guest physical
627 cap
->reduced_phys_bits
= 1;
630 g_free(cpu0_id_data
);
632 g_free(cert_chain_data
);
637 SevCapability
*qmp_query_sev_capabilities(Error
**errp
)
639 return sev_get_capabilities(errp
);
642 static OvmfSevMetadata
*ovmf_sev_metadata_table
;
644 #define OVMF_SEV_META_DATA_GUID "dc886566-984a-4798-A75e-5585a7bf67cc"
645 typedef struct __attribute__((__packed__
)) OvmfSevMetadataOffset
{
647 } OvmfSevMetadataOffset
;
649 OvmfSevMetadata
*pc_system_get_ovmf_sev_metadata_ptr(void)
651 return ovmf_sev_metadata_table
;
654 void pc_system_parse_sev_metadata(uint8_t *flash_ptr
, size_t flash_size
)
656 OvmfSevMetadata
*metadata
;
657 OvmfSevMetadataOffset
*data
;
659 if (!pc_system_ovmf_table_find(OVMF_SEV_META_DATA_GUID
, (uint8_t **)&data
,
664 metadata
= (OvmfSevMetadata
*)(flash_ptr
+ flash_size
- data
->offset
);
665 if (memcmp(metadata
->signature
, "ASEV", 4) != 0 ||
666 metadata
->len
< sizeof(OvmfSevMetadata
) ||
667 metadata
->len
> flash_size
- data
->offset
) {
671 ovmf_sev_metadata_table
= g_memdup2(metadata
, metadata
->len
);
674 static SevAttestationReport
*sev_get_attestation_report(const char *mnonce
,
677 struct kvm_sev_attestation_report input
= {};
678 SevAttestationReport
*report
= NULL
;
679 SevCommonState
*sev_common
;
680 g_autofree guchar
*data
= NULL
;
681 g_autofree guchar
*buf
= NULL
;
685 if (!sev_enabled()) {
686 error_setg(errp
, "SEV is not enabled");
690 /* lets decode the mnonce string */
691 buf
= g_base64_decode(mnonce
, &len
);
693 error_setg(errp
, "SEV: failed to decode mnonce input");
697 /* verify the input mnonce length */
698 if (len
!= sizeof(input
.mnonce
)) {
699 error_setg(errp
, "SEV: mnonce must be %zu bytes (got %" G_GSIZE_FORMAT
")",
700 sizeof(input
.mnonce
), len
);
704 sev_common
= SEV_COMMON(MACHINE(qdev_get_machine())->cgs
);
706 /* Query the report length */
707 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_GET_ATTESTATION_REPORT
,
710 if (err
!= SEV_RET_INVALID_LEN
) {
711 error_setg(errp
, "SEV: Failed to query the attestation report"
712 " length ret=%d fw_err=%d (%s)",
713 ret
, err
, fw_error_to_str(err
));
718 data
= g_malloc(input
.len
);
719 input
.uaddr
= (unsigned long)data
;
720 memcpy(input
.mnonce
, buf
, sizeof(input
.mnonce
));
722 /* Query the report */
723 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_GET_ATTESTATION_REPORT
,
726 error_setg_errno(errp
, errno
, "SEV: Failed to get attestation report"
727 " ret=%d fw_err=%d (%s)", ret
, err
, fw_error_to_str(err
));
731 report
= g_new0(SevAttestationReport
, 1);
732 report
->data
= g_base64_encode(data
, input
.len
);
734 trace_kvm_sev_attestation_report(mnonce
, report
->data
);
739 SevAttestationReport
*qmp_query_sev_attestation_report(const char *mnonce
,
742 return sev_get_attestation_report(mnonce
, errp
);
746 sev_read_file_base64(const char *filename
, guchar
**data
, gsize
*len
)
749 g_autofree gchar
*base64
= NULL
;
750 GError
*error
= NULL
;
752 if (!g_file_get_contents(filename
, &base64
, &sz
, &error
)) {
753 error_report("SEV: Failed to read '%s' (%s)", filename
, error
->message
);
758 *data
= g_base64_decode(base64
, len
);
763 sev_snp_launch_start(SevCommonState
*sev_common
)
766 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(sev_common
);
767 struct kvm_sev_snp_launch_start
*start
= &sev_snp_guest
->kvm_start_conf
;
769 trace_kvm_sev_snp_launch_start(start
->policy
,
770 sev_snp_guest
->guest_visible_workarounds
);
772 if (!kvm_enable_hypercall(BIT_ULL(KVM_HC_MAP_GPA_RANGE
))) {
776 rc
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_SNP_LAUNCH_START
,
779 error_report("%s: SNP_LAUNCH_START ret=%d fw_error=%d '%s'",
780 __func__
, rc
, fw_error
, fw_error_to_str(fw_error
));
784 QTAILQ_INIT(&launch_update
);
786 sev_set_guest_state(sev_common
, SEV_STATE_LAUNCH_UPDATE
);
792 sev_launch_start(SevCommonState
*sev_common
)
797 SevGuestState
*sev_guest
= SEV_GUEST(sev_common
);
798 struct kvm_sev_launch_start start
= {
799 .handle
= sev_guest
->handle
, .policy
= sev_guest
->policy
801 guchar
*session
= NULL
, *dh_cert
= NULL
;
803 if (sev_guest
->session_file
) {
804 if (sev_read_file_base64(sev_guest
->session_file
, &session
, &sz
) < 0) {
807 start
.session_uaddr
= (unsigned long)session
;
808 start
.session_len
= sz
;
811 if (sev_guest
->dh_cert_file
) {
812 if (sev_read_file_base64(sev_guest
->dh_cert_file
, &dh_cert
, &sz
) < 0) {
815 start
.dh_uaddr
= (unsigned long)dh_cert
;
819 trace_kvm_sev_launch_start(start
.policy
, session
, dh_cert
);
820 rc
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_LAUNCH_START
, &start
, &fw_error
);
822 error_report("%s: LAUNCH_START ret=%d fw_error=%d '%s'",
823 __func__
, ret
, fw_error
, fw_error_to_str(fw_error
));
827 sev_set_guest_state(sev_common
, SEV_STATE_LAUNCH_UPDATE
);
828 sev_guest
->handle
= start
.handle
;
838 sev_snp_cpuid_report_mismatches(SnpCpuidInfo
*old
,
843 if (old
->count
!= new->count
) {
844 error_report("SEV-SNP: CPUID validation failed due to count mismatch, "
845 "provided: %d, expected: %d", old
->count
, new->count
);
849 for (i
= 0; i
< old
->count
; i
++) {
850 SnpCpuidFunc
*old_func
, *new_func
;
852 old_func
= &old
->entries
[i
];
853 new_func
= &new->entries
[i
];
855 if (memcmp(old_func
, new_func
, sizeof(SnpCpuidFunc
))) {
856 error_report("SEV-SNP: CPUID validation failed for function 0x%x, index: 0x%x, "
857 "provided: eax:0x%08x, ebx: 0x%08x, ecx: 0x%08x, edx: 0x%08x, "
858 "expected: eax:0x%08x, ebx: 0x%08x, ecx: 0x%08x, edx: 0x%08x",
859 old_func
->eax_in
, old_func
->ecx_in
,
860 old_func
->eax
, old_func
->ebx
, old_func
->ecx
, old_func
->edx
,
861 new_func
->eax
, new_func
->ebx
, new_func
->ecx
, new_func
->edx
);
867 snp_page_type_to_str(int type
)
870 case KVM_SEV_SNP_PAGE_TYPE_NORMAL
: return "Normal";
871 case KVM_SEV_SNP_PAGE_TYPE_ZERO
: return "Zero";
872 case KVM_SEV_SNP_PAGE_TYPE_UNMEASURED
: return "Unmeasured";
873 case KVM_SEV_SNP_PAGE_TYPE_SECRETS
: return "Secrets";
874 case KVM_SEV_SNP_PAGE_TYPE_CPUID
: return "Cpuid";
875 default: return "unknown";
880 sev_snp_launch_update(SevSnpGuestState
*sev_snp_guest
,
881 SevLaunchUpdateData
*data
)
884 SnpCpuidInfo snp_cpuid_info
;
885 struct kvm_sev_snp_launch_update update
= {0};
887 if (!data
->hva
|| !data
->len
) {
888 error_report("SNP_LAUNCH_UPDATE called with invalid address"
889 "/ length: %p / %zx",
890 data
->hva
, data
->len
);
894 if (data
->type
== KVM_SEV_SNP_PAGE_TYPE_CPUID
) {
895 /* Save a copy for comparison in case the LAUNCH_UPDATE fails */
896 memcpy(&snp_cpuid_info
, data
->hva
, sizeof(snp_cpuid_info
));
899 update
.uaddr
= (__u64
)(unsigned long)data
->hva
;
900 update
.gfn_start
= data
->gpa
>> TARGET_PAGE_BITS
;
901 update
.len
= data
->len
;
902 update
.type
= data
->type
;
905 * KVM_SEV_SNP_LAUNCH_UPDATE requires that GPA ranges have the private
906 * memory attribute set in advance.
908 ret
= kvm_set_memory_attributes_private(data
->gpa
, data
->len
);
910 error_report("SEV-SNP: failed to configure initial"
911 "private guest memory");
915 while (update
.len
|| ret
== -EAGAIN
) {
916 trace_kvm_sev_snp_launch_update(update
.uaddr
, update
.gfn_start
<<
917 TARGET_PAGE_BITS
, update
.len
,
918 snp_page_type_to_str(update
.type
));
920 ret
= sev_ioctl(SEV_COMMON(sev_snp_guest
)->sev_fd
,
921 KVM_SEV_SNP_LAUNCH_UPDATE
,
923 if (ret
&& ret
!= -EAGAIN
) {
924 error_report("SNP_LAUNCH_UPDATE ret=%d fw_error=%d '%s'",
925 ret
, fw_error
, fw_error_to_str(fw_error
));
927 if (data
->type
== KVM_SEV_SNP_PAGE_TYPE_CPUID
) {
928 sev_snp_cpuid_report_mismatches(&snp_cpuid_info
, data
->hva
);
929 error_report("SEV-SNP: failed update CPUID page");
936 if (!ret
&& update
.gfn_start
<< TARGET_PAGE_BITS
!= data
->gpa
+ data
->len
) {
937 error_report("SEV-SNP: expected update of GPA range %"
938 HWADDR_PRIx
"-%" HWADDR_PRIx
","
939 "got GPA range %" HWADDR_PRIx
"-%llx",
940 data
->gpa
, data
->gpa
+ data
->len
, data
->gpa
,
941 update
.gfn_start
<< TARGET_PAGE_BITS
);
949 sev_snp_mask_cpuid_features(X86ConfidentialGuest
*cg
, uint32_t feature
, uint32_t index
,
950 int reg
, uint32_t value
)
955 return value
& ~CPUID_EXT_TSC_DEADLINE_TIMER
;
959 if (index
== 0 && reg
== R_EBX
) {
960 return value
& ~CPUID_7_0_EBX_TSC_ADJUST
;
962 if (index
== 0 && reg
== R_EDX
) {
963 return value
& ~(CPUID_7_0_EDX_SPEC_CTRL
|
964 CPUID_7_0_EDX_STIBP
|
965 CPUID_7_0_EDX_FLUSH_L1D
|
966 CPUID_7_0_EDX_ARCH_CAPABILITIES
|
967 CPUID_7_0_EDX_CORE_CAPABILITY
|
968 CPUID_7_0_EDX_SPEC_CTRL_SSBD
);
973 return value
& ~CPUID_8000_0008_EBX_VIRT_SSBD
;
981 sev_launch_update_data(SevCommonState
*sev_common
, hwaddr gpa
,
982 uint8_t *addr
, size_t len
)
985 struct kvm_sev_launch_update_data update
;
991 update
.uaddr
= (uintptr_t)addr
;
993 trace_kvm_sev_launch_update_data(addr
, len
);
994 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_LAUNCH_UPDATE_DATA
,
997 error_report("%s: LAUNCH_UPDATE ret=%d fw_error=%d '%s'",
998 __func__
, ret
, fw_error
, fw_error_to_str(fw_error
));
1005 sev_launch_update_vmsa(SevGuestState
*sev_guest
)
1009 ret
= sev_ioctl(SEV_COMMON(sev_guest
)->sev_fd
, KVM_SEV_LAUNCH_UPDATE_VMSA
,
1012 error_report("%s: LAUNCH_UPDATE_VMSA ret=%d fw_error=%d '%s'",
1013 __func__
, ret
, fw_error
, fw_error_to_str(fw_error
));
1020 sev_launch_get_measure(Notifier
*notifier
, void *unused
)
1022 SevCommonState
*sev_common
= SEV_COMMON(MACHINE(qdev_get_machine())->cgs
);
1023 SevGuestState
*sev_guest
= SEV_GUEST(sev_common
);
1025 g_autofree guchar
*data
= NULL
;
1026 struct kvm_sev_launch_measure measurement
= {};
1028 if (!sev_check_state(sev_common
, SEV_STATE_LAUNCH_UPDATE
)) {
1032 if (sev_es_enabled()) {
1033 /* measure all the VM save areas before getting launch_measure */
1034 ret
= sev_launch_update_vmsa(sev_guest
);
1038 kvm_mark_guest_state_protected();
1041 /* query the measurement blob length */
1042 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_LAUNCH_MEASURE
,
1043 &measurement
, &error
);
1044 if (!measurement
.len
) {
1045 error_report("%s: LAUNCH_MEASURE ret=%d fw_error=%d '%s'",
1046 __func__
, ret
, error
, fw_error_to_str(errno
));
1050 data
= g_new0(guchar
, measurement
.len
);
1051 measurement
.uaddr
= (unsigned long)data
;
1053 /* get the measurement blob */
1054 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_LAUNCH_MEASURE
,
1055 &measurement
, &error
);
1057 error_report("%s: LAUNCH_MEASURE ret=%d fw_error=%d '%s'",
1058 __func__
, ret
, error
, fw_error_to_str(errno
));
1062 sev_set_guest_state(sev_common
, SEV_STATE_LAUNCH_SECRET
);
1064 /* encode the measurement value and emit the event */
1065 sev_guest
->measurement
= g_base64_encode(data
, measurement
.len
);
1066 trace_kvm_sev_launch_measurement(sev_guest
->measurement
);
1069 static char *sev_get_launch_measurement(void)
1071 ConfidentialGuestSupport
*cgs
= MACHINE(qdev_get_machine())->cgs
;
1072 SevGuestState
*sev_guest
=
1073 (SevGuestState
*)object_dynamic_cast(OBJECT(cgs
), TYPE_SEV_GUEST
);
1076 SEV_COMMON(sev_guest
)->state
>= SEV_STATE_LAUNCH_SECRET
) {
1077 return g_strdup(sev_guest
->measurement
);
1083 SevLaunchMeasureInfo
*qmp_query_sev_launch_measure(Error
**errp
)
1086 SevLaunchMeasureInfo
*info
;
1088 data
= sev_get_launch_measurement();
1090 error_setg(errp
, "SEV launch measurement is not available");
1094 info
= g_malloc0(sizeof(*info
));
1100 static Notifier sev_machine_done_notify
= {
1101 .notify
= sev_launch_get_measure
,
1105 sev_launch_finish(SevCommonState
*sev_common
)
1109 trace_kvm_sev_launch_finish();
1110 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_LAUNCH_FINISH
, 0,
1113 error_report("%s: LAUNCH_FINISH ret=%d fw_error=%d '%s'",
1114 __func__
, ret
, error
, fw_error_to_str(error
));
1118 sev_set_guest_state(sev_common
, SEV_STATE_RUNNING
);
1120 /* add migration blocker */
1121 error_setg(&sev_mig_blocker
,
1122 "SEV: Migration is not implemented");
1123 migrate_add_blocker(&sev_mig_blocker
, &error_fatal
);
1127 snp_launch_update_data(uint64_t gpa
, void *hva
, size_t len
, int type
)
1129 SevLaunchUpdateData
*data
;
1131 data
= g_new0(SevLaunchUpdateData
, 1);
1137 QTAILQ_INSERT_TAIL(&launch_update
, data
, next
);
1143 sev_snp_launch_update_data(SevCommonState
*sev_common
, hwaddr gpa
,
1144 uint8_t *ptr
, size_t len
)
1146 int ret
= snp_launch_update_data(gpa
, ptr
, len
,
1147 KVM_SEV_SNP_PAGE_TYPE_NORMAL
);
1152 sev_snp_cpuid_info_fill(SnpCpuidInfo
*snp_cpuid_info
,
1153 const KvmCpuidInfo
*kvm_cpuid_info
)
1157 if (kvm_cpuid_info
->cpuid
.nent
> SNP_CPUID_FUNCTION_MAXCOUNT
) {
1158 error_report("SEV-SNP: CPUID entry count (%d) exceeds max (%d)",
1159 kvm_cpuid_info
->cpuid
.nent
, SNP_CPUID_FUNCTION_MAXCOUNT
);
1163 memset(snp_cpuid_info
, 0, sizeof(*snp_cpuid_info
));
1165 for (i
= 0; i
< kvm_cpuid_info
->cpuid
.nent
; i
++) {
1166 const struct kvm_cpuid_entry2
*kvm_cpuid_entry
;
1167 SnpCpuidFunc
*snp_cpuid_entry
;
1169 kvm_cpuid_entry
= &kvm_cpuid_info
->entries
[i
];
1170 snp_cpuid_entry
= &snp_cpuid_info
->entries
[i
];
1172 snp_cpuid_entry
->eax_in
= kvm_cpuid_entry
->function
;
1173 if (kvm_cpuid_entry
->flags
== KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) {
1174 snp_cpuid_entry
->ecx_in
= kvm_cpuid_entry
->index
;
1176 snp_cpuid_entry
->eax
= kvm_cpuid_entry
->eax
;
1177 snp_cpuid_entry
->ebx
= kvm_cpuid_entry
->ebx
;
1178 snp_cpuid_entry
->ecx
= kvm_cpuid_entry
->ecx
;
1179 snp_cpuid_entry
->edx
= kvm_cpuid_entry
->edx
;
1182 * Guest kernels will calculate EBX themselves using the 0xD
1183 * subfunctions corresponding to the individual XSAVE areas, so only
1184 * encode the base XSAVE size in the initial leaves, corresponding
1185 * to the initial XCR0=1 state.
1187 if (snp_cpuid_entry
->eax_in
== 0xD &&
1188 (snp_cpuid_entry
->ecx_in
== 0x0 || snp_cpuid_entry
->ecx_in
== 0x1)) {
1189 snp_cpuid_entry
->ebx
= 0x240;
1190 snp_cpuid_entry
->xcr0_in
= 1;
1191 snp_cpuid_entry
->xss_in
= 0;
1195 snp_cpuid_info
->count
= i
;
1201 snp_launch_update_cpuid(uint32_t cpuid_addr
, void *hva
, size_t cpuid_len
)
1203 KvmCpuidInfo kvm_cpuid_info
= {0};
1204 SnpCpuidInfo snp_cpuid_info
;
1205 CPUState
*cs
= first_cpu
;
1209 assert(sizeof(snp_cpuid_info
) <= cpuid_len
);
1211 /* get the cpuid list from KVM */
1213 kvm_cpuid_info
.cpuid
.nent
= ++i
;
1214 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_CPUID2
, &kvm_cpuid_info
);
1215 } while (ret
== -E2BIG
);
1218 error_report("SEV-SNP: unable to query CPUID values for CPU: '%s'",
1223 ret
= sev_snp_cpuid_info_fill(&snp_cpuid_info
, &kvm_cpuid_info
);
1225 error_report("SEV-SNP: failed to generate CPUID table information");
1229 memcpy(hva
, &snp_cpuid_info
, sizeof(snp_cpuid_info
));
1231 return snp_launch_update_data(cpuid_addr
, hva
, cpuid_len
,
1232 KVM_SEV_SNP_PAGE_TYPE_CPUID
);
1236 snp_launch_update_kernel_hashes(SevSnpGuestState
*sev_snp
, uint32_t addr
,
1237 void *hva
, uint32_t len
)
1239 int type
= KVM_SEV_SNP_PAGE_TYPE_ZERO
;
1240 if (sev_snp
->parent_obj
.kernel_hashes
) {
1241 assert(sev_snp
->kernel_hashes_data
);
1242 assert((sev_snp
->kernel_hashes_offset
+
1243 sizeof(*sev_snp
->kernel_hashes_data
)) <= len
);
1244 memset(hva
, 0, len
);
1245 memcpy(hva
+ sev_snp
->kernel_hashes_offset
, sev_snp
->kernel_hashes_data
,
1246 sizeof(*sev_snp
->kernel_hashes_data
));
1247 type
= KVM_SEV_SNP_PAGE_TYPE_NORMAL
;
1249 return snp_launch_update_data(addr
, hva
, len
, type
);
1253 snp_metadata_desc_to_page_type(int desc_type
)
1255 switch (desc_type
) {
1256 /* Add the umeasured prevalidated pages as a zero page */
1257 case SEV_DESC_TYPE_SNP_SEC_MEM
: return KVM_SEV_SNP_PAGE_TYPE_ZERO
;
1258 case SEV_DESC_TYPE_SNP_SECRETS
: return KVM_SEV_SNP_PAGE_TYPE_SECRETS
;
1259 case SEV_DESC_TYPE_CPUID
: return KVM_SEV_SNP_PAGE_TYPE_CPUID
;
1261 return KVM_SEV_SNP_PAGE_TYPE_ZERO
;
1266 snp_populate_metadata_pages(SevSnpGuestState
*sev_snp
,
1267 OvmfSevMetadata
*metadata
)
1269 OvmfSevMetadataDesc
*desc
;
1272 MemoryRegion
*mr
= NULL
;
1274 for (i
= 0; i
< metadata
->num_desc
; i
++) {
1275 desc
= &metadata
->descs
[i
];
1277 type
= snp_metadata_desc_to_page_type(desc
->type
);
1279 hva
= gpa2hva(&mr
, desc
->base
, desc
->len
, NULL
);
1281 error_report("%s: Failed to get HVA for GPA 0x%x sz 0x%x",
1282 __func__
, desc
->base
, desc
->len
);
1286 if (type
== KVM_SEV_SNP_PAGE_TYPE_CPUID
) {
1287 ret
= snp_launch_update_cpuid(desc
->base
, hva
, desc
->len
);
1288 } else if (desc
->type
== SEV_DESC_TYPE_SNP_KERNEL_HASHES
) {
1289 ret
= snp_launch_update_kernel_hashes(sev_snp
, desc
->base
, hva
,
1292 ret
= snp_launch_update_data(desc
->base
, hva
, desc
->len
, type
);
1296 error_report("%s: Failed to add metadata page gpa 0x%x+%x type %d",
1297 __func__
, desc
->base
, desc
->len
, desc
->type
);
1304 sev_snp_launch_finish(SevCommonState
*sev_common
)
1307 Error
*local_err
= NULL
;
1308 OvmfSevMetadata
*metadata
;
1309 SevLaunchUpdateData
*data
;
1310 SevSnpGuestState
*sev_snp
= SEV_SNP_GUEST(sev_common
);
1311 struct kvm_sev_snp_launch_finish
*finish
= &sev_snp
->kvm_finish_conf
;
1314 * To boot the SNP guest, the hypervisor is required to populate the CPUID
1315 * and Secrets page before finalizing the launch flow. The location of
1316 * the secrets and CPUID page is available through the OVMF metadata GUID.
1318 metadata
= pc_system_get_ovmf_sev_metadata_ptr();
1319 if (metadata
== NULL
) {
1320 error_report("%s: Failed to locate SEV metadata header", __func__
);
1324 /* Populate all the metadata pages */
1325 snp_populate_metadata_pages(sev_snp
, metadata
);
1327 QTAILQ_FOREACH(data
, &launch_update
, next
) {
1328 ret
= sev_snp_launch_update(sev_snp
, data
);
1334 trace_kvm_sev_snp_launch_finish(sev_snp
->id_block_base64
, sev_snp
->id_auth_base64
,
1335 sev_snp
->host_data
);
1336 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_SNP_LAUNCH_FINISH
,
1339 error_report("SNP_LAUNCH_FINISH ret=%d fw_error=%d '%s'",
1340 ret
, error
, fw_error_to_str(error
));
1344 kvm_mark_guest_state_protected();
1345 sev_set_guest_state(sev_common
, SEV_STATE_RUNNING
);
1347 /* add migration blocker */
1348 error_setg(&sev_mig_blocker
,
1349 "SEV-SNP: Migration is not implemented");
1350 ret
= migrate_add_blocker(&sev_mig_blocker
, &local_err
);
1352 error_report_err(local_err
);
1353 error_free(sev_mig_blocker
);
1360 sev_vm_state_change(void *opaque
, bool running
, RunState state
)
1362 SevCommonState
*sev_common
= opaque
;
1363 SevCommonStateClass
*klass
= SEV_COMMON_GET_CLASS(opaque
);
1366 if (!sev_check_state(sev_common
, SEV_STATE_RUNNING
)) {
1367 klass
->launch_finish(sev_common
);
1373 * This helper is to examine sev-guest properties and determine if any options
1374 * have been set which rely on the newer KVM_SEV_INIT2 interface and associated
1377 static bool sev_init2_required(SevGuestState
*sev_guest
)
1379 /* Currently no KVM_SEV_INIT2-specific options are exposed via QEMU */
1383 static int sev_kvm_type(X86ConfidentialGuest
*cg
)
1385 SevCommonState
*sev_common
= SEV_COMMON(cg
);
1386 SevGuestState
*sev_guest
= SEV_GUEST(sev_common
);
1389 if (sev_common
->kvm_type
!= -1) {
1393 /* These are the only cases where legacy VM types can be used. */
1394 if (sev_guest
->legacy_vm_type
== ON_OFF_AUTO_ON
||
1395 (sev_guest
->legacy_vm_type
== ON_OFF_AUTO_AUTO
&&
1396 !sev_init2_required(sev_guest
))) {
1397 sev_common
->kvm_type
= KVM_X86_DEFAULT_VM
;
1402 * Newer VM types are required, either explicitly via legacy-vm-type=on, or
1403 * implicitly via legacy-vm-type=auto along with additional sev-guest
1404 * properties that require the newer VM types.
1406 kvm_type
= (sev_guest
->policy
& SEV_POLICY_ES
) ?
1407 KVM_X86_SEV_ES_VM
: KVM_X86_SEV_VM
;
1408 if (!kvm_is_vm_type_supported(kvm_type
)) {
1409 if (sev_guest
->legacy_vm_type
== ON_OFF_AUTO_AUTO
) {
1410 error_report("SEV: host kernel does not support requested %s VM type, which is required "
1411 "for the set of options specified. To allow use of the legacy "
1412 "KVM_X86_DEFAULT_VM VM type, please disable any options that are not "
1413 "compatible with the legacy VM type, or upgrade your kernel.",
1414 kvm_type
== KVM_X86_SEV_VM
? "KVM_X86_SEV_VM" : "KVM_X86_SEV_ES_VM");
1416 error_report("SEV: host kernel does not support requested %s VM type. To allow use of "
1417 "the legacy KVM_X86_DEFAULT_VM VM type, the 'legacy-vm-type' argument "
1418 "must be set to 'on' or 'auto' for the sev-guest object.",
1419 kvm_type
== KVM_X86_SEV_VM
? "KVM_X86_SEV_VM" : "KVM_X86_SEV_ES_VM");
1425 sev_common
->kvm_type
= kvm_type
;
1427 return sev_common
->kvm_type
;
1430 static int sev_snp_kvm_type(X86ConfidentialGuest
*cg
)
1432 return KVM_X86_SNP_VM
;
1435 static int sev_common_kvm_init(ConfidentialGuestSupport
*cgs
, Error
**errp
)
1438 int ret
, fw_error
, cmd
;
1440 uint32_t host_cbitpos
;
1441 struct sev_user_data_status status
= {};
1442 SevCommonState
*sev_common
= SEV_COMMON(cgs
);
1443 SevCommonStateClass
*klass
= SEV_COMMON_GET_CLASS(cgs
);
1444 X86ConfidentialGuestClass
*x86_klass
=
1445 X86_CONFIDENTIAL_GUEST_GET_CLASS(cgs
);
1447 sev_common
->state
= SEV_STATE_UNINIT
;
1449 host_cpuid(0x8000001F, 0, NULL
, &ebx
, NULL
, NULL
);
1450 host_cbitpos
= ebx
& 0x3f;
1453 * The cbitpos value will be placed in bit positions 5:0 of the EBX
1454 * register of CPUID 0x8000001F. No need to verify the range as the
1455 * comparison against the host value accomplishes that.
1457 if (host_cbitpos
!= sev_common
->cbitpos
) {
1458 error_setg(errp
, "%s: cbitpos check failed, host '%d' requested '%d'",
1459 __func__
, host_cbitpos
, sev_common
->cbitpos
);
1464 * The reduced-phys-bits value will be placed in bit positions 11:6 of
1465 * the EBX register of CPUID 0x8000001F, so verify the supplied value
1466 * is in the range of 1 to 63.
1468 if (sev_common
->reduced_phys_bits
< 1 ||
1469 sev_common
->reduced_phys_bits
> 63) {
1470 error_setg(errp
, "%s: reduced_phys_bits check failed,"
1471 " it should be in the range of 1 to 63, requested '%d'",
1472 __func__
, sev_common
->reduced_phys_bits
);
1476 devname
= object_property_get_str(OBJECT(sev_common
), "sev-device", NULL
);
1477 sev_common
->sev_fd
= open(devname
, O_RDWR
);
1478 if (sev_common
->sev_fd
< 0) {
1479 error_setg(errp
, "%s: Failed to open %s '%s'", __func__
,
1480 devname
, strerror(errno
));
1486 ret
= sev_platform_ioctl(sev_common
->sev_fd
, SEV_PLATFORM_STATUS
, &status
,
1489 error_setg(errp
, "%s: failed to get platform status ret=%d "
1490 "fw_error='%d: %s'", __func__
, ret
, fw_error
,
1491 fw_error_to_str(fw_error
));
1494 sev_common
->build_id
= status
.build
;
1495 sev_common
->api_major
= status
.api_major
;
1496 sev_common
->api_minor
= status
.api_minor
;
1498 if (sev_es_enabled()) {
1499 if (!kvm_kernel_irqchip_allowed()) {
1500 error_setg(errp
, "%s: SEV-ES guests require in-kernel irqchip"
1501 "support", __func__
);
1506 if (sev_es_enabled() && !sev_snp_enabled()) {
1507 if (!(status
.flags
& SEV_STATUS_FLAGS_CONFIG_ES
)) {
1508 error_setg(errp
, "%s: guest policy requires SEV-ES, but "
1509 "host SEV-ES support unavailable",
1515 trace_kvm_sev_init();
1516 switch (x86_klass
->kvm_type(X86_CONFIDENTIAL_GUEST(sev_common
))) {
1517 case KVM_X86_DEFAULT_VM
:
1518 cmd
= sev_es_enabled() ? KVM_SEV_ES_INIT
: KVM_SEV_INIT
;
1520 ret
= sev_ioctl(sev_common
->sev_fd
, cmd
, NULL
, &fw_error
);
1522 case KVM_X86_SEV_VM
:
1523 case KVM_X86_SEV_ES_VM
:
1524 case KVM_X86_SNP_VM
: {
1525 struct kvm_sev_init args
= { 0 };
1527 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_INIT2
, &args
, &fw_error
);
1531 error_setg(errp
, "%s: host kernel does not support the requested SEV configuration.",
1537 error_setg(errp
, "%s: failed to initialize ret=%d fw_error=%d '%s'",
1538 __func__
, ret
, fw_error
, fw_error_to_str(fw_error
));
1542 ret
= klass
->launch_start(sev_common
);
1545 error_setg(errp
, "%s: failed to create encryption context", __func__
);
1549 if (klass
->kvm_init
&& klass
->kvm_init(cgs
, errp
)) {
1553 qemu_add_vm_change_state_handler(sev_vm_state_change
, sev_common
);
1560 static int sev_kvm_init(ConfidentialGuestSupport
*cgs
, Error
**errp
)
1565 * SEV/SEV-ES rely on pinned memory to back guest RAM so discarding
1566 * isn't actually possible. With SNP, only guest_memfd pages are used
1567 * for private guest memory, so discarding of shared memory is still
1570 ret
= ram_block_discard_disable(true);
1572 error_setg(errp
, "%s: cannot disable RAM discard", __func__
);
1577 * SEV uses these notifiers to register/pin pages prior to guest use,
1578 * but SNP relies on guest_memfd for private pages, which has its
1579 * own internal mechanisms for registering/pinning private memory.
1581 ram_block_notifier_add(&sev_ram_notifier
);
1584 * The machine done notify event is used for SEV guests to get the
1585 * measurement of the encrypted images. When SEV-SNP is enabled, the
1586 * measurement is part of the guest attestation process where it can
1587 * be collected without any reliance on the VMM. So skip registering
1588 * the notifier for SNP in favor of using guest attestation instead.
1590 qemu_add_machine_init_done_notifier(&sev_machine_done_notify
);
1595 static int sev_snp_kvm_init(ConfidentialGuestSupport
*cgs
, Error
**errp
)
1597 MachineState
*ms
= MACHINE(qdev_get_machine());
1598 X86MachineState
*x86ms
= X86_MACHINE(ms
);
1600 if (x86ms
->smm
== ON_OFF_AUTO_AUTO
) {
1601 x86ms
->smm
= ON_OFF_AUTO_OFF
;
1602 } else if (x86ms
->smm
== ON_OFF_AUTO_ON
) {
1603 error_setg(errp
, "SEV-SNP does not support SMM.");
1611 sev_encrypt_flash(hwaddr gpa
, uint8_t *ptr
, uint64_t len
, Error
**errp
)
1613 SevCommonState
*sev_common
= SEV_COMMON(MACHINE(qdev_get_machine())->cgs
);
1614 SevCommonStateClass
*klass
;
1619 klass
= SEV_COMMON_GET_CLASS(sev_common
);
1621 /* if SEV is in update state then encrypt the data else do nothing */
1622 if (sev_check_state(sev_common
, SEV_STATE_LAUNCH_UPDATE
)) {
1625 ret
= klass
->launch_update_data(sev_common
, gpa
, ptr
, len
);
1627 error_setg(errp
, "SEV: Failed to encrypt pflash rom");
1635 int sev_inject_launch_secret(const char *packet_hdr
, const char *secret
,
1636 uint64_t gpa
, Error
**errp
)
1639 struct kvm_sev_launch_secret input
;
1640 g_autofree guchar
*data
= NULL
, *hdr
= NULL
;
1643 gsize hdr_sz
= 0, data_sz
= 0;
1644 MemoryRegion
*mr
= NULL
;
1645 SevCommonState
*sev_common
= SEV_COMMON(MACHINE(qdev_get_machine())->cgs
);
1648 error_setg(errp
, "SEV not enabled for guest");
1652 /* secret can be injected only in this state */
1653 if (!sev_check_state(sev_common
, SEV_STATE_LAUNCH_SECRET
)) {
1654 error_setg(errp
, "SEV: Not in correct state. (LSECRET) %x",
1659 hdr
= g_base64_decode(packet_hdr
, &hdr_sz
);
1660 if (!hdr
|| !hdr_sz
) {
1661 error_setg(errp
, "SEV: Failed to decode sequence header");
1665 data
= g_base64_decode(secret
, &data_sz
);
1666 if (!data
|| !data_sz
) {
1667 error_setg(errp
, "SEV: Failed to decode data");
1671 hva
= gpa2hva(&mr
, gpa
, data_sz
, errp
);
1673 error_prepend(errp
, "SEV: Failed to calculate guest address: ");
1677 input
.hdr_uaddr
= (uint64_t)(unsigned long)hdr
;
1678 input
.hdr_len
= hdr_sz
;
1680 input
.trans_uaddr
= (uint64_t)(unsigned long)data
;
1681 input
.trans_len
= data_sz
;
1683 input
.guest_uaddr
= (uint64_t)(unsigned long)hva
;
1684 input
.guest_len
= data_sz
;
1686 trace_kvm_sev_launch_secret(gpa
, input
.guest_uaddr
,
1687 input
.trans_uaddr
, input
.trans_len
);
1689 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_LAUNCH_SECRET
,
1692 error_setg(errp
, "SEV: failed to inject secret ret=%d fw_error=%d '%s'",
1693 ret
, error
, fw_error_to_str(error
));
1700 #define SEV_SECRET_GUID "4c2eb361-7d9b-4cc3-8081-127c90d3d294"
1701 struct sev_secret_area
{
1706 void qmp_sev_inject_launch_secret(const char *packet_hdr
,
1708 bool has_gpa
, uint64_t gpa
,
1711 if (!sev_enabled()) {
1712 error_setg(errp
, "SEV not enabled for guest");
1717 struct sev_secret_area
*area
;
1719 if (!pc_system_ovmf_table_find(SEV_SECRET_GUID
, &data
, NULL
)) {
1720 error_setg(errp
, "SEV: no secret area found in OVMF,"
1721 " gpa must be specified.");
1724 area
= (struct sev_secret_area
*)data
;
1728 sev_inject_launch_secret(packet_hdr
, secret
, gpa
, errp
);
1732 sev_es_parse_reset_block(SevInfoBlock
*info
, uint32_t *addr
)
1734 if (!info
->reset_addr
) {
1735 error_report("SEV-ES reset address is zero");
1739 *addr
= info
->reset_addr
;
1745 sev_es_find_reset_vector(void *flash_ptr
, uint64_t flash_size
,
1748 QemuUUID info_guid
, *guid
;
1754 * Initialize the address to zero. An address of zero with a successful
1755 * return code indicates that SEV-ES is not active.
1760 * Extract the AP reset vector for SEV-ES guests by locating the SEV GUID.
1761 * The SEV GUID is located on its own (original implementation) or within
1762 * the Firmware GUID Table (new implementation), either of which are
1763 * located 32 bytes from the end of the flash.
1765 * Check the Firmware GUID Table first.
1767 if (pc_system_ovmf_table_find(SEV_INFO_BLOCK_GUID
, &data
, NULL
)) {
1768 return sev_es_parse_reset_block((SevInfoBlock
*)data
, addr
);
1772 * SEV info block not found in the Firmware GUID Table (or there isn't
1773 * a Firmware GUID Table), fall back to the original implementation.
1775 data
= flash_ptr
+ flash_size
- 0x20;
1777 qemu_uuid_parse(SEV_INFO_BLOCK_GUID
, &info_guid
);
1778 info_guid
= qemu_uuid_bswap(info_guid
); /* GUIDs are LE */
1780 guid
= (QemuUUID
*)(data
- sizeof(info_guid
));
1781 if (!qemu_uuid_is_equal(guid
, &info_guid
)) {
1782 error_report("SEV information block/Firmware GUID Table block not found in pflash rom");
1786 len
= (uint16_t *)((uint8_t *)guid
- sizeof(*len
));
1787 info
= (SevInfoBlock
*)(data
- le16_to_cpu(*len
));
1789 return sev_es_parse_reset_block(info
, addr
);
1792 void sev_es_set_reset_vector(CPUState
*cpu
)
1796 ConfidentialGuestSupport
*cgs
= MACHINE(qdev_get_machine())->cgs
;
1797 SevCommonState
*sev_common
= SEV_COMMON(
1798 object_dynamic_cast(OBJECT(cgs
), TYPE_SEV_COMMON
));
1800 /* Only update if we have valid reset information */
1801 if (!sev_common
|| !sev_common
->reset_data_valid
) {
1805 /* Do not update the BSP reset state */
1806 if (cpu
->cpu_index
== 0) {
1813 cpu_x86_load_seg_cache(env
, R_CS
, 0xf000, sev_common
->reset_cs
, 0xffff,
1814 DESC_P_MASK
| DESC_S_MASK
| DESC_CS_MASK
|
1815 DESC_R_MASK
| DESC_A_MASK
);
1817 env
->eip
= sev_common
->reset_ip
;
1820 int sev_es_save_reset_vector(void *flash_ptr
, uint64_t flash_size
)
1825 SevCommonState
*sev_common
= SEV_COMMON(MACHINE(qdev_get_machine())->cgs
);
1827 if (!sev_es_enabled()) {
1832 ret
= sev_es_find_reset_vector(flash_ptr
, flash_size
,
1839 sev_common
->reset_cs
= addr
& 0xffff0000;
1840 sev_common
->reset_ip
= addr
& 0x0000ffff;
1841 sev_common
->reset_data_valid
= true;
1844 sev_es_set_reset_vector(cpu
);
1851 static const QemuUUID sev_hash_table_header_guid
= {
1852 .data
= UUID_LE(0x9438d606, 0x4f22, 0x4cc9, 0xb4, 0x79, 0xa7, 0x93,
1853 0xd4, 0x11, 0xfd, 0x21)
1856 static const QemuUUID sev_kernel_entry_guid
= {
1857 .data
= UUID_LE(0x4de79437, 0xabd2, 0x427f, 0xb8, 0x35, 0xd5, 0xb1,
1858 0x72, 0xd2, 0x04, 0x5b)
1860 static const QemuUUID sev_initrd_entry_guid
= {
1861 .data
= UUID_LE(0x44baf731, 0x3a2f, 0x4bd7, 0x9a, 0xf1, 0x41, 0xe2,
1862 0x91, 0x69, 0x78, 0x1d)
1864 static const QemuUUID sev_cmdline_entry_guid
= {
1865 .data
= UUID_LE(0x97d02dd8, 0xbd20, 0x4c94, 0xaa, 0x78, 0xe7, 0x71,
1866 0x4d, 0x36, 0xab, 0x2a)
1869 static bool build_kernel_loader_hashes(PaddedSevHashTable
*padded_ht
,
1870 SevKernelLoaderContext
*ctx
,
1874 uint8_t cmdline_hash
[HASH_SIZE
];
1875 uint8_t initrd_hash
[HASH_SIZE
];
1876 uint8_t kernel_hash
[HASH_SIZE
];
1878 size_t hash_len
= HASH_SIZE
;
1881 * Calculate hash of kernel command-line with the terminating null byte. If
1882 * the user doesn't supply a command-line via -append, the 1-byte "\0" will
1885 hashp
= cmdline_hash
;
1886 if (qcrypto_hash_bytes(QCRYPTO_HASH_ALGO_SHA256
, ctx
->cmdline_data
,
1887 ctx
->cmdline_size
, &hashp
, &hash_len
, errp
) < 0) {
1890 assert(hash_len
== HASH_SIZE
);
1893 * Calculate hash of initrd. If the user doesn't supply an initrd via
1894 * -initrd, an empty buffer will be used (ctx->initrd_size == 0).
1896 hashp
= initrd_hash
;
1897 if (qcrypto_hash_bytes(QCRYPTO_HASH_ALGO_SHA256
, ctx
->initrd_data
,
1898 ctx
->initrd_size
, &hashp
, &hash_len
, errp
) < 0) {
1901 assert(hash_len
== HASH_SIZE
);
1903 /* Calculate hash of the kernel */
1904 hashp
= kernel_hash
;
1905 struct iovec iov
[2] = {
1906 { .iov_base
= ctx
->setup_data
, .iov_len
= ctx
->setup_size
},
1907 { .iov_base
= ctx
->kernel_data
, .iov_len
= ctx
->kernel_size
}
1909 if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALGO_SHA256
, iov
, ARRAY_SIZE(iov
),
1910 &hashp
, &hash_len
, errp
) < 0) {
1913 assert(hash_len
== HASH_SIZE
);
1915 ht
= &padded_ht
->ht
;
1917 ht
->guid
= sev_hash_table_header_guid
;
1918 ht
->len
= sizeof(*ht
);
1920 ht
->cmdline
.guid
= sev_cmdline_entry_guid
;
1921 ht
->cmdline
.len
= sizeof(ht
->cmdline
);
1922 memcpy(ht
->cmdline
.hash
, cmdline_hash
, sizeof(ht
->cmdline
.hash
));
1924 ht
->initrd
.guid
= sev_initrd_entry_guid
;
1925 ht
->initrd
.len
= sizeof(ht
->initrd
);
1926 memcpy(ht
->initrd
.hash
, initrd_hash
, sizeof(ht
->initrd
.hash
));
1928 ht
->kernel
.guid
= sev_kernel_entry_guid
;
1929 ht
->kernel
.len
= sizeof(ht
->kernel
);
1930 memcpy(ht
->kernel
.hash
, kernel_hash
, sizeof(ht
->kernel
.hash
));
1932 /* zero the excess data so the measurement can be reliably calculated */
1933 memset(padded_ht
->padding
, 0, sizeof(padded_ht
->padding
));
1938 static bool sev_snp_build_kernel_loader_hashes(SevCommonState
*sev_common
,
1939 SevHashTableDescriptor
*area
,
1940 SevKernelLoaderContext
*ctx
,
1944 * SNP: Populate the hashes table in an area that later in
1945 * snp_launch_update_kernel_hashes() will be copied to the guest memory
1948 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(sev_common
);
1949 sev_snp_guest
->kernel_hashes_offset
= area
->base
& ~TARGET_PAGE_MASK
;
1950 sev_snp_guest
->kernel_hashes_data
= g_new0(PaddedSevHashTable
, 1);
1951 return build_kernel_loader_hashes(sev_snp_guest
->kernel_hashes_data
, ctx
, errp
);
1954 static bool sev_build_kernel_loader_hashes(SevCommonState
*sev_common
,
1955 SevHashTableDescriptor
*area
,
1956 SevKernelLoaderContext
*ctx
,
1959 PaddedSevHashTable
*padded_ht
;
1960 hwaddr mapped_len
= sizeof(*padded_ht
);
1961 MemTxAttrs attrs
= { 0 };
1965 * Populate the hashes table in the guest's memory at the OVMF-designated
1966 * area for the SEV hashes table
1968 padded_ht
= address_space_map(&address_space_memory
, area
->base
,
1969 &mapped_len
, true, attrs
);
1970 if (!padded_ht
|| mapped_len
!= sizeof(*padded_ht
)) {
1971 error_setg(errp
, "SEV: cannot map hashes table guest memory area");
1975 if (build_kernel_loader_hashes(padded_ht
, ctx
, errp
)) {
1976 if (sev_encrypt_flash(area
->base
, (uint8_t *)padded_ht
,
1977 sizeof(*padded_ht
), errp
) < 0) {
1984 address_space_unmap(&address_space_memory
, padded_ht
,
1985 mapped_len
, true, mapped_len
);
1991 * Add the hashes of the linux kernel/initrd/cmdline to an encrypted guest page
1992 * which is included in SEV's initial memory measurement.
1994 bool sev_add_kernel_loader_hashes(SevKernelLoaderContext
*ctx
, Error
**errp
)
1997 SevHashTableDescriptor
*area
;
1998 SevCommonState
*sev_common
= SEV_COMMON(MACHINE(qdev_get_machine())->cgs
);
1999 SevCommonStateClass
*klass
= SEV_COMMON_GET_CLASS(sev_common
);
2002 * Only add the kernel hashes if the sev-guest configuration explicitly
2003 * stated kernel-hashes=on.
2005 if (!sev_common
->kernel_hashes
) {
2009 if (!pc_system_ovmf_table_find(SEV_HASH_TABLE_RV_GUID
, &data
, NULL
)) {
2010 error_setg(errp
, "SEV: kernel specified but guest firmware "
2011 "has no hashes table GUID");
2015 area
= (SevHashTableDescriptor
*)data
;
2016 if (!area
->base
|| area
->size
< sizeof(PaddedSevHashTable
)) {
2017 error_setg(errp
, "SEV: guest firmware hashes table area is invalid "
2018 "(base=0x%x size=0x%x)", area
->base
, area
->size
);
2022 return klass
->build_kernel_loader_hashes(sev_common
, area
, ctx
, errp
);
2026 sev_common_get_sev_device(Object
*obj
, Error
**errp
)
2028 return g_strdup(SEV_COMMON(obj
)->sev_device
);
2032 sev_common_set_sev_device(Object
*obj
, const char *value
, Error
**errp
)
2034 SEV_COMMON(obj
)->sev_device
= g_strdup(value
);
2037 static bool sev_common_get_kernel_hashes(Object
*obj
, Error
**errp
)
2039 return SEV_COMMON(obj
)->kernel_hashes
;
2042 static void sev_common_set_kernel_hashes(Object
*obj
, bool value
, Error
**errp
)
2044 SEV_COMMON(obj
)->kernel_hashes
= value
;
2048 sev_common_class_init(ObjectClass
*oc
, void *data
)
2050 ConfidentialGuestSupportClass
*klass
= CONFIDENTIAL_GUEST_SUPPORT_CLASS(oc
);
2052 klass
->kvm_init
= sev_common_kvm_init
;
2054 object_class_property_add_str(oc
, "sev-device",
2055 sev_common_get_sev_device
,
2056 sev_common_set_sev_device
);
2057 object_class_property_set_description(oc
, "sev-device",
2058 "SEV device to use");
2059 object_class_property_add_bool(oc
, "kernel-hashes",
2060 sev_common_get_kernel_hashes
,
2061 sev_common_set_kernel_hashes
);
2062 object_class_property_set_description(oc
, "kernel-hashes",
2063 "add kernel hashes to guest firmware for measured Linux boot");
2067 sev_common_instance_init(Object
*obj
)
2069 SevCommonState
*sev_common
= SEV_COMMON(obj
);
2071 sev_common
->kvm_type
= -1;
2073 sev_common
->sev_device
= g_strdup(DEFAULT_SEV_DEVICE
);
2075 object_property_add_uint32_ptr(obj
, "cbitpos", &sev_common
->cbitpos
,
2076 OBJ_PROP_FLAG_READWRITE
);
2077 object_property_add_uint32_ptr(obj
, "reduced-phys-bits",
2078 &sev_common
->reduced_phys_bits
,
2079 OBJ_PROP_FLAG_READWRITE
);
2082 /* sev guest info common to sev/sev-es/sev-snp */
2083 static const TypeInfo sev_common_info
= {
2084 .parent
= TYPE_X86_CONFIDENTIAL_GUEST
,
2085 .name
= TYPE_SEV_COMMON
,
2086 .instance_size
= sizeof(SevCommonState
),
2087 .instance_init
= sev_common_instance_init
,
2088 .class_size
= sizeof(SevCommonStateClass
),
2089 .class_init
= sev_common_class_init
,
2091 .interfaces
= (InterfaceInfo
[]) {
2092 { TYPE_USER_CREATABLE
},
2098 sev_guest_get_dh_cert_file(Object
*obj
, Error
**errp
)
2100 return g_strdup(SEV_GUEST(obj
)->dh_cert_file
);
2104 sev_guest_set_dh_cert_file(Object
*obj
, const char *value
, Error
**errp
)
2106 SEV_GUEST(obj
)->dh_cert_file
= g_strdup(value
);
2110 sev_guest_get_session_file(Object
*obj
, Error
**errp
)
2112 SevGuestState
*sev_guest
= SEV_GUEST(obj
);
2114 return sev_guest
->session_file
? g_strdup(sev_guest
->session_file
) : NULL
;
2118 sev_guest_set_session_file(Object
*obj
, const char *value
, Error
**errp
)
2120 SEV_GUEST(obj
)->session_file
= g_strdup(value
);
2123 static void sev_guest_get_legacy_vm_type(Object
*obj
, Visitor
*v
,
2124 const char *name
, void *opaque
,
2127 SevGuestState
*sev_guest
= SEV_GUEST(obj
);
2128 OnOffAuto legacy_vm_type
= sev_guest
->legacy_vm_type
;
2130 visit_type_OnOffAuto(v
, name
, &legacy_vm_type
, errp
);
2133 static void sev_guest_set_legacy_vm_type(Object
*obj
, Visitor
*v
,
2134 const char *name
, void *opaque
,
2137 SevGuestState
*sev_guest
= SEV_GUEST(obj
);
2139 visit_type_OnOffAuto(v
, name
, &sev_guest
->legacy_vm_type
, errp
);
2143 sev_guest_class_init(ObjectClass
*oc
, void *data
)
2145 SevCommonStateClass
*klass
= SEV_COMMON_CLASS(oc
);
2146 X86ConfidentialGuestClass
*x86_klass
= X86_CONFIDENTIAL_GUEST_CLASS(oc
);
2148 klass
->build_kernel_loader_hashes
= sev_build_kernel_loader_hashes
;
2149 klass
->launch_start
= sev_launch_start
;
2150 klass
->launch_finish
= sev_launch_finish
;
2151 klass
->launch_update_data
= sev_launch_update_data
;
2152 klass
->kvm_init
= sev_kvm_init
;
2153 x86_klass
->kvm_type
= sev_kvm_type
;
2155 object_class_property_add_str(oc
, "dh-cert-file",
2156 sev_guest_get_dh_cert_file
,
2157 sev_guest_set_dh_cert_file
);
2158 object_class_property_set_description(oc
, "dh-cert-file",
2159 "guest owners DH certificate (encoded with base64)");
2160 object_class_property_add_str(oc
, "session-file",
2161 sev_guest_get_session_file
,
2162 sev_guest_set_session_file
);
2163 object_class_property_set_description(oc
, "session-file",
2164 "guest owners session parameters (encoded with base64)");
2165 object_class_property_add(oc
, "legacy-vm-type", "OnOffAuto",
2166 sev_guest_get_legacy_vm_type
,
2167 sev_guest_set_legacy_vm_type
, NULL
, NULL
);
2168 object_class_property_set_description(oc
, "legacy-vm-type",
2169 "use legacy VM type to maintain measurement compatibility with older QEMU or kernel versions.");
2173 sev_guest_instance_init(Object
*obj
)
2175 SevGuestState
*sev_guest
= SEV_GUEST(obj
);
2177 sev_guest
->policy
= DEFAULT_GUEST_POLICY
;
2178 object_property_add_uint32_ptr(obj
, "handle", &sev_guest
->handle
,
2179 OBJ_PROP_FLAG_READWRITE
);
2180 object_property_add_uint32_ptr(obj
, "policy", &sev_guest
->policy
,
2181 OBJ_PROP_FLAG_READWRITE
);
2182 object_apply_compat_props(obj
);
2184 sev_guest
->legacy_vm_type
= ON_OFF_AUTO_AUTO
;
2187 /* guest info specific sev/sev-es */
2188 static const TypeInfo sev_guest_info
= {
2189 .parent
= TYPE_SEV_COMMON
,
2190 .name
= TYPE_SEV_GUEST
,
2191 .instance_size
= sizeof(SevGuestState
),
2192 .instance_init
= sev_guest_instance_init
,
2193 .class_init
= sev_guest_class_init
,
2197 sev_snp_guest_get_policy(Object
*obj
, Visitor
*v
, const char *name
,
2198 void *opaque
, Error
**errp
)
2200 visit_type_uint64(v
, name
,
2201 (uint64_t *)&SEV_SNP_GUEST(obj
)->kvm_start_conf
.policy
,
2206 sev_snp_guest_set_policy(Object
*obj
, Visitor
*v
, const char *name
,
2207 void *opaque
, Error
**errp
)
2209 visit_type_uint64(v
, name
,
2210 (uint64_t *)&SEV_SNP_GUEST(obj
)->kvm_start_conf
.policy
,
2215 sev_snp_guest_get_guest_visible_workarounds(Object
*obj
, Error
**errp
)
2217 return g_strdup(SEV_SNP_GUEST(obj
)->guest_visible_workarounds
);
2221 sev_snp_guest_set_guest_visible_workarounds(Object
*obj
, const char *value
,
2224 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2225 struct kvm_sev_snp_launch_start
*start
= &sev_snp_guest
->kvm_start_conf
;
2226 g_autofree guchar
*blob
;
2229 g_free(sev_snp_guest
->guest_visible_workarounds
);
2231 /* store the base64 str so we don't need to re-encode in getter */
2232 sev_snp_guest
->guest_visible_workarounds
= g_strdup(value
);
2234 blob
= qbase64_decode(sev_snp_guest
->guest_visible_workarounds
,
2240 if (len
!= sizeof(start
->gosvw
)) {
2241 error_setg(errp
, "parameter length of %" G_GSIZE_FORMAT
2242 " exceeds max of %zu",
2243 len
, sizeof(start
->gosvw
));
2247 memcpy(start
->gosvw
, blob
, len
);
2251 sev_snp_guest_get_id_block(Object
*obj
, Error
**errp
)
2253 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2255 return g_strdup(sev_snp_guest
->id_block_base64
);
2259 sev_snp_guest_set_id_block(Object
*obj
, const char *value
, Error
**errp
)
2261 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2262 struct kvm_sev_snp_launch_finish
*finish
= &sev_snp_guest
->kvm_finish_conf
;
2265 finish
->id_block_en
= 0;
2266 g_free(sev_snp_guest
->id_block
);
2267 g_free(sev_snp_guest
->id_block_base64
);
2269 /* store the base64 str so we don't need to re-encode in getter */
2270 sev_snp_guest
->id_block_base64
= g_strdup(value
);
2271 sev_snp_guest
->id_block
=
2272 qbase64_decode(sev_snp_guest
->id_block_base64
, -1, &len
, errp
);
2274 if (!sev_snp_guest
->id_block
) {
2278 if (len
!= KVM_SEV_SNP_ID_BLOCK_SIZE
) {
2279 error_setg(errp
, "parameter length of %" G_GSIZE_FORMAT
2281 len
, KVM_SEV_SNP_ID_BLOCK_SIZE
);
2285 finish
->id_block_en
= 1;
2286 finish
->id_block_uaddr
= (uintptr_t)sev_snp_guest
->id_block
;
2290 sev_snp_guest_get_id_auth(Object
*obj
, Error
**errp
)
2292 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2294 return g_strdup(sev_snp_guest
->id_auth_base64
);
2298 sev_snp_guest_set_id_auth(Object
*obj
, const char *value
, Error
**errp
)
2300 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2301 struct kvm_sev_snp_launch_finish
*finish
= &sev_snp_guest
->kvm_finish_conf
;
2304 finish
->id_auth_uaddr
= 0;
2305 g_free(sev_snp_guest
->id_auth
);
2306 g_free(sev_snp_guest
->id_auth_base64
);
2308 /* store the base64 str so we don't need to re-encode in getter */
2309 sev_snp_guest
->id_auth_base64
= g_strdup(value
);
2310 sev_snp_guest
->id_auth
=
2311 qbase64_decode(sev_snp_guest
->id_auth_base64
, -1, &len
, errp
);
2313 if (!sev_snp_guest
->id_auth
) {
2317 if (len
> KVM_SEV_SNP_ID_AUTH_SIZE
) {
2318 error_setg(errp
, "parameter length:ID_AUTH %" G_GSIZE_FORMAT
2319 " exceeds max of %u",
2320 len
, KVM_SEV_SNP_ID_AUTH_SIZE
);
2324 finish
->id_auth_uaddr
= (uintptr_t)sev_snp_guest
->id_auth
;
2328 sev_snp_guest_get_author_key_enabled(Object
*obj
, Error
**errp
)
2330 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2332 return !!sev_snp_guest
->kvm_finish_conf
.auth_key_en
;
2336 sev_snp_guest_set_author_key_enabled(Object
*obj
, bool value
, Error
**errp
)
2338 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2340 sev_snp_guest
->kvm_finish_conf
.auth_key_en
= value
;
2344 sev_snp_guest_get_vcek_disabled(Object
*obj
, Error
**errp
)
2346 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2348 return !!sev_snp_guest
->kvm_finish_conf
.vcek_disabled
;
2352 sev_snp_guest_set_vcek_disabled(Object
*obj
, bool value
, Error
**errp
)
2354 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2356 sev_snp_guest
->kvm_finish_conf
.vcek_disabled
= value
;
2360 sev_snp_guest_get_host_data(Object
*obj
, Error
**errp
)
2362 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2364 return g_strdup(sev_snp_guest
->host_data
);
2368 sev_snp_guest_set_host_data(Object
*obj
, const char *value
, Error
**errp
)
2370 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2371 struct kvm_sev_snp_launch_finish
*finish
= &sev_snp_guest
->kvm_finish_conf
;
2372 g_autofree guchar
*blob
;
2375 g_free(sev_snp_guest
->host_data
);
2377 /* store the base64 str so we don't need to re-encode in getter */
2378 sev_snp_guest
->host_data
= g_strdup(value
);
2380 blob
= qbase64_decode(sev_snp_guest
->host_data
, -1, &len
, errp
);
2386 if (len
!= sizeof(finish
->host_data
)) {
2387 error_setg(errp
, "parameter length of %" G_GSIZE_FORMAT
2388 " not equal to %zu",
2389 len
, sizeof(finish
->host_data
));
2393 memcpy(finish
->host_data
, blob
, len
);
2397 sev_snp_guest_class_init(ObjectClass
*oc
, void *data
)
2399 SevCommonStateClass
*klass
= SEV_COMMON_CLASS(oc
);
2400 X86ConfidentialGuestClass
*x86_klass
= X86_CONFIDENTIAL_GUEST_CLASS(oc
);
2402 klass
->build_kernel_loader_hashes
= sev_snp_build_kernel_loader_hashes
;
2403 klass
->launch_start
= sev_snp_launch_start
;
2404 klass
->launch_finish
= sev_snp_launch_finish
;
2405 klass
->launch_update_data
= sev_snp_launch_update_data
;
2406 klass
->kvm_init
= sev_snp_kvm_init
;
2407 x86_klass
->mask_cpuid_features
= sev_snp_mask_cpuid_features
;
2408 x86_klass
->kvm_type
= sev_snp_kvm_type
;
2410 object_class_property_add(oc
, "policy", "uint64",
2411 sev_snp_guest_get_policy
,
2412 sev_snp_guest_set_policy
, NULL
, NULL
);
2413 object_class_property_add_str(oc
, "guest-visible-workarounds",
2414 sev_snp_guest_get_guest_visible_workarounds
,
2415 sev_snp_guest_set_guest_visible_workarounds
);
2416 object_class_property_add_str(oc
, "id-block",
2417 sev_snp_guest_get_id_block
,
2418 sev_snp_guest_set_id_block
);
2419 object_class_property_add_str(oc
, "id-auth",
2420 sev_snp_guest_get_id_auth
,
2421 sev_snp_guest_set_id_auth
);
2422 object_class_property_add_bool(oc
, "author-key-enabled",
2423 sev_snp_guest_get_author_key_enabled
,
2424 sev_snp_guest_set_author_key_enabled
);
2425 object_class_property_add_bool(oc
, "vcek-disabled",
2426 sev_snp_guest_get_vcek_disabled
,
2427 sev_snp_guest_set_vcek_disabled
);
2428 object_class_property_add_str(oc
, "host-data",
2429 sev_snp_guest_get_host_data
,
2430 sev_snp_guest_set_host_data
);
2434 sev_snp_guest_instance_init(Object
*obj
)
2436 ConfidentialGuestSupport
*cgs
= CONFIDENTIAL_GUEST_SUPPORT(obj
);
2437 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2439 cgs
->require_guest_memfd
= true;
2441 /* default init/start/finish params for kvm */
2442 sev_snp_guest
->kvm_start_conf
.policy
= DEFAULT_SEV_SNP_POLICY
;
2445 /* guest info specific to sev-snp */
2446 static const TypeInfo sev_snp_guest_info
= {
2447 .parent
= TYPE_SEV_COMMON
,
2448 .name
= TYPE_SEV_SNP_GUEST
,
2449 .instance_size
= sizeof(SevSnpGuestState
),
2450 .class_init
= sev_snp_guest_class_init
,
2451 .instance_init
= sev_snp_guest_instance_init
,
2455 sev_register_types(void)
2457 type_register_static(&sev_common_info
);
2458 type_register_static(&sev_guest_info
);
2459 type_register_static(&sev_snp_guest_info
);
2462 type_init(sev_register_types
);