crash_dump_32.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Memory preserving reboot related code.
  4. *
  5. * Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
  6. * Copyright (C) IBM Corporation, 2004. All rights reserved
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/errno.h>
  10. #include <linux/highmem.h>
  11. #include <linux/crash_dump.h>
  12. #include <linux/uaccess.h>
  13. static void *kdump_buf_page;
  14. static inline bool is_crashed_pfn_valid(unsigned long pfn)
  15. {
  16. #ifndef CONFIG_X86_PAE
  17. /*
  18. * non-PAE kdump kernel executed from a PAE one will crop high pte
  19. * bits and poke unwanted space counting again from address 0, we
  20. * don't want that. pte must fit into unsigned long. In fact the
  21. * test checks high 12 bits for being zero (pfn will be shifted left
  22. * by PAGE_SHIFT).
  23. */
  24. return pte_pfn(pfn_pte(pfn, __pgprot(0))) == pfn;
  25. #else
  26. return true;
  27. #endif
  28. }
  29. /**
  30. * copy_oldmem_page - copy one page from "oldmem"
  31. * @pfn: page frame number to be copied
  32. * @buf: target memory address for the copy; this can be in kernel address
  33. * space or user address space (see @userbuf)
  34. * @csize: number of bytes to copy
  35. * @offset: offset in bytes into the page (based on pfn) to begin the copy
  36. * @userbuf: if set, @buf is in user address space, use copy_to_user(),
  37. * otherwise @buf is in kernel address space, use memcpy().
  38. *
  39. * Copy a page from "oldmem". For this page, there is no pte mapped
  40. * in the current kernel. We stitch up a pte, similar to kmap_atomic.
  41. *
  42. * Calling copy_to_user() in atomic context is not desirable. Hence first
  43. * copying the data to a pre-allocated kernel page and then copying to user
  44. * space in non-atomic context.
  45. */
  46. ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
  47. size_t csize, unsigned long offset, int userbuf)
  48. {
  49. void *vaddr;
  50. if (!csize)
  51. return 0;
  52. if (!is_crashed_pfn_valid(pfn))
  53. return -EFAULT;
  54. vaddr = kmap_atomic_pfn(pfn);
  55. if (!userbuf) {
  56. memcpy(buf, (vaddr + offset), csize);
  57. kunmap_atomic(vaddr);
  58. } else {
  59. if (!kdump_buf_page) {
  60. printk(KERN_WARNING "Kdump: Kdump buffer page not"
  61. " allocated\n");
  62. kunmap_atomic(vaddr);
  63. return -EFAULT;
  64. }
  65. copy_page(kdump_buf_page, vaddr);
  66. kunmap_atomic(vaddr);
  67. if (copy_to_user(buf, (kdump_buf_page + offset), csize))
  68. return -EFAULT;
  69. }
  70. return csize;
  71. }
  72. static int __init kdump_buf_page_init(void)
  73. {
  74. int ret = 0;
  75. kdump_buf_page = kmalloc(PAGE_SIZE, GFP_KERNEL);
  76. if (!kdump_buf_page) {
  77. printk(KERN_WARNING "Kdump: Failed to allocate kdump buffer"
  78. " page\n");
  79. ret = -ENOMEM;
  80. }
  81. return ret;
  82. }
  83. arch_initcall(kdump_buf_page_init);