ramoops.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * RAM Oops/Panic logger
  3. *
  4. * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. *
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/kernel.h>
  23. #include <linux/err.h>
  24. #include <linux/module.h>
  25. #include <linux/kmsg_dump.h>
  26. #include <linux/time.h>
  27. #include <linux/io.h>
  28. #include <linux/ioport.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/slab.h>
  31. #include <linux/ramoops.h>
  32. #define RAMOOPS_KERNMSG_HDR "===="
  33. #define MIN_MEM_SIZE 4096UL
  34. static ulong record_size = MIN_MEM_SIZE;
  35. module_param(record_size, ulong, 0400);
  36. MODULE_PARM_DESC(record_size,
  37. "size of each dump done on oops/panic");
  38. static ulong mem_address;
  39. module_param(mem_address, ulong, 0400);
  40. MODULE_PARM_DESC(mem_address,
  41. "start of reserved RAM used to store oops/panic logs");
  42. static ulong mem_size;
  43. module_param(mem_size, ulong, 0400);
  44. MODULE_PARM_DESC(mem_size,
  45. "size of reserved RAM used to store oops/panic logs");
  46. static int dump_oops = 1;
  47. module_param(dump_oops, int, 0600);
  48. MODULE_PARM_DESC(dump_oops,
  49. "set to 1 to dump oopses, 0 to only dump panics (default 1)");
  50. static struct ramoops_context {
  51. struct kmsg_dumper dump;
  52. void *virt_addr;
  53. phys_addr_t phys_addr;
  54. unsigned long size;
  55. unsigned long record_size;
  56. int dump_oops;
  57. int count;
  58. int max_count;
  59. } oops_cxt;
  60. static struct platform_device *dummy;
  61. static struct ramoops_platform_data *dummy_data;
  62. static void ramoops_do_dump(struct kmsg_dumper *dumper,
  63. enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
  64. const char *s2, unsigned long l2)
  65. {
  66. struct ramoops_context *cxt = container_of(dumper,
  67. struct ramoops_context, dump);
  68. unsigned long s1_start, s2_start;
  69. unsigned long l1_cpy, l2_cpy;
  70. int res, hdr_size;
  71. char *buf, *buf_orig;
  72. struct timeval timestamp;
  73. if (reason != KMSG_DUMP_OOPS &&
  74. reason != KMSG_DUMP_PANIC &&
  75. reason != KMSG_DUMP_KEXEC)
  76. return;
  77. /* Only dump oopses if dump_oops is set */
  78. if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
  79. return;
  80. buf = cxt->virt_addr + (cxt->count * cxt->record_size);
  81. buf_orig = buf;
  82. memset(buf, '\0', cxt->record_size);
  83. res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR);
  84. buf += res;
  85. do_gettimeofday(&timestamp);
  86. res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec);
  87. buf += res;
  88. hdr_size = buf - buf_orig;
  89. l2_cpy = min(l2, cxt->record_size - hdr_size);
  90. l1_cpy = min(l1, cxt->record_size - hdr_size - l2_cpy);
  91. s2_start = l2 - l2_cpy;
  92. s1_start = l1 - l1_cpy;
  93. memcpy(buf, s1 + s1_start, l1_cpy);
  94. memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy);
  95. cxt->count = (cxt->count + 1) % cxt->max_count;
  96. }
  97. static int __init ramoops_probe(struct platform_device *pdev)
  98. {
  99. struct ramoops_platform_data *pdata = pdev->dev.platform_data;
  100. struct ramoops_context *cxt = &oops_cxt;
  101. int err = -EINVAL;
  102. if (!pdata->mem_size || !pdata->record_size) {
  103. pr_err("The memory size and the record size must be "
  104. "non-zero\n");
  105. goto fail3;
  106. }
  107. rounddown_pow_of_two(pdata->mem_size);
  108. rounddown_pow_of_two(pdata->record_size);
  109. /* Check for the minimum memory size */
  110. if (pdata->mem_size < MIN_MEM_SIZE &&
  111. pdata->record_size < MIN_MEM_SIZE) {
  112. pr_err("memory size too small, minium is %lu\n", MIN_MEM_SIZE);
  113. goto fail3;
  114. }
  115. if (pdata->mem_size < pdata->record_size) {
  116. pr_err("The memory size must be larger than the "
  117. "records size\n");
  118. goto fail3;
  119. }
  120. cxt->max_count = pdata->mem_size / pdata->record_size;
  121. cxt->count = 0;
  122. cxt->size = pdata->mem_size;
  123. cxt->phys_addr = pdata->mem_address;
  124. cxt->record_size = pdata->record_size;
  125. cxt->dump_oops = pdata->dump_oops;
  126. /*
  127. * Update the module parameter variables as well so they are visible
  128. * through /sys/module/ramoops/parameters/
  129. */
  130. mem_size = pdata->mem_size;
  131. mem_address = pdata->mem_address;
  132. record_size = pdata->record_size;
  133. dump_oops = pdata->dump_oops;
  134. if (!request_mem_region(cxt->phys_addr, cxt->size, "ramoops")) {
  135. pr_err("request mem region failed\n");
  136. err = -EINVAL;
  137. goto fail3;
  138. }
  139. cxt->virt_addr = ioremap(cxt->phys_addr, cxt->size);
  140. if (!cxt->virt_addr) {
  141. pr_err("ioremap failed\n");
  142. goto fail2;
  143. }
  144. cxt->dump.dump = ramoops_do_dump;
  145. err = kmsg_dump_register(&cxt->dump);
  146. if (err) {
  147. pr_err("registering kmsg dumper failed\n");
  148. goto fail1;
  149. }
  150. return 0;
  151. fail1:
  152. iounmap(cxt->virt_addr);
  153. fail2:
  154. release_mem_region(cxt->phys_addr, cxt->size);
  155. fail3:
  156. return err;
  157. }
  158. static int __exit ramoops_remove(struct platform_device *pdev)
  159. {
  160. struct ramoops_context *cxt = &oops_cxt;
  161. if (kmsg_dump_unregister(&cxt->dump) < 0)
  162. pr_warn("could not unregister kmsg_dumper\n");
  163. iounmap(cxt->virt_addr);
  164. release_mem_region(cxt->phys_addr, cxt->size);
  165. return 0;
  166. }
  167. static struct platform_driver ramoops_driver = {
  168. .remove = __exit_p(ramoops_remove),
  169. .driver = {
  170. .name = "ramoops",
  171. .owner = THIS_MODULE,
  172. },
  173. };
  174. static int __init ramoops_init(void)
  175. {
  176. int ret;
  177. ret = platform_driver_probe(&ramoops_driver, ramoops_probe);
  178. if (ret == -ENODEV) {
  179. /*
  180. * If we didn't find a platform device, we use module parameters
  181. * building platform data on the fly.
  182. */
  183. pr_info("platform device not found, using module parameters\n");
  184. dummy_data = kzalloc(sizeof(struct ramoops_platform_data),
  185. GFP_KERNEL);
  186. if (!dummy_data)
  187. return -ENOMEM;
  188. dummy_data->mem_size = mem_size;
  189. dummy_data->mem_address = mem_address;
  190. dummy_data->record_size = record_size;
  191. dummy_data->dump_oops = dump_oops;
  192. dummy = platform_create_bundle(&ramoops_driver, ramoops_probe,
  193. NULL, 0, dummy_data,
  194. sizeof(struct ramoops_platform_data));
  195. if (IS_ERR(dummy))
  196. ret = PTR_ERR(dummy);
  197. else
  198. ret = 0;
  199. }
  200. return ret;
  201. }
  202. static void __exit ramoops_exit(void)
  203. {
  204. platform_driver_unregister(&ramoops_driver);
  205. kfree(dummy_data);
  206. }
  207. module_init(ramoops_init);
  208. module_exit(ramoops_exit);
  209. MODULE_LICENSE("GPL");
  210. MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
  211. MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");