kgdboc.c 7.1 KB

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