Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Kernel-based Virtual Machine driver for Linux |
| 4 | * |
| 5 | * AMD SVM-SEV support |
| 6 | * |
| 7 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kvm_types.h> |
| 11 | #include <linux/kvm_host.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/highmem.h> |
| 14 | #include <linux/psp-sev.h> |
Borislav Petkov | b2bce0a | 2020-04-11 18:09:27 +0200 | [diff] [blame] | 15 | #include <linux/pagemap.h> |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 16 | #include <linux/swap.h> |
Vipin Sharma | 7aef27f | 2021-03-29 21:42:06 -0700 | [diff] [blame] | 17 | #include <linux/misc_cgroup.h> |
Tom Lendacky | add5e2f | 2020-12-10 11:09:40 -0600 | [diff] [blame] | 18 | #include <linux/processor.h> |
Tom Lendacky | d523ab6b | 2020-12-10 11:09:48 -0600 | [diff] [blame] | 19 | #include <linux/trace_events.h> |
Tom Lendacky | 8613777 | 2020-12-10 11:10:07 -0600 | [diff] [blame] | 20 | #include <asm/fpu/internal.h> |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 21 | |
Dave Hansen | 784a4661 | 2021-06-23 14:02:05 +0200 | [diff] [blame] | 22 | #include <asm/pkru.h> |
Tom Lendacky | 8640ca5 | 2020-12-15 12:44:07 -0500 | [diff] [blame] | 23 | #include <asm/trapnr.h> |
| 24 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 25 | #include "x86.h" |
| 26 | #include "svm.h" |
Sean Christopherson | 35a7831 | 2020-12-30 16:27:00 -0800 | [diff] [blame] | 27 | #include "svm_ops.h" |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 28 | #include "cpuid.h" |
Tom Lendacky | d523ab6b | 2020-12-10 11:09:48 -0600 | [diff] [blame] | 29 | #include "trace.h" |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 30 | |
Vipin Sharma | 7aef27f | 2021-03-29 21:42:06 -0700 | [diff] [blame] | 31 | #ifndef CONFIG_KVM_AMD_SEV |
| 32 | /* |
| 33 | * When this config is not defined, SEV feature is not supported and APIs in |
| 34 | * this file are not used but this file still gets compiled into the KVM AMD |
| 35 | * module. |
| 36 | * |
| 37 | * We will not have MISC_CG_RES_SEV and MISC_CG_RES_SEV_ES entries in the enum |
| 38 | * misc_res_type {} defined in linux/misc_cgroup.h. |
| 39 | * |
| 40 | * Below macros allow compilation to succeed. |
| 41 | */ |
| 42 | #define MISC_CG_RES_SEV MISC_CG_RES_TYPES |
| 43 | #define MISC_CG_RES_SEV_ES MISC_CG_RES_TYPES |
| 44 | #endif |
| 45 | |
Sean Christopherson | a479c33 | 2021-04-21 19:11:18 -0700 | [diff] [blame] | 46 | #ifdef CONFIG_KVM_AMD_SEV |
Sean Christopherson | e8126bd | 2021-04-21 19:11:14 -0700 | [diff] [blame] | 47 | /* enable/disable SEV support */ |
Sean Christopherson | 6c2c7bf | 2021-04-21 19:11:19 -0700 | [diff] [blame] | 48 | static bool sev_enabled = true; |
Sean Christopherson | 8d364a0 | 2021-04-21 19:11:17 -0700 | [diff] [blame] | 49 | module_param_named(sev, sev_enabled, bool, 0444); |
Sean Christopherson | e8126bd | 2021-04-21 19:11:14 -0700 | [diff] [blame] | 50 | |
| 51 | /* enable/disable SEV-ES support */ |
Sean Christopherson | 6c2c7bf | 2021-04-21 19:11:19 -0700 | [diff] [blame] | 52 | static bool sev_es_enabled = true; |
Sean Christopherson | 8d364a0 | 2021-04-21 19:11:17 -0700 | [diff] [blame] | 53 | module_param_named(sev_es, sev_es_enabled, bool, 0444); |
Sean Christopherson | a479c33 | 2021-04-21 19:11:18 -0700 | [diff] [blame] | 54 | #else |
| 55 | #define sev_enabled false |
| 56 | #define sev_es_enabled false |
| 57 | #endif /* CONFIG_KVM_AMD_SEV */ |
Sean Christopherson | e8126bd | 2021-04-21 19:11:14 -0700 | [diff] [blame] | 58 | |
Tom Lendacky | 1edc145 | 2020-12-10 11:09:49 -0600 | [diff] [blame] | 59 | static u8 sev_enc_bit; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 60 | static DECLARE_RWSEM(sev_deactivate_lock); |
| 61 | static DEFINE_MUTEX(sev_bitmap_lock); |
| 62 | unsigned int max_sev_asid; |
| 63 | static unsigned int min_sev_asid; |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 64 | static unsigned long sev_me_mask; |
Mingwei Zhang | bb2baeb | 2021-08-02 11:09:03 -0700 | [diff] [blame] | 65 | static unsigned int nr_asids; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 66 | static unsigned long *sev_asid_bitmap; |
| 67 | static unsigned long *sev_reclaim_asid_bitmap; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 68 | |
| 69 | struct enc_region { |
| 70 | struct list_head list; |
| 71 | unsigned long npages; |
| 72 | struct page **pages; |
| 73 | unsigned long uaddr; |
| 74 | unsigned long size; |
| 75 | }; |
| 76 | |
Sean Christopherson | 469bb32 | 2021-04-21 19:11:25 -0700 | [diff] [blame] | 77 | /* Called with the sev_bitmap_lock held, or on shutdown */ |
| 78 | static int sev_flush_asids(int min_asid, int max_asid) |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 79 | { |
Mingwei Zhang | bb2baeb | 2021-08-02 11:09:03 -0700 | [diff] [blame] | 80 | int ret, asid, error = 0; |
Sean Christopherson | 469bb32 | 2021-04-21 19:11:25 -0700 | [diff] [blame] | 81 | |
| 82 | /* Check if there are any ASIDs to reclaim before performing a flush */ |
Mingwei Zhang | bb2baeb | 2021-08-02 11:09:03 -0700 | [diff] [blame] | 83 | asid = find_next_bit(sev_reclaim_asid_bitmap, nr_asids, min_asid); |
| 84 | if (asid > max_asid) |
Sean Christopherson | 469bb32 | 2021-04-21 19:11:25 -0700 | [diff] [blame] | 85 | return -EBUSY; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail, |
| 89 | * so it must be guarded. |
| 90 | */ |
| 91 | down_write(&sev_deactivate_lock); |
| 92 | |
| 93 | wbinvd_on_all_cpus(); |
| 94 | ret = sev_guest_df_flush(&error); |
| 95 | |
| 96 | up_write(&sev_deactivate_lock); |
| 97 | |
| 98 | if (ret) |
| 99 | pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error); |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
Nathan Tempelman | 54526d1 | 2021-04-08 22:32:14 +0000 | [diff] [blame] | 104 | static inline bool is_mirroring_enc_context(struct kvm *kvm) |
| 105 | { |
| 106 | return !!to_kvm_svm(kvm)->sev_info.enc_context_owner; |
| 107 | } |
| 108 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 109 | /* Must be called with the sev_bitmap_lock held */ |
Tom Lendacky | 80675b3 | 2020-12-10 11:10:05 -0600 | [diff] [blame] | 110 | static bool __sev_recycle_asids(int min_asid, int max_asid) |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 111 | { |
Sean Christopherson | 469bb32 | 2021-04-21 19:11:25 -0700 | [diff] [blame] | 112 | if (sev_flush_asids(min_asid, max_asid)) |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 113 | return false; |
| 114 | |
Tom Lendacky | 80675b3 | 2020-12-10 11:10:05 -0600 | [diff] [blame] | 115 | /* The flush process will flush all reclaimable SEV and SEV-ES ASIDs */ |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 116 | bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap, |
Mingwei Zhang | bb2baeb | 2021-08-02 11:09:03 -0700 | [diff] [blame] | 117 | nr_asids); |
| 118 | bitmap_zero(sev_reclaim_asid_bitmap, nr_asids); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 119 | |
| 120 | return true; |
| 121 | } |
| 122 | |
Tom Lendacky | 80675b3 | 2020-12-10 11:10:05 -0600 | [diff] [blame] | 123 | static int sev_asid_new(struct kvm_sev_info *sev) |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 124 | { |
Mingwei Zhang | bb2baeb | 2021-08-02 11:09:03 -0700 | [diff] [blame] | 125 | int asid, min_asid, max_asid, ret; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 126 | bool retry = true; |
Vipin Sharma | 7aef27f | 2021-03-29 21:42:06 -0700 | [diff] [blame] | 127 | enum misc_res_type type; |
| 128 | |
| 129 | type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV; |
| 130 | WARN_ON(sev->misc_cg); |
| 131 | sev->misc_cg = get_current_misc_cg(); |
| 132 | ret = misc_cg_try_charge(type, sev->misc_cg, 1); |
| 133 | if (ret) { |
| 134 | put_misc_cg(sev->misc_cg); |
| 135 | sev->misc_cg = NULL; |
| 136 | return ret; |
| 137 | } |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 138 | |
| 139 | mutex_lock(&sev_bitmap_lock); |
| 140 | |
| 141 | /* |
Tom Lendacky | 80675b3 | 2020-12-10 11:10:05 -0600 | [diff] [blame] | 142 | * SEV-enabled guests must use asid from min_sev_asid to max_sev_asid. |
| 143 | * SEV-ES-enabled guest can use from 1 to min_sev_asid - 1. |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 144 | */ |
Mingwei Zhang | bb2baeb | 2021-08-02 11:09:03 -0700 | [diff] [blame] | 145 | min_asid = sev->es_active ? 1 : min_sev_asid; |
Tom Lendacky | 80675b3 | 2020-12-10 11:10:05 -0600 | [diff] [blame] | 146 | max_asid = sev->es_active ? min_sev_asid - 1 : max_sev_asid; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 147 | again: |
Mingwei Zhang | bb2baeb | 2021-08-02 11:09:03 -0700 | [diff] [blame] | 148 | asid = find_next_zero_bit(sev_asid_bitmap, max_asid + 1, min_asid); |
| 149 | if (asid > max_asid) { |
Tom Lendacky | 80675b3 | 2020-12-10 11:10:05 -0600 | [diff] [blame] | 150 | if (retry && __sev_recycle_asids(min_asid, max_asid)) { |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 151 | retry = false; |
| 152 | goto again; |
| 153 | } |
| 154 | mutex_unlock(&sev_bitmap_lock); |
Vipin Sharma | 7aef27f | 2021-03-29 21:42:06 -0700 | [diff] [blame] | 155 | ret = -EBUSY; |
| 156 | goto e_uncharge; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Mingwei Zhang | bb2baeb | 2021-08-02 11:09:03 -0700 | [diff] [blame] | 159 | __set_bit(asid, sev_asid_bitmap); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 160 | |
| 161 | mutex_unlock(&sev_bitmap_lock); |
| 162 | |
Mingwei Zhang | bb2baeb | 2021-08-02 11:09:03 -0700 | [diff] [blame] | 163 | return asid; |
Vipin Sharma | 7aef27f | 2021-03-29 21:42:06 -0700 | [diff] [blame] | 164 | e_uncharge: |
| 165 | misc_cg_uncharge(type, sev->misc_cg, 1); |
| 166 | put_misc_cg(sev->misc_cg); |
| 167 | sev->misc_cg = NULL; |
| 168 | return ret; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static int sev_get_asid(struct kvm *kvm) |
| 172 | { |
| 173 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 174 | |
| 175 | return sev->asid; |
| 176 | } |
| 177 | |
Vipin Sharma | 7aef27f | 2021-03-29 21:42:06 -0700 | [diff] [blame] | 178 | static void sev_asid_free(struct kvm_sev_info *sev) |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 179 | { |
| 180 | struct svm_cpu_data *sd; |
Mingwei Zhang | bb2baeb | 2021-08-02 11:09:03 -0700 | [diff] [blame] | 181 | int cpu; |
Vipin Sharma | 7aef27f | 2021-03-29 21:42:06 -0700 | [diff] [blame] | 182 | enum misc_res_type type; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 183 | |
| 184 | mutex_lock(&sev_bitmap_lock); |
| 185 | |
Mingwei Zhang | bb2baeb | 2021-08-02 11:09:03 -0700 | [diff] [blame] | 186 | __set_bit(sev->asid, sev_reclaim_asid_bitmap); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 187 | |
| 188 | for_each_possible_cpu(cpu) { |
| 189 | sd = per_cpu(svm_data, cpu); |
Sean Christopherson | 179c6c2 | 2021-08-03 09:27:46 -0700 | [diff] [blame] | 190 | sd->sev_vmcbs[sev->asid] = NULL; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | mutex_unlock(&sev_bitmap_lock); |
Vipin Sharma | 7aef27f | 2021-03-29 21:42:06 -0700 | [diff] [blame] | 194 | |
| 195 | type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV; |
| 196 | misc_cg_uncharge(type, sev->misc_cg, 1); |
| 197 | put_misc_cg(sev->misc_cg); |
| 198 | sev->misc_cg = NULL; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Alper Gun | 934002c | 2021-06-10 17:46:04 +0000 | [diff] [blame] | 201 | static void sev_decommission(unsigned int handle) |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 202 | { |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 203 | struct sev_data_decommission decommission; |
Alper Gun | 934002c | 2021-06-10 17:46:04 +0000 | [diff] [blame] | 204 | |
| 205 | if (!handle) |
| 206 | return; |
| 207 | |
| 208 | decommission.handle = handle; |
| 209 | sev_guest_decommission(&decommission, NULL); |
| 210 | } |
| 211 | |
| 212 | static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) |
| 213 | { |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 214 | struct sev_data_deactivate deactivate; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 215 | |
| 216 | if (!handle) |
| 217 | return; |
| 218 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 219 | deactivate.handle = handle; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 220 | |
| 221 | /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */ |
| 222 | down_read(&sev_deactivate_lock); |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 223 | sev_guest_deactivate(&deactivate, NULL); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 224 | up_read(&sev_deactivate_lock); |
| 225 | |
Alper Gun | 934002c | 2021-06-10 17:46:04 +0000 | [diff] [blame] | 226 | sev_decommission(handle); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 230 | { |
| 231 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Sean Christopherson | 9fa1521 | 2021-03-30 20:19:35 -0700 | [diff] [blame] | 232 | bool es_active = argp->id == KVM_SEV_ES_INIT; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 233 | int asid, ret; |
| 234 | |
Sean Christopherson | 8727906 | 2021-03-30 20:19:36 -0700 | [diff] [blame] | 235 | if (kvm->created_vcpus) |
| 236 | return -EINVAL; |
| 237 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 238 | ret = -EBUSY; |
| 239 | if (unlikely(sev->active)) |
| 240 | return ret; |
| 241 | |
Paolo Bonzini | fd49e8e | 2021-04-22 02:39:48 -0400 | [diff] [blame] | 242 | sev->es_active = es_active; |
Tom Lendacky | 80675b3 | 2020-12-10 11:10:05 -0600 | [diff] [blame] | 243 | asid = sev_asid_new(sev); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 244 | if (asid < 0) |
Paolo Bonzini | fd49e8e | 2021-04-22 02:39:48 -0400 | [diff] [blame] | 245 | goto e_no_asid; |
Vipin Sharma | 7aef27f | 2021-03-29 21:42:06 -0700 | [diff] [blame] | 246 | sev->asid = asid; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 247 | |
| 248 | ret = sev_platform_init(&argp->error); |
| 249 | if (ret) |
| 250 | goto e_free; |
| 251 | |
| 252 | sev->active = true; |
| 253 | sev->asid = asid; |
| 254 | INIT_LIST_HEAD(&sev->regions_list); |
| 255 | |
| 256 | return 0; |
| 257 | |
| 258 | e_free: |
Vipin Sharma | 7aef27f | 2021-03-29 21:42:06 -0700 | [diff] [blame] | 259 | sev_asid_free(sev); |
| 260 | sev->asid = 0; |
Paolo Bonzini | fd49e8e | 2021-04-22 02:39:48 -0400 | [diff] [blame] | 261 | e_no_asid: |
| 262 | sev->es_active = false; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error) |
| 267 | { |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 268 | struct sev_data_activate activate; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 269 | int asid = sev_get_asid(kvm); |
| 270 | int ret; |
| 271 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 272 | /* activate ASID on the given handle */ |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 273 | activate.handle = handle; |
| 274 | activate.asid = asid; |
| 275 | ret = sev_guest_activate(&activate, error); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 276 | |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | static int __sev_issue_cmd(int fd, int id, void *data, int *error) |
| 281 | { |
| 282 | struct fd f; |
| 283 | int ret; |
| 284 | |
| 285 | f = fdget(fd); |
| 286 | if (!f.file) |
| 287 | return -EBADF; |
| 288 | |
| 289 | ret = sev_issue_cmd_external_user(f.file, id, data, error); |
| 290 | |
| 291 | fdput(f); |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error) |
| 296 | { |
| 297 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 298 | |
| 299 | return __sev_issue_cmd(sev->fd, id, data, error); |
| 300 | } |
| 301 | |
| 302 | static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 303 | { |
| 304 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 305 | struct sev_data_launch_start start; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 306 | struct kvm_sev_launch_start params; |
| 307 | void *dh_blob, *session_blob; |
| 308 | int *error = &argp->error; |
| 309 | int ret; |
| 310 | |
| 311 | if (!sev_guest(kvm)) |
| 312 | return -ENOTTY; |
| 313 | |
| 314 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 315 | return -EFAULT; |
| 316 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 317 | memset(&start, 0, sizeof(start)); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 318 | |
| 319 | dh_blob = NULL; |
| 320 | if (params.dh_uaddr) { |
| 321 | dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len); |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 322 | if (IS_ERR(dh_blob)) |
| 323 | return PTR_ERR(dh_blob); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 324 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 325 | start.dh_cert_address = __sme_set(__pa(dh_blob)); |
| 326 | start.dh_cert_len = params.dh_len; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | session_blob = NULL; |
| 330 | if (params.session_uaddr) { |
| 331 | session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len); |
| 332 | if (IS_ERR(session_blob)) { |
| 333 | ret = PTR_ERR(session_blob); |
| 334 | goto e_free_dh; |
| 335 | } |
| 336 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 337 | start.session_address = __sme_set(__pa(session_blob)); |
| 338 | start.session_len = params.session_len; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 341 | start.handle = params.handle; |
| 342 | start.policy = params.policy; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 343 | |
| 344 | /* create memory encryption context */ |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 345 | ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, &start, error); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 346 | if (ret) |
| 347 | goto e_free_session; |
| 348 | |
| 349 | /* Bind ASID to this guest */ |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 350 | ret = sev_bind_asid(kvm, start.handle, error); |
Alper Gun | 934002c | 2021-06-10 17:46:04 +0000 | [diff] [blame] | 351 | if (ret) { |
| 352 | sev_decommission(start.handle); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 353 | goto e_free_session; |
Alper Gun | 934002c | 2021-06-10 17:46:04 +0000 | [diff] [blame] | 354 | } |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 355 | |
| 356 | /* return handle to userspace */ |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 357 | params.handle = start.handle; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 358 | if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) { |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 359 | sev_unbind_asid(kvm, start.handle); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 360 | ret = -EFAULT; |
| 361 | goto e_free_session; |
| 362 | } |
| 363 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 364 | sev->handle = start.handle; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 365 | sev->fd = argp->sev_fd; |
| 366 | |
| 367 | e_free_session: |
| 368 | kfree(session_blob); |
| 369 | e_free_dh: |
| 370 | kfree(dh_blob); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr, |
| 375 | unsigned long ulen, unsigned long *n, |
| 376 | int write) |
| 377 | { |
| 378 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
John Hubbard | 78824fa | 2020-05-25 23:22:06 -0700 | [diff] [blame] | 379 | unsigned long npages, size; |
| 380 | int npinned; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 381 | unsigned long locked, lock_limit; |
| 382 | struct page **pages; |
| 383 | unsigned long first, last; |
Dan Carpenter | ff2bd9f | 2020-07-14 17:23:51 +0300 | [diff] [blame] | 384 | int ret; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 385 | |
Peter Gonda | 19a23da | 2021-01-27 08:15:24 -0800 | [diff] [blame] | 386 | lockdep_assert_held(&kvm->lock); |
| 387 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 388 | if (ulen == 0 || uaddr + ulen < uaddr) |
Paolo Bonzini | a8d908b | 2020-06-23 05:12:24 -0400 | [diff] [blame] | 389 | return ERR_PTR(-EINVAL); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 390 | |
| 391 | /* Calculate number of pages. */ |
| 392 | first = (uaddr & PAGE_MASK) >> PAGE_SHIFT; |
| 393 | last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT; |
| 394 | npages = (last - first + 1); |
| 395 | |
| 396 | locked = sev->pages_locked + npages; |
| 397 | lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 398 | if (locked > lock_limit && !capable(CAP_IPC_LOCK)) { |
| 399 | pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit); |
Paolo Bonzini | a8d908b | 2020-06-23 05:12:24 -0400 | [diff] [blame] | 400 | return ERR_PTR(-ENOMEM); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 401 | } |
| 402 | |
John Hubbard | 78824fa | 2020-05-25 23:22:06 -0700 | [diff] [blame] | 403 | if (WARN_ON_ONCE(npages > INT_MAX)) |
Paolo Bonzini | a8d908b | 2020-06-23 05:12:24 -0400 | [diff] [blame] | 404 | return ERR_PTR(-EINVAL); |
John Hubbard | 78824fa | 2020-05-25 23:22:06 -0700 | [diff] [blame] | 405 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 406 | /* Avoid using vmalloc for smaller buffers. */ |
| 407 | size = npages * sizeof(struct page *); |
| 408 | if (size > PAGE_SIZE) |
Christoph Hellwig | 88dca4c | 2020-06-01 21:51:40 -0700 | [diff] [blame] | 409 | pages = __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 410 | else |
| 411 | pages = kmalloc(size, GFP_KERNEL_ACCOUNT); |
| 412 | |
| 413 | if (!pages) |
Paolo Bonzini | a8d908b | 2020-06-23 05:12:24 -0400 | [diff] [blame] | 414 | return ERR_PTR(-ENOMEM); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 415 | |
| 416 | /* Pin the user virtual address. */ |
John Hubbard | dc42c8a | 2020-05-25 23:22:07 -0700 | [diff] [blame] | 417 | npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 418 | if (npinned != npages) { |
| 419 | pr_err("SEV: Failure locking %lu pages.\n", npages); |
Dan Carpenter | ff2bd9f | 2020-07-14 17:23:51 +0300 | [diff] [blame] | 420 | ret = -ENOMEM; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 421 | goto err; |
| 422 | } |
| 423 | |
| 424 | *n = npages; |
| 425 | sev->pages_locked = locked; |
| 426 | |
| 427 | return pages; |
| 428 | |
| 429 | err: |
Dan Carpenter | ff2bd9f | 2020-07-14 17:23:51 +0300 | [diff] [blame] | 430 | if (npinned > 0) |
John Hubbard | dc42c8a | 2020-05-25 23:22:07 -0700 | [diff] [blame] | 431 | unpin_user_pages(pages, npinned); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 432 | |
| 433 | kvfree(pages); |
Dan Carpenter | ff2bd9f | 2020-07-14 17:23:51 +0300 | [diff] [blame] | 434 | return ERR_PTR(ret); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static void sev_unpin_memory(struct kvm *kvm, struct page **pages, |
| 438 | unsigned long npages) |
| 439 | { |
| 440 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 441 | |
John Hubbard | dc42c8a | 2020-05-25 23:22:07 -0700 | [diff] [blame] | 442 | unpin_user_pages(pages, npages); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 443 | kvfree(pages); |
| 444 | sev->pages_locked -= npages; |
| 445 | } |
| 446 | |
| 447 | static void sev_clflush_pages(struct page *pages[], unsigned long npages) |
| 448 | { |
| 449 | uint8_t *page_virtual; |
| 450 | unsigned long i; |
| 451 | |
Krish Sadhukhan | e1ebb2b | 2020-09-17 21:20:38 +0000 | [diff] [blame] | 452 | if (this_cpu_has(X86_FEATURE_SME_COHERENT) || npages == 0 || |
| 453 | pages == NULL) |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 454 | return; |
| 455 | |
| 456 | for (i = 0; i < npages; i++) { |
| 457 | page_virtual = kmap_atomic(pages[i]); |
| 458 | clflush_cache_range(page_virtual, PAGE_SIZE); |
| 459 | kunmap_atomic(page_virtual); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | static unsigned long get_num_contig_pages(unsigned long idx, |
| 464 | struct page **inpages, unsigned long npages) |
| 465 | { |
| 466 | unsigned long paddr, next_paddr; |
| 467 | unsigned long i = idx + 1, pages = 1; |
| 468 | |
| 469 | /* find the number of contiguous pages starting from idx */ |
| 470 | paddr = __sme_page_pa(inpages[idx]); |
| 471 | while (i < npages) { |
| 472 | next_paddr = __sme_page_pa(inpages[i++]); |
| 473 | if ((paddr + PAGE_SIZE) == next_paddr) { |
| 474 | pages++; |
| 475 | paddr = next_paddr; |
| 476 | continue; |
| 477 | } |
| 478 | break; |
| 479 | } |
| 480 | |
| 481 | return pages; |
| 482 | } |
| 483 | |
| 484 | static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 485 | { |
| 486 | unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i; |
| 487 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 488 | struct kvm_sev_launch_update_data params; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 489 | struct sev_data_launch_update_data data; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 490 | struct page **inpages; |
| 491 | int ret; |
| 492 | |
| 493 | if (!sev_guest(kvm)) |
| 494 | return -ENOTTY; |
| 495 | |
| 496 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 497 | return -EFAULT; |
| 498 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 499 | vaddr = params.uaddr; |
| 500 | size = params.len; |
| 501 | vaddr_end = vaddr + size; |
| 502 | |
| 503 | /* Lock the user memory. */ |
| 504 | inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1); |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 505 | if (IS_ERR(inpages)) |
| 506 | return PTR_ERR(inpages); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 507 | |
| 508 | /* |
Paolo Bonzini | 14e3dd8 | 2020-09-23 13:01:33 -0400 | [diff] [blame] | 509 | * Flush (on non-coherent CPUs) before LAUNCH_UPDATE encrypts pages in |
| 510 | * place; the cache may contain the data that was written unencrypted. |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 511 | */ |
| 512 | sev_clflush_pages(inpages, npages); |
| 513 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 514 | data.reserved = 0; |
| 515 | data.handle = sev->handle; |
| 516 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 517 | for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) { |
| 518 | int offset, len; |
| 519 | |
| 520 | /* |
| 521 | * If the user buffer is not page-aligned, calculate the offset |
| 522 | * within the page. |
| 523 | */ |
| 524 | offset = vaddr & (PAGE_SIZE - 1); |
| 525 | |
| 526 | /* Calculate the number of pages that can be encrypted in one go. */ |
| 527 | pages = get_num_contig_pages(i, inpages, npages); |
| 528 | |
| 529 | len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size); |
| 530 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 531 | data.len = len; |
| 532 | data.address = __sme_page_pa(inpages[i]) + offset; |
| 533 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, &data, &argp->error); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 534 | if (ret) |
| 535 | goto e_unpin; |
| 536 | |
| 537 | size -= len; |
| 538 | next_vaddr = vaddr + len; |
| 539 | } |
| 540 | |
| 541 | e_unpin: |
| 542 | /* content of memory is updated, mark pages dirty */ |
| 543 | for (i = 0; i < npages; i++) { |
| 544 | set_page_dirty_lock(inpages[i]); |
| 545 | mark_page_accessed(inpages[i]); |
| 546 | } |
| 547 | /* unlock the user pages */ |
| 548 | sev_unpin_memory(kvm, inpages, npages); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 549 | return ret; |
| 550 | } |
| 551 | |
Tom Lendacky | ad73109 | 2020-12-10 11:10:09 -0600 | [diff] [blame] | 552 | static int sev_es_sync_vmsa(struct vcpu_svm *svm) |
| 553 | { |
| 554 | struct vmcb_save_area *save = &svm->vmcb->save; |
| 555 | |
| 556 | /* Check some debug related fields before encrypting the VMSA */ |
| 557 | if (svm->vcpu.guest_debug || (save->dr7 & ~DR7_FIXED_1)) |
| 558 | return -EINVAL; |
| 559 | |
| 560 | /* Sync registgers */ |
| 561 | save->rax = svm->vcpu.arch.regs[VCPU_REGS_RAX]; |
| 562 | save->rbx = svm->vcpu.arch.regs[VCPU_REGS_RBX]; |
| 563 | save->rcx = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
| 564 | save->rdx = svm->vcpu.arch.regs[VCPU_REGS_RDX]; |
| 565 | save->rsp = svm->vcpu.arch.regs[VCPU_REGS_RSP]; |
| 566 | save->rbp = svm->vcpu.arch.regs[VCPU_REGS_RBP]; |
| 567 | save->rsi = svm->vcpu.arch.regs[VCPU_REGS_RSI]; |
| 568 | save->rdi = svm->vcpu.arch.regs[VCPU_REGS_RDI]; |
Paolo Bonzini | d45f89f | 2020-12-16 13:08:21 -0500 | [diff] [blame] | 569 | #ifdef CONFIG_X86_64 |
Tom Lendacky | ad73109 | 2020-12-10 11:10:09 -0600 | [diff] [blame] | 570 | save->r8 = svm->vcpu.arch.regs[VCPU_REGS_R8]; |
| 571 | save->r9 = svm->vcpu.arch.regs[VCPU_REGS_R9]; |
| 572 | save->r10 = svm->vcpu.arch.regs[VCPU_REGS_R10]; |
| 573 | save->r11 = svm->vcpu.arch.regs[VCPU_REGS_R11]; |
| 574 | save->r12 = svm->vcpu.arch.regs[VCPU_REGS_R12]; |
| 575 | save->r13 = svm->vcpu.arch.regs[VCPU_REGS_R13]; |
| 576 | save->r14 = svm->vcpu.arch.regs[VCPU_REGS_R14]; |
| 577 | save->r15 = svm->vcpu.arch.regs[VCPU_REGS_R15]; |
Paolo Bonzini | d45f89f | 2020-12-16 13:08:21 -0500 | [diff] [blame] | 578 | #endif |
Tom Lendacky | ad73109 | 2020-12-10 11:10:09 -0600 | [diff] [blame] | 579 | save->rip = svm->vcpu.arch.regs[VCPU_REGS_RIP]; |
| 580 | |
| 581 | /* Sync some non-GPR registers before encrypting */ |
| 582 | save->xcr0 = svm->vcpu.arch.xcr0; |
| 583 | save->pkru = svm->vcpu.arch.pkru; |
| 584 | save->xss = svm->vcpu.arch.ia32_xss; |
Sean Christopherson | d0f9f82 | 2021-07-13 09:33:10 -0700 | [diff] [blame] | 585 | save->dr6 = svm->vcpu.arch.dr6; |
Tom Lendacky | ad73109 | 2020-12-10 11:10:09 -0600 | [diff] [blame] | 586 | |
| 587 | /* |
| 588 | * SEV-ES will use a VMSA that is pointed to by the VMCB, not |
| 589 | * the traditional VMSA that is part of the VMCB. Copy the |
| 590 | * traditional VMSA as it has been built so far (in prep |
| 591 | * for LAUNCH_UPDATE_VMSA) to be the initial SEV-ES state. |
| 592 | */ |
| 593 | memcpy(svm->vmsa, save, sizeof(*save)); |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
Peter Gonda | bb18a67 | 2021-09-15 10:17:55 -0700 | [diff] [blame] | 598 | static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu, |
| 599 | int *error) |
| 600 | { |
| 601 | struct sev_data_launch_update_vmsa vmsa; |
| 602 | struct vcpu_svm *svm = to_svm(vcpu); |
| 603 | int ret; |
| 604 | |
| 605 | /* Perform some pre-encryption checks against the VMSA */ |
| 606 | ret = sev_es_sync_vmsa(svm); |
| 607 | if (ret) |
| 608 | return ret; |
| 609 | |
| 610 | /* |
| 611 | * The LAUNCH_UPDATE_VMSA command will perform in-place encryption of |
| 612 | * the VMSA memory content (i.e it will write the same memory region |
| 613 | * with the guest's key), so invalidate it first. |
| 614 | */ |
| 615 | clflush_cache_range(svm->vmsa, PAGE_SIZE); |
| 616 | |
| 617 | vmsa.reserved = 0; |
| 618 | vmsa.handle = to_kvm_svm(kvm)->sev_info.handle; |
| 619 | vmsa.address = __sme_pa(svm->vmsa); |
| 620 | vmsa.len = PAGE_SIZE; |
| 621 | return sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_VMSA, &vmsa, error); |
| 622 | } |
| 623 | |
Tom Lendacky | ad73109 | 2020-12-10 11:10:09 -0600 | [diff] [blame] | 624 | static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 625 | { |
Sean Christopherson | c36b16d | 2021-03-30 20:19:34 -0700 | [diff] [blame] | 626 | struct kvm_vcpu *vcpu; |
Tom Lendacky | ad73109 | 2020-12-10 11:10:09 -0600 | [diff] [blame] | 627 | int i, ret; |
| 628 | |
| 629 | if (!sev_es_guest(kvm)) |
| 630 | return -ENOTTY; |
| 631 | |
Sean Christopherson | c36b16d | 2021-03-30 20:19:34 -0700 | [diff] [blame] | 632 | kvm_for_each_vcpu(i, vcpu, kvm) { |
Peter Gonda | bb18a67 | 2021-09-15 10:17:55 -0700 | [diff] [blame] | 633 | ret = mutex_lock_killable(&vcpu->mutex); |
Tom Lendacky | ad73109 | 2020-12-10 11:10:09 -0600 | [diff] [blame] | 634 | if (ret) |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 635 | return ret; |
Tom Lendacky | ad73109 | 2020-12-10 11:10:09 -0600 | [diff] [blame] | 636 | |
Peter Gonda | bb18a67 | 2021-09-15 10:17:55 -0700 | [diff] [blame] | 637 | ret = __sev_launch_update_vmsa(kvm, vcpu, &argp->error); |
Tom Lendacky | ad73109 | 2020-12-10 11:10:09 -0600 | [diff] [blame] | 638 | |
Peter Gonda | bb18a67 | 2021-09-15 10:17:55 -0700 | [diff] [blame] | 639 | mutex_unlock(&vcpu->mutex); |
Tom Lendacky | ad73109 | 2020-12-10 11:10:09 -0600 | [diff] [blame] | 640 | if (ret) |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 641 | return ret; |
Tom Lendacky | ad73109 | 2020-12-10 11:10:09 -0600 | [diff] [blame] | 642 | } |
| 643 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 644 | return 0; |
Tom Lendacky | ad73109 | 2020-12-10 11:10:09 -0600 | [diff] [blame] | 645 | } |
| 646 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 647 | static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 648 | { |
| 649 | void __user *measure = (void __user *)(uintptr_t)argp->data; |
| 650 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 651 | struct sev_data_launch_measure data; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 652 | struct kvm_sev_launch_measure params; |
| 653 | void __user *p = NULL; |
| 654 | void *blob = NULL; |
| 655 | int ret; |
| 656 | |
| 657 | if (!sev_guest(kvm)) |
| 658 | return -ENOTTY; |
| 659 | |
| 660 | if (copy_from_user(¶ms, measure, sizeof(params))) |
| 661 | return -EFAULT; |
| 662 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 663 | memset(&data, 0, sizeof(data)); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 664 | |
| 665 | /* User wants to query the blob length */ |
| 666 | if (!params.len) |
| 667 | goto cmd; |
| 668 | |
| 669 | p = (void __user *)(uintptr_t)params.uaddr; |
| 670 | if (p) { |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 671 | if (params.len > SEV_FW_BLOB_MAX_SIZE) |
| 672 | return -EINVAL; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 673 | |
Sean Christopherson | eba04b2 | 2021-03-30 19:30:25 -0700 | [diff] [blame] | 674 | blob = kmalloc(params.len, GFP_KERNEL_ACCOUNT); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 675 | if (!blob) |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 676 | return -ENOMEM; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 677 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 678 | data.address = __psp_pa(blob); |
| 679 | data.len = params.len; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | cmd: |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 683 | data.handle = sev->handle; |
| 684 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, &data, &argp->error); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 685 | |
| 686 | /* |
| 687 | * If we query the session length, FW responded with expected data. |
| 688 | */ |
| 689 | if (!params.len) |
| 690 | goto done; |
| 691 | |
| 692 | if (ret) |
| 693 | goto e_free_blob; |
| 694 | |
| 695 | if (blob) { |
| 696 | if (copy_to_user(p, blob, params.len)) |
| 697 | ret = -EFAULT; |
| 698 | } |
| 699 | |
| 700 | done: |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 701 | params.len = data.len; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 702 | if (copy_to_user(measure, ¶ms, sizeof(params))) |
| 703 | ret = -EFAULT; |
| 704 | e_free_blob: |
| 705 | kfree(blob); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 710 | { |
| 711 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 712 | struct sev_data_launch_finish data; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 713 | |
| 714 | if (!sev_guest(kvm)) |
| 715 | return -ENOTTY; |
| 716 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 717 | data.handle = sev->handle; |
| 718 | return sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, &data, &argp->error); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 722 | { |
| 723 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 724 | struct kvm_sev_guest_status params; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 725 | struct sev_data_guest_status data; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 726 | int ret; |
| 727 | |
| 728 | if (!sev_guest(kvm)) |
| 729 | return -ENOTTY; |
| 730 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 731 | memset(&data, 0, sizeof(data)); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 732 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 733 | data.handle = sev->handle; |
| 734 | ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, &data, &argp->error); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 735 | if (ret) |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 736 | return ret; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 737 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 738 | params.policy = data.policy; |
| 739 | params.state = data.state; |
| 740 | params.handle = data.handle; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 741 | |
| 742 | if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) |
| 743 | ret = -EFAULT; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 744 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 745 | return ret; |
| 746 | } |
| 747 | |
| 748 | static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src, |
| 749 | unsigned long dst, int size, |
| 750 | int *error, bool enc) |
| 751 | { |
| 752 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 753 | struct sev_data_dbg data; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 754 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 755 | data.reserved = 0; |
| 756 | data.handle = sev->handle; |
| 757 | data.dst_addr = dst; |
| 758 | data.src_addr = src; |
| 759 | data.len = size; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 760 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 761 | return sev_issue_cmd(kvm, |
| 762 | enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT, |
| 763 | &data, error); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr, |
| 767 | unsigned long dst_paddr, int sz, int *err) |
| 768 | { |
| 769 | int offset; |
| 770 | |
| 771 | /* |
| 772 | * Its safe to read more than we are asked, caller should ensure that |
| 773 | * destination has enough space. |
| 774 | */ |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 775 | offset = src_paddr & 15; |
Ashish Kalra | 854c57f | 2020-11-10 22:42:05 +0000 | [diff] [blame] | 776 | src_paddr = round_down(src_paddr, 16); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 777 | sz = round_up(sz + offset, 16); |
| 778 | |
| 779 | return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false); |
| 780 | } |
| 781 | |
| 782 | static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr, |
Sean Christopherson | 368340a | 2021-05-06 16:15:42 -0700 | [diff] [blame] | 783 | void __user *dst_uaddr, |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 784 | unsigned long dst_paddr, |
| 785 | int size, int *err) |
| 786 | { |
| 787 | struct page *tpage = NULL; |
| 788 | int ret, offset; |
| 789 | |
| 790 | /* if inputs are not 16-byte then use intermediate buffer */ |
| 791 | if (!IS_ALIGNED(dst_paddr, 16) || |
| 792 | !IS_ALIGNED(paddr, 16) || |
| 793 | !IS_ALIGNED(size, 16)) { |
| 794 | tpage = (void *)alloc_page(GFP_KERNEL); |
| 795 | if (!tpage) |
| 796 | return -ENOMEM; |
| 797 | |
| 798 | dst_paddr = __sme_page_pa(tpage); |
| 799 | } |
| 800 | |
| 801 | ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err); |
| 802 | if (ret) |
| 803 | goto e_free; |
| 804 | |
| 805 | if (tpage) { |
| 806 | offset = paddr & 15; |
Sean Christopherson | 368340a | 2021-05-06 16:15:42 -0700 | [diff] [blame] | 807 | if (copy_to_user(dst_uaddr, page_address(tpage) + offset, size)) |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 808 | ret = -EFAULT; |
| 809 | } |
| 810 | |
| 811 | e_free: |
| 812 | if (tpage) |
| 813 | __free_page(tpage); |
| 814 | |
| 815 | return ret; |
| 816 | } |
| 817 | |
| 818 | static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr, |
Sean Christopherson | 368340a | 2021-05-06 16:15:42 -0700 | [diff] [blame] | 819 | void __user *vaddr, |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 820 | unsigned long dst_paddr, |
Sean Christopherson | 368340a | 2021-05-06 16:15:42 -0700 | [diff] [blame] | 821 | void __user *dst_vaddr, |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 822 | int size, int *error) |
| 823 | { |
| 824 | struct page *src_tpage = NULL; |
| 825 | struct page *dst_tpage = NULL; |
| 826 | int ret, len = size; |
| 827 | |
| 828 | /* If source buffer is not aligned then use an intermediate buffer */ |
Sean Christopherson | 368340a | 2021-05-06 16:15:42 -0700 | [diff] [blame] | 829 | if (!IS_ALIGNED((unsigned long)vaddr, 16)) { |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 830 | src_tpage = alloc_page(GFP_KERNEL); |
| 831 | if (!src_tpage) |
| 832 | return -ENOMEM; |
| 833 | |
Sean Christopherson | 368340a | 2021-05-06 16:15:42 -0700 | [diff] [blame] | 834 | if (copy_from_user(page_address(src_tpage), vaddr, size)) { |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 835 | __free_page(src_tpage); |
| 836 | return -EFAULT; |
| 837 | } |
| 838 | |
| 839 | paddr = __sme_page_pa(src_tpage); |
| 840 | } |
| 841 | |
| 842 | /* |
| 843 | * If destination buffer or length is not aligned then do read-modify-write: |
| 844 | * - decrypt destination in an intermediate buffer |
| 845 | * - copy the source buffer in an intermediate buffer |
| 846 | * - use the intermediate buffer as source buffer |
| 847 | */ |
Sean Christopherson | 368340a | 2021-05-06 16:15:42 -0700 | [diff] [blame] | 848 | if (!IS_ALIGNED((unsigned long)dst_vaddr, 16) || !IS_ALIGNED(size, 16)) { |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 849 | int dst_offset; |
| 850 | |
| 851 | dst_tpage = alloc_page(GFP_KERNEL); |
| 852 | if (!dst_tpage) { |
| 853 | ret = -ENOMEM; |
| 854 | goto e_free; |
| 855 | } |
| 856 | |
| 857 | ret = __sev_dbg_decrypt(kvm, dst_paddr, |
| 858 | __sme_page_pa(dst_tpage), size, error); |
| 859 | if (ret) |
| 860 | goto e_free; |
| 861 | |
| 862 | /* |
| 863 | * If source is kernel buffer then use memcpy() otherwise |
| 864 | * copy_from_user(). |
| 865 | */ |
| 866 | dst_offset = dst_paddr & 15; |
| 867 | |
| 868 | if (src_tpage) |
| 869 | memcpy(page_address(dst_tpage) + dst_offset, |
| 870 | page_address(src_tpage), size); |
| 871 | else { |
| 872 | if (copy_from_user(page_address(dst_tpage) + dst_offset, |
Sean Christopherson | 368340a | 2021-05-06 16:15:42 -0700 | [diff] [blame] | 873 | vaddr, size)) { |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 874 | ret = -EFAULT; |
| 875 | goto e_free; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | paddr = __sme_page_pa(dst_tpage); |
| 880 | dst_paddr = round_down(dst_paddr, 16); |
| 881 | len = round_up(size, 16); |
| 882 | } |
| 883 | |
| 884 | ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true); |
| 885 | |
| 886 | e_free: |
| 887 | if (src_tpage) |
| 888 | __free_page(src_tpage); |
| 889 | if (dst_tpage) |
| 890 | __free_page(dst_tpage); |
| 891 | return ret; |
| 892 | } |
| 893 | |
| 894 | static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec) |
| 895 | { |
| 896 | unsigned long vaddr, vaddr_end, next_vaddr; |
| 897 | unsigned long dst_vaddr; |
| 898 | struct page **src_p, **dst_p; |
| 899 | struct kvm_sev_dbg debug; |
| 900 | unsigned long n; |
| 901 | unsigned int size; |
| 902 | int ret; |
| 903 | |
| 904 | if (!sev_guest(kvm)) |
| 905 | return -ENOTTY; |
| 906 | |
| 907 | if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug))) |
| 908 | return -EFAULT; |
| 909 | |
| 910 | if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr) |
| 911 | return -EINVAL; |
| 912 | if (!debug.dst_uaddr) |
| 913 | return -EINVAL; |
| 914 | |
| 915 | vaddr = debug.src_uaddr; |
| 916 | size = debug.len; |
| 917 | vaddr_end = vaddr + size; |
| 918 | dst_vaddr = debug.dst_uaddr; |
| 919 | |
| 920 | for (; vaddr < vaddr_end; vaddr = next_vaddr) { |
| 921 | int len, s_off, d_off; |
| 922 | |
| 923 | /* lock userspace source and destination page */ |
| 924 | src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0); |
Dan Carpenter | ff2bd9f | 2020-07-14 17:23:51 +0300 | [diff] [blame] | 925 | if (IS_ERR(src_p)) |
| 926 | return PTR_ERR(src_p); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 927 | |
| 928 | dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1); |
Dan Carpenter | ff2bd9f | 2020-07-14 17:23:51 +0300 | [diff] [blame] | 929 | if (IS_ERR(dst_p)) { |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 930 | sev_unpin_memory(kvm, src_p, n); |
Dan Carpenter | ff2bd9f | 2020-07-14 17:23:51 +0300 | [diff] [blame] | 931 | return PTR_ERR(dst_p); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /* |
Paolo Bonzini | 14e3dd8 | 2020-09-23 13:01:33 -0400 | [diff] [blame] | 935 | * Flush (on non-coherent CPUs) before DBG_{DE,EN}CRYPT read or modify |
| 936 | * the pages; flush the destination too so that future accesses do not |
| 937 | * see stale data. |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 938 | */ |
| 939 | sev_clflush_pages(src_p, 1); |
| 940 | sev_clflush_pages(dst_p, 1); |
| 941 | |
| 942 | /* |
| 943 | * Since user buffer may not be page aligned, calculate the |
| 944 | * offset within the page. |
| 945 | */ |
| 946 | s_off = vaddr & ~PAGE_MASK; |
| 947 | d_off = dst_vaddr & ~PAGE_MASK; |
| 948 | len = min_t(size_t, (PAGE_SIZE - s_off), size); |
| 949 | |
| 950 | if (dec) |
| 951 | ret = __sev_dbg_decrypt_user(kvm, |
| 952 | __sme_page_pa(src_p[0]) + s_off, |
Sean Christopherson | 368340a | 2021-05-06 16:15:42 -0700 | [diff] [blame] | 953 | (void __user *)dst_vaddr, |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 954 | __sme_page_pa(dst_p[0]) + d_off, |
| 955 | len, &argp->error); |
| 956 | else |
| 957 | ret = __sev_dbg_encrypt_user(kvm, |
| 958 | __sme_page_pa(src_p[0]) + s_off, |
Sean Christopherson | 368340a | 2021-05-06 16:15:42 -0700 | [diff] [blame] | 959 | (void __user *)vaddr, |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 960 | __sme_page_pa(dst_p[0]) + d_off, |
Sean Christopherson | 368340a | 2021-05-06 16:15:42 -0700 | [diff] [blame] | 961 | (void __user *)dst_vaddr, |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 962 | len, &argp->error); |
| 963 | |
| 964 | sev_unpin_memory(kvm, src_p, n); |
| 965 | sev_unpin_memory(kvm, dst_p, n); |
| 966 | |
| 967 | if (ret) |
| 968 | goto err; |
| 969 | |
| 970 | next_vaddr = vaddr + len; |
| 971 | dst_vaddr = dst_vaddr + len; |
| 972 | size -= len; |
| 973 | } |
| 974 | err: |
| 975 | return ret; |
| 976 | } |
| 977 | |
| 978 | static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 979 | { |
| 980 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 981 | struct sev_data_launch_secret data; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 982 | struct kvm_sev_launch_secret params; |
| 983 | struct page **pages; |
| 984 | void *blob, *hdr; |
Cfir Cohen | 50085be | 2020-08-07 17:37:46 -0700 | [diff] [blame] | 985 | unsigned long n, i; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 986 | int ret, offset; |
| 987 | |
| 988 | if (!sev_guest(kvm)) |
| 989 | return -ENOTTY; |
| 990 | |
| 991 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 992 | return -EFAULT; |
| 993 | |
| 994 | pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1); |
Paolo Bonzini | a8d908b | 2020-06-23 05:12:24 -0400 | [diff] [blame] | 995 | if (IS_ERR(pages)) |
| 996 | return PTR_ERR(pages); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 997 | |
| 998 | /* |
Paolo Bonzini | 14e3dd8 | 2020-09-23 13:01:33 -0400 | [diff] [blame] | 999 | * Flush (on non-coherent CPUs) before LAUNCH_SECRET encrypts pages in |
| 1000 | * place; the cache may contain the data that was written unencrypted. |
Cfir Cohen | 50085be | 2020-08-07 17:37:46 -0700 | [diff] [blame] | 1001 | */ |
| 1002 | sev_clflush_pages(pages, n); |
| 1003 | |
| 1004 | /* |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1005 | * The secret must be copied into contiguous memory region, lets verify |
| 1006 | * that userspace memory pages are contiguous before we issue command. |
| 1007 | */ |
| 1008 | if (get_num_contig_pages(0, pages, n) != n) { |
| 1009 | ret = -EINVAL; |
| 1010 | goto e_unpin_memory; |
| 1011 | } |
| 1012 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1013 | memset(&data, 0, sizeof(data)); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1014 | |
| 1015 | offset = params.guest_uaddr & (PAGE_SIZE - 1); |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1016 | data.guest_address = __sme_page_pa(pages[0]) + offset; |
| 1017 | data.guest_len = params.guest_len; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1018 | |
| 1019 | blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len); |
| 1020 | if (IS_ERR(blob)) { |
| 1021 | ret = PTR_ERR(blob); |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1022 | goto e_unpin_memory; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1023 | } |
| 1024 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1025 | data.trans_address = __psp_pa(blob); |
| 1026 | data.trans_len = params.trans_len; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1027 | |
| 1028 | hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len); |
| 1029 | if (IS_ERR(hdr)) { |
| 1030 | ret = PTR_ERR(hdr); |
| 1031 | goto e_free_blob; |
| 1032 | } |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1033 | data.hdr_address = __psp_pa(hdr); |
| 1034 | data.hdr_len = params.hdr_len; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1035 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1036 | data.handle = sev->handle; |
| 1037 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, &data, &argp->error); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1038 | |
| 1039 | kfree(hdr); |
| 1040 | |
| 1041 | e_free_blob: |
| 1042 | kfree(blob); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1043 | e_unpin_memory: |
Cfir Cohen | 50085be | 2020-08-07 17:37:46 -0700 | [diff] [blame] | 1044 | /* content of memory is updated, mark pages dirty */ |
| 1045 | for (i = 0; i < n; i++) { |
| 1046 | set_page_dirty_lock(pages[i]); |
| 1047 | mark_page_accessed(pages[i]); |
| 1048 | } |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1049 | sev_unpin_memory(kvm, pages, n); |
| 1050 | return ret; |
| 1051 | } |
| 1052 | |
Brijesh Singh | 2c07ded | 2021-01-04 09:17:49 -0600 | [diff] [blame] | 1053 | static int sev_get_attestation_report(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 1054 | { |
| 1055 | void __user *report = (void __user *)(uintptr_t)argp->data; |
| 1056 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1057 | struct sev_data_attestation_report data; |
Brijesh Singh | 2c07ded | 2021-01-04 09:17:49 -0600 | [diff] [blame] | 1058 | struct kvm_sev_attestation_report params; |
| 1059 | void __user *p; |
| 1060 | void *blob = NULL; |
| 1061 | int ret; |
| 1062 | |
| 1063 | if (!sev_guest(kvm)) |
| 1064 | return -ENOTTY; |
| 1065 | |
| 1066 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 1067 | return -EFAULT; |
| 1068 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1069 | memset(&data, 0, sizeof(data)); |
Brijesh Singh | 2c07ded | 2021-01-04 09:17:49 -0600 | [diff] [blame] | 1070 | |
| 1071 | /* User wants to query the blob length */ |
| 1072 | if (!params.len) |
| 1073 | goto cmd; |
| 1074 | |
| 1075 | p = (void __user *)(uintptr_t)params.uaddr; |
| 1076 | if (p) { |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1077 | if (params.len > SEV_FW_BLOB_MAX_SIZE) |
| 1078 | return -EINVAL; |
Brijesh Singh | 2c07ded | 2021-01-04 09:17:49 -0600 | [diff] [blame] | 1079 | |
Sean Christopherson | eba04b2 | 2021-03-30 19:30:25 -0700 | [diff] [blame] | 1080 | blob = kmalloc(params.len, GFP_KERNEL_ACCOUNT); |
Brijesh Singh | 2c07ded | 2021-01-04 09:17:49 -0600 | [diff] [blame] | 1081 | if (!blob) |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1082 | return -ENOMEM; |
Brijesh Singh | 2c07ded | 2021-01-04 09:17:49 -0600 | [diff] [blame] | 1083 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1084 | data.address = __psp_pa(blob); |
| 1085 | data.len = params.len; |
| 1086 | memcpy(data.mnonce, params.mnonce, sizeof(params.mnonce)); |
Brijesh Singh | 2c07ded | 2021-01-04 09:17:49 -0600 | [diff] [blame] | 1087 | } |
| 1088 | cmd: |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1089 | data.handle = sev->handle; |
| 1090 | ret = sev_issue_cmd(kvm, SEV_CMD_ATTESTATION_REPORT, &data, &argp->error); |
Brijesh Singh | 2c07ded | 2021-01-04 09:17:49 -0600 | [diff] [blame] | 1091 | /* |
| 1092 | * If we query the session length, FW responded with expected data. |
| 1093 | */ |
| 1094 | if (!params.len) |
| 1095 | goto done; |
| 1096 | |
| 1097 | if (ret) |
| 1098 | goto e_free_blob; |
| 1099 | |
| 1100 | if (blob) { |
| 1101 | if (copy_to_user(p, blob, params.len)) |
| 1102 | ret = -EFAULT; |
| 1103 | } |
| 1104 | |
| 1105 | done: |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1106 | params.len = data.len; |
Brijesh Singh | 2c07ded | 2021-01-04 09:17:49 -0600 | [diff] [blame] | 1107 | if (copy_to_user(report, ¶ms, sizeof(params))) |
| 1108 | ret = -EFAULT; |
| 1109 | e_free_blob: |
| 1110 | kfree(blob); |
Brijesh Singh | 2c07ded | 2021-01-04 09:17:49 -0600 | [diff] [blame] | 1111 | return ret; |
| 1112 | } |
| 1113 | |
Brijesh Singh | 4cfdd47 | 2021-04-15 15:53:14 +0000 | [diff] [blame] | 1114 | /* Userspace wants to query session length. */ |
| 1115 | static int |
| 1116 | __sev_send_start_query_session_length(struct kvm *kvm, struct kvm_sev_cmd *argp, |
| 1117 | struct kvm_sev_send_start *params) |
| 1118 | { |
| 1119 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1120 | struct sev_data_send_start data; |
Brijesh Singh | 4cfdd47 | 2021-04-15 15:53:14 +0000 | [diff] [blame] | 1121 | int ret; |
| 1122 | |
Ashish Kalra | 4f13d47 | 2021-06-07 06:15:32 +0000 | [diff] [blame] | 1123 | memset(&data, 0, sizeof(data)); |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1124 | data.handle = sev->handle; |
| 1125 | ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error); |
Brijesh Singh | 4cfdd47 | 2021-04-15 15:53:14 +0000 | [diff] [blame] | 1126 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1127 | params->session_len = data.session_len; |
Brijesh Singh | 4cfdd47 | 2021-04-15 15:53:14 +0000 | [diff] [blame] | 1128 | if (copy_to_user((void __user *)(uintptr_t)argp->data, params, |
| 1129 | sizeof(struct kvm_sev_send_start))) |
| 1130 | ret = -EFAULT; |
| 1131 | |
Brijesh Singh | 4cfdd47 | 2021-04-15 15:53:14 +0000 | [diff] [blame] | 1132 | return ret; |
| 1133 | } |
| 1134 | |
| 1135 | static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 1136 | { |
| 1137 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1138 | struct sev_data_send_start data; |
Brijesh Singh | 4cfdd47 | 2021-04-15 15:53:14 +0000 | [diff] [blame] | 1139 | struct kvm_sev_send_start params; |
| 1140 | void *amd_certs, *session_data; |
| 1141 | void *pdh_cert, *plat_certs; |
| 1142 | int ret; |
| 1143 | |
| 1144 | if (!sev_guest(kvm)) |
| 1145 | return -ENOTTY; |
| 1146 | |
| 1147 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, |
| 1148 | sizeof(struct kvm_sev_send_start))) |
| 1149 | return -EFAULT; |
| 1150 | |
| 1151 | /* if session_len is zero, userspace wants to query the session length */ |
| 1152 | if (!params.session_len) |
| 1153 | return __sev_send_start_query_session_length(kvm, argp, |
| 1154 | ¶ms); |
| 1155 | |
| 1156 | /* some sanity checks */ |
| 1157 | if (!params.pdh_cert_uaddr || !params.pdh_cert_len || |
| 1158 | !params.session_uaddr || params.session_len > SEV_FW_BLOB_MAX_SIZE) |
| 1159 | return -EINVAL; |
| 1160 | |
| 1161 | /* allocate the memory to hold the session data blob */ |
| 1162 | session_data = kmalloc(params.session_len, GFP_KERNEL_ACCOUNT); |
| 1163 | if (!session_data) |
| 1164 | return -ENOMEM; |
| 1165 | |
| 1166 | /* copy the certificate blobs from userspace */ |
| 1167 | pdh_cert = psp_copy_user_blob(params.pdh_cert_uaddr, |
| 1168 | params.pdh_cert_len); |
| 1169 | if (IS_ERR(pdh_cert)) { |
| 1170 | ret = PTR_ERR(pdh_cert); |
| 1171 | goto e_free_session; |
| 1172 | } |
| 1173 | |
| 1174 | plat_certs = psp_copy_user_blob(params.plat_certs_uaddr, |
| 1175 | params.plat_certs_len); |
| 1176 | if (IS_ERR(plat_certs)) { |
| 1177 | ret = PTR_ERR(plat_certs); |
| 1178 | goto e_free_pdh; |
| 1179 | } |
| 1180 | |
| 1181 | amd_certs = psp_copy_user_blob(params.amd_certs_uaddr, |
| 1182 | params.amd_certs_len); |
| 1183 | if (IS_ERR(amd_certs)) { |
| 1184 | ret = PTR_ERR(amd_certs); |
| 1185 | goto e_free_plat_cert; |
| 1186 | } |
| 1187 | |
Brijesh Singh | 4cfdd47 | 2021-04-15 15:53:14 +0000 | [diff] [blame] | 1188 | /* populate the FW SEND_START field with system physical address */ |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1189 | memset(&data, 0, sizeof(data)); |
| 1190 | data.pdh_cert_address = __psp_pa(pdh_cert); |
| 1191 | data.pdh_cert_len = params.pdh_cert_len; |
| 1192 | data.plat_certs_address = __psp_pa(plat_certs); |
| 1193 | data.plat_certs_len = params.plat_certs_len; |
| 1194 | data.amd_certs_address = __psp_pa(amd_certs); |
| 1195 | data.amd_certs_len = params.amd_certs_len; |
| 1196 | data.session_address = __psp_pa(session_data); |
| 1197 | data.session_len = params.session_len; |
| 1198 | data.handle = sev->handle; |
Brijesh Singh | 4cfdd47 | 2021-04-15 15:53:14 +0000 | [diff] [blame] | 1199 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1200 | ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error); |
Brijesh Singh | 4cfdd47 | 2021-04-15 15:53:14 +0000 | [diff] [blame] | 1201 | |
| 1202 | if (!ret && copy_to_user((void __user *)(uintptr_t)params.session_uaddr, |
| 1203 | session_data, params.session_len)) { |
| 1204 | ret = -EFAULT; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1205 | goto e_free_amd_cert; |
Brijesh Singh | 4cfdd47 | 2021-04-15 15:53:14 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1208 | params.policy = data.policy; |
| 1209 | params.session_len = data.session_len; |
Brijesh Singh | 4cfdd47 | 2021-04-15 15:53:14 +0000 | [diff] [blame] | 1210 | if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, |
| 1211 | sizeof(struct kvm_sev_send_start))) |
| 1212 | ret = -EFAULT; |
| 1213 | |
Brijesh Singh | 4cfdd47 | 2021-04-15 15:53:14 +0000 | [diff] [blame] | 1214 | e_free_amd_cert: |
| 1215 | kfree(amd_certs); |
| 1216 | e_free_plat_cert: |
| 1217 | kfree(plat_certs); |
| 1218 | e_free_pdh: |
| 1219 | kfree(pdh_cert); |
| 1220 | e_free_session: |
| 1221 | kfree(session_data); |
| 1222 | return ret; |
| 1223 | } |
| 1224 | |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1225 | /* Userspace wants to query either header or trans length. */ |
| 1226 | static int |
| 1227 | __sev_send_update_data_query_lengths(struct kvm *kvm, struct kvm_sev_cmd *argp, |
| 1228 | struct kvm_sev_send_update_data *params) |
| 1229 | { |
| 1230 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1231 | struct sev_data_send_update_data data; |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1232 | int ret; |
| 1233 | |
Ashish Kalra | 4f13d47 | 2021-06-07 06:15:32 +0000 | [diff] [blame] | 1234 | memset(&data, 0, sizeof(data)); |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1235 | data.handle = sev->handle; |
| 1236 | ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error); |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1237 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1238 | params->hdr_len = data.hdr_len; |
| 1239 | params->trans_len = data.trans_len; |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1240 | |
| 1241 | if (copy_to_user((void __user *)(uintptr_t)argp->data, params, |
| 1242 | sizeof(struct kvm_sev_send_update_data))) |
| 1243 | ret = -EFAULT; |
| 1244 | |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1245 | return ret; |
| 1246 | } |
| 1247 | |
| 1248 | static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 1249 | { |
| 1250 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1251 | struct sev_data_send_update_data data; |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1252 | struct kvm_sev_send_update_data params; |
| 1253 | void *hdr, *trans_data; |
| 1254 | struct page **guest_page; |
| 1255 | unsigned long n; |
| 1256 | int ret, offset; |
| 1257 | |
| 1258 | if (!sev_guest(kvm)) |
| 1259 | return -ENOTTY; |
| 1260 | |
| 1261 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, |
| 1262 | sizeof(struct kvm_sev_send_update_data))) |
| 1263 | return -EFAULT; |
| 1264 | |
| 1265 | /* userspace wants to query either header or trans length */ |
| 1266 | if (!params.trans_len || !params.hdr_len) |
| 1267 | return __sev_send_update_data_query_lengths(kvm, argp, ¶ms); |
| 1268 | |
| 1269 | if (!params.trans_uaddr || !params.guest_uaddr || |
| 1270 | !params.guest_len || !params.hdr_uaddr) |
| 1271 | return -EINVAL; |
| 1272 | |
| 1273 | /* Check if we are crossing the page boundary */ |
| 1274 | offset = params.guest_uaddr & (PAGE_SIZE - 1); |
| 1275 | if ((params.guest_len + offset > PAGE_SIZE)) |
| 1276 | return -EINVAL; |
| 1277 | |
| 1278 | /* Pin guest memory */ |
| 1279 | guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK, |
| 1280 | PAGE_SIZE, &n, 0); |
Sean Christopherson | c7a1b2b | 2021-05-06 10:58:26 -0700 | [diff] [blame] | 1281 | if (IS_ERR(guest_page)) |
| 1282 | return PTR_ERR(guest_page); |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1283 | |
| 1284 | /* allocate memory for header and transport buffer */ |
| 1285 | ret = -ENOMEM; |
| 1286 | hdr = kmalloc(params.hdr_len, GFP_KERNEL_ACCOUNT); |
| 1287 | if (!hdr) |
| 1288 | goto e_unpin; |
| 1289 | |
| 1290 | trans_data = kmalloc(params.trans_len, GFP_KERNEL_ACCOUNT); |
| 1291 | if (!trans_data) |
| 1292 | goto e_free_hdr; |
| 1293 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1294 | memset(&data, 0, sizeof(data)); |
| 1295 | data.hdr_address = __psp_pa(hdr); |
| 1296 | data.hdr_len = params.hdr_len; |
| 1297 | data.trans_address = __psp_pa(trans_data); |
| 1298 | data.trans_len = params.trans_len; |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1299 | |
| 1300 | /* The SEND_UPDATE_DATA command requires C-bit to be always set. */ |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1301 | data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset; |
| 1302 | data.guest_address |= sev_me_mask; |
| 1303 | data.guest_len = params.guest_len; |
| 1304 | data.handle = sev->handle; |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1305 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1306 | ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error); |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1307 | |
| 1308 | if (ret) |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1309 | goto e_free_trans_data; |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1310 | |
| 1311 | /* copy transport buffer to user space */ |
| 1312 | if (copy_to_user((void __user *)(uintptr_t)params.trans_uaddr, |
| 1313 | trans_data, params.trans_len)) { |
| 1314 | ret = -EFAULT; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1315 | goto e_free_trans_data; |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | /* Copy packet header to userspace. */ |
Sean Christopherson | b4a6939 | 2021-05-06 10:58:25 -0700 | [diff] [blame] | 1319 | if (copy_to_user((void __user *)(uintptr_t)params.hdr_uaddr, hdr, |
| 1320 | params.hdr_len)) |
| 1321 | ret = -EFAULT; |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1322 | |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1323 | e_free_trans_data: |
| 1324 | kfree(trans_data); |
| 1325 | e_free_hdr: |
| 1326 | kfree(hdr); |
| 1327 | e_unpin: |
| 1328 | sev_unpin_memory(kvm, guest_page, n); |
| 1329 | |
| 1330 | return ret; |
| 1331 | } |
| 1332 | |
Brijesh Singh | fddecf6 | 2021-04-15 15:54:15 +0000 | [diff] [blame] | 1333 | static int sev_send_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 1334 | { |
| 1335 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1336 | struct sev_data_send_finish data; |
Brijesh Singh | fddecf6 | 2021-04-15 15:54:15 +0000 | [diff] [blame] | 1337 | |
| 1338 | if (!sev_guest(kvm)) |
| 1339 | return -ENOTTY; |
| 1340 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1341 | data.handle = sev->handle; |
| 1342 | return sev_issue_cmd(kvm, SEV_CMD_SEND_FINISH, &data, &argp->error); |
Brijesh Singh | fddecf6 | 2021-04-15 15:54:15 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Steve Rutherford | 5569e2e | 2021-04-20 05:01:20 -0400 | [diff] [blame] | 1345 | static int sev_send_cancel(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 1346 | { |
| 1347 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1348 | struct sev_data_send_cancel data; |
Steve Rutherford | 5569e2e | 2021-04-20 05:01:20 -0400 | [diff] [blame] | 1349 | |
| 1350 | if (!sev_guest(kvm)) |
| 1351 | return -ENOTTY; |
| 1352 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1353 | data.handle = sev->handle; |
| 1354 | return sev_issue_cmd(kvm, SEV_CMD_SEND_CANCEL, &data, &argp->error); |
Steve Rutherford | 5569e2e | 2021-04-20 05:01:20 -0400 | [diff] [blame] | 1355 | } |
| 1356 | |
Brijesh Singh | af43cbb | 2021-04-15 15:54:50 +0000 | [diff] [blame] | 1357 | static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 1358 | { |
| 1359 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1360 | struct sev_data_receive_start start; |
Brijesh Singh | af43cbb | 2021-04-15 15:54:50 +0000 | [diff] [blame] | 1361 | struct kvm_sev_receive_start params; |
| 1362 | int *error = &argp->error; |
| 1363 | void *session_data; |
| 1364 | void *pdh_data; |
| 1365 | int ret; |
| 1366 | |
| 1367 | if (!sev_guest(kvm)) |
| 1368 | return -ENOTTY; |
| 1369 | |
| 1370 | /* Get parameter from the userspace */ |
| 1371 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, |
| 1372 | sizeof(struct kvm_sev_receive_start))) |
| 1373 | return -EFAULT; |
| 1374 | |
| 1375 | /* some sanity checks */ |
| 1376 | if (!params.pdh_uaddr || !params.pdh_len || |
| 1377 | !params.session_uaddr || !params.session_len) |
| 1378 | return -EINVAL; |
| 1379 | |
| 1380 | pdh_data = psp_copy_user_blob(params.pdh_uaddr, params.pdh_len); |
| 1381 | if (IS_ERR(pdh_data)) |
| 1382 | return PTR_ERR(pdh_data); |
| 1383 | |
| 1384 | session_data = psp_copy_user_blob(params.session_uaddr, |
| 1385 | params.session_len); |
| 1386 | if (IS_ERR(session_data)) { |
| 1387 | ret = PTR_ERR(session_data); |
| 1388 | goto e_free_pdh; |
| 1389 | } |
| 1390 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1391 | memset(&start, 0, sizeof(start)); |
| 1392 | start.handle = params.handle; |
| 1393 | start.policy = params.policy; |
| 1394 | start.pdh_cert_address = __psp_pa(pdh_data); |
| 1395 | start.pdh_cert_len = params.pdh_len; |
| 1396 | start.session_address = __psp_pa(session_data); |
| 1397 | start.session_len = params.session_len; |
Brijesh Singh | af43cbb | 2021-04-15 15:54:50 +0000 | [diff] [blame] | 1398 | |
| 1399 | /* create memory encryption context */ |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1400 | ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_RECEIVE_START, &start, |
Brijesh Singh | af43cbb | 2021-04-15 15:54:50 +0000 | [diff] [blame] | 1401 | error); |
| 1402 | if (ret) |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1403 | goto e_free_session; |
Brijesh Singh | af43cbb | 2021-04-15 15:54:50 +0000 | [diff] [blame] | 1404 | |
| 1405 | /* Bind ASID to this guest */ |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1406 | ret = sev_bind_asid(kvm, start.handle, error); |
Mingwei Zhang | f1815e0a | 2021-09-12 18:18:15 +0000 | [diff] [blame] | 1407 | if (ret) { |
| 1408 | sev_decommission(start.handle); |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1409 | goto e_free_session; |
Mingwei Zhang | f1815e0a | 2021-09-12 18:18:15 +0000 | [diff] [blame] | 1410 | } |
Brijesh Singh | af43cbb | 2021-04-15 15:54:50 +0000 | [diff] [blame] | 1411 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1412 | params.handle = start.handle; |
Brijesh Singh | af43cbb | 2021-04-15 15:54:50 +0000 | [diff] [blame] | 1413 | if (copy_to_user((void __user *)(uintptr_t)argp->data, |
| 1414 | ¶ms, sizeof(struct kvm_sev_receive_start))) { |
| 1415 | ret = -EFAULT; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1416 | sev_unbind_asid(kvm, start.handle); |
| 1417 | goto e_free_session; |
Brijesh Singh | af43cbb | 2021-04-15 15:54:50 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1420 | sev->handle = start.handle; |
Brijesh Singh | af43cbb | 2021-04-15 15:54:50 +0000 | [diff] [blame] | 1421 | sev->fd = argp->sev_fd; |
| 1422 | |
Brijesh Singh | af43cbb | 2021-04-15 15:54:50 +0000 | [diff] [blame] | 1423 | e_free_session: |
| 1424 | kfree(session_data); |
| 1425 | e_free_pdh: |
| 1426 | kfree(pdh_data); |
| 1427 | |
| 1428 | return ret; |
| 1429 | } |
| 1430 | |
Brijesh Singh | 15fb7de | 2021-04-15 15:55:17 +0000 | [diff] [blame] | 1431 | static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 1432 | { |
| 1433 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 1434 | struct kvm_sev_receive_update_data params; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1435 | struct sev_data_receive_update_data data; |
Brijesh Singh | 15fb7de | 2021-04-15 15:55:17 +0000 | [diff] [blame] | 1436 | void *hdr = NULL, *trans = NULL; |
| 1437 | struct page **guest_page; |
| 1438 | unsigned long n; |
| 1439 | int ret, offset; |
| 1440 | |
| 1441 | if (!sev_guest(kvm)) |
| 1442 | return -EINVAL; |
| 1443 | |
| 1444 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, |
| 1445 | sizeof(struct kvm_sev_receive_update_data))) |
| 1446 | return -EFAULT; |
| 1447 | |
| 1448 | if (!params.hdr_uaddr || !params.hdr_len || |
| 1449 | !params.guest_uaddr || !params.guest_len || |
| 1450 | !params.trans_uaddr || !params.trans_len) |
| 1451 | return -EINVAL; |
| 1452 | |
| 1453 | /* Check if we are crossing the page boundary */ |
| 1454 | offset = params.guest_uaddr & (PAGE_SIZE - 1); |
| 1455 | if ((params.guest_len + offset > PAGE_SIZE)) |
| 1456 | return -EINVAL; |
| 1457 | |
| 1458 | hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len); |
| 1459 | if (IS_ERR(hdr)) |
| 1460 | return PTR_ERR(hdr); |
| 1461 | |
| 1462 | trans = psp_copy_user_blob(params.trans_uaddr, params.trans_len); |
| 1463 | if (IS_ERR(trans)) { |
| 1464 | ret = PTR_ERR(trans); |
| 1465 | goto e_free_hdr; |
| 1466 | } |
| 1467 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1468 | memset(&data, 0, sizeof(data)); |
| 1469 | data.hdr_address = __psp_pa(hdr); |
| 1470 | data.hdr_len = params.hdr_len; |
| 1471 | data.trans_address = __psp_pa(trans); |
| 1472 | data.trans_len = params.trans_len; |
Brijesh Singh | 15fb7de | 2021-04-15 15:55:17 +0000 | [diff] [blame] | 1473 | |
| 1474 | /* Pin guest memory */ |
Brijesh Singh | 15fb7de | 2021-04-15 15:55:17 +0000 | [diff] [blame] | 1475 | guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK, |
Sean Christopherson | 50c0380 | 2021-09-14 14:09:50 -0700 | [diff] [blame] | 1476 | PAGE_SIZE, &n, 1); |
Sean Christopherson | c7a1b2b | 2021-05-06 10:58:26 -0700 | [diff] [blame] | 1477 | if (IS_ERR(guest_page)) { |
| 1478 | ret = PTR_ERR(guest_page); |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1479 | goto e_free_trans; |
Sean Christopherson | c7a1b2b | 2021-05-06 10:58:26 -0700 | [diff] [blame] | 1480 | } |
Brijesh Singh | 15fb7de | 2021-04-15 15:55:17 +0000 | [diff] [blame] | 1481 | |
| 1482 | /* The RECEIVE_UPDATE_DATA command requires C-bit to be always set. */ |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1483 | data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset; |
| 1484 | data.guest_address |= sev_me_mask; |
| 1485 | data.guest_len = params.guest_len; |
| 1486 | data.handle = sev->handle; |
Brijesh Singh | 15fb7de | 2021-04-15 15:55:17 +0000 | [diff] [blame] | 1487 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1488 | ret = sev_issue_cmd(kvm, SEV_CMD_RECEIVE_UPDATE_DATA, &data, |
Brijesh Singh | 15fb7de | 2021-04-15 15:55:17 +0000 | [diff] [blame] | 1489 | &argp->error); |
| 1490 | |
| 1491 | sev_unpin_memory(kvm, guest_page, n); |
| 1492 | |
Brijesh Singh | 15fb7de | 2021-04-15 15:55:17 +0000 | [diff] [blame] | 1493 | e_free_trans: |
| 1494 | kfree(trans); |
| 1495 | e_free_hdr: |
| 1496 | kfree(hdr); |
| 1497 | |
| 1498 | return ret; |
| 1499 | } |
| 1500 | |
Brijesh Singh | 6a443de | 2021-04-15 15:55:40 +0000 | [diff] [blame] | 1501 | static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 1502 | { |
| 1503 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1504 | struct sev_data_receive_finish data; |
Brijesh Singh | 6a443de | 2021-04-15 15:55:40 +0000 | [diff] [blame] | 1505 | |
| 1506 | if (!sev_guest(kvm)) |
| 1507 | return -ENOTTY; |
| 1508 | |
Sean Christopherson | 238eca8 | 2021-04-06 15:49:52 -0700 | [diff] [blame] | 1509 | data.handle = sev->handle; |
| 1510 | return sev_issue_cmd(kvm, SEV_CMD_RECEIVE_FINISH, &data, &argp->error); |
Brijesh Singh | 6a443de | 2021-04-15 15:55:40 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Peter Gonda | 5b92b6c | 2021-09-21 08:03:45 -0700 | [diff] [blame] | 1513 | static bool cmd_allowed_from_miror(u32 cmd_id) |
| 1514 | { |
| 1515 | /* |
| 1516 | * Allow mirrors VM to call KVM_SEV_LAUNCH_UPDATE_VMSA to enable SEV-ES |
| 1517 | * active mirror VMs. Also allow the debugging and status commands. |
| 1518 | */ |
| 1519 | if (cmd_id == KVM_SEV_LAUNCH_UPDATE_VMSA || |
| 1520 | cmd_id == KVM_SEV_GUEST_STATUS || cmd_id == KVM_SEV_DBG_DECRYPT || |
| 1521 | cmd_id == KVM_SEV_DBG_ENCRYPT) |
| 1522 | return true; |
| 1523 | |
| 1524 | return false; |
| 1525 | } |
| 1526 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1527 | int svm_mem_enc_op(struct kvm *kvm, void __user *argp) |
| 1528 | { |
| 1529 | struct kvm_sev_cmd sev_cmd; |
| 1530 | int r; |
| 1531 | |
Sean Christopherson | a5c1c5a | 2021-04-21 19:11:23 -0700 | [diff] [blame] | 1532 | if (!sev_enabled) |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1533 | return -ENOTTY; |
| 1534 | |
| 1535 | if (!argp) |
| 1536 | return 0; |
| 1537 | |
| 1538 | if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd))) |
| 1539 | return -EFAULT; |
| 1540 | |
| 1541 | mutex_lock(&kvm->lock); |
| 1542 | |
Peter Gonda | 5b92b6c | 2021-09-21 08:03:45 -0700 | [diff] [blame] | 1543 | /* Only the enc_context_owner handles some memory enc operations. */ |
| 1544 | if (is_mirroring_enc_context(kvm) && |
| 1545 | !cmd_allowed_from_miror(sev_cmd.id)) { |
Nathan Tempelman | 54526d1 | 2021-04-08 22:32:14 +0000 | [diff] [blame] | 1546 | r = -EINVAL; |
| 1547 | goto out; |
| 1548 | } |
| 1549 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1550 | switch (sev_cmd.id) { |
Sean Christopherson | 9fa1521 | 2021-03-30 20:19:35 -0700 | [diff] [blame] | 1551 | case KVM_SEV_ES_INIT: |
Sean Christopherson | 8d364a0 | 2021-04-21 19:11:17 -0700 | [diff] [blame] | 1552 | if (!sev_es_enabled) { |
Sean Christopherson | 9fa1521 | 2021-03-30 20:19:35 -0700 | [diff] [blame] | 1553 | r = -ENOTTY; |
| 1554 | goto out; |
| 1555 | } |
| 1556 | fallthrough; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1557 | case KVM_SEV_INIT: |
| 1558 | r = sev_guest_init(kvm, &sev_cmd); |
| 1559 | break; |
| 1560 | case KVM_SEV_LAUNCH_START: |
| 1561 | r = sev_launch_start(kvm, &sev_cmd); |
| 1562 | break; |
| 1563 | case KVM_SEV_LAUNCH_UPDATE_DATA: |
| 1564 | r = sev_launch_update_data(kvm, &sev_cmd); |
| 1565 | break; |
Tom Lendacky | ad73109 | 2020-12-10 11:10:09 -0600 | [diff] [blame] | 1566 | case KVM_SEV_LAUNCH_UPDATE_VMSA: |
| 1567 | r = sev_launch_update_vmsa(kvm, &sev_cmd); |
| 1568 | break; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1569 | case KVM_SEV_LAUNCH_MEASURE: |
| 1570 | r = sev_launch_measure(kvm, &sev_cmd); |
| 1571 | break; |
| 1572 | case KVM_SEV_LAUNCH_FINISH: |
| 1573 | r = sev_launch_finish(kvm, &sev_cmd); |
| 1574 | break; |
| 1575 | case KVM_SEV_GUEST_STATUS: |
| 1576 | r = sev_guest_status(kvm, &sev_cmd); |
| 1577 | break; |
| 1578 | case KVM_SEV_DBG_DECRYPT: |
| 1579 | r = sev_dbg_crypt(kvm, &sev_cmd, true); |
| 1580 | break; |
| 1581 | case KVM_SEV_DBG_ENCRYPT: |
| 1582 | r = sev_dbg_crypt(kvm, &sev_cmd, false); |
| 1583 | break; |
| 1584 | case KVM_SEV_LAUNCH_SECRET: |
| 1585 | r = sev_launch_secret(kvm, &sev_cmd); |
| 1586 | break; |
Brijesh Singh | 2c07ded | 2021-01-04 09:17:49 -0600 | [diff] [blame] | 1587 | case KVM_SEV_GET_ATTESTATION_REPORT: |
| 1588 | r = sev_get_attestation_report(kvm, &sev_cmd); |
| 1589 | break; |
Brijesh Singh | 4cfdd47 | 2021-04-15 15:53:14 +0000 | [diff] [blame] | 1590 | case KVM_SEV_SEND_START: |
| 1591 | r = sev_send_start(kvm, &sev_cmd); |
| 1592 | break; |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1593 | case KVM_SEV_SEND_UPDATE_DATA: |
| 1594 | r = sev_send_update_data(kvm, &sev_cmd); |
| 1595 | break; |
Brijesh Singh | fddecf6 | 2021-04-15 15:54:15 +0000 | [diff] [blame] | 1596 | case KVM_SEV_SEND_FINISH: |
| 1597 | r = sev_send_finish(kvm, &sev_cmd); |
| 1598 | break; |
Steve Rutherford | 5569e2e | 2021-04-20 05:01:20 -0400 | [diff] [blame] | 1599 | case KVM_SEV_SEND_CANCEL: |
| 1600 | r = sev_send_cancel(kvm, &sev_cmd); |
| 1601 | break; |
Brijesh Singh | af43cbb | 2021-04-15 15:54:50 +0000 | [diff] [blame] | 1602 | case KVM_SEV_RECEIVE_START: |
| 1603 | r = sev_receive_start(kvm, &sev_cmd); |
| 1604 | break; |
Brijesh Singh | 15fb7de | 2021-04-15 15:55:17 +0000 | [diff] [blame] | 1605 | case KVM_SEV_RECEIVE_UPDATE_DATA: |
| 1606 | r = sev_receive_update_data(kvm, &sev_cmd); |
| 1607 | break; |
Brijesh Singh | 6a443de | 2021-04-15 15:55:40 +0000 | [diff] [blame] | 1608 | case KVM_SEV_RECEIVE_FINISH: |
| 1609 | r = sev_receive_finish(kvm, &sev_cmd); |
| 1610 | break; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1611 | default: |
| 1612 | r = -EINVAL; |
| 1613 | goto out; |
| 1614 | } |
| 1615 | |
| 1616 | if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd))) |
| 1617 | r = -EFAULT; |
| 1618 | |
| 1619 | out: |
| 1620 | mutex_unlock(&kvm->lock); |
| 1621 | return r; |
| 1622 | } |
| 1623 | |
| 1624 | int svm_register_enc_region(struct kvm *kvm, |
| 1625 | struct kvm_enc_region *range) |
| 1626 | { |
| 1627 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 1628 | struct enc_region *region; |
| 1629 | int ret = 0; |
| 1630 | |
| 1631 | if (!sev_guest(kvm)) |
| 1632 | return -ENOTTY; |
| 1633 | |
Nathan Tempelman | 54526d1 | 2021-04-08 22:32:14 +0000 | [diff] [blame] | 1634 | /* If kvm is mirroring encryption context it isn't responsible for it */ |
| 1635 | if (is_mirroring_enc_context(kvm)) |
| 1636 | return -EINVAL; |
| 1637 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1638 | if (range->addr > ULONG_MAX || range->size > ULONG_MAX) |
| 1639 | return -EINVAL; |
| 1640 | |
| 1641 | region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT); |
| 1642 | if (!region) |
| 1643 | return -ENOMEM; |
| 1644 | |
Peter Gonda | 19a23da | 2021-01-27 08:15:24 -0800 | [diff] [blame] | 1645 | mutex_lock(&kvm->lock); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1646 | region->pages = sev_pin_memory(kvm, range->addr, range->size, ®ion->npages, 1); |
Paolo Bonzini | a8d908b | 2020-06-23 05:12:24 -0400 | [diff] [blame] | 1647 | if (IS_ERR(region->pages)) { |
| 1648 | ret = PTR_ERR(region->pages); |
Peter Gonda | 19a23da | 2021-01-27 08:15:24 -0800 | [diff] [blame] | 1649 | mutex_unlock(&kvm->lock); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1650 | goto e_free; |
| 1651 | } |
| 1652 | |
Peter Gonda | 19a23da | 2021-01-27 08:15:24 -0800 | [diff] [blame] | 1653 | region->uaddr = range->addr; |
| 1654 | region->size = range->size; |
| 1655 | |
| 1656 | list_add_tail(®ion->list, &sev->regions_list); |
| 1657 | mutex_unlock(&kvm->lock); |
| 1658 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1659 | /* |
| 1660 | * The guest may change the memory encryption attribute from C=0 -> C=1 |
| 1661 | * or vice versa for this memory range. Lets make sure caches are |
| 1662 | * flushed to ensure that guest data gets written into memory with |
| 1663 | * correct C-bit. |
| 1664 | */ |
| 1665 | sev_clflush_pages(region->pages, region->npages); |
| 1666 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1667 | return ret; |
| 1668 | |
| 1669 | e_free: |
| 1670 | kfree(region); |
| 1671 | return ret; |
| 1672 | } |
| 1673 | |
| 1674 | static struct enc_region * |
| 1675 | find_enc_region(struct kvm *kvm, struct kvm_enc_region *range) |
| 1676 | { |
| 1677 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 1678 | struct list_head *head = &sev->regions_list; |
| 1679 | struct enc_region *i; |
| 1680 | |
| 1681 | list_for_each_entry(i, head, list) { |
| 1682 | if (i->uaddr == range->addr && |
| 1683 | i->size == range->size) |
| 1684 | return i; |
| 1685 | } |
| 1686 | |
| 1687 | return NULL; |
| 1688 | } |
| 1689 | |
| 1690 | static void __unregister_enc_region_locked(struct kvm *kvm, |
| 1691 | struct enc_region *region) |
| 1692 | { |
| 1693 | sev_unpin_memory(kvm, region->pages, region->npages); |
| 1694 | list_del(®ion->list); |
| 1695 | kfree(region); |
| 1696 | } |
| 1697 | |
| 1698 | int svm_unregister_enc_region(struct kvm *kvm, |
| 1699 | struct kvm_enc_region *range) |
| 1700 | { |
| 1701 | struct enc_region *region; |
| 1702 | int ret; |
| 1703 | |
Nathan Tempelman | 54526d1 | 2021-04-08 22:32:14 +0000 | [diff] [blame] | 1704 | /* If kvm is mirroring encryption context it isn't responsible for it */ |
| 1705 | if (is_mirroring_enc_context(kvm)) |
| 1706 | return -EINVAL; |
| 1707 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1708 | mutex_lock(&kvm->lock); |
| 1709 | |
| 1710 | if (!sev_guest(kvm)) { |
| 1711 | ret = -ENOTTY; |
| 1712 | goto failed; |
| 1713 | } |
| 1714 | |
| 1715 | region = find_enc_region(kvm, range); |
| 1716 | if (!region) { |
| 1717 | ret = -EINVAL; |
| 1718 | goto failed; |
| 1719 | } |
| 1720 | |
| 1721 | /* |
| 1722 | * Ensure that all guest tagged cache entries are flushed before |
| 1723 | * releasing the pages back to the system for use. CLFLUSH will |
| 1724 | * not do this, so issue a WBINVD. |
| 1725 | */ |
| 1726 | wbinvd_on_all_cpus(); |
| 1727 | |
| 1728 | __unregister_enc_region_locked(kvm, region); |
| 1729 | |
| 1730 | mutex_unlock(&kvm->lock); |
| 1731 | return 0; |
| 1732 | |
| 1733 | failed: |
| 1734 | mutex_unlock(&kvm->lock); |
| 1735 | return ret; |
| 1736 | } |
| 1737 | |
Nathan Tempelman | 54526d1 | 2021-04-08 22:32:14 +0000 | [diff] [blame] | 1738 | int svm_vm_copy_asid_from(struct kvm *kvm, unsigned int source_fd) |
| 1739 | { |
| 1740 | struct file *source_kvm_file; |
| 1741 | struct kvm *source_kvm; |
Peter Gonda | f43c887 | 2021-09-21 08:03:44 -0700 | [diff] [blame] | 1742 | struct kvm_sev_info source_sev, *mirror_sev; |
Nathan Tempelman | 54526d1 | 2021-04-08 22:32:14 +0000 | [diff] [blame] | 1743 | int ret; |
| 1744 | |
| 1745 | source_kvm_file = fget(source_fd); |
| 1746 | if (!file_is_kvm(source_kvm_file)) { |
| 1747 | ret = -EBADF; |
| 1748 | goto e_source_put; |
| 1749 | } |
| 1750 | |
| 1751 | source_kvm = source_kvm_file->private_data; |
| 1752 | mutex_lock(&source_kvm->lock); |
| 1753 | |
| 1754 | if (!sev_guest(source_kvm)) { |
| 1755 | ret = -EINVAL; |
| 1756 | goto e_source_unlock; |
| 1757 | } |
| 1758 | |
| 1759 | /* Mirrors of mirrors should work, but let's not get silly */ |
| 1760 | if (is_mirroring_enc_context(source_kvm) || source_kvm == kvm) { |
| 1761 | ret = -EINVAL; |
| 1762 | goto e_source_unlock; |
| 1763 | } |
| 1764 | |
Peter Gonda | f43c887 | 2021-09-21 08:03:44 -0700 | [diff] [blame] | 1765 | memcpy(&source_sev, &to_kvm_svm(source_kvm)->sev_info, |
| 1766 | sizeof(source_sev)); |
Nathan Tempelman | 54526d1 | 2021-04-08 22:32:14 +0000 | [diff] [blame] | 1767 | |
| 1768 | /* |
| 1769 | * The mirror kvm holds an enc_context_owner ref so its asid can't |
| 1770 | * disappear until we're done with it |
| 1771 | */ |
| 1772 | kvm_get_kvm(source_kvm); |
| 1773 | |
| 1774 | fput(source_kvm_file); |
| 1775 | mutex_unlock(&source_kvm->lock); |
| 1776 | mutex_lock(&kvm->lock); |
| 1777 | |
| 1778 | if (sev_guest(kvm)) { |
| 1779 | ret = -EINVAL; |
| 1780 | goto e_mirror_unlock; |
| 1781 | } |
| 1782 | |
| 1783 | /* Set enc_context_owner and copy its encryption context over */ |
| 1784 | mirror_sev = &to_kvm_svm(kvm)->sev_info; |
| 1785 | mirror_sev->enc_context_owner = source_kvm; |
Nathan Tempelman | 54526d1 | 2021-04-08 22:32:14 +0000 | [diff] [blame] | 1786 | mirror_sev->active = true; |
Peter Gonda | f43c887 | 2021-09-21 08:03:44 -0700 | [diff] [blame] | 1787 | mirror_sev->asid = source_sev.asid; |
| 1788 | mirror_sev->fd = source_sev.fd; |
| 1789 | mirror_sev->es_active = source_sev.es_active; |
| 1790 | mirror_sev->handle = source_sev.handle; |
| 1791 | /* |
| 1792 | * Do not copy ap_jump_table. Since the mirror does not share the same |
| 1793 | * KVM contexts as the original, and they may have different |
| 1794 | * memory-views. |
| 1795 | */ |
Nathan Tempelman | 54526d1 | 2021-04-08 22:32:14 +0000 | [diff] [blame] | 1796 | |
| 1797 | mutex_unlock(&kvm->lock); |
| 1798 | return 0; |
| 1799 | |
| 1800 | e_mirror_unlock: |
| 1801 | mutex_unlock(&kvm->lock); |
| 1802 | kvm_put_kvm(source_kvm); |
| 1803 | return ret; |
| 1804 | e_source_unlock: |
| 1805 | mutex_unlock(&source_kvm->lock); |
| 1806 | e_source_put: |
Colin Ian King | 8899a5f | 2021-04-30 18:03:03 +0100 | [diff] [blame] | 1807 | if (source_kvm_file) |
| 1808 | fput(source_kvm_file); |
Nathan Tempelman | 54526d1 | 2021-04-08 22:32:14 +0000 | [diff] [blame] | 1809 | return ret; |
| 1810 | } |
| 1811 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1812 | void sev_vm_destroy(struct kvm *kvm) |
| 1813 | { |
| 1814 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 1815 | struct list_head *head = &sev->regions_list; |
| 1816 | struct list_head *pos, *q; |
| 1817 | |
| 1818 | if (!sev_guest(kvm)) |
| 1819 | return; |
| 1820 | |
Nathan Tempelman | 54526d1 | 2021-04-08 22:32:14 +0000 | [diff] [blame] | 1821 | /* If this is a mirror_kvm release the enc_context_owner and skip sev cleanup */ |
| 1822 | if (is_mirroring_enc_context(kvm)) { |
| 1823 | kvm_put_kvm(sev->enc_context_owner); |
| 1824 | return; |
| 1825 | } |
| 1826 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1827 | mutex_lock(&kvm->lock); |
| 1828 | |
| 1829 | /* |
| 1830 | * Ensure that all guest tagged cache entries are flushed before |
| 1831 | * releasing the pages back to the system for use. CLFLUSH will |
| 1832 | * not do this, so issue a WBINVD. |
| 1833 | */ |
| 1834 | wbinvd_on_all_cpus(); |
| 1835 | |
| 1836 | /* |
| 1837 | * if userspace was terminated before unregistering the memory regions |
| 1838 | * then lets unpin all the registered memory. |
| 1839 | */ |
| 1840 | if (!list_empty(head)) { |
| 1841 | list_for_each_safe(pos, q, head) { |
| 1842 | __unregister_enc_region_locked(kvm, |
| 1843 | list_entry(pos, struct enc_region, list)); |
David Rientjes | 7be7494 | 2020-08-25 12:56:28 -0700 | [diff] [blame] | 1844 | cond_resched(); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1845 | } |
| 1846 | } |
| 1847 | |
| 1848 | mutex_unlock(&kvm->lock); |
| 1849 | |
| 1850 | sev_unbind_asid(kvm, sev->handle); |
Vipin Sharma | 7aef27f | 2021-03-29 21:42:06 -0700 | [diff] [blame] | 1851 | sev_asid_free(sev); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1852 | } |
| 1853 | |
Paolo Bonzini | d9db0fd | 2021-04-21 19:11:15 -0700 | [diff] [blame] | 1854 | void __init sev_set_cpu_caps(void) |
| 1855 | { |
Sean Christopherson | 8d364a0 | 2021-04-21 19:11:17 -0700 | [diff] [blame] | 1856 | if (!sev_enabled) |
Paolo Bonzini | d9db0fd | 2021-04-21 19:11:15 -0700 | [diff] [blame] | 1857 | kvm_cpu_cap_clear(X86_FEATURE_SEV); |
Sean Christopherson | 8d364a0 | 2021-04-21 19:11:17 -0700 | [diff] [blame] | 1858 | if (!sev_es_enabled) |
Paolo Bonzini | d9db0fd | 2021-04-21 19:11:15 -0700 | [diff] [blame] | 1859 | kvm_cpu_cap_clear(X86_FEATURE_SEV_ES); |
| 1860 | } |
| 1861 | |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1862 | void __init sev_hardware_setup(void) |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1863 | { |
Sean Christopherson | a479c33 | 2021-04-21 19:11:18 -0700 | [diff] [blame] | 1864 | #ifdef CONFIG_KVM_AMD_SEV |
Vipin Sharma | 7aef27f | 2021-03-29 21:42:06 -0700 | [diff] [blame] | 1865 | unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count; |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1866 | bool sev_es_supported = false; |
| 1867 | bool sev_supported = false; |
| 1868 | |
Sean Christopherson | a479c33 | 2021-04-21 19:11:18 -0700 | [diff] [blame] | 1869 | if (!sev_enabled || !npt_enabled) |
Sean Christopherson | e8126bd | 2021-04-21 19:11:14 -0700 | [diff] [blame] | 1870 | goto out; |
| 1871 | |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1872 | /* Does the CPU support SEV? */ |
| 1873 | if (!boot_cpu_has(X86_FEATURE_SEV)) |
| 1874 | goto out; |
| 1875 | |
| 1876 | /* Retrieve SEV CPUID information */ |
| 1877 | cpuid(0x8000001f, &eax, &ebx, &ecx, &edx); |
| 1878 | |
Tom Lendacky | 1edc145 | 2020-12-10 11:09:49 -0600 | [diff] [blame] | 1879 | /* Set encryption bit location for SEV-ES guests */ |
| 1880 | sev_enc_bit = ebx & 0x3f; |
| 1881 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1882 | /* Maximum number of encrypted guests supported simultaneously */ |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1883 | max_sev_asid = ecx; |
Sean Christopherson | 8cb756b | 2021-04-21 19:11:21 -0700 | [diff] [blame] | 1884 | if (!max_sev_asid) |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1885 | goto out; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1886 | |
| 1887 | /* Minimum ASID value that should be used for SEV guest */ |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1888 | min_sev_asid = edx; |
Brijesh Singh | d3d1af8 | 2021-04-15 15:53:55 +0000 | [diff] [blame] | 1889 | sev_me_mask = 1UL << (ebx & 0x3f); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1890 | |
Mingwei Zhang | bb2baeb | 2021-08-02 11:09:03 -0700 | [diff] [blame] | 1891 | /* |
| 1892 | * Initialize SEV ASID bitmaps. Allocate space for ASID 0 in the bitmap, |
| 1893 | * even though it's never used, so that the bitmap is indexed by the |
| 1894 | * actual ASID. |
| 1895 | */ |
| 1896 | nr_asids = max_sev_asid + 1; |
| 1897 | sev_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1898 | if (!sev_asid_bitmap) |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1899 | goto out; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1900 | |
Mingwei Zhang | bb2baeb | 2021-08-02 11:09:03 -0700 | [diff] [blame] | 1901 | sev_reclaim_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL); |
Sean Christopherson | f31b88b | 2021-04-21 19:11:12 -0700 | [diff] [blame] | 1902 | if (!sev_reclaim_asid_bitmap) { |
| 1903 | bitmap_free(sev_asid_bitmap); |
| 1904 | sev_asid_bitmap = NULL; |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1905 | goto out; |
Sean Christopherson | f31b88b | 2021-04-21 19:11:12 -0700 | [diff] [blame] | 1906 | } |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1907 | |
Vipin Sharma | 7aef27f | 2021-03-29 21:42:06 -0700 | [diff] [blame] | 1908 | sev_asid_count = max_sev_asid - min_sev_asid + 1; |
| 1909 | if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count)) |
| 1910 | goto out; |
| 1911 | |
| 1912 | pr_info("SEV supported: %u ASIDs\n", sev_asid_count); |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1913 | sev_supported = true; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1914 | |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1915 | /* SEV-ES support requested? */ |
Sean Christopherson | 8d364a0 | 2021-04-21 19:11:17 -0700 | [diff] [blame] | 1916 | if (!sev_es_enabled) |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1917 | goto out; |
| 1918 | |
| 1919 | /* Does the CPU support SEV-ES? */ |
| 1920 | if (!boot_cpu_has(X86_FEATURE_SEV_ES)) |
| 1921 | goto out; |
| 1922 | |
| 1923 | /* Has the system been allocated ASIDs for SEV-ES? */ |
| 1924 | if (min_sev_asid == 1) |
| 1925 | goto out; |
| 1926 | |
Vipin Sharma | 7aef27f | 2021-03-29 21:42:06 -0700 | [diff] [blame] | 1927 | sev_es_asid_count = min_sev_asid - 1; |
| 1928 | if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count)) |
| 1929 | goto out; |
| 1930 | |
| 1931 | pr_info("SEV-ES supported: %u ASIDs\n", sev_es_asid_count); |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1932 | sev_es_supported = true; |
| 1933 | |
| 1934 | out: |
Sean Christopherson | 8d364a0 | 2021-04-21 19:11:17 -0700 | [diff] [blame] | 1935 | sev_enabled = sev_supported; |
| 1936 | sev_es_enabled = sev_es_supported; |
Sean Christopherson | a479c33 | 2021-04-21 19:11:18 -0700 | [diff] [blame] | 1937 | #endif |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1938 | } |
| 1939 | |
| 1940 | void sev_hardware_teardown(void) |
| 1941 | { |
Sean Christopherson | a5c1c5a | 2021-04-21 19:11:23 -0700 | [diff] [blame] | 1942 | if (!sev_enabled) |
Paolo Bonzini | 9ef1530 | 2020-04-13 03:20:06 -0400 | [diff] [blame] | 1943 | return; |
| 1944 | |
Sean Christopherson | 469bb32 | 2021-04-21 19:11:25 -0700 | [diff] [blame] | 1945 | /* No need to take sev_bitmap_lock, all VMs have been destroyed. */ |
Mingwei Zhang | bb2baeb | 2021-08-02 11:09:03 -0700 | [diff] [blame] | 1946 | sev_flush_asids(1, max_sev_asid); |
Sean Christopherson | 469bb32 | 2021-04-21 19:11:25 -0700 | [diff] [blame] | 1947 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1948 | bitmap_free(sev_asid_bitmap); |
| 1949 | bitmap_free(sev_reclaim_asid_bitmap); |
Sean Christopherson | 469bb32 | 2021-04-21 19:11:25 -0700 | [diff] [blame] | 1950 | |
Vipin Sharma | 7aef27f | 2021-03-29 21:42:06 -0700 | [diff] [blame] | 1951 | misc_cg_set_capacity(MISC_CG_RES_SEV, 0); |
| 1952 | misc_cg_set_capacity(MISC_CG_RES_SEV_ES, 0); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1953 | } |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1954 | |
Sean Christopherson | b95c221 | 2021-04-21 19:11:22 -0700 | [diff] [blame] | 1955 | int sev_cpu_init(struct svm_cpu_data *sd) |
| 1956 | { |
Sean Christopherson | a5c1c5a | 2021-04-21 19:11:23 -0700 | [diff] [blame] | 1957 | if (!sev_enabled) |
Sean Christopherson | b95c221 | 2021-04-21 19:11:22 -0700 | [diff] [blame] | 1958 | return 0; |
| 1959 | |
Mingwei Zhang | bb2baeb | 2021-08-02 11:09:03 -0700 | [diff] [blame] | 1960 | sd->sev_vmcbs = kcalloc(nr_asids, sizeof(void *), GFP_KERNEL); |
Sean Christopherson | b95c221 | 2021-04-21 19:11:22 -0700 | [diff] [blame] | 1961 | if (!sd->sev_vmcbs) |
| 1962 | return -ENOMEM; |
| 1963 | |
| 1964 | return 0; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1965 | } |
| 1966 | |
Tom Lendacky | add5e2f | 2020-12-10 11:09:40 -0600 | [diff] [blame] | 1967 | /* |
| 1968 | * Pages used by hardware to hold guest encrypted state must be flushed before |
| 1969 | * returning them to the system. |
| 1970 | */ |
| 1971 | static void sev_flush_guest_memory(struct vcpu_svm *svm, void *va, |
| 1972 | unsigned long len) |
| 1973 | { |
| 1974 | /* |
| 1975 | * If hardware enforced cache coherency for encrypted mappings of the |
| 1976 | * same physical page is supported, nothing to do. |
| 1977 | */ |
| 1978 | if (boot_cpu_has(X86_FEATURE_SME_COHERENT)) |
| 1979 | return; |
| 1980 | |
| 1981 | /* |
| 1982 | * If the VM Page Flush MSR is supported, use it to flush the page |
| 1983 | * (using the page virtual address and the guest ASID). |
| 1984 | */ |
| 1985 | if (boot_cpu_has(X86_FEATURE_VM_PAGE_FLUSH)) { |
| 1986 | struct kvm_sev_info *sev; |
| 1987 | unsigned long va_start; |
| 1988 | u64 start, stop; |
| 1989 | |
| 1990 | /* Align start and stop to page boundaries. */ |
| 1991 | va_start = (unsigned long)va; |
| 1992 | start = (u64)va_start & PAGE_MASK; |
| 1993 | stop = PAGE_ALIGN((u64)va_start + len); |
| 1994 | |
| 1995 | if (start < stop) { |
| 1996 | sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info; |
| 1997 | |
| 1998 | while (start < stop) { |
| 1999 | wrmsrl(MSR_AMD64_VM_PAGE_FLUSH, |
| 2000 | start | sev->asid); |
| 2001 | |
| 2002 | start += PAGE_SIZE; |
| 2003 | } |
| 2004 | |
| 2005 | return; |
| 2006 | } |
| 2007 | |
| 2008 | WARN(1, "Address overflow, using WBINVD\n"); |
| 2009 | } |
| 2010 | |
| 2011 | /* |
| 2012 | * Hardware should always have one of the above features, |
| 2013 | * but if not, use WBINVD and issue a warning. |
| 2014 | */ |
| 2015 | WARN_ONCE(1, "Using WBINVD to flush guest memory\n"); |
| 2016 | wbinvd_on_all_cpus(); |
| 2017 | } |
| 2018 | |
| 2019 | void sev_free_vcpu(struct kvm_vcpu *vcpu) |
| 2020 | { |
| 2021 | struct vcpu_svm *svm; |
| 2022 | |
| 2023 | if (!sev_es_guest(vcpu->kvm)) |
| 2024 | return; |
| 2025 | |
| 2026 | svm = to_svm(vcpu); |
| 2027 | |
| 2028 | if (vcpu->arch.guest_state_protected) |
| 2029 | sev_flush_guest_memory(svm, svm->vmsa, PAGE_SIZE); |
| 2030 | __free_page(virt_to_page(svm->vmsa)); |
Tom Lendacky | 8f423a8 | 2020-12-10 11:09:53 -0600 | [diff] [blame] | 2031 | |
| 2032 | if (svm->ghcb_sa_free) |
| 2033 | kfree(svm->ghcb_sa); |
Tom Lendacky | add5e2f | 2020-12-10 11:09:40 -0600 | [diff] [blame] | 2034 | } |
| 2035 | |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2036 | static void dump_ghcb(struct vcpu_svm *svm) |
| 2037 | { |
| 2038 | struct ghcb *ghcb = svm->ghcb; |
| 2039 | unsigned int nbits; |
| 2040 | |
| 2041 | /* Re-use the dump_invalid_vmcb module parameter */ |
| 2042 | if (!dump_invalid_vmcb) { |
| 2043 | pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n"); |
| 2044 | return; |
| 2045 | } |
| 2046 | |
| 2047 | nbits = sizeof(ghcb->save.valid_bitmap) * 8; |
| 2048 | |
| 2049 | pr_err("GHCB (GPA=%016llx):\n", svm->vmcb->control.ghcb_gpa); |
| 2050 | pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_code", |
| 2051 | ghcb->save.sw_exit_code, ghcb_sw_exit_code_is_valid(ghcb)); |
| 2052 | pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_1", |
| 2053 | ghcb->save.sw_exit_info_1, ghcb_sw_exit_info_1_is_valid(ghcb)); |
| 2054 | pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_2", |
| 2055 | ghcb->save.sw_exit_info_2, ghcb_sw_exit_info_2_is_valid(ghcb)); |
| 2056 | pr_err("%-20s%016llx is_valid: %u\n", "sw_scratch", |
| 2057 | ghcb->save.sw_scratch, ghcb_sw_scratch_is_valid(ghcb)); |
| 2058 | pr_err("%-20s%*pb\n", "valid_bitmap", nbits, ghcb->save.valid_bitmap); |
| 2059 | } |
| 2060 | |
| 2061 | static void sev_es_sync_to_ghcb(struct vcpu_svm *svm) |
| 2062 | { |
| 2063 | struct kvm_vcpu *vcpu = &svm->vcpu; |
| 2064 | struct ghcb *ghcb = svm->ghcb; |
| 2065 | |
| 2066 | /* |
| 2067 | * The GHCB protocol so far allows for the following data |
| 2068 | * to be returned: |
| 2069 | * GPRs RAX, RBX, RCX, RDX |
| 2070 | * |
Sean Christopherson | 2500914 | 2021-01-22 15:50:47 -0800 | [diff] [blame] | 2071 | * Copy their values, even if they may not have been written during the |
| 2072 | * VM-Exit. It's the guest's responsibility to not consume random data. |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2073 | */ |
Sean Christopherson | 2500914 | 2021-01-22 15:50:47 -0800 | [diff] [blame] | 2074 | ghcb_set_rax(ghcb, vcpu->arch.regs[VCPU_REGS_RAX]); |
| 2075 | ghcb_set_rbx(ghcb, vcpu->arch.regs[VCPU_REGS_RBX]); |
| 2076 | ghcb_set_rcx(ghcb, vcpu->arch.regs[VCPU_REGS_RCX]); |
| 2077 | ghcb_set_rdx(ghcb, vcpu->arch.regs[VCPU_REGS_RDX]); |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | static void sev_es_sync_from_ghcb(struct vcpu_svm *svm) |
| 2081 | { |
| 2082 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 2083 | struct kvm_vcpu *vcpu = &svm->vcpu; |
| 2084 | struct ghcb *ghcb = svm->ghcb; |
| 2085 | u64 exit_code; |
| 2086 | |
| 2087 | /* |
| 2088 | * The GHCB protocol so far allows for the following data |
| 2089 | * to be supplied: |
| 2090 | * GPRs RAX, RBX, RCX, RDX |
| 2091 | * XCR0 |
| 2092 | * CPL |
| 2093 | * |
| 2094 | * VMMCALL allows the guest to provide extra registers. KVM also |
| 2095 | * expects RSI for hypercalls, so include that, too. |
| 2096 | * |
| 2097 | * Copy their values to the appropriate location if supplied. |
| 2098 | */ |
| 2099 | memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); |
| 2100 | |
| 2101 | vcpu->arch.regs[VCPU_REGS_RAX] = ghcb_get_rax_if_valid(ghcb); |
| 2102 | vcpu->arch.regs[VCPU_REGS_RBX] = ghcb_get_rbx_if_valid(ghcb); |
| 2103 | vcpu->arch.regs[VCPU_REGS_RCX] = ghcb_get_rcx_if_valid(ghcb); |
| 2104 | vcpu->arch.regs[VCPU_REGS_RDX] = ghcb_get_rdx_if_valid(ghcb); |
| 2105 | vcpu->arch.regs[VCPU_REGS_RSI] = ghcb_get_rsi_if_valid(ghcb); |
| 2106 | |
| 2107 | svm->vmcb->save.cpl = ghcb_get_cpl_if_valid(ghcb); |
| 2108 | |
| 2109 | if (ghcb_xcr0_is_valid(ghcb)) { |
| 2110 | vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb); |
| 2111 | kvm_update_cpuid_runtime(vcpu); |
| 2112 | } |
| 2113 | |
| 2114 | /* Copy the GHCB exit information into the VMCB fields */ |
| 2115 | exit_code = ghcb_get_sw_exit_code(ghcb); |
| 2116 | control->exit_code = lower_32_bits(exit_code); |
| 2117 | control->exit_code_hi = upper_32_bits(exit_code); |
| 2118 | control->exit_info_1 = ghcb_get_sw_exit_info_1(ghcb); |
| 2119 | control->exit_info_2 = ghcb_get_sw_exit_info_2(ghcb); |
| 2120 | |
| 2121 | /* Clear the valid entries fields */ |
| 2122 | memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap)); |
| 2123 | } |
| 2124 | |
| 2125 | static int sev_es_validate_vmgexit(struct vcpu_svm *svm) |
| 2126 | { |
| 2127 | struct kvm_vcpu *vcpu; |
| 2128 | struct ghcb *ghcb; |
| 2129 | u64 exit_code = 0; |
| 2130 | |
| 2131 | ghcb = svm->ghcb; |
| 2132 | |
| 2133 | /* Only GHCB Usage code 0 is supported */ |
| 2134 | if (ghcb->ghcb_usage) |
| 2135 | goto vmgexit_err; |
| 2136 | |
| 2137 | /* |
| 2138 | * Retrieve the exit code now even though is may not be marked valid |
| 2139 | * as it could help with debugging. |
| 2140 | */ |
| 2141 | exit_code = ghcb_get_sw_exit_code(ghcb); |
| 2142 | |
| 2143 | if (!ghcb_sw_exit_code_is_valid(ghcb) || |
| 2144 | !ghcb_sw_exit_info_1_is_valid(ghcb) || |
| 2145 | !ghcb_sw_exit_info_2_is_valid(ghcb)) |
| 2146 | goto vmgexit_err; |
| 2147 | |
| 2148 | switch (ghcb_get_sw_exit_code(ghcb)) { |
| 2149 | case SVM_EXIT_READ_DR7: |
| 2150 | break; |
| 2151 | case SVM_EXIT_WRITE_DR7: |
| 2152 | if (!ghcb_rax_is_valid(ghcb)) |
| 2153 | goto vmgexit_err; |
| 2154 | break; |
| 2155 | case SVM_EXIT_RDTSC: |
| 2156 | break; |
| 2157 | case SVM_EXIT_RDPMC: |
| 2158 | if (!ghcb_rcx_is_valid(ghcb)) |
| 2159 | goto vmgexit_err; |
| 2160 | break; |
| 2161 | case SVM_EXIT_CPUID: |
| 2162 | if (!ghcb_rax_is_valid(ghcb) || |
| 2163 | !ghcb_rcx_is_valid(ghcb)) |
| 2164 | goto vmgexit_err; |
| 2165 | if (ghcb_get_rax(ghcb) == 0xd) |
| 2166 | if (!ghcb_xcr0_is_valid(ghcb)) |
| 2167 | goto vmgexit_err; |
| 2168 | break; |
| 2169 | case SVM_EXIT_INVD: |
| 2170 | break; |
| 2171 | case SVM_EXIT_IOIO: |
Tom Lendacky | 7ed9abf | 2020-12-10 11:09:54 -0600 | [diff] [blame] | 2172 | if (ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_STR_MASK) { |
| 2173 | if (!ghcb_sw_scratch_is_valid(ghcb)) |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2174 | goto vmgexit_err; |
Tom Lendacky | 7ed9abf | 2020-12-10 11:09:54 -0600 | [diff] [blame] | 2175 | } else { |
| 2176 | if (!(ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_TYPE_MASK)) |
| 2177 | if (!ghcb_rax_is_valid(ghcb)) |
| 2178 | goto vmgexit_err; |
| 2179 | } |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2180 | break; |
| 2181 | case SVM_EXIT_MSR: |
| 2182 | if (!ghcb_rcx_is_valid(ghcb)) |
| 2183 | goto vmgexit_err; |
| 2184 | if (ghcb_get_sw_exit_info_1(ghcb)) { |
| 2185 | if (!ghcb_rax_is_valid(ghcb) || |
| 2186 | !ghcb_rdx_is_valid(ghcb)) |
| 2187 | goto vmgexit_err; |
| 2188 | } |
| 2189 | break; |
| 2190 | case SVM_EXIT_VMMCALL: |
| 2191 | if (!ghcb_rax_is_valid(ghcb) || |
| 2192 | !ghcb_cpl_is_valid(ghcb)) |
| 2193 | goto vmgexit_err; |
| 2194 | break; |
| 2195 | case SVM_EXIT_RDTSCP: |
| 2196 | break; |
| 2197 | case SVM_EXIT_WBINVD: |
| 2198 | break; |
| 2199 | case SVM_EXIT_MONITOR: |
| 2200 | if (!ghcb_rax_is_valid(ghcb) || |
| 2201 | !ghcb_rcx_is_valid(ghcb) || |
| 2202 | !ghcb_rdx_is_valid(ghcb)) |
| 2203 | goto vmgexit_err; |
| 2204 | break; |
| 2205 | case SVM_EXIT_MWAIT: |
| 2206 | if (!ghcb_rax_is_valid(ghcb) || |
| 2207 | !ghcb_rcx_is_valid(ghcb)) |
| 2208 | goto vmgexit_err; |
| 2209 | break; |
Tom Lendacky | 8f423a8 | 2020-12-10 11:09:53 -0600 | [diff] [blame] | 2210 | case SVM_VMGEXIT_MMIO_READ: |
| 2211 | case SVM_VMGEXIT_MMIO_WRITE: |
| 2212 | if (!ghcb_sw_scratch_is_valid(ghcb)) |
| 2213 | goto vmgexit_err; |
| 2214 | break; |
Tom Lendacky | 4444dfe | 2020-12-14 11:16:03 -0500 | [diff] [blame] | 2215 | case SVM_VMGEXIT_NMI_COMPLETE: |
Tom Lendacky | 647daca | 2021-01-04 14:20:01 -0600 | [diff] [blame] | 2216 | case SVM_VMGEXIT_AP_HLT_LOOP: |
Tom Lendacky | 8640ca5 | 2020-12-15 12:44:07 -0500 | [diff] [blame] | 2217 | case SVM_VMGEXIT_AP_JUMP_TABLE: |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2218 | case SVM_VMGEXIT_UNSUPPORTED_EVENT: |
| 2219 | break; |
| 2220 | default: |
| 2221 | goto vmgexit_err; |
| 2222 | } |
| 2223 | |
| 2224 | return 0; |
| 2225 | |
| 2226 | vmgexit_err: |
| 2227 | vcpu = &svm->vcpu; |
| 2228 | |
| 2229 | if (ghcb->ghcb_usage) { |
| 2230 | vcpu_unimpl(vcpu, "vmgexit: ghcb usage %#x is not valid\n", |
| 2231 | ghcb->ghcb_usage); |
| 2232 | } else { |
| 2233 | vcpu_unimpl(vcpu, "vmgexit: exit reason %#llx is not valid\n", |
| 2234 | exit_code); |
| 2235 | dump_ghcb(svm); |
| 2236 | } |
| 2237 | |
| 2238 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 2239 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON; |
| 2240 | vcpu->run->internal.ndata = 2; |
| 2241 | vcpu->run->internal.data[0] = exit_code; |
| 2242 | vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu; |
| 2243 | |
| 2244 | return -EINVAL; |
| 2245 | } |
| 2246 | |
Tom Lendacky | ce7ea0c | 2021-05-06 15:14:41 -0500 | [diff] [blame] | 2247 | void sev_es_unmap_ghcb(struct vcpu_svm *svm) |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2248 | { |
| 2249 | if (!svm->ghcb) |
| 2250 | return; |
| 2251 | |
Tom Lendacky | 8f423a8 | 2020-12-10 11:09:53 -0600 | [diff] [blame] | 2252 | if (svm->ghcb_sa_free) { |
| 2253 | /* |
| 2254 | * The scratch area lives outside the GHCB, so there is a |
| 2255 | * buffer that, depending on the operation performed, may |
| 2256 | * need to be synced, then freed. |
| 2257 | */ |
| 2258 | if (svm->ghcb_sa_sync) { |
| 2259 | kvm_write_guest(svm->vcpu.kvm, |
| 2260 | ghcb_get_sw_scratch(svm->ghcb), |
| 2261 | svm->ghcb_sa, svm->ghcb_sa_len); |
| 2262 | svm->ghcb_sa_sync = false; |
| 2263 | } |
| 2264 | |
| 2265 | kfree(svm->ghcb_sa); |
| 2266 | svm->ghcb_sa = NULL; |
| 2267 | svm->ghcb_sa_free = false; |
| 2268 | } |
| 2269 | |
Tom Lendacky | d523ab6b | 2020-12-10 11:09:48 -0600 | [diff] [blame] | 2270 | trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, svm->ghcb); |
| 2271 | |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2272 | sev_es_sync_to_ghcb(svm); |
| 2273 | |
| 2274 | kvm_vcpu_unmap(&svm->vcpu, &svm->ghcb_map, true); |
| 2275 | svm->ghcb = NULL; |
| 2276 | } |
| 2277 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 2278 | void pre_sev_run(struct vcpu_svm *svm, int cpu) |
| 2279 | { |
| 2280 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
| 2281 | int asid = sev_get_asid(svm->vcpu.kvm); |
| 2282 | |
| 2283 | /* Assign the asid allocated with this SEV guest */ |
Paolo Bonzini | dee734a | 2020-11-30 09:39:59 -0500 | [diff] [blame] | 2284 | svm->asid = asid; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 2285 | |
| 2286 | /* |
| 2287 | * Flush guest TLB: |
| 2288 | * |
| 2289 | * 1) when different VMCB for the same ASID is to be run on the same host CPU. |
| 2290 | * 2) or this VMCB was executed on different host CPU in previous VMRUNs. |
| 2291 | */ |
| 2292 | if (sd->sev_vmcbs[asid] == svm->vmcb && |
Jim Mattson | 8a14fe4 | 2020-06-03 16:56:22 -0700 | [diff] [blame] | 2293 | svm->vcpu.arch.last_vmentry_cpu == cpu) |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 2294 | return; |
| 2295 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 2296 | sd->sev_vmcbs[asid] = svm->vmcb; |
| 2297 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 2298 | vmcb_mark_dirty(svm->vmcb, VMCB_ASID); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 2299 | } |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2300 | |
Tom Lendacky | 8f423a8 | 2020-12-10 11:09:53 -0600 | [diff] [blame] | 2301 | #define GHCB_SCRATCH_AREA_LIMIT (16ULL * PAGE_SIZE) |
| 2302 | static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) |
| 2303 | { |
| 2304 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 2305 | struct ghcb *ghcb = svm->ghcb; |
| 2306 | u64 ghcb_scratch_beg, ghcb_scratch_end; |
| 2307 | u64 scratch_gpa_beg, scratch_gpa_end; |
| 2308 | void *scratch_va; |
| 2309 | |
| 2310 | scratch_gpa_beg = ghcb_get_sw_scratch(ghcb); |
| 2311 | if (!scratch_gpa_beg) { |
| 2312 | pr_err("vmgexit: scratch gpa not provided\n"); |
| 2313 | return false; |
| 2314 | } |
| 2315 | |
| 2316 | scratch_gpa_end = scratch_gpa_beg + len; |
| 2317 | if (scratch_gpa_end < scratch_gpa_beg) { |
| 2318 | pr_err("vmgexit: scratch length (%#llx) not valid for scratch address (%#llx)\n", |
| 2319 | len, scratch_gpa_beg); |
| 2320 | return false; |
| 2321 | } |
| 2322 | |
| 2323 | if ((scratch_gpa_beg & PAGE_MASK) == control->ghcb_gpa) { |
| 2324 | /* Scratch area begins within GHCB */ |
| 2325 | ghcb_scratch_beg = control->ghcb_gpa + |
| 2326 | offsetof(struct ghcb, shared_buffer); |
| 2327 | ghcb_scratch_end = control->ghcb_gpa + |
| 2328 | offsetof(struct ghcb, reserved_1); |
| 2329 | |
| 2330 | /* |
| 2331 | * If the scratch area begins within the GHCB, it must be |
| 2332 | * completely contained in the GHCB shared buffer area. |
| 2333 | */ |
| 2334 | if (scratch_gpa_beg < ghcb_scratch_beg || |
| 2335 | scratch_gpa_end > ghcb_scratch_end) { |
| 2336 | pr_err("vmgexit: scratch area is outside of GHCB shared buffer area (%#llx - %#llx)\n", |
| 2337 | scratch_gpa_beg, scratch_gpa_end); |
| 2338 | return false; |
| 2339 | } |
| 2340 | |
| 2341 | scratch_va = (void *)svm->ghcb; |
| 2342 | scratch_va += (scratch_gpa_beg - control->ghcb_gpa); |
| 2343 | } else { |
| 2344 | /* |
| 2345 | * The guest memory must be read into a kernel buffer, so |
| 2346 | * limit the size |
| 2347 | */ |
| 2348 | if (len > GHCB_SCRATCH_AREA_LIMIT) { |
| 2349 | pr_err("vmgexit: scratch area exceeds KVM limits (%#llx requested, %#llx limit)\n", |
| 2350 | len, GHCB_SCRATCH_AREA_LIMIT); |
| 2351 | return false; |
| 2352 | } |
Sean Christopherson | eba04b2 | 2021-03-30 19:30:25 -0700 | [diff] [blame] | 2353 | scratch_va = kzalloc(len, GFP_KERNEL_ACCOUNT); |
Tom Lendacky | 8f423a8 | 2020-12-10 11:09:53 -0600 | [diff] [blame] | 2354 | if (!scratch_va) |
| 2355 | return false; |
| 2356 | |
| 2357 | if (kvm_read_guest(svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) { |
| 2358 | /* Unable to copy scratch area from guest */ |
| 2359 | pr_err("vmgexit: kvm_read_guest for scratch area failed\n"); |
| 2360 | |
| 2361 | kfree(scratch_va); |
| 2362 | return false; |
| 2363 | } |
| 2364 | |
| 2365 | /* |
| 2366 | * The scratch area is outside the GHCB. The operation will |
| 2367 | * dictate whether the buffer needs to be synced before running |
| 2368 | * the vCPU next time (i.e. a read was requested so the data |
| 2369 | * must be written back to the guest memory). |
| 2370 | */ |
| 2371 | svm->ghcb_sa_sync = sync; |
| 2372 | svm->ghcb_sa_free = true; |
| 2373 | } |
| 2374 | |
| 2375 | svm->ghcb_sa = scratch_va; |
| 2376 | svm->ghcb_sa_len = len; |
| 2377 | |
| 2378 | return true; |
| 2379 | } |
| 2380 | |
Tom Lendacky | d369466 | 2020-12-10 11:09:50 -0600 | [diff] [blame] | 2381 | static void set_ghcb_msr_bits(struct vcpu_svm *svm, u64 value, u64 mask, |
| 2382 | unsigned int pos) |
| 2383 | { |
| 2384 | svm->vmcb->control.ghcb_gpa &= ~(mask << pos); |
| 2385 | svm->vmcb->control.ghcb_gpa |= (value & mask) << pos; |
| 2386 | } |
| 2387 | |
| 2388 | static u64 get_ghcb_msr_bits(struct vcpu_svm *svm, u64 mask, unsigned int pos) |
| 2389 | { |
| 2390 | return (svm->vmcb->control.ghcb_gpa >> pos) & mask; |
| 2391 | } |
| 2392 | |
Tom Lendacky | 1edc145 | 2020-12-10 11:09:49 -0600 | [diff] [blame] | 2393 | static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) |
| 2394 | { |
| 2395 | svm->vmcb->control.ghcb_gpa = value; |
| 2396 | } |
| 2397 | |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2398 | static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) |
| 2399 | { |
Tom Lendacky | 1edc145 | 2020-12-10 11:09:49 -0600 | [diff] [blame] | 2400 | struct vmcb_control_area *control = &svm->vmcb->control; |
Tom Lendacky | d369466 | 2020-12-10 11:09:50 -0600 | [diff] [blame] | 2401 | struct kvm_vcpu *vcpu = &svm->vcpu; |
Tom Lendacky | 1edc145 | 2020-12-10 11:09:49 -0600 | [diff] [blame] | 2402 | u64 ghcb_info; |
Tom Lendacky | d369466 | 2020-12-10 11:09:50 -0600 | [diff] [blame] | 2403 | int ret = 1; |
Tom Lendacky | 1edc145 | 2020-12-10 11:09:49 -0600 | [diff] [blame] | 2404 | |
| 2405 | ghcb_info = control->ghcb_gpa & GHCB_MSR_INFO_MASK; |
| 2406 | |
Tom Lendacky | 59e38b5 | 2020-12-10 11:09:52 -0600 | [diff] [blame] | 2407 | trace_kvm_vmgexit_msr_protocol_enter(svm->vcpu.vcpu_id, |
| 2408 | control->ghcb_gpa); |
| 2409 | |
Tom Lendacky | 1edc145 | 2020-12-10 11:09:49 -0600 | [diff] [blame] | 2410 | switch (ghcb_info) { |
| 2411 | case GHCB_MSR_SEV_INFO_REQ: |
| 2412 | set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX, |
| 2413 | GHCB_VERSION_MIN, |
| 2414 | sev_enc_bit)); |
| 2415 | break; |
Tom Lendacky | d369466 | 2020-12-10 11:09:50 -0600 | [diff] [blame] | 2416 | case GHCB_MSR_CPUID_REQ: { |
| 2417 | u64 cpuid_fn, cpuid_reg, cpuid_value; |
| 2418 | |
| 2419 | cpuid_fn = get_ghcb_msr_bits(svm, |
| 2420 | GHCB_MSR_CPUID_FUNC_MASK, |
| 2421 | GHCB_MSR_CPUID_FUNC_POS); |
| 2422 | |
| 2423 | /* Initialize the registers needed by the CPUID intercept */ |
| 2424 | vcpu->arch.regs[VCPU_REGS_RAX] = cpuid_fn; |
| 2425 | vcpu->arch.regs[VCPU_REGS_RCX] = 0; |
| 2426 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2427 | ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_CPUID); |
Tom Lendacky | d369466 | 2020-12-10 11:09:50 -0600 | [diff] [blame] | 2428 | if (!ret) { |
| 2429 | ret = -EINVAL; |
| 2430 | break; |
| 2431 | } |
| 2432 | |
| 2433 | cpuid_reg = get_ghcb_msr_bits(svm, |
| 2434 | GHCB_MSR_CPUID_REG_MASK, |
| 2435 | GHCB_MSR_CPUID_REG_POS); |
| 2436 | if (cpuid_reg == 0) |
| 2437 | cpuid_value = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 2438 | else if (cpuid_reg == 1) |
| 2439 | cpuid_value = vcpu->arch.regs[VCPU_REGS_RBX]; |
| 2440 | else if (cpuid_reg == 2) |
| 2441 | cpuid_value = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 2442 | else |
| 2443 | cpuid_value = vcpu->arch.regs[VCPU_REGS_RDX]; |
| 2444 | |
| 2445 | set_ghcb_msr_bits(svm, cpuid_value, |
| 2446 | GHCB_MSR_CPUID_VALUE_MASK, |
| 2447 | GHCB_MSR_CPUID_VALUE_POS); |
| 2448 | |
| 2449 | set_ghcb_msr_bits(svm, GHCB_MSR_CPUID_RESP, |
| 2450 | GHCB_MSR_INFO_MASK, |
| 2451 | GHCB_MSR_INFO_POS); |
| 2452 | break; |
| 2453 | } |
Tom Lendacky | e1d7111 | 2020-12-10 11:09:51 -0600 | [diff] [blame] | 2454 | case GHCB_MSR_TERM_REQ: { |
| 2455 | u64 reason_set, reason_code; |
| 2456 | |
| 2457 | reason_set = get_ghcb_msr_bits(svm, |
| 2458 | GHCB_MSR_TERM_REASON_SET_MASK, |
| 2459 | GHCB_MSR_TERM_REASON_SET_POS); |
| 2460 | reason_code = get_ghcb_msr_bits(svm, |
| 2461 | GHCB_MSR_TERM_REASON_MASK, |
| 2462 | GHCB_MSR_TERM_REASON_POS); |
| 2463 | pr_info("SEV-ES guest requested termination: %#llx:%#llx\n", |
| 2464 | reason_set, reason_code); |
| 2465 | fallthrough; |
| 2466 | } |
Tom Lendacky | 1edc145 | 2020-12-10 11:09:49 -0600 | [diff] [blame] | 2467 | default: |
Tom Lendacky | d369466 | 2020-12-10 11:09:50 -0600 | [diff] [blame] | 2468 | ret = -EINVAL; |
Tom Lendacky | 1edc145 | 2020-12-10 11:09:49 -0600 | [diff] [blame] | 2469 | } |
| 2470 | |
Tom Lendacky | 59e38b5 | 2020-12-10 11:09:52 -0600 | [diff] [blame] | 2471 | trace_kvm_vmgexit_msr_protocol_exit(svm->vcpu.vcpu_id, |
| 2472 | control->ghcb_gpa, ret); |
| 2473 | |
Tom Lendacky | d369466 | 2020-12-10 11:09:50 -0600 | [diff] [blame] | 2474 | return ret; |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2475 | } |
| 2476 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2477 | int sev_handle_vmgexit(struct kvm_vcpu *vcpu) |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2478 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2479 | struct vcpu_svm *svm = to_svm(vcpu); |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2480 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 2481 | u64 ghcb_gpa, exit_code; |
| 2482 | struct ghcb *ghcb; |
| 2483 | int ret; |
| 2484 | |
| 2485 | /* Validate the GHCB */ |
| 2486 | ghcb_gpa = control->ghcb_gpa; |
| 2487 | if (ghcb_gpa & GHCB_MSR_INFO_MASK) |
| 2488 | return sev_handle_vmgexit_msr_protocol(svm); |
| 2489 | |
| 2490 | if (!ghcb_gpa) { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2491 | vcpu_unimpl(vcpu, "vmgexit: GHCB gpa is not set\n"); |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2492 | return -EINVAL; |
| 2493 | } |
| 2494 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2495 | if (kvm_vcpu_map(vcpu, ghcb_gpa >> PAGE_SHIFT, &svm->ghcb_map)) { |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2496 | /* Unable to map GHCB from guest */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2497 | vcpu_unimpl(vcpu, "vmgexit: error mapping GHCB [%#llx] from guest\n", |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2498 | ghcb_gpa); |
| 2499 | return -EINVAL; |
| 2500 | } |
| 2501 | |
| 2502 | svm->ghcb = svm->ghcb_map.hva; |
| 2503 | ghcb = svm->ghcb_map.hva; |
| 2504 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2505 | trace_kvm_vmgexit_enter(vcpu->vcpu_id, ghcb); |
Tom Lendacky | d523ab6b | 2020-12-10 11:09:48 -0600 | [diff] [blame] | 2506 | |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2507 | exit_code = ghcb_get_sw_exit_code(ghcb); |
| 2508 | |
| 2509 | ret = sev_es_validate_vmgexit(svm); |
| 2510 | if (ret) |
| 2511 | return ret; |
| 2512 | |
| 2513 | sev_es_sync_from_ghcb(svm); |
| 2514 | ghcb_set_sw_exit_info_1(ghcb, 0); |
| 2515 | ghcb_set_sw_exit_info_2(ghcb, 0); |
| 2516 | |
| 2517 | ret = -EINVAL; |
| 2518 | switch (exit_code) { |
Tom Lendacky | 8f423a8 | 2020-12-10 11:09:53 -0600 | [diff] [blame] | 2519 | case SVM_VMGEXIT_MMIO_READ: |
| 2520 | if (!setup_vmgexit_scratch(svm, true, control->exit_info_2)) |
| 2521 | break; |
| 2522 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2523 | ret = kvm_sev_es_mmio_read(vcpu, |
Tom Lendacky | 8f423a8 | 2020-12-10 11:09:53 -0600 | [diff] [blame] | 2524 | control->exit_info_1, |
| 2525 | control->exit_info_2, |
| 2526 | svm->ghcb_sa); |
| 2527 | break; |
| 2528 | case SVM_VMGEXIT_MMIO_WRITE: |
| 2529 | if (!setup_vmgexit_scratch(svm, false, control->exit_info_2)) |
| 2530 | break; |
| 2531 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2532 | ret = kvm_sev_es_mmio_write(vcpu, |
Tom Lendacky | 8f423a8 | 2020-12-10 11:09:53 -0600 | [diff] [blame] | 2533 | control->exit_info_1, |
| 2534 | control->exit_info_2, |
| 2535 | svm->ghcb_sa); |
| 2536 | break; |
Tom Lendacky | 4444dfe | 2020-12-14 11:16:03 -0500 | [diff] [blame] | 2537 | case SVM_VMGEXIT_NMI_COMPLETE: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2538 | ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_IRET); |
Tom Lendacky | 4444dfe | 2020-12-14 11:16:03 -0500 | [diff] [blame] | 2539 | break; |
Tom Lendacky | 647daca | 2021-01-04 14:20:01 -0600 | [diff] [blame] | 2540 | case SVM_VMGEXIT_AP_HLT_LOOP: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2541 | ret = kvm_emulate_ap_reset_hold(vcpu); |
Tom Lendacky | 647daca | 2021-01-04 14:20:01 -0600 | [diff] [blame] | 2542 | break; |
Tom Lendacky | 8640ca5 | 2020-12-15 12:44:07 -0500 | [diff] [blame] | 2543 | case SVM_VMGEXIT_AP_JUMP_TABLE: { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2544 | struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info; |
Tom Lendacky | 8640ca5 | 2020-12-15 12:44:07 -0500 | [diff] [blame] | 2545 | |
| 2546 | switch (control->exit_info_1) { |
| 2547 | case 0: |
| 2548 | /* Set AP jump table address */ |
| 2549 | sev->ap_jump_table = control->exit_info_2; |
| 2550 | break; |
| 2551 | case 1: |
| 2552 | /* Get AP jump table address */ |
| 2553 | ghcb_set_sw_exit_info_2(ghcb, sev->ap_jump_table); |
| 2554 | break; |
| 2555 | default: |
| 2556 | pr_err("svm: vmgexit: unsupported AP jump table request - exit_info_1=%#llx\n", |
| 2557 | control->exit_info_1); |
| 2558 | ghcb_set_sw_exit_info_1(ghcb, 1); |
| 2559 | ghcb_set_sw_exit_info_2(ghcb, |
| 2560 | X86_TRAP_UD | |
| 2561 | SVM_EVTINJ_TYPE_EXEPT | |
| 2562 | SVM_EVTINJ_VALID); |
| 2563 | } |
| 2564 | |
| 2565 | ret = 1; |
| 2566 | break; |
| 2567 | } |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2568 | case SVM_VMGEXIT_UNSUPPORTED_EVENT: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2569 | vcpu_unimpl(vcpu, |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2570 | "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", |
| 2571 | control->exit_info_1, control->exit_info_2); |
| 2572 | break; |
| 2573 | default: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2574 | ret = svm_invoke_exit_handler(vcpu, exit_code); |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | return ret; |
| 2578 | } |
Tom Lendacky | 7ed9abf | 2020-12-10 11:09:54 -0600 | [diff] [blame] | 2579 | |
| 2580 | int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in) |
| 2581 | { |
| 2582 | if (!setup_vmgexit_scratch(svm, in, svm->vmcb->control.exit_info_2)) |
| 2583 | return -EINVAL; |
| 2584 | |
| 2585 | return kvm_sev_es_string_io(&svm->vcpu, size, port, |
| 2586 | svm->ghcb_sa, svm->ghcb_sa_len, in); |
| 2587 | } |
Tom Lendacky | 376c6d2 | 2020-12-10 11:10:06 -0600 | [diff] [blame] | 2588 | |
| 2589 | void sev_es_init_vmcb(struct vcpu_svm *svm) |
| 2590 | { |
| 2591 | struct kvm_vcpu *vcpu = &svm->vcpu; |
| 2592 | |
| 2593 | svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ES_ENABLE; |
| 2594 | svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK; |
| 2595 | |
| 2596 | /* |
| 2597 | * An SEV-ES guest requires a VMSA area that is a separate from the |
| 2598 | * VMCB page. Do not include the encryption mask on the VMSA physical |
| 2599 | * address since hardware will access it using the guest key. |
| 2600 | */ |
| 2601 | svm->vmcb->control.vmsa_pa = __pa(svm->vmsa); |
| 2602 | |
| 2603 | /* Can't intercept CR register access, HV can't modify CR registers */ |
| 2604 | svm_clr_intercept(svm, INTERCEPT_CR0_READ); |
| 2605 | svm_clr_intercept(svm, INTERCEPT_CR4_READ); |
| 2606 | svm_clr_intercept(svm, INTERCEPT_CR8_READ); |
| 2607 | svm_clr_intercept(svm, INTERCEPT_CR0_WRITE); |
| 2608 | svm_clr_intercept(svm, INTERCEPT_CR4_WRITE); |
| 2609 | svm_clr_intercept(svm, INTERCEPT_CR8_WRITE); |
| 2610 | |
| 2611 | svm_clr_intercept(svm, INTERCEPT_SELECTIVE_CR0); |
| 2612 | |
| 2613 | /* Track EFER/CR register changes */ |
| 2614 | svm_set_intercept(svm, TRAP_EFER_WRITE); |
| 2615 | svm_set_intercept(svm, TRAP_CR0_WRITE); |
| 2616 | svm_set_intercept(svm, TRAP_CR4_WRITE); |
| 2617 | svm_set_intercept(svm, TRAP_CR8_WRITE); |
| 2618 | |
| 2619 | /* No support for enable_vmware_backdoor */ |
| 2620 | clr_exception_intercept(svm, GP_VECTOR); |
| 2621 | |
| 2622 | /* Can't intercept XSETBV, HV can't modify XCR0 directly */ |
| 2623 | svm_clr_intercept(svm, INTERCEPT_XSETBV); |
| 2624 | |
| 2625 | /* Clear intercepts on selected MSRs */ |
| 2626 | set_msr_interception(vcpu, svm->msrpm, MSR_EFER, 1, 1); |
| 2627 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_CR_PAT, 1, 1); |
| 2628 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1); |
| 2629 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1); |
| 2630 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1); |
| 2631 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1); |
| 2632 | } |
| 2633 | |
Sean Christopherson | 9ebe530 | 2021-09-20 17:03:02 -0700 | [diff] [blame^] | 2634 | void sev_es_vcpu_reset(struct vcpu_svm *svm) |
Tom Lendacky | 376c6d2 | 2020-12-10 11:10:06 -0600 | [diff] [blame] | 2635 | { |
| 2636 | /* |
Sean Christopherson | 9ebe530 | 2021-09-20 17:03:02 -0700 | [diff] [blame^] | 2637 | * Set the GHCB MSR value as per the GHCB specification when emulating |
| 2638 | * vCPU RESET for an SEV-ES guest. |
Tom Lendacky | 376c6d2 | 2020-12-10 11:10:06 -0600 | [diff] [blame] | 2639 | */ |
| 2640 | set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX, |
| 2641 | GHCB_VERSION_MIN, |
| 2642 | sev_enc_bit)); |
| 2643 | } |
Tom Lendacky | 8613777 | 2020-12-10 11:10:07 -0600 | [diff] [blame] | 2644 | |
Michael Roth | a7fc06d | 2021-02-02 13:01:26 -0600 | [diff] [blame] | 2645 | void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu) |
Tom Lendacky | 8613777 | 2020-12-10 11:10:07 -0600 | [diff] [blame] | 2646 | { |
| 2647 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
| 2648 | struct vmcb_save_area *hostsa; |
Tom Lendacky | 8613777 | 2020-12-10 11:10:07 -0600 | [diff] [blame] | 2649 | |
| 2650 | /* |
| 2651 | * As an SEV-ES guest, hardware will restore the host state on VMEXIT, |
| 2652 | * of which one step is to perform a VMLOAD. Since hardware does not |
| 2653 | * perform a VMSAVE on VMRUN, the host savearea must be updated. |
| 2654 | */ |
Sean Christopherson | 35a7831 | 2020-12-30 16:27:00 -0800 | [diff] [blame] | 2655 | vmsave(__sme_page_pa(sd->save_area)); |
Tom Lendacky | 8613777 | 2020-12-10 11:10:07 -0600 | [diff] [blame] | 2656 | |
Tom Lendacky | 8613777 | 2020-12-10 11:10:07 -0600 | [diff] [blame] | 2657 | /* XCR0 is restored on VMEXIT, save the current host value */ |
| 2658 | hostsa = (struct vmcb_save_area *)(page_address(sd->save_area) + 0x400); |
| 2659 | hostsa->xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK); |
| 2660 | |
Ingo Molnar | d9f6e12 | 2021-03-18 15:28:01 +0100 | [diff] [blame] | 2661 | /* PKRU is restored on VMEXIT, save the current host value */ |
Tom Lendacky | 8613777 | 2020-12-10 11:10:07 -0600 | [diff] [blame] | 2662 | hostsa->pkru = read_pkru(); |
| 2663 | |
| 2664 | /* MSR_IA32_XSS is restored on VMEXIT, save the currnet host value */ |
| 2665 | hostsa->xss = host_xss; |
| 2666 | } |
| 2667 | |
Tom Lendacky | 647daca | 2021-01-04 14:20:01 -0600 | [diff] [blame] | 2668 | void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) |
| 2669 | { |
| 2670 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2671 | |
| 2672 | /* First SIPI: Use the values as initially set by the VMM */ |
| 2673 | if (!svm->received_first_sipi) { |
| 2674 | svm->received_first_sipi = true; |
| 2675 | return; |
| 2676 | } |
| 2677 | |
| 2678 | /* |
| 2679 | * Subsequent SIPI: Return from an AP Reset Hold VMGEXIT, where |
| 2680 | * the guest will set the CS and RIP. Set SW_EXIT_INFO_2 to a |
| 2681 | * non-zero value. |
| 2682 | */ |
Tom Lendacky | a3ba26e | 2021-04-09 09:38:42 -0500 | [diff] [blame] | 2683 | if (!svm->ghcb) |
| 2684 | return; |
| 2685 | |
Tom Lendacky | 647daca | 2021-01-04 14:20:01 -0600 | [diff] [blame] | 2686 | ghcb_set_sw_exit_info_2(svm->ghcb, 1); |
| 2687 | } |