kgdboc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #define MAX_CONFIG_LEN 40
  22. static struct kgdb_io kgdboc_io_ops;
  23. /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
  24. static int configured = -1;
  25. static char config[MAX_CONFIG_LEN];
  26. static struct kparam_string kps = {
  27. .string = config,
  28. .maxlen = MAX_CONFIG_LEN,
  29. };
  30. static int kgdboc_use_kms; /* 1 if we use kernel mode switching */
  31. static struct tty_driver *kgdb_tty_driver;
  32. static int kgdb_tty_line;
  33. #ifdef CONFIG_KDB_KEYBOARD
  34. static int kgdboc_register_kbd(char **cptr)
  35. {
  36. if (strncmp(*cptr, "kbd", 3) == 0) {
  37. if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
  38. kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
  39. kdb_poll_idx++;
  40. if (cptr[0][3] == ',')
  41. *cptr += 4;
  42. else
  43. return 1;
  44. }
  45. }
  46. return 0;
  47. }
  48. static void kgdboc_unregister_kbd(void)
  49. {
  50. int i;
  51. for (i = 0; i < kdb_poll_idx; i++) {
  52. if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
  53. kdb_poll_idx--;
  54. kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
  55. kdb_poll_funcs[kdb_poll_idx] = NULL;
  56. i--;
  57. }
  58. }
  59. }
  60. #else /* ! CONFIG_KDB_KEYBOARD */
  61. #define kgdboc_register_kbd(x) 0
  62. #define kgdboc_unregister_kbd()
  63. #endif /* ! CONFIG_KDB_KEYBOARD */
  64. static int kgdboc_option_setup(char *opt)
  65. {
  66. if (strlen(opt) > MAX_CONFIG_LEN) {
  67. printk(KERN_ERR "kgdboc: config string too long\n");
  68. return -ENOSPC;
  69. }
  70. strcpy(config, opt);
  71. return 0;
  72. }
  73. __setup("kgdboc=", kgdboc_option_setup);
  74. static void cleanup_kgdboc(void)
  75. {
  76. kgdboc_unregister_kbd();
  77. if (configured == 1)
  78. kgdb_unregister_io_module(&kgdboc_io_ops);
  79. }
  80. static int configure_kgdboc(void)
  81. {
  82. struct tty_driver *p;
  83. int tty_line = 0;
  84. int err;
  85. char *cptr = config;
  86. struct console *cons;
  87. err = kgdboc_option_setup(config);
  88. if (err || !strlen(config) || isspace(config[0]))
  89. goto noconfig;
  90. err = -ENODEV;
  91. kgdboc_io_ops.is_console = 0;
  92. kgdb_tty_driver = NULL;
  93. kgdboc_use_kms = 0;
  94. if (strncmp(cptr, "kms,", 4) == 0) {
  95. cptr += 4;
  96. kgdboc_use_kms = 1;
  97. }
  98. if (kgdboc_register_kbd(&cptr))
  99. goto do_register;
  100. p = tty_find_polling_driver(cptr, &tty_line);
  101. if (!p)
  102. goto noconfig;
  103. cons = console_drivers;
  104. while (cons) {
  105. int idx;
  106. if (cons->device && cons->device(cons, &idx) == p &&
  107. idx == tty_line) {
  108. kgdboc_io_ops.is_console = 1;
  109. break;
  110. }
  111. cons = cons->next;
  112. }
  113. kgdb_tty_driver = p;
  114. kgdb_tty_line = tty_line;
  115. do_register:
  116. err = kgdb_register_io_module(&kgdboc_io_ops);
  117. if (err)
  118. goto noconfig;
  119. configured = 1;
  120. return 0;
  121. noconfig:
  122. config[0] = 0;
  123. configured = 0;
  124. cleanup_kgdboc();
  125. return err;
  126. }
  127. static int __init init_kgdboc(void)
  128. {
  129. /* Already configured? */
  130. if (configured == 1)
  131. return 0;
  132. return configure_kgdboc();
  133. }
  134. static int kgdboc_get_char(void)
  135. {
  136. if (!kgdb_tty_driver)
  137. return -1;
  138. return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
  139. kgdb_tty_line);
  140. }
  141. static void kgdboc_put_char(u8 chr)
  142. {
  143. if (!kgdb_tty_driver)
  144. return;
  145. kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
  146. kgdb_tty_line, chr);
  147. }
  148. static int param_set_kgdboc_var(const char *kmessage, struct kernel_param *kp)
  149. {
  150. int len = strlen(kmessage);
  151. if (len >= MAX_CONFIG_LEN) {
  152. printk(KERN_ERR "kgdboc: config string too long\n");
  153. return -ENOSPC;
  154. }
  155. /* Only copy in the string if the init function has not run yet */
  156. if (configured < 0) {
  157. strcpy(config, kmessage);
  158. return 0;
  159. }
  160. if (kgdb_connected) {
  161. printk(KERN_ERR
  162. "kgdboc: Cannot reconfigure while KGDB is connected.\n");
  163. return -EBUSY;
  164. }
  165. strcpy(config, kmessage);
  166. /* Chop out \n char as a result of echo */
  167. if (config[len - 1] == '\n')
  168. config[len - 1] = '\0';
  169. if (configured == 1)
  170. cleanup_kgdboc();
  171. /* Go and configure with the new params. */
  172. return configure_kgdboc();
  173. }
  174. static int dbg_restore_graphics;
  175. static void kgdboc_pre_exp_handler(void)
  176. {
  177. if (!dbg_restore_graphics && kgdboc_use_kms) {
  178. dbg_restore_graphics = 1;
  179. con_debug_enter(vc_cons[fg_console].d);
  180. }
  181. /* Increment the module count when the debugger is active */
  182. if (!kgdb_connected)
  183. try_module_get(THIS_MODULE);
  184. }
  185. static void kgdboc_post_exp_handler(void)
  186. {
  187. /* decrement the module count when the debugger detaches */
  188. if (!kgdb_connected)
  189. module_put(THIS_MODULE);
  190. if (kgdboc_use_kms && dbg_restore_graphics) {
  191. dbg_restore_graphics = 0;
  192. con_debug_leave();
  193. }
  194. }
  195. static struct kgdb_io kgdboc_io_ops = {
  196. .name = "kgdboc",
  197. .read_char = kgdboc_get_char,
  198. .write_char = kgdboc_put_char,
  199. .pre_exception = kgdboc_pre_exp_handler,
  200. .post_exception = kgdboc_post_exp_handler,
  201. };
  202. #ifdef CONFIG_KGDB_SERIAL_CONSOLE
  203. /* This is only available if kgdboc is a built in for early debugging */
  204. int __init kgdboc_early_init(char *opt)
  205. {
  206. /* save the first character of the config string because the
  207. * init routine can destroy it.
  208. */
  209. char save_ch;
  210. kgdboc_option_setup(opt);
  211. save_ch = config[0];
  212. init_kgdboc();
  213. config[0] = save_ch;
  214. return 0;
  215. }
  216. early_param("ekgdboc", kgdboc_early_init);
  217. #endif /* CONFIG_KGDB_SERIAL_CONSOLE */
  218. module_init(init_kgdboc);
  219. module_exit(cleanup_kgdboc);
  220. module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
  221. MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
  222. MODULE_DESCRIPTION("KGDB Console TTY Driver");
  223. MODULE_LICENSE("GPL");