kgdboc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. /* Retrun 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. if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
  85. kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
  86. kdb_poll_idx++;
  87. if (cptr[0][3] == ',')
  88. *cptr += 4;
  89. else
  90. return 1;
  91. }
  92. }
  93. return 0;
  94. }
  95. static void kgdboc_unregister_kbd(void)
  96. {
  97. int i;
  98. for (i = 0; i < kdb_poll_idx; i++) {
  99. if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
  100. kdb_poll_idx--;
  101. kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
  102. kdb_poll_funcs[kdb_poll_idx] = NULL;
  103. i--;
  104. }
  105. }
  106. flush_work_sync(&kgdboc_restore_input_work);
  107. }
  108. #else /* ! CONFIG_KDB_KEYBOARD */
  109. #define kgdboc_register_kbd(x) 0
  110. #define kgdboc_unregister_kbd()
  111. #define kgdboc_restore_input()
  112. #endif /* ! CONFIG_KDB_KEYBOARD */
  113. static int kgdboc_option_setup(char *opt)
  114. {
  115. if (strlen(opt) >= MAX_CONFIG_LEN) {
  116. printk(KERN_ERR "kgdboc: config string too long\n");
  117. return -ENOSPC;
  118. }
  119. strcpy(config, opt);
  120. return 0;
  121. }
  122. __setup("kgdboc=", kgdboc_option_setup);
  123. static void cleanup_kgdboc(void)
  124. {
  125. kgdboc_unregister_kbd();
  126. if (configured == 1)
  127. kgdb_unregister_io_module(&kgdboc_io_ops);
  128. }
  129. static int configure_kgdboc(void)
  130. {
  131. struct tty_driver *p;
  132. int tty_line = 0;
  133. int err;
  134. char *cptr = config;
  135. struct console *cons;
  136. err = kgdboc_option_setup(config);
  137. if (err || !strlen(config) || isspace(config[0]))
  138. goto noconfig;
  139. err = -ENODEV;
  140. kgdboc_io_ops.is_console = 0;
  141. kgdb_tty_driver = NULL;
  142. kgdboc_use_kms = 0;
  143. if (strncmp(cptr, "kms,", 4) == 0) {
  144. cptr += 4;
  145. kgdboc_use_kms = 1;
  146. }
  147. if (kgdboc_register_kbd(&cptr))
  148. goto do_register;
  149. p = tty_find_polling_driver(cptr, &tty_line);
  150. if (!p)
  151. goto noconfig;
  152. cons = console_drivers;
  153. while (cons) {
  154. int idx;
  155. if (cons->device && cons->device(cons, &idx) == p &&
  156. idx == tty_line) {
  157. kgdboc_io_ops.is_console = 1;
  158. break;
  159. }
  160. cons = cons->next;
  161. }
  162. kgdb_tty_driver = p;
  163. kgdb_tty_line = tty_line;
  164. do_register:
  165. err = kgdb_register_io_module(&kgdboc_io_ops);
  166. if (err)
  167. goto noconfig;
  168. configured = 1;
  169. return 0;
  170. noconfig:
  171. config[0] = 0;
  172. configured = 0;
  173. cleanup_kgdboc();
  174. return err;
  175. }
  176. static int __init init_kgdboc(void)
  177. {
  178. /* Already configured? */
  179. if (configured == 1)
  180. return 0;
  181. return configure_kgdboc();
  182. }
  183. static int kgdboc_get_char(void)
  184. {
  185. if (!kgdb_tty_driver)
  186. return -1;
  187. return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
  188. kgdb_tty_line);
  189. }
  190. static void kgdboc_put_char(u8 chr)
  191. {
  192. if (!kgdb_tty_driver)
  193. return;
  194. kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
  195. kgdb_tty_line, chr);
  196. }
  197. static int param_set_kgdboc_var(const char *kmessage, struct kernel_param *kp)
  198. {
  199. int len = strlen(kmessage);
  200. if (len >= MAX_CONFIG_LEN) {
  201. printk(KERN_ERR "kgdboc: config string too long\n");
  202. return -ENOSPC;
  203. }
  204. /* Only copy in the string if the init function has not run yet */
  205. if (configured < 0) {
  206. strcpy(config, kmessage);
  207. return 0;
  208. }
  209. if (kgdb_connected) {
  210. printk(KERN_ERR
  211. "kgdboc: Cannot reconfigure while KGDB is connected.\n");
  212. return -EBUSY;
  213. }
  214. strcpy(config, kmessage);
  215. /* Chop out \n char as a result of echo */
  216. if (config[len - 1] == '\n')
  217. config[len - 1] = '\0';
  218. if (configured == 1)
  219. cleanup_kgdboc();
  220. /* Go and configure with the new params. */
  221. return configure_kgdboc();
  222. }
  223. static int dbg_restore_graphics;
  224. static void kgdboc_pre_exp_handler(void)
  225. {
  226. if (!dbg_restore_graphics && kgdboc_use_kms) {
  227. dbg_restore_graphics = 1;
  228. con_debug_enter(vc_cons[fg_console].d);
  229. }
  230. /* Increment the module count when the debugger is active */
  231. if (!kgdb_connected)
  232. try_module_get(THIS_MODULE);
  233. }
  234. static void kgdboc_post_exp_handler(void)
  235. {
  236. /* decrement the module count when the debugger detaches */
  237. if (!kgdb_connected)
  238. module_put(THIS_MODULE);
  239. if (kgdboc_use_kms && dbg_restore_graphics) {
  240. dbg_restore_graphics = 0;
  241. con_debug_leave();
  242. }
  243. kgdboc_restore_input();
  244. }
  245. static struct kgdb_io kgdboc_io_ops = {
  246. .name = "kgdboc",
  247. .read_char = kgdboc_get_char,
  248. .write_char = kgdboc_put_char,
  249. .pre_exception = kgdboc_pre_exp_handler,
  250. .post_exception = kgdboc_post_exp_handler,
  251. };
  252. #ifdef CONFIG_KGDB_SERIAL_CONSOLE
  253. /* This is only available if kgdboc is a built in for early debugging */
  254. static int __init kgdboc_early_init(char *opt)
  255. {
  256. /* save the first character of the config string because the
  257. * init routine can destroy it.
  258. */
  259. char save_ch;
  260. kgdboc_option_setup(opt);
  261. save_ch = config[0];
  262. init_kgdboc();
  263. config[0] = save_ch;
  264. return 0;
  265. }
  266. early_param("ekgdboc", kgdboc_early_init);
  267. #endif /* CONFIG_KGDB_SERIAL_CONSOLE */
  268. module_init(init_kgdboc);
  269. module_exit(cleanup_kgdboc);
  270. module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
  271. MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
  272. MODULE_DESCRIPTION("KGDB Console TTY Driver");
  273. MODULE_LICENSE("GPL");