← Documents Documentation/virt/hyperv/clocks.rst GitHub 원문 ↗

Linux 6.18.37 · 가상화

Hyper-V clock과 timer

Hyper-V guest의 arm64·x86 clocksource, shared-page 10 MHz clock, vDSO와 stimer0 Direct Mode를 설명합니다.

Source pathDocumentation/virt/hyperv/clocks.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

clocks.rst:1-82

Hyper-V 시간 계층은 trap 없는 TSC, migration을 보정하는 shared-page scale·offset, 사용자 공간 vDSO, `stimer0` clockevent로 나뉩니다. Linux는 hardware TSC scaling과 Direct Mode를 감지해 가장 직접적인 경로를 고르고, 구형 환경에서는 synthetic clock과 VMBus 전달로 호환성을 유지합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

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

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

Clock과 timer 개요

1-5

이 문서는 Linux Hyper-V guest가 arm64와 x86/x64에서 사용하는 clocksource와 clockevent, synthetic clock·timer의 선택 방식을 설명합니다.

.. SPDX-License-Identifier: GPL-2.0

Clocks and Timers
=================

arm64

6-21

arm64에서 Hyper-V는 ARMv8 architectural system counter와 timer를 가상화합니다. Guest VM은 bare metal과 마찬가지로 표준 `arm_arch_timer.c` driver를 통해 이 가상 hardware를 Linux clocksource와 clockevent로 사용합니다.

Architectural system counter를 위한 Linux vDSO도 Hyper-V guest VM에서 동작합니다. TLFS에 설명된 synthetic system clock과 CPU별 synthetic timer 네 개도 Hyper-V가 제공하지만 arm64 Linux guest kernel은 사용하지 않습니다.

일부 구형 arm64 Hyper-V는 ARMv8 architectural timer를 부분적으로만 가상화해 VM에 timer interrupt를 생성하지 못합니다. 최신 Linux kernel을 이런 구형 Hyper-V에서 실행하려면 Hyper-V synthetic clock·timer를 대신 사용하는 out-of-tree patch가 필요합니다.

arm64 clock·timer 선택
환경Clocksource / clockevent
현재 Hyper-V가상 ARMv8 system counter·architectural timer
구형 Hyper-VArchitectural timer interrupt 미지원; synthetic 경로용 외부 patch 필요
vDSOArchitectural system counter를 사용자 공간에서 읽음

Hyper-V 세대에 따른 Linux 사용 경로입니다.

arm64
-----
On arm64, Hyper-V virtualizes the ARMv8 architectural system counter
and timer. Guest VMs use this virtualized hardware as the Linux
clocksource and clockevents via the standard arm_arch_timer.c
driver, just as they would on bare metal. Linux vDSO support for the
architectural system counter is functional in guest VMs on Hyper-V.
While Hyper-V also provides a synthetic system clock and four synthetic
per-CPU timers as described in the TLFS, they are not used by the
Linux kernel in a Hyper-V guest on arm64.  However, older versions
of Hyper-V for arm64 only partially virtualize the ARMv8
architectural timer, such that the timer does not generate
interrupts in the VM. Because of this limitation, running current
Linux kernel versions on these older Hyper-V versions requires an
out-of-tree patch to use the Hyper-V synthetic clocks/timers instead.

x86/x64 clock 장치

22-36

x86/x64에서 Hyper-V는 TLFS의 synthetic system clock과 CPU별 synthetic timer 네 개를 제공합니다. 또한 `RDTSC`와 관련 instruction으로 가상화된 TSC에 접근할 수 있습니다.

TSC instruction은 hypervisor로 trap되지 않아 VM에서도 성능이 좋습니다. Hyper-V가 TSC를 calibration하고 synthetic MSR로 주파수를 제공하므로 Linux 초기화 코드는 이 MSR을 읽어 자체 calibration을 건너뛰고 `tsc_reliable`을 설정합니다.

Hyper-V는 Generation 1 VM에만 PIT를 가상화하며 local APIC timer와 RTC도 가상화합니다. Guest VM에 가상 HPET은 제공하지 않습니다.

x86/x64 시간 장치
장치제공 여부 / 특징
Virtualized TSCRDTSC가 trap 없이 실행되고 synthetic MSR로 주파수 제공
Synthetic system clockMSR 또는 shared page로 읽음
Synthetic timersCPU당 4개 제공
PITGeneration 1 VM에서만 제공
Local APIC timer / RTC가상화 제공
HPETGuest VM에 제공하지 않음

Hyper-V가 제공하는 장치와 Linux 관점의 특징입니다.

x86/x64
-------
On x86/x64, Hyper-V provides guest VMs with a synthetic system clock
and four synthetic per-CPU timers as described in the TLFS. Hyper-V
also provides access to the virtualized TSC via the RDTSC and
related instructions. These TSC instructions do not trap to
the hypervisor and so provide excellent performance in a VM.
Hyper-V performs TSC calibration, and provides the TSC frequency
to the guest VM via a synthetic MSR.  Hyper-V initialization code
in Linux reads this MSR to get the frequency, so it skips TSC
calibration and sets tsc_reliable. Hyper-V provides virtualized
versions of the PIT (in Hyper-V  Generation 1 VMs only), local
APIC timer, and RTC. Hyper-V does not provide a virtualized HPET in
guest VMs.

Shared page synthetic clock

37-48

Synthetic MSR로 system clock을 읽으면 hypervisor trap이 발생합니다. 더 빠른 대안으로 guest와 hypervisor가 공유하는 memory page를 구성할 수 있습니다.

Hyper-V는 shared page에 64-bit scale과 offset 값을 기록합니다. Guest는 TSC를 읽은 뒤 TLFS의 알고리즘대로 scale과 offset을 적용해 일정한 10 MHz로 증가하는 synthetic clock 값을 계산합니다.

TSC 주파수가 다른 host로 live migration하면 Hyper-V가 shared page의 scale과 offset을 조정해 변환 결과의 10 MHz 주파수를 유지합니다.

Shared-page clock 읽기
Guest가 virtualized TSC를 읽음Shared page에서 64-bit scale과 offset을 읽음TLFS 공식으로 TSC에 scale과 offset 적용10 MHz synthetic clock 값 산출Live migration 시 Hyper-V가 scale·offset을 재조정

Trap 없이 constant-frequency clock을 계산합니다.

The Hyper-V synthetic system clock can be read via a synthetic MSR,
but this access traps to the hypervisor. As a faster alternative,
the guest can configure a memory page to be shared between the guest
and the hypervisor.  Hyper-V populates this memory page with a
64-bit scale value and offset value. To read the synthetic clock
value, the guest reads the TSC and then applies the scale and offset
as described in the Hyper-V TLFS. The resulting value advances
at a constant 10 MHz frequency. In the case of a live migration
to a host with a different TSC frequency, Hyper-V adjusts the
scale and offset values in the shared page so that the 10 MHz
frequency is maintained.

Hardware TSC scaling과 clocksource 선택

49-57

Windows Server 2022 Hyper-V부터는 hardware TSC frequency scaling을 사용해 TSC 주파수가 다른 Hyper-V host 사이에서도 VM live migration을 지원합니다.

Linux guest가 이 기능을 감지하면 표준 TSC 기반 Linux clocksource를 우선 사용합니다. 기능이 없으면 shared page로 구현된 Hyper-V synthetic system clock의 clocksource인 `hyperv_clocksource_tsc_page`를 사용합니다.

x86 clocksource 선택
Linux guest가 Hyper-V TSC frequency scaling capability 확인지원됨: 표준 TSC-based clocksource 사용지원되지 않음: hyperv_clocksource_tsc_page 선택Shared-page scale·offset으로 10 MHz clock 유지

TSC scaling 지원 여부가 선호 경로를 결정합니다.

Starting with Windows Server 2022 Hyper-V, Hyper-V uses hardware
support for TSC frequency scaling to enable live migration of VMs
across Hyper-V hosts where the TSC frequency may be different.
When a Linux guest detects that this Hyper-V functionality is
available, it prefers to use Linux's standard TSC-based clocksource.
Otherwise, it uses the clocksource for the Hyper-V synthetic system
clock implemented via the shared page (identified as
"hyperv_clocksource_tsc_page").

사용자 공간 vDSO

58-64

Hyper-V synthetic system clock은 vDSO를 통해 사용자 공간에서도 사용할 수 있으므로 `gettimeofday()`와 관련 system call이 전부 사용자 공간에서 실행될 수 있습니다.

vDSO는 scale과 offset이 있는 shared page를 사용자 공간에 mapping합니다. 사용자 코드는 kernel과 같은 방식으로 TSC를 읽고 scale·offset을 적용해 일정한 10 MHz clock을 계산합니다.

The Hyper-V synthetic system clock is available to user space via
vDSO, and gettimeofday() and related system calls can execute
entirely in user space.  The vDSO is implemented by mapping the
shared page with scale and offset values into user space.  User
space code performs the same algorithm of reading the TSC and
applying the scale and offset to get the constant 10 MHz clock.

stimer0 clockevent와 Direct Mode

65-79

Linux clockevent는 Hyper-V synthetic timer 0인 `stimer0`을 기반으로 합니다. Hyper-V가 CPU마다 네 synthetic timer를 제공하지만 Linux는 timer 0만 사용합니다.

구형 Hyper-V에서 `stimer0` interrupt는 VMBus control message가 되어 `vmbus_isr()`에서 demultiplex됩니다. 관련 흐름은 `Documentation/virt/hyperv/vmbus.rst`에 설명되어 있습니다.

새 Hyper-V는 `stimer0` interrupt를 architectural interrupt에 mapping하는 Direct Mode를 제공하며 Linux는 가능하면 이를 선호합니다. x86/x64에는 per-CPU interrupt가 없으므로 Direct Mode는 모든 CPU에 x86 interrupt vector `HYPERV_STIMER0_VECTOR`를 정적으로 할당하고 `stimer0` handler를 직접 호출합니다.

이 때문에 `stimer0` interrupt는 Linux IRQ에 연결되지 않고 `/proc/interrupts`의 `HVS` 행에 기록됩니다. 가상 PIT와 local APIC timer 기반 clockevent도 동작하지만 Hyper-V `stimer0`이 우선입니다.

stimer0 interrupt 전달
Mode전달 경로Linux 표시
LegacyVMBus control message -> vmbus_isr()VMBus 처리 경로
Direct ModeHYPERV_STIMER0_VECTOR -> stimer0 handler/proc/interrupts의 HVS
FallbackVirtual PIT 또는 local APIC timer지원되지만 비선호

Hyper-V 세대별 clockevent 경로입니다.

Hyper-V clockevent 선택
Hyper-V stimer0 사용 가능 여부 확인Direct Mode 가능: HYPERV_STIMER0_VECTOR 사용Direct Mode 불가: VMBus control message로 전달stimer0 불가 시 virtual PIT 또는 local APIC timer fallback

Linux가 가장 직접적인 timer interrupt 경로를 우선합니다.

Linux clockevents are based on Hyper-V synthetic timer 0 (stimer0).
While Hyper-V offers 4 synthetic timers for each CPU, Linux only uses
timer 0. In older versions of Hyper-V, an interrupt from stimer0
results in a VMBus control message that is demultiplexed by
vmbus_isr() as described in the Documentation/virt/hyperv/vmbus.rst
documentation. In newer versions of Hyper-V, stimer0 interrupts can
be mapped to an architectural interrupt, which is referred to as
"Direct Mode". Linux prefers to use Direct Mode when available. Since
x86/x64 doesn't support per-CPU interrupts, Direct Mode statically
allocates an x86 interrupt vector (HYPERV_STIMER0_VECTOR) across all CPUs
and explicitly codes it to call the stimer0 interrupt handler. Hence
interrupts from stimer0 are recorded on the "HVS" line in /proc/interrupts
rather than being associated with a Linux IRQ. Clockevents based on the
virtualized PIT and local APIC timer also work, but Hyper-V stimer0
is preferred.

구현 driver

80-82

Hyper-V synthetic system clock과 timer의 Linux driver는 `drivers/clocksource/hyperv_timer.c`에 있습니다.


The driver for the Hyper-V synthetic system clock and timers is
drivers/clocksource/hyperv_timer.c.