Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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