KVM: add vcpu-specific functions to read/write/translate GFNs
[deliverable/linux.git] / include / linux / kvm_host.h
... / ...
CommitLineData
1#ifndef __KVM_HOST_H
2#define __KVM_HOST_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/bug.h>
17#include <linux/mm.h>
18#include <linux/mmu_notifier.h>
19#include <linux/preempt.h>
20#include <linux/msi.h>
21#include <linux/slab.h>
22#include <linux/rcupdate.h>
23#include <linux/ratelimit.h>
24#include <linux/err.h>
25#include <linux/irqflags.h>
26#include <linux/context_tracking.h>
27#include <asm/signal.h>
28
29#include <linux/kvm.h>
30#include <linux/kvm_para.h>
31
32#include <linux/kvm_types.h>
33
34#include <asm/kvm_host.h>
35
36/*
37 * The bit 16 ~ bit 31 of kvm_memory_region::flags are internally used
38 * in kvm, other bits are visible for userspace which are defined in
39 * include/linux/kvm_h.
40 */
41#define KVM_MEMSLOT_INVALID (1UL << 16)
42#define KVM_MEMSLOT_INCOHERENT (1UL << 17)
43
44/* Two fragments for cross MMIO pages. */
45#define KVM_MAX_MMIO_FRAGMENTS 2
46
47/*
48 * For the normal pfn, the highest 12 bits should be zero,
49 * so we can mask bit 62 ~ bit 52 to indicate the error pfn,
50 * mask bit 63 to indicate the noslot pfn.
51 */
52#define KVM_PFN_ERR_MASK (0x7ffULL << 52)
53#define KVM_PFN_ERR_NOSLOT_MASK (0xfffULL << 52)
54#define KVM_PFN_NOSLOT (0x1ULL << 63)
55
56#define KVM_PFN_ERR_FAULT (KVM_PFN_ERR_MASK)
57#define KVM_PFN_ERR_HWPOISON (KVM_PFN_ERR_MASK + 1)
58#define KVM_PFN_ERR_RO_FAULT (KVM_PFN_ERR_MASK + 2)
59
60/*
61 * error pfns indicate that the gfn is in slot but faild to
62 * translate it to pfn on host.
63 */
64static inline bool is_error_pfn(pfn_t pfn)
65{
66 return !!(pfn & KVM_PFN_ERR_MASK);
67}
68
69/*
70 * error_noslot pfns indicate that the gfn can not be
71 * translated to pfn - it is not in slot or failed to
72 * translate it to pfn.
73 */
74static inline bool is_error_noslot_pfn(pfn_t pfn)
75{
76 return !!(pfn & KVM_PFN_ERR_NOSLOT_MASK);
77}
78
79/* noslot pfn indicates that the gfn is not in slot. */
80static inline bool is_noslot_pfn(pfn_t pfn)
81{
82 return pfn == KVM_PFN_NOSLOT;
83}
84
85/*
86 * architectures with KVM_HVA_ERR_BAD other than PAGE_OFFSET (e.g. s390)
87 * provide own defines and kvm_is_error_hva
88 */
89#ifndef KVM_HVA_ERR_BAD
90
91#define KVM_HVA_ERR_BAD (PAGE_OFFSET)
92#define KVM_HVA_ERR_RO_BAD (PAGE_OFFSET + PAGE_SIZE)
93
94static inline bool kvm_is_error_hva(unsigned long addr)
95{
96 return addr >= PAGE_OFFSET;
97}
98
99#endif
100
101#define KVM_ERR_PTR_BAD_PAGE (ERR_PTR(-ENOENT))
102
103static inline bool is_error_page(struct page *page)
104{
105 return IS_ERR(page);
106}
107
108/*
109 * vcpu->requests bit members
110 */
111#define KVM_REQ_TLB_FLUSH 0
112#define KVM_REQ_MIGRATE_TIMER 1
113#define KVM_REQ_REPORT_TPR_ACCESS 2
114#define KVM_REQ_MMU_RELOAD 3
115#define KVM_REQ_TRIPLE_FAULT 4
116#define KVM_REQ_PENDING_TIMER 5
117#define KVM_REQ_UNHALT 6
118#define KVM_REQ_MMU_SYNC 7
119#define KVM_REQ_CLOCK_UPDATE 8
120#define KVM_REQ_KICK 9
121#define KVM_REQ_DEACTIVATE_FPU 10
122#define KVM_REQ_EVENT 11
123#define KVM_REQ_APF_HALT 12
124#define KVM_REQ_STEAL_UPDATE 13
125#define KVM_REQ_NMI 14
126#define KVM_REQ_PMU 15
127#define KVM_REQ_PMI 16
128#define KVM_REQ_WATCHDOG 17
129#define KVM_REQ_MASTERCLOCK_UPDATE 18
130#define KVM_REQ_MCLOCK_INPROGRESS 19
131#define KVM_REQ_EPR_EXIT 20
132#define KVM_REQ_SCAN_IOAPIC 21
133#define KVM_REQ_GLOBAL_CLOCK_UPDATE 22
134#define KVM_REQ_ENABLE_IBS 23
135#define KVM_REQ_DISABLE_IBS 24
136#define KVM_REQ_APIC_PAGE_RELOAD 25
137#define KVM_REQ_SMI 26
138
139#define KVM_USERSPACE_IRQ_SOURCE_ID 0
140#define KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID 1
141
142extern struct kmem_cache *kvm_vcpu_cache;
143
144extern spinlock_t kvm_lock;
145extern struct list_head vm_list;
146
147struct kvm_io_range {
148 gpa_t addr;
149 int len;
150 struct kvm_io_device *dev;
151};
152
153#define NR_IOBUS_DEVS 1000
154
155struct kvm_io_bus {
156 int dev_count;
157 int ioeventfd_count;
158 struct kvm_io_range range[];
159};
160
161enum kvm_bus {
162 KVM_MMIO_BUS,
163 KVM_PIO_BUS,
164 KVM_VIRTIO_CCW_NOTIFY_BUS,
165 KVM_FAST_MMIO_BUS,
166 KVM_NR_BUSES
167};
168
169int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
170 int len, const void *val);
171int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx,
172 gpa_t addr, int len, const void *val, long cookie);
173int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
174 int len, void *val);
175int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
176 int len, struct kvm_io_device *dev);
177int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
178 struct kvm_io_device *dev);
179
180#ifdef CONFIG_KVM_ASYNC_PF
181struct kvm_async_pf {
182 struct work_struct work;
183 struct list_head link;
184 struct list_head queue;
185 struct kvm_vcpu *vcpu;
186 struct mm_struct *mm;
187 gva_t gva;
188 unsigned long addr;
189 struct kvm_arch_async_pf arch;
190 bool wakeup_all;
191};
192
193void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu);
194void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu);
195int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, unsigned long hva,
196 struct kvm_arch_async_pf *arch);
197int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu);
198#endif
199
200enum {
201 OUTSIDE_GUEST_MODE,
202 IN_GUEST_MODE,
203 EXITING_GUEST_MODE,
204 READING_SHADOW_PAGE_TABLES,
205};
206
207/*
208 * Sometimes a large or cross-page mmio needs to be broken up into separate
209 * exits for userspace servicing.
210 */
211struct kvm_mmio_fragment {
212 gpa_t gpa;
213 void *data;
214 unsigned len;
215};
216
217struct kvm_vcpu {
218 struct kvm *kvm;
219#ifdef CONFIG_PREEMPT_NOTIFIERS
220 struct preempt_notifier preempt_notifier;
221#endif
222 int cpu;
223 int vcpu_id;
224 int srcu_idx;
225 int mode;
226 unsigned long requests;
227 unsigned long guest_debug;
228
229 struct mutex mutex;
230 struct kvm_run *run;
231
232 int fpu_active;
233 int guest_fpu_loaded, guest_xcr0_loaded;
234 unsigned char fpu_counter;
235 wait_queue_head_t wq;
236 struct pid *pid;
237 int sigset_active;
238 sigset_t sigset;
239 struct kvm_vcpu_stat stat;
240
241#ifdef CONFIG_HAS_IOMEM
242 int mmio_needed;
243 int mmio_read_completed;
244 int mmio_is_write;
245 int mmio_cur_fragment;
246 int mmio_nr_fragments;
247 struct kvm_mmio_fragment mmio_fragments[KVM_MAX_MMIO_FRAGMENTS];
248#endif
249
250#ifdef CONFIG_KVM_ASYNC_PF
251 struct {
252 u32 queued;
253 struct list_head queue;
254 struct list_head done;
255 spinlock_t lock;
256 } async_pf;
257#endif
258
259#ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
260 /*
261 * Cpu relax intercept or pause loop exit optimization
262 * in_spin_loop: set when a vcpu does a pause loop exit
263 * or cpu relax intercepted.
264 * dy_eligible: indicates whether vcpu is eligible for directed yield.
265 */
266 struct {
267 bool in_spin_loop;
268 bool dy_eligible;
269 } spin_loop;
270#endif
271 bool preempted;
272 struct kvm_vcpu_arch arch;
273};
274
275static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu)
276{
277 return cmpxchg(&vcpu->mode, IN_GUEST_MODE, EXITING_GUEST_MODE);
278}
279
280/*
281 * Some of the bitops functions do not support too long bitmaps.
282 * This number must be determined not to exceed such limits.
283 */
284#define KVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1)
285
286struct kvm_memory_slot {
287 gfn_t base_gfn;
288 unsigned long npages;
289 unsigned long *dirty_bitmap;
290 struct kvm_arch_memory_slot arch;
291 unsigned long userspace_addr;
292 u32 flags;
293 short id;
294};
295
296static inline unsigned long kvm_dirty_bitmap_bytes(struct kvm_memory_slot *memslot)
297{
298 return ALIGN(memslot->npages, BITS_PER_LONG) / 8;
299}
300
301struct kvm_s390_adapter_int {
302 u64 ind_addr;
303 u64 summary_addr;
304 u64 ind_offset;
305 u32 summary_offset;
306 u32 adapter_id;
307};
308
309struct kvm_kernel_irq_routing_entry {
310 u32 gsi;
311 u32 type;
312 int (*set)(struct kvm_kernel_irq_routing_entry *e,
313 struct kvm *kvm, int irq_source_id, int level,
314 bool line_status);
315 union {
316 struct {
317 unsigned irqchip;
318 unsigned pin;
319 } irqchip;
320 struct msi_msg msi;
321 struct kvm_s390_adapter_int adapter;
322 };
323 struct hlist_node link;
324};
325
326#ifndef KVM_PRIVATE_MEM_SLOTS
327#define KVM_PRIVATE_MEM_SLOTS 0
328#endif
329
330#ifndef KVM_MEM_SLOTS_NUM
331#define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS)
332#endif
333
334/*
335 * Note:
336 * memslots are not sorted by id anymore, please use id_to_memslot()
337 * to get the memslot by its id.
338 */
339struct kvm_memslots {
340 u64 generation;
341 struct kvm_memory_slot memslots[KVM_MEM_SLOTS_NUM];
342 /* The mapping table from slot id to the index in memslots[]. */
343 short id_to_index[KVM_MEM_SLOTS_NUM];
344 atomic_t lru_slot;
345 int used_slots;
346};
347
348struct kvm {
349 spinlock_t mmu_lock;
350 struct mutex slots_lock;
351 struct mm_struct *mm; /* userspace tied to this vm */
352 struct kvm_memslots *memslots;
353 struct srcu_struct srcu;
354 struct srcu_struct irq_srcu;
355#ifdef CONFIG_KVM_APIC_ARCHITECTURE
356 u32 bsp_vcpu_id;
357#endif
358 struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
359 atomic_t online_vcpus;
360 int last_boosted_vcpu;
361 struct list_head vm_list;
362 struct mutex lock;
363 struct kvm_io_bus *buses[KVM_NR_BUSES];
364#ifdef CONFIG_HAVE_KVM_EVENTFD
365 struct {
366 spinlock_t lock;
367 struct list_head items;
368 struct list_head resampler_list;
369 struct mutex resampler_lock;
370 } irqfds;
371 struct list_head ioeventfds;
372#endif
373 struct kvm_vm_stat stat;
374 struct kvm_arch arch;
375 atomic_t users_count;
376#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
377 struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
378 spinlock_t ring_lock;
379 struct list_head coalesced_zones;
380#endif
381
382 struct mutex irq_lock;
383#ifdef CONFIG_HAVE_KVM_IRQCHIP
384 /*
385 * Update side is protected by irq_lock.
386 */
387 struct kvm_irq_routing_table __rcu *irq_routing;
388#endif
389#ifdef CONFIG_HAVE_KVM_IRQFD
390 struct hlist_head irq_ack_notifier_list;
391#endif
392
393#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
394 struct mmu_notifier mmu_notifier;
395 unsigned long mmu_notifier_seq;
396 long mmu_notifier_count;
397#endif
398 long tlbs_dirty;
399 struct list_head devices;
400};
401
402#define kvm_err(fmt, ...) \
403 pr_err("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
404#define kvm_info(fmt, ...) \
405 pr_info("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
406#define kvm_debug(fmt, ...) \
407 pr_debug("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
408#define kvm_pr_unimpl(fmt, ...) \
409 pr_err_ratelimited("kvm [%i]: " fmt, \
410 task_tgid_nr(current), ## __VA_ARGS__)
411
412/* The guest did something we don't support. */
413#define vcpu_unimpl(vcpu, fmt, ...) \
414 kvm_pr_unimpl("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__)
415
416static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i)
417{
418 smp_rmb();
419 return kvm->vcpus[i];
420}
421
422#define kvm_for_each_vcpu(idx, vcpup, kvm) \
423 for (idx = 0; \
424 idx < atomic_read(&kvm->online_vcpus) && \
425 (vcpup = kvm_get_vcpu(kvm, idx)) != NULL; \
426 idx++)
427
428#define kvm_for_each_memslot(memslot, slots) \
429 for (memslot = &slots->memslots[0]; \
430 memslot < slots->memslots + KVM_MEM_SLOTS_NUM && memslot->npages;\
431 memslot++)
432
433int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
434void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
435
436int __must_check vcpu_load(struct kvm_vcpu *vcpu);
437void vcpu_put(struct kvm_vcpu *vcpu);
438
439#ifdef __KVM_HAVE_IOAPIC
440void kvm_vcpu_request_scan_ioapic(struct kvm *kvm);
441#else
442static inline void kvm_vcpu_request_scan_ioapic(struct kvm *kvm)
443{
444}
445#endif
446
447#ifdef CONFIG_HAVE_KVM_IRQFD
448int kvm_irqfd_init(void);
449void kvm_irqfd_exit(void);
450#else
451static inline int kvm_irqfd_init(void)
452{
453 return 0;
454}
455
456static inline void kvm_irqfd_exit(void)
457{
458}
459#endif
460int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
461 struct module *module);
462void kvm_exit(void);
463
464void kvm_get_kvm(struct kvm *kvm);
465void kvm_put_kvm(struct kvm *kvm);
466
467static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm)
468{
469 return rcu_dereference_check(kvm->memslots,
470 srcu_read_lock_held(&kvm->srcu)
471 || lockdep_is_held(&kvm->slots_lock));
472}
473
474static inline struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu)
475{
476 return kvm_memslots(vcpu->kvm);
477}
478
479static inline struct kvm_memory_slot *
480id_to_memslot(struct kvm_memslots *slots, int id)
481{
482 int index = slots->id_to_index[id];
483 struct kvm_memory_slot *slot;
484
485 slot = &slots->memslots[index];
486
487 WARN_ON(slot->id != id);
488 return slot;
489}
490
491/*
492 * KVM_SET_USER_MEMORY_REGION ioctl allows the following operations:
493 * - create a new memory slot
494 * - delete an existing memory slot
495 * - modify an existing memory slot
496 * -- move it in the guest physical memory space
497 * -- just change its flags
498 *
499 * Since flags can be changed by some of these operations, the following
500 * differentiation is the best we can do for __kvm_set_memory_region():
501 */
502enum kvm_mr_change {
503 KVM_MR_CREATE,
504 KVM_MR_DELETE,
505 KVM_MR_MOVE,
506 KVM_MR_FLAGS_ONLY,
507};
508
509int kvm_set_memory_region(struct kvm *kvm,
510 const struct kvm_userspace_memory_region *mem);
511int __kvm_set_memory_region(struct kvm *kvm,
512 const struct kvm_userspace_memory_region *mem);
513void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
514 struct kvm_memory_slot *dont);
515int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
516 unsigned long npages);
517void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots);
518int kvm_arch_prepare_memory_region(struct kvm *kvm,
519 struct kvm_memory_slot *memslot,
520 const struct kvm_userspace_memory_region *mem,
521 enum kvm_mr_change change);
522void kvm_arch_commit_memory_region(struct kvm *kvm,
523 const struct kvm_userspace_memory_region *mem,
524 const struct kvm_memory_slot *old,
525 const struct kvm_memory_slot *new,
526 enum kvm_mr_change change);
527bool kvm_largepages_enabled(void);
528void kvm_disable_largepages(void);
529/* flush all memory translations */
530void kvm_arch_flush_shadow_all(struct kvm *kvm);
531/* flush memory translations pointing to 'slot' */
532void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
533 struct kvm_memory_slot *slot);
534
535int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
536 struct page **pages, int nr_pages);
537
538struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
539unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn);
540unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable);
541unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn);
542unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot, gfn_t gfn,
543 bool *writable);
544void kvm_release_page_clean(struct page *page);
545void kvm_release_page_dirty(struct page *page);
546void kvm_set_page_accessed(struct page *page);
547
548pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn);
549pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn);
550pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
551 bool *writable);
552pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn);
553pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn);
554pfn_t __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn, bool atomic,
555 bool *async, bool write_fault, bool *writable);
556
557void kvm_release_pfn_clean(pfn_t pfn);
558void kvm_set_pfn_dirty(pfn_t pfn);
559void kvm_set_pfn_accessed(pfn_t pfn);
560void kvm_get_pfn(pfn_t pfn);
561
562int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
563 int len);
564int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
565 unsigned long len);
566int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
567int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
568 void *data, unsigned long len);
569int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
570 int offset, int len);
571int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
572 unsigned long len);
573int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
574 void *data, unsigned long len);
575int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
576 gpa_t gpa, unsigned long len);
577int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
578int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
579struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
580int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn);
581unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn);
582void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
583
584struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu);
585struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn);
586pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn);
587pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
588struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn);
589unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn);
590unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable);
591int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, int offset,
592 int len);
593int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa, void *data,
594 unsigned long len);
595int kvm_vcpu_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, void *data,
596 unsigned long len);
597int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, const void *data,
598 int offset, int len);
599int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data,
600 unsigned long len);
601void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn);
602
603void kvm_vcpu_block(struct kvm_vcpu *vcpu);
604void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
605int kvm_vcpu_yield_to(struct kvm_vcpu *target);
606void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu);
607void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
608void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
609
610void kvm_flush_remote_tlbs(struct kvm *kvm);
611void kvm_reload_remote_mmus(struct kvm *kvm);
612void kvm_make_mclock_inprogress_request(struct kvm *kvm);
613void kvm_make_scan_ioapic_request(struct kvm *kvm);
614bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req);
615
616long kvm_arch_dev_ioctl(struct file *filp,
617 unsigned int ioctl, unsigned long arg);
618long kvm_arch_vcpu_ioctl(struct file *filp,
619 unsigned int ioctl, unsigned long arg);
620int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf);
621
622int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext);
623
624int kvm_get_dirty_log(struct kvm *kvm,
625 struct kvm_dirty_log *log, int *is_dirty);
626
627int kvm_get_dirty_log_protect(struct kvm *kvm,
628 struct kvm_dirty_log *log, bool *is_dirty);
629
630void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
631 struct kvm_memory_slot *slot,
632 gfn_t gfn_offset,
633 unsigned long mask);
634
635int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
636 struct kvm_dirty_log *log);
637
638int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
639 bool line_status);
640long kvm_arch_vm_ioctl(struct file *filp,
641 unsigned int ioctl, unsigned long arg);
642
643int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
644int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
645
646int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
647 struct kvm_translation *tr);
648
649int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
650int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
651int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
652 struct kvm_sregs *sregs);
653int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
654 struct kvm_sregs *sregs);
655int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
656 struct kvm_mp_state *mp_state);
657int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
658 struct kvm_mp_state *mp_state);
659int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
660 struct kvm_guest_debug *dbg);
661int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run);
662
663int kvm_arch_init(void *opaque);
664void kvm_arch_exit(void);
665
666int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu);
667void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu);
668
669void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu);
670
671void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu);
672void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
673void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
674struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id);
675int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu);
676void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu);
677void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu);
678
679int kvm_arch_hardware_enable(void);
680void kvm_arch_hardware_disable(void);
681int kvm_arch_hardware_setup(void);
682void kvm_arch_hardware_unsetup(void);
683void kvm_arch_check_processor_compat(void *rtn);
684int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
685int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
686
687void *kvm_kvzalloc(unsigned long size);
688
689#ifndef __KVM_HAVE_ARCH_VM_ALLOC
690static inline struct kvm *kvm_arch_alloc_vm(void)
691{
692 return kzalloc(sizeof(struct kvm), GFP_KERNEL);
693}
694
695static inline void kvm_arch_free_vm(struct kvm *kvm)
696{
697 kfree(kvm);
698}
699#endif
700
701#ifdef __KVM_HAVE_ARCH_NONCOHERENT_DMA
702void kvm_arch_register_noncoherent_dma(struct kvm *kvm);
703void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm);
704bool kvm_arch_has_noncoherent_dma(struct kvm *kvm);
705#else
706static inline void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
707{
708}
709
710static inline void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
711{
712}
713
714static inline bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
715{
716 return false;
717}
718#endif
719
720static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
721{
722#ifdef __KVM_HAVE_ARCH_WQP
723 return vcpu->arch.wqp;
724#else
725 return &vcpu->wq;
726#endif
727}
728
729#ifdef __KVM_HAVE_ARCH_INTC_INITIALIZED
730/*
731 * returns true if the virtual interrupt controller is initialized and
732 * ready to accept virtual IRQ. On some architectures the virtual interrupt
733 * controller is dynamically instantiated and this is not always true.
734 */
735bool kvm_arch_intc_initialized(struct kvm *kvm);
736#else
737static inline bool kvm_arch_intc_initialized(struct kvm *kvm)
738{
739 return true;
740}
741#endif
742
743int kvm_arch_init_vm(struct kvm *kvm, unsigned long type);
744void kvm_arch_destroy_vm(struct kvm *kvm);
745void kvm_arch_sync_events(struct kvm *kvm);
746
747int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu);
748void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
749
750bool kvm_is_reserved_pfn(pfn_t pfn);
751
752struct kvm_irq_ack_notifier {
753 struct hlist_node link;
754 unsigned gsi;
755 void (*irq_acked)(struct kvm_irq_ack_notifier *kian);
756};
757
758int kvm_irq_map_gsi(struct kvm *kvm,
759 struct kvm_kernel_irq_routing_entry *entries, int gsi);
760int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin);
761
762int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
763 bool line_status);
764int kvm_set_irq_inatomic(struct kvm *kvm, int irq_source_id, u32 irq, int level);
765int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
766 int irq_source_id, int level, bool line_status);
767bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin);
768void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
769void kvm_register_irq_ack_notifier(struct kvm *kvm,
770 struct kvm_irq_ack_notifier *kian);
771void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
772 struct kvm_irq_ack_notifier *kian);
773int kvm_request_irq_source_id(struct kvm *kvm);
774void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
775
776#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
777int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
778void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
779#else
780static inline int kvm_iommu_map_pages(struct kvm *kvm,
781 struct kvm_memory_slot *slot)
782{
783 return 0;
784}
785
786static inline void kvm_iommu_unmap_pages(struct kvm *kvm,
787 struct kvm_memory_slot *slot)
788{
789}
790#endif
791
792/* must be called with irqs disabled */
793static inline void __kvm_guest_enter(void)
794{
795 guest_enter();
796 /* KVM does not hold any references to rcu protected data when it
797 * switches CPU into a guest mode. In fact switching to a guest mode
798 * is very similar to exiting to userspace from rcu point of view. In
799 * addition CPU may stay in a guest mode for quite a long time (up to
800 * one time slice). Lets treat guest mode as quiescent state, just like
801 * we do with user-mode execution.
802 */
803 if (!context_tracking_cpu_is_enabled())
804 rcu_virt_note_context_switch(smp_processor_id());
805}
806
807/* must be called with irqs disabled */
808static inline void __kvm_guest_exit(void)
809{
810 guest_exit();
811}
812
813static inline void kvm_guest_enter(void)
814{
815 unsigned long flags;
816
817 local_irq_save(flags);
818 __kvm_guest_enter();
819 local_irq_restore(flags);
820}
821
822static inline void kvm_guest_exit(void)
823{
824 unsigned long flags;
825
826 local_irq_save(flags);
827 __kvm_guest_exit();
828 local_irq_restore(flags);
829}
830
831/*
832 * search_memslots() and __gfn_to_memslot() are here because they are
833 * used in non-modular code in arch/powerpc/kvm/book3s_hv_rm_mmu.c.
834 * gfn_to_memslot() itself isn't here as an inline because that would
835 * bloat other code too much.
836 */
837static inline struct kvm_memory_slot *
838search_memslots(struct kvm_memslots *slots, gfn_t gfn)
839{
840 int start = 0, end = slots->used_slots;
841 int slot = atomic_read(&slots->lru_slot);
842 struct kvm_memory_slot *memslots = slots->memslots;
843
844 if (gfn >= memslots[slot].base_gfn &&
845 gfn < memslots[slot].base_gfn + memslots[slot].npages)
846 return &memslots[slot];
847
848 while (start < end) {
849 slot = start + (end - start) / 2;
850
851 if (gfn >= memslots[slot].base_gfn)
852 end = slot;
853 else
854 start = slot + 1;
855 }
856
857 if (gfn >= memslots[start].base_gfn &&
858 gfn < memslots[start].base_gfn + memslots[start].npages) {
859 atomic_set(&slots->lru_slot, start);
860 return &memslots[start];
861 }
862
863 return NULL;
864}
865
866static inline struct kvm_memory_slot *
867__gfn_to_memslot(struct kvm_memslots *slots, gfn_t gfn)
868{
869 return search_memslots(slots, gfn);
870}
871
872static inline unsigned long
873__gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
874{
875 return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE;
876}
877
878static inline int memslot_id(struct kvm *kvm, gfn_t gfn)
879{
880 return gfn_to_memslot(kvm, gfn)->id;
881}
882
883static inline gfn_t
884hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot)
885{
886 gfn_t gfn_offset = (hva - slot->userspace_addr) >> PAGE_SHIFT;
887
888 return slot->base_gfn + gfn_offset;
889}
890
891static inline gpa_t gfn_to_gpa(gfn_t gfn)
892{
893 return (gpa_t)gfn << PAGE_SHIFT;
894}
895
896static inline gfn_t gpa_to_gfn(gpa_t gpa)
897{
898 return (gfn_t)(gpa >> PAGE_SHIFT);
899}
900
901static inline hpa_t pfn_to_hpa(pfn_t pfn)
902{
903 return (hpa_t)pfn << PAGE_SHIFT;
904}
905
906static inline bool kvm_is_error_gpa(struct kvm *kvm, gpa_t gpa)
907{
908 unsigned long hva = gfn_to_hva(kvm, gpa_to_gfn(gpa));
909
910 return kvm_is_error_hva(hva);
911}
912
913static inline void kvm_migrate_timers(struct kvm_vcpu *vcpu)
914{
915 set_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests);
916}
917
918enum kvm_stat_kind {
919 KVM_STAT_VM,
920 KVM_STAT_VCPU,
921};
922
923struct kvm_stats_debugfs_item {
924 const char *name;
925 int offset;
926 enum kvm_stat_kind kind;
927 struct dentry *dentry;
928};
929extern struct kvm_stats_debugfs_item debugfs_entries[];
930extern struct dentry *kvm_debugfs_dir;
931
932#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
933static inline int mmu_notifier_retry(struct kvm *kvm, unsigned long mmu_seq)
934{
935 if (unlikely(kvm->mmu_notifier_count))
936 return 1;
937 /*
938 * Ensure the read of mmu_notifier_count happens before the read
939 * of mmu_notifier_seq. This interacts with the smp_wmb() in
940 * mmu_notifier_invalidate_range_end to make sure that the caller
941 * either sees the old (non-zero) value of mmu_notifier_count or
942 * the new (incremented) value of mmu_notifier_seq.
943 * PowerPC Book3s HV KVM calls this under a per-page lock
944 * rather than under kvm->mmu_lock, for scalability, so
945 * can't rely on kvm->mmu_lock to keep things ordered.
946 */
947 smp_rmb();
948 if (kvm->mmu_notifier_seq != mmu_seq)
949 return 1;
950 return 0;
951}
952#endif
953
954#ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
955
956#ifdef CONFIG_S390
957#define KVM_MAX_IRQ_ROUTES 4096 //FIXME: we can have more than that...
958#else
959#define KVM_MAX_IRQ_ROUTES 1024
960#endif
961
962int kvm_setup_default_irq_routing(struct kvm *kvm);
963int kvm_set_irq_routing(struct kvm *kvm,
964 const struct kvm_irq_routing_entry *entries,
965 unsigned nr,
966 unsigned flags);
967int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
968 const struct kvm_irq_routing_entry *ue);
969void kvm_free_irq_routing(struct kvm *kvm);
970
971#else
972
973static inline void kvm_free_irq_routing(struct kvm *kvm) {}
974
975#endif
976
977int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi);
978
979#ifdef CONFIG_HAVE_KVM_EVENTFD
980
981void kvm_eventfd_init(struct kvm *kvm);
982int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args);
983
984#ifdef CONFIG_HAVE_KVM_IRQFD
985int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args);
986void kvm_irqfd_release(struct kvm *kvm);
987void kvm_irq_routing_update(struct kvm *);
988#else
989static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
990{
991 return -EINVAL;
992}
993
994static inline void kvm_irqfd_release(struct kvm *kvm) {}
995#endif
996
997#else
998
999static inline void kvm_eventfd_init(struct kvm *kvm) {}
1000
1001static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
1002{
1003 return -EINVAL;
1004}
1005
1006static inline void kvm_irqfd_release(struct kvm *kvm) {}
1007
1008#ifdef CONFIG_HAVE_KVM_IRQCHIP
1009static inline void kvm_irq_routing_update(struct kvm *kvm)
1010{
1011}
1012#endif
1013
1014static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
1015{
1016 return -ENOSYS;
1017}
1018
1019#endif /* CONFIG_HAVE_KVM_EVENTFD */
1020
1021#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1022static inline bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
1023{
1024 return vcpu->kvm->bsp_vcpu_id == vcpu->vcpu_id;
1025}
1026
1027static inline bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
1028{
1029 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
1030}
1031
1032bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu);
1033
1034#else
1035
1036static inline bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu) { return true; }
1037
1038#endif
1039
1040static inline void kvm_make_request(int req, struct kvm_vcpu *vcpu)
1041{
1042 set_bit(req, &vcpu->requests);
1043}
1044
1045static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu)
1046{
1047 if (test_bit(req, &vcpu->requests)) {
1048 clear_bit(req, &vcpu->requests);
1049 return true;
1050 } else {
1051 return false;
1052 }
1053}
1054
1055extern bool kvm_rebooting;
1056
1057struct kvm_device {
1058 struct kvm_device_ops *ops;
1059 struct kvm *kvm;
1060 void *private;
1061 struct list_head vm_node;
1062};
1063
1064/* create, destroy, and name are mandatory */
1065struct kvm_device_ops {
1066 const char *name;
1067 int (*create)(struct kvm_device *dev, u32 type);
1068
1069 /*
1070 * Destroy is responsible for freeing dev.
1071 *
1072 * Destroy may be called before or after destructors are called
1073 * on emulated I/O regions, depending on whether a reference is
1074 * held by a vcpu or other kvm component that gets destroyed
1075 * after the emulated I/O.
1076 */
1077 void (*destroy)(struct kvm_device *dev);
1078
1079 int (*set_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
1080 int (*get_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
1081 int (*has_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
1082 long (*ioctl)(struct kvm_device *dev, unsigned int ioctl,
1083 unsigned long arg);
1084};
1085
1086void kvm_device_get(struct kvm_device *dev);
1087void kvm_device_put(struct kvm_device *dev);
1088struct kvm_device *kvm_device_from_filp(struct file *filp);
1089int kvm_register_device_ops(struct kvm_device_ops *ops, u32 type);
1090void kvm_unregister_device_ops(u32 type);
1091
1092extern struct kvm_device_ops kvm_mpic_ops;
1093extern struct kvm_device_ops kvm_xics_ops;
1094extern struct kvm_device_ops kvm_arm_vgic_v2_ops;
1095extern struct kvm_device_ops kvm_arm_vgic_v3_ops;
1096
1097#ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
1098
1099static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
1100{
1101 vcpu->spin_loop.in_spin_loop = val;
1102}
1103static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
1104{
1105 vcpu->spin_loop.dy_eligible = val;
1106}
1107
1108#else /* !CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
1109
1110static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
1111{
1112}
1113
1114static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
1115{
1116}
1117#endif /* CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
1118#endif
1119
This page took 0.029405 seconds and 5 git commands to generate.