xref: /openbmc/linux/tools/testing/selftests/powerpc/stringloops/memcmp.c (revision cbecf716ca618fd44feda6bd9a64a8179d031fc5)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2521adf53SAnton Blanchard #include <malloc.h>
3521adf53SAnton Blanchard #include <stdlib.h>
4521adf53SAnton Blanchard #include <string.h>
58ac9b9d6SMichael Ellerman #include <sys/mman.h>
6c827ac45SSimon Guo #include <time.h>
7*178282a0SMichael Ellerman 
815ec3997SSimon Guo #include "utils.h"
9521adf53SAnton Blanchard 
10521adf53SAnton Blanchard #define SIZE 256
11521adf53SAnton Blanchard #define ITERATIONS 10000
12521adf53SAnton Blanchard 
13c827ac45SSimon Guo #define LARGE_SIZE (5 * 1024)
14c827ac45SSimon Guo #define LARGE_ITERATIONS 1000
15c827ac45SSimon Guo #define LARGE_MAX_OFFSET 32
16c827ac45SSimon Guo #define LARGE_SIZE_START 4096
17c827ac45SSimon Guo 
188ac9b9d6SMichael Ellerman /* This is big enough to fit LARGE_SIZE and works on 4K & 64K kernels */
198ac9b9d6SMichael Ellerman #define MAP_SIZE (64 * 1024)
208ac9b9d6SMichael Ellerman 
21c827ac45SSimon Guo #define MAX_OFFSET_DIFF_S1_S2 48
22c827ac45SSimon Guo 
23c827ac45SSimon Guo int vmx_count;
enter_vmx_ops(void)24c827ac45SSimon Guo int enter_vmx_ops(void)
25c827ac45SSimon Guo {
26c827ac45SSimon Guo 	vmx_count++;
27c827ac45SSimon Guo 	return 1;
28c827ac45SSimon Guo }
29c827ac45SSimon Guo 
exit_vmx_ops(void)30c827ac45SSimon Guo void exit_vmx_ops(void)
31c827ac45SSimon Guo {
32c827ac45SSimon Guo 	vmx_count--;
33c827ac45SSimon Guo }
34521adf53SAnton Blanchard int test_memcmp(const void *s1, const void *s2, size_t n);
35521adf53SAnton Blanchard 
36521adf53SAnton Blanchard /* test all offsets and lengths */
test_one(char * s1,char * s2,unsigned long max_offset,unsigned long size_start,unsigned long max_size)37c827ac45SSimon Guo static void test_one(char *s1, char *s2, unsigned long max_offset,
38c827ac45SSimon Guo 		unsigned long size_start, unsigned long max_size)
39521adf53SAnton Blanchard {
40521adf53SAnton Blanchard 	unsigned long offset, size;
41521adf53SAnton Blanchard 
42c827ac45SSimon Guo 	for (offset = 0; offset < max_offset; offset++) {
43c827ac45SSimon Guo 		for (size = size_start; size < (max_size - offset); size++) {
44521adf53SAnton Blanchard 			int x, y;
45521adf53SAnton Blanchard 			unsigned long i;
46521adf53SAnton Blanchard 
47521adf53SAnton Blanchard 			y = memcmp(s1+offset, s2+offset, size);
48521adf53SAnton Blanchard 			x = test_memcmp(s1+offset, s2+offset, size);
49521adf53SAnton Blanchard 
50521adf53SAnton Blanchard 			if (((x ^ y) < 0) &&	/* Trick to compare sign */
51521adf53SAnton Blanchard 				((x | y) != 0)) { /* check for zero */
52521adf53SAnton Blanchard 				printf("memcmp returned %d, should have returned %d (offset %ld size %ld)\n", x, y, offset, size);
53521adf53SAnton Blanchard 
54521adf53SAnton Blanchard 				for (i = offset; i < offset+size; i++)
55521adf53SAnton Blanchard 					printf("%02x ", s1[i]);
56521adf53SAnton Blanchard 				printf("\n");
57521adf53SAnton Blanchard 
58521adf53SAnton Blanchard 				for (i = offset; i < offset+size; i++)
59521adf53SAnton Blanchard 					printf("%02x ", s2[i]);
60521adf53SAnton Blanchard 				printf("\n");
61521adf53SAnton Blanchard 				abort();
62521adf53SAnton Blanchard 			}
63c827ac45SSimon Guo 
64c827ac45SSimon Guo 			if (vmx_count != 0) {
65c827ac45SSimon Guo 				printf("vmx enter/exit not paired.(offset:%ld size:%ld s1:%p s2:%p vc:%d\n",
66c827ac45SSimon Guo 					offset, size, s1, s2, vmx_count);
67c827ac45SSimon Guo 				printf("\n");
68c827ac45SSimon Guo 				abort();
69c827ac45SSimon Guo 			}
70521adf53SAnton Blanchard 		}
71521adf53SAnton Blanchard 	}
72521adf53SAnton Blanchard }
73521adf53SAnton Blanchard 
testcase(bool islarge)74c827ac45SSimon Guo static int testcase(bool islarge)
75521adf53SAnton Blanchard {
768ac9b9d6SMichael Ellerman 	unsigned long i, comp_size, alloc_size;
778ac9b9d6SMichael Ellerman 	char *p, *s1, *s2;
788ac9b9d6SMichael Ellerman 	int iterations;
79521adf53SAnton Blanchard 
808ac9b9d6SMichael Ellerman 	comp_size = (islarge ? LARGE_SIZE : SIZE);
818ac9b9d6SMichael Ellerman 	alloc_size = comp_size + MAX_OFFSET_DIFF_S1_S2;
828ac9b9d6SMichael Ellerman 	iterations = islarge ? LARGE_ITERATIONS : ITERATIONS;
83c827ac45SSimon Guo 
848ac9b9d6SMichael Ellerman 	p = mmap(NULL, 4 * MAP_SIZE, PROT_READ | PROT_WRITE,
858ac9b9d6SMichael Ellerman 		 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
868ac9b9d6SMichael Ellerman 	FAIL_IF(p == MAP_FAILED);
87521adf53SAnton Blanchard 
888ac9b9d6SMichael Ellerman 	/* Put s1/s2 at the end of a page */
898ac9b9d6SMichael Ellerman 	s1 = p + MAP_SIZE - alloc_size;
908ac9b9d6SMichael Ellerman 	s2 = p + 3 * MAP_SIZE - alloc_size;
918ac9b9d6SMichael Ellerman 
928ac9b9d6SMichael Ellerman 	/* And unmap the subsequent page to force a fault if we overread */
938ac9b9d6SMichael Ellerman 	munmap(p + MAP_SIZE, MAP_SIZE);
948ac9b9d6SMichael Ellerman 	munmap(p + 3 * MAP_SIZE, MAP_SIZE);
95521adf53SAnton Blanchard 
96c827ac45SSimon Guo 	srandom(time(0));
97521adf53SAnton Blanchard 
98c827ac45SSimon Guo 	for (i = 0; i < iterations; i++) {
99521adf53SAnton Blanchard 		unsigned long j;
100521adf53SAnton Blanchard 		unsigned long change;
101c827ac45SSimon Guo 		char *rand_s1 = s1;
102c827ac45SSimon Guo 		char *rand_s2 = s2;
103521adf53SAnton Blanchard 
104c827ac45SSimon Guo 		for (j = 0; j < alloc_size; j++)
105521adf53SAnton Blanchard 			s1[j] = random();
106521adf53SAnton Blanchard 
107c827ac45SSimon Guo 		rand_s1 += random() % MAX_OFFSET_DIFF_S1_S2;
108c827ac45SSimon Guo 		rand_s2 += random() % MAX_OFFSET_DIFF_S1_S2;
109c827ac45SSimon Guo 		memcpy(rand_s2, rand_s1, comp_size);
110521adf53SAnton Blanchard 
111521adf53SAnton Blanchard 		/* change one byte */
112c827ac45SSimon Guo 		change = random() % comp_size;
113c827ac45SSimon Guo 		rand_s2[change] = random() & 0xff;
114521adf53SAnton Blanchard 
115c827ac45SSimon Guo 		if (islarge)
116c827ac45SSimon Guo 			test_one(rand_s1, rand_s2, LARGE_MAX_OFFSET,
117c827ac45SSimon Guo 					LARGE_SIZE_START, comp_size);
118c827ac45SSimon Guo 		else
119c827ac45SSimon Guo 			test_one(rand_s1, rand_s2, SIZE, 0, comp_size);
120521adf53SAnton Blanchard 	}
121521adf53SAnton Blanchard 
122c827ac45SSimon Guo 	srandom(time(0));
123521adf53SAnton Blanchard 
124c827ac45SSimon Guo 	for (i = 0; i < iterations; i++) {
125521adf53SAnton Blanchard 		unsigned long j;
126521adf53SAnton Blanchard 		unsigned long change;
127c827ac45SSimon Guo 		char *rand_s1 = s1;
128c827ac45SSimon Guo 		char *rand_s2 = s2;
129521adf53SAnton Blanchard 
130c827ac45SSimon Guo 		for (j = 0; j < alloc_size; j++)
131521adf53SAnton Blanchard 			s1[j] = random();
132521adf53SAnton Blanchard 
133c827ac45SSimon Guo 		rand_s1 += random() % MAX_OFFSET_DIFF_S1_S2;
134c827ac45SSimon Guo 		rand_s2 += random() % MAX_OFFSET_DIFF_S1_S2;
135c827ac45SSimon Guo 		memcpy(rand_s2, rand_s1, comp_size);
136521adf53SAnton Blanchard 
137521adf53SAnton Blanchard 		/* change multiple bytes, 1/8 of total */
138c827ac45SSimon Guo 		for (j = 0; j < comp_size / 8; j++) {
139c827ac45SSimon Guo 			change = random() % comp_size;
140521adf53SAnton Blanchard 			s2[change] = random() & 0xff;
141521adf53SAnton Blanchard 		}
142521adf53SAnton Blanchard 
143c827ac45SSimon Guo 		if (islarge)
144c827ac45SSimon Guo 			test_one(rand_s1, rand_s2, LARGE_MAX_OFFSET,
145c827ac45SSimon Guo 					LARGE_SIZE_START, comp_size);
146c827ac45SSimon Guo 		else
147c827ac45SSimon Guo 			test_one(rand_s1, rand_s2, SIZE, 0, comp_size);
148521adf53SAnton Blanchard 	}
149521adf53SAnton Blanchard 
150521adf53SAnton Blanchard 	return 0;
151521adf53SAnton Blanchard }
152521adf53SAnton Blanchard 
testcases(void)153c827ac45SSimon Guo static int testcases(void)
154c827ac45SSimon Guo {
155872d11bcSMichael Ellerman #ifdef __powerpc64__
156872d11bcSMichael Ellerman 	// vcmpequd used in memcmp_64.S is v2.07
157872d11bcSMichael Ellerman 	SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_2_07));
158872d11bcSMichael Ellerman #endif
159872d11bcSMichael Ellerman 
160c827ac45SSimon Guo 	testcase(0);
161c827ac45SSimon Guo 	testcase(1);
162c827ac45SSimon Guo 	return 0;
163c827ac45SSimon Guo }
164c827ac45SSimon Guo 
main(void)165521adf53SAnton Blanchard int main(void)
166521adf53SAnton Blanchard {
167d97e7f19SMichael Ellerman 	test_harness_set_timeout(300);
168c827ac45SSimon Guo 	return test_harness(testcases, "memcmp");
169521adf53SAnton Blanchard }
170