xref: /linux/arch/x86/kernel/kgdb.c (revision 2e18e047981ae04be9bd0d9760057f7c1a7b3785)
1 /*
2  * This program is free software; you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by the
4  * Free Software Foundation; either version 2, or (at your option) any
5  * later version.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  * General Public License for more details.
11  *
12  */
13 
14 /*
15  * Copyright (C) 2004 Amit S. Kale <amitkale@linsyssoft.com>
16  * Copyright (C) 2000-2001 VERITAS Software Corporation.
17  * Copyright (C) 2002 Andi Kleen, SuSE Labs
18  * Copyright (C) 2004 LinSysSoft Technologies Pvt. Ltd.
19  * Copyright (C) 2007 MontaVista Software, Inc.
20  * Copyright (C) 2007-2008 Jason Wessel, Wind River Systems, Inc.
21  */
22 /****************************************************************************
23  *  Contributor:     Lake Stevens Instrument Division$
24  *  Written by:      Glenn Engel $
25  *  Updated by:	     Amit Kale<akale@veritas.com>
26  *  Updated by:	     Tom Rini <trini@kernel.crashing.org>
27  *  Updated by:	     Jason Wessel <jason.wessel@windriver.com>
28  *  Modified for 386 by Jim Kingdon, Cygnus Support.
29  *  Origianl kgdb, compatibility with 2.1.xx kernel by
30  *  David Grothe <dave@gcom.com>
31  *  Integrated into 2.2.5 kernel by Tigran Aivazian <tigran@sco.com>
32  *  X86_64 changes from Andi Kleen's patch merged by Jim Houston
33  */
34 #include <linux/spinlock.h>
35 #include <linux/kdebug.h>
36 #include <linux/string.h>
37 #include <linux/kernel.h>
38 #include <linux/ptrace.h>
39 #include <linux/sched.h>
40 #include <linux/delay.h>
41 #include <linux/kgdb.h>
42 #include <linux/init.h>
43 #include <linux/smp.h>
44 #include <linux/nmi.h>
45 #include <linux/hw_breakpoint.h>
46 
47 #include <asm/debugreg.h>
48 #include <asm/apicdef.h>
49 #include <asm/system.h>
50 
51 #include <asm/apic.h>
52 
53 /*
54  * Put the error code here just in case the user cares:
55  */
56 static int gdb_x86errcode;
57 
58 /*
59  * Likewise, the vector number here (since GDB only gets the signal
60  * number through the usual means, and that's not very specific):
61  */
62 static int gdb_x86vector = -1;
63 
64 /**
65  *	pt_regs_to_gdb_regs - Convert ptrace regs to GDB regs
66  *	@gdb_regs: A pointer to hold the registers in the order GDB wants.
67  *	@regs: The &struct pt_regs of the current process.
68  *
69  *	Convert the pt_regs in @regs into the format for registers that
70  *	GDB expects, stored in @gdb_regs.
71  */
72 void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
73 {
74 #ifndef CONFIG_X86_32
75 	u32 *gdb_regs32 = (u32 *)gdb_regs;
76 #endif
77 	gdb_regs[GDB_AX]	= regs->ax;
78 	gdb_regs[GDB_BX]	= regs->bx;
79 	gdb_regs[GDB_CX]	= regs->cx;
80 	gdb_regs[GDB_DX]	= regs->dx;
81 	gdb_regs[GDB_SI]	= regs->si;
82 	gdb_regs[GDB_DI]	= regs->di;
83 	gdb_regs[GDB_BP]	= regs->bp;
84 	gdb_regs[GDB_PC]	= regs->ip;
85 #ifdef CONFIG_X86_32
86 	gdb_regs[GDB_PS]	= regs->flags;
87 	gdb_regs[GDB_DS]	= regs->ds;
88 	gdb_regs[GDB_ES]	= regs->es;
89 	gdb_regs[GDB_CS]	= regs->cs;
90 	gdb_regs[GDB_FS]	= 0xFFFF;
91 	gdb_regs[GDB_GS]	= 0xFFFF;
92 	if (user_mode_vm(regs)) {
93 		gdb_regs[GDB_SS] = regs->ss;
94 		gdb_regs[GDB_SP] = regs->sp;
95 	} else {
96 		gdb_regs[GDB_SS] = __KERNEL_DS;
97 		gdb_regs[GDB_SP] = kernel_stack_pointer(regs);
98 	}
99 #else
100 	gdb_regs[GDB_R8]	= regs->r8;
101 	gdb_regs[GDB_R9]	= regs->r9;
102 	gdb_regs[GDB_R10]	= regs->r10;
103 	gdb_regs[GDB_R11]	= regs->r11;
104 	gdb_regs[GDB_R12]	= regs->r12;
105 	gdb_regs[GDB_R13]	= regs->r13;
106 	gdb_regs[GDB_R14]	= regs->r14;
107 	gdb_regs[GDB_R15]	= regs->r15;
108 	gdb_regs32[GDB_PS]	= regs->flags;
109 	gdb_regs32[GDB_CS]	= regs->cs;
110 	gdb_regs32[GDB_SS]	= regs->ss;
111 	gdb_regs[GDB_SP]	= kernel_stack_pointer(regs);
112 #endif
113 }
114 
115 /**
116  *	sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
117  *	@gdb_regs: A pointer to hold the registers in the order GDB wants.
118  *	@p: The &struct task_struct of the desired process.
119  *
120  *	Convert the register values of the sleeping process in @p to
121  *	the format that GDB expects.
122  *	This function is called when kgdb does not have access to the
123  *	&struct pt_regs and therefore it should fill the gdb registers
124  *	@gdb_regs with what has	been saved in &struct thread_struct
125  *	thread field during switch_to.
126  */
127 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
128 {
129 #ifndef CONFIG_X86_32
130 	u32 *gdb_regs32 = (u32 *)gdb_regs;
131 #endif
132 	gdb_regs[GDB_AX]	= 0;
133 	gdb_regs[GDB_BX]	= 0;
134 	gdb_regs[GDB_CX]	= 0;
135 	gdb_regs[GDB_DX]	= 0;
136 	gdb_regs[GDB_SI]	= 0;
137 	gdb_regs[GDB_DI]	= 0;
138 	gdb_regs[GDB_BP]	= *(unsigned long *)p->thread.sp;
139 #ifdef CONFIG_X86_32
140 	gdb_regs[GDB_DS]	= __KERNEL_DS;
141 	gdb_regs[GDB_ES]	= __KERNEL_DS;
142 	gdb_regs[GDB_PS]	= 0;
143 	gdb_regs[GDB_CS]	= __KERNEL_CS;
144 	gdb_regs[GDB_PC]	= p->thread.ip;
145 	gdb_regs[GDB_SS]	= __KERNEL_DS;
146 	gdb_regs[GDB_FS]	= 0xFFFF;
147 	gdb_regs[GDB_GS]	= 0xFFFF;
148 #else
149 	gdb_regs32[GDB_PS]	= *(unsigned long *)(p->thread.sp + 8);
150 	gdb_regs32[GDB_CS]	= __KERNEL_CS;
151 	gdb_regs32[GDB_SS]	= __KERNEL_DS;
152 	gdb_regs[GDB_PC]	= 0;
153 	gdb_regs[GDB_R8]	= 0;
154 	gdb_regs[GDB_R9]	= 0;
155 	gdb_regs[GDB_R10]	= 0;
156 	gdb_regs[GDB_R11]	= 0;
157 	gdb_regs[GDB_R12]	= 0;
158 	gdb_regs[GDB_R13]	= 0;
159 	gdb_regs[GDB_R14]	= 0;
160 	gdb_regs[GDB_R15]	= 0;
161 #endif
162 	gdb_regs[GDB_SP]	= p->thread.sp;
163 }
164 
165 /**
166  *	gdb_regs_to_pt_regs - Convert GDB regs to ptrace regs.
167  *	@gdb_regs: A pointer to hold the registers we've received from GDB.
168  *	@regs: A pointer to a &struct pt_regs to hold these values in.
169  *
170  *	Convert the GDB regs in @gdb_regs into the pt_regs, and store them
171  *	in @regs.
172  */
173 void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
174 {
175 #ifndef CONFIG_X86_32
176 	u32 *gdb_regs32 = (u32 *)gdb_regs;
177 #endif
178 	regs->ax		= gdb_regs[GDB_AX];
179 	regs->bx		= gdb_regs[GDB_BX];
180 	regs->cx		= gdb_regs[GDB_CX];
181 	regs->dx		= gdb_regs[GDB_DX];
182 	regs->si		= gdb_regs[GDB_SI];
183 	regs->di		= gdb_regs[GDB_DI];
184 	regs->bp		= gdb_regs[GDB_BP];
185 	regs->ip		= gdb_regs[GDB_PC];
186 #ifdef CONFIG_X86_32
187 	regs->flags		= gdb_regs[GDB_PS];
188 	regs->ds		= gdb_regs[GDB_DS];
189 	regs->es		= gdb_regs[GDB_ES];
190 	regs->cs		= gdb_regs[GDB_CS];
191 #else
192 	regs->r8		= gdb_regs[GDB_R8];
193 	regs->r9		= gdb_regs[GDB_R9];
194 	regs->r10		= gdb_regs[GDB_R10];
195 	regs->r11		= gdb_regs[GDB_R11];
196 	regs->r12		= gdb_regs[GDB_R12];
197 	regs->r13		= gdb_regs[GDB_R13];
198 	regs->r14		= gdb_regs[GDB_R14];
199 	regs->r15		= gdb_regs[GDB_R15];
200 	regs->flags		= gdb_regs32[GDB_PS];
201 	regs->cs		= gdb_regs32[GDB_CS];
202 	regs->ss		= gdb_regs32[GDB_SS];
203 #endif
204 }
205 
206 static struct hw_breakpoint {
207 	unsigned		enabled;
208 	unsigned long		addr;
209 	int			len;
210 	int			type;
211 	struct perf_event	**pev;
212 } breakinfo[4];
213 
214 static void kgdb_correct_hw_break(void)
215 {
216 	int breakno;
217 
218 	for (breakno = 0; breakno < 4; breakno++) {
219 		struct perf_event *bp;
220 		struct arch_hw_breakpoint *info;
221 		int val;
222 		int cpu = raw_smp_processor_id();
223 		if (!breakinfo[breakno].enabled)
224 			continue;
225 		bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu);
226 		info = counter_arch_bp(bp);
227 		if (bp->attr.disabled != 1)
228 			continue;
229 		bp->attr.bp_addr = breakinfo[breakno].addr;
230 		bp->attr.bp_len = breakinfo[breakno].len;
231 		bp->attr.bp_type = breakinfo[breakno].type;
232 		info->address = breakinfo[breakno].addr;
233 		info->len = breakinfo[breakno].len;
234 		info->type = breakinfo[breakno].type;
235 		val = arch_install_hw_breakpoint(bp);
236 		if (!val)
237 			bp->attr.disabled = 0;
238 	}
239 	hw_breakpoint_restore();
240 }
241 
242 static int hw_break_reserve_slot(int breakno)
243 {
244 	int cpu;
245 	int cnt = 0;
246 	struct perf_event **pevent;
247 
248 	for_each_online_cpu(cpu) {
249 		cnt++;
250 		pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
251 		if (dbg_reserve_bp_slot(*pevent))
252 			goto fail;
253 	}
254 
255 	return 0;
256 
257 fail:
258 	for_each_online_cpu(cpu) {
259 		cnt--;
260 		if (!cnt)
261 			break;
262 		pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
263 		dbg_release_bp_slot(*pevent);
264 	}
265 	return -1;
266 }
267 
268 static int hw_break_release_slot(int breakno)
269 {
270 	struct perf_event **pevent;
271 	int cpu;
272 
273 	for_each_online_cpu(cpu) {
274 		pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
275 		if (dbg_release_bp_slot(*pevent))
276 			/*
277 			 * The debugger is responisble for handing the retry on
278 			 * remove failure.
279 			 */
280 			return -1;
281 	}
282 	return 0;
283 }
284 
285 static int
286 kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
287 {
288 	int i;
289 
290 	for (i = 0; i < 4; i++)
291 		if (breakinfo[i].addr == addr && breakinfo[i].enabled)
292 			break;
293 	if (i == 4)
294 		return -1;
295 
296 	if (hw_break_release_slot(i)) {
297 		printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr);
298 		return -1;
299 	}
300 	breakinfo[i].enabled = 0;
301 
302 	return 0;
303 }
304 
305 static void kgdb_remove_all_hw_break(void)
306 {
307 	int i;
308 	int cpu = raw_smp_processor_id();
309 	struct perf_event *bp;
310 
311 	for (i = 0; i < 4; i++) {
312 		if (!breakinfo[i].enabled)
313 			continue;
314 		bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
315 		if (bp->attr.disabled == 1)
316 			continue;
317 		arch_uninstall_hw_breakpoint(bp);
318 		bp->attr.disabled = 1;
319 	}
320 }
321 
322 static int
323 kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
324 {
325 	int i;
326 
327 	for (i = 0; i < 4; i++)
328 		if (!breakinfo[i].enabled)
329 			break;
330 	if (i == 4)
331 		return -1;
332 
333 	switch (bptype) {
334 	case BP_HARDWARE_BREAKPOINT:
335 		len = 1;
336 		breakinfo[i].type = X86_BREAKPOINT_EXECUTE;
337 		break;
338 	case BP_WRITE_WATCHPOINT:
339 		breakinfo[i].type = X86_BREAKPOINT_WRITE;
340 		break;
341 	case BP_ACCESS_WATCHPOINT:
342 		breakinfo[i].type = X86_BREAKPOINT_RW;
343 		break;
344 	default:
345 		return -1;
346 	}
347 	switch (len) {
348 	case 1:
349 		breakinfo[i].len = X86_BREAKPOINT_LEN_1;
350 		break;
351 	case 2:
352 		breakinfo[i].len = X86_BREAKPOINT_LEN_2;
353 		break;
354 	case 4:
355 		breakinfo[i].len = X86_BREAKPOINT_LEN_4;
356 		break;
357 #ifdef CONFIG_X86_64
358 	case 8:
359 		breakinfo[i].len = X86_BREAKPOINT_LEN_8;
360 		break;
361 #endif
362 	default:
363 		return -1;
364 	}
365 	breakinfo[i].addr = addr;
366 	if (hw_break_reserve_slot(i)) {
367 		breakinfo[i].addr = 0;
368 		return -1;
369 	}
370 	breakinfo[i].enabled = 1;
371 
372 	return 0;
373 }
374 
375 /**
376  *	kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
377  *	@regs: Current &struct pt_regs.
378  *
379  *	This function will be called if the particular architecture must
380  *	disable hardware debugging while it is processing gdb packets or
381  *	handling exception.
382  */
383 void kgdb_disable_hw_debug(struct pt_regs *regs)
384 {
385 	int i;
386 	int cpu = raw_smp_processor_id();
387 	struct perf_event *bp;
388 
389 	/* Disable hardware debugging while we are in kgdb: */
390 	set_debugreg(0UL, 7);
391 	for (i = 0; i < 4; i++) {
392 		if (!breakinfo[i].enabled)
393 			continue;
394 		bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
395 		if (bp->attr.disabled == 1)
396 			continue;
397 		arch_uninstall_hw_breakpoint(bp);
398 		bp->attr.disabled = 1;
399 	}
400 }
401 
402 /**
403  *	kgdb_post_primary_code - Save error vector/code numbers.
404  *	@regs: Original pt_regs.
405  *	@e_vector: Original error vector.
406  *	@err_code: Original error code.
407  *
408  *	This is needed on architectures which support SMP and KGDB.
409  *	This function is called after all the slave cpus have been put
410  *	to a know spin state and the primary CPU has control over KGDB.
411  */
412 void kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
413 {
414 	/* primary processor is completely in the debugger */
415 	gdb_x86vector = e_vector;
416 	gdb_x86errcode = err_code;
417 }
418 
419 #ifdef CONFIG_SMP
420 /**
421  *	kgdb_roundup_cpus - Get other CPUs into a holding pattern
422  *	@flags: Current IRQ state
423  *
424  *	On SMP systems, we need to get the attention of the other CPUs
425  *	and get them be in a known state.  This should do what is needed
426  *	to get the other CPUs to call kgdb_wait(). Note that on some arches,
427  *	the NMI approach is not used for rounding up all the CPUs. For example,
428  *	in case of MIPS, smp_call_function() is used to roundup CPUs. In
429  *	this case, we have to make sure that interrupts are enabled before
430  *	calling smp_call_function(). The argument to this function is
431  *	the flags that will be used when restoring the interrupts. There is
432  *	local_irq_save() call before kgdb_roundup_cpus().
433  *
434  *	On non-SMP systems, this is not called.
435  */
436 void kgdb_roundup_cpus(unsigned long flags)
437 {
438 	apic->send_IPI_allbutself(APIC_DM_NMI);
439 }
440 #endif
441 
442 /**
443  *	kgdb_arch_handle_exception - Handle architecture specific GDB packets.
444  *	@vector: The error vector of the exception that happened.
445  *	@signo: The signal number of the exception that happened.
446  *	@err_code: The error code of the exception that happened.
447  *	@remcom_in_buffer: The buffer of the packet we have read.
448  *	@remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
449  *	@regs: The &struct pt_regs of the current process.
450  *
451  *	This function MUST handle the 'c' and 's' command packets,
452  *	as well packets to set / remove a hardware breakpoint, if used.
453  *	If there are additional packets which the hardware needs to handle,
454  *	they are handled here.  The code should return -1 if it wants to
455  *	process more packets, and a %0 or %1 if it wants to exit from the
456  *	kgdb callback.
457  */
458 int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
459 			       char *remcomInBuffer, char *remcomOutBuffer,
460 			       struct pt_regs *linux_regs)
461 {
462 	unsigned long addr;
463 	char *ptr;
464 	int newPC;
465 
466 	switch (remcomInBuffer[0]) {
467 	case 'c':
468 	case 's':
469 		/* try to read optional parameter, pc unchanged if no parm */
470 		ptr = &remcomInBuffer[1];
471 		if (kgdb_hex2long(&ptr, &addr))
472 			linux_regs->ip = addr;
473 	case 'D':
474 	case 'k':
475 		newPC = linux_regs->ip;
476 
477 		/* clear the trace bit */
478 		linux_regs->flags &= ~X86_EFLAGS_TF;
479 		atomic_set(&kgdb_cpu_doing_single_step, -1);
480 
481 		/* set the trace bit if we're stepping */
482 		if (remcomInBuffer[0] == 's') {
483 			linux_regs->flags |= X86_EFLAGS_TF;
484 			atomic_set(&kgdb_cpu_doing_single_step,
485 				   raw_smp_processor_id());
486 		}
487 
488 		kgdb_correct_hw_break();
489 
490 		return 0;
491 	}
492 
493 	/* this means that we do not want to exit from the handler: */
494 	return -1;
495 }
496 
497 static inline int
498 single_step_cont(struct pt_regs *regs, struct die_args *args)
499 {
500 	/*
501 	 * Single step exception from kernel space to user space so
502 	 * eat the exception and continue the process:
503 	 */
504 	printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
505 			"resuming...\n");
506 	kgdb_arch_handle_exception(args->trapnr, args->signr,
507 				   args->err, "c", "", regs);
508 	/*
509 	 * Reset the BS bit in dr6 (pointed by args->err) to
510 	 * denote completion of processing
511 	 */
512 	(*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
513 
514 	return NOTIFY_STOP;
515 }
516 
517 static int was_in_debug_nmi[NR_CPUS];
518 
519 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
520 {
521 	struct pt_regs *regs = args->regs;
522 
523 	switch (cmd) {
524 	case DIE_NMI:
525 		if (atomic_read(&kgdb_active) != -1) {
526 			/* KGDB CPU roundup */
527 			kgdb_nmicallback(raw_smp_processor_id(), regs);
528 			was_in_debug_nmi[raw_smp_processor_id()] = 1;
529 			touch_nmi_watchdog();
530 			return NOTIFY_STOP;
531 		}
532 		return NOTIFY_DONE;
533 
534 	case DIE_NMI_IPI:
535 		/* Just ignore, we will handle the roundup on DIE_NMI. */
536 		return NOTIFY_DONE;
537 
538 	case DIE_NMIUNKNOWN:
539 		if (was_in_debug_nmi[raw_smp_processor_id()]) {
540 			was_in_debug_nmi[raw_smp_processor_id()] = 0;
541 			return NOTIFY_STOP;
542 		}
543 		return NOTIFY_DONE;
544 
545 	case DIE_NMIWATCHDOG:
546 		if (atomic_read(&kgdb_active) != -1) {
547 			/* KGDB CPU roundup: */
548 			kgdb_nmicallback(raw_smp_processor_id(), regs);
549 			return NOTIFY_STOP;
550 		}
551 		/* Enter debugger: */
552 		break;
553 
554 	case DIE_DEBUG:
555 		if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
556 			if (user_mode(regs))
557 				return single_step_cont(regs, args);
558 			break;
559 		} else if (test_thread_flag(TIF_SINGLESTEP))
560 			/* This means a user thread is single stepping
561 			 * a system call which should be ignored
562 			 */
563 			return NOTIFY_DONE;
564 		/* fall through */
565 	default:
566 		if (user_mode(regs))
567 			return NOTIFY_DONE;
568 	}
569 
570 	if (kgdb_handle_exception(args->trapnr, args->signr, args->err, regs))
571 		return NOTIFY_DONE;
572 
573 	/* Must touch watchdog before return to normal operation */
574 	touch_nmi_watchdog();
575 	return NOTIFY_STOP;
576 }
577 
578 static int
579 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
580 {
581 	unsigned long flags;
582 	int ret;
583 
584 	local_irq_save(flags);
585 	ret = __kgdb_notify(ptr, cmd);
586 	local_irq_restore(flags);
587 
588 	return ret;
589 }
590 
591 static struct notifier_block kgdb_notifier = {
592 	.notifier_call	= kgdb_notify,
593 
594 	/*
595 	 * Lowest-prio notifier priority, we want to be notified last:
596 	 */
597 	.priority	= -INT_MAX,
598 };
599 
600 /**
601  *	kgdb_arch_init - Perform any architecture specific initalization.
602  *
603  *	This function will handle the initalization of any architecture
604  *	specific callbacks.
605  */
606 int kgdb_arch_init(void)
607 {
608 	int i, cpu;
609 	int ret;
610 	struct perf_event_attr attr;
611 	struct perf_event **pevent;
612 
613 	ret = register_die_notifier(&kgdb_notifier);
614 	if (ret != 0)
615 		return ret;
616 	/*
617 	 * Pre-allocate the hw breakpoint structions in the non-atomic
618 	 * portion of kgdb because this operation requires mutexs to
619 	 * complete.
620 	 */
621 	attr.bp_addr = (unsigned long)kgdb_arch_init;
622 	attr.type = PERF_TYPE_BREAKPOINT;
623 	attr.bp_len = HW_BREAKPOINT_LEN_1;
624 	attr.bp_type = HW_BREAKPOINT_W;
625 	attr.disabled = 1;
626 	for (i = 0; i < 4; i++) {
627 		breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL);
628 		if (IS_ERR(breakinfo[i].pev)) {
629 			printk(KERN_ERR "kgdb: Could not allocate hw breakpoints\n");
630 			breakinfo[i].pev = NULL;
631 			kgdb_arch_exit();
632 			return -1;
633 		}
634 		for_each_online_cpu(cpu) {
635 			pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
636 			pevent[0]->hw.sample_period = 1;
637 			if (pevent[0]->destroy != NULL) {
638 				pevent[0]->destroy = NULL;
639 				release_bp_slot(*pevent);
640 			}
641 		}
642 	}
643 	return ret;
644 }
645 
646 /**
647  *	kgdb_arch_exit - Perform any architecture specific uninitalization.
648  *
649  *	This function will handle the uninitalization of any architecture
650  *	specific callbacks, for dynamic registration and unregistration.
651  */
652 void kgdb_arch_exit(void)
653 {
654 	int i;
655 	for (i = 0; i < 4; i++) {
656 		if (breakinfo[i].pev) {
657 			unregister_wide_hw_breakpoint(breakinfo[i].pev);
658 			breakinfo[i].pev = NULL;
659 		}
660 	}
661 	unregister_die_notifier(&kgdb_notifier);
662 }
663 
664 /**
665  *
666  *	kgdb_skipexception - Bail out of KGDB when we've been triggered.
667  *	@exception: Exception vector number
668  *	@regs: Current &struct pt_regs.
669  *
670  *	On some architectures we need to skip a breakpoint exception when
671  *	it occurs after a breakpoint has been removed.
672  *
673  * Skip an int3 exception when it occurs after a breakpoint has been
674  * removed. Backtrack eip by 1 since the int3 would have caused it to
675  * increment by 1.
676  */
677 int kgdb_skipexception(int exception, struct pt_regs *regs)
678 {
679 	if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
680 		regs->ip -= 1;
681 		return 1;
682 	}
683 	return 0;
684 }
685 
686 unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
687 {
688 	if (exception == 3)
689 		return instruction_pointer(regs) - 1;
690 	return instruction_pointer(regs);
691 }
692 
693 struct kgdb_arch arch_kgdb_ops = {
694 	/* Breakpoint instruction: */
695 	.gdb_bpt_instr		= { 0xcc },
696 	.flags			= KGDB_HW_BREAKPOINT,
697 	.set_hw_breakpoint	= kgdb_set_hw_break,
698 	.remove_hw_breakpoint	= kgdb_remove_hw_break,
699 	.remove_all_hw_break	= kgdb_remove_all_hw_break,
700 	.correct_hw_break	= kgdb_correct_hw_break,
701 };
702