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