Skip to content

Commit 3565184

Browse files
David Vrabeljohnstultz-work
authored andcommitted
x86: Increase precision of x86_platform.get/set_wallclock()
All the virtualized platforms (KVM, lguest and Xen) have persistent wallclocks that have more than one second of precision. read_persistent_wallclock() and update_persistent_wallclock() allow for nanosecond precision but their implementation on x86 with x86_platform.get/set_wallclock() only allows for one second precision. This means guests may see a wallclock time that is off by up to 1 second. Make set_wallclock() and get_wallclock() take a struct timespec parameter (which allows for nanosecond precision) so KVM and Xen guests may start with a more accurate wallclock time and a Xen dom0 can maintain a more accurate wallclock for guests. Signed-off-by: David Vrabel <david.vrabel@citrix.com> Signed-off-by: John Stultz <john.stultz@linaro.org>
1 parent 0a0a7e6 commit 3565184

8 files changed

Lines changed: 32 additions & 41 deletions

File tree

‎arch/x86/include/asm/mc146818rtc.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ static inline unsigned char current_lock_cmos_reg(void)
9595
unsigned char rtc_cmos_read(unsigned char addr);
9696
void rtc_cmos_write(unsigned char val, unsigned char addr);
9797

98-
extern int mach_set_rtc_mmss(unsigned long nowtime);
99-
extern unsigned long mach_get_cmos_time(void);
98+
extern int mach_set_rtc_mmss(const struct timespec *now);
99+
extern void mach_get_cmos_time(struct timespec *now);
100100

101101
#define RTC_IRQ 8
102102

‎arch/x86/include/asm/x86_init.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ struct x86_cpuinit_ops {
142142
void (*fixup_cpu_id)(struct cpuinfo_x86 *c, int node);
143143
};
144144

145+
struct timespec;
146+
145147
/**
146148
* struct x86_platform_ops - platform specific runtime functions
147149
* @calibrate_tsc: calibrate TSC
@@ -156,8 +158,8 @@ struct x86_cpuinit_ops {
156158
*/
157159
struct x86_platform_ops {
158160
unsigned long (*calibrate_tsc)(void);
159-
unsigned long (*get_wallclock)(void);
160-
int (*set_wallclock)(unsigned long nowtime);
161+
void (*get_wallclock)(struct timespec *ts);
162+
int (*set_wallclock)(const struct timespec *ts);
161163
void (*iommu_shutdown)(void);
162164
bool (*is_untracked_pat_range)(u64 start, u64 end);
163165
void (*nmi_init)(void);

‎arch/x86/kernel/kvmclock.c‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ static struct pvclock_wall_clock wall_clock;
4848
* have elapsed since the hypervisor wrote the data. So we try to account for
4949
* that with system time
5050
*/
51-
static unsigned long kvm_get_wallclock(void)
51+
static void kvm_get_wallclock(struct timespec *now)
5252
{
5353
struct pvclock_vcpu_time_info *vcpu_time;
54-
struct timespec ts;
5554
int low, high;
5655
int cpu;
5756

@@ -64,14 +63,12 @@ static unsigned long kvm_get_wallclock(void)
6463
cpu = smp_processor_id();
6564

6665
vcpu_time = &hv_clock[cpu].pvti;
67-
pvclock_read_wallclock(&wall_clock, vcpu_time, &ts);
66+
pvclock_read_wallclock(&wall_clock, vcpu_time, now);
6867

6968
preempt_enable();
70-
71-
return ts.tv_sec;
7269
}
7370

74-
static int kvm_set_wallclock(unsigned long now)
71+
static int kvm_set_wallclock(const struct timespec *now)
7572
{
7673
return -1;
7774
}

‎arch/x86/kernel/rtc.c‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ EXPORT_SYMBOL(rtc_lock);
3838
* jump to the next second precisely 500 ms later. Check the Motorola
3939
* MC146818A or Dallas DS12887 data sheet for details.
4040
*/
41-
int mach_set_rtc_mmss(unsigned long nowtime)
41+
int mach_set_rtc_mmss(const struct timespec *now)
4242
{
43+
unsigned long nowtime = now->tv_sec;
4344
struct rtc_time tm;
4445
int retval = 0;
4546

@@ -58,7 +59,7 @@ int mach_set_rtc_mmss(unsigned long nowtime)
5859
return retval;
5960
}
6061

61-
unsigned long mach_get_cmos_time(void)
62+
void mach_get_cmos_time(struct timespec *now)
6263
{
6364
unsigned int status, year, mon, day, hour, min, sec, century = 0;
6465
unsigned long flags;
@@ -107,7 +108,8 @@ unsigned long mach_get_cmos_time(void)
107108
} else
108109
year += CMOS_YEARS_OFFS;
109110

110-
return mktime(year, mon, day, hour, min, sec);
111+
now->tv_sec = mktime(year, mon, day, hour, min, sec);
112+
now->tv_nsec = 0;
111113
}
112114

113115
/* Routines for accessing the CMOS RAM/RTC. */
@@ -135,18 +137,13 @@ EXPORT_SYMBOL(rtc_cmos_write);
135137

136138
int update_persistent_clock(struct timespec now)
137139
{
138-
return x86_platform.set_wallclock(now.tv_sec);
140+
return x86_platform.set_wallclock(&now);
139141
}
140142

141143
/* not static: needed by APM */
142144
void read_persistent_clock(struct timespec *ts)
143145
{
144-
unsigned long retval;
145-
146-
retval = x86_platform.get_wallclock();
147-
148-
ts->tv_sec = retval;
149-
ts->tv_nsec = 0;
146+
x86_platform.get_wallclock(ts);
150147
}
151148

152149

‎arch/x86/lguest/boot.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,9 +882,9 @@ int lguest_setup_irq(unsigned int irq)
882882
* It would be far better for everyone if the Guest had its own clock, but
883883
* until then the Host gives us the time on every interrupt.
884884
*/
885-
static unsigned long lguest_get_wallclock(void)
885+
static void lguest_get_wallclock(struct timespec *now)
886886
{
887-
return lguest_data.time.tv_sec;
887+
*now = lguest_data.time;
888888
}
889889

890890
/*

‎arch/x86/platform/efi/efi.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,9 @@ static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
352352
return status;
353353
}
354354

355-
int efi_set_rtc_mmss(unsigned long nowtime)
355+
int efi_set_rtc_mmss(const struct timespec *now)
356356
{
357+
unsigned long nowtime = now->tv_sec;
357358
efi_status_t status;
358359
efi_time_t eft;
359360
efi_time_cap_t cap;
@@ -388,7 +389,7 @@ int efi_set_rtc_mmss(unsigned long nowtime)
388389
return 0;
389390
}
390391

391-
unsigned long efi_get_time(void)
392+
void efi_get_time(struct timespec *now)
392393
{
393394
efi_status_t status;
394395
efi_time_t eft;
@@ -398,8 +399,9 @@ unsigned long efi_get_time(void)
398399
if (status != EFI_SUCCESS)
399400
pr_err("Oops: efitime: can't read time!\n");
400401

401-
return mktime(eft.year, eft.month, eft.day, eft.hour,
402-
eft.minute, eft.second);
402+
now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
403+
eft.minute, eft.second);
404+
now->tv_nsec = 0;
403405
}
404406

405407
/*

‎arch/x86/xen/time.c‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,32 +191,25 @@ static void xen_read_wallclock(struct timespec *ts)
191191
put_cpu_var(xen_vcpu);
192192
}
193193

194-
static unsigned long xen_get_wallclock(void)
194+
static void xen_get_wallclock(struct timespec *now)
195195
{
196-
struct timespec ts;
197-
198-
xen_read_wallclock(&ts);
199-
return ts.tv_sec;
196+
xen_read_wallclock(now);
200197
}
201198

202-
static int xen_set_wallclock(unsigned long now)
199+
static int xen_set_wallclock(const struct timespec *now)
203200
{
204201
struct xen_platform_op op;
205-
int rc;
206202

207203
/* do nothing for domU */
208204
if (!xen_initial_domain())
209205
return -1;
210206

211207
op.cmd = XENPF_settime;
212-
op.u.settime.secs = now;
213-
op.u.settime.nsecs = 0;
208+
op.u.settime.secs = now->tv_sec;
209+
op.u.settime.nsecs = now->tv_nsec;
214210
op.u.settime.system_time = xen_clocksource_read();
215211

216-
rc = HYPERVISOR_dom0_op(&op);
217-
WARN(rc != 0, "XENPF_settime failed: now=%ld\n", now);
218-
219-
return rc;
212+
return HYPERVISOR_dom0_op(&op);
220213
}
221214

222215
static struct clocksource xen_clocksource __read_mostly = {

‎include/linux/efi.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,8 @@ extern u64 efi_mem_attribute (unsigned long phys_addr, unsigned long size);
594594
extern int __init efi_uart_console_only (void);
595595
extern void efi_initialize_iomem_resources(struct resource *code_resource,
596596
struct resource *data_resource, struct resource *bss_resource);
597-
extern unsigned long efi_get_time(void);
598-
extern int efi_set_rtc_mmss(unsigned long nowtime);
597+
extern void efi_get_time(struct timespec *now);
598+
extern int efi_set_rtc_mmss(const struct timespec *now);
599599
extern void efi_reserve_boot_services(void);
600600
extern struct efi_memory_map memmap;
601601

0 commit comments

Comments
 (0)