xref: /kvm-unit-tests/s390x/migration-sck.c (revision 4c8a99ca02252d4a2bee43f4558fe47ce5ab7ec0)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * SET CLOCK migration tests
4  *
5  * Copyright IBM Corp. 2022
6  *
7  * Authors:
8  *  Nico Boehr <nrb@linux.ibm.com>
9  */
10 
11 #include <libcflat.h>
12 #include <asm/time.h>
13 
14 static void test_sck_migration(void)
15 {
16 	uint64_t now_before_set = 0, now_after_set = 0, now_after_migration, time_to_set, time_to_advance;
17 	int cc;
18 
19 	stckf(&now_before_set);
20 
21 	/* Advance the clock by a lot more than we might ever need to migrate (600s) */
22 	time_to_advance = (600ULL * 1000000) << STCK_SHIFT_US;
23 	time_to_set = now_before_set + time_to_advance;
24 
25 	cc = sck(&time_to_set);
26 	report(!cc, "setting clock succeeded");
27 
28 	/* Check the clock is running after being set */
29 	cc = stckf(&now_after_set);
30 	report(!cc, "clock running after set");
31 	report(now_after_set >= time_to_set, "TOD clock value is larger than what has been set");
32 
33 	puts("Please migrate me, then press return\n");
34 	(void)getchar();
35 
36 	cc = stckf(&now_after_migration);
37 	report(!cc, "clock still set");
38 
39 	/*
40 	 * The architectural requirement for the TOD clock is that it doesn't move backwards after
41 	 * migration. Implementations can just migrate the guest TOD value or do something more
42 	 * sophisticated (e.g. slowly adjust to the host TOD).
43 	 */
44 	report(now_after_migration >= time_to_set, "TOD clock value did not jump backwards");
45 }
46 
47 int main(void)
48 {
49 	report_prefix_push("migration-sck");
50 
51 	test_sck_migration();
52 	report_prefix_pop();
53 	return report_summary();
54 }
55