ramoops.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/kmsg_dump.h>
  24. #include <linux/time.h>
  25. #include <linux/io.h>
  26. #include <linux/ioport.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/ramoops.h>
  29. #define RAMOOPS_KERNMSG_HDR "===="
  30. #define RECORD_SIZE 4096
  31. static ulong mem_address;
  32. module_param(mem_address, ulong, 0400);
  33. MODULE_PARM_DESC(mem_address,
  34. "start of reserved RAM used to store oops/panic logs");
  35. static ulong mem_size;
  36. module_param(mem_size, ulong, 0400);
  37. MODULE_PARM_DESC(mem_size,
  38. "size of reserved RAM used to store oops/panic logs");
  39. static int dump_oops = 1;
  40. module_param(dump_oops, int, 0600);
  41. MODULE_PARM_DESC(dump_oops,
  42. "set to 1 to dump oopses, 0 to only dump panics (default 1)");
  43. static struct ramoops_context {
  44. struct kmsg_dumper dump;
  45. void *virt_addr;
  46. phys_addr_t phys_addr;
  47. unsigned long size;
  48. int count;
  49. int max_count;
  50. } oops_cxt;
  51. static void ramoops_do_dump(struct kmsg_dumper *dumper,
  52. enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
  53. const char *s2, unsigned long l2)
  54. {
  55. struct ramoops_context *cxt = container_of(dumper,
  56. struct ramoops_context, dump);
  57. unsigned long s1_start, s2_start;
  58. unsigned long l1_cpy, l2_cpy;
  59. int res, hdr_size;
  60. char *buf, *buf_orig;
  61. struct timeval timestamp;
  62. /* Only dump oopses if dump_oops is set */
  63. if (reason == KMSG_DUMP_OOPS && !dump_oops)
  64. return;
  65. buf = (char *)(cxt->virt_addr + (cxt->count * RECORD_SIZE));
  66. buf_orig = buf;
  67. memset(buf, '\0', RECORD_SIZE);
  68. res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR);
  69. buf += res;
  70. do_gettimeofday(&timestamp);
  71. res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec);
  72. buf += res;
  73. hdr_size = buf - buf_orig;
  74. l2_cpy = min(l2, (unsigned long)(RECORD_SIZE - hdr_size));
  75. l1_cpy = min(l1, (unsigned long)(RECORD_SIZE - hdr_size) - l2_cpy);
  76. s2_start = l2 - l2_cpy;
  77. s1_start = l1 - l1_cpy;
  78. memcpy(buf, s1 + s1_start, l1_cpy);
  79. memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy);
  80. cxt->count = (cxt->count + 1) % cxt->max_count;
  81. }
  82. static int __init ramoops_probe(struct platform_device *pdev)
  83. {
  84. struct ramoops_platform_data *pdata = pdev->dev.platform_data;
  85. struct ramoops_context *cxt = &oops_cxt;
  86. int err = -EINVAL;
  87. if (pdata) {
  88. mem_size = pdata->mem_size;
  89. mem_address = pdata->mem_address;
  90. }
  91. if (!mem_size) {
  92. printk(KERN_ERR "ramoops: invalid size specification");
  93. goto fail3;
  94. }
  95. rounddown_pow_of_two(mem_size);
  96. if (mem_size < RECORD_SIZE) {
  97. printk(KERN_ERR "ramoops: size too small");
  98. goto fail3;
  99. }
  100. cxt->max_count = mem_size / RECORD_SIZE;
  101. cxt->count = 0;
  102. cxt->size = mem_size;
  103. cxt->phys_addr = mem_address;
  104. if (!request_mem_region(cxt->phys_addr, cxt->size, "ramoops")) {
  105. printk(KERN_ERR "ramoops: request mem region failed");
  106. err = -EINVAL;
  107. goto fail3;
  108. }
  109. cxt->virt_addr = ioremap(cxt->phys_addr, cxt->size);
  110. if (!cxt->virt_addr) {
  111. printk(KERN_ERR "ramoops: ioremap failed");
  112. goto fail2;
  113. }
  114. cxt->dump.dump = ramoops_do_dump;
  115. err = kmsg_dump_register(&cxt->dump);
  116. if (err) {
  117. printk(KERN_ERR "ramoops: registering kmsg dumper failed");
  118. goto fail1;
  119. }
  120. return 0;
  121. fail1:
  122. iounmap(cxt->virt_addr);
  123. fail2:
  124. release_mem_region(cxt->phys_addr, cxt->size);
  125. fail3:
  126. return err;
  127. }
  128. static int __exit ramoops_remove(struct platform_device *pdev)
  129. {
  130. struct ramoops_context *cxt = &oops_cxt;
  131. if (kmsg_dump_unregister(&cxt->dump) < 0)
  132. printk(KERN_WARNING "ramoops: could not unregister kmsg_dumper");
  133. iounmap(cxt->virt_addr);
  134. release_mem_region(cxt->phys_addr, cxt->size);
  135. return 0;
  136. }
  137. static struct platform_driver ramoops_driver = {
  138. .remove = __exit_p(ramoops_remove),
  139. .driver = {
  140. .name = "ramoops",
  141. .owner = THIS_MODULE,
  142. },
  143. };
  144. static int __init ramoops_init(void)
  145. {
  146. return platform_driver_probe(&ramoops_driver, ramoops_probe);
  147. }
  148. static void __exit ramoops_exit(void)
  149. {
  150. platform_driver_unregister(&ramoops_driver);
  151. }
  152. module_init(ramoops_init);
  153. module_exit(ramoops_exit);
  154. MODULE_LICENSE("GPL");
  155. MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
  156. MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");