요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.
1. 요약·해설
원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.
2. 영어 원문 전체
번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.
원문 전체 펼치기
.. SPDX-License-Identifier: GPL-2.0
Clocks and Timers
=================
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
-------
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.
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.
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").
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.
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.
The driver for the Hyper-V synthetic system clock and timers is
drivers/clocksource/hyperv_timer.c.
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-21arm64에서 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가 필요합니다.
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-36x86/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은 제공하지 않습니다.
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.
Hardware TSC scaling과 clocksource 선택
49-57Windows 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`를 사용합니다.
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-64Hyper-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-79Linux 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`이 우선입니다.
Hyper-V 세대별 clockevent 경로입니다.
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-82Hyper-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.
요약·해설
clocks.rst:1-82Hyper-V 시간 계층은 trap 없는 TSC, migration을 보정하는 shared-page scale·offset, 사용자 공간 vDSO, `stimer0` clockevent로 나뉩니다. Linux는 hardware TSC scaling과 Direct Mode를 감지해 가장 직접적인 경로를 고르고, 구형 환경에서는 synthetic clock과 VMBus 전달로 호환성을 유지합니다.