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