| 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 "exec/target_page.h" |
| 30 | #include "system/kvm.h" |
| 31 | #include "kvm/kvm_i386.h" |
| 32 | #include "sev.h" |
| 33 | #include "system/cpus.h" |
| 34 | #include "system/system.h" |
| 35 | #include "system/runstate.h" |
| 36 | #include "system/reset.h" |
| 37 | #include "trace.h" |
| 38 | #include "migration/blocker.h" |
| 39 | #include "qom/compat-properties.h" |
| 40 | #include "qom/object.h" |
| 41 | #include "monitor/monitor.h" |
| 42 | #include "monitor/hmp.h" |
| 43 | #include "qapi/qapi-commands-misc-i386.h" |
| 44 | #include "confidential-guest.h" |
| 45 | #include "hw/i386/pc.h" |
| 46 | #include "system/address-spaces.h" |
| 47 | #include "system/ramlist.h" |
| 48 | #include "hw/i386/e820_memory_layout.h" |
| 49 | #include "qemu/queue.h" |
| 50 | #include "qemu/cutils.h" |
| 51 | |
| 52 | OBJECT_DECLARE_TYPE(SevCommonState, SevCommonStateClass, SEV_COMMON) |
| 53 | OBJECT_DECLARE_TYPE(SevGuestState, SevCommonStateClass, SEV_GUEST) |
| 54 | OBJECT_DECLARE_TYPE(SevSnpGuestState, SevCommonStateClass, SEV_SNP_GUEST) |
| 55 | |
| 56 | /* hard code sha256 digest size */ |
| 57 | #define HASH_SIZE 32 |
| 58 | |
| 59 | /* Hard coded GPA that KVM uses for the VMSA */ |
| 60 | #define KVM_VMSA_GPA 0xFFFFFFFFF000 |
| 61 | |
| 62 | /* Convert between SEV-ES VMSA and SegmentCache flags/attributes */ |
| 63 | #define FLAGS_VMSA_TO_SEGCACHE(flags) \ |
| 64 | ((((flags) & 0xff00) << 12) | (((flags) & 0xff) << 8)) |
| 65 | #define FLAGS_SEGCACHE_TO_VMSA(flags) \ |
| 66 | ((((flags) & 0xff00) >> 8) | (((flags) & 0xf00000) >> 12)) |
| 67 | |
| 68 | typedef struct QEMU_PACKED SevHashTableEntry { |
| 69 | QemuUUID guid; |
| 70 | uint16_t len; |
| 71 | uint8_t hash[HASH_SIZE]; |
| 72 | } SevHashTableEntry; |
| 73 | |
| 74 | typedef struct QEMU_PACKED SevHashTable { |
| 75 | QemuUUID guid; |
| 76 | uint16_t len; |
| 77 | SevHashTableEntry cmdline; |
| 78 | SevHashTableEntry initrd; |
| 79 | SevHashTableEntry kernel; |
| 80 | } SevHashTable; |
| 81 | |
| 82 | /* |
| 83 | * Data encrypted by sev_encrypt_flash() must be padded to a multiple of |
| 84 | * 16 bytes. |
| 85 | */ |
| 86 | typedef struct QEMU_PACKED PaddedSevHashTable { |
| 87 | SevHashTable ht; |
| 88 | uint8_t padding[ROUND_UP(sizeof(SevHashTable), 16) - sizeof(SevHashTable)]; |
| 89 | } PaddedSevHashTable; |
| 90 | |
| 91 | static void sev_handle_reset(Object *obj, ResetType type); |
| 92 | |
| 93 | SevKernelLoaderContext sev_load_ctx = {}; |
| 94 | |
| 95 | QEMU_BUILD_BUG_ON(sizeof(PaddedSevHashTable) % 16 != 0); |
| 96 | |
| 97 | #define SEV_INFO_BLOCK_GUID "00f771de-1a7e-4fcb-890e-68c77e2fb44e" |
| 98 | typedef struct __attribute__((__packed__)) SevInfoBlock { |
| 99 | /* SEV-ES Reset Vector Address */ |
| 100 | uint32_t reset_addr; |
| 101 | } SevInfoBlock; |
| 102 | |
| 103 | #define SEV_HASH_TABLE_RV_GUID "7255371f-3a3b-4b04-927b-1da6efa8d454" |
| 104 | typedef struct QEMU_PACKED SevHashTableDescriptor { |
| 105 | /* SEV hash table area guest address */ |
| 106 | uint32_t base; |
| 107 | /* SEV hash table area size (in bytes) */ |
| 108 | uint32_t size; |
| 109 | } SevHashTableDescriptor; |
| 110 | |
| 111 | typedef struct SevLaunchVmsa { |
| 112 | QTAILQ_ENTRY(SevLaunchVmsa) next; |
| 113 | |
| 114 | uint16_t cpu_index; |
| 115 | uint64_t gpa; |
| 116 | struct sev_es_save_area vmsa; |
| 117 | } SevLaunchVmsa; |
| 118 | |
| 119 | struct SevCommonState { |
| 120 | X86ConfidentialGuest parent_obj; |
| 121 | |
| 122 | int kvm_type; |
| 123 | |
| 124 | /* configuration parameters */ |
| 125 | char *sev_device; |
| 126 | uint32_t cbitpos; |
| 127 | uint32_t reduced_phys_bits; |
| 128 | bool kernel_hashes; |
| 129 | uint64_t sev_features; |
| 130 | uint64_t supported_sev_features; |
| 131 | |
| 132 | /* runtime state */ |
| 133 | uint8_t api_major; |
| 134 | uint8_t api_minor; |
| 135 | uint8_t build_id; |
| 136 | int sev_fd; |
| 137 | SevState state; |
| 138 | ResettableState reset_state; |
| 139 | |
| 140 | QTAILQ_HEAD(, SevLaunchVmsa) launch_vmsa; |
| 141 | }; |
| 142 | |
| 143 | struct SevCommonStateClass { |
| 144 | X86ConfidentialGuestClass parent_class; |
| 145 | |
| 146 | /* public */ |
| 147 | bool (*build_kernel_loader_hashes)(SevCommonState *sev_common, |
| 148 | SevHashTableDescriptor *area, |
| 149 | SevKernelLoaderContext *ctx, |
| 150 | Error **errp); |
| 151 | int (*launch_start)(SevCommonState *sev_common); |
| 152 | void (*launch_finish)(SevCommonState *sev_common); |
| 153 | int (*launch_update_data)(SevCommonState *sev_common, hwaddr gpa, |
| 154 | uint8_t *ptr, size_t len, Error **errp); |
| 155 | int (*kvm_init)(ConfidentialGuestSupport *cgs, Error **errp); |
| 156 | }; |
| 157 | |
| 158 | /** |
| 159 | * SevGuestState: |
| 160 | * |
| 161 | * The SevGuestState object is used for creating and managing a SEV |
| 162 | * guest. |
| 163 | */ |
| 164 | struct SevGuestState { |
| 165 | SevCommonState parent_obj; |
| 166 | gchar *measurement; |
| 167 | |
| 168 | /* configuration parameters */ |
| 169 | uint32_t handle; |
| 170 | uint32_t policy; |
| 171 | char *dh_cert_file; |
| 172 | char *session_file; |
| 173 | OnOffAuto legacy_vm_type; |
| 174 | }; |
| 175 | |
| 176 | struct SevSnpGuestState { |
| 177 | SevCommonState parent_obj; |
| 178 | |
| 179 | /* configuration parameters */ |
| 180 | char *guest_visible_workarounds; |
| 181 | char *id_block_base64; |
| 182 | uint8_t *id_block; |
| 183 | char *id_auth_base64; |
| 184 | uint8_t *id_auth; |
| 185 | char *host_data; |
| 186 | uint32_t tsc_khz; |
| 187 | |
| 188 | struct kvm_sev_snp_launch_start kvm_start_conf; |
| 189 | struct kvm_sev_snp_launch_finish kvm_finish_conf; |
| 190 | |
| 191 | uint32_t kernel_hashes_offset; |
| 192 | PaddedSevHashTable *kernel_hashes_data; |
| 193 | }; |
| 194 | |
| 195 | #define DEFAULT_GUEST_POLICY 0x1 /* disable debug */ |
| 196 | #define DEFAULT_SEV_DEVICE "/dev/sev" |
| 197 | #define DEFAULT_SEV_SNP_POLICY 0x30000 |
| 198 | |
| 199 | typedef struct SevLaunchUpdateData { |
| 200 | QTAILQ_ENTRY(SevLaunchUpdateData) next; |
| 201 | hwaddr gpa; |
| 202 | void *hva; |
| 203 | size_t len; |
| 204 | int type; |
| 205 | } SevLaunchUpdateData; |
| 206 | |
| 207 | static QTAILQ_HEAD(, SevLaunchUpdateData) launch_update; |
| 208 | |
| 209 | static Error *sev_mig_blocker; |
| 210 | |
| 211 | static const char *const sev_fw_errlist[] = { |
| 212 | [SEV_RET_SUCCESS] = "", |
| 213 | [SEV_RET_INVALID_PLATFORM_STATE] = "Platform state is invalid", |
| 214 | [SEV_RET_INVALID_GUEST_STATE] = "Guest state is invalid", |
| 215 | [SEV_RET_INAVLID_CONFIG] = "Platform configuration is invalid", |
| 216 | [SEV_RET_INVALID_LEN] = "Buffer too small", |
| 217 | [SEV_RET_ALREADY_OWNED] = "Platform is already owned", |
| 218 | [SEV_RET_INVALID_CERTIFICATE] = "Certificate is invalid", |
| 219 | [SEV_RET_POLICY_FAILURE] = "Policy is not allowed", |
| 220 | [SEV_RET_INACTIVE] = "Guest is not active", |
| 221 | [SEV_RET_INVALID_ADDRESS] = "Invalid address", |
| 222 | [SEV_RET_BAD_SIGNATURE] = "Bad signature", |
| 223 | [SEV_RET_BAD_MEASUREMENT] = "Bad measurement", |
| 224 | [SEV_RET_ASID_OWNED] = "ASID is already owned", |
| 225 | [SEV_RET_INVALID_ASID] = "Invalid ASID", |
| 226 | [SEV_RET_WBINVD_REQUIRED] = "WBINVD is required", |
| 227 | [SEV_RET_DFFLUSH_REQUIRED] = "DF_FLUSH is required", |
| 228 | [SEV_RET_INVALID_GUEST] = "Guest handle is invalid", |
| 229 | [SEV_RET_INVALID_COMMAND] = "Invalid command", |
| 230 | [SEV_RET_ACTIVE] = "Guest is active", |
| 231 | [SEV_RET_HWSEV_RET_PLATFORM] = "Hardware error", |
| 232 | [SEV_RET_HWSEV_RET_UNSAFE] = "Hardware unsafe", |
| 233 | [SEV_RET_UNSUPPORTED] = "Feature not supported", |
| 234 | [SEV_RET_INVALID_PARAM] = "Invalid parameter", |
| 235 | [SEV_RET_RESOURCE_LIMIT] = "Required firmware resource depleted", |
| 236 | [SEV_RET_SECURE_DATA_INVALID] = "Part-specific integrity check failure", |
| 237 | }; |
| 238 | |
| 239 | #define SEV_FW_MAX_ERROR ARRAY_SIZE(sev_fw_errlist) |
| 240 | |
| 241 | #define SNP_CPUID_FUNCTION_MAXCOUNT 64 |
| 242 | #define SNP_CPUID_FUNCTION_UNKNOWN 0xFFFFFFFF |
| 243 | |
| 244 | typedef struct { |
| 245 | uint32_t eax_in; |
| 246 | uint32_t ecx_in; |
| 247 | uint64_t xcr0_in; |
| 248 | uint64_t xss_in; |
| 249 | uint32_t eax; |
| 250 | uint32_t ebx; |
| 251 | uint32_t ecx; |
| 252 | uint32_t edx; |
| 253 | uint64_t reserved; |
| 254 | } __attribute__((packed)) SnpCpuidFunc; |
| 255 | |
| 256 | typedef struct { |
| 257 | uint32_t count; |
| 258 | uint32_t reserved1; |
| 259 | uint64_t reserved2; |
| 260 | SnpCpuidFunc entries[SNP_CPUID_FUNCTION_MAXCOUNT]; |
| 261 | } __attribute__((packed)) SnpCpuidInfo; |
| 262 | |
| 263 | static int |
| 264 | sev_ioctl(int fd, int cmd, void *data, int *error) |
| 265 | { |
| 266 | int r; |
| 267 | struct kvm_sev_cmd input; |
| 268 | |
| 269 | memset(&input, 0x0, sizeof(input)); |
| 270 | |
| 271 | input.id = cmd; |
| 272 | input.sev_fd = fd; |
| 273 | input.data = (uintptr_t)data; |
| 274 | |
| 275 | r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_OP, &input); |
| 276 | |
| 277 | if (error) { |
| 278 | *error = input.error; |
| 279 | } |
| 280 | |
| 281 | return r; |
| 282 | } |
| 283 | |
| 284 | static int |
| 285 | sev_platform_ioctl(int fd, int cmd, void *data, int *error) |
| 286 | { |
| 287 | int r; |
| 288 | struct sev_issue_cmd arg; |
| 289 | |
| 290 | arg.cmd = cmd; |
| 291 | arg.data = (unsigned long)data; |
| 292 | r = ioctl(fd, SEV_ISSUE_CMD, &arg); |
| 293 | if (error) { |
| 294 | *error = arg.error; |
| 295 | } |
| 296 | |
| 297 | return r; |
| 298 | } |
| 299 | |
| 300 | static const char * |
| 301 | fw_error_to_str(int code) |
| 302 | { |
| 303 | if (code < 0 || code >= SEV_FW_MAX_ERROR) { |
| 304 | return "unknown error"; |
| 305 | } |
| 306 | |
| 307 | return sev_fw_errlist[code]; |
| 308 | } |
| 309 | |
| 310 | static bool |
| 311 | sev_check_state(const SevCommonState *sev_common, SevState state) |
| 312 | { |
| 313 | assert(sev_common); |
| 314 | return sev_common->state == state ? true : false; |
| 315 | } |
| 316 | |
| 317 | static void |
| 318 | sev_set_guest_state(SevCommonState *sev_common, SevState new_state) |
| 319 | { |
| 320 | assert(new_state < SEV_STATE__MAX); |
| 321 | assert(sev_common); |
| 322 | |
| 323 | trace_kvm_sev_change_state(SevState_str(sev_common->state), |
| 324 | SevState_str(new_state)); |
| 325 | sev_common->state = new_state; |
| 326 | } |
| 327 | |
| 328 | static bool is_sev_feature_set(SevCommonState *sev_common, uint64_t feature) |
| 329 | { |
| 330 | return !!(sev_common->sev_features & feature); |
| 331 | } |
| 332 | |
| 333 | static void sev_set_feature(SevCommonState *sev_common, uint64_t feature, bool set) |
| 334 | { |
| 335 | if (set) { |
| 336 | sev_common->sev_features |= feature; |
| 337 | } else { |
| 338 | sev_common->sev_features &= ~feature; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | static void |
| 343 | sev_ram_block_added(RAMBlockNotifier *n, void *host, size_t size, |
| 344 | size_t max_size) |
| 345 | { |
| 346 | int r; |
| 347 | struct kvm_enc_region range; |
| 348 | ram_addr_t offset; |
| 349 | MemoryRegion *mr; |
| 350 | |
| 351 | /* |
| 352 | * The RAM device presents a memory region that should be treated |
| 353 | * as IO region and should not be pinned. |
| 354 | */ |
| 355 | mr = memory_region_from_host(host, &offset); |
| 356 | if (mr && memory_region_is_ram_device(mr)) { |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | range.addr = (uintptr_t)host; |
| 361 | range.size = max_size; |
| 362 | |
| 363 | trace_kvm_memcrypt_register_region(host, max_size); |
| 364 | r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_REG_REGION, &range); |
| 365 | if (r) { |
| 366 | error_report("SEV: Failed to register region (%p+%#zx) error '%s'", |
| 367 | host, max_size, strerror(errno)); |
| 368 | exit(1); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | static void |
| 373 | sev_ram_block_removed(RAMBlockNotifier *n, void *host, size_t size, |
| 374 | size_t max_size) |
| 375 | { |
| 376 | int r; |
| 377 | struct kvm_enc_region range; |
| 378 | ram_addr_t offset; |
| 379 | MemoryRegion *mr; |
| 380 | |
| 381 | /* |
| 382 | * The RAM device presents a memory region that should be treated |
| 383 | * as IO region and should not have been pinned. |
| 384 | */ |
| 385 | mr = memory_region_from_host(host, &offset); |
| 386 | if (mr && memory_region_is_ram_device(mr)) { |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | range.addr = (uintptr_t)host; |
| 391 | range.size = max_size; |
| 392 | |
| 393 | trace_kvm_memcrypt_unregister_region(host, max_size); |
| 394 | r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_UNREG_REGION, &range); |
| 395 | if (r) { |
| 396 | error_report("SEV: Failed to unregister region (%p+%#zx)", |
| 397 | host, max_size); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | static struct RAMBlockNotifier sev_ram_notifier = { |
| 402 | .ram_block_added = sev_ram_block_added, |
| 403 | .ram_block_removed = sev_ram_block_removed, |
| 404 | }; |
| 405 | |
| 406 | static void sev_apply_cpu_context(CPUState *cpu) |
| 407 | { |
| 408 | SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); |
| 409 | X86CPU *x86; |
| 410 | CPUX86State *env; |
| 411 | struct SevLaunchVmsa *launch_vmsa; |
| 412 | |
| 413 | /* See if an initial VMSA has been provided for this CPU */ |
| 414 | QTAILQ_FOREACH(launch_vmsa, &sev_common->launch_vmsa, next) |
| 415 | { |
| 416 | if (cpu->cpu_index == launch_vmsa->cpu_index) { |
| 417 | x86 = X86_CPU(cpu); |
| 418 | env = &x86->env; |
| 419 | |
| 420 | /* |
| 421 | * Ideally we would provide the VMSA directly to kvm which would |
| 422 | * ensure that the resulting initial VMSA measurement which is |
| 423 | * calculated during KVM_SEV_LAUNCH_UPDATE_VMSA is calculated from |
| 424 | * exactly what we provide here. Currently this is not possible so |
| 425 | * we need to copy the parts of the VMSA structure that we currently |
| 426 | * support into the CPU state. |
| 427 | */ |
| 428 | cpu_load_efer(env, launch_vmsa->vmsa.efer); |
| 429 | cpu_x86_update_cr4(env, launch_vmsa->vmsa.cr4); |
| 430 | cpu_x86_update_cr0(env, launch_vmsa->vmsa.cr0); |
| 431 | cpu_x86_update_cr3(env, launch_vmsa->vmsa.cr3); |
| 432 | env->xcr0 = launch_vmsa->vmsa.xcr0; |
| 433 | env->pat = launch_vmsa->vmsa.g_pat; |
| 434 | |
| 435 | cpu_x86_load_seg_cache( |
| 436 | env, R_CS, launch_vmsa->vmsa.cs.selector, |
| 437 | launch_vmsa->vmsa.cs.base, launch_vmsa->vmsa.cs.limit, |
| 438 | FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.cs.attrib)); |
| 439 | cpu_x86_load_seg_cache( |
| 440 | env, R_DS, launch_vmsa->vmsa.ds.selector, |
| 441 | launch_vmsa->vmsa.ds.base, launch_vmsa->vmsa.ds.limit, |
| 442 | FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.ds.attrib)); |
| 443 | cpu_x86_load_seg_cache( |
| 444 | env, R_ES, launch_vmsa->vmsa.es.selector, |
| 445 | launch_vmsa->vmsa.es.base, launch_vmsa->vmsa.es.limit, |
| 446 | FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.es.attrib)); |
| 447 | cpu_x86_load_seg_cache( |
| 448 | env, R_FS, launch_vmsa->vmsa.fs.selector, |
| 449 | launch_vmsa->vmsa.fs.base, launch_vmsa->vmsa.fs.limit, |
| 450 | FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.fs.attrib)); |
| 451 | cpu_x86_load_seg_cache( |
| 452 | env, R_GS, launch_vmsa->vmsa.gs.selector, |
| 453 | launch_vmsa->vmsa.gs.base, launch_vmsa->vmsa.gs.limit, |
| 454 | FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.gs.attrib)); |
| 455 | cpu_x86_load_seg_cache( |
| 456 | env, R_SS, launch_vmsa->vmsa.ss.selector, |
| 457 | launch_vmsa->vmsa.ss.base, launch_vmsa->vmsa.ss.limit, |
| 458 | FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.ss.attrib)); |
| 459 | |
| 460 | env->gdt.base = launch_vmsa->vmsa.gdtr.base; |
| 461 | env->gdt.limit = launch_vmsa->vmsa.gdtr.limit; |
| 462 | env->gdt.flags = |
| 463 | FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.gdtr.attrib); |
| 464 | env->idt.base = launch_vmsa->vmsa.idtr.base; |
| 465 | env->idt.limit = launch_vmsa->vmsa.idtr.limit; |
| 466 | env->idt.flags = |
| 467 | FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.idtr.attrib); |
| 468 | |
| 469 | cpu_x86_load_seg_cache( |
| 470 | env, R_LDTR, launch_vmsa->vmsa.ldtr.selector, |
| 471 | launch_vmsa->vmsa.ldtr.base, launch_vmsa->vmsa.ldtr.limit, |
| 472 | FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.ldtr.attrib)); |
| 473 | cpu_x86_load_seg_cache( |
| 474 | env, R_TR, launch_vmsa->vmsa.tr.selector, |
| 475 | launch_vmsa->vmsa.ldtr.base, launch_vmsa->vmsa.tr.limit, |
| 476 | FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.tr.attrib)); |
| 477 | |
| 478 | env->dr[6] = launch_vmsa->vmsa.dr6; |
| 479 | env->dr[7] = launch_vmsa->vmsa.dr7; |
| 480 | |
| 481 | env->regs[R_EAX] = launch_vmsa->vmsa.rax; |
| 482 | env->regs[R_ECX] = launch_vmsa->vmsa.rcx; |
| 483 | env->regs[R_EDX] = launch_vmsa->vmsa.rdx; |
| 484 | env->regs[R_EBX] = launch_vmsa->vmsa.rbx; |
| 485 | env->regs[R_ESP] = launch_vmsa->vmsa.rsp; |
| 486 | env->regs[R_EBP] = launch_vmsa->vmsa.rbp; |
| 487 | env->regs[R_ESI] = launch_vmsa->vmsa.rsi; |
| 488 | env->regs[R_EDI] = launch_vmsa->vmsa.rdi; |
| 489 | #ifdef TARGET_X86_64 |
| 490 | env->regs[R_R8] = launch_vmsa->vmsa.r8; |
| 491 | env->regs[R_R9] = launch_vmsa->vmsa.r9; |
| 492 | env->regs[R_R10] = launch_vmsa->vmsa.r10; |
| 493 | env->regs[R_R11] = launch_vmsa->vmsa.r11; |
| 494 | env->regs[R_R12] = launch_vmsa->vmsa.r12; |
| 495 | env->regs[R_R13] = launch_vmsa->vmsa.r13; |
| 496 | env->regs[R_R14] = launch_vmsa->vmsa.r14; |
| 497 | env->regs[R_R15] = launch_vmsa->vmsa.r15; |
| 498 | #endif |
| 499 | env->eip = launch_vmsa->vmsa.rip; |
| 500 | env->eflags = launch_vmsa->vmsa.rflags; |
| 501 | |
| 502 | cpu_set_fpuc(env, launch_vmsa->vmsa.x87_fcw); |
| 503 | env->mxcsr = launch_vmsa->vmsa.mxcsr; |
| 504 | |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Ensure SEV_FEATURES is configured for correct SEV hardware and that |
| 512 | * the requested features are supported. In addition, ensure feature |
| 513 | * dependencies are satisfied (allow tsc-frequency only if secure-tsc |
| 514 | * is also enabled, as an example). |
| 515 | */ |
| 516 | static int check_sev_features(SevCommonState *sev_common, uint64_t sev_features, |
| 517 | Error **errp) |
| 518 | { |
| 519 | if (sev_features && !sev_es_enabled()) { |
| 520 | error_setg(errp, |
| 521 | "SEV: SEV features require either SEV-ES or SEV-SNP to be enabled"); |
| 522 | return -1; |
| 523 | } |
| 524 | |
| 525 | if (sev_features & ~sev_common->supported_sev_features) { |
| 526 | error_setg(errp, |
| 527 | "SEV: VMSA contains unsupported sev_features: %lX, " |
| 528 | "supported features: %lX", |
| 529 | sev_features, sev_common->supported_sev_features); |
| 530 | return -1; |
| 531 | } |
| 532 | |
| 533 | if (sev_snp_enabled()) { |
| 534 | if (!(sev_features & SVM_SEV_FEAT_SNP_ACTIVE)) { |
| 535 | error_setg(errp, |
| 536 | "SEV: SEV_SNP is enabled but is not enabled in VMSA sev_features"); |
| 537 | return -1; |
| 538 | } |
| 539 | if (SEV_SNP_GUEST(sev_common)->tsc_khz && |
| 540 | !(sev_features & SVM_SEV_FEAT_SECURE_TSC)) { |
| 541 | error_setg(errp, |
| 542 | "SEV: TSC frequency can only be set if Secure TSC is enabled"); |
| 543 | return -1; |
| 544 | } |
| 545 | } else { |
| 546 | if (sev_features && sev_es_enabled()) { |
| 547 | error_setg(errp, |
| 548 | "SEV: SEV features are not supported with SEV-ES at this time"); |
| 549 | return -1; |
| 550 | } |
| 551 | if (sev_features & SVM_SEV_FEAT_SNP_ACTIVE) { |
| 552 | error_setg(errp, |
| 553 | "SEV: SEV_SNP is not enabled but is enabled in VMSA sev_features"); |
| 554 | return -1; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static int check_vmsa_supported(SevCommonState *sev_common, hwaddr gpa, |
| 562 | const struct sev_es_save_area *vmsa, |
| 563 | Error **errp) |
| 564 | { |
| 565 | struct sev_es_save_area vmsa_check; |
| 566 | |
| 567 | /* |
| 568 | * KVM always populates the VMSA at a fixed GPA which cannot be modified |
| 569 | * from userspace. Specifying a different GPA will not prevent the guest |
| 570 | * from starting but will cause the launch measurement to be different |
| 571 | * from expected. Therefore check that the provided GPA matches the KVM |
| 572 | * hardcoded value. |
| 573 | */ |
| 574 | if (gpa != KVM_VMSA_GPA) { |
| 575 | error_setg(errp, |
| 576 | "SEV: The VMSA GPA must be %lX but is specified as %lX", |
| 577 | KVM_VMSA_GPA, gpa); |
| 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Clear all supported fields so we can then check the entire structure |
| 583 | * is zero. |
| 584 | */ |
| 585 | memcpy(&vmsa_check, vmsa, sizeof(struct sev_es_save_area)); |
| 586 | memset(&vmsa_check.es, 0, sizeof(vmsa_check.es)); |
| 587 | memset(&vmsa_check.cs, 0, sizeof(vmsa_check.cs)); |
| 588 | memset(&vmsa_check.ss, 0, sizeof(vmsa_check.ss)); |
| 589 | memset(&vmsa_check.ds, 0, sizeof(vmsa_check.ds)); |
| 590 | memset(&vmsa_check.fs, 0, sizeof(vmsa_check.fs)); |
| 591 | memset(&vmsa_check.gs, 0, sizeof(vmsa_check.gs)); |
| 592 | memset(&vmsa_check.gdtr, 0, sizeof(vmsa_check.gdtr)); |
| 593 | memset(&vmsa_check.idtr, 0, sizeof(vmsa_check.idtr)); |
| 594 | memset(&vmsa_check.ldtr, 0, sizeof(vmsa_check.ldtr)); |
| 595 | memset(&vmsa_check.tr, 0, sizeof(vmsa_check.tr)); |
| 596 | vmsa_check.efer = 0; |
| 597 | vmsa_check.cr0 = 0; |
| 598 | vmsa_check.cr3 = 0; |
| 599 | vmsa_check.cr4 = 0; |
| 600 | vmsa_check.xcr0 = 0; |
| 601 | vmsa_check.dr6 = 0; |
| 602 | vmsa_check.dr7 = 0; |
| 603 | vmsa_check.rax = 0; |
| 604 | vmsa_check.rcx = 0; |
| 605 | vmsa_check.rdx = 0; |
| 606 | vmsa_check.rbx = 0; |
| 607 | vmsa_check.rsp = 0; |
| 608 | vmsa_check.rbp = 0; |
| 609 | vmsa_check.rsi = 0; |
| 610 | vmsa_check.rdi = 0; |
| 611 | vmsa_check.r8 = 0; |
| 612 | vmsa_check.r9 = 0; |
| 613 | vmsa_check.r10 = 0; |
| 614 | vmsa_check.r11 = 0; |
| 615 | vmsa_check.r12 = 0; |
| 616 | vmsa_check.r13 = 0; |
| 617 | vmsa_check.r14 = 0; |
| 618 | vmsa_check.r15 = 0; |
| 619 | vmsa_check.rip = 0; |
| 620 | vmsa_check.rflags = 0; |
| 621 | |
| 622 | vmsa_check.g_pat = 0; |
| 623 | vmsa_check.xcr0 = 0; |
| 624 | |
| 625 | vmsa_check.x87_fcw = 0; |
| 626 | vmsa_check.mxcsr = 0; |
| 627 | |
| 628 | vmsa_check.sev_features = 0; |
| 629 | |
| 630 | if (!buffer_is_zero(&vmsa_check, sizeof(vmsa_check))) { |
| 631 | error_setg(errp, |
| 632 | "SEV: The VMSA contains fields that are not " |
| 633 | "synchronized with KVM. Continuing would result in " |
| 634 | "either unpredictable guest behavior, or a " |
| 635 | "mismatched launch measurement."); |
| 636 | return -1; |
| 637 | } |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | static int sev_set_cpu_context(uint16_t cpu_index, const void *ctx, |
| 642 | uint32_t ctx_len, hwaddr gpa, Error **errp) |
| 643 | { |
| 644 | SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); |
| 645 | SevLaunchVmsa *launch_vmsa; |
| 646 | CPUState *cpu; |
| 647 | bool exists = false; |
| 648 | |
| 649 | /* |
| 650 | * Setting the CPU context is only supported for SEV-ES and SEV-SNP. The |
| 651 | * context buffer will contain a sev_es_save_area from the Linux kernel |
| 652 | * which is defined by "Table B-4. VMSA Layout, State Save Area for SEV-ES" |
| 653 | * in the AMD64 APM, Volume 2. |
| 654 | */ |
| 655 | |
| 656 | if (!sev_es_enabled()) { |
| 657 | error_setg(errp, "SEV: unable to set CPU context: Not supported"); |
| 658 | return -1; |
| 659 | } |
| 660 | |
| 661 | if (ctx_len < sizeof(struct sev_es_save_area)) { |
| 662 | error_setg(errp, "SEV: unable to set CPU context: " |
| 663 | "Invalid context provided"); |
| 664 | return -1; |
| 665 | } |
| 666 | |
| 667 | cpu = qemu_get_cpu(cpu_index); |
| 668 | if (!cpu) { |
| 669 | error_setg(errp, "SEV: unable to set CPU context for out of bounds " |
| 670 | "CPU index %d", cpu_index); |
| 671 | return -1; |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * If the context of this VP has already been set then replace it with the |
| 676 | * new context. |
| 677 | */ |
| 678 | QTAILQ_FOREACH(launch_vmsa, &sev_common->launch_vmsa, next) |
| 679 | { |
| 680 | if (cpu_index == launch_vmsa->cpu_index) { |
| 681 | launch_vmsa->gpa = gpa; |
| 682 | memcpy(&launch_vmsa->vmsa, ctx, sizeof(launch_vmsa->vmsa)); |
| 683 | exists = true; |
| 684 | break; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | if (!exists) { |
| 689 | /* New VP context */ |
| 690 | launch_vmsa = g_new0(SevLaunchVmsa, 1); |
| 691 | memcpy(&launch_vmsa->vmsa, ctx, sizeof(launch_vmsa->vmsa)); |
| 692 | launch_vmsa->cpu_index = cpu_index; |
| 693 | launch_vmsa->gpa = gpa; |
| 694 | QTAILQ_INSERT_TAIL(&sev_common->launch_vmsa, launch_vmsa, next); |
| 695 | } |
| 696 | |
| 697 | /* Synchronise the VMSA with the current CPU state */ |
| 698 | sev_apply_cpu_context(cpu); |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | bool |
| 704 | sev_enabled(void) |
| 705 | { |
| 706 | ConfidentialGuestSupport *cgs = MACHINE(qdev_get_machine())->cgs; |
| 707 | |
| 708 | return !!object_dynamic_cast(OBJECT(cgs), TYPE_SEV_COMMON); |
| 709 | } |
| 710 | |
| 711 | bool |
| 712 | sev_snp_enabled(void) |
| 713 | { |
| 714 | ConfidentialGuestSupport *cgs = MACHINE(qdev_get_machine())->cgs; |
| 715 | |
| 716 | return !!object_dynamic_cast(OBJECT(cgs), TYPE_SEV_SNP_GUEST); |
| 717 | } |
| 718 | |
| 719 | bool |
| 720 | sev_es_enabled(void) |
| 721 | { |
| 722 | ConfidentialGuestSupport *cgs = MACHINE(qdev_get_machine())->cgs; |
| 723 | |
| 724 | return sev_snp_enabled() || |
| 725 | (sev_enabled() && SEV_GUEST(cgs)->policy & SEV_POLICY_ES); |
| 726 | } |
| 727 | |
| 728 | uint32_t |
| 729 | sev_get_cbit_position(void) |
| 730 | { |
| 731 | SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); |
| 732 | |
| 733 | return sev_common ? sev_common->cbitpos : 0; |
| 734 | } |
| 735 | |
| 736 | uint32_t |
| 737 | sev_get_reduced_phys_bits(void) |
| 738 | { |
| 739 | SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); |
| 740 | |
| 741 | return sev_common ? sev_common->reduced_phys_bits : 0; |
| 742 | } |
| 743 | |
| 744 | static SevInfo *sev_get_info(void) |
| 745 | { |
| 746 | SevInfo *info; |
| 747 | SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); |
| 748 | |
| 749 | info = g_new0(SevInfo, 1); |
| 750 | info->enabled = sev_enabled(); |
| 751 | |
| 752 | if (info->enabled) { |
| 753 | info->api_major = sev_common->api_major; |
| 754 | info->api_minor = sev_common->api_minor; |
| 755 | info->build_id = sev_common->build_id; |
| 756 | info->state = sev_common->state; |
| 757 | |
| 758 | if (sev_snp_enabled()) { |
| 759 | info->sev_type = SEV_GUEST_TYPE_SEV_SNP; |
| 760 | info->u.sev_snp.snp_policy = |
| 761 | object_property_get_uint(OBJECT(sev_common), "policy", NULL); |
| 762 | } else { |
| 763 | info->sev_type = SEV_GUEST_TYPE_SEV; |
| 764 | info->u.sev.handle = SEV_GUEST(sev_common)->handle; |
| 765 | info->u.sev.policy = |
| 766 | (uint32_t)object_property_get_uint(OBJECT(sev_common), |
| 767 | "policy", NULL); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | return info; |
| 772 | } |
| 773 | |
| 774 | SevInfo *qmp_query_sev(Error **errp) |
| 775 | { |
| 776 | SevInfo *info; |
| 777 | |
| 778 | info = sev_get_info(); |
| 779 | if (!info) { |
| 780 | error_setg(errp, "SEV feature is not available"); |
| 781 | return NULL; |
| 782 | } |
| 783 | |
| 784 | return info; |
| 785 | } |
| 786 | |
| 787 | #ifdef CONFIG_HMP |
| 788 | void hmp_info_sev(MonitorHMP *hmp, const QDict *qdict) |
| 789 | { |
| 790 | SevInfo *info = sev_get_info(); |
| 791 | |
| 792 | if (!info || !info->enabled) { |
| 793 | monitor_hmp_printf(hmp, "SEV is not enabled\n"); |
| 794 | goto out; |
| 795 | } |
| 796 | |
| 797 | monitor_hmp_printf(hmp, "SEV type: %s\n", SevGuestType_str(info->sev_type)); |
| 798 | monitor_hmp_printf(hmp, "state: %s\n", SevState_str(info->state)); |
| 799 | monitor_hmp_printf(hmp, "build: %d\n", info->build_id); |
| 800 | monitor_hmp_printf(hmp, "api version: %d.%d\n", info->api_major, |
| 801 | info->api_minor); |
| 802 | |
| 803 | if (sev_snp_enabled()) { |
| 804 | monitor_hmp_printf(hmp, "debug: %s\n", |
| 805 | info->u.sev_snp.snp_policy & SEV_SNP_POLICY_DBG ? "on" |
| 806 | : "off"); |
| 807 | monitor_hmp_printf(hmp, "SMT allowed: %s\n", |
| 808 | info->u.sev_snp.snp_policy & SEV_SNP_POLICY_SMT ? "on" |
| 809 | : "off"); |
| 810 | } else { |
| 811 | monitor_hmp_printf(hmp, "handle: %d\n", info->u.sev.handle); |
| 812 | monitor_hmp_printf(hmp, "debug: %s\n", |
| 813 | info->u.sev.policy & SEV_POLICY_NODBG ? "off" : "on"); |
| 814 | monitor_hmp_printf(hmp, "key-sharing: %s\n", |
| 815 | info->u.sev.policy & SEV_POLICY_NOKS ? "off" : "on"); |
| 816 | } |
| 817 | |
| 818 | out: |
| 819 | qapi_free_SevInfo(info); |
| 820 | } |
| 821 | #endif |
| 822 | |
| 823 | static int |
| 824 | sev_get_pdh_info(int fd, guchar **pdh, size_t *pdh_len, guchar **cert_chain, |
| 825 | size_t *cert_chain_len, Error **errp) |
| 826 | { |
| 827 | guchar *pdh_data = NULL; |
| 828 | guchar *cert_chain_data = NULL; |
| 829 | struct sev_user_data_pdh_cert_export export = {}; |
| 830 | int err, r; |
| 831 | |
| 832 | /* query the certificate length */ |
| 833 | r = sev_platform_ioctl(fd, SEV_PDH_CERT_EXPORT, &export, &err); |
| 834 | if (r < 0) { |
| 835 | if (err != SEV_RET_INVALID_LEN) { |
| 836 | error_setg(errp, "SEV: Failed to export PDH cert" |
| 837 | " ret=%d fw_err=%d (%s)", |
| 838 | r, err, fw_error_to_str(err)); |
| 839 | return 1; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | pdh_data = g_new(guchar, export.pdh_cert_len); |
| 844 | cert_chain_data = g_new(guchar, export.cert_chain_len); |
| 845 | export.pdh_cert_address = (unsigned long)pdh_data; |
| 846 | export.cert_chain_address = (unsigned long)cert_chain_data; |
| 847 | |
| 848 | r = sev_platform_ioctl(fd, SEV_PDH_CERT_EXPORT, &export, &err); |
| 849 | if (r < 0) { |
| 850 | error_setg(errp, "SEV: Failed to export PDH cert ret=%d fw_err=%d (%s)", |
| 851 | r, err, fw_error_to_str(err)); |
| 852 | goto e_free; |
| 853 | } |
| 854 | |
| 855 | *pdh = pdh_data; |
| 856 | *pdh_len = export.pdh_cert_len; |
| 857 | *cert_chain = cert_chain_data; |
| 858 | *cert_chain_len = export.cert_chain_len; |
| 859 | return 0; |
| 860 | |
| 861 | e_free: |
| 862 | g_free(pdh_data); |
| 863 | g_free(cert_chain_data); |
| 864 | return 1; |
| 865 | } |
| 866 | |
| 867 | static int sev_get_cpu0_id(int fd, guchar **id, size_t *id_len, Error **errp) |
| 868 | { |
| 869 | guchar *id_data; |
| 870 | struct sev_user_data_get_id2 get_id2 = {}; |
| 871 | int err, r; |
| 872 | |
| 873 | /* query the ID length */ |
| 874 | r = sev_platform_ioctl(fd, SEV_GET_ID2, &get_id2, &err); |
| 875 | if (r < 0 && err != SEV_RET_INVALID_LEN) { |
| 876 | error_setg(errp, "SEV: Failed to get ID ret=%d fw_err=%d (%s)", |
| 877 | r, err, fw_error_to_str(err)); |
| 878 | return 1; |
| 879 | } |
| 880 | |
| 881 | id_data = g_new(guchar, get_id2.length); |
| 882 | get_id2.address = (unsigned long)id_data; |
| 883 | |
| 884 | r = sev_platform_ioctl(fd, SEV_GET_ID2, &get_id2, &err); |
| 885 | if (r < 0) { |
| 886 | error_setg(errp, "SEV: Failed to get ID ret=%d fw_err=%d (%s)", |
| 887 | r, err, fw_error_to_str(err)); |
| 888 | goto err; |
| 889 | } |
| 890 | |
| 891 | *id = id_data; |
| 892 | *id_len = get_id2.length; |
| 893 | return 0; |
| 894 | |
| 895 | err: |
| 896 | g_free(id_data); |
| 897 | return 1; |
| 898 | } |
| 899 | |
| 900 | static SevCapability *sev_get_capabilities(Error **errp) |
| 901 | { |
| 902 | SevCapability *cap = NULL; |
| 903 | guchar *pdh_data = NULL; |
| 904 | guchar *cert_chain_data = NULL; |
| 905 | guchar *cpu0_id_data = NULL; |
| 906 | size_t pdh_len = 0, cert_chain_len = 0, cpu0_id_len = 0; |
| 907 | uint32_t ebx; |
| 908 | int fd; |
| 909 | SevCommonState *sev_common; |
| 910 | char *sev_device; |
| 911 | |
| 912 | if (!kvm_enabled()) { |
| 913 | error_setg(errp, "KVM not enabled"); |
| 914 | return NULL; |
| 915 | } |
| 916 | if (kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_OP, NULL) < 0) { |
| 917 | error_setg(errp, "SEV is not enabled in KVM"); |
| 918 | return NULL; |
| 919 | } |
| 920 | |
| 921 | sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); |
| 922 | if (sev_common) { |
| 923 | sev_device = object_property_get_str(OBJECT(sev_common), "sev-device", |
| 924 | &error_abort); |
| 925 | } else { |
| 926 | sev_device = g_strdup(DEFAULT_SEV_DEVICE); |
| 927 | } |
| 928 | |
| 929 | fd = open(sev_device, O_RDWR); |
| 930 | if (fd < 0) { |
| 931 | error_setg_file_open(errp, errno, sev_device); |
| 932 | g_free(sev_device); |
| 933 | return NULL; |
| 934 | } |
| 935 | g_free(sev_device); |
| 936 | |
| 937 | if (sev_get_pdh_info(fd, &pdh_data, &pdh_len, |
| 938 | &cert_chain_data, &cert_chain_len, errp)) { |
| 939 | goto out; |
| 940 | } |
| 941 | |
| 942 | if (sev_get_cpu0_id(fd, &cpu0_id_data, &cpu0_id_len, errp)) { |
| 943 | goto out; |
| 944 | } |
| 945 | |
| 946 | cap = g_new0(SevCapability, 1); |
| 947 | cap->pdh = g_base64_encode(pdh_data, pdh_len); |
| 948 | cap->cert_chain = g_base64_encode(cert_chain_data, cert_chain_len); |
| 949 | cap->cpu0_id = g_base64_encode(cpu0_id_data, cpu0_id_len); |
| 950 | |
| 951 | host_cpuid(0x8000001F, 0, NULL, &ebx, NULL, NULL); |
| 952 | cap->cbitpos = ebx & 0x3f; |
| 953 | |
| 954 | /* |
| 955 | * When SEV feature is enabled, we loose one bit in guest physical |
| 956 | * addressing. |
| 957 | */ |
| 958 | cap->reduced_phys_bits = 1; |
| 959 | |
| 960 | out: |
| 961 | g_free(cpu0_id_data); |
| 962 | g_free(pdh_data); |
| 963 | g_free(cert_chain_data); |
| 964 | close(fd); |
| 965 | return cap; |
| 966 | } |
| 967 | |
| 968 | SevCapability *qmp_query_sev_capabilities(Error **errp) |
| 969 | { |
| 970 | return sev_get_capabilities(errp); |
| 971 | } |
| 972 | |
| 973 | static OvmfSevMetadata *ovmf_sev_metadata_table; |
| 974 | |
| 975 | #define OVMF_SEV_META_DATA_GUID "dc886566-984a-4798-A75e-5585a7bf67cc" |
| 976 | typedef struct __attribute__((__packed__)) OvmfSevMetadataOffset { |
| 977 | uint32_t offset; |
| 978 | } OvmfSevMetadataOffset; |
| 979 | |
| 980 | OvmfSevMetadata *pc_system_get_ovmf_sev_metadata_ptr(void) |
| 981 | { |
| 982 | return ovmf_sev_metadata_table; |
| 983 | } |
| 984 | |
| 985 | void pc_system_parse_sev_metadata(uint8_t *flash_ptr, size_t flash_size) |
| 986 | { |
| 987 | OvmfSevMetadata *metadata; |
| 988 | OvmfSevMetadataOffset *data; |
| 989 | |
| 990 | if (!pc_system_ovmf_table_find(OVMF_SEV_META_DATA_GUID, (uint8_t **)&data, |
| 991 | NULL)) { |
| 992 | return; |
| 993 | } |
| 994 | |
| 995 | metadata = (OvmfSevMetadata *)(flash_ptr + flash_size - data->offset); |
| 996 | if (memcmp(metadata->signature, "ASEV", 4) != 0 || |
| 997 | metadata->len < sizeof(OvmfSevMetadata) || |
| 998 | metadata->len > flash_size - data->offset) { |
| 999 | return; |
| 1000 | } |
| 1001 | |
| 1002 | ovmf_sev_metadata_table = g_memdup2(metadata, metadata->len); |
| 1003 | } |
| 1004 | |
| 1005 | static SevAttestationReport *sev_get_attestation_report(const char *mnonce, |
| 1006 | Error **errp) |
| 1007 | { |
| 1008 | struct kvm_sev_attestation_report input = {}; |
| 1009 | SevAttestationReport *report = NULL; |
| 1010 | SevCommonState *sev_common; |
| 1011 | g_autofree guchar *data = NULL; |
| 1012 | g_autofree guchar *buf = NULL; |
| 1013 | gsize len; |
| 1014 | int err = 0, ret; |
| 1015 | |
| 1016 | if (!sev_enabled()) { |
| 1017 | error_setg(errp, "SEV is not enabled"); |
| 1018 | return NULL; |
| 1019 | } |
| 1020 | |
| 1021 | /* lets decode the mnonce string */ |
| 1022 | buf = g_base64_decode(mnonce, &len); |
| 1023 | if (!buf) { |
| 1024 | error_setg(errp, "SEV: failed to decode mnonce input"); |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | |
| 1028 | /* verify the input mnonce length */ |
| 1029 | if (len != sizeof(input.mnonce)) { |
| 1030 | error_setg(errp, "SEV: mnonce must be %zu bytes (got %" G_GSIZE_FORMAT ")", |
| 1031 | sizeof(input.mnonce), len); |
| 1032 | return NULL; |
| 1033 | } |
| 1034 | |
| 1035 | sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); |
| 1036 | |
| 1037 | /* Query the report length */ |
| 1038 | ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_GET_ATTESTATION_REPORT, |
| 1039 | &input, &err); |
| 1040 | if (ret < 0) { |
| 1041 | if (err != SEV_RET_INVALID_LEN) { |
| 1042 | error_setg(errp, "SEV: Failed to query the attestation report" |
| 1043 | " length ret=%d fw_err=%d (%s)", |
| 1044 | ret, err, fw_error_to_str(err)); |
| 1045 | return NULL; |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | data = g_malloc(input.len); |
| 1050 | input.uaddr = (unsigned long)data; |
| 1051 | memcpy(input.mnonce, buf, sizeof(input.mnonce)); |
| 1052 | |
| 1053 | /* Query the report */ |
| 1054 | ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_GET_ATTESTATION_REPORT, |
| 1055 | &input, &err); |
| 1056 | if (ret) { |
| 1057 | error_setg_errno(errp, errno, "SEV: Failed to get attestation report" |
| 1058 | " ret=%d fw_err=%d (%s)", ret, err, fw_error_to_str(err)); |
| 1059 | return NULL; |
| 1060 | } |
| 1061 | |
| 1062 | report = g_new0(SevAttestationReport, 1); |
| 1063 | report->data = g_base64_encode(data, input.len); |
| 1064 | |
| 1065 | trace_kvm_sev_attestation_report(mnonce, report->data); |
| 1066 | |
| 1067 | return report; |
| 1068 | } |
| 1069 | |
| 1070 | SevAttestationReport *qmp_query_sev_attestation_report(const char *mnonce, |
| 1071 | Error **errp) |
| 1072 | { |
| 1073 | return sev_get_attestation_report(mnonce, errp); |
| 1074 | } |
| 1075 | |
| 1076 | static int |
| 1077 | sev_read_file_base64(const char *filename, guchar **data, gsize *len) |
| 1078 | { |
| 1079 | gsize sz; |
| 1080 | g_autofree gchar *base64 = NULL; |
| 1081 | GError *error = NULL; |
| 1082 | |
| 1083 | if (!g_file_get_contents(filename, &base64, &sz, &error)) { |
| 1084 | error_report("SEV: Failed to read '%s' (%s)", filename, error->message); |
| 1085 | g_error_free(error); |
| 1086 | return -1; |
| 1087 | } |
| 1088 | |
| 1089 | *data = g_base64_decode(base64, len); |
| 1090 | return 0; |
| 1091 | } |
| 1092 | |
| 1093 | static int |
| 1094 | sev_snp_launch_start(SevCommonState *sev_common) |
| 1095 | { |
| 1096 | int fw_error, rc; |
| 1097 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(sev_common); |
| 1098 | struct kvm_sev_snp_launch_start *start = &sev_snp_guest->kvm_start_conf; |
| 1099 | |
| 1100 | trace_kvm_sev_snp_launch_start(start->policy, |
| 1101 | sev_snp_guest->guest_visible_workarounds); |
| 1102 | |
| 1103 | if (!kvm_enable_hypercall(BIT_ULL(KVM_HC_MAP_GPA_RANGE))) { |
| 1104 | return 1; |
| 1105 | } |
| 1106 | |
| 1107 | if (is_sev_feature_set(sev_common, SVM_SEV_FEAT_SECURE_TSC) && |
| 1108 | sev_snp_guest->tsc_khz) { |
| 1109 | rc = -EINVAL; |
| 1110 | if (kvm_check_extension(kvm_state, KVM_CAP_VM_TSC_CONTROL)) { |
| 1111 | rc = kvm_vm_ioctl(kvm_state, KVM_SET_TSC_KHZ, sev_snp_guest->tsc_khz); |
| 1112 | } |
| 1113 | if (rc < 0) { |
| 1114 | error_report("SEV: Unable to set Secure TSC frequency to %u kHz ret=%d", |
| 1115 | sev_snp_guest->tsc_khz, rc); |
| 1116 | return 1; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | rc = sev_ioctl(sev_common->sev_fd, KVM_SEV_SNP_LAUNCH_START, |
| 1121 | start, &fw_error); |
| 1122 | if (rc < 0) { |
| 1123 | error_report("SEV: SNP_LAUNCH_START ret=%d fw_error=%d '%s'", |
| 1124 | rc, fw_error, fw_error_to_str(fw_error)); |
| 1125 | return 1; |
| 1126 | } |
| 1127 | |
| 1128 | QTAILQ_INIT(&launch_update); |
| 1129 | |
| 1130 | sev_set_guest_state(sev_common, SEV_STATE_LAUNCH_UPDATE); |
| 1131 | |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | static int |
| 1136 | sev_launch_start(SevCommonState *sev_common) |
| 1137 | { |
| 1138 | gsize sz; |
| 1139 | int ret = 1; |
| 1140 | int fw_error, rc; |
| 1141 | SevGuestState *sev_guest = SEV_GUEST(sev_common); |
| 1142 | struct kvm_sev_launch_start start = { |
| 1143 | .handle = sev_guest->handle, .policy = sev_guest->policy |
| 1144 | }; |
| 1145 | guchar *session = NULL, *dh_cert = NULL; |
| 1146 | |
| 1147 | if (sev_guest->session_file) { |
| 1148 | if (sev_read_file_base64(sev_guest->session_file, &session, &sz) < 0) { |
| 1149 | goto out; |
| 1150 | } |
| 1151 | start.session_uaddr = (unsigned long)session; |
| 1152 | start.session_len = sz; |
| 1153 | } |
| 1154 | |
| 1155 | if (sev_guest->dh_cert_file) { |
| 1156 | if (sev_read_file_base64(sev_guest->dh_cert_file, &dh_cert, &sz) < 0) { |
| 1157 | goto out; |
| 1158 | } |
| 1159 | start.dh_uaddr = (unsigned long)dh_cert; |
| 1160 | start.dh_len = sz; |
| 1161 | } |
| 1162 | |
| 1163 | trace_kvm_sev_launch_start(start.policy, session, dh_cert); |
| 1164 | rc = sev_ioctl(sev_common->sev_fd, KVM_SEV_LAUNCH_START, &start, &fw_error); |
| 1165 | if (rc < 0) { |
| 1166 | error_report("SEV: LAUNCH_START ret=%d fw_error=%d '%s'", |
| 1167 | ret, fw_error, fw_error_to_str(fw_error)); |
| 1168 | goto out; |
| 1169 | } |
| 1170 | |
| 1171 | sev_set_guest_state(sev_common, SEV_STATE_LAUNCH_UPDATE); |
| 1172 | sev_guest->handle = start.handle; |
| 1173 | ret = 0; |
| 1174 | |
| 1175 | out: |
| 1176 | g_free(session); |
| 1177 | g_free(dh_cert); |
| 1178 | return ret; |
| 1179 | } |
| 1180 | |
| 1181 | static void |
| 1182 | sev_snp_cpuid_report_mismatches(SnpCpuidInfo *old, |
| 1183 | SnpCpuidInfo *new) |
| 1184 | { |
| 1185 | size_t i; |
| 1186 | |
| 1187 | if (old->count != new->count) { |
| 1188 | error_report("SEV-SNP: CPUID validation failed due to count mismatch, " |
| 1189 | "provided: %d, expected: %d", old->count, new->count); |
| 1190 | return; |
| 1191 | } |
| 1192 | |
| 1193 | for (i = 0; i < old->count; i++) { |
| 1194 | SnpCpuidFunc *old_func, *new_func; |
| 1195 | |
| 1196 | old_func = &old->entries[i]; |
| 1197 | new_func = &new->entries[i]; |
| 1198 | |
| 1199 | if (memcmp(old_func, new_func, sizeof(SnpCpuidFunc))) { |
| 1200 | error_report("SEV-SNP: CPUID validation failed for function 0x%x, index: 0x%x, " |
| 1201 | "provided: eax:0x%08x, ebx: 0x%08x, ecx: 0x%08x, edx: 0x%08x, " |
| 1202 | "expected: eax:0x%08x, ebx: 0x%08x, ecx: 0x%08x, edx: 0x%08x", |
| 1203 | old_func->eax_in, old_func->ecx_in, |
| 1204 | old_func->eax, old_func->ebx, old_func->ecx, old_func->edx, |
| 1205 | new_func->eax, new_func->ebx, new_func->ecx, new_func->edx); |
| 1206 | } |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | static const char * |
| 1211 | snp_page_type_to_str(int type) |
| 1212 | { |
| 1213 | switch (type) { |
| 1214 | case KVM_SEV_SNP_PAGE_TYPE_NORMAL: return "Normal"; |
| 1215 | case KVM_SEV_SNP_PAGE_TYPE_ZERO: return "Zero"; |
| 1216 | case KVM_SEV_SNP_PAGE_TYPE_UNMEASURED: return "Unmeasured"; |
| 1217 | case KVM_SEV_SNP_PAGE_TYPE_SECRETS: return "Secrets"; |
| 1218 | case KVM_SEV_SNP_PAGE_TYPE_CPUID: return "Cpuid"; |
| 1219 | default: return "unknown"; |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | static int |
| 1224 | sev_snp_launch_update(SevSnpGuestState *sev_snp_guest, |
| 1225 | SevLaunchUpdateData *data) |
| 1226 | { |
| 1227 | int ret, fw_error; |
| 1228 | SnpCpuidInfo snp_cpuid_info; |
| 1229 | struct kvm_sev_snp_launch_update update = {0}; |
| 1230 | |
| 1231 | if (!data->hva || !data->len) { |
| 1232 | error_report("SNP_LAUNCH_UPDATE called with invalid address" |
| 1233 | "/ length: %p / %zx", |
| 1234 | data->hva, data->len); |
| 1235 | return 1; |
| 1236 | } |
| 1237 | |
| 1238 | if (data->type == KVM_SEV_SNP_PAGE_TYPE_CPUID) { |
| 1239 | /* Save a copy for comparison in case the LAUNCH_UPDATE fails */ |
| 1240 | memcpy(&snp_cpuid_info, data->hva, sizeof(snp_cpuid_info)); |
| 1241 | } |
| 1242 | |
| 1243 | update.uaddr = (__u64)(unsigned long)data->hva; |
| 1244 | update.gfn_start = data->gpa >> TARGET_PAGE_BITS; |
| 1245 | update.len = data->len; |
| 1246 | update.type = data->type; |
| 1247 | |
| 1248 | /* |
| 1249 | * KVM_SEV_SNP_LAUNCH_UPDATE requires that GPA ranges have the private |
| 1250 | * memory attribute set in advance. |
| 1251 | */ |
| 1252 | ret = kvm_set_memory_attributes_private(data->gpa, data->len); |
| 1253 | if (ret) { |
| 1254 | error_report("SEV-SNP: failed to configure initial" |
| 1255 | "private guest memory"); |
| 1256 | goto out; |
| 1257 | } |
| 1258 | |
| 1259 | while (update.len || ret == -EAGAIN) { |
| 1260 | trace_kvm_sev_snp_launch_update(update.uaddr, update.gfn_start << |
| 1261 | TARGET_PAGE_BITS, update.len, |
| 1262 | snp_page_type_to_str(update.type)); |
| 1263 | |
| 1264 | ret = sev_ioctl(SEV_COMMON(sev_snp_guest)->sev_fd, |
| 1265 | KVM_SEV_SNP_LAUNCH_UPDATE, |
| 1266 | &update, &fw_error); |
| 1267 | if (ret && ret != -EAGAIN) { |
| 1268 | error_report("SNP_LAUNCH_UPDATE ret=%d fw_error=%d '%s'", |
| 1269 | ret, fw_error, fw_error_to_str(fw_error)); |
| 1270 | |
| 1271 | if (data->type == KVM_SEV_SNP_PAGE_TYPE_CPUID) { |
| 1272 | sev_snp_cpuid_report_mismatches(&snp_cpuid_info, data->hva); |
| 1273 | error_report("SEV-SNP: failed update CPUID page"); |
| 1274 | } |
| 1275 | break; |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | out: |
| 1280 | if (!ret && update.gfn_start << TARGET_PAGE_BITS != data->gpa + data->len) { |
| 1281 | error_report("SEV-SNP: expected update of GPA range %" |
| 1282 | HWADDR_PRIx "-%" HWADDR_PRIx "," |
| 1283 | "got GPA range %" HWADDR_PRIx "-%llx", |
| 1284 | data->gpa, data->gpa + data->len, data->gpa, |
| 1285 | update.gfn_start << TARGET_PAGE_BITS); |
| 1286 | ret = -EIO; |
| 1287 | } |
| 1288 | |
| 1289 | return ret; |
| 1290 | } |
| 1291 | |
| 1292 | static uint32_t |
| 1293 | sev_snp_adjust_cpuid_features(X86ConfidentialGuest *cg, uint32_t feature, uint32_t index, |
| 1294 | int reg, uint32_t value) |
| 1295 | { |
| 1296 | switch (feature) { |
| 1297 | case 1: |
| 1298 | if (reg == R_ECX) { |
| 1299 | return value & ~CPUID_EXT_TSC_DEADLINE_TIMER; |
| 1300 | } |
| 1301 | break; |
| 1302 | case 7: |
| 1303 | if (index == 0 && reg == R_EBX) { |
| 1304 | return value & ~CPUID_7_0_EBX_TSC_ADJUST; |
| 1305 | } |
| 1306 | if (index == 0 && reg == R_EDX) { |
| 1307 | return value & ~(CPUID_7_0_EDX_SPEC_CTRL | |
| 1308 | CPUID_7_0_EDX_STIBP | |
| 1309 | CPUID_7_0_EDX_FLUSH_L1D | |
| 1310 | CPUID_7_0_EDX_ARCH_CAPABILITIES | |
| 1311 | CPUID_7_0_EDX_CORE_CAPABILITY | |
| 1312 | CPUID_7_0_EDX_SPEC_CTRL_SSBD); |
| 1313 | } |
| 1314 | break; |
| 1315 | case 0x80000008: |
| 1316 | if (reg == R_EBX) { |
| 1317 | return value & ~CPUID_8000_0008_EBX_VIRT_SSBD; |
| 1318 | } |
| 1319 | break; |
| 1320 | } |
| 1321 | return value; |
| 1322 | } |
| 1323 | |
| 1324 | static int sev_launch_update_data(SevCommonState *sev_common, hwaddr gpa, |
| 1325 | uint8_t *addr, size_t len, Error **errp) |
| 1326 | { |
| 1327 | int ret, fw_error; |
| 1328 | struct kvm_sev_launch_update_data update; |
| 1329 | |
| 1330 | if (!addr || !len) { |
| 1331 | return 1; |
| 1332 | } |
| 1333 | |
| 1334 | update.uaddr = (uintptr_t)addr; |
| 1335 | update.len = len; |
| 1336 | trace_kvm_sev_launch_update_data(addr, len); |
| 1337 | ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_LAUNCH_UPDATE_DATA, |
| 1338 | &update, &fw_error); |
| 1339 | if (ret) { |
| 1340 | error_setg(errp, "SEV: LAUNCH_UPDATE ret=%d fw_error=%d '%s'", |
| 1341 | ret, fw_error, fw_error_to_str(fw_error)); |
| 1342 | } |
| 1343 | |
| 1344 | return ret; |
| 1345 | } |
| 1346 | |
| 1347 | static int |
| 1348 | sev_launch_update_vmsa(SevGuestState *sev_guest) |
| 1349 | { |
| 1350 | int ret, fw_error; |
| 1351 | CPUState *cpu; |
| 1352 | |
| 1353 | /* |
| 1354 | * The initial CPU state is measured as part of KVM_SEV_LAUNCH_UPDATE_VMSA. |
| 1355 | * Synchronise the CPU state to any provided launch VMSA structures. |
| 1356 | */ |
| 1357 | CPU_FOREACH(cpu) { |
| 1358 | sev_apply_cpu_context(cpu); |
| 1359 | } |
| 1360 | |
| 1361 | |
| 1362 | ret = sev_ioctl(SEV_COMMON(sev_guest)->sev_fd, KVM_SEV_LAUNCH_UPDATE_VMSA, |
| 1363 | NULL, &fw_error); |
| 1364 | if (ret) { |
| 1365 | error_report("SEV: LAUNCH_UPDATE_VMSA ret=%d fw_error=%d '%s'", |
| 1366 | ret, fw_error, fw_error_to_str(fw_error)); |
| 1367 | } |
| 1368 | |
| 1369 | return ret; |
| 1370 | } |
| 1371 | |
| 1372 | static void |
| 1373 | sev_launch_get_measure(Notifier *notifier, void *unused) |
| 1374 | { |
| 1375 | SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); |
| 1376 | SevGuestState *sev_guest = SEV_GUEST(sev_common); |
| 1377 | int ret, error; |
| 1378 | g_autofree guchar *data = NULL; |
| 1379 | struct kvm_sev_launch_measure measurement = {}; |
| 1380 | |
| 1381 | if (!sev_check_state(sev_common, SEV_STATE_LAUNCH_UPDATE)) { |
| 1382 | return; |
| 1383 | } |
| 1384 | |
| 1385 | if (sev_es_enabled()) { |
| 1386 | /* measure all the VM save areas before getting launch_measure */ |
| 1387 | ret = sev_launch_update_vmsa(sev_guest); |
| 1388 | if (ret) { |
| 1389 | exit(1); |
| 1390 | } |
| 1391 | kvm_mark_guest_state_protected(); |
| 1392 | } |
| 1393 | |
| 1394 | /* query the measurement blob length */ |
| 1395 | ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_LAUNCH_MEASURE, |
| 1396 | &measurement, &error); |
| 1397 | if (!measurement.len) { |
| 1398 | error_report("SEV: LAUNCH_MEASURE ret=%d fw_error=%d '%s'", |
| 1399 | ret, error, fw_error_to_str(errno)); |
| 1400 | return; |
| 1401 | } |
| 1402 | |
| 1403 | data = g_new0(guchar, measurement.len); |
| 1404 | measurement.uaddr = (unsigned long)data; |
| 1405 | |
| 1406 | /* get the measurement blob */ |
| 1407 | ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_LAUNCH_MEASURE, |
| 1408 | &measurement, &error); |
| 1409 | if (ret) { |
| 1410 | error_report("SEV: LAUNCH_MEASURE ret=%d fw_error=%d '%s'", |
| 1411 | ret, error, fw_error_to_str(errno)); |
| 1412 | return; |
| 1413 | } |
| 1414 | |
| 1415 | sev_set_guest_state(sev_common, SEV_STATE_LAUNCH_SECRET); |
| 1416 | |
| 1417 | /* encode the measurement value and emit the event */ |
| 1418 | sev_guest->measurement = g_base64_encode(data, measurement.len); |
| 1419 | trace_kvm_sev_launch_measurement(sev_guest->measurement); |
| 1420 | } |
| 1421 | |
| 1422 | static char *sev_get_launch_measurement(void) |
| 1423 | { |
| 1424 | ConfidentialGuestSupport *cgs = MACHINE(qdev_get_machine())->cgs; |
| 1425 | SevGuestState *sev_guest = |
| 1426 | (SevGuestState *)object_dynamic_cast(OBJECT(cgs), TYPE_SEV_GUEST); |
| 1427 | |
| 1428 | if (sev_guest && |
| 1429 | SEV_COMMON(sev_guest)->state >= SEV_STATE_LAUNCH_SECRET) { |
| 1430 | return g_strdup(sev_guest->measurement); |
| 1431 | } |
| 1432 | |
| 1433 | return NULL; |
| 1434 | } |
| 1435 | |
| 1436 | SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp) |
| 1437 | { |
| 1438 | char *data; |
| 1439 | SevLaunchMeasureInfo *info; |
| 1440 | |
| 1441 | data = sev_get_launch_measurement(); |
| 1442 | if (!data) { |
| 1443 | error_setg(errp, "SEV launch measurement is not available"); |
| 1444 | return NULL; |
| 1445 | } |
| 1446 | |
| 1447 | info = g_malloc0(sizeof(*info)); |
| 1448 | info->data = data; |
| 1449 | |
| 1450 | return info; |
| 1451 | } |
| 1452 | |
| 1453 | static Notifier sev_machine_done_notify = { |
| 1454 | .notify = sev_launch_get_measure, |
| 1455 | }; |
| 1456 | |
| 1457 | static void |
| 1458 | sev_launch_finish(SevCommonState *sev_common) |
| 1459 | { |
| 1460 | int ret, error; |
| 1461 | |
| 1462 | trace_kvm_sev_launch_finish(); |
| 1463 | ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_LAUNCH_FINISH, 0, |
| 1464 | &error); |
| 1465 | if (ret) { |
| 1466 | error_report("SEV: LAUNCH_FINISH ret=%d fw_error=%d '%s'", |
| 1467 | ret, error, fw_error_to_str(error)); |
| 1468 | exit(1); |
| 1469 | } |
| 1470 | |
| 1471 | sev_set_guest_state(sev_common, SEV_STATE_RUNNING); |
| 1472 | } |
| 1473 | |
| 1474 | static int snp_launch_update_data(uint64_t gpa, void *hva, size_t len, |
| 1475 | int type, Error **errp) |
| 1476 | { |
| 1477 | SevLaunchUpdateData *data; |
| 1478 | |
| 1479 | data = g_new0(SevLaunchUpdateData, 1); |
| 1480 | data->gpa = gpa; |
| 1481 | data->hva = hva; |
| 1482 | data->len = len; |
| 1483 | data->type = type; |
| 1484 | |
| 1485 | QTAILQ_INSERT_TAIL(&launch_update, data, next); |
| 1486 | |
| 1487 | return 0; |
| 1488 | } |
| 1489 | |
| 1490 | static int sev_snp_launch_update_data(SevCommonState *sev_common, hwaddr gpa, |
| 1491 | uint8_t *ptr, size_t len, Error **errp) |
| 1492 | { |
| 1493 | return snp_launch_update_data(gpa, ptr, len, |
| 1494 | KVM_SEV_SNP_PAGE_TYPE_NORMAL, errp); |
| 1495 | } |
| 1496 | |
| 1497 | static int |
| 1498 | sev_snp_cpuid_info_fill(SnpCpuidInfo *snp_cpuid_info, |
| 1499 | const KvmCpuidInfo *kvm_cpuid_info, Error **errp) |
| 1500 | { |
| 1501 | size_t i; |
| 1502 | |
| 1503 | if (kvm_cpuid_info->cpuid.nent > SNP_CPUID_FUNCTION_MAXCOUNT) { |
| 1504 | error_setg(errp, "SEV-SNP: CPUID entry count (%d) exceeds max (%d)", |
| 1505 | kvm_cpuid_info->cpuid.nent, SNP_CPUID_FUNCTION_MAXCOUNT); |
| 1506 | return -1; |
| 1507 | } |
| 1508 | |
| 1509 | memset(snp_cpuid_info, 0, sizeof(*snp_cpuid_info)); |
| 1510 | |
| 1511 | for (i = 0; i < kvm_cpuid_info->cpuid.nent; i++) { |
| 1512 | const struct kvm_cpuid_entry2 *kvm_cpuid_entry; |
| 1513 | SnpCpuidFunc *snp_cpuid_entry; |
| 1514 | |
| 1515 | kvm_cpuid_entry = &kvm_cpuid_info->entries[i]; |
| 1516 | snp_cpuid_entry = &snp_cpuid_info->entries[i]; |
| 1517 | |
| 1518 | snp_cpuid_entry->eax_in = kvm_cpuid_entry->function; |
| 1519 | if (kvm_cpuid_entry->flags == KVM_CPUID_FLAG_SIGNIFCANT_INDEX) { |
| 1520 | snp_cpuid_entry->ecx_in = kvm_cpuid_entry->index; |
| 1521 | } |
| 1522 | snp_cpuid_entry->eax = kvm_cpuid_entry->eax; |
| 1523 | snp_cpuid_entry->ebx = kvm_cpuid_entry->ebx; |
| 1524 | snp_cpuid_entry->ecx = kvm_cpuid_entry->ecx; |
| 1525 | snp_cpuid_entry->edx = kvm_cpuid_entry->edx; |
| 1526 | |
| 1527 | /* |
| 1528 | * Guest kernels will calculate EBX themselves using the 0xD |
| 1529 | * subfunctions corresponding to the individual XSAVE areas, so only |
| 1530 | * encode the base XSAVE size in the initial leaves, corresponding |
| 1531 | * to the initial XCR0=1 state. |
| 1532 | */ |
| 1533 | if (snp_cpuid_entry->eax_in == 0xD && |
| 1534 | (snp_cpuid_entry->ecx_in == 0x0 || snp_cpuid_entry->ecx_in == 0x1)) { |
| 1535 | snp_cpuid_entry->ebx = 0x240; |
| 1536 | snp_cpuid_entry->xcr0_in = 1; |
| 1537 | snp_cpuid_entry->xss_in = 0; |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | snp_cpuid_info->count = i; |
| 1542 | |
| 1543 | return 0; |
| 1544 | } |
| 1545 | |
| 1546 | static int snp_launch_update_cpuid(uint32_t cpuid_addr, void *hva, |
| 1547 | size_t cpuid_len, Error **errp) |
| 1548 | { |
| 1549 | KvmCpuidInfo kvm_cpuid_info = {0}; |
| 1550 | SnpCpuidInfo snp_cpuid_info; |
| 1551 | CPUState *cs = first_cpu; |
| 1552 | int ret; |
| 1553 | uint32_t i = 0; |
| 1554 | |
| 1555 | assert(sizeof(snp_cpuid_info) <= cpuid_len); |
| 1556 | |
| 1557 | /* get the cpuid list from KVM */ |
| 1558 | do { |
| 1559 | kvm_cpuid_info.cpuid.nent = ++i; |
| 1560 | ret = kvm_vcpu_ioctl(cs, KVM_GET_CPUID2, &kvm_cpuid_info); |
| 1561 | } while (ret == -E2BIG); |
| 1562 | |
| 1563 | if (ret) { |
| 1564 | error_setg(errp, "SEV-SNP: unable to query CPUID values for CPU: '%s'", |
| 1565 | strerror(-ret)); |
| 1566 | return -1; |
| 1567 | } |
| 1568 | |
| 1569 | ret = sev_snp_cpuid_info_fill(&snp_cpuid_info, &kvm_cpuid_info, errp); |
| 1570 | if (ret < 0) { |
| 1571 | return -1; |
| 1572 | } |
| 1573 | |
| 1574 | memcpy(hva, &snp_cpuid_info, sizeof(snp_cpuid_info)); |
| 1575 | |
| 1576 | return snp_launch_update_data(cpuid_addr, hva, cpuid_len, |
| 1577 | KVM_SEV_SNP_PAGE_TYPE_CPUID, errp); |
| 1578 | } |
| 1579 | |
| 1580 | static int snp_launch_update_kernel_hashes(SevSnpGuestState *sev_snp, |
| 1581 | uint32_t addr, void *hva, |
| 1582 | uint32_t len, Error **errp) |
| 1583 | { |
| 1584 | int type = KVM_SEV_SNP_PAGE_TYPE_ZERO; |
| 1585 | if (sev_snp->parent_obj.kernel_hashes) { |
| 1586 | assert(sev_snp->kernel_hashes_data); |
| 1587 | assert((sev_snp->kernel_hashes_offset + |
| 1588 | sizeof(*sev_snp->kernel_hashes_data)) <= len); |
| 1589 | memset(hva, 0, len); |
| 1590 | memcpy(hva + sev_snp->kernel_hashes_offset, sev_snp->kernel_hashes_data, |
| 1591 | sizeof(*sev_snp->kernel_hashes_data)); |
| 1592 | type = KVM_SEV_SNP_PAGE_TYPE_NORMAL; |
| 1593 | } |
| 1594 | return snp_launch_update_data(addr, hva, len, type, errp); |
| 1595 | } |
| 1596 | |
| 1597 | static int |
| 1598 | snp_metadata_desc_to_page_type(int desc_type) |
| 1599 | { |
| 1600 | switch (desc_type) { |
| 1601 | /* Add the umeasured prevalidated pages as a zero page */ |
| 1602 | case SEV_DESC_TYPE_SNP_SEC_MEM: return KVM_SEV_SNP_PAGE_TYPE_ZERO; |
| 1603 | case SEV_DESC_TYPE_SNP_SECRETS: return KVM_SEV_SNP_PAGE_TYPE_SECRETS; |
| 1604 | case SEV_DESC_TYPE_CPUID: return KVM_SEV_SNP_PAGE_TYPE_CPUID; |
| 1605 | default: |
| 1606 | return KVM_SEV_SNP_PAGE_TYPE_ZERO; |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | static void |
| 1611 | snp_populate_metadata_pages(SevSnpGuestState *sev_snp, |
| 1612 | OvmfSevMetadata *metadata) |
| 1613 | { |
| 1614 | OvmfSevMetadataDesc *desc; |
| 1615 | int type, ret, i; |
| 1616 | void *hva; |
| 1617 | g_autoptr(MemoryRegion) mr = NULL; |
| 1618 | |
| 1619 | for (i = 0; i < metadata->num_desc; i++) { |
| 1620 | desc = &metadata->descs[i]; |
| 1621 | |
| 1622 | type = snp_metadata_desc_to_page_type(desc->type); |
| 1623 | |
| 1624 | hva = gpa2hva(&mr, desc->base, desc->len, NULL); |
| 1625 | if (!hva) { |
| 1626 | error_report("SEV: Failed to get HVA for GPA 0x%x sz 0x%x", |
| 1627 | desc->base, desc->len); |
| 1628 | exit(1); |
| 1629 | } |
| 1630 | |
| 1631 | if (type == KVM_SEV_SNP_PAGE_TYPE_CPUID) { |
| 1632 | ret = snp_launch_update_cpuid(desc->base, hva, desc->len, |
| 1633 | &error_fatal); |
| 1634 | } else if (desc->type == SEV_DESC_TYPE_SNP_KERNEL_HASHES) { |
| 1635 | ret = snp_launch_update_kernel_hashes(sev_snp, desc->base, hva, |
| 1636 | desc->len, &error_fatal); |
| 1637 | } else { |
| 1638 | ret = snp_launch_update_data(desc->base, hva, desc->len, type, |
| 1639 | &error_fatal); |
| 1640 | } |
| 1641 | |
| 1642 | if (ret) { |
| 1643 | error_report("SEV: Failed to add metadata page gpa 0x%x+%x type %d", |
| 1644 | desc->base, desc->len, desc->type); |
| 1645 | exit(1); |
| 1646 | } |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | static void |
| 1651 | sev_snp_launch_finish(SevCommonState *sev_common) |
| 1652 | { |
| 1653 | int ret, error; |
| 1654 | OvmfSevMetadata *metadata; |
| 1655 | SevLaunchUpdateData *data; |
| 1656 | SevSnpGuestState *sev_snp = SEV_SNP_GUEST(sev_common); |
| 1657 | struct kvm_sev_snp_launch_finish *finish = &sev_snp->kvm_finish_conf; |
| 1658 | |
| 1659 | /* |
| 1660 | * Populate all the metadata pages if not using an IGVM file. In the case |
| 1661 | * where an IGVM file is provided it will be used to configure the metadata |
| 1662 | * pages directly. |
| 1663 | */ |
| 1664 | if (!X86_MACHINE(qdev_get_machine())->igvm) { |
| 1665 | /* |
| 1666 | * To boot the SNP guest, the hypervisor is required to populate the |
| 1667 | * CPUID and Secrets page before finalizing the launch flow. The |
| 1668 | * location of the secrets and CPUID page is available through the |
| 1669 | * OVMF metadata GUID. |
| 1670 | */ |
| 1671 | metadata = pc_system_get_ovmf_sev_metadata_ptr(); |
| 1672 | if (metadata == NULL) { |
| 1673 | error_report("SEV: SNP_LAUNCH_FINISH failed to locate SEV metadata header"); |
| 1674 | exit(1); |
| 1675 | } |
| 1676 | |
| 1677 | /* Populate all the metadata pages */ |
| 1678 | snp_populate_metadata_pages(sev_snp, metadata); |
| 1679 | } |
| 1680 | |
| 1681 | QTAILQ_FOREACH(data, &launch_update, next) { |
| 1682 | ret = sev_snp_launch_update(sev_snp, data); |
| 1683 | if (ret) { |
| 1684 | exit(1); |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | trace_kvm_sev_snp_launch_finish(sev_snp->id_block_base64, sev_snp->id_auth_base64, |
| 1689 | sev_snp->host_data); |
| 1690 | ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_SNP_LAUNCH_FINISH, |
| 1691 | finish, &error); |
| 1692 | if (ret) { |
| 1693 | error_report("SNP_LAUNCH_FINISH ret=%d fw_error=%d '%s'", |
| 1694 | ret, error, fw_error_to_str(error)); |
| 1695 | exit(1); |
| 1696 | } |
| 1697 | |
| 1698 | kvm_mark_guest_state_protected(); |
| 1699 | sev_set_guest_state(sev_common, SEV_STATE_RUNNING); |
| 1700 | } |
| 1701 | |
| 1702 | |
| 1703 | static void |
| 1704 | sev_vm_state_change(void *opaque, bool running, RunState state) |
| 1705 | { |
| 1706 | SevCommonState *sev_common = opaque; |
| 1707 | SevCommonStateClass *klass = SEV_COMMON_GET_CLASS(opaque); |
| 1708 | |
| 1709 | if (running) { |
| 1710 | if (!sev_check_state(sev_common, SEV_STATE_RUNNING)) { |
| 1711 | klass->launch_finish(sev_common); |
| 1712 | |
| 1713 | /* add migration blocker */ |
| 1714 | error_setg(&sev_mig_blocker, |
| 1715 | "SEV: Migration is not implemented"); |
| 1716 | migrate_add_blocker(&sev_mig_blocker, &error_fatal); |
| 1717 | /* |
| 1718 | * mark SEV guest as resettable so that we can reinitialize |
| 1719 | * SEV upon reset. |
| 1720 | */ |
| 1721 | qemu_register_resettable(OBJECT(sev_common)); |
| 1722 | } |
| 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | /* |
| 1727 | * This helper is to examine sev-guest properties and determine if any options |
| 1728 | * have been set which rely on the newer KVM_SEV_INIT2 interface and associated |
| 1729 | * KVM VM types. |
| 1730 | */ |
| 1731 | static bool sev_init2_required(SevGuestState *sev_guest) |
| 1732 | { |
| 1733 | return !!SEV_COMMON(sev_guest)->sev_features; |
| 1734 | } |
| 1735 | |
| 1736 | static int sev_kvm_type(X86ConfidentialGuest *cg) |
| 1737 | { |
| 1738 | SevCommonState *sev_common = SEV_COMMON(cg); |
| 1739 | SevGuestState *sev_guest = SEV_GUEST(sev_common); |
| 1740 | int kvm_type; |
| 1741 | |
| 1742 | if (sev_common->kvm_type != -1) { |
| 1743 | goto out; |
| 1744 | } |
| 1745 | |
| 1746 | /* These are the only cases where legacy VM types can be used. */ |
| 1747 | if (sev_guest->legacy_vm_type == ON_OFF_AUTO_ON || |
| 1748 | (sev_guest->legacy_vm_type == ON_OFF_AUTO_AUTO && |
| 1749 | !sev_init2_required(sev_guest))) { |
| 1750 | sev_common->kvm_type = KVM_X86_DEFAULT_VM; |
| 1751 | goto out; |
| 1752 | } |
| 1753 | |
| 1754 | /* |
| 1755 | * Newer VM types are required, either explicitly via legacy-vm-type=on, or |
| 1756 | * implicitly via legacy-vm-type=auto along with additional sev-guest |
| 1757 | * properties that require the newer VM types. |
| 1758 | */ |
| 1759 | kvm_type = (sev_guest->policy & SEV_POLICY_ES) ? |
| 1760 | KVM_X86_SEV_ES_VM : KVM_X86_SEV_VM; |
| 1761 | if (!kvm_is_vm_type_supported(kvm_type)) { |
| 1762 | if (sev_guest->legacy_vm_type == ON_OFF_AUTO_AUTO) { |
| 1763 | error_report("SEV: host kernel does not support requested %s VM type, which is required " |
| 1764 | "for the set of options specified. To allow use of the legacy " |
| 1765 | "KVM_X86_DEFAULT_VM VM type, please disable any options that are not " |
| 1766 | "compatible with the legacy VM type, or upgrade your kernel.", |
| 1767 | kvm_type == KVM_X86_SEV_VM ? "KVM_X86_SEV_VM" : "KVM_X86_SEV_ES_VM"); |
| 1768 | } else { |
| 1769 | error_report("SEV: host kernel does not support requested %s VM type. To allow use of " |
| 1770 | "the legacy KVM_X86_DEFAULT_VM VM type, the 'legacy-vm-type' argument " |
| 1771 | "must be set to 'on' or 'auto' for the sev-guest object.", |
| 1772 | kvm_type == KVM_X86_SEV_VM ? "KVM_X86_SEV_VM" : "KVM_X86_SEV_ES_VM"); |
| 1773 | } |
| 1774 | |
| 1775 | return -1; |
| 1776 | } |
| 1777 | |
| 1778 | sev_common->kvm_type = kvm_type; |
| 1779 | out: |
| 1780 | return sev_common->kvm_type; |
| 1781 | } |
| 1782 | |
| 1783 | static int sev_snp_kvm_type(X86ConfidentialGuest *cg) |
| 1784 | { |
| 1785 | return KVM_X86_SNP_VM; |
| 1786 | } |
| 1787 | |
| 1788 | static int sev_init_supported_features(ConfidentialGuestSupport *cgs, |
| 1789 | SevCommonState *sev_common, Error **errp) |
| 1790 | { |
| 1791 | X86ConfidentialGuestClass *x86_klass = |
| 1792 | X86_CONFIDENTIAL_GUEST_GET_CLASS(cgs); |
| 1793 | /* |
| 1794 | * Older kernels do not support query or setting of sev_features. In this |
| 1795 | * case the set of supported features must be zero to match the settings |
| 1796 | * in the kernel. |
| 1797 | */ |
| 1798 | if (x86_klass->kvm_type(X86_CONFIDENTIAL_GUEST(sev_common)) == |
| 1799 | KVM_X86_DEFAULT_VM) { |
| 1800 | sev_common->supported_sev_features = 0; |
| 1801 | return 0; |
| 1802 | } |
| 1803 | |
| 1804 | /* Query KVM for the supported set of sev_features */ |
| 1805 | struct kvm_device_attr attr = { |
| 1806 | .group = KVM_X86_GRP_SEV, |
| 1807 | .attr = KVM_X86_SEV_VMSA_FEATURES, |
| 1808 | .addr = (unsigned long)&sev_common->supported_sev_features, |
| 1809 | }; |
| 1810 | if (kvm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr) < 0) { |
| 1811 | error_setg(errp, "SEV: failed to query supported sev_features"); |
| 1812 | return -1; |
| 1813 | } |
| 1814 | if (sev_snp_enabled()) { |
| 1815 | sev_common->supported_sev_features |= SVM_SEV_FEAT_SNP_ACTIVE; |
| 1816 | } |
| 1817 | return 0; |
| 1818 | } |
| 1819 | |
| 1820 | static int sev_common_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) |
| 1821 | { |
| 1822 | char *devname; |
| 1823 | int ret, fw_error, cmd; |
| 1824 | uint32_t ebx; |
| 1825 | uint32_t host_cbitpos; |
| 1826 | struct sev_user_data_status status = {}; |
| 1827 | SevLaunchUpdateData *data, *next_elm; |
| 1828 | SevCommonState *sev_common = SEV_COMMON(cgs); |
| 1829 | SevCommonStateClass *klass = SEV_COMMON_GET_CLASS(cgs); |
| 1830 | X86ConfidentialGuestClass *x86_klass = |
| 1831 | X86_CONFIDENTIAL_GUEST_GET_CLASS(cgs); |
| 1832 | |
| 1833 | sev_common->state = SEV_STATE_UNINIT; |
| 1834 | |
| 1835 | /* free existing launch update data if any */ |
| 1836 | QTAILQ_FOREACH_SAFE(data, &launch_update, next, next_elm) { |
| 1837 | g_free(data); |
| 1838 | } |
| 1839 | |
| 1840 | host_cpuid(0x8000001F, 0, NULL, &ebx, NULL, NULL); |
| 1841 | host_cbitpos = ebx & 0x3f; |
| 1842 | |
| 1843 | /* |
| 1844 | * The cbitpos value will be placed in bit positions 5:0 of the EBX |
| 1845 | * register of CPUID 0x8000001F. No need to verify the range as the |
| 1846 | * comparison against the host value accomplishes that. |
| 1847 | */ |
| 1848 | if (host_cbitpos != sev_common->cbitpos) { |
| 1849 | error_setg(errp, "SEV: cbitpos check failed, host '%d' requested '%d'", |
| 1850 | host_cbitpos, sev_common->cbitpos); |
| 1851 | return -1; |
| 1852 | } |
| 1853 | |
| 1854 | /* |
| 1855 | * The reduced-phys-bits value will be placed in bit positions 11:6 of |
| 1856 | * the EBX register of CPUID 0x8000001F, so verify the supplied value |
| 1857 | * is in the range of 1 to 63. |
| 1858 | */ |
| 1859 | if (sev_common->reduced_phys_bits < 1 || |
| 1860 | sev_common->reduced_phys_bits > 63) { |
| 1861 | error_setg(errp, "SEV: reduced_phys_bits check failed," |
| 1862 | " it should be in the range of 1 to 63, requested '%d'", |
| 1863 | sev_common->reduced_phys_bits); |
| 1864 | return -1; |
| 1865 | } |
| 1866 | |
| 1867 | devname = object_property_get_str(OBJECT(sev_common), "sev-device", NULL); |
| 1868 | sev_common->sev_fd = open(devname, O_RDWR); |
| 1869 | if (sev_common->sev_fd < 0) { |
| 1870 | error_setg_file_open(errp, errno, devname); |
| 1871 | g_free(devname); |
| 1872 | return -1; |
| 1873 | } |
| 1874 | g_free(devname); |
| 1875 | |
| 1876 | ret = sev_platform_ioctl(sev_common->sev_fd, SEV_PLATFORM_STATUS, &status, |
| 1877 | &fw_error); |
| 1878 | if (ret) { |
| 1879 | error_setg(errp, "SEV: failed to get platform status ret=%d " |
| 1880 | "fw_error='%d: %s'", ret, fw_error, |
| 1881 | fw_error_to_str(fw_error)); |
| 1882 | return -1; |
| 1883 | } |
| 1884 | sev_common->build_id = status.build; |
| 1885 | sev_common->api_major = status.api_major; |
| 1886 | sev_common->api_minor = status.api_minor; |
| 1887 | |
| 1888 | if (sev_es_enabled()) { |
| 1889 | if (!kvm_kernel_irqchip_allowed()) { |
| 1890 | error_setg(errp, "SEV: SEV-ES guests require in-kernel irqchip support"); |
| 1891 | return -1; |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | if (sev_es_enabled() && !sev_snp_enabled()) { |
| 1896 | if (!(status.flags & SEV_STATUS_FLAGS_CONFIG_ES)) { |
| 1897 | error_setg(errp, "SEV: guest policy requires SEV-ES, but " |
| 1898 | "host SEV-ES support unavailable"); |
| 1899 | return -1; |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | if (sev_init_supported_features(cgs, sev_common, errp) < 0) { |
| 1904 | return -1; |
| 1905 | } |
| 1906 | |
| 1907 | trace_kvm_sev_init(); |
| 1908 | switch (x86_klass->kvm_type(X86_CONFIDENTIAL_GUEST(sev_common))) { |
| 1909 | case KVM_X86_DEFAULT_VM: |
| 1910 | cmd = sev_es_enabled() ? KVM_SEV_ES_INIT : KVM_SEV_INIT; |
| 1911 | |
| 1912 | ret = sev_ioctl(sev_common->sev_fd, cmd, NULL, &fw_error); |
| 1913 | break; |
| 1914 | case KVM_X86_SEV_VM: |
| 1915 | case KVM_X86_SEV_ES_VM: |
| 1916 | case KVM_X86_SNP_VM: { |
| 1917 | struct kvm_sev_init args = { 0 }; |
| 1918 | MachineState *machine = MACHINE(qdev_get_machine()); |
| 1919 | X86MachineState *x86machine = X86_MACHINE(qdev_get_machine()); |
| 1920 | |
| 1921 | /* |
| 1922 | * If configuration is provided via an IGVM file then the IGVM file |
| 1923 | * might contain configuration of the initial vcpu context. For SEV |
| 1924 | * the vcpu context includes the sev_features which should be applied |
| 1925 | * to the vcpu. |
| 1926 | * |
| 1927 | * KVM does not synchronize sev_features from CPU state. Instead it |
| 1928 | * requires sev_features to be provided as part of this initialization |
| 1929 | * call which is subsequently automatically applied to the VMSA of |
| 1930 | * each vcpu. |
| 1931 | * |
| 1932 | * The IGVM file is normally processed after initialization. Therefore |
| 1933 | * we need to pre-process it here to extract sev_features in order to |
| 1934 | * provide it to KVM_SEV_INIT2. Each cgs_* function that is called by |
| 1935 | * the IGVM processor detects this pre-process by observing the state |
| 1936 | * as SEV_STATE_UNINIT. |
| 1937 | */ |
| 1938 | if (x86machine->igvm) { |
| 1939 | /* |
| 1940 | * Test only the user-set SEV features by masking out |
| 1941 | * SVM_SEV_FEAT_SNP_ACTIVE which is set by default. |
| 1942 | */ |
| 1943 | if (sev_common->sev_features & ~SVM_SEV_FEAT_SNP_ACTIVE) { |
| 1944 | error_setg(errp, |
| 1945 | "SEV: SEV features can't be specified when using IGVM files"); |
| 1946 | return -1; |
| 1947 | } |
| 1948 | if (IGVM_CFG_GET_CLASS(x86machine->igvm) |
| 1949 | ->process(x86machine->igvm, machine, true, errp) == -1) { |
| 1950 | return -1; |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | if (check_sev_features(sev_common, sev_common->sev_features, errp) < 0) { |
| 1955 | return -1; |
| 1956 | } |
| 1957 | |
| 1958 | /* |
| 1959 | * KVM maintains a bitmask of allowed sev_features. This does not |
| 1960 | * include SVM_SEV_FEAT_SNP_ACTIVE which is set accordingly by KVM |
| 1961 | * itself. Therefore we need to clear this flag. |
| 1962 | */ |
| 1963 | args.vmsa_features = sev_common->sev_features & ~SVM_SEV_FEAT_SNP_ACTIVE; |
| 1964 | |
| 1965 | ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_INIT2, &args, &fw_error); |
| 1966 | break; |
| 1967 | } |
| 1968 | default: |
| 1969 | error_setg(errp, "SEV: host kernel does not support the requested SEV configuration."); |
| 1970 | return -1; |
| 1971 | } |
| 1972 | |
| 1973 | if (ret) { |
| 1974 | error_setg(errp, "SEV: failed to initialize ret=%d fw_error=%d '%s'", |
| 1975 | ret, fw_error, fw_error_to_str(fw_error)); |
| 1976 | return -1; |
| 1977 | } |
| 1978 | |
| 1979 | ret = klass->launch_start(sev_common); |
| 1980 | |
| 1981 | if (ret) { |
| 1982 | error_setg(errp, "SEV: failed to create encryption context"); |
| 1983 | return -1; |
| 1984 | } |
| 1985 | |
| 1986 | if (klass->kvm_init && klass->kvm_init(cgs, errp)) { |
| 1987 | return -1; |
| 1988 | } |
| 1989 | |
| 1990 | if (!cgs->ready) { |
| 1991 | qemu_add_vm_change_state_handler(sev_vm_state_change, sev_common); |
| 1992 | } |
| 1993 | cgs->ready = true; |
| 1994 | |
| 1995 | return 0; |
| 1996 | } |
| 1997 | |
| 1998 | static int sev_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) |
| 1999 | { |
| 2000 | int ret; |
| 2001 | |
| 2002 | /* |
| 2003 | * SEV/SEV-ES rely on pinned memory to back guest RAM so discarding |
| 2004 | * isn't actually possible. With SNP, only guest_memfd pages are used |
| 2005 | * for private guest memory, so discarding of shared memory is still |
| 2006 | * possible.. |
| 2007 | */ |
| 2008 | ret = ram_block_discard_disable(true); |
| 2009 | if (ret) { |
| 2010 | error_setg(errp, "SEV: cannot disable RAM discard"); |
| 2011 | return -1; |
| 2012 | } |
| 2013 | |
| 2014 | if (!cgs->ready) { |
| 2015 | /* |
| 2016 | * SEV uses these notifiers to register/pin pages prior to guest use, |
| 2017 | * but SNP relies on guest_memfd for private pages, which has its |
| 2018 | * own internal mechanisms for registering/pinning private memory. |
| 2019 | */ |
| 2020 | ram_block_notifier_add(&sev_ram_notifier); |
| 2021 | |
| 2022 | /* |
| 2023 | * The machine done notify event is used for SEV guests to get the |
| 2024 | * measurement of the encrypted images. When SEV-SNP is enabled, the |
| 2025 | * measurement is part of the guest attestation process where it can |
| 2026 | * be collected without any reliance on the VMM. So skip registering |
| 2027 | * the notifier for SNP in favor of using guest attestation instead. |
| 2028 | */ |
| 2029 | qemu_add_machine_init_done_notifier(&sev_machine_done_notify); |
| 2030 | } |
| 2031 | return 0; |
| 2032 | } |
| 2033 | |
| 2034 | static int sev_snp_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) |
| 2035 | { |
| 2036 | MachineState *ms = MACHINE(qdev_get_machine()); |
| 2037 | X86MachineState *x86ms = X86_MACHINE(ms); |
| 2038 | SevCommonState *sev_common = SEV_COMMON(cgs); |
| 2039 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(sev_common); |
| 2040 | |
| 2041 | if (x86ms->smm == ON_OFF_AUTO_AUTO) { |
| 2042 | x86ms->smm = ON_OFF_AUTO_OFF; |
| 2043 | } else if (x86ms->smm == ON_OFF_AUTO_ON) { |
| 2044 | error_setg(errp, "SEV-SNP does not support SMM."); |
| 2045 | return -1; |
| 2046 | } |
| 2047 | |
| 2048 | /* free existing kernel hashes data if any */ |
| 2049 | g_free(sev_snp_guest->kernel_hashes_data); |
| 2050 | sev_snp_guest->kernel_hashes_data = NULL; |
| 2051 | |
| 2052 | return 0; |
| 2053 | } |
| 2054 | |
| 2055 | /* |
| 2056 | * handle sev vm reset |
| 2057 | */ |
| 2058 | static void sev_handle_reset(Object *obj, ResetType type) |
| 2059 | { |
| 2060 | SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); |
| 2061 | SevCommonStateClass *klass = SEV_COMMON_GET_CLASS(sev_common); |
| 2062 | |
| 2063 | if (!runstate_is_running()) { |
| 2064 | return; |
| 2065 | } |
| 2066 | |
| 2067 | sev_add_kernel_loader_hashes(&sev_load_ctx, &error_fatal); |
| 2068 | if (sev_es_enabled() && !sev_snp_enabled()) { |
| 2069 | sev_launch_get_measure(NULL, NULL); |
| 2070 | } |
| 2071 | if (!sev_check_state(sev_common, SEV_STATE_RUNNING)) { |
| 2072 | /* this calls sev_snp_launch_finish() etc */ |
| 2073 | klass->launch_finish(sev_common); |
| 2074 | } |
| 2075 | |
| 2076 | trace_sev_handle_reset(); |
| 2077 | return; |
| 2078 | } |
| 2079 | |
| 2080 | static ResettableState *sev_reset_state(Object *obj) |
| 2081 | { |
| 2082 | SevCommonState *sev_common = SEV_COMMON(obj); |
| 2083 | return &sev_common->reset_state; |
| 2084 | } |
| 2085 | |
| 2086 | int |
| 2087 | sev_encrypt_flash(hwaddr gpa, uint8_t *ptr, uint64_t len, Error **errp) |
| 2088 | { |
| 2089 | SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); |
| 2090 | SevCommonStateClass *klass; |
| 2091 | |
| 2092 | if (!sev_common) { |
| 2093 | return 0; |
| 2094 | } |
| 2095 | klass = SEV_COMMON_GET_CLASS(sev_common); |
| 2096 | |
| 2097 | /* if SEV is in update state then encrypt the data else do nothing */ |
| 2098 | if (sev_check_state(sev_common, SEV_STATE_LAUNCH_UPDATE)) { |
| 2099 | int ret; |
| 2100 | |
| 2101 | ret = klass->launch_update_data(sev_common, gpa, ptr, len, errp); |
| 2102 | if (ret < 0) { |
| 2103 | return ret; |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | return 0; |
| 2108 | } |
| 2109 | |
| 2110 | int sev_inject_launch_secret(const char *packet_hdr, const char *secret, |
| 2111 | uint64_t gpa, Error **errp) |
| 2112 | { |
| 2113 | ERRP_GUARD(); |
| 2114 | struct kvm_sev_launch_secret input; |
| 2115 | g_autofree guchar *data = NULL, *hdr = NULL; |
| 2116 | int error, ret = 1; |
| 2117 | void *hva; |
| 2118 | gsize hdr_sz = 0, data_sz = 0; |
| 2119 | g_autoptr(MemoryRegion) mr = NULL; |
| 2120 | SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); |
| 2121 | |
| 2122 | if (!sev_common) { |
| 2123 | error_setg(errp, "SEV not enabled for guest"); |
| 2124 | return 1; |
| 2125 | } |
| 2126 | |
| 2127 | /* secret can be injected only in this state */ |
| 2128 | if (!sev_check_state(sev_common, SEV_STATE_LAUNCH_SECRET)) { |
| 2129 | error_setg(errp, "SEV: Not in correct state. (LSECRET) %x", |
| 2130 | sev_common->state); |
| 2131 | return 1; |
| 2132 | } |
| 2133 | |
| 2134 | hdr = g_base64_decode(packet_hdr, &hdr_sz); |
| 2135 | if (!hdr || !hdr_sz) { |
| 2136 | error_setg(errp, "SEV: Failed to decode sequence header"); |
| 2137 | return 1; |
| 2138 | } |
| 2139 | |
| 2140 | data = g_base64_decode(secret, &data_sz); |
| 2141 | if (!data || !data_sz) { |
| 2142 | error_setg(errp, "SEV: Failed to decode data"); |
| 2143 | return 1; |
| 2144 | } |
| 2145 | |
| 2146 | hva = gpa2hva(&mr, gpa, data_sz, errp); |
| 2147 | if (!hva) { |
| 2148 | error_prepend(errp, "SEV: Failed to calculate guest address: "); |
| 2149 | return 1; |
| 2150 | } |
| 2151 | |
| 2152 | input.hdr_uaddr = (uint64_t)(unsigned long)hdr; |
| 2153 | input.hdr_len = hdr_sz; |
| 2154 | |
| 2155 | input.trans_uaddr = (uint64_t)(unsigned long)data; |
| 2156 | input.trans_len = data_sz; |
| 2157 | |
| 2158 | input.guest_uaddr = (uint64_t)(unsigned long)hva; |
| 2159 | input.guest_len = data_sz; |
| 2160 | |
| 2161 | trace_kvm_sev_launch_secret(gpa, input.guest_uaddr, |
| 2162 | input.trans_uaddr, input.trans_len); |
| 2163 | |
| 2164 | ret = sev_ioctl(sev_common->sev_fd, KVM_SEV_LAUNCH_SECRET, |
| 2165 | &input, &error); |
| 2166 | if (ret) { |
| 2167 | error_setg(errp, "SEV: failed to inject secret ret=%d fw_error=%d '%s'", |
| 2168 | ret, error, fw_error_to_str(error)); |
| 2169 | return ret; |
| 2170 | } |
| 2171 | |
| 2172 | return 0; |
| 2173 | } |
| 2174 | |
| 2175 | #define SEV_SECRET_GUID "4c2eb361-7d9b-4cc3-8081-127c90d3d294" |
| 2176 | struct sev_secret_area { |
| 2177 | uint32_t base; |
| 2178 | uint32_t size; |
| 2179 | }; |
| 2180 | |
| 2181 | void qmp_sev_inject_launch_secret(const char *packet_hdr, |
| 2182 | const char *secret, |
| 2183 | bool has_gpa, uint64_t gpa, |
| 2184 | Error **errp) |
| 2185 | { |
| 2186 | if (!sev_enabled()) { |
| 2187 | error_setg(errp, "SEV not enabled for guest"); |
| 2188 | return; |
| 2189 | } |
| 2190 | if (!has_gpa) { |
| 2191 | uint8_t *data; |
| 2192 | struct sev_secret_area *area; |
| 2193 | |
| 2194 | if (!pc_system_ovmf_table_find(SEV_SECRET_GUID, &data, NULL)) { |
| 2195 | error_setg(errp, "SEV: no secret area found in OVMF," |
| 2196 | " gpa must be specified."); |
| 2197 | return; |
| 2198 | } |
| 2199 | area = (struct sev_secret_area *)data; |
| 2200 | gpa = area->base; |
| 2201 | } |
| 2202 | |
| 2203 | sev_inject_launch_secret(packet_hdr, secret, gpa, errp); |
| 2204 | } |
| 2205 | |
| 2206 | static int |
| 2207 | sev_es_parse_reset_block(SevInfoBlock *info, uint32_t *addr) |
| 2208 | { |
| 2209 | if (!info->reset_addr) { |
| 2210 | error_report("SEV-ES reset address is zero"); |
| 2211 | return 1; |
| 2212 | } |
| 2213 | |
| 2214 | *addr = info->reset_addr; |
| 2215 | |
| 2216 | return 0; |
| 2217 | } |
| 2218 | |
| 2219 | static int |
| 2220 | sev_es_find_reset_vector(void *flash_ptr, uint64_t flash_size, |
| 2221 | uint32_t *addr) |
| 2222 | { |
| 2223 | QemuUUID info_guid, *guid; |
| 2224 | SevInfoBlock *info; |
| 2225 | uint8_t *data; |
| 2226 | uint16_t *len; |
| 2227 | |
| 2228 | /* |
| 2229 | * Initialize the address to zero. An address of zero with a successful |
| 2230 | * return code indicates that SEV-ES is not active. |
| 2231 | */ |
| 2232 | *addr = 0; |
| 2233 | |
| 2234 | /* |
| 2235 | * Extract the AP reset vector for SEV-ES guests by locating the SEV GUID. |
| 2236 | * The SEV GUID is located on its own (original implementation) or within |
| 2237 | * the Firmware GUID Table (new implementation), either of which are |
| 2238 | * located 32 bytes from the end of the flash. |
| 2239 | * |
| 2240 | * Check the Firmware GUID Table first. |
| 2241 | */ |
| 2242 | if (pc_system_ovmf_table_find(SEV_INFO_BLOCK_GUID, &data, NULL)) { |
| 2243 | return sev_es_parse_reset_block((SevInfoBlock *)data, addr); |
| 2244 | } |
| 2245 | |
| 2246 | /* |
| 2247 | * SEV info block not found in the Firmware GUID Table (or there isn't |
| 2248 | * a Firmware GUID Table), fall back to the original implementation. |
| 2249 | */ |
| 2250 | data = flash_ptr + flash_size - 0x20; |
| 2251 | |
| 2252 | qemu_uuid_parse(SEV_INFO_BLOCK_GUID, &info_guid); |
| 2253 | info_guid = qemu_uuid_bswap(info_guid); /* GUIDs are LE */ |
| 2254 | |
| 2255 | guid = (QemuUUID *)(data - sizeof(info_guid)); |
| 2256 | if (!qemu_uuid_is_equal(guid, &info_guid)) { |
| 2257 | error_report("SEV information block/Firmware GUID Table block not found in pflash rom"); |
| 2258 | return 1; |
| 2259 | } |
| 2260 | |
| 2261 | len = (uint16_t *)((uint8_t *)guid - sizeof(*len)); |
| 2262 | info = (SevInfoBlock *)(data - le16_to_cpu(*len)); |
| 2263 | |
| 2264 | return sev_es_parse_reset_block(info, addr); |
| 2265 | } |
| 2266 | |
| 2267 | |
| 2268 | static void seg_to_vmsa(const SegmentCache *cpu_seg, struct vmcb_seg *vmsa_seg) |
| 2269 | { |
| 2270 | vmsa_seg->selector = cpu_seg->selector; |
| 2271 | vmsa_seg->base = cpu_seg->base; |
| 2272 | vmsa_seg->limit = cpu_seg->limit; |
| 2273 | vmsa_seg->attrib = FLAGS_SEGCACHE_TO_VMSA(cpu_seg->flags); |
| 2274 | } |
| 2275 | |
| 2276 | static void initialize_vmsa(const CPUState *cpu, struct sev_es_save_area *vmsa) |
| 2277 | { |
| 2278 | const X86CPU *x86 = X86_CPU(cpu); |
| 2279 | const CPUX86State *env = &x86->env; |
| 2280 | |
| 2281 | /* |
| 2282 | * Initialize the SEV-ES save area from the current state of |
| 2283 | * the CPU. The entire state does not need to be copied, only the state |
| 2284 | * that is copied back to the CPUState in sev_apply_cpu_context. |
| 2285 | */ |
| 2286 | memset(vmsa, 0, sizeof(struct sev_es_save_area)); |
| 2287 | vmsa->efer = env->efer; |
| 2288 | vmsa->cr0 = env->cr[0]; |
| 2289 | vmsa->cr3 = env->cr[3]; |
| 2290 | vmsa->cr4 = env->cr[4]; |
| 2291 | vmsa->xcr0 = env->xcr0; |
| 2292 | vmsa->g_pat = env->pat; |
| 2293 | |
| 2294 | seg_to_vmsa(&env->segs[R_CS], &vmsa->cs); |
| 2295 | seg_to_vmsa(&env->segs[R_DS], &vmsa->ds); |
| 2296 | seg_to_vmsa(&env->segs[R_ES], &vmsa->es); |
| 2297 | seg_to_vmsa(&env->segs[R_FS], &vmsa->fs); |
| 2298 | seg_to_vmsa(&env->segs[R_GS], &vmsa->gs); |
| 2299 | seg_to_vmsa(&env->segs[R_SS], &vmsa->ss); |
| 2300 | |
| 2301 | seg_to_vmsa(&env->gdt, &vmsa->gdtr); |
| 2302 | seg_to_vmsa(&env->idt, &vmsa->idtr); |
| 2303 | seg_to_vmsa(&env->ldt, &vmsa->ldtr); |
| 2304 | seg_to_vmsa(&env->tr, &vmsa->tr); |
| 2305 | |
| 2306 | vmsa->dr6 = env->dr[6]; |
| 2307 | vmsa->dr7 = env->dr[7]; |
| 2308 | |
| 2309 | vmsa->rax = env->regs[R_EAX]; |
| 2310 | vmsa->rcx = env->regs[R_ECX]; |
| 2311 | vmsa->rdx = env->regs[R_EDX]; |
| 2312 | vmsa->rbx = env->regs[R_EBX]; |
| 2313 | vmsa->rsp = env->regs[R_ESP]; |
| 2314 | vmsa->rbp = env->regs[R_EBP]; |
| 2315 | vmsa->rsi = env->regs[R_ESI]; |
| 2316 | vmsa->rdi = env->regs[R_EDI]; |
| 2317 | |
| 2318 | #ifdef TARGET_X86_64 |
| 2319 | vmsa->r8 = env->regs[R_R8]; |
| 2320 | vmsa->r9 = env->regs[R_R9]; |
| 2321 | vmsa->r10 = env->regs[R_R10]; |
| 2322 | vmsa->r11 = env->regs[R_R11]; |
| 2323 | vmsa->r12 = env->regs[R_R12]; |
| 2324 | vmsa->r13 = env->regs[R_R13]; |
| 2325 | vmsa->r14 = env->regs[R_R14]; |
| 2326 | vmsa->r15 = env->regs[R_R15]; |
| 2327 | #endif |
| 2328 | |
| 2329 | vmsa->rip = env->eip; |
| 2330 | vmsa->rflags = env->eflags; |
| 2331 | } |
| 2332 | |
| 2333 | static void sev_es_set_ap_context(uint32_t reset_addr) |
| 2334 | { |
| 2335 | CPUState *cpu; |
| 2336 | struct sev_es_save_area vmsa; |
| 2337 | SegmentCache cs; |
| 2338 | |
| 2339 | cs.selector = 0xf000; |
| 2340 | cs.base = reset_addr & 0xffff0000; |
| 2341 | cs.limit = 0xffff; |
| 2342 | cs.flags = DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK | DESC_R_MASK | |
| 2343 | DESC_A_MASK; |
| 2344 | |
| 2345 | CPU_FOREACH(cpu) { |
| 2346 | if (cpu->cpu_index == 0) { |
| 2347 | /* Do not update the BSP reset state */ |
| 2348 | continue; |
| 2349 | } |
| 2350 | initialize_vmsa(cpu, &vmsa); |
| 2351 | seg_to_vmsa(&cs, &vmsa.cs); |
| 2352 | vmsa.rip = reset_addr & 0x0000ffff; |
| 2353 | sev_set_cpu_context(cpu->cpu_index, &vmsa, |
| 2354 | sizeof(struct sev_es_save_area), |
| 2355 | 0, &error_fatal); |
| 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | void sev_es_set_reset_vector(CPUState *cpu) |
| 2360 | { |
| 2361 | if (sev_enabled()) { |
| 2362 | sev_apply_cpu_context(cpu); |
| 2363 | } |
| 2364 | } |
| 2365 | |
| 2366 | int sev_es_save_reset_vector(void *flash_ptr, uint64_t flash_size) |
| 2367 | { |
| 2368 | uint32_t addr; |
| 2369 | int ret; |
| 2370 | |
| 2371 | if (!sev_es_enabled()) { |
| 2372 | return 0; |
| 2373 | } |
| 2374 | |
| 2375 | addr = 0; |
| 2376 | ret = sev_es_find_reset_vector(flash_ptr, flash_size, |
| 2377 | &addr); |
| 2378 | if (ret) { |
| 2379 | return ret; |
| 2380 | } |
| 2381 | |
| 2382 | /* |
| 2383 | * The reset vector is saved into a CPU context for each AP but not for |
| 2384 | * the BSP. This is applied during guest startup or when the CPU is reset. |
| 2385 | */ |
| 2386 | if (addr) { |
| 2387 | sev_es_set_ap_context(addr); |
| 2388 | } |
| 2389 | |
| 2390 | return 0; |
| 2391 | } |
| 2392 | |
| 2393 | static const QemuUUID sev_hash_table_header_guid = { |
| 2394 | .data = UUID_LE(0x9438d606, 0x4f22, 0x4cc9, 0xb4, 0x79, 0xa7, 0x93, |
| 2395 | 0xd4, 0x11, 0xfd, 0x21) |
| 2396 | }; |
| 2397 | |
| 2398 | static const QemuUUID sev_kernel_entry_guid = { |
| 2399 | .data = UUID_LE(0x4de79437, 0xabd2, 0x427f, 0xb8, 0x35, 0xd5, 0xb1, |
| 2400 | 0x72, 0xd2, 0x04, 0x5b) |
| 2401 | }; |
| 2402 | static const QemuUUID sev_initrd_entry_guid = { |
| 2403 | .data = UUID_LE(0x44baf731, 0x3a2f, 0x4bd7, 0x9a, 0xf1, 0x41, 0xe2, |
| 2404 | 0x91, 0x69, 0x78, 0x1d) |
| 2405 | }; |
| 2406 | static const QemuUUID sev_cmdline_entry_guid = { |
| 2407 | .data = UUID_LE(0x97d02dd8, 0xbd20, 0x4c94, 0xaa, 0x78, 0xe7, 0x71, |
| 2408 | 0x4d, 0x36, 0xab, 0x2a) |
| 2409 | }; |
| 2410 | |
| 2411 | static bool build_kernel_loader_hashes(PaddedSevHashTable *padded_ht, |
| 2412 | SevKernelLoaderContext *ctx, |
| 2413 | Error **errp) |
| 2414 | { |
| 2415 | SevHashTable *ht; |
| 2416 | uint8_t cmdline_hash[HASH_SIZE]; |
| 2417 | uint8_t initrd_hash[HASH_SIZE]; |
| 2418 | uint8_t kernel_hash[HASH_SIZE]; |
| 2419 | uint8_t *hashp; |
| 2420 | size_t hash_len = HASH_SIZE; |
| 2421 | |
| 2422 | /* |
| 2423 | * Calculate hash of kernel command-line with the terminating null byte. If |
| 2424 | * the user doesn't supply a command-line via -append, the 1-byte "\0" will |
| 2425 | * be used. |
| 2426 | */ |
| 2427 | hashp = cmdline_hash; |
| 2428 | if (qcrypto_hash_bytes(QCRYPTO_HASH_ALGO_SHA256, ctx->cmdline_data, |
| 2429 | ctx->cmdline_size, &hashp, &hash_len, errp) < 0) { |
| 2430 | return false; |
| 2431 | } |
| 2432 | assert(hash_len == HASH_SIZE); |
| 2433 | |
| 2434 | /* |
| 2435 | * Calculate hash of initrd. If the user doesn't supply an initrd via |
| 2436 | * -initrd, an empty buffer will be used (ctx->initrd_size == 0). |
| 2437 | */ |
| 2438 | hashp = initrd_hash; |
| 2439 | if (qcrypto_hash_bytes(QCRYPTO_HASH_ALGO_SHA256, ctx->initrd_data, |
| 2440 | ctx->initrd_size, &hashp, &hash_len, errp) < 0) { |
| 2441 | return false; |
| 2442 | } |
| 2443 | assert(hash_len == HASH_SIZE); |
| 2444 | |
| 2445 | /* Calculate hash of the kernel */ |
| 2446 | hashp = kernel_hash; |
| 2447 | struct iovec iov[2] = { |
| 2448 | { .iov_base = ctx->setup_data, .iov_len = ctx->setup_size }, |
| 2449 | { .iov_base = ctx->kernel_data, .iov_len = ctx->kernel_size } |
| 2450 | }; |
| 2451 | if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALGO_SHA256, iov, ARRAY_SIZE(iov), |
| 2452 | &hashp, &hash_len, errp) < 0) { |
| 2453 | return false; |
| 2454 | } |
| 2455 | assert(hash_len == HASH_SIZE); |
| 2456 | |
| 2457 | ht = &padded_ht->ht; |
| 2458 | |
| 2459 | ht->guid = sev_hash_table_header_guid; |
| 2460 | ht->len = sizeof(*ht); |
| 2461 | |
| 2462 | ht->cmdline.guid = sev_cmdline_entry_guid; |
| 2463 | ht->cmdline.len = sizeof(ht->cmdline); |
| 2464 | memcpy(ht->cmdline.hash, cmdline_hash, sizeof(ht->cmdline.hash)); |
| 2465 | |
| 2466 | ht->initrd.guid = sev_initrd_entry_guid; |
| 2467 | ht->initrd.len = sizeof(ht->initrd); |
| 2468 | memcpy(ht->initrd.hash, initrd_hash, sizeof(ht->initrd.hash)); |
| 2469 | |
| 2470 | ht->kernel.guid = sev_kernel_entry_guid; |
| 2471 | ht->kernel.len = sizeof(ht->kernel); |
| 2472 | memcpy(ht->kernel.hash, kernel_hash, sizeof(ht->kernel.hash)); |
| 2473 | |
| 2474 | /* zero the excess data so the measurement can be reliably calculated */ |
| 2475 | memset(padded_ht->padding, 0, sizeof(padded_ht->padding)); |
| 2476 | |
| 2477 | return true; |
| 2478 | } |
| 2479 | |
| 2480 | static bool sev_snp_build_kernel_loader_hashes(SevCommonState *sev_common, |
| 2481 | SevHashTableDescriptor *area, |
| 2482 | SevKernelLoaderContext *ctx, |
| 2483 | Error **errp) |
| 2484 | { |
| 2485 | /* |
| 2486 | * SNP: Populate the hashes table in an area that later in |
| 2487 | * snp_launch_update_kernel_hashes() will be copied to the guest memory |
| 2488 | * and encrypted. |
| 2489 | */ |
| 2490 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(sev_common); |
| 2491 | sev_snp_guest->kernel_hashes_offset = area->base & ~TARGET_PAGE_MASK; |
| 2492 | sev_snp_guest->kernel_hashes_data = g_new0(PaddedSevHashTable, 1); |
| 2493 | return build_kernel_loader_hashes(sev_snp_guest->kernel_hashes_data, ctx, errp); |
| 2494 | } |
| 2495 | |
| 2496 | static bool sev_build_kernel_loader_hashes(SevCommonState *sev_common, |
| 2497 | SevHashTableDescriptor *area, |
| 2498 | SevKernelLoaderContext *ctx, |
| 2499 | Error **errp) |
| 2500 | { |
| 2501 | PaddedSevHashTable *padded_ht; |
| 2502 | hwaddr mapped_len = sizeof(*padded_ht); |
| 2503 | MemTxAttrs attrs = { 0 }; |
| 2504 | bool ret = true; |
| 2505 | |
| 2506 | /* |
| 2507 | * Populate the hashes table in the guest's memory at the OVMF-designated |
| 2508 | * area for the SEV hashes table |
| 2509 | */ |
| 2510 | padded_ht = address_space_map(&address_space_memory, area->base, |
| 2511 | &mapped_len, true, attrs); |
| 2512 | if (!padded_ht || mapped_len != sizeof(*padded_ht)) { |
| 2513 | error_setg(errp, "SEV: cannot map hashes table guest memory area"); |
| 2514 | return false; |
| 2515 | } |
| 2516 | |
| 2517 | if (build_kernel_loader_hashes(padded_ht, ctx, errp)) { |
| 2518 | if (sev_encrypt_flash(area->base, (uint8_t *)padded_ht, |
| 2519 | sizeof(*padded_ht), errp) < 0) { |
| 2520 | ret = false; |
| 2521 | } |
| 2522 | } else { |
| 2523 | ret = false; |
| 2524 | } |
| 2525 | |
| 2526 | address_space_unmap(&address_space_memory, padded_ht, |
| 2527 | mapped_len, true, mapped_len); |
| 2528 | |
| 2529 | return ret; |
| 2530 | } |
| 2531 | |
| 2532 | /* |
| 2533 | * Add the hashes of the linux kernel/initrd/cmdline to an encrypted guest page |
| 2534 | * which is included in SEV's initial memory measurement. |
| 2535 | */ |
| 2536 | bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp) |
| 2537 | { |
| 2538 | uint8_t *data; |
| 2539 | SevHashTableDescriptor *area; |
| 2540 | SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); |
| 2541 | SevCommonStateClass *klass = SEV_COMMON_GET_CLASS(sev_common); |
| 2542 | |
| 2543 | /* |
| 2544 | * Only add the kernel hashes if the sev-guest configuration explicitly |
| 2545 | * stated kernel-hashes=on. |
| 2546 | */ |
| 2547 | if (!sev_common->kernel_hashes) { |
| 2548 | return false; |
| 2549 | } |
| 2550 | |
| 2551 | if (!pc_system_ovmf_table_find(SEV_HASH_TABLE_RV_GUID, &data, NULL)) { |
| 2552 | error_setg(errp, "SEV: kernel specified but guest firmware " |
| 2553 | "has no hashes table GUID"); |
| 2554 | return false; |
| 2555 | } |
| 2556 | |
| 2557 | area = (SevHashTableDescriptor *)data; |
| 2558 | if (!area->base || area->size < sizeof(PaddedSevHashTable)) { |
| 2559 | error_setg(errp, "SEV: guest firmware hashes table area is invalid " |
| 2560 | "(base=0x%x size=0x%x)", area->base, area->size); |
| 2561 | return false; |
| 2562 | } |
| 2563 | |
| 2564 | /* save the context here so that it can be re-used when vm is reset */ |
| 2565 | memcpy(&sev_load_ctx, ctx, sizeof(*ctx)); |
| 2566 | return klass->build_kernel_loader_hashes(sev_common, area, ctx, errp); |
| 2567 | } |
| 2568 | |
| 2569 | static char * |
| 2570 | sev_common_get_sev_device(Object *obj, Error **errp) |
| 2571 | { |
| 2572 | return g_strdup(SEV_COMMON(obj)->sev_device); |
| 2573 | } |
| 2574 | |
| 2575 | static void |
| 2576 | sev_common_set_sev_device(Object *obj, const char *value, Error **errp) |
| 2577 | { |
| 2578 | g_free(SEV_COMMON(obj)->sev_device); |
| 2579 | SEV_COMMON(obj)->sev_device = g_strdup(value); |
| 2580 | } |
| 2581 | |
| 2582 | static bool sev_common_get_kernel_hashes(Object *obj, Error **errp) |
| 2583 | { |
| 2584 | return SEV_COMMON(obj)->kernel_hashes; |
| 2585 | } |
| 2586 | |
| 2587 | static void sev_common_set_kernel_hashes(Object *obj, bool value, Error **errp) |
| 2588 | { |
| 2589 | SEV_COMMON(obj)->kernel_hashes = value; |
| 2590 | } |
| 2591 | |
| 2592 | static bool cgs_check_support(ConfidentialGuestPlatformType platform, |
| 2593 | uint16_t platform_version, uint8_t highest_vtl, |
| 2594 | uint64_t shared_gpa_boundary) |
| 2595 | { |
| 2596 | return (((platform == CGS_PLATFORM_SEV_SNP) && sev_snp_enabled()) || |
| 2597 | ((platform == CGS_PLATFORM_SEV_ES) && sev_es_enabled()) || |
| 2598 | ((platform == CGS_PLATFORM_SEV) && sev_enabled())); |
| 2599 | } |
| 2600 | |
| 2601 | static int cgs_set_guest_state(hwaddr gpa, uint8_t *ptr, uint64_t len, |
| 2602 | ConfidentialGuestPageType memory_type, |
| 2603 | uint16_t cpu_index, Error **errp) |
| 2604 | { |
| 2605 | SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); |
| 2606 | SevCommonStateClass *klass = SEV_COMMON_GET_CLASS(sev_common); |
| 2607 | |
| 2608 | if (sev_common->state == SEV_STATE_UNINIT) { |
| 2609 | /* Pre-processing of IGVM file called from sev_common_kvm_init() */ |
| 2610 | if ((cpu_index == 0) && (memory_type == CGS_PAGE_TYPE_VMSA)) { |
| 2611 | const struct sev_es_save_area *sa = |
| 2612 | (const struct sev_es_save_area *)ptr; |
| 2613 | if (len < sizeof(*sa)) { |
| 2614 | error_setg(errp, "SEV: invalid VMSA length encountered"); |
| 2615 | return -1; |
| 2616 | } |
| 2617 | sev_common->sev_features = sa->sev_features; |
| 2618 | } |
| 2619 | return 0; |
| 2620 | } |
| 2621 | |
| 2622 | if (!sev_enabled()) { |
| 2623 | error_setg(errp, "SEV: attempt to configure guest memory, but SEV " |
| 2624 | "is not enabled"); |
| 2625 | return -1; |
| 2626 | } |
| 2627 | |
| 2628 | switch (memory_type) { |
| 2629 | case CGS_PAGE_TYPE_NORMAL: |
| 2630 | case CGS_PAGE_TYPE_ZERO: |
| 2631 | return klass->launch_update_data(sev_common, gpa, ptr, len, errp); |
| 2632 | |
| 2633 | case CGS_PAGE_TYPE_VMSA: |
| 2634 | if (!sev_es_enabled()) { |
| 2635 | error_setg(errp, |
| 2636 | "SEV: attempt to configure initial VMSA, but SEV-ES " |
| 2637 | "is not supported"); |
| 2638 | return -1; |
| 2639 | } |
| 2640 | if (check_vmsa_supported(sev_common, gpa, |
| 2641 | (const struct sev_es_save_area *)ptr, |
| 2642 | errp) < 0) { |
| 2643 | return -1; |
| 2644 | } |
| 2645 | return sev_set_cpu_context(cpu_index, ptr, len, gpa, errp); |
| 2646 | |
| 2647 | case CGS_PAGE_TYPE_UNMEASURED: |
| 2648 | if (sev_snp_enabled()) { |
| 2649 | return snp_launch_update_data( |
| 2650 | gpa, ptr, len, KVM_SEV_SNP_PAGE_TYPE_UNMEASURED, errp); |
| 2651 | } |
| 2652 | /* No action required if not SEV-SNP */ |
| 2653 | return 0; |
| 2654 | |
| 2655 | case CGS_PAGE_TYPE_SECRETS: |
| 2656 | if (!sev_snp_enabled()) { |
| 2657 | error_setg(errp, |
| 2658 | "SEV: attempt to configure secrets page, but SEV-SNP " |
| 2659 | "is not supported"); |
| 2660 | return -1; |
| 2661 | } |
| 2662 | return snp_launch_update_data(gpa, ptr, len, |
| 2663 | KVM_SEV_SNP_PAGE_TYPE_SECRETS, errp); |
| 2664 | |
| 2665 | case CGS_PAGE_TYPE_REQUIRED_MEMORY: |
| 2666 | if (kvm_convert_memory(gpa, len, true) < 0) { |
| 2667 | error_setg( |
| 2668 | errp, |
| 2669 | "SEV: failed to configure required memory. gpa: %lX, type: %d", |
| 2670 | gpa, memory_type); |
| 2671 | return -1; |
| 2672 | } |
| 2673 | return 0; |
| 2674 | |
| 2675 | case CGS_PAGE_TYPE_CPUID: |
| 2676 | if (!sev_snp_enabled()) { |
| 2677 | error_setg(errp, |
| 2678 | "SEV: attempt to configure CPUID page, but SEV-SNP " |
| 2679 | "is not supported"); |
| 2680 | return -1; |
| 2681 | } |
| 2682 | return snp_launch_update_cpuid(gpa, ptr, len, errp); |
| 2683 | } |
| 2684 | error_setg(errp, "SEV: failed to update guest. gpa: %lX, type: %d", |
| 2685 | gpa, memory_type); |
| 2686 | return -1; |
| 2687 | } |
| 2688 | |
| 2689 | static int cgs_get_mem_map_entry(int index, |
| 2690 | ConfidentialGuestMemoryMapEntry *entry, |
| 2691 | Error **errp) |
| 2692 | { |
| 2693 | struct e820_entry *table; |
| 2694 | int num_entries; |
| 2695 | |
| 2696 | SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); |
| 2697 | if (sev_common->state == SEV_STATE_UNINIT) { |
| 2698 | /* Pre-processing of IGVM file called from sev_common_kvm_init() */ |
| 2699 | return 1; |
| 2700 | } |
| 2701 | |
| 2702 | num_entries = e820_get_table(&table); |
| 2703 | if ((index < 0) || (index >= num_entries)) { |
| 2704 | return 1; |
| 2705 | } |
| 2706 | entry->gpa = table[index].address; |
| 2707 | entry->size = table[index].length; |
| 2708 | switch (table[index].type) { |
| 2709 | case E820_RAM: |
| 2710 | entry->type = CGS_MEM_RAM; |
| 2711 | break; |
| 2712 | case E820_RESERVED: |
| 2713 | entry->type = CGS_MEM_RESERVED; |
| 2714 | break; |
| 2715 | case E820_ACPI: |
| 2716 | entry->type = CGS_MEM_ACPI; |
| 2717 | break; |
| 2718 | case E820_NVS: |
| 2719 | entry->type = CGS_MEM_NVS; |
| 2720 | break; |
| 2721 | case E820_UNUSABLE: |
| 2722 | entry->type = CGS_MEM_UNUSABLE; |
| 2723 | break; |
| 2724 | } |
| 2725 | return 0; |
| 2726 | } |
| 2727 | |
| 2728 | static int cgs_set_guest_policy(ConfidentialGuestPolicyType policy_type, |
| 2729 | uint64_t policy, void *policy_data1, |
| 2730 | uint32_t policy_data1_size, void *policy_data2, |
| 2731 | uint32_t policy_data2_size, Error **errp) |
| 2732 | { |
| 2733 | SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); |
| 2734 | if (sev_common->state == SEV_STATE_UNINIT) { |
| 2735 | /* Pre-processing of IGVM file called from sev_common_kvm_init() */ |
| 2736 | return 0; |
| 2737 | } |
| 2738 | |
| 2739 | if (policy_type != GUEST_POLICY_SEV) { |
| 2740 | error_setg(errp, "SEV: Invalid guest policy type provided for SEV: %d", |
| 2741 | policy_type); |
| 2742 | return -1; |
| 2743 | } |
| 2744 | /* |
| 2745 | * SEV-SNP handles policy differently. The policy flags are defined in |
| 2746 | * kvm_start_conf.policy and an ID block and ID auth can be provided. |
| 2747 | */ |
| 2748 | if (sev_snp_enabled()) { |
| 2749 | SevSnpGuestState *sev_snp_guest = |
| 2750 | SEV_SNP_GUEST(MACHINE(qdev_get_machine())->cgs); |
| 2751 | struct kvm_sev_snp_launch_finish *finish = |
| 2752 | &sev_snp_guest->kvm_finish_conf; |
| 2753 | |
| 2754 | /* |
| 2755 | * The policy consists of flags in 'policy' and optionally an ID block |
| 2756 | * and ID auth in policy_data1 and policy_data2 respectively. The ID |
| 2757 | * block and auth are optional so clear any previous ID block and auth |
| 2758 | * and set them if provided, but always set the policy flags. |
| 2759 | */ |
| 2760 | g_free(sev_snp_guest->id_block); |
| 2761 | g_free((guchar *)finish->id_block_uaddr); |
| 2762 | g_free(sev_snp_guest->id_auth); |
| 2763 | g_free((guchar *)finish->id_auth_uaddr); |
| 2764 | sev_snp_guest->id_block = NULL; |
| 2765 | finish->id_block_uaddr = 0; |
| 2766 | sev_snp_guest->id_auth = NULL; |
| 2767 | finish->id_auth_uaddr = 0; |
| 2768 | |
| 2769 | if (policy_data1_size > 0) { |
| 2770 | struct sev_snp_id_authentication *id_auth = |
| 2771 | (struct sev_snp_id_authentication *)policy_data2; |
| 2772 | |
| 2773 | if (policy_data1_size != KVM_SEV_SNP_ID_BLOCK_SIZE) { |
| 2774 | error_setg(errp, "SEV: Invalid SEV-SNP ID block: incorrect size"); |
| 2775 | return -1; |
| 2776 | } |
| 2777 | if (policy_data2_size != KVM_SEV_SNP_ID_AUTH_SIZE) { |
| 2778 | error_setg(errp, |
| 2779 | "SEV: Invalid SEV-SNP ID auth block: incorrect size"); |
| 2780 | return -1; |
| 2781 | } |
| 2782 | assert(policy_data1 != NULL); |
| 2783 | assert(policy_data2 != NULL); |
| 2784 | |
| 2785 | finish->id_block_uaddr = |
| 2786 | (__u64)g_memdup2(policy_data1, KVM_SEV_SNP_ID_BLOCK_SIZE); |
| 2787 | finish->id_auth_uaddr = |
| 2788 | (__u64)g_memdup2(policy_data2, KVM_SEV_SNP_ID_AUTH_SIZE); |
| 2789 | |
| 2790 | /* |
| 2791 | * Check if an author key has been provided and use that to flag |
| 2792 | * whether the author key is enabled. The first of the author key |
| 2793 | * must be non-zero to indicate the key type, which will currently |
| 2794 | * always be 2. |
| 2795 | */ |
| 2796 | sev_snp_guest->kvm_finish_conf.auth_key_en = |
| 2797 | id_auth->author_key[0] ? 1 : 0; |
| 2798 | finish->id_block_en = 1; |
| 2799 | } |
| 2800 | |
| 2801 | /* do not reset existing policy if policy was not set in IGVM */ |
| 2802 | if (policy != 0) { |
| 2803 | sev_snp_guest->kvm_start_conf.policy = policy; |
| 2804 | } |
| 2805 | } else { |
| 2806 | SevGuestState *sev_guest = SEV_GUEST(MACHINE(qdev_get_machine())->cgs); |
| 2807 | /* Only the policy flags are supported for SEV and SEV-ES */ |
| 2808 | if ((policy_data1_size > 0) || (policy_data2_size > 0) || !sev_guest) { |
| 2809 | error_setg(errp, "SEV: An ID block/ID auth block has been provided " |
| 2810 | "but SEV-SNP is not enabled"); |
| 2811 | return -1; |
| 2812 | } |
| 2813 | |
| 2814 | /* do not reset existing policy if policy was not set in IGVM */ |
| 2815 | if (policy != 0) { |
| 2816 | sev_guest->policy = policy; |
| 2817 | } |
| 2818 | } |
| 2819 | return 0; |
| 2820 | } |
| 2821 | |
| 2822 | static bool sev_common_get_debug_swap(Object *obj, Error **errp) |
| 2823 | { |
| 2824 | return is_sev_feature_set(SEV_COMMON(obj), SVM_SEV_FEAT_DEBUG_SWAP); |
| 2825 | } |
| 2826 | |
| 2827 | static void sev_common_set_debug_swap(Object *obj, bool value, Error **errp) |
| 2828 | { |
| 2829 | sev_set_feature(SEV_COMMON(obj), SVM_SEV_FEAT_DEBUG_SWAP, value); |
| 2830 | } |
| 2831 | |
| 2832 | static void |
| 2833 | sev_common_class_init(ObjectClass *oc, const void *data) |
| 2834 | { |
| 2835 | ConfidentialGuestSupportClass *klass = CONFIDENTIAL_GUEST_SUPPORT_CLASS(oc); |
| 2836 | ResettableClass *rc = RESETTABLE_CLASS(oc); |
| 2837 | |
| 2838 | klass->kvm_init = sev_common_kvm_init; |
| 2839 | /* |
| 2840 | * the exit phase makes sure sev handles reset after all legacy resets |
| 2841 | * have taken place (in the hold phase) and IGVM has also properly |
| 2842 | * set up the boot state. |
| 2843 | */ |
| 2844 | rc->phases.exit = sev_handle_reset; |
| 2845 | rc->get_state = sev_reset_state; |
| 2846 | |
| 2847 | object_class_property_add_str(oc, "sev-device", |
| 2848 | sev_common_get_sev_device, |
| 2849 | sev_common_set_sev_device); |
| 2850 | object_class_property_set_description(oc, "sev-device", |
| 2851 | "SEV device to use"); |
| 2852 | object_class_property_add_bool(oc, "kernel-hashes", |
| 2853 | sev_common_get_kernel_hashes, |
| 2854 | sev_common_set_kernel_hashes); |
| 2855 | object_class_property_set_description(oc, "kernel-hashes", |
| 2856 | "add kernel hashes to guest firmware for measured Linux boot"); |
| 2857 | object_class_property_add_bool(oc, "debug-swap", |
| 2858 | sev_common_get_debug_swap, |
| 2859 | sev_common_set_debug_swap); |
| 2860 | object_class_property_set_description(oc, "debug-swap", |
| 2861 | "enable virtualization of debug registers"); |
| 2862 | } |
| 2863 | |
| 2864 | static void |
| 2865 | sev_common_instance_init(Object *obj) |
| 2866 | { |
| 2867 | SevCommonState *sev_common = SEV_COMMON(obj); |
| 2868 | ConfidentialGuestSupportClass *cgs = |
| 2869 | CONFIDENTIAL_GUEST_SUPPORT_GET_CLASS(obj); |
| 2870 | |
| 2871 | sev_common->kvm_type = -1; |
| 2872 | |
| 2873 | sev_common->sev_device = g_strdup(DEFAULT_SEV_DEVICE); |
| 2874 | |
| 2875 | object_property_add_uint32_ptr(obj, "cbitpos", &sev_common->cbitpos, |
| 2876 | OBJ_PROP_FLAG_READWRITE); |
| 2877 | object_property_add_uint32_ptr(obj, "reduced-phys-bits", |
| 2878 | &sev_common->reduced_phys_bits, |
| 2879 | OBJ_PROP_FLAG_READWRITE); |
| 2880 | cgs->check_support = cgs_check_support; |
| 2881 | cgs->set_guest_state = cgs_set_guest_state; |
| 2882 | cgs->get_mem_map_entry = cgs_get_mem_map_entry; |
| 2883 | cgs->set_guest_policy = cgs_set_guest_policy; |
| 2884 | cgs->can_rebuild_guest_state = true; |
| 2885 | |
| 2886 | QTAILQ_INIT(&sev_common->launch_vmsa); |
| 2887 | } |
| 2888 | |
| 2889 | static void |
| 2890 | sev_common_finalize(Object *obj) |
| 2891 | { |
| 2892 | SevCommonState *sev_common = SEV_COMMON(obj); |
| 2893 | |
| 2894 | g_free(sev_common->sev_device); |
| 2895 | } |
| 2896 | |
| 2897 | /* sev guest info common to sev/sev-es/sev-snp */ |
| 2898 | static const TypeInfo sev_common_info = { |
| 2899 | .parent = TYPE_X86_CONFIDENTIAL_GUEST, |
| 2900 | .name = TYPE_SEV_COMMON, |
| 2901 | .instance_size = sizeof(SevCommonState), |
| 2902 | .instance_init = sev_common_instance_init, |
| 2903 | .instance_finalize = sev_common_finalize, |
| 2904 | .class_size = sizeof(SevCommonStateClass), |
| 2905 | .class_init = sev_common_class_init, |
| 2906 | .abstract = true, |
| 2907 | .interfaces = (const InterfaceInfo[]) { |
| 2908 | { TYPE_USER_CREATABLE }, |
| 2909 | { TYPE_RESETTABLE_INTERFACE }, |
| 2910 | { } |
| 2911 | } |
| 2912 | }; |
| 2913 | |
| 2914 | static char * |
| 2915 | sev_guest_get_dh_cert_file(Object *obj, Error **errp) |
| 2916 | { |
| 2917 | return g_strdup(SEV_GUEST(obj)->dh_cert_file); |
| 2918 | } |
| 2919 | |
| 2920 | static void |
| 2921 | sev_guest_set_dh_cert_file(Object *obj, const char *value, Error **errp) |
| 2922 | { |
| 2923 | g_free(SEV_GUEST(obj)->dh_cert_file); |
| 2924 | SEV_GUEST(obj)->dh_cert_file = g_strdup(value); |
| 2925 | } |
| 2926 | |
| 2927 | static char * |
| 2928 | sev_guest_get_session_file(Object *obj, Error **errp) |
| 2929 | { |
| 2930 | SevGuestState *sev_guest = SEV_GUEST(obj); |
| 2931 | |
| 2932 | return sev_guest->session_file ? g_strdup(sev_guest->session_file) : NULL; |
| 2933 | } |
| 2934 | |
| 2935 | static void |
| 2936 | sev_guest_set_session_file(Object *obj, const char *value, Error **errp) |
| 2937 | { |
| 2938 | g_free(SEV_GUEST(obj)->session_file); |
| 2939 | SEV_GUEST(obj)->session_file = g_strdup(value); |
| 2940 | } |
| 2941 | |
| 2942 | static void sev_guest_get_legacy_vm_type(Object *obj, Visitor *v, |
| 2943 | const char *name, void *opaque, |
| 2944 | Error **errp) |
| 2945 | { |
| 2946 | SevGuestState *sev_guest = SEV_GUEST(obj); |
| 2947 | OnOffAuto legacy_vm_type = sev_guest->legacy_vm_type; |
| 2948 | |
| 2949 | visit_type_OnOffAuto(v, name, &legacy_vm_type, errp); |
| 2950 | } |
| 2951 | |
| 2952 | static void sev_guest_set_legacy_vm_type(Object *obj, Visitor *v, |
| 2953 | const char *name, void *opaque, |
| 2954 | Error **errp) |
| 2955 | { |
| 2956 | SevGuestState *sev_guest = SEV_GUEST(obj); |
| 2957 | |
| 2958 | visit_type_OnOffAuto(v, name, &sev_guest->legacy_vm_type, errp); |
| 2959 | } |
| 2960 | |
| 2961 | static void |
| 2962 | sev_guest_class_init(ObjectClass *oc, const void *data) |
| 2963 | { |
| 2964 | SevCommonStateClass *klass = SEV_COMMON_CLASS(oc); |
| 2965 | X86ConfidentialGuestClass *x86_klass = X86_CONFIDENTIAL_GUEST_CLASS(oc); |
| 2966 | |
| 2967 | klass->build_kernel_loader_hashes = sev_build_kernel_loader_hashes; |
| 2968 | klass->launch_start = sev_launch_start; |
| 2969 | klass->launch_finish = sev_launch_finish; |
| 2970 | klass->launch_update_data = sev_launch_update_data; |
| 2971 | klass->kvm_init = sev_kvm_init; |
| 2972 | x86_klass->kvm_type = sev_kvm_type; |
| 2973 | |
| 2974 | object_class_property_add_str(oc, "dh-cert-file", |
| 2975 | sev_guest_get_dh_cert_file, |
| 2976 | sev_guest_set_dh_cert_file); |
| 2977 | object_class_property_set_description(oc, "dh-cert-file", |
| 2978 | "guest owners DH certificate (encoded with base64)"); |
| 2979 | object_class_property_add_str(oc, "session-file", |
| 2980 | sev_guest_get_session_file, |
| 2981 | sev_guest_set_session_file); |
| 2982 | object_class_property_set_description(oc, "session-file", |
| 2983 | "guest owners session parameters (encoded with base64)"); |
| 2984 | object_class_property_add(oc, "legacy-vm-type", "OnOffAuto", |
| 2985 | sev_guest_get_legacy_vm_type, |
| 2986 | sev_guest_set_legacy_vm_type, NULL, NULL); |
| 2987 | object_class_property_set_description(oc, "legacy-vm-type", |
| 2988 | "use legacy VM type to maintain measurement compatibility with older QEMU or kernel versions."); |
| 2989 | } |
| 2990 | |
| 2991 | static void |
| 2992 | sev_guest_instance_init(Object *obj) |
| 2993 | { |
| 2994 | SevGuestState *sev_guest = SEV_GUEST(obj); |
| 2995 | |
| 2996 | sev_guest->policy = DEFAULT_GUEST_POLICY; |
| 2997 | object_property_add_uint32_ptr(obj, "handle", &sev_guest->handle, |
| 2998 | OBJ_PROP_FLAG_READWRITE); |
| 2999 | object_property_add_uint32_ptr(obj, "policy", &sev_guest->policy, |
| 3000 | OBJ_PROP_FLAG_READWRITE); |
| 3001 | object_apply_compat_props(obj); |
| 3002 | |
| 3003 | sev_guest->legacy_vm_type = ON_OFF_AUTO_AUTO; |
| 3004 | } |
| 3005 | |
| 3006 | static void |
| 3007 | sev_guest_finalize(Object *obj) |
| 3008 | { |
| 3009 | SevGuestState *sev_guest = SEV_GUEST(obj); |
| 3010 | |
| 3011 | g_free(sev_guest->dh_cert_file); |
| 3012 | g_free(sev_guest->session_file); |
| 3013 | g_free(sev_guest->measurement); |
| 3014 | } |
| 3015 | |
| 3016 | /* guest info specific sev/sev-es */ |
| 3017 | static const TypeInfo sev_guest_info = { |
| 3018 | .parent = TYPE_SEV_COMMON, |
| 3019 | .name = TYPE_SEV_GUEST, |
| 3020 | .instance_size = sizeof(SevGuestState), |
| 3021 | .instance_init = sev_guest_instance_init, |
| 3022 | .instance_finalize = sev_guest_finalize, |
| 3023 | .class_init = sev_guest_class_init, |
| 3024 | }; |
| 3025 | |
| 3026 | static void |
| 3027 | sev_snp_guest_get_policy(Object *obj, Visitor *v, const char *name, |
| 3028 | void *opaque, Error **errp) |
| 3029 | { |
| 3030 | visit_type_uint64(v, name, |
| 3031 | (uint64_t *)&SEV_SNP_GUEST(obj)->kvm_start_conf.policy, |
| 3032 | errp); |
| 3033 | } |
| 3034 | |
| 3035 | static void |
| 3036 | sev_snp_guest_set_policy(Object *obj, Visitor *v, const char *name, |
| 3037 | void *opaque, Error **errp) |
| 3038 | { |
| 3039 | visit_type_uint64(v, name, |
| 3040 | (uint64_t *)&SEV_SNP_GUEST(obj)->kvm_start_conf.policy, |
| 3041 | errp); |
| 3042 | } |
| 3043 | |
| 3044 | static char * |
| 3045 | sev_snp_guest_get_guest_visible_workarounds(Object *obj, Error **errp) |
| 3046 | { |
| 3047 | return g_strdup(SEV_SNP_GUEST(obj)->guest_visible_workarounds); |
| 3048 | } |
| 3049 | |
| 3050 | static void |
| 3051 | sev_snp_guest_set_guest_visible_workarounds(Object *obj, const char *value, |
| 3052 | Error **errp) |
| 3053 | { |
| 3054 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj); |
| 3055 | struct kvm_sev_snp_launch_start *start = &sev_snp_guest->kvm_start_conf; |
| 3056 | g_autofree guchar *blob; |
| 3057 | gsize len; |
| 3058 | |
| 3059 | g_free(sev_snp_guest->guest_visible_workarounds); |
| 3060 | |
| 3061 | /* store the base64 str so we don't need to re-encode in getter */ |
| 3062 | sev_snp_guest->guest_visible_workarounds = g_strdup(value); |
| 3063 | |
| 3064 | blob = qbase64_decode(sev_snp_guest->guest_visible_workarounds, |
| 3065 | -1, &len, errp); |
| 3066 | if (!blob) { |
| 3067 | return; |
| 3068 | } |
| 3069 | |
| 3070 | if (len != sizeof(start->gosvw)) { |
| 3071 | error_setg(errp, "parameter length of %" G_GSIZE_FORMAT |
| 3072 | " exceeds max of %zu", |
| 3073 | len, sizeof(start->gosvw)); |
| 3074 | return; |
| 3075 | } |
| 3076 | |
| 3077 | memcpy(start->gosvw, blob, len); |
| 3078 | } |
| 3079 | |
| 3080 | static char * |
| 3081 | sev_snp_guest_get_id_block(Object *obj, Error **errp) |
| 3082 | { |
| 3083 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj); |
| 3084 | |
| 3085 | return g_strdup(sev_snp_guest->id_block_base64); |
| 3086 | } |
| 3087 | |
| 3088 | static void |
| 3089 | sev_snp_guest_set_id_block(Object *obj, const char *value, Error **errp) |
| 3090 | { |
| 3091 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj); |
| 3092 | struct kvm_sev_snp_launch_finish *finish = &sev_snp_guest->kvm_finish_conf; |
| 3093 | gsize len; |
| 3094 | |
| 3095 | finish->id_block_en = 0; |
| 3096 | g_free(sev_snp_guest->id_block); |
| 3097 | g_free(sev_snp_guest->id_block_base64); |
| 3098 | |
| 3099 | /* store the base64 str so we don't need to re-encode in getter */ |
| 3100 | sev_snp_guest->id_block_base64 = g_strdup(value); |
| 3101 | sev_snp_guest->id_block = |
| 3102 | qbase64_decode(sev_snp_guest->id_block_base64, -1, &len, errp); |
| 3103 | |
| 3104 | if (!sev_snp_guest->id_block) { |
| 3105 | return; |
| 3106 | } |
| 3107 | |
| 3108 | if (len != KVM_SEV_SNP_ID_BLOCK_SIZE) { |
| 3109 | error_setg(errp, "parameter length of %" G_GSIZE_FORMAT |
| 3110 | " not equal to %u", |
| 3111 | len, KVM_SEV_SNP_ID_BLOCK_SIZE); |
| 3112 | return; |
| 3113 | } |
| 3114 | |
| 3115 | finish->id_block_en = 1; |
| 3116 | finish->id_block_uaddr = (uintptr_t)sev_snp_guest->id_block; |
| 3117 | } |
| 3118 | |
| 3119 | static char * |
| 3120 | sev_snp_guest_get_id_auth(Object *obj, Error **errp) |
| 3121 | { |
| 3122 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj); |
| 3123 | |
| 3124 | return g_strdup(sev_snp_guest->id_auth_base64); |
| 3125 | } |
| 3126 | |
| 3127 | static void |
| 3128 | sev_snp_guest_set_id_auth(Object *obj, const char *value, Error **errp) |
| 3129 | { |
| 3130 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj); |
| 3131 | struct kvm_sev_snp_launch_finish *finish = &sev_snp_guest->kvm_finish_conf; |
| 3132 | gsize len; |
| 3133 | |
| 3134 | finish->id_auth_uaddr = 0; |
| 3135 | g_free(sev_snp_guest->id_auth); |
| 3136 | g_free(sev_snp_guest->id_auth_base64); |
| 3137 | |
| 3138 | /* store the base64 str so we don't need to re-encode in getter */ |
| 3139 | sev_snp_guest->id_auth_base64 = g_strdup(value); |
| 3140 | sev_snp_guest->id_auth = |
| 3141 | qbase64_decode(sev_snp_guest->id_auth_base64, -1, &len, errp); |
| 3142 | |
| 3143 | if (!sev_snp_guest->id_auth) { |
| 3144 | return; |
| 3145 | } |
| 3146 | |
| 3147 | if (len > KVM_SEV_SNP_ID_AUTH_SIZE) { |
| 3148 | error_setg(errp, "parameter length:ID_AUTH %" G_GSIZE_FORMAT |
| 3149 | " exceeds max of %u", |
| 3150 | len, KVM_SEV_SNP_ID_AUTH_SIZE); |
| 3151 | return; |
| 3152 | } |
| 3153 | |
| 3154 | finish->id_auth_uaddr = (uintptr_t)sev_snp_guest->id_auth; |
| 3155 | } |
| 3156 | |
| 3157 | static bool |
| 3158 | sev_snp_guest_get_author_key_enabled(Object *obj, Error **errp) |
| 3159 | { |
| 3160 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj); |
| 3161 | |
| 3162 | return !!sev_snp_guest->kvm_finish_conf.auth_key_en; |
| 3163 | } |
| 3164 | |
| 3165 | static void |
| 3166 | sev_snp_guest_set_author_key_enabled(Object *obj, bool value, Error **errp) |
| 3167 | { |
| 3168 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj); |
| 3169 | |
| 3170 | sev_snp_guest->kvm_finish_conf.auth_key_en = value; |
| 3171 | } |
| 3172 | |
| 3173 | static bool |
| 3174 | sev_snp_guest_get_vcek_disabled(Object *obj, Error **errp) |
| 3175 | { |
| 3176 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj); |
| 3177 | |
| 3178 | return !!sev_snp_guest->kvm_finish_conf.vcek_disabled; |
| 3179 | } |
| 3180 | |
| 3181 | static void |
| 3182 | sev_snp_guest_set_vcek_disabled(Object *obj, bool value, Error **errp) |
| 3183 | { |
| 3184 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj); |
| 3185 | |
| 3186 | sev_snp_guest->kvm_finish_conf.vcek_disabled = value; |
| 3187 | } |
| 3188 | |
| 3189 | static char * |
| 3190 | sev_snp_guest_get_host_data(Object *obj, Error **errp) |
| 3191 | { |
| 3192 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj); |
| 3193 | |
| 3194 | return g_strdup(sev_snp_guest->host_data); |
| 3195 | } |
| 3196 | |
| 3197 | static void |
| 3198 | sev_snp_guest_set_host_data(Object *obj, const char *value, Error **errp) |
| 3199 | { |
| 3200 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj); |
| 3201 | struct kvm_sev_snp_launch_finish *finish = &sev_snp_guest->kvm_finish_conf; |
| 3202 | g_autofree guchar *blob; |
| 3203 | gsize len; |
| 3204 | |
| 3205 | g_free(sev_snp_guest->host_data); |
| 3206 | |
| 3207 | /* store the base64 str so we don't need to re-encode in getter */ |
| 3208 | sev_snp_guest->host_data = g_strdup(value); |
| 3209 | |
| 3210 | blob = qbase64_decode(sev_snp_guest->host_data, -1, &len, errp); |
| 3211 | |
| 3212 | if (!blob) { |
| 3213 | return; |
| 3214 | } |
| 3215 | |
| 3216 | if (len != sizeof(finish->host_data)) { |
| 3217 | error_setg(errp, "parameter length of %" G_GSIZE_FORMAT |
| 3218 | " not equal to %zu", |
| 3219 | len, sizeof(finish->host_data)); |
| 3220 | return; |
| 3221 | } |
| 3222 | |
| 3223 | memcpy(finish->host_data, blob, len); |
| 3224 | } |
| 3225 | |
| 3226 | static bool sev_snp_guest_get_secure_tsc(Object *obj, Error **errp) |
| 3227 | { |
| 3228 | return is_sev_feature_set(SEV_COMMON(obj), SVM_SEV_FEAT_SECURE_TSC); |
| 3229 | } |
| 3230 | |
| 3231 | static void sev_snp_guest_set_secure_tsc(Object *obj, bool value, Error **errp) |
| 3232 | { |
| 3233 | sev_set_feature(SEV_COMMON(obj), SVM_SEV_FEAT_SECURE_TSC, value); |
| 3234 | } |
| 3235 | |
| 3236 | static void |
| 3237 | sev_snp_guest_get_tsc_frequency(Object *obj, Visitor *v, const char *name, |
| 3238 | void *opaque, Error **errp) |
| 3239 | { |
| 3240 | uint32_t value = SEV_SNP_GUEST(obj)->tsc_khz * 1000; |
| 3241 | |
| 3242 | visit_type_uint32(v, name, &value, errp); |
| 3243 | } |
| 3244 | |
| 3245 | static void |
| 3246 | sev_snp_guest_set_tsc_frequency(Object *obj, Visitor *v, const char *name, |
| 3247 | void *opaque, Error **errp) |
| 3248 | { |
| 3249 | uint32_t value; |
| 3250 | |
| 3251 | if (!visit_type_uint32(v, name, &value, errp)) { |
| 3252 | return; |
| 3253 | } |
| 3254 | |
| 3255 | SEV_SNP_GUEST(obj)->tsc_khz = value / 1000; |
| 3256 | } |
| 3257 | |
| 3258 | static void |
| 3259 | sev_snp_guest_class_init(ObjectClass *oc, const void *data) |
| 3260 | { |
| 3261 | SevCommonStateClass *klass = SEV_COMMON_CLASS(oc); |
| 3262 | X86ConfidentialGuestClass *x86_klass = X86_CONFIDENTIAL_GUEST_CLASS(oc); |
| 3263 | |
| 3264 | klass->build_kernel_loader_hashes = sev_snp_build_kernel_loader_hashes; |
| 3265 | klass->launch_start = sev_snp_launch_start; |
| 3266 | klass->launch_finish = sev_snp_launch_finish; |
| 3267 | klass->launch_update_data = sev_snp_launch_update_data; |
| 3268 | klass->kvm_init = sev_snp_kvm_init; |
| 3269 | x86_klass->adjust_cpuid_features = sev_snp_adjust_cpuid_features; |
| 3270 | x86_klass->kvm_type = sev_snp_kvm_type; |
| 3271 | |
| 3272 | object_class_property_add(oc, "policy", "uint64", |
| 3273 | sev_snp_guest_get_policy, |
| 3274 | sev_snp_guest_set_policy, NULL, NULL); |
| 3275 | object_class_property_add_str(oc, "guest-visible-workarounds", |
| 3276 | sev_snp_guest_get_guest_visible_workarounds, |
| 3277 | sev_snp_guest_set_guest_visible_workarounds); |
| 3278 | object_class_property_add_str(oc, "id-block", |
| 3279 | sev_snp_guest_get_id_block, |
| 3280 | sev_snp_guest_set_id_block); |
| 3281 | object_class_property_add_str(oc, "id-auth", |
| 3282 | sev_snp_guest_get_id_auth, |
| 3283 | sev_snp_guest_set_id_auth); |
| 3284 | object_class_property_add_bool(oc, "author-key-enabled", |
| 3285 | sev_snp_guest_get_author_key_enabled, |
| 3286 | sev_snp_guest_set_author_key_enabled); |
| 3287 | object_class_property_add_bool(oc, "vcek-disabled", |
| 3288 | sev_snp_guest_get_vcek_disabled, |
| 3289 | sev_snp_guest_set_vcek_disabled); |
| 3290 | object_class_property_add_str(oc, "host-data", |
| 3291 | sev_snp_guest_get_host_data, |
| 3292 | sev_snp_guest_set_host_data); |
| 3293 | object_class_property_add_bool(oc, "secure-tsc", |
| 3294 | sev_snp_guest_get_secure_tsc, |
| 3295 | sev_snp_guest_set_secure_tsc); |
| 3296 | object_class_property_add(oc, "tsc-frequency", "uint32", |
| 3297 | sev_snp_guest_get_tsc_frequency, |
| 3298 | sev_snp_guest_set_tsc_frequency, NULL, NULL); |
| 3299 | } |
| 3300 | |
| 3301 | static void |
| 3302 | sev_snp_guest_instance_init(Object *obj) |
| 3303 | { |
| 3304 | ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj); |
| 3305 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj); |
| 3306 | |
| 3307 | cgs->require_guest_memfd = true; |
| 3308 | |
| 3309 | /* default init/start/finish params for kvm */ |
| 3310 | sev_snp_guest->kvm_start_conf.policy = DEFAULT_SEV_SNP_POLICY; |
| 3311 | sev_set_feature(SEV_COMMON(sev_snp_guest), SVM_SEV_FEAT_SNP_ACTIVE, true); |
| 3312 | } |
| 3313 | |
| 3314 | static void |
| 3315 | sev_snp_guest_finalize(Object *obj) |
| 3316 | { |
| 3317 | SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj); |
| 3318 | |
| 3319 | g_free(sev_snp_guest->guest_visible_workarounds); |
| 3320 | g_free(sev_snp_guest->id_block_base64); |
| 3321 | g_free(sev_snp_guest->id_block); |
| 3322 | g_free(sev_snp_guest->id_auth_base64); |
| 3323 | g_free(sev_snp_guest->id_auth); |
| 3324 | g_free(sev_snp_guest->host_data); |
| 3325 | } |
| 3326 | |
| 3327 | /* guest info specific to sev-snp */ |
| 3328 | static const TypeInfo sev_snp_guest_info = { |
| 3329 | .parent = TYPE_SEV_COMMON, |
| 3330 | .name = TYPE_SEV_SNP_GUEST, |
| 3331 | .instance_size = sizeof(SevSnpGuestState), |
| 3332 | .class_init = sev_snp_guest_class_init, |
| 3333 | .instance_init = sev_snp_guest_instance_init, |
| 3334 | .instance_finalize = sev_snp_guest_finalize, |
| 3335 | }; |
| 3336 | |
| 3337 | static void |
| 3338 | sev_register_types(void) |
| 3339 | { |
| 3340 | type_register_static(&sev_common_info); |
| 3341 | type_register_static(&sev_guest_info); |
| 3342 | type_register_static(&sev_snp_guest_info); |
| 3343 | } |
| 3344 | |
| 3345 | type_init(sev_register_types); |