ram.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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/pstore_ram.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. static int ramoops_ecc;
  52. module_param_named(ecc, ramoops_ecc, int, 0600);
  53. MODULE_PARM_DESC(ramoops_ecc,
  54. "set to 1 to enable ECC support");
  55. struct ramoops_context {
  56. struct persistent_ram_zone **przs;
  57. phys_addr_t phys_addr;
  58. unsigned long size;
  59. size_t record_size;
  60. int dump_oops;
  61. bool ecc;
  62. unsigned int max_dump_cnt;
  63. unsigned int dump_write_cnt;
  64. unsigned int dump_read_cnt;
  65. struct pstore_info pstore;
  66. };
  67. static struct platform_device *dummy;
  68. static struct ramoops_platform_data *dummy_data;
  69. static int ramoops_pstore_open(struct pstore_info *psi)
  70. {
  71. struct ramoops_context *cxt = psi->data;
  72. cxt->dump_read_cnt = 0;
  73. return 0;
  74. }
  75. static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type,
  76. struct timespec *time,
  77. char **buf,
  78. struct pstore_info *psi)
  79. {
  80. ssize_t size;
  81. struct ramoops_context *cxt = psi->data;
  82. struct persistent_ram_zone *prz;
  83. if (cxt->dump_read_cnt >= cxt->max_dump_cnt)
  84. return -EINVAL;
  85. *id = cxt->dump_read_cnt++;
  86. prz = cxt->przs[*id];
  87. /* Only supports dmesg output so far. */
  88. *type = PSTORE_TYPE_DMESG;
  89. /* TODO(kees): Bogus time for the moment. */
  90. time->tv_sec = 0;
  91. time->tv_nsec = 0;
  92. /* Update old/shadowed buffer. */
  93. persistent_ram_save_old(prz);
  94. size = persistent_ram_old_size(prz);
  95. *buf = kmalloc(size, GFP_KERNEL);
  96. if (*buf == NULL)
  97. return -ENOMEM;
  98. memcpy(*buf, persistent_ram_old(prz), size);
  99. return size;
  100. }
  101. static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz)
  102. {
  103. char *hdr;
  104. struct timeval timestamp;
  105. size_t len;
  106. do_gettimeofday(&timestamp);
  107. hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu\n",
  108. (long)timestamp.tv_sec, (long)timestamp.tv_usec);
  109. WARN_ON_ONCE(!hdr);
  110. len = hdr ? strlen(hdr) : 0;
  111. persistent_ram_write(prz, hdr, len);
  112. kfree(hdr);
  113. return len;
  114. }
  115. static int ramoops_pstore_write(enum pstore_type_id type,
  116. enum kmsg_dump_reason reason,
  117. u64 *id,
  118. unsigned int part,
  119. size_t size, struct pstore_info *psi)
  120. {
  121. struct ramoops_context *cxt = psi->data;
  122. struct persistent_ram_zone *prz = cxt->przs[cxt->dump_write_cnt];
  123. size_t hlen;
  124. /* Currently ramoops is designed to only store dmesg dumps. */
  125. if (type != PSTORE_TYPE_DMESG)
  126. return -EINVAL;
  127. /* Out of the various dmesg dump types, ramoops is currently designed
  128. * to only store crash logs, rather than storing general kernel logs.
  129. */
  130. if (reason != KMSG_DUMP_OOPS &&
  131. reason != KMSG_DUMP_PANIC)
  132. return -EINVAL;
  133. /* Skip Oopes when configured to do so. */
  134. if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
  135. return -EINVAL;
  136. /* Explicitly only take the first part of any new crash.
  137. * If our buffer is larger than kmsg_bytes, this can never happen,
  138. * and if our buffer is smaller than kmsg_bytes, we don't want the
  139. * report split across multiple records.
  140. */
  141. if (part != 1)
  142. return -ENOSPC;
  143. hlen = ramoops_write_kmsg_hdr(prz);
  144. if (size + hlen > prz->buffer_size)
  145. size = prz->buffer_size - hlen;
  146. persistent_ram_write(prz, cxt->pstore.buf, size);
  147. cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt;
  148. return 0;
  149. }
  150. static int ramoops_pstore_erase(enum pstore_type_id type, u64 id,
  151. struct pstore_info *psi)
  152. {
  153. struct ramoops_context *cxt = psi->data;
  154. if (id >= cxt->max_dump_cnt)
  155. return -EINVAL;
  156. persistent_ram_free_old(cxt->przs[id]);
  157. persistent_ram_zap(cxt->przs[id]);
  158. return 0;
  159. }
  160. static struct ramoops_context oops_cxt = {
  161. .pstore = {
  162. .owner = THIS_MODULE,
  163. .name = "ramoops",
  164. .open = ramoops_pstore_open,
  165. .read = ramoops_pstore_read,
  166. .write = ramoops_pstore_write,
  167. .erase = ramoops_pstore_erase,
  168. },
  169. };
  170. static void ramoops_free_przs(struct ramoops_context *cxt)
  171. {
  172. int i;
  173. if (!cxt->przs)
  174. return;
  175. for (i = 0; cxt->przs[i]; i++)
  176. persistent_ram_free(cxt->przs[i]);
  177. kfree(cxt->przs);
  178. }
  179. static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt,
  180. phys_addr_t *paddr, size_t dump_mem_sz)
  181. {
  182. int err = -ENOMEM;
  183. int i;
  184. if (!cxt->record_size)
  185. return 0;
  186. cxt->max_dump_cnt = dump_mem_sz / cxt->record_size;
  187. if (!cxt->max_dump_cnt)
  188. return -ENOMEM;
  189. cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_dump_cnt,
  190. GFP_KERNEL);
  191. if (!cxt->przs) {
  192. dev_err(dev, "failed to initialize a prz array for dumps\n");
  193. return -ENOMEM;
  194. }
  195. for (i = 0; i < cxt->max_dump_cnt; i++) {
  196. size_t sz = cxt->record_size;
  197. cxt->przs[i] = persistent_ram_new(*paddr, sz, cxt->ecc);
  198. if (IS_ERR(cxt->przs[i])) {
  199. err = PTR_ERR(cxt->przs[i]);
  200. dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
  201. sz, (unsigned long long)*paddr, err);
  202. goto fail_prz;
  203. }
  204. *paddr += sz;
  205. }
  206. return 0;
  207. fail_prz:
  208. ramoops_free_przs(cxt);
  209. return err;
  210. }
  211. static int __init ramoops_probe(struct platform_device *pdev)
  212. {
  213. struct device *dev = &pdev->dev;
  214. struct ramoops_platform_data *pdata = pdev->dev.platform_data;
  215. struct ramoops_context *cxt = &oops_cxt;
  216. size_t dump_mem_sz;
  217. phys_addr_t paddr;
  218. int err = -EINVAL;
  219. /* Only a single ramoops area allowed at a time, so fail extra
  220. * probes.
  221. */
  222. if (cxt->max_dump_cnt)
  223. goto fail_out;
  224. if (!pdata->mem_size || !pdata->record_size) {
  225. pr_err("The memory size and the record size must be "
  226. "non-zero\n");
  227. goto fail_out;
  228. }
  229. pdata->mem_size = rounddown_pow_of_two(pdata->mem_size);
  230. pdata->record_size = rounddown_pow_of_two(pdata->record_size);
  231. cxt->dump_read_cnt = 0;
  232. cxt->size = pdata->mem_size;
  233. cxt->phys_addr = pdata->mem_address;
  234. cxt->record_size = pdata->record_size;
  235. cxt->dump_oops = pdata->dump_oops;
  236. cxt->ecc = pdata->ecc;
  237. paddr = cxt->phys_addr;
  238. dump_mem_sz = cxt->size;
  239. err = ramoops_init_przs(dev, cxt, &paddr, dump_mem_sz);
  240. if (err) {
  241. pr_err("memory size too small, minimum is %lu\n",
  242. cxt->record_size);
  243. goto fail_count;
  244. }
  245. cxt->pstore.data = cxt;
  246. cxt->pstore.bufsize = cxt->przs[0]->buffer_size;
  247. cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
  248. spin_lock_init(&cxt->pstore.buf_lock);
  249. if (!cxt->pstore.buf) {
  250. pr_err("cannot allocate pstore buffer\n");
  251. goto fail_clear;
  252. }
  253. err = pstore_register(&cxt->pstore);
  254. if (err) {
  255. pr_err("registering with pstore failed\n");
  256. goto fail_buf;
  257. }
  258. /*
  259. * Update the module parameter variables as well so they are visible
  260. * through /sys/module/ramoops/parameters/
  261. */
  262. mem_size = pdata->mem_size;
  263. mem_address = pdata->mem_address;
  264. record_size = pdata->record_size;
  265. dump_oops = pdata->dump_oops;
  266. pr_info("attached 0x%lx@0x%llx (%ux0x%zx), ecc: %s\n",
  267. cxt->size, (unsigned long long)cxt->phys_addr,
  268. cxt->max_dump_cnt, cxt->record_size,
  269. ramoops_ecc ? "on" : "off");
  270. return 0;
  271. fail_buf:
  272. kfree(cxt->pstore.buf);
  273. fail_clear:
  274. cxt->pstore.bufsize = 0;
  275. cxt->max_dump_cnt = 0;
  276. fail_count:
  277. ramoops_free_przs(cxt);
  278. fail_out:
  279. return err;
  280. }
  281. static int __exit ramoops_remove(struct platform_device *pdev)
  282. {
  283. #if 0
  284. /* TODO(kees): We cannot unload ramoops since pstore doesn't support
  285. * unregistering yet.
  286. */
  287. struct ramoops_context *cxt = &oops_cxt;
  288. iounmap(cxt->virt_addr);
  289. release_mem_region(cxt->phys_addr, cxt->size);
  290. cxt->max_dump_cnt = 0;
  291. /* TODO(kees): When pstore supports unregistering, call it here. */
  292. kfree(cxt->pstore.buf);
  293. cxt->pstore.bufsize = 0;
  294. return 0;
  295. #endif
  296. return -EBUSY;
  297. }
  298. static struct platform_driver ramoops_driver = {
  299. .remove = __exit_p(ramoops_remove),
  300. .driver = {
  301. .name = "ramoops",
  302. .owner = THIS_MODULE,
  303. },
  304. };
  305. static int __init ramoops_init(void)
  306. {
  307. int ret;
  308. ret = platform_driver_probe(&ramoops_driver, ramoops_probe);
  309. if (ret == -ENODEV) {
  310. /*
  311. * If we didn't find a platform device, we use module parameters
  312. * building platform data on the fly.
  313. */
  314. pr_info("platform device not found, using module parameters\n");
  315. dummy_data = kzalloc(sizeof(struct ramoops_platform_data),
  316. GFP_KERNEL);
  317. if (!dummy_data)
  318. return -ENOMEM;
  319. dummy_data->mem_size = mem_size;
  320. dummy_data->mem_address = mem_address;
  321. dummy_data->record_size = record_size;
  322. dummy_data->dump_oops = dump_oops;
  323. dummy_data->ecc = ramoops_ecc;
  324. dummy = platform_create_bundle(&ramoops_driver, ramoops_probe,
  325. NULL, 0, dummy_data,
  326. sizeof(struct ramoops_platform_data));
  327. if (IS_ERR(dummy))
  328. ret = PTR_ERR(dummy);
  329. else
  330. ret = 0;
  331. }
  332. return ret;
  333. }
  334. static void __exit ramoops_exit(void)
  335. {
  336. platform_driver_unregister(&ramoops_driver);
  337. kfree(dummy_data);
  338. }
  339. module_init(ramoops_init);
  340. module_exit(ramoops_exit);
  341. MODULE_LICENSE("GPL");
  342. MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
  343. MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");