aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/powerpc/ptrace/ptrace-hwbreak.c
blob: 3066d310f32b64486ff9b345063444de24844384 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// SPDX-License-Identifier: GPL-2.0+

/*
 * Ptrace test for hw breakpoints
 *
 * Based on tools/testing/selftests/breakpoints/breakpoint_test.c
 *
 * This test forks and the parent then traces the child doing various
 * types of ptrace enabled breakpoints
 *
 * Copyright (C) 2018 Michael Neuling, IBM Corporation.
 */

#include <sys/ptrace.h>
#include <unistd.h>
#include <stddef.h>
#include <sys/user.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "ptrace.h"

/* Breakpoint access modes */
enum {
	BP_X = 1,
	BP_RW = 2,
	BP_W = 4,
};

static pid_t child_pid;
static struct ppc_debug_info dbginfo;

static void get_dbginfo(void)
{
	int ret;

	ret = ptrace(PPC_PTRACE_GETHWDBGINFO, child_pid, NULL, &dbginfo);
	if (ret) {
		perror("Can't get breakpoint info\n");
		exit(-1);
	}
}

static bool hwbreak_present(void)
{
	return (dbginfo.num_data_bps != 0);
}

static bool dawr_present(void)
{
	return !!(dbginfo.features & PPC_DEBUG_FEATURE_DATA_BP_DAWR);
}

static void set_breakpoint_addr(void *addr)
{
	int ret;

	ret = ptrace(PTRACE_SET_DEBUGREG, child_pid, 0, addr);
	if (ret) {
		perror("Can't set breakpoint addr\n");
		exit(-1);
	}
}

static int set_hwbreakpoint_addr(void *addr, int range)
{
	int ret;

	struct ppc_hw_breakpoint info;

	info.version = 1;
	info.trigger_type = PPC_BREAKPOINT_TRIGGER_RW;
	info.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
	if (range > 0)
		info.addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
	info.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
	info.addr = (__u64)addr;
	info.addr2 = (__u64)addr + range;
	info.condition_value = 0;

	ret = ptrace(PPC_PTRACE_SETHWDEBUG, child_pid, 0, &info);
	if (ret < 0) {
		perror("Can't set breakpoint\n");
		exit(-1);
	}
	return ret;
}

static int del_hwbreakpoint_addr(int watchpoint_handle)
{
	int ret;

	ret = ptrace(PPC_PTRACE_DELHWDEBUG, child_pid, 0, watchpoint_handle);
	if (ret < 0) {
		perror("Can't delete hw breakpoint\n");
		exit(-1);
	}
	return ret;
}

#define DAWR_LENGTH_MAX 512

/* Dummy variables to test read/write accesses */
static unsigned long long
	dummy_array[DAWR_LENGTH_MAX / sizeof(unsigned long long)]
	__attribute__((aligned(512)));
static unsigned long long *dummy_var = dummy_array;

static void write_var(int len)
{
	long long *plval;
	char *pcval;
	short *psval;
	int *pival;

	switch (len) {
	case 1:
		pcval = (char *)dummy_var;
		*pcval = 0xff;
		break;
	case 2:
		psval = (short *)dummy_var;
		*psval = 0xffff;
		break;
	case 4:
		pival = (int *)dummy_var;
		*pival = 0xffffffff;
		break;
	case 8:
		plval = (long long *)dummy_var;
		*plval = 0xffffffffffffffffLL;
		break;
	}
}

static void read_var(int len)
{
	char cval __attribute__((unused));
	short sval __attribute__((unused));
	int ival __attribute__((unused));
	long long lval __attribute__((unused));

	switch (len) {
	case 1:
		cval = *(char *)dummy_var;
		break;
	case 2:
		sval = *(short *)dummy_var;
		break;
	case 4:
		ival = *(int *)dummy_var;
		break;
	case 8:
		lval = *(long long *)dummy_var;
		break;
	}
}

/*
 * Do the r/w accesses to trigger the breakpoints. And run
 * the usual traps.
 */
static void trigger_tests(void)
{
	int len, ret;

	ret = ptrace(PTRACE_TRACEME, 0, NULL, 0);
	if (ret) {
		perror("Can't be traced?\n");
		return;
	}

	/* Wake up father so that it sets up the first test */
	kill(getpid(), SIGUSR1);

	/* Test write watchpoints */
	for (len = 1; len <= sizeof(long); len <<= 1)
		write_var(len);

	/* Test read/write watchpoints (on read accesses) */
	for (len = 1; len <= sizeof(long); len <<= 1)
		read_var(len);

	/* Test when breakpoint is unset */

	/* Test write watchpoints */
	for (len = 1; len <= sizeof(long); len <<= 1)
		write_var(len);

	/* Test read/write watchpoints (on read accesses) */
	for (len = 1; len <= sizeof(long); len <<= 1)
		read_var(len);
}

static void check_success(const char *msg)
{
	const char *msg2;
	int status;

	/* Wait for the child to SIGTRAP */
	wait(&status);

	msg2 = "Failed";

	if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
		msg2 = "Child process hit the breakpoint";
	}

	printf("%s Result: [%s]\n", msg, msg2);
}

static void launch_watchpoints(char *buf, int mode, int len,
			       struct ppc_debug_info *dbginfo, bool dawr)
{
	const char *mode_str;
	unsigned long data = (unsigned long)(dummy_var);
	int wh, range;

	data &= ~0x7UL;

	if (mode == BP_W) {
		data |= (1UL << 1);
		mode_str = "write";
	} else {
		data |= (1UL << 0);
		data |= (1UL << 1);
		mode_str = "read";
	}

	/* Set DABR_TRANSLATION bit */
	data |= (1UL << 2);

	/* use PTRACE_SET_DEBUGREG breakpoints */
	set_breakpoint_addr((void *)data);
	ptrace(PTRACE_CONT, child_pid, NULL, 0);
	sprintf(buf, "Test %s watchpoint with len: %d ", mode_str, len);
	check_success(buf);
	/* Unregister hw brkpoint */
	set_breakpoint_addr(NULL);

	data = (data & ~7); /* remove dabr control bits */

	/* use PPC_PTRACE_SETHWDEBUG breakpoint */
	if (!(dbginfo->features & PPC_DEBUG_FEATURE_DATA_BP_RANGE))
		return; /* not supported */
	wh = set_hwbreakpoint_addr((void *)data, 0);
	ptrace(PTRACE_CONT, child_pid, NULL, 0);
	sprintf(buf, "Test %s watchpoint with len: %d ", mode_str, len);
	check_success(buf);
	/* Unregister hw brkpoint */
	del_hwbreakpoint_addr(wh);

	/* try a wider range */
	range = 8;
	if (dawr)
		range = 512 - ((int)data & (DAWR_LENGTH_MAX - 1));
	wh = set_hwbreakpoint_addr((void *)data, range);
	ptrace(PTRACE_CONT, child_pid, NULL, 0);
	sprintf(buf, "Test %s watchpoint with len: %d ", mode_str, len);
	check_success(buf);
	/* Unregister hw brkpoint */
	del_hwbreakpoint_addr(wh);
}

/* Set the breakpoints and check the child successfully trigger them */
static int launch_tests(bool dawr)
{
	char buf[1024];
	int len, i, status;

	struct ppc_debug_info dbginfo;

	i = ptrace(PPC_PTRACE_GETHWDBGINFO, child_pid, NULL, &dbginfo);
	if (i) {
		perror("Can't set breakpoint info\n");
		exit(-1);
	}
	if (!(dbginfo.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE))
		printf("WARNING: Kernel doesn't support PPC_PTRACE_SETHWDEBUG\n");

	/* Write watchpoint */
	for (len = 1; len <= sizeof(long); len <<= 1)
		launch_watchpoints(buf, BP_W, len, &dbginfo, dawr);

	/* Read-Write watchpoint */
	for (len = 1; len <= sizeof(long); len <<= 1)
		launch_watchpoints(buf, BP_RW, len, &dbginfo, dawr);

	ptrace(PTRACE_CONT, child_pid, NULL, 0);

	/*
	 * Now we have unregistered the breakpoint, access by child
	 * should not cause SIGTRAP.
	 */

	wait(&status);

	if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
		printf("FAIL: Child process hit the breakpoint, which is not expected\n");
		ptrace(PTRACE_CONT, child_pid, NULL, 0);
		return TEST_FAIL;
	}

	if (WIFEXITED(status))
		printf("Child exited normally\n");

	return TEST_PASS;
}

static int ptrace_hwbreak(void)
{
	pid_t pid;
	int ret;
	bool dawr;

	pid = fork();
	if (!pid) {
		trigger_tests();
		return 0;
	}

	wait(NULL);

	child_pid = pid;

	get_dbginfo();
	SKIP_IF(!hwbreak_present());
	dawr = dawr_present();

	ret = launch_tests(dawr);

	wait(NULL);

	return ret;
}

int main(int argc, char **argv, char **envp)
{
	return test_harness(ptrace_hwbreak, "ptrace-hwbreak");
}