ram.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 ramoops_console_size = MIN_MEM_SIZE;
  40. module_param_named(console_size, ramoops_console_size, ulong, 0400);
  41. MODULE_PARM_DESC(console_size, "size of kernel console log");
  42. static ulong mem_address;
  43. module_param(mem_address, ulong, 0400);
  44. MODULE_PARM_DESC(mem_address,
  45. "start of reserved RAM used to store oops/panic logs");
  46. static ulong mem_size;
  47. module_param(mem_size, ulong, 0400);
  48. MODULE_PARM_DESC(mem_size,
  49. "size of reserved RAM used to store oops/panic logs");
  50. static int dump_oops = 1;
  51. module_param(dump_oops, int, 0600);
  52. MODULE_PARM_DESC(dump_oops,
  53. "set to 1 to dump oopses, 0 to only dump panics (default 1)");
  54. static int ramoops_ecc;
  55. module_param_named(ecc, ramoops_ecc, int, 0600);
  56. MODULE_PARM_DESC(ramoops_ecc,
  57. "if non-zero, the option enables ECC support and specifies "
  58. "ECC buffer size in bytes (1 is a special value, means 16 "
  59. "bytes ECC)");
  60. struct ramoops_context {
  61. struct persistent_ram_zone **przs;
  62. struct persistent_ram_zone *cprz;
  63. phys_addr_t phys_addr;
  64. unsigned long size;
  65. size_t record_size;
  66. size_t console_size;
  67. int dump_oops;
  68. int ecc_size;
  69. unsigned int max_dump_cnt;
  70. unsigned int dump_write_cnt;
  71. unsigned int dump_read_cnt;
  72. unsigned int console_read_cnt;
  73. struct pstore_info pstore;
  74. };
  75. static struct platform_device *dummy;
  76. static struct ramoops_platform_data *dummy_data;
  77. static int ramoops_pstore_open(struct pstore_info *psi)
  78. {
  79. struct ramoops_context *cxt = psi->data;
  80. cxt->dump_read_cnt = 0;
  81. cxt->console_read_cnt = 0;
  82. return 0;
  83. }
  84. static struct persistent_ram_zone *
  85. ramoops_get_next_prz(struct persistent_ram_zone *przs[], uint *c, uint max,
  86. u64 *id,
  87. enum pstore_type_id *typep, enum pstore_type_id type,
  88. bool update)
  89. {
  90. struct persistent_ram_zone *prz;
  91. int i = (*c)++;
  92. if (i >= max)
  93. return NULL;
  94. prz = przs[i];
  95. if (update) {
  96. /* Update old/shadowed buffer. */
  97. persistent_ram_save_old(prz);
  98. if (!persistent_ram_old_size(prz))
  99. return NULL;
  100. }
  101. *typep = type;
  102. *id = i;
  103. return prz;
  104. }
  105. static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type,
  106. struct timespec *time,
  107. char **buf,
  108. struct pstore_info *psi)
  109. {
  110. ssize_t size;
  111. struct ramoops_context *cxt = psi->data;
  112. struct persistent_ram_zone *prz;
  113. prz = ramoops_get_next_prz(cxt->przs, &cxt->dump_read_cnt,
  114. cxt->max_dump_cnt, id, type,
  115. PSTORE_TYPE_DMESG, 1);
  116. if (!prz)
  117. prz = ramoops_get_next_prz(&cxt->cprz, &cxt->console_read_cnt,
  118. 1, id, type, PSTORE_TYPE_CONSOLE, 0);
  119. if (!prz)
  120. return 0;
  121. /* TODO(kees): Bogus time for the moment. */
  122. time->tv_sec = 0;
  123. time->tv_nsec = 0;
  124. size = persistent_ram_old_size(prz);
  125. *buf = kmalloc(size, GFP_KERNEL);
  126. if (*buf == NULL)
  127. return -ENOMEM;
  128. memcpy(*buf, persistent_ram_old(prz), size);
  129. return size;
  130. }
  131. static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz)
  132. {
  133. char *hdr;
  134. struct timeval timestamp;
  135. size_t len;
  136. do_gettimeofday(&timestamp);
  137. hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu\n",
  138. (long)timestamp.tv_sec, (long)timestamp.tv_usec);
  139. WARN_ON_ONCE(!hdr);
  140. len = hdr ? strlen(hdr) : 0;
  141. persistent_ram_write(prz, hdr, len);
  142. kfree(hdr);
  143. return len;
  144. }
  145. static int ramoops_pstore_write_buf(enum pstore_type_id type,
  146. enum kmsg_dump_reason reason,
  147. u64 *id, unsigned int part,
  148. const char *buf, size_t size,
  149. struct pstore_info *psi)
  150. {
  151. struct ramoops_context *cxt = psi->data;
  152. struct persistent_ram_zone *prz = cxt->przs[cxt->dump_write_cnt];
  153. size_t hlen;
  154. if (type == PSTORE_TYPE_CONSOLE) {
  155. if (!cxt->cprz)
  156. return -ENOMEM;
  157. persistent_ram_write(cxt->cprz, buf, size);
  158. return 0;
  159. }
  160. if (type != PSTORE_TYPE_DMESG)
  161. return -EINVAL;
  162. /* Out of the various dmesg dump types, ramoops is currently designed
  163. * to only store crash logs, rather than storing general kernel logs.
  164. */
  165. if (reason != KMSG_DUMP_OOPS &&
  166. reason != KMSG_DUMP_PANIC)
  167. return -EINVAL;
  168. /* Skip Oopes when configured to do so. */
  169. if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
  170. return -EINVAL;
  171. /* Explicitly only take the first part of any new crash.
  172. * If our buffer is larger than kmsg_bytes, this can never happen,
  173. * and if our buffer is smaller than kmsg_bytes, we don't want the
  174. * report split across multiple records.
  175. */
  176. if (part != 1)
  177. return -ENOSPC;
  178. hlen = ramoops_write_kmsg_hdr(prz);
  179. if (size + hlen > prz->buffer_size)
  180. size = prz->buffer_size - hlen;
  181. persistent_ram_write(prz, buf, size);
  182. cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt;
  183. return 0;
  184. }
  185. static int ramoops_pstore_erase(enum pstore_type_id type, u64 id,
  186. struct pstore_info *psi)
  187. {
  188. struct ramoops_context *cxt = psi->data;
  189. struct persistent_ram_zone *prz;
  190. switch (type) {
  191. case PSTORE_TYPE_DMESG:
  192. if (id >= cxt->max_dump_cnt)
  193. return -EINVAL;
  194. prz = cxt->przs[id];
  195. break;
  196. case PSTORE_TYPE_CONSOLE:
  197. prz = cxt->cprz;
  198. break;
  199. default:
  200. return -EINVAL;
  201. }
  202. persistent_ram_free_old(prz);
  203. persistent_ram_zap(prz);
  204. return 0;
  205. }
  206. static struct ramoops_context oops_cxt = {
  207. .pstore = {
  208. .owner = THIS_MODULE,
  209. .name = "ramoops",
  210. .open = ramoops_pstore_open,
  211. .read = ramoops_pstore_read,
  212. .write_buf = ramoops_pstore_write_buf,
  213. .erase = ramoops_pstore_erase,
  214. },
  215. };
  216. static void ramoops_free_przs(struct ramoops_context *cxt)
  217. {
  218. int i;
  219. if (!cxt->przs)
  220. return;
  221. for (i = 0; !IS_ERR_OR_NULL(cxt->przs[i]); i++)
  222. persistent_ram_free(cxt->przs[i]);
  223. kfree(cxt->przs);
  224. }
  225. static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt,
  226. phys_addr_t *paddr, size_t dump_mem_sz)
  227. {
  228. int err = -ENOMEM;
  229. int i;
  230. if (!cxt->record_size)
  231. return 0;
  232. cxt->max_dump_cnt = dump_mem_sz / cxt->record_size;
  233. if (!cxt->max_dump_cnt)
  234. return -ENOMEM;
  235. cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_dump_cnt,
  236. GFP_KERNEL);
  237. if (!cxt->przs) {
  238. dev_err(dev, "failed to initialize a prz array for dumps\n");
  239. return -ENOMEM;
  240. }
  241. for (i = 0; i < cxt->max_dump_cnt; i++) {
  242. size_t sz = cxt->record_size;
  243. cxt->przs[i] = persistent_ram_new(*paddr, sz, cxt->ecc_size);
  244. if (IS_ERR(cxt->przs[i])) {
  245. err = PTR_ERR(cxt->przs[i]);
  246. dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
  247. sz, (unsigned long long)*paddr, err);
  248. goto fail_prz;
  249. }
  250. *paddr += sz;
  251. }
  252. return 0;
  253. fail_prz:
  254. ramoops_free_przs(cxt);
  255. return err;
  256. }
  257. static int ramoops_init_prz(struct device *dev, struct ramoops_context *cxt,
  258. struct persistent_ram_zone **prz,
  259. phys_addr_t *paddr, size_t sz)
  260. {
  261. if (!sz)
  262. return 0;
  263. if (*paddr + sz > *paddr + cxt->size)
  264. return -ENOMEM;
  265. *prz = persistent_ram_new(*paddr, sz, cxt->ecc_size);
  266. if (IS_ERR(*prz)) {
  267. int err = PTR_ERR(*prz);
  268. dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
  269. sz, (unsigned long long)*paddr, err);
  270. return err;
  271. }
  272. persistent_ram_zap(*prz);
  273. *paddr += sz;
  274. return 0;
  275. }
  276. static int __devinit ramoops_probe(struct platform_device *pdev)
  277. {
  278. struct device *dev = &pdev->dev;
  279. struct ramoops_platform_data *pdata = pdev->dev.platform_data;
  280. struct ramoops_context *cxt = &oops_cxt;
  281. size_t dump_mem_sz;
  282. phys_addr_t paddr;
  283. int err = -EINVAL;
  284. /* Only a single ramoops area allowed at a time, so fail extra
  285. * probes.
  286. */
  287. if (cxt->max_dump_cnt)
  288. goto fail_out;
  289. if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size)) {
  290. pr_err("The memory size and the record/console size must be "
  291. "non-zero\n");
  292. goto fail_out;
  293. }
  294. pdata->mem_size = rounddown_pow_of_two(pdata->mem_size);
  295. pdata->record_size = rounddown_pow_of_two(pdata->record_size);
  296. pdata->console_size = rounddown_pow_of_two(pdata->console_size);
  297. cxt->dump_read_cnt = 0;
  298. cxt->size = pdata->mem_size;
  299. cxt->phys_addr = pdata->mem_address;
  300. cxt->record_size = pdata->record_size;
  301. cxt->console_size = pdata->console_size;
  302. cxt->dump_oops = pdata->dump_oops;
  303. cxt->ecc_size = pdata->ecc_size;
  304. paddr = cxt->phys_addr;
  305. dump_mem_sz = cxt->size - cxt->console_size;
  306. err = ramoops_init_przs(dev, cxt, &paddr, dump_mem_sz);
  307. if (err)
  308. goto fail_out;
  309. err = ramoops_init_prz(dev, cxt, &cxt->cprz, &paddr, cxt->console_size);
  310. if (err)
  311. goto fail_init_cprz;
  312. if (!cxt->przs && !cxt->cprz) {
  313. pr_err("memory size too small, minimum is %lu\n",
  314. cxt->console_size + cxt->record_size);
  315. goto fail_cnt;
  316. }
  317. cxt->pstore.data = cxt;
  318. /*
  319. * Console can handle any buffer size, so prefer dumps buffer
  320. * size since usually it is smaller.
  321. */
  322. if (cxt->przs)
  323. cxt->pstore.bufsize = cxt->przs[0]->buffer_size;
  324. else
  325. cxt->pstore.bufsize = cxt->cprz->buffer_size;
  326. cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
  327. spin_lock_init(&cxt->pstore.buf_lock);
  328. if (!cxt->pstore.buf) {
  329. pr_err("cannot allocate pstore buffer\n");
  330. goto fail_clear;
  331. }
  332. err = pstore_register(&cxt->pstore);
  333. if (err) {
  334. pr_err("registering with pstore failed\n");
  335. goto fail_buf;
  336. }
  337. /*
  338. * Update the module parameter variables as well so they are visible
  339. * through /sys/module/ramoops/parameters/
  340. */
  341. mem_size = pdata->mem_size;
  342. mem_address = pdata->mem_address;
  343. record_size = pdata->record_size;
  344. dump_oops = pdata->dump_oops;
  345. pr_info("attached 0x%lx@0x%llx, ecc: %d\n",
  346. cxt->size, (unsigned long long)cxt->phys_addr,
  347. cxt->ecc_size);
  348. return 0;
  349. fail_buf:
  350. kfree(cxt->pstore.buf);
  351. fail_clear:
  352. cxt->pstore.bufsize = 0;
  353. cxt->max_dump_cnt = 0;
  354. fail_cnt:
  355. kfree(cxt->cprz);
  356. fail_init_cprz:
  357. ramoops_free_przs(cxt);
  358. fail_out:
  359. return err;
  360. }
  361. static int __exit ramoops_remove(struct platform_device *pdev)
  362. {
  363. #if 0
  364. /* TODO(kees): We cannot unload ramoops since pstore doesn't support
  365. * unregistering yet.
  366. */
  367. struct ramoops_context *cxt = &oops_cxt;
  368. iounmap(cxt->virt_addr);
  369. release_mem_region(cxt->phys_addr, cxt->size);
  370. cxt->max_dump_cnt = 0;
  371. /* TODO(kees): When pstore supports unregistering, call it here. */
  372. kfree(cxt->pstore.buf);
  373. cxt->pstore.bufsize = 0;
  374. return 0;
  375. #endif
  376. return -EBUSY;
  377. }
  378. static struct platform_driver ramoops_driver = {
  379. .probe = ramoops_probe,
  380. .remove = __exit_p(ramoops_remove),
  381. .driver = {
  382. .name = "ramoops",
  383. .owner = THIS_MODULE,
  384. },
  385. };
  386. static void ramoops_register_dummy(void)
  387. {
  388. if (!mem_size)
  389. return;
  390. pr_info("using module parameters\n");
  391. dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL);
  392. if (!dummy_data) {
  393. pr_info("could not allocate pdata\n");
  394. return;
  395. }
  396. dummy_data->mem_size = mem_size;
  397. dummy_data->mem_address = mem_address;
  398. dummy_data->record_size = record_size;
  399. dummy_data->console_size = ramoops_console_size;
  400. dummy_data->dump_oops = dump_oops;
  401. /*
  402. * For backwards compatibility ramoops.ecc=1 means 16 bytes ECC
  403. * (using 1 byte for ECC isn't much of use anyway).
  404. */
  405. dummy_data->ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
  406. dummy = platform_device_register_data(NULL, "ramoops", -1,
  407. dummy_data, sizeof(struct ramoops_platform_data));
  408. if (IS_ERR(dummy)) {
  409. pr_info("could not create platform device: %ld\n",
  410. PTR_ERR(dummy));
  411. }
  412. }
  413. static int __init ramoops_init(void)
  414. {
  415. ramoops_register_dummy();
  416. return platform_driver_register(&ramoops_driver);
  417. }
  418. postcore_initcall(ramoops_init);
  419. static void __exit ramoops_exit(void)
  420. {
  421. platform_driver_unregister(&ramoops_driver);
  422. kfree(dummy_data);
  423. }
  424. module_exit(ramoops_exit);
  425. MODULE_LICENSE("GPL");
  426. MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
  427. MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");