1/*
2 * Userland implementation of clock_gettime() for 64 bits processes in a
3 * s390 kernel for use in the vDSO
4 *
5 *  Copyright IBM Corp. 2008
6 *  Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License (version 2 only)
10 * as published by the Free Software Foundation.
11 */
12#include <asm/vdso.h>
13#include <asm/asm-offsets.h>
14#include <asm/unistd.h>
15
16	.text
17	.align 4
18	.globl __kernel_clock_gettime
19	.type  __kernel_clock_gettime,@function
20__kernel_clock_gettime:
21	.cfi_startproc
22	aghi	%r15,-16
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	br	%r14
60
61	/* CLOCK_MONOTONIC_COARSE */
623:	lg	%r4,__VDSO_UPD_COUNT(%r5)	/* load update counter */
63	tmll	%r4,0x0001			/* pending update ? loop */
64	jnz	3b
65	lg	%r0,__VDSO_WTOM_CRS_SEC(%r5)
66	lg	%r1,__VDSO_WTOM_CRS_NSEC(%r5)
67	clg	%r4,__VDSO_UPD_COUNT(%r5)	/* check update counter */
68	jne	3b
69	j	2b
70
71	/* CLOCK_REALTIME_COARSE */
724:	lg	%r4,__VDSO_UPD_COUNT(%r5)	/* load update counter */
73	tmll	%r4,0x0001			/* pending update ? loop */
74	jnz	4b
75	lg	%r0,__VDSO_XTIME_CRS_SEC(%r5)
76	lg	%r1,__VDSO_XTIME_CRS_NSEC(%r5)
77	clg	%r4,__VDSO_UPD_COUNT(%r5)	/* check update counter */
78	jne	4b
79	j	7f
80
81	/* CLOCK_REALTIME */
825:	lg	%r4,__VDSO_UPD_COUNT(%r5)	/* load update counter */
83	tmll	%r4,0x0001			/* pending update ? loop */
84	jnz	5b
85	stcke	0(%r15)				/* Store TOD clock */
86	lgf	%r2,__VDSO_TK_SHIFT(%r5)	/* Timekeeper shift */
87	lg	%r1,1(%r15)
88	sg	%r1,__VDSO_XTIME_STAMP(%r5)	/* TOD - cycle_last */
89	msgf	%r1,__VDSO_TK_MULT(%r5)		/*  * tk->mult */
90	alg	%r1,__VDSO_XTIME_NSEC(%r5)	/*  + tk->xtime_nsec */
91	srlg	%r1,%r1,0(%r2)			/*  >> tk->shift */
92	lg	%r0,__VDSO_XTIME_SEC(%r5)	/* tk->xtime_sec */
93	clg	%r4,__VDSO_UPD_COUNT(%r5)	/* check update counter */
94	jne	5b
95	larl	%r5,13f
966:	clg	%r1,0(%r5)
97	jl	7f
98	slg	%r1,0(%r5)
99	aghi	%r0,1
100	j	6b
1017:	stg	%r0,0(%r3)			/* store tp->tv_sec */
102	stg	%r1,8(%r3)			/* store tp->tv_nsec */
103	lghi	%r2,0
104	aghi	%r15,16
105	br	%r14
106
107	/* CPUCLOCK_VIRT for this thread */
1089:	icm	%r0,15,__VDSO_ECTG_OK(%r5)
109	jz	12f
110	ear	%r2,%a4
111	llilh	%r4,0x0100
112	sar	%a4,%r4
113	lghi	%r4,0
114	epsw	%r5,0
115	sacf	512				/* Magic ectg instruction */
116	.insn	ssf,0xc80100000000,__VDSO_ECTG_BASE(4),__VDSO_ECTG_USER(4),4
117	tml	%r5,0x4000
118	jo	11f
119	tml	%r5,0x8000
120	jno	10f
121	sacf	256
122	j	11f
12310:	sacf	0
12411:	sar	%a4,%r2
125	algr	%r1,%r0				/* r1 = cputime as TOD value */
126	mghi	%r1,1000			/* convert to nanoseconds */
127	srlg	%r1,%r1,12			/* r1 = cputime in nanosec */
128	lgr	%r4,%r1
129	larl	%r5,13f
130	srlg	%r1,%r1,9			/* divide by 1000000000 */
131	mlg	%r0,8(%r5)
132	srlg	%r0,%r0,11			/* r0 = tv_sec */
133	stg	%r0,0(%r3)
134	msg	%r0,0(%r5)			/* calculate tv_nsec */
135	slgr	%r4,%r0				/* r4 = tv_nsec */
136	stg	%r4,8(%r3)
137	lghi	%r2,0
138	aghi	%r15,16
139	br	%r14
140
141	/* Fallback to system call */
14212:	lghi	%r1,__NR_clock_gettime
143	svc	0
144	aghi	%r15,16
145	br	%r14
146
14713:	.quad	1000000000
14814:	.quad	19342813113834067
149	.cfi_endproc
150	.size	__kernel_clock_gettime,.-__kernel_clock_gettime
151