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