blob: 9ea7149e741de6f6b06ab21b856a934fdd75989a [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001#ifndef __KVM_H
2#define __KVM_H
3
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>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040018#include <asm/signal.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Avi Kivity6aa8b732006-12-10 02:21:36 -080020#include <linux/kvm.h>
Ingo Molnar102d8322007-02-19 14:37:47 +020021#include <linux/kvm_para.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022
Hollis Blanchardd77a39d2007-12-03 15:30:23 -060023#include "types.h"
24
Zhang Xiantaod657a982007-12-14 09:41:22 +080025#include "x86.h"
26
Avi Kivityef9254d2007-06-05 14:37:09 +030027#define KVM_MAX_VCPUS 4
Avi Kivitye8207542007-03-30 16:54:30 +030028#define KVM_ALIAS_SLOTS 4
Izik Eidus2e2c6182007-08-20 18:11:00 +030029#define KVM_MEMORY_SLOTS 8
Izik Eiduse0d62c72007-10-24 23:57:46 +020030/* memory slots that does not exposed to userspace */
31#define KVM_PRIVATE_MEM_SLOTS 4
Avi Kivity6aa8b732006-12-10 02:21:36 -080032
Avi Kivity039576c2007-03-20 12:46:50 +020033#define KVM_PIO_PAGE_OFFSET 1
34
Avi Kivity6aa8b732006-12-10 02:21:36 -080035/*
Avi Kivityd9e368d2007-06-07 19:18:30 +030036 * vcpu->requests bit members
37 */
Avi Kivity3176bc32007-10-16 17:22:08 +020038#define KVM_REQ_TLB_FLUSH 0
Avi Kivityd9e368d2007-06-07 19:18:30 +030039
Avi Kivity6aa8b732006-12-10 02:21:36 -080040
Avi Kivity6aa8b732006-12-10 02:21:36 -080041struct kvm_vcpu;
Rusty Russellc16f8622007-07-30 21:12:19 +100042extern struct kmem_cache *kvm_vcpu_cache;
Avi Kivity6aa8b732006-12-10 02:21:36 -080043
Avi Kivity6aa8b732006-12-10 02:21:36 -080044struct kvm_guest_debug {
45 int enabled;
46 unsigned long bp[4];
47 int singlestep;
48};
49
Gregory Haskins2eeb2e92007-05-31 14:08:53 -040050/*
51 * It would be nice to use something smarter than a linear search, TBD...
52 * Thankfully we dont expect many devices to register (famous last words :),
53 * so until then it will suffice. At least its abstracted so we can change
54 * in one place.
55 */
56struct kvm_io_bus {
57 int dev_count;
58#define NR_IOBUS_DEVS 6
59 struct kvm_io_device *devs[NR_IOBUS_DEVS];
60};
61
62void kvm_io_bus_init(struct kvm_io_bus *bus);
63void kvm_io_bus_destroy(struct kvm_io_bus *bus);
64struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr);
65void kvm_io_bus_register_dev(struct kvm_io_bus *bus,
66 struct kvm_io_device *dev);
67
Zhang Xiantaod657a982007-12-14 09:41:22 +080068struct kvm_vcpu {
Zhang Xiantaod17fbbf2007-12-14 09:45:31 +080069 struct kvm *kvm;
70 struct preempt_notifier preempt_notifier;
71 int vcpu_id;
72 struct mutex mutex;
73 int cpu;
74 struct kvm_run *run;
75 int guest_mode;
76 unsigned long requests;
77 struct kvm_guest_debug guest_debug;
78 int fpu_active;
79 int guest_fpu_loaded;
80 wait_queue_head_t wq;
81 int sigset_active;
82 sigset_t sigset;
83 struct kvm_vcpu_stat stat;
84
85#ifdef CONFIG_HAS_IOMEM
86 int mmio_needed;
87 int mmio_read_completed;
88 int mmio_is_write;
89 int mmio_size;
90 unsigned char mmio_data[8];
91 gpa_t mmio_phys_addr;
92#endif
Zhang Xiantaod657a982007-12-14 09:41:22 +080093
94 struct kvm_vcpu_arch arch;
95};
96
Avi Kivitye8207542007-03-30 16:54:30 +030097struct kvm_mem_alias {
98 gfn_t base_gfn;
99 unsigned long npages;
100 gfn_t target_gfn;
101};
102
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103struct kvm_memory_slot {
104 gfn_t base_gfn;
105 unsigned long npages;
106 unsigned long flags;
Izik Eidus290fc382007-09-27 14:11:22 +0200107 unsigned long *rmap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108 unsigned long *dirty_bitmap;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200109 unsigned long userspace_addr;
Izik Eidus80b14b52007-10-25 11:54:04 +0200110 int user_alloc;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800111};
112
Avi Kivityba1389b2007-11-18 16:24:12 +0200113struct kvm_vm_stat {
Avi Kivity4cee5762007-11-18 16:37:07 +0200114 u32 mmu_shadow_zapped;
115 u32 mmu_pte_write;
116 u32 mmu_pte_updated;
117 u32 mmu_pde_zapped;
118 u32 mmu_flooded;
119 u32 mmu_recycled;
Avi Kivity0f74a242007-11-20 23:01:14 +0200120 u32 remote_tlb_flush;
Avi Kivityba1389b2007-11-18 16:24:12 +0200121};
122
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123struct kvm {
Shaohua Li11ec2802007-07-23 14:51:37 +0800124 struct mutex lock; /* protects everything except vcpus */
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200125 struct mm_struct *mm; /* userspace tied to this vm */
Avi Kivitye8207542007-03-30 16:54:30 +0300126 int naliases;
127 struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128 int nmemslots;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200129 struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS +
130 KVM_PRIVATE_MEM_SLOTS];
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800131 /*
132 * Hash table of struct kvm_mmu_page.
133 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800134 struct list_head active_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200135 unsigned int n_free_mmu_pages;
136 unsigned int n_requested_mmu_pages;
137 unsigned int n_alloc_mmu_pages;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800138 struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000139 struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
Avi Kivity133de902007-02-12 00:54:44 -0800140 struct list_head vm_list;
Avi Kivitybccf2152007-02-21 18:04:26 +0200141 struct file *filp;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400142 struct kvm_io_bus mmio_bus;
Eddie Dong74906342007-06-19 18:05:03 +0300143 struct kvm_io_bus pio_bus;
Eddie Dong85f455f2007-07-06 12:20:49 +0300144 struct kvm_pic *vpic;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300145 struct kvm_ioapic *vioapic;
He, Qing932f72a2007-09-03 17:01:36 +0300146 int round_robin_prev_vcpu;
Izik Eiduscbc94022007-10-25 00:29:55 +0200147 unsigned int tss_addr;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800148 struct page *apic_access_page;
Avi Kivityba1389b2007-11-18 16:24:12 +0200149 struct kvm_vm_stat stat;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800150};
151
Rusty Russellf0242472007-08-01 10:48:02 +1000152/* The guest did something we don't support. */
153#define pr_unimpl(vcpu, fmt, ...) \
154 do { \
155 if (printk_ratelimit()) \
156 printk(KERN_ERR "kvm: %i: cpu%i " fmt, \
157 current->tgid, (vcpu)->vcpu_id , ## __VA_ARGS__); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400158 } while (0)
Rusty Russellf0242472007-08-01 10:48:02 +1000159
Avi Kivity6aa8b732006-12-10 02:21:36 -0800160#define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt)
161#define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt)
162
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000163int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
164void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
165
Carsten Otte313a3dc2007-10-11 19:16:52 +0200166void vcpu_load(struct kvm_vcpu *vcpu);
167void vcpu_put(struct kvm_vcpu *vcpu);
168
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800169void decache_vcpus_on_cpu(int cpu);
170
Carsten Otte313a3dc2007-10-11 19:16:52 +0200171
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +0800172int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +1000173 struct module *module);
Zhang Xiantaocb498ea2007-11-14 20:39:31 +0800174void kvm_exit(void);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176#define HPA_MSB ((sizeof(hpa_t) * 8) - 1)
177#define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB)
178static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; }
Avi Kivity039576c2007-03-20 12:46:50 +0200179struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180
Izik Eiduscea7bb22007-10-17 19:17:48 +0200181extern struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182
Izik Eiduscea7bb22007-10-17 19:17:48 +0200183int is_error_page(struct page *page);
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200184int kvm_is_error_hva(unsigned long addr);
Izik Eidus210c7c42007-10-24 23:52:57 +0200185int kvm_set_memory_region(struct kvm *kvm,
186 struct kvm_userspace_memory_region *mem,
187 int user_alloc);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800188int __kvm_set_memory_region(struct kvm *kvm,
189 struct kvm_userspace_memory_region *mem,
190 int user_alloc);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800191int kvm_arch_set_memory_region(struct kvm *kvm,
192 struct kvm_userspace_memory_region *mem,
193 struct kvm_memory_slot old,
194 int user_alloc);
Izik Eidus290fc382007-09-27 14:11:22 +0200195gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300196struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200197void kvm_release_page_clean(struct page *page);
198void kvm_release_page_dirty(struct page *page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200199int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
200 int len);
201int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
202int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
203 int offset, int len);
204int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
205 unsigned long len);
206int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
207int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200209int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800210void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
211
Hollis Blanchard8776e512007-10-31 17:24:24 -0500212void kvm_vcpu_block(struct kvm_vcpu *vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800213void kvm_resched(struct kvm_vcpu *vcpu);
Avi Kivity7702fd12007-06-14 16:27:40 +0300214void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
215void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300216void kvm_flush_remote_tlbs(struct kvm *kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217
Carsten Otte043405e2007-10-10 17:16:19 +0200218long kvm_arch_dev_ioctl(struct file *filp,
219 unsigned int ioctl, unsigned long arg);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200220long kvm_arch_vcpu_ioctl(struct file *filp,
221 unsigned int ioctl, unsigned long arg);
222void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
223void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800224
225int kvm_dev_ioctl_check_extension(long ext);
226
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800227int kvm_get_dirty_log(struct kvm *kvm,
228 struct kvm_dirty_log *log, int *is_dirty);
229int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
230 struct kvm_dirty_log *log);
231
Carsten Otte1fe779f2007-10-29 16:08:35 +0100232int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
233 struct
234 kvm_userspace_memory_region *mem,
235 int user_alloc);
236long kvm_arch_vm_ioctl(struct file *filp,
237 unsigned int ioctl, unsigned long arg);
238void kvm_arch_destroy_vm(struct kvm *kvm);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200239
Hollis Blanchardd0752062007-10-31 17:24:25 -0500240int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
241int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
242
Zhang Xiantao8b006792007-11-16 13:05:55 +0800243int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
244 struct kvm_translation *tr);
245
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500246int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
247int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
248int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
249 struct kvm_sregs *sregs);
250int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
251 struct kvm_sregs *sregs);
252int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
253 struct kvm_debug_guest *dbg);
254int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run);
255
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +0800256int kvm_arch_init(void *opaque);
257void kvm_arch_exit(void);
Carsten Otte043405e2007-10-10 17:16:19 +0200258
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800259int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu);
260void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu);
261
262void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu);
263void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
264void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
265struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id);
Avi Kivity26e52152007-11-20 15:30:24 +0200266int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -0600267void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800268
269int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu);
270void kvm_arch_hardware_enable(void *garbage);
271void kvm_arch_hardware_disable(void *garbage);
272int kvm_arch_hardware_setup(void);
273void kvm_arch_hardware_unsetup(void);
274void kvm_arch_check_processor_compat(void *rtn);
Zhang Xiantao1d737c82007-12-14 09:35:10 +0800275int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800276
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800277void kvm_free_physmem(struct kvm *kvm);
278
279struct kvm *kvm_arch_create_vm(void);
280void kvm_arch_destroy_vm(struct kvm *kvm);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800281
Zhang Xiantao682c59a2007-12-11 20:36:00 +0800282int kvm_cpu_get_interrupt(struct kvm_vcpu *v);
283int kvm_cpu_has_interrupt(struct kvm_vcpu *v);
284
Laurent Vivierd172fcd2007-10-15 17:00:19 +0200285static inline void kvm_guest_enter(void)
286{
Christian Borntraegere56a7a22007-10-18 14:39:10 +0200287 account_system_vtime(current);
Laurent Vivierd172fcd2007-10-15 17:00:19 +0200288 current->flags |= PF_VCPU;
289}
290
291static inline void kvm_guest_exit(void)
292{
Christian Borntraegere56a7a22007-10-18 14:39:10 +0200293 account_system_vtime(current);
Laurent Vivierd172fcd2007-10-15 17:00:19 +0200294 current->flags &= ~PF_VCPU;
295}
296
Avi Kivity6aa8b732006-12-10 02:21:36 -0800297static inline int memslot_id(struct kvm *kvm, struct kvm_memory_slot *slot)
298{
299 return slot - kvm->memslots;
300}
301
Avi Kivity1755fbc2007-11-21 14:44:45 +0200302static inline gpa_t gfn_to_gpa(gfn_t gfn)
303{
304 return (gpa_t)gfn << PAGE_SHIFT;
305}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306
Avi Kivityba1389b2007-11-18 16:24:12 +0200307enum kvm_stat_kind {
308 KVM_STAT_VM,
309 KVM_STAT_VCPU,
310};
311
Hollis Blanchard417bc302007-10-31 17:24:23 -0500312struct kvm_stats_debugfs_item {
313 const char *name;
314 int offset;
Avi Kivityba1389b2007-11-18 16:24:12 +0200315 enum kvm_stat_kind kind;
Hollis Blanchard417bc302007-10-31 17:24:23 -0500316 struct dentry *dentry;
317};
318extern struct kvm_stats_debugfs_item debugfs_entries[];
319
Avi Kivity6aa8b732006-12-10 02:21:36 -0800320#endif