1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * KVM paravirt_ops implementation
5 * Copyright (C) 2007, Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
6 * Copyright IBM Corporation, 2007
7 * Authors: Anthony Liguori <aliguori@us.ibm.com>
10 #include <linux/context_tracking.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/kvm_para.h>
14 #include <linux/cpu.h>
16 #include <linux/highmem.h>
17 #include <linux/hardirq.h>
18 #include <linux/notifier.h>
19 #include <linux/reboot.h>
20 #include <linux/hash.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/kprobes.h>
24 #include <linux/debugfs.h>
25 #include <linux/nmi.h>
26 #include <linux/swait.h>
27 #include <linux/syscore_ops.h>
28 #include <asm/timer.h>
30 #include <asm/traps.h>
32 #include <asm/tlbflush.h>
34 #include <asm/apicdef.h>
35 #include <asm/hypervisor.h>
37 #include <asm/reboot.h>
39 static int kvmapf = 1;
41 static int __init parse_no_kvmapf(char *arg)
47 early_param("no-kvmapf", parse_no_kvmapf);
49 static int steal_acc = 1;
50 static int __init parse_no_stealacc(char *arg)
56 early_param("no-steal-acc", parse_no_stealacc);
58 static DEFINE_PER_CPU_DECRYPTED(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
59 DEFINE_PER_CPU_DECRYPTED(struct kvm_steal_time, steal_time) __aligned(64) __visible;
60 static int has_steal_clock = 0;
62 static int has_guest_poll = 0;
64 * No need for any "IO delay" on KVM
66 static void kvm_io_delay(void)
70 #define KVM_TASK_SLEEP_HASHBITS 8
71 #define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
73 struct kvm_task_sleep_node {
74 struct hlist_node link;
75 struct swait_queue_head wq;
81 static struct kvm_task_sleep_head {
83 struct hlist_head list;
84 } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
86 static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b,
91 hlist_for_each(p, &b->list) {
92 struct kvm_task_sleep_node *n =
93 hlist_entry(p, typeof(*n), link);
94 if (n->token == token)
102 * @interrupt_kernel: Is this called from a routine which interrupts the kernel
103 * (other than user space)?
105 void kvm_async_pf_task_wait(u32 token, int interrupt_kernel)
107 u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
108 struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
109 struct kvm_task_sleep_node n, *e;
110 DECLARE_SWAITQUEUE(wait);
114 raw_spin_lock(&b->lock);
115 e = _find_apf_task(b, token);
117 /* dummy entry exist -> wake up was delivered ahead of PF */
120 raw_spin_unlock(&b->lock);
127 n.cpu = smp_processor_id();
128 n.halted = is_idle_task(current) ||
129 (IS_ENABLED(CONFIG_PREEMPT_COUNT)
130 ? preempt_count() > 1 || rcu_preempt_depth()
132 init_swait_queue_head(&n.wq);
133 hlist_add_head(&n.link, &b->list);
134 raw_spin_unlock(&b->lock);
138 prepare_to_swait_exclusive(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
139 if (hlist_unhashed(&n.link))
150 * We cannot reschedule. So halt.
159 finish_swait(&n.wq, &wait);
164 EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait);
166 static void apf_task_wake_one(struct kvm_task_sleep_node *n)
168 hlist_del_init(&n->link);
170 smp_send_reschedule(n->cpu);
171 else if (swq_has_sleeper(&n->wq))
172 swake_up_one(&n->wq);
175 static void apf_task_wake_all(void)
179 for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
180 struct hlist_node *p, *next;
181 struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
182 raw_spin_lock(&b->lock);
183 hlist_for_each_safe(p, next, &b->list) {
184 struct kvm_task_sleep_node *n =
185 hlist_entry(p, typeof(*n), link);
186 if (n->cpu == smp_processor_id())
187 apf_task_wake_one(n);
189 raw_spin_unlock(&b->lock);
193 void kvm_async_pf_task_wake(u32 token)
195 u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
196 struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
197 struct kvm_task_sleep_node *n;
205 raw_spin_lock(&b->lock);
206 n = _find_apf_task(b, token);
209 * async PF was not yet handled.
210 * Add dummy entry for the token.
212 n = kzalloc(sizeof(*n), GFP_ATOMIC);
215 * Allocation failed! Busy wait while other cpu
218 raw_spin_unlock(&b->lock);
223 n->cpu = smp_processor_id();
224 init_swait_queue_head(&n->wq);
225 hlist_add_head(&n->link, &b->list);
227 apf_task_wake_one(n);
228 raw_spin_unlock(&b->lock);
231 EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
233 u32 kvm_read_and_reset_pf_reason(void)
237 if (__this_cpu_read(apf_reason.enabled)) {
238 reason = __this_cpu_read(apf_reason.reason);
239 __this_cpu_write(apf_reason.reason, 0);
244 EXPORT_SYMBOL_GPL(kvm_read_and_reset_pf_reason);
245 NOKPROBE_SYMBOL(kvm_read_and_reset_pf_reason);
248 do_async_page_fault(struct pt_regs *regs, unsigned long error_code, unsigned long address)
250 enum ctx_state prev_state;
252 switch (kvm_read_and_reset_pf_reason()) {
254 do_page_fault(regs, error_code, address);
256 case KVM_PV_REASON_PAGE_NOT_PRESENT:
257 /* page is swapped out by the host. */
258 prev_state = exception_enter();
259 kvm_async_pf_task_wait((u32)address, !user_mode(regs));
260 exception_exit(prev_state);
262 case KVM_PV_REASON_PAGE_READY:
264 kvm_async_pf_task_wake((u32)address);
269 NOKPROBE_SYMBOL(do_async_page_fault);
271 static void __init paravirt_ops_setup(void)
273 pv_info.name = "KVM";
275 if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
276 pv_ops.cpu.io_delay = kvm_io_delay;
278 #ifdef CONFIG_X86_IO_APIC
283 static void kvm_register_steal_time(void)
285 int cpu = smp_processor_id();
286 struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
288 if (!has_steal_clock)
291 wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED));
292 pr_info("kvm-stealtime: cpu %d, msr %llx\n",
293 cpu, (unsigned long long) slow_virt_to_phys(st));
296 static DEFINE_PER_CPU_DECRYPTED(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED;
298 static notrace void kvm_guest_apic_eoi_write(u32 reg, u32 val)
301 * This relies on __test_and_clear_bit to modify the memory
302 * in a way that is atomic with respect to the local CPU.
303 * The hypervisor only accesses this memory from the local CPU so
304 * there's no need for lock or memory barriers.
305 * An optimization barrier is implied in apic write.
307 if (__test_and_clear_bit(KVM_PV_EOI_BIT, this_cpu_ptr(&kvm_apic_eoi)))
309 apic->native_eoi_write(APIC_EOI, APIC_EOI_ACK);
312 static void kvm_guest_cpu_init(void)
314 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) {
315 u64 pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason));
317 #ifdef CONFIG_PREEMPTION
318 pa |= KVM_ASYNC_PF_SEND_ALWAYS;
320 pa |= KVM_ASYNC_PF_ENABLED;
322 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_VMEXIT))
323 pa |= KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
325 wrmsrl(MSR_KVM_ASYNC_PF_EN, pa);
326 __this_cpu_write(apf_reason.enabled, 1);
327 printk(KERN_INFO"KVM setup async PF for cpu %d\n",
331 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
333 /* Size alignment is implied but just to make it explicit. */
334 BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4);
335 __this_cpu_write(kvm_apic_eoi, 0);
336 pa = slow_virt_to_phys(this_cpu_ptr(&kvm_apic_eoi))
338 wrmsrl(MSR_KVM_PV_EOI_EN, pa);
342 kvm_register_steal_time();
345 static void kvm_pv_disable_apf(void)
347 if (!__this_cpu_read(apf_reason.enabled))
350 wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
351 __this_cpu_write(apf_reason.enabled, 0);
353 printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
357 static void kvm_disable_steal_time(void)
359 if (!has_steal_clock)
362 wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
365 static void kvm_pv_guest_cpu_reboot(void *unused)
368 * We disable PV EOI before we load a new kernel by kexec,
369 * since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory.
370 * New kernel can re-enable when it boots.
372 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
373 wrmsrl(MSR_KVM_PV_EOI_EN, 0);
374 kvm_pv_disable_apf();
375 kvm_disable_steal_time();
378 static int kvm_pv_reboot_notify(struct notifier_block *nb,
379 unsigned long code, void *unused)
381 if (code == SYS_RESTART)
382 on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
386 static struct notifier_block kvm_pv_reboot_nb = {
387 .notifier_call = kvm_pv_reboot_notify,
390 static u64 kvm_steal_clock(int cpu)
393 struct kvm_steal_time *src;
396 src = &per_cpu(steal_time, cpu);
398 version = src->version;
402 } while ((version & 1) || (version != src->version));
407 static inline void __set_percpu_decrypted(void *ptr, unsigned long size)
409 early_set_memory_decrypted((unsigned long) ptr, size);
413 * Iterate through all possible CPUs and map the memory region pointed
414 * by apf_reason, steal_time and kvm_apic_eoi as decrypted at once.
416 * Note: we iterate through all possible CPUs to ensure that CPUs
417 * hotplugged will have their per-cpu variable already mapped as
420 static void __init sev_map_percpu_data(void)
427 for_each_possible_cpu(cpu) {
428 __set_percpu_decrypted(&per_cpu(apf_reason, cpu), sizeof(apf_reason));
429 __set_percpu_decrypted(&per_cpu(steal_time, cpu), sizeof(steal_time));
430 __set_percpu_decrypted(&per_cpu(kvm_apic_eoi, cpu), sizeof(kvm_apic_eoi));
434 static void kvm_guest_cpu_offline(bool shutdown)
436 kvm_disable_steal_time();
437 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
438 wrmsrl(MSR_KVM_PV_EOI_EN, 0);
439 kvm_pv_disable_apf();
445 static int kvm_cpu_online(unsigned int cpu)
449 local_irq_save(flags);
450 kvm_guest_cpu_init();
451 local_irq_restore(flags);
456 #define KVM_IPI_CLUSTER_SIZE (2 * BITS_PER_LONG)
458 static void __send_ipi_mask(const struct cpumask *mask, int vector)
461 int cpu, apic_id, icr;
462 int min = 0, max = 0;
464 __uint128_t ipi_bitmap = 0;
470 if (cpumask_empty(mask))
473 local_irq_save(flags);
477 icr = APIC_DM_FIXED | vector;
484 for_each_cpu(cpu, mask) {
485 apic_id = per_cpu(x86_cpu_to_apicid, cpu);
488 } else if (apic_id < min && max - apic_id < KVM_IPI_CLUSTER_SIZE) {
489 ipi_bitmap <<= min - apic_id;
491 } else if (apic_id > min && apic_id < min + KVM_IPI_CLUSTER_SIZE) {
492 max = apic_id < max ? max : apic_id;
494 ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap,
495 (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr);
496 WARN_ONCE(ret < 0, "KVM: failed to send PV IPI: %ld", ret);
500 __set_bit(apic_id - min, (unsigned long *)&ipi_bitmap);
504 ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap,
505 (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr);
506 WARN_ONCE(ret < 0, "KVM: failed to send PV IPI: %ld", ret);
509 local_irq_restore(flags);
512 static void kvm_send_ipi_mask(const struct cpumask *mask, int vector)
514 __send_ipi_mask(mask, vector);
517 static void kvm_send_ipi_mask_allbutself(const struct cpumask *mask, int vector)
519 unsigned int this_cpu = smp_processor_id();
520 struct cpumask new_mask;
521 const struct cpumask *local_mask;
523 cpumask_copy(&new_mask, mask);
524 cpumask_clear_cpu(this_cpu, &new_mask);
525 local_mask = &new_mask;
526 __send_ipi_mask(local_mask, vector);
530 * Set the IPI entry points
532 static void kvm_setup_pv_ipi(void)
534 apic->send_IPI_mask = kvm_send_ipi_mask;
535 apic->send_IPI_mask_allbutself = kvm_send_ipi_mask_allbutself;
536 pr_info("KVM setup pv IPIs\n");
539 static void kvm_smp_send_call_func_ipi(const struct cpumask *mask)
543 native_send_call_func_ipi(mask);
545 /* Make sure other vCPUs get a chance to run if they need to. */
546 for_each_cpu(cpu, mask) {
547 if (vcpu_is_preempted(cpu)) {
548 kvm_hypercall1(KVM_HC_SCHED_YIELD, per_cpu(x86_cpu_to_apicid, cpu));
554 static void __init kvm_smp_prepare_cpus(unsigned int max_cpus)
556 native_smp_prepare_cpus(max_cpus);
557 if (kvm_para_has_hint(KVM_HINTS_REALTIME))
558 static_branch_disable(&virt_spin_lock_key);
561 static void __init kvm_smp_prepare_boot_cpu(void)
564 * Map the per-cpu variables as decrypted before kvm_guest_cpu_init()
565 * shares the guest physical address with the hypervisor.
567 sev_map_percpu_data();
569 kvm_guest_cpu_init();
570 native_smp_prepare_boot_cpu();
574 static int kvm_cpu_down_prepare(unsigned int cpu)
578 local_irq_save(flags);
579 kvm_guest_cpu_offline(false);
580 local_irq_restore(flags);
586 static int kvm_suspend(void)
590 kvm_guest_cpu_offline(false);
592 #ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL
593 if (kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL))
594 rdmsrl(MSR_KVM_POLL_CONTROL, val);
595 has_guest_poll = !(val & 1);
600 static void kvm_resume(void)
602 kvm_cpu_online(raw_smp_processor_id());
604 #ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL
605 if (kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL) && has_guest_poll)
606 wrmsrl(MSR_KVM_POLL_CONTROL, 0);
610 static struct syscore_ops kvm_syscore_ops = {
611 .suspend = kvm_suspend,
612 .resume = kvm_resume,
616 * After a PV feature is registered, the host will keep writing to the
617 * registered memory location. If the guest happens to shutdown, this memory
618 * won't be valid. In cases like kexec, in which you install a new kernel, this
619 * means a random memory location will be kept being written.
621 #ifdef CONFIG_KEXEC_CORE
622 static void kvm_crash_shutdown(struct pt_regs *regs)
624 kvm_guest_cpu_offline(true);
625 native_machine_crash_shutdown(regs);
629 static void __init kvm_apf_trap_init(void)
631 update_intr_gate(X86_TRAP_PF, async_page_fault);
634 static DEFINE_PER_CPU(cpumask_var_t, __pv_tlb_mask);
636 static void kvm_flush_tlb_others(const struct cpumask *cpumask,
637 const struct flush_tlb_info *info)
641 struct kvm_steal_time *src;
642 struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_tlb_mask);
644 cpumask_copy(flushmask, cpumask);
646 * We have to call flush only on online vCPUs. And
647 * queue flush_on_enter for pre-empted vCPUs
649 for_each_cpu(cpu, flushmask) {
650 src = &per_cpu(steal_time, cpu);
651 state = READ_ONCE(src->preempted);
652 if ((state & KVM_VCPU_PREEMPTED)) {
653 if (try_cmpxchg(&src->preempted, &state,
654 state | KVM_VCPU_FLUSH_TLB))
655 __cpumask_clear_cpu(cpu, flushmask);
659 native_flush_tlb_others(flushmask, info);
662 static void __init kvm_guest_init(void)
666 paravirt_ops_setup();
667 register_reboot_notifier(&kvm_pv_reboot_nb);
668 for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
669 raw_spin_lock_init(&async_pf_sleepers[i].lock);
670 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
671 x86_init.irqs.trap_init = kvm_apf_trap_init;
673 if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
675 pv_ops.time.steal_clock = kvm_steal_clock;
678 if (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) &&
679 !kvm_para_has_hint(KVM_HINTS_REALTIME) &&
680 kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
681 pv_ops.mmu.flush_tlb_others = kvm_flush_tlb_others;
682 pv_ops.mmu.tlb_remove_table = tlb_remove_table;
685 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
686 apic_set_eoi_write(kvm_guest_apic_eoi_write);
689 smp_ops.smp_prepare_cpus = kvm_smp_prepare_cpus;
690 smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
691 if (kvm_para_has_feature(KVM_FEATURE_PV_SCHED_YIELD) &&
692 !kvm_para_has_hint(KVM_HINTS_REALTIME) &&
693 kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
694 smp_ops.send_call_func_ipi = kvm_smp_send_call_func_ipi;
695 pr_info("KVM setup pv sched yield\n");
697 if (cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/kvm:online",
698 kvm_cpu_online, kvm_cpu_down_prepare) < 0)
699 pr_err("kvm_guest: Failed to install cpu hotplug callbacks\n");
701 sev_map_percpu_data();
702 kvm_guest_cpu_init();
705 #ifdef CONFIG_KEXEC_CORE
706 machine_ops.crash_shutdown = kvm_crash_shutdown;
709 register_syscore_ops(&kvm_syscore_ops);
712 * Hard lockup detection is enabled by default. Disable it, as guests
713 * can get false positives too easily, for example if the host is
716 hardlockup_detector_disable();
719 static noinline uint32_t __kvm_cpuid_base(void)
721 if (boot_cpu_data.cpuid_level < 0)
722 return 0; /* So we don't blow up on old processors */
724 if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
725 return hypervisor_cpuid_base("KVMKVMKVM\0\0\0", 0);
730 static inline uint32_t kvm_cpuid_base(void)
732 static int kvm_cpuid_base = -1;
734 if (kvm_cpuid_base == -1)
735 kvm_cpuid_base = __kvm_cpuid_base();
737 return kvm_cpuid_base;
740 bool kvm_para_available(void)
742 return kvm_cpuid_base() != 0;
744 EXPORT_SYMBOL_GPL(kvm_para_available);
746 unsigned int kvm_arch_para_features(void)
748 return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES);
751 unsigned int kvm_arch_para_hints(void)
753 return cpuid_edx(kvm_cpuid_base() | KVM_CPUID_FEATURES);
755 EXPORT_SYMBOL_GPL(kvm_arch_para_hints);
757 static uint32_t __init kvm_detect(void)
759 return kvm_cpuid_base();
762 static void __init kvm_apic_init(void)
764 #if defined(CONFIG_SMP)
765 if (kvm_para_has_feature(KVM_FEATURE_PV_SEND_IPI))
770 static void __init kvm_init_platform(void)
773 x86_platform.apic_post_init = kvm_apic_init;
776 const __initconst struct hypervisor_x86 x86_hyper_kvm = {
778 .detect = kvm_detect,
779 .type = X86_HYPER_KVM,
780 .init.guest_late_init = kvm_guest_init,
781 .init.x2apic_available = kvm_para_available,
782 .init.init_platform = kvm_init_platform,
785 static __init int activate_jump_labels(void)
787 if (has_steal_clock) {
788 static_key_slow_inc(¶virt_steal_enabled);
790 static_key_slow_inc(¶virt_steal_rq_enabled);
795 arch_initcall(activate_jump_labels);
797 static __init int kvm_setup_pv_tlb_flush(void)
801 if (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) &&
802 !kvm_para_has_hint(KVM_HINTS_REALTIME) &&
803 kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
804 for_each_possible_cpu(cpu) {
805 zalloc_cpumask_var_node(per_cpu_ptr(&__pv_tlb_mask, cpu),
806 GFP_KERNEL, cpu_to_node(cpu));
808 pr_info("KVM setup pv remote TLB flush\n");
813 arch_initcall(kvm_setup_pv_tlb_flush);
815 #ifdef CONFIG_PARAVIRT_SPINLOCKS
817 /* Kick a cpu by its apicid. Used to wake up a halted vcpu */
818 static void kvm_kick_cpu(int cpu)
821 unsigned long flags = 0;
823 apicid = per_cpu(x86_cpu_to_apicid, cpu);
824 kvm_hypercall2(KVM_HC_KICK_CPU, flags, apicid);
827 #include <asm/qspinlock.h>
829 static void kvm_wait(u8 *ptr, u8 val)
836 local_irq_save(flags);
838 if (READ_ONCE(*ptr) != val)
842 * halt until it's our turn and kicked. Note that we do safe halt
843 * for irq enabled case to avoid hang when lock info is overwritten
844 * in irq spinlock slowpath and no spurious interrupt occur to save us.
846 if (arch_irqs_disabled_flags(flags))
852 local_irq_restore(flags);
856 __visible bool __kvm_vcpu_is_preempted(long cpu)
858 struct kvm_steal_time *src = &per_cpu(steal_time, cpu);
860 return !!(src->preempted & KVM_VCPU_PREEMPTED);
862 PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_is_preempted);
866 #include <asm/asm-offsets.h>
868 extern bool __raw_callee_save___kvm_vcpu_is_preempted(long);
871 * Hand-optimize version for x86-64 to avoid 8 64-bit register saving and
872 * restoring to/from the stack.
875 ".pushsection .text;"
876 ".global __raw_callee_save___kvm_vcpu_is_preempted;"
877 ".type __raw_callee_save___kvm_vcpu_is_preempted, @function;"
878 "__raw_callee_save___kvm_vcpu_is_preempted:"
879 "movq __per_cpu_offset(,%rdi,8), %rax;"
880 "cmpb $0, " __stringify(KVM_STEAL_TIME_preempted) "+steal_time(%rax);"
883 ".size __raw_callee_save___kvm_vcpu_is_preempted, .-__raw_callee_save___kvm_vcpu_is_preempted;"
889 * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
891 void __init kvm_spinlock_init(void)
893 /* Does host kernel support KVM_FEATURE_PV_UNHALT? */
894 if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT))
897 if (kvm_para_has_hint(KVM_HINTS_REALTIME))
900 /* Don't use the pvqspinlock code if there is only 1 vCPU. */
901 if (num_possible_cpus() == 1)
904 __pv_init_lock_hash();
905 pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
906 pv_ops.lock.queued_spin_unlock =
907 PV_CALLEE_SAVE(__pv_queued_spin_unlock);
908 pv_ops.lock.wait = kvm_wait;
909 pv_ops.lock.kick = kvm_kick_cpu;
911 if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
912 pv_ops.lock.vcpu_is_preempted =
913 PV_CALLEE_SAVE(__kvm_vcpu_is_preempted);
917 #endif /* CONFIG_PARAVIRT_SPINLOCKS */
919 #ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL
921 static void kvm_disable_host_haltpoll(void *i)
923 wrmsrl(MSR_KVM_POLL_CONTROL, 0);
926 static void kvm_enable_host_haltpoll(void *i)
928 wrmsrl(MSR_KVM_POLL_CONTROL, 1);
931 void arch_haltpoll_enable(unsigned int cpu)
933 if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL)) {
934 pr_err_once("kvm: host does not support poll control\n");
935 pr_err_once("kvm: host upgrade recommended\n");
939 /* Enable guest halt poll disables host halt poll */
940 smp_call_function_single(cpu, kvm_disable_host_haltpoll, NULL, 1);
942 EXPORT_SYMBOL_GPL(arch_haltpoll_enable);
944 void arch_haltpoll_disable(unsigned int cpu)
946 if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL))
949 /* Enable guest halt poll disables host halt poll */
950 smp_call_function_single(cpu, kvm_enable_host_haltpoll, NULL, 1);
952 EXPORT_SYMBOL_GPL(arch_haltpoll_disable);