ramoops.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * RAM Oops/Panic logger
  3. *
  4. * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
  5. * Copyright (C) 2011 Kees Cook <keescook@chromium.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. *
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/kernel.h>
  24. #include <linux/err.h>
  25. #include <linux/module.h>
  26. #include <linux/pstore.h>
  27. #include <linux/time.h>
  28. #include <linux/io.h>
  29. #include <linux/ioport.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/slab.h>
  32. #include <linux/ramoops.h>
  33. #define RAMOOPS_KERNMSG_HDR "===="
  34. #define MIN_MEM_SIZE 4096UL
  35. static ulong record_size = MIN_MEM_SIZE;
  36. module_param(record_size, ulong, 0400);
  37. MODULE_PARM_DESC(record_size,
  38. "size of each dump done on oops/panic");
  39. static ulong mem_address;
  40. module_param(mem_address, ulong, 0400);
  41. MODULE_PARM_DESC(mem_address,
  42. "start of reserved RAM used to store oops/panic logs");
  43. static ulong mem_size;
  44. module_param(mem_size, ulong, 0400);
  45. MODULE_PARM_DESC(mem_size,
  46. "size of reserved RAM used to store oops/panic logs");
  47. static int dump_oops = 1;
  48. module_param(dump_oops, int, 0600);
  49. MODULE_PARM_DESC(dump_oops,
  50. "set to 1 to dump oopses, 0 to only dump panics (default 1)");
  51. struct ramoops_context {
  52. void *virt_addr;
  53. phys_addr_t phys_addr;
  54. unsigned long size;
  55. size_t record_size;
  56. int dump_oops;
  57. unsigned int count;
  58. unsigned int max_count;
  59. unsigned int read_count;
  60. struct pstore_info pstore;
  61. };
  62. static struct platform_device *dummy;
  63. static struct ramoops_platform_data *dummy_data;
  64. static int ramoops_pstore_open(struct pstore_info *psi)
  65. {
  66. struct ramoops_context *cxt = psi->data;
  67. cxt->read_count = 0;
  68. return 0;
  69. }
  70. static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type,
  71. struct timespec *time,
  72. char **buf,
  73. struct pstore_info *psi)
  74. {
  75. ssize_t size;
  76. char *rambuf;
  77. struct ramoops_context *cxt = psi->data;
  78. if (cxt->read_count >= cxt->max_count)
  79. return -EINVAL;
  80. *id = cxt->read_count++;
  81. /* Only supports dmesg output so far. */
  82. *type = PSTORE_TYPE_DMESG;
  83. /* TODO(kees): Bogus time for the moment. */
  84. time->tv_sec = 0;
  85. time->tv_nsec = 0;
  86. rambuf = cxt->virt_addr + (*id * cxt->record_size);
  87. size = strnlen(rambuf, cxt->record_size);
  88. *buf = kmalloc(size, GFP_KERNEL);
  89. if (*buf == NULL)
  90. return -ENOMEM;
  91. memcpy(*buf, rambuf, size);
  92. return size;
  93. }
  94. static int ramoops_pstore_write(enum pstore_type_id type,
  95. enum kmsg_dump_reason reason,
  96. u64 *id,
  97. unsigned int part,
  98. size_t size, struct pstore_info *psi)
  99. {
  100. char *buf;
  101. size_t res;
  102. struct timeval timestamp;
  103. struct ramoops_context *cxt = psi->data;
  104. size_t available = cxt->record_size;
  105. /* Currently ramoops is designed to only store dmesg dumps. */
  106. if (type != PSTORE_TYPE_DMESG)
  107. return -EINVAL;
  108. /* Out of the various dmesg dump types, ramoops is currently designed
  109. * to only store crash logs, rather than storing general kernel logs.
  110. */
  111. if (reason != KMSG_DUMP_OOPS &&
  112. reason != KMSG_DUMP_PANIC)
  113. return -EINVAL;
  114. /* Skip Oopes when configured to do so. */
  115. if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
  116. return -EINVAL;
  117. /* Explicitly only take the first part of any new crash.
  118. * If our buffer is larger than kmsg_bytes, this can never happen,
  119. * and if our buffer is smaller than kmsg_bytes, we don't want the
  120. * report split across multiple records.
  121. */
  122. if (part != 1)
  123. return -ENOSPC;
  124. buf = cxt->virt_addr + (cxt->count * cxt->record_size);
  125. res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR);
  126. buf += res;
  127. available -= res;
  128. do_gettimeofday(&timestamp);
  129. res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec);
  130. buf += res;
  131. available -= res;
  132. if (size > available)
  133. size = available;
  134. memcpy(buf, cxt->pstore.buf, size);
  135. memset(buf + size, '\0', available - size);
  136. cxt->count = (cxt->count + 1) % cxt->max_count;
  137. return 0;
  138. }
  139. static int ramoops_pstore_erase(enum pstore_type_id type, u64 id,
  140. struct pstore_info *psi)
  141. {
  142. char *buf;
  143. struct ramoops_context *cxt = psi->data;
  144. if (id >= cxt->max_count)
  145. return -EINVAL;
  146. buf = cxt->virt_addr + (id * cxt->record_size);
  147. memset(buf, '\0', cxt->record_size);
  148. return 0;
  149. }
  150. static struct ramoops_context oops_cxt = {
  151. .pstore = {
  152. .owner = THIS_MODULE,
  153. .name = "ramoops",
  154. .open = ramoops_pstore_open,
  155. .read = ramoops_pstore_read,
  156. .write = ramoops_pstore_write,
  157. .erase = ramoops_pstore_erase,
  158. },
  159. };
  160. static int __init ramoops_probe(struct platform_device *pdev)
  161. {
  162. struct ramoops_platform_data *pdata = pdev->dev.platform_data;
  163. struct ramoops_context *cxt = &oops_cxt;
  164. int err = -EINVAL;
  165. /* Only a single ramoops area allowed at a time, so fail extra
  166. * probes.
  167. */
  168. if (cxt->max_count)
  169. goto fail_out;
  170. if (!pdata->mem_size || !pdata->record_size) {
  171. pr_err("The memory size and the record size must be "
  172. "non-zero\n");
  173. goto fail_out;
  174. }
  175. pdata->mem_size = rounddown_pow_of_two(pdata->mem_size);
  176. pdata->record_size = rounddown_pow_of_two(pdata->record_size);
  177. /* Check for the minimum memory size */
  178. if (pdata->mem_size < MIN_MEM_SIZE &&
  179. pdata->record_size < MIN_MEM_SIZE) {
  180. pr_err("memory size too small, minimum is %lu\n",
  181. MIN_MEM_SIZE);
  182. goto fail_out;
  183. }
  184. if (pdata->mem_size < pdata->record_size) {
  185. pr_err("The memory size must be larger than the "
  186. "records size\n");
  187. goto fail_out;
  188. }
  189. cxt->max_count = pdata->mem_size / pdata->record_size;
  190. cxt->count = 0;
  191. cxt->size = pdata->mem_size;
  192. cxt->phys_addr = pdata->mem_address;
  193. cxt->record_size = pdata->record_size;
  194. cxt->dump_oops = pdata->dump_oops;
  195. cxt->pstore.data = cxt;
  196. cxt->pstore.bufsize = cxt->record_size;
  197. cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
  198. spin_lock_init(&cxt->pstore.buf_lock);
  199. if (!cxt->pstore.buf) {
  200. pr_err("cannot allocate pstore buffer\n");
  201. goto fail_clear;
  202. }
  203. if (!request_mem_region(cxt->phys_addr, cxt->size, "ramoops")) {
  204. pr_err("request mem region (0x%lx@0x%llx) failed\n",
  205. cxt->size, (unsigned long long)cxt->phys_addr);
  206. err = -EINVAL;
  207. goto fail_buf;
  208. }
  209. cxt->virt_addr = ioremap(cxt->phys_addr, cxt->size);
  210. if (!cxt->virt_addr) {
  211. pr_err("ioremap failed\n");
  212. goto fail_mem_region;
  213. }
  214. err = pstore_register(&cxt->pstore);
  215. if (err) {
  216. pr_err("registering with pstore failed\n");
  217. goto fail_iounmap;
  218. }
  219. /*
  220. * Update the module parameter variables as well so they are visible
  221. * through /sys/module/ramoops/parameters/
  222. */
  223. mem_size = pdata->mem_size;
  224. mem_address = pdata->mem_address;
  225. record_size = pdata->record_size;
  226. dump_oops = pdata->dump_oops;
  227. pr_info("attached 0x%lx@0x%llx (%ux0x%zx)\n",
  228. cxt->size, (unsigned long long)cxt->phys_addr,
  229. cxt->max_count, cxt->record_size);
  230. return 0;
  231. fail_iounmap:
  232. iounmap(cxt->virt_addr);
  233. fail_mem_region:
  234. release_mem_region(cxt->phys_addr, cxt->size);
  235. fail_buf:
  236. kfree(cxt->pstore.buf);
  237. fail_clear:
  238. cxt->pstore.bufsize = 0;
  239. cxt->max_count = 0;
  240. fail_out:
  241. return err;
  242. }
  243. static int __exit ramoops_remove(struct platform_device *pdev)
  244. {
  245. #if 0
  246. /* TODO(kees): We cannot unload ramoops since pstore doesn't support
  247. * unregistering yet.
  248. */
  249. struct ramoops_context *cxt = &oops_cxt;
  250. iounmap(cxt->virt_addr);
  251. release_mem_region(cxt->phys_addr, cxt->size);
  252. cxt->max_count = 0;
  253. /* TODO(kees): When pstore supports unregistering, call it here. */
  254. kfree(cxt->pstore.buf);
  255. cxt->pstore.bufsize = 0;
  256. return 0;
  257. #endif
  258. return -EBUSY;
  259. }
  260. static struct platform_driver ramoops_driver = {
  261. .remove = __exit_p(ramoops_remove),
  262. .driver = {
  263. .name = "ramoops",
  264. .owner = THIS_MODULE,
  265. },
  266. };
  267. static int __init ramoops_init(void)
  268. {
  269. int ret;
  270. ret = platform_driver_probe(&ramoops_driver, ramoops_probe);
  271. if (ret == -ENODEV) {
  272. /*
  273. * If we didn't find a platform device, we use module parameters
  274. * building platform data on the fly.
  275. */
  276. pr_info("platform device not found, using module parameters\n");
  277. dummy_data = kzalloc(sizeof(struct ramoops_platform_data),
  278. GFP_KERNEL);
  279. if (!dummy_data)
  280. return -ENOMEM;
  281. dummy_data->mem_size = mem_size;
  282. dummy_data->mem_address = mem_address;
  283. dummy_data->record_size = record_size;
  284. dummy_data->dump_oops = dump_oops;
  285. dummy = platform_create_bundle(&ramoops_driver, ramoops_probe,
  286. NULL, 0, dummy_data,
  287. sizeof(struct ramoops_platform_data));
  288. if (IS_ERR(dummy))
  289. ret = PTR_ERR(dummy);
  290. else
  291. ret = 0;
  292. }
  293. return ret;
  294. }
  295. static void __exit ramoops_exit(void)
  296. {
  297. platform_driver_unregister(&ramoops_driver);
  298. kfree(dummy_data);
  299. }
  300. module_init(ramoops_init);
  301. module_exit(ramoops_exit);
  302. MODULE_LICENSE("GPL");
  303. MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
  304. MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");