2 * Copyright (C) 2001 MandrakeSoft S.A.
3 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
8 * http://www.linux-mandrake.com/
9 * http://www.mandrakesoft.com/
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Yunhong Jiang <yunhong.jiang@intel.com>
26 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
27 * Based on Xen 3.1 code.
30 #include <linux/kvm_host.h>
31 #include <linux/kvm.h>
33 #include <linux/highmem.h>
34 #include <linux/smp.h>
35 #include <linux/hrtimer.h>
37 #include <linux/slab.h>
38 #include <linux/export.h>
39 #include <linux/nospec.h>
40 #include <asm/processor.h>
42 #include <asm/current.h>
43 #include <trace/events/kvm.h>
49 static int ioapic_service(struct kvm_ioapic *vioapic, int irq,
52 static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
56 unsigned long result = 0;
58 switch (ioapic->ioregsel) {
59 case IOAPIC_REG_VERSION:
60 result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16)
61 | (IOAPIC_VERSION_ID & 0xff));
64 case IOAPIC_REG_APIC_ID:
65 case IOAPIC_REG_ARB_ID:
66 result = ((ioapic->id & 0xf) << 24);
71 u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
72 u64 redir_content = ~0ULL;
74 if (redir_index < IOAPIC_NUM_PINS) {
75 u32 index = array_index_nospec(
76 redir_index, IOAPIC_NUM_PINS);
78 redir_content = ioapic->redirtbl[index].bits;
81 result = (ioapic->ioregsel & 0x1) ?
82 (redir_content >> 32) & 0xffffffff :
83 redir_content & 0xffffffff;
91 static void rtc_irq_eoi_tracking_reset(struct kvm_ioapic *ioapic)
93 ioapic->rtc_status.pending_eoi = 0;
94 bitmap_zero(ioapic->rtc_status.dest_map.map, KVM_MAX_VCPU_ID);
97 static void kvm_rtc_eoi_tracking_restore_all(struct kvm_ioapic *ioapic);
99 static void rtc_status_pending_eoi_check_valid(struct kvm_ioapic *ioapic)
101 if (WARN_ON(ioapic->rtc_status.pending_eoi < 0))
102 kvm_rtc_eoi_tracking_restore_all(ioapic);
105 static void __rtc_irq_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)
107 bool new_val, old_val;
108 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
109 struct dest_map *dest_map = &ioapic->rtc_status.dest_map;
110 union kvm_ioapic_redirect_entry *e;
112 e = &ioapic->redirtbl[RTC_GSI];
113 if (!kvm_apic_match_dest(vcpu, NULL, APIC_DEST_NOSHORT,
115 kvm_lapic_irq_dest_mode(!!e->fields.dest_mode)))
118 new_val = kvm_apic_pending_eoi(vcpu, e->fields.vector);
119 old_val = test_bit(vcpu->vcpu_id, dest_map->map);
121 if (new_val == old_val)
125 __set_bit(vcpu->vcpu_id, dest_map->map);
126 dest_map->vectors[vcpu->vcpu_id] = e->fields.vector;
127 ioapic->rtc_status.pending_eoi++;
129 __clear_bit(vcpu->vcpu_id, dest_map->map);
130 ioapic->rtc_status.pending_eoi--;
131 rtc_status_pending_eoi_check_valid(ioapic);
135 void kvm_rtc_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)
137 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
139 spin_lock(&ioapic->lock);
140 __rtc_irq_eoi_tracking_restore_one(vcpu);
141 spin_unlock(&ioapic->lock);
144 static void kvm_rtc_eoi_tracking_restore_all(struct kvm_ioapic *ioapic)
146 struct kvm_vcpu *vcpu;
149 if (RTC_GSI >= IOAPIC_NUM_PINS)
152 rtc_irq_eoi_tracking_reset(ioapic);
153 kvm_for_each_vcpu(i, vcpu, ioapic->kvm)
154 __rtc_irq_eoi_tracking_restore_one(vcpu);
157 static void rtc_irq_eoi(struct kvm_ioapic *ioapic, struct kvm_vcpu *vcpu,
160 struct dest_map *dest_map = &ioapic->rtc_status.dest_map;
162 /* RTC special handling */
163 if (test_bit(vcpu->vcpu_id, dest_map->map) &&
164 (vector == dest_map->vectors[vcpu->vcpu_id]) &&
165 (test_and_clear_bit(vcpu->vcpu_id,
166 ioapic->rtc_status.dest_map.map))) {
167 --ioapic->rtc_status.pending_eoi;
168 rtc_status_pending_eoi_check_valid(ioapic);
172 static bool rtc_irq_check_coalesced(struct kvm_ioapic *ioapic)
174 if (ioapic->rtc_status.pending_eoi > 0)
175 return true; /* coalesced */
180 static int ioapic_set_irq(struct kvm_ioapic *ioapic, unsigned int irq,
181 int irq_level, bool line_status)
183 union kvm_ioapic_redirect_entry entry;
188 entry = ioapic->redirtbl[irq];
189 edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
192 ioapic->irr &= ~mask;
198 * Return 0 for coalesced interrupts; for edge-triggered interrupts,
199 * this only happens if a previous edge has not been delivered due
200 * to masking. For level interrupts, the remote_irr field tells
201 * us if the interrupt is waiting for an EOI.
203 * RTC is special: it is edge-triggered, but userspace likes to know
204 * if it has been already ack-ed via EOI because coalesced RTC
205 * interrupts lead to time drift in Windows guests. So we track
206 * EOI manually for the RTC interrupt.
208 if (irq == RTC_GSI && line_status &&
209 rtc_irq_check_coalesced(ioapic)) {
214 old_irr = ioapic->irr;
217 ioapic->irr_delivered &= ~mask;
218 if (old_irr == ioapic->irr) {
224 ret = ioapic_service(ioapic, irq, line_status);
227 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
231 static void kvm_ioapic_inject_all(struct kvm_ioapic *ioapic, unsigned long irr)
235 rtc_irq_eoi_tracking_reset(ioapic);
236 for_each_set_bit(idx, &irr, IOAPIC_NUM_PINS)
237 ioapic_set_irq(ioapic, idx, 1, true);
239 kvm_rtc_eoi_tracking_restore_all(ioapic);
243 void kvm_ioapic_scan_entry(struct kvm_vcpu *vcpu, ulong *ioapic_handled_vectors)
245 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
246 struct dest_map *dest_map = &ioapic->rtc_status.dest_map;
247 union kvm_ioapic_redirect_entry *e;
250 spin_lock(&ioapic->lock);
252 /* Make sure we see any missing RTC EOI */
253 if (test_bit(vcpu->vcpu_id, dest_map->map))
254 __set_bit(dest_map->vectors[vcpu->vcpu_id],
255 ioapic_handled_vectors);
257 for (index = 0; index < IOAPIC_NUM_PINS; index++) {
258 e = &ioapic->redirtbl[index];
259 if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG ||
260 kvm_irq_has_notifier(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index) ||
262 u16 dm = kvm_lapic_irq_dest_mode(!!e->fields.dest_mode);
264 if (kvm_apic_match_dest(vcpu, NULL, APIC_DEST_NOSHORT,
265 e->fields.dest_id, dm) ||
266 kvm_apic_pending_eoi(vcpu, e->fields.vector))
267 __set_bit(e->fields.vector,
268 ioapic_handled_vectors);
271 spin_unlock(&ioapic->lock);
274 void kvm_arch_post_irq_ack_notifier_list_update(struct kvm *kvm)
276 if (!ioapic_in_kernel(kvm))
278 kvm_make_scan_ioapic_request(kvm);
281 static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
284 bool mask_before, mask_after;
285 union kvm_ioapic_redirect_entry *e;
286 unsigned long vcpu_bitmap;
287 int old_remote_irr, old_delivery_status, old_dest_id, old_dest_mode;
289 switch (ioapic->ioregsel) {
290 case IOAPIC_REG_VERSION:
291 /* Writes are ignored. */
294 case IOAPIC_REG_APIC_ID:
295 ioapic->id = (val >> 24) & 0xf;
298 case IOAPIC_REG_ARB_ID:
302 index = (ioapic->ioregsel - 0x10) >> 1;
304 if (index >= IOAPIC_NUM_PINS)
306 index = array_index_nospec(index, IOAPIC_NUM_PINS);
307 e = &ioapic->redirtbl[index];
308 mask_before = e->fields.mask;
309 /* Preserve read-only fields */
310 old_remote_irr = e->fields.remote_irr;
311 old_delivery_status = e->fields.delivery_status;
312 old_dest_id = e->fields.dest_id;
313 old_dest_mode = e->fields.dest_mode;
314 if (ioapic->ioregsel & 1) {
315 e->bits &= 0xffffffff;
316 e->bits |= (u64) val << 32;
318 e->bits &= ~0xffffffffULL;
319 e->bits |= (u32) val;
321 e->fields.remote_irr = old_remote_irr;
322 e->fields.delivery_status = old_delivery_status;
325 * Some OSes (Linux, Xen) assume that Remote IRR bit will
326 * be cleared by IOAPIC hardware when the entry is configured
327 * as edge-triggered. This behavior is used to simulate an
328 * explicit EOI on IOAPICs that don't have the EOI register.
330 if (e->fields.trig_mode == IOAPIC_EDGE_TRIG)
331 e->fields.remote_irr = 0;
333 mask_after = e->fields.mask;
334 if (mask_before != mask_after)
335 kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after);
336 if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
337 && ioapic->irr & (1 << index))
338 ioapic_service(ioapic, index, false);
339 if (e->fields.delivery_mode == APIC_DM_FIXED) {
340 struct kvm_lapic_irq irq;
342 irq.shorthand = APIC_DEST_NOSHORT;
343 irq.vector = e->fields.vector;
344 irq.delivery_mode = e->fields.delivery_mode << 8;
345 irq.dest_id = e->fields.dest_id;
347 kvm_lapic_irq_dest_mode(!!e->fields.dest_mode);
348 bitmap_zero(&vcpu_bitmap, 16);
349 kvm_bitmap_or_dest_vcpus(ioapic->kvm, &irq,
351 if (old_dest_mode != e->fields.dest_mode ||
352 old_dest_id != e->fields.dest_id) {
354 * Update vcpu_bitmap with vcpus specified in
355 * the previous request as well. This is done to
356 * keep ioapic_handled_vectors synchronized.
358 irq.dest_id = old_dest_id;
360 kvm_lapic_irq_dest_mode(
361 !!e->fields.dest_mode);
362 kvm_bitmap_or_dest_vcpus(ioapic->kvm, &irq,
365 kvm_make_scan_ioapic_request_mask(ioapic->kvm,
368 kvm_make_scan_ioapic_request(ioapic->kvm);
374 static int ioapic_service(struct kvm_ioapic *ioapic, int irq, bool line_status)
376 union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
377 struct kvm_lapic_irq irqe;
380 if (entry->fields.mask ||
381 (entry->fields.trig_mode == IOAPIC_LEVEL_TRIG &&
382 entry->fields.remote_irr))
385 irqe.dest_id = entry->fields.dest_id;
386 irqe.vector = entry->fields.vector;
387 irqe.dest_mode = kvm_lapic_irq_dest_mode(!!entry->fields.dest_mode);
388 irqe.trig_mode = entry->fields.trig_mode;
389 irqe.delivery_mode = entry->fields.delivery_mode << 8;
391 irqe.shorthand = APIC_DEST_NOSHORT;
392 irqe.msi_redir_hint = false;
394 if (irqe.trig_mode == IOAPIC_EDGE_TRIG)
395 ioapic->irr_delivered |= 1 << irq;
397 if (irq == RTC_GSI && line_status) {
399 * pending_eoi cannot ever become negative (see
400 * rtc_status_pending_eoi_check_valid) and the caller
401 * ensures that it is only called if it is >= zero, namely
402 * if rtc_irq_check_coalesced returns false).
404 BUG_ON(ioapic->rtc_status.pending_eoi != 0);
405 ret = kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe,
406 &ioapic->rtc_status.dest_map);
407 ioapic->rtc_status.pending_eoi = (ret < 0 ? 0 : ret);
409 ret = kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe, NULL);
411 if (ret && irqe.trig_mode == IOAPIC_LEVEL_TRIG)
412 entry->fields.remote_irr = 1;
417 int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
418 int level, bool line_status)
422 BUG_ON(irq < 0 || irq >= IOAPIC_NUM_PINS);
424 spin_lock(&ioapic->lock);
425 irq_level = __kvm_irq_line_state(&ioapic->irq_states[irq],
426 irq_source_id, level);
427 ret = ioapic_set_irq(ioapic, irq, irq_level, line_status);
429 spin_unlock(&ioapic->lock);
434 void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id)
438 spin_lock(&ioapic->lock);
439 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
440 __clear_bit(irq_source_id, &ioapic->irq_states[i]);
441 spin_unlock(&ioapic->lock);
444 static void kvm_ioapic_eoi_inject_work(struct work_struct *work)
447 struct kvm_ioapic *ioapic = container_of(work, struct kvm_ioapic,
449 spin_lock(&ioapic->lock);
450 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
451 union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
453 if (ent->fields.trig_mode != IOAPIC_LEVEL_TRIG)
456 if (ioapic->irr & (1 << i) && !ent->fields.remote_irr)
457 ioapic_service(ioapic, i, false);
459 spin_unlock(&ioapic->lock);
462 #define IOAPIC_SUCCESSIVE_IRQ_MAX_COUNT 10000
463 static void kvm_ioapic_update_eoi_one(struct kvm_vcpu *vcpu,
464 struct kvm_ioapic *ioapic,
468 struct kvm_lapic *apic = vcpu->arch.apic;
469 union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[pin];
472 * We are dropping lock while calling ack notifiers because ack
473 * notifier callbacks for assigned devices call into IOAPIC
474 * recursively. Since remote_irr is cleared only after call
475 * to notifiers if the same vector will be delivered while lock
476 * is dropped it will be put into irr and will be delivered
477 * after ack notifier returns.
479 spin_unlock(&ioapic->lock);
480 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, pin);
481 spin_lock(&ioapic->lock);
483 if (trigger_mode != IOAPIC_LEVEL_TRIG ||
484 kvm_lapic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI)
487 ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
488 ent->fields.remote_irr = 0;
489 if (!ent->fields.mask && (ioapic->irr & (1 << pin))) {
490 ++ioapic->irq_eoi[pin];
491 if (ioapic->irq_eoi[pin] == IOAPIC_SUCCESSIVE_IRQ_MAX_COUNT) {
493 * Real hardware does not deliver the interrupt
494 * immediately during eoi broadcast, and this
495 * lets a buggy guest make slow progress
496 * even if it does not correctly handle a
497 * level-triggered interrupt. Emulate this
498 * behavior if we detect an interrupt storm.
500 schedule_delayed_work(&ioapic->eoi_inject, HZ / 100);
501 ioapic->irq_eoi[pin] = 0;
502 trace_kvm_ioapic_delayed_eoi_inj(ent->bits);
504 ioapic_service(ioapic, pin, false);
507 ioapic->irq_eoi[pin] = 0;
511 void kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu, int vector, int trigger_mode)
514 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
516 spin_lock(&ioapic->lock);
517 rtc_irq_eoi(ioapic, vcpu, vector);
518 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
519 union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
521 if (ent->fields.vector != vector)
523 kvm_ioapic_update_eoi_one(vcpu, ioapic, trigger_mode, i);
525 spin_unlock(&ioapic->lock);
528 static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
530 return container_of(dev, struct kvm_ioapic, dev);
533 static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
535 return ((addr >= ioapic->base_address &&
536 (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
539 static int ioapic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
540 gpa_t addr, int len, void *val)
542 struct kvm_ioapic *ioapic = to_ioapic(this);
544 if (!ioapic_in_range(ioapic, addr))
547 ASSERT(!(addr & 0xf)); /* check alignment */
550 spin_lock(&ioapic->lock);
552 case IOAPIC_REG_SELECT:
553 result = ioapic->ioregsel;
556 case IOAPIC_REG_WINDOW:
557 result = ioapic_read_indirect(ioapic, addr, len);
564 spin_unlock(&ioapic->lock);
568 *(u64 *) val = result;
573 memcpy(val, (char *)&result, len);
576 printk(KERN_WARNING "ioapic: wrong length %d\n", len);
581 static int ioapic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
582 gpa_t addr, int len, const void *val)
584 struct kvm_ioapic *ioapic = to_ioapic(this);
586 if (!ioapic_in_range(ioapic, addr))
589 ASSERT(!(addr & 0xf)); /* check alignment */
603 printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
608 spin_lock(&ioapic->lock);
610 case IOAPIC_REG_SELECT:
611 ioapic->ioregsel = data & 0xFF; /* 8-bit register */
614 case IOAPIC_REG_WINDOW:
615 ioapic_write_indirect(ioapic, data);
621 spin_unlock(&ioapic->lock);
625 static void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
629 cancel_delayed_work_sync(&ioapic->eoi_inject);
630 for (i = 0; i < IOAPIC_NUM_PINS; i++)
631 ioapic->redirtbl[i].fields.mask = 1;
632 ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
633 ioapic->ioregsel = 0;
635 ioapic->irr_delivered = 0;
637 memset(ioapic->irq_eoi, 0x00, sizeof(ioapic->irq_eoi));
638 rtc_irq_eoi_tracking_reset(ioapic);
641 static const struct kvm_io_device_ops ioapic_mmio_ops = {
642 .read = ioapic_mmio_read,
643 .write = ioapic_mmio_write,
646 int kvm_ioapic_init(struct kvm *kvm)
648 struct kvm_ioapic *ioapic;
651 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL_ACCOUNT);
654 spin_lock_init(&ioapic->lock);
655 INIT_DELAYED_WORK(&ioapic->eoi_inject, kvm_ioapic_eoi_inject_work);
656 kvm->arch.vioapic = ioapic;
657 kvm_ioapic_reset(ioapic);
658 kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
660 mutex_lock(&kvm->slots_lock);
661 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address,
662 IOAPIC_MEM_LENGTH, &ioapic->dev);
663 mutex_unlock(&kvm->slots_lock);
665 kvm->arch.vioapic = NULL;
672 void kvm_ioapic_destroy(struct kvm *kvm)
674 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
679 cancel_delayed_work_sync(&ioapic->eoi_inject);
680 mutex_lock(&kvm->slots_lock);
681 kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
682 mutex_unlock(&kvm->slots_lock);
683 kvm->arch.vioapic = NULL;
687 void kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
689 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
691 spin_lock(&ioapic->lock);
692 memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
693 state->irr &= ~ioapic->irr_delivered;
694 spin_unlock(&ioapic->lock);
697 void kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
699 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
701 spin_lock(&ioapic->lock);
702 memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
704 ioapic->irr_delivered = 0;
705 kvm_make_scan_ioapic_request(kvm);
706 kvm_ioapic_inject_all(ioapic, state->irr);
707 spin_unlock(&ioapic->lock);