]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/powerpc/kernel/vdso64/gettimeofday.S
powerpc: Fix vDSO clock_getres()
[mirror_ubuntu-bionic-kernel.git] / arch / powerpc / kernel / vdso64 / gettimeofday.S
CommitLineData
74609f45 1/*
a7f290da
BH
2 * Userland implementation of gettimeofday() for 64 bits processes in a
3 * ppc64 kernel for use in the vDSO
4 *
5 * Copyright (C) 2004 Benjamin Herrenschmuidt (benh@kernel.crashing.org),
6 * IBM Corp.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
a7f290da
BH
13#include <asm/processor.h>
14#include <asm/ppc_asm.h>
15#include <asm/vdso.h>
16#include <asm/asm-offsets.h>
17#include <asm/unistd.h>
18
19 .text
20/*
21 * Exact prototype of gettimeofday
22 *
23 * int __kernel_gettimeofday(struct timeval *tv, struct timezone *tz);
24 *
25 */
26V_FUNCTION_BEGIN(__kernel_gettimeofday)
27 .cfi_startproc
28 mflr r12
29 .cfi_register lr,r12
30
31 mr r11,r3 /* r11 holds tv */
32 mr r10,r4 /* r10 holds tz */
33 bl V_LOCAL_FUNC(__get_datapage) /* get data page */
fde937d8 34 cmpldi r11,0 /* check if tv is NULL */
74609f45 35 beq 2f
8fd63a9e
PM
36 lis r7,1000000@ha /* load up USEC_PER_SEC */
37 addi r7,r7,1000000@l
38 bl V_LOCAL_FUNC(__do_get_tspec) /* get sec/us from tb & kernel */
39 std r4,TVAL64_TV_SEC(r11) /* store sec in tv */
40 std r5,TVAL64_TV_USEC(r11) /* store usec in tv */
74609f45 412: cmpldi r10,0 /* check if tz is NULL */
a7f290da
BH
42 beq 1f
43 lwz r4,CFG_TZ_MINUTEWEST(r3)/* fill tz */
44 lwz r5,CFG_TZ_DSTTIME(r3)
45 stw r4,TZONE_TZ_MINWEST(r10)
46 stw r5,TZONE_TZ_DSTTIME(r10)
471: mtlr r12
5d66da3d 48 crclr cr0*4+so
a7f290da
BH
49 li r3,0 /* always success */
50 blr
51 .cfi_endproc
52V_FUNCTION_END(__kernel_gettimeofday)
53
54
55/*
56 * Exact prototype of clock_gettime()
57 *
58 * int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp);
59 *
60 */
61V_FUNCTION_BEGIN(__kernel_clock_gettime)
62 .cfi_startproc
63 /* Check for supported clock IDs */
64 cmpwi cr0,r3,CLOCK_REALTIME
65 cmpwi cr1,r3,CLOCK_MONOTONIC
0c37ec2a 66 cror cr0*4+eq,cr0*4+eq,cr1*4+eq
a7f290da
BH
67 bne cr0,99f
68
69 mflr r12 /* r12 saves lr */
70 .cfi_register lr,r12
a7f290da
BH
71 mr r11,r4 /* r11 saves tp */
72 bl V_LOCAL_FUNC(__get_datapage) /* get data page */
8fd63a9e
PM
73 lis r7,NSEC_PER_SEC@h /* want nanoseconds */
74 ori r7,r7,NSEC_PER_SEC@l
597bc5c0
PM
7550: bl V_LOCAL_FUNC(__do_get_tspec) /* get time from tb & kernel */
76 bne cr1,80f /* if not monotonic, all done */
a7f290da
BH
77
78 /*
79 * CLOCK_MONOTONIC
80 */
81
a7f290da
BH
82 /* now we must fixup using wall to monotonic. We need to snapshot
83 * that value and do the counter trick again. Fortunately, we still
597bc5c0
PM
84 * have the counter value in r8 that was returned by __do_get_tspec.
85 * At this point, r4,r5 contain our sec/nsec values.
a7f290da
BH
86 */
87
6e2ac320 88 ld r6,WTOM_CLOCK_SEC(r3)
597bc5c0 89 lwa r9,WTOM_CLOCK_NSEC(r3)
a7f290da 90
597bc5c0 91 /* We now have our result in r6,r9. We create a fake dependency
a7f290da
BH
92 * on that result and re-check the counter
93 */
597bc5c0
PM
94 or r0,r6,r9
95 xor r0,r0,r0
a7f290da
BH
96 add r3,r3,r0
97 ld r0,CFG_TB_UPDATE_COUNT(r3)
98 cmpld cr0,r0,r8 /* check if updated */
99 bne- 50b
100
597bc5c0 101 /* Add wall->monotonic offset and check for overflow or underflow.
a7f290da 102 */
597bc5c0
PM
103 add r4,r4,r6
104 add r5,r5,r9
105 cmpd cr0,r5,r7
106 cmpdi cr1,r5,0
a7f290da 107 blt 1f
597bc5c0 108 subf r5,r7,r5
a7f290da 109 addi r4,r4,1
597bc5c0 1101: bge cr1,80f
0c37ec2a 111 addi r4,r4,-1
597bc5c0
PM
112 add r5,r5,r7
113
11480: std r4,TSPC64_TV_SEC(r11)
115 std r5,TSPC64_TV_NSEC(r11)
a7f290da
BH
116
117 mtlr r12
5d66da3d 118 crclr cr0*4+so
a7f290da
BH
119 li r3,0
120 blr
121
122 /*
123 * syscall fallback
124 */
a7f290da
BH
12599:
126 li r0,__NR_clock_gettime
f198c330 127 .cfi_restore lr
a7f290da
BH
128 sc
129 blr
130 .cfi_endproc
131V_FUNCTION_END(__kernel_clock_gettime)
132
133
134/*
135 * Exact prototype of clock_getres()
136 *
137 * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res);
138 *
139 */
140V_FUNCTION_BEGIN(__kernel_clock_getres)
141 .cfi_startproc
142 /* Check for supported clock IDs */
143 cmpwi cr0,r3,CLOCK_REALTIME
144 cmpwi cr1,r3,CLOCK_MONOTONIC
0c37ec2a 145 cror cr0*4+eq,cr0*4+eq,cr1*4+eq
a7f290da
BH
146 bne cr0,99f
147
50a54755
VF
148 mflr r12
149 .cfi_register lr,r12
150 bl V_LOCAL_FUNC(__get_datapage)
151 lwz r5, CLOCK_HRTIMER_RES(r3)
152 mtlr r12
a7f290da 153 li r3,0
5045ea37 154 cmpldi cr0,r4,0
5d66da3d 155 crclr cr0*4+so
a7f290da 156 beqlr
a7f290da
BH
157 std r3,TSPC64_TV_SEC(r4)
158 std r5,TSPC64_TV_NSEC(r4)
159 blr
160
161 /*
162 * syscall fallback
163 */
16499:
165 li r0,__NR_clock_getres
166 sc
167 blr
168 .cfi_endproc
169V_FUNCTION_END(__kernel_clock_getres)
170
fcb41a20
AZ
171/*
172 * Exact prototype of time()
173 *
174 * time_t time(time *t);
175 *
176 */
177V_FUNCTION_BEGIN(__kernel_time)
178 .cfi_startproc
179 mflr r12
180 .cfi_register lr,r12
181
182 mr r11,r3 /* r11 holds t */
183 bl V_LOCAL_FUNC(__get_datapage)
184
185 ld r4,STAMP_XTIME+TSPC64_TV_SEC(r3)
186
187 cmpldi r11,0 /* check if t is NULL */
188 beq 2f
189 std r4,0(r11) /* store result at *t */
1902: mtlr r12
191 crclr cr0*4+so
192 mr r3,r4
193 blr
194 .cfi_endproc
195V_FUNCTION_END(__kernel_time)
196
a7f290da
BH
197
198/*
8fd63a9e
PM
199 * This is the core of clock_gettime() and gettimeofday(),
200 * it returns the current time in r4 (seconds) and r5.
201 * On entry, r7 gives the resolution of r5, either USEC_PER_SEC
202 * or NSEC_PER_SEC, giving r5 in microseconds or nanoseconds.
597bc5c0 203 * It expects the datapage ptr in r3 and doesn't clobber it.
8fd63a9e 204 * It clobbers r0, r6 and r9.
597bc5c0
PM
205 * On return, r8 contains the counter value that can be reused.
206 * This clobbers cr0 but not any other cr field.
207 */
208V_FUNCTION_BEGIN(__do_get_tspec)
209 .cfi_startproc
210 /* check for update count & load values */
2111: ld r8,CFG_TB_UPDATE_COUNT(r3)
212 andi. r0,r8,1 /* pending update ? loop */
213 bne- 1b
214 xor r0,r8,r8 /* create dependency */
215 add r3,r3,r0
216
217 /* Get TB & offset it. We use the MFTB macro which will generate
218 * workaround code for Cell.
219 */
8fd63a9e 220 MFTB(r6)
597bc5c0 221 ld r9,CFG_TB_ORIG_STAMP(r3)
8fd63a9e 222 subf r6,r9,r6
597bc5c0
PM
223
224 /* Scale result */
225 ld r5,CFG_TB_TO_XS(r3)
8fd63a9e
PM
226 sldi r6,r6,12 /* compute time since stamp_xtime */
227 mulhdu r6,r6,r5 /* in units of 2^-32 seconds */
597bc5c0
PM
228
229 /* Add stamp since epoch */
230 ld r4,STAMP_XTIME+TSPC64_TV_SEC(r3)
8fd63a9e 231 lwz r5,STAMP_SEC_FRAC(r3)
597bc5c0
PM
232 or r0,r4,r5
233 or r0,r0,r6
234 xor r0,r0,r0
235 add r3,r3,r0
236 ld r0,CFG_TB_UPDATE_COUNT(r3)
237 cmpld r0,r8 /* check if updated */
238 bne- 1b /* reload if so */
239
240 /* convert to seconds & nanoseconds and add to stamp */
8fd63a9e
PM
241 add r6,r6,r5 /* add on fractional seconds of xtime */
242 mulhwu r5,r6,r7 /* compute micro or nanoseconds and */
597bc5c0 243 srdi r6,r6,32 /* seconds since stamp_xtime */
8fd63a9e 244 clrldi r5,r5,32
597bc5c0 245 add r4,r4,r6
597bc5c0
PM
246 blr
247 .cfi_endproc
248V_FUNCTION_END(__do_get_tspec)