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