blob: 3f9b48732aea689943142ebde8a2e8d8725f954a [file] [log] [blame]
Joerg Roedelef0f6492020-03-31 12:17:38 -04001// SPDX-License-Identifier: GPL-2.0-only
2/*
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>
13 */
14
15#define pr_fmt(fmt) "SVM: " fmt
16
17#include <linux/kvm_types.h>
18#include <linux/hashtable.h>
19#include <linux/amd-iommu.h>
20#include <linux/kvm_host.h>
21
22#include <asm/irq_remapping.h>
23
24#include "trace.h"
25#include "lapic.h"
26#include "x86.h"
27#include "irq.h"
28#include "svm.h"
29
Joerg Roedelef0f6492020-03-31 12:17:38 -040030/* AVIC GATAG is encoded using VM and VCPU IDs */
31#define AVIC_VCPU_ID_BITS 8
32#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1)
33
34#define AVIC_VM_ID_BITS 24
35#define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS)
36#define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1)
37
38#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \
39 (y & AVIC_VCPU_ID_MASK))
40#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
41#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
42
43/* Note:
44 * This hash table is used to map VM_ID to a struct kvm_svm,
45 * when handling AMD IOMMU GALOG notification to schedule in
46 * a particular vCPU.
47 */
48#define SVM_VM_DATA_HASH_BITS 8
49static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
50static u32 next_vm_id = 0;
51static bool next_vm_id_wrapped = 0;
52static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
53
54/*
55 * This is a wrapper of struct amd_iommu_ir_data.
56 */
57struct amd_svm_iommu_ir {
58 struct list_head node; /* Used by SVM for per-vcpu ir_list */
59 void *data; /* Storing pointer to struct amd_ir_data */
60};
61
Joerg Roedelef0f6492020-03-31 12:17:38 -040062
63/* Note:
64 * This function is called from IOMMU driver to notify
65 * SVM to schedule in a particular vCPU of a particular VM.
66 */
67int avic_ga_log_notifier(u32 ga_tag)
68{
69 unsigned long flags;
70 struct kvm_svm *kvm_svm;
71 struct kvm_vcpu *vcpu = NULL;
72 u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
73 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
74
75 pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
76 trace_kvm_avic_ga_log(vm_id, vcpu_id);
77
78 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
79 hash_for_each_possible(svm_vm_data_hash, kvm_svm, hnode, vm_id) {
80 if (kvm_svm->avic_vm_id != vm_id)
81 continue;
82 vcpu = kvm_get_vcpu_by_id(&kvm_svm->kvm, vcpu_id);
83 break;
84 }
85 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
86
87 /* Note:
88 * At this point, the IOMMU should have already set the pending
89 * bit in the vAPIC backing page. So, we just need to schedule
90 * in the vcpu.
91 */
92 if (vcpu)
93 kvm_vcpu_wake_up(vcpu);
94
95 return 0;
96}
97
98void avic_vm_destroy(struct kvm *kvm)
99{
100 unsigned long flags;
101 struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
102
Vitaly Kuznetsovfdf513e2021-06-09 17:09:08 +0200103 if (!enable_apicv)
Joerg Roedelef0f6492020-03-31 12:17:38 -0400104 return;
105
106 if (kvm_svm->avic_logical_id_table_page)
107 __free_page(kvm_svm->avic_logical_id_table_page);
108 if (kvm_svm->avic_physical_id_table_page)
109 __free_page(kvm_svm->avic_physical_id_table_page);
110
111 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
112 hash_del(&kvm_svm->hnode);
113 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
114}
115
116int avic_vm_init(struct kvm *kvm)
117{
118 unsigned long flags;
119 int err = -ENOMEM;
120 struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
121 struct kvm_svm *k2;
122 struct page *p_page;
123 struct page *l_page;
124 u32 vm_id;
125
Vitaly Kuznetsovfdf513e2021-06-09 17:09:08 +0200126 if (!enable_apicv)
Joerg Roedelef0f6492020-03-31 12:17:38 -0400127 return 0;
128
129 /* Allocating physical APIC ID table (4KB) */
Haiwei Liae5a2a32020-09-16 16:36:21 +0800130 p_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400131 if (!p_page)
132 goto free_avic;
133
134 kvm_svm->avic_physical_id_table_page = p_page;
Joerg Roedelef0f6492020-03-31 12:17:38 -0400135
136 /* Allocating logical APIC ID table (4KB) */
Haiwei Liae5a2a32020-09-16 16:36:21 +0800137 l_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400138 if (!l_page)
139 goto free_avic;
140
141 kvm_svm->avic_logical_id_table_page = l_page;
Joerg Roedelef0f6492020-03-31 12:17:38 -0400142
143 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
144 again:
145 vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
146 if (vm_id == 0) { /* id is 1-based, zero is not okay */
147 next_vm_id_wrapped = 1;
148 goto again;
149 }
150 /* Is it still in use? Only possible if wrapped at least once */
151 if (next_vm_id_wrapped) {
152 hash_for_each_possible(svm_vm_data_hash, k2, hnode, vm_id) {
153 if (k2->avic_vm_id == vm_id)
154 goto again;
155 }
156 }
157 kvm_svm->avic_vm_id = vm_id;
158 hash_add(svm_vm_data_hash, &kvm_svm->hnode, kvm_svm->avic_vm_id);
159 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
160
161 return 0;
162
163free_avic:
164 avic_vm_destroy(kvm);
165 return err;
166}
167
168void avic_init_vmcb(struct vcpu_svm *svm)
169{
170 struct vmcb *vmcb = svm->vmcb;
171 struct kvm_svm *kvm_svm = to_kvm_svm(svm->vcpu.kvm);
172 phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page));
173 phys_addr_t lpa = __sme_set(page_to_phys(kvm_svm->avic_logical_id_table_page));
174 phys_addr_t ppa = __sme_set(page_to_phys(kvm_svm->avic_physical_id_table_page));
175
176 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
177 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
178 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
179 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
Maxim Levitsky73143032021-08-10 23:52:51 +0300180 vmcb->control.avic_vapic_bar = APIC_DEFAULT_PHYS_BASE & VMCB_AVIC_APIC_BAR_MASK;
181
Joerg Roedelef0f6492020-03-31 12:17:38 -0400182 if (kvm_apicv_activated(svm->vcpu.kvm))
183 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
184 else
185 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
186}
187
188static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
189 unsigned int index)
190{
191 u64 *avic_physical_id_table;
192 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
193
194 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
195 return NULL;
196
197 avic_physical_id_table = page_address(kvm_svm->avic_physical_id_table_page);
198
199 return &avic_physical_id_table[index];
200}
201
ChenXiaoSong02ffbe62021-06-09 20:22:17 +0800202/*
Joerg Roedelef0f6492020-03-31 12:17:38 -0400203 * Note:
204 * AVIC hardware walks the nested page table to check permissions,
205 * but does not use the SPA address specified in the leaf page
206 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
207 * field of the VMCB. Therefore, we set up the
208 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
209 */
Maxim Levitsky36222b12021-08-10 23:52:43 +0300210static int avic_alloc_access_page(struct kvm *kvm)
Joerg Roedelef0f6492020-03-31 12:17:38 -0400211{
Peter Xuff5a9832020-09-30 21:20:33 -0400212 void __user *ret;
213 int r = 0;
Joerg Roedelef0f6492020-03-31 12:17:38 -0400214
215 mutex_lock(&kvm->slots_lock);
Maxim Levitsky36222b12021-08-10 23:52:43 +0300216
217 if (kvm->arch.apic_access_memslot_enabled)
Joerg Roedelef0f6492020-03-31 12:17:38 -0400218 goto out;
219
220 ret = __x86_set_memory_region(kvm,
221 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
222 APIC_DEFAULT_PHYS_BASE,
Maxim Levitsky36222b12021-08-10 23:52:43 +0300223 PAGE_SIZE);
Peter Xuff5a9832020-09-30 21:20:33 -0400224 if (IS_ERR(ret)) {
225 r = PTR_ERR(ret);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400226 goto out;
Peter Xuff5a9832020-09-30 21:20:33 -0400227 }
Joerg Roedelef0f6492020-03-31 12:17:38 -0400228
Maxim Levitsky36222b12021-08-10 23:52:43 +0300229 kvm->arch.apic_access_memslot_enabled = true;
Joerg Roedelef0f6492020-03-31 12:17:38 -0400230out:
231 mutex_unlock(&kvm->slots_lock);
Peter Xuff5a9832020-09-30 21:20:33 -0400232 return r;
Joerg Roedelef0f6492020-03-31 12:17:38 -0400233}
234
235static int avic_init_backing_page(struct kvm_vcpu *vcpu)
236{
237 u64 *entry, new_entry;
238 int id = vcpu->vcpu_id;
239 struct vcpu_svm *svm = to_svm(vcpu);
240
241 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
242 return -EINVAL;
243
Paolo Bonzini63129752021-03-02 14:40:39 -0500244 if (!vcpu->arch.apic->regs)
Joerg Roedelef0f6492020-03-31 12:17:38 -0400245 return -EINVAL;
246
247 if (kvm_apicv_activated(vcpu->kvm)) {
248 int ret;
249
Maxim Levitsky36222b12021-08-10 23:52:43 +0300250 ret = avic_alloc_access_page(vcpu->kvm);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400251 if (ret)
252 return ret;
253 }
254
Paolo Bonzini63129752021-03-02 14:40:39 -0500255 svm->avic_backing_page = virt_to_page(vcpu->arch.apic->regs);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400256
257 /* Setting AVIC backing page address in the phy APIC ID table */
258 entry = avic_get_physical_id_entry(vcpu, id);
259 if (!entry)
260 return -EINVAL;
261
262 new_entry = __sme_set((page_to_phys(svm->avic_backing_page) &
263 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
264 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK);
265 WRITE_ONCE(*entry, new_entry);
266
267 svm->avic_physical_id_cache = entry;
268
269 return 0;
270}
271
Sean Christophersone6c804a2021-02-04 16:57:42 -0800272static void avic_kick_target_vcpus(struct kvm *kvm, struct kvm_lapic *source,
273 u32 icrl, u32 icrh)
274{
275 struct kvm_vcpu *vcpu;
Marc Zyngier46808a42021-11-16 16:04:02 +0000276 unsigned long i;
Sean Christophersone6c804a2021-02-04 16:57:42 -0800277
Sean Christopherson202470d2021-12-08 01:52:21 +0000278 /*
279 * Wake any target vCPUs that are blocking, i.e. waiting for a wake
280 * event. There's no need to signal doorbells, as hardware has handled
281 * vCPUs that were in guest at the time of the IPI, and vCPUs that have
282 * since entered the guest will have processed pending IRQs at VMRUN.
283 */
Sean Christophersone6c804a2021-02-04 16:57:42 -0800284 kvm_for_each_vcpu(i, vcpu, kvm) {
Sean Christopherson202470d2021-12-08 01:52:21 +0000285 if (kvm_apic_match_dest(vcpu, source, icrl & APIC_SHORT_MASK,
286 GET_APIC_DEST_FIELD(icrh),
287 icrl & APIC_DEST_MASK))
Sean Christophersone6c804a2021-02-04 16:57:42 -0800288 kvm_vcpu_wake_up(vcpu);
289 }
290}
291
Paolo Bonzini63129752021-03-02 14:40:39 -0500292int avic_incomplete_ipi_interception(struct kvm_vcpu *vcpu)
Joerg Roedelef0f6492020-03-31 12:17:38 -0400293{
Paolo Bonzini63129752021-03-02 14:40:39 -0500294 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400295 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
296 u32 icrl = svm->vmcb->control.exit_info_1;
297 u32 id = svm->vmcb->control.exit_info_2 >> 32;
298 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
Paolo Bonzini63129752021-03-02 14:40:39 -0500299 struct kvm_lapic *apic = vcpu->arch.apic;
Joerg Roedelef0f6492020-03-31 12:17:38 -0400300
Paolo Bonzini63129752021-03-02 14:40:39 -0500301 trace_kvm_avic_incomplete_ipi(vcpu->vcpu_id, icrh, icrl, id, index);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400302
303 switch (id) {
304 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
305 /*
306 * AVIC hardware handles the generation of
307 * IPIs when the specified Message Type is Fixed
308 * (also known as fixed delivery mode) and
309 * the Trigger Mode is edge-triggered. The hardware
310 * also supports self and broadcast delivery modes
311 * specified via the Destination Shorthand(DSH)
312 * field of the ICRL. Logical and physical APIC ID
313 * formats are supported. All other IPI types cause
314 * a #VMEXIT, which needs to emulated.
315 */
316 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
317 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
318 break;
Sean Christophersone6c804a2021-02-04 16:57:42 -0800319 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING:
Joerg Roedelef0f6492020-03-31 12:17:38 -0400320 /*
321 * At this point, we expect that the AVIC HW has already
322 * set the appropriate IRR bits on the valid target
323 * vcpus. So, we just need to kick the appropriate vcpu.
324 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500325 avic_kick_target_vcpus(vcpu->kvm, apic, icrl, icrh);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400326 break;
Joerg Roedelef0f6492020-03-31 12:17:38 -0400327 case AVIC_IPI_FAILURE_INVALID_TARGET:
Joerg Roedelef0f6492020-03-31 12:17:38 -0400328 break;
329 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
330 WARN_ONCE(1, "Invalid backing page\n");
331 break;
332 default:
333 pr_err("Unknown IPI interception\n");
334 }
335
336 return 1;
337}
338
339static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
340{
341 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
342 int index;
343 u32 *logical_apic_id_table;
344 int dlid = GET_APIC_LOGICAL_ID(ldr);
345
346 if (!dlid)
347 return NULL;
348
349 if (flat) { /* flat */
350 index = ffs(dlid) - 1;
351 if (index > 7)
352 return NULL;
353 } else { /* cluster */
354 int cluster = (dlid & 0xf0) >> 4;
355 int apic = ffs(dlid & 0x0f) - 1;
356
357 if ((apic < 0) || (apic > 7) ||
358 (cluster >= 0xf))
359 return NULL;
360 index = (cluster << 2) + apic;
361 }
362
363 logical_apic_id_table = (u32 *) page_address(kvm_svm->avic_logical_id_table_page);
364
365 return &logical_apic_id_table[index];
366}
367
368static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr)
369{
370 bool flat;
371 u32 *entry, new_entry;
372
373 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
374 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
375 if (!entry)
376 return -EINVAL;
377
378 new_entry = READ_ONCE(*entry);
379 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
380 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
381 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
382 WRITE_ONCE(*entry, new_entry);
383
384 return 0;
385}
386
387static void avic_invalidate_logical_id_entry(struct kvm_vcpu *vcpu)
388{
389 struct vcpu_svm *svm = to_svm(vcpu);
390 bool flat = svm->dfr_reg == APIC_DFR_FLAT;
391 u32 *entry = avic_get_logical_id_entry(vcpu, svm->ldr_reg, flat);
392
393 if (entry)
394 clear_bit(AVIC_LOGICAL_ID_ENTRY_VALID_BIT, (unsigned long *)entry);
395}
396
397static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
398{
399 int ret = 0;
400 struct vcpu_svm *svm = to_svm(vcpu);
401 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
402 u32 id = kvm_xapic_id(vcpu->arch.apic);
403
404 if (ldr == svm->ldr_reg)
405 return 0;
406
407 avic_invalidate_logical_id_entry(vcpu);
408
409 if (ldr)
410 ret = avic_ldr_write(vcpu, id, ldr);
411
412 if (!ret)
413 svm->ldr_reg = ldr;
414
415 return ret;
416}
417
418static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
419{
420 u64 *old, *new;
421 struct vcpu_svm *svm = to_svm(vcpu);
422 u32 id = kvm_xapic_id(vcpu->arch.apic);
423
424 if (vcpu->vcpu_id == id)
425 return 0;
426
427 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
428 new = avic_get_physical_id_entry(vcpu, id);
429 if (!new || !old)
430 return 1;
431
432 /* We need to move physical_id_entry to new offset */
433 *new = *old;
434 *old = 0ULL;
435 to_svm(vcpu)->avic_physical_id_cache = new;
436
437 /*
438 * Also update the guest physical APIC ID in the logical
439 * APIC ID table entry if already setup the LDR.
440 */
441 if (svm->ldr_reg)
442 avic_handle_ldr_update(vcpu);
443
444 return 0;
445}
446
447static void avic_handle_dfr_update(struct kvm_vcpu *vcpu)
448{
449 struct vcpu_svm *svm = to_svm(vcpu);
450 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
451
452 if (svm->dfr_reg == dfr)
453 return;
454
455 avic_invalidate_logical_id_entry(vcpu);
456 svm->dfr_reg = dfr;
457}
458
459static int avic_unaccel_trap_write(struct vcpu_svm *svm)
460{
461 struct kvm_lapic *apic = svm->vcpu.arch.apic;
462 u32 offset = svm->vmcb->control.exit_info_1 &
463 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
464
465 switch (offset) {
466 case APIC_ID:
467 if (avic_handle_apic_id_update(&svm->vcpu))
468 return 0;
469 break;
470 case APIC_LDR:
471 if (avic_handle_ldr_update(&svm->vcpu))
472 return 0;
473 break;
474 case APIC_DFR:
475 avic_handle_dfr_update(&svm->vcpu);
476 break;
477 default:
478 break;
479 }
480
481 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
482
483 return 1;
484}
485
486static bool is_avic_unaccelerated_access_trap(u32 offset)
487{
488 bool ret = false;
489
490 switch (offset) {
491 case APIC_ID:
492 case APIC_EOI:
493 case APIC_RRR:
494 case APIC_LDR:
495 case APIC_DFR:
496 case APIC_SPIV:
497 case APIC_ESR:
498 case APIC_ICR:
499 case APIC_LVTT:
500 case APIC_LVTTHMR:
501 case APIC_LVTPC:
502 case APIC_LVT0:
503 case APIC_LVT1:
504 case APIC_LVTERR:
505 case APIC_TMICT:
506 case APIC_TDCR:
507 ret = true;
508 break;
509 default:
510 break;
511 }
512 return ret;
513}
514
Paolo Bonzini63129752021-03-02 14:40:39 -0500515int avic_unaccelerated_access_interception(struct kvm_vcpu *vcpu)
Joerg Roedelef0f6492020-03-31 12:17:38 -0400516{
Paolo Bonzini63129752021-03-02 14:40:39 -0500517 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400518 int ret = 0;
519 u32 offset = svm->vmcb->control.exit_info_1 &
520 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
521 u32 vector = svm->vmcb->control.exit_info_2 &
522 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
523 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
524 AVIC_UNACCEL_ACCESS_WRITE_MASK;
525 bool trap = is_avic_unaccelerated_access_trap(offset);
526
Paolo Bonzini63129752021-03-02 14:40:39 -0500527 trace_kvm_avic_unaccelerated_access(vcpu->vcpu_id, offset,
Joerg Roedelef0f6492020-03-31 12:17:38 -0400528 trap, write, vector);
529 if (trap) {
530 /* Handling Trap */
531 WARN_ONCE(!write, "svm: Handling trap read.\n");
532 ret = avic_unaccel_trap_write(svm);
533 } else {
534 /* Handling Fault */
Paolo Bonzini63129752021-03-02 14:40:39 -0500535 ret = kvm_emulate_instruction(vcpu, 0);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400536 }
537
538 return ret;
539}
540
541int avic_init_vcpu(struct vcpu_svm *svm)
542{
543 int ret;
544 struct kvm_vcpu *vcpu = &svm->vcpu;
545
Vitaly Kuznetsovfdf513e2021-06-09 17:09:08 +0200546 if (!enable_apicv || !irqchip_in_kernel(vcpu->kvm))
Joerg Roedelef0f6492020-03-31 12:17:38 -0400547 return 0;
548
Paolo Bonzini63129752021-03-02 14:40:39 -0500549 ret = avic_init_backing_page(vcpu);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400550 if (ret)
551 return ret;
552
553 INIT_LIST_HEAD(&svm->ir_list);
554 spin_lock_init(&svm->ir_list_lock);
555 svm->dfr_reg = APIC_DFR_FLAT;
556
557 return ret;
558}
559
560void avic_post_state_restore(struct kvm_vcpu *vcpu)
561{
562 if (avic_handle_apic_id_update(vcpu) != 0)
563 return;
564 avic_handle_dfr_update(vcpu);
565 avic_handle_ldr_update(vcpu);
566}
567
Joerg Roedelef0f6492020-03-31 12:17:38 -0400568void svm_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
569{
570 return;
571}
572
573void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
574{
575}
576
577void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
578{
579}
580
581static int svm_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate)
582{
583 int ret = 0;
584 unsigned long flags;
585 struct amd_svm_iommu_ir *ir;
586 struct vcpu_svm *svm = to_svm(vcpu);
587
588 if (!kvm_arch_has_assigned_device(vcpu->kvm))
589 return 0;
590
591 /*
592 * Here, we go through the per-vcpu ir_list to update all existing
593 * interrupt remapping table entry targeting this vcpu.
594 */
595 spin_lock_irqsave(&svm->ir_list_lock, flags);
596
597 if (list_empty(&svm->ir_list))
598 goto out;
599
600 list_for_each_entry(ir, &svm->ir_list, node) {
601 if (activate)
602 ret = amd_iommu_activate_guest_mode(ir->data);
603 else
604 ret = amd_iommu_deactivate_guest_mode(ir->data);
605 if (ret)
606 break;
607 }
608out:
609 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
610 return ret;
611}
612
613void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
614{
615 struct vcpu_svm *svm = to_svm(vcpu);
Maxim Levitsky5868b822021-07-13 17:20:18 +0300616 struct vmcb *vmcb = svm->vmcb01.ptr;
Joerg Roedelef0f6492020-03-31 12:17:38 -0400617 bool activated = kvm_vcpu_apicv_active(vcpu);
618
Vitaly Kuznetsovfdf513e2021-06-09 17:09:08 +0200619 if (!enable_apicv)
Joerg Roedelef0f6492020-03-31 12:17:38 -0400620 return;
621
622 if (activated) {
623 /**
624 * During AVIC temporary deactivation, guest could update
625 * APIC ID, DFR and LDR registers, which would not be trapped
626 * by avic_unaccelerated_access_interception(). In this case,
627 * we need to check and update the AVIC logical APIC ID table
628 * accordingly before re-activating.
629 */
630 avic_post_state_restore(vcpu);
631 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
632 } else {
633 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
634 }
Joerg Roedel06e78522020-06-25 10:03:23 +0200635 vmcb_mark_dirty(vmcb, VMCB_AVIC);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400636
Maxim Levitskydf7e4822021-08-10 23:52:50 +0300637 if (activated)
638 avic_vcpu_load(vcpu, vcpu->cpu);
639 else
640 avic_vcpu_put(vcpu);
641
Joerg Roedelef0f6492020-03-31 12:17:38 -0400642 svm_set_pi_irte_mode(vcpu, activated);
643}
644
645void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
646{
647 return;
648}
649
650int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
651{
652 if (!vcpu->arch.apicv_active)
653 return -1;
654
655 kvm_lapic_set_irr(vec, vcpu->arch.apic);
Sean Christopherson31f251d2021-12-08 01:52:20 +0000656
657 /*
658 * Pairs with the smp_mb_*() after setting vcpu->guest_mode in
659 * vcpu_enter_guest() to ensure the write to the vIRR is ordered before
660 * the read of guest_mode, which guarantees that either VMRUN will see
661 * and process the new vIRR entry, or that the below code will signal
662 * the doorbell if the vCPU is already running in the guest.
663 */
Joerg Roedelef0f6492020-03-31 12:17:38 -0400664 smp_mb__after_atomic();
665
Sean Christopherson31f251d2021-12-08 01:52:20 +0000666 /*
667 * Signal the doorbell to tell hardware to inject the IRQ if the vCPU
668 * is in the guest. If the vCPU is not in the guest, hardware will
669 * automatically process AVIC interrupts at VMRUN.
670 */
671 if (vcpu->mode == IN_GUEST_MODE) {
Sean Christopherson91b01892021-10-08 19:11:55 -0700672 int cpu = READ_ONCE(vcpu->cpu);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400673
Sean Christopherson91b01892021-10-08 19:11:55 -0700674 /*
675 * Note, the vCPU could get migrated to a different pCPU at any
676 * point, which could result in signalling the wrong/previous
677 * pCPU. But if that happens the vCPU is guaranteed to do a
678 * VMRUN (after being migrated) and thus will process pending
679 * interrupts, i.e. a doorbell is not needed (and the spurious
680 * one is harmless).
681 */
682 if (cpu != get_cpu())
Maxim Levitsky39150352022-02-07 17:54:26 +0200683 wrmsrl(MSR_AMD64_SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpu));
Joerg Roedelef0f6492020-03-31 12:17:38 -0400684 put_cpu();
Sean Christopherson31f251d2021-12-08 01:52:20 +0000685 } else {
686 /*
687 * Wake the vCPU if it was blocking. KVM will then detect the
688 * pending IRQ when checking if the vCPU has a wake event.
689 */
Joerg Roedelef0f6492020-03-31 12:17:38 -0400690 kvm_vcpu_wake_up(vcpu);
Sean Christopherson31f251d2021-12-08 01:52:20 +0000691 }
Joerg Roedelef0f6492020-03-31 12:17:38 -0400692
693 return 0;
694}
695
696bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
697{
698 return false;
699}
700
701static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
702{
703 unsigned long flags;
704 struct amd_svm_iommu_ir *cur;
705
706 spin_lock_irqsave(&svm->ir_list_lock, flags);
707 list_for_each_entry(cur, &svm->ir_list, node) {
708 if (cur->data != pi->ir_data)
709 continue;
710 list_del(&cur->node);
711 kfree(cur);
712 break;
713 }
714 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
715}
716
717static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
718{
719 int ret = 0;
720 unsigned long flags;
721 struct amd_svm_iommu_ir *ir;
722
723 /**
Ingo Molnar163b0992021-03-21 22:28:53 +0100724 * In some cases, the existing irte is updated and re-set,
Joerg Roedelef0f6492020-03-31 12:17:38 -0400725 * so we need to check here if it's already been * added
726 * to the ir_list.
727 */
728 if (pi->ir_data && (pi->prev_ga_tag != 0)) {
729 struct kvm *kvm = svm->vcpu.kvm;
730 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
731 struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
732 struct vcpu_svm *prev_svm;
733
734 if (!prev_vcpu) {
735 ret = -EINVAL;
736 goto out;
737 }
738
739 prev_svm = to_svm(prev_vcpu);
740 svm_ir_list_del(prev_svm, pi);
741 }
742
743 /**
744 * Allocating new amd_iommu_pi_data, which will get
745 * add to the per-vcpu ir_list.
746 */
747 ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL_ACCOUNT);
748 if (!ir) {
749 ret = -ENOMEM;
750 goto out;
751 }
752 ir->data = pi->ir_data;
753
754 spin_lock_irqsave(&svm->ir_list_lock, flags);
755 list_add(&ir->node, &svm->ir_list);
756 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
757out:
758 return ret;
759}
760
ChenXiaoSong02ffbe62021-06-09 20:22:17 +0800761/*
Joerg Roedelef0f6492020-03-31 12:17:38 -0400762 * Note:
763 * The HW cannot support posting multicast/broadcast
764 * interrupts to a vCPU. So, we still use legacy interrupt
765 * remapping for these kind of interrupts.
766 *
767 * For lowest-priority interrupts, we only support
768 * those with single CPU as the destination, e.g. user
769 * configures the interrupts via /proc/irq or uses
770 * irqbalance to make the interrupts single-CPU.
771 */
772static int
773get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
774 struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
775{
776 struct kvm_lapic_irq irq;
777 struct kvm_vcpu *vcpu = NULL;
778
779 kvm_set_msi_irq(kvm, e, &irq);
780
781 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
782 !kvm_irq_is_postable(&irq)) {
783 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
784 __func__, irq.vector);
785 return -1;
786 }
787
788 pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
789 irq.vector);
790 *svm = to_svm(vcpu);
791 vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
792 vcpu_info->vector = irq.vector;
793
794 return 0;
795}
796
797/*
798 * svm_update_pi_irte - set IRTE for Posted-Interrupts
799 *
800 * @kvm: kvm
801 * @host_irq: host irq of the interrupt
802 * @guest_irq: gsi of the interrupt
803 * @set: set or unset PI
804 * returns 0 on success, < 0 on failure
805 */
806int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
807 uint32_t guest_irq, bool set)
808{
809 struct kvm_kernel_irq_routing_entry *e;
810 struct kvm_irq_routing_table *irq_rt;
811 int idx, ret = -EINVAL;
812
813 if (!kvm_arch_has_assigned_device(kvm) ||
814 !irq_remapping_cap(IRQ_POSTING_CAP))
815 return 0;
816
817 pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
818 __func__, host_irq, guest_irq, set);
819
820 idx = srcu_read_lock(&kvm->irq_srcu);
821 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
822 WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
823
824 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
825 struct vcpu_data vcpu_info;
826 struct vcpu_svm *svm = NULL;
827
828 if (e->type != KVM_IRQ_ROUTING_MSI)
829 continue;
830
831 /**
832 * Here, we setup with legacy mode in the following cases:
833 * 1. When cannot target interrupt to a specific vcpu.
834 * 2. Unsetting posted interrupt.
Ingo Molnard9f6e122021-03-18 15:28:01 +0100835 * 3. APIC virtualization is disabled for the vcpu.
Joerg Roedelef0f6492020-03-31 12:17:38 -0400836 * 4. IRQ has incompatible delivery mode (SMI, INIT, etc)
837 */
838 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
839 kvm_vcpu_apicv_active(&svm->vcpu)) {
840 struct amd_iommu_pi_data pi;
841
842 /* Try to enable guest_mode in IRTE */
843 pi.base = __sme_set(page_to_phys(svm->avic_backing_page) &
844 AVIC_HPA_MASK);
845 pi.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id,
846 svm->vcpu.vcpu_id);
847 pi.is_guest_mode = true;
848 pi.vcpu_data = &vcpu_info;
849 ret = irq_set_vcpu_affinity(host_irq, &pi);
850
851 /**
852 * Here, we successfully setting up vcpu affinity in
853 * IOMMU guest mode. Now, we need to store the posted
854 * interrupt information in a per-vcpu ir_list so that
855 * we can reference to them directly when we update vcpu
856 * scheduling information in IOMMU irte.
857 */
858 if (!ret && pi.is_guest_mode)
859 svm_ir_list_add(svm, &pi);
860 } else {
861 /* Use legacy mode in IRTE */
862 struct amd_iommu_pi_data pi;
863
864 /**
865 * Here, pi is used to:
866 * - Tell IOMMU to use legacy mode for this interrupt.
867 * - Retrieve ga_tag of prior interrupt remapping data.
868 */
Suravee Suthikulpanitf6426ab2020-10-03 23:27:07 +0000869 pi.prev_ga_tag = 0;
Joerg Roedelef0f6492020-03-31 12:17:38 -0400870 pi.is_guest_mode = false;
871 ret = irq_set_vcpu_affinity(host_irq, &pi);
872
873 /**
874 * Check if the posted interrupt was previously
875 * setup with the guest_mode by checking if the ga_tag
876 * was cached. If so, we need to clean up the per-vcpu
877 * ir_list.
878 */
879 if (!ret && pi.prev_ga_tag) {
880 int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
881 struct kvm_vcpu *vcpu;
882
883 vcpu = kvm_get_vcpu_by_id(kvm, id);
884 if (vcpu)
885 svm_ir_list_del(to_svm(vcpu), &pi);
886 }
887 }
888
889 if (!ret && svm) {
890 trace_kvm_pi_irte_update(host_irq, svm->vcpu.vcpu_id,
891 e->gsi, vcpu_info.vector,
892 vcpu_info.pi_desc_addr, set);
893 }
894
895 if (ret < 0) {
896 pr_err("%s: failed to update PI IRTE\n", __func__);
897 goto out;
898 }
899 }
900
901 ret = 0;
902out:
903 srcu_read_unlock(&kvm->irq_srcu, idx);
904 return ret;
905}
906
907bool svm_check_apicv_inhibit_reasons(ulong bit)
908{
909 ulong supported = BIT(APICV_INHIBIT_REASON_DISABLE) |
Paolo Bonzinief8b4b72021-11-30 07:37:45 -0500910 BIT(APICV_INHIBIT_REASON_ABSENT) |
Joerg Roedelef0f6492020-03-31 12:17:38 -0400911 BIT(APICV_INHIBIT_REASON_HYPERV) |
912 BIT(APICV_INHIBIT_REASON_NESTED) |
913 BIT(APICV_INHIBIT_REASON_IRQWIN) |
914 BIT(APICV_INHIBIT_REASON_PIT_REINJ) |
Maxim Levitskycae72dc2021-11-08 11:02:45 +0200915 BIT(APICV_INHIBIT_REASON_X2APIC) |
916 BIT(APICV_INHIBIT_REASON_BLOCKIRQ);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400917
918 return supported & BIT(bit);
919}
920
Joerg Roedelef0f6492020-03-31 12:17:38 -0400921
922static inline int
923avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
924{
925 int ret = 0;
926 unsigned long flags;
927 struct amd_svm_iommu_ir *ir;
928 struct vcpu_svm *svm = to_svm(vcpu);
929
930 if (!kvm_arch_has_assigned_device(vcpu->kvm))
931 return 0;
932
933 /*
934 * Here, we go through the per-vcpu ir_list to update all existing
935 * interrupt remapping table entry targeting this vcpu.
936 */
937 spin_lock_irqsave(&svm->ir_list_lock, flags);
938
939 if (list_empty(&svm->ir_list))
940 goto out;
941
942 list_for_each_entry(ir, &svm->ir_list, node) {
943 ret = amd_iommu_update_ga(cpu, r, ir->data);
944 if (ret)
945 break;
946 }
947out:
948 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
949 return ret;
950}
951
952void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
953{
954 u64 entry;
955 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
956 int h_physical_id = kvm_cpu_get_apicid(cpu);
957 struct vcpu_svm *svm = to_svm(vcpu);
958
Sean Christopherson935a7332021-12-08 01:52:31 +0000959 lockdep_assert_preemption_disabled();
960
Joerg Roedelef0f6492020-03-31 12:17:38 -0400961 /*
962 * Since the host physical APIC id is 8 bits,
963 * we can support host APIC ID upto 255.
964 */
965 if (WARN_ON(h_physical_id > AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK))
966 return;
967
Sean Christopherson782f6452021-12-08 01:52:24 +0000968 /*
969 * No need to update anything if the vCPU is blocking, i.e. if the vCPU
970 * is being scheduled in after being preempted. The CPU entries in the
971 * Physical APIC table and IRTE are consumed iff IsRun{ning} is '1'.
972 * If the vCPU was migrated, its new CPU value will be stuffed when the
973 * vCPU unblocks.
974 */
975 if (kvm_vcpu_is_blocking(vcpu))
976 return;
977
Joerg Roedelef0f6492020-03-31 12:17:38 -0400978 entry = READ_ONCE(*(svm->avic_physical_id_cache));
979 WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
980
981 entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
982 entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
Sean Christopherson782f6452021-12-08 01:52:24 +0000983 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
Joerg Roedelef0f6492020-03-31 12:17:38 -0400984
985 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Sean Christopherson782f6452021-12-08 01:52:24 +0000986 avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, true);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400987}
988
989void avic_vcpu_put(struct kvm_vcpu *vcpu)
990{
991 u64 entry;
992 struct vcpu_svm *svm = to_svm(vcpu);
993
Sean Christopherson935a7332021-12-08 01:52:31 +0000994 lockdep_assert_preemption_disabled();
995
Joerg Roedelef0f6492020-03-31 12:17:38 -0400996 entry = READ_ONCE(*(svm->avic_physical_id_cache));
Sean Christopherson782f6452021-12-08 01:52:24 +0000997
998 /* Nothing to do if IsRunning == '0' due to vCPU blocking. */
999 if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK))
1000 return;
1001
1002 avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
Joerg Roedelef0f6492020-03-31 12:17:38 -04001003
1004 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1005 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
1006}
1007
Sean Christophersona3c19d52021-12-08 01:52:33 +00001008void avic_vcpu_blocking(struct kvm_vcpu *vcpu)
Joerg Roedelef0f6492020-03-31 12:17:38 -04001009{
Sean Christopherson935a7332021-12-08 01:52:31 +00001010 if (!kvm_vcpu_apicv_active(vcpu))
1011 return;
1012
1013 preempt_disable();
1014
1015 /*
1016 * Unload the AVIC when the vCPU is about to block, _before_
1017 * the vCPU actually blocks.
1018 *
1019 * Any IRQs that arrive before IsRunning=0 will not cause an
1020 * incomplete IPI vmexit on the source, therefore vIRR will also
1021 * be checked by kvm_vcpu_check_block() before blocking. The
1022 * memory barrier implicit in set_current_state orders writing
1023 * IsRunning=0 before reading the vIRR. The processor needs a
1024 * matching memory barrier on interrupt delivery between writing
1025 * IRR and reading IsRunning; the lack of this barrier might be
1026 * the cause of errata #1235).
1027 */
1028 avic_vcpu_put(vcpu);
1029
1030 preempt_enable();
Joerg Roedelef0f6492020-03-31 12:17:38 -04001031}
1032
Sean Christophersona3c19d52021-12-08 01:52:33 +00001033void avic_vcpu_unblocking(struct kvm_vcpu *vcpu)
Joerg Roedelef0f6492020-03-31 12:17:38 -04001034{
Sean Christopherson935a7332021-12-08 01:52:31 +00001035 int cpu;
1036
1037 if (!kvm_vcpu_apicv_active(vcpu))
1038 return;
1039
1040 cpu = get_cpu();
1041 WARN_ON(cpu != vcpu->cpu);
1042
1043 avic_vcpu_load(vcpu, cpu);
1044
1045 put_cpu();
Joerg Roedelef0f6492020-03-31 12:17:38 -04001046}