1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Userland implementation of clock_gettime() for 64 bits processes in a
4 * s390 kernel for use in the vDSO
5 *
6 *  Copyright IBM Corp. 2008
7 *  Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
8 */
9#include <asm/vdso.h>
10#include <asm/asm-offsets.h>
11#include <asm/unistd.h>
12#include <asm/dwarf.h>
13
14	.text
15	.align 4
16	.globl __kernel_clock_gettime
17	.type  __kernel_clock_gettime,@function
18__kernel_clock_gettime:
19	CFI_STARTPROC
20	aghi	%r15,-16
21	CFI_DEF_CFA_OFFSET 176
22	CFI_VAL_OFFSET 15, -160
23	larl	%r5,_vdso_data
24	cghi	%r2,__CLOCK_REALTIME_COARSE
25	je	4f
26	cghi	%r2,__CLOCK_REALTIME
27	je	5f
28	cghi	%r2,-3		/* Per-thread CPUCLOCK with PID=0, VIRT=1 */
29	je	9f
30	cghi	%r2,__CLOCK_MONOTONIC_COARSE
31	je	3f
32	cghi	%r2,__CLOCK_MONOTONIC
33	jne	12f
34
35	/* CLOCK_MONOTONIC */
360:	lg	%r4,__VDSO_UPD_COUNT(%r5)	/* load update counter */
37	tmll	%r4,0x0001			/* pending update ? loop */
38	jnz	0b
39	stcke	0(%r15)				/* Store TOD clock */
40	lgf	%r2,__VDSO_TK_SHIFT(%r5)	/* Timekeeper shift */
41	lg	%r0,__VDSO_WTOM_SEC(%r5)
42	lg	%r1,1(%r15)
43	sg	%r1,__VDSO_XTIME_STAMP(%r5)	/* TOD - cycle_last */
44	msgf	%r1,__VDSO_TK_MULT(%r5)		/*  * tk->mult */
45	alg	%r1,__VDSO_WTOM_NSEC(%r5)
46	srlg	%r1,%r1,0(%r2)			/*  >> tk->shift */
47	clg	%r4,__VDSO_UPD_COUNT(%r5)	/* check update counter */
48	jne	0b
49	larl	%r5,13f
501:	clg	%r1,0(%r5)
51	jl	2f
52	slg	%r1,0(%r5)
53	aghi	%r0,1
54	j	1b
552:	stg	%r0,0(%r3)			/* store tp->tv_sec */
56	stg	%r1,8(%r3)			/* store tp->tv_nsec */
57	lghi	%r2,0
58	aghi	%r15,16
59	CFI_DEF_CFA_OFFSET 160
60	CFI_RESTORE 15
61	br	%r14
62
63	/* CLOCK_MONOTONIC_COARSE */
64	CFI_DEF_CFA_OFFSET 176
65	CFI_VAL_OFFSET 15, -160
663:	lg	%r4,__VDSO_UPD_COUNT(%r5)	/* load update counter */
67	tmll	%r4,0x0001			/* pending update ? loop */
68	jnz	3b
69	lg	%r0,__VDSO_WTOM_CRS_SEC(%r5)
70	lg	%r1,__VDSO_WTOM_CRS_NSEC(%r5)
71	clg	%r4,__VDSO_UPD_COUNT(%r5)	/* check update counter */
72	jne	3b
73	j	2b
74
75	/* CLOCK_REALTIME_COARSE */
764:	lg	%r4,__VDSO_UPD_COUNT(%r5)	/* load update counter */
77	tmll	%r4,0x0001			/* pending update ? loop */
78	jnz	4b
79	lg	%r0,__VDSO_XTIME_CRS_SEC(%r5)
80	lg	%r1,__VDSO_XTIME_CRS_NSEC(%r5)
81	clg	%r4,__VDSO_UPD_COUNT(%r5)	/* check update counter */
82	jne	4b
83	j	7f
84
85	/* CLOCK_REALTIME */
865:	lg	%r4,__VDSO_UPD_COUNT(%r5)	/* load update counter */
87	tmll	%r4,0x0001			/* pending update ? loop */
88	jnz	5b
89	stcke	0(%r15)				/* Store TOD clock */
90	lg	%r1,1(%r15)
91	lg	%r0,__VDSO_TS_END(%r5)		/* TOD steering end time */
92	slgr	%r0,%r1				/* now - ts_steering_end */
93	ltgr	%r0,%r0				/* past end of steering ? */
94	jm	17f
95	srlg	%r0,%r0,15			/* 1 per 2^16 */
96	tm	__VDSO_TS_DIR+3(%r5),0x01	/* steering direction? */
97	jz	18f
98	lcgr	%r0,%r0				/* negative TOD offset */
9918:	algr	%r1,%r0				/* add steering offset */
10017:	lgf	%r2,__VDSO_TK_SHIFT(%r5)	/* Timekeeper shift */
101	sg	%r1,__VDSO_XTIME_STAMP(%r5)	/* TOD - cycle_last */
102	msgf	%r1,__VDSO_TK_MULT(%r5)		/*  * tk->mult */
103	alg	%r1,__VDSO_XTIME_NSEC(%r5)	/*  + tk->xtime_nsec */
104	srlg	%r1,%r1,0(%r2)			/*  >> tk->shift */
105	lg	%r0,__VDSO_XTIME_SEC(%r5)	/* tk->xtime_sec */
106	clg	%r4,__VDSO_UPD_COUNT(%r5)	/* check update counter */
107	jne	5b
108	larl	%r5,13f
1096:	clg	%r1,0(%r5)
110	jl	7f
111	slg	%r1,0(%r5)
112	aghi	%r0,1
113	j	6b
1147:	stg	%r0,0(%r3)			/* store tp->tv_sec */
115	stg	%r1,8(%r3)			/* store tp->tv_nsec */
116	lghi	%r2,0
117	aghi	%r15,16
118	CFI_DEF_CFA_OFFSET 160
119	CFI_RESTORE 15
120	br	%r14
121
122	/* CPUCLOCK_VIRT for this thread */
123	CFI_DEF_CFA_OFFSET 176
124	CFI_VAL_OFFSET 15, -160
1259:	lghi	%r4,0
126	icm	%r0,15,__VDSO_ECTG_OK(%r5)
127	jz	12f
128	sacf	256				/* Magic ectg instruction */
129	.insn	ssf,0xc80100000000,__VDSO_ECTG_BASE(4),__VDSO_ECTG_USER(4),4
130	sacf	0
131	algr	%r1,%r0				/* r1 = cputime as TOD value */
132	mghi	%r1,1000			/* convert to nanoseconds */
133	srlg	%r1,%r1,12			/* r1 = cputime in nanosec */
134	lgr	%r4,%r1
135	larl	%r5,13f
136	srlg	%r1,%r1,9			/* divide by 1000000000 */
137	mlg	%r0,8(%r5)
138	srlg	%r0,%r0,11			/* r0 = tv_sec */
139	stg	%r0,0(%r3)
140	msg	%r0,0(%r5)			/* calculate tv_nsec */
141	slgr	%r4,%r0				/* r4 = tv_nsec */
142	stg	%r4,8(%r3)
143	lghi	%r2,0
144	aghi	%r15,16
145	CFI_DEF_CFA_OFFSET 160
146	CFI_RESTORE 15
147	br	%r14
148
149	/* Fallback to system call */
150	CFI_DEF_CFA_OFFSET 176
151	CFI_VAL_OFFSET 15, -160
15212:	lghi	%r1,__NR_clock_gettime
153	svc	0
154	aghi	%r15,16
155	CFI_DEF_CFA_OFFSET 160
156	CFI_RESTORE 15
157	br	%r14
158	CFI_ENDPROC
159
16013:	.quad	1000000000
16114:	.quad	19342813113834067
162	.size	__kernel_clock_gettime,.-__kernel_clock_gettime
163