crash_dump_32.c 2.6 KB

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