blob: 2df77f894746d8fd245619344430c842a77553ed [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Based on the same principle as kgdboe using the NETPOLL api, this
4 * driver uses a console polling api to implement a gdb serial inteface
5 * which is multiplexed on a console port.
6 *
7 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
8 *
9 * 2007-2008 (c) Jason Wessel - Wind River Systems, Inc.
10 */
11
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14#include <linux/kernel.h>
15#include <linux/ctype.h>
16#include <linux/kgdb.h>
17#include <linux/kdb.h>
18#include <linux/tty.h>
19#include <linux/console.h>
20#include <linux/vt_kern.h>
21#include <linux/input.h>
22#include <linux/irq_work.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
25
26#define MAX_CONFIG_LEN 40
27
28static struct kgdb_io kgdboc_io_ops;
29
30/* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
31static int configured = -1;
32static DEFINE_MUTEX(config_mutex);
33
34static char config[MAX_CONFIG_LEN];
35static struct kparam_string kps = {
36 .string = config,
37 .maxlen = MAX_CONFIG_LEN,
38};
39
40static int kgdboc_use_kms; /* 1 if we use kernel mode switching */
41static struct tty_driver *kgdb_tty_driver;
42static int kgdb_tty_line;
43
44static struct platform_device *kgdboc_pdev;
45
46/*
47 * When we leave the debug trap handler we need to reset the keyboard status
48 * (since the original keyboard state gets partially clobbered by kdb use of
49 * the keyboard).
50 *
51 * The path to deliver the reset is somewhat circuitous.
52 *
53 * To deliver the reset we register an input handler, reset the keyboard and
54 * then deregister the input handler. However, to get this done right, we do
55 * have to carefully manage the calling context because we can only register
56 * input handlers from task context.
57 *
58 * In particular we need to trigger the action from the debug trap handler with
59 * all its NMI and/or NMI-like oddities. To solve this the kgdboc trap exit code
60 * (the "post_exception" callback) uses irq_work_queue(), which is NMI-safe, to
61 * schedule a callback from a hardirq context. From there we have to defer the
62 * work again, this time using schedule_work(), to get a callback using the
63 * system workqueue, which runs in task context.
64 */
65#ifdef CONFIG_KDB_KEYBOARD
66static int kgdboc_reset_connect(struct input_handler *handler,
67 struct input_dev *dev,
68 const struct input_device_id *id)
69{
70 input_reset_device(dev);
71
72 /* Return an error - we do not want to bind, just to reset */
73 return -ENODEV;
74}
75
76static void kgdboc_reset_disconnect(struct input_handle *handle)
77{
78 /* We do not expect anyone to actually bind to us */
79 BUG();
80}
81
82static const struct input_device_id kgdboc_reset_ids[] = {
83 {
84 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
85 .evbit = { BIT_MASK(EV_KEY) },
86 },
87 { }
88};
89
90static struct input_handler kgdboc_reset_handler = {
91 .connect = kgdboc_reset_connect,
92 .disconnect = kgdboc_reset_disconnect,
93 .name = "kgdboc_reset",
94 .id_table = kgdboc_reset_ids,
95};
96
97static DEFINE_MUTEX(kgdboc_reset_mutex);
98
99static void kgdboc_restore_input_helper(struct work_struct *dummy)
100{
101 /*
102 * We need to take a mutex to prevent several instances of
103 * this work running on different CPUs so they don't try
104 * to register again already registered handler.
105 */
106 mutex_lock(&kgdboc_reset_mutex);
107
108 if (input_register_handler(&kgdboc_reset_handler) == 0)
109 input_unregister_handler(&kgdboc_reset_handler);
110
111 mutex_unlock(&kgdboc_reset_mutex);
112}
113
114static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
115
116static void kgdboc_queue_restore_input_helper(struct irq_work *unused)
117{
118 schedule_work(&kgdboc_restore_input_work);
119}
120
121static DEFINE_IRQ_WORK(kgdboc_restore_input_irq_work, kgdboc_queue_restore_input_helper);
122
123static void kgdboc_restore_input(void)
124{
125 if (likely(system_state == SYSTEM_RUNNING))
126 irq_work_queue(&kgdboc_restore_input_irq_work);
127}
128
129static int kgdboc_register_kbd(char **cptr)
130{
131 if (strncmp(*cptr, "kbd", 3) == 0 ||
132 strncmp(*cptr, "kdb", 3) == 0) {
133 if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
134 kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
135 kdb_poll_idx++;
136 if (cptr[0][3] == ',')
137 *cptr += 4;
138 else
139 return 1;
140 }
141 }
142 return 0;
143}
144
145static void kgdboc_unregister_kbd(void)
146{
147 int i;
148
149 for (i = 0; i < kdb_poll_idx; i++) {
150 if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
151 kdb_poll_idx--;
152 kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
153 kdb_poll_funcs[kdb_poll_idx] = NULL;
154 i--;
155 }
156 }
157 irq_work_sync(&kgdboc_restore_input_irq_work);
158 flush_work(&kgdboc_restore_input_work);
159}
160#else /* ! CONFIG_KDB_KEYBOARD */
161#define kgdboc_register_kbd(x) 0
162#define kgdboc_unregister_kbd()
163#define kgdboc_restore_input()
164#endif /* ! CONFIG_KDB_KEYBOARD */
165
166static void cleanup_kgdboc(void)
167{
168 if (configured != 1)
169 return;
170
171 if (kgdb_unregister_nmi_console())
172 return;
173 kgdboc_unregister_kbd();
174 kgdb_unregister_io_module(&kgdboc_io_ops);
175}
176
177static int configure_kgdboc(void)
178{
179 struct tty_driver *p;
180 int tty_line = 0;
181 int err = -ENODEV;
182 char *cptr = config;
183 struct console *cons;
184
185 if (!strlen(config) || isspace(config[0])) {
186 err = 0;
187 goto noconfig;
188 }
189
190 kgdboc_io_ops.is_console = 0;
191 kgdb_tty_driver = NULL;
192
193 kgdboc_use_kms = 0;
194 if (strncmp(cptr, "kms,", 4) == 0) {
195 cptr += 4;
196 kgdboc_use_kms = 1;
197 }
198
199 if (kgdboc_register_kbd(&cptr))
200 goto do_register;
201
202 p = tty_find_polling_driver(cptr, &tty_line);
203 if (!p)
204 goto noconfig;
205
206 cons = console_drivers;
207 while (cons) {
208 int idx;
209 if (cons->device && cons->device(cons, &idx) == p &&
210 idx == tty_line) {
211 kgdboc_io_ops.is_console = 1;
212 break;
213 }
214 cons = cons->next;
215 }
216
217 kgdb_tty_driver = p;
218 kgdb_tty_line = tty_line;
219
220do_register:
221 err = kgdb_register_io_module(&kgdboc_io_ops);
222 if (err)
223 goto noconfig;
224
225 err = kgdb_register_nmi_console();
226 if (err)
227 goto nmi_con_failed;
228
229 configured = 1;
230
231 return 0;
232
233nmi_con_failed:
234 kgdb_unregister_io_module(&kgdboc_io_ops);
235noconfig:
236 kgdboc_unregister_kbd();
237 configured = 0;
238
239 return err;
240}
241
242static int kgdboc_probe(struct platform_device *pdev)
243{
244 int ret = 0;
245
246 mutex_lock(&config_mutex);
247 if (configured != 1) {
248 ret = configure_kgdboc();
249
250 /* Convert "no device" to "defer" so we'll keep trying */
251 if (ret == -ENODEV)
252 ret = -EPROBE_DEFER;
253 }
254 mutex_unlock(&config_mutex);
255
256 return ret;
257}
258
259static struct platform_driver kgdboc_platform_driver = {
260 .probe = kgdboc_probe,
261 .driver = {
262 .name = "kgdboc",
263 .suppress_bind_attrs = true,
264 },
265};
266
267static int __init init_kgdboc(void)
268{
269 int ret;
270
271 /*
272 * kgdboc is a little bit of an odd "platform_driver". It can be
273 * up and running long before the platform_driver object is
274 * created and thus doesn't actually store anything in it. There's
275 * only one instance of kgdb so anything is stored as global state.
276 * The platform_driver is only created so that we can leverage the
277 * kernel's mechanisms (like -EPROBE_DEFER) to call us when our
278 * underlying tty is ready. Here we init our platform driver and
279 * then create the single kgdboc instance.
280 */
281 ret = platform_driver_register(&kgdboc_platform_driver);
282 if (ret)
283 return ret;
284
285 kgdboc_pdev = platform_device_alloc("kgdboc", PLATFORM_DEVID_NONE);
286 if (!kgdboc_pdev) {
287 ret = -ENOMEM;
288 goto err_did_register;
289 }
290
291 ret = platform_device_add(kgdboc_pdev);
292 if (!ret)
293 return 0;
294
295 platform_device_put(kgdboc_pdev);
296
297err_did_register:
298 platform_driver_unregister(&kgdboc_platform_driver);
299 return ret;
300}
301
302static void exit_kgdboc(void)
303{
304 mutex_lock(&config_mutex);
305 cleanup_kgdboc();
306 mutex_unlock(&config_mutex);
307
308 platform_device_unregister(kgdboc_pdev);
309 platform_driver_unregister(&kgdboc_platform_driver);
310}
311
312static int kgdboc_get_char(void)
313{
314 if (!kgdb_tty_driver)
315 return -1;
316 return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
317 kgdb_tty_line);
318}
319
320static void kgdboc_put_char(u8 chr)
321{
322 if (!kgdb_tty_driver)
323 return;
324 kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
325 kgdb_tty_line, chr);
326}
327
328static int param_set_kgdboc_var(const char *kmessage,
329 const struct kernel_param *kp)
330{
331 size_t len = strlen(kmessage);
332 int ret = 0;
333
334 if (len >= MAX_CONFIG_LEN) {
335 pr_err("config string too long\n");
336 return -ENOSPC;
337 }
338
339 if (kgdb_connected) {
340 pr_err("Cannot reconfigure while KGDB is connected.\n");
341 return -EBUSY;
342 }
343
344 mutex_lock(&config_mutex);
345
346 strcpy(config, kmessage);
347 /* Chop out \n char as a result of echo */
348 if (len && config[len - 1] == '\n')
349 config[len - 1] = '\0';
350
351 if (configured == 1)
352 cleanup_kgdboc();
353
354 /*
355 * Configure with the new params as long as init already ran.
356 * Note that we can get called before init if someone loads us
357 * with "modprobe kgdboc kgdboc=..." or if they happen to use the
358 * the odd syntax of "kgdboc.kgdboc=..." on the kernel command.
359 */
360 if (configured >= 0)
361 ret = configure_kgdboc();
362
363 /*
364 * If we couldn't configure then clear out the config. Note that
365 * specifying an invalid config on the kernel command line vs.
366 * through sysfs have slightly different behaviors. If we fail
367 * to configure what was specified on the kernel command line
368 * we'll leave it in the 'config' and return -EPROBE_DEFER from
369 * our probe. When specified through sysfs userspace is
370 * responsible for loading the tty driver before setting up.
371 */
372 if (ret)
373 config[0] = '\0';
374
375 mutex_unlock(&config_mutex);
376
377 return ret;
378}
379
380static int dbg_restore_graphics;
381
382static void kgdboc_pre_exp_handler(void)
383{
384 if (!dbg_restore_graphics && kgdboc_use_kms) {
385 dbg_restore_graphics = 1;
386 con_debug_enter(vc_cons[fg_console].d);
387 }
388 /* Increment the module count when the debugger is active */
389 if (!kgdb_connected)
390 try_module_get(THIS_MODULE);
391
392 atomic_inc(&ignore_console_lock_warning);
393}
394
395static void kgdboc_post_exp_handler(void)
396{
397 atomic_dec(&ignore_console_lock_warning);
398
399 /* decrement the module count when the debugger detaches */
400 if (!kgdb_connected)
401 module_put(THIS_MODULE);
402 if (kgdboc_use_kms && dbg_restore_graphics) {
403 dbg_restore_graphics = 0;
404 con_debug_leave();
405 }
406 kgdboc_restore_input();
407}
408
409static struct kgdb_io kgdboc_io_ops = {
410 .name = "kgdboc",
411 .read_char = kgdboc_get_char,
412 .write_char = kgdboc_put_char,
413 .pre_exception = kgdboc_pre_exp_handler,
414 .post_exception = kgdboc_post_exp_handler,
415};
416
417#ifdef CONFIG_KGDB_SERIAL_CONSOLE
418static int kgdboc_option_setup(char *opt)
419{
420 if (!opt) {
421 pr_err("config string not provided\n");
422 return 1;
423 }
424
425 if (strlen(opt) >= MAX_CONFIG_LEN) {
426 pr_err("config string too long\n");
427 return 1;
428 }
429 strcpy(config, opt);
430
431 return 1;
432}
433
434__setup("kgdboc=", kgdboc_option_setup);
435
436
437/* This is only available if kgdboc is a built in for early debugging */
438static int __init kgdboc_early_init(char *opt)
439{
440 kgdboc_option_setup(opt);
441 configure_kgdboc();
442 return 0;
443}
444
445early_param("ekgdboc", kgdboc_early_init);
446#endif /* CONFIG_KGDB_SERIAL_CONSOLE */
447
448module_init(init_kgdboc);
449module_exit(exit_kgdboc);
450module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
451MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
452MODULE_DESCRIPTION("KGDB Console TTY Driver");
453MODULE_LICENSE("GPL");