xref: /linux/tools/testing/selftests/powerpc/ptrace/ptrace-vsx.c (revision ebf68996de0ab250c5d520eb2291ab65643e9a1e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Ptrace test for VMX/VSX registers
4  *
5  * Copyright (C) 2015 Anshuman Khandual, IBM Corporation.
6  */
7 #include "ptrace.h"
8 #include "ptrace-vsx.h"
9 
10 /* Tracer and Tracee Shared Data */
11 int shm_id;
12 int *cptr, *pptr;
13 
14 unsigned long fp_load[VEC_MAX];
15 unsigned long fp_load_new[VEC_MAX];
16 unsigned long fp_store[VEC_MAX];
17 
18 void vsx(void)
19 {
20 	int ret;
21 
22 	cptr = (int *)shmat(shm_id, NULL, 0);
23 	loadvsx(fp_load, 0);
24 	cptr[1] = 1;
25 
26 	while (!cptr[0])
27 		asm volatile("" : : : "memory");
28 	shmdt((void *) cptr);
29 
30 	storevsx(fp_store, 0);
31 	ret = compare_vsx_vmx(fp_store, fp_load_new);
32 	if (ret)
33 		exit(1);
34 	exit(0);
35 }
36 
37 int trace_vsx(pid_t child)
38 {
39 	unsigned long vsx[VSX_MAX];
40 	unsigned long vmx[VMX_MAX + 2][2];
41 
42 	FAIL_IF(start_trace(child));
43 	FAIL_IF(show_vsx(child, vsx));
44 	FAIL_IF(validate_vsx(vsx, fp_load));
45 	FAIL_IF(show_vmx(child, vmx));
46 	FAIL_IF(validate_vmx(vmx, fp_load));
47 
48 	memset(vsx, 0, sizeof(vsx));
49 	memset(vmx, 0, sizeof(vmx));
50 	load_vsx_vmx(fp_load_new, vsx, vmx);
51 
52 	FAIL_IF(write_vsx(child, vsx));
53 	FAIL_IF(write_vmx(child, vmx));
54 	FAIL_IF(stop_trace(child));
55 
56 	return TEST_PASS;
57 }
58 
59 int ptrace_vsx(void)
60 {
61 	pid_t pid;
62 	int ret, status, i;
63 
64 	shm_id = shmget(IPC_PRIVATE, sizeof(int) * 2, 0777|IPC_CREAT);
65 
66 	for (i = 0; i < VEC_MAX; i++)
67 		fp_load[i] = i + rand();
68 
69 	for (i = 0; i < VEC_MAX; i++)
70 		fp_load_new[i] = i + 2 * rand();
71 
72 	pid = fork();
73 	if (pid < 0) {
74 		perror("fork() failed");
75 		return TEST_FAIL;
76 	}
77 
78 	if (pid == 0)
79 		vsx();
80 
81 	if (pid) {
82 		pptr = (int *)shmat(shm_id, NULL, 0);
83 		while (!pptr[1])
84 			asm volatile("" : : : "memory");
85 
86 		ret = trace_vsx(pid);
87 		if (ret) {
88 			kill(pid, SIGTERM);
89 			shmdt((void *)pptr);
90 			shmctl(shm_id, IPC_RMID, NULL);
91 			return TEST_FAIL;
92 		}
93 
94 		pptr[0] = 1;
95 		shmdt((void *)pptr);
96 
97 		ret = wait(&status);
98 		shmctl(shm_id, IPC_RMID, NULL);
99 		if (ret != pid) {
100 			printf("Child's exit status not captured\n");
101 			return TEST_FAIL;
102 		}
103 
104 		return (WIFEXITED(status) && WEXITSTATUS(status)) ? TEST_FAIL :
105 			TEST_PASS;
106 	}
107 	return TEST_PASS;
108 }
109 
110 int main(int argc, char *argv[])
111 {
112 	return test_harness(ptrace_vsx, "ptrace_vsx");
113 }
114