blob: 425444d080712316da1025fa70171f71dfea031e [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * AMD SVM support
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
9 *
10 * Authors:
11 * Yaniv Kamay <yaniv@qumranet.com>
12 * Avi Kivity <avi@qumranet.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013 */
14
15#define pr_fmt(fmt) "SVM: " fmt
16
17#include <linux/kvm_host.h>
18
19#include "irq.h"
20#include "mmu.h"
21#include "kvm_cache_regs.h"
22#include "x86.h"
23#include "cpuid.h"
24#include "pmu.h"
25
26#include <linux/module.h>
27#include <linux/mod_devicetable.h>
28#include <linux/kernel.h>
29#include <linux/vmalloc.h>
30#include <linux/highmem.h>
31#include <linux/sched.h>
32#include <linux/trace_events.h>
33#include <linux/slab.h>
34#include <linux/amd-iommu.h>
35#include <linux/hashtable.h>
36#include <linux/frame.h>
37#include <linux/psp-sev.h>
38#include <linux/file.h>
39#include <linux/pagemap.h>
40#include <linux/swap.h>
41
42#include <asm/apic.h>
43#include <asm/perf_event.h>
44#include <asm/tlbflush.h>
45#include <asm/desc.h>
46#include <asm/debugreg.h>
47#include <asm/kvm_para.h>
48#include <asm/irq_remapping.h>
49#include <asm/spec-ctrl.h>
50
51#include <asm/virtext.h>
52#include "trace.h"
53
54#define __ex(x) __kvm_handle_fault_on_reboot(x)
55
56MODULE_AUTHOR("Qumranet");
57MODULE_LICENSE("GPL");
58
59static const struct x86_cpu_id svm_cpu_id[] = {
60 X86_FEATURE_MATCH(X86_FEATURE_SVM),
61 {}
62};
63MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
64
65#define IOPM_ALLOC_ORDER 2
66#define MSRPM_ALLOC_ORDER 1
67
68#define SEG_TYPE_LDT 2
69#define SEG_TYPE_BUSY_TSS16 3
70
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071#define SVM_FEATURE_LBRV (1 << 1)
72#define SVM_FEATURE_SVML (1 << 2)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073#define SVM_FEATURE_TSC_RATE (1 << 4)
74#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
75#define SVM_FEATURE_FLUSH_ASID (1 << 6)
76#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
77#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
78
79#define SVM_AVIC_DOORBELL 0xc001011b
80
81#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
82#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
83#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
84
85#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
86
87#define TSC_RATIO_RSVD 0xffffff0000000000ULL
88#define TSC_RATIO_MIN 0x0000000000000001ULL
89#define TSC_RATIO_MAX 0x000000ffffffffffULL
90
91#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
92
93/*
94 * 0xff is broadcast, so the max index allowed for physical APIC ID
95 * table is 0xfe. APIC IDs above 0xff are reserved.
96 */
97#define AVIC_MAX_PHYSICAL_ID_COUNT 255
98
99#define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
100#define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
101#define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
102
103/* AVIC GATAG is encoded using VM and VCPU IDs */
104#define AVIC_VCPU_ID_BITS 8
105#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1)
106
107#define AVIC_VM_ID_BITS 24
108#define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS)
109#define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1)
110
111#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \
112 (y & AVIC_VCPU_ID_MASK))
113#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
114#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
115
116static bool erratum_383_found __read_mostly;
117
118static const u32 host_save_user_msrs[] = {
119#ifdef CONFIG_X86_64
120 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
121 MSR_FS_BASE,
122#endif
123 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
124 MSR_TSC_AUX,
125};
126
127#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
128
129struct kvm_sev_info {
130 bool active; /* SEV enabled guest */
131 unsigned int asid; /* ASID used for this guest */
132 unsigned int handle; /* SEV firmware handle */
133 int fd; /* SEV device fd */
134 unsigned long pages_locked; /* Number of pages locked */
135 struct list_head regions_list; /* List of registered regions */
136};
137
138struct kvm_svm {
139 struct kvm kvm;
140
141 /* Struct members for AVIC */
142 u32 avic_vm_id;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000143 struct page *avic_logical_id_table_page;
144 struct page *avic_physical_id_table_page;
145 struct hlist_node hnode;
146
147 struct kvm_sev_info sev_info;
148};
149
150struct kvm_vcpu;
151
152struct nested_state {
153 struct vmcb *hsave;
154 u64 hsave_msr;
155 u64 vm_cr_msr;
156 u64 vmcb;
157
158 /* These are the merged vectors */
159 u32 *msrpm;
160
161 /* gpa pointers to the real vectors */
162 u64 vmcb_msrpm;
163 u64 vmcb_iopm;
164
165 /* A VMEXIT is required but not yet emulated */
166 bool exit_required;
167
168 /* cache for intercepts of the guest */
169 u32 intercept_cr;
170 u32 intercept_dr;
171 u32 intercept_exceptions;
172 u64 intercept;
173
174 /* Nested Paging related state */
175 u64 nested_cr3;
176};
177
178#define MSRPM_OFFSETS 16
179static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
180
181/*
182 * Set osvw_len to higher value when updated Revision Guides
183 * are published and we know what the new status bits are
184 */
185static uint64_t osvw_len = 4, osvw_status;
186
187struct vcpu_svm {
188 struct kvm_vcpu vcpu;
189 struct vmcb *vmcb;
190 unsigned long vmcb_pa;
191 struct svm_cpu_data *svm_data;
192 uint64_t asid_generation;
193 uint64_t sysenter_esp;
194 uint64_t sysenter_eip;
195 uint64_t tsc_aux;
196
197 u64 msr_decfg;
198
199 u64 next_rip;
200
201 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
202 struct {
203 u16 fs;
204 u16 gs;
205 u16 ldt;
206 u64 gs_base;
207 } host;
208
209 u64 spec_ctrl;
210 /*
211 * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
212 * translated into the appropriate L2_CFG bits on the host to
213 * perform speculative control.
214 */
215 u64 virt_spec_ctrl;
216
217 u32 *msrpm;
218
219 ulong nmi_iret_rip;
220
221 struct nested_state nested;
222
223 bool nmi_singlestep;
224 u64 nmi_singlestep_guest_rflags;
225
226 unsigned int3_injected;
227 unsigned long int3_rip;
228
229 /* cached guest cpuid flags for faster access */
230 bool nrips_enabled : 1;
231
232 u32 ldr_reg;
David Brazdil0f672f62019-12-10 10:32:29 +0000233 u32 dfr_reg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000234 struct page *avic_backing_page;
235 u64 *avic_physical_id_cache;
236 bool avic_is_running;
237
238 /*
239 * Per-vcpu list of struct amd_svm_iommu_ir:
240 * This is used mainly to store interrupt remapping information used
241 * when update the vcpu affinity. This avoids the need to scan for
242 * IRTE and try to match ga_tag in the IOMMU driver.
243 */
244 struct list_head ir_list;
245 spinlock_t ir_list_lock;
246
247 /* which host CPU was used for running this vcpu */
248 unsigned int last_cpu;
249};
250
251/*
252 * This is a wrapper of struct amd_iommu_ir_data.
253 */
254struct amd_svm_iommu_ir {
255 struct list_head node; /* Used by SVM for per-vcpu ir_list */
256 void *data; /* Storing pointer to struct amd_ir_data */
257};
258
259#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
David Brazdil0f672f62019-12-10 10:32:29 +0000260#define AVIC_LOGICAL_ID_ENTRY_VALID_BIT 31
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000261#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
262
263#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
264#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
265#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
266#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
267
268static DEFINE_PER_CPU(u64, current_tsc_ratio);
269#define TSC_RATIO_DEFAULT 0x0100000000ULL
270
271#define MSR_INVALID 0xffffffffU
272
273static const struct svm_direct_access_msrs {
274 u32 index; /* Index of the MSR */
275 bool always; /* True if intercept is always on */
276} direct_access_msrs[] = {
277 { .index = MSR_STAR, .always = true },
278 { .index = MSR_IA32_SYSENTER_CS, .always = true },
279#ifdef CONFIG_X86_64
280 { .index = MSR_GS_BASE, .always = true },
281 { .index = MSR_FS_BASE, .always = true },
282 { .index = MSR_KERNEL_GS_BASE, .always = true },
283 { .index = MSR_LSTAR, .always = true },
284 { .index = MSR_CSTAR, .always = true },
285 { .index = MSR_SYSCALL_MASK, .always = true },
286#endif
287 { .index = MSR_IA32_SPEC_CTRL, .always = false },
288 { .index = MSR_IA32_PRED_CMD, .always = false },
289 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
290 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
291 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
292 { .index = MSR_IA32_LASTINTTOIP, .always = false },
293 { .index = MSR_INVALID, .always = false },
294};
295
296/* enable NPT for AMD64 and X86 with PAE */
297#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
298static bool npt_enabled = true;
299#else
300static bool npt_enabled;
301#endif
302
303/*
304 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
305 * pause_filter_count: On processors that support Pause filtering(indicated
306 * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter
307 * count value. On VMRUN this value is loaded into an internal counter.
308 * Each time a pause instruction is executed, this counter is decremented
309 * until it reaches zero at which time a #VMEXIT is generated if pause
310 * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause
311 * Intercept Filtering for more details.
312 * This also indicate if ple logic enabled.
313 *
314 * pause_filter_thresh: In addition, some processor families support advanced
315 * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on
316 * the amount of time a guest is allowed to execute in a pause loop.
317 * In this mode, a 16-bit pause filter threshold field is added in the
318 * VMCB. The threshold value is a cycle count that is used to reset the
319 * pause counter. As with simple pause filtering, VMRUN loads the pause
320 * count value from VMCB into an internal counter. Then, on each pause
321 * instruction the hardware checks the elapsed number of cycles since
322 * the most recent pause instruction against the pause filter threshold.
323 * If the elapsed cycle count is greater than the pause filter threshold,
324 * then the internal pause count is reloaded from the VMCB and execution
325 * continues. If the elapsed cycle count is less than the pause filter
326 * threshold, then the internal pause count is decremented. If the count
327 * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is
328 * triggered. If advanced pause filtering is supported and pause filter
329 * threshold field is set to zero, the filter will operate in the simpler,
330 * count only mode.
331 */
332
333static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP;
334module_param(pause_filter_thresh, ushort, 0444);
335
336static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW;
337module_param(pause_filter_count, ushort, 0444);
338
339/* Default doubles per-vcpu window every exit. */
340static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
341module_param(pause_filter_count_grow, ushort, 0444);
342
343/* Default resets per-vcpu window every exit to pause_filter_count. */
344static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
345module_param(pause_filter_count_shrink, ushort, 0444);
346
347/* Default is to compute the maximum so we can never overflow. */
348static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX;
349module_param(pause_filter_count_max, ushort, 0444);
350
351/* allow nested paging (virtualized MMU) for all guests */
352static int npt = true;
353module_param(npt, int, S_IRUGO);
354
355/* allow nested virtualization in KVM/SVM */
356static int nested = true;
357module_param(nested, int, S_IRUGO);
358
359/* enable / disable AVIC */
360static int avic;
361#ifdef CONFIG_X86_LOCAL_APIC
362module_param(avic, int, S_IRUGO);
363#endif
364
David Brazdil0f672f62019-12-10 10:32:29 +0000365/* enable/disable Next RIP Save */
366static int nrips = true;
367module_param(nrips, int, 0444);
368
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000369/* enable/disable Virtual VMLOAD VMSAVE */
370static int vls = true;
371module_param(vls, int, 0444);
372
373/* enable/disable Virtual GIF */
374static int vgif = true;
375module_param(vgif, int, 0444);
376
377/* enable/disable SEV support */
378static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
379module_param(sev, int, 0444);
380
David Brazdil0f672f62019-12-10 10:32:29 +0000381static bool __read_mostly dump_invalid_vmcb = 0;
382module_param(dump_invalid_vmcb, bool, 0644);
383
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000384static u8 rsm_ins_bytes[] = "\x0f\xaa";
385
386static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
387static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa);
388static void svm_complete_interrupts(struct vcpu_svm *svm);
389
390static int nested_svm_exit_handled(struct vcpu_svm *svm);
391static int nested_svm_intercept(struct vcpu_svm *svm);
392static int nested_svm_vmexit(struct vcpu_svm *svm);
393static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
394 bool has_error_code, u32 error_code);
395
396enum {
397 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
398 pause filter count */
399 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
400 VMCB_ASID, /* ASID */
401 VMCB_INTR, /* int_ctl, int_vector */
402 VMCB_NPT, /* npt_en, nCR3, gPAT */
403 VMCB_CR, /* CR0, CR3, CR4, EFER */
404 VMCB_DR, /* DR6, DR7 */
405 VMCB_DT, /* GDT, IDT */
406 VMCB_SEG, /* CS, DS, SS, ES, CPL */
407 VMCB_CR2, /* CR2 only */
408 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
409 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
410 * AVIC PHYSICAL_TABLE pointer,
411 * AVIC LOGICAL_TABLE pointer
412 */
413 VMCB_DIRTY_MAX,
414};
415
416/* TPR and CR2 are always written before VMRUN */
417#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
418
419#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
420
421static unsigned int max_sev_asid;
422static unsigned int min_sev_asid;
423static unsigned long *sev_asid_bitmap;
424#define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
425
426struct enc_region {
427 struct list_head list;
428 unsigned long npages;
429 struct page **pages;
430 unsigned long uaddr;
431 unsigned long size;
432};
433
434
435static inline struct kvm_svm *to_kvm_svm(struct kvm *kvm)
436{
437 return container_of(kvm, struct kvm_svm, kvm);
438}
439
440static inline bool svm_sev_enabled(void)
441{
442 return IS_ENABLED(CONFIG_KVM_AMD_SEV) ? max_sev_asid : 0;
443}
444
445static inline bool sev_guest(struct kvm *kvm)
446{
447#ifdef CONFIG_KVM_AMD_SEV
448 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
449
450 return sev->active;
451#else
452 return false;
453#endif
454}
455
456static inline int sev_get_asid(struct kvm *kvm)
457{
458 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
459
460 return sev->asid;
461}
462
463static inline void mark_all_dirty(struct vmcb *vmcb)
464{
465 vmcb->control.clean = 0;
466}
467
468static inline void mark_all_clean(struct vmcb *vmcb)
469{
470 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
471 & ~VMCB_ALWAYS_DIRTY_MASK;
472}
473
474static inline void mark_dirty(struct vmcb *vmcb, int bit)
475{
476 vmcb->control.clean &= ~(1 << bit);
477}
478
479static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
480{
481 return container_of(vcpu, struct vcpu_svm, vcpu);
482}
483
484static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
485{
486 svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
487 mark_dirty(svm->vmcb, VMCB_AVIC);
488}
489
490static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
491{
492 struct vcpu_svm *svm = to_svm(vcpu);
493 u64 *entry = svm->avic_physical_id_cache;
494
495 if (!entry)
496 return false;
497
498 return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
499}
500
501static void recalc_intercepts(struct vcpu_svm *svm)
502{
503 struct vmcb_control_area *c, *h;
504 struct nested_state *g;
505
506 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
507
508 if (!is_guest_mode(&svm->vcpu))
509 return;
510
511 c = &svm->vmcb->control;
512 h = &svm->nested.hsave->control;
513 g = &svm->nested;
514
515 c->intercept_cr = h->intercept_cr | g->intercept_cr;
516 c->intercept_dr = h->intercept_dr | g->intercept_dr;
517 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
518 c->intercept = h->intercept | g->intercept;
Olivier Deprez0e641232021-09-23 10:07:05 +0200519
520 c->intercept |= (1ULL << INTERCEPT_VMLOAD);
521 c->intercept |= (1ULL << INTERCEPT_VMSAVE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000522}
523
524static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
525{
526 if (is_guest_mode(&svm->vcpu))
527 return svm->nested.hsave;
528 else
529 return svm->vmcb;
530}
531
532static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
533{
534 struct vmcb *vmcb = get_host_vmcb(svm);
535
536 vmcb->control.intercept_cr |= (1U << bit);
537
538 recalc_intercepts(svm);
539}
540
541static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
542{
543 struct vmcb *vmcb = get_host_vmcb(svm);
544
545 vmcb->control.intercept_cr &= ~(1U << bit);
546
547 recalc_intercepts(svm);
548}
549
550static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
551{
552 struct vmcb *vmcb = get_host_vmcb(svm);
553
554 return vmcb->control.intercept_cr & (1U << bit);
555}
556
557static inline void set_dr_intercepts(struct vcpu_svm *svm)
558{
559 struct vmcb *vmcb = get_host_vmcb(svm);
560
561 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
562 | (1 << INTERCEPT_DR1_READ)
563 | (1 << INTERCEPT_DR2_READ)
564 | (1 << INTERCEPT_DR3_READ)
565 | (1 << INTERCEPT_DR4_READ)
566 | (1 << INTERCEPT_DR5_READ)
567 | (1 << INTERCEPT_DR6_READ)
568 | (1 << INTERCEPT_DR7_READ)
569 | (1 << INTERCEPT_DR0_WRITE)
570 | (1 << INTERCEPT_DR1_WRITE)
571 | (1 << INTERCEPT_DR2_WRITE)
572 | (1 << INTERCEPT_DR3_WRITE)
573 | (1 << INTERCEPT_DR4_WRITE)
574 | (1 << INTERCEPT_DR5_WRITE)
575 | (1 << INTERCEPT_DR6_WRITE)
576 | (1 << INTERCEPT_DR7_WRITE);
577
578 recalc_intercepts(svm);
579}
580
581static inline void clr_dr_intercepts(struct vcpu_svm *svm)
582{
583 struct vmcb *vmcb = get_host_vmcb(svm);
584
585 vmcb->control.intercept_dr = 0;
586
587 recalc_intercepts(svm);
588}
589
590static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
591{
592 struct vmcb *vmcb = get_host_vmcb(svm);
593
594 vmcb->control.intercept_exceptions |= (1U << bit);
595
596 recalc_intercepts(svm);
597}
598
599static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
600{
601 struct vmcb *vmcb = get_host_vmcb(svm);
602
603 vmcb->control.intercept_exceptions &= ~(1U << bit);
604
605 recalc_intercepts(svm);
606}
607
608static inline void set_intercept(struct vcpu_svm *svm, int bit)
609{
610 struct vmcb *vmcb = get_host_vmcb(svm);
611
612 vmcb->control.intercept |= (1ULL << bit);
613
614 recalc_intercepts(svm);
615}
616
617static inline void clr_intercept(struct vcpu_svm *svm, int bit)
618{
619 struct vmcb *vmcb = get_host_vmcb(svm);
620
621 vmcb->control.intercept &= ~(1ULL << bit);
622
623 recalc_intercepts(svm);
624}
625
626static inline bool vgif_enabled(struct vcpu_svm *svm)
627{
628 return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
629}
630
631static inline void enable_gif(struct vcpu_svm *svm)
632{
633 if (vgif_enabled(svm))
634 svm->vmcb->control.int_ctl |= V_GIF_MASK;
635 else
636 svm->vcpu.arch.hflags |= HF_GIF_MASK;
637}
638
639static inline void disable_gif(struct vcpu_svm *svm)
640{
641 if (vgif_enabled(svm))
642 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
643 else
644 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
645}
646
647static inline bool gif_set(struct vcpu_svm *svm)
648{
649 if (vgif_enabled(svm))
650 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
651 else
652 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
653}
654
655static unsigned long iopm_base;
656
657struct kvm_ldttss_desc {
658 u16 limit0;
659 u16 base0;
660 unsigned base1:8, type:5, dpl:2, p:1;
661 unsigned limit1:4, zero0:3, g:1, base2:8;
662 u32 base3;
663 u32 zero1;
664} __attribute__((packed));
665
666struct svm_cpu_data {
667 int cpu;
668
669 u64 asid_generation;
670 u32 max_asid;
671 u32 next_asid;
672 u32 min_asid;
673 struct kvm_ldttss_desc *tss_desc;
674
675 struct page *save_area;
676 struct vmcb *current_vmcb;
677
678 /* index = sev_asid, value = vmcb pointer */
679 struct vmcb **sev_vmcbs;
680};
681
682static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
683
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000684static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
685
686#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
687#define MSRS_RANGE_SIZE 2048
688#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
689
690static u32 svm_msrpm_offset(u32 msr)
691{
692 u32 offset;
693 int i;
694
695 for (i = 0; i < NUM_MSR_MAPS; i++) {
696 if (msr < msrpm_ranges[i] ||
697 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
698 continue;
699
700 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
701 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
702
703 /* Now we have the u8 offset - but need the u32 offset */
704 return offset / 4;
705 }
706
707 /* MSR not in any range */
708 return MSR_INVALID;
709}
710
711#define MAX_INST_SIZE 15
712
713static inline void clgi(void)
714{
David Brazdil0f672f62019-12-10 10:32:29 +0000715 asm volatile (__ex("clgi"));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000716}
717
718static inline void stgi(void)
719{
David Brazdil0f672f62019-12-10 10:32:29 +0000720 asm volatile (__ex("stgi"));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000721}
722
723static inline void invlpga(unsigned long addr, u32 asid)
724{
David Brazdil0f672f62019-12-10 10:32:29 +0000725 asm volatile (__ex("invlpga %1, %0") : : "c"(asid), "a"(addr));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000726}
727
728static int get_npt_level(struct kvm_vcpu *vcpu)
729{
730#ifdef CONFIG_X86_64
731 return PT64_ROOT_4LEVEL;
732#else
733 return PT32E_ROOT_LEVEL;
734#endif
735}
736
737static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
738{
739 vcpu->arch.efer = efer;
David Brazdil0f672f62019-12-10 10:32:29 +0000740
741 if (!npt_enabled) {
742 /* Shadow paging assumes NX to be available. */
743 efer |= EFER_NX;
744
745 if (!(efer & EFER_LMA))
746 efer &= ~EFER_LME;
747 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000748
749 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
750 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
751}
752
753static int is_external_interrupt(u32 info)
754{
755 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
756 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
757}
758
759static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
760{
761 struct vcpu_svm *svm = to_svm(vcpu);
762 u32 ret = 0;
763
764 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
765 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
766 return ret;
767}
768
769static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
770{
771 struct vcpu_svm *svm = to_svm(vcpu);
772
773 if (mask == 0)
774 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
775 else
776 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
777
778}
779
David Brazdil0f672f62019-12-10 10:32:29 +0000780static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000781{
782 struct vcpu_svm *svm = to_svm(vcpu);
783
David Brazdil0f672f62019-12-10 10:32:29 +0000784 if (nrips && svm->vmcb->control.next_rip != 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000785 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
786 svm->next_rip = svm->vmcb->control.next_rip;
787 }
788
789 if (!svm->next_rip) {
David Brazdil0f672f62019-12-10 10:32:29 +0000790 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
791 return 0;
792 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000793 kvm_rip_write(vcpu, svm->next_rip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000794 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000795 svm_set_interrupt_shadow(vcpu, 0);
David Brazdil0f672f62019-12-10 10:32:29 +0000796
797 return 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000798}
799
800static void svm_queue_exception(struct kvm_vcpu *vcpu)
801{
802 struct vcpu_svm *svm = to_svm(vcpu);
803 unsigned nr = vcpu->arch.exception.nr;
804 bool has_error_code = vcpu->arch.exception.has_error_code;
805 bool reinject = vcpu->arch.exception.injected;
806 u32 error_code = vcpu->arch.exception.error_code;
807
808 /*
809 * If we are within a nested VM we'd better #VMEXIT and let the guest
810 * handle the exception
811 */
812 if (!reinject &&
813 nested_svm_check_exception(svm, nr, has_error_code, error_code))
814 return;
815
David Brazdil0f672f62019-12-10 10:32:29 +0000816 kvm_deliver_exception_payload(&svm->vcpu);
817
818 if (nr == BP_VECTOR && !nrips) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000819 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
820
821 /*
822 * For guest debugging where we have to reinject #BP if some
823 * INT3 is guest-owned:
824 * Emulate nRIP by moving RIP forward. Will fail if injection
825 * raises a fault that is not intercepted. Still better than
826 * failing in all cases.
827 */
David Brazdil0f672f62019-12-10 10:32:29 +0000828 (void)skip_emulated_instruction(&svm->vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000829 rip = kvm_rip_read(&svm->vcpu);
830 svm->int3_rip = rip + svm->vmcb->save.cs.base;
831 svm->int3_injected = rip - old_rip;
832 }
833
834 svm->vmcb->control.event_inj = nr
835 | SVM_EVTINJ_VALID
836 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
837 | SVM_EVTINJ_TYPE_EXEPT;
838 svm->vmcb->control.event_inj_err = error_code;
839}
840
841static void svm_init_erratum_383(void)
842{
843 u32 low, high;
844 int err;
845 u64 val;
846
847 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
848 return;
849
850 /* Use _safe variants to not break nested virtualization */
851 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
852 if (err)
853 return;
854
855 val |= (1ULL << 47);
856
857 low = lower_32_bits(val);
858 high = upper_32_bits(val);
859
860 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
861
862 erratum_383_found = true;
863}
864
865static void svm_init_osvw(struct kvm_vcpu *vcpu)
866{
867 /*
868 * Guests should see errata 400 and 415 as fixed (assuming that
869 * HLT and IO instructions are intercepted).
870 */
871 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
872 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
873
874 /*
875 * By increasing VCPU's osvw.length to 3 we are telling the guest that
876 * all osvw.status bits inside that length, including bit 0 (which is
877 * reserved for erratum 298), are valid. However, if host processor's
878 * osvw_len is 0 then osvw_status[0] carries no information. We need to
879 * be conservative here and therefore we tell the guest that erratum 298
880 * is present (because we really don't know).
881 */
882 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
883 vcpu->arch.osvw.status |= 1;
884}
885
886static int has_svm(void)
887{
888 const char *msg;
889
890 if (!cpu_has_svm(&msg)) {
891 printk(KERN_INFO "has_svm: %s\n", msg);
892 return 0;
893 }
894
Olivier Deprez0e641232021-09-23 10:07:05 +0200895 if (sev_active()) {
896 pr_info("KVM is unsupported when running as an SEV guest\n");
897 return 0;
898 }
899
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000900 return 1;
901}
902
903static void svm_hardware_disable(void)
904{
905 /* Make sure we clean up behind us */
906 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
907 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
908
909 cpu_svm_disable();
910
911 amd_pmu_disable_virt();
912}
913
914static int svm_hardware_enable(void)
915{
916
917 struct svm_cpu_data *sd;
918 uint64_t efer;
919 struct desc_struct *gdt;
920 int me = raw_smp_processor_id();
921
922 rdmsrl(MSR_EFER, efer);
923 if (efer & EFER_SVME)
924 return -EBUSY;
925
926 if (!has_svm()) {
927 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
928 return -EINVAL;
929 }
930 sd = per_cpu(svm_data, me);
931 if (!sd) {
932 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
933 return -EINVAL;
934 }
935
936 sd->asid_generation = 1;
937 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
938 sd->next_asid = sd->max_asid + 1;
939 sd->min_asid = max_sev_asid + 1;
940
941 gdt = get_current_gdt_rw();
942 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
943
944 wrmsrl(MSR_EFER, efer | EFER_SVME);
945
946 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
947
948 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
949 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
950 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
951 }
952
953
954 /*
955 * Get OSVW bits.
956 *
957 * Note that it is possible to have a system with mixed processor
958 * revisions and therefore different OSVW bits. If bits are not the same
959 * on different processors then choose the worst case (i.e. if erratum
960 * is present on one processor and not on another then assume that the
961 * erratum is present everywhere).
962 */
963 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
964 uint64_t len, status = 0;
965 int err;
966
967 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
968 if (!err)
969 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
970 &err);
971
972 if (err)
973 osvw_status = osvw_len = 0;
974 else {
975 if (len < osvw_len)
976 osvw_len = len;
977 osvw_status |= status;
978 osvw_status &= (1ULL << osvw_len) - 1;
979 }
980 } else
981 osvw_status = osvw_len = 0;
982
983 svm_init_erratum_383();
984
985 amd_pmu_enable_virt();
986
987 return 0;
988}
989
990static void svm_cpu_uninit(int cpu)
991{
992 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
993
994 if (!sd)
995 return;
996
997 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
998 kfree(sd->sev_vmcbs);
999 __free_page(sd->save_area);
1000 kfree(sd);
1001}
1002
1003static int svm_cpu_init(int cpu)
1004{
1005 struct svm_cpu_data *sd;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001006
1007 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
1008 if (!sd)
1009 return -ENOMEM;
1010 sd->cpu = cpu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001011 sd->save_area = alloc_page(GFP_KERNEL);
1012 if (!sd->save_area)
Olivier Deprez0e641232021-09-23 10:07:05 +02001013 goto free_cpu_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001014
1015 if (svm_sev_enabled()) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001016 sd->sev_vmcbs = kmalloc_array(max_sev_asid + 1,
1017 sizeof(void *),
1018 GFP_KERNEL);
1019 if (!sd->sev_vmcbs)
Olivier Deprez0e641232021-09-23 10:07:05 +02001020 goto free_save_area;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001021 }
1022
1023 per_cpu(svm_data, cpu) = sd;
1024
1025 return 0;
1026
Olivier Deprez0e641232021-09-23 10:07:05 +02001027free_save_area:
1028 __free_page(sd->save_area);
1029free_cpu_data:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001030 kfree(sd);
Olivier Deprez0e641232021-09-23 10:07:05 +02001031 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001032
1033}
1034
1035static bool valid_msr_intercept(u32 index)
1036{
1037 int i;
1038
1039 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
1040 if (direct_access_msrs[i].index == index)
1041 return true;
1042
1043 return false;
1044}
1045
1046static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr)
1047{
1048 u8 bit_write;
1049 unsigned long tmp;
1050 u32 offset;
1051 u32 *msrpm;
1052
1053 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
1054 to_svm(vcpu)->msrpm;
1055
1056 offset = svm_msrpm_offset(msr);
1057 bit_write = 2 * (msr & 0x0f) + 1;
1058 tmp = msrpm[offset];
1059
1060 BUG_ON(offset == MSR_INVALID);
1061
1062 return !!test_bit(bit_write, &tmp);
1063}
1064
1065static void set_msr_interception(u32 *msrpm, unsigned msr,
1066 int read, int write)
1067{
1068 u8 bit_read, bit_write;
1069 unsigned long tmp;
1070 u32 offset;
1071
1072 /*
1073 * If this warning triggers extend the direct_access_msrs list at the
1074 * beginning of the file
1075 */
1076 WARN_ON(!valid_msr_intercept(msr));
1077
1078 offset = svm_msrpm_offset(msr);
1079 bit_read = 2 * (msr & 0x0f);
1080 bit_write = 2 * (msr & 0x0f) + 1;
1081 tmp = msrpm[offset];
1082
1083 BUG_ON(offset == MSR_INVALID);
1084
1085 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
1086 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
1087
1088 msrpm[offset] = tmp;
1089}
1090
1091static void svm_vcpu_init_msrpm(u32 *msrpm)
1092{
1093 int i;
1094
1095 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
1096
1097 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1098 if (!direct_access_msrs[i].always)
1099 continue;
1100
1101 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
1102 }
1103}
1104
1105static void add_msr_offset(u32 offset)
1106{
1107 int i;
1108
1109 for (i = 0; i < MSRPM_OFFSETS; ++i) {
1110
1111 /* Offset already in list? */
1112 if (msrpm_offsets[i] == offset)
1113 return;
1114
1115 /* Slot used by another offset? */
1116 if (msrpm_offsets[i] != MSR_INVALID)
1117 continue;
1118
1119 /* Add offset to list */
1120 msrpm_offsets[i] = offset;
1121
1122 return;
1123 }
1124
1125 /*
1126 * If this BUG triggers the msrpm_offsets table has an overflow. Just
1127 * increase MSRPM_OFFSETS in this case.
1128 */
1129 BUG();
1130}
1131
1132static void init_msrpm_offsets(void)
1133{
1134 int i;
1135
1136 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
1137
1138 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1139 u32 offset;
1140
1141 offset = svm_msrpm_offset(direct_access_msrs[i].index);
1142 BUG_ON(offset == MSR_INVALID);
1143
1144 add_msr_offset(offset);
1145 }
1146}
1147
1148static void svm_enable_lbrv(struct vcpu_svm *svm)
1149{
1150 u32 *msrpm = svm->msrpm;
1151
1152 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
1153 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
1154 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
1155 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
1156 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
1157}
1158
1159static void svm_disable_lbrv(struct vcpu_svm *svm)
1160{
1161 u32 *msrpm = svm->msrpm;
1162
1163 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
1164 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
1165 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
1166 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
1167 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
1168}
1169
1170static void disable_nmi_singlestep(struct vcpu_svm *svm)
1171{
1172 svm->nmi_singlestep = false;
1173
1174 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
1175 /* Clear our flags if they were not set by the guest */
1176 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1177 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
1178 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1179 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
1180 }
1181}
1182
1183/* Note:
1184 * This hash table is used to map VM_ID to a struct kvm_svm,
1185 * when handling AMD IOMMU GALOG notification to schedule in
1186 * a particular vCPU.
1187 */
1188#define SVM_VM_DATA_HASH_BITS 8
1189static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
1190static u32 next_vm_id = 0;
1191static bool next_vm_id_wrapped = 0;
1192static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
1193
1194/* Note:
1195 * This function is called from IOMMU driver to notify
1196 * SVM to schedule in a particular vCPU of a particular VM.
1197 */
1198static int avic_ga_log_notifier(u32 ga_tag)
1199{
1200 unsigned long flags;
1201 struct kvm_svm *kvm_svm;
1202 struct kvm_vcpu *vcpu = NULL;
1203 u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
1204 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
1205
1206 pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
1207
1208 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1209 hash_for_each_possible(svm_vm_data_hash, kvm_svm, hnode, vm_id) {
1210 if (kvm_svm->avic_vm_id != vm_id)
1211 continue;
1212 vcpu = kvm_get_vcpu_by_id(&kvm_svm->kvm, vcpu_id);
1213 break;
1214 }
1215 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1216
1217 /* Note:
1218 * At this point, the IOMMU should have already set the pending
1219 * bit in the vAPIC backing page. So, we just need to schedule
1220 * in the vcpu.
1221 */
1222 if (vcpu)
1223 kvm_vcpu_wake_up(vcpu);
1224
1225 return 0;
1226}
1227
1228static __init int sev_hardware_setup(void)
1229{
1230 struct sev_user_data_status *status;
1231 int rc;
1232
1233 /* Maximum number of encrypted guests supported simultaneously */
1234 max_sev_asid = cpuid_ecx(0x8000001F);
1235
1236 if (!max_sev_asid)
1237 return 1;
1238
1239 /* Minimum ASID value that should be used for SEV guest */
1240 min_sev_asid = cpuid_edx(0x8000001F);
1241
1242 /* Initialize SEV ASID bitmap */
1243 sev_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL);
1244 if (!sev_asid_bitmap)
1245 return 1;
1246
1247 status = kmalloc(sizeof(*status), GFP_KERNEL);
1248 if (!status)
1249 return 1;
1250
1251 /*
1252 * Check SEV platform status.
1253 *
1254 * PLATFORM_STATUS can be called in any state, if we failed to query
1255 * the PLATFORM status then either PSP firmware does not support SEV
1256 * feature or SEV firmware is dead.
1257 */
1258 rc = sev_platform_status(status, NULL);
1259 if (rc)
1260 goto err;
1261
1262 pr_info("SEV supported\n");
1263
1264err:
1265 kfree(status);
1266 return rc;
1267}
1268
1269static void grow_ple_window(struct kvm_vcpu *vcpu)
1270{
1271 struct vcpu_svm *svm = to_svm(vcpu);
1272 struct vmcb_control_area *control = &svm->vmcb->control;
1273 int old = control->pause_filter_count;
1274
1275 control->pause_filter_count = __grow_ple_window(old,
1276 pause_filter_count,
1277 pause_filter_count_grow,
1278 pause_filter_count_max);
1279
David Brazdil0f672f62019-12-10 10:32:29 +00001280 if (control->pause_filter_count != old) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001281 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
David Brazdil0f672f62019-12-10 10:32:29 +00001282 trace_kvm_ple_window_update(vcpu->vcpu_id,
1283 control->pause_filter_count, old);
1284 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001285}
1286
1287static void shrink_ple_window(struct kvm_vcpu *vcpu)
1288{
1289 struct vcpu_svm *svm = to_svm(vcpu);
1290 struct vmcb_control_area *control = &svm->vmcb->control;
1291 int old = control->pause_filter_count;
1292
1293 control->pause_filter_count =
1294 __shrink_ple_window(old,
1295 pause_filter_count,
1296 pause_filter_count_shrink,
1297 pause_filter_count);
David Brazdil0f672f62019-12-10 10:32:29 +00001298 if (control->pause_filter_count != old) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001299 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
David Brazdil0f672f62019-12-10 10:32:29 +00001300 trace_kvm_ple_window_update(vcpu->vcpu_id,
1301 control->pause_filter_count, old);
1302 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001303}
1304
Olivier Deprez0e641232021-09-23 10:07:05 +02001305/*
1306 * The default MMIO mask is a single bit (excluding the present bit),
1307 * which could conflict with the memory encryption bit. Check for
1308 * memory encryption support and override the default MMIO mask if
1309 * memory encryption is enabled.
1310 */
1311static __init void svm_adjust_mmio_mask(void)
1312{
1313 unsigned int enc_bit, mask_bit;
1314 u64 msr, mask;
1315
1316 /* If there is no memory encryption support, use existing mask */
1317 if (cpuid_eax(0x80000000) < 0x8000001f)
1318 return;
1319
1320 /* If memory encryption is not enabled, use existing mask */
1321 rdmsrl(MSR_K8_SYSCFG, msr);
1322 if (!(msr & MSR_K8_SYSCFG_MEM_ENCRYPT))
1323 return;
1324
1325 enc_bit = cpuid_ebx(0x8000001f) & 0x3f;
1326 mask_bit = boot_cpu_data.x86_phys_bits;
1327
1328 /* Increment the mask bit if it is the same as the encryption bit */
1329 if (enc_bit == mask_bit)
1330 mask_bit++;
1331
1332 /*
1333 * If the mask bit location is below 52, then some bits above the
1334 * physical addressing limit will always be reserved, so use the
1335 * rsvd_bits() function to generate the mask. This mask, along with
1336 * the present bit, will be used to generate a page fault with
1337 * PFER.RSV = 1.
1338 *
1339 * If the mask bit location is 52 (or above), then clear the mask.
1340 */
1341 mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0;
1342
1343 kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK);
1344}
1345
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001346static __init int svm_hardware_setup(void)
1347{
1348 int cpu;
1349 struct page *iopm_pages;
1350 void *iopm_va;
1351 int r;
1352
1353 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
1354
1355 if (!iopm_pages)
1356 return -ENOMEM;
1357
1358 iopm_va = page_address(iopm_pages);
1359 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
1360 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
1361
1362 init_msrpm_offsets();
1363
1364 if (boot_cpu_has(X86_FEATURE_NX))
1365 kvm_enable_efer_bits(EFER_NX);
1366
1367 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
1368 kvm_enable_efer_bits(EFER_FFXSR);
1369
1370 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
1371 kvm_has_tsc_control = true;
1372 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
1373 kvm_tsc_scaling_ratio_frac_bits = 32;
1374 }
1375
1376 /* Check for pause filtering support */
1377 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
1378 pause_filter_count = 0;
1379 pause_filter_thresh = 0;
1380 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
1381 pause_filter_thresh = 0;
1382 }
1383
1384 if (nested) {
1385 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
1386 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
1387 }
1388
1389 if (sev) {
1390 if (boot_cpu_has(X86_FEATURE_SEV) &&
1391 IS_ENABLED(CONFIG_KVM_AMD_SEV)) {
1392 r = sev_hardware_setup();
1393 if (r)
1394 sev = false;
1395 } else {
1396 sev = false;
1397 }
1398 }
1399
Olivier Deprez0e641232021-09-23 10:07:05 +02001400 svm_adjust_mmio_mask();
1401
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001402 for_each_possible_cpu(cpu) {
1403 r = svm_cpu_init(cpu);
1404 if (r)
1405 goto err;
1406 }
1407
1408 if (!boot_cpu_has(X86_FEATURE_NPT))
1409 npt_enabled = false;
1410
1411 if (npt_enabled && !npt) {
1412 printk(KERN_INFO "kvm: Nested Paging disabled\n");
1413 npt_enabled = false;
1414 }
1415
1416 if (npt_enabled) {
1417 printk(KERN_INFO "kvm: Nested Paging enabled\n");
1418 kvm_enable_tdp();
1419 } else
1420 kvm_disable_tdp();
1421
David Brazdil0f672f62019-12-10 10:32:29 +00001422 if (nrips) {
1423 if (!boot_cpu_has(X86_FEATURE_NRIPS))
1424 nrips = false;
1425 }
1426
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001427 if (avic) {
1428 if (!npt_enabled ||
1429 !boot_cpu_has(X86_FEATURE_AVIC) ||
1430 !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
1431 avic = false;
1432 } else {
1433 pr_info("AVIC enabled\n");
1434
1435 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1436 }
1437 }
1438
1439 if (vls) {
1440 if (!npt_enabled ||
1441 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
1442 !IS_ENABLED(CONFIG_X86_64)) {
1443 vls = false;
1444 } else {
1445 pr_info("Virtual VMLOAD VMSAVE supported\n");
1446 }
1447 }
1448
Olivier Deprez0e641232021-09-23 10:07:05 +02001449 vgif = false; /* Disabled for CVE-2021-3653 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001450
1451 return 0;
1452
1453err:
1454 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
1455 iopm_base = 0;
1456 return r;
1457}
1458
1459static __exit void svm_hardware_unsetup(void)
1460{
1461 int cpu;
1462
1463 if (svm_sev_enabled())
1464 bitmap_free(sev_asid_bitmap);
1465
1466 for_each_possible_cpu(cpu)
1467 svm_cpu_uninit(cpu);
1468
1469 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
1470 iopm_base = 0;
1471}
1472
1473static void init_seg(struct vmcb_seg *seg)
1474{
1475 seg->selector = 0;
1476 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
1477 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
1478 seg->limit = 0xffff;
1479 seg->base = 0;
1480}
1481
1482static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1483{
1484 seg->selector = 0;
1485 seg->attrib = SVM_SELECTOR_P_MASK | type;
1486 seg->limit = 0xffff;
1487 seg->base = 0;
1488}
1489
1490static u64 svm_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
1491{
1492 struct vcpu_svm *svm = to_svm(vcpu);
1493
1494 if (is_guest_mode(vcpu))
1495 return svm->nested.hsave->control.tsc_offset;
1496
1497 return vcpu->arch.tsc_offset;
1498}
1499
1500static u64 svm_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1501{
1502 struct vcpu_svm *svm = to_svm(vcpu);
1503 u64 g_tsc_offset = 0;
1504
1505 if (is_guest_mode(vcpu)) {
1506 /* Write L1's TSC offset. */
1507 g_tsc_offset = svm->vmcb->control.tsc_offset -
1508 svm->nested.hsave->control.tsc_offset;
1509 svm->nested.hsave->control.tsc_offset = offset;
David Brazdil0f672f62019-12-10 10:32:29 +00001510 }
1511
1512 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1513 svm->vmcb->control.tsc_offset - g_tsc_offset,
1514 offset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001515
1516 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
1517
1518 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
1519 return svm->vmcb->control.tsc_offset;
1520}
1521
1522static void avic_init_vmcb(struct vcpu_svm *svm)
1523{
1524 struct vmcb *vmcb = svm->vmcb;
1525 struct kvm_svm *kvm_svm = to_kvm_svm(svm->vcpu.kvm);
1526 phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page));
1527 phys_addr_t lpa = __sme_set(page_to_phys(kvm_svm->avic_logical_id_table_page));
1528 phys_addr_t ppa = __sme_set(page_to_phys(kvm_svm->avic_physical_id_table_page));
1529
1530 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1531 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1532 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1533 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1534 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
1535}
1536
1537static void init_vmcb(struct vcpu_svm *svm)
1538{
1539 struct vmcb_control_area *control = &svm->vmcb->control;
1540 struct vmcb_save_area *save = &svm->vmcb->save;
1541
1542 svm->vcpu.arch.hflags = 0;
1543
1544 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1545 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1546 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1547 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1548 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1549 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
1550 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1551 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
1552
1553 set_dr_intercepts(svm);
1554
1555 set_exception_intercept(svm, PF_VECTOR);
1556 set_exception_intercept(svm, UD_VECTOR);
1557 set_exception_intercept(svm, MC_VECTOR);
1558 set_exception_intercept(svm, AC_VECTOR);
1559 set_exception_intercept(svm, DB_VECTOR);
1560 /*
1561 * Guest access to VMware backdoor ports could legitimately
1562 * trigger #GP because of TSS I/O permission bitmap.
1563 * We intercept those #GP and allow access to them anyway
1564 * as VMware does.
1565 */
1566 if (enable_vmware_backdoor)
1567 set_exception_intercept(svm, GP_VECTOR);
1568
1569 set_intercept(svm, INTERCEPT_INTR);
1570 set_intercept(svm, INTERCEPT_NMI);
1571 set_intercept(svm, INTERCEPT_SMI);
1572 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
1573 set_intercept(svm, INTERCEPT_RDPMC);
1574 set_intercept(svm, INTERCEPT_CPUID);
1575 set_intercept(svm, INTERCEPT_INVD);
1576 set_intercept(svm, INTERCEPT_INVLPG);
1577 set_intercept(svm, INTERCEPT_INVLPGA);
1578 set_intercept(svm, INTERCEPT_IOIO_PROT);
1579 set_intercept(svm, INTERCEPT_MSR_PROT);
1580 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1581 set_intercept(svm, INTERCEPT_SHUTDOWN);
1582 set_intercept(svm, INTERCEPT_VMRUN);
1583 set_intercept(svm, INTERCEPT_VMMCALL);
1584 set_intercept(svm, INTERCEPT_VMLOAD);
1585 set_intercept(svm, INTERCEPT_VMSAVE);
1586 set_intercept(svm, INTERCEPT_STGI);
1587 set_intercept(svm, INTERCEPT_CLGI);
1588 set_intercept(svm, INTERCEPT_SKINIT);
1589 set_intercept(svm, INTERCEPT_WBINVD);
1590 set_intercept(svm, INTERCEPT_XSETBV);
David Brazdil0f672f62019-12-10 10:32:29 +00001591 set_intercept(svm, INTERCEPT_RDPRU);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001592 set_intercept(svm, INTERCEPT_RSM);
1593
1594 if (!kvm_mwait_in_guest(svm->vcpu.kvm)) {
1595 set_intercept(svm, INTERCEPT_MONITOR);
1596 set_intercept(svm, INTERCEPT_MWAIT);
1597 }
1598
1599 if (!kvm_hlt_in_guest(svm->vcpu.kvm))
1600 set_intercept(svm, INTERCEPT_HLT);
1601
1602 control->iopm_base_pa = __sme_set(iopm_base);
1603 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
1604 control->int_ctl = V_INTR_MASKING_MASK;
1605
1606 init_seg(&save->es);
1607 init_seg(&save->ss);
1608 init_seg(&save->ds);
1609 init_seg(&save->fs);
1610 init_seg(&save->gs);
1611
1612 save->cs.selector = 0xf000;
1613 save->cs.base = 0xffff0000;
1614 /* Executable/Readable Code Segment */
1615 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1616 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1617 save->cs.limit = 0xffff;
1618
1619 save->gdtr.limit = 0xffff;
1620 save->idtr.limit = 0xffff;
1621
1622 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1623 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1624
1625 svm_set_efer(&svm->vcpu, 0);
1626 save->dr6 = 0xffff0ff0;
1627 kvm_set_rflags(&svm->vcpu, 2);
1628 save->rip = 0x0000fff0;
1629 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
1630
1631 /*
1632 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
1633 * It also updates the guest-visible cr0 value.
1634 */
1635 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
1636 kvm_mmu_reset_context(&svm->vcpu);
1637
1638 save->cr4 = X86_CR4_PAE;
1639 /* rdx = ?? */
1640
1641 if (npt_enabled) {
1642 /* Setup VMCB for Nested Paging */
1643 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
1644 clr_intercept(svm, INTERCEPT_INVLPG);
1645 clr_exception_intercept(svm, PF_VECTOR);
1646 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1647 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1648 save->g_pat = svm->vcpu.arch.pat;
1649 save->cr3 = 0;
1650 save->cr4 = 0;
1651 }
1652 svm->asid_generation = 0;
1653
1654 svm->nested.vmcb = 0;
1655 svm->vcpu.arch.hflags = 0;
1656
1657 if (pause_filter_count) {
1658 control->pause_filter_count = pause_filter_count;
1659 if (pause_filter_thresh)
1660 control->pause_filter_thresh = pause_filter_thresh;
1661 set_intercept(svm, INTERCEPT_PAUSE);
1662 } else {
1663 clr_intercept(svm, INTERCEPT_PAUSE);
1664 }
1665
1666 if (kvm_vcpu_apicv_active(&svm->vcpu))
1667 avic_init_vmcb(svm);
1668
1669 /*
1670 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1671 * in VMCB and clear intercepts to avoid #VMEXIT.
1672 */
1673 if (vls) {
1674 clr_intercept(svm, INTERCEPT_VMLOAD);
1675 clr_intercept(svm, INTERCEPT_VMSAVE);
1676 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1677 }
1678
1679 if (vgif) {
1680 clr_intercept(svm, INTERCEPT_STGI);
1681 clr_intercept(svm, INTERCEPT_CLGI);
1682 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1683 }
1684
1685 if (sev_guest(svm->vcpu.kvm)) {
1686 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
1687 clr_exception_intercept(svm, UD_VECTOR);
1688 }
1689
1690 mark_all_dirty(svm->vmcb);
1691
1692 enable_gif(svm);
1693
1694}
1695
1696static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
1697 unsigned int index)
1698{
1699 u64 *avic_physical_id_table;
1700 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
1701
1702 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1703 return NULL;
1704
1705 avic_physical_id_table = page_address(kvm_svm->avic_physical_id_table_page);
1706
1707 return &avic_physical_id_table[index];
1708}
1709
1710/**
1711 * Note:
1712 * AVIC hardware walks the nested page table to check permissions,
1713 * but does not use the SPA address specified in the leaf page
1714 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1715 * field of the VMCB. Therefore, we set up the
1716 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1717 */
1718static int avic_init_access_page(struct kvm_vcpu *vcpu)
1719{
1720 struct kvm *kvm = vcpu->kvm;
1721 int ret = 0;
1722
1723 mutex_lock(&kvm->slots_lock);
1724 if (kvm->arch.apic_access_page_done)
1725 goto out;
1726
1727 ret = __x86_set_memory_region(kvm,
1728 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1729 APIC_DEFAULT_PHYS_BASE,
1730 PAGE_SIZE);
1731 if (ret)
1732 goto out;
1733
1734 kvm->arch.apic_access_page_done = true;
1735out:
1736 mutex_unlock(&kvm->slots_lock);
1737 return ret;
1738}
1739
1740static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1741{
1742 int ret;
1743 u64 *entry, new_entry;
1744 int id = vcpu->vcpu_id;
1745 struct vcpu_svm *svm = to_svm(vcpu);
1746
1747 ret = avic_init_access_page(vcpu);
1748 if (ret)
1749 return ret;
1750
1751 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1752 return -EINVAL;
1753
1754 if (!svm->vcpu.arch.apic->regs)
1755 return -EINVAL;
1756
1757 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1758
1759 /* Setting AVIC backing page address in the phy APIC ID table */
1760 entry = avic_get_physical_id_entry(vcpu, id);
1761 if (!entry)
1762 return -EINVAL;
1763
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001764 new_entry = __sme_set((page_to_phys(svm->avic_backing_page) &
1765 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1766 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK);
1767 WRITE_ONCE(*entry, new_entry);
1768
1769 svm->avic_physical_id_cache = entry;
1770
1771 return 0;
1772}
1773
1774static void __sev_asid_free(int asid)
1775{
1776 struct svm_cpu_data *sd;
1777 int cpu, pos;
1778
1779 pos = asid - 1;
1780 clear_bit(pos, sev_asid_bitmap);
1781
1782 for_each_possible_cpu(cpu) {
1783 sd = per_cpu(svm_data, cpu);
Olivier Deprez0e641232021-09-23 10:07:05 +02001784 sd->sev_vmcbs[asid] = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001785 }
1786}
1787
1788static void sev_asid_free(struct kvm *kvm)
1789{
1790 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1791
1792 __sev_asid_free(sev->asid);
1793}
1794
Olivier Deprez0e641232021-09-23 10:07:05 +02001795static void sev_decommission(unsigned int handle)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001796{
1797 struct sev_data_decommission *decommission;
Olivier Deprez0e641232021-09-23 10:07:05 +02001798
1799 if (!handle)
1800 return;
1801
1802 decommission = kzalloc(sizeof(*decommission), GFP_KERNEL);
1803 if (!decommission)
1804 return;
1805
1806 decommission->handle = handle;
1807 sev_guest_decommission(decommission, NULL);
1808
1809 kfree(decommission);
1810}
1811
1812static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
1813{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001814 struct sev_data_deactivate *data;
1815
1816 if (!handle)
1817 return;
1818
1819 data = kzalloc(sizeof(*data), GFP_KERNEL);
1820 if (!data)
1821 return;
1822
1823 /* deactivate handle */
1824 data->handle = handle;
1825 sev_guest_deactivate(data, NULL);
1826
1827 wbinvd_on_all_cpus();
1828 sev_guest_df_flush(NULL);
1829 kfree(data);
1830
Olivier Deprez0e641232021-09-23 10:07:05 +02001831 sev_decommission(handle);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001832}
1833
1834static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
1835 unsigned long ulen, unsigned long *n,
1836 int write)
1837{
1838 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1839 unsigned long npages, npinned, size;
1840 unsigned long locked, lock_limit;
1841 struct page **pages;
1842 unsigned long first, last;
1843
Olivier Deprez0e641232021-09-23 10:07:05 +02001844 lockdep_assert_held(&kvm->lock);
1845
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001846 if (ulen == 0 || uaddr + ulen < uaddr)
1847 return NULL;
1848
1849 /* Calculate number of pages. */
1850 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
1851 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
1852 npages = (last - first + 1);
1853
1854 locked = sev->pages_locked + npages;
1855 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1856 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
1857 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
1858 return NULL;
1859 }
1860
1861 /* Avoid using vmalloc for smaller buffers. */
1862 size = npages * sizeof(struct page *);
1863 if (size > PAGE_SIZE)
David Brazdil0f672f62019-12-10 10:32:29 +00001864 pages = __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO,
1865 PAGE_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001866 else
David Brazdil0f672f62019-12-10 10:32:29 +00001867 pages = kmalloc(size, GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001868
1869 if (!pages)
1870 return NULL;
1871
1872 /* Pin the user virtual address. */
Olivier Deprez0e641232021-09-23 10:07:05 +02001873 npinned = get_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001874 if (npinned != npages) {
1875 pr_err("SEV: Failure locking %lu pages.\n", npages);
1876 goto err;
1877 }
1878
1879 *n = npages;
1880 sev->pages_locked = locked;
1881
1882 return pages;
1883
1884err:
1885 if (npinned > 0)
1886 release_pages(pages, npinned);
1887
1888 kvfree(pages);
1889 return NULL;
1890}
1891
1892static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
1893 unsigned long npages)
1894{
1895 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1896
1897 release_pages(pages, npages);
1898 kvfree(pages);
1899 sev->pages_locked -= npages;
1900}
1901
1902static void sev_clflush_pages(struct page *pages[], unsigned long npages)
1903{
1904 uint8_t *page_virtual;
1905 unsigned long i;
1906
1907 if (npages == 0 || pages == NULL)
1908 return;
1909
1910 for (i = 0; i < npages; i++) {
1911 page_virtual = kmap_atomic(pages[i]);
1912 clflush_cache_range(page_virtual, PAGE_SIZE);
1913 kunmap_atomic(page_virtual);
1914 }
1915}
1916
1917static void __unregister_enc_region_locked(struct kvm *kvm,
1918 struct enc_region *region)
1919{
1920 /*
1921 * The guest may change the memory encryption attribute from C=0 -> C=1
1922 * or vice versa for this memory range. Lets make sure caches are
1923 * flushed to ensure that guest data gets written into memory with
1924 * correct C-bit.
1925 */
1926 sev_clflush_pages(region->pages, region->npages);
1927
1928 sev_unpin_memory(kvm, region->pages, region->npages);
1929 list_del(&region->list);
1930 kfree(region);
1931}
1932
1933static struct kvm *svm_vm_alloc(void)
1934{
David Brazdil0f672f62019-12-10 10:32:29 +00001935 struct kvm_svm *kvm_svm = __vmalloc(sizeof(struct kvm_svm),
1936 GFP_KERNEL_ACCOUNT | __GFP_ZERO,
1937 PAGE_KERNEL);
Olivier Deprez0e641232021-09-23 10:07:05 +02001938
1939 if (!kvm_svm)
1940 return NULL;
1941
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001942 return &kvm_svm->kvm;
1943}
1944
1945static void svm_vm_free(struct kvm *kvm)
1946{
1947 vfree(to_kvm_svm(kvm));
1948}
1949
1950static void sev_vm_destroy(struct kvm *kvm)
1951{
1952 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1953 struct list_head *head = &sev->regions_list;
1954 struct list_head *pos, *q;
1955
1956 if (!sev_guest(kvm))
1957 return;
1958
1959 mutex_lock(&kvm->lock);
1960
1961 /*
1962 * if userspace was terminated before unregistering the memory regions
1963 * then lets unpin all the registered memory.
1964 */
1965 if (!list_empty(head)) {
1966 list_for_each_safe(pos, q, head) {
1967 __unregister_enc_region_locked(kvm,
1968 list_entry(pos, struct enc_region, list));
Olivier Deprez0e641232021-09-23 10:07:05 +02001969 cond_resched();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001970 }
1971 }
1972
1973 mutex_unlock(&kvm->lock);
1974
1975 sev_unbind_asid(kvm, sev->handle);
1976 sev_asid_free(kvm);
1977}
1978
1979static void avic_vm_destroy(struct kvm *kvm)
1980{
1981 unsigned long flags;
1982 struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
1983
1984 if (!avic)
1985 return;
1986
1987 if (kvm_svm->avic_logical_id_table_page)
1988 __free_page(kvm_svm->avic_logical_id_table_page);
1989 if (kvm_svm->avic_physical_id_table_page)
1990 __free_page(kvm_svm->avic_physical_id_table_page);
1991
1992 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1993 hash_del(&kvm_svm->hnode);
1994 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1995}
1996
1997static void svm_vm_destroy(struct kvm *kvm)
1998{
1999 avic_vm_destroy(kvm);
2000 sev_vm_destroy(kvm);
2001}
2002
2003static int avic_vm_init(struct kvm *kvm)
2004{
2005 unsigned long flags;
2006 int err = -ENOMEM;
2007 struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
2008 struct kvm_svm *k2;
2009 struct page *p_page;
2010 struct page *l_page;
2011 u32 vm_id;
2012
2013 if (!avic)
2014 return 0;
2015
2016 /* Allocating physical APIC ID table (4KB) */
David Brazdil0f672f62019-12-10 10:32:29 +00002017 p_page = alloc_page(GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002018 if (!p_page)
2019 goto free_avic;
2020
2021 kvm_svm->avic_physical_id_table_page = p_page;
2022 clear_page(page_address(p_page));
2023
2024 /* Allocating logical APIC ID table (4KB) */
David Brazdil0f672f62019-12-10 10:32:29 +00002025 l_page = alloc_page(GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002026 if (!l_page)
2027 goto free_avic;
2028
2029 kvm_svm->avic_logical_id_table_page = l_page;
2030 clear_page(page_address(l_page));
2031
2032 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
2033 again:
2034 vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
2035 if (vm_id == 0) { /* id is 1-based, zero is not okay */
2036 next_vm_id_wrapped = 1;
2037 goto again;
2038 }
2039 /* Is it still in use? Only possible if wrapped at least once */
2040 if (next_vm_id_wrapped) {
2041 hash_for_each_possible(svm_vm_data_hash, k2, hnode, vm_id) {
2042 if (k2->avic_vm_id == vm_id)
2043 goto again;
2044 }
2045 }
2046 kvm_svm->avic_vm_id = vm_id;
2047 hash_add(svm_vm_data_hash, &kvm_svm->hnode, kvm_svm->avic_vm_id);
2048 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
2049
2050 return 0;
2051
2052free_avic:
2053 avic_vm_destroy(kvm);
2054 return err;
2055}
2056
2057static inline int
2058avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
2059{
2060 int ret = 0;
2061 unsigned long flags;
2062 struct amd_svm_iommu_ir *ir;
2063 struct vcpu_svm *svm = to_svm(vcpu);
2064
2065 if (!kvm_arch_has_assigned_device(vcpu->kvm))
2066 return 0;
2067
2068 /*
2069 * Here, we go through the per-vcpu ir_list to update all existing
2070 * interrupt remapping table entry targeting this vcpu.
2071 */
2072 spin_lock_irqsave(&svm->ir_list_lock, flags);
2073
2074 if (list_empty(&svm->ir_list))
2075 goto out;
2076
2077 list_for_each_entry(ir, &svm->ir_list, node) {
2078 ret = amd_iommu_update_ga(cpu, r, ir->data);
2079 if (ret)
2080 break;
2081 }
2082out:
2083 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
2084 return ret;
2085}
2086
2087static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2088{
2089 u64 entry;
2090 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
2091 int h_physical_id = kvm_cpu_get_apicid(cpu);
2092 struct vcpu_svm *svm = to_svm(vcpu);
2093
2094 if (!kvm_vcpu_apicv_active(vcpu))
2095 return;
2096
David Brazdil0f672f62019-12-10 10:32:29 +00002097 /*
2098 * Since the host physical APIC id is 8 bits,
2099 * we can support host APIC ID upto 255.
2100 */
2101 if (WARN_ON(h_physical_id > AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002102 return;
2103
2104 entry = READ_ONCE(*(svm->avic_physical_id_cache));
2105 WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
2106
2107 entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
2108 entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
2109
2110 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2111 if (svm->avic_is_running)
2112 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2113
2114 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
2115 avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
2116 svm->avic_is_running);
2117}
2118
2119static void avic_vcpu_put(struct kvm_vcpu *vcpu)
2120{
2121 u64 entry;
2122 struct vcpu_svm *svm = to_svm(vcpu);
2123
2124 if (!kvm_vcpu_apicv_active(vcpu))
2125 return;
2126
2127 entry = READ_ONCE(*(svm->avic_physical_id_cache));
2128 if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
2129 avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
2130
2131 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2132 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
2133}
2134
2135/**
2136 * This function is called during VCPU halt/unhalt.
2137 */
2138static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
2139{
2140 struct vcpu_svm *svm = to_svm(vcpu);
2141
2142 svm->avic_is_running = is_run;
2143 if (is_run)
2144 avic_vcpu_load(vcpu, vcpu->cpu);
2145 else
2146 avic_vcpu_put(vcpu);
2147}
2148
2149static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
2150{
2151 struct vcpu_svm *svm = to_svm(vcpu);
2152 u32 dummy;
2153 u32 eax = 1;
2154
2155 vcpu->arch.microcode_version = 0x01000065;
2156 svm->spec_ctrl = 0;
2157 svm->virt_spec_ctrl = 0;
2158
2159 if (!init_event) {
2160 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
2161 MSR_IA32_APICBASE_ENABLE;
2162 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
2163 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
2164 }
2165 init_vmcb(svm);
2166
2167 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true);
David Brazdil0f672f62019-12-10 10:32:29 +00002168 kvm_rdx_write(vcpu, eax);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002169
2170 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
2171 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
2172}
2173
2174static int avic_init_vcpu(struct vcpu_svm *svm)
2175{
2176 int ret;
2177
2178 if (!kvm_vcpu_apicv_active(&svm->vcpu))
2179 return 0;
2180
2181 ret = avic_init_backing_page(&svm->vcpu);
2182 if (ret)
2183 return ret;
2184
2185 INIT_LIST_HEAD(&svm->ir_list);
2186 spin_lock_init(&svm->ir_list_lock);
David Brazdil0f672f62019-12-10 10:32:29 +00002187 svm->dfr_reg = APIC_DFR_FLAT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002188
2189 return ret;
2190}
2191
2192static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
2193{
2194 struct vcpu_svm *svm;
2195 struct page *page;
2196 struct page *msrpm_pages;
2197 struct page *hsave_page;
2198 struct page *nested_msrpm_pages;
2199 int err;
2200
David Brazdil0f672f62019-12-10 10:32:29 +00002201 BUILD_BUG_ON_MSG(offsetof(struct vcpu_svm, vcpu) != 0,
2202 "struct kvm_vcpu must be at offset 0 for arch usercopy region");
2203
2204 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002205 if (!svm) {
2206 err = -ENOMEM;
2207 goto out;
2208 }
2209
David Brazdil0f672f62019-12-10 10:32:29 +00002210 svm->vcpu.arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
2211 GFP_KERNEL_ACCOUNT);
2212 if (!svm->vcpu.arch.user_fpu) {
2213 printk(KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n");
2214 err = -ENOMEM;
2215 goto free_partial_svm;
2216 }
2217
2218 svm->vcpu.arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
2219 GFP_KERNEL_ACCOUNT);
2220 if (!svm->vcpu.arch.guest_fpu) {
2221 printk(KERN_ERR "kvm: failed to allocate vcpu's fpu\n");
2222 err = -ENOMEM;
2223 goto free_user_fpu;
2224 }
2225
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002226 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
2227 if (err)
2228 goto free_svm;
2229
2230 err = -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +00002231 page = alloc_page(GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002232 if (!page)
2233 goto uninit;
2234
David Brazdil0f672f62019-12-10 10:32:29 +00002235 msrpm_pages = alloc_pages(GFP_KERNEL_ACCOUNT, MSRPM_ALLOC_ORDER);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002236 if (!msrpm_pages)
2237 goto free_page1;
2238
David Brazdil0f672f62019-12-10 10:32:29 +00002239 nested_msrpm_pages = alloc_pages(GFP_KERNEL_ACCOUNT, MSRPM_ALLOC_ORDER);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002240 if (!nested_msrpm_pages)
2241 goto free_page2;
2242
David Brazdil0f672f62019-12-10 10:32:29 +00002243 hsave_page = alloc_page(GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002244 if (!hsave_page)
2245 goto free_page3;
2246
2247 err = avic_init_vcpu(svm);
2248 if (err)
2249 goto free_page4;
2250
2251 /* We initialize this flag to true to make sure that the is_running
2252 * bit would be set the first time the vcpu is loaded.
2253 */
2254 svm->avic_is_running = true;
2255
2256 svm->nested.hsave = page_address(hsave_page);
2257
2258 svm->msrpm = page_address(msrpm_pages);
2259 svm_vcpu_init_msrpm(svm->msrpm);
2260
2261 svm->nested.msrpm = page_address(nested_msrpm_pages);
2262 svm_vcpu_init_msrpm(svm->nested.msrpm);
2263
2264 svm->vmcb = page_address(page);
2265 clear_page(svm->vmcb);
2266 svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT);
2267 svm->asid_generation = 0;
2268 init_vmcb(svm);
2269
2270 svm_init_osvw(&svm->vcpu);
2271
2272 return &svm->vcpu;
2273
2274free_page4:
2275 __free_page(hsave_page);
2276free_page3:
2277 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
2278free_page2:
2279 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
2280free_page1:
2281 __free_page(page);
2282uninit:
2283 kvm_vcpu_uninit(&svm->vcpu);
2284free_svm:
David Brazdil0f672f62019-12-10 10:32:29 +00002285 kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.guest_fpu);
2286free_user_fpu:
2287 kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.user_fpu);
2288free_partial_svm:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002289 kmem_cache_free(kvm_vcpu_cache, svm);
2290out:
2291 return ERR_PTR(err);
2292}
2293
2294static void svm_clear_current_vmcb(struct vmcb *vmcb)
2295{
2296 int i;
2297
2298 for_each_online_cpu(i)
2299 cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL);
2300}
2301
2302static void svm_free_vcpu(struct kvm_vcpu *vcpu)
2303{
2304 struct vcpu_svm *svm = to_svm(vcpu);
2305
2306 /*
2307 * The vmcb page can be recycled, causing a false negative in
2308 * svm_vcpu_load(). So, ensure that no logical CPU has this
2309 * vmcb page recorded as its current vmcb.
2310 */
2311 svm_clear_current_vmcb(svm->vmcb);
2312
2313 __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT));
2314 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
2315 __free_page(virt_to_page(svm->nested.hsave));
2316 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
2317 kvm_vcpu_uninit(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00002318 kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.user_fpu);
2319 kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.guest_fpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002320 kmem_cache_free(kvm_vcpu_cache, svm);
2321}
2322
2323static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2324{
2325 struct vcpu_svm *svm = to_svm(vcpu);
2326 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2327 int i;
2328
2329 if (unlikely(cpu != vcpu->cpu)) {
2330 svm->asid_generation = 0;
2331 mark_all_dirty(svm->vmcb);
2332 }
2333
2334#ifdef CONFIG_X86_64
2335 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
2336#endif
2337 savesegment(fs, svm->host.fs);
2338 savesegment(gs, svm->host.gs);
2339 svm->host.ldt = kvm_read_ldt();
2340
2341 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
2342 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
2343
2344 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
2345 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
2346 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
2347 __this_cpu_write(current_tsc_ratio, tsc_ratio);
2348 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
2349 }
2350 }
2351 /* This assumes that the kernel never uses MSR_TSC_AUX */
2352 if (static_cpu_has(X86_FEATURE_RDTSCP))
2353 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
2354
2355 if (sd->current_vmcb != svm->vmcb) {
2356 sd->current_vmcb = svm->vmcb;
2357 indirect_branch_prediction_barrier();
2358 }
2359 avic_vcpu_load(vcpu, cpu);
2360}
2361
2362static void svm_vcpu_put(struct kvm_vcpu *vcpu)
2363{
2364 struct vcpu_svm *svm = to_svm(vcpu);
2365 int i;
2366
2367 avic_vcpu_put(vcpu);
2368
2369 ++vcpu->stat.host_state_reload;
2370 kvm_load_ldt(svm->host.ldt);
2371#ifdef CONFIG_X86_64
2372 loadsegment(fs, svm->host.fs);
2373 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
2374 load_gs_index(svm->host.gs);
2375#else
2376#ifdef CONFIG_X86_32_LAZY_GS
2377 loadsegment(gs, svm->host.gs);
2378#endif
2379#endif
2380 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
2381 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
2382}
2383
2384static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
2385{
2386 avic_set_running(vcpu, false);
2387}
2388
2389static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
2390{
2391 avic_set_running(vcpu, true);
2392}
2393
2394static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
2395{
2396 struct vcpu_svm *svm = to_svm(vcpu);
2397 unsigned long rflags = svm->vmcb->save.rflags;
2398
2399 if (svm->nmi_singlestep) {
2400 /* Hide our flags if they were not set by the guest */
2401 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
2402 rflags &= ~X86_EFLAGS_TF;
2403 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
2404 rflags &= ~X86_EFLAGS_RF;
2405 }
2406 return rflags;
2407}
2408
2409static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2410{
2411 if (to_svm(vcpu)->nmi_singlestep)
2412 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2413
2414 /*
2415 * Any change of EFLAGS.VM is accompanied by a reload of SS
2416 * (caused by either a task switch or an inter-privilege IRET),
2417 * so we do not need to update the CPL here.
2418 */
2419 to_svm(vcpu)->vmcb->save.rflags = rflags;
2420}
2421
2422static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
2423{
2424 switch (reg) {
2425 case VCPU_EXREG_PDPTR:
2426 BUG_ON(!npt_enabled);
2427 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
2428 break;
2429 default:
2430 BUG();
2431 }
2432}
2433
2434static void svm_set_vintr(struct vcpu_svm *svm)
2435{
2436 set_intercept(svm, INTERCEPT_VINTR);
2437}
2438
2439static void svm_clear_vintr(struct vcpu_svm *svm)
2440{
2441 clr_intercept(svm, INTERCEPT_VINTR);
2442}
2443
2444static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
2445{
2446 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
2447
2448 switch (seg) {
2449 case VCPU_SREG_CS: return &save->cs;
2450 case VCPU_SREG_DS: return &save->ds;
2451 case VCPU_SREG_ES: return &save->es;
2452 case VCPU_SREG_FS: return &save->fs;
2453 case VCPU_SREG_GS: return &save->gs;
2454 case VCPU_SREG_SS: return &save->ss;
2455 case VCPU_SREG_TR: return &save->tr;
2456 case VCPU_SREG_LDTR: return &save->ldtr;
2457 }
2458 BUG();
2459 return NULL;
2460}
2461
2462static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2463{
2464 struct vmcb_seg *s = svm_seg(vcpu, seg);
2465
2466 return s->base;
2467}
2468
2469static void svm_get_segment(struct kvm_vcpu *vcpu,
2470 struct kvm_segment *var, int seg)
2471{
2472 struct vmcb_seg *s = svm_seg(vcpu, seg);
2473
2474 var->base = s->base;
2475 var->limit = s->limit;
2476 var->selector = s->selector;
2477 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
2478 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
2479 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
2480 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
2481 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
2482 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
2483 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
2484
2485 /*
2486 * AMD CPUs circa 2014 track the G bit for all segments except CS.
2487 * However, the SVM spec states that the G bit is not observed by the
2488 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
2489 * So let's synthesize a legal G bit for all segments, this helps
2490 * running KVM nested. It also helps cross-vendor migration, because
2491 * Intel's vmentry has a check on the 'G' bit.
2492 */
2493 var->g = s->limit > 0xfffff;
2494
2495 /*
2496 * AMD's VMCB does not have an explicit unusable field, so emulate it
2497 * for cross vendor migration purposes by "not present"
2498 */
2499 var->unusable = !var->present;
2500
2501 switch (seg) {
2502 case VCPU_SREG_TR:
2503 /*
2504 * Work around a bug where the busy flag in the tr selector
2505 * isn't exposed
2506 */
2507 var->type |= 0x2;
2508 break;
2509 case VCPU_SREG_DS:
2510 case VCPU_SREG_ES:
2511 case VCPU_SREG_FS:
2512 case VCPU_SREG_GS:
2513 /*
2514 * The accessed bit must always be set in the segment
2515 * descriptor cache, although it can be cleared in the
2516 * descriptor, the cached bit always remains at 1. Since
2517 * Intel has a check on this, set it here to support
2518 * cross-vendor migration.
2519 */
2520 if (!var->unusable)
2521 var->type |= 0x1;
2522 break;
2523 case VCPU_SREG_SS:
2524 /*
2525 * On AMD CPUs sometimes the DB bit in the segment
2526 * descriptor is left as 1, although the whole segment has
2527 * been made unusable. Clear it here to pass an Intel VMX
2528 * entry check when cross vendor migrating.
2529 */
2530 if (var->unusable)
2531 var->db = 0;
2532 /* This is symmetric with svm_set_segment() */
2533 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
2534 break;
2535 }
2536}
2537
2538static int svm_get_cpl(struct kvm_vcpu *vcpu)
2539{
2540 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
2541
2542 return save->cpl;
2543}
2544
2545static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
2546{
2547 struct vcpu_svm *svm = to_svm(vcpu);
2548
2549 dt->size = svm->vmcb->save.idtr.limit;
2550 dt->address = svm->vmcb->save.idtr.base;
2551}
2552
2553static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
2554{
2555 struct vcpu_svm *svm = to_svm(vcpu);
2556
2557 svm->vmcb->save.idtr.limit = dt->size;
2558 svm->vmcb->save.idtr.base = dt->address ;
2559 mark_dirty(svm->vmcb, VMCB_DT);
2560}
2561
2562static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
2563{
2564 struct vcpu_svm *svm = to_svm(vcpu);
2565
2566 dt->size = svm->vmcb->save.gdtr.limit;
2567 dt->address = svm->vmcb->save.gdtr.base;
2568}
2569
2570static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
2571{
2572 struct vcpu_svm *svm = to_svm(vcpu);
2573
2574 svm->vmcb->save.gdtr.limit = dt->size;
2575 svm->vmcb->save.gdtr.base = dt->address ;
2576 mark_dirty(svm->vmcb, VMCB_DT);
2577}
2578
2579static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2580{
2581}
2582
2583static void svm_decache_cr3(struct kvm_vcpu *vcpu)
2584{
2585}
2586
2587static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
2588{
2589}
2590
2591static void update_cr0_intercept(struct vcpu_svm *svm)
2592{
2593 ulong gcr0 = svm->vcpu.arch.cr0;
2594 u64 *hcr0 = &svm->vmcb->save.cr0;
2595
2596 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
2597 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
2598
2599 mark_dirty(svm->vmcb, VMCB_CR);
2600
2601 if (gcr0 == *hcr0) {
2602 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
2603 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
2604 } else {
2605 set_cr_intercept(svm, INTERCEPT_CR0_READ);
2606 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
2607 }
2608}
2609
2610static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2611{
2612 struct vcpu_svm *svm = to_svm(vcpu);
2613
2614#ifdef CONFIG_X86_64
2615 if (vcpu->arch.efer & EFER_LME) {
2616 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
2617 vcpu->arch.efer |= EFER_LMA;
2618 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
2619 }
2620
2621 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
2622 vcpu->arch.efer &= ~EFER_LMA;
2623 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
2624 }
2625 }
2626#endif
2627 vcpu->arch.cr0 = cr0;
2628
2629 if (!npt_enabled)
2630 cr0 |= X86_CR0_PG | X86_CR0_WP;
2631
2632 /*
2633 * re-enable caching here because the QEMU bios
2634 * does not do it - this results in some delay at
2635 * reboot
2636 */
2637 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
2638 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
2639 svm->vmcb->save.cr0 = cr0;
2640 mark_dirty(svm->vmcb, VMCB_CR);
2641 update_cr0_intercept(svm);
2642}
2643
2644static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
2645{
2646 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
2647 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
2648
2649 if (cr4 & X86_CR4_VMXE)
2650 return 1;
2651
2652 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
2653 svm_flush_tlb(vcpu, true);
2654
2655 vcpu->arch.cr4 = cr4;
2656 if (!npt_enabled)
2657 cr4 |= X86_CR4_PAE;
2658 cr4 |= host_cr4_mce;
2659 to_svm(vcpu)->vmcb->save.cr4 = cr4;
2660 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
2661 return 0;
2662}
2663
2664static void svm_set_segment(struct kvm_vcpu *vcpu,
2665 struct kvm_segment *var, int seg)
2666{
2667 struct vcpu_svm *svm = to_svm(vcpu);
2668 struct vmcb_seg *s = svm_seg(vcpu, seg);
2669
2670 s->base = var->base;
2671 s->limit = var->limit;
2672 s->selector = var->selector;
2673 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
2674 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
2675 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
2676 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
2677 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
2678 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
2679 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
2680 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
2681
2682 /*
2683 * This is always accurate, except if SYSRET returned to a segment
2684 * with SS.DPL != 3. Intel does not have this quirk, and always
2685 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
2686 * would entail passing the CPL to userspace and back.
2687 */
2688 if (seg == VCPU_SREG_SS)
2689 /* This is symmetric with svm_get_segment() */
2690 svm->vmcb->save.cpl = (var->dpl & 3);
2691
2692 mark_dirty(svm->vmcb, VMCB_SEG);
2693}
2694
2695static void update_bp_intercept(struct kvm_vcpu *vcpu)
2696{
2697 struct vcpu_svm *svm = to_svm(vcpu);
2698
2699 clr_exception_intercept(svm, BP_VECTOR);
2700
2701 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
2702 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2703 set_exception_intercept(svm, BP_VECTOR);
2704 } else
2705 vcpu->guest_debug = 0;
2706}
2707
2708static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
2709{
2710 if (sd->next_asid > sd->max_asid) {
2711 ++sd->asid_generation;
2712 sd->next_asid = sd->min_asid;
2713 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
2714 }
2715
2716 svm->asid_generation = sd->asid_generation;
2717 svm->vmcb->control.asid = sd->next_asid++;
2718
2719 mark_dirty(svm->vmcb, VMCB_ASID);
2720}
2721
2722static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
2723{
2724 return to_svm(vcpu)->vmcb->save.dr6;
2725}
2726
2727static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
2728{
2729 struct vcpu_svm *svm = to_svm(vcpu);
2730
2731 svm->vmcb->save.dr6 = value;
2732 mark_dirty(svm->vmcb, VMCB_DR);
2733}
2734
2735static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
2736{
2737 struct vcpu_svm *svm = to_svm(vcpu);
2738
2739 get_debugreg(vcpu->arch.db[0], 0);
2740 get_debugreg(vcpu->arch.db[1], 1);
2741 get_debugreg(vcpu->arch.db[2], 2);
2742 get_debugreg(vcpu->arch.db[3], 3);
2743 vcpu->arch.dr6 = svm_get_dr6(vcpu);
2744 vcpu->arch.dr7 = svm->vmcb->save.dr7;
2745
2746 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
2747 set_dr_intercepts(svm);
2748}
2749
2750static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
2751{
2752 struct vcpu_svm *svm = to_svm(vcpu);
2753
2754 svm->vmcb->save.dr7 = value;
2755 mark_dirty(svm->vmcb, VMCB_DR);
2756}
2757
2758static int pf_interception(struct vcpu_svm *svm)
2759{
2760 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
2761 u64 error_code = svm->vmcb->control.exit_info_1;
2762
2763 return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
2764 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2765 svm->vmcb->control.insn_bytes : NULL,
2766 svm->vmcb->control.insn_len);
2767}
2768
2769static int npf_interception(struct vcpu_svm *svm)
2770{
2771 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
2772 u64 error_code = svm->vmcb->control.exit_info_1;
2773
2774 trace_kvm_page_fault(fault_address, error_code);
2775 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
2776 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2777 svm->vmcb->control.insn_bytes : NULL,
2778 svm->vmcb->control.insn_len);
2779}
2780
2781static int db_interception(struct vcpu_svm *svm)
2782{
2783 struct kvm_run *kvm_run = svm->vcpu.run;
David Brazdil0f672f62019-12-10 10:32:29 +00002784 struct kvm_vcpu *vcpu = &svm->vcpu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002785
2786 if (!(svm->vcpu.guest_debug &
2787 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
2788 !svm->nmi_singlestep) {
2789 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
2790 return 1;
2791 }
2792
2793 if (svm->nmi_singlestep) {
2794 disable_nmi_singlestep(svm);
David Brazdil0f672f62019-12-10 10:32:29 +00002795 /* Make sure we check for pending NMIs upon entry */
2796 kvm_make_request(KVM_REQ_EVENT, vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002797 }
2798
2799 if (svm->vcpu.guest_debug &
2800 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
2801 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2802 kvm_run->debug.arch.pc =
2803 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2804 kvm_run->debug.arch.exception = DB_VECTOR;
2805 return 0;
2806 }
2807
2808 return 1;
2809}
2810
2811static int bp_interception(struct vcpu_svm *svm)
2812{
2813 struct kvm_run *kvm_run = svm->vcpu.run;
2814
2815 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2816 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2817 kvm_run->debug.arch.exception = BP_VECTOR;
2818 return 0;
2819}
2820
2821static int ud_interception(struct vcpu_svm *svm)
2822{
2823 return handle_ud(&svm->vcpu);
2824}
2825
2826static int ac_interception(struct vcpu_svm *svm)
2827{
2828 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
2829 return 1;
2830}
2831
2832static int gp_interception(struct vcpu_svm *svm)
2833{
2834 struct kvm_vcpu *vcpu = &svm->vcpu;
2835 u32 error_code = svm->vmcb->control.exit_info_1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002836
2837 WARN_ON_ONCE(!enable_vmware_backdoor);
2838
David Brazdil0f672f62019-12-10 10:32:29 +00002839 /*
2840 * VMware backdoor emulation on #GP interception only handles IN{S},
2841 * OUT{S}, and RDPMC, none of which generate a non-zero error code.
2842 */
2843 if (error_code) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002844 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
David Brazdil0f672f62019-12-10 10:32:29 +00002845 return 1;
2846 }
2847 return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002848}
2849
2850static bool is_erratum_383(void)
2851{
2852 int err, i;
2853 u64 value;
2854
2855 if (!erratum_383_found)
2856 return false;
2857
2858 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2859 if (err)
2860 return false;
2861
2862 /* Bit 62 may or may not be set for this mce */
2863 value &= ~(1ULL << 62);
2864
2865 if (value != 0xb600000000010015ULL)
2866 return false;
2867
2868 /* Clear MCi_STATUS registers */
2869 for (i = 0; i < 6; ++i)
2870 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2871
2872 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2873 if (!err) {
2874 u32 low, high;
2875
2876 value &= ~(1ULL << 2);
2877 low = lower_32_bits(value);
2878 high = upper_32_bits(value);
2879
2880 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2881 }
2882
2883 /* Flush tlb to evict multi-match entries */
2884 __flush_tlb_all();
2885
2886 return true;
2887}
2888
2889static void svm_handle_mce(struct vcpu_svm *svm)
2890{
2891 if (is_erratum_383()) {
2892 /*
2893 * Erratum 383 triggered. Guest state is corrupt so kill the
2894 * guest.
2895 */
2896 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2897
2898 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
2899
2900 return;
2901 }
2902
2903 /*
2904 * On an #MC intercept the MCE handler is not called automatically in
2905 * the host. So do it by hand here.
2906 */
2907 asm volatile (
2908 "int $0x12\n");
2909 /* not sure if we ever come back to this point */
2910
2911 return;
2912}
2913
2914static int mc_interception(struct vcpu_svm *svm)
2915{
2916 return 1;
2917}
2918
2919static int shutdown_interception(struct vcpu_svm *svm)
2920{
2921 struct kvm_run *kvm_run = svm->vcpu.run;
2922
2923 /*
2924 * VMCB is undefined after a SHUTDOWN intercept
2925 * so reinitialize it.
2926 */
2927 clear_page(svm->vmcb);
2928 init_vmcb(svm);
2929
2930 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2931 return 0;
2932}
2933
2934static int io_interception(struct vcpu_svm *svm)
2935{
2936 struct kvm_vcpu *vcpu = &svm->vcpu;
2937 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
2938 int size, in, string;
2939 unsigned port;
2940
2941 ++svm->vcpu.stat.io_exits;
2942 string = (io_info & SVM_IOIO_STR_MASK) != 0;
2943 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
2944 if (string)
David Brazdil0f672f62019-12-10 10:32:29 +00002945 return kvm_emulate_instruction(vcpu, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002946
2947 port = io_info >> 16;
2948 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
2949 svm->next_rip = svm->vmcb->control.exit_info_2;
2950
2951 return kvm_fast_pio(&svm->vcpu, size, port, in);
2952}
2953
2954static int nmi_interception(struct vcpu_svm *svm)
2955{
2956 return 1;
2957}
2958
2959static int intr_interception(struct vcpu_svm *svm)
2960{
2961 ++svm->vcpu.stat.irq_exits;
2962 return 1;
2963}
2964
2965static int nop_on_interception(struct vcpu_svm *svm)
2966{
2967 return 1;
2968}
2969
2970static int halt_interception(struct vcpu_svm *svm)
2971{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002972 return kvm_emulate_halt(&svm->vcpu);
2973}
2974
2975static int vmmcall_interception(struct vcpu_svm *svm)
2976{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002977 return kvm_emulate_hypercall(&svm->vcpu);
2978}
2979
2980static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2981{
2982 struct vcpu_svm *svm = to_svm(vcpu);
2983
2984 return svm->nested.nested_cr3;
2985}
2986
2987static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2988{
2989 struct vcpu_svm *svm = to_svm(vcpu);
2990 u64 cr3 = svm->nested.nested_cr3;
2991 u64 pdpte;
2992 int ret;
2993
2994 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
2995 offset_in_page(cr3) + index * 8, 8);
2996 if (ret)
2997 return 0;
2998 return pdpte;
2999}
3000
3001static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
3002 unsigned long root)
3003{
3004 struct vcpu_svm *svm = to_svm(vcpu);
3005
3006 svm->vmcb->control.nested_cr3 = __sme_set(root);
3007 mark_dirty(svm->vmcb, VMCB_NPT);
3008}
3009
3010static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
3011 struct x86_exception *fault)
3012{
3013 struct vcpu_svm *svm = to_svm(vcpu);
3014
3015 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
3016 /*
3017 * TODO: track the cause of the nested page fault, and
3018 * correctly fill in the high bits of exit_info_1.
3019 */
3020 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
3021 svm->vmcb->control.exit_code_hi = 0;
3022 svm->vmcb->control.exit_info_1 = (1ULL << 32);
3023 svm->vmcb->control.exit_info_2 = fault->address;
3024 }
3025
3026 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
3027 svm->vmcb->control.exit_info_1 |= fault->error_code;
3028
3029 /*
3030 * The present bit is always zero for page structure faults on real
3031 * hardware.
3032 */
3033 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
3034 svm->vmcb->control.exit_info_1 &= ~1;
3035
3036 nested_svm_vmexit(svm);
3037}
3038
3039static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
3040{
3041 WARN_ON(mmu_is_nested(vcpu));
David Brazdil0f672f62019-12-10 10:32:29 +00003042
3043 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003044 kvm_init_shadow_mmu(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00003045 vcpu->arch.mmu->set_cr3 = nested_svm_set_tdp_cr3;
3046 vcpu->arch.mmu->get_cr3 = nested_svm_get_tdp_cr3;
3047 vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
3048 vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
3049 vcpu->arch.mmu->shadow_root_level = get_npt_level(vcpu);
3050 reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003051 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
3052}
3053
3054static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
3055{
David Brazdil0f672f62019-12-10 10:32:29 +00003056 vcpu->arch.mmu = &vcpu->arch.root_mmu;
3057 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003058}
3059
3060static int nested_svm_check_permissions(struct vcpu_svm *svm)
3061{
3062 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
3063 !is_paging(&svm->vcpu)) {
3064 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3065 return 1;
3066 }
3067
3068 if (svm->vmcb->save.cpl) {
3069 kvm_inject_gp(&svm->vcpu, 0);
3070 return 1;
3071 }
3072
3073 return 0;
3074}
3075
3076static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
3077 bool has_error_code, u32 error_code)
3078{
3079 int vmexit;
3080
3081 if (!is_guest_mode(&svm->vcpu))
3082 return 0;
3083
3084 vmexit = nested_svm_intercept(svm);
3085 if (vmexit != NESTED_EXIT_DONE)
3086 return 0;
3087
3088 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
3089 svm->vmcb->control.exit_code_hi = 0;
3090 svm->vmcb->control.exit_info_1 = error_code;
3091
3092 /*
David Brazdil0f672f62019-12-10 10:32:29 +00003093 * EXITINFO2 is undefined for all exception intercepts other
3094 * than #PF.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003095 */
3096 if (svm->vcpu.arch.exception.nested_apf)
3097 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
David Brazdil0f672f62019-12-10 10:32:29 +00003098 else if (svm->vcpu.arch.exception.has_payload)
3099 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003100 else
3101 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
3102
3103 svm->nested.exit_required = true;
3104 return vmexit;
3105}
3106
3107/* This function returns true if it is save to enable the irq window */
3108static inline bool nested_svm_intr(struct vcpu_svm *svm)
3109{
3110 if (!is_guest_mode(&svm->vcpu))
3111 return true;
3112
3113 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
3114 return true;
3115
3116 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
3117 return false;
3118
3119 /*
3120 * if vmexit was already requested (by intercepted exception
3121 * for instance) do not overwrite it with "external interrupt"
3122 * vmexit.
3123 */
3124 if (svm->nested.exit_required)
3125 return false;
3126
3127 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
3128 svm->vmcb->control.exit_info_1 = 0;
3129 svm->vmcb->control.exit_info_2 = 0;
3130
3131 if (svm->nested.intercept & 1ULL) {
3132 /*
3133 * The #vmexit can't be emulated here directly because this
3134 * code path runs with irqs and preemption disabled. A
3135 * #vmexit emulation might sleep. Only signal request for
3136 * the #vmexit here.
3137 */
3138 svm->nested.exit_required = true;
3139 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
3140 return false;
3141 }
3142
3143 return true;
3144}
3145
3146/* This function returns true if it is save to enable the nmi window */
3147static inline bool nested_svm_nmi(struct vcpu_svm *svm)
3148{
3149 if (!is_guest_mode(&svm->vcpu))
3150 return true;
3151
3152 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
3153 return true;
3154
3155 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
3156 svm->nested.exit_required = true;
3157
3158 return false;
3159}
3160
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003161static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
3162{
3163 unsigned port, size, iopm_len;
3164 u16 val, mask;
3165 u8 start_bit;
3166 u64 gpa;
3167
3168 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
3169 return NESTED_EXIT_HOST;
3170
3171 port = svm->vmcb->control.exit_info_1 >> 16;
3172 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
3173 SVM_IOIO_SIZE_SHIFT;
3174 gpa = svm->nested.vmcb_iopm + (port / 8);
3175 start_bit = port % 8;
3176 iopm_len = (start_bit + size > 8) ? 2 : 1;
3177 mask = (0xf >> (4 - size)) << start_bit;
3178 val = 0;
3179
3180 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
3181 return NESTED_EXIT_DONE;
3182
3183 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
3184}
3185
3186static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
3187{
3188 u32 offset, msr, value;
3189 int write, mask;
3190
3191 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
3192 return NESTED_EXIT_HOST;
3193
3194 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
3195 offset = svm_msrpm_offset(msr);
3196 write = svm->vmcb->control.exit_info_1 & 1;
3197 mask = 1 << ((2 * (msr & 0xf)) + write);
3198
3199 if (offset == MSR_INVALID)
3200 return NESTED_EXIT_DONE;
3201
3202 /* Offset is in 32 bit units but need in 8 bit units */
3203 offset *= 4;
3204
3205 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
3206 return NESTED_EXIT_DONE;
3207
3208 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
3209}
3210
3211/* DB exceptions for our internal use must not cause vmexit */
3212static int nested_svm_intercept_db(struct vcpu_svm *svm)
3213{
3214 unsigned long dr6;
3215
3216 /* if we're not singlestepping, it's not ours */
3217 if (!svm->nmi_singlestep)
3218 return NESTED_EXIT_DONE;
3219
3220 /* if it's not a singlestep exception, it's not ours */
3221 if (kvm_get_dr(&svm->vcpu, 6, &dr6))
3222 return NESTED_EXIT_DONE;
3223 if (!(dr6 & DR6_BS))
3224 return NESTED_EXIT_DONE;
3225
3226 /* if the guest is singlestepping, it should get the vmexit */
3227 if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
3228 disable_nmi_singlestep(svm);
3229 return NESTED_EXIT_DONE;
3230 }
3231
3232 /* it's ours, the nested hypervisor must not see this one */
3233 return NESTED_EXIT_HOST;
3234}
3235
3236static int nested_svm_exit_special(struct vcpu_svm *svm)
3237{
3238 u32 exit_code = svm->vmcb->control.exit_code;
3239
3240 switch (exit_code) {
3241 case SVM_EXIT_INTR:
3242 case SVM_EXIT_NMI:
3243 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
3244 return NESTED_EXIT_HOST;
3245 case SVM_EXIT_NPF:
3246 /* For now we are always handling NPFs when using them */
3247 if (npt_enabled)
3248 return NESTED_EXIT_HOST;
3249 break;
3250 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Olivier Deprez0e641232021-09-23 10:07:05 +02003251 /* Trap async PF even if not shadowing */
3252 if (!npt_enabled || svm->vcpu.arch.apf.host_apf_reason)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003253 return NESTED_EXIT_HOST;
3254 break;
3255 default:
3256 break;
3257 }
3258
3259 return NESTED_EXIT_CONTINUE;
3260}
3261
3262/*
3263 * If this function returns true, this #vmexit was already handled
3264 */
3265static int nested_svm_intercept(struct vcpu_svm *svm)
3266{
3267 u32 exit_code = svm->vmcb->control.exit_code;
3268 int vmexit = NESTED_EXIT_HOST;
3269
3270 switch (exit_code) {
3271 case SVM_EXIT_MSR:
3272 vmexit = nested_svm_exit_handled_msr(svm);
3273 break;
3274 case SVM_EXIT_IOIO:
3275 vmexit = nested_svm_intercept_ioio(svm);
3276 break;
3277 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
3278 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
3279 if (svm->nested.intercept_cr & bit)
3280 vmexit = NESTED_EXIT_DONE;
3281 break;
3282 }
3283 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
3284 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
3285 if (svm->nested.intercept_dr & bit)
3286 vmexit = NESTED_EXIT_DONE;
3287 break;
3288 }
3289 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
3290 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
3291 if (svm->nested.intercept_exceptions & excp_bits) {
3292 if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
3293 vmexit = nested_svm_intercept_db(svm);
3294 else
3295 vmexit = NESTED_EXIT_DONE;
3296 }
3297 /* async page fault always cause vmexit */
3298 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
3299 svm->vcpu.arch.exception.nested_apf != 0)
3300 vmexit = NESTED_EXIT_DONE;
3301 break;
3302 }
3303 case SVM_EXIT_ERR: {
3304 vmexit = NESTED_EXIT_DONE;
3305 break;
3306 }
3307 default: {
3308 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
3309 if (svm->nested.intercept & exit_bits)
3310 vmexit = NESTED_EXIT_DONE;
3311 }
3312 }
3313
3314 return vmexit;
3315}
3316
3317static int nested_svm_exit_handled(struct vcpu_svm *svm)
3318{
3319 int vmexit;
3320
3321 vmexit = nested_svm_intercept(svm);
3322
3323 if (vmexit == NESTED_EXIT_DONE)
3324 nested_svm_vmexit(svm);
3325
3326 return vmexit;
3327}
3328
3329static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
3330{
3331 struct vmcb_control_area *dst = &dst_vmcb->control;
3332 struct vmcb_control_area *from = &from_vmcb->control;
3333
3334 dst->intercept_cr = from->intercept_cr;
3335 dst->intercept_dr = from->intercept_dr;
3336 dst->intercept_exceptions = from->intercept_exceptions;
3337 dst->intercept = from->intercept;
3338 dst->iopm_base_pa = from->iopm_base_pa;
3339 dst->msrpm_base_pa = from->msrpm_base_pa;
3340 dst->tsc_offset = from->tsc_offset;
Olivier Deprez0e641232021-09-23 10:07:05 +02003341 /* asid not copied, it is handled manually for svm->vmcb. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003342 dst->tlb_ctl = from->tlb_ctl;
3343 dst->int_ctl = from->int_ctl;
3344 dst->int_vector = from->int_vector;
3345 dst->int_state = from->int_state;
3346 dst->exit_code = from->exit_code;
3347 dst->exit_code_hi = from->exit_code_hi;
3348 dst->exit_info_1 = from->exit_info_1;
3349 dst->exit_info_2 = from->exit_info_2;
3350 dst->exit_int_info = from->exit_int_info;
3351 dst->exit_int_info_err = from->exit_int_info_err;
3352 dst->nested_ctl = from->nested_ctl;
3353 dst->event_inj = from->event_inj;
3354 dst->event_inj_err = from->event_inj_err;
3355 dst->nested_cr3 = from->nested_cr3;
3356 dst->virt_ext = from->virt_ext;
David Brazdil0f672f62019-12-10 10:32:29 +00003357 dst->pause_filter_count = from->pause_filter_count;
3358 dst->pause_filter_thresh = from->pause_filter_thresh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003359}
3360
3361static int nested_svm_vmexit(struct vcpu_svm *svm)
3362{
David Brazdil0f672f62019-12-10 10:32:29 +00003363 int rc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003364 struct vmcb *nested_vmcb;
3365 struct vmcb *hsave = svm->nested.hsave;
3366 struct vmcb *vmcb = svm->vmcb;
David Brazdil0f672f62019-12-10 10:32:29 +00003367 struct kvm_host_map map;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003368
3369 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
3370 vmcb->control.exit_info_1,
3371 vmcb->control.exit_info_2,
3372 vmcb->control.exit_int_info,
3373 vmcb->control.exit_int_info_err,
3374 KVM_ISA_SVM);
3375
David Brazdil0f672f62019-12-10 10:32:29 +00003376 rc = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->nested.vmcb), &map);
3377 if (rc) {
3378 if (rc == -EINVAL)
3379 kvm_inject_gp(&svm->vcpu, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003380 return 1;
David Brazdil0f672f62019-12-10 10:32:29 +00003381 }
3382
3383 nested_vmcb = map.hva;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003384
3385 /* Exit Guest-Mode */
3386 leave_guest_mode(&svm->vcpu);
3387 svm->nested.vmcb = 0;
3388
3389 /* Give the current vmcb to the guest */
3390 disable_gif(svm);
3391
3392 nested_vmcb->save.es = vmcb->save.es;
3393 nested_vmcb->save.cs = vmcb->save.cs;
3394 nested_vmcb->save.ss = vmcb->save.ss;
3395 nested_vmcb->save.ds = vmcb->save.ds;
3396 nested_vmcb->save.gdtr = vmcb->save.gdtr;
3397 nested_vmcb->save.idtr = vmcb->save.idtr;
3398 nested_vmcb->save.efer = svm->vcpu.arch.efer;
3399 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
3400 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
3401 nested_vmcb->save.cr2 = vmcb->save.cr2;
3402 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
3403 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
3404 nested_vmcb->save.rip = vmcb->save.rip;
3405 nested_vmcb->save.rsp = vmcb->save.rsp;
3406 nested_vmcb->save.rax = vmcb->save.rax;
3407 nested_vmcb->save.dr7 = vmcb->save.dr7;
3408 nested_vmcb->save.dr6 = vmcb->save.dr6;
3409 nested_vmcb->save.cpl = vmcb->save.cpl;
3410
3411 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
3412 nested_vmcb->control.int_vector = vmcb->control.int_vector;
3413 nested_vmcb->control.int_state = vmcb->control.int_state;
3414 nested_vmcb->control.exit_code = vmcb->control.exit_code;
3415 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
3416 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
3417 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
3418 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
3419 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
3420
3421 if (svm->nrips_enabled)
3422 nested_vmcb->control.next_rip = vmcb->control.next_rip;
3423
3424 /*
3425 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
3426 * to make sure that we do not lose injected events. So check event_inj
3427 * here and copy it to exit_int_info if it is valid.
3428 * Exit_int_info and event_inj can't be both valid because the case
3429 * below only happens on a VMRUN instruction intercept which has
3430 * no valid exit_int_info set.
3431 */
3432 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
3433 struct vmcb_control_area *nc = &nested_vmcb->control;
3434
3435 nc->exit_int_info = vmcb->control.event_inj;
3436 nc->exit_int_info_err = vmcb->control.event_inj_err;
3437 }
3438
3439 nested_vmcb->control.tlb_ctl = 0;
3440 nested_vmcb->control.event_inj = 0;
3441 nested_vmcb->control.event_inj_err = 0;
3442
David Brazdil0f672f62019-12-10 10:32:29 +00003443 nested_vmcb->control.pause_filter_count =
3444 svm->vmcb->control.pause_filter_count;
3445 nested_vmcb->control.pause_filter_thresh =
3446 svm->vmcb->control.pause_filter_thresh;
3447
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003448 /* We always set V_INTR_MASKING and remember the old value in hflags */
3449 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
3450 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
3451
3452 /* Restore the original control entries */
3453 copy_vmcb_control_area(vmcb, hsave);
3454
3455 svm->vcpu.arch.tsc_offset = svm->vmcb->control.tsc_offset;
3456 kvm_clear_exception_queue(&svm->vcpu);
3457 kvm_clear_interrupt_queue(&svm->vcpu);
3458
3459 svm->nested.nested_cr3 = 0;
3460
3461 /* Restore selected save entries */
3462 svm->vmcb->save.es = hsave->save.es;
3463 svm->vmcb->save.cs = hsave->save.cs;
3464 svm->vmcb->save.ss = hsave->save.ss;
3465 svm->vmcb->save.ds = hsave->save.ds;
3466 svm->vmcb->save.gdtr = hsave->save.gdtr;
3467 svm->vmcb->save.idtr = hsave->save.idtr;
3468 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
3469 svm_set_efer(&svm->vcpu, hsave->save.efer);
3470 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
3471 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
3472 if (npt_enabled) {
3473 svm->vmcb->save.cr3 = hsave->save.cr3;
3474 svm->vcpu.arch.cr3 = hsave->save.cr3;
3475 } else {
3476 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
3477 }
David Brazdil0f672f62019-12-10 10:32:29 +00003478 kvm_rax_write(&svm->vcpu, hsave->save.rax);
3479 kvm_rsp_write(&svm->vcpu, hsave->save.rsp);
3480 kvm_rip_write(&svm->vcpu, hsave->save.rip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003481 svm->vmcb->save.dr7 = 0;
3482 svm->vmcb->save.cpl = 0;
3483 svm->vmcb->control.exit_int_info = 0;
3484
3485 mark_all_dirty(svm->vmcb);
3486
David Brazdil0f672f62019-12-10 10:32:29 +00003487 kvm_vcpu_unmap(&svm->vcpu, &map, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003488
3489 nested_svm_uninit_mmu_context(&svm->vcpu);
3490 kvm_mmu_reset_context(&svm->vcpu);
3491 kvm_mmu_load(&svm->vcpu);
3492
David Brazdil0f672f62019-12-10 10:32:29 +00003493 /*
3494 * Drop what we picked up for L2 via svm_complete_interrupts() so it
3495 * doesn't end up in L1.
3496 */
3497 svm->vcpu.arch.nmi_injected = false;
3498 kvm_clear_exception_queue(&svm->vcpu);
3499 kvm_clear_interrupt_queue(&svm->vcpu);
3500
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003501 return 0;
3502}
3503
3504static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
3505{
3506 /*
3507 * This function merges the msr permission bitmaps of kvm and the
3508 * nested vmcb. It is optimized in that it only merges the parts where
3509 * the kvm msr permission bitmap may contain zero bits
3510 */
3511 int i;
3512
3513 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
3514 return true;
3515
3516 for (i = 0; i < MSRPM_OFFSETS; i++) {
3517 u32 value, p;
3518 u64 offset;
3519
3520 if (msrpm_offsets[i] == 0xffffffff)
3521 break;
3522
3523 p = msrpm_offsets[i];
3524 offset = svm->nested.vmcb_msrpm + (p * 4);
3525
3526 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
3527 return false;
3528
3529 svm->nested.msrpm[p] = svm->msrpm[p] | value;
3530 }
3531
3532 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
3533
3534 return true;
3535}
3536
3537static bool nested_vmcb_checks(struct vmcb *vmcb)
3538{
3539 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
3540 return false;
3541
3542 if (vmcb->control.asid == 0)
3543 return false;
3544
3545 if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
3546 !npt_enabled)
3547 return false;
3548
3549 return true;
3550}
3551
3552static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
David Brazdil0f672f62019-12-10 10:32:29 +00003553 struct vmcb *nested_vmcb, struct kvm_host_map *map)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003554{
3555 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
3556 svm->vcpu.arch.hflags |= HF_HIF_MASK;
3557 else
3558 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
3559
3560 if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003561 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
3562 nested_svm_init_mmu_context(&svm->vcpu);
3563 }
3564
3565 /* Load the nested guest state */
3566 svm->vmcb->save.es = nested_vmcb->save.es;
3567 svm->vmcb->save.cs = nested_vmcb->save.cs;
3568 svm->vmcb->save.ss = nested_vmcb->save.ss;
3569 svm->vmcb->save.ds = nested_vmcb->save.ds;
3570 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
3571 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
3572 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
3573 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
3574 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
3575 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
3576 if (npt_enabled) {
3577 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
3578 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
3579 } else
3580 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
3581
3582 /* Guest paging mode is active - reset mmu */
3583 kvm_mmu_reset_context(&svm->vcpu);
3584
3585 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
David Brazdil0f672f62019-12-10 10:32:29 +00003586 kvm_rax_write(&svm->vcpu, nested_vmcb->save.rax);
3587 kvm_rsp_write(&svm->vcpu, nested_vmcb->save.rsp);
3588 kvm_rip_write(&svm->vcpu, nested_vmcb->save.rip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003589
3590 /* In case we don't even reach vcpu_run, the fields are not updated */
3591 svm->vmcb->save.rax = nested_vmcb->save.rax;
3592 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
3593 svm->vmcb->save.rip = nested_vmcb->save.rip;
3594 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
3595 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
3596 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
3597
3598 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
3599 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
3600
3601 /* cache intercepts */
3602 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
3603 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
3604 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
3605 svm->nested.intercept = nested_vmcb->control.intercept;
3606
3607 svm_flush_tlb(&svm->vcpu, true);
Olivier Deprez0e641232021-09-23 10:07:05 +02003608
3609 svm->vmcb->control.int_ctl &=
3610 V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK;
3611
3612 svm->vmcb->control.int_ctl |= nested_vmcb->control.int_ctl &
3613 (V_TPR_MASK | V_IRQ_INJECTION_BITS_MASK);
3614
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003615 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
3616 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
3617 else
3618 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
3619
3620 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
3621 /* We only want the cr8 intercept bits of the guest */
3622 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
3623 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
3624 }
3625
3626 /* We don't want to see VMMCALLs from a nested guest */
3627 clr_intercept(svm, INTERCEPT_VMMCALL);
3628
3629 svm->vcpu.arch.tsc_offset += nested_vmcb->control.tsc_offset;
3630 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
3631
3632 svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
3633 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
3634 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
3635 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
3636 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
3637
David Brazdil0f672f62019-12-10 10:32:29 +00003638 svm->vmcb->control.pause_filter_count =
3639 nested_vmcb->control.pause_filter_count;
3640 svm->vmcb->control.pause_filter_thresh =
3641 nested_vmcb->control.pause_filter_thresh;
3642
3643 kvm_vcpu_unmap(&svm->vcpu, map, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003644
3645 /* Enter Guest-Mode */
3646 enter_guest_mode(&svm->vcpu);
3647
3648 /*
3649 * Merge guest and host intercepts - must be called with vcpu in
3650 * guest-mode to take affect here
3651 */
3652 recalc_intercepts(svm);
3653
3654 svm->nested.vmcb = vmcb_gpa;
3655
3656 enable_gif(svm);
3657
3658 mark_all_dirty(svm->vmcb);
3659}
3660
David Brazdil0f672f62019-12-10 10:32:29 +00003661static int nested_svm_vmrun(struct vcpu_svm *svm)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003662{
David Brazdil0f672f62019-12-10 10:32:29 +00003663 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003664 struct vmcb *nested_vmcb;
3665 struct vmcb *hsave = svm->nested.hsave;
3666 struct vmcb *vmcb = svm->vmcb;
David Brazdil0f672f62019-12-10 10:32:29 +00003667 struct kvm_host_map map;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003668 u64 vmcb_gpa;
3669
3670 vmcb_gpa = svm->vmcb->save.rax;
3671
David Brazdil0f672f62019-12-10 10:32:29 +00003672 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb_gpa), &map);
3673 if (ret == -EINVAL) {
3674 kvm_inject_gp(&svm->vcpu, 0);
3675 return 1;
3676 } else if (ret) {
3677 return kvm_skip_emulated_instruction(&svm->vcpu);
3678 }
3679
3680 ret = kvm_skip_emulated_instruction(&svm->vcpu);
3681
3682 nested_vmcb = map.hva;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003683
3684 if (!nested_vmcb_checks(nested_vmcb)) {
3685 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
3686 nested_vmcb->control.exit_code_hi = 0;
3687 nested_vmcb->control.exit_info_1 = 0;
3688 nested_vmcb->control.exit_info_2 = 0;
3689
David Brazdil0f672f62019-12-10 10:32:29 +00003690 kvm_vcpu_unmap(&svm->vcpu, &map, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003691
David Brazdil0f672f62019-12-10 10:32:29 +00003692 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003693 }
3694
3695 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
3696 nested_vmcb->save.rip,
3697 nested_vmcb->control.int_ctl,
3698 nested_vmcb->control.event_inj,
3699 nested_vmcb->control.nested_ctl);
3700
3701 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
3702 nested_vmcb->control.intercept_cr >> 16,
3703 nested_vmcb->control.intercept_exceptions,
3704 nested_vmcb->control.intercept);
3705
3706 /* Clear internal status */
3707 kvm_clear_exception_queue(&svm->vcpu);
3708 kvm_clear_interrupt_queue(&svm->vcpu);
3709
3710 /*
3711 * Save the old vmcb, so we don't need to pick what we save, but can
3712 * restore everything when a VMEXIT occurs
3713 */
3714 hsave->save.es = vmcb->save.es;
3715 hsave->save.cs = vmcb->save.cs;
3716 hsave->save.ss = vmcb->save.ss;
3717 hsave->save.ds = vmcb->save.ds;
3718 hsave->save.gdtr = vmcb->save.gdtr;
3719 hsave->save.idtr = vmcb->save.idtr;
3720 hsave->save.efer = svm->vcpu.arch.efer;
3721 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
3722 hsave->save.cr4 = svm->vcpu.arch.cr4;
3723 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
3724 hsave->save.rip = kvm_rip_read(&svm->vcpu);
3725 hsave->save.rsp = vmcb->save.rsp;
3726 hsave->save.rax = vmcb->save.rax;
3727 if (npt_enabled)
3728 hsave->save.cr3 = vmcb->save.cr3;
3729 else
3730 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
3731
3732 copy_vmcb_control_area(hsave, vmcb);
3733
David Brazdil0f672f62019-12-10 10:32:29 +00003734 enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, &map);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003735
David Brazdil0f672f62019-12-10 10:32:29 +00003736 if (!nested_svm_vmrun_msrpm(svm)) {
3737 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3738 svm->vmcb->control.exit_code_hi = 0;
3739 svm->vmcb->control.exit_info_1 = 0;
3740 svm->vmcb->control.exit_info_2 = 0;
3741
3742 nested_svm_vmexit(svm);
3743 }
3744
3745 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003746}
3747
3748static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
3749{
3750 to_vmcb->save.fs = from_vmcb->save.fs;
3751 to_vmcb->save.gs = from_vmcb->save.gs;
3752 to_vmcb->save.tr = from_vmcb->save.tr;
3753 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
3754 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
3755 to_vmcb->save.star = from_vmcb->save.star;
3756 to_vmcb->save.lstar = from_vmcb->save.lstar;
3757 to_vmcb->save.cstar = from_vmcb->save.cstar;
3758 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
3759 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
3760 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
3761 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
3762}
3763
3764static int vmload_interception(struct vcpu_svm *svm)
3765{
3766 struct vmcb *nested_vmcb;
David Brazdil0f672f62019-12-10 10:32:29 +00003767 struct kvm_host_map map;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003768 int ret;
3769
3770 if (nested_svm_check_permissions(svm))
3771 return 1;
3772
David Brazdil0f672f62019-12-10 10:32:29 +00003773 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
3774 if (ret) {
3775 if (ret == -EINVAL)
3776 kvm_inject_gp(&svm->vcpu, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003777 return 1;
David Brazdil0f672f62019-12-10 10:32:29 +00003778 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003779
David Brazdil0f672f62019-12-10 10:32:29 +00003780 nested_vmcb = map.hva;
3781
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003782 ret = kvm_skip_emulated_instruction(&svm->vcpu);
3783
3784 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
David Brazdil0f672f62019-12-10 10:32:29 +00003785 kvm_vcpu_unmap(&svm->vcpu, &map, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003786
3787 return ret;
3788}
3789
3790static int vmsave_interception(struct vcpu_svm *svm)
3791{
3792 struct vmcb *nested_vmcb;
David Brazdil0f672f62019-12-10 10:32:29 +00003793 struct kvm_host_map map;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003794 int ret;
3795
3796 if (nested_svm_check_permissions(svm))
3797 return 1;
3798
David Brazdil0f672f62019-12-10 10:32:29 +00003799 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
3800 if (ret) {
3801 if (ret == -EINVAL)
3802 kvm_inject_gp(&svm->vcpu, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003803 return 1;
David Brazdil0f672f62019-12-10 10:32:29 +00003804 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003805
David Brazdil0f672f62019-12-10 10:32:29 +00003806 nested_vmcb = map.hva;
3807
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003808 ret = kvm_skip_emulated_instruction(&svm->vcpu);
3809
3810 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
David Brazdil0f672f62019-12-10 10:32:29 +00003811 kvm_vcpu_unmap(&svm->vcpu, &map, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003812
3813 return ret;
3814}
3815
3816static int vmrun_interception(struct vcpu_svm *svm)
3817{
3818 if (nested_svm_check_permissions(svm))
3819 return 1;
3820
David Brazdil0f672f62019-12-10 10:32:29 +00003821 return nested_svm_vmrun(svm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003822}
3823
3824static int stgi_interception(struct vcpu_svm *svm)
3825{
3826 int ret;
3827
3828 if (nested_svm_check_permissions(svm))
3829 return 1;
3830
3831 /*
3832 * If VGIF is enabled, the STGI intercept is only added to
3833 * detect the opening of the SMI/NMI window; remove it now.
3834 */
3835 if (vgif_enabled(svm))
3836 clr_intercept(svm, INTERCEPT_STGI);
3837
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003838 ret = kvm_skip_emulated_instruction(&svm->vcpu);
3839 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3840
3841 enable_gif(svm);
3842
3843 return ret;
3844}
3845
3846static int clgi_interception(struct vcpu_svm *svm)
3847{
3848 int ret;
3849
3850 if (nested_svm_check_permissions(svm))
3851 return 1;
3852
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003853 ret = kvm_skip_emulated_instruction(&svm->vcpu);
3854
3855 disable_gif(svm);
3856
3857 /* After a CLGI no interrupts should come */
3858 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
3859 svm_clear_vintr(svm);
3860 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
3861 mark_dirty(svm->vmcb, VMCB_INTR);
3862 }
3863
3864 return ret;
3865}
3866
3867static int invlpga_interception(struct vcpu_svm *svm)
3868{
3869 struct kvm_vcpu *vcpu = &svm->vcpu;
3870
David Brazdil0f672f62019-12-10 10:32:29 +00003871 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_rcx_read(&svm->vcpu),
3872 kvm_rax_read(&svm->vcpu));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003873
3874 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Brazdil0f672f62019-12-10 10:32:29 +00003875 kvm_mmu_invlpg(vcpu, kvm_rax_read(&svm->vcpu));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003876
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003877 return kvm_skip_emulated_instruction(&svm->vcpu);
3878}
3879
3880static int skinit_interception(struct vcpu_svm *svm)
3881{
David Brazdil0f672f62019-12-10 10:32:29 +00003882 trace_kvm_skinit(svm->vmcb->save.rip, kvm_rax_read(&svm->vcpu));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003883
3884 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3885 return 1;
3886}
3887
3888static int wbinvd_interception(struct vcpu_svm *svm)
3889{
3890 return kvm_emulate_wbinvd(&svm->vcpu);
3891}
3892
3893static int xsetbv_interception(struct vcpu_svm *svm)
3894{
3895 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00003896 u32 index = kvm_rcx_read(&svm->vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003897
3898 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003899 return kvm_skip_emulated_instruction(&svm->vcpu);
3900 }
3901
3902 return 1;
3903}
3904
David Brazdil0f672f62019-12-10 10:32:29 +00003905static int rdpru_interception(struct vcpu_svm *svm)
3906{
3907 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3908 return 1;
3909}
3910
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003911static int task_switch_interception(struct vcpu_svm *svm)
3912{
3913 u16 tss_selector;
3914 int reason;
3915 int int_type = svm->vmcb->control.exit_int_info &
3916 SVM_EXITINTINFO_TYPE_MASK;
3917 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
3918 uint32_t type =
3919 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
3920 uint32_t idt_v =
3921 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
3922 bool has_error_code = false;
3923 u32 error_code = 0;
3924
3925 tss_selector = (u16)svm->vmcb->control.exit_info_1;
3926
3927 if (svm->vmcb->control.exit_info_2 &
3928 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
3929 reason = TASK_SWITCH_IRET;
3930 else if (svm->vmcb->control.exit_info_2 &
3931 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
3932 reason = TASK_SWITCH_JMP;
3933 else if (idt_v)
3934 reason = TASK_SWITCH_GATE;
3935 else
3936 reason = TASK_SWITCH_CALL;
3937
3938 if (reason == TASK_SWITCH_GATE) {
3939 switch (type) {
3940 case SVM_EXITINTINFO_TYPE_NMI:
3941 svm->vcpu.arch.nmi_injected = false;
3942 break;
3943 case SVM_EXITINTINFO_TYPE_EXEPT:
3944 if (svm->vmcb->control.exit_info_2 &
3945 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
3946 has_error_code = true;
3947 error_code =
3948 (u32)svm->vmcb->control.exit_info_2;
3949 }
3950 kvm_clear_exception_queue(&svm->vcpu);
3951 break;
3952 case SVM_EXITINTINFO_TYPE_INTR:
3953 kvm_clear_interrupt_queue(&svm->vcpu);
3954 break;
3955 default:
3956 break;
3957 }
3958 }
3959
3960 if (reason != TASK_SWITCH_GATE ||
3961 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
3962 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
David Brazdil0f672f62019-12-10 10:32:29 +00003963 (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
3964 if (!skip_emulated_instruction(&svm->vcpu))
3965 return 0;
3966 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003967
3968 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
3969 int_vec = -1;
3970
David Brazdil0f672f62019-12-10 10:32:29 +00003971 return kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
3972 has_error_code, error_code);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003973}
3974
3975static int cpuid_interception(struct vcpu_svm *svm)
3976{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003977 return kvm_emulate_cpuid(&svm->vcpu);
3978}
3979
3980static int iret_interception(struct vcpu_svm *svm)
3981{
3982 ++svm->vcpu.stat.nmi_window_exits;
3983 clr_intercept(svm, INTERCEPT_IRET);
3984 svm->vcpu.arch.hflags |= HF_IRET_MASK;
3985 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
3986 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3987 return 1;
3988}
3989
Olivier Deprez0e641232021-09-23 10:07:05 +02003990static int invd_interception(struct vcpu_svm *svm)
3991{
3992 /* Treat an INVD instruction as a NOP and just skip it. */
3993 return kvm_skip_emulated_instruction(&svm->vcpu);
3994}
3995
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003996static int invlpg_interception(struct vcpu_svm *svm)
3997{
3998 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
David Brazdil0f672f62019-12-10 10:32:29 +00003999 return kvm_emulate_instruction(&svm->vcpu, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004000
4001 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
4002 return kvm_skip_emulated_instruction(&svm->vcpu);
4003}
4004
4005static int emulate_on_interception(struct vcpu_svm *svm)
4006{
David Brazdil0f672f62019-12-10 10:32:29 +00004007 return kvm_emulate_instruction(&svm->vcpu, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004008}
4009
4010static int rsm_interception(struct vcpu_svm *svm)
4011{
David Brazdil0f672f62019-12-10 10:32:29 +00004012 return kvm_emulate_instruction_from_buffer(&svm->vcpu, rsm_ins_bytes, 2);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004013}
4014
4015static int rdpmc_interception(struct vcpu_svm *svm)
4016{
4017 int err;
4018
David Brazdil0f672f62019-12-10 10:32:29 +00004019 if (!nrips)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004020 return emulate_on_interception(svm);
4021
4022 err = kvm_rdpmc(&svm->vcpu);
4023 return kvm_complete_insn_gp(&svm->vcpu, err);
4024}
4025
4026static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
4027 unsigned long val)
4028{
4029 unsigned long cr0 = svm->vcpu.arch.cr0;
4030 bool ret = false;
4031 u64 intercept;
4032
4033 intercept = svm->nested.intercept;
4034
4035 if (!is_guest_mode(&svm->vcpu) ||
4036 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
4037 return false;
4038
4039 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
4040 val &= ~SVM_CR0_SELECTIVE_MASK;
4041
4042 if (cr0 ^ val) {
4043 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4044 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
4045 }
4046
4047 return ret;
4048}
4049
4050#define CR_VALID (1ULL << 63)
4051
4052static int cr_interception(struct vcpu_svm *svm)
4053{
4054 int reg, cr;
4055 unsigned long val;
4056 int err;
4057
4058 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
4059 return emulate_on_interception(svm);
4060
4061 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
4062 return emulate_on_interception(svm);
4063
4064 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
4065 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
4066 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
4067 else
4068 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
4069
4070 err = 0;
4071 if (cr >= 16) { /* mov to cr */
4072 cr -= 16;
Olivier Deprez0e641232021-09-23 10:07:05 +02004073 val = kvm_register_readl(&svm->vcpu, reg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004074 switch (cr) {
4075 case 0:
4076 if (!check_selective_cr0_intercepted(svm, val))
4077 err = kvm_set_cr0(&svm->vcpu, val);
4078 else
4079 return 1;
4080
4081 break;
4082 case 3:
4083 err = kvm_set_cr3(&svm->vcpu, val);
4084 break;
4085 case 4:
4086 err = kvm_set_cr4(&svm->vcpu, val);
4087 break;
4088 case 8:
4089 err = kvm_set_cr8(&svm->vcpu, val);
4090 break;
4091 default:
4092 WARN(1, "unhandled write to CR%d", cr);
4093 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
4094 return 1;
4095 }
4096 } else { /* mov from cr */
4097 switch (cr) {
4098 case 0:
4099 val = kvm_read_cr0(&svm->vcpu);
4100 break;
4101 case 2:
4102 val = svm->vcpu.arch.cr2;
4103 break;
4104 case 3:
4105 val = kvm_read_cr3(&svm->vcpu);
4106 break;
4107 case 4:
4108 val = kvm_read_cr4(&svm->vcpu);
4109 break;
4110 case 8:
4111 val = kvm_get_cr8(&svm->vcpu);
4112 break;
4113 default:
4114 WARN(1, "unhandled read from CR%d", cr);
4115 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
4116 return 1;
4117 }
Olivier Deprez0e641232021-09-23 10:07:05 +02004118 kvm_register_writel(&svm->vcpu, reg, val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004119 }
4120 return kvm_complete_insn_gp(&svm->vcpu, err);
4121}
4122
4123static int dr_interception(struct vcpu_svm *svm)
4124{
4125 int reg, dr;
4126 unsigned long val;
4127
4128 if (svm->vcpu.guest_debug == 0) {
4129 /*
4130 * No more DR vmexits; force a reload of the debug registers
4131 * and reenter on this instruction. The next vmexit will
4132 * retrieve the full state of the debug registers.
4133 */
4134 clr_dr_intercepts(svm);
4135 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
4136 return 1;
4137 }
4138
4139 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
4140 return emulate_on_interception(svm);
4141
4142 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
4143 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
4144
4145 if (dr >= 16) { /* mov to DRn */
4146 if (!kvm_require_dr(&svm->vcpu, dr - 16))
4147 return 1;
Olivier Deprez0e641232021-09-23 10:07:05 +02004148 val = kvm_register_readl(&svm->vcpu, reg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004149 kvm_set_dr(&svm->vcpu, dr - 16, val);
4150 } else {
4151 if (!kvm_require_dr(&svm->vcpu, dr))
4152 return 1;
4153 kvm_get_dr(&svm->vcpu, dr, &val);
Olivier Deprez0e641232021-09-23 10:07:05 +02004154 kvm_register_writel(&svm->vcpu, reg, val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004155 }
4156
4157 return kvm_skip_emulated_instruction(&svm->vcpu);
4158}
4159
4160static int cr8_write_interception(struct vcpu_svm *svm)
4161{
4162 struct kvm_run *kvm_run = svm->vcpu.run;
4163 int r;
4164
4165 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
4166 /* instruction emulation calls kvm_set_cr8() */
4167 r = cr_interception(svm);
4168 if (lapic_in_kernel(&svm->vcpu))
4169 return r;
4170 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
4171 return r;
4172 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
4173 return 0;
4174}
4175
4176static int svm_get_msr_feature(struct kvm_msr_entry *msr)
4177{
4178 msr->data = 0;
4179
4180 switch (msr->index) {
4181 case MSR_F10H_DECFG:
4182 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
4183 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
4184 break;
4185 default:
4186 return 1;
4187 }
4188
4189 return 0;
4190}
4191
4192static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
4193{
4194 struct vcpu_svm *svm = to_svm(vcpu);
4195
4196 switch (msr_info->index) {
4197 case MSR_STAR:
4198 msr_info->data = svm->vmcb->save.star;
4199 break;
4200#ifdef CONFIG_X86_64
4201 case MSR_LSTAR:
4202 msr_info->data = svm->vmcb->save.lstar;
4203 break;
4204 case MSR_CSTAR:
4205 msr_info->data = svm->vmcb->save.cstar;
4206 break;
4207 case MSR_KERNEL_GS_BASE:
4208 msr_info->data = svm->vmcb->save.kernel_gs_base;
4209 break;
4210 case MSR_SYSCALL_MASK:
4211 msr_info->data = svm->vmcb->save.sfmask;
4212 break;
4213#endif
4214 case MSR_IA32_SYSENTER_CS:
4215 msr_info->data = svm->vmcb->save.sysenter_cs;
4216 break;
4217 case MSR_IA32_SYSENTER_EIP:
4218 msr_info->data = svm->sysenter_eip;
4219 break;
4220 case MSR_IA32_SYSENTER_ESP:
4221 msr_info->data = svm->sysenter_esp;
4222 break;
4223 case MSR_TSC_AUX:
4224 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4225 return 1;
4226 msr_info->data = svm->tsc_aux;
4227 break;
4228 /*
4229 * Nobody will change the following 5 values in the VMCB so we can
4230 * safely return them on rdmsr. They will always be 0 until LBRV is
4231 * implemented.
4232 */
4233 case MSR_IA32_DEBUGCTLMSR:
4234 msr_info->data = svm->vmcb->save.dbgctl;
4235 break;
4236 case MSR_IA32_LASTBRANCHFROMIP:
4237 msr_info->data = svm->vmcb->save.br_from;
4238 break;
4239 case MSR_IA32_LASTBRANCHTOIP:
4240 msr_info->data = svm->vmcb->save.br_to;
4241 break;
4242 case MSR_IA32_LASTINTFROMIP:
4243 msr_info->data = svm->vmcb->save.last_excp_from;
4244 break;
4245 case MSR_IA32_LASTINTTOIP:
4246 msr_info->data = svm->vmcb->save.last_excp_to;
4247 break;
4248 case MSR_VM_HSAVE_PA:
4249 msr_info->data = svm->nested.hsave_msr;
4250 break;
4251 case MSR_VM_CR:
4252 msr_info->data = svm->nested.vm_cr_msr;
4253 break;
4254 case MSR_IA32_SPEC_CTRL:
4255 if (!msr_info->host_initiated &&
Olivier Deprez0e641232021-09-23 10:07:05 +02004256 !guest_has_spec_ctrl_msr(vcpu))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004257 return 1;
4258
4259 msr_info->data = svm->spec_ctrl;
4260 break;
4261 case MSR_AMD64_VIRT_SPEC_CTRL:
4262 if (!msr_info->host_initiated &&
4263 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
4264 return 1;
4265
4266 msr_info->data = svm->virt_spec_ctrl;
4267 break;
4268 case MSR_F15H_IC_CFG: {
4269
4270 int family, model;
4271
4272 family = guest_cpuid_family(vcpu);
4273 model = guest_cpuid_model(vcpu);
4274
4275 if (family < 0 || model < 0)
4276 return kvm_get_msr_common(vcpu, msr_info);
4277
4278 msr_info->data = 0;
4279
4280 if (family == 0x15 &&
4281 (model >= 0x2 && model < 0x20))
4282 msr_info->data = 0x1E;
4283 }
4284 break;
4285 case MSR_F10H_DECFG:
4286 msr_info->data = svm->msr_decfg;
4287 break;
4288 default:
4289 return kvm_get_msr_common(vcpu, msr_info);
4290 }
4291 return 0;
4292}
4293
4294static int rdmsr_interception(struct vcpu_svm *svm)
4295{
David Brazdil0f672f62019-12-10 10:32:29 +00004296 return kvm_emulate_rdmsr(&svm->vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004297}
4298
4299static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
4300{
4301 struct vcpu_svm *svm = to_svm(vcpu);
4302 int svm_dis, chg_mask;
4303
4304 if (data & ~SVM_VM_CR_VALID_MASK)
4305 return 1;
4306
4307 chg_mask = SVM_VM_CR_VALID_MASK;
4308
4309 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
4310 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
4311
4312 svm->nested.vm_cr_msr &= ~chg_mask;
4313 svm->nested.vm_cr_msr |= (data & chg_mask);
4314
4315 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
4316
4317 /* check for svm_disable while efer.svme is set */
4318 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
4319 return 1;
4320
4321 return 0;
4322}
4323
4324static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
4325{
4326 struct vcpu_svm *svm = to_svm(vcpu);
4327
4328 u32 ecx = msr->index;
4329 u64 data = msr->data;
4330 switch (ecx) {
4331 case MSR_IA32_CR_PAT:
4332 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4333 return 1;
4334 vcpu->arch.pat = data;
4335 svm->vmcb->save.g_pat = data;
4336 mark_dirty(svm->vmcb, VMCB_NPT);
4337 break;
4338 case MSR_IA32_SPEC_CTRL:
4339 if (!msr->host_initiated &&
Olivier Deprez0e641232021-09-23 10:07:05 +02004340 !guest_has_spec_ctrl_msr(vcpu))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004341 return 1;
4342
Olivier Deprez0e641232021-09-23 10:07:05 +02004343 if (kvm_spec_ctrl_test_value(data))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004344 return 1;
4345
4346 svm->spec_ctrl = data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004347 if (!data)
4348 break;
4349
4350 /*
4351 * For non-nested:
4352 * When it's written (to non-zero) for the first time, pass
4353 * it through.
4354 *
4355 * For nested:
4356 * The handling of the MSR bitmap for L2 guests is done in
4357 * nested_svm_vmrun_msrpm.
4358 * We update the L1 MSR bit as well since it will end up
4359 * touching the MSR anyway now.
4360 */
4361 set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
4362 break;
4363 case MSR_IA32_PRED_CMD:
4364 if (!msr->host_initiated &&
Olivier Deprez0e641232021-09-23 10:07:05 +02004365 !guest_has_pred_cmd_msr(vcpu))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004366 return 1;
4367
4368 if (data & ~PRED_CMD_IBPB)
4369 return 1;
Olivier Deprez0e641232021-09-23 10:07:05 +02004370 if (!boot_cpu_has(X86_FEATURE_IBPB))
4371 return 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004372 if (!data)
4373 break;
4374
4375 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004376 set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
4377 break;
4378 case MSR_AMD64_VIRT_SPEC_CTRL:
4379 if (!msr->host_initiated &&
4380 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
4381 return 1;
4382
4383 if (data & ~SPEC_CTRL_SSBD)
4384 return 1;
4385
4386 svm->virt_spec_ctrl = data;
4387 break;
4388 case MSR_STAR:
4389 svm->vmcb->save.star = data;
4390 break;
4391#ifdef CONFIG_X86_64
4392 case MSR_LSTAR:
4393 svm->vmcb->save.lstar = data;
4394 break;
4395 case MSR_CSTAR:
4396 svm->vmcb->save.cstar = data;
4397 break;
4398 case MSR_KERNEL_GS_BASE:
4399 svm->vmcb->save.kernel_gs_base = data;
4400 break;
4401 case MSR_SYSCALL_MASK:
4402 svm->vmcb->save.sfmask = data;
4403 break;
4404#endif
4405 case MSR_IA32_SYSENTER_CS:
4406 svm->vmcb->save.sysenter_cs = data;
4407 break;
4408 case MSR_IA32_SYSENTER_EIP:
4409 svm->sysenter_eip = data;
4410 svm->vmcb->save.sysenter_eip = data;
4411 break;
4412 case MSR_IA32_SYSENTER_ESP:
4413 svm->sysenter_esp = data;
4414 svm->vmcb->save.sysenter_esp = data;
4415 break;
4416 case MSR_TSC_AUX:
4417 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4418 return 1;
4419
4420 /*
4421 * This is rare, so we update the MSR here instead of using
4422 * direct_access_msrs. Doing that would require a rdmsr in
4423 * svm_vcpu_put.
4424 */
4425 svm->tsc_aux = data;
4426 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
4427 break;
4428 case MSR_IA32_DEBUGCTLMSR:
4429 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
4430 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
4431 __func__, data);
4432 break;
4433 }
4434 if (data & DEBUGCTL_RESERVED_BITS)
4435 return 1;
4436
4437 svm->vmcb->save.dbgctl = data;
4438 mark_dirty(svm->vmcb, VMCB_LBR);
4439 if (data & (1ULL<<0))
4440 svm_enable_lbrv(svm);
4441 else
4442 svm_disable_lbrv(svm);
4443 break;
4444 case MSR_VM_HSAVE_PA:
4445 svm->nested.hsave_msr = data;
4446 break;
4447 case MSR_VM_CR:
4448 return svm_set_vm_cr(vcpu, data);
4449 case MSR_VM_IGNNE:
4450 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
4451 break;
4452 case MSR_F10H_DECFG: {
4453 struct kvm_msr_entry msr_entry;
4454
4455 msr_entry.index = msr->index;
4456 if (svm_get_msr_feature(&msr_entry))
4457 return 1;
4458
4459 /* Check the supported bits */
4460 if (data & ~msr_entry.data)
4461 return 1;
4462
4463 /* Don't allow the guest to change a bit, #GP */
4464 if (!msr->host_initiated && (data ^ msr_entry.data))
4465 return 1;
4466
4467 svm->msr_decfg = data;
4468 break;
4469 }
4470 case MSR_IA32_APICBASE:
4471 if (kvm_vcpu_apicv_active(vcpu))
4472 avic_update_vapic_bar(to_svm(vcpu), data);
David Brazdil0f672f62019-12-10 10:32:29 +00004473 /* Fall through */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004474 default:
4475 return kvm_set_msr_common(vcpu, msr);
4476 }
4477 return 0;
4478}
4479
4480static int wrmsr_interception(struct vcpu_svm *svm)
4481{
David Brazdil0f672f62019-12-10 10:32:29 +00004482 return kvm_emulate_wrmsr(&svm->vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004483}
4484
4485static int msr_interception(struct vcpu_svm *svm)
4486{
4487 if (svm->vmcb->control.exit_info_1)
4488 return wrmsr_interception(svm);
4489 else
4490 return rdmsr_interception(svm);
4491}
4492
4493static int interrupt_window_interception(struct vcpu_svm *svm)
4494{
4495 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
4496 svm_clear_vintr(svm);
4497 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
4498 mark_dirty(svm->vmcb, VMCB_INTR);
4499 ++svm->vcpu.stat.irq_window_exits;
4500 return 1;
4501}
4502
4503static int pause_interception(struct vcpu_svm *svm)
4504{
4505 struct kvm_vcpu *vcpu = &svm->vcpu;
4506 bool in_kernel = (svm_get_cpl(vcpu) == 0);
4507
4508 if (pause_filter_thresh)
4509 grow_ple_window(vcpu);
4510
4511 kvm_vcpu_on_spin(vcpu, in_kernel);
4512 return 1;
4513}
4514
4515static int nop_interception(struct vcpu_svm *svm)
4516{
4517 return kvm_skip_emulated_instruction(&(svm->vcpu));
4518}
4519
4520static int monitor_interception(struct vcpu_svm *svm)
4521{
4522 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
4523 return nop_interception(svm);
4524}
4525
4526static int mwait_interception(struct vcpu_svm *svm)
4527{
4528 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
4529 return nop_interception(svm);
4530}
4531
4532enum avic_ipi_failure_cause {
4533 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
4534 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
4535 AVIC_IPI_FAILURE_INVALID_TARGET,
4536 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
4537};
4538
4539static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
4540{
4541 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
4542 u32 icrl = svm->vmcb->control.exit_info_1;
4543 u32 id = svm->vmcb->control.exit_info_2 >> 32;
4544 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
4545 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4546
4547 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
4548
4549 switch (id) {
4550 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
4551 /*
4552 * AVIC hardware handles the generation of
4553 * IPIs when the specified Message Type is Fixed
4554 * (also known as fixed delivery mode) and
4555 * the Trigger Mode is edge-triggered. The hardware
4556 * also supports self and broadcast delivery modes
4557 * specified via the Destination Shorthand(DSH)
4558 * field of the ICRL. Logical and physical APIC ID
4559 * formats are supported. All other IPI types cause
4560 * a #VMEXIT, which needs to emulated.
4561 */
4562 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
4563 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
4564 break;
4565 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
4566 int i;
4567 struct kvm_vcpu *vcpu;
4568 struct kvm *kvm = svm->vcpu.kvm;
4569 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4570
4571 /*
4572 * At this point, we expect that the AVIC HW has already
4573 * set the appropriate IRR bits on the valid target
4574 * vcpus. So, we just need to kick the appropriate vcpu.
4575 */
4576 kvm_for_each_vcpu(i, vcpu, kvm) {
4577 bool m = kvm_apic_match_dest(vcpu, apic,
4578 icrl & KVM_APIC_SHORT_MASK,
4579 GET_APIC_DEST_FIELD(icrh),
4580 icrl & KVM_APIC_DEST_MASK);
4581
4582 if (m && !avic_vcpu_is_running(vcpu))
4583 kvm_vcpu_wake_up(vcpu);
4584 }
4585 break;
4586 }
4587 case AVIC_IPI_FAILURE_INVALID_TARGET:
David Brazdil0f672f62019-12-10 10:32:29 +00004588 WARN_ONCE(1, "Invalid IPI target: index=%u, vcpu=%d, icr=%#0x:%#0x\n",
4589 index, svm->vcpu.vcpu_id, icrh, icrl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004590 break;
4591 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
4592 WARN_ONCE(1, "Invalid backing page\n");
4593 break;
4594 default:
4595 pr_err("Unknown IPI interception\n");
4596 }
4597
4598 return 1;
4599}
4600
4601static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
4602{
4603 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
4604 int index;
4605 u32 *logical_apic_id_table;
4606 int dlid = GET_APIC_LOGICAL_ID(ldr);
4607
4608 if (!dlid)
4609 return NULL;
4610
4611 if (flat) { /* flat */
4612 index = ffs(dlid) - 1;
4613 if (index > 7)
4614 return NULL;
4615 } else { /* cluster */
4616 int cluster = (dlid & 0xf0) >> 4;
4617 int apic = ffs(dlid & 0x0f) - 1;
4618
4619 if ((apic < 0) || (apic > 7) ||
4620 (cluster >= 0xf))
4621 return NULL;
4622 index = (cluster << 2) + apic;
4623 }
4624
4625 logical_apic_id_table = (u32 *) page_address(kvm_svm->avic_logical_id_table_page);
4626
4627 return &logical_apic_id_table[index];
4628}
4629
David Brazdil0f672f62019-12-10 10:32:29 +00004630static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004631{
4632 bool flat;
4633 u32 *entry, new_entry;
4634
4635 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
4636 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
4637 if (!entry)
4638 return -EINVAL;
4639
4640 new_entry = READ_ONCE(*entry);
4641 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
4642 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
David Brazdil0f672f62019-12-10 10:32:29 +00004643 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004644 WRITE_ONCE(*entry, new_entry);
4645
4646 return 0;
4647}
4648
David Brazdil0f672f62019-12-10 10:32:29 +00004649static void avic_invalidate_logical_id_entry(struct kvm_vcpu *vcpu)
4650{
4651 struct vcpu_svm *svm = to_svm(vcpu);
4652 bool flat = svm->dfr_reg == APIC_DFR_FLAT;
4653 u32 *entry = avic_get_logical_id_entry(vcpu, svm->ldr_reg, flat);
4654
4655 if (entry)
4656 clear_bit(AVIC_LOGICAL_ID_ENTRY_VALID_BIT, (unsigned long *)entry);
4657}
4658
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004659static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
4660{
David Brazdil0f672f62019-12-10 10:32:29 +00004661 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004662 struct vcpu_svm *svm = to_svm(vcpu);
4663 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
David Brazdil0f672f62019-12-10 10:32:29 +00004664 u32 id = kvm_xapic_id(vcpu->arch.apic);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004665
David Brazdil0f672f62019-12-10 10:32:29 +00004666 if (ldr == svm->ldr_reg)
4667 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004668
David Brazdil0f672f62019-12-10 10:32:29 +00004669 avic_invalidate_logical_id_entry(vcpu);
4670
4671 if (ldr)
4672 ret = avic_ldr_write(vcpu, id, ldr);
4673
4674 if (!ret)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004675 svm->ldr_reg = ldr;
David Brazdil0f672f62019-12-10 10:32:29 +00004676
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004677 return ret;
4678}
4679
4680static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
4681{
4682 u64 *old, *new;
4683 struct vcpu_svm *svm = to_svm(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00004684 u32 id = kvm_xapic_id(vcpu->arch.apic);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004685
4686 if (vcpu->vcpu_id == id)
4687 return 0;
4688
4689 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
4690 new = avic_get_physical_id_entry(vcpu, id);
4691 if (!new || !old)
4692 return 1;
4693
4694 /* We need to move physical_id_entry to new offset */
4695 *new = *old;
4696 *old = 0ULL;
4697 to_svm(vcpu)->avic_physical_id_cache = new;
4698
4699 /*
4700 * Also update the guest physical APIC ID in the logical
4701 * APIC ID table entry if already setup the LDR.
4702 */
4703 if (svm->ldr_reg)
4704 avic_handle_ldr_update(vcpu);
4705
4706 return 0;
4707}
4708
David Brazdil0f672f62019-12-10 10:32:29 +00004709static void avic_handle_dfr_update(struct kvm_vcpu *vcpu)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004710{
4711 struct vcpu_svm *svm = to_svm(vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004712 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004713
David Brazdil0f672f62019-12-10 10:32:29 +00004714 if (svm->dfr_reg == dfr)
4715 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004716
David Brazdil0f672f62019-12-10 10:32:29 +00004717 avic_invalidate_logical_id_entry(vcpu);
4718 svm->dfr_reg = dfr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004719}
4720
4721static int avic_unaccel_trap_write(struct vcpu_svm *svm)
4722{
4723 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4724 u32 offset = svm->vmcb->control.exit_info_1 &
4725 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4726
4727 switch (offset) {
4728 case APIC_ID:
4729 if (avic_handle_apic_id_update(&svm->vcpu))
4730 return 0;
4731 break;
4732 case APIC_LDR:
4733 if (avic_handle_ldr_update(&svm->vcpu))
4734 return 0;
4735 break;
4736 case APIC_DFR:
4737 avic_handle_dfr_update(&svm->vcpu);
4738 break;
4739 default:
4740 break;
4741 }
4742
4743 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
4744
4745 return 1;
4746}
4747
4748static bool is_avic_unaccelerated_access_trap(u32 offset)
4749{
4750 bool ret = false;
4751
4752 switch (offset) {
4753 case APIC_ID:
4754 case APIC_EOI:
4755 case APIC_RRR:
4756 case APIC_LDR:
4757 case APIC_DFR:
4758 case APIC_SPIV:
4759 case APIC_ESR:
4760 case APIC_ICR:
4761 case APIC_LVTT:
4762 case APIC_LVTTHMR:
4763 case APIC_LVTPC:
4764 case APIC_LVT0:
4765 case APIC_LVT1:
4766 case APIC_LVTERR:
4767 case APIC_TMICT:
4768 case APIC_TDCR:
4769 ret = true;
4770 break;
4771 default:
4772 break;
4773 }
4774 return ret;
4775}
4776
4777static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
4778{
4779 int ret = 0;
4780 u32 offset = svm->vmcb->control.exit_info_1 &
4781 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4782 u32 vector = svm->vmcb->control.exit_info_2 &
4783 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
4784 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
4785 AVIC_UNACCEL_ACCESS_WRITE_MASK;
4786 bool trap = is_avic_unaccelerated_access_trap(offset);
4787
4788 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
4789 trap, write, vector);
4790 if (trap) {
4791 /* Handling Trap */
4792 WARN_ONCE(!write, "svm: Handling trap read.\n");
4793 ret = avic_unaccel_trap_write(svm);
4794 } else {
4795 /* Handling Fault */
David Brazdil0f672f62019-12-10 10:32:29 +00004796 ret = kvm_emulate_instruction(&svm->vcpu, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004797 }
4798
4799 return ret;
4800}
4801
4802static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
4803 [SVM_EXIT_READ_CR0] = cr_interception,
4804 [SVM_EXIT_READ_CR3] = cr_interception,
4805 [SVM_EXIT_READ_CR4] = cr_interception,
4806 [SVM_EXIT_READ_CR8] = cr_interception,
4807 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
4808 [SVM_EXIT_WRITE_CR0] = cr_interception,
4809 [SVM_EXIT_WRITE_CR3] = cr_interception,
4810 [SVM_EXIT_WRITE_CR4] = cr_interception,
4811 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
4812 [SVM_EXIT_READ_DR0] = dr_interception,
4813 [SVM_EXIT_READ_DR1] = dr_interception,
4814 [SVM_EXIT_READ_DR2] = dr_interception,
4815 [SVM_EXIT_READ_DR3] = dr_interception,
4816 [SVM_EXIT_READ_DR4] = dr_interception,
4817 [SVM_EXIT_READ_DR5] = dr_interception,
4818 [SVM_EXIT_READ_DR6] = dr_interception,
4819 [SVM_EXIT_READ_DR7] = dr_interception,
4820 [SVM_EXIT_WRITE_DR0] = dr_interception,
4821 [SVM_EXIT_WRITE_DR1] = dr_interception,
4822 [SVM_EXIT_WRITE_DR2] = dr_interception,
4823 [SVM_EXIT_WRITE_DR3] = dr_interception,
4824 [SVM_EXIT_WRITE_DR4] = dr_interception,
4825 [SVM_EXIT_WRITE_DR5] = dr_interception,
4826 [SVM_EXIT_WRITE_DR6] = dr_interception,
4827 [SVM_EXIT_WRITE_DR7] = dr_interception,
4828 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
4829 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
4830 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
4831 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
4832 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
4833 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
4834 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
4835 [SVM_EXIT_INTR] = intr_interception,
4836 [SVM_EXIT_NMI] = nmi_interception,
4837 [SVM_EXIT_SMI] = nop_on_interception,
4838 [SVM_EXIT_INIT] = nop_on_interception,
4839 [SVM_EXIT_VINTR] = interrupt_window_interception,
4840 [SVM_EXIT_RDPMC] = rdpmc_interception,
4841 [SVM_EXIT_CPUID] = cpuid_interception,
4842 [SVM_EXIT_IRET] = iret_interception,
Olivier Deprez0e641232021-09-23 10:07:05 +02004843 [SVM_EXIT_INVD] = invd_interception,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004844 [SVM_EXIT_PAUSE] = pause_interception,
4845 [SVM_EXIT_HLT] = halt_interception,
4846 [SVM_EXIT_INVLPG] = invlpg_interception,
4847 [SVM_EXIT_INVLPGA] = invlpga_interception,
4848 [SVM_EXIT_IOIO] = io_interception,
4849 [SVM_EXIT_MSR] = msr_interception,
4850 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
4851 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
4852 [SVM_EXIT_VMRUN] = vmrun_interception,
4853 [SVM_EXIT_VMMCALL] = vmmcall_interception,
4854 [SVM_EXIT_VMLOAD] = vmload_interception,
4855 [SVM_EXIT_VMSAVE] = vmsave_interception,
4856 [SVM_EXIT_STGI] = stgi_interception,
4857 [SVM_EXIT_CLGI] = clgi_interception,
4858 [SVM_EXIT_SKINIT] = skinit_interception,
4859 [SVM_EXIT_WBINVD] = wbinvd_interception,
4860 [SVM_EXIT_MONITOR] = monitor_interception,
4861 [SVM_EXIT_MWAIT] = mwait_interception,
4862 [SVM_EXIT_XSETBV] = xsetbv_interception,
David Brazdil0f672f62019-12-10 10:32:29 +00004863 [SVM_EXIT_RDPRU] = rdpru_interception,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004864 [SVM_EXIT_NPF] = npf_interception,
4865 [SVM_EXIT_RSM] = rsm_interception,
4866 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
4867 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
4868};
4869
4870static void dump_vmcb(struct kvm_vcpu *vcpu)
4871{
4872 struct vcpu_svm *svm = to_svm(vcpu);
4873 struct vmcb_control_area *control = &svm->vmcb->control;
4874 struct vmcb_save_area *save = &svm->vmcb->save;
4875
David Brazdil0f672f62019-12-10 10:32:29 +00004876 if (!dump_invalid_vmcb) {
4877 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
4878 return;
4879 }
4880
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004881 pr_err("VMCB Control Area:\n");
4882 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
4883 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
4884 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
4885 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
4886 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
4887 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
4888 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
4889 pr_err("%-20s%d\n", "pause filter threshold:",
4890 control->pause_filter_thresh);
4891 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
4892 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
4893 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
4894 pr_err("%-20s%d\n", "asid:", control->asid);
4895 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
4896 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
4897 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
4898 pr_err("%-20s%08x\n", "int_state:", control->int_state);
4899 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
4900 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
4901 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
4902 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
4903 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
4904 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
4905 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
4906 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
4907 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
4908 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
4909 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
4910 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
4911 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
4912 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
4913 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
4914 pr_err("VMCB State Save Area:\n");
4915 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4916 "es:",
4917 save->es.selector, save->es.attrib,
4918 save->es.limit, save->es.base);
4919 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4920 "cs:",
4921 save->cs.selector, save->cs.attrib,
4922 save->cs.limit, save->cs.base);
4923 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4924 "ss:",
4925 save->ss.selector, save->ss.attrib,
4926 save->ss.limit, save->ss.base);
4927 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4928 "ds:",
4929 save->ds.selector, save->ds.attrib,
4930 save->ds.limit, save->ds.base);
4931 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4932 "fs:",
4933 save->fs.selector, save->fs.attrib,
4934 save->fs.limit, save->fs.base);
4935 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4936 "gs:",
4937 save->gs.selector, save->gs.attrib,
4938 save->gs.limit, save->gs.base);
4939 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4940 "gdtr:",
4941 save->gdtr.selector, save->gdtr.attrib,
4942 save->gdtr.limit, save->gdtr.base);
4943 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4944 "ldtr:",
4945 save->ldtr.selector, save->ldtr.attrib,
4946 save->ldtr.limit, save->ldtr.base);
4947 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4948 "idtr:",
4949 save->idtr.selector, save->idtr.attrib,
4950 save->idtr.limit, save->idtr.base);
4951 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4952 "tr:",
4953 save->tr.selector, save->tr.attrib,
4954 save->tr.limit, save->tr.base);
4955 pr_err("cpl: %d efer: %016llx\n",
4956 save->cpl, save->efer);
4957 pr_err("%-15s %016llx %-13s %016llx\n",
4958 "cr0:", save->cr0, "cr2:", save->cr2);
4959 pr_err("%-15s %016llx %-13s %016llx\n",
4960 "cr3:", save->cr3, "cr4:", save->cr4);
4961 pr_err("%-15s %016llx %-13s %016llx\n",
4962 "dr6:", save->dr6, "dr7:", save->dr7);
4963 pr_err("%-15s %016llx %-13s %016llx\n",
4964 "rip:", save->rip, "rflags:", save->rflags);
4965 pr_err("%-15s %016llx %-13s %016llx\n",
4966 "rsp:", save->rsp, "rax:", save->rax);
4967 pr_err("%-15s %016llx %-13s %016llx\n",
4968 "star:", save->star, "lstar:", save->lstar);
4969 pr_err("%-15s %016llx %-13s %016llx\n",
4970 "cstar:", save->cstar, "sfmask:", save->sfmask);
4971 pr_err("%-15s %016llx %-13s %016llx\n",
4972 "kernel_gs_base:", save->kernel_gs_base,
4973 "sysenter_cs:", save->sysenter_cs);
4974 pr_err("%-15s %016llx %-13s %016llx\n",
4975 "sysenter_esp:", save->sysenter_esp,
4976 "sysenter_eip:", save->sysenter_eip);
4977 pr_err("%-15s %016llx %-13s %016llx\n",
4978 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
4979 pr_err("%-15s %016llx %-13s %016llx\n",
4980 "br_from:", save->br_from, "br_to:", save->br_to);
4981 pr_err("%-15s %016llx %-13s %016llx\n",
4982 "excp_from:", save->last_excp_from,
4983 "excp_to:", save->last_excp_to);
4984}
4985
4986static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4987{
4988 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
4989
4990 *info1 = control->exit_info_1;
4991 *info2 = control->exit_info_2;
4992}
4993
4994static int handle_exit(struct kvm_vcpu *vcpu)
4995{
4996 struct vcpu_svm *svm = to_svm(vcpu);
4997 struct kvm_run *kvm_run = vcpu->run;
4998 u32 exit_code = svm->vmcb->control.exit_code;
4999
5000 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
5001
5002 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
5003 vcpu->arch.cr0 = svm->vmcb->save.cr0;
5004 if (npt_enabled)
5005 vcpu->arch.cr3 = svm->vmcb->save.cr3;
5006
5007 if (unlikely(svm->nested.exit_required)) {
5008 nested_svm_vmexit(svm);
5009 svm->nested.exit_required = false;
5010
5011 return 1;
5012 }
5013
5014 if (is_guest_mode(vcpu)) {
5015 int vmexit;
5016
5017 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
5018 svm->vmcb->control.exit_info_1,
5019 svm->vmcb->control.exit_info_2,
5020 svm->vmcb->control.exit_int_info,
5021 svm->vmcb->control.exit_int_info_err,
5022 KVM_ISA_SVM);
5023
5024 vmexit = nested_svm_exit_special(svm);
5025
5026 if (vmexit == NESTED_EXIT_CONTINUE)
5027 vmexit = nested_svm_exit_handled(svm);
5028
5029 if (vmexit == NESTED_EXIT_DONE)
5030 return 1;
5031 }
5032
5033 svm_complete_interrupts(svm);
5034
5035 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
5036 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
5037 kvm_run->fail_entry.hardware_entry_failure_reason
5038 = svm->vmcb->control.exit_code;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005039 dump_vmcb(vcpu);
5040 return 0;
5041 }
5042
5043 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
5044 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
5045 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
5046 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
5047 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
5048 "exit_code 0x%x\n",
5049 __func__, svm->vmcb->control.exit_int_info,
5050 exit_code);
5051
5052 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
5053 || !svm_exit_handlers[exit_code]) {
David Brazdil0f672f62019-12-10 10:32:29 +00005054 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%x\n", exit_code);
5055 dump_vmcb(vcpu);
5056 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5057 vcpu->run->internal.suberror =
5058 KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
5059 vcpu->run->internal.ndata = 1;
5060 vcpu->run->internal.data[0] = exit_code;
5061 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005062 }
5063
5064 return svm_exit_handlers[exit_code](svm);
5065}
5066
5067static void reload_tss(struct kvm_vcpu *vcpu)
5068{
5069 int cpu = raw_smp_processor_id();
5070
5071 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
5072 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
5073 load_TR_desc();
5074}
5075
5076static void pre_sev_run(struct vcpu_svm *svm, int cpu)
5077{
5078 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
5079 int asid = sev_get_asid(svm->vcpu.kvm);
5080
5081 /* Assign the asid allocated with this SEV guest */
5082 svm->vmcb->control.asid = asid;
5083
5084 /*
5085 * Flush guest TLB:
5086 *
5087 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
5088 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
5089 */
5090 if (sd->sev_vmcbs[asid] == svm->vmcb &&
5091 svm->last_cpu == cpu)
5092 return;
5093
5094 svm->last_cpu = cpu;
5095 sd->sev_vmcbs[asid] = svm->vmcb;
5096 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
5097 mark_dirty(svm->vmcb, VMCB_ASID);
5098}
5099
5100static void pre_svm_run(struct vcpu_svm *svm)
5101{
5102 int cpu = raw_smp_processor_id();
5103
5104 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
5105
5106 if (sev_guest(svm->vcpu.kvm))
5107 return pre_sev_run(svm, cpu);
5108
5109 /* FIXME: handle wraparound of asid_generation */
5110 if (svm->asid_generation != sd->asid_generation)
5111 new_asid(svm, sd);
5112}
5113
5114static void svm_inject_nmi(struct kvm_vcpu *vcpu)
5115{
5116 struct vcpu_svm *svm = to_svm(vcpu);
5117
5118 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
5119 vcpu->arch.hflags |= HF_NMI_MASK;
5120 set_intercept(svm, INTERCEPT_IRET);
5121 ++vcpu->stat.nmi_injections;
5122}
5123
5124static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
5125{
5126 struct vmcb_control_area *control;
5127
5128 /* The following fields are ignored when AVIC is enabled */
5129 control = &svm->vmcb->control;
5130 control->int_vector = irq;
5131 control->int_ctl &= ~V_INTR_PRIO_MASK;
5132 control->int_ctl |= V_IRQ_MASK |
5133 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
5134 mark_dirty(svm->vmcb, VMCB_INTR);
5135}
5136
5137static void svm_set_irq(struct kvm_vcpu *vcpu)
5138{
5139 struct vcpu_svm *svm = to_svm(vcpu);
5140
5141 BUG_ON(!(gif_set(svm)));
5142
5143 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
5144 ++vcpu->stat.irq_injections;
5145
5146 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
5147 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
5148}
5149
5150static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
5151{
5152 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
5153}
5154
5155static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
5156{
5157 struct vcpu_svm *svm = to_svm(vcpu);
5158
5159 if (svm_nested_virtualize_tpr(vcpu) ||
5160 kvm_vcpu_apicv_active(vcpu))
5161 return;
5162
5163 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
5164
5165 if (irr == -1)
5166 return;
5167
5168 if (tpr >= irr)
5169 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
5170}
5171
5172static void svm_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
5173{
5174 return;
5175}
5176
5177static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu)
5178{
5179 return avic && irqchip_split(vcpu->kvm);
5180}
5181
5182static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
5183{
5184}
5185
5186static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
5187{
5188}
5189
5190/* Note: Currently only used by Hyper-V. */
5191static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5192{
5193 struct vcpu_svm *svm = to_svm(vcpu);
5194 struct vmcb *vmcb = svm->vmcb;
5195
David Brazdil0f672f62019-12-10 10:32:29 +00005196 if (kvm_vcpu_apicv_active(vcpu))
5197 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
5198 else
5199 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
5200 mark_dirty(vmcb, VMCB_AVIC);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005201}
5202
5203static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
5204{
5205 return;
5206}
5207
Olivier Deprez0e641232021-09-23 10:07:05 +02005208static int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005209{
Olivier Deprez0e641232021-09-23 10:07:05 +02005210 if (!vcpu->arch.apicv_active)
5211 return -1;
5212
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005213 kvm_lapic_set_irr(vec, vcpu->arch.apic);
5214 smp_mb__after_atomic();
5215
David Brazdil0f672f62019-12-10 10:32:29 +00005216 if (avic_vcpu_is_running(vcpu)) {
5217 int cpuid = vcpu->cpu;
5218
5219 if (cpuid != get_cpu())
5220 wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpuid));
5221 put_cpu();
5222 } else
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005223 kvm_vcpu_wake_up(vcpu);
Olivier Deprez0e641232021-09-23 10:07:05 +02005224
5225 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005226}
5227
David Brazdil0f672f62019-12-10 10:32:29 +00005228static bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
5229{
5230 return false;
5231}
5232
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005233static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
5234{
5235 unsigned long flags;
5236 struct amd_svm_iommu_ir *cur;
5237
5238 spin_lock_irqsave(&svm->ir_list_lock, flags);
5239 list_for_each_entry(cur, &svm->ir_list, node) {
5240 if (cur->data != pi->ir_data)
5241 continue;
5242 list_del(&cur->node);
5243 kfree(cur);
5244 break;
5245 }
5246 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
5247}
5248
5249static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
5250{
5251 int ret = 0;
5252 unsigned long flags;
5253 struct amd_svm_iommu_ir *ir;
5254
5255 /**
5256 * In some cases, the existing irte is updaed and re-set,
5257 * so we need to check here if it's already been * added
5258 * to the ir_list.
5259 */
5260 if (pi->ir_data && (pi->prev_ga_tag != 0)) {
5261 struct kvm *kvm = svm->vcpu.kvm;
5262 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
5263 struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
5264 struct vcpu_svm *prev_svm;
5265
5266 if (!prev_vcpu) {
5267 ret = -EINVAL;
5268 goto out;
5269 }
5270
5271 prev_svm = to_svm(prev_vcpu);
5272 svm_ir_list_del(prev_svm, pi);
5273 }
5274
5275 /**
5276 * Allocating new amd_iommu_pi_data, which will get
5277 * add to the per-vcpu ir_list.
5278 */
David Brazdil0f672f62019-12-10 10:32:29 +00005279 ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005280 if (!ir) {
5281 ret = -ENOMEM;
5282 goto out;
5283 }
5284 ir->data = pi->ir_data;
5285
5286 spin_lock_irqsave(&svm->ir_list_lock, flags);
5287 list_add(&ir->node, &svm->ir_list);
5288 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
5289out:
5290 return ret;
5291}
5292
5293/**
5294 * Note:
5295 * The HW cannot support posting multicast/broadcast
5296 * interrupts to a vCPU. So, we still use legacy interrupt
5297 * remapping for these kind of interrupts.
5298 *
5299 * For lowest-priority interrupts, we only support
5300 * those with single CPU as the destination, e.g. user
5301 * configures the interrupts via /proc/irq or uses
5302 * irqbalance to make the interrupts single-CPU.
5303 */
5304static int
5305get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
5306 struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
5307{
5308 struct kvm_lapic_irq irq;
5309 struct kvm_vcpu *vcpu = NULL;
5310
5311 kvm_set_msi_irq(kvm, e, &irq);
5312
David Brazdil0f672f62019-12-10 10:32:29 +00005313 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
5314 !kvm_irq_is_postable(&irq)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005315 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
5316 __func__, irq.vector);
5317 return -1;
5318 }
5319
5320 pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
5321 irq.vector);
5322 *svm = to_svm(vcpu);
5323 vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
5324 vcpu_info->vector = irq.vector;
5325
5326 return 0;
5327}
5328
5329/*
5330 * svm_update_pi_irte - set IRTE for Posted-Interrupts
5331 *
5332 * @kvm: kvm
5333 * @host_irq: host irq of the interrupt
5334 * @guest_irq: gsi of the interrupt
5335 * @set: set or unset PI
5336 * returns 0 on success, < 0 on failure
5337 */
5338static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
5339 uint32_t guest_irq, bool set)
5340{
5341 struct kvm_kernel_irq_routing_entry *e;
5342 struct kvm_irq_routing_table *irq_rt;
5343 int idx, ret = -EINVAL;
5344
5345 if (!kvm_arch_has_assigned_device(kvm) ||
5346 !irq_remapping_cap(IRQ_POSTING_CAP))
5347 return 0;
5348
5349 pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
5350 __func__, host_irq, guest_irq, set);
5351
5352 idx = srcu_read_lock(&kvm->irq_srcu);
5353 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
5354 WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
5355
5356 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
5357 struct vcpu_data vcpu_info;
5358 struct vcpu_svm *svm = NULL;
5359
5360 if (e->type != KVM_IRQ_ROUTING_MSI)
5361 continue;
5362
5363 /**
5364 * Here, we setup with legacy mode in the following cases:
5365 * 1. When cannot target interrupt to a specific vcpu.
5366 * 2. Unsetting posted interrupt.
5367 * 3. APIC virtialization is disabled for the vcpu.
David Brazdil0f672f62019-12-10 10:32:29 +00005368 * 4. IRQ has incompatible delivery mode (SMI, INIT, etc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005369 */
5370 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
5371 kvm_vcpu_apicv_active(&svm->vcpu)) {
5372 struct amd_iommu_pi_data pi;
5373
5374 /* Try to enable guest_mode in IRTE */
5375 pi.base = __sme_set(page_to_phys(svm->avic_backing_page) &
5376 AVIC_HPA_MASK);
5377 pi.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id,
5378 svm->vcpu.vcpu_id);
5379 pi.is_guest_mode = true;
5380 pi.vcpu_data = &vcpu_info;
5381 ret = irq_set_vcpu_affinity(host_irq, &pi);
5382
5383 /**
5384 * Here, we successfully setting up vcpu affinity in
5385 * IOMMU guest mode. Now, we need to store the posted
5386 * interrupt information in a per-vcpu ir_list so that
5387 * we can reference to them directly when we update vcpu
5388 * scheduling information in IOMMU irte.
5389 */
5390 if (!ret && pi.is_guest_mode)
5391 svm_ir_list_add(svm, &pi);
5392 } else {
5393 /* Use legacy mode in IRTE */
5394 struct amd_iommu_pi_data pi;
5395
5396 /**
5397 * Here, pi is used to:
5398 * - Tell IOMMU to use legacy mode for this interrupt.
5399 * - Retrieve ga_tag of prior interrupt remapping data.
5400 */
Olivier Deprez0e641232021-09-23 10:07:05 +02005401 pi.prev_ga_tag = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005402 pi.is_guest_mode = false;
5403 ret = irq_set_vcpu_affinity(host_irq, &pi);
5404
5405 /**
5406 * Check if the posted interrupt was previously
5407 * setup with the guest_mode by checking if the ga_tag
5408 * was cached. If so, we need to clean up the per-vcpu
5409 * ir_list.
5410 */
5411 if (!ret && pi.prev_ga_tag) {
5412 int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
5413 struct kvm_vcpu *vcpu;
5414
5415 vcpu = kvm_get_vcpu_by_id(kvm, id);
5416 if (vcpu)
5417 svm_ir_list_del(to_svm(vcpu), &pi);
5418 }
5419 }
5420
5421 if (!ret && svm) {
5422 trace_kvm_pi_irte_update(host_irq, svm->vcpu.vcpu_id,
5423 e->gsi, vcpu_info.vector,
5424 vcpu_info.pi_desc_addr, set);
5425 }
5426
5427 if (ret < 0) {
5428 pr_err("%s: failed to update PI IRTE\n", __func__);
5429 goto out;
5430 }
5431 }
5432
5433 ret = 0;
5434out:
5435 srcu_read_unlock(&kvm->irq_srcu, idx);
5436 return ret;
5437}
5438
5439static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
5440{
5441 struct vcpu_svm *svm = to_svm(vcpu);
5442 struct vmcb *vmcb = svm->vmcb;
5443 int ret;
5444 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
5445 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
5446 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
5447
5448 return ret;
5449}
5450
5451static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
5452{
5453 struct vcpu_svm *svm = to_svm(vcpu);
5454
5455 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
5456}
5457
5458static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5459{
5460 struct vcpu_svm *svm = to_svm(vcpu);
5461
5462 if (masked) {
5463 svm->vcpu.arch.hflags |= HF_NMI_MASK;
5464 set_intercept(svm, INTERCEPT_IRET);
5465 } else {
5466 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
5467 clr_intercept(svm, INTERCEPT_IRET);
5468 }
5469}
5470
5471static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
5472{
5473 struct vcpu_svm *svm = to_svm(vcpu);
5474 struct vmcb *vmcb = svm->vmcb;
5475 int ret;
5476
5477 if (!gif_set(svm) ||
5478 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
5479 return 0;
5480
5481 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
5482
5483 if (is_guest_mode(vcpu))
5484 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
5485
5486 return ret;
5487}
5488
5489static void enable_irq_window(struct kvm_vcpu *vcpu)
5490{
5491 struct vcpu_svm *svm = to_svm(vcpu);
5492
5493 if (kvm_vcpu_apicv_active(vcpu))
5494 return;
5495
5496 /*
5497 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
5498 * 1, because that's a separate STGI/VMRUN intercept. The next time we
5499 * get that intercept, this function will be called again though and
5500 * we'll get the vintr intercept. However, if the vGIF feature is
5501 * enabled, the STGI interception will not occur. Enable the irq
5502 * window under the assumption that the hardware will set the GIF.
5503 */
5504 if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) {
5505 svm_set_vintr(svm);
5506 svm_inject_irq(svm, 0x0);
5507 }
5508}
5509
5510static void enable_nmi_window(struct kvm_vcpu *vcpu)
5511{
5512 struct vcpu_svm *svm = to_svm(vcpu);
5513
5514 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
5515 == HF_NMI_MASK)
5516 return; /* IRET will cause a vm exit */
5517
5518 if (!gif_set(svm)) {
5519 if (vgif_enabled(svm))
5520 set_intercept(svm, INTERCEPT_STGI);
5521 return; /* STGI will cause a vm exit */
5522 }
5523
5524 if (svm->nested.exit_required)
5525 return; /* we're not going to run the guest yet */
5526
5527 /*
5528 * Something prevents NMI from been injected. Single step over possible
5529 * problem (IRET or exception injection or interrupt shadow)
5530 */
5531 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
5532 svm->nmi_singlestep = true;
5533 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
5534}
5535
5536static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
5537{
5538 return 0;
5539}
5540
5541static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
5542{
5543 return 0;
5544}
5545
5546static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
5547{
5548 struct vcpu_svm *svm = to_svm(vcpu);
5549
5550 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
5551 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
5552 else
5553 svm->asid_generation--;
5554}
5555
5556static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
5557{
5558 struct vcpu_svm *svm = to_svm(vcpu);
5559
5560 invlpga(gva, svm->vmcb->control.asid);
5561}
5562
5563static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
5564{
5565}
5566
5567static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
5568{
5569 struct vcpu_svm *svm = to_svm(vcpu);
5570
5571 if (svm_nested_virtualize_tpr(vcpu))
5572 return;
5573
5574 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
5575 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
5576 kvm_set_cr8(vcpu, cr8);
5577 }
5578}
5579
5580static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
5581{
5582 struct vcpu_svm *svm = to_svm(vcpu);
5583 u64 cr8;
5584
5585 if (svm_nested_virtualize_tpr(vcpu) ||
5586 kvm_vcpu_apicv_active(vcpu))
5587 return;
5588
5589 cr8 = kvm_get_cr8(vcpu);
5590 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
5591 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
5592}
5593
5594static void svm_complete_interrupts(struct vcpu_svm *svm)
5595{
5596 u8 vector;
5597 int type;
5598 u32 exitintinfo = svm->vmcb->control.exit_int_info;
5599 unsigned int3_injected = svm->int3_injected;
5600
5601 svm->int3_injected = 0;
5602
5603 /*
5604 * If we've made progress since setting HF_IRET_MASK, we've
5605 * executed an IRET and can allow NMI injection.
5606 */
5607 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
5608 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
5609 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
5610 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5611 }
5612
5613 svm->vcpu.arch.nmi_injected = false;
5614 kvm_clear_exception_queue(&svm->vcpu);
5615 kvm_clear_interrupt_queue(&svm->vcpu);
5616
5617 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
5618 return;
5619
5620 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5621
5622 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
5623 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
5624
5625 switch (type) {
5626 case SVM_EXITINTINFO_TYPE_NMI:
5627 svm->vcpu.arch.nmi_injected = true;
5628 break;
5629 case SVM_EXITINTINFO_TYPE_EXEPT:
5630 /*
5631 * In case of software exceptions, do not reinject the vector,
5632 * but re-execute the instruction instead. Rewind RIP first
5633 * if we emulated INT3 before.
5634 */
5635 if (kvm_exception_is_soft(vector)) {
5636 if (vector == BP_VECTOR && int3_injected &&
5637 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
5638 kvm_rip_write(&svm->vcpu,
5639 kvm_rip_read(&svm->vcpu) -
5640 int3_injected);
5641 break;
5642 }
5643 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
5644 u32 err = svm->vmcb->control.exit_int_info_err;
5645 kvm_requeue_exception_e(&svm->vcpu, vector, err);
5646
5647 } else
5648 kvm_requeue_exception(&svm->vcpu, vector);
5649 break;
5650 case SVM_EXITINTINFO_TYPE_INTR:
5651 kvm_queue_interrupt(&svm->vcpu, vector, false);
5652 break;
5653 default:
5654 break;
5655 }
5656}
5657
5658static void svm_cancel_injection(struct kvm_vcpu *vcpu)
5659{
5660 struct vcpu_svm *svm = to_svm(vcpu);
5661 struct vmcb_control_area *control = &svm->vmcb->control;
5662
5663 control->exit_int_info = control->event_inj;
5664 control->exit_int_info_err = control->event_inj_err;
5665 control->event_inj = 0;
5666 svm_complete_interrupts(svm);
5667}
5668
5669static void svm_vcpu_run(struct kvm_vcpu *vcpu)
5670{
5671 struct vcpu_svm *svm = to_svm(vcpu);
5672
5673 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5674 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5675 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5676
5677 /*
5678 * A vmexit emulation is required before the vcpu can be executed
5679 * again.
5680 */
5681 if (unlikely(svm->nested.exit_required))
5682 return;
5683
5684 /*
5685 * Disable singlestep if we're injecting an interrupt/exception.
5686 * We don't want our modified rflags to be pushed on the stack where
5687 * we might not be able to easily reset them if we disabled NMI
5688 * singlestep later.
5689 */
5690 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
5691 /*
5692 * Event injection happens before external interrupts cause a
5693 * vmexit and interrupts are disabled here, so smp_send_reschedule
5694 * is enough to force an immediate vmexit.
5695 */
5696 disable_nmi_singlestep(svm);
5697 smp_send_reschedule(vcpu->cpu);
5698 }
5699
5700 pre_svm_run(svm);
5701
5702 sync_lapic_to_cr8(vcpu);
5703
5704 svm->vmcb->save.cr2 = vcpu->arch.cr2;
5705
5706 clgi();
David Brazdil0f672f62019-12-10 10:32:29 +00005707 kvm_load_guest_xcr0(vcpu);
5708
5709 if (lapic_in_kernel(vcpu) &&
5710 vcpu->arch.apic->lapic_timer.timer_advance_ns)
5711 kvm_wait_lapic_expire(vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005712
5713 /*
5714 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
5715 * it's non-zero. Since vmentry is serialising on affected CPUs, there
5716 * is no need to worry about the conditional branch over the wrmsr
5717 * being speculatively taken.
5718 */
5719 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
5720
5721 local_irq_enable();
5722
5723 asm volatile (
5724 "push %%" _ASM_BP "; \n\t"
5725 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
5726 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
5727 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
5728 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
5729 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
5730 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
5731#ifdef CONFIG_X86_64
5732 "mov %c[r8](%[svm]), %%r8 \n\t"
5733 "mov %c[r9](%[svm]), %%r9 \n\t"
5734 "mov %c[r10](%[svm]), %%r10 \n\t"
5735 "mov %c[r11](%[svm]), %%r11 \n\t"
5736 "mov %c[r12](%[svm]), %%r12 \n\t"
5737 "mov %c[r13](%[svm]), %%r13 \n\t"
5738 "mov %c[r14](%[svm]), %%r14 \n\t"
5739 "mov %c[r15](%[svm]), %%r15 \n\t"
5740#endif
5741
5742 /* Enter guest mode */
5743 "push %%" _ASM_AX " \n\t"
5744 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
David Brazdil0f672f62019-12-10 10:32:29 +00005745 __ex("vmload %%" _ASM_AX) "\n\t"
5746 __ex("vmrun %%" _ASM_AX) "\n\t"
5747 __ex("vmsave %%" _ASM_AX) "\n\t"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005748 "pop %%" _ASM_AX " \n\t"
5749
5750 /* Save guest registers, load host registers */
5751 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
5752 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
5753 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
5754 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
5755 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
5756 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
5757#ifdef CONFIG_X86_64
5758 "mov %%r8, %c[r8](%[svm]) \n\t"
5759 "mov %%r9, %c[r9](%[svm]) \n\t"
5760 "mov %%r10, %c[r10](%[svm]) \n\t"
5761 "mov %%r11, %c[r11](%[svm]) \n\t"
5762 "mov %%r12, %c[r12](%[svm]) \n\t"
5763 "mov %%r13, %c[r13](%[svm]) \n\t"
5764 "mov %%r14, %c[r14](%[svm]) \n\t"
5765 "mov %%r15, %c[r15](%[svm]) \n\t"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005766 /*
5767 * Clear host registers marked as clobbered to prevent
5768 * speculative use.
5769 */
David Brazdil0f672f62019-12-10 10:32:29 +00005770 "xor %%r8d, %%r8d \n\t"
5771 "xor %%r9d, %%r9d \n\t"
5772 "xor %%r10d, %%r10d \n\t"
5773 "xor %%r11d, %%r11d \n\t"
5774 "xor %%r12d, %%r12d \n\t"
5775 "xor %%r13d, %%r13d \n\t"
5776 "xor %%r14d, %%r14d \n\t"
5777 "xor %%r15d, %%r15d \n\t"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005778#endif
David Brazdil0f672f62019-12-10 10:32:29 +00005779 "xor %%ebx, %%ebx \n\t"
5780 "xor %%ecx, %%ecx \n\t"
5781 "xor %%edx, %%edx \n\t"
5782 "xor %%esi, %%esi \n\t"
5783 "xor %%edi, %%edi \n\t"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005784 "pop %%" _ASM_BP
5785 :
5786 : [svm]"a"(svm),
5787 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
5788 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
5789 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
5790 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
5791 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
5792 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
5793 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
5794#ifdef CONFIG_X86_64
5795 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
5796 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
5797 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
5798 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
5799 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
5800 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
5801 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
5802 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
5803#endif
5804 : "cc", "memory"
5805#ifdef CONFIG_X86_64
5806 , "rbx", "rcx", "rdx", "rsi", "rdi"
5807 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
5808#else
5809 , "ebx", "ecx", "edx", "esi", "edi"
5810#endif
5811 );
5812
5813 /* Eliminate branch target predictions from guest mode */
5814 vmexit_fill_RSB();
5815
5816#ifdef CONFIG_X86_64
5817 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
5818#else
5819 loadsegment(fs, svm->host.fs);
5820#ifndef CONFIG_X86_32_LAZY_GS
5821 loadsegment(gs, svm->host.gs);
5822#endif
5823#endif
5824
5825 /*
5826 * We do not use IBRS in the kernel. If this vCPU has used the
5827 * SPEC_CTRL MSR it may have left it on; save the value and
5828 * turn it off. This is much more efficient than blindly adding
5829 * it to the atomic save/restore list. Especially as the former
5830 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
5831 *
5832 * For non-nested case:
5833 * If the L01 MSR bitmap does not intercept the MSR, then we need to
5834 * save it.
5835 *
5836 * For nested case:
5837 * If the L02 MSR bitmap does not intercept the MSR, then we need to
5838 * save it.
5839 */
5840 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
5841 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
5842
5843 reload_tss(vcpu);
5844
5845 local_irq_disable();
5846
5847 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
5848
5849 vcpu->arch.cr2 = svm->vmcb->save.cr2;
5850 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
5851 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
5852 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
5853
5854 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5855 kvm_before_interrupt(&svm->vcpu);
5856
David Brazdil0f672f62019-12-10 10:32:29 +00005857 kvm_put_guest_xcr0(vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005858 stgi();
5859
5860 /* Any pending NMI will happen here */
5861
5862 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5863 kvm_after_interrupt(&svm->vcpu);
5864
5865 sync_cr8_to_lapic(vcpu);
5866
5867 svm->next_rip = 0;
5868
5869 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
5870
5871 /* if exit due to PF check for async PF */
5872 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
5873 svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
5874
5875 if (npt_enabled) {
5876 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
5877 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
5878 }
5879
5880 /*
5881 * We need to handle MC intercepts here before the vcpu has a chance to
5882 * change the physical cpu
5883 */
5884 if (unlikely(svm->vmcb->control.exit_code ==
5885 SVM_EXIT_EXCP_BASE + MC_VECTOR))
5886 svm_handle_mce(svm);
5887
5888 mark_all_clean(svm->vmcb);
5889}
5890STACK_FRAME_NON_STANDARD(svm_vcpu_run);
5891
5892static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5893{
5894 struct vcpu_svm *svm = to_svm(vcpu);
5895
5896 svm->vmcb->save.cr3 = __sme_set(root);
5897 mark_dirty(svm->vmcb, VMCB_CR);
5898}
5899
5900static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5901{
5902 struct vcpu_svm *svm = to_svm(vcpu);
5903
5904 svm->vmcb->control.nested_cr3 = __sme_set(root);
5905 mark_dirty(svm->vmcb, VMCB_NPT);
5906
5907 /* Also sync guest cr3 here in case we live migrate */
5908 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
5909 mark_dirty(svm->vmcb, VMCB_CR);
5910}
5911
5912static int is_disabled(void)
5913{
5914 u64 vm_cr;
5915
5916 rdmsrl(MSR_VM_CR, vm_cr);
5917 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
5918 return 1;
5919
5920 return 0;
5921}
5922
5923static void
5924svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5925{
5926 /*
5927 * Patch in the VMMCALL instruction:
5928 */
5929 hypercall[0] = 0x0f;
5930 hypercall[1] = 0x01;
5931 hypercall[2] = 0xd9;
5932}
5933
David Brazdil0f672f62019-12-10 10:32:29 +00005934static int __init svm_check_processor_compat(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005935{
David Brazdil0f672f62019-12-10 10:32:29 +00005936 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005937}
5938
5939static bool svm_cpu_has_accelerated_tpr(void)
5940{
5941 return false;
5942}
5943
5944static bool svm_has_emulated_msr(int index)
5945{
David Brazdil0f672f62019-12-10 10:32:29 +00005946 switch (index) {
5947 case MSR_IA32_MCG_EXT_CTL:
5948 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
5949 return false;
5950 default:
5951 break;
5952 }
5953
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005954 return true;
5955}
5956
5957static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
5958{
5959 return 0;
5960}
5961
5962static void svm_cpuid_update(struct kvm_vcpu *vcpu)
5963{
5964 struct vcpu_svm *svm = to_svm(vcpu);
5965
5966 /* Update nrips enabled cache */
5967 svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
5968
5969 if (!kvm_vcpu_apicv_active(vcpu))
5970 return;
5971
5972 guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
5973}
5974
David Brazdil0f672f62019-12-10 10:32:29 +00005975#define F(x) bit(X86_FEATURE_##x)
5976
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005977static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5978{
5979 switch (func) {
5980 case 0x1:
5981 if (avic)
5982 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
5983 break;
5984 case 0x80000001:
5985 if (nested)
5986 entry->ecx |= (1 << 2); /* Set SVM bit */
5987 break;
David Brazdil0f672f62019-12-10 10:32:29 +00005988 case 0x80000008:
5989 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
5990 boot_cpu_has(X86_FEATURE_AMD_SSBD))
5991 entry->ebx |= F(VIRT_SSBD);
5992 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005993 case 0x8000000A:
5994 entry->eax = 1; /* SVM revision 1 */
5995 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
5996 ASID emulation to nested SVM */
5997 entry->ecx = 0; /* Reserved */
5998 entry->edx = 0; /* Per default do not support any
5999 additional features */
6000
6001 /* Support next_rip if host supports it */
6002 if (boot_cpu_has(X86_FEATURE_NRIPS))
David Brazdil0f672f62019-12-10 10:32:29 +00006003 entry->edx |= F(NRIPS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006004
6005 /* Support NPT for the guest if enabled */
6006 if (npt_enabled)
David Brazdil0f672f62019-12-10 10:32:29 +00006007 entry->edx |= F(NPT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006008
6009 break;
6010 case 0x8000001F:
6011 /* Support memory encryption cpuid if host supports it */
6012 if (boot_cpu_has(X86_FEATURE_SEV))
6013 cpuid(0x8000001f, &entry->eax, &entry->ebx,
6014 &entry->ecx, &entry->edx);
6015
6016 }
6017}
6018
6019static int svm_get_lpage_level(void)
6020{
6021 return PT_PDPE_LEVEL;
6022}
6023
6024static bool svm_rdtscp_supported(void)
6025{
6026 return boot_cpu_has(X86_FEATURE_RDTSCP);
6027}
6028
6029static bool svm_invpcid_supported(void)
6030{
6031 return false;
6032}
6033
6034static bool svm_mpx_supported(void)
6035{
6036 return false;
6037}
6038
6039static bool svm_xsaves_supported(void)
6040{
6041 return false;
6042}
6043
6044static bool svm_umip_emulated(void)
6045{
6046 return false;
6047}
6048
David Brazdil0f672f62019-12-10 10:32:29 +00006049static bool svm_pt_supported(void)
6050{
6051 return false;
6052}
6053
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006054static bool svm_has_wbinvd_exit(void)
6055{
6056 return true;
6057}
6058
Olivier Deprez0e641232021-09-23 10:07:05 +02006059static bool svm_pku_supported(void)
6060{
6061 return false;
6062}
6063
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006064#define PRE_EX(exit) { .exit_code = (exit), \
6065 .stage = X86_ICPT_PRE_EXCEPT, }
6066#define POST_EX(exit) { .exit_code = (exit), \
6067 .stage = X86_ICPT_POST_EXCEPT, }
6068#define POST_MEM(exit) { .exit_code = (exit), \
6069 .stage = X86_ICPT_POST_MEMACCESS, }
6070
6071static const struct __x86_intercept {
6072 u32 exit_code;
6073 enum x86_intercept_stage stage;
6074} x86_intercept_map[] = {
6075 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
6076 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
6077 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
6078 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
6079 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
6080 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
6081 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
6082 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
6083 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
6084 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
6085 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
6086 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
6087 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
6088 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
6089 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
6090 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
6091 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
6092 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
6093 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
6094 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
6095 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
6096 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
6097 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
6098 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
6099 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
6100 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
6101 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
6102 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
6103 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
6104 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
6105 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
6106 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
6107 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
6108 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
6109 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
6110 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
6111 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
6112 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
6113 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
6114 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
6115 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
6116 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
6117 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
6118 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
6119 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
6120 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
David Brazdil0f672f62019-12-10 10:32:29 +00006121 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006122};
6123
6124#undef PRE_EX
6125#undef POST_EX
6126#undef POST_MEM
6127
6128static int svm_check_intercept(struct kvm_vcpu *vcpu,
6129 struct x86_instruction_info *info,
6130 enum x86_intercept_stage stage)
6131{
6132 struct vcpu_svm *svm = to_svm(vcpu);
6133 int vmexit, ret = X86EMUL_CONTINUE;
6134 struct __x86_intercept icpt_info;
6135 struct vmcb *vmcb = svm->vmcb;
6136
6137 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
6138 goto out;
6139
6140 icpt_info = x86_intercept_map[info->intercept];
6141
6142 if (stage != icpt_info.stage)
6143 goto out;
6144
6145 switch (icpt_info.exit_code) {
6146 case SVM_EXIT_READ_CR0:
6147 if (info->intercept == x86_intercept_cr_read)
6148 icpt_info.exit_code += info->modrm_reg;
6149 break;
6150 case SVM_EXIT_WRITE_CR0: {
6151 unsigned long cr0, val;
6152 u64 intercept;
6153
6154 if (info->intercept == x86_intercept_cr_write)
6155 icpt_info.exit_code += info->modrm_reg;
6156
6157 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
6158 info->intercept == x86_intercept_clts)
6159 break;
6160
6161 intercept = svm->nested.intercept;
6162
6163 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
6164 break;
6165
6166 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
6167 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
6168
6169 if (info->intercept == x86_intercept_lmsw) {
6170 cr0 &= 0xfUL;
6171 val &= 0xfUL;
6172 /* lmsw can't clear PE - catch this here */
6173 if (cr0 & X86_CR0_PE)
6174 val |= X86_CR0_PE;
6175 }
6176
6177 if (cr0 ^ val)
6178 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
6179
6180 break;
6181 }
6182 case SVM_EXIT_READ_DR0:
6183 case SVM_EXIT_WRITE_DR0:
6184 icpt_info.exit_code += info->modrm_reg;
6185 break;
6186 case SVM_EXIT_MSR:
6187 if (info->intercept == x86_intercept_wrmsr)
6188 vmcb->control.exit_info_1 = 1;
6189 else
6190 vmcb->control.exit_info_1 = 0;
6191 break;
6192 case SVM_EXIT_PAUSE:
6193 /*
6194 * We get this for NOP only, but pause
6195 * is rep not, check this here
6196 */
6197 if (info->rep_prefix != REPE_PREFIX)
6198 goto out;
6199 break;
6200 case SVM_EXIT_IOIO: {
6201 u64 exit_info;
6202 u32 bytes;
6203
6204 if (info->intercept == x86_intercept_in ||
6205 info->intercept == x86_intercept_ins) {
6206 exit_info = ((info->src_val & 0xffff) << 16) |
6207 SVM_IOIO_TYPE_MASK;
6208 bytes = info->dst_bytes;
6209 } else {
6210 exit_info = (info->dst_val & 0xffff) << 16;
6211 bytes = info->src_bytes;
6212 }
6213
6214 if (info->intercept == x86_intercept_outs ||
6215 info->intercept == x86_intercept_ins)
6216 exit_info |= SVM_IOIO_STR_MASK;
6217
6218 if (info->rep_prefix)
6219 exit_info |= SVM_IOIO_REP_MASK;
6220
6221 bytes = min(bytes, 4u);
6222
6223 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
6224
6225 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
6226
6227 vmcb->control.exit_info_1 = exit_info;
6228 vmcb->control.exit_info_2 = info->next_rip;
6229
6230 break;
6231 }
6232 default:
6233 break;
6234 }
6235
6236 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
6237 if (static_cpu_has(X86_FEATURE_NRIPS))
6238 vmcb->control.next_rip = info->next_rip;
6239 vmcb->control.exit_code = icpt_info.exit_code;
6240 vmexit = nested_svm_exit_handled(svm);
6241
6242 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
6243 : X86EMUL_CONTINUE;
6244
6245out:
6246 return ret;
6247}
6248
David Brazdil0f672f62019-12-10 10:32:29 +00006249static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006250{
David Brazdil0f672f62019-12-10 10:32:29 +00006251
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006252}
6253
6254static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
6255{
6256 if (pause_filter_thresh)
6257 shrink_ple_window(vcpu);
6258}
6259
6260static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
6261{
6262 if (avic_handle_apic_id_update(vcpu) != 0)
6263 return;
David Brazdil0f672f62019-12-10 10:32:29 +00006264 avic_handle_dfr_update(vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006265 avic_handle_ldr_update(vcpu);
6266}
6267
6268static void svm_setup_mce(struct kvm_vcpu *vcpu)
6269{
6270 /* [63:9] are reserved. */
6271 vcpu->arch.mcg_cap &= 0x1ff;
6272}
6273
6274static int svm_smi_allowed(struct kvm_vcpu *vcpu)
6275{
6276 struct vcpu_svm *svm = to_svm(vcpu);
6277
6278 /* Per APM Vol.2 15.22.2 "Response to SMI" */
6279 if (!gif_set(svm))
6280 return 0;
6281
6282 if (is_guest_mode(&svm->vcpu) &&
6283 svm->nested.intercept & (1ULL << INTERCEPT_SMI)) {
6284 /* TODO: Might need to set exit_info_1 and exit_info_2 here */
6285 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
6286 svm->nested.exit_required = true;
6287 return 0;
6288 }
6289
6290 return 1;
6291}
6292
6293static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
6294{
6295 struct vcpu_svm *svm = to_svm(vcpu);
6296 int ret;
6297
6298 if (is_guest_mode(vcpu)) {
6299 /* FED8h - SVM Guest */
6300 put_smstate(u64, smstate, 0x7ed8, 1);
6301 /* FEE0h - SVM Guest VMCB Physical Address */
6302 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb);
6303
6304 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
6305 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
6306 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
6307
6308 ret = nested_svm_vmexit(svm);
6309 if (ret)
6310 return ret;
6311 }
6312 return 0;
6313}
6314
David Brazdil0f672f62019-12-10 10:32:29 +00006315static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006316{
6317 struct vcpu_svm *svm = to_svm(vcpu);
6318 struct vmcb *nested_vmcb;
David Brazdil0f672f62019-12-10 10:32:29 +00006319 struct kvm_host_map map;
6320 u64 guest;
6321 u64 vmcb;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006322
David Brazdil0f672f62019-12-10 10:32:29 +00006323 guest = GET_SMSTATE(u64, smstate, 0x7ed8);
6324 vmcb = GET_SMSTATE(u64, smstate, 0x7ee0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006325
David Brazdil0f672f62019-12-10 10:32:29 +00006326 if (guest) {
6327 if (kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb), &map) == -EINVAL)
6328 return 1;
6329 nested_vmcb = map.hva;
6330 enter_svm_guest_mode(svm, vmcb, nested_vmcb, &map);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006331 }
David Brazdil0f672f62019-12-10 10:32:29 +00006332 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006333}
6334
6335static int enable_smi_window(struct kvm_vcpu *vcpu)
6336{
6337 struct vcpu_svm *svm = to_svm(vcpu);
6338
6339 if (!gif_set(svm)) {
6340 if (vgif_enabled(svm))
6341 set_intercept(svm, INTERCEPT_STGI);
6342 /* STGI will cause a vm exit */
6343 return 1;
6344 }
6345 return 0;
6346}
6347
6348static int sev_asid_new(void)
6349{
6350 int pos;
6351
6352 /*
6353 * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid.
6354 */
6355 pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1);
6356 if (pos >= max_sev_asid)
6357 return -EBUSY;
6358
6359 set_bit(pos, sev_asid_bitmap);
6360 return pos + 1;
6361}
6362
6363static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
6364{
6365 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
6366 int asid, ret;
6367
6368 ret = -EBUSY;
David Brazdil0f672f62019-12-10 10:32:29 +00006369 if (unlikely(sev->active))
6370 return ret;
6371
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006372 asid = sev_asid_new();
6373 if (asid < 0)
6374 return ret;
6375
6376 ret = sev_platform_init(&argp->error);
6377 if (ret)
6378 goto e_free;
6379
6380 sev->active = true;
6381 sev->asid = asid;
6382 INIT_LIST_HEAD(&sev->regions_list);
6383
6384 return 0;
6385
6386e_free:
6387 __sev_asid_free(asid);
6388 return ret;
6389}
6390
6391static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
6392{
6393 struct sev_data_activate *data;
6394 int asid = sev_get_asid(kvm);
6395 int ret;
6396
6397 wbinvd_on_all_cpus();
6398
6399 ret = sev_guest_df_flush(error);
6400 if (ret)
6401 return ret;
6402
David Brazdil0f672f62019-12-10 10:32:29 +00006403 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006404 if (!data)
6405 return -ENOMEM;
6406
6407 /* activate ASID on the given handle */
6408 data->handle = handle;
6409 data->asid = asid;
6410 ret = sev_guest_activate(data, error);
6411 kfree(data);
6412
6413 return ret;
6414}
6415
6416static int __sev_issue_cmd(int fd, int id, void *data, int *error)
6417{
6418 struct fd f;
6419 int ret;
6420
6421 f = fdget(fd);
6422 if (!f.file)
6423 return -EBADF;
6424
6425 ret = sev_issue_cmd_external_user(f.file, id, data, error);
6426
6427 fdput(f);
6428 return ret;
6429}
6430
6431static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
6432{
6433 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
6434
6435 return __sev_issue_cmd(sev->fd, id, data, error);
6436}
6437
6438static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
6439{
6440 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
6441 struct sev_data_launch_start *start;
6442 struct kvm_sev_launch_start params;
6443 void *dh_blob, *session_blob;
6444 int *error = &argp->error;
6445 int ret;
6446
6447 if (!sev_guest(kvm))
6448 return -ENOTTY;
6449
6450 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6451 return -EFAULT;
6452
David Brazdil0f672f62019-12-10 10:32:29 +00006453 start = kzalloc(sizeof(*start), GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006454 if (!start)
6455 return -ENOMEM;
6456
6457 dh_blob = NULL;
6458 if (params.dh_uaddr) {
6459 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
6460 if (IS_ERR(dh_blob)) {
6461 ret = PTR_ERR(dh_blob);
6462 goto e_free;
6463 }
6464
6465 start->dh_cert_address = __sme_set(__pa(dh_blob));
6466 start->dh_cert_len = params.dh_len;
6467 }
6468
6469 session_blob = NULL;
6470 if (params.session_uaddr) {
6471 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
6472 if (IS_ERR(session_blob)) {
6473 ret = PTR_ERR(session_blob);
6474 goto e_free_dh;
6475 }
6476
6477 start->session_address = __sme_set(__pa(session_blob));
6478 start->session_len = params.session_len;
6479 }
6480
6481 start->handle = params.handle;
6482 start->policy = params.policy;
6483
6484 /* create memory encryption context */
6485 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error);
6486 if (ret)
6487 goto e_free_session;
6488
6489 /* Bind ASID to this guest */
6490 ret = sev_bind_asid(kvm, start->handle, error);
Olivier Deprez0e641232021-09-23 10:07:05 +02006491 if (ret) {
6492 sev_decommission(start->handle);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006493 goto e_free_session;
Olivier Deprez0e641232021-09-23 10:07:05 +02006494 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006495
6496 /* return handle to userspace */
6497 params.handle = start->handle;
6498 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
6499 sev_unbind_asid(kvm, start->handle);
6500 ret = -EFAULT;
6501 goto e_free_session;
6502 }
6503
6504 sev->handle = start->handle;
6505 sev->fd = argp->sev_fd;
6506
6507e_free_session:
6508 kfree(session_blob);
6509e_free_dh:
6510 kfree(dh_blob);
6511e_free:
6512 kfree(start);
6513 return ret;
6514}
6515
David Brazdil0f672f62019-12-10 10:32:29 +00006516static unsigned long get_num_contig_pages(unsigned long idx,
6517 struct page **inpages, unsigned long npages)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006518{
6519 unsigned long paddr, next_paddr;
David Brazdil0f672f62019-12-10 10:32:29 +00006520 unsigned long i = idx + 1, pages = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006521
6522 /* find the number of contiguous pages starting from idx */
6523 paddr = __sme_page_pa(inpages[idx]);
6524 while (i < npages) {
6525 next_paddr = __sme_page_pa(inpages[i++]);
6526 if ((paddr + PAGE_SIZE) == next_paddr) {
6527 pages++;
6528 paddr = next_paddr;
6529 continue;
6530 }
6531 break;
6532 }
6533
6534 return pages;
6535}
6536
6537static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
6538{
David Brazdil0f672f62019-12-10 10:32:29 +00006539 unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006540 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
6541 struct kvm_sev_launch_update_data params;
6542 struct sev_data_launch_update_data *data;
6543 struct page **inpages;
David Brazdil0f672f62019-12-10 10:32:29 +00006544 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006545
6546 if (!sev_guest(kvm))
6547 return -ENOTTY;
6548
6549 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6550 return -EFAULT;
6551
David Brazdil0f672f62019-12-10 10:32:29 +00006552 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006553 if (!data)
6554 return -ENOMEM;
6555
6556 vaddr = params.uaddr;
6557 size = params.len;
6558 vaddr_end = vaddr + size;
6559
6560 /* Lock the user memory. */
6561 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
6562 if (!inpages) {
6563 ret = -ENOMEM;
6564 goto e_free;
6565 }
6566
6567 /*
6568 * The LAUNCH_UPDATE command will perform in-place encryption of the
6569 * memory content (i.e it will write the same memory region with C=1).
6570 * It's possible that the cache may contain the data with C=0, i.e.,
6571 * unencrypted so invalidate it first.
6572 */
6573 sev_clflush_pages(inpages, npages);
6574
6575 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
6576 int offset, len;
6577
6578 /*
6579 * If the user buffer is not page-aligned, calculate the offset
6580 * within the page.
6581 */
6582 offset = vaddr & (PAGE_SIZE - 1);
6583
6584 /* Calculate the number of pages that can be encrypted in one go. */
6585 pages = get_num_contig_pages(i, inpages, npages);
6586
6587 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
6588
6589 data->handle = sev->handle;
6590 data->len = len;
6591 data->address = __sme_page_pa(inpages[i]) + offset;
6592 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error);
6593 if (ret)
6594 goto e_unpin;
6595
6596 size -= len;
6597 next_vaddr = vaddr + len;
6598 }
6599
6600e_unpin:
6601 /* content of memory is updated, mark pages dirty */
6602 for (i = 0; i < npages; i++) {
6603 set_page_dirty_lock(inpages[i]);
6604 mark_page_accessed(inpages[i]);
6605 }
6606 /* unlock the user pages */
6607 sev_unpin_memory(kvm, inpages, npages);
6608e_free:
6609 kfree(data);
6610 return ret;
6611}
6612
6613static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
6614{
6615 void __user *measure = (void __user *)(uintptr_t)argp->data;
6616 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
6617 struct sev_data_launch_measure *data;
6618 struct kvm_sev_launch_measure params;
6619 void __user *p = NULL;
6620 void *blob = NULL;
6621 int ret;
6622
6623 if (!sev_guest(kvm))
6624 return -ENOTTY;
6625
6626 if (copy_from_user(&params, measure, sizeof(params)))
6627 return -EFAULT;
6628
David Brazdil0f672f62019-12-10 10:32:29 +00006629 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006630 if (!data)
6631 return -ENOMEM;
6632
6633 /* User wants to query the blob length */
6634 if (!params.len)
6635 goto cmd;
6636
6637 p = (void __user *)(uintptr_t)params.uaddr;
6638 if (p) {
6639 if (params.len > SEV_FW_BLOB_MAX_SIZE) {
6640 ret = -EINVAL;
6641 goto e_free;
6642 }
6643
6644 ret = -ENOMEM;
6645 blob = kmalloc(params.len, GFP_KERNEL);
6646 if (!blob)
6647 goto e_free;
6648
6649 data->address = __psp_pa(blob);
6650 data->len = params.len;
6651 }
6652
6653cmd:
6654 data->handle = sev->handle;
6655 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error);
6656
6657 /*
6658 * If we query the session length, FW responded with expected data.
6659 */
6660 if (!params.len)
6661 goto done;
6662
6663 if (ret)
6664 goto e_free_blob;
6665
6666 if (blob) {
6667 if (copy_to_user(p, blob, params.len))
6668 ret = -EFAULT;
6669 }
6670
6671done:
6672 params.len = data->len;
6673 if (copy_to_user(measure, &params, sizeof(params)))
6674 ret = -EFAULT;
6675e_free_blob:
6676 kfree(blob);
6677e_free:
6678 kfree(data);
6679 return ret;
6680}
6681
6682static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
6683{
6684 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
6685 struct sev_data_launch_finish *data;
6686 int ret;
6687
6688 if (!sev_guest(kvm))
6689 return -ENOTTY;
6690
David Brazdil0f672f62019-12-10 10:32:29 +00006691 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006692 if (!data)
6693 return -ENOMEM;
6694
6695 data->handle = sev->handle;
6696 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error);
6697
6698 kfree(data);
6699 return ret;
6700}
6701
6702static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
6703{
6704 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
6705 struct kvm_sev_guest_status params;
6706 struct sev_data_guest_status *data;
6707 int ret;
6708
6709 if (!sev_guest(kvm))
6710 return -ENOTTY;
6711
David Brazdil0f672f62019-12-10 10:32:29 +00006712 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006713 if (!data)
6714 return -ENOMEM;
6715
6716 data->handle = sev->handle;
6717 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error);
6718 if (ret)
6719 goto e_free;
6720
6721 params.policy = data->policy;
6722 params.state = data->state;
6723 params.handle = data->handle;
6724
6725 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
6726 ret = -EFAULT;
6727e_free:
6728 kfree(data);
6729 return ret;
6730}
6731
6732static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
6733 unsigned long dst, int size,
6734 int *error, bool enc)
6735{
6736 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
6737 struct sev_data_dbg *data;
6738 int ret;
6739
David Brazdil0f672f62019-12-10 10:32:29 +00006740 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006741 if (!data)
6742 return -ENOMEM;
6743
6744 data->handle = sev->handle;
6745 data->dst_addr = dst;
6746 data->src_addr = src;
6747 data->len = size;
6748
6749 ret = sev_issue_cmd(kvm,
6750 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
6751 data, error);
6752 kfree(data);
6753 return ret;
6754}
6755
6756static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
6757 unsigned long dst_paddr, int sz, int *err)
6758{
6759 int offset;
6760
6761 /*
6762 * Its safe to read more than we are asked, caller should ensure that
6763 * destination has enough space.
6764 */
6765 src_paddr = round_down(src_paddr, 16);
6766 offset = src_paddr & 15;
6767 sz = round_up(sz + offset, 16);
6768
6769 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
6770}
6771
6772static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
6773 unsigned long __user dst_uaddr,
6774 unsigned long dst_paddr,
6775 int size, int *err)
6776{
6777 struct page *tpage = NULL;
6778 int ret, offset;
6779
6780 /* if inputs are not 16-byte then use intermediate buffer */
6781 if (!IS_ALIGNED(dst_paddr, 16) ||
6782 !IS_ALIGNED(paddr, 16) ||
6783 !IS_ALIGNED(size, 16)) {
6784 tpage = (void *)alloc_page(GFP_KERNEL);
6785 if (!tpage)
6786 return -ENOMEM;
6787
6788 dst_paddr = __sme_page_pa(tpage);
6789 }
6790
6791 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
6792 if (ret)
6793 goto e_free;
6794
6795 if (tpage) {
6796 offset = paddr & 15;
6797 if (copy_to_user((void __user *)(uintptr_t)dst_uaddr,
6798 page_address(tpage) + offset, size))
6799 ret = -EFAULT;
6800 }
6801
6802e_free:
6803 if (tpage)
6804 __free_page(tpage);
6805
6806 return ret;
6807}
6808
6809static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
6810 unsigned long __user vaddr,
6811 unsigned long dst_paddr,
6812 unsigned long __user dst_vaddr,
6813 int size, int *error)
6814{
6815 struct page *src_tpage = NULL;
6816 struct page *dst_tpage = NULL;
6817 int ret, len = size;
6818
6819 /* If source buffer is not aligned then use an intermediate buffer */
6820 if (!IS_ALIGNED(vaddr, 16)) {
6821 src_tpage = alloc_page(GFP_KERNEL);
6822 if (!src_tpage)
6823 return -ENOMEM;
6824
6825 if (copy_from_user(page_address(src_tpage),
6826 (void __user *)(uintptr_t)vaddr, size)) {
6827 __free_page(src_tpage);
6828 return -EFAULT;
6829 }
6830
6831 paddr = __sme_page_pa(src_tpage);
6832 }
6833
6834 /*
6835 * If destination buffer or length is not aligned then do read-modify-write:
6836 * - decrypt destination in an intermediate buffer
6837 * - copy the source buffer in an intermediate buffer
6838 * - use the intermediate buffer as source buffer
6839 */
6840 if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
6841 int dst_offset;
6842
6843 dst_tpage = alloc_page(GFP_KERNEL);
6844 if (!dst_tpage) {
6845 ret = -ENOMEM;
6846 goto e_free;
6847 }
6848
6849 ret = __sev_dbg_decrypt(kvm, dst_paddr,
6850 __sme_page_pa(dst_tpage), size, error);
6851 if (ret)
6852 goto e_free;
6853
6854 /*
6855 * If source is kernel buffer then use memcpy() otherwise
6856 * copy_from_user().
6857 */
6858 dst_offset = dst_paddr & 15;
6859
6860 if (src_tpage)
6861 memcpy(page_address(dst_tpage) + dst_offset,
6862 page_address(src_tpage), size);
6863 else {
6864 if (copy_from_user(page_address(dst_tpage) + dst_offset,
6865 (void __user *)(uintptr_t)vaddr, size)) {
6866 ret = -EFAULT;
6867 goto e_free;
6868 }
6869 }
6870
6871 paddr = __sme_page_pa(dst_tpage);
6872 dst_paddr = round_down(dst_paddr, 16);
6873 len = round_up(size, 16);
6874 }
6875
6876 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
6877
6878e_free:
6879 if (src_tpage)
6880 __free_page(src_tpage);
6881 if (dst_tpage)
6882 __free_page(dst_tpage);
6883 return ret;
6884}
6885
6886static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
6887{
6888 unsigned long vaddr, vaddr_end, next_vaddr;
6889 unsigned long dst_vaddr;
6890 struct page **src_p, **dst_p;
6891 struct kvm_sev_dbg debug;
6892 unsigned long n;
David Brazdil0f672f62019-12-10 10:32:29 +00006893 unsigned int size;
6894 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006895
6896 if (!sev_guest(kvm))
6897 return -ENOTTY;
6898
6899 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
6900 return -EFAULT;
6901
David Brazdil0f672f62019-12-10 10:32:29 +00006902 if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr)
6903 return -EINVAL;
6904 if (!debug.dst_uaddr)
6905 return -EINVAL;
6906
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006907 vaddr = debug.src_uaddr;
6908 size = debug.len;
6909 vaddr_end = vaddr + size;
6910 dst_vaddr = debug.dst_uaddr;
6911
6912 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
6913 int len, s_off, d_off;
6914
6915 /* lock userspace source and destination page */
6916 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
6917 if (!src_p)
6918 return -EFAULT;
6919
6920 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
6921 if (!dst_p) {
6922 sev_unpin_memory(kvm, src_p, n);
6923 return -EFAULT;
6924 }
6925
6926 /*
6927 * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the
6928 * memory content (i.e it will write the same memory region with C=1).
6929 * It's possible that the cache may contain the data with C=0, i.e.,
6930 * unencrypted so invalidate it first.
6931 */
6932 sev_clflush_pages(src_p, 1);
6933 sev_clflush_pages(dst_p, 1);
6934
6935 /*
6936 * Since user buffer may not be page aligned, calculate the
6937 * offset within the page.
6938 */
6939 s_off = vaddr & ~PAGE_MASK;
6940 d_off = dst_vaddr & ~PAGE_MASK;
6941 len = min_t(size_t, (PAGE_SIZE - s_off), size);
6942
6943 if (dec)
6944 ret = __sev_dbg_decrypt_user(kvm,
6945 __sme_page_pa(src_p[0]) + s_off,
6946 dst_vaddr,
6947 __sme_page_pa(dst_p[0]) + d_off,
6948 len, &argp->error);
6949 else
6950 ret = __sev_dbg_encrypt_user(kvm,
6951 __sme_page_pa(src_p[0]) + s_off,
6952 vaddr,
6953 __sme_page_pa(dst_p[0]) + d_off,
6954 dst_vaddr,
6955 len, &argp->error);
6956
David Brazdil0f672f62019-12-10 10:32:29 +00006957 sev_unpin_memory(kvm, src_p, n);
6958 sev_unpin_memory(kvm, dst_p, n);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006959
6960 if (ret)
6961 goto err;
6962
6963 next_vaddr = vaddr + len;
6964 dst_vaddr = dst_vaddr + len;
6965 size -= len;
6966 }
6967err:
6968 return ret;
6969}
6970
6971static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
6972{
6973 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
6974 struct sev_data_launch_secret *data;
6975 struct kvm_sev_launch_secret params;
6976 struct page **pages;
6977 void *blob, *hdr;
6978 unsigned long n;
6979 int ret, offset;
6980
6981 if (!sev_guest(kvm))
6982 return -ENOTTY;
6983
6984 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6985 return -EFAULT;
6986
6987 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
6988 if (!pages)
6989 return -ENOMEM;
6990
6991 /*
6992 * The secret must be copied into contiguous memory region, lets verify
6993 * that userspace memory pages are contiguous before we issue command.
6994 */
6995 if (get_num_contig_pages(0, pages, n) != n) {
6996 ret = -EINVAL;
6997 goto e_unpin_memory;
6998 }
6999
7000 ret = -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +00007001 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007002 if (!data)
7003 goto e_unpin_memory;
7004
7005 offset = params.guest_uaddr & (PAGE_SIZE - 1);
7006 data->guest_address = __sme_page_pa(pages[0]) + offset;
7007 data->guest_len = params.guest_len;
7008
7009 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
7010 if (IS_ERR(blob)) {
7011 ret = PTR_ERR(blob);
7012 goto e_free;
7013 }
7014
7015 data->trans_address = __psp_pa(blob);
7016 data->trans_len = params.trans_len;
7017
7018 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
7019 if (IS_ERR(hdr)) {
7020 ret = PTR_ERR(hdr);
7021 goto e_free_blob;
7022 }
7023 data->hdr_address = __psp_pa(hdr);
7024 data->hdr_len = params.hdr_len;
7025
7026 data->handle = sev->handle;
7027 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error);
7028
7029 kfree(hdr);
7030
7031e_free_blob:
7032 kfree(blob);
7033e_free:
7034 kfree(data);
7035e_unpin_memory:
7036 sev_unpin_memory(kvm, pages, n);
7037 return ret;
7038}
7039
7040static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
7041{
7042 struct kvm_sev_cmd sev_cmd;
7043 int r;
7044
7045 if (!svm_sev_enabled())
7046 return -ENOTTY;
7047
7048 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
7049 return -EFAULT;
7050
7051 mutex_lock(&kvm->lock);
7052
7053 switch (sev_cmd.id) {
7054 case KVM_SEV_INIT:
7055 r = sev_guest_init(kvm, &sev_cmd);
7056 break;
7057 case KVM_SEV_LAUNCH_START:
7058 r = sev_launch_start(kvm, &sev_cmd);
7059 break;
7060 case KVM_SEV_LAUNCH_UPDATE_DATA:
7061 r = sev_launch_update_data(kvm, &sev_cmd);
7062 break;
7063 case KVM_SEV_LAUNCH_MEASURE:
7064 r = sev_launch_measure(kvm, &sev_cmd);
7065 break;
7066 case KVM_SEV_LAUNCH_FINISH:
7067 r = sev_launch_finish(kvm, &sev_cmd);
7068 break;
7069 case KVM_SEV_GUEST_STATUS:
7070 r = sev_guest_status(kvm, &sev_cmd);
7071 break;
7072 case KVM_SEV_DBG_DECRYPT:
7073 r = sev_dbg_crypt(kvm, &sev_cmd, true);
7074 break;
7075 case KVM_SEV_DBG_ENCRYPT:
7076 r = sev_dbg_crypt(kvm, &sev_cmd, false);
7077 break;
7078 case KVM_SEV_LAUNCH_SECRET:
7079 r = sev_launch_secret(kvm, &sev_cmd);
7080 break;
7081 default:
7082 r = -EINVAL;
7083 goto out;
7084 }
7085
7086 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
7087 r = -EFAULT;
7088
7089out:
7090 mutex_unlock(&kvm->lock);
7091 return r;
7092}
7093
7094static int svm_register_enc_region(struct kvm *kvm,
7095 struct kvm_enc_region *range)
7096{
7097 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
7098 struct enc_region *region;
7099 int ret = 0;
7100
7101 if (!sev_guest(kvm))
7102 return -ENOTTY;
7103
7104 if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
7105 return -EINVAL;
7106
David Brazdil0f672f62019-12-10 10:32:29 +00007107 region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007108 if (!region)
7109 return -ENOMEM;
7110
Olivier Deprez0e641232021-09-23 10:07:05 +02007111 mutex_lock(&kvm->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007112 region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
7113 if (!region->pages) {
7114 ret = -ENOMEM;
Olivier Deprez0e641232021-09-23 10:07:05 +02007115 mutex_unlock(&kvm->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007116 goto e_free;
7117 }
7118
Olivier Deprez0e641232021-09-23 10:07:05 +02007119 region->uaddr = range->addr;
7120 region->size = range->size;
7121
7122 list_add_tail(&region->list, &sev->regions_list);
7123 mutex_unlock(&kvm->lock);
7124
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007125 /*
7126 * The guest may change the memory encryption attribute from C=0 -> C=1
7127 * or vice versa for this memory range. Lets make sure caches are
7128 * flushed to ensure that guest data gets written into memory with
7129 * correct C-bit.
7130 */
7131 sev_clflush_pages(region->pages, region->npages);
7132
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007133 return ret;
7134
7135e_free:
7136 kfree(region);
7137 return ret;
7138}
7139
7140static struct enc_region *
7141find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
7142{
7143 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
7144 struct list_head *head = &sev->regions_list;
7145 struct enc_region *i;
7146
7147 list_for_each_entry(i, head, list) {
7148 if (i->uaddr == range->addr &&
7149 i->size == range->size)
7150 return i;
7151 }
7152
7153 return NULL;
7154}
7155
7156
7157static int svm_unregister_enc_region(struct kvm *kvm,
7158 struct kvm_enc_region *range)
7159{
7160 struct enc_region *region;
7161 int ret;
7162
7163 mutex_lock(&kvm->lock);
7164
7165 if (!sev_guest(kvm)) {
7166 ret = -ENOTTY;
7167 goto failed;
7168 }
7169
7170 region = find_enc_region(kvm, range);
7171 if (!region) {
7172 ret = -EINVAL;
7173 goto failed;
7174 }
7175
7176 __unregister_enc_region_locked(kvm, region);
7177
7178 mutex_unlock(&kvm->lock);
7179 return 0;
7180
7181failed:
7182 mutex_unlock(&kvm->lock);
7183 return ret;
7184}
7185
David Brazdil0f672f62019-12-10 10:32:29 +00007186static bool svm_need_emulation_on_page_fault(struct kvm_vcpu *vcpu)
7187{
7188 unsigned long cr4 = kvm_read_cr4(vcpu);
7189 bool smep = cr4 & X86_CR4_SMEP;
7190 bool smap = cr4 & X86_CR4_SMAP;
7191 bool is_user = svm_get_cpl(vcpu) == 3;
7192
7193 /*
7194 * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
7195 *
7196 * Errata:
7197 * When CPU raise #NPF on guest data access and vCPU CR4.SMAP=1, it is
7198 * possible that CPU microcode implementing DecodeAssist will fail
7199 * to read bytes of instruction which caused #NPF. In this case,
7200 * GuestIntrBytes field of the VMCB on a VMEXIT will incorrectly
7201 * return 0 instead of the correct guest instruction bytes.
7202 *
7203 * This happens because CPU microcode reading instruction bytes
7204 * uses a special opcode which attempts to read data using CPL=0
7205 * priviledges. The microcode reads CS:RIP and if it hits a SMAP
7206 * fault, it gives up and returns no instruction bytes.
7207 *
7208 * Detection:
7209 * We reach here in case CPU supports DecodeAssist, raised #NPF and
7210 * returned 0 in GuestIntrBytes field of the VMCB.
7211 * First, errata can only be triggered in case vCPU CR4.SMAP=1.
7212 * Second, if vCPU CR4.SMEP=1, errata could only be triggered
7213 * in case vCPU CPL==3 (Because otherwise guest would have triggered
7214 * a SMEP fault instead of #NPF).
7215 * Otherwise, vCPU CR4.SMEP=0, errata could be triggered by any vCPU CPL.
7216 * As most guests enable SMAP if they have also enabled SMEP, use above
7217 * logic in order to attempt minimize false-positive of detecting errata
7218 * while still preserving all cases semantic correctness.
7219 *
7220 * Workaround:
7221 * To determine what instruction the guest was executing, the hypervisor
7222 * will have to decode the instruction at the instruction pointer.
7223 *
7224 * In non SEV guest, hypervisor will be able to read the guest
7225 * memory to decode the instruction pointer when insn_len is zero
7226 * so we return true to indicate that decoding is possible.
7227 *
7228 * But in the SEV guest, the guest memory is encrypted with the
7229 * guest specific key and hypervisor will not be able to decode the
7230 * instruction pointer so we will not able to workaround it. Lets
7231 * print the error and request to kill the guest.
7232 */
7233 if (smap && (!smep || is_user)) {
7234 if (!sev_guest(vcpu->kvm))
7235 return true;
7236
7237 pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n");
7238 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7239 }
7240
7241 return false;
7242}
7243
7244static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
7245{
7246 struct vcpu_svm *svm = to_svm(vcpu);
7247
7248 /*
7249 * TODO: Last condition latch INIT signals on vCPU when
7250 * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
7251 * To properly emulate the INIT intercept, SVM should implement
7252 * kvm_x86_ops->check_nested_events() and call nested_svm_vmexit()
7253 * there if an INIT signal is pending.
7254 */
7255 return !gif_set(svm) ||
7256 (svm->vmcb->control.intercept & (1ULL << INTERCEPT_INIT));
7257}
7258
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007259static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
7260 .cpu_has_kvm_support = has_svm,
7261 .disabled_by_bios = is_disabled,
7262 .hardware_setup = svm_hardware_setup,
7263 .hardware_unsetup = svm_hardware_unsetup,
7264 .check_processor_compatibility = svm_check_processor_compat,
7265 .hardware_enable = svm_hardware_enable,
7266 .hardware_disable = svm_hardware_disable,
7267 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
7268 .has_emulated_msr = svm_has_emulated_msr,
7269
7270 .vcpu_create = svm_create_vcpu,
7271 .vcpu_free = svm_free_vcpu,
7272 .vcpu_reset = svm_vcpu_reset,
7273
7274 .vm_alloc = svm_vm_alloc,
7275 .vm_free = svm_vm_free,
7276 .vm_init = avic_vm_init,
7277 .vm_destroy = svm_vm_destroy,
7278
7279 .prepare_guest_switch = svm_prepare_guest_switch,
7280 .vcpu_load = svm_vcpu_load,
7281 .vcpu_put = svm_vcpu_put,
7282 .vcpu_blocking = svm_vcpu_blocking,
7283 .vcpu_unblocking = svm_vcpu_unblocking,
7284
7285 .update_bp_intercept = update_bp_intercept,
7286 .get_msr_feature = svm_get_msr_feature,
7287 .get_msr = svm_get_msr,
7288 .set_msr = svm_set_msr,
7289 .get_segment_base = svm_get_segment_base,
7290 .get_segment = svm_get_segment,
7291 .set_segment = svm_set_segment,
7292 .get_cpl = svm_get_cpl,
7293 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
7294 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
7295 .decache_cr3 = svm_decache_cr3,
7296 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
7297 .set_cr0 = svm_set_cr0,
7298 .set_cr3 = svm_set_cr3,
7299 .set_cr4 = svm_set_cr4,
7300 .set_efer = svm_set_efer,
7301 .get_idt = svm_get_idt,
7302 .set_idt = svm_set_idt,
7303 .get_gdt = svm_get_gdt,
7304 .set_gdt = svm_set_gdt,
7305 .get_dr6 = svm_get_dr6,
7306 .set_dr6 = svm_set_dr6,
7307 .set_dr7 = svm_set_dr7,
7308 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
7309 .cache_reg = svm_cache_reg,
7310 .get_rflags = svm_get_rflags,
7311 .set_rflags = svm_set_rflags,
7312
7313 .tlb_flush = svm_flush_tlb,
7314 .tlb_flush_gva = svm_flush_tlb_gva,
7315
7316 .run = svm_vcpu_run,
7317 .handle_exit = handle_exit,
7318 .skip_emulated_instruction = skip_emulated_instruction,
7319 .set_interrupt_shadow = svm_set_interrupt_shadow,
7320 .get_interrupt_shadow = svm_get_interrupt_shadow,
7321 .patch_hypercall = svm_patch_hypercall,
7322 .set_irq = svm_set_irq,
7323 .set_nmi = svm_inject_nmi,
7324 .queue_exception = svm_queue_exception,
7325 .cancel_injection = svm_cancel_injection,
7326 .interrupt_allowed = svm_interrupt_allowed,
7327 .nmi_allowed = svm_nmi_allowed,
7328 .get_nmi_mask = svm_get_nmi_mask,
7329 .set_nmi_mask = svm_set_nmi_mask,
7330 .enable_nmi_window = enable_nmi_window,
7331 .enable_irq_window = enable_irq_window,
7332 .update_cr8_intercept = update_cr8_intercept,
7333 .set_virtual_apic_mode = svm_set_virtual_apic_mode,
7334 .get_enable_apicv = svm_get_enable_apicv,
7335 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
7336 .load_eoi_exitmap = svm_load_eoi_exitmap,
7337 .hwapic_irr_update = svm_hwapic_irr_update,
7338 .hwapic_isr_update = svm_hwapic_isr_update,
7339 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
7340 .apicv_post_state_restore = avic_post_state_restore,
7341
7342 .set_tss_addr = svm_set_tss_addr,
7343 .set_identity_map_addr = svm_set_identity_map_addr,
7344 .get_tdp_level = get_npt_level,
7345 .get_mt_mask = svm_get_mt_mask,
7346
7347 .get_exit_info = svm_get_exit_info,
7348
7349 .get_lpage_level = svm_get_lpage_level,
7350
7351 .cpuid_update = svm_cpuid_update,
7352
7353 .rdtscp_supported = svm_rdtscp_supported,
7354 .invpcid_supported = svm_invpcid_supported,
7355 .mpx_supported = svm_mpx_supported,
7356 .xsaves_supported = svm_xsaves_supported,
7357 .umip_emulated = svm_umip_emulated,
David Brazdil0f672f62019-12-10 10:32:29 +00007358 .pt_supported = svm_pt_supported,
Olivier Deprez0e641232021-09-23 10:07:05 +02007359 .pku_supported = svm_pku_supported,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007360
7361 .set_supported_cpuid = svm_set_supported_cpuid,
7362
7363 .has_wbinvd_exit = svm_has_wbinvd_exit,
7364
7365 .read_l1_tsc_offset = svm_read_l1_tsc_offset,
7366 .write_l1_tsc_offset = svm_write_l1_tsc_offset,
7367
7368 .set_tdp_cr3 = set_tdp_cr3,
7369
7370 .check_intercept = svm_check_intercept,
David Brazdil0f672f62019-12-10 10:32:29 +00007371 .handle_exit_irqoff = svm_handle_exit_irqoff,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007372
7373 .request_immediate_exit = __kvm_request_immediate_exit,
7374
7375 .sched_in = svm_sched_in,
7376
7377 .pmu_ops = &amd_pmu_ops,
7378 .deliver_posted_interrupt = svm_deliver_avic_intr,
David Brazdil0f672f62019-12-10 10:32:29 +00007379 .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007380 .update_pi_irte = svm_update_pi_irte,
7381 .setup_mce = svm_setup_mce,
7382
7383 .smi_allowed = svm_smi_allowed,
7384 .pre_enter_smm = svm_pre_enter_smm,
7385 .pre_leave_smm = svm_pre_leave_smm,
7386 .enable_smi_window = enable_smi_window,
7387
7388 .mem_enc_op = svm_mem_enc_op,
7389 .mem_enc_reg_region = svm_register_enc_region,
7390 .mem_enc_unreg_region = svm_unregister_enc_region,
David Brazdil0f672f62019-12-10 10:32:29 +00007391
7392 .nested_enable_evmcs = NULL,
7393 .nested_get_evmcs_version = NULL,
7394
7395 .need_emulation_on_page_fault = svm_need_emulation_on_page_fault,
7396
7397 .apic_init_signal_blocked = svm_apic_init_signal_blocked,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007398};
7399
7400static int __init svm_init(void)
7401{
7402 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
7403 __alignof__(struct vcpu_svm), THIS_MODULE);
7404}
7405
7406static void __exit svm_exit(void)
7407{
7408 kvm_exit();
7409}
7410
7411module_init(svm_init)
7412module_exit(svm_exit)