coresight-event.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/sched.h>
  17. #include <linux/coresight.h>
  18. #include <trace/events/exception.h>
  19. static int event_abort_enable;
  20. static int event_abort_set(const char *val, const struct kernel_param *kp);
  21. module_param_call(event_abort_enable, event_abort_set, param_get_int,
  22. &event_abort_enable, 0644);
  23. static int event_abort_early_panic = 1;
  24. static int event_abort_on_panic_set(const char *val,
  25. const struct kernel_param *kp);
  26. module_param_call(event_abort_early_panic, event_abort_on_panic_set,
  27. param_get_int, &event_abort_early_panic, 0644);
  28. static void event_abort_user_fault(void *ignore,
  29. struct task_struct *task,
  30. unsigned long addr,
  31. unsigned int fsr)
  32. {
  33. coresight_abort();
  34. pr_debug("coresight_event: task_name: %s, addr: %lu, fsr:%u",
  35. (char *)task->comm, addr, fsr);
  36. }
  37. static void event_abort_undef_instr(void *ignore,
  38. struct pt_regs *regs,
  39. void __user *pc)
  40. {
  41. if (user_mode(regs)) {
  42. coresight_abort();
  43. pr_debug("coresight_event: pc: %pK", pc);
  44. }
  45. }
  46. static void event_abort_unhandled_abort(void *ignore,
  47. struct pt_regs *regs,
  48. unsigned long addr,
  49. unsigned int fsr)
  50. {
  51. if (user_mode(regs)) {
  52. coresight_abort();
  53. pr_debug("coresight_event: addr: %lu, fsr:%u", addr, fsr);
  54. }
  55. }
  56. static void event_abort_kernel_panic(void *ignore, long state)
  57. {
  58. coresight_abort();
  59. }
  60. static int event_abort_register(void)
  61. {
  62. int ret;
  63. ret = register_trace_user_fault(event_abort_user_fault, NULL);
  64. if (ret)
  65. goto err_usr_fault;
  66. ret = register_trace_undef_instr(event_abort_undef_instr, NULL);
  67. if (ret)
  68. goto err_undef_instr;
  69. ret = register_trace_unhandled_abort(event_abort_unhandled_abort, NULL);
  70. if (ret)
  71. goto err_unhandled_abort;
  72. return 0;
  73. err_unhandled_abort:
  74. unregister_trace_undef_instr(event_abort_undef_instr, NULL);
  75. err_undef_instr:
  76. unregister_trace_user_fault(event_abort_user_fault, NULL);
  77. err_usr_fault:
  78. return ret;
  79. }
  80. static void event_abort_unregister(void)
  81. {
  82. unregister_trace_user_fault(event_abort_user_fault, NULL);
  83. unregister_trace_undef_instr(event_abort_undef_instr, NULL);
  84. unregister_trace_unhandled_abort(event_abort_unhandled_abort, NULL);
  85. }
  86. static int event_abort_set(const char *val, const struct kernel_param *kp)
  87. {
  88. int ret;
  89. ret = param_set_int(val, kp);
  90. if (ret) {
  91. pr_err("coresight_event: error setting value %d\n", ret);
  92. return ret;
  93. }
  94. if (event_abort_enable)
  95. ret = event_abort_register();
  96. else
  97. event_abort_unregister();
  98. return ret;
  99. }
  100. static int event_abort_on_panic_set(const char *val,
  101. const struct kernel_param *kp)
  102. {
  103. int ret;
  104. ret = param_set_int(val, kp);
  105. if (ret) {
  106. pr_err("coresight_event: error setting val on panic %d\n", ret);
  107. return ret;
  108. }
  109. if (event_abort_early_panic) {
  110. unregister_trace_kernel_panic_late(event_abort_kernel_panic,
  111. NULL);
  112. ret = register_trace_kernel_panic(event_abort_kernel_panic,
  113. NULL);
  114. if (ret)
  115. goto err;
  116. } else {
  117. unregister_trace_kernel_panic(event_abort_kernel_panic, NULL);
  118. ret = register_trace_kernel_panic_late(event_abort_kernel_panic,
  119. NULL);
  120. if (ret)
  121. goto err;
  122. }
  123. return 0;
  124. err:
  125. pr_err("coresight_event: error registering panic event %d\n", ret);
  126. return ret;
  127. }
  128. static int __init event_init(void)
  129. {
  130. int ret;
  131. ret = register_trace_kernel_panic(event_abort_kernel_panic, NULL);
  132. if (ret) {
  133. /* We do not want to fail module init. This module can still
  134. * be used to register other abort events.
  135. */
  136. pr_err("coresight_event: error registering on panic %d\n", ret);
  137. }
  138. return 0;
  139. }
  140. module_init(event_init);
  141. static void __exit event_exit(void)
  142. {
  143. unregister_trace_kernel_panic(event_abort_kernel_panic, NULL);
  144. }
  145. module_exit(event_exit);
  146. MODULE_LICENSE("GPL v2");
  147. MODULE_DESCRIPTION("Coresight Event driver to abort tracing");