kgdboc.c 7.4 KB

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