Linux Audio

Check our new training course

Loading...
v3.1
  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#include <asm/apic.h>
 51#include <asm/nmi.h>
 52
 53struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
 54{
 55#ifdef CONFIG_X86_32
 56	{ "ax", 4, offsetof(struct pt_regs, ax) },
 57	{ "cx", 4, offsetof(struct pt_regs, cx) },
 58	{ "dx", 4, offsetof(struct pt_regs, dx) },
 59	{ "bx", 4, offsetof(struct pt_regs, bx) },
 60	{ "sp", 4, offsetof(struct pt_regs, sp) },
 61	{ "bp", 4, offsetof(struct pt_regs, bp) },
 62	{ "si", 4, offsetof(struct pt_regs, si) },
 63	{ "di", 4, offsetof(struct pt_regs, di) },
 64	{ "ip", 4, offsetof(struct pt_regs, ip) },
 65	{ "flags", 4, offsetof(struct pt_regs, flags) },
 66	{ "cs", 4, offsetof(struct pt_regs, cs) },
 67	{ "ss", 4, offsetof(struct pt_regs, ss) },
 68	{ "ds", 4, offsetof(struct pt_regs, ds) },
 69	{ "es", 4, offsetof(struct pt_regs, es) },
 70	{ "fs", 4, -1 },
 71	{ "gs", 4, -1 },
 72#else
 73	{ "ax", 8, offsetof(struct pt_regs, ax) },
 74	{ "bx", 8, offsetof(struct pt_regs, bx) },
 75	{ "cx", 8, offsetof(struct pt_regs, cx) },
 76	{ "dx", 8, offsetof(struct pt_regs, dx) },
 77	{ "si", 8, offsetof(struct pt_regs, dx) },
 78	{ "di", 8, offsetof(struct pt_regs, di) },
 79	{ "bp", 8, offsetof(struct pt_regs, bp) },
 80	{ "sp", 8, offsetof(struct pt_regs, sp) },
 81	{ "r8", 8, offsetof(struct pt_regs, r8) },
 82	{ "r9", 8, offsetof(struct pt_regs, r9) },
 83	{ "r10", 8, offsetof(struct pt_regs, r10) },
 84	{ "r11", 8, offsetof(struct pt_regs, r11) },
 85	{ "r12", 8, offsetof(struct pt_regs, r12) },
 86	{ "r13", 8, offsetof(struct pt_regs, r13) },
 87	{ "r14", 8, offsetof(struct pt_regs, r14) },
 88	{ "r15", 8, offsetof(struct pt_regs, r15) },
 89	{ "ip", 8, offsetof(struct pt_regs, ip) },
 90	{ "flags", 4, offsetof(struct pt_regs, flags) },
 91	{ "cs", 4, offsetof(struct pt_regs, cs) },
 92	{ "ss", 4, offsetof(struct pt_regs, ss) },
 
 
 93#endif
 
 
 94};
 95
 96int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
 97{
 98	if (
 99#ifdef CONFIG_X86_32
100	    regno == GDB_SS || regno == GDB_FS || regno == GDB_GS ||
101#endif
102	    regno == GDB_SP || regno == GDB_ORIG_AX)
103		return 0;
104
105	if (dbg_reg_def[regno].offset != -1)
106		memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
107		       dbg_reg_def[regno].size);
108	return 0;
109}
110
111char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
112{
113	if (regno == GDB_ORIG_AX) {
114		memcpy(mem, &regs->orig_ax, sizeof(regs->orig_ax));
115		return "orig_ax";
116	}
117	if (regno >= DBG_MAX_REG_NUM || regno < 0)
118		return NULL;
119
120	if (dbg_reg_def[regno].offset != -1)
121		memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
122		       dbg_reg_def[regno].size);
123
124#ifdef CONFIG_X86_32
125	switch (regno) {
126	case GDB_SS:
127		if (!user_mode_vm(regs))
128			*(unsigned long *)mem = __KERNEL_DS;
129		break;
130	case GDB_SP:
131		if (!user_mode_vm(regs))
132			*(unsigned long *)mem = kernel_stack_pointer(regs);
133		break;
134	case GDB_GS:
135	case GDB_FS:
136		*(unsigned long *)mem = 0xFFFF;
137		break;
138	}
139#endif
140	return dbg_reg_def[regno].name;
141}
142
143/**
144 *	sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
145 *	@gdb_regs: A pointer to hold the registers in the order GDB wants.
146 *	@p: The &struct task_struct of the desired process.
147 *
148 *	Convert the register values of the sleeping process in @p to
149 *	the format that GDB expects.
150 *	This function is called when kgdb does not have access to the
151 *	&struct pt_regs and therefore it should fill the gdb registers
152 *	@gdb_regs with what has	been saved in &struct thread_struct
153 *	thread field during switch_to.
154 */
155void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
156{
157#ifndef CONFIG_X86_32
158	u32 *gdb_regs32 = (u32 *)gdb_regs;
159#endif
160	gdb_regs[GDB_AX]	= 0;
161	gdb_regs[GDB_BX]	= 0;
162	gdb_regs[GDB_CX]	= 0;
163	gdb_regs[GDB_DX]	= 0;
164	gdb_regs[GDB_SI]	= 0;
165	gdb_regs[GDB_DI]	= 0;
166	gdb_regs[GDB_BP]	= *(unsigned long *)p->thread.sp;
167#ifdef CONFIG_X86_32
168	gdb_regs[GDB_DS]	= __KERNEL_DS;
169	gdb_regs[GDB_ES]	= __KERNEL_DS;
170	gdb_regs[GDB_PS]	= 0;
171	gdb_regs[GDB_CS]	= __KERNEL_CS;
172	gdb_regs[GDB_PC]	= p->thread.ip;
173	gdb_regs[GDB_SS]	= __KERNEL_DS;
174	gdb_regs[GDB_FS]	= 0xFFFF;
175	gdb_regs[GDB_GS]	= 0xFFFF;
176#else
177	gdb_regs32[GDB_PS]	= *(unsigned long *)(p->thread.sp + 8);
178	gdb_regs32[GDB_CS]	= __KERNEL_CS;
179	gdb_regs32[GDB_SS]	= __KERNEL_DS;
180	gdb_regs[GDB_PC]	= 0;
181	gdb_regs[GDB_R8]	= 0;
182	gdb_regs[GDB_R9]	= 0;
183	gdb_regs[GDB_R10]	= 0;
184	gdb_regs[GDB_R11]	= 0;
185	gdb_regs[GDB_R12]	= 0;
186	gdb_regs[GDB_R13]	= 0;
187	gdb_regs[GDB_R14]	= 0;
188	gdb_regs[GDB_R15]	= 0;
189#endif
190	gdb_regs[GDB_SP]	= p->thread.sp;
191}
192
193static struct hw_breakpoint {
194	unsigned		enabled;
195	unsigned long		addr;
196	int			len;
197	int			type;
198	struct perf_event	* __percpu *pev;
199} breakinfo[HBP_NUM];
200
201static unsigned long early_dr7;
202
203static void kgdb_correct_hw_break(void)
204{
205	int breakno;
206
207	for (breakno = 0; breakno < HBP_NUM; breakno++) {
208		struct perf_event *bp;
209		struct arch_hw_breakpoint *info;
210		int val;
211		int cpu = raw_smp_processor_id();
212		if (!breakinfo[breakno].enabled)
213			continue;
214		if (dbg_is_early) {
215			set_debugreg(breakinfo[breakno].addr, breakno);
216			early_dr7 |= encode_dr7(breakno,
217						breakinfo[breakno].len,
218						breakinfo[breakno].type);
219			set_debugreg(early_dr7, 7);
220			continue;
221		}
222		bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu);
223		info = counter_arch_bp(bp);
224		if (bp->attr.disabled != 1)
225			continue;
226		bp->attr.bp_addr = breakinfo[breakno].addr;
227		bp->attr.bp_len = breakinfo[breakno].len;
228		bp->attr.bp_type = breakinfo[breakno].type;
229		info->address = breakinfo[breakno].addr;
230		info->len = breakinfo[breakno].len;
231		info->type = breakinfo[breakno].type;
232		val = arch_install_hw_breakpoint(bp);
233		if (!val)
234			bp->attr.disabled = 0;
235	}
236	if (!dbg_is_early)
237		hw_breakpoint_restore();
238}
239
240static int hw_break_reserve_slot(int breakno)
241{
242	int cpu;
243	int cnt = 0;
244	struct perf_event **pevent;
245
246	if (dbg_is_early)
247		return 0;
248
249	for_each_online_cpu(cpu) {
250		cnt++;
251		pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
252		if (dbg_reserve_bp_slot(*pevent))
253			goto fail;
254	}
255
256	return 0;
257
258fail:
259	for_each_online_cpu(cpu) {
260		cnt--;
261		if (!cnt)
262			break;
263		pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
264		dbg_release_bp_slot(*pevent);
265	}
266	return -1;
267}
268
269static int hw_break_release_slot(int breakno)
270{
271	struct perf_event **pevent;
272	int cpu;
273
274	if (dbg_is_early)
275		return 0;
276
277	for_each_online_cpu(cpu) {
278		pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
279		if (dbg_release_bp_slot(*pevent))
280			/*
281			 * The debugger is responsible for handing the retry on
282			 * remove failure.
283			 */
284			return -1;
285	}
286	return 0;
287}
288
289static int
290kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
291{
292	int i;
293
294	for (i = 0; i < HBP_NUM; i++)
295		if (breakinfo[i].addr == addr && breakinfo[i].enabled)
296			break;
297	if (i == HBP_NUM)
298		return -1;
299
300	if (hw_break_release_slot(i)) {
301		printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr);
302		return -1;
303	}
304	breakinfo[i].enabled = 0;
305
306	return 0;
307}
308
309static void kgdb_remove_all_hw_break(void)
310{
311	int i;
312	int cpu = raw_smp_processor_id();
313	struct perf_event *bp;
314
315	for (i = 0; i < HBP_NUM; i++) {
316		if (!breakinfo[i].enabled)
317			continue;
318		bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
319		if (!bp->attr.disabled) {
320			arch_uninstall_hw_breakpoint(bp);
321			bp->attr.disabled = 1;
322			continue;
323		}
324		if (dbg_is_early)
325			early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
326						 breakinfo[i].type);
327		else if (hw_break_release_slot(i))
328			printk(KERN_ERR "KGDB: hw bpt remove failed %lx\n",
329			       breakinfo[i].addr);
330		breakinfo[i].enabled = 0;
331	}
332}
333
334static int
335kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
336{
337	int i;
338
339	for (i = 0; i < HBP_NUM; i++)
340		if (!breakinfo[i].enabled)
341			break;
342	if (i == HBP_NUM)
343		return -1;
344
345	switch (bptype) {
346	case BP_HARDWARE_BREAKPOINT:
347		len = 1;
348		breakinfo[i].type = X86_BREAKPOINT_EXECUTE;
349		break;
350	case BP_WRITE_WATCHPOINT:
351		breakinfo[i].type = X86_BREAKPOINT_WRITE;
352		break;
353	case BP_ACCESS_WATCHPOINT:
354		breakinfo[i].type = X86_BREAKPOINT_RW;
355		break;
356	default:
357		return -1;
358	}
359	switch (len) {
360	case 1:
361		breakinfo[i].len = X86_BREAKPOINT_LEN_1;
362		break;
363	case 2:
364		breakinfo[i].len = X86_BREAKPOINT_LEN_2;
365		break;
366	case 4:
367		breakinfo[i].len = X86_BREAKPOINT_LEN_4;
368		break;
369#ifdef CONFIG_X86_64
370	case 8:
371		breakinfo[i].len = X86_BREAKPOINT_LEN_8;
372		break;
373#endif
374	default:
375		return -1;
376	}
377	breakinfo[i].addr = addr;
378	if (hw_break_reserve_slot(i)) {
379		breakinfo[i].addr = 0;
380		return -1;
381	}
382	breakinfo[i].enabled = 1;
383
384	return 0;
385}
386
387/**
388 *	kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
389 *	@regs: Current &struct pt_regs.
390 *
391 *	This function will be called if the particular architecture must
392 *	disable hardware debugging while it is processing gdb packets or
393 *	handling exception.
394 */
395static void kgdb_disable_hw_debug(struct pt_regs *regs)
396{
397	int i;
398	int cpu = raw_smp_processor_id();
399	struct perf_event *bp;
400
401	/* Disable hardware debugging while we are in kgdb: */
402	set_debugreg(0UL, 7);
403	for (i = 0; i < HBP_NUM; i++) {
404		if (!breakinfo[i].enabled)
405			continue;
406		if (dbg_is_early) {
407			early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
408						 breakinfo[i].type);
409			continue;
410		}
411		bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
412		if (bp->attr.disabled == 1)
413			continue;
414		arch_uninstall_hw_breakpoint(bp);
415		bp->attr.disabled = 1;
416	}
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 */
436void 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 */
458int 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
465	switch (remcomInBuffer[0]) {
466	case 'c':
467	case 's':
468		/* try to read optional parameter, pc unchanged if no parm */
469		ptr = &remcomInBuffer[1];
470		if (kgdb_hex2long(&ptr, &addr))
471			linux_regs->ip = addr;
472	case 'D':
473	case 'k':
474		/* clear the trace bit */
475		linux_regs->flags &= ~X86_EFLAGS_TF;
476		atomic_set(&kgdb_cpu_doing_single_step, -1);
477
478		/* set the trace bit if we're stepping */
479		if (remcomInBuffer[0] == 's') {
480			linux_regs->flags |= X86_EFLAGS_TF;
481			atomic_set(&kgdb_cpu_doing_single_step,
482				   raw_smp_processor_id());
483		}
484
485		return 0;
486	}
487
488	/* this means that we do not want to exit from the handler: */
489	return -1;
490}
491
492static inline int
493single_step_cont(struct pt_regs *regs, struct die_args *args)
494{
495	/*
496	 * Single step exception from kernel space to user space so
497	 * eat the exception and continue the process:
498	 */
499	printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
500			"resuming...\n");
501	kgdb_arch_handle_exception(args->trapnr, args->signr,
502				   args->err, "c", "", regs);
503	/*
504	 * Reset the BS bit in dr6 (pointed by args->err) to
505	 * denote completion of processing
506	 */
507	(*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
508
509	return NOTIFY_STOP;
510}
511
512static int was_in_debug_nmi[NR_CPUS];
513
514static int __kgdb_notify(struct die_args *args, unsigned long cmd)
515{
516	struct pt_regs *regs = args->regs;
517
518	switch (cmd) {
519	case DIE_NMI:
520		if (atomic_read(&kgdb_active) != -1) {
521			/* KGDB CPU roundup */
522			kgdb_nmicallback(raw_smp_processor_id(), regs);
523			was_in_debug_nmi[raw_smp_processor_id()] = 1;
 
524			touch_nmi_watchdog();
525			return NOTIFY_STOP;
526		}
527		return NOTIFY_DONE;
528
529	case DIE_NMIUNKNOWN:
530		if (was_in_debug_nmi[raw_smp_processor_id()]) {
531			was_in_debug_nmi[raw_smp_processor_id()] = 0;
532			return NOTIFY_STOP;
533		}
534		return NOTIFY_DONE;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
535
 
536	case DIE_DEBUG:
537		if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
538			if (user_mode(regs))
539				return single_step_cont(regs, args);
540			break;
541		} else if (test_thread_flag(TIF_SINGLESTEP))
542			/* This means a user thread is single stepping
543			 * a system call which should be ignored
544			 */
545			return NOTIFY_DONE;
546		/* fall through */
547	default:
548		if (user_mode(regs))
549			return NOTIFY_DONE;
550	}
551
552	if (kgdb_handle_exception(args->trapnr, args->signr, cmd, regs))
553		return NOTIFY_DONE;
554
555	/* Must touch watchdog before return to normal operation */
556	touch_nmi_watchdog();
557	return NOTIFY_STOP;
558}
559
560int kgdb_ll_trap(int cmd, const char *str,
561		 struct pt_regs *regs, long err, int trap, int sig)
562{
563	struct die_args args = {
564		.regs	= regs,
565		.str	= str,
566		.err	= err,
567		.trapnr	= trap,
568		.signr	= sig,
569
570	};
571
572	if (!kgdb_io_module_registered)
573		return NOTIFY_DONE;
574
575	return __kgdb_notify(&args, cmd);
576}
577
578static int
579kgdb_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
591static 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	= NMI_LOCAL_LOW_PRIOR,
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 */
606int kgdb_arch_init(void)
607{
608	return register_die_notifier(&kgdb_notifier);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
609}
610
611static void kgdb_hw_overflow_handler(struct perf_event *event,
612		struct perf_sample_data *data, struct pt_regs *regs)
613{
614	struct task_struct *tsk = current;
615	int i;
616
617	for (i = 0; i < 4; i++)
618		if (breakinfo[i].enabled)
619			tsk->thread.debugreg6 |= (DR_TRAP0 << i);
620}
621
622void kgdb_arch_late(void)
623{
624	int i, cpu;
625	struct perf_event_attr attr;
626	struct perf_event **pevent;
627
628	/*
629	 * Pre-allocate the hw breakpoint structions in the non-atomic
630	 * portion of kgdb because this operation requires mutexs to
631	 * complete.
632	 */
633	hw_breakpoint_init(&attr);
634	attr.bp_addr = (unsigned long)kgdb_arch_init;
635	attr.bp_len = HW_BREAKPOINT_LEN_1;
636	attr.bp_type = HW_BREAKPOINT_W;
637	attr.disabled = 1;
638	for (i = 0; i < HBP_NUM; i++) {
639		if (breakinfo[i].pev)
640			continue;
641		breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL, NULL);
642		if (IS_ERR((void * __force)breakinfo[i].pev)) {
643			printk(KERN_ERR "kgdb: Could not allocate hw"
644			       "breakpoints\nDisabling the kernel debugger\n");
645			breakinfo[i].pev = NULL;
646			kgdb_arch_exit();
647			return;
648		}
649		for_each_online_cpu(cpu) {
650			pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
651			pevent[0]->hw.sample_period = 1;
652			pevent[0]->overflow_handler = kgdb_hw_overflow_handler;
653			if (pevent[0]->destroy != NULL) {
654				pevent[0]->destroy = NULL;
655				release_bp_slot(*pevent);
656			}
657		}
658	}
659}
660
661/**
662 *	kgdb_arch_exit - Perform any architecture specific uninitalization.
663 *
664 *	This function will handle the uninitalization of any architecture
665 *	specific callbacks, for dynamic registration and unregistration.
666 */
667void kgdb_arch_exit(void)
668{
669	int i;
670	for (i = 0; i < 4; i++) {
671		if (breakinfo[i].pev) {
672			unregister_wide_hw_breakpoint(breakinfo[i].pev);
673			breakinfo[i].pev = NULL;
674		}
675	}
 
 
676	unregister_die_notifier(&kgdb_notifier);
677}
678
679/**
680 *
681 *	kgdb_skipexception - Bail out of KGDB when we've been triggered.
682 *	@exception: Exception vector number
683 *	@regs: Current &struct pt_regs.
684 *
685 *	On some architectures we need to skip a breakpoint exception when
686 *	it occurs after a breakpoint has been removed.
687 *
688 * Skip an int3 exception when it occurs after a breakpoint has been
689 * removed. Backtrack eip by 1 since the int3 would have caused it to
690 * increment by 1.
691 */
692int kgdb_skipexception(int exception, struct pt_regs *regs)
693{
694	if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
695		regs->ip -= 1;
696		return 1;
697	}
698	return 0;
699}
700
701unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
702{
703	if (exception == 3)
704		return instruction_pointer(regs) - 1;
705	return instruction_pointer(regs);
706}
707
708void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
709{
710	regs->ip = ip;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
711}
712
713struct kgdb_arch arch_kgdb_ops = {
714	/* Breakpoint instruction: */
715	.gdb_bpt_instr		= { 0xcc },
716	.flags			= KGDB_HW_BREAKPOINT,
717	.set_hw_breakpoint	= kgdb_set_hw_break,
718	.remove_hw_breakpoint	= kgdb_remove_hw_break,
719	.disable_hw_break	= kgdb_disable_hw_debug,
720	.remove_all_hw_break	= kgdb_remove_all_hw_break,
721	.correct_hw_break	= kgdb_correct_hw_break,
722};
v4.6
  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/smp.h>
 43#include <linux/nmi.h>
 44#include <linux/hw_breakpoint.h>
 45#include <linux/uaccess.h>
 46#include <linux/memory.h>
 47
 48#include <asm/debugreg.h>
 49#include <asm/apicdef.h>
 
 50#include <asm/apic.h>
 51#include <asm/nmi.h>
 52
 53struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
 54{
 55#ifdef CONFIG_X86_32
 56	{ "ax", 4, offsetof(struct pt_regs, ax) },
 57	{ "cx", 4, offsetof(struct pt_regs, cx) },
 58	{ "dx", 4, offsetof(struct pt_regs, dx) },
 59	{ "bx", 4, offsetof(struct pt_regs, bx) },
 60	{ "sp", 4, offsetof(struct pt_regs, sp) },
 61	{ "bp", 4, offsetof(struct pt_regs, bp) },
 62	{ "si", 4, offsetof(struct pt_regs, si) },
 63	{ "di", 4, offsetof(struct pt_regs, di) },
 64	{ "ip", 4, offsetof(struct pt_regs, ip) },
 65	{ "flags", 4, offsetof(struct pt_regs, flags) },
 66	{ "cs", 4, offsetof(struct pt_regs, cs) },
 67	{ "ss", 4, offsetof(struct pt_regs, ss) },
 68	{ "ds", 4, offsetof(struct pt_regs, ds) },
 69	{ "es", 4, offsetof(struct pt_regs, es) },
 
 
 70#else
 71	{ "ax", 8, offsetof(struct pt_regs, ax) },
 72	{ "bx", 8, offsetof(struct pt_regs, bx) },
 73	{ "cx", 8, offsetof(struct pt_regs, cx) },
 74	{ "dx", 8, offsetof(struct pt_regs, dx) },
 75	{ "si", 8, offsetof(struct pt_regs, si) },
 76	{ "di", 8, offsetof(struct pt_regs, di) },
 77	{ "bp", 8, offsetof(struct pt_regs, bp) },
 78	{ "sp", 8, offsetof(struct pt_regs, sp) },
 79	{ "r8", 8, offsetof(struct pt_regs, r8) },
 80	{ "r9", 8, offsetof(struct pt_regs, r9) },
 81	{ "r10", 8, offsetof(struct pt_regs, r10) },
 82	{ "r11", 8, offsetof(struct pt_regs, r11) },
 83	{ "r12", 8, offsetof(struct pt_regs, r12) },
 84	{ "r13", 8, offsetof(struct pt_regs, r13) },
 85	{ "r14", 8, offsetof(struct pt_regs, r14) },
 86	{ "r15", 8, offsetof(struct pt_regs, r15) },
 87	{ "ip", 8, offsetof(struct pt_regs, ip) },
 88	{ "flags", 4, offsetof(struct pt_regs, flags) },
 89	{ "cs", 4, offsetof(struct pt_regs, cs) },
 90	{ "ss", 4, offsetof(struct pt_regs, ss) },
 91	{ "ds", 4, -1 },
 92	{ "es", 4, -1 },
 93#endif
 94	{ "fs", 4, -1 },
 95	{ "gs", 4, -1 },
 96};
 97
 98int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
 99{
100	if (
101#ifdef CONFIG_X86_32
102	    regno == GDB_SS || regno == GDB_FS || regno == GDB_GS ||
103#endif
104	    regno == GDB_SP || regno == GDB_ORIG_AX)
105		return 0;
106
107	if (dbg_reg_def[regno].offset != -1)
108		memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
109		       dbg_reg_def[regno].size);
110	return 0;
111}
112
113char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
114{
115	if (regno == GDB_ORIG_AX) {
116		memcpy(mem, &regs->orig_ax, sizeof(regs->orig_ax));
117		return "orig_ax";
118	}
119	if (regno >= DBG_MAX_REG_NUM || regno < 0)
120		return NULL;
121
122	if (dbg_reg_def[regno].offset != -1)
123		memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
124		       dbg_reg_def[regno].size);
125
126#ifdef CONFIG_X86_32
127	switch (regno) {
128	case GDB_SS:
129		if (!user_mode(regs))
130			*(unsigned long *)mem = __KERNEL_DS;
131		break;
132	case GDB_SP:
133		if (!user_mode(regs))
134			*(unsigned long *)mem = kernel_stack_pointer(regs);
135		break;
136	case GDB_GS:
137	case GDB_FS:
138		*(unsigned long *)mem = 0xFFFF;
139		break;
140	}
141#endif
142	return dbg_reg_def[regno].name;
143}
144
145/**
146 *	sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
147 *	@gdb_regs: A pointer to hold the registers in the order GDB wants.
148 *	@p: The &struct task_struct of the desired process.
149 *
150 *	Convert the register values of the sleeping process in @p to
151 *	the format that GDB expects.
152 *	This function is called when kgdb does not have access to the
153 *	&struct pt_regs and therefore it should fill the gdb registers
154 *	@gdb_regs with what has	been saved in &struct thread_struct
155 *	thread field during switch_to.
156 */
157void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
158{
159#ifndef CONFIG_X86_32
160	u32 *gdb_regs32 = (u32 *)gdb_regs;
161#endif
162	gdb_regs[GDB_AX]	= 0;
163	gdb_regs[GDB_BX]	= 0;
164	gdb_regs[GDB_CX]	= 0;
165	gdb_regs[GDB_DX]	= 0;
166	gdb_regs[GDB_SI]	= 0;
167	gdb_regs[GDB_DI]	= 0;
168	gdb_regs[GDB_BP]	= *(unsigned long *)p->thread.sp;
169#ifdef CONFIG_X86_32
170	gdb_regs[GDB_DS]	= __KERNEL_DS;
171	gdb_regs[GDB_ES]	= __KERNEL_DS;
172	gdb_regs[GDB_PS]	= 0;
173	gdb_regs[GDB_CS]	= __KERNEL_CS;
174	gdb_regs[GDB_PC]	= p->thread.ip;
175	gdb_regs[GDB_SS]	= __KERNEL_DS;
176	gdb_regs[GDB_FS]	= 0xFFFF;
177	gdb_regs[GDB_GS]	= 0xFFFF;
178#else
179	gdb_regs32[GDB_PS]	= *(unsigned long *)(p->thread.sp + 8);
180	gdb_regs32[GDB_CS]	= __KERNEL_CS;
181	gdb_regs32[GDB_SS]	= __KERNEL_DS;
182	gdb_regs[GDB_PC]	= 0;
183	gdb_regs[GDB_R8]	= 0;
184	gdb_regs[GDB_R9]	= 0;
185	gdb_regs[GDB_R10]	= 0;
186	gdb_regs[GDB_R11]	= 0;
187	gdb_regs[GDB_R12]	= 0;
188	gdb_regs[GDB_R13]	= 0;
189	gdb_regs[GDB_R14]	= 0;
190	gdb_regs[GDB_R15]	= 0;
191#endif
192	gdb_regs[GDB_SP]	= p->thread.sp;
193}
194
195static struct hw_breakpoint {
196	unsigned		enabled;
197	unsigned long		addr;
198	int			len;
199	int			type;
200	struct perf_event	* __percpu *pev;
201} breakinfo[HBP_NUM];
202
203static unsigned long early_dr7;
204
205static void kgdb_correct_hw_break(void)
206{
207	int breakno;
208
209	for (breakno = 0; breakno < HBP_NUM; breakno++) {
210		struct perf_event *bp;
211		struct arch_hw_breakpoint *info;
212		int val;
213		int cpu = raw_smp_processor_id();
214		if (!breakinfo[breakno].enabled)
215			continue;
216		if (dbg_is_early) {
217			set_debugreg(breakinfo[breakno].addr, breakno);
218			early_dr7 |= encode_dr7(breakno,
219						breakinfo[breakno].len,
220						breakinfo[breakno].type);
221			set_debugreg(early_dr7, 7);
222			continue;
223		}
224		bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu);
225		info = counter_arch_bp(bp);
226		if (bp->attr.disabled != 1)
227			continue;
228		bp->attr.bp_addr = breakinfo[breakno].addr;
229		bp->attr.bp_len = breakinfo[breakno].len;
230		bp->attr.bp_type = breakinfo[breakno].type;
231		info->address = breakinfo[breakno].addr;
232		info->len = breakinfo[breakno].len;
233		info->type = breakinfo[breakno].type;
234		val = arch_install_hw_breakpoint(bp);
235		if (!val)
236			bp->attr.disabled = 0;
237	}
238	if (!dbg_is_early)
239		hw_breakpoint_restore();
240}
241
242static int hw_break_reserve_slot(int breakno)
243{
244	int cpu;
245	int cnt = 0;
246	struct perf_event **pevent;
247
248	if (dbg_is_early)
249		return 0;
250
251	for_each_online_cpu(cpu) {
252		cnt++;
253		pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
254		if (dbg_reserve_bp_slot(*pevent))
255			goto fail;
256	}
257
258	return 0;
259
260fail:
261	for_each_online_cpu(cpu) {
262		cnt--;
263		if (!cnt)
264			break;
265		pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
266		dbg_release_bp_slot(*pevent);
267	}
268	return -1;
269}
270
271static int hw_break_release_slot(int breakno)
272{
273	struct perf_event **pevent;
274	int cpu;
275
276	if (dbg_is_early)
277		return 0;
278
279	for_each_online_cpu(cpu) {
280		pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
281		if (dbg_release_bp_slot(*pevent))
282			/*
283			 * The debugger is responsible for handing the retry on
284			 * remove failure.
285			 */
286			return -1;
287	}
288	return 0;
289}
290
291static int
292kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
293{
294	int i;
295
296	for (i = 0; i < HBP_NUM; i++)
297		if (breakinfo[i].addr == addr && breakinfo[i].enabled)
298			break;
299	if (i == HBP_NUM)
300		return -1;
301
302	if (hw_break_release_slot(i)) {
303		printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr);
304		return -1;
305	}
306	breakinfo[i].enabled = 0;
307
308	return 0;
309}
310
311static void kgdb_remove_all_hw_break(void)
312{
313	int i;
314	int cpu = raw_smp_processor_id();
315	struct perf_event *bp;
316
317	for (i = 0; i < HBP_NUM; i++) {
318		if (!breakinfo[i].enabled)
319			continue;
320		bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
321		if (!bp->attr.disabled) {
322			arch_uninstall_hw_breakpoint(bp);
323			bp->attr.disabled = 1;
324			continue;
325		}
326		if (dbg_is_early)
327			early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
328						 breakinfo[i].type);
329		else if (hw_break_release_slot(i))
330			printk(KERN_ERR "KGDB: hw bpt remove failed %lx\n",
331			       breakinfo[i].addr);
332		breakinfo[i].enabled = 0;
333	}
334}
335
336static int
337kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
338{
339	int i;
340
341	for (i = 0; i < HBP_NUM; i++)
342		if (!breakinfo[i].enabled)
343			break;
344	if (i == HBP_NUM)
345		return -1;
346
347	switch (bptype) {
348	case BP_HARDWARE_BREAKPOINT:
349		len = 1;
350		breakinfo[i].type = X86_BREAKPOINT_EXECUTE;
351		break;
352	case BP_WRITE_WATCHPOINT:
353		breakinfo[i].type = X86_BREAKPOINT_WRITE;
354		break;
355	case BP_ACCESS_WATCHPOINT:
356		breakinfo[i].type = X86_BREAKPOINT_RW;
357		break;
358	default:
359		return -1;
360	}
361	switch (len) {
362	case 1:
363		breakinfo[i].len = X86_BREAKPOINT_LEN_1;
364		break;
365	case 2:
366		breakinfo[i].len = X86_BREAKPOINT_LEN_2;
367		break;
368	case 4:
369		breakinfo[i].len = X86_BREAKPOINT_LEN_4;
370		break;
371#ifdef CONFIG_X86_64
372	case 8:
373		breakinfo[i].len = X86_BREAKPOINT_LEN_8;
374		break;
375#endif
376	default:
377		return -1;
378	}
379	breakinfo[i].addr = addr;
380	if (hw_break_reserve_slot(i)) {
381		breakinfo[i].addr = 0;
382		return -1;
383	}
384	breakinfo[i].enabled = 1;
385
386	return 0;
387}
388
389/**
390 *	kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
391 *	@regs: Current &struct pt_regs.
392 *
393 *	This function will be called if the particular architecture must
394 *	disable hardware debugging while it is processing gdb packets or
395 *	handling exception.
396 */
397static void kgdb_disable_hw_debug(struct pt_regs *regs)
398{
399	int i;
400	int cpu = raw_smp_processor_id();
401	struct perf_event *bp;
402
403	/* Disable hardware debugging while we are in kgdb: */
404	set_debugreg(0UL, 7);
405	for (i = 0; i < HBP_NUM; i++) {
406		if (!breakinfo[i].enabled)
407			continue;
408		if (dbg_is_early) {
409			early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
410						 breakinfo[i].type);
411			continue;
412		}
413		bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
414		if (bp->attr.disabled == 1)
415			continue;
416		arch_uninstall_hw_breakpoint(bp);
417		bp->attr.disabled = 1;
418	}
419}
420
421#ifdef CONFIG_SMP
422/**
423 *	kgdb_roundup_cpus - Get other CPUs into a holding pattern
424 *	@flags: Current IRQ state
425 *
426 *	On SMP systems, we need to get the attention of the other CPUs
427 *	and get them be in a known state.  This should do what is needed
428 *	to get the other CPUs to call kgdb_wait(). Note that on some arches,
429 *	the NMI approach is not used for rounding up all the CPUs. For example,
430 *	in case of MIPS, smp_call_function() is used to roundup CPUs. In
431 *	this case, we have to make sure that interrupts are enabled before
432 *	calling smp_call_function(). The argument to this function is
433 *	the flags that will be used when restoring the interrupts. There is
434 *	local_irq_save() call before kgdb_roundup_cpus().
435 *
436 *	On non-SMP systems, this is not called.
437 */
438void kgdb_roundup_cpus(unsigned long flags)
439{
440	apic->send_IPI_allbutself(APIC_DM_NMI);
441}
442#endif
443
444/**
445 *	kgdb_arch_handle_exception - Handle architecture specific GDB packets.
446 *	@e_vector: The error vector of the exception that happened.
447 *	@signo: The signal number of the exception that happened.
448 *	@err_code: The error code of the exception that happened.
449 *	@remcomInBuffer: The buffer of the packet we have read.
450 *	@remcomOutBuffer: The buffer of %BUFMAX bytes to write a packet into.
451 *	@linux_regs: The &struct pt_regs of the current process.
452 *
453 *	This function MUST handle the 'c' and 's' command packets,
454 *	as well packets to set / remove a hardware breakpoint, if used.
455 *	If there are additional packets which the hardware needs to handle,
456 *	they are handled here.  The code should return -1 if it wants to
457 *	process more packets, and a %0 or %1 if it wants to exit from the
458 *	kgdb callback.
459 */
460int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
461			       char *remcomInBuffer, char *remcomOutBuffer,
462			       struct pt_regs *linux_regs)
463{
464	unsigned long addr;
465	char *ptr;
466
467	switch (remcomInBuffer[0]) {
468	case 'c':
469	case 's':
470		/* try to read optional parameter, pc unchanged if no parm */
471		ptr = &remcomInBuffer[1];
472		if (kgdb_hex2long(&ptr, &addr))
473			linux_regs->ip = addr;
474	case 'D':
475	case 'k':
476		/* clear the trace bit */
477		linux_regs->flags &= ~X86_EFLAGS_TF;
478		atomic_set(&kgdb_cpu_doing_single_step, -1);
479
480		/* set the trace bit if we're stepping */
481		if (remcomInBuffer[0] == 's') {
482			linux_regs->flags |= X86_EFLAGS_TF;
483			atomic_set(&kgdb_cpu_doing_single_step,
484				   raw_smp_processor_id());
485		}
486
487		return 0;
488	}
489
490	/* this means that we do not want to exit from the handler: */
491	return -1;
492}
493
494static inline int
495single_step_cont(struct pt_regs *regs, struct die_args *args)
496{
497	/*
498	 * Single step exception from kernel space to user space so
499	 * eat the exception and continue the process:
500	 */
501	printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
502			"resuming...\n");
503	kgdb_arch_handle_exception(args->trapnr, args->signr,
504				   args->err, "c", "", regs);
505	/*
506	 * Reset the BS bit in dr6 (pointed by args->err) to
507	 * denote completion of processing
508	 */
509	(*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
510
511	return NOTIFY_STOP;
512}
513
514static DECLARE_BITMAP(was_in_debug_nmi, NR_CPUS);
515
516static int kgdb_nmi_handler(unsigned int cmd, struct pt_regs *regs)
517{
518	int cpu;
519
520	switch (cmd) {
521	case NMI_LOCAL:
522		if (atomic_read(&kgdb_active) != -1) {
523			/* KGDB CPU roundup */
524			cpu = raw_smp_processor_id();
525			kgdb_nmicallback(cpu, regs);
526			set_bit(cpu, was_in_debug_nmi);
527			touch_nmi_watchdog();
 
 
 
528
529			return NMI_HANDLED;
 
 
 
530		}
531		break;
532
533	case NMI_UNKNOWN:
534		cpu = raw_smp_processor_id();
535
536		if (__test_and_clear_bit(cpu, was_in_debug_nmi))
537			return NMI_HANDLED;
538
539		break;
540	default:
541		/* do nothing */
542		break;
543	}
544	return NMI_DONE;
545}
546
547static int __kgdb_notify(struct die_args *args, unsigned long cmd)
548{
549	struct pt_regs *regs = args->regs;
550
551	switch (cmd) {
552	case DIE_DEBUG:
553		if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
554			if (user_mode(regs))
555				return single_step_cont(regs, args);
556			break;
557		} else if (test_thread_flag(TIF_SINGLESTEP))
558			/* This means a user thread is single stepping
559			 * a system call which should be ignored
560			 */
561			return NOTIFY_DONE;
562		/* fall through */
563	default:
564		if (user_mode(regs))
565			return NOTIFY_DONE;
566	}
567
568	if (kgdb_handle_exception(args->trapnr, args->signr, cmd, regs))
569		return NOTIFY_DONE;
570
571	/* Must touch watchdog before return to normal operation */
572	touch_nmi_watchdog();
573	return NOTIFY_STOP;
574}
575
576int kgdb_ll_trap(int cmd, const char *str,
577		 struct pt_regs *regs, long err, int trap, int sig)
578{
579	struct die_args args = {
580		.regs	= regs,
581		.str	= str,
582		.err	= err,
583		.trapnr	= trap,
584		.signr	= sig,
585
586	};
587
588	if (!kgdb_io_module_registered)
589		return NOTIFY_DONE;
590
591	return __kgdb_notify(&args, cmd);
592}
593
594static int
595kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
596{
597	unsigned long flags;
598	int ret;
599
600	local_irq_save(flags);
601	ret = __kgdb_notify(ptr, cmd);
602	local_irq_restore(flags);
603
604	return ret;
605}
606
607static struct notifier_block kgdb_notifier = {
608	.notifier_call	= kgdb_notify,
 
 
 
 
 
609};
610
611/**
612 *	kgdb_arch_init - Perform any architecture specific initialization.
613 *
614 *	This function will handle the initialization of any architecture
615 *	specific callbacks.
616 */
617int kgdb_arch_init(void)
618{
619	int retval;
620
621	retval = register_die_notifier(&kgdb_notifier);
622	if (retval)
623		goto out;
624
625	retval = register_nmi_handler(NMI_LOCAL, kgdb_nmi_handler,
626					0, "kgdb");
627	if (retval)
628		goto out1;
629
630	retval = register_nmi_handler(NMI_UNKNOWN, kgdb_nmi_handler,
631					0, "kgdb");
632
633	if (retval)
634		goto out2;
635
636	return retval;
637
638out2:
639	unregister_nmi_handler(NMI_LOCAL, "kgdb");
640out1:
641	unregister_die_notifier(&kgdb_notifier);
642out:
643	return retval;
644}
645
646static void kgdb_hw_overflow_handler(struct perf_event *event,
647		struct perf_sample_data *data, struct pt_regs *regs)
648{
649	struct task_struct *tsk = current;
650	int i;
651
652	for (i = 0; i < 4; i++)
653		if (breakinfo[i].enabled)
654			tsk->thread.debugreg6 |= (DR_TRAP0 << i);
655}
656
657void kgdb_arch_late(void)
658{
659	int i, cpu;
660	struct perf_event_attr attr;
661	struct perf_event **pevent;
662
663	/*
664	 * Pre-allocate the hw breakpoint structions in the non-atomic
665	 * portion of kgdb because this operation requires mutexs to
666	 * complete.
667	 */
668	hw_breakpoint_init(&attr);
669	attr.bp_addr = (unsigned long)kgdb_arch_init;
670	attr.bp_len = HW_BREAKPOINT_LEN_1;
671	attr.bp_type = HW_BREAKPOINT_W;
672	attr.disabled = 1;
673	for (i = 0; i < HBP_NUM; i++) {
674		if (breakinfo[i].pev)
675			continue;
676		breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL, NULL);
677		if (IS_ERR((void * __force)breakinfo[i].pev)) {
678			printk(KERN_ERR "kgdb: Could not allocate hw"
679			       "breakpoints\nDisabling the kernel debugger\n");
680			breakinfo[i].pev = NULL;
681			kgdb_arch_exit();
682			return;
683		}
684		for_each_online_cpu(cpu) {
685			pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
686			pevent[0]->hw.sample_period = 1;
687			pevent[0]->overflow_handler = kgdb_hw_overflow_handler;
688			if (pevent[0]->destroy != NULL) {
689				pevent[0]->destroy = NULL;
690				release_bp_slot(*pevent);
691			}
692		}
693	}
694}
695
696/**
697 *	kgdb_arch_exit - Perform any architecture specific uninitalization.
698 *
699 *	This function will handle the uninitalization of any architecture
700 *	specific callbacks, for dynamic registration and unregistration.
701 */
702void kgdb_arch_exit(void)
703{
704	int i;
705	for (i = 0; i < 4; i++) {
706		if (breakinfo[i].pev) {
707			unregister_wide_hw_breakpoint(breakinfo[i].pev);
708			breakinfo[i].pev = NULL;
709		}
710	}
711	unregister_nmi_handler(NMI_UNKNOWN, "kgdb");
712	unregister_nmi_handler(NMI_LOCAL, "kgdb");
713	unregister_die_notifier(&kgdb_notifier);
714}
715
716/**
717 *
718 *	kgdb_skipexception - Bail out of KGDB when we've been triggered.
719 *	@exception: Exception vector number
720 *	@regs: Current &struct pt_regs.
721 *
722 *	On some architectures we need to skip a breakpoint exception when
723 *	it occurs after a breakpoint has been removed.
724 *
725 * Skip an int3 exception when it occurs after a breakpoint has been
726 * removed. Backtrack eip by 1 since the int3 would have caused it to
727 * increment by 1.
728 */
729int kgdb_skipexception(int exception, struct pt_regs *regs)
730{
731	if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
732		regs->ip -= 1;
733		return 1;
734	}
735	return 0;
736}
737
738unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
739{
740	if (exception == 3)
741		return instruction_pointer(regs) - 1;
742	return instruction_pointer(regs);
743}
744
745void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
746{
747	regs->ip = ip;
748}
749
750int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
751{
752	int err;
753	char opc[BREAK_INSTR_SIZE];
754
755	bpt->type = BP_BREAKPOINT;
756	err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
757				BREAK_INSTR_SIZE);
758	if (err)
759		return err;
760	err = probe_kernel_write((char *)bpt->bpt_addr,
761				 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
762	if (!err)
763		return err;
764	/*
765	 * It is safe to call text_poke() because normal kernel execution
766	 * is stopped on all cores, so long as the text_mutex is not locked.
767	 */
768	if (mutex_is_locked(&text_mutex))
769		return -EBUSY;
770	text_poke((void *)bpt->bpt_addr, arch_kgdb_ops.gdb_bpt_instr,
771		  BREAK_INSTR_SIZE);
772	err = probe_kernel_read(opc, (char *)bpt->bpt_addr, BREAK_INSTR_SIZE);
773	if (err)
774		return err;
775	if (memcmp(opc, arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE))
776		return -EINVAL;
777	bpt->type = BP_POKE_BREAKPOINT;
778
779	return err;
780}
781
782int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
783{
784	int err;
785	char opc[BREAK_INSTR_SIZE];
786
787	if (bpt->type != BP_POKE_BREAKPOINT)
788		goto knl_write;
789	/*
790	 * It is safe to call text_poke() because normal kernel execution
791	 * is stopped on all cores, so long as the text_mutex is not locked.
792	 */
793	if (mutex_is_locked(&text_mutex))
794		goto knl_write;
795	text_poke((void *)bpt->bpt_addr, bpt->saved_instr, BREAK_INSTR_SIZE);
796	err = probe_kernel_read(opc, (char *)bpt->bpt_addr, BREAK_INSTR_SIZE);
797	if (err || memcmp(opc, bpt->saved_instr, BREAK_INSTR_SIZE))
798		goto knl_write;
799	return err;
800
801knl_write:
802	return probe_kernel_write((char *)bpt->bpt_addr,
803				  (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
804}
805
806struct kgdb_arch arch_kgdb_ops = {
807	/* Breakpoint instruction: */
808	.gdb_bpt_instr		= { 0xcc },
809	.flags			= KGDB_HW_BREAKPOINT,
810	.set_hw_breakpoint	= kgdb_set_hw_break,
811	.remove_hw_breakpoint	= kgdb_remove_hw_break,
812	.disable_hw_break	= kgdb_disable_hw_debug,
813	.remove_all_hw_break	= kgdb_remove_all_hw_break,
814	.correct_hw_break	= kgdb_correct_hw_break,
815};