clocks.rst 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. .. SPDX-License-Identifier: GPL-2.0
  2. Clocks and Timers
  3. =================
  4. arm64
  5. -----
  6. On arm64, Hyper-V virtualizes the ARMv8 architectural system counter
  7. and timer. Guest VMs use this virtualized hardware as the Linux
  8. clocksource and clockevents via the standard arm_arch_timer.c
  9. driver, just as they would on bare metal. Linux vDSO support for the
  10. architectural system counter is functional in guest VMs on Hyper-V.
  11. While Hyper-V also provides a synthetic system clock and four synthetic
  12. per-CPU timers as described in the TLFS, they are not used by the
  13. Linux kernel in a Hyper-V guest on arm64. However, older versions
  14. of Hyper-V for arm64 only partially virtualize the ARMv8
  15. architectural timer, such that the timer does not generate
  16. interrupts in the VM. Because of this limitation, running current
  17. Linux kernel versions on these older Hyper-V versions requires an
  18. out-of-tree patch to use the Hyper-V synthetic clocks/timers instead.
  19. x86/x64
  20. -------
  21. On x86/x64, Hyper-V provides guest VMs with a synthetic system clock
  22. and four synthetic per-CPU timers as described in the TLFS. Hyper-V
  23. also provides access to the virtualized TSC via the RDTSC and
  24. related instructions. These TSC instructions do not trap to
  25. the hypervisor and so provide excellent performance in a VM.
  26. Hyper-V performs TSC calibration, and provides the TSC frequency
  27. to the guest VM via a synthetic MSR. Hyper-V initialization code
  28. in Linux reads this MSR to get the frequency, so it skips TSC
  29. calibration and sets tsc_reliable. Hyper-V provides virtualized
  30. versions of the PIT (in Hyper-V Generation 1 VMs only), local
  31. APIC timer, and RTC. Hyper-V does not provide a virtualized HPET in
  32. guest VMs.
  33. The Hyper-V synthetic system clock can be read via a synthetic MSR,
  34. but this access traps to the hypervisor. As a faster alternative,
  35. the guest can configure a memory page to be shared between the guest
  36. and the hypervisor. Hyper-V populates this memory page with a
  37. 64-bit scale value and offset value. To read the synthetic clock
  38. value, the guest reads the TSC and then applies the scale and offset
  39. as described in the Hyper-V TLFS. The resulting value advances
  40. at a constant 10 MHz frequency. In the case of a live migration
  41. to a host with a different TSC frequency, Hyper-V adjusts the
  42. scale and offset values in the shared page so that the 10 MHz
  43. frequency is maintained.
  44. Starting with Windows Server 2022 Hyper-V, Hyper-V uses hardware
  45. support for TSC frequency scaling to enable live migration of VMs
  46. across Hyper-V hosts where the TSC frequency may be different.
  47. When a Linux guest detects that this Hyper-V functionality is
  48. available, it prefers to use Linux's standard TSC-based clocksource.
  49. Otherwise, it uses the clocksource for the Hyper-V synthetic system
  50. clock implemented via the shared page (identified as
  51. "hyperv_clocksource_tsc_page").
  52. The Hyper-V synthetic system clock is available to user space via
  53. vDSO, and gettimeofday() and related system calls can execute
  54. entirely in user space. The vDSO is implemented by mapping the
  55. shared page with scale and offset values into user space. User
  56. space code performs the same algorithm of reading the TSC and
  57. appying the scale and offset to get the constant 10 MHz clock.
  58. Linux clockevents are based on Hyper-V synthetic timer 0. While
  59. Hyper-V offers 4 synthetic timers for each CPU, Linux only uses
  60. timer 0. Interrupts from stimer0 are recorded on the "HVS" line in
  61. /proc/interrupts. Clockevents based on the virtualized PIT and
  62. local APIC timer also work, but the Hyper-V synthetic timer is
  63. preferred.
  64. The driver for the Hyper-V synthetic system clock and timers is
  65. drivers/clocksource/hyperv_timer.c.