1 // SPDX-License-Identifier: GPL-2.0+
2 
3 /*
4  * Copyright 2018 IBM Corporation.
5  */
6 
7 #define __SANE_USERSPACE_TYPES__
8 
9 #include <sys/types.h>
10 #include <stdint.h>
11 #include <malloc.h>
12 #include <unistd.h>
13 #include <signal.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include "utils.h"
18 
19 #define CACHELINE_SIZE 128
20 
21 struct perf_event_read {
22 	__u64 nr;
23 	__u64 l1d_misses;
24 };
25 
26 static inline __u64 load(void *addr)
27 {
28 	__u64 tmp;
29 
30 	asm volatile("ld %0,0(%1)" : "=r"(tmp) : "b"(addr));
31 
32 	return tmp;
33 }
34 
35 static void syscall_loop(char *p, unsigned long iterations,
36 			 unsigned long zero_size)
37 {
38 	for (unsigned long i = 0; i < iterations; i++) {
39 		for (unsigned long j = 0; j < zero_size; j += CACHELINE_SIZE)
40 			load(p + j);
41 		getppid();
42 	}
43 }
44 
45 static void sigill_handler(int signr, siginfo_t *info, void *unused)
46 {
47 	static int warned = 0;
48 	ucontext_t *ctx = (ucontext_t *)unused;
49 	unsigned long *pc = &UCONTEXT_NIA(ctx);
50 
51 	/* mtspr 3,RS to check for move to DSCR below */
52 	if ((*((unsigned int *)*pc) & 0xfc1fffff) == 0x7c0303a6) {
53 		if (!warned++)
54 			printf("WARNING: Skipping over dscr setup. Consider running 'ppc64_cpu --dscr=1' manually.\n");
55 		*pc += 4;
56 	} else {
57 		printf("SIGILL at %p\n", pc);
58 		abort();
59 	}
60 }
61 
62 static void set_dscr(unsigned long val)
63 {
64 	static int init = 0;
65 	struct sigaction sa;
66 
67 	if (!init) {
68 		memset(&sa, 0, sizeof(sa));
69 		sa.sa_sigaction = sigill_handler;
70 		sa.sa_flags = SA_SIGINFO;
71 		if (sigaction(SIGILL, &sa, NULL))
72 			perror("sigill_handler");
73 		init = 1;
74 	}
75 
76 	asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR));
77 }
78 
79 int rfi_flush_test(void)
80 {
81 	char *p;
82 	int repetitions = 10;
83 	int fd, passes = 0, iter, rc = 0;
84 	struct perf_event_read v;
85 	__u64 l1d_misses_total = 0;
86 	unsigned long iterations = 100000, zero_size = 24 * 1024;
87 	unsigned long l1d_misses_expected;
88 	int rfi_flush_org, rfi_flush;
89 
90 	SKIP_IF(geteuid() != 0);
91 
92 	// The PMU event we use only works on Power7 or later
93 	SKIP_IF(!have_hwcap(PPC_FEATURE_ARCH_2_06));
94 
95 	if (read_debugfs_file("powerpc/rfi_flush", &rfi_flush_org)) {
96 		perror("Unable to read powerpc/rfi_flush debugfs file");
97 		SKIP_IF(1);
98 	}
99 
100 	rfi_flush = rfi_flush_org;
101 
102 	fd = perf_event_open_counter(PERF_TYPE_RAW, /* L1d miss */ 0x400f0, -1);
103 	FAIL_IF(fd < 0);
104 
105 	p = (char *)memalign(zero_size, CACHELINE_SIZE);
106 
107 	FAIL_IF(perf_event_enable(fd));
108 
109 	set_dscr(1);
110 
111 	iter = repetitions;
112 
113 	/*
114 	 * We expect to see l1d miss for each cacheline access when rfi_flush
115 	 * is set. Allow a small variation on this.
116 	 */
117 	l1d_misses_expected = iterations * (zero_size / CACHELINE_SIZE - 2);
118 
119 again:
120 	FAIL_IF(perf_event_reset(fd));
121 
122 	syscall_loop(p, iterations, zero_size);
123 
124 	FAIL_IF(read(fd, &v, sizeof(v)) != sizeof(v));
125 
126 	if (rfi_flush && v.l1d_misses >= l1d_misses_expected)
127 		passes++;
128 	else if (!rfi_flush && v.l1d_misses < (l1d_misses_expected / 2))
129 		passes++;
130 
131 	l1d_misses_total += v.l1d_misses;
132 
133 	while (--iter)
134 		goto again;
135 
136 	if (passes < repetitions) {
137 		printf("FAIL (L1D misses with rfi_flush=%d: %llu %c %lu) [%d/%d failures]\n",
138 		       rfi_flush, l1d_misses_total, rfi_flush ? '<' : '>',
139 		       rfi_flush ? repetitions * l1d_misses_expected :
140 		       repetitions * l1d_misses_expected / 2,
141 		       repetitions - passes, repetitions);
142 		rc = 1;
143 	} else
144 		printf("PASS (L1D misses with rfi_flush=%d: %llu %c %lu) [%d/%d pass]\n",
145 		       rfi_flush, l1d_misses_total, rfi_flush ? '>' : '<',
146 		       rfi_flush ? repetitions * l1d_misses_expected :
147 		       repetitions * l1d_misses_expected / 2,
148 		       passes, repetitions);
149 
150 	if (rfi_flush == rfi_flush_org) {
151 		rfi_flush = !rfi_flush_org;
152 		if (write_debugfs_file("powerpc/rfi_flush", rfi_flush) < 0) {
153 			perror("error writing to powerpc/rfi_flush debugfs file");
154 			return 1;
155 		}
156 		iter = repetitions;
157 		l1d_misses_total = 0;
158 		passes = 0;
159 		goto again;
160 	}
161 
162 	perf_event_disable(fd);
163 	close(fd);
164 
165 	set_dscr(0);
166 
167 	if (write_debugfs_file("powerpc/rfi_flush", rfi_flush_org) < 0) {
168 		perror("unable to restore original value of powerpc/rfi_flush debugfs file");
169 		return 1;
170 	}
171 
172 	return rc;
173 }
174 
175 int main(int argc, char *argv[])
176 {
177 	return test_harness(rfi_flush_test, "rfi_flush_test");
178 }
179