crash_dump.c 2.0 KB

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