blob: 0bb9aa295e6c5fa93eabf25ab70b2cde96bdadcd [file] [log] [blame]
Avi Kivityedf88412007-12-16 11:02:48 +02001#ifndef __KVM_HOST_H
2#define __KVM_HOST_H
Avi Kivity6aa8b732006-12-10 02:21:36 -08003
4/*
5 * This work is licensed under the terms of the GNU GPL, version 2. See
6 * the COPYING file in the top-level directory.
7 */
8
9#include <linux/types.h>
Christian Borntraegere56a7a22007-10-18 14:39:10 +020010#include <linux/hardirq.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080011#include <linux/list.h>
12#include <linux/mutex.h>
13#include <linux/spinlock.h>
Markus Rechberger06ff0d32007-05-27 10:46:52 +030014#include <linux/signal.h>
15#include <linux/sched.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080016#include <linux/mm.h>
Avi Kivity15ad7142007-07-11 18:17:21 +030017#include <linux/preempt.h>
Sheng Yang0937c482008-11-24 14:32:53 +080018#include <linux/msi.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040019#include <asm/signal.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080020
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include <linux/kvm.h>
Ingo Molnar102d8322007-02-19 14:37:47 +020022#include <linux/kvm_para.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_types.h>
Hollis Blanchardd77a39d92007-12-03 15:30:23 -060025
Avi Kivityedf88412007-12-16 11:02:48 +020026#include <asm/kvm_host.h>
Zhang Xiantaod657a982007-12-14 09:41:22 +080027
Avi Kivity6aa8b732006-12-10 02:21:36 -080028/*
Avi Kivityd9e368d2007-06-07 19:18:30 +030029 * vcpu->requests bit members
30 */
Avi Kivity3176bc32007-10-16 17:22:08 +020031#define KVM_REQ_TLB_FLUSH 0
Avi Kivity2f52d582008-01-16 12:49:30 +020032#define KVM_REQ_MIGRATE_TIMER 1
Avi Kivityb209749f2007-10-22 16:50:39 +020033#define KVM_REQ_REPORT_TPR_ACCESS 2
Marcelo Tosatti2e53d632008-02-20 14:47:24 -050034#define KVM_REQ_MMU_RELOAD 3
Joerg Roedel71c4dfa2008-02-26 16:49:16 +010035#define KVM_REQ_TRIPLE_FAULT 4
Marcelo Tosatti06e05642008-06-06 16:37:36 -030036#define KVM_REQ_PENDING_TIMER 5
Marcelo Tosattid7690172008-09-08 15:23:48 -030037#define KVM_REQ_UNHALT 6
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -030038#define KVM_REQ_MMU_SYNC 7
Gerd Hoffmannc8076602009-02-04 17:52:04 +010039#define KVM_REQ_KVMCLOCK_UPDATE 8
Marcelo Tosatti32f88402009-05-07 17:55:12 -030040#define KVM_REQ_KICK 9
Avi Kivity6aa8b732006-12-10 02:21:36 -080041
Sheng Yang5550af42008-10-15 20:15:06 +080042#define KVM_USERSPACE_IRQ_SOURCE_ID 0
43
Michael S. Tsirkin6c474692009-06-29 22:24:26 +030044struct kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -080045struct kvm_vcpu;
Rusty Russellc16f8622007-07-30 21:12:19 +100046extern struct kmem_cache *kvm_vcpu_cache;
Avi Kivity6aa8b732006-12-10 02:21:36 -080047
Gregory Haskins2eeb2e92007-05-31 14:08:53 -040048/*
49 * It would be nice to use something smarter than a linear search, TBD...
50 * Thankfully we dont expect many devices to register (famous last words :),
51 * so until then it will suffice. At least its abstracted so we can change
52 * in one place.
53 */
54struct kvm_io_bus {
55 int dev_count;
56#define NR_IOBUS_DEVS 6
57 struct kvm_io_device *devs[NR_IOBUS_DEVS];
58};
59
Marcelo Tosattie93f8a02009-12-23 14:35:24 -020060enum kvm_bus {
61 KVM_MMIO_BUS,
62 KVM_PIO_BUS,
63 KVM_NR_BUSES
64};
65
66int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
67 int len, const void *val);
68int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, int len,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +030069 void *val);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -020070int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
Gregory Haskins090b7af2009-07-07 17:08:44 -040071 struct kvm_io_device *dev);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -020072int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
73 struct kvm_io_device *dev);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -040074
Zhang Xiantaod657a982007-12-14 09:41:22 +080075struct kvm_vcpu {
Zhang Xiantaod17fbbf2007-12-14 09:45:31 +080076 struct kvm *kvm;
Hollis Blanchard31bb1172008-01-28 17:42:34 -060077#ifdef CONFIG_PREEMPT_NOTIFIERS
Zhang Xiantaod17fbbf2007-12-14 09:45:31 +080078 struct preempt_notifier preempt_notifier;
Hollis Blanchard31bb1172008-01-28 17:42:34 -060079#endif
Zhang Xiantaod17fbbf2007-12-14 09:45:31 +080080 int vcpu_id;
81 struct mutex mutex;
82 int cpu;
83 struct kvm_run *run;
Zhang Xiantaod17fbbf2007-12-14 09:45:31 +080084 unsigned long requests;
Jan Kiszkad0bfb942008-12-15 13:52:10 +010085 unsigned long guest_debug;
Marcelo Tosattif656ce02009-12-23 14:35:25 -020086 int srcu_idx;
87
Zhang Xiantaod17fbbf2007-12-14 09:45:31 +080088 int fpu_active;
89 int guest_fpu_loaded;
90 wait_queue_head_t wq;
91 int sigset_active;
92 sigset_t sigset;
93 struct kvm_vcpu_stat stat;
94
95#ifdef CONFIG_HAS_IOMEM
96 int mmio_needed;
97 int mmio_read_completed;
98 int mmio_is_write;
99 int mmio_size;
100 unsigned char mmio_data[8];
101 gpa_t mmio_phys_addr;
102#endif
Zhang Xiantaod657a982007-12-14 09:41:22 +0800103
104 struct kvm_vcpu_arch arch;
105};
106
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107struct kvm_memory_slot {
108 gfn_t base_gfn;
109 unsigned long npages;
110 unsigned long flags;
Izik Eidus290fc382007-09-27 14:11:22 +0200111 unsigned long *rmap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112 unsigned long *dirty_bitmap;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300113 struct {
114 unsigned long rmap_pde;
115 int write_count;
Joerg Roedelec04b262009-06-19 15:16:23 +0200116 } *lpage_info[KVM_NR_PAGE_SIZES - 1];
Izik Eidus8a7ae052007-10-18 11:09:33 +0200117 unsigned long userspace_addr;
Izik Eidus80b14b52007-10-25 11:54:04 +0200118 int user_alloc;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119};
120
Avi Kivity399ec802008-11-19 13:58:46 +0200121struct kvm_kernel_irq_routing_entry {
122 u32 gsi;
Michael S. Tsirkin5116d8f2009-07-26 17:10:01 +0300123 u32 type;
Gleb Natapov49256632009-02-04 17:28:14 +0200124 int (*set)(struct kvm_kernel_irq_routing_entry *e,
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300125 struct kvm *kvm, int irq_source_id, int level);
Avi Kivity399ec802008-11-19 13:58:46 +0200126 union {
127 struct {
128 unsigned irqchip;
129 unsigned pin;
130 } irqchip;
Sheng Yang79950e12009-02-10 13:57:06 +0800131 struct msi_msg msi;
Avi Kivity399ec802008-11-19 13:58:46 +0200132 };
Gleb Natapov46e624b2009-08-24 11:54:20 +0300133 struct hlist_node link;
134};
135
Gleb Natapov3e71f882009-08-24 11:54:21 +0300136#ifdef __KVM_HAVE_IOAPIC
137
Gleb Natapov46e624b2009-08-24 11:54:20 +0300138struct kvm_irq_routing_table {
Gleb Natapov3e71f882009-08-24 11:54:21 +0300139 int chip[KVM_NR_IRQCHIPS][KVM_IOAPIC_NUM_PINS];
Gleb Natapov46e624b2009-08-24 11:54:20 +0300140 struct kvm_kernel_irq_routing_entry *rt_entries;
141 u32 nr_rt_entries;
142 /*
143 * Array indexed by gsi. Each entry contains list of irq chips
144 * the gsi is connected to.
145 */
146 struct hlist_head map[0];
Avi Kivity399ec802008-11-19 13:58:46 +0200147};
148
Gleb Natapov3e71f882009-08-24 11:54:21 +0300149#else
150
151struct kvm_irq_routing_table {};
152
153#endif
154
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200155struct kvm_memslots {
156 int nmemslots;
157 struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS +
158 KVM_PRIVATE_MEM_SLOTS];
159};
160
Avi Kivity6aa8b732006-12-10 02:21:36 -0800161struct kvm {
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500162 spinlock_t mmu_lock;
Marcelo Tosatti84261922009-06-17 10:53:47 -0300163 spinlock_t requests_lock;
Izik Eidus72dc67a2008-02-10 18:04:15 +0200164 struct rw_semaphore slots_lock;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200165 struct mm_struct *mm; /* userspace tied to this vm */
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200166 struct kvm_memslots *memslots;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200167 struct srcu_struct srcu;
Gleb Natapov73880c82009-06-09 15:56:28 +0300168#ifdef CONFIG_KVM_APIC_ARCHITECTURE
169 u32 bsp_vcpu_id;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300170 struct kvm_vcpu *bsp_vcpu;
Gleb Natapov73880c82009-06-09 15:56:28 +0300171#endif
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000172 struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
Gleb Natapov73880c82009-06-09 15:56:28 +0300173 atomic_t online_vcpus;
Avi Kivity133de902007-02-12 00:54:44 -0800174 struct list_head vm_list;
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300175 struct mutex lock;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200176 struct kvm_io_bus *buses[KVM_NR_BUSES];
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400177#ifdef CONFIG_HAVE_KVM_EVENTFD
178 struct {
179 spinlock_t lock;
180 struct list_head items;
181 } irqfds;
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400182 struct list_head ioeventfds;
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400183#endif
Avi Kivityba1389b2007-11-18 16:24:12 +0200184 struct kvm_vm_stat stat;
Zhang Xiantaod69fb812007-12-14 09:54:20 +0800185 struct kvm_arch arch;
Izik Eidusd39f13b2008-03-30 16:01:25 +0300186 atomic_t users_count;
Laurent Vivier5f94c172008-05-30 16:05:54 +0200187#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
188 struct kvm_coalesced_mmio_dev *coalesced_mmio_dev;
189 struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
190#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200191
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300192 struct mutex irq_lock;
Avi Kivity75858a82009-01-04 17:10:50 +0200193#ifdef CONFIG_HAVE_KVM_IRQCHIP
Gleb Natapov46e624b2009-08-24 11:54:20 +0300194 struct kvm_irq_routing_table *irq_routing;
Avi Kivity75858a82009-01-04 17:10:50 +0200195 struct hlist_head mask_notifier_list;
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300196 struct hlist_head irq_ack_notifier_list;
Avi Kivity75858a82009-01-04 17:10:50 +0200197#endif
198
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200199#ifdef KVM_ARCH_WANT_MMU_NOTIFIER
200 struct mmu_notifier mmu_notifier;
201 unsigned long mmu_notifier_seq;
202 long mmu_notifier_count;
203#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800204};
205
Rusty Russellf0242472007-08-01 10:48:02 +1000206/* The guest did something we don't support. */
207#define pr_unimpl(vcpu, fmt, ...) \
208 do { \
209 if (printk_ratelimit()) \
210 printk(KERN_ERR "kvm: %i: cpu%i " fmt, \
211 current->tgid, (vcpu)->vcpu_id , ## __VA_ARGS__); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400212 } while (0)
Rusty Russellf0242472007-08-01 10:48:02 +1000213
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214#define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt)
215#define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt)
216
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300217static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i)
218{
219 smp_rmb();
220 return kvm->vcpus[i];
221}
222
223#define kvm_for_each_vcpu(idx, vcpup, kvm) \
224 for (idx = 0, vcpup = kvm_get_vcpu(kvm, idx); \
225 idx < atomic_read(&kvm->online_vcpus) && vcpup; \
226 vcpup = kvm_get_vcpu(kvm, ++idx))
227
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000228int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
229void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
230
Carsten Otte313a3dc2007-10-11 19:16:52 +0200231void vcpu_load(struct kvm_vcpu *vcpu);
232void vcpu_put(struct kvm_vcpu *vcpu);
233
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +0800234int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +1000235 struct module *module);
Zhang Xiantaocb498ea2007-11-14 20:39:31 +0800236void kvm_exit(void);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237
Izik Eidusd39f13b2008-03-30 16:01:25 +0300238void kvm_get_kvm(struct kvm *kvm);
239void kvm_put_kvm(struct kvm *kvm);
240
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241#define HPA_MSB ((sizeof(hpa_t) * 8) - 1)
242#define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB)
243static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; }
Avi Kivity039576c2007-03-20 12:46:50 +0200244struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245
Izik Eiduscea7bb22007-10-17 19:17:48 +0200246extern struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -0500247extern pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248
Izik Eiduscea7bb22007-10-17 19:17:48 +0200249int is_error_page(struct page *page);
Anthony Liguori35149e22008-04-02 14:46:56 -0500250int is_error_pfn(pfn_t pfn);
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200251int kvm_is_error_hva(unsigned long addr);
Izik Eidus210c7c42007-10-24 23:52:57 +0200252int kvm_set_memory_region(struct kvm *kvm,
253 struct kvm_userspace_memory_region *mem,
254 int user_alloc);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800255int __kvm_set_memory_region(struct kvm *kvm,
256 struct kvm_userspace_memory_region *mem,
257 int user_alloc);
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200258int kvm_arch_prepare_memory_region(struct kvm *kvm,
259 struct kvm_memory_slot *memslot,
260 struct kvm_memory_slot old,
261 struct kvm_userspace_memory_region *mem,
262 int user_alloc);
263void kvm_arch_commit_memory_region(struct kvm *kvm,
Zhang Xiantao0de10342007-11-20 16:25:04 +0800264 struct kvm_userspace_memory_region *mem,
265 struct kvm_memory_slot old,
266 int user_alloc);
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300267void kvm_disable_largepages(void);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300268void kvm_arch_flush_shadow(struct kvm *kvm);
Izik Eidus290fc382007-09-27 14:11:22 +0200269gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn);
Marcelo Tosattia983fb22009-12-23 14:35:23 -0200270gfn_t unalias_gfn_instantiation(struct kvm *kvm, gfn_t gfn);
271
Avi Kivity954bbbc2007-03-30 14:02:32 +0300272struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300273unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200274void kvm_release_page_clean(struct page *page);
275void kvm_release_page_dirty(struct page *page);
Anthony Liguori35149e22008-04-02 14:46:56 -0500276void kvm_set_page_dirty(struct page *page);
277void kvm_set_page_accessed(struct page *page);
278
279pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -0200280pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
281 struct kvm_memory_slot *slot, gfn_t gfn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200282int memslot_id(struct kvm *kvm, gfn_t gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -0500283void kvm_release_pfn_dirty(pfn_t);
284void kvm_release_pfn_clean(pfn_t pfn);
285void kvm_set_pfn_dirty(pfn_t pfn);
286void kvm_set_pfn_accessed(pfn_t pfn);
287void kvm_get_pfn(pfn_t pfn);
288
Izik Eidus195aefd2007-10-01 22:14:18 +0200289int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
290 int len);
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500291int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
292 unsigned long len);
Izik Eidus195aefd2007-10-01 22:14:18 +0200293int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
294int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
295 int offset, int len);
296int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
297 unsigned long len);
298int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
299int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800300struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200301int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
303
Hollis Blanchard8776e512007-10-31 17:24:24 -0500304void kvm_vcpu_block(struct kvm_vcpu *vcpu);
Zhai, Edwind255f4f2009-10-09 18:03:20 +0800305void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306void kvm_resched(struct kvm_vcpu *vcpu);
Avi Kivity7702fd12007-06-14 16:27:40 +0300307void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
308void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300309void kvm_flush_remote_tlbs(struct kvm *kvm);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500310void kvm_reload_remote_mmus(struct kvm *kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800311
Carsten Otte043405e2007-10-10 17:16:19 +0200312long kvm_arch_dev_ioctl(struct file *filp,
313 unsigned int ioctl, unsigned long arg);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200314long kvm_arch_vcpu_ioctl(struct file *filp,
315 unsigned int ioctl, unsigned long arg);
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800316
317int kvm_dev_ioctl_check_extension(long ext);
318
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800319int kvm_get_dirty_log(struct kvm *kvm,
320 struct kvm_dirty_log *log, int *is_dirty);
321int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
322 struct kvm_dirty_log *log);
323
Carsten Otte1fe779f2007-10-29 16:08:35 +0100324int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
325 struct
326 kvm_userspace_memory_region *mem,
327 int user_alloc);
328long kvm_arch_vm_ioctl(struct file *filp,
329 unsigned int ioctl, unsigned long arg);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200330
Hollis Blanchardd0752062007-10-31 17:24:25 -0500331int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
332int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
333
Zhang Xiantao8b006792007-11-16 13:05:55 +0800334int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
335 struct kvm_translation *tr);
336
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500337int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
338int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
339int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
340 struct kvm_sregs *sregs);
341int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
342 struct kvm_sregs *sregs);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -0300343int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
344 struct kvm_mp_state *mp_state);
345int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
346 struct kvm_mp_state *mp_state);
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100347int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
348 struct kvm_guest_debug *dbg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500349int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run);
350
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +0800351int kvm_arch_init(void *opaque);
352void kvm_arch_exit(void);
Carsten Otte043405e2007-10-10 17:16:19 +0200353
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800354int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu);
355void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu);
356
357void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu);
358void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
359void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
360struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id);
Avi Kivity26e52152007-11-20 15:30:24 +0200361int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -0600362void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800363
364int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu);
Alexander Graf10474ae2009-09-15 11:37:46 +0200365int kvm_arch_hardware_enable(void *garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800366void kvm_arch_hardware_disable(void *garbage);
367int kvm_arch_hardware_setup(void);
368void kvm_arch_hardware_unsetup(void);
369void kvm_arch_check_processor_compat(void *rtn);
Zhang Xiantao1d737c82007-12-14 09:35:10 +0800370int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800371
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800372void kvm_free_physmem(struct kvm *kvm);
373
374struct kvm *kvm_arch_create_vm(void);
375void kvm_arch_destroy_vm(struct kvm *kvm);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800376void kvm_free_all_assigned_devices(struct kvm *kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800377void kvm_arch_sync_events(struct kvm *kvm);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800378
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300379int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu);
Zhang Xiantao57361992007-12-17 14:21:40 +0800380void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
Zhang Xiantao682c59a2007-12-11 20:36:00 +0800381
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800382int kvm_is_mmio_pfn(pfn_t pfn);
383
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300384struct kvm_irq_ack_notifier {
385 struct hlist_node link;
386 unsigned gsi;
387 void (*irq_acked)(struct kvm_irq_ack_notifier *kian);
388};
389
Sheng Yang2350bd12009-02-25 17:22:27 +0800390#define KVM_ASSIGNED_MSIX_PENDING 0x1
Sheng Yangc1e01512009-02-25 17:22:26 +0800391struct kvm_guest_msix_entry {
392 u32 vector;
393 u16 entry;
394 u16 flags;
395};
396
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300397struct kvm_assigned_dev_kernel {
398 struct kvm_irq_ack_notifier ack_notifier;
399 struct work_struct interrupt_work;
400 struct list_head list;
401 int assigned_dev_id;
402 int host_busnr;
403 int host_devfn;
Sheng Yangc1e01512009-02-25 17:22:26 +0800404 unsigned int entries_nr;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300405 int host_irq;
Mark McLoughlindefaf152008-12-02 12:16:33 +0000406 bool host_irq_disabled;
Sheng Yangc1e01512009-02-25 17:22:26 +0800407 struct msix_entry *host_msix_entries;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300408 int guest_irq;
Sheng Yangc1e01512009-02-25 17:22:26 +0800409 struct kvm_guest_msix_entry *guest_msix_entries;
Sheng Yang4f906c12008-11-24 14:32:51 +0800410 unsigned long irq_requested_type;
Sheng Yang5550af42008-10-15 20:15:06 +0800411 int irq_source_id;
Weidong Hanb6535742008-12-08 23:29:53 +0800412 int flags;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300413 struct pci_dev *dev;
414 struct kvm *kvm;
Marcelo Tosatti547de292009-05-07 17:55:13 -0300415 spinlock_t assigned_dev_lock;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300416};
Avi Kivity75858a82009-01-04 17:10:50 +0200417
418struct kvm_irq_mask_notifier {
419 void (*func)(struct kvm_irq_mask_notifier *kimn, bool masked);
420 int irq;
421 struct hlist_node link;
422};
423
424void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
425 struct kvm_irq_mask_notifier *kimn);
426void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
427 struct kvm_irq_mask_notifier *kimn);
428void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask);
429
Gleb Natapov46e624b2009-08-24 11:54:20 +0300430#ifdef __KVM_HAVE_IOAPIC
431void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
432 union kvm_ioapic_redirect_entry *entry,
433 unsigned long *deliver_bitmask);
434#endif
435int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200436void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800437void kvm_register_irq_ack_notifier(struct kvm *kvm,
438 struct kvm_irq_ack_notifier *kian);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300439void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
440 struct kvm_irq_ack_notifier *kian);
Sheng Yang5550af42008-10-15 20:15:06 +0800441int kvm_request_irq_source_id(struct kvm *kvm);
442void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300443
Sheng Yang522c68c2009-04-27 20:35:43 +0800444/* For vcpu->arch.iommu_flags */
445#define KVM_IOMMU_CACHE_COHERENCY 0x1
446
Joerg Roedel19de40a2008-12-03 14:43:34 +0100447#ifdef CONFIG_IOMMU_API
Marcelo Tosatti3ad26d82009-12-23 14:35:20 -0200448int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
Weidong Han260782b2008-12-02 21:03:39 +0800449int kvm_iommu_map_guest(struct kvm *kvm);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300450int kvm_iommu_unmap_guest(struct kvm *kvm);
Weidong Han260782b2008-12-02 21:03:39 +0800451int kvm_assign_device(struct kvm *kvm,
452 struct kvm_assigned_dev_kernel *assigned_dev);
Weidong Han0a920352008-12-02 21:24:23 +0800453int kvm_deassign_device(struct kvm *kvm,
454 struct kvm_assigned_dev_kernel *assigned_dev);
Joerg Roedel19de40a2008-12-03 14:43:34 +0100455#else /* CONFIG_IOMMU_API */
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300456static inline int kvm_iommu_map_pages(struct kvm *kvm,
457 gfn_t base_gfn,
458 unsigned long npages)
459{
460 return 0;
461}
462
Weidong Han260782b2008-12-02 21:03:39 +0800463static inline int kvm_iommu_map_guest(struct kvm *kvm)
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300464{
465 return -ENODEV;
466}
467
468static inline int kvm_iommu_unmap_guest(struct kvm *kvm)
469{
470 return 0;
471}
Weidong Han260782b2008-12-02 21:03:39 +0800472
473static inline int kvm_assign_device(struct kvm *kvm,
474 struct kvm_assigned_dev_kernel *assigned_dev)
475{
476 return 0;
477}
Weidong Han0a920352008-12-02 21:24:23 +0800478
479static inline int kvm_deassign_device(struct kvm *kvm,
480 struct kvm_assigned_dev_kernel *assigned_dev)
481{
482 return 0;
483}
Joerg Roedel19de40a2008-12-03 14:43:34 +0100484#endif /* CONFIG_IOMMU_API */
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300485
Laurent Vivierd172fcd2007-10-15 17:00:19 +0200486static inline void kvm_guest_enter(void)
487{
Christian Borntraegere56a7a22007-10-18 14:39:10 +0200488 account_system_vtime(current);
Laurent Vivierd172fcd2007-10-15 17:00:19 +0200489 current->flags |= PF_VCPU;
490}
491
492static inline void kvm_guest_exit(void)
493{
Christian Borntraegere56a7a22007-10-18 14:39:10 +0200494 account_system_vtime(current);
Laurent Vivierd172fcd2007-10-15 17:00:19 +0200495 current->flags &= ~PF_VCPU;
496}
497
Avi Kivity1755fbc2007-11-21 14:44:45 +0200498static inline gpa_t gfn_to_gpa(gfn_t gfn)
499{
500 return (gpa_t)gfn << PAGE_SHIFT;
501}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300503static inline hpa_t pfn_to_hpa(pfn_t pfn)
504{
505 return (hpa_t)pfn << PAGE_SHIFT;
506}
507
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300508static inline void kvm_migrate_timers(struct kvm_vcpu *vcpu)
Avi Kivity2f52d582008-01-16 12:49:30 +0200509{
510 set_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests);
511}
512
Avi Kivityba1389b2007-11-18 16:24:12 +0200513enum kvm_stat_kind {
514 KVM_STAT_VM,
515 KVM_STAT_VCPU,
516};
517
Hollis Blanchard417bc302007-10-31 17:24:23 -0500518struct kvm_stats_debugfs_item {
519 const char *name;
520 int offset;
Avi Kivityba1389b2007-11-18 16:24:12 +0200521 enum kvm_stat_kind kind;
Hollis Blanchard417bc302007-10-31 17:24:23 -0500522 struct dentry *dentry;
523};
524extern struct kvm_stats_debugfs_item debugfs_entries[];
Hollis Blanchard76f7c872008-04-15 16:05:42 -0500525extern struct dentry *kvm_debugfs_dir;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -0400526
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200527#ifdef KVM_ARCH_WANT_MMU_NOTIFIER
528static inline int mmu_notifier_retry(struct kvm_vcpu *vcpu, unsigned long mmu_seq)
529{
530 if (unlikely(vcpu->kvm->mmu_notifier_count))
531 return 1;
532 /*
533 * Both reads happen under the mmu_lock and both values are
534 * modified under mmu_lock, so there's no need of smb_rmb()
535 * here in between, otherwise mmu_notifier_count should be
536 * read before mmu_notifier_seq, see
537 * mmu_notifier_invalidate_range_end write side.
538 */
539 if (vcpu->kvm->mmu_notifier_seq != mmu_seq)
540 return 1;
541 return 0;
542}
543#endif
544
Marcelo Tosattia983fb22009-12-23 14:35:23 -0200545#ifndef KVM_ARCH_HAS_UNALIAS_INSTANTIATION
546#define unalias_gfn_instantiation unalias_gfn
547#endif
548
Avi Kivity399ec802008-11-19 13:58:46 +0200549#ifdef CONFIG_HAVE_KVM_IRQCHIP
550
551#define KVM_MAX_IRQ_ROUTES 1024
552
553int kvm_setup_default_irq_routing(struct kvm *kvm);
554int kvm_set_irq_routing(struct kvm *kvm,
555 const struct kvm_irq_routing_entry *entries,
556 unsigned nr,
557 unsigned flags);
558void kvm_free_irq_routing(struct kvm *kvm);
559
560#else
561
562static inline void kvm_free_irq_routing(struct kvm *kvm) {}
563
564#endif
565
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400566#ifdef CONFIG_HAVE_KVM_EVENTFD
567
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400568void kvm_eventfd_init(struct kvm *kvm);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400569int kvm_irqfd(struct kvm *kvm, int fd, int gsi, int flags);
570void kvm_irqfd_release(struct kvm *kvm);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400571int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400572
573#else
574
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400575static inline void kvm_eventfd_init(struct kvm *kvm) {}
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400576static inline int kvm_irqfd(struct kvm *kvm, int fd, int gsi, int flags)
577{
578 return -EINVAL;
579}
580
581static inline void kvm_irqfd_release(struct kvm *kvm) {}
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400582static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
583{
584 return -ENOSYS;
585}
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400586
587#endif /* CONFIG_HAVE_KVM_EVENTFD */
588
Gleb Natapov73880c82009-06-09 15:56:28 +0300589#ifdef CONFIG_KVM_APIC_ARCHITECTURE
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300590static inline bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
591{
Marcelo Tosattid3efc8e2009-06-17 10:07:59 -0300592 return vcpu->kvm->bsp_vcpu_id == vcpu->vcpu_id;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300593}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594#endif
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300595
596#ifdef __KVM_HAVE_DEVICE_ASSIGNMENT
597
598long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
599 unsigned long arg);
600
601#else
602
603static inline long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
604 unsigned long arg)
605{
606 return -ENOTTY;
607}
608
Gleb Natapov73880c82009-06-09 15:56:28 +0300609#endif
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300610
611#endif
612