crash_dump.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <linux/highmem.h>
  2. #include <linux/bootmem.h>
  3. #include <linux/crash_dump.h>
  4. #include <asm/uaccess.h>
  5. #include <linux/slab.h>
  6. static void *kdump_buf_page;
  7. /**
  8. * copy_oldmem_page - copy one page from "oldmem"
  9. * @pfn: page frame number to be copied
  10. * @buf: target memory address for the copy; this can be in kernel address
  11. * space or user address space (see @userbuf)
  12. * @csize: number of bytes to copy
  13. * @offset: offset in bytes into the page (based on pfn) to begin the copy
  14. * @userbuf: if set, @buf is in user address space, use copy_to_user(),
  15. * otherwise @buf is in kernel address space, use memcpy().
  16. *
  17. * Copy a page from "oldmem". For this page, there is no pte mapped
  18. * in the current kernel.
  19. *
  20. * Calling copy_to_user() in atomic context is not desirable. Hence first
  21. * copying the data to a pre-allocated kernel page and then copying to user
  22. * space in non-atomic context.
  23. */
  24. ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
  25. size_t csize, unsigned long offset, int userbuf)
  26. {
  27. void *vaddr;
  28. if (!csize)
  29. return 0;
  30. vaddr = kmap_atomic_pfn(pfn);
  31. if (!userbuf) {
  32. memcpy(buf, (vaddr + offset), csize);
  33. kunmap_atomic(vaddr);
  34. } else {
  35. if (!kdump_buf_page) {
  36. pr_warn("Kdump: Kdump buffer page not allocated\n");
  37. return -EFAULT;
  38. }
  39. copy_page(kdump_buf_page, vaddr);
  40. kunmap_atomic(vaddr);
  41. if (copy_to_user(buf, (kdump_buf_page + offset), csize))
  42. return -EFAULT;
  43. }
  44. return csize;
  45. }
  46. static int __init kdump_buf_page_init(void)
  47. {
  48. int ret = 0;
  49. kdump_buf_page = kmalloc(PAGE_SIZE, GFP_KERNEL);
  50. if (!kdump_buf_page) {
  51. pr_warn("Kdump: Failed to allocate kdump buffer page\n");
  52. ret = -ENOMEM;
  53. }
  54. return ret;
  55. }
  56. arch_initcall(kdump_buf_page_init);