blob: 93d3a0ec5e112138fe46f7f3fea5c0882da20468 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// 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#include <linux/kernel.h>
12#include <linux/ctype.h>
13#include <linux/kgdb.h>
14#include <linux/kdb.h>
15#include <linux/tty.h>
16#include <linux/console.h>
17#include <linux/vt_kern.h>
18#include <linux/input.h>
19#include <linux/module.h>
20
21#define MAX_CONFIG_LEN 40
22
23static struct kgdb_io kgdboc_io_ops;
24
25/* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
26static int configured = -1;
27
28static char config[MAX_CONFIG_LEN];
29static struct kparam_string kps = {
30 .string = config,
31 .maxlen = MAX_CONFIG_LEN,
32};
33
34static int kgdboc_use_kms; /* 1 if we use kernel mode switching */
35static struct tty_driver *kgdb_tty_driver;
36static int kgdb_tty_line;
37
38#ifdef CONFIG_KDB_KEYBOARD
39static int kgdboc_reset_connect(struct input_handler *handler,
40 struct input_dev *dev,
41 const struct input_device_id *id)
42{
43 input_reset_device(dev);
44
45 /* Return an error - we do not want to bind, just to reset */
46 return -ENODEV;
47}
48
49static void kgdboc_reset_disconnect(struct input_handle *handle)
50{
51 /* We do not expect anyone to actually bind to us */
52 BUG();
53}
54
55static const struct input_device_id kgdboc_reset_ids[] = {
56 {
57 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
58 .evbit = { BIT_MASK(EV_KEY) },
59 },
60 { }
61};
62
63static struct input_handler kgdboc_reset_handler = {
64 .connect = kgdboc_reset_connect,
65 .disconnect = kgdboc_reset_disconnect,
66 .name = "kgdboc_reset",
67 .id_table = kgdboc_reset_ids,
68};
69
70static DEFINE_MUTEX(kgdboc_reset_mutex);
71
72static void kgdboc_restore_input_helper(struct work_struct *dummy)
73{
74 /*
75 * We need to take a mutex to prevent several instances of
76 * this work running on different CPUs so they don't try
77 * to register again already registered handler.
78 */
79 mutex_lock(&kgdboc_reset_mutex);
80
81 if (input_register_handler(&kgdboc_reset_handler) == 0)
82 input_unregister_handler(&kgdboc_reset_handler);
83
84 mutex_unlock(&kgdboc_reset_mutex);
85}
86
87static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
88
89static void kgdboc_restore_input(void)
90{
91 if (likely(system_state == SYSTEM_RUNNING))
92 schedule_work(&kgdboc_restore_input_work);
93}
94
95static int kgdboc_register_kbd(char **cptr)
96{
97 if (strncmp(*cptr, "kbd", 3) == 0 ||
98 strncmp(*cptr, "kdb", 3) == 0) {
99 if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
100 kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
101 kdb_poll_idx++;
102 if (cptr[0][3] == ',')
103 *cptr += 4;
104 else
105 return 1;
106 }
107 }
108 return 0;
109}
110
111static void kgdboc_unregister_kbd(void)
112{
113 int i;
114
115 for (i = 0; i < kdb_poll_idx; i++) {
116 if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
117 kdb_poll_idx--;
118 kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
119 kdb_poll_funcs[kdb_poll_idx] = NULL;
120 i--;
121 }
122 }
123 flush_work(&kgdboc_restore_input_work);
124}
125#else /* ! CONFIG_KDB_KEYBOARD */
126#define kgdboc_register_kbd(x) 0
127#define kgdboc_unregister_kbd()
128#define kgdboc_restore_input()
129#endif /* ! CONFIG_KDB_KEYBOARD */
130
131static void cleanup_kgdboc(void)
132{
133 if (kgdb_unregister_nmi_console())
134 return;
135 kgdboc_unregister_kbd();
136 if (configured == 1)
137 kgdb_unregister_io_module(&kgdboc_io_ops);
138}
139
140static int configure_kgdboc(void)
141{
142 struct tty_driver *p;
143 int tty_line = 0;
144 int err = -ENODEV;
145 char *cptr = config;
146 struct console *cons;
147
148 if (!strlen(config) || isspace(config[0]))
149 goto noconfig;
150
151 kgdboc_io_ops.is_console = 0;
152 kgdb_tty_driver = NULL;
153
154 kgdboc_use_kms = 0;
155 if (strncmp(cptr, "kms,", 4) == 0) {
156 cptr += 4;
157 kgdboc_use_kms = 1;
158 }
159
160 if (kgdboc_register_kbd(&cptr))
161 goto do_register;
162
163 p = tty_find_polling_driver(cptr, &tty_line);
164 if (!p)
165 goto noconfig;
166
167 cons = console_drivers;
168 while (cons) {
169 int idx;
170 if (cons->device && cons->device(cons, &idx) == p &&
171 idx == tty_line) {
172 kgdboc_io_ops.is_console = 1;
173 break;
174 }
175 cons = cons->next;
176 }
177
178 kgdb_tty_driver = p;
179 kgdb_tty_line = tty_line;
180
181do_register:
182 err = kgdb_register_io_module(&kgdboc_io_ops);
183 if (err)
184 goto noconfig;
185
186 err = kgdb_register_nmi_console();
187 if (err)
188 goto nmi_con_failed;
189
190 configured = 1;
191
192 return 0;
193
194nmi_con_failed:
195 kgdb_unregister_io_module(&kgdboc_io_ops);
196noconfig:
197 kgdboc_unregister_kbd();
198 config[0] = 0;
199 configured = 0;
200 cleanup_kgdboc();
201
202 return err;
203}
204
205static int __init init_kgdboc(void)
206{
207 /* Already configured? */
208 if (configured == 1)
209 return 0;
210
211 return configure_kgdboc();
212}
213
214static int kgdboc_get_char(void)
215{
216 if (!kgdb_tty_driver)
217 return -1;
218 return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
219 kgdb_tty_line);
220}
221
222static void kgdboc_put_char(u8 chr)
223{
224 if (!kgdb_tty_driver)
225 return;
226 kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
227 kgdb_tty_line, chr);
228}
229
230static int param_set_kgdboc_var(const char *kmessage,
231 const struct kernel_param *kp)
232{
233 size_t len = strlen(kmessage);
234
235 if (len >= MAX_CONFIG_LEN) {
236 printk(KERN_ERR "kgdboc: config string too long\n");
237 return -ENOSPC;
238 }
239
240 /* Only copy in the string if the init function has not run yet */
241 if (configured < 0) {
242 strcpy(config, kmessage);
243 return 0;
244 }
245
246 if (kgdb_connected) {
247 printk(KERN_ERR
248 "kgdboc: Cannot reconfigure while KGDB is connected.\n");
249
250 return -EBUSY;
251 }
252
253 strcpy(config, kmessage);
254 /* Chop out \n char as a result of echo */
255 if (len && config[len - 1] == '\n')
256 config[len - 1] = '\0';
257
258 if (configured == 1)
259 cleanup_kgdboc();
260
261 /* Go and configure with the new params. */
262 return configure_kgdboc();
263}
264
265static int dbg_restore_graphics;
266
267static void kgdboc_pre_exp_handler(void)
268{
269 if (!dbg_restore_graphics && kgdboc_use_kms) {
270 dbg_restore_graphics = 1;
271 con_debug_enter(vc_cons[fg_console].d);
272 }
273 /* Increment the module count when the debugger is active */
274 if (!kgdb_connected)
275 try_module_get(THIS_MODULE);
276}
277
278static void kgdboc_post_exp_handler(void)
279{
280 /* decrement the module count when the debugger detaches */
281 if (!kgdb_connected)
282 module_put(THIS_MODULE);
283 if (kgdboc_use_kms && dbg_restore_graphics) {
284 dbg_restore_graphics = 0;
285 con_debug_leave();
286 }
287 kgdboc_restore_input();
288}
289
290static struct kgdb_io kgdboc_io_ops = {
291 .name = "kgdboc",
292 .read_char = kgdboc_get_char,
293 .write_char = kgdboc_put_char,
294 .pre_exception = kgdboc_pre_exp_handler,
295 .post_exception = kgdboc_post_exp_handler,
296};
297
298#ifdef CONFIG_KGDB_SERIAL_CONSOLE
299static int kgdboc_option_setup(char *opt)
300{
301 if (!opt) {
302 pr_err("config string not provided\n");
303 return -EINVAL;
304 }
305
306 if (strlen(opt) >= MAX_CONFIG_LEN) {
307 pr_err("config string too long\n");
308 return -ENOSPC;
309 }
310 strcpy(config, opt);
311
312 return 0;
313}
314
315__setup("kgdboc=", kgdboc_option_setup);
316
317
318/* This is only available if kgdboc is a built in for early debugging */
319static int __init kgdboc_early_init(char *opt)
320{
321 /* save the first character of the config string because the
322 * init routine can destroy it.
323 */
324 char save_ch;
325
326 kgdboc_option_setup(opt);
327 save_ch = config[0];
328 init_kgdboc();
329 config[0] = save_ch;
330 return 0;
331}
332
333early_param("ekgdboc", kgdboc_early_init);
334#endif /* CONFIG_KGDB_SERIAL_CONSOLE */
335
336module_init(init_kgdboc);
337module_exit(cleanup_kgdboc);
338module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
339MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
340MODULE_DESCRIPTION("KGDB Console TTY Driver");
341MODULE_LICENSE("GPL");