Merge remote-tracking branch 'upstream/master' into kvm-devel
[linux-2.6/kvm.git] / include / linux / kvm_host.h
blobc6a2ec925846fd2b264972e4bb677c9c26b4d4a0
1 #ifndef __KVM_HOST_H
2 #define __KVM_HOST_H
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 */
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/mmu_notifier.h>
18 #include <linux/preempt.h>
19 #include <linux/msi.h>
20 #include <linux/slab.h>
21 #include <linux/rcupdate.h>
22 #include <linux/ratelimit.h>
23 #include <asm/signal.h>
25 #include <linux/kvm.h>
26 #include <linux/kvm_para.h>
28 #include <linux/kvm_types.h>
30 #include <asm/kvm_host.h>
32 #ifndef KVM_MMIO_SIZE
33 #define KVM_MMIO_SIZE 8
34 #endif
37 * vcpu->requests bit members
39 #define KVM_REQ_TLB_FLUSH 0
40 #define KVM_REQ_MIGRATE_TIMER 1
41 #define KVM_REQ_REPORT_TPR_ACCESS 2
42 #define KVM_REQ_MMU_RELOAD 3
43 #define KVM_REQ_TRIPLE_FAULT 4
44 #define KVM_REQ_PENDING_TIMER 5
45 #define KVM_REQ_UNHALT 6
46 #define KVM_REQ_MMU_SYNC 7
47 #define KVM_REQ_CLOCK_UPDATE 8
48 #define KVM_REQ_KICK 9
49 #define KVM_REQ_DEACTIVATE_FPU 10
50 #define KVM_REQ_EVENT 11
51 #define KVM_REQ_APF_HALT 12
52 #define KVM_REQ_STEAL_UPDATE 13
53 #define KVM_REQ_NMI 14
54 #define KVM_REQ_IMMEDIATE_EXIT 15
56 #define KVM_USERSPACE_IRQ_SOURCE_ID 0
58 struct kvm;
59 struct kvm_vcpu;
60 extern struct kmem_cache *kvm_vcpu_cache;
62 struct kvm_io_range {
63 gpa_t addr;
64 int len;
65 struct kvm_io_device *dev;
68 struct kvm_io_bus {
69 int dev_count;
70 #define NR_IOBUS_DEVS 300
71 struct kvm_io_range range[NR_IOBUS_DEVS];
74 enum kvm_bus {
75 KVM_MMIO_BUS,
76 KVM_PIO_BUS,
77 KVM_NR_BUSES
80 int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
81 int len, const void *val);
82 int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, int len,
83 void *val);
84 int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
85 int len, struct kvm_io_device *dev);
86 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
87 struct kvm_io_device *dev);
89 #ifdef CONFIG_KVM_ASYNC_PF
90 struct kvm_async_pf {
91 struct work_struct work;
92 struct list_head link;
93 struct list_head queue;
94 struct kvm_vcpu *vcpu;
95 struct mm_struct *mm;
96 gva_t gva;
97 unsigned long addr;
98 struct kvm_arch_async_pf arch;
99 struct page *page;
100 bool done;
103 void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu);
104 void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu);
105 int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
106 struct kvm_arch_async_pf *arch);
107 int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu);
108 #endif
110 enum {
111 OUTSIDE_GUEST_MODE,
112 IN_GUEST_MODE,
113 EXITING_GUEST_MODE
116 struct kvm_vcpu {
117 struct kvm *kvm;
118 #ifdef CONFIG_PREEMPT_NOTIFIERS
119 struct preempt_notifier preempt_notifier;
120 #endif
121 int cpu;
122 int vcpu_id;
123 int srcu_idx;
124 int mode;
125 unsigned long requests;
126 unsigned long guest_debug;
128 struct mutex mutex;
129 struct kvm_run *run;
131 int fpu_active;
132 int guest_fpu_loaded, guest_xcr0_loaded;
133 wait_queue_head_t wq;
134 struct pid *pid;
135 int sigset_active;
136 sigset_t sigset;
137 struct kvm_vcpu_stat stat;
139 #ifdef CONFIG_HAS_IOMEM
140 int mmio_needed;
141 int mmio_read_completed;
142 int mmio_is_write;
143 int mmio_size;
144 int mmio_index;
145 unsigned char mmio_data[KVM_MMIO_SIZE];
146 gpa_t mmio_phys_addr;
147 #endif
149 #ifdef CONFIG_KVM_ASYNC_PF
150 struct {
151 u32 queued;
152 struct list_head queue;
153 struct list_head done;
154 spinlock_t lock;
155 } async_pf;
156 #endif
158 struct kvm_vcpu_arch arch;
161 static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu)
163 return cmpxchg(&vcpu->mode, IN_GUEST_MODE, EXITING_GUEST_MODE);
167 * Some of the bitops functions do not support too long bitmaps.
168 * This number must be determined not to exceed such limits.
170 #define KVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1)
172 struct kvm_lpage_info {
173 unsigned long rmap_pde;
174 int write_count;
177 struct kvm_memory_slot {
178 gfn_t base_gfn;
179 unsigned long npages;
180 unsigned long flags;
181 unsigned long *rmap;
182 unsigned long *dirty_bitmap;
183 unsigned long *dirty_bitmap_head;
184 struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES - 1];
185 unsigned long userspace_addr;
186 int user_alloc;
187 int id;
190 static inline unsigned long kvm_dirty_bitmap_bytes(struct kvm_memory_slot *memslot)
192 return ALIGN(memslot->npages, BITS_PER_LONG) / 8;
195 struct kvm_kernel_irq_routing_entry {
196 u32 gsi;
197 u32 type;
198 int (*set)(struct kvm_kernel_irq_routing_entry *e,
199 struct kvm *kvm, int irq_source_id, int level);
200 union {
201 struct {
202 unsigned irqchip;
203 unsigned pin;
204 } irqchip;
205 struct msi_msg msi;
207 struct hlist_node link;
210 #ifdef __KVM_HAVE_IOAPIC
212 struct kvm_irq_routing_table {
213 int chip[KVM_NR_IRQCHIPS][KVM_IOAPIC_NUM_PINS];
214 struct kvm_kernel_irq_routing_entry *rt_entries;
215 u32 nr_rt_entries;
217 * Array indexed by gsi. Each entry contains list of irq chips
218 * the gsi is connected to.
220 struct hlist_head map[0];
223 #else
225 struct kvm_irq_routing_table {};
227 #endif
229 struct kvm_memslots {
230 int nmemslots;
231 u64 generation;
232 struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS +
233 KVM_PRIVATE_MEM_SLOTS];
236 struct kvm {
237 spinlock_t mmu_lock;
238 struct mutex slots_lock;
239 struct mm_struct *mm; /* userspace tied to this vm */
240 struct kvm_memslots *memslots;
241 struct srcu_struct srcu;
242 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
243 u32 bsp_vcpu_id;
244 struct kvm_vcpu *bsp_vcpu;
245 #endif
246 struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
247 atomic_t online_vcpus;
248 int last_boosted_vcpu;
249 struct list_head vm_list;
250 struct mutex lock;
251 struct kvm_io_bus *buses[KVM_NR_BUSES];
252 #ifdef CONFIG_HAVE_KVM_EVENTFD
253 struct {
254 spinlock_t lock;
255 struct list_head items;
256 } irqfds;
257 struct list_head ioeventfds;
258 #endif
259 struct kvm_vm_stat stat;
260 struct kvm_arch arch;
261 atomic_t users_count;
262 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
263 struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
264 spinlock_t ring_lock;
265 struct list_head coalesced_zones;
266 #endif
268 struct mutex irq_lock;
269 #ifdef CONFIG_HAVE_KVM_IRQCHIP
271 * Update side is protected by irq_lock and,
272 * if configured, irqfds.lock.
274 struct kvm_irq_routing_table __rcu *irq_routing;
275 struct hlist_head mask_notifier_list;
276 struct hlist_head irq_ack_notifier_list;
277 #endif
279 #ifdef KVM_ARCH_WANT_MMU_NOTIFIER
280 struct mmu_notifier mmu_notifier;
281 unsigned long mmu_notifier_seq;
282 long mmu_notifier_count;
283 #endif
284 long tlbs_dirty;
287 /* The guest did something we don't support. */
288 #define pr_unimpl(vcpu, fmt, ...) \
289 pr_err_ratelimited("kvm: %i: cpu%i " fmt, \
290 current->tgid, (vcpu)->vcpu_id , ## __VA_ARGS__)
292 #define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt)
293 #define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt)
295 static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i)
297 smp_rmb();
298 return kvm->vcpus[i];
301 #define kvm_for_each_vcpu(idx, vcpup, kvm) \
302 for (idx = 0; \
303 idx < atomic_read(&kvm->online_vcpus) && \
304 (vcpup = kvm_get_vcpu(kvm, idx)) != NULL; \
305 idx++)
307 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
308 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
310 void vcpu_load(struct kvm_vcpu *vcpu);
311 void vcpu_put(struct kvm_vcpu *vcpu);
313 int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
314 struct module *module);
315 void kvm_exit(void);
317 void kvm_get_kvm(struct kvm *kvm);
318 void kvm_put_kvm(struct kvm *kvm);
320 static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm)
322 return rcu_dereference_check(kvm->memslots,
323 srcu_read_lock_held(&kvm->srcu)
324 || lockdep_is_held(&kvm->slots_lock));
327 #define HPA_MSB ((sizeof(hpa_t) * 8) - 1)
328 #define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB)
329 static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; }
331 extern struct page *bad_page;
332 extern struct page *fault_page;
334 extern pfn_t bad_pfn;
335 extern pfn_t fault_pfn;
337 int is_error_page(struct page *page);
338 int is_error_pfn(pfn_t pfn);
339 int is_hwpoison_pfn(pfn_t pfn);
340 int is_fault_pfn(pfn_t pfn);
341 int is_noslot_pfn(pfn_t pfn);
342 int is_invalid_pfn(pfn_t pfn);
343 int kvm_is_error_hva(unsigned long addr);
344 int kvm_set_memory_region(struct kvm *kvm,
345 struct kvm_userspace_memory_region *mem,
346 int user_alloc);
347 int __kvm_set_memory_region(struct kvm *kvm,
348 struct kvm_userspace_memory_region *mem,
349 int user_alloc);
350 int kvm_arch_prepare_memory_region(struct kvm *kvm,
351 struct kvm_memory_slot *memslot,
352 struct kvm_memory_slot old,
353 struct kvm_userspace_memory_region *mem,
354 int user_alloc);
355 void kvm_arch_commit_memory_region(struct kvm *kvm,
356 struct kvm_userspace_memory_region *mem,
357 struct kvm_memory_slot old,
358 int user_alloc);
359 void kvm_disable_largepages(void);
360 void kvm_arch_flush_shadow(struct kvm *kvm);
362 int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
363 int nr_pages);
365 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
366 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn);
367 void kvm_release_page_clean(struct page *page);
368 void kvm_release_page_dirty(struct page *page);
369 void kvm_set_page_dirty(struct page *page);
370 void kvm_set_page_accessed(struct page *page);
372 pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr);
373 pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn);
374 pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
375 bool write_fault, bool *writable);
376 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn);
377 pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
378 bool *writable);
379 pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
380 struct kvm_memory_slot *slot, gfn_t gfn);
381 void kvm_release_pfn_dirty(pfn_t);
382 void kvm_release_pfn_clean(pfn_t pfn);
383 void kvm_set_pfn_dirty(pfn_t pfn);
384 void kvm_set_pfn_accessed(pfn_t pfn);
385 void kvm_get_pfn(pfn_t pfn);
387 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
388 int len);
389 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
390 unsigned long len);
391 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
392 int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
393 void *data, unsigned long len);
394 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
395 int offset, int len);
396 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
397 unsigned long len);
398 int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
399 void *data, unsigned long len);
400 int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
401 gpa_t gpa);
402 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
403 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
404 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
405 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn);
406 unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn);
407 void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
408 void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
409 gfn_t gfn);
411 void kvm_vcpu_block(struct kvm_vcpu *vcpu);
412 void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu);
413 void kvm_resched(struct kvm_vcpu *vcpu);
414 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
415 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
417 void kvm_flush_remote_tlbs(struct kvm *kvm);
418 void kvm_reload_remote_mmus(struct kvm *kvm);
420 long kvm_arch_dev_ioctl(struct file *filp,
421 unsigned int ioctl, unsigned long arg);
422 long kvm_arch_vcpu_ioctl(struct file *filp,
423 unsigned int ioctl, unsigned long arg);
425 int kvm_dev_ioctl_check_extension(long ext);
427 int kvm_get_dirty_log(struct kvm *kvm,
428 struct kvm_dirty_log *log, int *is_dirty);
429 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
430 struct kvm_dirty_log *log);
432 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
433 struct
434 kvm_userspace_memory_region *mem,
435 int user_alloc);
436 long kvm_arch_vm_ioctl(struct file *filp,
437 unsigned int ioctl, unsigned long arg);
439 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
440 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
442 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
443 struct kvm_translation *tr);
445 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
446 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
447 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
448 struct kvm_sregs *sregs);
449 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
450 struct kvm_sregs *sregs);
451 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
452 struct kvm_mp_state *mp_state);
453 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
454 struct kvm_mp_state *mp_state);
455 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
456 struct kvm_guest_debug *dbg);
457 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run);
459 int kvm_arch_init(void *opaque);
460 void kvm_arch_exit(void);
462 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu);
463 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu);
465 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu);
466 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
467 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
468 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id);
469 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu);
470 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu);
472 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu);
473 int kvm_arch_hardware_enable(void *garbage);
474 void kvm_arch_hardware_disable(void *garbage);
475 int kvm_arch_hardware_setup(void);
476 void kvm_arch_hardware_unsetup(void);
477 void kvm_arch_check_processor_compat(void *rtn);
478 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
480 void kvm_free_physmem(struct kvm *kvm);
482 #ifndef __KVM_HAVE_ARCH_VM_ALLOC
483 static inline struct kvm *kvm_arch_alloc_vm(void)
485 return kzalloc(sizeof(struct kvm), GFP_KERNEL);
488 static inline void kvm_arch_free_vm(struct kvm *kvm)
490 kfree(kvm);
492 #endif
494 int kvm_arch_init_vm(struct kvm *kvm);
495 void kvm_arch_destroy_vm(struct kvm *kvm);
496 void kvm_free_all_assigned_devices(struct kvm *kvm);
497 void kvm_arch_sync_events(struct kvm *kvm);
499 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu);
500 void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
502 int kvm_is_mmio_pfn(pfn_t pfn);
504 struct kvm_irq_ack_notifier {
505 struct hlist_node link;
506 unsigned gsi;
507 void (*irq_acked)(struct kvm_irq_ack_notifier *kian);
510 struct kvm_assigned_dev_kernel {
511 struct kvm_irq_ack_notifier ack_notifier;
512 struct list_head list;
513 int assigned_dev_id;
514 int host_segnr;
515 int host_busnr;
516 int host_devfn;
517 unsigned int entries_nr;
518 int host_irq;
519 bool host_irq_disabled;
520 struct msix_entry *host_msix_entries;
521 int guest_irq;
522 struct msix_entry *guest_msix_entries;
523 unsigned long irq_requested_type;
524 int irq_source_id;
525 int flags;
526 struct pci_dev *dev;
527 struct kvm *kvm;
528 spinlock_t intx_lock;
529 char irq_name[32];
530 struct pci_saved_state *pci_saved_state;
533 struct kvm_irq_mask_notifier {
534 void (*func)(struct kvm_irq_mask_notifier *kimn, bool masked);
535 int irq;
536 struct hlist_node link;
539 void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
540 struct kvm_irq_mask_notifier *kimn);
541 void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
542 struct kvm_irq_mask_notifier *kimn);
543 void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
544 bool mask);
546 #ifdef __KVM_HAVE_IOAPIC
547 void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
548 union kvm_ioapic_redirect_entry *entry,
549 unsigned long *deliver_bitmask);
550 #endif
551 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
552 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
553 int irq_source_id, int level);
554 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
555 void kvm_register_irq_ack_notifier(struct kvm *kvm,
556 struct kvm_irq_ack_notifier *kian);
557 void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
558 struct kvm_irq_ack_notifier *kian);
559 int kvm_request_irq_source_id(struct kvm *kvm);
560 void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
562 /* For vcpu->arch.iommu_flags */
563 #define KVM_IOMMU_CACHE_COHERENCY 0x1
565 #ifdef CONFIG_IOMMU_API
566 int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
567 int kvm_iommu_map_guest(struct kvm *kvm);
568 int kvm_iommu_unmap_guest(struct kvm *kvm);
569 int kvm_assign_device(struct kvm *kvm,
570 struct kvm_assigned_dev_kernel *assigned_dev);
571 int kvm_deassign_device(struct kvm *kvm,
572 struct kvm_assigned_dev_kernel *assigned_dev);
573 #else /* CONFIG_IOMMU_API */
574 static inline int kvm_iommu_map_pages(struct kvm *kvm,
575 struct kvm_memory_slot *slot)
577 return 0;
580 static inline int kvm_iommu_map_guest(struct kvm *kvm)
582 return -ENODEV;
585 static inline int kvm_iommu_unmap_guest(struct kvm *kvm)
587 return 0;
590 static inline int kvm_assign_device(struct kvm *kvm,
591 struct kvm_assigned_dev_kernel *assigned_dev)
593 return 0;
596 static inline int kvm_deassign_device(struct kvm *kvm,
597 struct kvm_assigned_dev_kernel *assigned_dev)
599 return 0;
601 #endif /* CONFIG_IOMMU_API */
603 static inline void kvm_guest_enter(void)
605 BUG_ON(preemptible());
606 account_system_vtime(current);
607 current->flags |= PF_VCPU;
608 /* KVM does not hold any references to rcu protected data when it
609 * switches CPU into a guest mode. In fact switching to a guest mode
610 * is very similar to exiting to userspase from rcu point of view. In
611 * addition CPU may stay in a guest mode for quite a long time (up to
612 * one time slice). Lets treat guest mode as quiescent state, just like
613 * we do with user-mode execution.
615 rcu_virt_note_context_switch(smp_processor_id());
618 static inline void kvm_guest_exit(void)
620 account_system_vtime(current);
621 current->flags &= ~PF_VCPU;
624 static inline int memslot_id(struct kvm *kvm, gfn_t gfn)
626 return gfn_to_memslot(kvm, gfn)->id;
629 static inline unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
630 gfn_t gfn)
632 return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE;
635 static inline gpa_t gfn_to_gpa(gfn_t gfn)
637 return (gpa_t)gfn << PAGE_SHIFT;
640 static inline gfn_t gpa_to_gfn(gpa_t gpa)
642 return (gfn_t)(gpa >> PAGE_SHIFT);
645 static inline hpa_t pfn_to_hpa(pfn_t pfn)
647 return (hpa_t)pfn << PAGE_SHIFT;
650 static inline void kvm_migrate_timers(struct kvm_vcpu *vcpu)
652 set_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests);
655 enum kvm_stat_kind {
656 KVM_STAT_VM,
657 KVM_STAT_VCPU,
660 struct kvm_stats_debugfs_item {
661 const char *name;
662 int offset;
663 enum kvm_stat_kind kind;
664 struct dentry *dentry;
666 extern struct kvm_stats_debugfs_item debugfs_entries[];
667 extern struct dentry *kvm_debugfs_dir;
669 #ifdef KVM_ARCH_WANT_MMU_NOTIFIER
670 static inline int mmu_notifier_retry(struct kvm_vcpu *vcpu, unsigned long mmu_seq)
672 if (unlikely(vcpu->kvm->mmu_notifier_count))
673 return 1;
675 * Both reads happen under the mmu_lock and both values are
676 * modified under mmu_lock, so there's no need of smb_rmb()
677 * here in between, otherwise mmu_notifier_count should be
678 * read before mmu_notifier_seq, see
679 * mmu_notifier_invalidate_range_end write side.
681 if (vcpu->kvm->mmu_notifier_seq != mmu_seq)
682 return 1;
683 return 0;
685 #endif
687 #ifdef CONFIG_HAVE_KVM_IRQCHIP
689 #define KVM_MAX_IRQ_ROUTES 1024
691 int kvm_setup_default_irq_routing(struct kvm *kvm);
692 int kvm_set_irq_routing(struct kvm *kvm,
693 const struct kvm_irq_routing_entry *entries,
694 unsigned nr,
695 unsigned flags);
696 void kvm_free_irq_routing(struct kvm *kvm);
698 #else
700 static inline void kvm_free_irq_routing(struct kvm *kvm) {}
702 #endif
704 #ifdef CONFIG_HAVE_KVM_EVENTFD
706 void kvm_eventfd_init(struct kvm *kvm);
707 int kvm_irqfd(struct kvm *kvm, int fd, int gsi, int flags);
708 void kvm_irqfd_release(struct kvm *kvm);
709 void kvm_irq_routing_update(struct kvm *, struct kvm_irq_routing_table *);
710 int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args);
712 #else
714 static inline void kvm_eventfd_init(struct kvm *kvm) {}
716 static inline int kvm_irqfd(struct kvm *kvm, int fd, int gsi, int flags)
718 return -EINVAL;
721 static inline void kvm_irqfd_release(struct kvm *kvm) {}
723 #ifdef CONFIG_HAVE_KVM_IRQCHIP
724 static inline void kvm_irq_routing_update(struct kvm *kvm,
725 struct kvm_irq_routing_table *irq_rt)
727 rcu_assign_pointer(kvm->irq_routing, irq_rt);
729 #endif
731 static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
733 return -ENOSYS;
736 #endif /* CONFIG_HAVE_KVM_EVENTFD */
738 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
739 static inline bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
741 return vcpu->kvm->bsp_vcpu_id == vcpu->vcpu_id;
743 #endif
745 #ifdef __KVM_HAVE_DEVICE_ASSIGNMENT
747 long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
748 unsigned long arg);
750 #else
752 static inline long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
753 unsigned long arg)
755 return -ENOTTY;
758 #endif
760 static inline void kvm_make_request(int req, struct kvm_vcpu *vcpu)
762 set_bit(req, &vcpu->requests);
765 static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu)
767 if (test_bit(req, &vcpu->requests)) {
768 clear_bit(req, &vcpu->requests);
769 return true;
770 } else {
771 return false;
775 #endif