kgdboc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. /* Return 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(&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 void cleanup_kgdboc(void)
  115. {
  116. if (kgdb_unregister_nmi_console())
  117. return;
  118. kgdboc_unregister_kbd();
  119. if (configured == 1)
  120. kgdb_unregister_io_module(&kgdboc_io_ops);
  121. }
  122. static int configure_kgdboc(void)
  123. {
  124. struct tty_driver *p;
  125. int tty_line = 0;
  126. int err = -ENODEV;
  127. char *cptr = config;
  128. struct console *cons;
  129. if (!strlen(config) || isspace(config[0])) {
  130. err = 0;
  131. goto noconfig;
  132. }
  133. kgdboc_io_ops.is_console = 0;
  134. kgdb_tty_driver = NULL;
  135. kgdboc_use_kms = 0;
  136. if (strncmp(cptr, "kms,", 4) == 0) {
  137. cptr += 4;
  138. kgdboc_use_kms = 1;
  139. }
  140. if (kgdboc_register_kbd(&cptr))
  141. goto do_register;
  142. p = tty_find_polling_driver(cptr, &tty_line);
  143. if (!p)
  144. goto noconfig;
  145. cons = console_drivers;
  146. while (cons) {
  147. int idx;
  148. if (cons->device && cons->device(cons, &idx) == p &&
  149. idx == tty_line) {
  150. kgdboc_io_ops.is_console = 1;
  151. break;
  152. }
  153. cons = cons->next;
  154. }
  155. kgdb_tty_driver = p;
  156. kgdb_tty_line = tty_line;
  157. do_register:
  158. err = kgdb_register_io_module(&kgdboc_io_ops);
  159. if (err)
  160. goto noconfig;
  161. err = kgdb_register_nmi_console();
  162. if (err)
  163. goto nmi_con_failed;
  164. configured = 1;
  165. return 0;
  166. nmi_con_failed:
  167. kgdb_unregister_io_module(&kgdboc_io_ops);
  168. noconfig:
  169. kgdboc_unregister_kbd();
  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. size_t 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 (len && 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. static int kgdboc_option_setup(char *opt)
  253. {
  254. if (!opt) {
  255. pr_err("config string not provided\n");
  256. return -EINVAL;
  257. }
  258. if (strlen(opt) >= MAX_CONFIG_LEN) {
  259. pr_err("config string too long\n");
  260. return -ENOSPC;
  261. }
  262. strcpy(config, opt);
  263. return 0;
  264. }
  265. __setup("kgdboc=", kgdboc_option_setup);
  266. /* This is only available if kgdboc is a built in for early debugging */
  267. static int __init kgdboc_early_init(char *opt)
  268. {
  269. /* save the first character of the config string because the
  270. * init routine can destroy it.
  271. */
  272. char save_ch;
  273. kgdboc_option_setup(opt);
  274. save_ch = config[0];
  275. init_kgdboc();
  276. config[0] = save_ch;
  277. return 0;
  278. }
  279. early_param("ekgdboc", kgdboc_early_init);
  280. #endif /* CONFIG_KGDB_SERIAL_CONSOLE */
  281. module_init(init_kgdboc);
  282. module_exit(cleanup_kgdboc);
  283. module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
  284. MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
  285. MODULE_DESCRIPTION("KGDB Console TTY Driver");
  286. MODULE_LICENSE("GPL");