xref: /linux/tools/testing/selftests/powerpc/ptrace/ptrace-tm-spd-gpr.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 /*
2  * Ptrace test for GPR/FPR registers in TM Suspend context
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-gpr.h"
13 #include "tm.h"
14 
15 /* Tracer and Tracee Shared Data */
16 int shm_id;
17 int *cptr, *pptr;
18 
19 float a = FPR_1;
20 float b = FPR_2;
21 float c = FPR_3;
22 float d = FPR_4;
23 
24 __attribute__((used)) void wait_parent(void)
25 {
26 	cptr[2] = 1;
27 	while (!cptr[1])
28 		asm volatile("" : : : "memory");
29 }
30 
31 void tm_spd_gpr(void)
32 {
33 	unsigned long gpr_buf[18];
34 	unsigned long result, texasr;
35 	float fpr_buf[32];
36 
37 	cptr = (int *)shmat(shm_id, NULL, 0);
38 
39 trans:
40 	cptr[2] = 0;
41 	asm __volatile__(
42 		ASM_LOAD_GPR_IMMED(gpr_1)
43 		ASM_LOAD_FPR_SINGLE_PRECISION(flt_1)
44 
45 		"1: ;"
46 		"tbegin.;"
47 		"beq 2f;"
48 
49 		ASM_LOAD_GPR_IMMED(gpr_2)
50 		"tsuspend.;"
51 		ASM_LOAD_GPR_IMMED(gpr_4)
52 		ASM_LOAD_FPR_SINGLE_PRECISION(flt_4)
53 
54 		"bl wait_parent;"
55 		"tresume.;"
56 		"tend.;"
57 		"li 0, 0;"
58 		"ori %[res], 0, 0;"
59 		"b 3f;"
60 
61 		/* Transaction abort handler */
62 		"2: ;"
63 		"li 0, 1;"
64 		"ori %[res], 0, 0;"
65 		"mfspr %[texasr], %[sprn_texasr];"
66 
67 		"3: ;"
68 		: [res] "=r" (result), [texasr] "=r" (texasr)
69 		: [gpr_1]"i"(GPR_1), [gpr_2]"i"(GPR_2), [gpr_4]"i"(GPR_4),
70 		[sprn_texasr] "i" (SPRN_TEXASR), [flt_1] "r" (&a),
71 		[flt_2] "r" (&b), [flt_4] "r" (&d)
72 		: "memory", "r5", "r6", "r7",
73 		"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
74 		"r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
75 		"r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
76 		);
77 
78 	if (result) {
79 		if (!cptr[0])
80 			goto trans;
81 
82 		shmdt((void *)cptr);
83 		store_gpr(gpr_buf);
84 		store_fpr_single_precision(fpr_buf);
85 
86 		if (validate_gpr(gpr_buf, GPR_3))
87 			exit(1);
88 
89 		if (validate_fpr_float(fpr_buf, c))
90 			exit(1);
91 		exit(0);
92 	}
93 	shmdt((void *)cptr);
94 	exit(1);
95 }
96 
97 int trace_tm_spd_gpr(pid_t child)
98 {
99 	unsigned long gpr[18];
100 	unsigned long fpr[32];
101 
102 	FAIL_IF(start_trace(child));
103 	FAIL_IF(show_gpr(child, gpr));
104 	FAIL_IF(validate_gpr(gpr, GPR_4));
105 	FAIL_IF(show_fpr(child, fpr));
106 	FAIL_IF(validate_fpr(fpr, FPR_4_REP));
107 	FAIL_IF(show_ckpt_fpr(child, fpr));
108 	FAIL_IF(validate_fpr(fpr, FPR_1_REP));
109 	FAIL_IF(show_ckpt_gpr(child, gpr));
110 	FAIL_IF(validate_gpr(gpr, GPR_1));
111 	FAIL_IF(write_ckpt_gpr(child, GPR_3));
112 	FAIL_IF(write_ckpt_fpr(child, FPR_3_REP));
113 
114 	pptr[0] = 1;
115 	pptr[1] = 1;
116 	FAIL_IF(stop_trace(child));
117 	return TEST_PASS;
118 }
119 
120 int ptrace_tm_spd_gpr(void)
121 {
122 	pid_t pid;
123 	int ret, status;
124 
125 	SKIP_IF(!have_htm());
126 	shm_id = shmget(IPC_PRIVATE, sizeof(int) * 3, 0777|IPC_CREAT);
127 	pid = fork();
128 	if (pid < 0) {
129 		perror("fork() failed");
130 		return TEST_FAIL;
131 	}
132 
133 	if (pid == 0)
134 		tm_spd_gpr();
135 
136 	if (pid) {
137 		pptr = (int *)shmat(shm_id, NULL, 0);
138 		pptr[0] = 0;
139 		pptr[1] = 0;
140 
141 		while (!pptr[2])
142 			asm volatile("" : : : "memory");
143 		ret = trace_tm_spd_gpr(pid);
144 		if (ret) {
145 			kill(pid, SIGTERM);
146 			shmdt((void *)pptr);
147 			shmctl(shm_id, IPC_RMID, NULL);
148 			return TEST_FAIL;
149 		}
150 
151 		shmdt((void *)pptr);
152 
153 		ret = wait(&status);
154 		shmctl(shm_id, IPC_RMID, NULL);
155 		if (ret != pid) {
156 			printf("Child's exit status not captured\n");
157 			return TEST_FAIL;
158 		}
159 
160 		return (WIFEXITED(status) && WEXITSTATUS(status)) ? TEST_FAIL :
161 			TEST_PASS;
162 	}
163 	return TEST_PASS;
164 }
165 
166 int main(int argc, char *argv[])
167 {
168 	return test_harness(ptrace_tm_spd_gpr, "ptrace_tm_spd_gpr");
169 }
170