linux/arch/powerpc/kernel
Paul Mackerras 0e469db8f7 powerpc: Rework VDSO gettimeofday to prevent time going backwards
Currently it is possible for userspace to see the result of
gettimeofday() going backwards by 1 microsecond, assuming that
userspace is using the gettimeofday() in the VDSO.  The VDSO
gettimeofday() algorithm computes the time in "xsecs", which are
units of 2^-20 seconds, or approximately 0.954 microseconds,
using the algorithm

	now = (timebase - tb_orig_stamp) * tb_to_xs + stamp_xsec

and then converts the time in xsecs to seconds and microseconds.

The kernel updates the tb_orig_stamp and stamp_xsec values every
tick in update_vsyscall().  If the length of the tick is not an
integer number of xsecs, then some precision is lost in converting
the current time to xsecs.  For example, with CONFIG_HZ=1000, the
tick is 1ms long, which is 1048.576 xsecs.  That means that
stamp_xsec will advance by either 1048 or 1049 on each tick.
With the right conditions, it is possible for userspace to get
(timebase - tb_orig_stamp) * tb_to_xs being 1049 if the kernel is
slightly late in updating the vdso_datapage, and then for stamp_xsec
to advance by 1048 when the kernel does update it, and for userspace
to then see (timebase - tb_orig_stamp) * tb_to_xs being zero due to
integer truncation.  The result is that time appears to go backwards
by 1 microsecond.

To fix this we change the VDSO gettimeofday to use a new field in the
VDSO datapage which stores the nanoseconds part of the time as a
fractional number of seconds in a 0.32 binary fraction format.
(Or put another way, as a 32-bit number in units of 0.23283 ns.)
This is convenient because we can use the mulhwu instruction to
convert it to either microseconds or nanoseconds.

Since it turns out that computing the time of day using this new field
is simpler than either using stamp_xsec (as gettimeofday does) or
stamp_xtime.tv_nsec (as clock_gettime does), this converts both
gettimeofday and clock_gettime to use the new field.  The existing
__do_get_tspec function is converted to use the new field and take
a parameter in r7 that indicates the desired resolution, 1,000,000
for microseconds or 1,000,000,000 for nanoseconds.  The __do_get_xsec
function is then unused and is deleted.

The new algorithm is

	now = ((timebase - tb_orig_stamp) << 12) * tb_to_xs
		+ (stamp_xtime_seconds << 32) + stamp_sec_fraction

with 'now' in units of 2^-32 seconds.  That is then converted to
seconds and either microseconds or nanoseconds with

	seconds = now >> 32
	partseconds = ((now & 0xffffffff) * resolution) >> 32

The 32-bit VDSO code also makes a further simplification: it ignores
the bottom 32 bits of the tb_to_xs value, which is a 0.64 format binary
fraction.  Doing so gets rid of 4 multiply instructions.  Assuming
a timebase frequency of 1GHz or less and an update interval of no
more than 10ms, the upper 32 bits of tb_to_xs will be at least
4503599, so the error from ignoring the low 32 bits will be at most
2.2ns, which is more than an order of magnitude less than the time
taken to do gettimeofday or clock_gettime on our fastest processors,
so there is no possibility of seeing inconsistent values due to this.

This also moves update_gtod() down next to its only caller, and makes
update_vsyscall use the time passed in via the wall_time argument rather
than accessing xtime directly.  At present, wall_time always points to
xtime, but that could change in future.

Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2010-07-28 21:06:47 +02:00
..
vdso32 powerpc: Rework VDSO gettimeofday to prevent time going backwards 2010-07-28 21:06:47 +02:00
vdso64 powerpc: Rework VDSO gettimeofday to prevent time going backwards 2010-07-28 21:06:47 +02:00
.gitignore powerpc: Ignore generated vmlinux.lds in git 2008-10-07 14:26:18 +11:00
align.c powerpc: Handle VSX alignment faults correctly in little-endian mode 2009-12-18 14:55:43 +11:00
asm-offsets.c powerpc: Rework VDSO gettimeofday to prevent time going backwards 2010-07-28 21:06:47 +02:00
audit.c
btext.c lmb: rename to memblock 2010-07-14 17:14:00 +10:00
cacheinfo.c include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h 2010-03-30 22:02:32 +09:00
cacheinfo.h powerpc: Rewrite sysfs processor cache info code 2009-01-08 16:25:10 +11:00
clock.c
compat_audit.c
cpu_setup_6xx.S powerpc: Use names rather than numbers for SPRGs (v2) 2009-08-20 10:12:27 +10:00
cpu_setup_44x.S AMCC PPC 460SX redwood SoC platform initial framework 2009-02-14 14:41:29 -05:00
cpu_setup_fsl_booke.S powerpc/fsl-booke: Enable L1 cache on e500v1/e500v2/e500mc CPUs 2009-06-15 21:45:30 -05:00
cpu_setup_pa6t.S
cpu_setup_ppc970.S powerpc: Use LOAD_REG_IMMEDIATE only for constants on 64-bit 2008-09-15 11:08:35 -07:00
cputable.c powerpc/e500mc: Implement machine check handler. 2010-05-21 07:41:52 -05:00
crash.c lmb: rename to memblock 2010-07-14 17:14:00 +10:00
crash_dump.c lmb: rename to memblock 2010-07-14 17:14:00 +10:00
dbell.c powerpc: Add support for using doorbells for SMP IPI 2009-02-23 15:53:03 +11:00
dma-iommu.c powerpc: Change archdata dma_data to a union 2009-09-24 15:31:43 +10:00
dma-swiotlb.c lmb: rename to memblock 2010-07-14 17:14:00 +10:00
dma.c lmb: rename to memblock 2010-07-14 17:14:00 +10:00
e500-pmu.c powerpc/perf: e500 support 2010-03-05 03:04:08 -06:00
entry_32.S powerpc/47x: Base ppc476 support 2010-05-05 09:11:10 -04:00
entry_64.S powerpc/perf_event: Fix oops due to perf_event_do_pending call 2010-05-12 14:34:00 +10:00
exceptions-64e.S powerpc/book3e-64: Remove duplicated #include 2009-09-24 15:31:41 +10:00
exceptions-64s.S powerpc: Disable interrupts for data breakpoint exceptions 2010-04-07 14:44:38 +10:00
firmware.c powerpc: Make powerpc_firmware_features __read_mostly 2010-02-09 13:56:07 +11:00
fpu.S powerpc: Use names rather than numbers for SPRGs (v2) 2009-08-20 10:12:27 +10:00
fsl_booke_entry_mapping.S powerpc/fsl-booke: Fix address issue when using relocatable kernels 2010-07-11 11:04:08 -05:00
ftrace.c Merge branch 'tracing-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip 2009-06-20 10:56:46 -07:00
head_8xx.S powerpc/8xx: Use SPRG2 and DAR registers to stash r11 and cr. 2010-04-07 18:00:34 +10:00
head_32.S KVM: PPC: Add KVM intercept handlers 2010-05-17 12:18:52 +03:00
head_40x.S powerpc: Use names rather than numbers for SPRGs (v2) 2009-08-20 10:12:27 +10:00
head_44x.S powerpc/4xx: Simple platform for the ISS 4xx simulator 2010-05-05 11:11:56 -04:00
head_64.S KVM: PPC: Name generic 64-bit code generic 2010-05-17 12:18:14 +03:00
head_booke.h powerpc/booke: Add Stack Marking support to Booke Exception Prolog 2010-05-05 08:01:52 -04:00
head_fsl_booke.S powerpc/kexec: Add support for FSL-BookE 2010-05-24 21:25:32 -05:00
ibmebus.c of: Remove duplicate fields from of_platform_driver 2010-05-22 00:10:40 -06:00
idle.c sysctl: Drop & in front of every proc_handler. 2009-11-18 08:37:40 -08:00
idle_6xx.S powerpc: Fix for getting CPU number in power_save_ppc32_restore() 2008-09-03 20:53:47 +10:00
idle_e500.S powerpc: Fix for getting CPU number in power_save_ppc32_restore() 2008-09-03 20:53:47 +10:00
idle_power4.S
init_task.c Use new __init_task_data macro in arch init_task.c files. 2009-09-21 06:27:08 +02:00
io.c powerpc: tiny memcpy_(to|from)io optimisation 2009-11-04 16:43:12 -07:00
iomap.c
iommu.c powerpc: Remove unused 'protect4gb' boot parameter 2010-05-21 17:31:13 +10:00
irq.c powerpc: Fix logic error in fixup_irqs 2010-07-08 18:11:44 +10:00
isa-bridge.c [POWERPC] Remove leftover printk in isa-bridge.c 2008-05-09 20:22:59 +10:00
kgdb.c powerpc,kgdb: Introduce low level trap catching 2010-05-20 21:04:25 -05:00
kprobes.c powerpc/kprobes: Remove resume_execution() in kprobes 2010-06-02 17:50:37 +10:00
l2cr_6xx.S
legacy_serial.c Fix spelling of 'platform' in comments and doc 2010-02-05 12:22:34 +01:00
lparcfg.c powerpc/pseries: Export data from new hcall H_EM_GET_PARMS 2010-04-07 18:00:29 +10:00
machine_kexec.c lmb: rename to memblock 2010-07-14 17:14:00 +10:00
machine_kexec_32.c kexec jump: rename KEXEC_CONTROL_CODE_SIZE to KEXEC_CONTROL_PAGE_SIZE 2008-08-15 08:35:42 -07:00
machine_kexec_64.c powerpc/kexec: Wait for online/possible CPUs only. 2010-06-15 15:02:33 +10:00
Makefile powerpc/fsl-booke: Add hibernation support for FSL BookE processors 2010-05-21 07:41:53 -05:00
misc.S perf: Always build the powerpc perf_arch_fetch_caller_regs version 2010-04-03 12:42:00 +02:00
misc_32.S powerpc: Unconditionally enabled irq stacks 2010-06-15 15:02:37 +10:00
misc_64.S powerpc: Unconditionally enabled irq stacks 2010-06-15 15:02:37 +10:00
module.c module: cleanup FIXME comments about trimming exception table entries. 2009-06-12 21:47:05 +09:30
module_32.c powerpc/ppc32: ftrace, dynamic ftrace to handle modules 2008-11-20 10:52:53 -08:00
module_64.c powerpc: Unify opcode definitions and support 2009-02-23 10:48:56 +11:00
mpc7450-pmu.c perf: Do the big rename: Performance Counters -> Performance Events 2009-09-21 14:28:04 +02:00
msi.c powerpc/PCI: include pci.h in powerpc MSI implementation 2009-03-25 08:54:29 -07:00
nvram_64.c arch/powerpc: Fix continuation line formats 2010-02-09 13:55:05 +11:00
of_device.c arch/powerpc: Move dma_mask from of_device into pdev_archdata 2010-05-22 00:10:40 -06:00
of_platform.c of: Remove duplicate fields from of_platform_driver 2010-05-22 00:10:40 -06:00
paca.c lmb: rename to memblock 2010-07-14 17:14:00 +10:00
pci-common.c PCI: clear bridge resource range if BIOS assigned bad one 2010-06-11 13:24:51 -07:00
pci_32.c include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h 2010-03-30 22:02:32 +09:00
pci_64.c of: add 'of_' prefix to machine_is_compatible() 2010-02-09 08:33:00 -07:00
pci_dn.c include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h 2010-03-30 22:02:32 +09:00
pci_of_scan.c powerpc/pci: Check devices status property when scanning OF tree 2010-05-21 17:31:09 +10:00
perf_callchain.c perf: Fix inconsistency between IP and callchain sampling 2010-01-28 14:31:20 +01:00
perf_event.c powerpc/perf_event: Fix for power_pmu_disable() 2010-07-08 18:11:37 +10:00
perf_event_fsl_emb.c powerpc/perf: e500 support 2010-03-05 03:04:08 -06:00
pmc.c powerpc: Convert pmc_owner_lock to raw_spinlock 2010-02-19 14:52:33 +11:00
power4-pmu.c perf: Do the big rename: Performance Counters -> Performance Events 2009-09-21 14:28:04 +02:00
power5+-pmu.c powerpc: perf_event: Enable SDAR in continous sample mode 2009-10-28 16:13:02 +11:00
power5-pmu.c powerpc: perf_event: Enable SDAR in continous sample mode 2009-10-28 16:13:02 +11:00
power6-pmu.c powerpc: perf_event: Enable SDAR in continous sample mode 2009-10-28 16:13:02 +11:00
power7-pmu.c powerpc: perf_event: Enable SDAR in continous sample mode 2009-10-28 16:13:02 +11:00
ppc32.h powerpc: Add VSX context save/restore, ptrace and signal support 2008-07-01 11:28:50 +10:00
ppc970-pmu.c powerpc: perf_event: Enable SDAR in continous sample mode 2009-10-28 16:13:02 +11:00
ppc_ksyms.c powerpc: Don't export cvt_fd & _df when CONFIG_PPC_FPU is not set 2010-05-31 11:51:54 +10:00
ppc_save_regs.S powerpc: Prepare xmon_save_regs for use with kdump 2008-12-23 15:13:28 +11:00
proc_powerpc.c include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h 2010-03-30 22:02:32 +09:00
process.c powerpc: Unconditionally enabled irq stacks 2010-06-15 15:02:37 +10:00
prom.c powerpc: Fix erroneous lmb->memblock conversions 2010-07-23 12:56:57 +10:00
prom_init.c powerpc: Linux cannot run with 0 cores 2010-07-08 18:11:42 +10:00
prom_init_check.sh powerpc: Fix compile errors in prom_init_check for gcc 4.5 2010-07-08 18:11:39 +10:00
prom_parse.c powerpc: Fix of_node_put() exit path in of_irq_map_one() 2009-04-20 12:18:43 -06:00
ptrace.c powerpc: Add kprobe-based event tracer 2010-04-07 18:11:43 +10:00
ptrace32.c headers: smp_lock.h redux 2009-07-12 12:22:34 -07:00
reloc_64.S powerpc: Make the 64-bit kernel as a position-independent executable 2008-09-15 11:08:38 -07:00
rtas-proc.c powerpc: Move /proc/ppc64 to /proc/powerpc update 2010-01-15 13:26:17 +11:00
rtas-rtc.c
rtas.c lmb: rename to memblock 2010-07-14 17:14:00 +10:00
rtas_flash.c powerpc: rtas_flash needs to use rtas_data_buf 2010-06-15 15:02:37 +10:00
rtas_pci.c powerpc/pci: Clean up direct access to sysdata by RTAS 2009-05-21 15:44:23 +10:00
rtasd.c powerpc/rtasd: Don't start event scan if scan rate is zero 2010-05-21 17:29:39 +10:00
setup-common.c lmb: rename to memblock 2010-07-14 17:14:00 +10:00
setup.h
setup_32.c lmb: rename to memblock 2010-07-14 17:14:00 +10:00
setup_64.c lmb: rename to memblock 2010-07-14 17:14:00 +10:00
signal.c powerpc/booke: Add support for advanced debug registers 2010-02-17 14:03:17 +11:00
signal.h powerpc: Sanitize stack pointer in signal handling code 2009-03-27 16:58:24 +11:00
signal_32.c powerpc/booke: Add support for advanced debug registers 2010-02-17 14:03:17 +11:00
signal_64.c powerpc: Sanitize stack pointer in signal handling code 2009-03-27 16:58:24 +11:00
smp-tbsync.c include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h 2010-03-30 22:02:32 +09:00
smp.c powerpc: Use common cpu_die (fixes SMP+SUSPEND build) 2010-05-21 17:31:08 +10:00
softemu8xx.c include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h 2010-03-30 22:02:32 +09:00
stacktrace.c powerpc: Removed duplicated include in stacktrace.c 2008-07-28 16:30:47 +10:00
suspend.c PAGE_ALIGN(): correctly handle 64-bit values on 32-bit architectures 2008-07-24 10:47:21 -07:00
swsusp.c powerpc/mm: Split mmu_context handling 2008-12-21 14:21:15 +11:00
swsusp_32.S powerpc/swsusp_32: Fix TLB invalidation 2010-01-15 13:20:07 +11:00
swsusp_64.c
swsusp_asm64.S powerpc: Fix 64-bit hibernation with 64k pages 2008-10-07 14:26:20 +11:00
swsusp_booke.S powerpc/fsl-booke: Add hibernation support for FSL BookE processors 2010-05-21 07:41:53 -05:00
sys_ppc32.c include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h 2010-03-30 22:02:32 +09:00
syscalls.c Add generic sys_olduname() 2010-03-12 15:52:32 -08:00
sysfs.c powerpc: Use smt_snooze_delay=-1 to always busy loop 2010-05-21 17:31:12 +10:00
systbl.S
systbl_chk.c
systbl_chk.sh
tau_6xx.c tree-wide: fix assorted typos all over the place 2009-12-04 15:39:55 +01:00
time.c powerpc: Rework VDSO gettimeofday to prevent time going backwards 2010-07-28 21:06:47 +02:00
traps.c Merge commit 'kumar/next' into next 2010-05-31 10:01:50 +10:00
udbg.c powerpc: gamecube/wii: early debugging using usbgecko 2009-12-12 22:24:31 -07:00
udbg_16550.c trivial: fix typo "for for" in multiple files 2009-09-21 15:14:54 +02:00
vdso.c lmb: rename to memblock 2010-07-14 17:14:00 +10:00
vecemu.c
vector.S powerpc: Fix usage of 64-bit instruction in 32-bit altivec code 2009-12-09 18:10:12 +11:00
vio.c Merge remote branch 'origin' into secretlab/next-devicetree 2010-05-22 00:36:56 -06:00
vmlinux.lds.S Rename .data.read_mostly to .data..read_mostly. 2010-03-03 11:26:00 +01:00