usercopy.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * User address space access functions.
  3. *
  4. * For licencing details see kernel-base/COPYING
  5. */
  6. #include <linux/highmem.h>
  7. #include <linux/export.h>
  8. #include <asm/word-at-a-time.h>
  9. #include <linux/sched.h>
  10. /*
  11. * We rely on the nested NMI work to allow atomic faults from the NMI path; the
  12. * nested NMI paths are careful to preserve CR2.
  13. */
  14. unsigned long
  15. copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
  16. {
  17. unsigned long ret;
  18. if (__range_not_ok(from, n, TASK_SIZE))
  19. return n;
  20. /*
  21. * Even though this function is typically called from NMI/IRQ context
  22. * disable pagefaults so that its behaviour is consistent even when
  23. * called form other contexts.
  24. */
  25. pagefault_disable();
  26. ret = __copy_from_user_inatomic(to, from, n);
  27. pagefault_enable();
  28. return ret;
  29. }
  30. EXPORT_SYMBOL_GPL(copy_from_user_nmi);
  31. /**
  32. * copy_to_user: - Copy a block of data into user space.
  33. * @to: Destination address, in user space.
  34. * @from: Source address, in kernel space.
  35. * @n: Number of bytes to copy.
  36. *
  37. * Context: User context only. This function may sleep if pagefaults are
  38. * enabled.
  39. *
  40. * Copy data from kernel space to user space.
  41. *
  42. * Returns number of bytes that could not be copied.
  43. * On success, this will be zero.
  44. */
  45. unsigned long _copy_to_user(void __user *to, const void *from, unsigned n)
  46. {
  47. if (access_ok(VERIFY_WRITE, to, n))
  48. n = __copy_to_user(to, from, n);
  49. return n;
  50. }
  51. EXPORT_SYMBOL(_copy_to_user);
  52. /**
  53. * copy_from_user: - Copy a block of data from user space.
  54. * @to: Destination address, in kernel space.
  55. * @from: Source address, in user space.
  56. * @n: Number of bytes to copy.
  57. *
  58. * Context: User context only. This function may sleep if pagefaults are
  59. * enabled.
  60. *
  61. * Copy data from user space to kernel space.
  62. *
  63. * Returns number of bytes that could not be copied.
  64. * On success, this will be zero.
  65. *
  66. * If some data could not be copied, this function will pad the copied
  67. * data to the requested size using zero bytes.
  68. */
  69. unsigned long _copy_from_user(void *to, const void __user *from, unsigned n)
  70. {
  71. if (access_ok(VERIFY_READ, from, n))
  72. n = __copy_from_user(to, from, n);
  73. else
  74. memset(to, 0, n);
  75. return n;
  76. }
  77. EXPORT_SYMBOL(_copy_from_user);