KVM: Portability: Expand the KVM_VCPU_COMM in kvm_vcpu structure.
[powerpc.git] / drivers / kvm / kvm.h
1 #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>
10 #include <linux/hardirq.h>
11 #include <linux/list.h>
12 #include <linux/mutex.h>
13 #include <linux/spinlock.h>
14 #include <linux/signal.h>
15 #include <linux/sched.h>
16 #include <linux/mm.h>
17 #include <linux/preempt.h>
18 #include <asm/signal.h>
19
20 #include <linux/kvm.h>
21 #include <linux/kvm_para.h>
22
23 #include "types.h"
24
25 #include "x86.h"
26
27 #define KVM_MAX_VCPUS 4
28 #define KVM_ALIAS_SLOTS 4
29 #define KVM_MEMORY_SLOTS 8
30 /* memory slots that does not exposed to userspace */
31 #define KVM_PRIVATE_MEM_SLOTS 4
32
33 #define KVM_PIO_PAGE_OFFSET 1
34
35 /*
36  * vcpu->requests bit members
37  */
38 #define KVM_REQ_TLB_FLUSH          0
39
40
41 struct kvm_vcpu;
42 extern struct kmem_cache *kvm_vcpu_cache;
43
44 struct kvm_guest_debug {
45         int enabled;
46         unsigned long bp[4];
47         int singlestep;
48 };
49
50 struct kvm_vcpu_stat {
51         u32 pf_fixed;
52         u32 pf_guest;
53         u32 tlb_flush;
54         u32 invlpg;
55
56         u32 exits;
57         u32 io_exits;
58         u32 mmio_exits;
59         u32 signal_exits;
60         u32 irq_window_exits;
61         u32 halt_exits;
62         u32 halt_wakeup;
63         u32 request_irq_exits;
64         u32 irq_exits;
65         u32 host_state_reload;
66         u32 efer_reload;
67         u32 fpu_reload;
68         u32 insn_emulation;
69         u32 insn_emulation_fail;
70 };
71
72 /*
73  * It would be nice to use something smarter than a linear search, TBD...
74  * Thankfully we dont expect many devices to register (famous last words :),
75  * so until then it will suffice.  At least its abstracted so we can change
76  * in one place.
77  */
78 struct kvm_io_bus {
79         int                   dev_count;
80 #define NR_IOBUS_DEVS 6
81         struct kvm_io_device *devs[NR_IOBUS_DEVS];
82 };
83
84 void kvm_io_bus_init(struct kvm_io_bus *bus);
85 void kvm_io_bus_destroy(struct kvm_io_bus *bus);
86 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr);
87 void kvm_io_bus_register_dev(struct kvm_io_bus *bus,
88                              struct kvm_io_device *dev);
89
90 struct kvm_vcpu {
91         struct kvm *kvm;
92         struct preempt_notifier preempt_notifier;
93         int vcpu_id;
94         struct mutex mutex;
95         int   cpu;
96         struct kvm_run *run;
97         int guest_mode;
98         unsigned long requests;
99         struct kvm_guest_debug guest_debug;
100         int fpu_active;
101         int guest_fpu_loaded;
102         wait_queue_head_t wq;
103         int sigset_active;
104         sigset_t sigset;
105         struct kvm_vcpu_stat stat;
106
107 #ifdef CONFIG_HAS_IOMEM
108         int mmio_needed;
109         int mmio_read_completed;
110         int mmio_is_write;
111         int mmio_size;
112         unsigned char mmio_data[8];
113         gpa_t mmio_phys_addr;
114 #endif
115
116         struct kvm_vcpu_arch arch;
117 };
118
119 struct kvm_mem_alias {
120         gfn_t base_gfn;
121         unsigned long npages;
122         gfn_t target_gfn;
123 };
124
125 struct kvm_memory_slot {
126         gfn_t base_gfn;
127         unsigned long npages;
128         unsigned long flags;
129         unsigned long *rmap;
130         unsigned long *dirty_bitmap;
131         unsigned long userspace_addr;
132         int user_alloc;
133 };
134
135 struct kvm_vm_stat {
136         u32 mmu_shadow_zapped;
137         u32 mmu_pte_write;
138         u32 mmu_pte_updated;
139         u32 mmu_pde_zapped;
140         u32 mmu_flooded;
141         u32 mmu_recycled;
142         u32 remote_tlb_flush;
143 };
144
145 struct kvm {
146         struct mutex lock; /* protects everything except vcpus */
147         struct mm_struct *mm; /* userspace tied to this vm */
148         int naliases;
149         struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
150         int nmemslots;
151         struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS +
152                                         KVM_PRIVATE_MEM_SLOTS];
153         /*
154          * Hash table of struct kvm_mmu_page.
155          */
156         struct list_head active_mmu_pages;
157         unsigned int n_free_mmu_pages;
158         unsigned int n_requested_mmu_pages;
159         unsigned int n_alloc_mmu_pages;
160         struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
161         struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
162         struct list_head vm_list;
163         struct file *filp;
164         struct kvm_io_bus mmio_bus;
165         struct kvm_io_bus pio_bus;
166         struct kvm_pic *vpic;
167         struct kvm_ioapic *vioapic;
168         int round_robin_prev_vcpu;
169         unsigned int tss_addr;
170         struct page *apic_access_page;
171         struct kvm_vm_stat stat;
172 };
173
174 /* The guest did something we don't support. */
175 #define pr_unimpl(vcpu, fmt, ...)                                       \
176  do {                                                                   \
177         if (printk_ratelimit())                                         \
178                 printk(KERN_ERR "kvm: %i: cpu%i " fmt,                  \
179                        current->tgid, (vcpu)->vcpu_id , ## __VA_ARGS__); \
180  } while (0)
181
182 #define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt)
183 #define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt)
184
185 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
186 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
187
188 void vcpu_load(struct kvm_vcpu *vcpu);
189 void vcpu_put(struct kvm_vcpu *vcpu);
190
191 void decache_vcpus_on_cpu(int cpu);
192
193
194 int kvm_init(void *opaque, unsigned int vcpu_size,
195                   struct module *module);
196 void kvm_exit(void);
197
198 #define HPA_MSB ((sizeof(hpa_t) * 8) - 1)
199 #define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB)
200 static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; }
201 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva);
202
203 extern struct page *bad_page;
204
205 int is_error_page(struct page *page);
206 int kvm_is_error_hva(unsigned long addr);
207 int kvm_set_memory_region(struct kvm *kvm,
208                           struct kvm_userspace_memory_region *mem,
209                           int user_alloc);
210 int __kvm_set_memory_region(struct kvm *kvm,
211                             struct kvm_userspace_memory_region *mem,
212                             int user_alloc);
213 int kvm_arch_set_memory_region(struct kvm *kvm,
214                                 struct kvm_userspace_memory_region *mem,
215                                 struct kvm_memory_slot old,
216                                 int user_alloc);
217 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn);
218 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
219 void kvm_release_page_clean(struct page *page);
220 void kvm_release_page_dirty(struct page *page);
221 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
222                         int len);
223 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
224 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
225                          int offset, int len);
226 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
227                     unsigned long len);
228 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
229 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
230 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
231 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn);
232 void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
233
234 void kvm_vcpu_block(struct kvm_vcpu *vcpu);
235 void kvm_resched(struct kvm_vcpu *vcpu);
236 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
237 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
238 void kvm_flush_remote_tlbs(struct kvm *kvm);
239
240 long kvm_arch_dev_ioctl(struct file *filp,
241                         unsigned int ioctl, unsigned long arg);
242 long kvm_arch_vcpu_ioctl(struct file *filp,
243                          unsigned int ioctl, unsigned long arg);
244 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
245 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
246
247 int kvm_dev_ioctl_check_extension(long ext);
248
249 int kvm_get_dirty_log(struct kvm *kvm,
250                         struct kvm_dirty_log *log, int *is_dirty);
251 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
252                                 struct kvm_dirty_log *log);
253
254 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
255                                    struct
256                                    kvm_userspace_memory_region *mem,
257                                    int user_alloc);
258 long kvm_arch_vm_ioctl(struct file *filp,
259                        unsigned int ioctl, unsigned long arg);
260 void kvm_arch_destroy_vm(struct kvm *kvm);
261
262 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
263 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
264
265 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
266                                     struct kvm_translation *tr);
267
268 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
269 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
270 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
271                                   struct kvm_sregs *sregs);
272 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
273                                   struct kvm_sregs *sregs);
274 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
275                                     struct kvm_debug_guest *dbg);
276 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run);
277
278 int kvm_arch_init(void *opaque);
279 void kvm_arch_exit(void);
280
281 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu);
282 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu);
283
284 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu);
285 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
286 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
287 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id);
288 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu);
289 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu);
290
291 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu);
292 void kvm_arch_hardware_enable(void *garbage);
293 void kvm_arch_hardware_disable(void *garbage);
294 int kvm_arch_hardware_setup(void);
295 void kvm_arch_hardware_unsetup(void);
296 void kvm_arch_check_processor_compat(void *rtn);
297 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
298
299 void kvm_free_physmem(struct kvm *kvm);
300
301 struct  kvm *kvm_arch_create_vm(void);
302 void kvm_arch_destroy_vm(struct kvm *kvm);
303
304 int kvm_cpu_get_interrupt(struct kvm_vcpu *v);
305 int kvm_cpu_has_interrupt(struct kvm_vcpu *v);
306
307 static inline void kvm_guest_enter(void)
308 {
309         account_system_vtime(current);
310         current->flags |= PF_VCPU;
311 }
312
313 static inline void kvm_guest_exit(void)
314 {
315         account_system_vtime(current);
316         current->flags &= ~PF_VCPU;
317 }
318
319 static inline int memslot_id(struct kvm *kvm, struct kvm_memory_slot *slot)
320 {
321         return slot - kvm->memslots;
322 }
323
324 static inline gpa_t gfn_to_gpa(gfn_t gfn)
325 {
326         return (gpa_t)gfn << PAGE_SHIFT;
327 }
328
329 enum kvm_stat_kind {
330         KVM_STAT_VM,
331         KVM_STAT_VCPU,
332 };
333
334 struct kvm_stats_debugfs_item {
335         const char *name;
336         int offset;
337         enum kvm_stat_kind kind;
338         struct dentry *dentry;
339 };
340 extern struct kvm_stats_debugfs_item debugfs_entries[];
341
342 #endif