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