Skip to content

Commit a20ae85

Browse files
committed
Merge tag 'for_linus-3.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jwessel/kgdb
Pull KGDB/KDB updates from Jason Wessel: "Fixes: - Fix KDB keyboard repeat scan codes and leaked keyboard events - Fix kernel crash with kdb_printf() for users who compile new kdb_printf()'s in early code - Return all segment registers to gdb on x86_64 Features: - KDB/KGDB hook the reboot notifier and end user can control if it stops, detaches or does nothing (updated docs as well) - Notify users who use CONFIG_DEBUG_RODATA to use hw breakpoints" * tag 'for_linus-3.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jwessel/kgdb: kdb: Add message about CONFIG_DEBUG_RODATA on failure to install breakpoint kdb: Avoid using dbg_io_ops until it is initialized kgdb,debug_core: add the ability to control the reboot notifier KDB: Fix usability issues relating to the 'enter' key. kgdb,debug-core,gdbstub: Hook the reboot notifier for debugger detach kgdb: Respect that flush op is optional kgdb: x86: Return all segment registers also in 64-bit mode
2 parents f0a5ec0 + 1ba0c17 commit a20ae85

10 files changed

Lines changed: 161 additions & 29 deletions

File tree

‎Documentation/DocBook/kgdb.tmpl‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,23 @@
361361
<para>It is possible to use this option with kgdboc on a tty that is not a system console.
362362
</para>
363363
</para>
364+
</sect1>
365+
<sect1 id="kgdbreboot">
366+
<title>Run time parameter: kgdbreboot</title>
367+
<para> The kgdbreboot feature allows you to change how the debugger
368+
deals with the reboot notification. You have 3 choices for the
369+
behavior. The default behavior is always set to 0.</para>
370+
<orderedlist>
371+
<listitem><para>echo -1 > /sys/module/debug_core/parameters/kgdbreboot</para>
372+
<para>Ignore the reboot notification entirely.</para>
373+
</listitem>
374+
<listitem><para>echo 0 > /sys/module/debug_core/parameters/kgdbreboot</para>
375+
<para>Send the detach message to any attached debugger client.</para>
376+
</listitem>
377+
<listitem><para>echo 1 > /sys/module/debug_core/parameters/kgdbreboot</para>
378+
<para>Enter the debugger on reboot notify.</para>
379+
</listitem>
380+
</orderedlist>
364381
</sect1>
365382
</chapter>
366383
<chapter id="usingKDB">

‎arch/x86/include/asm/kgdb.h‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,15 @@ enum regnames {
6464
GDB_PS, /* 17 */
6565
GDB_CS, /* 18 */
6666
GDB_SS, /* 19 */
67+
GDB_DS, /* 20 */
68+
GDB_ES, /* 21 */
69+
GDB_FS, /* 22 */
70+
GDB_GS, /* 23 */
6771
};
6872
#define GDB_ORIG_AX 57
69-
#define DBG_MAX_REG_NUM 20
70-
/* 17 64 bit regs and 3 32 bit regs */
71-
#define NUMREGBYTES ((17 * 8) + (3 * 4))
73+
#define DBG_MAX_REG_NUM 24
74+
/* 17 64 bit regs and 5 32 bit regs */
75+
#define NUMREGBYTES ((17 * 8) + (5 * 4))
7276
#endif /* ! CONFIG_X86_32 */
7377

7478
static inline void arch_kgdb_breakpoint(void)

‎arch/x86/kernel/kgdb.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
6767
{ "ss", 4, offsetof(struct pt_regs, ss) },
6868
{ "ds", 4, offsetof(struct pt_regs, ds) },
6969
{ "es", 4, offsetof(struct pt_regs, es) },
70-
{ "fs", 4, -1 },
71-
{ "gs", 4, -1 },
7270
#else
7371
{ "ax", 8, offsetof(struct pt_regs, ax) },
7472
{ "bx", 8, offsetof(struct pt_regs, bx) },
@@ -90,7 +88,11 @@ struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
9088
{ "flags", 4, offsetof(struct pt_regs, flags) },
9189
{ "cs", 4, offsetof(struct pt_regs, cs) },
9290
{ "ss", 4, offsetof(struct pt_regs, ss) },
91+
{ "ds", 4, -1 },
92+
{ "es", 4, -1 },
9393
#endif
94+
{ "fs", 4, -1 },
95+
{ "gs", 4, -1 },
9496
};
9597

9698
int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)

‎kernel/debug/debug_core.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <linux/delay.h>
4242
#include <linux/sched.h>
4343
#include <linux/sysrq.h>
44+
#include <linux/reboot.h>
4445
#include <linux/init.h>
4546
#include <linux/kgdb.h>
4647
#include <linux/kdb.h>
@@ -75,6 +76,8 @@ static int exception_level;
7576
struct kgdb_io *dbg_io_ops;
7677
static DEFINE_SPINLOCK(kgdb_registration_lock);
7778

79+
/* Action for the reboot notifiter, a global allow kdb to change it */
80+
static int kgdbreboot;
7881
/* kgdb console driver is loaded */
7982
static int kgdb_con_registered;
8083
/* determine if kgdb console output should be used */
@@ -96,6 +99,7 @@ static int __init opt_kgdb_con(char *str)
9699
early_param("kgdbcon", opt_kgdb_con);
97100

98101
module_param(kgdb_use_con, int, 0644);
102+
module_param(kgdbreboot, int, 0644);
99103

100104
/*
101105
* Holds information about breakpoints in a kernel. These breakpoints are
@@ -784,13 +788,41 @@ void __init dbg_late_init(void)
784788
kdb_init(KDB_INIT_FULL);
785789
}
786790

791+
static int
792+
dbg_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
793+
{
794+
/*
795+
* Take the following action on reboot notify depending on value:
796+
* 1 == Enter debugger
797+
* 0 == [the default] detatch debug client
798+
* -1 == Do nothing... and use this until the board resets
799+
*/
800+
switch (kgdbreboot) {
801+
case 1:
802+
kgdb_breakpoint();
803+
case -1:
804+
goto done;
805+
}
806+
if (!dbg_kdb_mode)
807+
gdbstub_exit(code);
808+
done:
809+
return NOTIFY_DONE;
810+
}
811+
812+
static struct notifier_block dbg_reboot_notifier = {
813+
.notifier_call = dbg_notify_reboot,
814+
.next = NULL,
815+
.priority = INT_MAX,
816+
};
817+
787818
static void kgdb_register_callbacks(void)
788819
{
789820
if (!kgdb_io_module_registered) {
790821
kgdb_io_module_registered = 1;
791822
kgdb_arch_init();
792823
if (!dbg_is_early)
793824
kgdb_arch_late();
825+
register_reboot_notifier(&dbg_reboot_notifier);
794826
atomic_notifier_chain_register(&panic_notifier_list,
795827
&kgdb_panic_event_nb);
796828
#ifdef CONFIG_MAGIC_SYSRQ
@@ -812,6 +844,7 @@ static void kgdb_unregister_callbacks(void)
812844
*/
813845
if (kgdb_io_module_registered) {
814846
kgdb_io_module_registered = 0;
847+
unregister_reboot_notifier(&dbg_reboot_notifier);
815848
atomic_notifier_chain_unregister(&panic_notifier_list,
816849
&kgdb_panic_event_nb);
817850
kgdb_arch_exit();

‎kernel/debug/gdbstub.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,13 @@ void gdbstub_exit(int status)
11111111
unsigned char checksum, ch, buffer[3];
11121112
int loop;
11131113

1114+
if (!kgdb_connected)
1115+
return;
1116+
kgdb_connected = 0;
1117+
1118+
if (!dbg_io_ops || dbg_kdb_mode)
1119+
return;
1120+
11141121
buffer[0] = 'W';
11151122
buffer[1] = hex_asc_hi(status);
11161123
buffer[2] = hex_asc_lo(status);
@@ -1129,5 +1136,6 @@ void gdbstub_exit(int status)
11291136
dbg_io_ops->write_char(hex_asc_lo(checksum));
11301137

11311138
/* make sure the output is flushed, lest the bootloader clobber it */
1132-
dbg_io_ops->flush();
1139+
if (dbg_io_ops->flush)
1140+
dbg_io_ops->flush();
11331141
}

‎kernel/debug/kdb/kdb_bp.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ static int _kdb_bp_install(struct pt_regs *regs, kdb_bp_t *bp)
153153
} else {
154154
kdb_printf("%s: failed to set breakpoint at 0x%lx\n",
155155
__func__, bp->bp_addr);
156+
#ifdef CONFIG_DEBUG_RODATA
157+
if (!bp->bp_type) {
158+
kdb_printf("Software breakpoints are unavailable.\n"
159+
" Change the kernel CONFIG_DEBUG_RODATA=n\n"
160+
" OR use hw breaks: help bph\n");
161+
}
162+
#endif
156163
return 1;
157164
}
158165
return 0;

‎kernel/debug/kdb/kdb_io.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ int vkdb_printf(const char *fmt, va_list ap)
689689
if (!dbg_kdb_mode && kgdb_connected) {
690690
gdbstub_msg_write(kdb_buffer, retlen);
691691
} else {
692-
if (!dbg_io_ops->is_console) {
692+
if (dbg_io_ops && !dbg_io_ops->is_console) {
693693
len = strlen(kdb_buffer);
694694
cp = kdb_buffer;
695695
while (len--) {

‎kernel/debug/kdb/kdb_keyboard.c‎

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
2626

2727
static int kbd_exists;
28+
static int kbd_last_ret;
2829

2930
/*
3031
* Check if the keyboard controller has a keypress for us.
@@ -90,8 +91,11 @@ int kdb_get_kbd_char(void)
9091
return -1;
9192
}
9293

93-
if ((scancode & 0x80) != 0)
94+
if ((scancode & 0x80) != 0) {
95+
if (scancode == 0x9c)
96+
kbd_last_ret = 0;
9497
return -1;
98+
}
9599

96100
scancode &= 0x7f;
97101

@@ -178,35 +182,82 @@ int kdb_get_kbd_char(void)
178182
return -1; /* ignore unprintables */
179183
}
180184

181-
if ((scancode & 0x7f) == 0x1c) {
182-
/*
183-
* enter key. All done. Absorb the release scancode.
184-
*/
185+
if (scancode == 0x1c) {
186+
kbd_last_ret = 1;
187+
return 13;
188+
}
189+
190+
return keychar & 0xff;
191+
}
192+
EXPORT_SYMBOL_GPL(kdb_get_kbd_char);
193+
194+
/*
195+
* Best effort cleanup of ENTER break codes on leaving KDB. Called on
196+
* exiting KDB, when we know we processed an ENTER or KP ENTER scan
197+
* code.
198+
*/
199+
void kdb_kbd_cleanup_state(void)
200+
{
201+
int scancode, scanstatus;
202+
203+
/*
204+
* Nothing to clean up, since either
205+
* ENTER was never pressed, or has already
206+
* gotten cleaned up.
207+
*/
208+
if (!kbd_last_ret)
209+
return;
210+
211+
kbd_last_ret = 0;
212+
/*
213+
* Enter key. Need to absorb the break code here, lest it gets
214+
* leaked out if we exit KDB as the result of processing 'g'.
215+
*
216+
* This has several interesting implications:
217+
* + Need to handle KP ENTER, which has break code 0xe0 0x9c.
218+
* + Need to handle repeat ENTER and repeat KP ENTER. Repeats
219+
* only get a break code at the end of the repeated
220+
* sequence. This means we can't propagate the repeated key
221+
* press, and must swallow it away.
222+
* + Need to handle possible PS/2 mouse input.
223+
* + Need to handle mashed keys.
224+
*/
225+
226+
while (1) {
185227
while ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
186-
;
228+
cpu_relax();
187229

188230
/*
189-
* Fetch the scancode
231+
* Fetch the scancode.
190232
*/
191233
scancode = inb(KBD_DATA_REG);
192234
scanstatus = inb(KBD_STATUS_REG);
193235

194-
while (scanstatus & KBD_STAT_MOUSE_OBF) {
195-
scancode = inb(KBD_DATA_REG);
196-
scanstatus = inb(KBD_STATUS_REG);
197-
}
236+
/*
237+
* Skip mouse input.
238+
*/
239+
if (scanstatus & KBD_STAT_MOUSE_OBF)
240+
continue;
198241

199-
if (scancode != 0x9c) {
200-
/*
201-
* Wasn't an enter-release, why not?
202-
*/
203-
kdb_printf("kdb: expected enter got 0x%x status 0x%x\n",
204-
scancode, scanstatus);
205-
}
242+
/*
243+
* If we see 0xe0, this is either a break code for KP
244+
* ENTER, or a repeat make for KP ENTER. Either way,
245+
* since the second byte is equivalent to an ENTER,
246+
* skip the 0xe0 and try again.
247+
*
248+
* If we see 0x1c, this must be a repeat ENTER or KP
249+
* ENTER (and we swallowed 0xe0 before). Try again.
250+
*
251+
* We can also see make and break codes for other keys
252+
* mashed before or after pressing ENTER. Thus, if we
253+
* see anything other than 0x9c, we have to try again.
254+
*
255+
* Note, if you held some key as ENTER was depressed,
256+
* that break code would get leaked out.
257+
*/
258+
if (scancode != 0x9c)
259+
continue;
206260

207-
return 13;
261+
return;
208262
}
209-
210-
return keychar & 0xff;
211263
}
212-
EXPORT_SYMBOL_GPL(kdb_get_kbd_char);

‎kernel/debug/kdb/kdb_main.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,9 @@ int kdb_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error,
14001400
if (KDB_STATE(DOING_SS))
14011401
KDB_STATE_CLEAR(SSBPT);
14021402

1403+
/* Clean up any keyboard devices before leaving */
1404+
kdb_kbd_cleanup_state();
1405+
14031406
return result;
14041407
}
14051408

‎kernel/debug/kdb/kdb_private.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,13 @@ extern void debug_kusage(void);
246246

247247
extern void kdb_set_current_task(struct task_struct *);
248248
extern struct task_struct *kdb_current_task;
249+
250+
#ifdef CONFIG_KDB_KEYBOARD
251+
extern void kdb_kbd_cleanup_state(void);
252+
#else /* ! CONFIG_KDB_KEYBOARD */
253+
#define kdb_kbd_cleanup_state()
254+
#endif /* ! CONFIG_KDB_KEYBOARD */
255+
249256
#ifdef CONFIG_MODULES
250257
extern struct list_head *kdb_modules;
251258
#endif /* CONFIG_MODULES */

0 commit comments

Comments
 (0)