From patchwork Thu Mar 2 15:17:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 9600661 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 870A360429 for ; Thu, 2 Mar 2017 15:53:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 74D5728485 for ; Thu, 2 Mar 2017 15:53:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 68E69285A3; Thu, 2 Mar 2017 15:53:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 720C228485 for ; Thu, 2 Mar 2017 15:53:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751422AbdCBPxa (ORCPT ); Thu, 2 Mar 2017 10:53:30 -0500 Received: from mail-cys01nam02on0053.outbound.protection.outlook.com ([104.47.37.53]:19434 "EHLO NAM02-CY1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752105AbdCBPwn (ORCPT ); Thu, 2 Mar 2017 10:52:43 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=BabRD+8SP09AKdgdetzTcMHogaAs/xaiaTq1N87Grxc=; b=whh6Jd88o78nR0Drtq4obpwu2F/VkTI+sb0znASq2+o68m93HJ/WJ/iqI+YRcwH0NuFgz5IFojuTKVW4rPonIfYEQl8OTcEG09fbF4hfeJIHVjeHQAKxQqpSc9P4mTeje1Bf9ebbhE1nQZpLRXb2JFtbDWWsCEFKhdQ/xdYKgmQ= Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none; vger.kernel.org; dmarc=none action=none header.from=amd.com; Received: from [127.0.1.1] (165.204.77.1) by DM5PR12MB1610.namprd12.prod.outlook.com (10.172.40.16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.947.12; Thu, 2 Mar 2017 15:17:26 +0000 Subject: [RFC PATCH v2 24/32] kvm: x86: prepare for SEV guest management API support From: Brijesh Singh To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Date: Thu, 2 Mar 2017 10:17:22 -0500 Message-ID: <148846784278.2349.17771314083820274411.stgit@brijesh-build-machine> In-Reply-To: <148846752022.2349.13667498174822419498.stgit@brijesh-build-machine> References: <148846752022.2349.13667498174822419498.stgit@brijesh-build-machine> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: BN6PR17CA0002.namprd17.prod.outlook.com (10.173.147.12) To DM5PR12MB1610.namprd12.prod.outlook.com (10.172.40.16) X-MS-Office365-Filtering-Correlation-Id: a603cbfb-dcee-4c1b-f216-08d4617f3e92 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(48565401081); SRVR:DM5PR12MB1610; X-Microsoft-Exchange-Diagnostics: 1; DM5PR12MB1610; 3:3rwDFMak7XXHt4ud2HmmP1D26747cg6y1uqVUPcIG9xweNjJeGiU96Yj2y/XmRk/nSZKuLesMKvkj70FM6FSboaIrX76N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gcx0pRZAc9+r5ttUlYZbiRd/VhrsbKYHhvMh4vtGpZX1p5ldZp45PKN7n6kbYYjWLr+tXQH501V1n9SEKyZfybZWdF5OsTQl7LA1iz1gtqtZFBiRGoH1TWi5CoZuVpkU5t1IrIjggvut69EyyoOrDlRcSDg7MjlFg386ztsZNh5wq/j0WJhlsHz7AK/yydHuj8cSmYFbzNe0gdHbOeVbTgYV6oqrNIlMCAAn2a88axoWngldCJK5kK1zg2Bh0/qU8/ipgETLi5R1yoB4u/mM0uOiyyAF2dcfym8SQVQFMcEEh5y4znu0RDAa0lJGgKzu2MwvFk2wqMMG9zqTohSgYs1Hpbjp X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001)(6055026)(6041248)(20161123555025)(20161123564025)(20161123562025)(20161123558025)(20161123560025)(6072148); SRVR:DM5PR12MB1610; BCL:0; PCL:0; RULEID:; SRVR:DM5PR12MB1610; X-Microsoft-Exchange-Diagnostics: 1; DM5PR12MB1610; 4:+sCCXQ8KjKTdr8OCbnKVZzinT0gMzKt97zgZ4k14yHtUTVJvUfYmPZPfdKycTtP16Qs+bW3XlC1+ziqdB/3Sj4fRzZpO9KVoQmT2Wa3gabY6eE6WLVx8/ahjPGsHa0ecDWMe/L1Qk+M0egwrr4KinPT6QlwL4TnFgLkZ5qxG24ceC1o6PFznOW+NgWWP26GQKnqg7sddRy29ymgKnMLkV4368l4gRPHgkljuMpHjWzUFTPsg4ckrvMoIkI6rNcADLTUlj2GC5e9Qm/tqpWMRosmE1+GaXuPTmnaIEu3nD2eOSh95dJkFX3qh0xDeT/Vs4CyAaxoXwX1boFGshkkrAiVPfgYpIJ1fJRIPn+DHcvB62vUEjPcDKK0pHdV323kMehyRT+IdRfBEb7GGFipCyqDbxkvE+1+mvkwuu++IBiO7x80d24QWqcTXXASPY/K/C27Lk8+HL7xp9APklo5tUN45x3H7SzhsoQgKma7QWOfjlyv00etv0hNvQFDD5J4gdvStwXKzFbMiL9Yd87RocXAaxLTMZaFRG3TOqcTBqoNssp6B11a94TC0HsJnURkZYTO8o+BMk2rgWJvd0TFA25SnxgiX63P8CdO7oYdOahd1qoUAVSFbF0Lpe+cEsXxlk0xQdfdZx0EhytZObgFce0034HI6qRMSPtthajYFDJs= X-Forefront-PRVS: 023495660C X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6049001)(6009001)(7916002)(39850400002)(39860400002)(39840400002)(39410400002)(39450400003)(50466002)(2906002)(9686003)(50986999)(76176999)(33646002)(54356999)(7416002)(2201001)(2950100002)(103116003)(7406005)(23676002)(5660300001)(7736002)(4001350100001)(189998001)(83506001)(8676002)(38730400002)(1191002)(305945005)(53936002)(42186005)(6486002)(47776003)(77096006)(230700001)(6116002)(3846002)(33716001)(90366009)(6666003)(66066001)(86362001)(25786008)(81166006)(92566002)(921003)(84006005)(2101003)(217873001)(83996005)(1121003); DIR:OUT; SFP:1101; SCL:1; SRVR:DM5PR12MB1610; H:[127.0.1.1]; FPR:; SPF:None; MLV:sfv; LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtETTVQUjEyTUIxNjEwOzIzOm9tL0xaRCtxd2QySWhiV3U3RmFJbkx5cE94?= =?utf-8?B?SnJJMjYyanIyYll1djJLdlBJUEdtVlNQSnZXZ0FCUVFqRWExNWRaYzJqaEhD?= =?utf-8?B?bVBFRk90RUR0Yng4SWpFK1BXZTgvbDh5RElVWExDTmxuYmhHT2ptN05LTkxT?= =?utf-8?B?a2tud0ZVcUNqdzR4YityaHpHZEdINy9ET0pSWVZka0prZVpvOStkRHJRRkZv?= =?utf-8?B?ckwrRkxUUjRwNEo5UzdrWnNTM285T2hKT0JnSSs1T0xXN1M5NERwMTBpRzU4?= =?utf-8?B?YXo1aG5icXRBWldUa3Y1blJJNzdjTU9OUkI4dVRKV0xpeFpWcTNO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X8mrjz80kdJhNVYxD/WdHhtScgk=; 5:rGMR1UnqLXH6ZuynpdJPXda56NUOsFQUNpRanhvOjnOsW1sf5fxQMSsk8ESwhTESGIBukW1VFqG7OeY+lybXICaHZ6kk0iKTPpidtWaSGX34aPnslqmyByeQT92RhJFPk2+h2zO8/EzZi1ITffJJtw==; 24:oJrG9LOOBi8qB6u5qv4LvtFfiDh7TMZJvDYggCo0kNKzXvTB5ol/T0kma8j5Xjeva9dSjzeK2uNcabdTpwMJ32K7rqX2cfQ04cMlSG+nY/0= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; DM5PR12MB1610; 7:Nlr7sHYQ1Mvq/6yhTKnh1Sn6GGZvEuktQcWGcU0f9ylHnH2YnyXaFu+yJFvAwFbX9MEcBphy3E3OPAZhqbWD46Xa/x9NjQcfwd4cXb82bAQHJA9kvDOhDRy+3YDvqR71g4CAt7k5uX/jdNlc+f8xZAzvILuT8zQWfQ3iVftmO6DvxtgzlCDOw20cHrnv/NyCB/ew+FSttLB99LMzrTnA/w8QgJlXPWwktszdPeI4xarx4jI3TxfYk2cyzK8m9HuTh8wvP1F2AfUMVbkNQwOjeCmKc42yQuFdmG2IWRzzkWImysEr/Z6Wpsf9GmNG7kQlGBTrUrEBxitwOQ/yJADOFQ==; 20:bd09UplDCicDOmJdtbRslW3LvLVgcU2bRlbbsdl8iA0hreEX5UMzlpEaXcKkr1J7azfJ/ArDvEx2fzEAfFIwq6DZ9/JwD+xBKl0BwGsCBzcAChZ/kkAvArDpO4A+FqeyJVGOGUEWesTpqsSeRLOxwMDylz4kZx6TQpjE/tVK0FSVdLjJrielZN34XNlHTptKANgHiB981vHkNtYAbNTndAF5P5ZkFdDzk5zsz+tpa9EoopVNXbQB/QNXOgtYK3xX X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Mar 2017 15:17:26.0654 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1610 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The patch adds initial support required to integrate Secure Encrypted Virtualization (SEV) feature. ASID management: - Reserve asid range for SEV guest, SEV asid range is obtained through CPUID Fn8000_001f[ECX]. A non-SEV guest can use any asid outside the SEV asid range. - SEV guest must have asid value within asid range obtained through CPUID. - SEV guest must have the same asid for all vcpu's. A TLB flush is required if different vcpu for the same ASID is to be run on the same host CPU. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/kvm_host.h | 8 ++ arch/x86/kvm/svm.c | 189 +++++++++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 98 ++++++++++++++++++++ 3 files changed, 294 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 62651ad..fcc4710 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -719,6 +719,12 @@ struct kvm_hv { HV_REFERENCE_TSC_PAGE tsc_ref; }; +struct kvm_sev_info { + unsigned int handle; /* firmware handle */ + unsigned int asid; /* asid for this guest */ + int sev_fd; /* SEV device fd */ +}; + struct kvm_arch { unsigned int n_used_mmu_pages; unsigned int n_requested_mmu_pages; @@ -805,6 +811,8 @@ struct kvm_arch { bool x2apic_format; bool x2apic_broadcast_quirk_disabled; + + struct kvm_sev_info sev_info; }; struct kvm_vm_stat { diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 8d8fe62..fb63398 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -211,6 +212,9 @@ struct vcpu_svm { */ struct list_head ir_list; spinlock_t ir_list_lock; + + /* which host cpu was used for running this vcpu */ + bool last_cpuid; }; /* @@ -490,6 +494,64 @@ static inline bool gif_set(struct vcpu_svm *svm) return !!(svm->vcpu.arch.hflags & HF_GIF_MASK); } +/* Secure Encrypted Virtualization */ +static unsigned int max_sev_asid; +static unsigned long *sev_asid_bitmap; + +static bool kvm_sev_enabled(void) +{ + return max_sev_asid ? 1 : 0; +} + +static inline struct kvm_sev_info *sev_get_info(struct kvm *kvm) +{ + struct kvm_arch *vm_data = &kvm->arch; + + return &vm_data->sev_info; +} + +static unsigned int sev_get_handle(struct kvm *kvm) +{ + struct kvm_sev_info *sev_info = sev_get_info(kvm); + + return sev_info->handle; +} + +static inline int sev_guest(struct kvm *kvm) +{ + return sev_get_handle(kvm); +} + +static inline int sev_get_asid(struct kvm *kvm) +{ + struct kvm_sev_info *sev_info = sev_get_info(kvm); + + if (!sev_info) + return -EINVAL; + + return sev_info->asid; +} + +static inline int sev_get_fd(struct kvm *kvm) +{ + struct kvm_sev_info *sev_info = sev_get_info(kvm); + + if (!sev_info) + return -EINVAL; + + return sev_info->sev_fd; +} + +static inline void sev_set_asid(struct kvm *kvm, int asid) +{ + struct kvm_sev_info *sev_info = sev_get_info(kvm); + + if (!sev_info) + return; + + sev_info->asid = asid; +} + static unsigned long iopm_base; struct kvm_ldttss_desc { @@ -511,6 +573,8 @@ struct svm_cpu_data { struct kvm_ldttss_desc *tss_desc; struct page *save_area; + + struct vmcb **sev_vmcbs; /* index = sev_asid, value = vmcb pointer */ }; static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); @@ -764,7 +828,7 @@ static int svm_hardware_enable(void) sd->asid_generation = 1; sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; sd->next_asid = sd->max_asid + 1; - sd->min_asid = 1; + sd->min_asid = max_sev_asid + 1; native_store_gdt(&gdt_descr); gdt = (struct desc_struct *)gdt_descr.address; @@ -825,6 +889,7 @@ static void svm_cpu_uninit(int cpu) per_cpu(svm_data, raw_smp_processor_id()) = NULL; __free_page(sd->save_area); + kfree(sd->sev_vmcbs); kfree(sd); } @@ -842,6 +907,14 @@ static int svm_cpu_init(int cpu) if (!sd->save_area) goto err_1; + if (kvm_sev_enabled()) { + sd->sev_vmcbs = kmalloc((max_sev_asid + 1) * sizeof(void *), + GFP_KERNEL); + r = -ENOMEM; + if (!sd->sev_vmcbs) + goto err_1; + } + per_cpu(svm_data, cpu) = sd; return 0; @@ -1017,6 +1090,61 @@ static int avic_ga_log_notifier(u32 ga_tag) return 0; } +static __init void sev_hardware_setup(void) +{ + int ret, error, nguests; + struct sev_data_init *init; + struct sev_data_status *status; + + /* + * Get maximum number of encrypted guest supported: Fn8001_001F[ECX] + * Bit 31:0: Number of supported guest + */ + nguests = cpuid_ecx(0x8000001F); + if (!nguests) + return; + + init = kzalloc(sizeof(*init), GFP_KERNEL); + if (!init) + return; + + status = kzalloc(sizeof(*status), GFP_KERNEL); + if (!status) + goto err_1; + + /* Initialize SEV firmware */ + ret = sev_platform_init(init, &error); + if (ret) { + pr_err("SEV: PLATFORM_INIT ret=%d (%#x)\n", ret, error); + goto err_2; + } + + /* Initialize SEV ASID bitmap */ + sev_asid_bitmap = kcalloc(BITS_TO_LONGS(nguests), + sizeof(unsigned long), GFP_KERNEL); + if (IS_ERR(sev_asid_bitmap)) { + sev_platform_shutdown(&error); + goto err_2; + } + + /* Query the platform status and print API version */ + ret = sev_platform_status(status, &error); + if (ret) { + printk(KERN_ERR "SEV: PLATFORM_STATUS ret=%#x\n", error); + goto err_2; + } + + max_sev_asid = nguests; + + printk(KERN_INFO "kvm: SEV enabled\n"); + printk(KERN_INFO "SEV API: %d.%d\n", + status->api_major, status->api_minor); +err_2: + kfree(status); +err_1: + kfree(init); +} + static __init int svm_hardware_setup(void) { int cpu; @@ -1052,6 +1180,9 @@ static __init int svm_hardware_setup(void) kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE); } + if (boot_cpu_has(X86_FEATURE_SEV)) + sev_hardware_setup(); + for_each_possible_cpu(cpu) { r = svm_cpu_init(cpu); if (r) @@ -1094,10 +1225,25 @@ static __init int svm_hardware_setup(void) return r; } +static __exit void sev_hardware_unsetup(void) +{ + int ret, err; + + ret = sev_platform_shutdown(&err); + if (ret) + printk(KERN_ERR "failed to shutdown PSP rc=%d (%#0x10x)\n", + ret, err); + + kfree(sev_asid_bitmap); +} + static __exit void svm_hardware_unsetup(void) { int cpu; + if (kvm_sev_enabled()) + sev_hardware_unsetup(); + for_each_possible_cpu(cpu) svm_cpu_uninit(cpu); @@ -1157,6 +1303,11 @@ static void avic_init_vmcb(struct vcpu_svm *svm) svm->vcpu.arch.apicv_active = true; } +static void sev_init_vmcb(struct vcpu_svm *svm) +{ + svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE; +} + static void init_vmcb(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -1271,6 +1422,9 @@ static void init_vmcb(struct vcpu_svm *svm) if (avic) avic_init_vmcb(svm); + if (sev_guest(svm->vcpu.kvm)) + sev_init_vmcb(svm); + mark_all_dirty(svm->vmcb); enable_gif(svm); @@ -2084,6 +2238,11 @@ static int pf_interception(struct vcpu_svm *svm) default: error_code = svm->vmcb->control.exit_info_1; + /* In SEV mode, the guest physical address will have C-bit + * set. C-bit must be cleared before handling the fault. + */ + if (sev_guest(svm->vcpu.kvm)) + fault_address &= ~sme_me_mask; trace_kvm_page_fault(fault_address, error_code); if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu)) kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address); @@ -4258,12 +4417,40 @@ static void reload_tss(struct kvm_vcpu *vcpu) load_TR_desc(); } +static void pre_sev_run(struct vcpu_svm *svm) +{ + int asid = sev_get_asid(svm->vcpu.kvm); + int cpu = raw_smp_processor_id(); + struct svm_cpu_data *sd = per_cpu(svm_data, cpu); + + /* Assign the asid allocated for this SEV guest */ + svm->vmcb->control.asid = asid; + + /* Flush guest TLB: + * - when different VMCB for the same ASID is to be run on the + * same host CPU + * or + * - this VMCB was executed on different host cpu in previous VMRUNs. + */ + if (sd->sev_vmcbs[asid] != (void *)svm->vmcb || + svm->last_cpuid != cpu) + svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; + + svm->last_cpuid = cpu; + sd->sev_vmcbs[asid] = (void *)svm->vmcb; + + mark_dirty(svm->vmcb, VMCB_ASID); +} + static void pre_svm_run(struct vcpu_svm *svm) { int cpu = raw_smp_processor_id(); struct svm_cpu_data *sd = per_cpu(svm_data, cpu); + if (sev_guest(svm->vcpu.kvm)) + return pre_sev_run(svm); + /* FIXME: handle wraparound of asid_generation */ if (svm->asid_generation != sd->asid_generation) new_asid(svm, sd); diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index fef7d83..9df37a2 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1284,6 +1284,104 @@ struct kvm_s390_ucas_mapping { /* Memory Encryption Commands */ #define KVM_MEMORY_ENCRYPT_OP _IOWR(KVMIO, 0xb8, unsigned long) +/* Secure Encrypted Virtualization mode */ +enum sev_cmd_id { + /* Guest launch commands */ + KVM_SEV_LAUNCH_START = 0, + KVM_SEV_LAUNCH_UPDATE_DATA, + KVM_SEV_LAUNCH_MEASURE, + KVM_SEV_LAUNCH_FINISH, + /* Guest migration commands (outgoing) */ + KVM_SEV_SEND_START, + KVM_SEV_SEND_UPDATE_DATA, + KVM_SEV_SEND_FINISH, + /* Guest migration commands (incoming) */ + KVM_SEV_RECEIVE_START, + KVM_SEV_RECEIVE_UPDATE_DATA, + KVM_SEV_RECEIVE_FINISH, + /* Guest status and debug commands */ + KVM_SEV_GUEST_STATUS, + KVM_SEV_DBG_DECRYPT, + KVM_SEV_DBG_ENCRYPT, + + KVM_SEV_NR_MAX, +}; + +struct kvm_sev_cmd { + __u32 id; + __u64 data; + __u32 error; + __u32 sev_fd; +}; + +struct kvm_sev_launch_start { + __u32 handle; + __u32 policy; + __u64 dh_cert_data; + __u32 dh_cert_length; + __u64 session_data; + __u32 session_length; +}; + +struct kvm_sev_launch_update_data { + __u64 address; + __u32 length; +}; + +struct kvm_sev_launch_measure { + __u64 address; + __u32 length; +}; + +struct kvm_sev_send_start { + __u64 pdh_cert_data; + __u32 pdh_cert_length; + __u64 plat_cert_data; + __u32 plat_cert_length; + __u64 amd_cert_data; + __u32 amd_cert_length; + __u64 session_data; + __u32 session_length; +}; + +struct kvm_sev_send_update_data { + __u64 hdr_data; + __u32 hdr_length; + __u64 guest_address; + __u32 guest_length; + __u64 host_address; + __u32 host_length; +}; + +struct kvm_sev_receive_start { + __u32 handle; + __u64 pdh_cert_data; + __u32 pdh_cert_length; + __u64 session_data; + __u32 session_length; +}; + +struct kvm_sev_receive_update_data { + __u64 hdr_data; + __u32 hdr_length; + __u64 guest_address; + __u32 guest_length; + __u64 host_address; + __u32 host_length; +}; + +struct kvm_sev_guest_status { + __u32 handle; + __u32 policy; + __u32 state; +}; + +struct kvm_sev_dbg { + __u64 src_addr; + __u64 dst_addr; + __u32 length; +}; + #define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0) #define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1) #define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)