... | ... | ||
---|---|---|---|
29 | 29 | ||
30 | The values and fields are based on the nascent virtio-rtc specification, | 30 | The values and fields are based on the nascent virtio-rtc specification, |
31 | and the intent is that a version (hopefully precisely this version) of | 31 | and the intent is that a version (hopefully precisely this version) of |
32 | this structure will be included as an optional part of that spec. In the | 32 | this structure will be included as an optional part of that spec. In the |
33 | meantime, a simple ACPI device along the lines of VMGENID is perfectly | 33 | meantime, a simple ACPI device along the lines of VMGENID is perfectly |
34 | sufficient. | 34 | sufficient and is compatible with what's being shipped in certain |
35 | commercial hypervisors. | ||
36 | |||
37 | Linux guest support was merged into the 6.13-rc1 kernel: | ||
38 | https://git.kernel.org/torvalds/c/205032724226 | ||
35 | 39 | ||
36 | Signed-off-by: David Woodhouse <dwmw@amazon.co.uk> | 40 | Signed-off-by: David Woodhouse <dwmw@amazon.co.uk> |
41 | Reviewed-by: Paul Durrant <paul@xen.org> | ||
37 | --- | 42 | --- |
43 | v6: | ||
44 | • Rebase for DEFINE_PROP_END_OF_LIST removal and sysemu→system | ||
45 | rename. | ||
46 | |||
47 | v5: | ||
48 | • Trivial simplification to AML generation. | ||
49 | • Import vmclock-abi.h from Linux now the guest support is merged. | ||
50 | |||
51 | v4: | ||
52 | • Trivial checkpatch fixes and comment improvements. | ||
53 | |||
54 | v3: | ||
55 | • Add comment that vmclock-abi.h will come from the Linux kernel | ||
56 | headers once it gets merged there. | ||
57 | |||
38 | v2: | 58 | v2: |
39 | • Change esterror/maxerror fields to nanoseconds | 59 | • Change esterror/maxerror fields to nanoseconds. |
40 | • Change to officially assigned AMZNC10C ACPI HID | 60 | • Change to officially assigned AMZNC10C ACPI HID. |
41 | • Fix little-endian handling of fields in update | 61 | • Fix little-endian handling of fields in update. |
42 | 62 | ||
43 | hw/acpi/Kconfig | 5 + | 63 | |
44 | hw/acpi/meson.build | 1 + | 64 | hw/acpi/Kconfig | 5 + |
45 | hw/acpi/vmclock-abi.h | 186 ++++++++++++++++++++++++++++++++++++++ | 65 | hw/acpi/meson.build | 1 + |
46 | hw/acpi/vmclock.c | 183 +++++++++++++++++++++++++++++++++++++ | 66 | hw/acpi/vmclock.c | 179 ++++++++++++++++++ |
47 | hw/i386/Kconfig | 1 + | 67 | hw/i386/Kconfig | 1 + |
48 | hw/i386/acpi-build.c | 10 +- | 68 | hw/i386/acpi-build.c | 10 +- |
49 | include/hw/acpi/vmclock.h | 34 +++++++ | 69 | include/hw/acpi/vmclock.h | 34 ++++ |
50 | 7 files changed, 419 insertions(+), 1 deletion(-) | 70 | include/standard-headers/linux/vmclock-abi.h | 182 +++++++++++++++++++ |
51 | create mode 100644 hw/acpi/vmclock-abi.h | 71 | scripts/update-linux-headers.sh | 1 + |
72 | 8 files changed, 412 insertions(+), 1 deletion(-) | ||
52 | create mode 100644 hw/acpi/vmclock.c | 73 | create mode 100644 hw/acpi/vmclock.c |
53 | create mode 100644 include/hw/acpi/vmclock.h | 74 | create mode 100644 include/hw/acpi/vmclock.h |
75 | create mode 100644 include/standard-headers/linux/vmclock-abi.h | ||
54 | 76 | ||
55 | diff --git a/hw/acpi/Kconfig b/hw/acpi/Kconfig | 77 | diff --git a/hw/acpi/Kconfig b/hw/acpi/Kconfig |
56 | index XXXXXXX..XXXXXXX 100644 | 78 | index XXXXXXX..XXXXXXX 100644 |
57 | --- a/hw/acpi/Kconfig | 79 | --- a/hw/acpi/Kconfig |
58 | +++ b/hw/acpi/Kconfig | 80 | +++ b/hw/acpi/Kconfig |
... | ... | ||
78 | acpi_ss.add(when: 'CONFIG_ACPI_VMGENID', if_true: files('vmgenid.c')) | 100 | acpi_ss.add(when: 'CONFIG_ACPI_VMGENID', if_true: files('vmgenid.c')) |
79 | +acpi_ss.add(when: 'CONFIG_ACPI_VMCLOCK', if_true: files('vmclock.c')) | 101 | +acpi_ss.add(when: 'CONFIG_ACPI_VMCLOCK', if_true: files('vmclock.c')) |
80 | acpi_ss.add(when: 'CONFIG_ACPI_HW_REDUCED', if_true: files('generic_event_device.c')) | 102 | acpi_ss.add(when: 'CONFIG_ACPI_HW_REDUCED', if_true: files('generic_event_device.c')) |
81 | acpi_ss.add(when: 'CONFIG_ACPI_HMAT', if_true: files('hmat.c')) | 103 | acpi_ss.add(when: 'CONFIG_ACPI_HMAT', if_true: files('hmat.c')) |
82 | acpi_ss.add(when: 'CONFIG_ACPI_APEI', if_true: files('ghes.c'), if_false: files('ghes-stub.c')) | 104 | acpi_ss.add(when: 'CONFIG_ACPI_APEI', if_true: files('ghes.c'), if_false: files('ghes-stub.c')) |
83 | diff --git a/hw/acpi/vmclock-abi.h b/hw/acpi/vmclock-abi.h | ||
84 | new file mode 100644 | ||
85 | index XXXXXXX..XXXXXXX | ||
86 | --- /dev/null | ||
87 | +++ b/hw/acpi/vmclock-abi.h | ||
88 | @@ -XXX,XX +XXX,XX @@ | ||
89 | +/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) */ | ||
90 | + | ||
91 | +/* | ||
92 | + * This structure provides a vDSO-style clock to VM guests, exposing the | ||
93 | + * relationship (or lack thereof) between the CPU clock (TSC, timebase, arch | ||
94 | + * counter, etc.) and real time. It is designed to address the problem of | ||
95 | + * live migration, which other clock enlightenments do not. | ||
96 | + * | ||
97 | + * When a guest is live migrated, this affects the clock in two ways. | ||
98 | + * | ||
99 | + * First, even between identical hosts the actual frequency of the underlying | ||
100 | + * counter will change within the tolerances of its specification (typically | ||
101 | + * ±50PPM, or 4 seconds a day). This frequency also varies over time on the | ||
102 | + * same host, but can be tracked by NTP as it generally varies slowly. With | ||
103 | + * live migration there is a step change in the frequency, with no warning. | ||
104 | + * | ||
105 | + * Second, there may be a step change in the value of the counter itself, as | ||
106 | + * its accuracy is limited by the precision of the NTP synchronization on the | ||
107 | + * source and destination hosts. | ||
108 | + * | ||
109 | + * So any calibration (NTP, PTP, etc.) which the guest has done on the source | ||
110 | + * host before migration is invalid, and needs to be redone on the new host. | ||
111 | + * | ||
112 | + * In its most basic mode, this structure provides only an indication to the | ||
113 | + * guest that live migration has occurred. This allows the guest to know that | ||
114 | + * its clock is invalid and take remedial action. For applications that need | ||
115 | + * reliable accurate timestamps (e.g. distributed databases), the structure | ||
116 | + * can be mapped all the way to userspace. This allows the application to see | ||
117 | + * directly for itself that the clock is disrupted and take appropriate | ||
118 | + * action, even when using a vDSO-style method to get the time instead of a | ||
119 | + * system call. | ||
120 | + * | ||
121 | + * In its more advanced mode. this structure can also be used to expose the | ||
122 | + * precise relationship of the CPU counter to real time, as calibrated by the | ||
123 | + * host. This means that userspace applications can have accurate time | ||
124 | + * immediately after live migration, rather than having to pause operations | ||
125 | + * and wait for NTP to recover. This mode does, of course, rely on the | ||
126 | + * counter being reliable and consistent across CPUs. | ||
127 | + * | ||
128 | + * Note that this must be true UTC, never with smeared leap seconds. If a | ||
129 | + * guest wishes to construct a smeared clock, it can do so. Presenting a | ||
130 | + * smeared clock through this interface would be problematic because it | ||
131 | + * actually messes with the apparent counter *period*. A linear smearing | ||
132 | + * of 1 ms per second would effectively tweak the counter period by 1000PPM | ||
133 | + * at the start/end of the smearing period, while a sinusoidal smear would | ||
134 | + * basically be impossible to represent. | ||
135 | + * | ||
136 | + * This structure is offered with the intent that it be adopted into the | ||
137 | + * nascent virtio-rtc standard, as a virtio-rtc that does not address the live | ||
138 | + * migration problem seems a little less than fit for purpose. For that | ||
139 | + * reason, certain fields use precisely the same numeric definitions as in | ||
140 | + * the virtio-rtc proposal. The structure can also be exposed through an ACPI | ||
141 | + * device with the CID "VMCLOCK", modelled on the "VMGENID" device except for | ||
142 | + * the fact that it uses a real _CRS to convey the address of the structure | ||
143 | + * (which should be a full page, to allow for mapping directly to userspace). | ||
144 | + */ | ||
145 | + | ||
146 | +#ifndef __VMCLOCK_ABI_H__ | ||
147 | +#define __VMCLOCK_ABI_H__ | ||
148 | + | ||
149 | +#ifdef __KERNEL__ | ||
150 | +#include <linux/types.h> | ||
151 | +#else | ||
152 | +#include <stdint.h> | ||
153 | +#endif | ||
154 | + | ||
155 | +struct vmclock_abi { | ||
156 | + /* CONSTANT FIELDS */ | ||
157 | + uint32_t magic; | ||
158 | +#define VMCLOCK_MAGIC 0x4b4c4356 /* "VCLK" */ | ||
159 | + uint32_t size; /* Size of region containing this structure */ | ||
160 | + uint16_t version; /* 1 */ | ||
161 | + uint8_t counter_id; /* Matches VIRTIO_RTC_COUNTER_xxx except INVALID */ | ||
162 | +#define VMCLOCK_COUNTER_ARM_VCNT 0 | ||
163 | +#define VMCLOCK_COUNTER_X86_TSC 1 | ||
164 | +#define VMCLOCK_COUNTER_INVALID 0xff | ||
165 | + uint8_t time_type; /* Matches VIRTIO_RTC_TYPE_xxx */ | ||
166 | +#define VMCLOCK_TIME_UTC 0 /* Since 1970-01-01 00:00:00z */ | ||
167 | +#define VMCLOCK_TIME_TAI 1 /* Since 1970-01-01 00:00:00z */ | ||
168 | +#define VMCLOCK_TIME_MONOTONIC 2 /* Since undefined epoch */ | ||
169 | +#define VMCLOCK_TIME_INVALID_SMEARED 3 /* Not supported */ | ||
170 | +#define VMCLOCK_TIME_INVALID_MAYBE_SMEARED 4 /* Not supported */ | ||
171 | + | ||
172 | + /* NON-CONSTANT FIELDS PROTECTED BY SEQCOUNT LOCK */ | ||
173 | + uint32_t seq_count; /* Low bit means an update is in progress */ | ||
174 | + /* | ||
175 | + * This field changes to another non-repeating value when the CPU | ||
176 | + * counter is disrupted, for example on live migration. This lets | ||
177 | + * the guest know that it should discard any calibration it has | ||
178 | + * performed of the counter against external sources (NTP/PTP/etc.). | ||
179 | + */ | ||
180 | + uint64_t disruption_marker; | ||
181 | + uint64_t flags; | ||
182 | + /* Indicates that the tai_offset_sec field is valid */ | ||
183 | +#define VMCLOCK_FLAG_TAI_OFFSET_VALID (1 << 0) | ||
184 | + /* | ||
185 | + * Optionally used to notify guests of pending maintenance events. | ||
186 | + * A guest which provides latency-sensitive services may wish to | ||
187 | + * remove itself from service if an event is coming up. Two flags | ||
188 | + * indicate the approximate imminence of the event. | ||
189 | + */ | ||
190 | +#define VMCLOCK_FLAG_DISRUPTION_SOON (1 << 1) /* About a day */ | ||
191 | +#define VMCLOCK_FLAG_DISRUPTION_IMMINENT (1 << 2) /* About an hour */ | ||
192 | +#define VMCLOCK_FLAG_PERIOD_ESTERROR_VALID (1 << 3) | ||
193 | +#define VMCLOCK_FLAG_PERIOD_MAXERROR_VALID (1 << 4) | ||
194 | +#define VMCLOCK_FLAG_TIME_ESTERROR_VALID (1 << 5) | ||
195 | +#define VMCLOCK_FLAG_TIME_MAXERROR_VALID (1 << 6) | ||
196 | + /* | ||
197 | + * If the MONOTONIC flag is set then (other than leap seconds) it is | ||
198 | + * guaranteed that the time calculated according this structure at | ||
199 | + * any given moment shall never appear to be later than the time | ||
200 | + * calculated via the structure at any *later* moment. | ||
201 | + * | ||
202 | + * In particular, a timestamp based on a counter reading taken | ||
203 | + * immediately after setting the low bit of seq_count (and the | ||
204 | + * associated memory barrier), using the previously-valid time and | ||
205 | + * period fields, shall never be later than a timestamp based on | ||
206 | + * a counter reading taken immediately before *clearing* the low | ||
207 | + * bit again after the update, using the about-to-be-valid fields. | ||
208 | + */ | ||
209 | +#define VMCLOCK_FLAG_TIME_MONOTONIC (1 << 7) | ||
210 | + | ||
211 | + uint8_t pad[2]; | ||
212 | + uint8_t clock_status; | ||
213 | +#define VMCLOCK_STATUS_UNKNOWN 0 | ||
214 | +#define VMCLOCK_STATUS_INITIALIZING 1 | ||
215 | +#define VMCLOCK_STATUS_SYNCHRONIZED 2 | ||
216 | +#define VMCLOCK_STATUS_FREERUNNING 3 | ||
217 | +#define VMCLOCK_STATUS_UNRELIABLE 4 | ||
218 | + | ||
219 | + /* | ||
220 | + * The time exposed through this device is never smeared. This field | ||
221 | + * corresponds to the 'subtype' field in virtio-rtc, which indicates | ||
222 | + * the smearing method. However in this case it provides a *hint* to | ||
223 | + * the guest operating system, such that *if* the guest OS wants to | ||
224 | + * provide its users with an alternative clock which does not follow | ||
225 | + * UTC, it may do so in a fashion consistent with the other systems | ||
226 | + * in the nearby environment. | ||
227 | + */ | ||
228 | + uint8_t leap_second_smearing_hint; /* Matches VIRTIO_RTC_SUBTYPE_xxx */ | ||
229 | +#define VMCLOCK_SMEARING_STRICT 0 | ||
230 | +#define VMCLOCK_SMEARING_NOON_LINEAR 1 | ||
231 | +#define VMCLOCK_SMEARING_UTC_SLS 2 | ||
232 | + int16_t tai_offset_sec; | ||
233 | + uint8_t leap_indicator; | ||
234 | + /* | ||
235 | + * This field is based on the VIRTIO_RTC_LEAP_xxx values as defined | ||
236 | + * in the current draft of virtio-rtc, but since smearing cannot be | ||
237 | + * used with the shared memory device, some values are not used. | ||
238 | + * | ||
239 | + * The _POST_POS and _POST_NEG values allow the guest to perform | ||
240 | + * its own smearing during the day or so after a leap second when | ||
241 | + * such smearing may need to continue being applied for a leap | ||
242 | + * second which is now theoretically "historical". | ||
243 | + */ | ||
244 | +#define VMCLOCK_LEAP_NONE 0x00 /* No known nearby leap second */ | ||
245 | +#define VMCLOCK_LEAP_PRE_POS 0x01 /* Positive leap second at EOM */ | ||
246 | +#define VMCLOCK_LEAP_PRE_NEG 0x02 /* Negative leap second at EOM */ | ||
247 | +#define VMCLOCK_LEAP_POS 0x03 /* Set during 23:59:60 second */ | ||
248 | +#define VMCLOCK_LEAP_POST_POS 0x04 | ||
249 | +#define VMCLOCK_LEAP_POST_NEG 0x05 | ||
250 | + | ||
251 | + /* Bit shift for counter_period_frac_sec and its error rate */ | ||
252 | + uint8_t counter_period_shift; | ||
253 | + /* | ||
254 | + * Paired values of counter and UTC at a given point in time. | ||
255 | + */ | ||
256 | + uint64_t counter_value; | ||
257 | + /* | ||
258 | + * Counter period, and error margin of same. The unit of these | ||
259 | + * fields is 1/2^(64 + counter_period_shift) of a second. | ||
260 | + */ | ||
261 | + uint64_t counter_period_frac_sec; | ||
262 | + uint64_t counter_period_esterror_rate_frac_sec; | ||
263 | + uint64_t counter_period_maxerror_rate_frac_sec; | ||
264 | + | ||
265 | + /* | ||
266 | + * Time according to time_type field above. | ||
267 | + */ | ||
268 | + uint64_t time_sec; /* Seconds since time_type epoch */ | ||
269 | + uint64_t time_frac_sec; /* Units of 1/2^64 of a second */ | ||
270 | + uint64_t time_esterror_nanosec; | ||
271 | + uint64_t time_maxerror_nanosec; | ||
272 | +}; | ||
273 | + | ||
274 | +#endif /* __VMCLOCK_ABI_H__ */ | ||
275 | diff --git a/hw/acpi/vmclock.c b/hw/acpi/vmclock.c | 105 | diff --git a/hw/acpi/vmclock.c b/hw/acpi/vmclock.c |
276 | new file mode 100644 | 106 | new file mode 100644 |
277 | index XXXXXXX..XXXXXXX | 107 | index XXXXXXX..XXXXXXX |
278 | --- /dev/null | 108 | --- /dev/null |
279 | +++ b/hw/acpi/vmclock.c | 109 | +++ b/hw/acpi/vmclock.c |
... | ... | ||
298 | +#include "hw/acpi/vmclock.h" | 128 | +#include "hw/acpi/vmclock.h" |
299 | +#include "hw/nvram/fw_cfg.h" | 129 | +#include "hw/nvram/fw_cfg.h" |
300 | +#include "hw/qdev-properties.h" | 130 | +#include "hw/qdev-properties.h" |
301 | +#include "hw/qdev-properties-system.h" | 131 | +#include "hw/qdev-properties-system.h" |
302 | +#include "migration/vmstate.h" | 132 | +#include "migration/vmstate.h" |
303 | +#include "sysemu/reset.h" | 133 | +#include "system/reset.h" |
304 | + | 134 | + |
305 | +#include "vmclock-abi.h" | 135 | +#include "standard-headers/linux/vmclock-abi.h" |
306 | + | 136 | + |
307 | +void vmclock_build_acpi(VmclockState *vms, GArray *table_data, | 137 | +void vmclock_build_acpi(VmclockState *vms, GArray *table_data, |
308 | + BIOSLinker *linker, const char *oem_id) | 138 | + BIOSLinker *linker, const char *oem_id) |
309 | +{ | 139 | +{ |
310 | + Aml *ssdt, *dev, *scope, *method, *addr, *crs; | 140 | + Aml *ssdt, *dev, *scope, *crs; |
311 | + AcpiTable table = { .sig = "SSDT", .rev = 1, | 141 | + AcpiTable table = { .sig = "SSDT", .rev = 1, |
312 | + .oem_id = oem_id, .oem_table_id = "VMCLOCK" }; | 142 | + .oem_id = oem_id, .oem_table_id = "VMCLOCK" }; |
313 | + | 143 | + |
314 | + /* Put VMCLOCK into a separate SSDT table */ | 144 | + /* Put VMCLOCK into a separate SSDT table */ |
315 | + acpi_table_begin(&table, table_data); | 145 | + acpi_table_begin(&table, table_data); |
... | ... | ||
320 | + aml_append(dev, aml_name_decl("_HID", aml_string("AMZNC10C"))); | 150 | + aml_append(dev, aml_name_decl("_HID", aml_string("AMZNC10C"))); |
321 | + aml_append(dev, aml_name_decl("_CID", aml_string("VMCLOCK"))); | 151 | + aml_append(dev, aml_name_decl("_CID", aml_string("VMCLOCK"))); |
322 | + aml_append(dev, aml_name_decl("_DDN", aml_string("VMCLOCK"))); | 152 | + aml_append(dev, aml_name_decl("_DDN", aml_string("VMCLOCK"))); |
323 | + | 153 | + |
324 | + /* Simple status method */ | 154 | + /* Simple status method */ |
325 | + method = aml_method("_STA", 0, AML_NOTSERIALIZED); | 155 | + aml_append(dev, aml_name_decl("_STA", aml_int(0xf))); |
326 | + addr = aml_local(0); | ||
327 | + aml_append(method, aml_store(aml_int(0xf), addr)); | ||
328 | + aml_append(method, aml_return(addr)); | ||
329 | + aml_append(dev, method); | ||
330 | + | 156 | + |
331 | + crs = aml_resource_template(); | 157 | + crs = aml_resource_template(); |
332 | + aml_append(crs, aml_qword_memory(AML_POS_DECODE, | 158 | + aml_append(crs, aml_qword_memory(AML_POS_DECODE, |
333 | + AML_MIN_FIXED, AML_MAX_FIXED, | 159 | + AML_MIN_FIXED, AML_MAX_FIXED, |
334 | + AML_CACHEABLE, AML_READ_ONLY, | 160 | + AML_CACHEABLE, AML_READ_ONLY, |
... | ... | ||
354 | + return; | 180 | + return; |
355 | + } | 181 | + } |
356 | + | 182 | + |
357 | + seq_count = le32_to_cpu(vms->clk->seq_count) | 1; | 183 | + seq_count = le32_to_cpu(vms->clk->seq_count) | 1; |
358 | + vms->clk->seq_count = cpu_to_le32(seq_count); | 184 | + vms->clk->seq_count = cpu_to_le32(seq_count); |
185 | + /* These barriers pair with read barriers in the guest */ | ||
359 | + smp_wmb(); | 186 | + smp_wmb(); |
360 | + | 187 | + |
361 | + disruption_marker = le64_to_cpu(vms->clk->disruption_marker); | 188 | + disruption_marker = le64_to_cpu(vms->clk->disruption_marker); |
362 | + disruption_marker++; | 189 | + disruption_marker++; |
363 | + vms->clk->disruption_marker = cpu_to_le64(disruption_marker); | 190 | + vms->clk->disruption_marker = cpu_to_le64(disruption_marker); |
364 | + | 191 | + |
192 | + /* These barriers pair with read barriers in the guest */ | ||
365 | + smp_wmb(); | 193 | + smp_wmb(); |
366 | + vms->clk->seq_count = cpu_to_le32(seq_count + 1); | 194 | + vms->clk->seq_count = cpu_to_le32(seq_count + 1); |
367 | +} | 195 | +} |
368 | + | 196 | + |
369 | +/* After restoring an image, we need to update the guest memory and notify | 197 | +/* |
370 | + * it of a potential change to VM Generation ID | 198 | + * After restoring an image, we need to update the guest memory to notify |
199 | + * it of clock disruption. | ||
371 | + */ | 200 | + */ |
372 | +static int vmclock_post_load(void *opaque, int version_id) | 201 | +static int vmclock_post_load(void *opaque, int version_id) |
373 | +{ | 202 | +{ |
374 | + VmclockState *vms = opaque; | 203 | + VmclockState *vms = opaque; |
204 | + | ||
375 | + vmclock_update_guest(vms); | 205 | + vmclock_update_guest(vms); |
376 | + return 0; | 206 | + return 0; |
377 | +} | 207 | +} |
378 | + | 208 | + |
379 | +static const VMStateDescription vmstate_vmclock = { | 209 | +static const VMStateDescription vmstate_vmclock = { |
... | ... | ||
400 | + | 230 | + |
401 | +static void vmclock_realize(DeviceState *dev, Error **errp) | 231 | +static void vmclock_realize(DeviceState *dev, Error **errp) |
402 | +{ | 232 | +{ |
403 | + VmclockState *vms = VMCLOCK(dev); | 233 | + VmclockState *vms = VMCLOCK(dev); |
404 | + | 234 | + |
405 | + /* Given that this function is executing, there is at least one VMCLOCK | 235 | + /* |
236 | + * Given that this function is executing, there is at least one VMCLOCK | ||
406 | + * device. Check if there are several. | 237 | + * device. Check if there are several. |
407 | + */ | 238 | + */ |
408 | + if (!find_vmclock_dev()) { | 239 | + if (!find_vmclock_dev()) { |
409 | + error_setg(errp, "at most one %s device is permitted", TYPE_VMCLOCK); | 240 | + error_setg(errp, "at most one %s device is permitted", TYPE_VMCLOCK); |
410 | + return; | 241 | + return; |
... | ... | ||
431 | + qemu_register_reset(vmclock_handle_reset, vms); | 262 | + qemu_register_reset(vmclock_handle_reset, vms); |
432 | + | 263 | + |
433 | + vmclock_update_guest(vms); | 264 | + vmclock_update_guest(vms); |
434 | +} | 265 | +} |
435 | + | 266 | + |
436 | +static Property vmclock_device_properties[] = { | ||
437 | + DEFINE_PROP_END_OF_LIST(), | ||
438 | +}; | ||
439 | + | ||
440 | +static void vmclock_device_class_init(ObjectClass *klass, void *data) | 267 | +static void vmclock_device_class_init(ObjectClass *klass, void *data) |
441 | +{ | 268 | +{ |
442 | + DeviceClass *dc = DEVICE_CLASS(klass); | 269 | + DeviceClass *dc = DEVICE_CLASS(klass); |
443 | + | 270 | + |
444 | + dc->vmsd = &vmstate_vmclock; | 271 | + dc->vmsd = &vmstate_vmclock; |
445 | + dc->realize = vmclock_realize; | 272 | + dc->realize = vmclock_realize; |
446 | + device_class_set_props(dc, vmclock_device_properties); | ||
447 | + dc->hotpluggable = false; | 273 | + dc->hotpluggable = false; |
448 | + set_bit(DEVICE_CATEGORY_MISC, dc->categories); | 274 | + set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
449 | +} | 275 | +} |
450 | + | 276 | + |
451 | +static const TypeInfo vmclock_device_info = { | 277 | +static const TypeInfo vmclock_device_info = { |
... | ... | ||
476 | diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c | 302 | diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c |
477 | index XXXXXXX..XXXXXXX 100644 | 303 | index XXXXXXX..XXXXXXX 100644 |
478 | --- a/hw/i386/acpi-build.c | 304 | --- a/hw/i386/acpi-build.c |
479 | +++ b/hw/i386/acpi-build.c | 305 | +++ b/hw/i386/acpi-build.c |
480 | @@ -XXX,XX +XXX,XX @@ | 306 | @@ -XXX,XX +XXX,XX @@ |
481 | #include "sysemu/tpm.h" | 307 | #include "system/tpm.h" |
482 | #include "hw/acpi/tpm.h" | 308 | #include "hw/acpi/tpm.h" |
483 | #include "hw/acpi/vmgenid.h" | 309 | #include "hw/acpi/vmgenid.h" |
484 | +#include "hw/acpi/vmclock.h" | 310 | +#include "hw/acpi/vmclock.h" |
485 | #include "hw/acpi/erst.h" | 311 | #include "hw/acpi/erst.h" |
486 | #include "hw/acpi/piix4.h" | 312 | #include "hw/acpi/piix4.h" |
487 | #include "sysemu/tpm_backend.h" | 313 | #include "system/tpm_backend.h" |
488 | @@ -XXX,XX +XXX,XX @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine) | 314 | @@ -XXX,XX +XXX,XX @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine) |
489 | uint8_t *u; | 315 | uint8_t *u; |
490 | GArray *tables_blob = tables->table_data; | 316 | GArray *tables_blob = tables->table_data; |
491 | AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL }; | 317 | AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL }; |
492 | - Object *vmgenid_dev; | 318 | - Object *vmgenid_dev; |
... | ... | ||
546 | + | 372 | + |
547 | +void vmclock_build_acpi(VmclockState *vms, GArray *table_data, | 373 | +void vmclock_build_acpi(VmclockState *vms, GArray *table_data, |
548 | + BIOSLinker *linker, const char *oem_id); | 374 | + BIOSLinker *linker, const char *oem_id); |
549 | + | 375 | + |
550 | +#endif | 376 | +#endif |
377 | diff --git a/include/standard-headers/linux/vmclock-abi.h b/include/standard-headers/linux/vmclock-abi.h | ||
378 | new file mode 100644 | ||
379 | index XXXXXXX..XXXXXXX | ||
380 | --- /dev/null | ||
381 | +++ b/include/standard-headers/linux/vmclock-abi.h | ||
382 | @@ -XXX,XX +XXX,XX @@ | ||
383 | +/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) */ | ||
384 | + | ||
385 | +/* | ||
386 | + * This structure provides a vDSO-style clock to VM guests, exposing the | ||
387 | + * relationship (or lack thereof) between the CPU clock (TSC, timebase, arch | ||
388 | + * counter, etc.) and real time. It is designed to address the problem of | ||
389 | + * live migration, which other clock enlightenments do not. | ||
390 | + * | ||
391 | + * When a guest is live migrated, this affects the clock in two ways. | ||
392 | + * | ||
393 | + * First, even between identical hosts the actual frequency of the underlying | ||
394 | + * counter will change within the tolerances of its specification (typically | ||
395 | + * ±50PPM, or 4 seconds a day). This frequency also varies over time on the | ||
396 | + * same host, but can be tracked by NTP as it generally varies slowly. With | ||
397 | + * live migration there is a step change in the frequency, with no warning. | ||
398 | + * | ||
399 | + * Second, there may be a step change in the value of the counter itself, as | ||
400 | + * its accuracy is limited by the precision of the NTP synchronization on the | ||
401 | + * source and destination hosts. | ||
402 | + * | ||
403 | + * So any calibration (NTP, PTP, etc.) which the guest has done on the source | ||
404 | + * host before migration is invalid, and needs to be redone on the new host. | ||
405 | + * | ||
406 | + * In its most basic mode, this structure provides only an indication to the | ||
407 | + * guest that live migration has occurred. This allows the guest to know that | ||
408 | + * its clock is invalid and take remedial action. For applications that need | ||
409 | + * reliable accurate timestamps (e.g. distributed databases), the structure | ||
410 | + * can be mapped all the way to userspace. This allows the application to see | ||
411 | + * directly for itself that the clock is disrupted and take appropriate | ||
412 | + * action, even when using a vDSO-style method to get the time instead of a | ||
413 | + * system call. | ||
414 | + * | ||
415 | + * In its more advanced mode. this structure can also be used to expose the | ||
416 | + * precise relationship of the CPU counter to real time, as calibrated by the | ||
417 | + * host. This means that userspace applications can have accurate time | ||
418 | + * immediately after live migration, rather than having to pause operations | ||
419 | + * and wait for NTP to recover. This mode does, of course, rely on the | ||
420 | + * counter being reliable and consistent across CPUs. | ||
421 | + * | ||
422 | + * Note that this must be true UTC, never with smeared leap seconds. If a | ||
423 | + * guest wishes to construct a smeared clock, it can do so. Presenting a | ||
424 | + * smeared clock through this interface would be problematic because it | ||
425 | + * actually messes with the apparent counter *period*. A linear smearing | ||
426 | + * of 1 ms per second would effectively tweak the counter period by 1000PPM | ||
427 | + * at the start/end of the smearing period, while a sinusoidal smear would | ||
428 | + * basically be impossible to represent. | ||
429 | + * | ||
430 | + * This structure is offered with the intent that it be adopted into the | ||
431 | + * nascent virtio-rtc standard, as a virtio-rtc that does not address the live | ||
432 | + * migration problem seems a little less than fit for purpose. For that | ||
433 | + * reason, certain fields use precisely the same numeric definitions as in | ||
434 | + * the virtio-rtc proposal. The structure can also be exposed through an ACPI | ||
435 | + * device with the CID "VMCLOCK", modelled on the "VMGENID" device except for | ||
436 | + * the fact that it uses a real _CRS to convey the address of the structure | ||
437 | + * (which should be a full page, to allow for mapping directly to userspace). | ||
438 | + */ | ||
439 | + | ||
440 | +#ifndef __VMCLOCK_ABI_H__ | ||
441 | +#define __VMCLOCK_ABI_H__ | ||
442 | + | ||
443 | +#include "standard-headers/linux/types.h" | ||
444 | + | ||
445 | +struct vmclock_abi { | ||
446 | + /* CONSTANT FIELDS */ | ||
447 | + uint32_t magic; | ||
448 | +#define VMCLOCK_MAGIC 0x4b4c4356 /* "VCLK" */ | ||
449 | + uint32_t size; /* Size of region containing this structure */ | ||
450 | + uint16_t version; /* 1 */ | ||
451 | + uint8_t counter_id; /* Matches VIRTIO_RTC_COUNTER_xxx except INVALID */ | ||
452 | +#define VMCLOCK_COUNTER_ARM_VCNT 0 | ||
453 | +#define VMCLOCK_COUNTER_X86_TSC 1 | ||
454 | +#define VMCLOCK_COUNTER_INVALID 0xff | ||
455 | + uint8_t time_type; /* Matches VIRTIO_RTC_TYPE_xxx */ | ||
456 | +#define VMCLOCK_TIME_UTC 0 /* Since 1970-01-01 00:00:00z */ | ||
457 | +#define VMCLOCK_TIME_TAI 1 /* Since 1970-01-01 00:00:00z */ | ||
458 | +#define VMCLOCK_TIME_MONOTONIC 2 /* Since undefined epoch */ | ||
459 | +#define VMCLOCK_TIME_INVALID_SMEARED 3 /* Not supported */ | ||
460 | +#define VMCLOCK_TIME_INVALID_MAYBE_SMEARED 4 /* Not supported */ | ||
461 | + | ||
462 | + /* NON-CONSTANT FIELDS PROTECTED BY SEQCOUNT LOCK */ | ||
463 | + uint32_t seq_count; /* Low bit means an update is in progress */ | ||
464 | + /* | ||
465 | + * This field changes to another non-repeating value when the CPU | ||
466 | + * counter is disrupted, for example on live migration. This lets | ||
467 | + * the guest know that it should discard any calibration it has | ||
468 | + * performed of the counter against external sources (NTP/PTP/etc.). | ||
469 | + */ | ||
470 | + uint64_t disruption_marker; | ||
471 | + uint64_t flags; | ||
472 | + /* Indicates that the tai_offset_sec field is valid */ | ||
473 | +#define VMCLOCK_FLAG_TAI_OFFSET_VALID (1 << 0) | ||
474 | + /* | ||
475 | + * Optionally used to notify guests of pending maintenance events. | ||
476 | + * A guest which provides latency-sensitive services may wish to | ||
477 | + * remove itself from service if an event is coming up. Two flags | ||
478 | + * indicate the approximate imminence of the event. | ||
479 | + */ | ||
480 | +#define VMCLOCK_FLAG_DISRUPTION_SOON (1 << 1) /* About a day */ | ||
481 | +#define VMCLOCK_FLAG_DISRUPTION_IMMINENT (1 << 2) /* About an hour */ | ||
482 | +#define VMCLOCK_FLAG_PERIOD_ESTERROR_VALID (1 << 3) | ||
483 | +#define VMCLOCK_FLAG_PERIOD_MAXERROR_VALID (1 << 4) | ||
484 | +#define VMCLOCK_FLAG_TIME_ESTERROR_VALID (1 << 5) | ||
485 | +#define VMCLOCK_FLAG_TIME_MAXERROR_VALID (1 << 6) | ||
486 | + /* | ||
487 | + * If the MONOTONIC flag is set then (other than leap seconds) it is | ||
488 | + * guaranteed that the time calculated according this structure at | ||
489 | + * any given moment shall never appear to be later than the time | ||
490 | + * calculated via the structure at any *later* moment. | ||
491 | + * | ||
492 | + * In particular, a timestamp based on a counter reading taken | ||
493 | + * immediately after setting the low bit of seq_count (and the | ||
494 | + * associated memory barrier), using the previously-valid time and | ||
495 | + * period fields, shall never be later than a timestamp based on | ||
496 | + * a counter reading taken immediately before *clearing* the low | ||
497 | + * bit again after the update, using the about-to-be-valid fields. | ||
498 | + */ | ||
499 | +#define VMCLOCK_FLAG_TIME_MONOTONIC (1 << 7) | ||
500 | + | ||
501 | + uint8_t pad[2]; | ||
502 | + uint8_t clock_status; | ||
503 | +#define VMCLOCK_STATUS_UNKNOWN 0 | ||
504 | +#define VMCLOCK_STATUS_INITIALIZING 1 | ||
505 | +#define VMCLOCK_STATUS_SYNCHRONIZED 2 | ||
506 | +#define VMCLOCK_STATUS_FREERUNNING 3 | ||
507 | +#define VMCLOCK_STATUS_UNRELIABLE 4 | ||
508 | + | ||
509 | + /* | ||
510 | + * The time exposed through this device is never smeared. This field | ||
511 | + * corresponds to the 'subtype' field in virtio-rtc, which indicates | ||
512 | + * the smearing method. However in this case it provides a *hint* to | ||
513 | + * the guest operating system, such that *if* the guest OS wants to | ||
514 | + * provide its users with an alternative clock which does not follow | ||
515 | + * UTC, it may do so in a fashion consistent with the other systems | ||
516 | + * in the nearby environment. | ||
517 | + */ | ||
518 | + uint8_t leap_second_smearing_hint; /* Matches VIRTIO_RTC_SUBTYPE_xxx */ | ||
519 | +#define VMCLOCK_SMEARING_STRICT 0 | ||
520 | +#define VMCLOCK_SMEARING_NOON_LINEAR 1 | ||
521 | +#define VMCLOCK_SMEARING_UTC_SLS 2 | ||
522 | + uint16_t tai_offset_sec; /* Actually two's complement signed */ | ||
523 | + uint8_t leap_indicator; | ||
524 | + /* | ||
525 | + * This field is based on the VIRTIO_RTC_LEAP_xxx values as defined | ||
526 | + * in the current draft of virtio-rtc, but since smearing cannot be | ||
527 | + * used with the shared memory device, some values are not used. | ||
528 | + * | ||
529 | + * The _POST_POS and _POST_NEG values allow the guest to perform | ||
530 | + * its own smearing during the day or so after a leap second when | ||
531 | + * such smearing may need to continue being applied for a leap | ||
532 | + * second which is now theoretically "historical". | ||
533 | + */ | ||
534 | +#define VMCLOCK_LEAP_NONE 0x00 /* No known nearby leap second */ | ||
535 | +#define VMCLOCK_LEAP_PRE_POS 0x01 /* Positive leap second at EOM */ | ||
536 | +#define VMCLOCK_LEAP_PRE_NEG 0x02 /* Negative leap second at EOM */ | ||
537 | +#define VMCLOCK_LEAP_POS 0x03 /* Set during 23:59:60 second */ | ||
538 | +#define VMCLOCK_LEAP_POST_POS 0x04 | ||
539 | +#define VMCLOCK_LEAP_POST_NEG 0x05 | ||
540 | + | ||
541 | + /* Bit shift for counter_period_frac_sec and its error rate */ | ||
542 | + uint8_t counter_period_shift; | ||
543 | + /* | ||
544 | + * Paired values of counter and UTC at a given point in time. | ||
545 | + */ | ||
546 | + uint64_t counter_value; | ||
547 | + /* | ||
548 | + * Counter period, and error margin of same. The unit of these | ||
549 | + * fields is 1/2^(64 + counter_period_shift) of a second. | ||
550 | + */ | ||
551 | + uint64_t counter_period_frac_sec; | ||
552 | + uint64_t counter_period_esterror_rate_frac_sec; | ||
553 | + uint64_t counter_period_maxerror_rate_frac_sec; | ||
554 | + | ||
555 | + /* | ||
556 | + * Time according to time_type field above. | ||
557 | + */ | ||
558 | + uint64_t time_sec; /* Seconds since time_type epoch */ | ||
559 | + uint64_t time_frac_sec; /* Units of 1/2^64 of a second */ | ||
560 | + uint64_t time_esterror_nanosec; | ||
561 | + uint64_t time_maxerror_nanosec; | ||
562 | +}; | ||
563 | + | ||
564 | +#endif /* __VMCLOCK_ABI_H__ */ | ||
565 | diff --git a/scripts/update-linux-headers.sh b/scripts/update-linux-headers.sh | ||
566 | index XXXXXXX..XXXXXXX 100755 | ||
567 | --- a/scripts/update-linux-headers.sh | ||
568 | +++ b/scripts/update-linux-headers.sh | ||
569 | @@ -XXX,XX +XXX,XX @@ for i in "$hdrdir"/include/linux/*virtio*.h \ | ||
570 | "$hdrdir/include/linux/kernel.h" \ | ||
571 | "$hdrdir/include/linux/kvm_para.h" \ | ||
572 | "$hdrdir/include/linux/vhost_types.h" \ | ||
573 | + "$hdrdir/include/linux/vmclock-abi.h" \ | ||
574 | "$hdrdir/include/linux/sysinfo.h"; do | ||
575 | cp_portable "$i" "$output/include/standard-headers/linux" | ||
576 | done | ||
551 | -- | 577 | -- |
552 | 2.44.0 | 578 | 2.47.0 |
553 | 579 | ||
554 | 580 | diff view generated by jsdifflib |