usercopy.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * User address space access functions.
  3. * The non inlined parts of asm-i386/uaccess.h are here.
  4. *
  5. * Copyright 1997 Andi Kleen <ak@muc.de>
  6. * Copyright 1997 Linus Torvalds
  7. */
  8. #include <linux/config.h>
  9. #include <linux/mm.h>
  10. #include <linux/highmem.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/module.h>
  13. #include <asm/uaccess.h>
  14. #include <asm/mmx.h>
  15. static inline int __movsl_is_ok(unsigned long a1, unsigned long a2, unsigned long n)
  16. {
  17. #ifdef CONFIG_X86_INTEL_USERCOPY
  18. if (n >= 64 && ((a1 ^ a2) & movsl_mask.mask))
  19. return 0;
  20. #endif
  21. return 1;
  22. }
  23. #define movsl_is_ok(a1,a2,n) \
  24. __movsl_is_ok((unsigned long)(a1),(unsigned long)(a2),(n))
  25. /*
  26. * Copy a null terminated string from userspace.
  27. */
  28. #define __do_strncpy_from_user(dst,src,count,res) \
  29. do { \
  30. int __d0, __d1, __d2; \
  31. might_sleep(); \
  32. __asm__ __volatile__( \
  33. " testl %1,%1\n" \
  34. " jz 2f\n" \
  35. "0: lodsb\n" \
  36. " stosb\n" \
  37. " testb %%al,%%al\n" \
  38. " jz 1f\n" \
  39. " decl %1\n" \
  40. " jnz 0b\n" \
  41. "1: subl %1,%0\n" \
  42. "2:\n" \
  43. ".section .fixup,\"ax\"\n" \
  44. "3: movl %5,%0\n" \
  45. " jmp 2b\n" \
  46. ".previous\n" \
  47. ".section __ex_table,\"a\"\n" \
  48. " .align 4\n" \
  49. " .long 0b,3b\n" \
  50. ".previous" \
  51. : "=d"(res), "=c"(count), "=&a" (__d0), "=&S" (__d1), \
  52. "=&D" (__d2) \
  53. : "i"(-EFAULT), "0"(count), "1"(count), "3"(src), "4"(dst) \
  54. : "memory"); \
  55. } while (0)
  56. /**
  57. * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
  58. * @dst: Destination address, in kernel space. This buffer must be at
  59. * least @count bytes long.
  60. * @src: Source address, in user space.
  61. * @count: Maximum number of bytes to copy, including the trailing NUL.
  62. *
  63. * Copies a NUL-terminated string from userspace to kernel space.
  64. * Caller must check the specified block with access_ok() before calling
  65. * this function.
  66. *
  67. * On success, returns the length of the string (not including the trailing
  68. * NUL).
  69. *
  70. * If access to userspace fails, returns -EFAULT (some data may have been
  71. * copied).
  72. *
  73. * If @count is smaller than the length of the string, copies @count bytes
  74. * and returns @count.
  75. */
  76. long
  77. __strncpy_from_user(char *dst, const char __user *src, long count)
  78. {
  79. long res;
  80. __do_strncpy_from_user(dst, src, count, res);
  81. return res;
  82. }
  83. EXPORT_SYMBOL(__strncpy_from_user);
  84. /**
  85. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  86. * @dst: Destination address, in kernel space. This buffer must be at
  87. * least @count bytes long.
  88. * @src: Source address, in user space.
  89. * @count: Maximum number of bytes to copy, including the trailing NUL.
  90. *
  91. * Copies a NUL-terminated string from userspace to kernel space.
  92. *
  93. * On success, returns the length of the string (not including the trailing
  94. * NUL).
  95. *
  96. * If access to userspace fails, returns -EFAULT (some data may have been
  97. * copied).
  98. *
  99. * If @count is smaller than the length of the string, copies @count bytes
  100. * and returns @count.
  101. */
  102. long
  103. strncpy_from_user(char *dst, const char __user *src, long count)
  104. {
  105. long res = -EFAULT;
  106. if (access_ok(VERIFY_READ, src, 1))
  107. __do_strncpy_from_user(dst, src, count, res);
  108. return res;
  109. }
  110. EXPORT_SYMBOL(strncpy_from_user);
  111. /*
  112. * Zero Userspace
  113. */
  114. #define __do_clear_user(addr,size) \
  115. do { \
  116. int __d0; \
  117. might_sleep(); \
  118. __asm__ __volatile__( \
  119. "0: rep; stosl\n" \
  120. " movl %2,%0\n" \
  121. "1: rep; stosb\n" \
  122. "2:\n" \
  123. ".section .fixup,\"ax\"\n" \
  124. "3: lea 0(%2,%0,4),%0\n" \
  125. " jmp 2b\n" \
  126. ".previous\n" \
  127. ".section __ex_table,\"a\"\n" \
  128. " .align 4\n" \
  129. " .long 0b,3b\n" \
  130. " .long 1b,2b\n" \
  131. ".previous" \
  132. : "=&c"(size), "=&D" (__d0) \
  133. : "r"(size & 3), "0"(size / 4), "1"(addr), "a"(0)); \
  134. } while (0)
  135. /**
  136. * clear_user: - Zero a block of memory in user space.
  137. * @to: Destination address, in user space.
  138. * @n: Number of bytes to zero.
  139. *
  140. * Zero a block of memory in user space.
  141. *
  142. * Returns number of bytes that could not be cleared.
  143. * On success, this will be zero.
  144. */
  145. unsigned long
  146. clear_user(void __user *to, unsigned long n)
  147. {
  148. might_sleep();
  149. if (access_ok(VERIFY_WRITE, to, n))
  150. __do_clear_user(to, n);
  151. return n;
  152. }
  153. EXPORT_SYMBOL(clear_user);
  154. /**
  155. * __clear_user: - Zero a block of memory in user space, with less checking.
  156. * @to: Destination address, in user space.
  157. * @n: Number of bytes to zero.
  158. *
  159. * Zero a block of memory in user space. Caller must check
  160. * the specified block with access_ok() before calling this function.
  161. *
  162. * Returns number of bytes that could not be cleared.
  163. * On success, this will be zero.
  164. */
  165. unsigned long
  166. __clear_user(void __user *to, unsigned long n)
  167. {
  168. __do_clear_user(to, n);
  169. return n;
  170. }
  171. EXPORT_SYMBOL(__clear_user);
  172. /**
  173. * strlen_user: - Get the size of a string in user space.
  174. * @s: The string to measure.
  175. * @n: The maximum valid length
  176. *
  177. * Get the size of a NUL-terminated string in user space.
  178. *
  179. * Returns the size of the string INCLUDING the terminating NUL.
  180. * On exception, returns 0.
  181. * If the string is too long, returns a value greater than @n.
  182. */
  183. long strnlen_user(const char __user *s, long n)
  184. {
  185. unsigned long mask = -__addr_ok(s);
  186. unsigned long res, tmp;
  187. might_sleep();
  188. __asm__ __volatile__(
  189. " testl %0, %0\n"
  190. " jz 3f\n"
  191. " andl %0,%%ecx\n"
  192. "0: repne; scasb\n"
  193. " setne %%al\n"
  194. " subl %%ecx,%0\n"
  195. " addl %0,%%eax\n"
  196. "1:\n"
  197. ".section .fixup,\"ax\"\n"
  198. "2: xorl %%eax,%%eax\n"
  199. " jmp 1b\n"
  200. "3: movb $1,%%al\n"
  201. " jmp 1b\n"
  202. ".previous\n"
  203. ".section __ex_table,\"a\"\n"
  204. " .align 4\n"
  205. " .long 0b,2b\n"
  206. ".previous"
  207. :"=r" (n), "=D" (s), "=a" (res), "=c" (tmp)
  208. :"0" (n), "1" (s), "2" (0), "3" (mask)
  209. :"cc");
  210. return res & mask;
  211. }
  212. EXPORT_SYMBOL(strnlen_user);
  213. #ifdef CONFIG_X86_INTEL_USERCOPY
  214. static unsigned long
  215. __copy_user_intel(void __user *to, const void *from, unsigned long size)
  216. {
  217. int d0, d1;
  218. __asm__ __volatile__(
  219. " .align 2,0x90\n"
  220. "1: movl 32(%4), %%eax\n"
  221. " cmpl $67, %0\n"
  222. " jbe 3f\n"
  223. "2: movl 64(%4), %%eax\n"
  224. " .align 2,0x90\n"
  225. "3: movl 0(%4), %%eax\n"
  226. "4: movl 4(%4), %%edx\n"
  227. "5: movl %%eax, 0(%3)\n"
  228. "6: movl %%edx, 4(%3)\n"
  229. "7: movl 8(%4), %%eax\n"
  230. "8: movl 12(%4),%%edx\n"
  231. "9: movl %%eax, 8(%3)\n"
  232. "10: movl %%edx, 12(%3)\n"
  233. "11: movl 16(%4), %%eax\n"
  234. "12: movl 20(%4), %%edx\n"
  235. "13: movl %%eax, 16(%3)\n"
  236. "14: movl %%edx, 20(%3)\n"
  237. "15: movl 24(%4), %%eax\n"
  238. "16: movl 28(%4), %%edx\n"
  239. "17: movl %%eax, 24(%3)\n"
  240. "18: movl %%edx, 28(%3)\n"
  241. "19: movl 32(%4), %%eax\n"
  242. "20: movl 36(%4), %%edx\n"
  243. "21: movl %%eax, 32(%3)\n"
  244. "22: movl %%edx, 36(%3)\n"
  245. "23: movl 40(%4), %%eax\n"
  246. "24: movl 44(%4), %%edx\n"
  247. "25: movl %%eax, 40(%3)\n"
  248. "26: movl %%edx, 44(%3)\n"
  249. "27: movl 48(%4), %%eax\n"
  250. "28: movl 52(%4), %%edx\n"
  251. "29: movl %%eax, 48(%3)\n"
  252. "30: movl %%edx, 52(%3)\n"
  253. "31: movl 56(%4), %%eax\n"
  254. "32: movl 60(%4), %%edx\n"
  255. "33: movl %%eax, 56(%3)\n"
  256. "34: movl %%edx, 60(%3)\n"
  257. " addl $-64, %0\n"
  258. " addl $64, %4\n"
  259. " addl $64, %3\n"
  260. " cmpl $63, %0\n"
  261. " ja 1b\n"
  262. "35: movl %0, %%eax\n"
  263. " shrl $2, %0\n"
  264. " andl $3, %%eax\n"
  265. " cld\n"
  266. "99: rep; movsl\n"
  267. "36: movl %%eax, %0\n"
  268. "37: rep; movsb\n"
  269. "100:\n"
  270. ".section .fixup,\"ax\"\n"
  271. "101: lea 0(%%eax,%0,4),%0\n"
  272. " jmp 100b\n"
  273. ".previous\n"
  274. ".section __ex_table,\"a\"\n"
  275. " .align 4\n"
  276. " .long 1b,100b\n"
  277. " .long 2b,100b\n"
  278. " .long 3b,100b\n"
  279. " .long 4b,100b\n"
  280. " .long 5b,100b\n"
  281. " .long 6b,100b\n"
  282. " .long 7b,100b\n"
  283. " .long 8b,100b\n"
  284. " .long 9b,100b\n"
  285. " .long 10b,100b\n"
  286. " .long 11b,100b\n"
  287. " .long 12b,100b\n"
  288. " .long 13b,100b\n"
  289. " .long 14b,100b\n"
  290. " .long 15b,100b\n"
  291. " .long 16b,100b\n"
  292. " .long 17b,100b\n"
  293. " .long 18b,100b\n"
  294. " .long 19b,100b\n"
  295. " .long 20b,100b\n"
  296. " .long 21b,100b\n"
  297. " .long 22b,100b\n"
  298. " .long 23b,100b\n"
  299. " .long 24b,100b\n"
  300. " .long 25b,100b\n"
  301. " .long 26b,100b\n"
  302. " .long 27b,100b\n"
  303. " .long 28b,100b\n"
  304. " .long 29b,100b\n"
  305. " .long 30b,100b\n"
  306. " .long 31b,100b\n"
  307. " .long 32b,100b\n"
  308. " .long 33b,100b\n"
  309. " .long 34b,100b\n"
  310. " .long 35b,100b\n"
  311. " .long 36b,100b\n"
  312. " .long 37b,100b\n"
  313. " .long 99b,101b\n"
  314. ".previous"
  315. : "=&c"(size), "=&D" (d0), "=&S" (d1)
  316. : "1"(to), "2"(from), "0"(size)
  317. : "eax", "edx", "memory");
  318. return size;
  319. }
  320. static unsigned long
  321. __copy_user_zeroing_intel(void *to, const void __user *from, unsigned long size)
  322. {
  323. int d0, d1;
  324. __asm__ __volatile__(
  325. " .align 2,0x90\n"
  326. "0: movl 32(%4), %%eax\n"
  327. " cmpl $67, %0\n"
  328. " jbe 2f\n"
  329. "1: movl 64(%4), %%eax\n"
  330. " .align 2,0x90\n"
  331. "2: movl 0(%4), %%eax\n"
  332. "21: movl 4(%4), %%edx\n"
  333. " movl %%eax, 0(%3)\n"
  334. " movl %%edx, 4(%3)\n"
  335. "3: movl 8(%4), %%eax\n"
  336. "31: movl 12(%4),%%edx\n"
  337. " movl %%eax, 8(%3)\n"
  338. " movl %%edx, 12(%3)\n"
  339. "4: movl 16(%4), %%eax\n"
  340. "41: movl 20(%4), %%edx\n"
  341. " movl %%eax, 16(%3)\n"
  342. " movl %%edx, 20(%3)\n"
  343. "10: movl 24(%4), %%eax\n"
  344. "51: movl 28(%4), %%edx\n"
  345. " movl %%eax, 24(%3)\n"
  346. " movl %%edx, 28(%3)\n"
  347. "11: movl 32(%4), %%eax\n"
  348. "61: movl 36(%4), %%edx\n"
  349. " movl %%eax, 32(%3)\n"
  350. " movl %%edx, 36(%3)\n"
  351. "12: movl 40(%4), %%eax\n"
  352. "71: movl 44(%4), %%edx\n"
  353. " movl %%eax, 40(%3)\n"
  354. " movl %%edx, 44(%3)\n"
  355. "13: movl 48(%4), %%eax\n"
  356. "81: movl 52(%4), %%edx\n"
  357. " movl %%eax, 48(%3)\n"
  358. " movl %%edx, 52(%3)\n"
  359. "14: movl 56(%4), %%eax\n"
  360. "91: movl 60(%4), %%edx\n"
  361. " movl %%eax, 56(%3)\n"
  362. " movl %%edx, 60(%3)\n"
  363. " addl $-64, %0\n"
  364. " addl $64, %4\n"
  365. " addl $64, %3\n"
  366. " cmpl $63, %0\n"
  367. " ja 0b\n"
  368. "5: movl %0, %%eax\n"
  369. " shrl $2, %0\n"
  370. " andl $3, %%eax\n"
  371. " cld\n"
  372. "6: rep; movsl\n"
  373. " movl %%eax,%0\n"
  374. "7: rep; movsb\n"
  375. "8:\n"
  376. ".section .fixup,\"ax\"\n"
  377. "9: lea 0(%%eax,%0,4),%0\n"
  378. "16: pushl %0\n"
  379. " pushl %%eax\n"
  380. " xorl %%eax,%%eax\n"
  381. " rep; stosb\n"
  382. " popl %%eax\n"
  383. " popl %0\n"
  384. " jmp 8b\n"
  385. ".previous\n"
  386. ".section __ex_table,\"a\"\n"
  387. " .align 4\n"
  388. " .long 0b,16b\n"
  389. " .long 1b,16b\n"
  390. " .long 2b,16b\n"
  391. " .long 21b,16b\n"
  392. " .long 3b,16b\n"
  393. " .long 31b,16b\n"
  394. " .long 4b,16b\n"
  395. " .long 41b,16b\n"
  396. " .long 10b,16b\n"
  397. " .long 51b,16b\n"
  398. " .long 11b,16b\n"
  399. " .long 61b,16b\n"
  400. " .long 12b,16b\n"
  401. " .long 71b,16b\n"
  402. " .long 13b,16b\n"
  403. " .long 81b,16b\n"
  404. " .long 14b,16b\n"
  405. " .long 91b,16b\n"
  406. " .long 6b,9b\n"
  407. " .long 7b,16b\n"
  408. ".previous"
  409. : "=&c"(size), "=&D" (d0), "=&S" (d1)
  410. : "1"(to), "2"(from), "0"(size)
  411. : "eax", "edx", "memory");
  412. return size;
  413. }
  414. #else
  415. /*
  416. * Leave these declared but undefined. They should not be any references to
  417. * them
  418. */
  419. unsigned long
  420. __copy_user_zeroing_intel(void *to, const void __user *from, unsigned long size);
  421. unsigned long
  422. __copy_user_intel(void __user *to, const void *from, unsigned long size);
  423. #endif /* CONFIG_X86_INTEL_USERCOPY */
  424. /* Generic arbitrary sized copy. */
  425. #define __copy_user(to,from,size) \
  426. do { \
  427. int __d0, __d1, __d2; \
  428. __asm__ __volatile__( \
  429. " cmp $7,%0\n" \
  430. " jbe 1f\n" \
  431. " movl %1,%0\n" \
  432. " negl %0\n" \
  433. " andl $7,%0\n" \
  434. " subl %0,%3\n" \
  435. "4: rep; movsb\n" \
  436. " movl %3,%0\n" \
  437. " shrl $2,%0\n" \
  438. " andl $3,%3\n" \
  439. " .align 2,0x90\n" \
  440. "0: rep; movsl\n" \
  441. " movl %3,%0\n" \
  442. "1: rep; movsb\n" \
  443. "2:\n" \
  444. ".section .fixup,\"ax\"\n" \
  445. "5: addl %3,%0\n" \
  446. " jmp 2b\n" \
  447. "3: lea 0(%3,%0,4),%0\n" \
  448. " jmp 2b\n" \
  449. ".previous\n" \
  450. ".section __ex_table,\"a\"\n" \
  451. " .align 4\n" \
  452. " .long 4b,5b\n" \
  453. " .long 0b,3b\n" \
  454. " .long 1b,2b\n" \
  455. ".previous" \
  456. : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
  457. : "3"(size), "0"(size), "1"(to), "2"(from) \
  458. : "memory"); \
  459. } while (0)
  460. #define __copy_user_zeroing(to,from,size) \
  461. do { \
  462. int __d0, __d1, __d2; \
  463. __asm__ __volatile__( \
  464. " cmp $7,%0\n" \
  465. " jbe 1f\n" \
  466. " movl %1,%0\n" \
  467. " negl %0\n" \
  468. " andl $7,%0\n" \
  469. " subl %0,%3\n" \
  470. "4: rep; movsb\n" \
  471. " movl %3,%0\n" \
  472. " shrl $2,%0\n" \
  473. " andl $3,%3\n" \
  474. " .align 2,0x90\n" \
  475. "0: rep; movsl\n" \
  476. " movl %3,%0\n" \
  477. "1: rep; movsb\n" \
  478. "2:\n" \
  479. ".section .fixup,\"ax\"\n" \
  480. "5: addl %3,%0\n" \
  481. " jmp 6f\n" \
  482. "3: lea 0(%3,%0,4),%0\n" \
  483. "6: pushl %0\n" \
  484. " pushl %%eax\n" \
  485. " xorl %%eax,%%eax\n" \
  486. " rep; stosb\n" \
  487. " popl %%eax\n" \
  488. " popl %0\n" \
  489. " jmp 2b\n" \
  490. ".previous\n" \
  491. ".section __ex_table,\"a\"\n" \
  492. " .align 4\n" \
  493. " .long 4b,5b\n" \
  494. " .long 0b,3b\n" \
  495. " .long 1b,6b\n" \
  496. ".previous" \
  497. : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
  498. : "3"(size), "0"(size), "1"(to), "2"(from) \
  499. : "memory"); \
  500. } while (0)
  501. unsigned long __copy_to_user_ll(void __user *to, const void *from, unsigned long n)
  502. {
  503. BUG_ON((long) n < 0);
  504. #ifndef CONFIG_X86_WP_WORKS_OK
  505. if (unlikely(boot_cpu_data.wp_works_ok == 0) &&
  506. ((unsigned long )to) < TASK_SIZE) {
  507. /*
  508. * CPU does not honor the WP bit when writing
  509. * from supervisory mode, and due to preemption or SMP,
  510. * the page tables can change at any time.
  511. * Do it manually. Manfred <manfred@colorfullife.com>
  512. */
  513. while (n) {
  514. unsigned long offset = ((unsigned long)to)%PAGE_SIZE;
  515. unsigned long len = PAGE_SIZE - offset;
  516. int retval;
  517. struct page *pg;
  518. void *maddr;
  519. if (len > n)
  520. len = n;
  521. survive:
  522. down_read(&current->mm->mmap_sem);
  523. retval = get_user_pages(current, current->mm,
  524. (unsigned long )to, 1, 1, 0, &pg, NULL);
  525. if (retval == -ENOMEM && current->pid == 1) {
  526. up_read(&current->mm->mmap_sem);
  527. blk_congestion_wait(WRITE, HZ/50);
  528. goto survive;
  529. }
  530. if (retval != 1) {
  531. up_read(&current->mm->mmap_sem);
  532. break;
  533. }
  534. maddr = kmap_atomic(pg, KM_USER0);
  535. memcpy(maddr + offset, from, len);
  536. kunmap_atomic(maddr, KM_USER0);
  537. set_page_dirty_lock(pg);
  538. put_page(pg);
  539. up_read(&current->mm->mmap_sem);
  540. from += len;
  541. to += len;
  542. n -= len;
  543. }
  544. return n;
  545. }
  546. #endif
  547. if (movsl_is_ok(to, from, n))
  548. __copy_user(to, from, n);
  549. else
  550. n = __copy_user_intel(to, from, n);
  551. return n;
  552. }
  553. EXPORT_SYMBOL(__copy_to_user_ll);
  554. unsigned long
  555. __copy_from_user_ll(void *to, const void __user *from, unsigned long n)
  556. {
  557. BUG_ON((long)n < 0);
  558. if (movsl_is_ok(to, from, n))
  559. __copy_user_zeroing(to, from, n);
  560. else
  561. n = __copy_user_zeroing_intel(to, from, n);
  562. return n;
  563. }
  564. EXPORT_SYMBOL(__copy_from_user_ll);
  565. /**
  566. * copy_to_user: - Copy a block of data into user space.
  567. * @to: Destination address, in user space.
  568. * @from: Source address, in kernel space.
  569. * @n: Number of bytes to copy.
  570. *
  571. * Context: User context only. This function may sleep.
  572. *
  573. * Copy data from kernel space to user space.
  574. *
  575. * Returns number of bytes that could not be copied.
  576. * On success, this will be zero.
  577. */
  578. unsigned long
  579. copy_to_user(void __user *to, const void *from, unsigned long n)
  580. {
  581. might_sleep();
  582. BUG_ON((long) n < 0);
  583. if (access_ok(VERIFY_WRITE, to, n))
  584. n = __copy_to_user(to, from, n);
  585. return n;
  586. }
  587. EXPORT_SYMBOL(copy_to_user);
  588. /**
  589. * copy_from_user: - Copy a block of data from user space.
  590. * @to: Destination address, in kernel space.
  591. * @from: Source address, in user space.
  592. * @n: Number of bytes to copy.
  593. *
  594. * Context: User context only. This function may sleep.
  595. *
  596. * Copy data from user space to kernel space.
  597. *
  598. * Returns number of bytes that could not be copied.
  599. * On success, this will be zero.
  600. *
  601. * If some data could not be copied, this function will pad the copied
  602. * data to the requested size using zero bytes.
  603. */
  604. unsigned long
  605. copy_from_user(void *to, const void __user *from, unsigned long n)
  606. {
  607. might_sleep();
  608. BUG_ON((long) n < 0);
  609. if (access_ok(VERIFY_READ, from, n))
  610. n = __copy_from_user(to, from, n);
  611. else
  612. memset(to, 0, n);
  613. return n;
  614. }
  615. EXPORT_SYMBOL(copy_from_user);