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 <stdlib.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include "utils.h"
17 
18 #define CACHELINE_SIZE 128
19 
20 struct perf_event_read {
21 	__u64 nr;
22 	__u64 l1d_misses;
23 };
24 
25 static inline __u64 load(void *addr)
26 {
27 	__u64 tmp;
28 
29 	asm volatile("ld %0,0(%1)" : "=r"(tmp) : "b"(addr));
30 
31 	return tmp;
32 }
33 
34 static void syscall_loop(char *p, unsigned long iterations,
35 			 unsigned long zero_size)
36 {
37 	for (unsigned long i = 0; i < iterations; i++) {
38 		for (unsigned long j = 0; j < zero_size; j += CACHELINE_SIZE)
39 			load(p + j);
40 		getppid();
41 	}
42 }
43 
44 int rfi_flush_test(void)
45 {
46 	char *p;
47 	int repetitions = 10;
48 	int fd, passes = 0, iter, rc = 0;
49 	struct perf_event_read v;
50 	__u64 l1d_misses_total = 0;
51 	unsigned long iterations = 100000, zero_size = 24 * 1024;
52 	int rfi_flush_org, rfi_flush;
53 
54 	SKIP_IF(geteuid() != 0);
55 
56 	if (read_debugfs_file("powerpc/rfi_flush", &rfi_flush_org)) {
57 		perror("Unable to read powerpc/rfi_flush debugfs file");
58 		SKIP_IF(1);
59 	}
60 
61 	rfi_flush = rfi_flush_org;
62 
63 	fd = perf_event_open_counter(PERF_TYPE_RAW, /* L1d miss */ 0x400f0, -1);
64 	FAIL_IF(fd < 0);
65 
66 	p = (char *)memalign(zero_size, CACHELINE_SIZE);
67 
68 	FAIL_IF(perf_event_enable(fd));
69 
70 	set_dscr(1);
71 
72 	iter = repetitions;
73 
74 again:
75 	FAIL_IF(perf_event_reset(fd));
76 
77 	syscall_loop(p, iterations, zero_size);
78 
79 	FAIL_IF(read(fd, &v, sizeof(v)) != sizeof(v));
80 
81 	/* Expect at least zero_size/CACHELINE_SIZE misses per iteration */
82 	if (v.l1d_misses >= (iterations * zero_size / CACHELINE_SIZE) && rfi_flush)
83 		passes++;
84 	else if (v.l1d_misses < iterations && !rfi_flush)
85 		passes++;
86 
87 	l1d_misses_total += v.l1d_misses;
88 
89 	while (--iter)
90 		goto again;
91 
92 	if (passes < repetitions) {
93 		printf("FAIL (L1D misses with rfi_flush=%d: %llu %c %lu) [%d/%d failures]\n",
94 		       rfi_flush, l1d_misses_total, rfi_flush ? '<' : '>',
95 		       rfi_flush ? (repetitions * iterations * zero_size / CACHELINE_SIZE) : iterations,
96 		       repetitions - passes, repetitions);
97 		rc = 1;
98 	} else
99 		printf("PASS (L1D misses with rfi_flush=%d: %llu %c %lu) [%d/%d pass]\n",
100 		       rfi_flush, l1d_misses_total, rfi_flush ? '>' : '<',
101 		       rfi_flush ? (repetitions * iterations * zero_size / CACHELINE_SIZE) : iterations,
102 		       passes, repetitions);
103 
104 	if (rfi_flush == rfi_flush_org) {
105 		rfi_flush = !rfi_flush_org;
106 		if (write_debugfs_file("powerpc/rfi_flush", rfi_flush) < 0) {
107 			perror("error writing to powerpc/rfi_flush debugfs file");
108 			return 1;
109 		}
110 		iter = repetitions;
111 		l1d_misses_total = 0;
112 		passes = 0;
113 		goto again;
114 	}
115 
116 	perf_event_disable(fd);
117 	close(fd);
118 
119 	set_dscr(0);
120 
121 	if (write_debugfs_file("powerpc/rfi_flush", rfi_flush_org) < 0) {
122 		perror("unable to restore original value of powerpc/rfi_flush debugfs file");
123 		return 1;
124 	}
125 
126 	return rc;
127 }
128 
129 int main(int argc, char *argv[])
130 {
131 	return test_harness(rfi_flush_test, "rfi_flush_test");
132 }
133