xref: /linux/tools/testing/selftests/powerpc/ptrace/ptrace-vsx.c (revision a1c613ae4c322ddd58d5a8539dbfba2a0380a8c0)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20da535c0SAnshuman Khandual /*
30da535c0SAnshuman Khandual  * Ptrace test for VMX/VSX registers
40da535c0SAnshuman Khandual  *
50da535c0SAnshuman Khandual  * Copyright (C) 2015 Anshuman Khandual, IBM Corporation.
60da535c0SAnshuman Khandual  */
70da535c0SAnshuman Khandual #include "ptrace.h"
80da535c0SAnshuman Khandual #include "ptrace-vsx.h"
90da535c0SAnshuman Khandual 
100da535c0SAnshuman Khandual /* Tracer and Tracee Shared Data */
110da535c0SAnshuman Khandual int shm_id;
120da535c0SAnshuman Khandual int *cptr, *pptr;
130da535c0SAnshuman Khandual 
140da535c0SAnshuman Khandual unsigned long fp_load[VEC_MAX];
150da535c0SAnshuman Khandual unsigned long fp_load_new[VEC_MAX];
160da535c0SAnshuman Khandual unsigned long fp_store[VEC_MAX];
170da535c0SAnshuman Khandual 
vsx(void)180da535c0SAnshuman Khandual void vsx(void)
190da535c0SAnshuman Khandual {
200da535c0SAnshuman Khandual 	int ret;
210da535c0SAnshuman Khandual 
220da535c0SAnshuman Khandual 	cptr = (int *)shmat(shm_id, NULL, 0);
230da535c0SAnshuman Khandual 	loadvsx(fp_load, 0);
240da535c0SAnshuman Khandual 	cptr[1] = 1;
250da535c0SAnshuman Khandual 
260da535c0SAnshuman Khandual 	while (!cptr[0])
270da535c0SAnshuman Khandual 		asm volatile("" : : : "memory");
280da535c0SAnshuman Khandual 	shmdt((void *) cptr);
290da535c0SAnshuman Khandual 
300da535c0SAnshuman Khandual 	storevsx(fp_store, 0);
310da535c0SAnshuman Khandual 	ret = compare_vsx_vmx(fp_store, fp_load_new);
320da535c0SAnshuman Khandual 	if (ret)
330da535c0SAnshuman Khandual 		exit(1);
340da535c0SAnshuman Khandual 	exit(0);
350da535c0SAnshuman Khandual }
360da535c0SAnshuman Khandual 
trace_vsx(pid_t child)370da535c0SAnshuman Khandual int trace_vsx(pid_t child)
380da535c0SAnshuman Khandual {
390da535c0SAnshuman Khandual 	unsigned long vsx[VSX_MAX];
400da535c0SAnshuman Khandual 	unsigned long vmx[VMX_MAX + 2][2];
410da535c0SAnshuman Khandual 
420da535c0SAnshuman Khandual 	FAIL_IF(start_trace(child));
430da535c0SAnshuman Khandual 	FAIL_IF(show_vsx(child, vsx));
440da535c0SAnshuman Khandual 	FAIL_IF(validate_vsx(vsx, fp_load));
450da535c0SAnshuman Khandual 	FAIL_IF(show_vmx(child, vmx));
460da535c0SAnshuman Khandual 	FAIL_IF(validate_vmx(vmx, fp_load));
470da535c0SAnshuman Khandual 
480da535c0SAnshuman Khandual 	memset(vsx, 0, sizeof(vsx));
490da535c0SAnshuman Khandual 	memset(vmx, 0, sizeof(vmx));
500da535c0SAnshuman Khandual 	load_vsx_vmx(fp_load_new, vsx, vmx);
510da535c0SAnshuman Khandual 
520da535c0SAnshuman Khandual 	FAIL_IF(write_vsx(child, vsx));
530da535c0SAnshuman Khandual 	FAIL_IF(write_vmx(child, vmx));
540da535c0SAnshuman Khandual 	FAIL_IF(stop_trace(child));
550da535c0SAnshuman Khandual 
560da535c0SAnshuman Khandual 	return TEST_PASS;
570da535c0SAnshuman Khandual }
580da535c0SAnshuman Khandual 
ptrace_vsx(void)590da535c0SAnshuman Khandual int ptrace_vsx(void)
600da535c0SAnshuman Khandual {
610da535c0SAnshuman Khandual 	pid_t pid;
620da535c0SAnshuman Khandual 	int ret, status, i;
630da535c0SAnshuman Khandual 
64*68877ff2SBenjamin Gray 	SKIP_IF_MSG(!have_hwcap(PPC_FEATURE_HAS_VSX), "Don't have VSX");
65872d11bcSMichael Ellerman 
660da535c0SAnshuman Khandual 	shm_id = shmget(IPC_PRIVATE, sizeof(int) * 2, 0777|IPC_CREAT);
670da535c0SAnshuman Khandual 
680da535c0SAnshuman Khandual 	for (i = 0; i < VEC_MAX; i++)
690da535c0SAnshuman Khandual 		fp_load[i] = i + rand();
700da535c0SAnshuman Khandual 
710da535c0SAnshuman Khandual 	for (i = 0; i < VEC_MAX; i++)
720da535c0SAnshuman Khandual 		fp_load_new[i] = i + 2 * rand();
730da535c0SAnshuman Khandual 
740da535c0SAnshuman Khandual 	pid = fork();
750da535c0SAnshuman Khandual 	if (pid < 0) {
760da535c0SAnshuman Khandual 		perror("fork() failed");
770da535c0SAnshuman Khandual 		return TEST_FAIL;
780da535c0SAnshuman Khandual 	}
790da535c0SAnshuman Khandual 
800da535c0SAnshuman Khandual 	if (pid == 0)
810da535c0SAnshuman Khandual 		vsx();
820da535c0SAnshuman Khandual 
830da535c0SAnshuman Khandual 	if (pid) {
840da535c0SAnshuman Khandual 		pptr = (int *)shmat(shm_id, NULL, 0);
850da535c0SAnshuman Khandual 		while (!pptr[1])
860da535c0SAnshuman Khandual 			asm volatile("" : : : "memory");
870da535c0SAnshuman Khandual 
880da535c0SAnshuman Khandual 		ret = trace_vsx(pid);
890da535c0SAnshuman Khandual 		if (ret) {
900da535c0SAnshuman Khandual 			kill(pid, SIGTERM);
910da535c0SAnshuman Khandual 			shmdt((void *)pptr);
920da535c0SAnshuman Khandual 			shmctl(shm_id, IPC_RMID, NULL);
930da535c0SAnshuman Khandual 			return TEST_FAIL;
940da535c0SAnshuman Khandual 		}
950da535c0SAnshuman Khandual 
960da535c0SAnshuman Khandual 		pptr[0] = 1;
970da535c0SAnshuman Khandual 		shmdt((void *)pptr);
980da535c0SAnshuman Khandual 
990da535c0SAnshuman Khandual 		ret = wait(&status);
1000da535c0SAnshuman Khandual 		shmctl(shm_id, IPC_RMID, NULL);
1010da535c0SAnshuman Khandual 		if (ret != pid) {
1020da535c0SAnshuman Khandual 			printf("Child's exit status not captured\n");
1030da535c0SAnshuman Khandual 			return TEST_FAIL;
1040da535c0SAnshuman Khandual 		}
1050da535c0SAnshuman Khandual 
1060da535c0SAnshuman Khandual 		return (WIFEXITED(status) && WEXITSTATUS(status)) ? TEST_FAIL :
1070da535c0SAnshuman Khandual 			TEST_PASS;
1080da535c0SAnshuman Khandual 	}
1090da535c0SAnshuman Khandual 	return TEST_PASS;
1100da535c0SAnshuman Khandual }
1110da535c0SAnshuman Khandual 
main(int argc,char * argv[])1120da535c0SAnshuman Khandual int main(int argc, char *argv[])
1130da535c0SAnshuman Khandual {
1140da535c0SAnshuman Khandual 	return test_harness(ptrace_vsx, "ptrace_vsx");
1150da535c0SAnshuman Khandual }
116