1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2019 Linaro Limited. All rights reserved.
4f08c3bdfSopenharmony_ci * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * clock_adjtime() syscall might have as execution path:
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci *   1) a regular POSIX clock (only REALTIME clock implements adjtime())
11f08c3bdfSopenharmony_ci *      - will behave exactly like adjtimex() system call.
12f08c3bdfSopenharmony_ci *      - only one being tested here.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci *   2) a dynamic POSIX clock (which ops are implemented by PTP clocks)
15f08c3bdfSopenharmony_ci *      - will trigger the PTP clock driver function "adjtime()"
16f08c3bdfSopenharmony_ci *      - different implementations from one PTP clock to another
17f08c3bdfSopenharmony_ci *      - might return EOPNOTSUPP (like ptp_kvm_caps, for example)
18f08c3bdfSopenharmony_ci *      - no entry point for clock_adjtime(), missing "CLOCK_PTP" model
19f08c3bdfSopenharmony_ci *
20f08c3bdfSopenharmony_ci * so it is sane to check for the following errors:
21f08c3bdfSopenharmony_ci *
22f08c3bdfSopenharmony_ci *   EINVAL -  clock id being used does not exist
23f08c3bdfSopenharmony_ci *
24f08c3bdfSopenharmony_ci *   EFAULT - (struct timex *) does not point to valid memory
25f08c3bdfSopenharmony_ci *
26f08c3bdfSopenharmony_ci *   EINVAL - ADJ_OFFSET + .offset outside range -512000 < x < 512000
27f08c3bdfSopenharmony_ci *            (after 2.6.26, kernels normalize to the limit if outside range)
28f08c3bdfSopenharmony_ci *
29f08c3bdfSopenharmony_ci *   EINVAL - ADJ_FREQUENCY + .freq outside range -32768000 < x < 3276800
30f08c3bdfSopenharmony_ci *            (after 2.6.26, kernels normalize to the limit if outside range)
31f08c3bdfSopenharmony_ci *
32f08c3bdfSopenharmony_ci *   EINVAL - .tick outside permitted range (900000/HZ < .tick < 1100000/HZ)
33f08c3bdfSopenharmony_ci *
34f08c3bdfSopenharmony_ci *   EPERM  - .modes is neither 0 nor ADJ_OFFSET_SS_READ (CAP_SYS_TIME required)
35f08c3bdfSopenharmony_ci *
36f08c3bdfSopenharmony_ci *   EINVAL - .status other than those listed bellow
37f08c3bdfSopenharmony_ci *
38f08c3bdfSopenharmony_ci * For ADJ_STATUS, consider the following flags:
39f08c3bdfSopenharmony_ci *
40f08c3bdfSopenharmony_ci *      rw  STA_PLL - enable phase-locked loop updates (ADJ_OFFSET)
41f08c3bdfSopenharmony_ci *      rw  STA_PPSFREQ - enable PPS (pulse-per-second) freq discipline
42f08c3bdfSopenharmony_ci *      rw  STA_PPSTIME - enable PPS time discipline
43f08c3bdfSopenharmony_ci *      rw  STA_FLL - select freq-locked loop mode.
44f08c3bdfSopenharmony_ci *      rw  STA_INS - ins leap sec after the last sec of UTC day (all days)
45f08c3bdfSopenharmony_ci *      rw  STA_DEL - del leap sec at last sec of UTC day (all days)
46f08c3bdfSopenharmony_ci *      rw  STA_UNSYNC - clock unsynced
47f08c3bdfSopenharmony_ci *      rw  STA_FREQHOLD - hold freq. ADJ_OFFSET made w/out auto small adjs
48f08c3bdfSopenharmony_ci *      ro  STA_PPSSIGNAL - valid PPS (pulse-per-second) signal is present
49f08c3bdfSopenharmony_ci *      ro  STA_PPSJITTER - PPS signal jitter exceeded.
50f08c3bdfSopenharmony_ci *      ro  STA_PPSWANDER - PPS signal wander exceeded.
51f08c3bdfSopenharmony_ci *      ro  STA_PPSERROR - PPS signal calibration error.
52f08c3bdfSopenharmony_ci *      ro  STA_CLOKERR - clock HW fault.
53f08c3bdfSopenharmony_ci *      ro  STA_NANO - 0 = us, 1 = ns (set = ADJ_NANO, cl = ADJ_MICRO)
54f08c3bdfSopenharmony_ci *      rw  STA_MODE - mode: 0 = phased locked loop. 1 = freq locked loop
55f08c3bdfSopenharmony_ci *      ro  STA_CLK - clock source. unused.
56f08c3bdfSopenharmony_ci */
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci#include "clock_adjtime.h"
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_cistatic long hz;
61f08c3bdfSopenharmony_cistatic struct tst_timex saved, ttxc;
62f08c3bdfSopenharmony_cistatic int supported;
63f08c3bdfSopenharmony_cistatic void *bad_addr;
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_cistatic void cleanup(void);
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_cistruct test_case {
68f08c3bdfSopenharmony_ci	clockid_t clktype;
69f08c3bdfSopenharmony_ci	unsigned int modes;
70f08c3bdfSopenharmony_ci	long lowlimit;
71f08c3bdfSopenharmony_ci	long highlimit;
72f08c3bdfSopenharmony_ci	long delta;
73f08c3bdfSopenharmony_ci	int exp_err;
74f08c3bdfSopenharmony_ci	int droproot;
75f08c3bdfSopenharmony_ci};
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_cistruct test_case tc[] = {
78f08c3bdfSopenharmony_ci	{
79f08c3bdfSopenharmony_ci	 .clktype = MAX_CLOCKS,
80f08c3bdfSopenharmony_ci	 .exp_err = EINVAL,
81f08c3bdfSopenharmony_ci	},
82f08c3bdfSopenharmony_ci	{
83f08c3bdfSopenharmony_ci	 .clktype = MAX_CLOCKS + 1,
84f08c3bdfSopenharmony_ci	 .exp_err = EINVAL,
85f08c3bdfSopenharmony_ci	},
86f08c3bdfSopenharmony_ci	{
87f08c3bdfSopenharmony_ci	 .clktype = CLOCK_REALTIME,
88f08c3bdfSopenharmony_ci	 .modes = ADJ_ALL,
89f08c3bdfSopenharmony_ci	 .exp_err = EFAULT,
90f08c3bdfSopenharmony_ci	},
91f08c3bdfSopenharmony_ci	{
92f08c3bdfSopenharmony_ci	 .clktype = CLOCK_REALTIME,
93f08c3bdfSopenharmony_ci	 .modes = ADJ_TICK,
94f08c3bdfSopenharmony_ci	 .lowlimit = 900000,
95f08c3bdfSopenharmony_ci	 .delta = 1,
96f08c3bdfSopenharmony_ci	 .exp_err = EINVAL,
97f08c3bdfSopenharmony_ci	},
98f08c3bdfSopenharmony_ci	{
99f08c3bdfSopenharmony_ci	 .clktype = CLOCK_REALTIME,
100f08c3bdfSopenharmony_ci	 .modes = ADJ_TICK,
101f08c3bdfSopenharmony_ci	 .highlimit = 1100000,
102f08c3bdfSopenharmony_ci	 .delta = 1,
103f08c3bdfSopenharmony_ci	 .exp_err = EINVAL,
104f08c3bdfSopenharmony_ci	},
105f08c3bdfSopenharmony_ci	{
106f08c3bdfSopenharmony_ci	 .clktype = CLOCK_REALTIME,
107f08c3bdfSopenharmony_ci	 .modes = ADJ_ALL,
108f08c3bdfSopenharmony_ci	 .exp_err = EPERM,
109f08c3bdfSopenharmony_ci	 .droproot = 1,
110f08c3bdfSopenharmony_ci	},
111f08c3bdfSopenharmony_ci};
112f08c3bdfSopenharmony_ci
113f08c3bdfSopenharmony_cistatic struct test_variants {
114f08c3bdfSopenharmony_ci	int (*clock_adjtime)(clockid_t clk_id, void *timex);
115f08c3bdfSopenharmony_ci	enum tst_timex_type type;
116f08c3bdfSopenharmony_ci	char *desc;
117f08c3bdfSopenharmony_ci} variants[] = {
118f08c3bdfSopenharmony_ci#if (__NR_clock_adjtime != __LTP__NR_INVALID_SYSCALL)
119f08c3bdfSopenharmony_ci	{.clock_adjtime = sys_clock_adjtime, .type = TST_KERN_OLD_TIMEX, .desc = "syscall with old kernel spec"},
120f08c3bdfSopenharmony_ci#endif
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci#if (__NR_clock_adjtime64 != __LTP__NR_INVALID_SYSCALL)
123f08c3bdfSopenharmony_ci	{.clock_adjtime = sys_clock_adjtime64, .type = TST_KERN_TIMEX, .desc = "syscall time64 with kernel spec"},
124f08c3bdfSopenharmony_ci#endif
125f08c3bdfSopenharmony_ci};
126f08c3bdfSopenharmony_ci
127f08c3bdfSopenharmony_cistatic void verify_clock_adjtime(unsigned int i)
128f08c3bdfSopenharmony_ci{
129f08c3bdfSopenharmony_ci	struct test_variants *tv = &variants[tst_variant];
130f08c3bdfSopenharmony_ci	uid_t whoami = 0;
131f08c3bdfSopenharmony_ci	struct tst_timex *txcptr = &ttxc;
132f08c3bdfSopenharmony_ci	struct passwd *nobody;
133f08c3bdfSopenharmony_ci	static const char name[] = "nobody";
134f08c3bdfSopenharmony_ci	int rval;
135f08c3bdfSopenharmony_ci
136f08c3bdfSopenharmony_ci	memset(txcptr, 0, sizeof(*txcptr));
137f08c3bdfSopenharmony_ci
138f08c3bdfSopenharmony_ci	txcptr->type = tv->type;
139f08c3bdfSopenharmony_ci	rval = tv->clock_adjtime(CLOCK_REALTIME, tst_timex_get(txcptr));
140f08c3bdfSopenharmony_ci	if (rval < 0) {
141f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO, "clock_adjtime() failed %i", rval);
142f08c3bdfSopenharmony_ci		return;
143f08c3bdfSopenharmony_ci	}
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_ci	timex_show("GET", txcptr);
146f08c3bdfSopenharmony_ci
147f08c3bdfSopenharmony_ci	if (tc[i].droproot) {
148f08c3bdfSopenharmony_ci		nobody = SAFE_GETPWNAM(name);
149f08c3bdfSopenharmony_ci		whoami = nobody->pw_uid;
150f08c3bdfSopenharmony_ci		SAFE_SETEUID(whoami);
151f08c3bdfSopenharmony_ci	}
152f08c3bdfSopenharmony_ci
153f08c3bdfSopenharmony_ci	timex_set_field_uint(txcptr, ADJ_MODES, tc[i].modes);
154f08c3bdfSopenharmony_ci
155f08c3bdfSopenharmony_ci	if (tc[i].delta) {
156f08c3bdfSopenharmony_ci		if (tc[i].lowlimit)
157f08c3bdfSopenharmony_ci			timex_set_field_long(&ttxc, tc[i].modes, tc[i].lowlimit - tc[i].delta);
158f08c3bdfSopenharmony_ci
159f08c3bdfSopenharmony_ci		if (tc[i].highlimit)
160f08c3bdfSopenharmony_ci			timex_set_field_long(&ttxc, tc[i].modes, tc[i].highlimit + tc[i].delta);
161f08c3bdfSopenharmony_ci	}
162f08c3bdfSopenharmony_ci
163f08c3bdfSopenharmony_ci	/* special case: EFAULT for bad addresses */
164f08c3bdfSopenharmony_ci	if (tc[i].exp_err == EFAULT) {
165f08c3bdfSopenharmony_ci		TEST(tv->clock_adjtime(tc[i].clktype, bad_addr));
166f08c3bdfSopenharmony_ci	} else {
167f08c3bdfSopenharmony_ci		TEST(tv->clock_adjtime(tc[i].clktype, tst_timex_get(txcptr)));
168f08c3bdfSopenharmony_ci		timex_show("TEST", txcptr);
169f08c3bdfSopenharmony_ci	}
170f08c3bdfSopenharmony_ci
171f08c3bdfSopenharmony_ci	if (TST_RET >= 0) {
172f08c3bdfSopenharmony_ci		tst_res(TFAIL, "clock_adjtime(): passed unexpectedly (mode=%x, "
173f08c3bdfSopenharmony_ci				"uid=%d)", tc[i].modes, whoami);
174f08c3bdfSopenharmony_ci		return;
175f08c3bdfSopenharmony_ci	}
176f08c3bdfSopenharmony_ci
177f08c3bdfSopenharmony_ci	if (tc[i].exp_err != TST_ERR) {
178f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "clock_adjtime(): expected %d but "
179f08c3bdfSopenharmony_ci				"failed with %d (mode=%x, uid=%d)",
180f08c3bdfSopenharmony_ci				tc[i].exp_err, TST_ERR, tc[i].modes, whoami);
181f08c3bdfSopenharmony_ci		return;
182f08c3bdfSopenharmony_ci	}
183f08c3bdfSopenharmony_ci
184f08c3bdfSopenharmony_ci	tst_res(TPASS, "clock_adjtime(): failed as expected (mode=0x%x, "
185f08c3bdfSopenharmony_ci			"uid=%d)", tc[i].modes, whoami);
186f08c3bdfSopenharmony_ci
187f08c3bdfSopenharmony_ci	if (tc[i].droproot)
188f08c3bdfSopenharmony_ci		SAFE_SETEUID(0);
189f08c3bdfSopenharmony_ci}
190f08c3bdfSopenharmony_ci
191f08c3bdfSopenharmony_cistatic void setup(void)
192f08c3bdfSopenharmony_ci{
193f08c3bdfSopenharmony_ci	struct test_variants *tv = &variants[tst_variant];
194f08c3bdfSopenharmony_ci	size_t i;
195f08c3bdfSopenharmony_ci	int rval;
196f08c3bdfSopenharmony_ci
197f08c3bdfSopenharmony_ci	tst_res(TINFO, "Testing variant: %s", tv->desc);
198f08c3bdfSopenharmony_ci
199f08c3bdfSopenharmony_ci	bad_addr = tst_get_bad_addr(NULL);
200f08c3bdfSopenharmony_ci
201f08c3bdfSopenharmony_ci	saved.type = tv->type;
202f08c3bdfSopenharmony_ci	rval = tv->clock_adjtime(CLOCK_REALTIME, tst_timex_get(&saved));
203f08c3bdfSopenharmony_ci	if (rval < 0) {
204f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO, "clock_adjtime() failed %i", rval);
205f08c3bdfSopenharmony_ci		return;
206f08c3bdfSopenharmony_ci	}
207f08c3bdfSopenharmony_ci
208f08c3bdfSopenharmony_ci	supported = 1;
209f08c3bdfSopenharmony_ci
210f08c3bdfSopenharmony_ci	if (rval != TIME_OK && rval != TIME_ERROR) {
211f08c3bdfSopenharmony_ci		timex_show("SAVE_STATUS", &saved);
212f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "clock has on-going leap changes, "
213f08c3bdfSopenharmony_ci				"returned: %i", rval);
214f08c3bdfSopenharmony_ci	}
215f08c3bdfSopenharmony_ci
216f08c3bdfSopenharmony_ci	hz = SAFE_SYSCONF(_SC_CLK_TCK);
217f08c3bdfSopenharmony_ci
218f08c3bdfSopenharmony_ci	/* fix high and low limits by dividing it per HZ value */
219f08c3bdfSopenharmony_ci
220f08c3bdfSopenharmony_ci	for (i = 0; i < ARRAY_SIZE(tc); i++) {
221f08c3bdfSopenharmony_ci		if (tc[i].modes == ADJ_TICK) {
222f08c3bdfSopenharmony_ci			tc[i].highlimit /= hz;
223f08c3bdfSopenharmony_ci			tc[i].lowlimit /= hz;
224f08c3bdfSopenharmony_ci		}
225f08c3bdfSopenharmony_ci	}
226f08c3bdfSopenharmony_ci}
227f08c3bdfSopenharmony_ci
228f08c3bdfSopenharmony_cistatic void cleanup(void)
229f08c3bdfSopenharmony_ci{
230f08c3bdfSopenharmony_ci	struct test_variants *tv = &variants[tst_variant];
231f08c3bdfSopenharmony_ci	unsigned int modes = ADJ_ALL;
232f08c3bdfSopenharmony_ci	int rval;
233f08c3bdfSopenharmony_ci
234f08c3bdfSopenharmony_ci	if (supported == 0)
235f08c3bdfSopenharmony_ci		return;
236f08c3bdfSopenharmony_ci
237f08c3bdfSopenharmony_ci	/* restore clock resolution based on original status flag */
238f08c3bdfSopenharmony_ci
239f08c3bdfSopenharmony_ci	if (timex_get_field_uint(&saved, ADJ_STATUS) & STA_NANO)
240f08c3bdfSopenharmony_ci		modes |= ADJ_NANO;
241f08c3bdfSopenharmony_ci	else
242f08c3bdfSopenharmony_ci		modes |= ADJ_MICRO;
243f08c3bdfSopenharmony_ci
244f08c3bdfSopenharmony_ci	timex_set_field_uint(&saved, ADJ_MODES, modes);
245f08c3bdfSopenharmony_ci
246f08c3bdfSopenharmony_ci	/* restore original clock flags */
247f08c3bdfSopenharmony_ci
248f08c3bdfSopenharmony_ci	rval = tv->clock_adjtime(CLOCK_REALTIME, tst_timex_get(&saved));
249f08c3bdfSopenharmony_ci	if (rval < 0) {
250f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO, "clock_adjtime() failed %i", rval);
251f08c3bdfSopenharmony_ci		return;
252f08c3bdfSopenharmony_ci	}
253f08c3bdfSopenharmony_ci}
254f08c3bdfSopenharmony_ci
255f08c3bdfSopenharmony_cistatic struct tst_test test = {
256f08c3bdfSopenharmony_ci	.test = verify_clock_adjtime,
257f08c3bdfSopenharmony_ci	.setup = setup,
258f08c3bdfSopenharmony_ci	.cleanup = cleanup,
259f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tc),
260f08c3bdfSopenharmony_ci	.test_variants = ARRAY_SIZE(variants),
261f08c3bdfSopenharmony_ci	.needs_root = 1,
262f08c3bdfSopenharmony_ci	.restore_wallclock = 1,
263f08c3bdfSopenharmony_ci};
264