1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 #ifndef __KVM_HOST_H 3 #define __KVM_HOST_H 4 5 6 #include <linux/types.h> 7 #include <linux/hardirq.h> 8 #include <linux/list.h> 9 #include <linux/mutex.h> 10 #include <linux/spinlock.h> 11 #include <linux/signal.h> 12 #include <linux/sched.h> 13 #include <linux/sched/stat.h> 14 #include <linux/bug.h> 15 #include <linux/minmax.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/vmalloc.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 <linux/irqbypass.h> 28 #include <linux/rcuwait.h> 29 #include <linux/refcount.h> 30 #include <linux/nospec.h> 31 #include <linux/notifier.h> 32 #include <linux/ftrace.h> 33 #include <linux/hashtable.h> 34 #include <linux/instrumentation.h> 35 #include <linux/interval_tree.h> 36 #include <linux/rbtree.h> 37 #include <linux/xarray.h> 38 #include <asm/signal.h> 39 40 #include <linux/kvm.h> 41 #include <linux/kvm_para.h> 42 43 #include <linux/kvm_types.h> 44 45 #include <asm/kvm_host.h> 46 #include <linux/kvm_dirty_ring.h> 47 48 #ifndef KVM_MAX_VCPU_IDS 49 #define KVM_MAX_VCPU_IDS KVM_MAX_VCPUS 50 #endif 51 52 /* 53 * The bit 16 ~ bit 31 of kvm_memory_region::flags are internally used 54 * in kvm, other bits are visible for userspace which are defined in 55 * include/linux/kvm_h. 56 */ 57 #define KVM_MEMSLOT_INVALID (1UL << 16) 58 59 /* 60 * Bit 63 of the memslot generation number is an "update in-progress flag", 61 * e.g. is temporarily set for the duration of install_new_memslots(). 62 * This flag effectively creates a unique generation number that is used to 63 * mark cached memslot data, e.g. MMIO accesses, as potentially being stale, 64 * i.e. may (or may not) have come from the previous memslots generation. 65 * 66 * This is necessary because the actual memslots update is not atomic with 67 * respect to the generation number update. Updating the generation number 68 * first would allow a vCPU to cache a spte from the old memslots using the 69 * new generation number, and updating the generation number after switching 70 * to the new memslots would allow cache hits using the old generation number 71 * to reference the defunct memslots. 72 * 73 * This mechanism is used to prevent getting hits in KVM's caches while a 74 * memslot update is in-progress, and to prevent cache hits *after* updating 75 * the actual generation number against accesses that were inserted into the 76 * cache *before* the memslots were updated. 77 */ 78 #define KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS BIT_ULL(63) 79 80 /* Two fragments for cross MMIO pages. */ 81 #define KVM_MAX_MMIO_FRAGMENTS 2 82 83 #ifndef KVM_ADDRESS_SPACE_NUM 84 #define KVM_ADDRESS_SPACE_NUM 1 85 #endif 86 87 /* 88 * For the normal pfn, the highest 12 bits should be zero, 89 * so we can mask bit 62 ~ bit 52 to indicate the error pfn, 90 * mask bit 63 to indicate the noslot pfn. 91 */ 92 #define KVM_PFN_ERR_MASK (0x7ffULL << 52) 93 #define KVM_PFN_ERR_NOSLOT_MASK (0xfffULL << 52) 94 #define KVM_PFN_NOSLOT (0x1ULL << 63) 95 96 #define KVM_PFN_ERR_FAULT (KVM_PFN_ERR_MASK) 97 #define KVM_PFN_ERR_HWPOISON (KVM_PFN_ERR_MASK + 1) 98 #define KVM_PFN_ERR_RO_FAULT (KVM_PFN_ERR_MASK + 2) 99 #define KVM_PFN_ERR_SIGPENDING (KVM_PFN_ERR_MASK + 3) 100 101 /* 102 * error pfns indicate that the gfn is in slot but faild to 103 * translate it to pfn on host. 104 */ 105 static inline bool is_error_pfn(kvm_pfn_t pfn) 106 { 107 return !!(pfn & KVM_PFN_ERR_MASK); 108 } 109 110 /* 111 * KVM_PFN_ERR_SIGPENDING indicates that fetching the PFN was interrupted 112 * by a pending signal. Note, the signal may or may not be fatal. 113 */ 114 static inline bool is_sigpending_pfn(kvm_pfn_t pfn) 115 { 116 return pfn == KVM_PFN_ERR_SIGPENDING; 117 } 118 119 /* 120 * error_noslot pfns indicate that the gfn can not be 121 * translated to pfn - it is not in slot or failed to 122 * translate it to pfn. 123 */ 124 static inline bool is_error_noslot_pfn(kvm_pfn_t pfn) 125 { 126 return !!(pfn & KVM_PFN_ERR_NOSLOT_MASK); 127 } 128 129 /* noslot pfn indicates that the gfn is not in slot. */ 130 static inline bool is_noslot_pfn(kvm_pfn_t pfn) 131 { 132 return pfn == KVM_PFN_NOSLOT; 133 } 134 135 /* 136 * architectures with KVM_HVA_ERR_BAD other than PAGE_OFFSET (e.g. s390) 137 * provide own defines and kvm_is_error_hva 138 */ 139 #ifndef KVM_HVA_ERR_BAD 140 141 #define KVM_HVA_ERR_BAD (PAGE_OFFSET) 142 #define KVM_HVA_ERR_RO_BAD (PAGE_OFFSET + PAGE_SIZE) 143 144 static inline bool kvm_is_error_hva(unsigned long addr) 145 { 146 return addr >= PAGE_OFFSET; 147 } 148 149 #endif 150 151 #define KVM_ERR_PTR_BAD_PAGE (ERR_PTR(-ENOENT)) 152 153 static inline bool is_error_page(struct page *page) 154 { 155 return IS_ERR(page); 156 } 157 158 #define KVM_REQUEST_MASK GENMASK(7,0) 159 #define KVM_REQUEST_NO_WAKEUP BIT(8) 160 #define KVM_REQUEST_WAIT BIT(9) 161 #define KVM_REQUEST_NO_ACTION BIT(10) 162 /* 163 * Architecture-independent vcpu->requests bit members 164 * Bits 3-7 are reserved for more arch-independent bits. 165 */ 166 #define KVM_REQ_TLB_FLUSH (0 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) 167 #define KVM_REQ_VM_DEAD (1 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) 168 #define KVM_REQ_UNBLOCK 2 169 #define KVM_REQUEST_ARCH_BASE 8 170 171 /* 172 * KVM_REQ_OUTSIDE_GUEST_MODE exists is purely as way to force the vCPU to 173 * OUTSIDE_GUEST_MODE. KVM_REQ_OUTSIDE_GUEST_MODE differs from a vCPU "kick" 174 * in that it ensures the vCPU has reached OUTSIDE_GUEST_MODE before continuing 175 * on. A kick only guarantees that the vCPU is on its way out, e.g. a previous 176 * kick may have set vcpu->mode to EXITING_GUEST_MODE, and so there's no 177 * guarantee the vCPU received an IPI and has actually exited guest mode. 178 */ 179 #define KVM_REQ_OUTSIDE_GUEST_MODE (KVM_REQUEST_NO_ACTION | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) 180 181 #define KVM_ARCH_REQ_FLAGS(nr, flags) ({ \ 182 BUILD_BUG_ON((unsigned)(nr) >= (sizeof_field(struct kvm_vcpu, requests) * 8) - KVM_REQUEST_ARCH_BASE); \ 183 (unsigned)(((nr) + KVM_REQUEST_ARCH_BASE) | (flags)); \ 184 }) 185 #define KVM_ARCH_REQ(nr) KVM_ARCH_REQ_FLAGS(nr, 0) 186 187 bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req, 188 unsigned long *vcpu_bitmap); 189 bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req); 190 bool kvm_make_all_cpus_request_except(struct kvm *kvm, unsigned int req, 191 struct kvm_vcpu *except); 192 bool kvm_make_cpus_request_mask(struct kvm *kvm, unsigned int req, 193 unsigned long *vcpu_bitmap); 194 195 #define KVM_USERSPACE_IRQ_SOURCE_ID 0 196 #define KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID 1 197 198 extern struct mutex kvm_lock; 199 extern struct list_head vm_list; 200 201 struct kvm_io_range { 202 gpa_t addr; 203 int len; 204 struct kvm_io_device *dev; 205 }; 206 207 #define NR_IOBUS_DEVS 1000 208 209 struct kvm_io_bus { 210 int dev_count; 211 int ioeventfd_count; 212 struct kvm_io_range range[]; 213 }; 214 215 enum kvm_bus { 216 KVM_MMIO_BUS, 217 KVM_PIO_BUS, 218 KVM_VIRTIO_CCW_NOTIFY_BUS, 219 KVM_FAST_MMIO_BUS, 220 KVM_NR_BUSES 221 }; 222 223 int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, 224 int len, const void *val); 225 int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, 226 gpa_t addr, int len, const void *val, long cookie); 227 int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, 228 int len, void *val); 229 int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, 230 int len, struct kvm_io_device *dev); 231 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, 232 struct kvm_io_device *dev); 233 struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx, 234 gpa_t addr); 235 236 #ifdef CONFIG_KVM_ASYNC_PF 237 struct kvm_async_pf { 238 struct work_struct work; 239 struct list_head link; 240 struct list_head queue; 241 struct kvm_vcpu *vcpu; 242 struct mm_struct *mm; 243 gpa_t cr2_or_gpa; 244 unsigned long addr; 245 struct kvm_arch_async_pf arch; 246 bool wakeup_all; 247 bool notpresent_injected; 248 }; 249 250 void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu); 251 void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu); 252 bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, 253 unsigned long hva, struct kvm_arch_async_pf *arch); 254 int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu); 255 #endif 256 257 #ifdef KVM_ARCH_WANT_MMU_NOTIFIER 258 struct kvm_gfn_range { 259 struct kvm_memory_slot *slot; 260 gfn_t start; 261 gfn_t end; 262 pte_t pte; 263 bool may_block; 264 }; 265 bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range); 266 bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range); 267 bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range); 268 bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range); 269 #endif 270 271 enum { 272 OUTSIDE_GUEST_MODE, 273 IN_GUEST_MODE, 274 EXITING_GUEST_MODE, 275 READING_SHADOW_PAGE_TABLES, 276 }; 277 278 #define KVM_UNMAPPED_PAGE ((void *) 0x500 + POISON_POINTER_DELTA) 279 280 struct kvm_host_map { 281 /* 282 * Only valid if the 'pfn' is managed by the host kernel (i.e. There is 283 * a 'struct page' for it. When using mem= kernel parameter some memory 284 * can be used as guest memory but they are not managed by host 285 * kernel). 286 * If 'pfn' is not managed by the host kernel, this field is 287 * initialized to KVM_UNMAPPED_PAGE. 288 */ 289 struct page *page; 290 void *hva; 291 kvm_pfn_t pfn; 292 kvm_pfn_t gfn; 293 }; 294 295 /* 296 * Used to check if the mapping is valid or not. Never use 'kvm_host_map' 297 * directly to check for that. 298 */ 299 static inline bool kvm_vcpu_mapped(struct kvm_host_map *map) 300 { 301 return !!map->hva; 302 } 303 304 static inline bool kvm_vcpu_can_poll(ktime_t cur, ktime_t stop) 305 { 306 return single_task_running() && !need_resched() && ktime_before(cur, stop); 307 } 308 309 /* 310 * Sometimes a large or cross-page mmio needs to be broken up into separate 311 * exits for userspace servicing. 312 */ 313 struct kvm_mmio_fragment { 314 gpa_t gpa; 315 void *data; 316 unsigned len; 317 }; 318 319 struct kvm_vcpu { 320 struct kvm *kvm; 321 #ifdef CONFIG_PREEMPT_NOTIFIERS 322 struct preempt_notifier preempt_notifier; 323 #endif 324 int cpu; 325 int vcpu_id; /* id given by userspace at creation */ 326 int vcpu_idx; /* index in kvm->vcpus array */ 327 int ____srcu_idx; /* Don't use this directly. You've been warned. */ 328 #ifdef CONFIG_PROVE_RCU 329 int srcu_depth; 330 #endif 331 int mode; 332 u64 requests; 333 unsigned long guest_debug; 334 335 struct mutex mutex; 336 struct kvm_run *run; 337 338 #ifndef __KVM_HAVE_ARCH_WQP 339 struct rcuwait wait; 340 #endif 341 struct pid __rcu *pid; 342 int sigset_active; 343 sigset_t sigset; 344 unsigned int halt_poll_ns; 345 bool valid_wakeup; 346 347 #ifdef CONFIG_HAS_IOMEM 348 int mmio_needed; 349 int mmio_read_completed; 350 int mmio_is_write; 351 int mmio_cur_fragment; 352 int mmio_nr_fragments; 353 struct kvm_mmio_fragment mmio_fragments[KVM_MAX_MMIO_FRAGMENTS]; 354 #endif 355 356 #ifdef CONFIG_KVM_ASYNC_PF 357 struct { 358 u32 queued; 359 struct list_head queue; 360 struct list_head done; 361 spinlock_t lock; 362 } async_pf; 363 #endif 364 365 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT 366 /* 367 * Cpu relax intercept or pause loop exit optimization 368 * in_spin_loop: set when a vcpu does a pause loop exit 369 * or cpu relax intercepted. 370 * dy_eligible: indicates whether vcpu is eligible for directed yield. 371 */ 372 struct { 373 bool in_spin_loop; 374 bool dy_eligible; 375 } spin_loop; 376 #endif 377 bool preempted; 378 bool ready; 379 struct kvm_vcpu_arch arch; 380 struct kvm_vcpu_stat stat; 381 char stats_id[KVM_STATS_NAME_SIZE]; 382 struct kvm_dirty_ring dirty_ring; 383 384 /* 385 * The most recently used memslot by this vCPU and the slots generation 386 * for which it is valid. 387 * No wraparound protection is needed since generations won't overflow in 388 * thousands of years, even assuming 1M memslot operations per second. 389 */ 390 struct kvm_memory_slot *last_used_slot; 391 u64 last_used_slot_gen; 392 }; 393 394 /* 395 * Start accounting time towards a guest. 396 * Must be called before entering guest context. 397 */ 398 static __always_inline void guest_timing_enter_irqoff(void) 399 { 400 /* 401 * This is running in ioctl context so its safe to assume that it's the 402 * stime pending cputime to flush. 403 */ 404 instrumentation_begin(); 405 vtime_account_guest_enter(); 406 instrumentation_end(); 407 } 408 409 /* 410 * Enter guest context and enter an RCU extended quiescent state. 411 * 412 * Between guest_context_enter_irqoff() and guest_context_exit_irqoff() it is 413 * unsafe to use any code which may directly or indirectly use RCU, tracing 414 * (including IRQ flag tracing), or lockdep. All code in this period must be 415 * non-instrumentable. 416 */ 417 static __always_inline void guest_context_enter_irqoff(void) 418 { 419 /* 420 * KVM does not hold any references to rcu protected data when it 421 * switches CPU into a guest mode. In fact switching to a guest mode 422 * is very similar to exiting to userspace from rcu point of view. In 423 * addition CPU may stay in a guest mode for quite a long time (up to 424 * one time slice). Lets treat guest mode as quiescent state, just like 425 * we do with user-mode execution. 426 */ 427 if (!context_tracking_guest_enter()) { 428 instrumentation_begin(); 429 rcu_virt_note_context_switch(smp_processor_id()); 430 instrumentation_end(); 431 } 432 } 433 434 /* 435 * Deprecated. Architectures should move to guest_timing_enter_irqoff() and 436 * guest_state_enter_irqoff(). 437 */ 438 static __always_inline void guest_enter_irqoff(void) 439 { 440 guest_timing_enter_irqoff(); 441 guest_context_enter_irqoff(); 442 } 443 444 /** 445 * guest_state_enter_irqoff - Fixup state when entering a guest 446 * 447 * Entry to a guest will enable interrupts, but the kernel state is interrupts 448 * disabled when this is invoked. Also tell RCU about it. 449 * 450 * 1) Trace interrupts on state 451 * 2) Invoke context tracking if enabled to adjust RCU state 452 * 3) Tell lockdep that interrupts are enabled 453 * 454 * Invoked from architecture specific code before entering a guest. 455 * Must be called with interrupts disabled and the caller must be 456 * non-instrumentable. 457 * The caller has to invoke guest_timing_enter_irqoff() before this. 458 * 459 * Note: this is analogous to exit_to_user_mode(). 460 */ 461 static __always_inline void guest_state_enter_irqoff(void) 462 { 463 instrumentation_begin(); 464 trace_hardirqs_on_prepare(); 465 lockdep_hardirqs_on_prepare(); 466 instrumentation_end(); 467 468 guest_context_enter_irqoff(); 469 lockdep_hardirqs_on(CALLER_ADDR0); 470 } 471 472 /* 473 * Exit guest context and exit an RCU extended quiescent state. 474 * 475 * Between guest_context_enter_irqoff() and guest_context_exit_irqoff() it is 476 * unsafe to use any code which may directly or indirectly use RCU, tracing 477 * (including IRQ flag tracing), or lockdep. All code in this period must be 478 * non-instrumentable. 479 */ 480 static __always_inline void guest_context_exit_irqoff(void) 481 { 482 context_tracking_guest_exit(); 483 } 484 485 /* 486 * Stop accounting time towards a guest. 487 * Must be called after exiting guest context. 488 */ 489 static __always_inline void guest_timing_exit_irqoff(void) 490 { 491 instrumentation_begin(); 492 /* Flush the guest cputime we spent on the guest */ 493 vtime_account_guest_exit(); 494 instrumentation_end(); 495 } 496 497 /* 498 * Deprecated. Architectures should move to guest_state_exit_irqoff() and 499 * guest_timing_exit_irqoff(). 500 */ 501 static __always_inline void guest_exit_irqoff(void) 502 { 503 guest_context_exit_irqoff(); 504 guest_timing_exit_irqoff(); 505 } 506 507 static inline void guest_exit(void) 508 { 509 unsigned long flags; 510 511 local_irq_save(flags); 512 guest_exit_irqoff(); 513 local_irq_restore(flags); 514 } 515 516 /** 517 * guest_state_exit_irqoff - Establish state when returning from guest mode 518 * 519 * Entry from a guest disables interrupts, but guest mode is traced as 520 * interrupts enabled. Also with NO_HZ_FULL RCU might be idle. 521 * 522 * 1) Tell lockdep that interrupts are disabled 523 * 2) Invoke context tracking if enabled to reactivate RCU 524 * 3) Trace interrupts off state 525 * 526 * Invoked from architecture specific code after exiting a guest. 527 * Must be invoked with interrupts disabled and the caller must be 528 * non-instrumentable. 529 * The caller has to invoke guest_timing_exit_irqoff() after this. 530 * 531 * Note: this is analogous to enter_from_user_mode(). 532 */ 533 static __always_inline void guest_state_exit_irqoff(void) 534 { 535 lockdep_hardirqs_off(CALLER_ADDR0); 536 guest_context_exit_irqoff(); 537 538 instrumentation_begin(); 539 trace_hardirqs_off_finish(); 540 instrumentation_end(); 541 } 542 543 static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu) 544 { 545 /* 546 * The memory barrier ensures a previous write to vcpu->requests cannot 547 * be reordered with the read of vcpu->mode. It pairs with the general 548 * memory barrier following the write of vcpu->mode in VCPU RUN. 549 */ 550 smp_mb__before_atomic(); 551 return cmpxchg(&vcpu->mode, IN_GUEST_MODE, EXITING_GUEST_MODE); 552 } 553 554 /* 555 * Some of the bitops functions do not support too long bitmaps. 556 * This number must be determined not to exceed such limits. 557 */ 558 #define KVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1) 559 560 /* 561 * Since at idle each memslot belongs to two memslot sets it has to contain 562 * two embedded nodes for each data structure that it forms a part of. 563 * 564 * Two memslot sets (one active and one inactive) are necessary so the VM 565 * continues to run on one memslot set while the other is being modified. 566 * 567 * These two memslot sets normally point to the same set of memslots. 568 * They can, however, be desynchronized when performing a memslot management 569 * operation by replacing the memslot to be modified by its copy. 570 * After the operation is complete, both memslot sets once again point to 571 * the same, common set of memslot data. 572 * 573 * The memslots themselves are independent of each other so they can be 574 * individually added or deleted. 575 */ 576 struct kvm_memory_slot { 577 struct hlist_node id_node[2]; 578 struct interval_tree_node hva_node[2]; 579 struct rb_node gfn_node[2]; 580 gfn_t base_gfn; 581 unsigned long npages; 582 unsigned long *dirty_bitmap; 583 struct kvm_arch_memory_slot arch; 584 unsigned long userspace_addr; 585 u32 flags; 586 short id; 587 u16 as_id; 588 }; 589 590 static inline bool kvm_slot_dirty_track_enabled(const struct kvm_memory_slot *slot) 591 { 592 return slot->flags & KVM_MEM_LOG_DIRTY_PAGES; 593 } 594 595 static inline unsigned long kvm_dirty_bitmap_bytes(struct kvm_memory_slot *memslot) 596 { 597 return ALIGN(memslot->npages, BITS_PER_LONG) / 8; 598 } 599 600 static inline unsigned long *kvm_second_dirty_bitmap(struct kvm_memory_slot *memslot) 601 { 602 unsigned long len = kvm_dirty_bitmap_bytes(memslot); 603 604 return memslot->dirty_bitmap + len / sizeof(*memslot->dirty_bitmap); 605 } 606 607 #ifndef KVM_DIRTY_LOG_MANUAL_CAPS 608 #define KVM_DIRTY_LOG_MANUAL_CAPS KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE 609 #endif 610 611 struct kvm_s390_adapter_int { 612 u64 ind_addr; 613 u64 summary_addr; 614 u64 ind_offset; 615 u32 summary_offset; 616 u32 adapter_id; 617 }; 618 619 struct kvm_hv_sint { 620 u32 vcpu; 621 u32 sint; 622 }; 623 624 struct kvm_xen_evtchn { 625 u32 port; 626 u32 vcpu_id; 627 int vcpu_idx; 628 u32 priority; 629 }; 630 631 struct kvm_kernel_irq_routing_entry { 632 u32 gsi; 633 u32 type; 634 int (*set)(struct kvm_kernel_irq_routing_entry *e, 635 struct kvm *kvm, int irq_source_id, int level, 636 bool line_status); 637 union { 638 struct { 639 unsigned irqchip; 640 unsigned pin; 641 } irqchip; 642 struct { 643 u32 address_lo; 644 u32 address_hi; 645 u32 data; 646 u32 flags; 647 u32 devid; 648 } msi; 649 struct kvm_s390_adapter_int adapter; 650 struct kvm_hv_sint hv_sint; 651 struct kvm_xen_evtchn xen_evtchn; 652 }; 653 struct hlist_node link; 654 }; 655 656 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING 657 struct kvm_irq_routing_table { 658 int chip[KVM_NR_IRQCHIPS][KVM_IRQCHIP_NUM_PINS]; 659 u32 nr_rt_entries; 660 /* 661 * Array indexed by gsi. Each entry contains list of irq chips 662 * the gsi is connected to. 663 */ 664 struct hlist_head map[]; 665 }; 666 667 bool kvm_arch_irqchip_in_kernel(struct kvm *kvm); 668 #endif 669 670 #ifndef KVM_INTERNAL_MEM_SLOTS 671 #define KVM_INTERNAL_MEM_SLOTS 0 672 #endif 673 674 #define KVM_MEM_SLOTS_NUM SHRT_MAX 675 #define KVM_USER_MEM_SLOTS (KVM_MEM_SLOTS_NUM - KVM_INTERNAL_MEM_SLOTS) 676 677 #ifndef __KVM_VCPU_MULTIPLE_ADDRESS_SPACE 678 static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu) 679 { 680 return 0; 681 } 682 #endif 683 684 struct kvm_memslots { 685 u64 generation; 686 atomic_long_t last_used_slot; 687 struct rb_root_cached hva_tree; 688 struct rb_root gfn_tree; 689 /* 690 * The mapping table from slot id to memslot. 691 * 692 * 7-bit bucket count matches the size of the old id to index array for 693 * 512 slots, while giving good performance with this slot count. 694 * Higher bucket counts bring only small performance improvements but 695 * always result in higher memory usage (even for lower memslot counts). 696 */ 697 DECLARE_HASHTABLE(id_hash, 7); 698 int node_idx; 699 }; 700 701 struct kvm { 702 #ifdef KVM_HAVE_MMU_RWLOCK 703 rwlock_t mmu_lock; 704 #else 705 spinlock_t mmu_lock; 706 #endif /* KVM_HAVE_MMU_RWLOCK */ 707 708 struct mutex slots_lock; 709 710 /* 711 * Protects the arch-specific fields of struct kvm_memory_slots in 712 * use by the VM. To be used under the slots_lock (above) or in a 713 * kvm->srcu critical section where acquiring the slots_lock would 714 * lead to deadlock with the synchronize_srcu in 715 * install_new_memslots. 716 */ 717 struct mutex slots_arch_lock; 718 struct mm_struct *mm; /* userspace tied to this vm */ 719 unsigned long nr_memslot_pages; 720 /* The two memslot sets - active and inactive (per address space) */ 721 struct kvm_memslots __memslots[KVM_ADDRESS_SPACE_NUM][2]; 722 /* The current active memslot set for each address space */ 723 struct kvm_memslots __rcu *memslots[KVM_ADDRESS_SPACE_NUM]; 724 struct xarray vcpu_array; 725 726 /* Used to wait for completion of MMU notifiers. */ 727 spinlock_t mn_invalidate_lock; 728 unsigned long mn_active_invalidate_count; 729 struct rcuwait mn_memslots_update_rcuwait; 730 731 /* For management / invalidation of gfn_to_pfn_caches */ 732 spinlock_t gpc_lock; 733 struct list_head gpc_list; 734 735 /* 736 * created_vcpus is protected by kvm->lock, and is incremented 737 * at the beginning of KVM_CREATE_VCPU. online_vcpus is only 738 * incremented after storing the kvm_vcpu pointer in vcpus, 739 * and is accessed atomically. 740 */ 741 atomic_t online_vcpus; 742 int max_vcpus; 743 int created_vcpus; 744 int last_boosted_vcpu; 745 struct list_head vm_list; 746 struct mutex lock; 747 struct kvm_io_bus __rcu *buses[KVM_NR_BUSES]; 748 #ifdef CONFIG_HAVE_KVM_EVENTFD 749 struct { 750 spinlock_t lock; 751 struct list_head items; 752 struct list_head resampler_list; 753 struct mutex resampler_lock; 754 } irqfds; 755 struct list_head ioeventfds; 756 #endif 757 struct kvm_vm_stat stat; 758 struct kvm_arch arch; 759 refcount_t users_count; 760 #ifdef CONFIG_KVM_MMIO 761 struct kvm_coalesced_mmio_ring *coalesced_mmio_ring; 762 spinlock_t ring_lock; 763 struct list_head coalesced_zones; 764 #endif 765 766 struct mutex irq_lock; 767 #ifdef CONFIG_HAVE_KVM_IRQCHIP 768 /* 769 * Update side is protected by irq_lock. 770 */ 771 struct kvm_irq_routing_table __rcu *irq_routing; 772 #endif 773 #ifdef CONFIG_HAVE_KVM_IRQFD 774 struct hlist_head irq_ack_notifier_list; 775 #endif 776 777 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) 778 struct mmu_notifier mmu_notifier; 779 unsigned long mmu_invalidate_seq; 780 long mmu_invalidate_in_progress; 781 unsigned long mmu_invalidate_range_start; 782 unsigned long mmu_invalidate_range_end; 783 #endif 784 struct list_head devices; 785 u64 manual_dirty_log_protect; 786 struct dentry *debugfs_dentry; 787 struct kvm_stat_data **debugfs_stat_data; 788 struct srcu_struct srcu; 789 struct srcu_struct irq_srcu; 790 pid_t userspace_pid; 791 unsigned int max_halt_poll_ns; 792 u32 dirty_ring_size; 793 bool vm_bugged; 794 bool vm_dead; 795 796 #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER 797 struct notifier_block pm_notifier; 798 #endif 799 char stats_id[KVM_STATS_NAME_SIZE]; 800 }; 801 802 #define kvm_err(fmt, ...) \ 803 pr_err("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__) 804 #define kvm_info(fmt, ...) \ 805 pr_info("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__) 806 #define kvm_debug(fmt, ...) \ 807 pr_debug("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__) 808 #define kvm_debug_ratelimited(fmt, ...) \ 809 pr_debug_ratelimited("kvm [%i]: " fmt, task_pid_nr(current), \ 810 ## __VA_ARGS__) 811 #define kvm_pr_unimpl(fmt, ...) \ 812 pr_err_ratelimited("kvm [%i]: " fmt, \ 813 task_tgid_nr(current), ## __VA_ARGS__) 814 815 /* The guest did something we don't support. */ 816 #define vcpu_unimpl(vcpu, fmt, ...) \ 817 kvm_pr_unimpl("vcpu%i, guest rIP: 0x%lx " fmt, \ 818 (vcpu)->vcpu_id, kvm_rip_read(vcpu), ## __VA_ARGS__) 819 820 #define vcpu_debug(vcpu, fmt, ...) \ 821 kvm_debug("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__) 822 #define vcpu_debug_ratelimited(vcpu, fmt, ...) \ 823 kvm_debug_ratelimited("vcpu%i " fmt, (vcpu)->vcpu_id, \ 824 ## __VA_ARGS__) 825 #define vcpu_err(vcpu, fmt, ...) \ 826 kvm_err("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__) 827 828 static inline void kvm_vm_dead(struct kvm *kvm) 829 { 830 kvm->vm_dead = true; 831 kvm_make_all_cpus_request(kvm, KVM_REQ_VM_DEAD); 832 } 833 834 static inline void kvm_vm_bugged(struct kvm *kvm) 835 { 836 kvm->vm_bugged = true; 837 kvm_vm_dead(kvm); 838 } 839 840 841 #define KVM_BUG(cond, kvm, fmt...) \ 842 ({ \ 843 int __ret = (cond); \ 844 \ 845 if (WARN_ONCE(__ret && !(kvm)->vm_bugged, fmt)) \ 846 kvm_vm_bugged(kvm); \ 847 unlikely(__ret); \ 848 }) 849 850 #define KVM_BUG_ON(cond, kvm) \ 851 ({ \ 852 int __ret = (cond); \ 853 \ 854 if (WARN_ON_ONCE(__ret && !(kvm)->vm_bugged)) \ 855 kvm_vm_bugged(kvm); \ 856 unlikely(__ret); \ 857 }) 858 859 static inline void kvm_vcpu_srcu_read_lock(struct kvm_vcpu *vcpu) 860 { 861 #ifdef CONFIG_PROVE_RCU 862 WARN_ONCE(vcpu->srcu_depth++, 863 "KVM: Illegal vCPU srcu_idx LOCK, depth=%d", vcpu->srcu_depth - 1); 864 #endif 865 vcpu->____srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 866 } 867 868 static inline void kvm_vcpu_srcu_read_unlock(struct kvm_vcpu *vcpu) 869 { 870 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->____srcu_idx); 871 872 #ifdef CONFIG_PROVE_RCU 873 WARN_ONCE(--vcpu->srcu_depth, 874 "KVM: Illegal vCPU srcu_idx UNLOCK, depth=%d", vcpu->srcu_depth); 875 #endif 876 } 877 878 static inline bool kvm_dirty_log_manual_protect_and_init_set(struct kvm *kvm) 879 { 880 return !!(kvm->manual_dirty_log_protect & KVM_DIRTY_LOG_INITIALLY_SET); 881 } 882 883 static inline struct kvm_io_bus *kvm_get_bus(struct kvm *kvm, enum kvm_bus idx) 884 { 885 return srcu_dereference_check(kvm->buses[idx], &kvm->srcu, 886 lockdep_is_held(&kvm->slots_lock) || 887 !refcount_read(&kvm->users_count)); 888 } 889 890 static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i) 891 { 892 int num_vcpus = atomic_read(&kvm->online_vcpus); 893 i = array_index_nospec(i, num_vcpus); 894 895 /* Pairs with smp_wmb() in kvm_vm_ioctl_create_vcpu. */ 896 smp_rmb(); 897 return xa_load(&kvm->vcpu_array, i); 898 } 899 900 #define kvm_for_each_vcpu(idx, vcpup, kvm) \ 901 xa_for_each_range(&kvm->vcpu_array, idx, vcpup, 0, \ 902 (atomic_read(&kvm->online_vcpus) - 1)) 903 904 static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id) 905 { 906 struct kvm_vcpu *vcpu = NULL; 907 unsigned long i; 908 909 if (id < 0) 910 return NULL; 911 if (id < KVM_MAX_VCPUS) 912 vcpu = kvm_get_vcpu(kvm, id); 913 if (vcpu && vcpu->vcpu_id == id) 914 return vcpu; 915 kvm_for_each_vcpu(i, vcpu, kvm) 916 if (vcpu->vcpu_id == id) 917 return vcpu; 918 return NULL; 919 } 920 921 void kvm_destroy_vcpus(struct kvm *kvm); 922 923 void vcpu_load(struct kvm_vcpu *vcpu); 924 void vcpu_put(struct kvm_vcpu *vcpu); 925 926 #ifdef __KVM_HAVE_IOAPIC 927 void kvm_arch_post_irq_ack_notifier_list_update(struct kvm *kvm); 928 void kvm_arch_post_irq_routing_update(struct kvm *kvm); 929 #else 930 static inline void kvm_arch_post_irq_ack_notifier_list_update(struct kvm *kvm) 931 { 932 } 933 static inline void kvm_arch_post_irq_routing_update(struct kvm *kvm) 934 { 935 } 936 #endif 937 938 #ifdef CONFIG_HAVE_KVM_IRQFD 939 int kvm_irqfd_init(void); 940 void kvm_irqfd_exit(void); 941 #else 942 static inline int kvm_irqfd_init(void) 943 { 944 return 0; 945 } 946 947 static inline void kvm_irqfd_exit(void) 948 { 949 } 950 #endif 951 int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, 952 struct module *module); 953 void kvm_exit(void); 954 955 void kvm_get_kvm(struct kvm *kvm); 956 bool kvm_get_kvm_safe(struct kvm *kvm); 957 void kvm_put_kvm(struct kvm *kvm); 958 bool file_is_kvm(struct file *file); 959 void kvm_put_kvm_no_destroy(struct kvm *kvm); 960 961 static inline struct kvm_memslots *__kvm_memslots(struct kvm *kvm, int as_id) 962 { 963 as_id = array_index_nospec(as_id, KVM_ADDRESS_SPACE_NUM); 964 return srcu_dereference_check(kvm->memslots[as_id], &kvm->srcu, 965 lockdep_is_held(&kvm->slots_lock) || 966 !refcount_read(&kvm->users_count)); 967 } 968 969 static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm) 970 { 971 return __kvm_memslots(kvm, 0); 972 } 973 974 static inline struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu) 975 { 976 int as_id = kvm_arch_vcpu_memslots_id(vcpu); 977 978 return __kvm_memslots(vcpu->kvm, as_id); 979 } 980 981 static inline bool kvm_memslots_empty(struct kvm_memslots *slots) 982 { 983 return RB_EMPTY_ROOT(&slots->gfn_tree); 984 } 985 986 #define kvm_for_each_memslot(memslot, bkt, slots) \ 987 hash_for_each(slots->id_hash, bkt, memslot, id_node[slots->node_idx]) \ 988 if (WARN_ON_ONCE(!memslot->npages)) { \ 989 } else 990 991 static inline 992 struct kvm_memory_slot *id_to_memslot(struct kvm_memslots *slots, int id) 993 { 994 struct kvm_memory_slot *slot; 995 int idx = slots->node_idx; 996 997 hash_for_each_possible(slots->id_hash, slot, id_node[idx], id) { 998 if (slot->id == id) 999 return slot; 1000 } 1001 1002 return NULL; 1003 } 1004 1005 /* Iterator used for walking memslots that overlap a gfn range. */ 1006 struct kvm_memslot_iter { 1007 struct kvm_memslots *slots; 1008 struct rb_node *node; 1009 struct kvm_memory_slot *slot; 1010 }; 1011 1012 static inline void kvm_memslot_iter_next(struct kvm_memslot_iter *iter) 1013 { 1014 iter->node = rb_next(iter->node); 1015 if (!iter->node) 1016 return; 1017 1018 iter->slot = container_of(iter->node, struct kvm_memory_slot, gfn_node[iter->slots->node_idx]); 1019 } 1020 1021 static inline void kvm_memslot_iter_start(struct kvm_memslot_iter *iter, 1022 struct kvm_memslots *slots, 1023 gfn_t start) 1024 { 1025 int idx = slots->node_idx; 1026 struct rb_node *tmp; 1027 struct kvm_memory_slot *slot; 1028 1029 iter->slots = slots; 1030 1031 /* 1032 * Find the so called "upper bound" of a key - the first node that has 1033 * its key strictly greater than the searched one (the start gfn in our case). 1034 */ 1035 iter->node = NULL; 1036 for (tmp = slots->gfn_tree.rb_node; tmp; ) { 1037 slot = container_of(tmp, struct kvm_memory_slot, gfn_node[idx]); 1038 if (start < slot->base_gfn) { 1039 iter->node = tmp; 1040 tmp = tmp->rb_left; 1041 } else { 1042 tmp = tmp->rb_right; 1043 } 1044 } 1045 1046 /* 1047 * Find the slot with the lowest gfn that can possibly intersect with 1048 * the range, so we'll ideally have slot start <= range start 1049 */ 1050 if (iter->node) { 1051 /* 1052 * A NULL previous node means that the very first slot 1053 * already has a higher start gfn. 1054 * In this case slot start > range start. 1055 */ 1056 tmp = rb_prev(iter->node); 1057 if (tmp) 1058 iter->node = tmp; 1059 } else { 1060 /* a NULL node below means no slots */ 1061 iter->node = rb_last(&slots->gfn_tree); 1062 } 1063 1064 if (iter->node) { 1065 iter->slot = container_of(iter->node, struct kvm_memory_slot, gfn_node[idx]); 1066 1067 /* 1068 * It is possible in the slot start < range start case that the 1069 * found slot ends before or at range start (slot end <= range start) 1070 * and so it does not overlap the requested range. 1071 * 1072 * In such non-overlapping case the next slot (if it exists) will 1073 * already have slot start > range start, otherwise the logic above 1074 * would have found it instead of the current slot. 1075 */ 1076 if (iter->slot->base_gfn + iter->slot->npages <= start) 1077 kvm_memslot_iter_next(iter); 1078 } 1079 } 1080 1081 static inline bool kvm_memslot_iter_is_valid(struct kvm_memslot_iter *iter, gfn_t end) 1082 { 1083 if (!iter->node) 1084 return false; 1085 1086 /* 1087 * If this slot starts beyond or at the end of the range so does 1088 * every next one 1089 */ 1090 return iter->slot->base_gfn < end; 1091 } 1092 1093 /* Iterate over each memslot at least partially intersecting [start, end) range */ 1094 #define kvm_for_each_memslot_in_gfn_range(iter, slots, start, end) \ 1095 for (kvm_memslot_iter_start(iter, slots, start); \ 1096 kvm_memslot_iter_is_valid(iter, end); \ 1097 kvm_memslot_iter_next(iter)) 1098 1099 /* 1100 * KVM_SET_USER_MEMORY_REGION ioctl allows the following operations: 1101 * - create a new memory slot 1102 * - delete an existing memory slot 1103 * - modify an existing memory slot 1104 * -- move it in the guest physical memory space 1105 * -- just change its flags 1106 * 1107 * Since flags can be changed by some of these operations, the following 1108 * differentiation is the best we can do for __kvm_set_memory_region(): 1109 */ 1110 enum kvm_mr_change { 1111 KVM_MR_CREATE, 1112 KVM_MR_DELETE, 1113 KVM_MR_MOVE, 1114 KVM_MR_FLAGS_ONLY, 1115 }; 1116 1117 int kvm_set_memory_region(struct kvm *kvm, 1118 const struct kvm_userspace_memory_region *mem); 1119 int __kvm_set_memory_region(struct kvm *kvm, 1120 const struct kvm_userspace_memory_region *mem); 1121 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot); 1122 void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen); 1123 int kvm_arch_prepare_memory_region(struct kvm *kvm, 1124 const struct kvm_memory_slot *old, 1125 struct kvm_memory_slot *new, 1126 enum kvm_mr_change change); 1127 void kvm_arch_commit_memory_region(struct kvm *kvm, 1128 struct kvm_memory_slot *old, 1129 const struct kvm_memory_slot *new, 1130 enum kvm_mr_change change); 1131 /* flush all memory translations */ 1132 void kvm_arch_flush_shadow_all(struct kvm *kvm); 1133 /* flush memory translations pointing to 'slot' */ 1134 void kvm_arch_flush_shadow_memslot(struct kvm *kvm, 1135 struct kvm_memory_slot *slot); 1136 1137 int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn, 1138 struct page **pages, int nr_pages); 1139 1140 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn); 1141 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn); 1142 unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable); 1143 unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn); 1144 unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot, gfn_t gfn, 1145 bool *writable); 1146 void kvm_release_page_clean(struct page *page); 1147 void kvm_release_page_dirty(struct page *page); 1148 1149 kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn); 1150 kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, 1151 bool *writable); 1152 kvm_pfn_t gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn); 1153 kvm_pfn_t gfn_to_pfn_memslot_atomic(const struct kvm_memory_slot *slot, gfn_t gfn); 1154 kvm_pfn_t __gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn, 1155 bool atomic, bool interruptible, bool *async, 1156 bool write_fault, bool *writable, hva_t *hva); 1157 1158 void kvm_release_pfn_clean(kvm_pfn_t pfn); 1159 void kvm_release_pfn_dirty(kvm_pfn_t pfn); 1160 void kvm_set_pfn_dirty(kvm_pfn_t pfn); 1161 void kvm_set_pfn_accessed(kvm_pfn_t pfn); 1162 1163 void kvm_release_pfn(kvm_pfn_t pfn, bool dirty); 1164 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, 1165 int len); 1166 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len); 1167 int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, 1168 void *data, unsigned long len); 1169 int kvm_read_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, 1170 void *data, unsigned int offset, 1171 unsigned long len); 1172 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, 1173 int offset, int len); 1174 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, 1175 unsigned long len); 1176 int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, 1177 void *data, unsigned long len); 1178 int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, 1179 void *data, unsigned int offset, 1180 unsigned long len); 1181 int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc, 1182 gpa_t gpa, unsigned long len); 1183 1184 #define __kvm_get_guest(kvm, gfn, offset, v) \ 1185 ({ \ 1186 unsigned long __addr = gfn_to_hva(kvm, gfn); \ 1187 typeof(v) __user *__uaddr = (typeof(__uaddr))(__addr + offset); \ 1188 int __ret = -EFAULT; \ 1189 \ 1190 if (!kvm_is_error_hva(__addr)) \ 1191 __ret = get_user(v, __uaddr); \ 1192 __ret; \ 1193 }) 1194 1195 #define kvm_get_guest(kvm, gpa, v) \ 1196 ({ \ 1197 gpa_t __gpa = gpa; \ 1198 struct kvm *__kvm = kvm; \ 1199 \ 1200 __kvm_get_guest(__kvm, __gpa >> PAGE_SHIFT, \ 1201 offset_in_page(__gpa), v); \ 1202 }) 1203 1204 #define __kvm_put_guest(kvm, gfn, offset, v) \ 1205 ({ \ 1206 unsigned long __addr = gfn_to_hva(kvm, gfn); \ 1207 typeof(v) __user *__uaddr = (typeof(__uaddr))(__addr + offset); \ 1208 int __ret = -EFAULT; \ 1209 \ 1210 if (!kvm_is_error_hva(__addr)) \ 1211 __ret = put_user(v, __uaddr); \ 1212 if (!__ret) \ 1213 mark_page_dirty(kvm, gfn); \ 1214 __ret; \ 1215 }) 1216 1217 #define kvm_put_guest(kvm, gpa, v) \ 1218 ({ \ 1219 gpa_t __gpa = gpa; \ 1220 struct kvm *__kvm = kvm; \ 1221 \ 1222 __kvm_put_guest(__kvm, __gpa >> PAGE_SHIFT, \ 1223 offset_in_page(__gpa), v); \ 1224 }) 1225 1226 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len); 1227 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn); 1228 bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn); 1229 bool kvm_vcpu_is_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn); 1230 unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn); 1231 void mark_page_dirty_in_slot(struct kvm *kvm, const struct kvm_memory_slot *memslot, gfn_t gfn); 1232 void mark_page_dirty(struct kvm *kvm, gfn_t gfn); 1233 1234 struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu); 1235 struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn); 1236 kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn); 1237 kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn); 1238 int kvm_vcpu_map(struct kvm_vcpu *vcpu, gpa_t gpa, struct kvm_host_map *map); 1239 void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty); 1240 unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn); 1241 unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable); 1242 int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, int offset, 1243 int len); 1244 int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa, void *data, 1245 unsigned long len); 1246 int kvm_vcpu_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, void *data, 1247 unsigned long len); 1248 int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, const void *data, 1249 int offset, int len); 1250 int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data, 1251 unsigned long len); 1252 void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn); 1253 1254 /** 1255 * kvm_gpc_init - initialize gfn_to_pfn_cache. 1256 * 1257 * @gpc: struct gfn_to_pfn_cache object. 1258 * 1259 * This sets up a gfn_to_pfn_cache by initializing locks. Note, the cache must 1260 * be zero-allocated (or zeroed by the caller before init). 1261 */ 1262 void kvm_gpc_init(struct gfn_to_pfn_cache *gpc); 1263 1264 /** 1265 * kvm_gpc_activate - prepare a cached kernel mapping and HPA for a given guest 1266 * physical address. 1267 * 1268 * @kvm: pointer to kvm instance. 1269 * @gpc: struct gfn_to_pfn_cache object. 1270 * @vcpu: vCPU to be used for marking pages dirty and to be woken on 1271 * invalidation. 1272 * @usage: indicates if the resulting host physical PFN is used while 1273 * the @vcpu is IN_GUEST_MODE (in which case invalidation of 1274 * the cache from MMU notifiers---but not for KVM memslot 1275 * changes!---will also force @vcpu to exit the guest and 1276 * refresh the cache); and/or if the PFN used directly 1277 * by KVM (and thus needs a kernel virtual mapping). 1278 * @gpa: guest physical address to map. 1279 * @len: sanity check; the range being access must fit a single page. 1280 * 1281 * @return: 0 for success. 1282 * -EINVAL for a mapping which would cross a page boundary. 1283 * -EFAULT for an untranslatable guest physical address. 1284 * 1285 * This primes a gfn_to_pfn_cache and links it into the @kvm's list for 1286 * invalidations to be processed. Callers are required to use 1287 * kvm_gfn_to_pfn_cache_check() to ensure that the cache is valid before 1288 * accessing the target page. 1289 */ 1290 int kvm_gpc_activate(struct kvm *kvm, struct gfn_to_pfn_cache *gpc, 1291 struct kvm_vcpu *vcpu, enum pfn_cache_usage usage, 1292 gpa_t gpa, unsigned long len); 1293 1294 /** 1295 * kvm_gfn_to_pfn_cache_check - check validity of a gfn_to_pfn_cache. 1296 * 1297 * @kvm: pointer to kvm instance. 1298 * @gpc: struct gfn_to_pfn_cache object. 1299 * @gpa: current guest physical address to map. 1300 * @len: sanity check; the range being access must fit a single page. 1301 * 1302 * @return: %true if the cache is still valid and the address matches. 1303 * %false if the cache is not valid. 1304 * 1305 * Callers outside IN_GUEST_MODE context should hold a read lock on @gpc->lock 1306 * while calling this function, and then continue to hold the lock until the 1307 * access is complete. 1308 * 1309 * Callers in IN_GUEST_MODE may do so without locking, although they should 1310 * still hold a read lock on kvm->scru for the memslot checks. 1311 */ 1312 bool kvm_gfn_to_pfn_cache_check(struct kvm *kvm, struct gfn_to_pfn_cache *gpc, 1313 gpa_t gpa, unsigned long len); 1314 1315 /** 1316 * kvm_gfn_to_pfn_cache_refresh - update a previously initialized cache. 1317 * 1318 * @kvm: pointer to kvm instance. 1319 * @gpc: struct gfn_to_pfn_cache object. 1320 * @gpa: updated guest physical address to map. 1321 * @len: sanity check; the range being access must fit a single page. 1322 * 1323 * @return: 0 for success. 1324 * -EINVAL for a mapping which would cross a page boundary. 1325 * -EFAULT for an untranslatable guest physical address. 1326 * 1327 * This will attempt to refresh a gfn_to_pfn_cache. Note that a successful 1328 * returm from this function does not mean the page can be immediately 1329 * accessed because it may have raced with an invalidation. Callers must 1330 * still lock and check the cache status, as this function does not return 1331 * with the lock still held to permit access. 1332 */ 1333 int kvm_gfn_to_pfn_cache_refresh(struct kvm *kvm, struct gfn_to_pfn_cache *gpc, 1334 gpa_t gpa, unsigned long len); 1335 1336 /** 1337 * kvm_gfn_to_pfn_cache_unmap - temporarily unmap a gfn_to_pfn_cache. 1338 * 1339 * @kvm: pointer to kvm instance. 1340 * @gpc: struct gfn_to_pfn_cache object. 1341 * 1342 * This unmaps the referenced page. The cache is left in the invalid state 1343 * but at least the mapping from GPA to userspace HVA will remain cached 1344 * and can be reused on a subsequent refresh. 1345 */ 1346 void kvm_gfn_to_pfn_cache_unmap(struct kvm *kvm, struct gfn_to_pfn_cache *gpc); 1347 1348 /** 1349 * kvm_gpc_deactivate - deactivate and unlink a gfn_to_pfn_cache. 1350 * 1351 * @kvm: pointer to kvm instance. 1352 * @gpc: struct gfn_to_pfn_cache object. 1353 * 1354 * This removes a cache from the @kvm's list to be processed on MMU notifier 1355 * invocation. 1356 */ 1357 void kvm_gpc_deactivate(struct kvm *kvm, struct gfn_to_pfn_cache *gpc); 1358 1359 void kvm_sigset_activate(struct kvm_vcpu *vcpu); 1360 void kvm_sigset_deactivate(struct kvm_vcpu *vcpu); 1361 1362 void kvm_vcpu_halt(struct kvm_vcpu *vcpu); 1363 bool kvm_vcpu_block(struct kvm_vcpu *vcpu); 1364 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu); 1365 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu); 1366 bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu); 1367 void kvm_vcpu_kick(struct kvm_vcpu *vcpu); 1368 int kvm_vcpu_yield_to(struct kvm_vcpu *target); 1369 void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu, bool usermode_vcpu_not_eligible); 1370 1371 void kvm_flush_remote_tlbs(struct kvm *kvm); 1372 1373 #ifdef KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 1374 int kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int min); 1375 int __kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int capacity, int min); 1376 int kvm_mmu_memory_cache_nr_free_objects(struct kvm_mmu_memory_cache *mc); 1377 void kvm_mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc); 1378 void *kvm_mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc); 1379 #endif 1380 1381 void kvm_mmu_invalidate_begin(struct kvm *kvm, unsigned long start, 1382 unsigned long end); 1383 void kvm_mmu_invalidate_end(struct kvm *kvm, unsigned long start, 1384 unsigned long end); 1385 1386 long kvm_arch_dev_ioctl(struct file *filp, 1387 unsigned int ioctl, unsigned long arg); 1388 long kvm_arch_vcpu_ioctl(struct file *filp, 1389 unsigned int ioctl, unsigned long arg); 1390 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf); 1391 1392 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext); 1393 1394 void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm, 1395 struct kvm_memory_slot *slot, 1396 gfn_t gfn_offset, 1397 unsigned long mask); 1398 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot); 1399 1400 #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT 1401 void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm, 1402 const struct kvm_memory_slot *memslot); 1403 #else /* !CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */ 1404 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log); 1405 int kvm_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log, 1406 int *is_dirty, struct kvm_memory_slot **memslot); 1407 #endif 1408 1409 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, 1410 bool line_status); 1411 int kvm_vm_ioctl_enable_cap(struct kvm *kvm, 1412 struct kvm_enable_cap *cap); 1413 long kvm_arch_vm_ioctl(struct file *filp, 1414 unsigned int ioctl, unsigned long arg); 1415 long kvm_arch_vm_compat_ioctl(struct file *filp, unsigned int ioctl, 1416 unsigned long arg); 1417 1418 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu); 1419 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu); 1420 1421 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 1422 struct kvm_translation *tr); 1423 1424 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs); 1425 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs); 1426 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 1427 struct kvm_sregs *sregs); 1428 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 1429 struct kvm_sregs *sregs); 1430 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 1431 struct kvm_mp_state *mp_state); 1432 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 1433 struct kvm_mp_state *mp_state); 1434 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 1435 struct kvm_guest_debug *dbg); 1436 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu); 1437 1438 int kvm_arch_init(void *opaque); 1439 void kvm_arch_exit(void); 1440 1441 void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu); 1442 1443 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu); 1444 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu); 1445 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id); 1446 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu); 1447 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu); 1448 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu); 1449 1450 #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER 1451 int kvm_arch_pm_notifier(struct kvm *kvm, unsigned long state); 1452 #endif 1453 1454 #ifdef __KVM_HAVE_ARCH_VCPU_DEBUGFS 1455 void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry); 1456 #else 1457 static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {} 1458 #endif 1459 1460 int kvm_arch_hardware_enable(void); 1461 void kvm_arch_hardware_disable(void); 1462 int kvm_arch_hardware_setup(void *opaque); 1463 void kvm_arch_hardware_unsetup(void); 1464 int kvm_arch_check_processor_compat(void *opaque); 1465 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu); 1466 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu); 1467 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); 1468 bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu); 1469 bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu); 1470 int kvm_arch_post_init_vm(struct kvm *kvm); 1471 void kvm_arch_pre_destroy_vm(struct kvm *kvm); 1472 int kvm_arch_create_vm_debugfs(struct kvm *kvm); 1473 1474 #ifndef __KVM_HAVE_ARCH_VM_ALLOC 1475 /* 1476 * All architectures that want to use vzalloc currently also 1477 * need their own kvm_arch_alloc_vm implementation. 1478 */ 1479 static inline struct kvm *kvm_arch_alloc_vm(void) 1480 { 1481 return kzalloc(sizeof(struct kvm), GFP_KERNEL); 1482 } 1483 #endif 1484 1485 static inline void __kvm_arch_free_vm(struct kvm *kvm) 1486 { 1487 kvfree(kvm); 1488 } 1489 1490 #ifndef __KVM_HAVE_ARCH_VM_FREE 1491 static inline void kvm_arch_free_vm(struct kvm *kvm) 1492 { 1493 __kvm_arch_free_vm(kvm); 1494 } 1495 #endif 1496 1497 #ifndef __KVM_HAVE_ARCH_FLUSH_REMOTE_TLB 1498 static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm) 1499 { 1500 return -ENOTSUPP; 1501 } 1502 #endif 1503 1504 #ifdef __KVM_HAVE_ARCH_NONCOHERENT_DMA 1505 void kvm_arch_register_noncoherent_dma(struct kvm *kvm); 1506 void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm); 1507 bool kvm_arch_has_noncoherent_dma(struct kvm *kvm); 1508 #else 1509 static inline void kvm_arch_register_noncoherent_dma(struct kvm *kvm) 1510 { 1511 } 1512 1513 static inline void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm) 1514 { 1515 } 1516 1517 static inline bool kvm_arch_has_noncoherent_dma(struct kvm *kvm) 1518 { 1519 return false; 1520 } 1521 #endif 1522 #ifdef __KVM_HAVE_ARCH_ASSIGNED_DEVICE 1523 void kvm_arch_start_assignment(struct kvm *kvm); 1524 void kvm_arch_end_assignment(struct kvm *kvm); 1525 bool kvm_arch_has_assigned_device(struct kvm *kvm); 1526 #else 1527 static inline void kvm_arch_start_assignment(struct kvm *kvm) 1528 { 1529 } 1530 1531 static inline void kvm_arch_end_assignment(struct kvm *kvm) 1532 { 1533 } 1534 1535 static __always_inline bool kvm_arch_has_assigned_device(struct kvm *kvm) 1536 { 1537 return false; 1538 } 1539 #endif 1540 1541 static inline struct rcuwait *kvm_arch_vcpu_get_wait(struct kvm_vcpu *vcpu) 1542 { 1543 #ifdef __KVM_HAVE_ARCH_WQP 1544 return vcpu->arch.waitp; 1545 #else 1546 return &vcpu->wait; 1547 #endif 1548 } 1549 1550 /* 1551 * Wake a vCPU if necessary, but don't do any stats/metadata updates. Returns 1552 * true if the vCPU was blocking and was awakened, false otherwise. 1553 */ 1554 static inline bool __kvm_vcpu_wake_up(struct kvm_vcpu *vcpu) 1555 { 1556 return !!rcuwait_wake_up(kvm_arch_vcpu_get_wait(vcpu)); 1557 } 1558 1559 static inline bool kvm_vcpu_is_blocking(struct kvm_vcpu *vcpu) 1560 { 1561 return rcuwait_active(kvm_arch_vcpu_get_wait(vcpu)); 1562 } 1563 1564 #ifdef __KVM_HAVE_ARCH_INTC_INITIALIZED 1565 /* 1566 * returns true if the virtual interrupt controller is initialized and 1567 * ready to accept virtual IRQ. On some architectures the virtual interrupt 1568 * controller is dynamically instantiated and this is not always true. 1569 */ 1570 bool kvm_arch_intc_initialized(struct kvm *kvm); 1571 #else 1572 static inline bool kvm_arch_intc_initialized(struct kvm *kvm) 1573 { 1574 return true; 1575 } 1576 #endif 1577 1578 #ifdef CONFIG_GUEST_PERF_EVENTS 1579 unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu); 1580 1581 void kvm_register_perf_callbacks(unsigned int (*pt_intr_handler)(void)); 1582 void kvm_unregister_perf_callbacks(void); 1583 #else 1584 static inline void kvm_register_perf_callbacks(void *ign) {} 1585 static inline void kvm_unregister_perf_callbacks(void) {} 1586 #endif /* CONFIG_GUEST_PERF_EVENTS */ 1587 1588 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type); 1589 void kvm_arch_destroy_vm(struct kvm *kvm); 1590 void kvm_arch_sync_events(struct kvm *kvm); 1591 1592 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu); 1593 1594 struct page *kvm_pfn_to_refcounted_page(kvm_pfn_t pfn); 1595 bool kvm_is_zone_device_page(struct page *page); 1596 1597 struct kvm_irq_ack_notifier { 1598 struct hlist_node link; 1599 unsigned gsi; 1600 void (*irq_acked)(struct kvm_irq_ack_notifier *kian); 1601 }; 1602 1603 int kvm_irq_map_gsi(struct kvm *kvm, 1604 struct kvm_kernel_irq_routing_entry *entries, int gsi); 1605 int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin); 1606 1607 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level, 1608 bool line_status); 1609 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm, 1610 int irq_source_id, int level, bool line_status); 1611 int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e, 1612 struct kvm *kvm, int irq_source_id, 1613 int level, bool line_status); 1614 bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin); 1615 void kvm_notify_acked_gsi(struct kvm *kvm, int gsi); 1616 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin); 1617 void kvm_register_irq_ack_notifier(struct kvm *kvm, 1618 struct kvm_irq_ack_notifier *kian); 1619 void kvm_unregister_irq_ack_notifier(struct kvm *kvm, 1620 struct kvm_irq_ack_notifier *kian); 1621 int kvm_request_irq_source_id(struct kvm *kvm); 1622 void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id); 1623 bool kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args); 1624 1625 /* 1626 * Returns a pointer to the memslot if it contains gfn. 1627 * Otherwise returns NULL. 1628 */ 1629 static inline struct kvm_memory_slot * 1630 try_get_memslot(struct kvm_memory_slot *slot, gfn_t gfn) 1631 { 1632 if (!slot) 1633 return NULL; 1634 1635 if (gfn >= slot->base_gfn && gfn < slot->base_gfn + slot->npages) 1636 return slot; 1637 else 1638 return NULL; 1639 } 1640 1641 /* 1642 * Returns a pointer to the memslot that contains gfn. Otherwise returns NULL. 1643 * 1644 * With "approx" set returns the memslot also when the address falls 1645 * in a hole. In that case one of the memslots bordering the hole is 1646 * returned. 1647 */ 1648 static inline struct kvm_memory_slot * 1649 search_memslots(struct kvm_memslots *slots, gfn_t gfn, bool approx) 1650 { 1651 struct kvm_memory_slot *slot; 1652 struct rb_node *node; 1653 int idx = slots->node_idx; 1654 1655 slot = NULL; 1656 for (node = slots->gfn_tree.rb_node; node; ) { 1657 slot = container_of(node, struct kvm_memory_slot, gfn_node[idx]); 1658 if (gfn >= slot->base_gfn) { 1659 if (gfn < slot->base_gfn + slot->npages) 1660 return slot; 1661 node = node->rb_right; 1662 } else 1663 node = node->rb_left; 1664 } 1665 1666 return approx ? slot : NULL; 1667 } 1668 1669 static inline struct kvm_memory_slot * 1670 ____gfn_to_memslot(struct kvm_memslots *slots, gfn_t gfn, bool approx) 1671 { 1672 struct kvm_memory_slot *slot; 1673 1674 slot = (struct kvm_memory_slot *)atomic_long_read(&slots->last_used_slot); 1675 slot = try_get_memslot(slot, gfn); 1676 if (slot) 1677 return slot; 1678 1679 slot = search_memslots(slots, gfn, approx); 1680 if (slot) { 1681 atomic_long_set(&slots->last_used_slot, (unsigned long)slot); 1682 return slot; 1683 } 1684 1685 return NULL; 1686 } 1687 1688 /* 1689 * __gfn_to_memslot() and its descendants are here to allow arch code to inline 1690 * the lookups in hot paths. gfn_to_memslot() itself isn't here as an inline 1691 * because that would bloat other code too much. 1692 */ 1693 static inline struct kvm_memory_slot * 1694 __gfn_to_memslot(struct kvm_memslots *slots, gfn_t gfn) 1695 { 1696 return ____gfn_to_memslot(slots, gfn, false); 1697 } 1698 1699 static inline unsigned long 1700 __gfn_to_hva_memslot(const struct kvm_memory_slot *slot, gfn_t gfn) 1701 { 1702 /* 1703 * The index was checked originally in search_memslots. To avoid 1704 * that a malicious guest builds a Spectre gadget out of e.g. page 1705 * table walks, do not let the processor speculate loads outside 1706 * the guest's registered memslots. 1707 */ 1708 unsigned long offset = gfn - slot->base_gfn; 1709 offset = array_index_nospec(offset, slot->npages); 1710 return slot->userspace_addr + offset * PAGE_SIZE; 1711 } 1712 1713 static inline int memslot_id(struct kvm *kvm, gfn_t gfn) 1714 { 1715 return gfn_to_memslot(kvm, gfn)->id; 1716 } 1717 1718 static inline gfn_t 1719 hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot) 1720 { 1721 gfn_t gfn_offset = (hva - slot->userspace_addr) >> PAGE_SHIFT; 1722 1723 return slot->base_gfn + gfn_offset; 1724 } 1725 1726 static inline gpa_t gfn_to_gpa(gfn_t gfn) 1727 { 1728 return (gpa_t)gfn << PAGE_SHIFT; 1729 } 1730 1731 static inline gfn_t gpa_to_gfn(gpa_t gpa) 1732 { 1733 return (gfn_t)(gpa >> PAGE_SHIFT); 1734 } 1735 1736 static inline hpa_t pfn_to_hpa(kvm_pfn_t pfn) 1737 { 1738 return (hpa_t)pfn << PAGE_SHIFT; 1739 } 1740 1741 static inline bool kvm_is_error_gpa(struct kvm *kvm, gpa_t gpa) 1742 { 1743 unsigned long hva = gfn_to_hva(kvm, gpa_to_gfn(gpa)); 1744 1745 return kvm_is_error_hva(hva); 1746 } 1747 1748 enum kvm_stat_kind { 1749 KVM_STAT_VM, 1750 KVM_STAT_VCPU, 1751 }; 1752 1753 struct kvm_stat_data { 1754 struct kvm *kvm; 1755 const struct _kvm_stats_desc *desc; 1756 enum kvm_stat_kind kind; 1757 }; 1758 1759 struct _kvm_stats_desc { 1760 struct kvm_stats_desc desc; 1761 char name[KVM_STATS_NAME_SIZE]; 1762 }; 1763 1764 #define STATS_DESC_COMMON(type, unit, base, exp, sz, bsz) \ 1765 .flags = type | unit | base | \ 1766 BUILD_BUG_ON_ZERO(type & ~KVM_STATS_TYPE_MASK) | \ 1767 BUILD_BUG_ON_ZERO(unit & ~KVM_STATS_UNIT_MASK) | \ 1768 BUILD_BUG_ON_ZERO(base & ~KVM_STATS_BASE_MASK), \ 1769 .exponent = exp, \ 1770 .size = sz, \ 1771 .bucket_size = bsz 1772 1773 #define VM_GENERIC_STATS_DESC(stat, type, unit, base, exp, sz, bsz) \ 1774 { \ 1775 { \ 1776 STATS_DESC_COMMON(type, unit, base, exp, sz, bsz), \ 1777 .offset = offsetof(struct kvm_vm_stat, generic.stat) \ 1778 }, \ 1779 .name = #stat, \ 1780 } 1781 #define VCPU_GENERIC_STATS_DESC(stat, type, unit, base, exp, sz, bsz) \ 1782 { \ 1783 { \ 1784 STATS_DESC_COMMON(type, unit, base, exp, sz, bsz), \ 1785 .offset = offsetof(struct kvm_vcpu_stat, generic.stat) \ 1786 }, \ 1787 .name = #stat, \ 1788 } 1789 #define VM_STATS_DESC(stat, type, unit, base, exp, sz, bsz) \ 1790 { \ 1791 { \ 1792 STATS_DESC_COMMON(type, unit, base, exp, sz, bsz), \ 1793 .offset = offsetof(struct kvm_vm_stat, stat) \ 1794 }, \ 1795 .name = #stat, \ 1796 } 1797 #define VCPU_STATS_DESC(stat, type, unit, base, exp, sz, bsz) \ 1798 { \ 1799 { \ 1800 STATS_DESC_COMMON(type, unit, base, exp, sz, bsz), \ 1801 .offset = offsetof(struct kvm_vcpu_stat, stat) \ 1802 }, \ 1803 .name = #stat, \ 1804 } 1805 /* SCOPE: VM, VM_GENERIC, VCPU, VCPU_GENERIC */ 1806 #define STATS_DESC(SCOPE, stat, type, unit, base, exp, sz, bsz) \ 1807 SCOPE##_STATS_DESC(stat, type, unit, base, exp, sz, bsz) 1808 1809 #define STATS_DESC_CUMULATIVE(SCOPE, name, unit, base, exponent) \ 1810 STATS_DESC(SCOPE, name, KVM_STATS_TYPE_CUMULATIVE, \ 1811 unit, base, exponent, 1, 0) 1812 #define STATS_DESC_INSTANT(SCOPE, name, unit, base, exponent) \ 1813 STATS_DESC(SCOPE, name, KVM_STATS_TYPE_INSTANT, \ 1814 unit, base, exponent, 1, 0) 1815 #define STATS_DESC_PEAK(SCOPE, name, unit, base, exponent) \ 1816 STATS_DESC(SCOPE, name, KVM_STATS_TYPE_PEAK, \ 1817 unit, base, exponent, 1, 0) 1818 #define STATS_DESC_LINEAR_HIST(SCOPE, name, unit, base, exponent, sz, bsz) \ 1819 STATS_DESC(SCOPE, name, KVM_STATS_TYPE_LINEAR_HIST, \ 1820 unit, base, exponent, sz, bsz) 1821 #define STATS_DESC_LOG_HIST(SCOPE, name, unit, base, exponent, sz) \ 1822 STATS_DESC(SCOPE, name, KVM_STATS_TYPE_LOG_HIST, \ 1823 unit, base, exponent, sz, 0) 1824 1825 /* Cumulative counter, read/write */ 1826 #define STATS_DESC_COUNTER(SCOPE, name) \ 1827 STATS_DESC_CUMULATIVE(SCOPE, name, KVM_STATS_UNIT_NONE, \ 1828 KVM_STATS_BASE_POW10, 0) 1829 /* Instantaneous counter, read only */ 1830 #define STATS_DESC_ICOUNTER(SCOPE, name) \ 1831 STATS_DESC_INSTANT(SCOPE, name, KVM_STATS_UNIT_NONE, \ 1832 KVM_STATS_BASE_POW10, 0) 1833 /* Peak counter, read/write */ 1834 #define STATS_DESC_PCOUNTER(SCOPE, name) \ 1835 STATS_DESC_PEAK(SCOPE, name, KVM_STATS_UNIT_NONE, \ 1836 KVM_STATS_BASE_POW10, 0) 1837 1838 /* Instantaneous boolean value, read only */ 1839 #define STATS_DESC_IBOOLEAN(SCOPE, name) \ 1840 STATS_DESC_INSTANT(SCOPE, name, KVM_STATS_UNIT_BOOLEAN, \ 1841 KVM_STATS_BASE_POW10, 0) 1842 /* Peak (sticky) boolean value, read/write */ 1843 #define STATS_DESC_PBOOLEAN(SCOPE, name) \ 1844 STATS_DESC_PEAK(SCOPE, name, KVM_STATS_UNIT_BOOLEAN, \ 1845 KVM_STATS_BASE_POW10, 0) 1846 1847 /* Cumulative time in nanosecond */ 1848 #define STATS_DESC_TIME_NSEC(SCOPE, name) \ 1849 STATS_DESC_CUMULATIVE(SCOPE, name, KVM_STATS_UNIT_SECONDS, \ 1850 KVM_STATS_BASE_POW10, -9) 1851 /* Linear histogram for time in nanosecond */ 1852 #define STATS_DESC_LINHIST_TIME_NSEC(SCOPE, name, sz, bsz) \ 1853 STATS_DESC_LINEAR_HIST(SCOPE, name, KVM_STATS_UNIT_SECONDS, \ 1854 KVM_STATS_BASE_POW10, -9, sz, bsz) 1855 /* Logarithmic histogram for time in nanosecond */ 1856 #define STATS_DESC_LOGHIST_TIME_NSEC(SCOPE, name, sz) \ 1857 STATS_DESC_LOG_HIST(SCOPE, name, KVM_STATS_UNIT_SECONDS, \ 1858 KVM_STATS_BASE_POW10, -9, sz) 1859 1860 #define KVM_GENERIC_VM_STATS() \ 1861 STATS_DESC_COUNTER(VM_GENERIC, remote_tlb_flush), \ 1862 STATS_DESC_COUNTER(VM_GENERIC, remote_tlb_flush_requests) 1863 1864 #define KVM_GENERIC_VCPU_STATS() \ 1865 STATS_DESC_COUNTER(VCPU_GENERIC, halt_successful_poll), \ 1866 STATS_DESC_COUNTER(VCPU_GENERIC, halt_attempted_poll), \ 1867 STATS_DESC_COUNTER(VCPU_GENERIC, halt_poll_invalid), \ 1868 STATS_DESC_COUNTER(VCPU_GENERIC, halt_wakeup), \ 1869 STATS_DESC_TIME_NSEC(VCPU_GENERIC, halt_poll_success_ns), \ 1870 STATS_DESC_TIME_NSEC(VCPU_GENERIC, halt_poll_fail_ns), \ 1871 STATS_DESC_TIME_NSEC(VCPU_GENERIC, halt_wait_ns), \ 1872 STATS_DESC_LOGHIST_TIME_NSEC(VCPU_GENERIC, halt_poll_success_hist, \ 1873 HALT_POLL_HIST_COUNT), \ 1874 STATS_DESC_LOGHIST_TIME_NSEC(VCPU_GENERIC, halt_poll_fail_hist, \ 1875 HALT_POLL_HIST_COUNT), \ 1876 STATS_DESC_LOGHIST_TIME_NSEC(VCPU_GENERIC, halt_wait_hist, \ 1877 HALT_POLL_HIST_COUNT), \ 1878 STATS_DESC_IBOOLEAN(VCPU_GENERIC, blocking) 1879 1880 extern struct dentry *kvm_debugfs_dir; 1881 1882 ssize_t kvm_stats_read(char *id, const struct kvm_stats_header *header, 1883 const struct _kvm_stats_desc *desc, 1884 void *stats, size_t size_stats, 1885 char __user *user_buffer, size_t size, loff_t *offset); 1886 1887 /** 1888 * kvm_stats_linear_hist_update() - Update bucket value for linear histogram 1889 * statistics data. 1890 * 1891 * @data: start address of the stats data 1892 * @size: the number of bucket of the stats data 1893 * @value: the new value used to update the linear histogram's bucket 1894 * @bucket_size: the size (width) of a bucket 1895 */ 1896 static inline void kvm_stats_linear_hist_update(u64 *data, size_t size, 1897 u64 value, size_t bucket_size) 1898 { 1899 size_t index = div64_u64(value, bucket_size); 1900 1901 index = min(index, size - 1); 1902 ++data[index]; 1903 } 1904 1905 /** 1906 * kvm_stats_log_hist_update() - Update bucket value for logarithmic histogram 1907 * statistics data. 1908 * 1909 * @data: start address of the stats data 1910 * @size: the number of bucket of the stats data 1911 * @value: the new value used to update the logarithmic histogram's bucket 1912 */ 1913 static inline void kvm_stats_log_hist_update(u64 *data, size_t size, u64 value) 1914 { 1915 size_t index = fls64(value); 1916 1917 index = min(index, size - 1); 1918 ++data[index]; 1919 } 1920 1921 #define KVM_STATS_LINEAR_HIST_UPDATE(array, value, bsize) \ 1922 kvm_stats_linear_hist_update(array, ARRAY_SIZE(array), value, bsize) 1923 #define KVM_STATS_LOG_HIST_UPDATE(array, value) \ 1924 kvm_stats_log_hist_update(array, ARRAY_SIZE(array), value) 1925 1926 1927 extern const struct kvm_stats_header kvm_vm_stats_header; 1928 extern const struct _kvm_stats_desc kvm_vm_stats_desc[]; 1929 extern const struct kvm_stats_header kvm_vcpu_stats_header; 1930 extern const struct _kvm_stats_desc kvm_vcpu_stats_desc[]; 1931 1932 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) 1933 static inline int mmu_invalidate_retry(struct kvm *kvm, unsigned long mmu_seq) 1934 { 1935 if (unlikely(kvm->mmu_invalidate_in_progress)) 1936 return 1; 1937 /* 1938 * Ensure the read of mmu_invalidate_in_progress happens before 1939 * the read of mmu_invalidate_seq. This interacts with the 1940 * smp_wmb() in mmu_notifier_invalidate_range_end to make sure 1941 * that the caller either sees the old (non-zero) value of 1942 * mmu_invalidate_in_progress or the new (incremented) value of 1943 * mmu_invalidate_seq. 1944 * 1945 * PowerPC Book3s HV KVM calls this under a per-page lock rather 1946 * than under kvm->mmu_lock, for scalability, so can't rely on 1947 * kvm->mmu_lock to keep things ordered. 1948 */ 1949 smp_rmb(); 1950 if (kvm->mmu_invalidate_seq != mmu_seq) 1951 return 1; 1952 return 0; 1953 } 1954 1955 static inline int mmu_invalidate_retry_hva(struct kvm *kvm, 1956 unsigned long mmu_seq, 1957 unsigned long hva) 1958 { 1959 lockdep_assert_held(&kvm->mmu_lock); 1960 /* 1961 * If mmu_invalidate_in_progress is non-zero, then the range maintained 1962 * by kvm_mmu_notifier_invalidate_range_start contains all addresses 1963 * that might be being invalidated. Note that it may include some false 1964 * positives, due to shortcuts when handing concurrent invalidations. 1965 */ 1966 if (unlikely(kvm->mmu_invalidate_in_progress) && 1967 hva >= kvm->mmu_invalidate_range_start && 1968 hva < kvm->mmu_invalidate_range_end) 1969 return 1; 1970 if (kvm->mmu_invalidate_seq != mmu_seq) 1971 return 1; 1972 return 0; 1973 } 1974 #endif 1975 1976 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING 1977 1978 #define KVM_MAX_IRQ_ROUTES 4096 /* might need extension/rework in the future */ 1979 1980 bool kvm_arch_can_set_irq_routing(struct kvm *kvm); 1981 int kvm_set_irq_routing(struct kvm *kvm, 1982 const struct kvm_irq_routing_entry *entries, 1983 unsigned nr, 1984 unsigned flags); 1985 int kvm_set_routing_entry(struct kvm *kvm, 1986 struct kvm_kernel_irq_routing_entry *e, 1987 const struct kvm_irq_routing_entry *ue); 1988 void kvm_free_irq_routing(struct kvm *kvm); 1989 1990 #else 1991 1992 static inline void kvm_free_irq_routing(struct kvm *kvm) {} 1993 1994 #endif 1995 1996 int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi); 1997 1998 #ifdef CONFIG_HAVE_KVM_EVENTFD 1999 2000 void kvm_eventfd_init(struct kvm *kvm); 2001 int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args); 2002 2003 #ifdef CONFIG_HAVE_KVM_IRQFD 2004 int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args); 2005 void kvm_irqfd_release(struct kvm *kvm); 2006 void kvm_irq_routing_update(struct kvm *); 2007 #else 2008 static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args) 2009 { 2010 return -EINVAL; 2011 } 2012 2013 static inline void kvm_irqfd_release(struct kvm *kvm) {} 2014 #endif 2015 2016 #else 2017 2018 static inline void kvm_eventfd_init(struct kvm *kvm) {} 2019 2020 static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args) 2021 { 2022 return -EINVAL; 2023 } 2024 2025 static inline void kvm_irqfd_release(struct kvm *kvm) {} 2026 2027 #ifdef CONFIG_HAVE_KVM_IRQCHIP 2028 static inline void kvm_irq_routing_update(struct kvm *kvm) 2029 { 2030 } 2031 #endif 2032 2033 static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) 2034 { 2035 return -ENOSYS; 2036 } 2037 2038 #endif /* CONFIG_HAVE_KVM_EVENTFD */ 2039 2040 void kvm_arch_irq_routing_update(struct kvm *kvm); 2041 2042 static inline void __kvm_make_request(int req, struct kvm_vcpu *vcpu) 2043 { 2044 /* 2045 * Ensure the rest of the request is published to kvm_check_request's 2046 * caller. Paired with the smp_mb__after_atomic in kvm_check_request. 2047 */ 2048 smp_wmb(); 2049 set_bit(req & KVM_REQUEST_MASK, (void *)&vcpu->requests); 2050 } 2051 2052 static __always_inline void kvm_make_request(int req, struct kvm_vcpu *vcpu) 2053 { 2054 /* 2055 * Request that don't require vCPU action should never be logged in 2056 * vcpu->requests. The vCPU won't clear the request, so it will stay 2057 * logged indefinitely and prevent the vCPU from entering the guest. 2058 */ 2059 BUILD_BUG_ON(!__builtin_constant_p(req) || 2060 (req & KVM_REQUEST_NO_ACTION)); 2061 2062 __kvm_make_request(req, vcpu); 2063 } 2064 2065 static inline bool kvm_request_pending(struct kvm_vcpu *vcpu) 2066 { 2067 return READ_ONCE(vcpu->requests); 2068 } 2069 2070 static inline bool kvm_test_request(int req, struct kvm_vcpu *vcpu) 2071 { 2072 return test_bit(req & KVM_REQUEST_MASK, (void *)&vcpu->requests); 2073 } 2074 2075 static inline void kvm_clear_request(int req, struct kvm_vcpu *vcpu) 2076 { 2077 clear_bit(req & KVM_REQUEST_MASK, (void *)&vcpu->requests); 2078 } 2079 2080 static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu) 2081 { 2082 if (kvm_test_request(req, vcpu)) { 2083 kvm_clear_request(req, vcpu); 2084 2085 /* 2086 * Ensure the rest of the request is visible to kvm_check_request's 2087 * caller. Paired with the smp_wmb in kvm_make_request. 2088 */ 2089 smp_mb__after_atomic(); 2090 return true; 2091 } else { 2092 return false; 2093 } 2094 } 2095 2096 extern bool kvm_rebooting; 2097 2098 extern unsigned int halt_poll_ns; 2099 extern unsigned int halt_poll_ns_grow; 2100 extern unsigned int halt_poll_ns_grow_start; 2101 extern unsigned int halt_poll_ns_shrink; 2102 2103 struct kvm_device { 2104 const struct kvm_device_ops *ops; 2105 struct kvm *kvm; 2106 void *private; 2107 struct list_head vm_node; 2108 }; 2109 2110 /* create, destroy, and name are mandatory */ 2111 struct kvm_device_ops { 2112 const char *name; 2113 2114 /* 2115 * create is called holding kvm->lock and any operations not suitable 2116 * to do while holding the lock should be deferred to init (see 2117 * below). 2118 */ 2119 int (*create)(struct kvm_device *dev, u32 type); 2120 2121 /* 2122 * init is called after create if create is successful and is called 2123 * outside of holding kvm->lock. 2124 */ 2125 void (*init)(struct kvm_device *dev); 2126 2127 /* 2128 * Destroy is responsible for freeing dev. 2129 * 2130 * Destroy may be called before or after destructors are called 2131 * on emulated I/O regions, depending on whether a reference is 2132 * held by a vcpu or other kvm component that gets destroyed 2133 * after the emulated I/O. 2134 */ 2135 void (*destroy)(struct kvm_device *dev); 2136 2137 /* 2138 * Release is an alternative method to free the device. It is 2139 * called when the device file descriptor is closed. Once 2140 * release is called, the destroy method will not be called 2141 * anymore as the device is removed from the device list of 2142 * the VM. kvm->lock is held. 2143 */ 2144 void (*release)(struct kvm_device *dev); 2145 2146 int (*set_attr)(struct kvm_device *dev, struct kvm_device_attr *attr); 2147 int (*get_attr)(struct kvm_device *dev, struct kvm_device_attr *attr); 2148 int (*has_attr)(struct kvm_device *dev, struct kvm_device_attr *attr); 2149 long (*ioctl)(struct kvm_device *dev, unsigned int ioctl, 2150 unsigned long arg); 2151 int (*mmap)(struct kvm_device *dev, struct vm_area_struct *vma); 2152 }; 2153 2154 void kvm_device_get(struct kvm_device *dev); 2155 void kvm_device_put(struct kvm_device *dev); 2156 struct kvm_device *kvm_device_from_filp(struct file *filp); 2157 int kvm_register_device_ops(const struct kvm_device_ops *ops, u32 type); 2158 void kvm_unregister_device_ops(u32 type); 2159 2160 extern struct kvm_device_ops kvm_mpic_ops; 2161 extern struct kvm_device_ops kvm_arm_vgic_v2_ops; 2162 extern struct kvm_device_ops kvm_arm_vgic_v3_ops; 2163 2164 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT 2165 2166 static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val) 2167 { 2168 vcpu->spin_loop.in_spin_loop = val; 2169 } 2170 static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val) 2171 { 2172 vcpu->spin_loop.dy_eligible = val; 2173 } 2174 2175 #else /* !CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */ 2176 2177 static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val) 2178 { 2179 } 2180 2181 static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val) 2182 { 2183 } 2184 #endif /* CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */ 2185 2186 static inline bool kvm_is_visible_memslot(struct kvm_memory_slot *memslot) 2187 { 2188 return (memslot && memslot->id < KVM_USER_MEM_SLOTS && 2189 !(memslot->flags & KVM_MEMSLOT_INVALID)); 2190 } 2191 2192 struct kvm_vcpu *kvm_get_running_vcpu(void); 2193 struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void); 2194 2195 #ifdef CONFIG_HAVE_KVM_IRQ_BYPASS 2196 bool kvm_arch_has_irq_bypass(void); 2197 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *, 2198 struct irq_bypass_producer *); 2199 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *, 2200 struct irq_bypass_producer *); 2201 void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *); 2202 void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *); 2203 int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq, 2204 uint32_t guest_irq, bool set); 2205 bool kvm_arch_irqfd_route_changed(struct kvm_kernel_irq_routing_entry *, 2206 struct kvm_kernel_irq_routing_entry *); 2207 #endif /* CONFIG_HAVE_KVM_IRQ_BYPASS */ 2208 2209 #ifdef CONFIG_HAVE_KVM_INVALID_WAKEUPS 2210 /* If we wakeup during the poll time, was it a sucessful poll? */ 2211 static inline bool vcpu_valid_wakeup(struct kvm_vcpu *vcpu) 2212 { 2213 return vcpu->valid_wakeup; 2214 } 2215 2216 #else 2217 static inline bool vcpu_valid_wakeup(struct kvm_vcpu *vcpu) 2218 { 2219 return true; 2220 } 2221 #endif /* CONFIG_HAVE_KVM_INVALID_WAKEUPS */ 2222 2223 #ifdef CONFIG_HAVE_KVM_NO_POLL 2224 /* Callback that tells if we must not poll */ 2225 bool kvm_arch_no_poll(struct kvm_vcpu *vcpu); 2226 #else 2227 static inline bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) 2228 { 2229 return false; 2230 } 2231 #endif /* CONFIG_HAVE_KVM_NO_POLL */ 2232 2233 #ifdef CONFIG_HAVE_KVM_VCPU_ASYNC_IOCTL 2234 long kvm_arch_vcpu_async_ioctl(struct file *filp, 2235 unsigned int ioctl, unsigned long arg); 2236 #else 2237 static inline long kvm_arch_vcpu_async_ioctl(struct file *filp, 2238 unsigned int ioctl, 2239 unsigned long arg) 2240 { 2241 return -ENOIOCTLCMD; 2242 } 2243 #endif /* CONFIG_HAVE_KVM_VCPU_ASYNC_IOCTL */ 2244 2245 void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, 2246 unsigned long start, unsigned long end); 2247 2248 void kvm_arch_guest_memory_reclaimed(struct kvm *kvm); 2249 2250 #ifdef CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE 2251 int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu); 2252 #else 2253 static inline int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) 2254 { 2255 return 0; 2256 } 2257 #endif /* CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE */ 2258 2259 typedef int (*kvm_vm_thread_fn_t)(struct kvm *kvm, uintptr_t data); 2260 2261 int kvm_vm_create_worker_thread(struct kvm *kvm, kvm_vm_thread_fn_t thread_fn, 2262 uintptr_t data, const char *name, 2263 struct task_struct **thread_ptr); 2264 2265 #ifdef CONFIG_KVM_XFER_TO_GUEST_WORK 2266 static inline void kvm_handle_signal_exit(struct kvm_vcpu *vcpu) 2267 { 2268 vcpu->run->exit_reason = KVM_EXIT_INTR; 2269 vcpu->stat.signal_exits++; 2270 } 2271 #endif /* CONFIG_KVM_XFER_TO_GUEST_WORK */ 2272 2273 /* 2274 * If more than one page is being (un)accounted, @virt must be the address of 2275 * the first page of a block of pages what were allocated together (i.e 2276 * accounted together). 2277 * 2278 * kvm_account_pgtable_pages() is thread-safe because mod_lruvec_page_state() 2279 * is thread-safe. 2280 */ 2281 static inline void kvm_account_pgtable_pages(void *virt, int nr) 2282 { 2283 mod_lruvec_page_state(virt_to_page(virt), NR_SECONDARY_PAGETABLE, nr); 2284 } 2285 2286 /* 2287 * This defines how many reserved entries we want to keep before we 2288 * kick the vcpu to the userspace to avoid dirty ring full. This 2289 * value can be tuned to higher if e.g. PML is enabled on the host. 2290 */ 2291 #define KVM_DIRTY_RING_RSVD_ENTRIES 64 2292 2293 /* Max number of entries allowed for each kvm dirty ring */ 2294 #define KVM_DIRTY_RING_MAX_ENTRIES 65536 2295 2296 #endif 2297