xref: /openbmc/qemu/target/i386/sev.c (revision a0124e33)
1 /*
2  * QEMU SEV support
3  *
4  * Copyright Advanced Micro Devices 2016-2018
5  *
6  * Author:
7  *      Brijesh Singh <brijesh.singh@amd.com>
8  *
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.
11  *
12  */
13 
14 #include "qemu/osdep.h"
15 
16 #include <linux/kvm.h>
17 #include <linux/kvm_para.h>
18 #include <linux/psp-sev.h>
19 
20 #include <sys/ioctl.h>
21 
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"
31 #include "sev.h"
32 #include "sysemu/sysemu.h"
33 #include "sysemu/runstate.h"
34 #include "trace.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"
44 
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)
48 
49 /* hard code sha256 digest size */
50 #define HASH_SIZE 32
51 
52 typedef struct QEMU_PACKED SevHashTableEntry {
53     QemuUUID guid;
54     uint16_t len;
55     uint8_t hash[HASH_SIZE];
56 } SevHashTableEntry;
57 
58 typedef struct QEMU_PACKED SevHashTable {
59     QemuUUID guid;
60     uint16_t len;
61     SevHashTableEntry cmdline;
62     SevHashTableEntry initrd;
63     SevHashTableEntry kernel;
64 } SevHashTable;
65 
66 /*
67  * Data encrypted by sev_encrypt_flash() must be padded to a multiple of
68  * 16 bytes.
69  */
70 typedef struct QEMU_PACKED PaddedSevHashTable {
71     SevHashTable ht;
72     uint8_t padding[ROUND_UP(sizeof(SevHashTable), 16) - sizeof(SevHashTable)];
73 } PaddedSevHashTable;
74 
75 QEMU_BUILD_BUG_ON(sizeof(PaddedSevHashTable) % 16 != 0);
76 
77 #define SEV_INFO_BLOCK_GUID     "00f771de-1a7e-4fcb-890e-68c77e2fb44e"
78 typedef struct __attribute__((__packed__)) SevInfoBlock {
79     /* SEV-ES Reset Vector Address */
80     uint32_t reset_addr;
81 } SevInfoBlock;
82 
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 */
86     uint32_t base;
87     /* SEV hash table area size (in bytes) */
88     uint32_t size;
89 } SevHashTableDescriptor;
90 
91 struct SevCommonState {
92     X86ConfidentialGuest parent_obj;
93 
94     int kvm_type;
95 
96     /* configuration parameters */
97     char *sev_device;
98     uint32_t cbitpos;
99     uint32_t reduced_phys_bits;
100     bool kernel_hashes;
101 
102     /* runtime state */
103     uint8_t api_major;
104     uint8_t api_minor;
105     uint8_t build_id;
106     int sev_fd;
107     SevState state;
108 
109     uint32_t reset_cs;
110     uint32_t reset_ip;
111     bool reset_data_valid;
112 };
113 
114 struct SevCommonStateClass {
115     X86ConfidentialGuestClass parent_class;
116 
117     /* public */
118     bool (*build_kernel_loader_hashes)(SevCommonState *sev_common,
119                                        SevHashTableDescriptor *area,
120                                        SevKernelLoaderContext *ctx,
121                                        Error **errp);
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);
126 };
127 
128 /**
129  * SevGuestState:
130  *
131  * The SevGuestState object is used for creating and managing a SEV
132  * guest.
133  *
134  * # $QEMU \
135  *         -object sev-guest,id=sev0 \
136  *         -machine ...,memory-encryption=sev0
137  */
138 struct SevGuestState {
139     SevCommonState parent_obj;
140     gchar *measurement;
141 
142     /* configuration parameters */
143     uint32_t handle;
144     uint32_t policy;
145     char *dh_cert_file;
146     char *session_file;
147     bool legacy_vm_type;
148 };
149 
150 struct SevSnpGuestState {
151     SevCommonState parent_obj;
152 
153     /* configuration parameters */
154     char *guest_visible_workarounds;
155     char *id_block_base64;
156     uint8_t *id_block;
157     char *id_auth_base64;
158     uint8_t *id_auth;
159     char *host_data;
160 
161     struct kvm_sev_snp_launch_start kvm_start_conf;
162     struct kvm_sev_snp_launch_finish kvm_finish_conf;
163 
164     uint32_t kernel_hashes_offset;
165     PaddedSevHashTable *kernel_hashes_data;
166 };
167 
168 #define DEFAULT_GUEST_POLICY    0x1 /* disable debug */
169 #define DEFAULT_SEV_DEVICE      "/dev/sev"
170 #define DEFAULT_SEV_SNP_POLICY  0x30000
171 
172 typedef struct SevLaunchUpdateData {
173     QTAILQ_ENTRY(SevLaunchUpdateData) next;
174     hwaddr gpa;
175     void *hva;
176     size_t len;
177     int type;
178 } SevLaunchUpdateData;
179 
180 static QTAILQ_HEAD(, SevLaunchUpdateData) launch_update;
181 
182 static Error *sev_mig_blocker;
183 
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",
210 };
211 
212 #define SEV_FW_MAX_ERROR      ARRAY_SIZE(sev_fw_errlist)
213 
214 /* <linux/kvm.h> doesn't expose this, so re-use the max from kvm.c */
215 #define KVM_MAX_CPUID_ENTRIES 100
216 
217 typedef struct KvmCpuidInfo {
218     struct kvm_cpuid2 cpuid;
219     struct kvm_cpuid_entry2 entries[KVM_MAX_CPUID_ENTRIES];
220 } KvmCpuidInfo;
221 
222 #define SNP_CPUID_FUNCTION_MAXCOUNT 64
223 #define SNP_CPUID_FUNCTION_UNKNOWN 0xFFFFFFFF
224 
225 typedef struct {
226     uint32_t eax_in;
227     uint32_t ecx_in;
228     uint64_t xcr0_in;
229     uint64_t xss_in;
230     uint32_t eax;
231     uint32_t ebx;
232     uint32_t ecx;
233     uint32_t edx;
234     uint64_t reserved;
235 } __attribute__((packed)) SnpCpuidFunc;
236 
237 typedef struct {
238     uint32_t count;
239     uint32_t reserved1;
240     uint64_t reserved2;
241     SnpCpuidFunc entries[SNP_CPUID_FUNCTION_MAXCOUNT];
242 } __attribute__((packed)) SnpCpuidInfo;
243 
244 static int
245 sev_ioctl(int fd, int cmd, void *data, int *error)
246 {
247     int r;
248     struct kvm_sev_cmd input;
249 
250     memset(&input, 0x0, sizeof(input));
251 
252     input.id = cmd;
253     input.sev_fd = fd;
254     input.data = (uintptr_t)data;
255 
256     r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_OP, &input);
257 
258     if (error) {
259         *error = input.error;
260     }
261 
262     return r;
263 }
264 
265 static int
266 sev_platform_ioctl(int fd, int cmd, void *data, int *error)
267 {
268     int r;
269     struct sev_issue_cmd arg;
270 
271     arg.cmd = cmd;
272     arg.data = (unsigned long)data;
273     r = ioctl(fd, SEV_ISSUE_CMD, &arg);
274     if (error) {
275         *error = arg.error;
276     }
277 
278     return r;
279 }
280 
281 static const char *
282 fw_error_to_str(int code)
283 {
284     if (code < 0 || code >= SEV_FW_MAX_ERROR) {
285         return "unknown error";
286     }
287 
288     return sev_fw_errlist[code];
289 }
290 
291 static bool
292 sev_check_state(const SevCommonState *sev_common, SevState state)
293 {
294     assert(sev_common);
295     return sev_common->state == state ? true : false;
296 }
297 
298 static void
299 sev_set_guest_state(SevCommonState *sev_common, SevState new_state)
300 {
301     assert(new_state < SEV_STATE__MAX);
302     assert(sev_common);
303 
304     trace_kvm_sev_change_state(SevState_str(sev_common->state),
305                                SevState_str(new_state));
306     sev_common->state = new_state;
307 }
308 
309 static void
310 sev_ram_block_added(RAMBlockNotifier *n, void *host, size_t size,
311                     size_t max_size)
312 {
313     int r;
314     struct kvm_enc_region range;
315     ram_addr_t offset;
316     MemoryRegion *mr;
317 
318     /*
319      * The RAM device presents a memory region that should be treated
320      * as IO region and should not be pinned.
321      */
322     mr = memory_region_from_host(host, &offset);
323     if (mr && memory_region_is_ram_device(mr)) {
324         return;
325     }
326 
327     range.addr = (uintptr_t)host;
328     range.size = max_size;
329 
330     trace_kvm_memcrypt_register_region(host, max_size);
331     r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_REG_REGION, &range);
332     if (r) {
333         error_report("%s: failed to register region (%p+%#zx) error '%s'",
334                      __func__, host, max_size, strerror(errno));
335         exit(1);
336     }
337 }
338 
339 static void
340 sev_ram_block_removed(RAMBlockNotifier *n, void *host, size_t size,
341                       size_t max_size)
342 {
343     int r;
344     struct kvm_enc_region range;
345     ram_addr_t offset;
346     MemoryRegion *mr;
347 
348     /*
349      * The RAM device presents a memory region that should be treated
350      * as IO region and should not have been pinned.
351      */
352     mr = memory_region_from_host(host, &offset);
353     if (mr && memory_region_is_ram_device(mr)) {
354         return;
355     }
356 
357     range.addr = (uintptr_t)host;
358     range.size = max_size;
359 
360     trace_kvm_memcrypt_unregister_region(host, max_size);
361     r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_UNREG_REGION, &range);
362     if (r) {
363         error_report("%s: failed to unregister region (%p+%#zx)",
364                      __func__, host, max_size);
365     }
366 }
367 
368 static struct RAMBlockNotifier sev_ram_notifier = {
369     .ram_block_added = sev_ram_block_added,
370     .ram_block_removed = sev_ram_block_removed,
371 };
372 
373 bool
374 sev_enabled(void)
375 {
376     ConfidentialGuestSupport *cgs = MACHINE(qdev_get_machine())->cgs;
377 
378     return !!object_dynamic_cast(OBJECT(cgs), TYPE_SEV_COMMON);
379 }
380 
381 bool
382 sev_snp_enabled(void)
383 {
384     ConfidentialGuestSupport *cgs = MACHINE(qdev_get_machine())->cgs;
385 
386     return !!object_dynamic_cast(OBJECT(cgs), TYPE_SEV_SNP_GUEST);
387 }
388 
389 bool
390 sev_es_enabled(void)
391 {
392     ConfidentialGuestSupport *cgs = MACHINE(qdev_get_machine())->cgs;
393 
394     return sev_snp_enabled() ||
395             (sev_enabled() && SEV_GUEST(cgs)->policy & SEV_POLICY_ES);
396 }
397 
398 uint32_t
399 sev_get_cbit_position(void)
400 {
401     SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs);
402 
403     return sev_common ? sev_common->cbitpos : 0;
404 }
405 
406 uint32_t
407 sev_get_reduced_phys_bits(void)
408 {
409     SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs);
410 
411     return sev_common ? sev_common->reduced_phys_bits : 0;
412 }
413 
414 static SevInfo *sev_get_info(void)
415 {
416     SevInfo *info;
417     SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs);
418 
419     info = g_new0(SevInfo, 1);
420     info->enabled = sev_enabled();
421 
422     if (info->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;
427 
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);
432         } else {
433             info->sev_type = SEV_GUEST_TYPE_SEV;
434             info->u.sev.handle = SEV_GUEST(sev_common)->handle;
435             info->u.sev.policy =
436                 (uint32_t)object_property_get_uint(OBJECT(sev_common),
437                                                    "policy", NULL);
438         }
439     }
440 
441     return info;
442 }
443 
444 SevInfo *qmp_query_sev(Error **errp)
445 {
446     SevInfo *info;
447 
448     info = sev_get_info();
449     if (!info) {
450         error_setg(errp, "SEV feature is not available");
451         return NULL;
452     }
453 
454     return info;
455 }
456 
457 void hmp_info_sev(Monitor *mon, const QDict *qdict)
458 {
459     SevInfo *info = sev_get_info();
460 
461     if (!info || !info->enabled) {
462         monitor_printf(mon, "SEV is not enabled\n");
463         goto out;
464     }
465 
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,
470                    info->api_minor);
471 
472     if (sev_snp_enabled()) {
473         monitor_printf(mon, "debug: %s\n",
474                        info->u.sev_snp.snp_policy & SEV_SNP_POLICY_DBG ? "on"
475                                                                        : "off");
476         monitor_printf(mon, "SMT allowed: %s\n",
477                        info->u.sev_snp.snp_policy & SEV_SNP_POLICY_SMT ? "on"
478                                                                        : "off");
479     } else {
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");
485     }
486 
487 out:
488     qapi_free_SevInfo(info);
489 }
490 
491 static int
492 sev_get_pdh_info(int fd, guchar **pdh, size_t *pdh_len, guchar **cert_chain,
493                  size_t *cert_chain_len, Error **errp)
494 {
495     guchar *pdh_data = NULL;
496     guchar *cert_chain_data = NULL;
497     struct sev_user_data_pdh_cert_export export = {};
498     int err, r;
499 
500     /* query the certificate length */
501     r = sev_platform_ioctl(fd, SEV_PDH_CERT_EXPORT, &export, &err);
502     if (r < 0) {
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));
507             return 1;
508         }
509     }
510 
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;
515 
516     r = sev_platform_ioctl(fd, SEV_PDH_CERT_EXPORT, &export, &err);
517     if (r < 0) {
518         error_setg(errp, "SEV: Failed to export PDH cert ret=%d fw_err=%d (%s)",
519                    r, err, fw_error_to_str(err));
520         goto e_free;
521     }
522 
523     *pdh = pdh_data;
524     *pdh_len = export.pdh_cert_len;
525     *cert_chain = cert_chain_data;
526     *cert_chain_len = export.cert_chain_len;
527     return 0;
528 
529 e_free:
530     g_free(pdh_data);
531     g_free(cert_chain_data);
532     return 1;
533 }
534 
535 static int sev_get_cpu0_id(int fd, guchar **id, size_t *id_len, Error **errp)
536 {
537     guchar *id_data;
538     struct sev_user_data_get_id2 get_id2 = {};
539     int err, r;
540 
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));
546         return 1;
547     }
548 
549     id_data = g_new(guchar, get_id2.length);
550     get_id2.address = (unsigned long)id_data;
551 
552     r = sev_platform_ioctl(fd, SEV_GET_ID2, &get_id2, &err);
553     if (r < 0) {
554         error_setg(errp, "SEV: Failed to get ID ret=%d fw_err=%d (%s)",
555                    r, err, fw_error_to_str(err));
556         goto err;
557     }
558 
559     *id = id_data;
560     *id_len = get_id2.length;
561     return 0;
562 
563 err:
564     g_free(id_data);
565     return 1;
566 }
567 
568 static SevCapability *sev_get_capabilities(Error **errp)
569 {
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;
575     uint32_t ebx;
576     int fd;
577     SevCommonState *sev_common;
578     char *sev_device;
579 
580     if (!kvm_enabled()) {
581         error_setg(errp, "KVM not enabled");
582         return NULL;
583     }
584     if (kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_OP, NULL) < 0) {
585         error_setg(errp, "SEV is not enabled in KVM");
586         return NULL;
587     }
588 
589     sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs);
590     if (sev_common) {
591         sev_device = object_property_get_str(OBJECT(sev_common), "sev-device",
592                                              &error_abort);
593     } else {
594         sev_device = g_strdup(DEFAULT_SEV_DEVICE);
595     }
596 
597     fd = open(sev_device, O_RDWR);
598     if (fd < 0) {
599         error_setg_errno(errp, errno, "SEV: Failed to open %s",
600                          sev_device);
601         g_free(sev_device);
602         return NULL;
603     }
604     g_free(sev_device);
605 
606     if (sev_get_pdh_info(fd, &pdh_data, &pdh_len,
607                          &cert_chain_data, &cert_chain_len, errp)) {
608         goto out;
609     }
610 
611     if (sev_get_cpu0_id(fd, &cpu0_id_data, &cpu0_id_len, errp)) {
612         goto out;
613     }
614 
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);
619 
620     host_cpuid(0x8000001F, 0, NULL, &ebx, NULL, NULL);
621     cap->cbitpos = ebx & 0x3f;
622 
623     /*
624      * When SEV feature is enabled, we loose one bit in guest physical
625      * addressing.
626      */
627     cap->reduced_phys_bits = 1;
628 
629 out:
630     g_free(cpu0_id_data);
631     g_free(pdh_data);
632     g_free(cert_chain_data);
633     close(fd);
634     return cap;
635 }
636 
637 SevCapability *qmp_query_sev_capabilities(Error **errp)
638 {
639     return sev_get_capabilities(errp);
640 }
641 
642 static OvmfSevMetadata *ovmf_sev_metadata_table;
643 
644 #define OVMF_SEV_META_DATA_GUID "dc886566-984a-4798-A75e-5585a7bf67cc"
645 typedef struct __attribute__((__packed__)) OvmfSevMetadataOffset {
646     uint32_t offset;
647 } OvmfSevMetadataOffset;
648 
649 OvmfSevMetadata *pc_system_get_ovmf_sev_metadata_ptr(void)
650 {
651     return ovmf_sev_metadata_table;
652 }
653 
654 void pc_system_parse_sev_metadata(uint8_t *flash_ptr, size_t flash_size)
655 {
656     OvmfSevMetadata     *metadata;
657     OvmfSevMetadataOffset  *data;
658 
659     if (!pc_system_ovmf_table_find(OVMF_SEV_META_DATA_GUID, (uint8_t **)&data,
660                                    NULL)) {
661         return;
662     }
663 
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) {
668         return;
669     }
670 
671     ovmf_sev_metadata_table = g_memdup2(metadata, metadata->len);
672 }
673 
674 static SevAttestationReport *sev_get_attestation_report(const char *mnonce,
675                                                         Error **errp)
676 {
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;
682     gsize len;
683     int err = 0, ret;
684 
685     if (!sev_enabled()) {
686         error_setg(errp, "SEV is not enabled");
687         return NULL;
688     }
689 
690     /* lets decode the mnonce string */
691     buf = g_base64_decode(mnonce, &len);
692     if (!buf) {
693         error_setg(errp, "SEV: failed to decode mnonce input");
694         return NULL;
695     }
696 
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);
701         return NULL;
702     }
703 
704     sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs);
705 
706     /* Query the report length */
707     ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_GET_ATTESTATION_REPORT,
708             &input, &err);
709     if (ret < 0) {
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));
714             return NULL;
715         }
716     }
717 
718     data = g_malloc(input.len);
719     input.uaddr = (unsigned long)data;
720     memcpy(input.mnonce, buf, sizeof(input.mnonce));
721 
722     /* Query the report */
723     ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_GET_ATTESTATION_REPORT,
724             &input, &err);
725     if (ret) {
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));
728         return NULL;
729     }
730 
731     report = g_new0(SevAttestationReport, 1);
732     report->data = g_base64_encode(data, input.len);
733 
734     trace_kvm_sev_attestation_report(mnonce, report->data);
735 
736     return report;
737 }
738 
739 SevAttestationReport *qmp_query_sev_attestation_report(const char *mnonce,
740                                                        Error **errp)
741 {
742     return sev_get_attestation_report(mnonce, errp);
743 }
744 
745 static int
746 sev_read_file_base64(const char *filename, guchar **data, gsize *len)
747 {
748     gsize sz;
749     g_autofree gchar *base64 = NULL;
750     GError *error = NULL;
751 
752     if (!g_file_get_contents(filename, &base64, &sz, &error)) {
753         error_report("SEV: Failed to read '%s' (%s)", filename, error->message);
754         g_error_free(error);
755         return -1;
756     }
757 
758     *data = g_base64_decode(base64, len);
759     return 0;
760 }
761 
762 static int
763 sev_snp_launch_start(SevCommonState *sev_common)
764 {
765     int fw_error, rc;
766     SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(sev_common);
767     struct kvm_sev_snp_launch_start *start = &sev_snp_guest->kvm_start_conf;
768 
769     trace_kvm_sev_snp_launch_start(start->policy,
770                                    sev_snp_guest->guest_visible_workarounds);
771 
772     if (!kvm_enable_hypercall(BIT_ULL(KVM_HC_MAP_GPA_RANGE))) {
773             return 1;
774     }
775 
776     rc = sev_ioctl(sev_common->sev_fd, KVM_SEV_SNP_LAUNCH_START,
777                    start, &fw_error);
778     if (rc < 0) {
779         error_report("%s: SNP_LAUNCH_START ret=%d fw_error=%d '%s'",
780                 __func__, rc, fw_error, fw_error_to_str(fw_error));
781         return 1;
782     }
783 
784     QTAILQ_INIT(&launch_update);
785 
786     sev_set_guest_state(sev_common, SEV_STATE_LAUNCH_UPDATE);
787 
788     return 0;
789 }
790 
791 static int
792 sev_launch_start(SevCommonState *sev_common)
793 {
794     gsize sz;
795     int ret = 1;
796     int fw_error, rc;
797     SevGuestState *sev_guest = SEV_GUEST(sev_common);
798     struct kvm_sev_launch_start start = {
799         .handle = sev_guest->handle, .policy = sev_guest->policy
800     };
801     guchar *session = NULL, *dh_cert = NULL;
802 
803     if (sev_guest->session_file) {
804         if (sev_read_file_base64(sev_guest->session_file, &session, &sz) < 0) {
805             goto out;
806         }
807         start.session_uaddr = (unsigned long)session;
808         start.session_len = sz;
809     }
810 
811     if (sev_guest->dh_cert_file) {
812         if (sev_read_file_base64(sev_guest->dh_cert_file, &dh_cert, &sz) < 0) {
813             goto out;
814         }
815         start.dh_uaddr = (unsigned long)dh_cert;
816         start.dh_len = sz;
817     }
818 
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);
821     if (rc < 0) {
822         error_report("%s: LAUNCH_START ret=%d fw_error=%d '%s'",
823                 __func__, ret, fw_error, fw_error_to_str(fw_error));
824         goto out;
825     }
826 
827     sev_set_guest_state(sev_common, SEV_STATE_LAUNCH_UPDATE);
828     sev_guest->handle = start.handle;
829     ret = 0;
830 
831 out:
832     g_free(session);
833     g_free(dh_cert);
834     return ret;
835 }
836 
837 static void
838 sev_snp_cpuid_report_mismatches(SnpCpuidInfo *old,
839                                 SnpCpuidInfo *new)
840 {
841     size_t i;
842 
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);
846         return;
847     }
848 
849     for (i = 0; i < old->count; i++) {
850         SnpCpuidFunc *old_func, *new_func;
851 
852         old_func = &old->entries[i];
853         new_func = &new->entries[i];
854 
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);
862         }
863     }
864 }
865 
866 static const char *
867 snp_page_type_to_str(int type)
868 {
869     switch (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";
876     }
877 }
878 
879 static int
880 sev_snp_launch_update(SevSnpGuestState *sev_snp_guest,
881                       SevLaunchUpdateData *data)
882 {
883     int ret, fw_error;
884     SnpCpuidInfo snp_cpuid_info;
885     struct kvm_sev_snp_launch_update update = {0};
886 
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);
891         return 1;
892     }
893 
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));
897     }
898 
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;
903 
904     /*
905      * KVM_SEV_SNP_LAUNCH_UPDATE requires that GPA ranges have the private
906      * memory attribute set in advance.
907      */
908     ret = kvm_set_memory_attributes_private(data->gpa, data->len);
909     if (ret) {
910         error_report("SEV-SNP: failed to configure initial"
911                      "private guest memory");
912         goto out;
913     }
914 
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));
919 
920         ret = sev_ioctl(SEV_COMMON(sev_snp_guest)->sev_fd,
921                         KVM_SEV_SNP_LAUNCH_UPDATE,
922                         &update, &fw_error);
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));
926 
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");
930             }
931             break;
932         }
933     }
934 
935 out:
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);
942         ret = -EIO;
943     }
944 
945     return ret;
946 }
947 
948 static int
949 sev_launch_update_data(SevCommonState *sev_common, hwaddr gpa,
950                        uint8_t *addr, size_t len)
951 {
952     int ret, fw_error;
953     struct kvm_sev_launch_update_data update;
954 
955     if (!addr || !len) {
956         return 1;
957     }
958 
959     update.uaddr = (uintptr_t)addr;
960     update.len = len;
961     trace_kvm_sev_launch_update_data(addr, len);
962     ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_LAUNCH_UPDATE_DATA,
963                     &update, &fw_error);
964     if (ret) {
965         error_report("%s: LAUNCH_UPDATE ret=%d fw_error=%d '%s'",
966                 __func__, ret, fw_error, fw_error_to_str(fw_error));
967     }
968 
969     return ret;
970 }
971 
972 static int
973 sev_launch_update_vmsa(SevGuestState *sev_guest)
974 {
975     int ret, fw_error;
976 
977     ret = sev_ioctl(SEV_COMMON(sev_guest)->sev_fd, KVM_SEV_LAUNCH_UPDATE_VMSA,
978                     NULL, &fw_error);
979     if (ret) {
980         error_report("%s: LAUNCH_UPDATE_VMSA ret=%d fw_error=%d '%s'",
981                 __func__, ret, fw_error, fw_error_to_str(fw_error));
982     }
983 
984     return ret;
985 }
986 
987 static void
988 sev_launch_get_measure(Notifier *notifier, void *unused)
989 {
990     SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs);
991     SevGuestState *sev_guest = SEV_GUEST(sev_common);
992     int ret, error;
993     g_autofree guchar *data = NULL;
994     struct kvm_sev_launch_measure measurement = {};
995 
996     if (!sev_check_state(sev_common, SEV_STATE_LAUNCH_UPDATE)) {
997         return;
998     }
999 
1000     if (sev_es_enabled()) {
1001         /* measure all the VM save areas before getting launch_measure */
1002         ret = sev_launch_update_vmsa(sev_guest);
1003         if (ret) {
1004             exit(1);
1005         }
1006         kvm_mark_guest_state_protected();
1007     }
1008 
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));
1015         return;
1016     }
1017 
1018     data = g_new0(guchar, measurement.len);
1019     measurement.uaddr = (unsigned long)data;
1020 
1021     /* get the measurement blob */
1022     ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_LAUNCH_MEASURE,
1023                     &measurement, &error);
1024     if (ret) {
1025         error_report("%s: LAUNCH_MEASURE ret=%d fw_error=%d '%s'",
1026                      __func__, ret, error, fw_error_to_str(errno));
1027         return;
1028     }
1029 
1030     sev_set_guest_state(sev_common, SEV_STATE_LAUNCH_SECRET);
1031 
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);
1035 }
1036 
1037 static char *sev_get_launch_measurement(void)
1038 {
1039     ConfidentialGuestSupport *cgs = MACHINE(qdev_get_machine())->cgs;
1040     SevGuestState *sev_guest =
1041         (SevGuestState *)object_dynamic_cast(OBJECT(cgs), TYPE_SEV_GUEST);
1042 
1043     if (sev_guest &&
1044         SEV_COMMON(sev_guest)->state >= SEV_STATE_LAUNCH_SECRET) {
1045         return g_strdup(sev_guest->measurement);
1046     }
1047 
1048     return NULL;
1049 }
1050 
1051 SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
1052 {
1053     char *data;
1054     SevLaunchMeasureInfo *info;
1055 
1056     data = sev_get_launch_measurement();
1057     if (!data) {
1058         error_setg(errp, "SEV launch measurement is not available");
1059         return NULL;
1060     }
1061 
1062     info = g_malloc0(sizeof(*info));
1063     info->data = data;
1064 
1065     return info;
1066 }
1067 
1068 static Notifier sev_machine_done_notify = {
1069     .notify = sev_launch_get_measure,
1070 };
1071 
1072 static void
1073 sev_launch_finish(SevCommonState *sev_common)
1074 {
1075     int ret, error;
1076 
1077     trace_kvm_sev_launch_finish();
1078     ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_LAUNCH_FINISH, 0,
1079                     &error);
1080     if (ret) {
1081         error_report("%s: LAUNCH_FINISH ret=%d fw_error=%d '%s'",
1082                      __func__, ret, error, fw_error_to_str(error));
1083         exit(1);
1084     }
1085 
1086     sev_set_guest_state(sev_common, SEV_STATE_RUNNING);
1087 
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);
1092 }
1093 
1094 static int
1095 snp_launch_update_data(uint64_t gpa, void *hva, size_t len, int type)
1096 {
1097     SevLaunchUpdateData *data;
1098 
1099     data = g_new0(SevLaunchUpdateData, 1);
1100     data->gpa = gpa;
1101     data->hva = hva;
1102     data->len = len;
1103     data->type = type;
1104 
1105     QTAILQ_INSERT_TAIL(&launch_update, data, next);
1106 
1107     return 0;
1108 }
1109 
1110 static int
1111 sev_snp_launch_update_data(SevCommonState *sev_common, hwaddr gpa,
1112                            uint8_t *ptr, size_t len)
1113 {
1114        int ret = snp_launch_update_data(gpa, ptr, len,
1115                                          KVM_SEV_SNP_PAGE_TYPE_NORMAL);
1116        return ret;
1117 }
1118 
1119 static int
1120 sev_snp_cpuid_info_fill(SnpCpuidInfo *snp_cpuid_info,
1121                         const KvmCpuidInfo *kvm_cpuid_info)
1122 {
1123     size_t i;
1124 
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);
1128         return -1;
1129     }
1130 
1131     memset(snp_cpuid_info, 0, sizeof(*snp_cpuid_info));
1132 
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;
1136 
1137         kvm_cpuid_entry = &kvm_cpuid_info->entries[i];
1138         snp_cpuid_entry = &snp_cpuid_info->entries[i];
1139 
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;
1143         }
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;
1148 
1149         /*
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.
1154          */
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;
1160         }
1161     }
1162 
1163     snp_cpuid_info->count = i;
1164 
1165     return 0;
1166 }
1167 
1168 static int
1169 snp_launch_update_cpuid(uint32_t cpuid_addr, void *hva, size_t cpuid_len)
1170 {
1171     KvmCpuidInfo kvm_cpuid_info = {0};
1172     SnpCpuidInfo snp_cpuid_info;
1173     CPUState *cs = first_cpu;
1174     int ret;
1175     uint32_t i = 0;
1176 
1177     assert(sizeof(snp_cpuid_info) <= cpuid_len);
1178 
1179     /* get the cpuid list from KVM */
1180     do {
1181         kvm_cpuid_info.cpuid.nent = ++i;
1182         ret = kvm_vcpu_ioctl(cs, KVM_GET_CPUID2, &kvm_cpuid_info);
1183     } while (ret == -E2BIG);
1184 
1185     if (ret) {
1186         error_report("SEV-SNP: unable to query CPUID values for CPU: '%s'",
1187                      strerror(-ret));
1188         return 1;
1189     }
1190 
1191     ret = sev_snp_cpuid_info_fill(&snp_cpuid_info, &kvm_cpuid_info);
1192     if (ret) {
1193         error_report("SEV-SNP: failed to generate CPUID table information");
1194         return 1;
1195     }
1196 
1197     memcpy(hva, &snp_cpuid_info, sizeof(snp_cpuid_info));
1198 
1199     return snp_launch_update_data(cpuid_addr, hva, cpuid_len,
1200                                   KVM_SEV_SNP_PAGE_TYPE_CPUID);
1201 }
1202 
1203 static int
1204 snp_launch_update_kernel_hashes(SevSnpGuestState *sev_snp, uint32_t addr,
1205                                 void *hva, uint32_t len)
1206 {
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;
1216     }
1217     return snp_launch_update_data(addr, hva, len, type);
1218 }
1219 
1220 static int
1221 snp_metadata_desc_to_page_type(int desc_type)
1222 {
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;
1228     default:
1229          return KVM_SEV_SNP_PAGE_TYPE_ZERO;
1230     }
1231 }
1232 
1233 static void
1234 snp_populate_metadata_pages(SevSnpGuestState *sev_snp,
1235                             OvmfSevMetadata *metadata)
1236 {
1237     OvmfSevMetadataDesc *desc;
1238     int type, ret, i;
1239     void *hva;
1240     MemoryRegion *mr = NULL;
1241 
1242     for (i = 0; i < metadata->num_desc; i++) {
1243         desc = &metadata->descs[i];
1244 
1245         type = snp_metadata_desc_to_page_type(desc->type);
1246 
1247         hva = gpa2hva(&mr, desc->base, desc->len, NULL);
1248         if (!hva) {
1249             error_report("%s: Failed to get HVA for GPA 0x%x sz 0x%x",
1250                          __func__, desc->base, desc->len);
1251             exit(1);
1252         }
1253 
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,
1258                                                   desc->len);
1259         } else {
1260             ret = snp_launch_update_data(desc->base, hva, desc->len, type);
1261         }
1262 
1263         if (ret) {
1264             error_report("%s: Failed to add metadata page gpa 0x%x+%x type %d",
1265                          __func__, desc->base, desc->len, desc->type);
1266             exit(1);
1267         }
1268     }
1269 }
1270 
1271 static void
1272 sev_snp_launch_finish(SevCommonState *sev_common)
1273 {
1274     int ret, error;
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;
1280 
1281     /*
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.
1285      */
1286     metadata = pc_system_get_ovmf_sev_metadata_ptr();
1287     if (metadata == NULL) {
1288         error_report("%s: Failed to locate SEV metadata header", __func__);
1289         exit(1);
1290     }
1291 
1292     /* Populate all the metadata pages */
1293     snp_populate_metadata_pages(sev_snp, metadata);
1294 
1295     QTAILQ_FOREACH(data, &launch_update, next) {
1296         ret = sev_snp_launch_update(sev_snp, data);
1297         if (ret) {
1298             exit(1);
1299         }
1300     }
1301 
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,
1305                     finish, &error);
1306     if (ret) {
1307         error_report("SNP_LAUNCH_FINISH ret=%d fw_error=%d '%s'",
1308                      ret, error, fw_error_to_str(error));
1309         exit(1);
1310     }
1311 
1312     kvm_mark_guest_state_protected();
1313     sev_set_guest_state(sev_common, SEV_STATE_RUNNING);
1314 
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);
1319     if (local_err) {
1320         error_report_err(local_err);
1321         error_free(sev_mig_blocker);
1322         exit(1);
1323     }
1324 }
1325 
1326 
1327 static void
1328 sev_vm_state_change(void *opaque, bool running, RunState state)
1329 {
1330     SevCommonState *sev_common = opaque;
1331     SevCommonStateClass *klass = SEV_COMMON_GET_CLASS(opaque);
1332 
1333     if (running) {
1334         if (!sev_check_state(sev_common, SEV_STATE_RUNNING)) {
1335             klass->launch_finish(sev_common);
1336         }
1337     }
1338 }
1339 
1340 static int sev_kvm_type(X86ConfidentialGuest *cg)
1341 {
1342     SevCommonState *sev_common = SEV_COMMON(cg);
1343     SevGuestState *sev_guest = SEV_GUEST(sev_common);
1344     int kvm_type;
1345 
1346     if (sev_common->kvm_type != -1) {
1347         goto out;
1348     }
1349 
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;
1354     } else {
1355         sev_common->kvm_type = KVM_X86_DEFAULT_VM;
1356     }
1357 
1358 out:
1359     return sev_common->kvm_type;
1360 }
1361 
1362 static int sev_snp_kvm_type(X86ConfidentialGuest *cg)
1363 {
1364     return KVM_X86_SNP_VM;
1365 }
1366 
1367 static int sev_common_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
1368 {
1369     char *devname;
1370     int ret, fw_error, cmd;
1371     uint32_t ebx;
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);
1378 
1379     sev_common->state = SEV_STATE_UNINIT;
1380 
1381     host_cpuid(0x8000001F, 0, NULL, &ebx, NULL, NULL);
1382     host_cbitpos = ebx & 0x3f;
1383 
1384     /*
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.
1388      */
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);
1392         return -1;
1393     }
1394 
1395     /*
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.
1399      */
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);
1405         return -1;
1406     }
1407 
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));
1413         g_free(devname);
1414         return -1;
1415     }
1416     g_free(devname);
1417 
1418     ret = sev_platform_ioctl(sev_common->sev_fd, SEV_PLATFORM_STATUS, &status,
1419                              &fw_error);
1420     if (ret) {
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));
1424         return -1;
1425     }
1426     sev_common->build_id = status.build;
1427     sev_common->api_major = status.api_major;
1428     sev_common->api_minor = status.api_minor;
1429 
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__);
1434             return -1;
1435         }
1436     }
1437 
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",
1442                          __func__);
1443             return -1;
1444         }
1445     }
1446 
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;
1450 
1451         ret = sev_ioctl(sev_common->sev_fd, cmd, NULL, &fw_error);
1452     } else {
1453         struct kvm_sev_init args = { 0 };
1454 
1455         ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_INIT2, &args, &fw_error);
1456     }
1457 
1458     if (ret) {
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));
1461         return -1;
1462     }
1463 
1464     ret = klass->launch_start(sev_common);
1465 
1466     if (ret) {
1467         error_setg(errp, "%s: failed to create encryption context", __func__);
1468         return -1;
1469     }
1470 
1471     if (klass->kvm_init && klass->kvm_init(cgs, errp)) {
1472         return -1;
1473     }
1474 
1475     qemu_add_vm_change_state_handler(sev_vm_state_change, sev_common);
1476 
1477     cgs->ready = true;
1478 
1479     return 0;
1480 }
1481 
1482 static int sev_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
1483 {
1484      int ret;
1485 
1486     /*
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
1490      * possible..
1491      */
1492     ret = ram_block_discard_disable(true);
1493     if (ret) {
1494         error_setg(errp, "%s: cannot disable RAM discard", __func__);
1495         return -1;
1496     }
1497 
1498     /*
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.
1502      */
1503     ram_block_notifier_add(&sev_ram_notifier);
1504 
1505     /*
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.
1511      */
1512     qemu_add_machine_init_done_notifier(&sev_machine_done_notify);
1513 
1514     return 0;
1515 }
1516 
1517 static int sev_snp_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
1518 {
1519     MachineState *ms = MACHINE(qdev_get_machine());
1520     X86MachineState *x86ms = X86_MACHINE(ms);
1521 
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.");
1526         return -1;
1527     }
1528 
1529     return 0;
1530 }
1531 
1532 int
1533 sev_encrypt_flash(hwaddr gpa, uint8_t *ptr, uint64_t len, Error **errp)
1534 {
1535     SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs);
1536     SevCommonStateClass *klass;
1537 
1538     if (!sev_common) {
1539         return 0;
1540     }
1541     klass = SEV_COMMON_GET_CLASS(sev_common);
1542 
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)) {
1545         int ret;
1546 
1547         ret = klass->launch_update_data(sev_common, gpa, ptr, len);
1548         if (ret < 0) {
1549             error_setg(errp, "SEV: Failed to encrypt pflash rom");
1550             return ret;
1551         }
1552     }
1553 
1554     return 0;
1555 }
1556 
1557 int sev_inject_launch_secret(const char *packet_hdr, const char *secret,
1558                              uint64_t gpa, Error **errp)
1559 {
1560     ERRP_GUARD();
1561     struct kvm_sev_launch_secret input;
1562     g_autofree guchar *data = NULL, *hdr = NULL;
1563     int error, ret = 1;
1564     void *hva;
1565     gsize hdr_sz = 0, data_sz = 0;
1566     MemoryRegion *mr = NULL;
1567     SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs);
1568 
1569     if (!sev_common) {
1570         error_setg(errp, "SEV not enabled for guest");
1571         return 1;
1572     }
1573 
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",
1577                    sev_common->state);
1578         return 1;
1579     }
1580 
1581     hdr = g_base64_decode(packet_hdr, &hdr_sz);
1582     if (!hdr || !hdr_sz) {
1583         error_setg(errp, "SEV: Failed to decode sequence header");
1584         return 1;
1585     }
1586 
1587     data = g_base64_decode(secret, &data_sz);
1588     if (!data || !data_sz) {
1589         error_setg(errp, "SEV: Failed to decode data");
1590         return 1;
1591     }
1592 
1593     hva = gpa2hva(&mr, gpa, data_sz, errp);
1594     if (!hva) {
1595         error_prepend(errp, "SEV: Failed to calculate guest address: ");
1596         return 1;
1597     }
1598 
1599     input.hdr_uaddr = (uint64_t)(unsigned long)hdr;
1600     input.hdr_len = hdr_sz;
1601 
1602     input.trans_uaddr = (uint64_t)(unsigned long)data;
1603     input.trans_len = data_sz;
1604 
1605     input.guest_uaddr = (uint64_t)(unsigned long)hva;
1606     input.guest_len = data_sz;
1607 
1608     trace_kvm_sev_launch_secret(gpa, input.guest_uaddr,
1609                                 input.trans_uaddr, input.trans_len);
1610 
1611     ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_LAUNCH_SECRET,
1612                     &input, &error);
1613     if (ret) {
1614         error_setg(errp, "SEV: failed to inject secret ret=%d fw_error=%d '%s'",
1615                      ret, error, fw_error_to_str(error));
1616         return ret;
1617     }
1618 
1619     return 0;
1620 }
1621 
1622 #define SEV_SECRET_GUID "4c2eb361-7d9b-4cc3-8081-127c90d3d294"
1623 struct sev_secret_area {
1624     uint32_t base;
1625     uint32_t size;
1626 };
1627 
1628 void qmp_sev_inject_launch_secret(const char *packet_hdr,
1629                                   const char *secret,
1630                                   bool has_gpa, uint64_t gpa,
1631                                   Error **errp)
1632 {
1633     if (!sev_enabled()) {
1634         error_setg(errp, "SEV not enabled for guest");
1635         return;
1636     }
1637     if (!has_gpa) {
1638         uint8_t *data;
1639         struct sev_secret_area *area;
1640 
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.");
1644             return;
1645         }
1646         area = (struct sev_secret_area *)data;
1647         gpa = area->base;
1648     }
1649 
1650     sev_inject_launch_secret(packet_hdr, secret, gpa, errp);
1651 }
1652 
1653 static int
1654 sev_es_parse_reset_block(SevInfoBlock *info, uint32_t *addr)
1655 {
1656     if (!info->reset_addr) {
1657         error_report("SEV-ES reset address is zero");
1658         return 1;
1659     }
1660 
1661     *addr = info->reset_addr;
1662 
1663     return 0;
1664 }
1665 
1666 static int
1667 sev_es_find_reset_vector(void *flash_ptr, uint64_t flash_size,
1668                          uint32_t *addr)
1669 {
1670     QemuUUID info_guid, *guid;
1671     SevInfoBlock *info;
1672     uint8_t *data;
1673     uint16_t *len;
1674 
1675     /*
1676      * Initialize the address to zero. An address of zero with a successful
1677      * return code indicates that SEV-ES is not active.
1678      */
1679     *addr = 0;
1680 
1681     /*
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.
1686      *
1687      * Check the Firmware GUID Table first.
1688      */
1689     if (pc_system_ovmf_table_find(SEV_INFO_BLOCK_GUID, &data, NULL)) {
1690         return sev_es_parse_reset_block((SevInfoBlock *)data, addr);
1691     }
1692 
1693     /*
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.
1696      */
1697     data = flash_ptr + flash_size - 0x20;
1698 
1699     qemu_uuid_parse(SEV_INFO_BLOCK_GUID, &info_guid);
1700     info_guid = qemu_uuid_bswap(info_guid); /* GUIDs are LE */
1701 
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");
1705         return 1;
1706     }
1707 
1708     len = (uint16_t *)((uint8_t *)guid - sizeof(*len));
1709     info = (SevInfoBlock *)(data - le16_to_cpu(*len));
1710 
1711     return sev_es_parse_reset_block(info, addr);
1712 }
1713 
1714 void sev_es_set_reset_vector(CPUState *cpu)
1715 {
1716     X86CPU *x86;
1717     CPUX86State *env;
1718     ConfidentialGuestSupport *cgs = MACHINE(qdev_get_machine())->cgs;
1719     SevCommonState *sev_common = SEV_COMMON(
1720         object_dynamic_cast(OBJECT(cgs), TYPE_SEV_COMMON));
1721 
1722     /* Only update if we have valid reset information */
1723     if (!sev_common || !sev_common->reset_data_valid) {
1724         return;
1725     }
1726 
1727     /* Do not update the BSP reset state */
1728     if (cpu->cpu_index == 0) {
1729         return;
1730     }
1731 
1732     x86 = X86_CPU(cpu);
1733     env = &x86->env;
1734 
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);
1738 
1739     env->eip = sev_common->reset_ip;
1740 }
1741 
1742 int sev_es_save_reset_vector(void *flash_ptr, uint64_t flash_size)
1743 {
1744     CPUState *cpu;
1745     uint32_t addr;
1746     int ret;
1747     SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs);
1748 
1749     if (!sev_es_enabled()) {
1750         return 0;
1751     }
1752 
1753     addr = 0;
1754     ret = sev_es_find_reset_vector(flash_ptr, flash_size,
1755                                    &addr);
1756     if (ret) {
1757         return ret;
1758     }
1759 
1760     if (addr) {
1761         sev_common->reset_cs = addr & 0xffff0000;
1762         sev_common->reset_ip = addr & 0x0000ffff;
1763         sev_common->reset_data_valid = true;
1764 
1765         CPU_FOREACH(cpu) {
1766             sev_es_set_reset_vector(cpu);
1767         }
1768     }
1769 
1770     return 0;
1771 }
1772 
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)
1776 };
1777 
1778 static const QemuUUID sev_kernel_entry_guid = {
1779     .data = UUID_LE(0x4de79437, 0xabd2, 0x427f, 0xb8, 0x35, 0xd5, 0xb1,
1780                     0x72, 0xd2, 0x04, 0x5b)
1781 };
1782 static const QemuUUID sev_initrd_entry_guid = {
1783     .data = UUID_LE(0x44baf731, 0x3a2f, 0x4bd7, 0x9a, 0xf1, 0x41, 0xe2,
1784                     0x91, 0x69, 0x78, 0x1d)
1785 };
1786 static const QemuUUID sev_cmdline_entry_guid = {
1787     .data = UUID_LE(0x97d02dd8, 0xbd20, 0x4c94, 0xaa, 0x78, 0xe7, 0x71,
1788                     0x4d, 0x36, 0xab, 0x2a)
1789 };
1790 
1791 static bool build_kernel_loader_hashes(PaddedSevHashTable *padded_ht,
1792                                        SevKernelLoaderContext *ctx,
1793                                        Error **errp)
1794 {
1795     SevHashTable *ht;
1796     uint8_t cmdline_hash[HASH_SIZE];
1797     uint8_t initrd_hash[HASH_SIZE];
1798     uint8_t kernel_hash[HASH_SIZE];
1799     uint8_t *hashp;
1800     size_t hash_len = HASH_SIZE;
1801 
1802     /*
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
1805      * be used.
1806      */
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) {
1810         return false;
1811     }
1812     assert(hash_len == HASH_SIZE);
1813 
1814     /*
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).
1817      */
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) {
1821         return false;
1822     }
1823     assert(hash_len == HASH_SIZE);
1824 
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 }
1830     };
1831     if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA256, iov, ARRAY_SIZE(iov),
1832                             &hashp, &hash_len, errp) < 0) {
1833         return false;
1834     }
1835     assert(hash_len == HASH_SIZE);
1836 
1837     ht = &padded_ht->ht;
1838 
1839     ht->guid = sev_hash_table_header_guid;
1840     ht->len = sizeof(*ht);
1841 
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));
1845 
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));
1849 
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));
1853 
1854     /* zero the excess data so the measurement can be reliably calculated */
1855     memset(padded_ht->padding, 0, sizeof(padded_ht->padding));
1856 
1857     return true;
1858 }
1859 
1860 static bool sev_snp_build_kernel_loader_hashes(SevCommonState *sev_common,
1861                                                SevHashTableDescriptor *area,
1862                                                SevKernelLoaderContext *ctx,
1863                                                Error **errp)
1864 {
1865     /*
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
1868      * and encrypted.
1869      */
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);
1874 }
1875 
1876 static bool sev_build_kernel_loader_hashes(SevCommonState *sev_common,
1877                                            SevHashTableDescriptor *area,
1878                                            SevKernelLoaderContext *ctx,
1879                                            Error **errp)
1880 {
1881     PaddedSevHashTable *padded_ht;
1882     hwaddr mapped_len = sizeof(*padded_ht);
1883     MemTxAttrs attrs = { 0 };
1884     bool ret = true;
1885 
1886     /*
1887      * Populate the hashes table in the guest's memory at the OVMF-designated
1888      * area for the SEV hashes table
1889      */
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");
1894         return false;
1895     }
1896 
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) {
1900             ret = false;
1901         }
1902     } else {
1903         ret = false;
1904     }
1905 
1906     address_space_unmap(&address_space_memory, padded_ht,
1907                         mapped_len, true, mapped_len);
1908 
1909     return ret;
1910 }
1911 
1912 /*
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.
1915  */
1916 bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp)
1917 {
1918     uint8_t *data;
1919     SevHashTableDescriptor *area;
1920     SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs);
1921     SevCommonStateClass *klass = SEV_COMMON_GET_CLASS(sev_common);
1922 
1923     /*
1924      * Only add the kernel hashes if the sev-guest configuration explicitly
1925      * stated kernel-hashes=on.
1926      */
1927     if (!sev_common->kernel_hashes) {
1928         return false;
1929     }
1930 
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");
1934         return false;
1935     }
1936 
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);
1941         return false;
1942     }
1943 
1944     return klass->build_kernel_loader_hashes(sev_common, area, ctx, errp);
1945 }
1946 
1947 static char *
1948 sev_common_get_sev_device(Object *obj, Error **errp)
1949 {
1950     return g_strdup(SEV_COMMON(obj)->sev_device);
1951 }
1952 
1953 static void
1954 sev_common_set_sev_device(Object *obj, const char *value, Error **errp)
1955 {
1956     SEV_COMMON(obj)->sev_device = g_strdup(value);
1957 }
1958 
1959 static bool sev_common_get_kernel_hashes(Object *obj, Error **errp)
1960 {
1961     return SEV_COMMON(obj)->kernel_hashes;
1962 }
1963 
1964 static void sev_common_set_kernel_hashes(Object *obj, bool value, Error **errp)
1965 {
1966     SEV_COMMON(obj)->kernel_hashes = value;
1967 }
1968 
1969 static void
1970 sev_common_class_init(ObjectClass *oc, void *data)
1971 {
1972     ConfidentialGuestSupportClass *klass = CONFIDENTIAL_GUEST_SUPPORT_CLASS(oc);
1973 
1974     klass->kvm_init = sev_common_kvm_init;
1975 
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");
1986 }
1987 
1988 static void
1989 sev_common_instance_init(Object *obj)
1990 {
1991     SevCommonState *sev_common = SEV_COMMON(obj);
1992 
1993     sev_common->kvm_type = -1;
1994 
1995     sev_common->sev_device = g_strdup(DEFAULT_SEV_DEVICE);
1996 
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);
2002 }
2003 
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,
2012     .abstract = true,
2013     .interfaces = (InterfaceInfo[]) {
2014         { TYPE_USER_CREATABLE },
2015         { }
2016     }
2017 };
2018 
2019 static char *
2020 sev_guest_get_dh_cert_file(Object *obj, Error **errp)
2021 {
2022     return g_strdup(SEV_GUEST(obj)->dh_cert_file);
2023 }
2024 
2025 static void
2026 sev_guest_set_dh_cert_file(Object *obj, const char *value, Error **errp)
2027 {
2028     SEV_GUEST(obj)->dh_cert_file = g_strdup(value);
2029 }
2030 
2031 static char *
2032 sev_guest_get_session_file(Object *obj, Error **errp)
2033 {
2034     SevGuestState *sev_guest = SEV_GUEST(obj);
2035 
2036     return sev_guest->session_file ? g_strdup(sev_guest->session_file) : NULL;
2037 }
2038 
2039 static void
2040 sev_guest_set_session_file(Object *obj, const char *value, Error **errp)
2041 {
2042     SEV_GUEST(obj)->session_file = g_strdup(value);
2043 }
2044 
2045 static bool sev_guest_get_legacy_vm_type(Object *obj, Error **errp)
2046 {
2047     return SEV_GUEST(obj)->legacy_vm_type;
2048 }
2049 
2050 static void sev_guest_set_legacy_vm_type(Object *obj, bool value, Error **errp)
2051 {
2052     SEV_GUEST(obj)->legacy_vm_type = value;
2053 }
2054 
2055 static void
2056 sev_guest_class_init(ObjectClass *oc, void *data)
2057 {
2058     SevCommonStateClass *klass = SEV_COMMON_CLASS(oc);
2059     X86ConfidentialGuestClass *x86_klass = X86_CONFIDENTIAL_GUEST_CLASS(oc);
2060 
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;
2067 
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.");
2083 }
2084 
2085 static void
2086 sev_guest_instance_init(Object *obj)
2087 {
2088     SevGuestState *sev_guest = SEV_GUEST(obj);
2089 
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);
2096 }
2097 
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,
2105 };
2106 
2107 static void
2108 sev_snp_guest_get_policy(Object *obj, Visitor *v, const char *name,
2109                          void *opaque, Error **errp)
2110 {
2111     visit_type_uint64(v, name,
2112                       (uint64_t *)&SEV_SNP_GUEST(obj)->kvm_start_conf.policy,
2113                       errp);
2114 }
2115 
2116 static void
2117 sev_snp_guest_set_policy(Object *obj, Visitor *v, const char *name,
2118                          void *opaque, Error **errp)
2119 {
2120     visit_type_uint64(v, name,
2121                       (uint64_t *)&SEV_SNP_GUEST(obj)->kvm_start_conf.policy,
2122                       errp);
2123 }
2124 
2125 static char *
2126 sev_snp_guest_get_guest_visible_workarounds(Object *obj, Error **errp)
2127 {
2128     return g_strdup(SEV_SNP_GUEST(obj)->guest_visible_workarounds);
2129 }
2130 
2131 static void
2132 sev_snp_guest_set_guest_visible_workarounds(Object *obj, const char *value,
2133                                             Error **errp)
2134 {
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;
2138     gsize len;
2139 
2140     g_free(sev_snp_guest->guest_visible_workarounds);
2141 
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);
2144 
2145     blob = qbase64_decode(sev_snp_guest->guest_visible_workarounds,
2146                           -1, &len, errp);
2147     if (!blob) {
2148         return;
2149     }
2150 
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));
2155         return;
2156     }
2157 
2158     memcpy(start->gosvw, blob, len);
2159 }
2160 
2161 static char *
2162 sev_snp_guest_get_id_block(Object *obj, Error **errp)
2163 {
2164     SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj);
2165 
2166     return g_strdup(sev_snp_guest->id_block_base64);
2167 }
2168 
2169 static void
2170 sev_snp_guest_set_id_block(Object *obj, const char *value, Error **errp)
2171 {
2172     SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj);
2173     struct kvm_sev_snp_launch_finish *finish = &sev_snp_guest->kvm_finish_conf;
2174     gsize len;
2175 
2176     finish->id_block_en = 0;
2177     g_free(sev_snp_guest->id_block);
2178     g_free(sev_snp_guest->id_block_base64);
2179 
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);
2184 
2185     if (!sev_snp_guest->id_block) {
2186         return;
2187     }
2188 
2189     if (len != KVM_SEV_SNP_ID_BLOCK_SIZE) {
2190         error_setg(errp, "parameter length of %" G_GSIZE_FORMAT
2191                    " not equal to %u",
2192                    len, KVM_SEV_SNP_ID_BLOCK_SIZE);
2193         return;
2194     }
2195 
2196     finish->id_block_en = 1;
2197     finish->id_block_uaddr = (uintptr_t)sev_snp_guest->id_block;
2198 }
2199 
2200 static char *
2201 sev_snp_guest_get_id_auth(Object *obj, Error **errp)
2202 {
2203     SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj);
2204 
2205     return g_strdup(sev_snp_guest->id_auth_base64);
2206 }
2207 
2208 static void
2209 sev_snp_guest_set_id_auth(Object *obj, const char *value, Error **errp)
2210 {
2211     SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj);
2212     struct kvm_sev_snp_launch_finish *finish = &sev_snp_guest->kvm_finish_conf;
2213     gsize len;
2214 
2215     finish->id_auth_uaddr = 0;
2216     g_free(sev_snp_guest->id_auth);
2217     g_free(sev_snp_guest->id_auth_base64);
2218 
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);
2223 
2224     if (!sev_snp_guest->id_auth) {
2225         return;
2226     }
2227 
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);
2232         return;
2233     }
2234 
2235     finish->id_auth_uaddr = (uintptr_t)sev_snp_guest->id_auth;
2236 }
2237 
2238 static bool
2239 sev_snp_guest_get_author_key_enabled(Object *obj, Error **errp)
2240 {
2241     SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj);
2242 
2243     return !!sev_snp_guest->kvm_finish_conf.auth_key_en;
2244 }
2245 
2246 static void
2247 sev_snp_guest_set_author_key_enabled(Object *obj, bool value, Error **errp)
2248 {
2249     SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj);
2250 
2251     sev_snp_guest->kvm_finish_conf.auth_key_en = value;
2252 }
2253 
2254 static bool
2255 sev_snp_guest_get_vcek_disabled(Object *obj, Error **errp)
2256 {
2257     SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj);
2258 
2259     return !!sev_snp_guest->kvm_finish_conf.vcek_disabled;
2260 }
2261 
2262 static void
2263 sev_snp_guest_set_vcek_disabled(Object *obj, bool value, Error **errp)
2264 {
2265     SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj);
2266 
2267     sev_snp_guest->kvm_finish_conf.vcek_disabled = value;
2268 }
2269 
2270 static char *
2271 sev_snp_guest_get_host_data(Object *obj, Error **errp)
2272 {
2273     SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj);
2274 
2275     return g_strdup(sev_snp_guest->host_data);
2276 }
2277 
2278 static void
2279 sev_snp_guest_set_host_data(Object *obj, const char *value, Error **errp)
2280 {
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;
2284     gsize len;
2285 
2286     g_free(sev_snp_guest->host_data);
2287 
2288     /* store the base64 str so we don't need to re-encode in getter */
2289     sev_snp_guest->host_data = g_strdup(value);
2290 
2291     blob = qbase64_decode(sev_snp_guest->host_data, -1, &len, errp);
2292 
2293     if (!blob) {
2294         return;
2295     }
2296 
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));
2301         return;
2302     }
2303 
2304     memcpy(finish->host_data, blob, len);
2305 }
2306 
2307 static void
2308 sev_snp_guest_class_init(ObjectClass *oc, void *data)
2309 {
2310     SevCommonStateClass *klass = SEV_COMMON_CLASS(oc);
2311     X86ConfidentialGuestClass *x86_klass = X86_CONFIDENTIAL_GUEST_CLASS(oc);
2312 
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;
2319 
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);
2341 }
2342 
2343 static void
2344 sev_snp_guest_instance_init(Object *obj)
2345 {
2346     ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
2347     SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj);
2348 
2349     cgs->require_guest_memfd = true;
2350 
2351     /* default init/start/finish params for kvm */
2352     sev_snp_guest->kvm_start_conf.policy = DEFAULT_SEV_SNP_POLICY;
2353 }
2354 
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,
2362 };
2363 
2364 static void
2365 sev_register_types(void)
2366 {
2367     type_register_static(&sev_common_info);
2368     type_register_static(&sev_guest_info);
2369     type_register_static(&sev_snp_guest_info);
2370 }
2371 
2372 type_init(sev_register_types);
2373