f820b73c7f28461f9f979228adc26a281d955ed4
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / x86 / kernel / kgdb.c
1 /*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License as published by the
4 * Free Software Foundation; either version 2, or (at your option) any
5 * later version.
6 *
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 *
12 */
13
14 /*
15 * Copyright (C) 2004 Amit S. Kale <amitkale@linsyssoft.com>
16 * Copyright (C) 2000-2001 VERITAS Software Corporation.
17 * Copyright (C) 2002 Andi Kleen, SuSE Labs
18 * Copyright (C) 2004 LinSysSoft Technologies Pvt. Ltd.
19 * Copyright (C) 2007 MontaVista Software, Inc.
20 * Copyright (C) 2007-2008 Jason Wessel, Wind River Systems, Inc.
21 */
22 /****************************************************************************
23 * Contributor: Lake Stevens Instrument Division$
24 * Written by: Glenn Engel $
25 * Updated by: Amit Kale<akale@veritas.com>
26 * Updated by: Tom Rini <trini@kernel.crashing.org>
27 * Updated by: Jason Wessel <jason.wessel@windriver.com>
28 * Modified for 386 by Jim Kingdon, Cygnus Support.
29 * Origianl kgdb, compatibility with 2.1.xx kernel by
30 * David Grothe <dave@gcom.com>
31 * Integrated into 2.2.5 kernel by Tigran Aivazian <tigran@sco.com>
32 * X86_64 changes from Andi Kleen's patch merged by Jim Houston
33 */
34 #include <linux/spinlock.h>
35 #include <linux/kdebug.h>
36 #include <linux/string.h>
37 #include <linux/kernel.h>
38 #include <linux/ptrace.h>
39 #include <linux/sched.h>
40 #include <linux/delay.h>
41 #include <linux/kgdb.h>
42 #include <linux/init.h>
43 #include <linux/smp.h>
44 #include <linux/nmi.h>
45
46 #include <asm/debugreg.h>
47 #include <asm/apicdef.h>
48 #include <asm/system.h>
49
50 #include <asm/apic.h>
51
52 /*
53 * Put the error code here just in case the user cares:
54 */
55 static int gdb_x86errcode;
56
57 /*
58 * Likewise, the vector number here (since GDB only gets the signal
59 * number through the usual means, and that's not very specific):
60 */
61 static int gdb_x86vector = -1;
62
63 /**
64 * pt_regs_to_gdb_regs - Convert ptrace regs to GDB regs
65 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
66 * @regs: The &struct pt_regs of the current process.
67 *
68 * Convert the pt_regs in @regs into the format for registers that
69 * GDB expects, stored in @gdb_regs.
70 */
71 void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
72 {
73 #ifndef CONFIG_X86_32
74 u32 *gdb_regs32 = (u32 *)gdb_regs;
75 #endif
76 gdb_regs[GDB_AX] = regs->ax;
77 gdb_regs[GDB_BX] = regs->bx;
78 gdb_regs[GDB_CX] = regs->cx;
79 gdb_regs[GDB_DX] = regs->dx;
80 gdb_regs[GDB_SI] = regs->si;
81 gdb_regs[GDB_DI] = regs->di;
82 gdb_regs[GDB_BP] = regs->bp;
83 gdb_regs[GDB_PC] = regs->ip;
84 #ifdef CONFIG_X86_32
85 gdb_regs[GDB_PS] = regs->flags;
86 gdb_regs[GDB_DS] = regs->ds;
87 gdb_regs[GDB_ES] = regs->es;
88 gdb_regs[GDB_CS] = regs->cs;
89 gdb_regs[GDB_SS] = __KERNEL_DS;
90 gdb_regs[GDB_FS] = 0xFFFF;
91 gdb_regs[GDB_GS] = 0xFFFF;
92 gdb_regs[GDB_SP] = (int)&regs->sp;
93 #else
94 gdb_regs[GDB_R8] = regs->r8;
95 gdb_regs[GDB_R9] = regs->r9;
96 gdb_regs[GDB_R10] = regs->r10;
97 gdb_regs[GDB_R11] = regs->r11;
98 gdb_regs[GDB_R12] = regs->r12;
99 gdb_regs[GDB_R13] = regs->r13;
100 gdb_regs[GDB_R14] = regs->r14;
101 gdb_regs[GDB_R15] = regs->r15;
102 gdb_regs32[GDB_PS] = regs->flags;
103 gdb_regs32[GDB_CS] = regs->cs;
104 gdb_regs32[GDB_SS] = regs->ss;
105 gdb_regs[GDB_SP] = regs->sp;
106 #endif
107 }
108
109 /**
110 * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
111 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
112 * @p: The &struct task_struct of the desired process.
113 *
114 * Convert the register values of the sleeping process in @p to
115 * the format that GDB expects.
116 * This function is called when kgdb does not have access to the
117 * &struct pt_regs and therefore it should fill the gdb registers
118 * @gdb_regs with what has been saved in &struct thread_struct
119 * thread field during switch_to.
120 */
121 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
122 {
123 #ifndef CONFIG_X86_32
124 u32 *gdb_regs32 = (u32 *)gdb_regs;
125 #endif
126 gdb_regs[GDB_AX] = 0;
127 gdb_regs[GDB_BX] = 0;
128 gdb_regs[GDB_CX] = 0;
129 gdb_regs[GDB_DX] = 0;
130 gdb_regs[GDB_SI] = 0;
131 gdb_regs[GDB_DI] = 0;
132 gdb_regs[GDB_BP] = *(unsigned long *)p->thread.sp;
133 #ifdef CONFIG_X86_32
134 gdb_regs[GDB_DS] = __KERNEL_DS;
135 gdb_regs[GDB_ES] = __KERNEL_DS;
136 gdb_regs[GDB_PS] = 0;
137 gdb_regs[GDB_CS] = __KERNEL_CS;
138 gdb_regs[GDB_PC] = p->thread.ip;
139 gdb_regs[GDB_SS] = __KERNEL_DS;
140 gdb_regs[GDB_FS] = 0xFFFF;
141 gdb_regs[GDB_GS] = 0xFFFF;
142 #else
143 gdb_regs32[GDB_PS] = *(unsigned long *)(p->thread.sp + 8);
144 gdb_regs32[GDB_CS] = __KERNEL_CS;
145 gdb_regs32[GDB_SS] = __KERNEL_DS;
146 gdb_regs[GDB_PC] = p->thread.ip;
147 gdb_regs[GDB_R8] = 0;
148 gdb_regs[GDB_R9] = 0;
149 gdb_regs[GDB_R10] = 0;
150 gdb_regs[GDB_R11] = 0;
151 gdb_regs[GDB_R12] = 0;
152 gdb_regs[GDB_R13] = 0;
153 gdb_regs[GDB_R14] = 0;
154 gdb_regs[GDB_R15] = 0;
155 #endif
156 gdb_regs[GDB_SP] = p->thread.sp;
157 }
158
159 /**
160 * gdb_regs_to_pt_regs - Convert GDB regs to ptrace regs.
161 * @gdb_regs: A pointer to hold the registers we've received from GDB.
162 * @regs: A pointer to a &struct pt_regs to hold these values in.
163 *
164 * Convert the GDB regs in @gdb_regs into the pt_regs, and store them
165 * in @regs.
166 */
167 void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
168 {
169 #ifndef CONFIG_X86_32
170 u32 *gdb_regs32 = (u32 *)gdb_regs;
171 #endif
172 regs->ax = gdb_regs[GDB_AX];
173 regs->bx = gdb_regs[GDB_BX];
174 regs->cx = gdb_regs[GDB_CX];
175 regs->dx = gdb_regs[GDB_DX];
176 regs->si = gdb_regs[GDB_SI];
177 regs->di = gdb_regs[GDB_DI];
178 regs->bp = gdb_regs[GDB_BP];
179 regs->ip = gdb_regs[GDB_PC];
180 #ifdef CONFIG_X86_32
181 regs->flags = gdb_regs[GDB_PS];
182 regs->ds = gdb_regs[GDB_DS];
183 regs->es = gdb_regs[GDB_ES];
184 regs->cs = gdb_regs[GDB_CS];
185 #else
186 regs->r8 = gdb_regs[GDB_R8];
187 regs->r9 = gdb_regs[GDB_R9];
188 regs->r10 = gdb_regs[GDB_R10];
189 regs->r11 = gdb_regs[GDB_R11];
190 regs->r12 = gdb_regs[GDB_R12];
191 regs->r13 = gdb_regs[GDB_R13];
192 regs->r14 = gdb_regs[GDB_R14];
193 regs->r15 = gdb_regs[GDB_R15];
194 regs->flags = gdb_regs32[GDB_PS];
195 regs->cs = gdb_regs32[GDB_CS];
196 regs->ss = gdb_regs32[GDB_SS];
197 #endif
198 }
199
200 static struct hw_breakpoint {
201 unsigned enabled;
202 unsigned type;
203 unsigned len;
204 unsigned long addr;
205 } breakinfo[4];
206
207 static void kgdb_correct_hw_break(void)
208 {
209 unsigned long dr7;
210 int correctit = 0;
211 int breakbit;
212 int breakno;
213
214 get_debugreg(dr7, 7);
215 for (breakno = 0; breakno < 4; breakno++) {
216 breakbit = 2 << (breakno << 1);
217 if (!(dr7 & breakbit) && breakinfo[breakno].enabled) {
218 correctit = 1;
219 dr7 |= breakbit;
220 dr7 &= ~(0xf0000 << (breakno << 2));
221 dr7 |= ((breakinfo[breakno].len << 2) |
222 breakinfo[breakno].type) <<
223 ((breakno << 2) + 16);
224 if (breakno >= 0 && breakno <= 3)
225 set_debugreg(breakinfo[breakno].addr, breakno);
226
227 } else {
228 if ((dr7 & breakbit) && !breakinfo[breakno].enabled) {
229 correctit = 1;
230 dr7 &= ~breakbit;
231 dr7 &= ~(0xf0000 << (breakno << 2));
232 }
233 }
234 }
235 if (correctit)
236 set_debugreg(dr7, 7);
237 }
238
239 static int
240 kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
241 {
242 int i;
243
244 for (i = 0; i < 4; i++)
245 if (breakinfo[i].addr == addr && breakinfo[i].enabled)
246 break;
247 if (i == 4)
248 return -1;
249
250 breakinfo[i].enabled = 0;
251
252 return 0;
253 }
254
255 static void kgdb_remove_all_hw_break(void)
256 {
257 int i;
258
259 for (i = 0; i < 4; i++)
260 memset(&breakinfo[i], 0, sizeof(struct hw_breakpoint));
261 }
262
263 static int
264 kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
265 {
266 unsigned type;
267 int i;
268
269 for (i = 0; i < 4; i++)
270 if (!breakinfo[i].enabled)
271 break;
272 if (i == 4)
273 return -1;
274
275 switch (bptype) {
276 case BP_HARDWARE_BREAKPOINT:
277 type = 0;
278 len = 1;
279 break;
280 case BP_WRITE_WATCHPOINT:
281 type = 1;
282 break;
283 case BP_ACCESS_WATCHPOINT:
284 type = 3;
285 break;
286 default:
287 return -1;
288 }
289
290 if (len == 1 || len == 2 || len == 4)
291 breakinfo[i].len = len - 1;
292 else
293 return -1;
294
295 breakinfo[i].enabled = 1;
296 breakinfo[i].addr = addr;
297 breakinfo[i].type = type;
298
299 return 0;
300 }
301
302 /**
303 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
304 * @regs: Current &struct pt_regs.
305 *
306 * This function will be called if the particular architecture must
307 * disable hardware debugging while it is processing gdb packets or
308 * handling exception.
309 */
310 void kgdb_disable_hw_debug(struct pt_regs *regs)
311 {
312 /* Disable hardware debugging while we are in kgdb: */
313 set_debugreg(0UL, 7);
314 }
315
316 /**
317 * kgdb_post_primary_code - Save error vector/code numbers.
318 * @regs: Original pt_regs.
319 * @e_vector: Original error vector.
320 * @err_code: Original error code.
321 *
322 * This is needed on architectures which support SMP and KGDB.
323 * This function is called after all the slave cpus have been put
324 * to a know spin state and the primary CPU has control over KGDB.
325 */
326 void kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
327 {
328 /* primary processor is completely in the debugger */
329 gdb_x86vector = e_vector;
330 gdb_x86errcode = err_code;
331 }
332
333 #ifdef CONFIG_SMP
334 /**
335 * kgdb_roundup_cpus - Get other CPUs into a holding pattern
336 * @flags: Current IRQ state
337 *
338 * On SMP systems, we need to get the attention of the other CPUs
339 * and get them be in a known state. This should do what is needed
340 * to get the other CPUs to call kgdb_wait(). Note that on some arches,
341 * the NMI approach is not used for rounding up all the CPUs. For example,
342 * in case of MIPS, smp_call_function() is used to roundup CPUs. In
343 * this case, we have to make sure that interrupts are enabled before
344 * calling smp_call_function(). The argument to this function is
345 * the flags that will be used when restoring the interrupts. There is
346 * local_irq_save() call before kgdb_roundup_cpus().
347 *
348 * On non-SMP systems, this is not called.
349 */
350 void kgdb_roundup_cpus(unsigned long flags)
351 {
352 apic->send_IPI_allbutself(APIC_DM_NMI);
353 }
354 #endif
355
356 /**
357 * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
358 * @vector: The error vector of the exception that happened.
359 * @signo: The signal number of the exception that happened.
360 * @err_code: The error code of the exception that happened.
361 * @remcom_in_buffer: The buffer of the packet we have read.
362 * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
363 * @regs: The &struct pt_regs of the current process.
364 *
365 * This function MUST handle the 'c' and 's' command packets,
366 * as well packets to set / remove a hardware breakpoint, if used.
367 * If there are additional packets which the hardware needs to handle,
368 * they are handled here. The code should return -1 if it wants to
369 * process more packets, and a %0 or %1 if it wants to exit from the
370 * kgdb callback.
371 */
372 int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
373 char *remcomInBuffer, char *remcomOutBuffer,
374 struct pt_regs *linux_regs)
375 {
376 unsigned long addr;
377 unsigned long dr6;
378 char *ptr;
379 int newPC;
380
381 switch (remcomInBuffer[0]) {
382 case 'c':
383 case 's':
384 /* try to read optional parameter, pc unchanged if no parm */
385 ptr = &remcomInBuffer[1];
386 if (kgdb_hex2long(&ptr, &addr))
387 linux_regs->ip = addr;
388 case 'D':
389 case 'k':
390 newPC = linux_regs->ip;
391
392 /* clear the trace bit */
393 linux_regs->flags &= ~X86_EFLAGS_TF;
394 atomic_set(&kgdb_cpu_doing_single_step, -1);
395
396 /* set the trace bit if we're stepping */
397 if (remcomInBuffer[0] == 's') {
398 linux_regs->flags |= X86_EFLAGS_TF;
399 kgdb_single_step = 1;
400 atomic_set(&kgdb_cpu_doing_single_step,
401 raw_smp_processor_id());
402 }
403
404 get_debugreg(dr6, 6);
405 if (!(dr6 & 0x4000)) {
406 int breakno;
407
408 for (breakno = 0; breakno < 4; breakno++) {
409 if (dr6 & (1 << breakno) &&
410 breakinfo[breakno].type == 0) {
411 /* Set restore flag: */
412 linux_regs->flags |= X86_EFLAGS_RF;
413 break;
414 }
415 }
416 }
417 set_debugreg(0UL, 6);
418 kgdb_correct_hw_break();
419
420 return 0;
421 }
422
423 /* this means that we do not want to exit from the handler: */
424 return -1;
425 }
426
427 static inline int
428 single_step_cont(struct pt_regs *regs, struct die_args *args)
429 {
430 /*
431 * Single step exception from kernel space to user space so
432 * eat the exception and continue the process:
433 */
434 printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
435 "resuming...\n");
436 kgdb_arch_handle_exception(args->trapnr, args->signr,
437 args->err, "c", "", regs);
438 /*
439 * Reset the BS bit in dr6 (pointed by args->err) to
440 * denote completion of processing
441 */
442 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
443
444 return NOTIFY_STOP;
445 }
446
447 static int was_in_debug_nmi[NR_CPUS];
448
449 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
450 {
451 struct pt_regs *regs = args->regs;
452
453 switch (cmd) {
454 case DIE_NMI:
455 if (atomic_read(&kgdb_active) != -1) {
456 /* KGDB CPU roundup */
457 kgdb_nmicallback(raw_smp_processor_id(), regs);
458 was_in_debug_nmi[raw_smp_processor_id()] = 1;
459 touch_nmi_watchdog();
460 return NOTIFY_STOP;
461 }
462 return NOTIFY_DONE;
463
464 case DIE_NMI_IPI:
465 /* Just ignore, we will handle the roundup on DIE_NMI. */
466 return NOTIFY_DONE;
467
468 case DIE_NMIUNKNOWN:
469 if (was_in_debug_nmi[raw_smp_processor_id()]) {
470 was_in_debug_nmi[raw_smp_processor_id()] = 0;
471 return NOTIFY_STOP;
472 }
473 return NOTIFY_DONE;
474
475 case DIE_NMIWATCHDOG:
476 if (atomic_read(&kgdb_active) != -1) {
477 /* KGDB CPU roundup: */
478 kgdb_nmicallback(raw_smp_processor_id(), regs);
479 return NOTIFY_STOP;
480 }
481 /* Enter debugger: */
482 break;
483
484 case DIE_DEBUG:
485 if (atomic_read(&kgdb_cpu_doing_single_step) ==
486 raw_smp_processor_id()) {
487 if (user_mode(regs))
488 return single_step_cont(regs, args);
489 break;
490 } else if (test_thread_flag(TIF_SINGLESTEP))
491 /* This means a user thread is single stepping
492 * a system call which should be ignored
493 */
494 return NOTIFY_DONE;
495 /* fall through */
496 default:
497 if (user_mode(regs))
498 return NOTIFY_DONE;
499 }
500
501 if (kgdb_handle_exception(args->trapnr, args->signr, args->err, regs))
502 return NOTIFY_DONE;
503
504 /* Must touch watchdog before return to normal operation */
505 touch_nmi_watchdog();
506 return NOTIFY_STOP;
507 }
508
509 static int
510 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
511 {
512 unsigned long flags;
513 int ret;
514
515 local_irq_save(flags);
516 ret = __kgdb_notify(ptr, cmd);
517 local_irq_restore(flags);
518
519 return ret;
520 }
521
522 static struct notifier_block kgdb_notifier = {
523 .notifier_call = kgdb_notify,
524
525 /*
526 * Lowest-prio notifier priority, we want to be notified last:
527 */
528 .priority = -INT_MAX,
529 };
530
531 /**
532 * kgdb_arch_init - Perform any architecture specific initalization.
533 *
534 * This function will handle the initalization of any architecture
535 * specific callbacks.
536 */
537 int kgdb_arch_init(void)
538 {
539 return register_die_notifier(&kgdb_notifier);
540 }
541
542 /**
543 * kgdb_arch_exit - Perform any architecture specific uninitalization.
544 *
545 * This function will handle the uninitalization of any architecture
546 * specific callbacks, for dynamic registration and unregistration.
547 */
548 void kgdb_arch_exit(void)
549 {
550 unregister_die_notifier(&kgdb_notifier);
551 }
552
553 /**
554 *
555 * kgdb_skipexception - Bail out of KGDB when we've been triggered.
556 * @exception: Exception vector number
557 * @regs: Current &struct pt_regs.
558 *
559 * On some architectures we need to skip a breakpoint exception when
560 * it occurs after a breakpoint has been removed.
561 *
562 * Skip an int3 exception when it occurs after a breakpoint has been
563 * removed. Backtrack eip by 1 since the int3 would have caused it to
564 * increment by 1.
565 */
566 int kgdb_skipexception(int exception, struct pt_regs *regs)
567 {
568 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
569 regs->ip -= 1;
570 return 1;
571 }
572 return 0;
573 }
574
575 unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
576 {
577 if (exception == 3)
578 return instruction_pointer(regs) - 1;
579 return instruction_pointer(regs);
580 }
581
582 struct kgdb_arch arch_kgdb_ops = {
583 /* Breakpoint instruction: */
584 .gdb_bpt_instr = { 0xcc },
585 .flags = KGDB_HW_BREAKPOINT,
586 .set_hw_breakpoint = kgdb_set_hw_break,
587 .remove_hw_breakpoint = kgdb_remove_hw_break,
588 .remove_all_hw_break = kgdb_remove_all_hw_break,
589 .correct_hw_break = kgdb_correct_hw_break,
590 };