Skip to content

Commit 4fe1da4

Browse files
committed
echi-dbgp: Add kernel debugger support for the usb debug port
This patch adds the capability to use the usb debug port with the kernel debugger. It is also still possible to use this functionality with or without the earlyprintk=dbgpX. It is possible to use the kgdbwait boot argument to debug very early in the kernel start up code. There are two ways to use this driver extension with a kernel boot argument. 1) kgdbdbgp=# -- Where # is the number of the usb debug controller You must use sysrq-g to break into the kernel debugger on another connection type other than the dbgp. 2) kgdbdbgp=#debugControlNum#,#Seconds# In this mode, the usb debug port is polled every #Seconds# for character input. It is possible to use gdb or press control-c to break into the kernel debugger. From the implementation perspective there are 3 high level changes. 1) Allow variable retries for the the hardware via dbgp_bulk_read(). The amount of retries for the dbgp_bulk_read() needed to be variable instead of fixed. We do not want to poll at all when the kernel is operating in interrupt driven mode. The polling only occurs if the kernel was booted when specifying some number of seconds via the kgdbdbgp boot argument (IE kgdbdbgp=0,1). In this case the loop count is reduced to 1 so as introduce the smallest amount of latency as possible. 2) Save the bulk IN endpoint address for use by the kgdb code. 3) The addition of the kgdb interface code. This consisted of adding in a character read function for the dbgp as well as a polling thread to allow the dbgp to interrupt the kernel execution. The rest is the typical kgdb I/O api. CC: Eric Biederman <ebiederm@xmission.com> CC: Yinghai Lu <yhlu.kernel@gmail.com> CC: linux-usb@vger.kernel.org Signed-off-by: Jason Wessel <jason.wessel@windriver.com> Acked-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent 61eaf53 commit 4fe1da4

2 files changed

Lines changed: 122 additions & 9 deletions

File tree

‎Documentation/kernel-parameters.txt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,17 @@ and is between 256 and 4096 characters. It is defined in the file
11271127
use the HighMem zone if it exists, and the Normal
11281128
zone if it does not.
11291129

1130+
kgdbdbgp= [KGDB,HW] kgdb over EHCI usb debug port.
1131+
Format: <Controller#>[,poll interval]
1132+
The controller # is the number of the ehci usb debug
1133+
port as it is probed via PCI. The poll interval is
1134+
optional and is the number seconds in between
1135+
each poll cycle to the debug port in case you need
1136+
the functionality for interrupting the kernel with
1137+
gdb or control-c on the dbgp connection. When
1138+
not using this parameter you use sysrq-g to break into
1139+
the kernel debugger.
1140+
11301141
kgdboc= [KGDB,HW] kgdb over consoles.
11311142
Requires a tty driver that supports console polling,
11321143
or a supported polling keyboard driver (non-usb).

‎drivers/usb/early/ehci-dbgp.c‎

Lines changed: 111 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#include <linux/usb/ch9.h>
2020
#include <linux/usb/ehci_def.h>
2121
#include <linux/delay.h>
22+
#include <linux/serial_core.h>
23+
#include <linux/kgdb.h>
24+
#include <linux/kthread.h>
2225
#include <asm/io.h>
2326
#include <asm/pci-direct.h>
2427
#include <asm/fixmap.h>
@@ -55,6 +58,7 @@ static struct ehci_regs __iomem *ehci_regs;
5558
static struct ehci_dbg_port __iomem *ehci_debug;
5659
static int dbgp_not_safe; /* Cannot use debug device during ehci reset */
5760
static unsigned int dbgp_endpoint_out;
61+
static unsigned int dbgp_endpoint_in;
5862

5963
struct ehci_dev {
6064
u32 bus;
@@ -91,6 +95,13 @@ static inline u32 dbgp_len_update(u32 x, u32 len)
9195
return (x & ~0x0f) | (len & 0x0f);
9296
}
9397

98+
#ifdef CONFIG_KGDB
99+
static struct kgdb_io kgdbdbgp_io_ops;
100+
#define dbgp_kgdb_mode (dbg_io_ops == &kgdbdbgp_io_ops)
101+
#else
102+
#define dbgp_kgdb_mode (0)
103+
#endif
104+
94105
/*
95106
* USB Packet IDs (PIDs)
96107
*/
@@ -182,11 +193,10 @@ static void dbgp_breath(void)
182193
/* Sleep to give the debug port a chance to breathe */
183194
}
184195

185-
static int dbgp_wait_until_done(unsigned ctrl)
196+
static int dbgp_wait_until_done(unsigned ctrl, int loop)
186197
{
187198
u32 pids, lpid;
188199
int ret;
189-
int loop = DBGP_LOOPS;
190200

191201
retry:
192202
writel(ctrl | DBGP_GO, &ehci_debug->control);
@@ -276,13 +286,13 @@ static int dbgp_bulk_write(unsigned devnum, unsigned endpoint,
276286
dbgp_set_data(bytes, size);
277287
writel(addr, &ehci_debug->address);
278288
writel(pids, &ehci_debug->pids);
279-
ret = dbgp_wait_until_done(ctrl);
289+
ret = dbgp_wait_until_done(ctrl, DBGP_LOOPS);
280290

281291
return ret;
282292
}
283293

284294
static int dbgp_bulk_read(unsigned devnum, unsigned endpoint, void *data,
285-
int size)
295+
int size, int loops)
286296
{
287297
u32 pids, addr, ctrl;
288298
int ret;
@@ -302,7 +312,7 @@ static int dbgp_bulk_read(unsigned devnum, unsigned endpoint, void *data,
302312

303313
writel(addr, &ehci_debug->address);
304314
writel(pids, &ehci_debug->pids);
305-
ret = dbgp_wait_until_done(ctrl);
315+
ret = dbgp_wait_until_done(ctrl, loops);
306316
if (ret < 0)
307317
return ret;
308318

@@ -343,12 +353,12 @@ static int dbgp_control_msg(unsigned devnum, int requesttype,
343353
dbgp_set_data(&req, sizeof(req));
344354
writel(addr, &ehci_debug->address);
345355
writel(pids, &ehci_debug->pids);
346-
ret = dbgp_wait_until_done(ctrl);
356+
ret = dbgp_wait_until_done(ctrl, DBGP_LOOPS);
347357
if (ret < 0)
348358
return ret;
349359

350360
/* Read the result */
351-
return dbgp_bulk_read(devnum, 0, data, size);
361+
return dbgp_bulk_read(devnum, 0, data, size, DBGP_LOOPS);
352362
}
353363

354364
/* Find a PCI capability */
@@ -559,6 +569,7 @@ int dbgp_external_startup(void)
559569
goto err;
560570
}
561571
dbgp_endpoint_out = dbgp_desc.bDebugOutEndpoint;
572+
dbgp_endpoint_in = dbgp_desc.bDebugInEndpoint;
562573

563574
/* Move the device to 127 if it isn't already there */
564575
if (devnum != USB_DEBUG_DEVNUM) {
@@ -968,8 +979,9 @@ int dbgp_reset_prep(void)
968979
if (!ehci_debug)
969980
return 0;
970981

971-
if (early_dbgp_console.index != -1 &&
972-
!(early_dbgp_console.flags & CON_BOOT))
982+
if ((early_dbgp_console.index != -1 &&
983+
!(early_dbgp_console.flags & CON_BOOT)) ||
984+
dbgp_kgdb_mode)
973985
return 1;
974986
/* This means the console is not initialized, or should get
975987
* shutdown so as to allow for reuse of the usb device, which
@@ -982,3 +994,93 @@ int dbgp_reset_prep(void)
982994
return 0;
983995
}
984996
EXPORT_SYMBOL_GPL(dbgp_reset_prep);
997+
998+
#ifdef CONFIG_KGDB
999+
1000+
static char kgdbdbgp_buf[DBGP_MAX_PACKET];
1001+
static int kgdbdbgp_buf_sz;
1002+
static int kgdbdbgp_buf_idx;
1003+
static int kgdbdbgp_loop_cnt = DBGP_LOOPS;
1004+
1005+
static int kgdbdbgp_read_char(void)
1006+
{
1007+
int ret;
1008+
1009+
if (kgdbdbgp_buf_idx < kgdbdbgp_buf_sz) {
1010+
char ch = kgdbdbgp_buf[kgdbdbgp_buf_idx++];
1011+
return ch;
1012+
}
1013+
1014+
ret = dbgp_bulk_read(USB_DEBUG_DEVNUM, dbgp_endpoint_in,
1015+
&kgdbdbgp_buf, DBGP_MAX_PACKET,
1016+
kgdbdbgp_loop_cnt);
1017+
if (ret <= 0)
1018+
return NO_POLL_CHAR;
1019+
kgdbdbgp_buf_sz = ret;
1020+
kgdbdbgp_buf_idx = 1;
1021+
return kgdbdbgp_buf[0];
1022+
}
1023+
1024+
static void kgdbdbgp_write_char(u8 chr)
1025+
{
1026+
early_dbgp_write(NULL, &chr, 1);
1027+
}
1028+
1029+
static struct kgdb_io kgdbdbgp_io_ops = {
1030+
.name = "kgdbdbgp",
1031+
.read_char = kgdbdbgp_read_char,
1032+
.write_char = kgdbdbgp_write_char,
1033+
};
1034+
1035+
static int kgdbdbgp_wait_time;
1036+
1037+
static int __init kgdbdbgp_parse_config(char *str)
1038+
{
1039+
char *ptr;
1040+
1041+
if (!ehci_debug) {
1042+
if (early_dbgp_init(str))
1043+
return -1;
1044+
}
1045+
ptr = strchr(str, ',');
1046+
if (ptr) {
1047+
ptr++;
1048+
kgdbdbgp_wait_time = simple_strtoul(ptr, &ptr, 10);
1049+
}
1050+
kgdb_register_io_module(&kgdbdbgp_io_ops);
1051+
kgdbdbgp_io_ops.is_console = early_dbgp_console.index != -1;
1052+
1053+
return 0;
1054+
}
1055+
early_param("kgdbdbgp", kgdbdbgp_parse_config);
1056+
1057+
static int kgdbdbgp_reader_thread(void *ptr)
1058+
{
1059+
int ret;
1060+
1061+
while (readl(&ehci_debug->control) & DBGP_ENABLED) {
1062+
kgdbdbgp_loop_cnt = 1;
1063+
ret = kgdbdbgp_read_char();
1064+
kgdbdbgp_loop_cnt = DBGP_LOOPS;
1065+
if (ret != NO_POLL_CHAR) {
1066+
if (ret == 0x3 || ret == '$') {
1067+
if (ret == '$')
1068+
kgdbdbgp_buf_idx--;
1069+
kgdb_breakpoint();
1070+
}
1071+
continue;
1072+
}
1073+
schedule_timeout_interruptible(kgdbdbgp_wait_time * HZ);
1074+
}
1075+
return 0;
1076+
}
1077+
1078+
static int __init kgdbdbgp_start_thread(void)
1079+
{
1080+
if (dbgp_kgdb_mode && kgdbdbgp_wait_time)
1081+
kthread_run(kgdbdbgp_reader_thread, NULL, "%s", "dbgp");
1082+
1083+
return 0;
1084+
}
1085+
module_init(kgdbdbgp_start_thread);
1086+
#endif /* CONFIG_KGDB */

0 commit comments

Comments
 (0)