Subversion Repositories HelenOS

Rev

Rev 1705 | Rev 1787 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1072 palkovsky 1
/*
2
 * Copyright (C) 2006 Ondrej Palkovsky
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
1705 cejka 29
 /** @addtogroup amd64debug
1702 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
1072 palkovsky 35
#include <arch/debugger.h>
36
#include <console/kconsole.h>
37
#include <console/cmd.h>
38
#include <symtab.h>
39
#include <print.h>
40
#include <panic.h>
41
#include <interrupt.h>
42
#include <arch/asm.h>
43
#include <arch/cpu.h>
44
#include <debug.h>
45
#include <func.h>
1077 palkovsky 46
#include <smp/ipi.h>
1072 palkovsky 47
 
48
typedef struct  {
1780 jermar 49
	uintptr_t address;      /**< Breakpoint address */
1072 palkovsky 50
	int flags;              /**< Flags regarding breakpoint */
51
	int counter;            /**< How many times the exception occured */
52
} bpinfo_t;
53
 
54
static bpinfo_t breakpoints[BKPOINTS_MAX];
55
SPINLOCK_INITIALIZE(bkpoint_lock);
56
 
57
static int cmd_print_breakpoints(cmd_arg_t *argv);
58
static cmd_info_t bkpts_info = {
59
	.name = "bkpts",
60
	.description = "Print breakpoint table.",
61
	.func = cmd_print_breakpoints,
62
	.argc = 0,
63
};
64
 
1077 palkovsky 65
#ifndef CONFIG_DEBUG_AS_WATCHPOINT
66
 
1072 palkovsky 67
static int cmd_del_breakpoint(cmd_arg_t *argv);
68
static cmd_arg_t del_argv = {
69
	.type = ARG_TYPE_INT
70
};
71
static cmd_info_t delbkpt_info = {
72
	.name = "delbkpt",
73
	.description = "delbkpt <number> - Delete breakpoint.",
74
	.func = cmd_del_breakpoint,
75
	.argc = 1,
76
	.argv = &del_argv
77
};
78
 
79
static int cmd_add_breakpoint(cmd_arg_t *argv);
80
static cmd_arg_t add_argv = {
81
	.type = ARG_TYPE_INT
82
};
83
static cmd_info_t addbkpt_info = {
84
	.name = "addbkpt",
85
	.description = "addbkpt <&symbol> - new breakpoint.",
86
	.func = cmd_add_breakpoint,
87
	.argc = 1,
88
	.argv = &add_argv
89
};
90
 
91
static cmd_arg_t addw_argv = {
92
	.type = ARG_TYPE_INT
93
};
94
static cmd_info_t addwatchp_info = {
95
	.name = "addwatchp",
96
	.description = "addbwatchp <&symbol> - new write watchpoint.",
97
	.func = cmd_add_breakpoint,
98
	.argc = 1,
99
	.argv = &addw_argv
100
};
101
 
1077 palkovsky 102
#endif
1072 palkovsky 103
 
104
/** Print table of active breakpoints */
105
int cmd_print_breakpoints(cmd_arg_t *argv)
106
{
107
	int i;
108
	char *symbol;
109
 
110
	printf("Breakpoint table.\n");
111
	for (i=0; i < BKPOINTS_MAX; i++)
112
		if (breakpoints[i].address) {
113
			symbol = get_symtab_entry(breakpoints[i].address);
1196 cejka 114
			printf("%d. %p in %s\n",i,
1072 palkovsky 115
			       breakpoints[i].address, symbol);
116
			printf("     Count(%d) ", breakpoints[i].counter);
117
			printf("\n");
118
		}
119
	return 1;
120
}
121
 
1077 palkovsky 122
/* Setup DR register according to table */
123
static void setup_dr(int curidx)
124
{
1780 jermar 125
	unative_t dr7;
1077 palkovsky 126
	bpinfo_t *cur = &breakpoints[curidx];
127
	int flags = breakpoints[curidx].flags;
128
 
129
	/* Disable breakpoint in DR7 */
130
	dr7 = read_dr7();
131
	dr7 &= ~(0x2 << (curidx*2));
132
 
133
	if (cur->address) { /* Setup DR register */
134
		/* Set breakpoint to debug registers */
135
		switch (curidx) {
136
		case 0:
137
			write_dr0(cur->address);
138
			break;
139
		case 1:
140
			write_dr1(cur->address);
141
			break;
142
		case 2:
143
			write_dr2(cur->address);
144
			break;
145
		case 3:
146
			write_dr3(cur->address);
147
			break;
148
		}
149
		/* Set type to requested breakpoint & length*/
150
		dr7 &= ~ (0x3 << (16 + 4*curidx));
151
		dr7 &= ~ (0x3 << (18 + 4*curidx));
152
		if ((flags & BKPOINT_INSTR)) {
153
			;
154
		} else {
155
			if (sizeof(int) == 4)
1780 jermar 156
				dr7 |= ((unative_t) 0x3) << (18 + 4*curidx);
1077 palkovsky 157
			else /* 8 */
1780 jermar 158
				dr7 |= ((unative_t) 0x2) << (18 + 4*curidx);
1077 palkovsky 159
 
160
			if ((flags & BKPOINT_WRITE))
1780 jermar 161
				dr7 |= ((unative_t) 0x1) << (16 + 4*curidx);
1077 palkovsky 162
			else if ((flags & BKPOINT_READ_WRITE))
1780 jermar 163
				dr7 |= ((unative_t) 0x3) << (16 + 4*curidx);
1077 palkovsky 164
		}
165
 
166
		/* Enable global breakpoint */
167
		dr7 |= 0x2 << (curidx*2);
168
 
169
		write_dr7(dr7);
170
 
171
	} 
172
}
173
 
1072 palkovsky 174
/** Enable hardware breakpoint
175
 *
176
 *
177
 * @param where Address of HW breakpoint
178
 * @param flags Type of breakpoint (EXECUTE, WRITE)
179
 * @return Debug slot on success, -1 - no available HW breakpoint
180
 */
1077 palkovsky 181
int breakpoint_add(void * where, int flags, int curidx)
1072 palkovsky 182
{
183
	ipl_t ipl;
184
	int i;
1077 palkovsky 185
	bpinfo_t *cur;
1072 palkovsky 186
 
187
	ASSERT( flags & (BKPOINT_INSTR | BKPOINT_WRITE | BKPOINT_READ_WRITE));
188
 
189
	ipl = interrupts_disable();
190
	spinlock_lock(&bkpoint_lock);
191
 
1077 palkovsky 192
	if (curidx == -1) {
193
		/* Find free space in slots */
194
		for (i=0; i<BKPOINTS_MAX; i++)
195
			if (!breakpoints[i].address) {
196
				curidx = i;
197
				break;
198
			}
199
		if (curidx == -1) {
200
			/* Too many breakpoints */
201
			spinlock_unlock(&bkpoint_lock);
202
			interrupts_restore(ipl);
203
			return -1;
1072 palkovsky 204
		}
205
	}
1077 palkovsky 206
	cur = &breakpoints[curidx];
207
 
1780 jermar 208
	cur->address = (uintptr_t) where;
1072 palkovsky 209
	cur->flags = flags;
210
	cur->counter = 0;
211
 
1077 palkovsky 212
	setup_dr(curidx);
1072 palkovsky 213
 
214
	spinlock_unlock(&bkpoint_lock);
215
	interrupts_restore(ipl);
216
 
1077 palkovsky 217
	/* Send IPI */
218
#ifdef CONFIG_SMP
219
//	ipi_broadcast(VECTOR_DEBUG_IPI);	
220
#endif	
221
 
1072 palkovsky 222
	return curidx;
223
}
224
 
1074 palkovsky 225
#ifdef amd64
226
# define getip(x)  ((x)->rip)
227
#else
228
# define getip(x)  ((x)->eip)
229
#endif
230
 
1072 palkovsky 231
static void handle_exception(int slot, istate_t *istate)
232
{
233
	ASSERT(breakpoints[slot].address);
234
 
235
	/* Handle zero checker */
236
	if (! (breakpoints[slot].flags & BKPOINT_INSTR)) {
237
		if ((breakpoints[slot].flags & BKPOINT_CHECK_ZERO)) {
1780 jermar 238
			if (*((unative_t *) breakpoints[slot].address) != 0)
1072 palkovsky 239
				return;
1221 decky 240
			printf("**** Found ZERO on address %p ****\n",
1072 palkovsky 241
			       slot, breakpoints[slot].address);
242
		} else {
1221 decky 243
			printf("Data watchpoint - new data: %p\n",
1780 jermar 244
			       *((unative_t *) breakpoints[slot].address));
1072 palkovsky 245
		}
246
	}
1221 decky 247
	printf("Reached breakpoint %d:%p(%s)\n", slot, getip(istate),
1074 palkovsky 248
	       get_symtab_entry(getip(istate)));
1072 palkovsky 249
	printf("***Type 'exit' to exit kconsole.\n");
250
	atomic_set(&haltstate,1);
251
	kconsole("debug");
252
	atomic_set(&haltstate,0);
253
}
254
 
255
void breakpoint_del(int slot)
256
{
257
	bpinfo_t *cur;
258
	ipl_t ipl;
259
 
260
	ipl = interrupts_disable();
261
	spinlock_lock(&bkpoint_lock);
262
 
263
	cur = &breakpoints[slot];
264
	if (!cur->address) {
265
		spinlock_unlock(&bkpoint_lock);
266
		interrupts_restore(ipl);
267
		return;
268
	}
269
 
270
	cur->address = NULL;
271
 
1077 palkovsky 272
	setup_dr(slot);
1072 palkovsky 273
 
274
	spinlock_unlock(&bkpoint_lock);
275
	interrupts_restore(ipl);
1077 palkovsky 276
#ifdef CONFIG_SMP
277
//	ipi_broadcast(VECTOR_DEBUG_IPI);	
278
#endif
1072 palkovsky 279
}
280
 
1077 palkovsky 281
#ifndef CONFIG_DEBUG_AS_WATCHPOINT
282
 
1072 palkovsky 283
/** Remove breakpoint from table */
284
int cmd_del_breakpoint(cmd_arg_t *argv)
285
{
286
	if (argv->intval < 0 || argv->intval > BKPOINTS_MAX) {
287
		printf("Invalid breakpoint number.\n");
288
		return 0;
289
	}
290
	breakpoint_del(argv->intval);
291
	return 1;
292
}
293
 
294
/** Add new breakpoint to table */
295
static int cmd_add_breakpoint(cmd_arg_t *argv)
296
{
297
	int flags;
1077 palkovsky 298
	int id;
1072 palkovsky 299
 
300
	if (argv == &add_argv) {
301
		flags = BKPOINT_INSTR;
302
	} else { /* addwatchp */
303
		flags = BKPOINT_WRITE;
304
	}
305
	printf("Adding breakpoint on address: %p\n", argv->intval);
1077 palkovsky 306
	id = breakpoint_add((void *)argv->intval, flags, -1);
307
	if (id < 0)
1072 palkovsky 308
		printf("Add breakpoint failed.\n");
1077 palkovsky 309
	else
310
		printf("Added breakpoint %d.\n", id);
1072 palkovsky 311
 
312
	return 1;
313
}
1077 palkovsky 314
#endif
1072 palkovsky 315
 
1077 palkovsky 316
static void debug_exception(int n, istate_t *istate)
317
{
1780 jermar 318
	unative_t dr6;
1077 palkovsky 319
	int i;
320
 
321
	/* Set RF to restart the instruction  */
322
#ifdef amd64       
323
	istate->rflags |= RFLAGS_RF;
324
#else
325
	istate->eflags |= EFLAGS_RF;
326
#endif
327
 
328
	dr6 = read_dr6();
329
	for (i=0; i < BKPOINTS_MAX; i++) {
330
		if (dr6 & (1 << i)) {
331
			dr6 &= ~ (1 << i);
332
			write_dr6(dr6);
333
 
334
			handle_exception(i, istate);
335
		}
336
	}
337
}
338
 
339
#ifdef CONFIG_SMP
340
static void debug_ipi(int n, istate_t *istate)
341
{
342
	int i;
343
 
344
	spinlock_lock(&bkpoint_lock);
345
	for (i=0; i < BKPOINTS_MAX; i++)
346
		setup_dr(i);
347
	spinlock_unlock(&bkpoint_lock);
348
}
349
#endif
350
 
1072 palkovsky 351
/** Initialize debugger */
352
void debugger_init()
353
{
354
	int i;
355
 
356
	for (i=0; i<BKPOINTS_MAX; i++)
357
		breakpoints[i].address = NULL;
358
 
359
	cmd_initialize(&bkpts_info);
360
	if (!cmd_register(&bkpts_info))
361
		panic("could not register command %s\n", bkpts_info.name);
362
 
1077 palkovsky 363
#ifndef CONFIG_DEBUG_AS_WATCHPOINT
1072 palkovsky 364
	cmd_initialize(&delbkpt_info);
365
	if (!cmd_register(&delbkpt_info))
366
		panic("could not register command %s\n", delbkpt_info.name);
367
 
368
	cmd_initialize(&addbkpt_info);
369
	if (!cmd_register(&addbkpt_info))
370
		panic("could not register command %s\n", addbkpt_info.name);
371
 
372
	cmd_initialize(&addwatchp_info);
373
	if (!cmd_register(&addwatchp_info))
374
		panic("could not register command %s\n", addwatchp_info.name);
1077 palkovsky 375
#endif
1072 palkovsky 376
 
377
	exc_register(VECTOR_DEBUG, "debugger",
378
		     debug_exception);
1077 palkovsky 379
#ifdef CONFIG_SMP
380
	exc_register(VECTOR_DEBUG_IPI, "debugger_smp",
381
		     debug_ipi);
382
#endif
1072 palkovsky 383
}
1702 cejka 384
 
385
 /** @}
386
 */
387