kgdboc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Based on the same principle as kgdboe using the NETPOLL api, this
  3. * driver uses a console polling api to implement a gdb serial inteface
  4. * which is multiplexed on a console port.
  5. *
  6. * Maintainer: Jason Wessel <jason.wessel@windriver.com>
  7. *
  8. * 2007-2008 (c) Jason Wessel - Wind River Systems, Inc.
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  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/module.h>
  23. #define MAX_CONFIG_LEN 40
  24. static struct kgdb_io kgdboc_io_ops;
  25. /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
  26. static int configured = -1;
  27. static char config[MAX_CONFIG_LEN];
  28. static struct kparam_string kps = {
  29. .string = config,
  30. .maxlen = MAX_CONFIG_LEN,
  31. };
  32. static int kgdboc_use_kms; /* 1 if we use kernel mode switching */
  33. static struct tty_driver *kgdb_tty_driver;
  34. static int kgdb_tty_line;
  35. #ifdef CONFIG_KDB_KEYBOARD
  36. static int kgdboc_reset_connect(struct input_handler *handler,
  37. struct input_dev *dev,
  38. const struct input_device_id *id)
  39. {
  40. input_reset_device(dev);
  41. /* Return an error - we do not want to bind, just to reset */
  42. return -ENODEV;
  43. }
  44. static void kgdboc_reset_disconnect(struct input_handle *handle)
  45. {
  46. /* We do not expect anyone to actually bind to us */
  47. BUG();
  48. }
  49. static const struct input_device_id kgdboc_reset_ids[] = {
  50. {
  51. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  52. .evbit = { BIT_MASK(EV_KEY) },
  53. },
  54. { }
  55. };
  56. static struct input_handler kgdboc_reset_handler = {
  57. .connect = kgdboc_reset_connect,
  58. .disconnect = kgdboc_reset_disconnect,
  59. .name = "kgdboc_reset",
  60. .id_table = kgdboc_reset_ids,
  61. };
  62. static DEFINE_MUTEX(kgdboc_reset_mutex);
  63. static void kgdboc_restore_input_helper(struct work_struct *dummy)
  64. {
  65. /*
  66. * We need to take a mutex to prevent several instances of
  67. * this work running on different CPUs so they don't try
  68. * to register again already registered handler.
  69. */
  70. mutex_lock(&kgdboc_reset_mutex);
  71. if (input_register_handler(&kgdboc_reset_handler) == 0)
  72. input_unregister_handler(&kgdboc_reset_handler);
  73. mutex_unlock(&kgdboc_reset_mutex);
  74. }
  75. static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
  76. static void kgdboc_restore_input(void)
  77. {
  78. if (likely(system_state == SYSTEM_RUNNING))
  79. schedule_work(&kgdboc_restore_input_work);
  80. }
  81. static int kgdboc_register_kbd(char **cptr)
  82. {
  83. if (strncmp(*cptr, "kbd", 3) == 0 ||
  84. strncmp(*cptr, "kdb", 3) == 0) {
  85. if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
  86. kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
  87. kdb_poll_idx++;
  88. if (cptr[0][3] == ',')
  89. *cptr += 4;
  90. else
  91. return 1;
  92. }
  93. }
  94. return 0;
  95. }
  96. static void kgdboc_unregister_kbd(void)
  97. {
  98. int i;
  99. for (i = 0; i < kdb_poll_idx; i++) {
  100. if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
  101. kdb_poll_idx--;
  102. kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
  103. kdb_poll_funcs[kdb_poll_idx] = NULL;
  104. i--;
  105. }
  106. }
  107. flush_work(&kgdboc_restore_input_work);
  108. }
  109. #else /* ! CONFIG_KDB_KEYBOARD */
  110. #define kgdboc_register_kbd(x) 0
  111. #define kgdboc_unregister_kbd()
  112. #define kgdboc_restore_input()
  113. #endif /* ! CONFIG_KDB_KEYBOARD */
  114. static int kgdboc_option_setup(char *opt)
  115. {
  116. if (strlen(opt) >= MAX_CONFIG_LEN) {
  117. printk(KERN_ERR "kgdboc: config string too long\n");
  118. return -ENOSPC;
  119. }
  120. strcpy(config, opt);
  121. return 0;
  122. }
  123. __setup("kgdboc=", kgdboc_option_setup);
  124. static void cleanup_kgdboc(void)
  125. {
  126. if (kgdb_unregister_nmi_console())
  127. return;
  128. kgdboc_unregister_kbd();
  129. if (configured == 1)
  130. kgdb_unregister_io_module(&kgdboc_io_ops);
  131. }
  132. static int configure_kgdboc(void)
  133. {
  134. struct tty_driver *p;
  135. int tty_line = 0;
  136. int err;
  137. char *cptr = config;
  138. struct console *cons;
  139. err = kgdboc_option_setup(config);
  140. if (err || !strlen(config) || isspace(config[0]))
  141. goto noconfig;
  142. err = -ENODEV;
  143. kgdboc_io_ops.is_console = 0;
  144. kgdb_tty_driver = NULL;
  145. kgdboc_use_kms = 0;
  146. if (strncmp(cptr, "kms,", 4) == 0) {
  147. cptr += 4;
  148. kgdboc_use_kms = 1;
  149. }
  150. if (kgdboc_register_kbd(&cptr))
  151. goto do_register;
  152. p = tty_find_polling_driver(cptr, &tty_line);
  153. if (!p)
  154. goto noconfig;
  155. cons = console_drivers;
  156. while (cons) {
  157. int idx;
  158. if (cons->device && cons->device(cons, &idx) == p &&
  159. idx == tty_line) {
  160. kgdboc_io_ops.is_console = 1;
  161. break;
  162. }
  163. cons = cons->next;
  164. }
  165. kgdb_tty_driver = p;
  166. kgdb_tty_line = tty_line;
  167. do_register:
  168. err = kgdb_register_io_module(&kgdboc_io_ops);
  169. if (err)
  170. goto noconfig;
  171. err = kgdb_register_nmi_console();
  172. if (err)
  173. goto nmi_con_failed;
  174. configured = 1;
  175. return 0;
  176. nmi_con_failed:
  177. kgdb_unregister_io_module(&kgdboc_io_ops);
  178. noconfig:
  179. kgdboc_unregister_kbd();
  180. config[0] = 0;
  181. configured = 0;
  182. cleanup_kgdboc();
  183. return err;
  184. }
  185. static int __init init_kgdboc(void)
  186. {
  187. /* Already configured? */
  188. if (configured == 1)
  189. return 0;
  190. return configure_kgdboc();
  191. }
  192. static int kgdboc_get_char(void)
  193. {
  194. if (!kgdb_tty_driver)
  195. return -1;
  196. return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
  197. kgdb_tty_line);
  198. }
  199. static void kgdboc_put_char(u8 chr)
  200. {
  201. if (!kgdb_tty_driver)
  202. return;
  203. kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
  204. kgdb_tty_line, chr);
  205. }
  206. static int param_set_kgdboc_var(const char *kmessage, struct kernel_param *kp)
  207. {
  208. int len = strlen(kmessage);
  209. if (len >= MAX_CONFIG_LEN) {
  210. printk(KERN_ERR "kgdboc: config string too long\n");
  211. return -ENOSPC;
  212. }
  213. /* Only copy in the string if the init function has not run yet */
  214. if (configured < 0) {
  215. strcpy(config, kmessage);
  216. return 0;
  217. }
  218. if (kgdb_connected) {
  219. printk(KERN_ERR
  220. "kgdboc: Cannot reconfigure while KGDB is connected.\n");
  221. return -EBUSY;
  222. }
  223. strcpy(config, kmessage);
  224. /* Chop out \n char as a result of echo */
  225. if (config[len - 1] == '\n')
  226. config[len - 1] = '\0';
  227. if (configured == 1)
  228. cleanup_kgdboc();
  229. /* Go and configure with the new params. */
  230. return configure_kgdboc();
  231. }
  232. static int dbg_restore_graphics;
  233. static void kgdboc_pre_exp_handler(void)
  234. {
  235. if (!dbg_restore_graphics && kgdboc_use_kms) {
  236. dbg_restore_graphics = 1;
  237. con_debug_enter(vc_cons[fg_console].d);
  238. }
  239. /* Increment the module count when the debugger is active */
  240. if (!kgdb_connected)
  241. try_module_get(THIS_MODULE);
  242. }
  243. static void kgdboc_post_exp_handler(void)
  244. {
  245. /* decrement the module count when the debugger detaches */
  246. if (!kgdb_connected)
  247. module_put(THIS_MODULE);
  248. if (kgdboc_use_kms && dbg_restore_graphics) {
  249. dbg_restore_graphics = 0;
  250. con_debug_leave();
  251. }
  252. kgdboc_restore_input();
  253. }
  254. static struct kgdb_io kgdboc_io_ops = {
  255. .name = "kgdboc",
  256. .read_char = kgdboc_get_char,
  257. .write_char = kgdboc_put_char,
  258. .pre_exception = kgdboc_pre_exp_handler,
  259. .post_exception = kgdboc_post_exp_handler,
  260. };
  261. #ifdef CONFIG_KGDB_SERIAL_CONSOLE
  262. /* This is only available if kgdboc is a built in for early debugging */
  263. static int __init kgdboc_early_init(char *opt)
  264. {
  265. /* save the first character of the config string because the
  266. * init routine can destroy it.
  267. */
  268. char save_ch;
  269. kgdboc_option_setup(opt);
  270. save_ch = config[0];
  271. init_kgdboc();
  272. config[0] = save_ch;
  273. return 0;
  274. }
  275. early_param("ekgdboc", kgdboc_early_init);
  276. #endif /* CONFIG_KGDB_SERIAL_CONSOLE */
  277. module_init(init_kgdboc);
  278. module_exit(cleanup_kgdboc);
  279. module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
  280. MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
  281. MODULE_DESCRIPTION("KGDB Console TTY Driver");
  282. MODULE_LICENSE("GPL");