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 */
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 error_setg(errp
, "SEV is not configured");
595 sev_device
= object_property_get_str(OBJECT(sev_common
), "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_launch_update_data(SevCommonState
*sev_common
, hwaddr gpa
,
950 uint8_t *addr
, size_t len
)
953 struct kvm_sev_launch_update_data update
;
959 update
.uaddr
= (uintptr_t)addr
;
961 trace_kvm_sev_launch_update_data(addr
, len
);
962 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_LAUNCH_UPDATE_DATA
,
965 error_report("%s: LAUNCH_UPDATE ret=%d fw_error=%d '%s'",
966 __func__
, ret
, fw_error
, fw_error_to_str(fw_error
));
973 sev_launch_update_vmsa(SevGuestState
*sev_guest
)
977 ret
= sev_ioctl(SEV_COMMON(sev_guest
)->sev_fd
, KVM_SEV_LAUNCH_UPDATE_VMSA
,
980 error_report("%s: LAUNCH_UPDATE_VMSA ret=%d fw_error=%d '%s'",
981 __func__
, ret
, fw_error
, fw_error_to_str(fw_error
));
988 sev_launch_get_measure(Notifier
*notifier
, void *unused
)
990 SevCommonState
*sev_common
= SEV_COMMON(MACHINE(qdev_get_machine())->cgs
);
991 SevGuestState
*sev_guest
= SEV_GUEST(sev_common
);
993 g_autofree guchar
*data
= NULL
;
994 struct kvm_sev_launch_measure measurement
= {};
996 if (!sev_check_state(sev_common
, SEV_STATE_LAUNCH_UPDATE
)) {
1000 if (sev_es_enabled()) {
1001 /* measure all the VM save areas before getting launch_measure */
1002 ret
= sev_launch_update_vmsa(sev_guest
);
1006 kvm_mark_guest_state_protected();
1009 /* query the measurement blob length */
1010 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_LAUNCH_MEASURE
,
1011 &measurement
, &error
);
1012 if (!measurement
.len
) {
1013 error_report("%s: LAUNCH_MEASURE ret=%d fw_error=%d '%s'",
1014 __func__
, ret
, error
, fw_error_to_str(errno
));
1018 data
= g_new0(guchar
, measurement
.len
);
1019 measurement
.uaddr
= (unsigned long)data
;
1021 /* get the measurement blob */
1022 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_LAUNCH_MEASURE
,
1023 &measurement
, &error
);
1025 error_report("%s: LAUNCH_MEASURE ret=%d fw_error=%d '%s'",
1026 __func__
, ret
, error
, fw_error_to_str(errno
));
1030 sev_set_guest_state(sev_common
, SEV_STATE_LAUNCH_SECRET
);
1032 /* encode the measurement value and emit the event */
1033 sev_guest
->measurement
= g_base64_encode(data
, measurement
.len
);
1034 trace_kvm_sev_launch_measurement(sev_guest
->measurement
);
1037 static char *sev_get_launch_measurement(void)
1039 ConfidentialGuestSupport
*cgs
= MACHINE(qdev_get_machine())->cgs
;
1040 SevGuestState
*sev_guest
=
1041 (SevGuestState
*)object_dynamic_cast(OBJECT(cgs
), TYPE_SEV_GUEST
);
1044 SEV_COMMON(sev_guest
)->state
>= SEV_STATE_LAUNCH_SECRET
) {
1045 return g_strdup(sev_guest
->measurement
);
1051 SevLaunchMeasureInfo
*qmp_query_sev_launch_measure(Error
**errp
)
1054 SevLaunchMeasureInfo
*info
;
1056 data
= sev_get_launch_measurement();
1058 error_setg(errp
, "SEV launch measurement is not available");
1062 info
= g_malloc0(sizeof(*info
));
1068 static Notifier sev_machine_done_notify
= {
1069 .notify
= sev_launch_get_measure
,
1073 sev_launch_finish(SevCommonState
*sev_common
)
1077 trace_kvm_sev_launch_finish();
1078 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_LAUNCH_FINISH
, 0,
1081 error_report("%s: LAUNCH_FINISH ret=%d fw_error=%d '%s'",
1082 __func__
, ret
, error
, fw_error_to_str(error
));
1086 sev_set_guest_state(sev_common
, SEV_STATE_RUNNING
);
1088 /* add migration blocker */
1089 error_setg(&sev_mig_blocker
,
1090 "SEV: Migration is not implemented");
1091 migrate_add_blocker(&sev_mig_blocker
, &error_fatal
);
1095 snp_launch_update_data(uint64_t gpa
, void *hva
, size_t len
, int type
)
1097 SevLaunchUpdateData
*data
;
1099 data
= g_new0(SevLaunchUpdateData
, 1);
1105 QTAILQ_INSERT_TAIL(&launch_update
, data
, next
);
1111 sev_snp_launch_update_data(SevCommonState
*sev_common
, hwaddr gpa
,
1112 uint8_t *ptr
, size_t len
)
1114 int ret
= snp_launch_update_data(gpa
, ptr
, len
,
1115 KVM_SEV_SNP_PAGE_TYPE_NORMAL
);
1120 sev_snp_cpuid_info_fill(SnpCpuidInfo
*snp_cpuid_info
,
1121 const KvmCpuidInfo
*kvm_cpuid_info
)
1125 if (kvm_cpuid_info
->cpuid
.nent
> SNP_CPUID_FUNCTION_MAXCOUNT
) {
1126 error_report("SEV-SNP: CPUID entry count (%d) exceeds max (%d)",
1127 kvm_cpuid_info
->cpuid
.nent
, SNP_CPUID_FUNCTION_MAXCOUNT
);
1131 memset(snp_cpuid_info
, 0, sizeof(*snp_cpuid_info
));
1133 for (i
= 0; i
< kvm_cpuid_info
->cpuid
.nent
; i
++) {
1134 const struct kvm_cpuid_entry2
*kvm_cpuid_entry
;
1135 SnpCpuidFunc
*snp_cpuid_entry
;
1137 kvm_cpuid_entry
= &kvm_cpuid_info
->entries
[i
];
1138 snp_cpuid_entry
= &snp_cpuid_info
->entries
[i
];
1140 snp_cpuid_entry
->eax_in
= kvm_cpuid_entry
->function
;
1141 if (kvm_cpuid_entry
->flags
== KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) {
1142 snp_cpuid_entry
->ecx_in
= kvm_cpuid_entry
->index
;
1144 snp_cpuid_entry
->eax
= kvm_cpuid_entry
->eax
;
1145 snp_cpuid_entry
->ebx
= kvm_cpuid_entry
->ebx
;
1146 snp_cpuid_entry
->ecx
= kvm_cpuid_entry
->ecx
;
1147 snp_cpuid_entry
->edx
= kvm_cpuid_entry
->edx
;
1150 * Guest kernels will calculate EBX themselves using the 0xD
1151 * subfunctions corresponding to the individual XSAVE areas, so only
1152 * encode the base XSAVE size in the initial leaves, corresponding
1153 * to the initial XCR0=1 state.
1155 if (snp_cpuid_entry
->eax_in
== 0xD &&
1156 (snp_cpuid_entry
->ecx_in
== 0x0 || snp_cpuid_entry
->ecx_in
== 0x1)) {
1157 snp_cpuid_entry
->ebx
= 0x240;
1158 snp_cpuid_entry
->xcr0_in
= 1;
1159 snp_cpuid_entry
->xss_in
= 0;
1163 snp_cpuid_info
->count
= i
;
1169 snp_launch_update_cpuid(uint32_t cpuid_addr
, void *hva
, size_t cpuid_len
)
1171 KvmCpuidInfo kvm_cpuid_info
= {0};
1172 SnpCpuidInfo snp_cpuid_info
;
1173 CPUState
*cs
= first_cpu
;
1177 assert(sizeof(snp_cpuid_info
) <= cpuid_len
);
1179 /* get the cpuid list from KVM */
1181 kvm_cpuid_info
.cpuid
.nent
= ++i
;
1182 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_CPUID2
, &kvm_cpuid_info
);
1183 } while (ret
== -E2BIG
);
1186 error_report("SEV-SNP: unable to query CPUID values for CPU: '%s'",
1191 ret
= sev_snp_cpuid_info_fill(&snp_cpuid_info
, &kvm_cpuid_info
);
1193 error_report("SEV-SNP: failed to generate CPUID table information");
1197 memcpy(hva
, &snp_cpuid_info
, sizeof(snp_cpuid_info
));
1199 return snp_launch_update_data(cpuid_addr
, hva
, cpuid_len
,
1200 KVM_SEV_SNP_PAGE_TYPE_CPUID
);
1204 snp_launch_update_kernel_hashes(SevSnpGuestState
*sev_snp
, uint32_t addr
,
1205 void *hva
, uint32_t len
)
1207 int type
= KVM_SEV_SNP_PAGE_TYPE_ZERO
;
1208 if (sev_snp
->parent_obj
.kernel_hashes
) {
1209 assert(sev_snp
->kernel_hashes_data
);
1210 assert((sev_snp
->kernel_hashes_offset
+
1211 sizeof(*sev_snp
->kernel_hashes_data
)) <= len
);
1212 memset(hva
, 0, len
);
1213 memcpy(hva
+ sev_snp
->kernel_hashes_offset
, sev_snp
->kernel_hashes_data
,
1214 sizeof(*sev_snp
->kernel_hashes_data
));
1215 type
= KVM_SEV_SNP_PAGE_TYPE_NORMAL
;
1217 return snp_launch_update_data(addr
, hva
, len
, type
);
1221 snp_metadata_desc_to_page_type(int desc_type
)
1223 switch (desc_type
) {
1224 /* Add the umeasured prevalidated pages as a zero page */
1225 case SEV_DESC_TYPE_SNP_SEC_MEM
: return KVM_SEV_SNP_PAGE_TYPE_ZERO
;
1226 case SEV_DESC_TYPE_SNP_SECRETS
: return KVM_SEV_SNP_PAGE_TYPE_SECRETS
;
1227 case SEV_DESC_TYPE_CPUID
: return KVM_SEV_SNP_PAGE_TYPE_CPUID
;
1229 return KVM_SEV_SNP_PAGE_TYPE_ZERO
;
1234 snp_populate_metadata_pages(SevSnpGuestState
*sev_snp
,
1235 OvmfSevMetadata
*metadata
)
1237 OvmfSevMetadataDesc
*desc
;
1240 MemoryRegion
*mr
= NULL
;
1242 for (i
= 0; i
< metadata
->num_desc
; i
++) {
1243 desc
= &metadata
->descs
[i
];
1245 type
= snp_metadata_desc_to_page_type(desc
->type
);
1247 hva
= gpa2hva(&mr
, desc
->base
, desc
->len
, NULL
);
1249 error_report("%s: Failed to get HVA for GPA 0x%x sz 0x%x",
1250 __func__
, desc
->base
, desc
->len
);
1254 if (type
== KVM_SEV_SNP_PAGE_TYPE_CPUID
) {
1255 ret
= snp_launch_update_cpuid(desc
->base
, hva
, desc
->len
);
1256 } else if (desc
->type
== SEV_DESC_TYPE_SNP_KERNEL_HASHES
) {
1257 ret
= snp_launch_update_kernel_hashes(sev_snp
, desc
->base
, hva
,
1260 ret
= snp_launch_update_data(desc
->base
, hva
, desc
->len
, type
);
1264 error_report("%s: Failed to add metadata page gpa 0x%x+%x type %d",
1265 __func__
, desc
->base
, desc
->len
, desc
->type
);
1272 sev_snp_launch_finish(SevCommonState
*sev_common
)
1275 Error
*local_err
= NULL
;
1276 OvmfSevMetadata
*metadata
;
1277 SevLaunchUpdateData
*data
;
1278 SevSnpGuestState
*sev_snp
= SEV_SNP_GUEST(sev_common
);
1279 struct kvm_sev_snp_launch_finish
*finish
= &sev_snp
->kvm_finish_conf
;
1282 * To boot the SNP guest, the hypervisor is required to populate the CPUID
1283 * and Secrets page before finalizing the launch flow. The location of
1284 * the secrets and CPUID page is available through the OVMF metadata GUID.
1286 metadata
= pc_system_get_ovmf_sev_metadata_ptr();
1287 if (metadata
== NULL
) {
1288 error_report("%s: Failed to locate SEV metadata header", __func__
);
1292 /* Populate all the metadata pages */
1293 snp_populate_metadata_pages(sev_snp
, metadata
);
1295 QTAILQ_FOREACH(data
, &launch_update
, next
) {
1296 ret
= sev_snp_launch_update(sev_snp
, data
);
1302 trace_kvm_sev_snp_launch_finish(sev_snp
->id_block_base64
, sev_snp
->id_auth_base64
,
1303 sev_snp
->host_data
);
1304 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_SNP_LAUNCH_FINISH
,
1307 error_report("SNP_LAUNCH_FINISH ret=%d fw_error=%d '%s'",
1308 ret
, error
, fw_error_to_str(error
));
1312 kvm_mark_guest_state_protected();
1313 sev_set_guest_state(sev_common
, SEV_STATE_RUNNING
);
1315 /* add migration blocker */
1316 error_setg(&sev_mig_blocker
,
1317 "SEV-SNP: Migration is not implemented");
1318 ret
= migrate_add_blocker(&sev_mig_blocker
, &local_err
);
1320 error_report_err(local_err
);
1321 error_free(sev_mig_blocker
);
1328 sev_vm_state_change(void *opaque
, bool running
, RunState state
)
1330 SevCommonState
*sev_common
= opaque
;
1331 SevCommonStateClass
*klass
= SEV_COMMON_GET_CLASS(opaque
);
1334 if (!sev_check_state(sev_common
, SEV_STATE_RUNNING
)) {
1335 klass
->launch_finish(sev_common
);
1340 static int sev_kvm_type(X86ConfidentialGuest
*cg
)
1342 SevCommonState
*sev_common
= SEV_COMMON(cg
);
1343 SevGuestState
*sev_guest
= SEV_GUEST(sev_common
);
1346 if (sev_common
->kvm_type
!= -1) {
1350 kvm_type
= (sev_guest
->policy
& SEV_POLICY_ES
) ?
1351 KVM_X86_SEV_ES_VM
: KVM_X86_SEV_VM
;
1352 if (kvm_is_vm_type_supported(kvm_type
) && !sev_guest
->legacy_vm_type
) {
1353 sev_common
->kvm_type
= kvm_type
;
1355 sev_common
->kvm_type
= KVM_X86_DEFAULT_VM
;
1359 return sev_common
->kvm_type
;
1362 static int sev_snp_kvm_type(X86ConfidentialGuest
*cg
)
1364 return KVM_X86_SNP_VM
;
1367 static int sev_common_kvm_init(ConfidentialGuestSupport
*cgs
, Error
**errp
)
1370 int ret
, fw_error
, cmd
;
1372 uint32_t host_cbitpos
;
1373 struct sev_user_data_status status
= {};
1374 SevCommonState
*sev_common
= SEV_COMMON(cgs
);
1375 SevCommonStateClass
*klass
= SEV_COMMON_GET_CLASS(cgs
);
1376 X86ConfidentialGuestClass
*x86_klass
=
1377 X86_CONFIDENTIAL_GUEST_GET_CLASS(cgs
);
1379 sev_common
->state
= SEV_STATE_UNINIT
;
1381 host_cpuid(0x8000001F, 0, NULL
, &ebx
, NULL
, NULL
);
1382 host_cbitpos
= ebx
& 0x3f;
1385 * The cbitpos value will be placed in bit positions 5:0 of the EBX
1386 * register of CPUID 0x8000001F. No need to verify the range as the
1387 * comparison against the host value accomplishes that.
1389 if (host_cbitpos
!= sev_common
->cbitpos
) {
1390 error_setg(errp
, "%s: cbitpos check failed, host '%d' requested '%d'",
1391 __func__
, host_cbitpos
, sev_common
->cbitpos
);
1396 * The reduced-phys-bits value will be placed in bit positions 11:6 of
1397 * the EBX register of CPUID 0x8000001F, so verify the supplied value
1398 * is in the range of 1 to 63.
1400 if (sev_common
->reduced_phys_bits
< 1 ||
1401 sev_common
->reduced_phys_bits
> 63) {
1402 error_setg(errp
, "%s: reduced_phys_bits check failed,"
1403 " it should be in the range of 1 to 63, requested '%d'",
1404 __func__
, sev_common
->reduced_phys_bits
);
1408 devname
= object_property_get_str(OBJECT(sev_common
), "sev-device", NULL
);
1409 sev_common
->sev_fd
= open(devname
, O_RDWR
);
1410 if (sev_common
->sev_fd
< 0) {
1411 error_setg(errp
, "%s: Failed to open %s '%s'", __func__
,
1412 devname
, strerror(errno
));
1418 ret
= sev_platform_ioctl(sev_common
->sev_fd
, SEV_PLATFORM_STATUS
, &status
,
1421 error_setg(errp
, "%s: failed to get platform status ret=%d "
1422 "fw_error='%d: %s'", __func__
, ret
, fw_error
,
1423 fw_error_to_str(fw_error
));
1426 sev_common
->build_id
= status
.build
;
1427 sev_common
->api_major
= status
.api_major
;
1428 sev_common
->api_minor
= status
.api_minor
;
1430 if (sev_es_enabled()) {
1431 if (!kvm_kernel_irqchip_allowed()) {
1432 error_setg(errp
, "%s: SEV-ES guests require in-kernel irqchip"
1433 "support", __func__
);
1438 if (sev_es_enabled() && !sev_snp_enabled()) {
1439 if (!(status
.flags
& SEV_STATUS_FLAGS_CONFIG_ES
)) {
1440 error_setg(errp
, "%s: guest policy requires SEV-ES, but "
1441 "host SEV-ES support unavailable",
1447 trace_kvm_sev_init();
1448 if (x86_klass
->kvm_type(X86_CONFIDENTIAL_GUEST(sev_common
)) == KVM_X86_DEFAULT_VM
) {
1449 cmd
= sev_es_enabled() ? KVM_SEV_ES_INIT
: KVM_SEV_INIT
;
1451 ret
= sev_ioctl(sev_common
->sev_fd
, cmd
, NULL
, &fw_error
);
1453 struct kvm_sev_init args
= { 0 };
1455 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_INIT2
, &args
, &fw_error
);
1459 error_setg(errp
, "%s: failed to initialize ret=%d fw_error=%d '%s'",
1460 __func__
, ret
, fw_error
, fw_error_to_str(fw_error
));
1464 ret
= klass
->launch_start(sev_common
);
1467 error_setg(errp
, "%s: failed to create encryption context", __func__
);
1471 if (klass
->kvm_init
&& klass
->kvm_init(cgs
, errp
)) {
1475 qemu_add_vm_change_state_handler(sev_vm_state_change
, sev_common
);
1482 static int sev_kvm_init(ConfidentialGuestSupport
*cgs
, Error
**errp
)
1487 * SEV/SEV-ES rely on pinned memory to back guest RAM so discarding
1488 * isn't actually possible. With SNP, only guest_memfd pages are used
1489 * for private guest memory, so discarding of shared memory is still
1492 ret
= ram_block_discard_disable(true);
1494 error_setg(errp
, "%s: cannot disable RAM discard", __func__
);
1499 * SEV uses these notifiers to register/pin pages prior to guest use,
1500 * but SNP relies on guest_memfd for private pages, which has its
1501 * own internal mechanisms for registering/pinning private memory.
1503 ram_block_notifier_add(&sev_ram_notifier
);
1506 * The machine done notify event is used for SEV guests to get the
1507 * measurement of the encrypted images. When SEV-SNP is enabled, the
1508 * measurement is part of the guest attestation process where it can
1509 * be collected without any reliance on the VMM. So skip registering
1510 * the notifier for SNP in favor of using guest attestation instead.
1512 qemu_add_machine_init_done_notifier(&sev_machine_done_notify
);
1517 static int sev_snp_kvm_init(ConfidentialGuestSupport
*cgs
, Error
**errp
)
1519 MachineState
*ms
= MACHINE(qdev_get_machine());
1520 X86MachineState
*x86ms
= X86_MACHINE(ms
);
1522 if (x86ms
->smm
== ON_OFF_AUTO_AUTO
) {
1523 x86ms
->smm
= ON_OFF_AUTO_OFF
;
1524 } else if (x86ms
->smm
== ON_OFF_AUTO_ON
) {
1525 error_setg(errp
, "SEV-SNP does not support SMM.");
1533 sev_encrypt_flash(hwaddr gpa
, uint8_t *ptr
, uint64_t len
, Error
**errp
)
1535 SevCommonState
*sev_common
= SEV_COMMON(MACHINE(qdev_get_machine())->cgs
);
1536 SevCommonStateClass
*klass
;
1541 klass
= SEV_COMMON_GET_CLASS(sev_common
);
1543 /* if SEV is in update state then encrypt the data else do nothing */
1544 if (sev_check_state(sev_common
, SEV_STATE_LAUNCH_UPDATE
)) {
1547 ret
= klass
->launch_update_data(sev_common
, gpa
, ptr
, len
);
1549 error_setg(errp
, "SEV: Failed to encrypt pflash rom");
1557 int sev_inject_launch_secret(const char *packet_hdr
, const char *secret
,
1558 uint64_t gpa
, Error
**errp
)
1561 struct kvm_sev_launch_secret input
;
1562 g_autofree guchar
*data
= NULL
, *hdr
= NULL
;
1565 gsize hdr_sz
= 0, data_sz
= 0;
1566 MemoryRegion
*mr
= NULL
;
1567 SevCommonState
*sev_common
= SEV_COMMON(MACHINE(qdev_get_machine())->cgs
);
1570 error_setg(errp
, "SEV not enabled for guest");
1574 /* secret can be injected only in this state */
1575 if (!sev_check_state(sev_common
, SEV_STATE_LAUNCH_SECRET
)) {
1576 error_setg(errp
, "SEV: Not in correct state. (LSECRET) %x",
1581 hdr
= g_base64_decode(packet_hdr
, &hdr_sz
);
1582 if (!hdr
|| !hdr_sz
) {
1583 error_setg(errp
, "SEV: Failed to decode sequence header");
1587 data
= g_base64_decode(secret
, &data_sz
);
1588 if (!data
|| !data_sz
) {
1589 error_setg(errp
, "SEV: Failed to decode data");
1593 hva
= gpa2hva(&mr
, gpa
, data_sz
, errp
);
1595 error_prepend(errp
, "SEV: Failed to calculate guest address: ");
1599 input
.hdr_uaddr
= (uint64_t)(unsigned long)hdr
;
1600 input
.hdr_len
= hdr_sz
;
1602 input
.trans_uaddr
= (uint64_t)(unsigned long)data
;
1603 input
.trans_len
= data_sz
;
1605 input
.guest_uaddr
= (uint64_t)(unsigned long)hva
;
1606 input
.guest_len
= data_sz
;
1608 trace_kvm_sev_launch_secret(gpa
, input
.guest_uaddr
,
1609 input
.trans_uaddr
, input
.trans_len
);
1611 ret
= sev_ioctl(sev_common
->sev_fd
, KVM_SEV_LAUNCH_SECRET
,
1614 error_setg(errp
, "SEV: failed to inject secret ret=%d fw_error=%d '%s'",
1615 ret
, error
, fw_error_to_str(error
));
1622 #define SEV_SECRET_GUID "4c2eb361-7d9b-4cc3-8081-127c90d3d294"
1623 struct sev_secret_area
{
1628 void qmp_sev_inject_launch_secret(const char *packet_hdr
,
1630 bool has_gpa
, uint64_t gpa
,
1633 if (!sev_enabled()) {
1634 error_setg(errp
, "SEV not enabled for guest");
1639 struct sev_secret_area
*area
;
1641 if (!pc_system_ovmf_table_find(SEV_SECRET_GUID
, &data
, NULL
)) {
1642 error_setg(errp
, "SEV: no secret area found in OVMF,"
1643 " gpa must be specified.");
1646 area
= (struct sev_secret_area
*)data
;
1650 sev_inject_launch_secret(packet_hdr
, secret
, gpa
, errp
);
1654 sev_es_parse_reset_block(SevInfoBlock
*info
, uint32_t *addr
)
1656 if (!info
->reset_addr
) {
1657 error_report("SEV-ES reset address is zero");
1661 *addr
= info
->reset_addr
;
1667 sev_es_find_reset_vector(void *flash_ptr
, uint64_t flash_size
,
1670 QemuUUID info_guid
, *guid
;
1676 * Initialize the address to zero. An address of zero with a successful
1677 * return code indicates that SEV-ES is not active.
1682 * Extract the AP reset vector for SEV-ES guests by locating the SEV GUID.
1683 * The SEV GUID is located on its own (original implementation) or within
1684 * the Firmware GUID Table (new implementation), either of which are
1685 * located 32 bytes from the end of the flash.
1687 * Check the Firmware GUID Table first.
1689 if (pc_system_ovmf_table_find(SEV_INFO_BLOCK_GUID
, &data
, NULL
)) {
1690 return sev_es_parse_reset_block((SevInfoBlock
*)data
, addr
);
1694 * SEV info block not found in the Firmware GUID Table (or there isn't
1695 * a Firmware GUID Table), fall back to the original implementation.
1697 data
= flash_ptr
+ flash_size
- 0x20;
1699 qemu_uuid_parse(SEV_INFO_BLOCK_GUID
, &info_guid
);
1700 info_guid
= qemu_uuid_bswap(info_guid
); /* GUIDs are LE */
1702 guid
= (QemuUUID
*)(data
- sizeof(info_guid
));
1703 if (!qemu_uuid_is_equal(guid
, &info_guid
)) {
1704 error_report("SEV information block/Firmware GUID Table block not found in pflash rom");
1708 len
= (uint16_t *)((uint8_t *)guid
- sizeof(*len
));
1709 info
= (SevInfoBlock
*)(data
- le16_to_cpu(*len
));
1711 return sev_es_parse_reset_block(info
, addr
);
1714 void sev_es_set_reset_vector(CPUState
*cpu
)
1718 ConfidentialGuestSupport
*cgs
= MACHINE(qdev_get_machine())->cgs
;
1719 SevCommonState
*sev_common
= SEV_COMMON(
1720 object_dynamic_cast(OBJECT(cgs
), TYPE_SEV_COMMON
));
1722 /* Only update if we have valid reset information */
1723 if (!sev_common
|| !sev_common
->reset_data_valid
) {
1727 /* Do not update the BSP reset state */
1728 if (cpu
->cpu_index
== 0) {
1735 cpu_x86_load_seg_cache(env
, R_CS
, 0xf000, sev_common
->reset_cs
, 0xffff,
1736 DESC_P_MASK
| DESC_S_MASK
| DESC_CS_MASK
|
1737 DESC_R_MASK
| DESC_A_MASK
);
1739 env
->eip
= sev_common
->reset_ip
;
1742 int sev_es_save_reset_vector(void *flash_ptr
, uint64_t flash_size
)
1747 SevCommonState
*sev_common
= SEV_COMMON(MACHINE(qdev_get_machine())->cgs
);
1749 if (!sev_es_enabled()) {
1754 ret
= sev_es_find_reset_vector(flash_ptr
, flash_size
,
1761 sev_common
->reset_cs
= addr
& 0xffff0000;
1762 sev_common
->reset_ip
= addr
& 0x0000ffff;
1763 sev_common
->reset_data_valid
= true;
1766 sev_es_set_reset_vector(cpu
);
1773 static const QemuUUID sev_hash_table_header_guid
= {
1774 .data
= UUID_LE(0x9438d606, 0x4f22, 0x4cc9, 0xb4, 0x79, 0xa7, 0x93,
1775 0xd4, 0x11, 0xfd, 0x21)
1778 static const QemuUUID sev_kernel_entry_guid
= {
1779 .data
= UUID_LE(0x4de79437, 0xabd2, 0x427f, 0xb8, 0x35, 0xd5, 0xb1,
1780 0x72, 0xd2, 0x04, 0x5b)
1782 static const QemuUUID sev_initrd_entry_guid
= {
1783 .data
= UUID_LE(0x44baf731, 0x3a2f, 0x4bd7, 0x9a, 0xf1, 0x41, 0xe2,
1784 0x91, 0x69, 0x78, 0x1d)
1786 static const QemuUUID sev_cmdline_entry_guid
= {
1787 .data
= UUID_LE(0x97d02dd8, 0xbd20, 0x4c94, 0xaa, 0x78, 0xe7, 0x71,
1788 0x4d, 0x36, 0xab, 0x2a)
1791 static bool build_kernel_loader_hashes(PaddedSevHashTable
*padded_ht
,
1792 SevKernelLoaderContext
*ctx
,
1796 uint8_t cmdline_hash
[HASH_SIZE
];
1797 uint8_t initrd_hash
[HASH_SIZE
];
1798 uint8_t kernel_hash
[HASH_SIZE
];
1800 size_t hash_len
= HASH_SIZE
;
1803 * Calculate hash of kernel command-line with the terminating null byte. If
1804 * the user doesn't supply a command-line via -append, the 1-byte "\0" will
1807 hashp
= cmdline_hash
;
1808 if (qcrypto_hash_bytes(QCRYPTO_HASH_ALG_SHA256
, ctx
->cmdline_data
,
1809 ctx
->cmdline_size
, &hashp
, &hash_len
, errp
) < 0) {
1812 assert(hash_len
== HASH_SIZE
);
1815 * Calculate hash of initrd. If the user doesn't supply an initrd via
1816 * -initrd, an empty buffer will be used (ctx->initrd_size == 0).
1818 hashp
= initrd_hash
;
1819 if (qcrypto_hash_bytes(QCRYPTO_HASH_ALG_SHA256
, ctx
->initrd_data
,
1820 ctx
->initrd_size
, &hashp
, &hash_len
, errp
) < 0) {
1823 assert(hash_len
== HASH_SIZE
);
1825 /* Calculate hash of the kernel */
1826 hashp
= kernel_hash
;
1827 struct iovec iov
[2] = {
1828 { .iov_base
= ctx
->setup_data
, .iov_len
= ctx
->setup_size
},
1829 { .iov_base
= ctx
->kernel_data
, .iov_len
= ctx
->kernel_size
}
1831 if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA256
, iov
, ARRAY_SIZE(iov
),
1832 &hashp
, &hash_len
, errp
) < 0) {
1835 assert(hash_len
== HASH_SIZE
);
1837 ht
= &padded_ht
->ht
;
1839 ht
->guid
= sev_hash_table_header_guid
;
1840 ht
->len
= sizeof(*ht
);
1842 ht
->cmdline
.guid
= sev_cmdline_entry_guid
;
1843 ht
->cmdline
.len
= sizeof(ht
->cmdline
);
1844 memcpy(ht
->cmdline
.hash
, cmdline_hash
, sizeof(ht
->cmdline
.hash
));
1846 ht
->initrd
.guid
= sev_initrd_entry_guid
;
1847 ht
->initrd
.len
= sizeof(ht
->initrd
);
1848 memcpy(ht
->initrd
.hash
, initrd_hash
, sizeof(ht
->initrd
.hash
));
1850 ht
->kernel
.guid
= sev_kernel_entry_guid
;
1851 ht
->kernel
.len
= sizeof(ht
->kernel
);
1852 memcpy(ht
->kernel
.hash
, kernel_hash
, sizeof(ht
->kernel
.hash
));
1854 /* zero the excess data so the measurement can be reliably calculated */
1855 memset(padded_ht
->padding
, 0, sizeof(padded_ht
->padding
));
1860 static bool sev_snp_build_kernel_loader_hashes(SevCommonState
*sev_common
,
1861 SevHashTableDescriptor
*area
,
1862 SevKernelLoaderContext
*ctx
,
1866 * SNP: Populate the hashes table in an area that later in
1867 * snp_launch_update_kernel_hashes() will be copied to the guest memory
1870 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(sev_common
);
1871 sev_snp_guest
->kernel_hashes_offset
= area
->base
& ~TARGET_PAGE_MASK
;
1872 sev_snp_guest
->kernel_hashes_data
= g_new0(PaddedSevHashTable
, 1);
1873 return build_kernel_loader_hashes(sev_snp_guest
->kernel_hashes_data
, ctx
, errp
);
1876 static bool sev_build_kernel_loader_hashes(SevCommonState
*sev_common
,
1877 SevHashTableDescriptor
*area
,
1878 SevKernelLoaderContext
*ctx
,
1881 PaddedSevHashTable
*padded_ht
;
1882 hwaddr mapped_len
= sizeof(*padded_ht
);
1883 MemTxAttrs attrs
= { 0 };
1887 * Populate the hashes table in the guest's memory at the OVMF-designated
1888 * area for the SEV hashes table
1890 padded_ht
= address_space_map(&address_space_memory
, area
->base
,
1891 &mapped_len
, true, attrs
);
1892 if (!padded_ht
|| mapped_len
!= sizeof(*padded_ht
)) {
1893 error_setg(errp
, "SEV: cannot map hashes table guest memory area");
1897 if (build_kernel_loader_hashes(padded_ht
, ctx
, errp
)) {
1898 if (sev_encrypt_flash(area
->base
, (uint8_t *)padded_ht
,
1899 sizeof(*padded_ht
), errp
) < 0) {
1906 address_space_unmap(&address_space_memory
, padded_ht
,
1907 mapped_len
, true, mapped_len
);
1913 * Add the hashes of the linux kernel/initrd/cmdline to an encrypted guest page
1914 * which is included in SEV's initial memory measurement.
1916 bool sev_add_kernel_loader_hashes(SevKernelLoaderContext
*ctx
, Error
**errp
)
1919 SevHashTableDescriptor
*area
;
1920 SevCommonState
*sev_common
= SEV_COMMON(MACHINE(qdev_get_machine())->cgs
);
1921 SevCommonStateClass
*klass
= SEV_COMMON_GET_CLASS(sev_common
);
1924 * Only add the kernel hashes if the sev-guest configuration explicitly
1925 * stated kernel-hashes=on.
1927 if (!sev_common
->kernel_hashes
) {
1931 if (!pc_system_ovmf_table_find(SEV_HASH_TABLE_RV_GUID
, &data
, NULL
)) {
1932 error_setg(errp
, "SEV: kernel specified but guest firmware "
1933 "has no hashes table GUID");
1937 area
= (SevHashTableDescriptor
*)data
;
1938 if (!area
->base
|| area
->size
< sizeof(PaddedSevHashTable
)) {
1939 error_setg(errp
, "SEV: guest firmware hashes table area is invalid "
1940 "(base=0x%x size=0x%x)", area
->base
, area
->size
);
1944 return klass
->build_kernel_loader_hashes(sev_common
, area
, ctx
, errp
);
1948 sev_common_get_sev_device(Object
*obj
, Error
**errp
)
1950 return g_strdup(SEV_COMMON(obj
)->sev_device
);
1954 sev_common_set_sev_device(Object
*obj
, const char *value
, Error
**errp
)
1956 SEV_COMMON(obj
)->sev_device
= g_strdup(value
);
1959 static bool sev_common_get_kernel_hashes(Object
*obj
, Error
**errp
)
1961 return SEV_COMMON(obj
)->kernel_hashes
;
1964 static void sev_common_set_kernel_hashes(Object
*obj
, bool value
, Error
**errp
)
1966 SEV_COMMON(obj
)->kernel_hashes
= value
;
1970 sev_common_class_init(ObjectClass
*oc
, void *data
)
1972 ConfidentialGuestSupportClass
*klass
= CONFIDENTIAL_GUEST_SUPPORT_CLASS(oc
);
1974 klass
->kvm_init
= sev_common_kvm_init
;
1976 object_class_property_add_str(oc
, "sev-device",
1977 sev_common_get_sev_device
,
1978 sev_common_set_sev_device
);
1979 object_class_property_set_description(oc
, "sev-device",
1980 "SEV device to use");
1981 object_class_property_add_bool(oc
, "kernel-hashes",
1982 sev_common_get_kernel_hashes
,
1983 sev_common_set_kernel_hashes
);
1984 object_class_property_set_description(oc
, "kernel-hashes",
1985 "add kernel hashes to guest firmware for measured Linux boot");
1989 sev_common_instance_init(Object
*obj
)
1991 SevCommonState
*sev_common
= SEV_COMMON(obj
);
1993 sev_common
->kvm_type
= -1;
1995 sev_common
->sev_device
= g_strdup(DEFAULT_SEV_DEVICE
);
1997 object_property_add_uint32_ptr(obj
, "cbitpos", &sev_common
->cbitpos
,
1998 OBJ_PROP_FLAG_READWRITE
);
1999 object_property_add_uint32_ptr(obj
, "reduced-phys-bits",
2000 &sev_common
->reduced_phys_bits
,
2001 OBJ_PROP_FLAG_READWRITE
);
2004 /* sev guest info common to sev/sev-es/sev-snp */
2005 static const TypeInfo sev_common_info
= {
2006 .parent
= TYPE_X86_CONFIDENTIAL_GUEST
,
2007 .name
= TYPE_SEV_COMMON
,
2008 .instance_size
= sizeof(SevCommonState
),
2009 .instance_init
= sev_common_instance_init
,
2010 .class_size
= sizeof(SevCommonStateClass
),
2011 .class_init
= sev_common_class_init
,
2013 .interfaces
= (InterfaceInfo
[]) {
2014 { TYPE_USER_CREATABLE
},
2020 sev_guest_get_dh_cert_file(Object
*obj
, Error
**errp
)
2022 return g_strdup(SEV_GUEST(obj
)->dh_cert_file
);
2026 sev_guest_set_dh_cert_file(Object
*obj
, const char *value
, Error
**errp
)
2028 SEV_GUEST(obj
)->dh_cert_file
= g_strdup(value
);
2032 sev_guest_get_session_file(Object
*obj
, Error
**errp
)
2034 SevGuestState
*sev_guest
= SEV_GUEST(obj
);
2036 return sev_guest
->session_file
? g_strdup(sev_guest
->session_file
) : NULL
;
2040 sev_guest_set_session_file(Object
*obj
, const char *value
, Error
**errp
)
2042 SEV_GUEST(obj
)->session_file
= g_strdup(value
);
2045 static bool sev_guest_get_legacy_vm_type(Object
*obj
, Error
**errp
)
2047 return SEV_GUEST(obj
)->legacy_vm_type
;
2050 static void sev_guest_set_legacy_vm_type(Object
*obj
, bool value
, Error
**errp
)
2052 SEV_GUEST(obj
)->legacy_vm_type
= value
;
2056 sev_guest_class_init(ObjectClass
*oc
, void *data
)
2058 SevCommonStateClass
*klass
= SEV_COMMON_CLASS(oc
);
2059 X86ConfidentialGuestClass
*x86_klass
= X86_CONFIDENTIAL_GUEST_CLASS(oc
);
2061 klass
->build_kernel_loader_hashes
= sev_build_kernel_loader_hashes
;
2062 klass
->launch_start
= sev_launch_start
;
2063 klass
->launch_finish
= sev_launch_finish
;
2064 klass
->launch_update_data
= sev_launch_update_data
;
2065 klass
->kvm_init
= sev_kvm_init
;
2066 x86_klass
->kvm_type
= sev_kvm_type
;
2068 object_class_property_add_str(oc
, "dh-cert-file",
2069 sev_guest_get_dh_cert_file
,
2070 sev_guest_set_dh_cert_file
);
2071 object_class_property_set_description(oc
, "dh-cert-file",
2072 "guest owners DH certificate (encoded with base64)");
2073 object_class_property_add_str(oc
, "session-file",
2074 sev_guest_get_session_file
,
2075 sev_guest_set_session_file
);
2076 object_class_property_set_description(oc
, "session-file",
2077 "guest owners session parameters (encoded with base64)");
2078 object_class_property_add_bool(oc
, "legacy-vm-type",
2079 sev_guest_get_legacy_vm_type
,
2080 sev_guest_set_legacy_vm_type
);
2081 object_class_property_set_description(oc
, "legacy-vm-type",
2082 "use legacy VM type to maintain measurement compatibility with older QEMU or kernel versions.");
2086 sev_guest_instance_init(Object
*obj
)
2088 SevGuestState
*sev_guest
= SEV_GUEST(obj
);
2090 sev_guest
->policy
= DEFAULT_GUEST_POLICY
;
2091 object_property_add_uint32_ptr(obj
, "handle", &sev_guest
->handle
,
2092 OBJ_PROP_FLAG_READWRITE
);
2093 object_property_add_uint32_ptr(obj
, "policy", &sev_guest
->policy
,
2094 OBJ_PROP_FLAG_READWRITE
);
2095 object_apply_compat_props(obj
);
2098 /* guest info specific sev/sev-es */
2099 static const TypeInfo sev_guest_info
= {
2100 .parent
= TYPE_SEV_COMMON
,
2101 .name
= TYPE_SEV_GUEST
,
2102 .instance_size
= sizeof(SevGuestState
),
2103 .instance_init
= sev_guest_instance_init
,
2104 .class_init
= sev_guest_class_init
,
2108 sev_snp_guest_get_policy(Object
*obj
, Visitor
*v
, const char *name
,
2109 void *opaque
, Error
**errp
)
2111 visit_type_uint64(v
, name
,
2112 (uint64_t *)&SEV_SNP_GUEST(obj
)->kvm_start_conf
.policy
,
2117 sev_snp_guest_set_policy(Object
*obj
, Visitor
*v
, const char *name
,
2118 void *opaque
, Error
**errp
)
2120 visit_type_uint64(v
, name
,
2121 (uint64_t *)&SEV_SNP_GUEST(obj
)->kvm_start_conf
.policy
,
2126 sev_snp_guest_get_guest_visible_workarounds(Object
*obj
, Error
**errp
)
2128 return g_strdup(SEV_SNP_GUEST(obj
)->guest_visible_workarounds
);
2132 sev_snp_guest_set_guest_visible_workarounds(Object
*obj
, const char *value
,
2135 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2136 struct kvm_sev_snp_launch_start
*start
= &sev_snp_guest
->kvm_start_conf
;
2137 g_autofree guchar
*blob
;
2140 g_free(sev_snp_guest
->guest_visible_workarounds
);
2142 /* store the base64 str so we don't need to re-encode in getter */
2143 sev_snp_guest
->guest_visible_workarounds
= g_strdup(value
);
2145 blob
= qbase64_decode(sev_snp_guest
->guest_visible_workarounds
,
2151 if (len
!= sizeof(start
->gosvw
)) {
2152 error_setg(errp
, "parameter length of %" G_GSIZE_FORMAT
2153 " exceeds max of %zu",
2154 len
, sizeof(start
->gosvw
));
2158 memcpy(start
->gosvw
, blob
, len
);
2162 sev_snp_guest_get_id_block(Object
*obj
, Error
**errp
)
2164 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2166 return g_strdup(sev_snp_guest
->id_block_base64
);
2170 sev_snp_guest_set_id_block(Object
*obj
, const char *value
, Error
**errp
)
2172 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2173 struct kvm_sev_snp_launch_finish
*finish
= &sev_snp_guest
->kvm_finish_conf
;
2176 finish
->id_block_en
= 0;
2177 g_free(sev_snp_guest
->id_block
);
2178 g_free(sev_snp_guest
->id_block_base64
);
2180 /* store the base64 str so we don't need to re-encode in getter */
2181 sev_snp_guest
->id_block_base64
= g_strdup(value
);
2182 sev_snp_guest
->id_block
=
2183 qbase64_decode(sev_snp_guest
->id_block_base64
, -1, &len
, errp
);
2185 if (!sev_snp_guest
->id_block
) {
2189 if (len
!= KVM_SEV_SNP_ID_BLOCK_SIZE
) {
2190 error_setg(errp
, "parameter length of %" G_GSIZE_FORMAT
2192 len
, KVM_SEV_SNP_ID_BLOCK_SIZE
);
2196 finish
->id_block_en
= 1;
2197 finish
->id_block_uaddr
= (uintptr_t)sev_snp_guest
->id_block
;
2201 sev_snp_guest_get_id_auth(Object
*obj
, Error
**errp
)
2203 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2205 return g_strdup(sev_snp_guest
->id_auth_base64
);
2209 sev_snp_guest_set_id_auth(Object
*obj
, const char *value
, Error
**errp
)
2211 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2212 struct kvm_sev_snp_launch_finish
*finish
= &sev_snp_guest
->kvm_finish_conf
;
2215 finish
->id_auth_uaddr
= 0;
2216 g_free(sev_snp_guest
->id_auth
);
2217 g_free(sev_snp_guest
->id_auth_base64
);
2219 /* store the base64 str so we don't need to re-encode in getter */
2220 sev_snp_guest
->id_auth_base64
= g_strdup(value
);
2221 sev_snp_guest
->id_auth
=
2222 qbase64_decode(sev_snp_guest
->id_auth_base64
, -1, &len
, errp
);
2224 if (!sev_snp_guest
->id_auth
) {
2228 if (len
> KVM_SEV_SNP_ID_AUTH_SIZE
) {
2229 error_setg(errp
, "parameter length:ID_AUTH %" G_GSIZE_FORMAT
2230 " exceeds max of %u",
2231 len
, KVM_SEV_SNP_ID_AUTH_SIZE
);
2235 finish
->id_auth_uaddr
= (uintptr_t)sev_snp_guest
->id_auth
;
2239 sev_snp_guest_get_author_key_enabled(Object
*obj
, Error
**errp
)
2241 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2243 return !!sev_snp_guest
->kvm_finish_conf
.auth_key_en
;
2247 sev_snp_guest_set_author_key_enabled(Object
*obj
, bool value
, Error
**errp
)
2249 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2251 sev_snp_guest
->kvm_finish_conf
.auth_key_en
= value
;
2255 sev_snp_guest_get_vcek_disabled(Object
*obj
, Error
**errp
)
2257 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2259 return !!sev_snp_guest
->kvm_finish_conf
.vcek_disabled
;
2263 sev_snp_guest_set_vcek_disabled(Object
*obj
, bool value
, Error
**errp
)
2265 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2267 sev_snp_guest
->kvm_finish_conf
.vcek_disabled
= value
;
2271 sev_snp_guest_get_host_data(Object
*obj
, Error
**errp
)
2273 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2275 return g_strdup(sev_snp_guest
->host_data
);
2279 sev_snp_guest_set_host_data(Object
*obj
, const char *value
, Error
**errp
)
2281 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2282 struct kvm_sev_snp_launch_finish
*finish
= &sev_snp_guest
->kvm_finish_conf
;
2283 g_autofree guchar
*blob
;
2286 g_free(sev_snp_guest
->host_data
);
2288 /* store the base64 str so we don't need to re-encode in getter */
2289 sev_snp_guest
->host_data
= g_strdup(value
);
2291 blob
= qbase64_decode(sev_snp_guest
->host_data
, -1, &len
, errp
);
2297 if (len
!= sizeof(finish
->host_data
)) {
2298 error_setg(errp
, "parameter length of %" G_GSIZE_FORMAT
2299 " not equal to %zu",
2300 len
, sizeof(finish
->host_data
));
2304 memcpy(finish
->host_data
, blob
, len
);
2308 sev_snp_guest_class_init(ObjectClass
*oc
, void *data
)
2310 SevCommonStateClass
*klass
= SEV_COMMON_CLASS(oc
);
2311 X86ConfidentialGuestClass
*x86_klass
= X86_CONFIDENTIAL_GUEST_CLASS(oc
);
2313 klass
->build_kernel_loader_hashes
= sev_snp_build_kernel_loader_hashes
;
2314 klass
->launch_start
= sev_snp_launch_start
;
2315 klass
->launch_finish
= sev_snp_launch_finish
;
2316 klass
->launch_update_data
= sev_snp_launch_update_data
;
2317 klass
->kvm_init
= sev_snp_kvm_init
;
2318 x86_klass
->kvm_type
= sev_snp_kvm_type
;
2320 object_class_property_add(oc
, "policy", "uint64",
2321 sev_snp_guest_get_policy
,
2322 sev_snp_guest_set_policy
, NULL
, NULL
);
2323 object_class_property_add_str(oc
, "guest-visible-workarounds",
2324 sev_snp_guest_get_guest_visible_workarounds
,
2325 sev_snp_guest_set_guest_visible_workarounds
);
2326 object_class_property_add_str(oc
, "id-block",
2327 sev_snp_guest_get_id_block
,
2328 sev_snp_guest_set_id_block
);
2329 object_class_property_add_str(oc
, "id-auth",
2330 sev_snp_guest_get_id_auth
,
2331 sev_snp_guest_set_id_auth
);
2332 object_class_property_add_bool(oc
, "author-key-enabled",
2333 sev_snp_guest_get_author_key_enabled
,
2334 sev_snp_guest_set_author_key_enabled
);
2335 object_class_property_add_bool(oc
, "vcek-required",
2336 sev_snp_guest_get_vcek_disabled
,
2337 sev_snp_guest_set_vcek_disabled
);
2338 object_class_property_add_str(oc
, "host-data",
2339 sev_snp_guest_get_host_data
,
2340 sev_snp_guest_set_host_data
);
2344 sev_snp_guest_instance_init(Object
*obj
)
2346 ConfidentialGuestSupport
*cgs
= CONFIDENTIAL_GUEST_SUPPORT(obj
);
2347 SevSnpGuestState
*sev_snp_guest
= SEV_SNP_GUEST(obj
);
2349 cgs
->require_guest_memfd
= true;
2351 /* default init/start/finish params for kvm */
2352 sev_snp_guest
->kvm_start_conf
.policy
= DEFAULT_SEV_SNP_POLICY
;
2355 /* guest info specific to sev-snp */
2356 static const TypeInfo sev_snp_guest_info
= {
2357 .parent
= TYPE_SEV_COMMON
,
2358 .name
= TYPE_SEV_SNP_GUEST
,
2359 .instance_size
= sizeof(SevSnpGuestState
),
2360 .class_init
= sev_snp_guest_class_init
,
2361 .instance_init
= sev_snp_guest_instance_init
,
2365 sev_register_types(void)
2367 type_register_static(&sev_common_info
);
2368 type_register_static(&sev_guest_info
);
2369 type_register_static(&sev_snp_guest_info
);
2372 type_init(sev_register_types
);