PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/char/ramoops.c

https://bitbucket.org/cresqo/cm7-p500-kernel
C | 162 lines | 111 code | 30 blank | 21 comment | 9 complexity | 47f0708dbd5d7f502b46033c63003212 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  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. #define RAMOOPS_KERNMSG_HDR "===="
  28. #define RAMOOPS_HEADER_SIZE (5 + sizeof(struct timeval))
  29. #define RECORD_SIZE 4096
  30. static ulong mem_address;
  31. module_param(mem_address, ulong, 0400);
  32. MODULE_PARM_DESC(mem_address,
  33. "start of reserved RAM used to store oops/panic logs");
  34. static ulong mem_size;
  35. module_param(mem_size, ulong, 0400);
  36. MODULE_PARM_DESC(mem_size,
  37. "size of reserved RAM used to store oops/panic logs");
  38. static int dump_oops = 1;
  39. module_param(dump_oops, int, 0600);
  40. MODULE_PARM_DESC(dump_oops,
  41. "set to 1 to dump oopses, 0 to only dump panics (default 1)");
  42. static struct ramoops_context {
  43. struct kmsg_dumper dump;
  44. void *virt_addr;
  45. phys_addr_t phys_addr;
  46. unsigned long size;
  47. int count;
  48. int max_count;
  49. } oops_cxt;
  50. static void ramoops_do_dump(struct kmsg_dumper *dumper,
  51. enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
  52. const char *s2, unsigned long l2)
  53. {
  54. struct ramoops_context *cxt = container_of(dumper,
  55. struct ramoops_context, dump);
  56. unsigned long s1_start, s2_start;
  57. unsigned long l1_cpy, l2_cpy;
  58. int res;
  59. char *buf;
  60. struct timeval timestamp;
  61. /* Only dump oopses if dump_oops is set */
  62. if (reason == KMSG_DUMP_OOPS && !dump_oops)
  63. return;
  64. buf = (char *)(cxt->virt_addr + (cxt->count * RECORD_SIZE));
  65. memset(buf, '\0', RECORD_SIZE);
  66. res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR);
  67. buf += res;
  68. do_gettimeofday(&timestamp);
  69. res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec);
  70. buf += res;
  71. l2_cpy = min(l2, (unsigned long)(RECORD_SIZE - RAMOOPS_HEADER_SIZE));
  72. l1_cpy = min(l1, (unsigned long)(RECORD_SIZE - RAMOOPS_HEADER_SIZE) - l2_cpy);
  73. s2_start = l2 - l2_cpy;
  74. s1_start = l1 - l1_cpy;
  75. memcpy(buf, s1 + s1_start, l1_cpy);
  76. memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy);
  77. cxt->count = (cxt->count + 1) % cxt->max_count;
  78. }
  79. static int __init ramoops_init(void)
  80. {
  81. struct ramoops_context *cxt = &oops_cxt;
  82. int err = -EINVAL;
  83. if (!mem_size) {
  84. printk(KERN_ERR "ramoops: invalid size specification");
  85. goto fail3;
  86. }
  87. rounddown_pow_of_two(mem_size);
  88. if (mem_size < RECORD_SIZE) {
  89. printk(KERN_ERR "ramoops: size too small");
  90. goto fail3;
  91. }
  92. cxt->max_count = mem_size / RECORD_SIZE;
  93. cxt->count = 0;
  94. cxt->size = mem_size;
  95. cxt->phys_addr = mem_address;
  96. if (!request_mem_region(cxt->phys_addr, cxt->size, "ramoops")) {
  97. printk(KERN_ERR "ramoops: request mem region failed");
  98. err = -EINVAL;
  99. goto fail3;
  100. }
  101. cxt->virt_addr = ioremap(cxt->phys_addr, cxt->size);
  102. if (!cxt->virt_addr) {
  103. printk(KERN_ERR "ramoops: ioremap failed");
  104. goto fail2;
  105. }
  106. cxt->dump.dump = ramoops_do_dump;
  107. err = kmsg_dump_register(&cxt->dump);
  108. if (err) {
  109. printk(KERN_ERR "ramoops: registering kmsg dumper failed");
  110. goto fail1;
  111. }
  112. return 0;
  113. fail1:
  114. iounmap(cxt->virt_addr);
  115. fail2:
  116. release_mem_region(cxt->phys_addr, cxt->size);
  117. fail3:
  118. return err;
  119. }
  120. static void __exit ramoops_exit(void)
  121. {
  122. struct ramoops_context *cxt = &oops_cxt;
  123. if (kmsg_dump_unregister(&cxt->dump) < 0)
  124. printk(KERN_WARNING "ramoops: could not unregister kmsg_dumper");
  125. iounmap(cxt->virt_addr);
  126. release_mem_region(cxt->phys_addr, cxt->size);
  127. }
  128. module_init(ramoops_init);
  129. module_exit(ramoops_exit);
  130. MODULE_LICENSE("GPL");
  131. MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
  132. MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");