xref: /linux/Documentation/virt/kvm/devices/vcpu.rst (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1.. SPDX-License-Identifier: GPL-2.0
2
3======================
4Generic vcpu interface
5======================
6
7The virtual cpu "device" also accepts the ioctls KVM_SET_DEVICE_ATTR,
8KVM_GET_DEVICE_ATTR, and KVM_HAS_DEVICE_ATTR. The interface uses the same struct
9kvm_device_attr as other devices, but targets VCPU-wide settings and controls.
10
11The groups and attributes per virtual cpu, if any, are architecture specific.
12
131. GROUP: KVM_ARM_VCPU_PMU_V3_CTRL
14==================================
15
16:Architectures: ARM64
17
181.1. ATTRIBUTE: KVM_ARM_VCPU_PMU_V3_IRQ
19---------------------------------------
20
21:Parameters: in kvm_device_attr.addr the address for PMU overflow interrupt is a
22	     pointer to an int
23
24Returns:
25
26	 =======  ========================================================
27	 -EBUSY   The PMU overflow interrupt is already set
28	 -EFAULT  Error reading interrupt number
29	 -ENXIO   PMUv3 not supported or the overflow interrupt not set
30		  when attempting to get it
31	 -ENODEV  KVM_ARM_VCPU_PMU_V3 feature missing from VCPU
32	 -EINVAL  Invalid PMU overflow interrupt number supplied or
33		  trying to set the IRQ number without using an in-kernel
34		  irqchip.
35	 =======  ========================================================
36
37A value describing the PMUv3 (Performance Monitor Unit v3) overflow interrupt
38number for this vcpu. This interrupt could be a PPI or SPI, but the interrupt
39type must be same for each vcpu. As a PPI, the interrupt number is the same for
40all vcpus, while as an SPI it must be a separate number per vcpu.
41
42For GICv5-based guests, the architected PPI (23) must be used, and must be
43communicated as the full GICv5-style Interrupt ID, i.e., 0x20000017. This ioctl
44can be omitted altogether for a GICv5-based guest.
45
461.2 ATTRIBUTE: KVM_ARM_VCPU_PMU_V3_INIT
47---------------------------------------
48
49:Parameters: no additional parameter in kvm_device_attr.addr
50
51Returns:
52
53	 =======  ======================================================
54	 -EEXIST  Interrupt number already used
55	 -ENODEV  PMUv3 not supported or GIC not initialized
56	 -ENXIO   PMUv3 not supported, missing VCPU feature, missing
57                  hardware PMU, or interrupt number not set (non-GICv5
58                  guests, only)
59	 -EBUSY   PMUv3 already initialized
60	 =======  ======================================================
61
62Request the initialization of the PMUv3.  If using the PMUv3 with an in-kernel
63virtual GIC implementation, this must be done after initializing the in-kernel
64irqchip.
65
66When the KVM_ARM_VCPU_PMU_V3_STRICT vCPU feature is enabled this must be done
67after selecting a hardware PMU.
68
691.3 ATTRIBUTE: KVM_ARM_VCPU_PMU_V3_FILTER
70-----------------------------------------
71
72:Parameters: in kvm_device_attr.addr the address for a PMU event filter is a
73             pointer to a struct kvm_pmu_event_filter
74
75:Returns:
76
77	 =======  ======================================================
78	 -ENODEV  PMUv3 not supported or GIC not initialized
79	 -ENXIO   PMUv3 not properly configured or in-kernel irqchip not
80	 	  configured as required prior to calling this attribute
81	 -EBUSY   PMUv3 already initialized or a VCPU has already run
82	 -EINVAL  Invalid filter range
83	 =======  ======================================================
84
85Request the installation of a PMU event filter described as follows::
86
87    struct kvm_pmu_event_filter {
88	    __u16	base_event;
89	    __u16	nevents;
90
91    #define KVM_PMU_EVENT_ALLOW	0
92    #define KVM_PMU_EVENT_DENY	1
93
94	    __u8	action;
95	    __u8	pad[3];
96    };
97
98A filter range is defined as the range [@base_event, @base_event + @nevents),
99together with an @action (KVM_PMU_EVENT_ALLOW or KVM_PMU_EVENT_DENY). The
100first registered range defines the global policy (global ALLOW if the first
101@action is DENY, global DENY if the first @action is ALLOW). Multiple ranges
102can be programmed, and must fit within the event space defined by the PMU
103architecture (10 bits on ARMv8.0, 16 bits from ARMv8.1 onwards).
104
105Note: "Cancelling" a filter by registering the opposite action for the same
106range doesn't change the default action. For example, installing an ALLOW
107filter for event range [0:10) as the first filter and then applying a DENY
108action for the same range will leave the whole range as disabled.
109
110Restrictions: Event 0 (SW_INCR) is never filtered, as it doesn't count a
111hardware event. Filtering event 0x1E (CHAIN) has no effect either, as it
112isn't strictly speaking an event. Filtering the cycle counter is possible
113using event 0x11 (CPU_CYCLES).
114
115When the KVM_ARM_VCPU_PMU_V3_STRICT vCPU feature is enabled this must be done
116after selecting a hardware PMU.
117
1181.4 ATTRIBUTE: KVM_ARM_VCPU_PMU_V3_SET_PMU
119------------------------------------------
120
121:Parameters: in kvm_device_attr.addr the address to an int representing the PMU
122             identifier.
123
124:Returns:
125
126	 =======  ====================================================
127	 -EBUSY   PMUv3 already initialized, a VCPU has already run or
128                  an event filter has already been set
129	 -EFAULT  Error accessing the PMU identifier
130	 -ENXIO   PMU not found
131	 -ENODEV  PMUv3 not supported or GIC not initialized
132	 -ENOMEM  Could not allocate memory
133	 =======  ====================================================
134
135Request that the VCPU uses the specified hardware PMU when creating guest events
136for the purpose of PMU emulation. The PMU identifier can be read from the "type"
137file for the desired PMU instance under /sys/devices (or, equivalent,
138/sys/bus/even_source). This attribute is particularly useful on heterogeneous
139systems where there are at least two CPU PMUs on the system. The PMU that is set
140for one VCPU will be used by all the other VCPUs. It isn't possible to set a PMU
141if a PMU event filter is already present.
142
143Note that KVM will not make any attempts to run the VCPU on the physical CPUs
144associated with the PMU specified by this attribute. This is entirely left to
145userspace. However, attempting to run the VCPU on a physical CPU not supported
146by the PMU will fail and KVM_RUN will return with
147exit_reason = KVM_EXIT_FAIL_ENTRY and populate the fail_entry struct by setting
148hardare_entry_failure_reason field to KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED and
149the cpu field to the processor id.
150
1511.5 ATTRIBUTE: KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS
152--------------------------------------------------
153
154:Parameters: in kvm_device_attr.addr the address to an unsigned int
155	     representing the maximum value taken by PMCR_EL0.N
156
157:Returns:
158
159	 =======  ====================================================
160	 -EBUSY   PMUv3 already initialized, a VCPU has already run or
161                  an event filter has already been set
162	 -EFAULT  Error accessing the value pointed to by addr
163	 -ENODEV  PMUv3 not supported or GIC not initialized
164	 -EINVAL  No PMUv3 explicitly selected, or value of N out of
165	 	  range
166	 =======  ====================================================
167
168Set the number of implemented event counters in the virtual PMU. This
169mandates that a PMU has explicitly been selected via
170KVM_ARM_VCPU_PMU_V3_SET_PMU, and will fail when no PMU has been
171explicitly selected, or the number of counters is out of range for the
172selected PMU. Selecting a new PMU cancels the effect of setting this
173attribute.
174
1752. GROUP: KVM_ARM_VCPU_TIMER_CTRL
176=================================
177
178:Architectures: ARM64
179
1802.1. ATTRIBUTES: KVM_ARM_VCPU_TIMER_IRQ_{VTIMER,PTIMER,HVTIMER,HPTIMER}
181-----------------------------------------------------------------------
182
183:Parameters: in kvm_device_attr.addr the address for the timer interrupt is a
184	     pointer to an int
185
186Returns:
187
188	 =======  =================================
189	 -EINVAL  Invalid timer interrupt number
190	 -EBUSY   One or more VCPUs has already run
191	 =======  =================================
192
193A value describing the architected timer interrupt number when connected to an
194in-kernel virtual GIC.  These must be a PPI (16 <= intid < 32).  Setting the
195attribute overrides the default values (see below).
196
197==============================  ==========================================
198KVM_ARM_VCPU_TIMER_IRQ_VTIMER   The EL1 virtual timer intid (default: 27)
199KVM_ARM_VCPU_TIMER_IRQ_PTIMER   The EL1 physical timer intid (default: 30)
200KVM_ARM_VCPU_TIMER_IRQ_HVTIMER  The EL2 virtual timer intid (default: 28)
201KVM_ARM_VCPU_TIMER_IRQ_HPTIMER  The EL2 physical timer intid (default: 26)
202==============================  ==========================================
203
204Setting the same PPI for different timers will prevent the VCPUs from running.
205Setting the interrupt number on a VCPU configures all VCPUs created at that
206time to use the number provided for a given timer, overwriting any previously
207configured values on other VCPUs.  Userspace should configure the interrupt
208numbers on at least one VCPU after creating all VCPUs and before running any
209VCPUs.
210
211.. _kvm_arm_vcpu_pvtime_ctrl:
212
2133. GROUP: KVM_ARM_VCPU_PVTIME_CTRL
214==================================
215
216:Architectures: ARM64
217
2183.1 ATTRIBUTE: KVM_ARM_VCPU_PVTIME_IPA
219--------------------------------------
220
221:Parameters: 64-bit base address
222
223Returns:
224
225	 =======  ======================================
226	 -ENXIO   Stolen time not implemented
227	 -EEXIST  Base address already set for this VCPU
228	 -EINVAL  Base address not 64 byte aligned
229	 =======  ======================================
230
231Specifies the base address of the stolen time structure for this VCPU. The
232base address must be 64 byte aligned and exist within a valid guest memory
233region. See Documentation/virt/kvm/arm/pvtime.rst for more information
234including the layout of the stolen time structure.
235
2364. GROUP: KVM_VCPU_TSC_CTRL
237===========================
238
239:Architectures: x86
240
2414.1 ATTRIBUTE: KVM_VCPU_TSC_OFFSET
242
243:Parameters: 64-bit unsigned TSC offset
244
245Returns:
246
247	 ======= ======================================
248	 -EFAULT Error reading/writing the provided
249		 parameter address.
250	 -ENXIO  Attribute not supported
251	 ======= ======================================
252
253Specifies the guest's TSC offset relative to the host's TSC. The guest's
254TSC is then derived by the following equation:
255
256  guest_tsc = host_tsc + KVM_VCPU_TSC_OFFSET
257
258This attribute is useful to adjust the guest's TSC on live migration,
259so that the TSC counts the time during which the VM was paused. The
260following describes a possible algorithm to use for this purpose.
261
262From the source VMM process:
263
2641. Invoke the KVM_GET_CLOCK ioctl to record the host TSC (tsc_src),
265   kvmclock nanoseconds (guest_src), and host CLOCK_REALTIME nanoseconds
266   (host_src).
267
2682. Read the KVM_VCPU_TSC_OFFSET attribute for every vCPU to record the
269   guest TSC offset (ofs_src[i]).
270
2713. Invoke the KVM_GET_TSC_KHZ ioctl to record the frequency of the
272   guest's TSC (freq).
273
274From the destination VMM process:
275
2764. Invoke the KVM_SET_CLOCK ioctl, providing the source nanoseconds from
277   kvmclock (guest_src) and CLOCK_REALTIME (host_src) in their respective
278   fields.  Ensure that the KVM_CLOCK_REALTIME flag is set in the provided
279   structure.
280
281   KVM will advance the VM's kvmclock to account for elapsed time since
282   recording the clock values.  Note that this will cause problems in
283   the guest (e.g., timeouts) unless CLOCK_REALTIME is synchronized
284   between the source and destination, and a reasonably short time passes
285   between the source pausing the VMs and the destination executing
286   steps 4-7.
287
2885. Invoke the KVM_GET_CLOCK ioctl to record the host TSC (tsc_dest) and
289   kvmclock nanoseconds (guest_dest).
290
2916. Adjust the guest TSC offsets for every vCPU to account for (1) time
292   elapsed since recording state and (2) difference in TSCs between the
293   source and destination machine:
294
295   ofs_dst[i] = ofs_src[i] -
296     (guest_src - guest_dest) * freq +
297     (tsc_src - tsc_dest)
298
299   ("ofs[i] + tsc - guest * freq" is the guest TSC value corresponding to
300   a time of 0 in kvmclock.  The above formula ensures that it is the
301   same on the destination as it was on the source).
302
3037. Write the KVM_VCPU_TSC_OFFSET attribute for every vCPU with the
304   respective value derived in the previous step.
305