kvmclock.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* KVM paravirtual clock driver. A clocksource implementation
  2. Copyright (C) 2008 Glauber de Oliveira Costa, Red Hat Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. */
  15. #include <linux/clocksource.h>
  16. #include <linux/kvm_para.h>
  17. #include <asm/pvclock.h>
  18. #include <asm/arch_hooks.h>
  19. #include <asm/msr.h>
  20. #include <asm/apic.h>
  21. #include <linux/percpu.h>
  22. #include <asm/reboot.h>
  23. #define KVM_SCALE 22
  24. static int kvmclock = 1;
  25. static int parse_no_kvmclock(char *arg)
  26. {
  27. kvmclock = 0;
  28. return 0;
  29. }
  30. early_param("no-kvmclock", parse_no_kvmclock);
  31. /* The hypervisor will put information about time periodically here */
  32. static DEFINE_PER_CPU_SHARED_ALIGNED(struct pvclock_vcpu_time_info, hv_clock);
  33. static struct pvclock_wall_clock wall_clock;
  34. /*
  35. * The wallclock is the time of day when we booted. Since then, some time may
  36. * have elapsed since the hypervisor wrote the data. So we try to account for
  37. * that with system time
  38. */
  39. static unsigned long kvm_get_wallclock(void)
  40. {
  41. struct pvclock_vcpu_time_info *vcpu_time;
  42. struct timespec ts;
  43. int low, high;
  44. low = (int)__pa(&wall_clock);
  45. high = ((u64)__pa(&wall_clock) >> 32);
  46. native_write_msr(MSR_KVM_WALL_CLOCK, low, high);
  47. vcpu_time = &get_cpu_var(hv_clock);
  48. pvclock_read_wallclock(&wall_clock, vcpu_time, &ts);
  49. put_cpu_var(hv_clock);
  50. return ts.tv_sec;
  51. }
  52. static int kvm_set_wallclock(unsigned long now)
  53. {
  54. return -1;
  55. }
  56. static cycle_t kvm_clock_read(void)
  57. {
  58. struct pvclock_vcpu_time_info *src;
  59. cycle_t ret;
  60. src = &get_cpu_var(hv_clock);
  61. ret = pvclock_clocksource_read(src);
  62. put_cpu_var(hv_clock);
  63. return ret;
  64. }
  65. static struct clocksource kvm_clock = {
  66. .name = "kvm-clock",
  67. .read = kvm_clock_read,
  68. .rating = 400,
  69. .mask = CLOCKSOURCE_MASK(64),
  70. .mult = 1 << KVM_SCALE,
  71. .shift = KVM_SCALE,
  72. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  73. };
  74. static int kvm_register_clock(char *txt)
  75. {
  76. int cpu = smp_processor_id();
  77. int low, high;
  78. low = (int)__pa(&per_cpu(hv_clock, cpu)) | 1;
  79. high = ((u64)__pa(&per_cpu(hv_clock, cpu)) >> 32);
  80. printk(KERN_INFO "kvm-clock: cpu %d, msr %x:%x, %s\n",
  81. cpu, high, low, txt);
  82. return native_write_msr_safe(MSR_KVM_SYSTEM_TIME, low, high);
  83. }
  84. #ifdef CONFIG_X86_LOCAL_APIC
  85. static void kvm_setup_secondary_clock(void)
  86. {
  87. /*
  88. * Now that the first cpu already had this clocksource initialized,
  89. * we shouldn't fail.
  90. */
  91. WARN_ON(kvm_register_clock("secondary cpu clock"));
  92. /* ok, done with our trickery, call native */
  93. setup_secondary_APIC_clock();
  94. }
  95. #endif
  96. #ifdef CONFIG_SMP
  97. static void __init kvm_smp_prepare_boot_cpu(void)
  98. {
  99. WARN_ON(kvm_register_clock("primary cpu clock"));
  100. native_smp_prepare_boot_cpu();
  101. }
  102. #endif
  103. /*
  104. * After the clock is registered, the host will keep writing to the
  105. * registered memory location. If the guest happens to shutdown, this memory
  106. * won't be valid. In cases like kexec, in which you install a new kernel, this
  107. * means a random memory location will be kept being written. So before any
  108. * kind of shutdown from our side, we unregister the clock by writting anything
  109. * that does not have the 'enable' bit set in the msr
  110. */
  111. #ifdef CONFIG_KEXEC
  112. static void kvm_crash_shutdown(struct pt_regs *regs)
  113. {
  114. native_write_msr_safe(MSR_KVM_SYSTEM_TIME, 0, 0);
  115. native_machine_crash_shutdown(regs);
  116. }
  117. #endif
  118. static void kvm_shutdown(void)
  119. {
  120. native_write_msr_safe(MSR_KVM_SYSTEM_TIME, 0, 0);
  121. native_machine_shutdown();
  122. }
  123. void __init kvmclock_init(void)
  124. {
  125. if (!kvm_para_available())
  126. return;
  127. if (kvmclock && kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)) {
  128. if (kvm_register_clock("boot clock"))
  129. return;
  130. pv_time_ops.get_wallclock = kvm_get_wallclock;
  131. pv_time_ops.set_wallclock = kvm_set_wallclock;
  132. pv_time_ops.sched_clock = kvm_clock_read;
  133. #ifdef CONFIG_X86_LOCAL_APIC
  134. pv_apic_ops.setup_secondary_clock = kvm_setup_secondary_clock;
  135. #endif
  136. #ifdef CONFIG_SMP
  137. smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
  138. #endif
  139. machine_ops.shutdown = kvm_shutdown;
  140. #ifdef CONFIG_KEXEC
  141. machine_ops.crash_shutdown = kvm_crash_shutdown;
  142. #endif
  143. clocksource_register(&kvm_clock);
  144. }
  145. }