kgdboc.c 7.2 KB

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