Linux Audio

Check our new training course

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