xref: /kvm-unit-tests/powerpc/tm.c (revision f0ca153cc2d1e6a670b3571a8ac24c2ef7042594)
1 /*
2  * Transactional Memory Unit Tests
3  *
4  * Copyright 2016 Suraj Jitindar Singh, IBM.
5  *
6  * This work is licensed under the terms of the GNU LGPL, version 2.
7  */
8 #include <libcflat.h>
9 #include <asm/hcall.h>
10 #include <asm/processor.h>
11 #include <asm/handlers.h>
12 #include <asm/smp.h>
13 #include <asm/setup.h>
14 #include <devicetree.h>
15 
16 /* Check "ibm,pa-features" property of a CPU node for the TM flag */
17 static void cpu_has_tm(int fdtnode, u32 regval __unused, void *ptr)
18 {
19 	const struct fdt_property *prop;
20 	int plen;
21 
22 	prop = fdt_get_property(dt_fdt(), fdtnode, "ibm,pa-features", &plen);
23 	if (!prop)	/* No features means TM is also not available */
24 		return;
25 	/* Sanity check for the property layout (first two bytes are header) */
26 	assert(plen >= 8 && prop->data[1] == 0 && prop->data[0] <= plen - 2);
27 
28 	/*
29 	 * The "Transactional Memory Category Support" flags are at byte
30 	 * offset 22 and 23 of the attribute type 0, so when adding the
31 	 * two bytes for the header, we've got to look at offset 24 for
32 	 * the TM support bit.
33 	 */
34 	if (prop->data[0] >= 24 && (prop->data[24] & 0x80) != 0)
35 		*(int *)ptr += 1;
36 }
37 
38 /* Check whether all CPU nodes have the TM flag */
39 static bool all_cpus_have_tm(void)
40 {
41 	int ret;
42 	int available = 0;
43 
44 	ret = dt_for_each_cpu_node(cpu_has_tm, &available);
45 
46 	return ret == 0 && available == nr_cpus;
47 }
48 
49 static int h_cede(void)
50 {
51 	register uint64_t r3 asm("r3") = H_CEDE;
52 
53 	asm volatile ("sc 1" : "+r"(r3) :
54 			     : "r0", "r4", "r5", "r6", "r7", "r8", "r9",
55 			       "r10", "r11", "r12", "xer", "ctr", "cc");
56 
57 	return r3;
58 }
59 
60 /*
61  * Enable transactional memory
62  * Returns:	FALSE - Failure
63  *		TRUE - Success
64  */
65 static bool enable_tm(void)
66 {
67 	uint64_t msr = 0;
68 
69 	asm volatile ("mfmsr %[msr]" : [msr] "=r" (msr));
70 
71 	msr |= (((uint64_t) 1) << 32);
72 
73 	asm volatile ("mtmsrd %[msr]\n\t"
74 		      "mfmsr %[msr]" : [msr] "+r" (msr));
75 
76 	return !!(msr & (((uint64_t) 1) << 32));
77 }
78 
79 /*
80  * Test H_CEDE call while transactional memory transaction is suspended
81  *
82  * WARNING: This tests for a known vulnerability in which the host may go down.
83  * Probably best not to run this if your host going down is going to cause
84  * problems.
85  *
86  * If the test passes then your kernel probably has the necessary patch.
87  * If the test fails then the H_CEDE call was unsuccessful and the
88  * vulnerability wasn't tested.
89  * If the test hits the vulnerability then it will never complete or report and
90  * the qemu process will block indefinitely. RCU stalls will be detected on the
91  * cpu and any process scheduled on the lost cpu will also block indefinitely.
92  */
93 static void test_h_cede_tm(int argc, char **argv)
94 {
95 	int i;
96 
97 	if (argc > 2)
98 		report_abort("Unsupported argument: '%s'", argv[2]);
99 
100 	handle_exception(0x900, &dec_except_handler, NULL);
101 
102 	if (!start_all_cpus(halt, 0))
103 		report_abort("Failed to start secondary cpus");
104 
105 	if (!enable_tm())
106 		report_abort("Failed to enable tm");
107 
108 	/*
109 	 * Begin a transaction and guarantee we are in the suspend state
110 	 * before continuing
111 	 */
112 	asm volatile ("1: .long 0x7c00051d\n\t"	/* tbegin. */
113 		      "beq 2f\n\t"
114 		      ".long 0x7c0005dd\n\t"	/* tsuspend. */
115 		      "2: .long 0x7c00059c\n\t"	/* tcheck cr0 */
116 		      "bf 2,1b" : : : "cr0");
117 
118 	for (i = 0; i < 500; i++) {
119 		uint64_t rval = h_cede();
120 
121 		if (rval != H_SUCCESS)
122 			break;
123 		mdelay(5);
124 	}
125 
126 	report("H_CEDE TM", i == 500);
127 }
128 
129 struct {
130 	const char *name;
131 	void (*func)(int argc, char **argv);
132 } hctests[] = {
133 	{ "h_cede_tm", test_h_cede_tm },
134 	{ NULL, NULL }
135 };
136 
137 int main(int argc, char **argv)
138 {
139 	bool all, has_tm;
140 	int i;
141 
142 	report_prefix_push("tm");
143 
144 	has_tm = all_cpus_have_tm();
145 	report_xfail("TM available in 'ibm,pa-features' property",
146 		     !has_tm, has_tm);
147 	if (!has_tm)
148 		return report_summary();
149 
150 	all = argc == 1 || !strcmp(argv[1], "all");
151 
152 	for (i = 0; hctests[i].name != NULL; i++) {
153 		if (all || strcmp(argv[1], hctests[i].name) == 0) {
154 			report_prefix_push(hctests[i].name);
155 			hctests[i].func(argc, argv);
156 			report_prefix_pop();
157 		}
158 	}
159 
160 	return report_summary();
161 }
162