usercopy_32.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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/mm.h>
  9. #include <linux/highmem.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/module.h>
  12. #include <linux/backing-dev.h>
  13. #include <linux/interrupt.h>
  14. #include <asm/uaccess.h>
  15. #include <asm/mmx.h>
  16. static inline int __movsl_is_ok(unsigned long a1, unsigned long a2, unsigned long n)
  17. {
  18. #ifdef CONFIG_X86_INTEL_USERCOPY
  19. if (n >= 64 && ((a1 ^ a2) & movsl_mask.mask))
  20. return 0;
  21. #endif
  22. return 1;
  23. }
  24. #define movsl_is_ok(a1, a2, n) \
  25. __movsl_is_ok((unsigned long)(a1), (unsigned long)(a2), (n))
  26. /*
  27. * Copy a null terminated string from userspace.
  28. */
  29. #define __do_strncpy_from_user(dst, src, count, res) \
  30. do { \
  31. int __d0, __d1, __d2; \
  32. might_sleep(); \
  33. __asm__ __volatile__( \
  34. " testl %1,%1\n" \
  35. " jz 2f\n" \
  36. "0: lodsb\n" \
  37. " stosb\n" \
  38. " testb %%al,%%al\n" \
  39. " jz 1f\n" \
  40. " decl %1\n" \
  41. " jnz 0b\n" \
  42. "1: subl %1,%0\n" \
  43. "2:\n" \
  44. ".section .fixup,\"ax\"\n" \
  45. "3: movl %5,%0\n" \
  46. " jmp 2b\n" \
  47. ".previous\n" \
  48. _ASM_EXTABLE(0b,3b) \
  49. : "=d"(res), "=c"(count), "=&a" (__d0), "=&S" (__d1), \
  50. "=&D" (__d2) \
  51. : "i"(-EFAULT), "0"(count), "1"(count), "3"(src), "4"(dst) \
  52. : "memory"); \
  53. } while (0)
  54. /**
  55. * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
  56. * @dst: Destination address, in kernel space. This buffer must be at
  57. * least @count bytes long.
  58. * @src: Source address, in user space.
  59. * @count: Maximum number of bytes to copy, including the trailing NUL.
  60. *
  61. * Copies a NUL-terminated string from userspace to kernel space.
  62. * Caller must check the specified block with access_ok() before calling
  63. * this function.
  64. *
  65. * On success, returns the length of the string (not including the trailing
  66. * NUL).
  67. *
  68. * If access to userspace fails, returns -EFAULT (some data may have been
  69. * copied).
  70. *
  71. * If @count is smaller than the length of the string, copies @count bytes
  72. * and returns @count.
  73. */
  74. long
  75. __strncpy_from_user(char *dst, const char __user *src, long count)
  76. {
  77. long res;
  78. __do_strncpy_from_user(dst, src, count, res);
  79. return res;
  80. }
  81. EXPORT_SYMBOL(__strncpy_from_user);
  82. /**
  83. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  84. * @dst: Destination address, in kernel space. This buffer must be at
  85. * least @count bytes long.
  86. * @src: Source address, in user space.
  87. * @count: Maximum number of bytes to copy, including the trailing NUL.
  88. *
  89. * Copies a NUL-terminated string from userspace to kernel space.
  90. *
  91. * On success, returns the length of the string (not including the trailing
  92. * NUL).
  93. *
  94. * If access to userspace fails, returns -EFAULT (some data may have been
  95. * copied).
  96. *
  97. * If @count is smaller than the length of the string, copies @count bytes
  98. * and returns @count.
  99. */
  100. long
  101. strncpy_from_user(char *dst, const char __user *src, long count)
  102. {
  103. long res = -EFAULT;
  104. if (access_ok(VERIFY_READ, src, 1))
  105. __do_strncpy_from_user(dst, src, count, res);
  106. return res;
  107. }
  108. EXPORT_SYMBOL(strncpy_from_user);
  109. /*
  110. * Zero Userspace
  111. */
  112. #define __do_clear_user(addr,size) \
  113. do { \
  114. int __d0; \
  115. might_sleep(); \
  116. __asm__ __volatile__( \
  117. "0: rep; stosl\n" \
  118. " movl %2,%0\n" \
  119. "1: rep; stosb\n" \
  120. "2:\n" \
  121. ".section .fixup,\"ax\"\n" \
  122. "3: lea 0(%2,%0,4),%0\n" \
  123. " jmp 2b\n" \
  124. ".previous\n" \
  125. _ASM_EXTABLE(0b,3b) \
  126. _ASM_EXTABLE(1b,2b) \
  127. : "=&c"(size), "=&D" (__d0) \
  128. : "r"(size & 3), "0"(size / 4), "1"(addr), "a"(0)); \
  129. } while (0)
  130. /**
  131. * clear_user: - Zero a block of memory in user space.
  132. * @to: Destination address, in user space.
  133. * @n: Number of bytes to zero.
  134. *
  135. * Zero a block of memory in user space.
  136. *
  137. * Returns number of bytes that could not be cleared.
  138. * On success, this will be zero.
  139. */
  140. unsigned long
  141. clear_user(void __user *to, unsigned long n)
  142. {
  143. might_sleep();
  144. if (access_ok(VERIFY_WRITE, to, n))
  145. __do_clear_user(to, n);
  146. return n;
  147. }
  148. EXPORT_SYMBOL(clear_user);
  149. /**
  150. * __clear_user: - Zero a block of memory in user space, with less checking.
  151. * @to: Destination address, in user space.
  152. * @n: Number of bytes to zero.
  153. *
  154. * Zero a block of memory in user space. Caller must check
  155. * the specified block with access_ok() before calling this function.
  156. *
  157. * Returns number of bytes that could not be cleared.
  158. * On success, this will be zero.
  159. */
  160. unsigned long
  161. __clear_user(void __user *to, unsigned long n)
  162. {
  163. __do_clear_user(to, n);
  164. return n;
  165. }
  166. EXPORT_SYMBOL(__clear_user);
  167. /**
  168. * strnlen_user: - Get the size of a string in user space.
  169. * @s: The string to measure.
  170. * @n: The maximum valid length
  171. *
  172. * Get the size of a NUL-terminated string in user space.
  173. *
  174. * Returns the size of the string INCLUDING the terminating NUL.
  175. * On exception, returns 0.
  176. * If the string is too long, returns a value greater than @n.
  177. */
  178. long strnlen_user(const char __user *s, long n)
  179. {
  180. unsigned long mask = -__addr_ok(s);
  181. unsigned long res, tmp;
  182. might_sleep();
  183. __asm__ __volatile__(
  184. " testl %0, %0\n"
  185. " jz 3f\n"
  186. " andl %0,%%ecx\n"
  187. "0: repne; scasb\n"
  188. " setne %%al\n"
  189. " subl %%ecx,%0\n"
  190. " addl %0,%%eax\n"
  191. "1:\n"
  192. ".section .fixup,\"ax\"\n"
  193. "2: xorl %%eax,%%eax\n"
  194. " jmp 1b\n"
  195. "3: movb $1,%%al\n"
  196. " jmp 1b\n"
  197. ".previous\n"
  198. ".section __ex_table,\"a\"\n"
  199. " .align 4\n"
  200. " .long 0b,2b\n"
  201. ".previous"
  202. :"=r" (n), "=D" (s), "=a" (res), "=c" (tmp)
  203. :"0" (n), "1" (s), "2" (0), "3" (mask)
  204. :"cc");
  205. return res & mask;
  206. }
  207. EXPORT_SYMBOL(strnlen_user);
  208. #ifdef CONFIG_X86_INTEL_USERCOPY
  209. static unsigned long
  210. __copy_user_intel(void __user *to, const void *from, unsigned long size)
  211. {
  212. int d0, d1;
  213. __asm__ __volatile__(
  214. " .align 2,0x90\n"
  215. "1: movl 32(%4), %%eax\n"
  216. " cmpl $67, %0\n"
  217. " jbe 3f\n"
  218. "2: movl 64(%4), %%eax\n"
  219. " .align 2,0x90\n"
  220. "3: movl 0(%4), %%eax\n"
  221. "4: movl 4(%4), %%edx\n"
  222. "5: movl %%eax, 0(%3)\n"
  223. "6: movl %%edx, 4(%3)\n"
  224. "7: movl 8(%4), %%eax\n"
  225. "8: movl 12(%4),%%edx\n"
  226. "9: movl %%eax, 8(%3)\n"
  227. "10: movl %%edx, 12(%3)\n"
  228. "11: movl 16(%4), %%eax\n"
  229. "12: movl 20(%4), %%edx\n"
  230. "13: movl %%eax, 16(%3)\n"
  231. "14: movl %%edx, 20(%3)\n"
  232. "15: movl 24(%4), %%eax\n"
  233. "16: movl 28(%4), %%edx\n"
  234. "17: movl %%eax, 24(%3)\n"
  235. "18: movl %%edx, 28(%3)\n"
  236. "19: movl 32(%4), %%eax\n"
  237. "20: movl 36(%4), %%edx\n"
  238. "21: movl %%eax, 32(%3)\n"
  239. "22: movl %%edx, 36(%3)\n"
  240. "23: movl 40(%4), %%eax\n"
  241. "24: movl 44(%4), %%edx\n"
  242. "25: movl %%eax, 40(%3)\n"
  243. "26: movl %%edx, 44(%3)\n"
  244. "27: movl 48(%4), %%eax\n"
  245. "28: movl 52(%4), %%edx\n"
  246. "29: movl %%eax, 48(%3)\n"
  247. "30: movl %%edx, 52(%3)\n"
  248. "31: movl 56(%4), %%eax\n"
  249. "32: movl 60(%4), %%edx\n"
  250. "33: movl %%eax, 56(%3)\n"
  251. "34: movl %%edx, 60(%3)\n"
  252. " addl $-64, %0\n"
  253. " addl $64, %4\n"
  254. " addl $64, %3\n"
  255. " cmpl $63, %0\n"
  256. " ja 1b\n"
  257. "35: movl %0, %%eax\n"
  258. " shrl $2, %0\n"
  259. " andl $3, %%eax\n"
  260. " cld\n"
  261. "99: rep; movsl\n"
  262. "36: movl %%eax, %0\n"
  263. "37: rep; movsb\n"
  264. "100:\n"
  265. ".section .fixup,\"ax\"\n"
  266. "101: lea 0(%%eax,%0,4),%0\n"
  267. " jmp 100b\n"
  268. ".previous\n"
  269. ".section __ex_table,\"a\"\n"
  270. " .align 4\n"
  271. " .long 1b,100b\n"
  272. " .long 2b,100b\n"
  273. " .long 3b,100b\n"
  274. " .long 4b,100b\n"
  275. " .long 5b,100b\n"
  276. " .long 6b,100b\n"
  277. " .long 7b,100b\n"
  278. " .long 8b,100b\n"
  279. " .long 9b,100b\n"
  280. " .long 10b,100b\n"
  281. " .long 11b,100b\n"
  282. " .long 12b,100b\n"
  283. " .long 13b,100b\n"
  284. " .long 14b,100b\n"
  285. " .long 15b,100b\n"
  286. " .long 16b,100b\n"
  287. " .long 17b,100b\n"
  288. " .long 18b,100b\n"
  289. " .long 19b,100b\n"
  290. " .long 20b,100b\n"
  291. " .long 21b,100b\n"
  292. " .long 22b,100b\n"
  293. " .long 23b,100b\n"
  294. " .long 24b,100b\n"
  295. " .long 25b,100b\n"
  296. " .long 26b,100b\n"
  297. " .long 27b,100b\n"
  298. " .long 28b,100b\n"
  299. " .long 29b,100b\n"
  300. " .long 30b,100b\n"
  301. " .long 31b,100b\n"
  302. " .long 32b,100b\n"
  303. " .long 33b,100b\n"
  304. " .long 34b,100b\n"
  305. " .long 35b,100b\n"
  306. " .long 36b,100b\n"
  307. " .long 37b,100b\n"
  308. " .long 99b,101b\n"
  309. ".previous"
  310. : "=&c"(size), "=&D" (d0), "=&S" (d1)
  311. : "1"(to), "2"(from), "0"(size)
  312. : "eax", "edx", "memory");
  313. return size;
  314. }
  315. static unsigned long
  316. __copy_user_zeroing_intel(void *to, const void __user *from, unsigned long size)
  317. {
  318. int d0, d1;
  319. __asm__ __volatile__(
  320. " .align 2,0x90\n"
  321. "0: movl 32(%4), %%eax\n"
  322. " cmpl $67, %0\n"
  323. " jbe 2f\n"
  324. "1: movl 64(%4), %%eax\n"
  325. " .align 2,0x90\n"
  326. "2: movl 0(%4), %%eax\n"
  327. "21: movl 4(%4), %%edx\n"
  328. " movl %%eax, 0(%3)\n"
  329. " movl %%edx, 4(%3)\n"
  330. "3: movl 8(%4), %%eax\n"
  331. "31: movl 12(%4),%%edx\n"
  332. " movl %%eax, 8(%3)\n"
  333. " movl %%edx, 12(%3)\n"
  334. "4: movl 16(%4), %%eax\n"
  335. "41: movl 20(%4), %%edx\n"
  336. " movl %%eax, 16(%3)\n"
  337. " movl %%edx, 20(%3)\n"
  338. "10: movl 24(%4), %%eax\n"
  339. "51: movl 28(%4), %%edx\n"
  340. " movl %%eax, 24(%3)\n"
  341. " movl %%edx, 28(%3)\n"
  342. "11: movl 32(%4), %%eax\n"
  343. "61: movl 36(%4), %%edx\n"
  344. " movl %%eax, 32(%3)\n"
  345. " movl %%edx, 36(%3)\n"
  346. "12: movl 40(%4), %%eax\n"
  347. "71: movl 44(%4), %%edx\n"
  348. " movl %%eax, 40(%3)\n"
  349. " movl %%edx, 44(%3)\n"
  350. "13: movl 48(%4), %%eax\n"
  351. "81: movl 52(%4), %%edx\n"
  352. " movl %%eax, 48(%3)\n"
  353. " movl %%edx, 52(%3)\n"
  354. "14: movl 56(%4), %%eax\n"
  355. "91: movl 60(%4), %%edx\n"
  356. " movl %%eax, 56(%3)\n"
  357. " movl %%edx, 60(%3)\n"
  358. " addl $-64, %0\n"
  359. " addl $64, %4\n"
  360. " addl $64, %3\n"
  361. " cmpl $63, %0\n"
  362. " ja 0b\n"
  363. "5: movl %0, %%eax\n"
  364. " shrl $2, %0\n"
  365. " andl $3, %%eax\n"
  366. " cld\n"
  367. "6: rep; movsl\n"
  368. " movl %%eax,%0\n"
  369. "7: rep; movsb\n"
  370. "8:\n"
  371. ".section .fixup,\"ax\"\n"
  372. "9: lea 0(%%eax,%0,4),%0\n"
  373. "16: pushl %0\n"
  374. " pushl %%eax\n"
  375. " xorl %%eax,%%eax\n"
  376. " rep; stosb\n"
  377. " popl %%eax\n"
  378. " popl %0\n"
  379. " jmp 8b\n"
  380. ".previous\n"
  381. ".section __ex_table,\"a\"\n"
  382. " .align 4\n"
  383. " .long 0b,16b\n"
  384. " .long 1b,16b\n"
  385. " .long 2b,16b\n"
  386. " .long 21b,16b\n"
  387. " .long 3b,16b\n"
  388. " .long 31b,16b\n"
  389. " .long 4b,16b\n"
  390. " .long 41b,16b\n"
  391. " .long 10b,16b\n"
  392. " .long 51b,16b\n"
  393. " .long 11b,16b\n"
  394. " .long 61b,16b\n"
  395. " .long 12b,16b\n"
  396. " .long 71b,16b\n"
  397. " .long 13b,16b\n"
  398. " .long 81b,16b\n"
  399. " .long 14b,16b\n"
  400. " .long 91b,16b\n"
  401. " .long 6b,9b\n"
  402. " .long 7b,16b\n"
  403. ".previous"
  404. : "=&c"(size), "=&D" (d0), "=&S" (d1)
  405. : "1"(to), "2"(from), "0"(size)
  406. : "eax", "edx", "memory");
  407. return size;
  408. }
  409. /*
  410. * Non Temporal Hint version of __copy_user_zeroing_intel. It is cache aware.
  411. * hyoshiok@miraclelinux.com
  412. */
  413. static unsigned long __copy_user_zeroing_intel_nocache(void *to,
  414. const void __user *from, unsigned long size)
  415. {
  416. int d0, d1;
  417. __asm__ __volatile__(
  418. " .align 2,0x90\n"
  419. "0: movl 32(%4), %%eax\n"
  420. " cmpl $67, %0\n"
  421. " jbe 2f\n"
  422. "1: movl 64(%4), %%eax\n"
  423. " .align 2,0x90\n"
  424. "2: movl 0(%4), %%eax\n"
  425. "21: movl 4(%4), %%edx\n"
  426. " movnti %%eax, 0(%3)\n"
  427. " movnti %%edx, 4(%3)\n"
  428. "3: movl 8(%4), %%eax\n"
  429. "31: movl 12(%4),%%edx\n"
  430. " movnti %%eax, 8(%3)\n"
  431. " movnti %%edx, 12(%3)\n"
  432. "4: movl 16(%4), %%eax\n"
  433. "41: movl 20(%4), %%edx\n"
  434. " movnti %%eax, 16(%3)\n"
  435. " movnti %%edx, 20(%3)\n"
  436. "10: movl 24(%4), %%eax\n"
  437. "51: movl 28(%4), %%edx\n"
  438. " movnti %%eax, 24(%3)\n"
  439. " movnti %%edx, 28(%3)\n"
  440. "11: movl 32(%4), %%eax\n"
  441. "61: movl 36(%4), %%edx\n"
  442. " movnti %%eax, 32(%3)\n"
  443. " movnti %%edx, 36(%3)\n"
  444. "12: movl 40(%4), %%eax\n"
  445. "71: movl 44(%4), %%edx\n"
  446. " movnti %%eax, 40(%3)\n"
  447. " movnti %%edx, 44(%3)\n"
  448. "13: movl 48(%4), %%eax\n"
  449. "81: movl 52(%4), %%edx\n"
  450. " movnti %%eax, 48(%3)\n"
  451. " movnti %%edx, 52(%3)\n"
  452. "14: movl 56(%4), %%eax\n"
  453. "91: movl 60(%4), %%edx\n"
  454. " movnti %%eax, 56(%3)\n"
  455. " movnti %%edx, 60(%3)\n"
  456. " addl $-64, %0\n"
  457. " addl $64, %4\n"
  458. " addl $64, %3\n"
  459. " cmpl $63, %0\n"
  460. " ja 0b\n"
  461. " sfence \n"
  462. "5: movl %0, %%eax\n"
  463. " shrl $2, %0\n"
  464. " andl $3, %%eax\n"
  465. " cld\n"
  466. "6: rep; movsl\n"
  467. " movl %%eax,%0\n"
  468. "7: rep; movsb\n"
  469. "8:\n"
  470. ".section .fixup,\"ax\"\n"
  471. "9: lea 0(%%eax,%0,4),%0\n"
  472. "16: pushl %0\n"
  473. " pushl %%eax\n"
  474. " xorl %%eax,%%eax\n"
  475. " rep; stosb\n"
  476. " popl %%eax\n"
  477. " popl %0\n"
  478. " jmp 8b\n"
  479. ".previous\n"
  480. ".section __ex_table,\"a\"\n"
  481. " .align 4\n"
  482. " .long 0b,16b\n"
  483. " .long 1b,16b\n"
  484. " .long 2b,16b\n"
  485. " .long 21b,16b\n"
  486. " .long 3b,16b\n"
  487. " .long 31b,16b\n"
  488. " .long 4b,16b\n"
  489. " .long 41b,16b\n"
  490. " .long 10b,16b\n"
  491. " .long 51b,16b\n"
  492. " .long 11b,16b\n"
  493. " .long 61b,16b\n"
  494. " .long 12b,16b\n"
  495. " .long 71b,16b\n"
  496. " .long 13b,16b\n"
  497. " .long 81b,16b\n"
  498. " .long 14b,16b\n"
  499. " .long 91b,16b\n"
  500. " .long 6b,9b\n"
  501. " .long 7b,16b\n"
  502. ".previous"
  503. : "=&c"(size), "=&D" (d0), "=&S" (d1)
  504. : "1"(to), "2"(from), "0"(size)
  505. : "eax", "edx", "memory");
  506. return size;
  507. }
  508. static unsigned long __copy_user_intel_nocache(void *to,
  509. const void __user *from, unsigned long size)
  510. {
  511. int d0, d1;
  512. __asm__ __volatile__(
  513. " .align 2,0x90\n"
  514. "0: movl 32(%4), %%eax\n"
  515. " cmpl $67, %0\n"
  516. " jbe 2f\n"
  517. "1: movl 64(%4), %%eax\n"
  518. " .align 2,0x90\n"
  519. "2: movl 0(%4), %%eax\n"
  520. "21: movl 4(%4), %%edx\n"
  521. " movnti %%eax, 0(%3)\n"
  522. " movnti %%edx, 4(%3)\n"
  523. "3: movl 8(%4), %%eax\n"
  524. "31: movl 12(%4),%%edx\n"
  525. " movnti %%eax, 8(%3)\n"
  526. " movnti %%edx, 12(%3)\n"
  527. "4: movl 16(%4), %%eax\n"
  528. "41: movl 20(%4), %%edx\n"
  529. " movnti %%eax, 16(%3)\n"
  530. " movnti %%edx, 20(%3)\n"
  531. "10: movl 24(%4), %%eax\n"
  532. "51: movl 28(%4), %%edx\n"
  533. " movnti %%eax, 24(%3)\n"
  534. " movnti %%edx, 28(%3)\n"
  535. "11: movl 32(%4), %%eax\n"
  536. "61: movl 36(%4), %%edx\n"
  537. " movnti %%eax, 32(%3)\n"
  538. " movnti %%edx, 36(%3)\n"
  539. "12: movl 40(%4), %%eax\n"
  540. "71: movl 44(%4), %%edx\n"
  541. " movnti %%eax, 40(%3)\n"
  542. " movnti %%edx, 44(%3)\n"
  543. "13: movl 48(%4), %%eax\n"
  544. "81: movl 52(%4), %%edx\n"
  545. " movnti %%eax, 48(%3)\n"
  546. " movnti %%edx, 52(%3)\n"
  547. "14: movl 56(%4), %%eax\n"
  548. "91: movl 60(%4), %%edx\n"
  549. " movnti %%eax, 56(%3)\n"
  550. " movnti %%edx, 60(%3)\n"
  551. " addl $-64, %0\n"
  552. " addl $64, %4\n"
  553. " addl $64, %3\n"
  554. " cmpl $63, %0\n"
  555. " ja 0b\n"
  556. " sfence \n"
  557. "5: movl %0, %%eax\n"
  558. " shrl $2, %0\n"
  559. " andl $3, %%eax\n"
  560. " cld\n"
  561. "6: rep; movsl\n"
  562. " movl %%eax,%0\n"
  563. "7: rep; movsb\n"
  564. "8:\n"
  565. ".section .fixup,\"ax\"\n"
  566. "9: lea 0(%%eax,%0,4),%0\n"
  567. "16: jmp 8b\n"
  568. ".previous\n"
  569. ".section __ex_table,\"a\"\n"
  570. " .align 4\n"
  571. " .long 0b,16b\n"
  572. " .long 1b,16b\n"
  573. " .long 2b,16b\n"
  574. " .long 21b,16b\n"
  575. " .long 3b,16b\n"
  576. " .long 31b,16b\n"
  577. " .long 4b,16b\n"
  578. " .long 41b,16b\n"
  579. " .long 10b,16b\n"
  580. " .long 51b,16b\n"
  581. " .long 11b,16b\n"
  582. " .long 61b,16b\n"
  583. " .long 12b,16b\n"
  584. " .long 71b,16b\n"
  585. " .long 13b,16b\n"
  586. " .long 81b,16b\n"
  587. " .long 14b,16b\n"
  588. " .long 91b,16b\n"
  589. " .long 6b,9b\n"
  590. " .long 7b,16b\n"
  591. ".previous"
  592. : "=&c"(size), "=&D" (d0), "=&S" (d1)
  593. : "1"(to), "2"(from), "0"(size)
  594. : "eax", "edx", "memory");
  595. return size;
  596. }
  597. #else
  598. /*
  599. * Leave these declared but undefined. They should not be any references to
  600. * them
  601. */
  602. unsigned long __copy_user_zeroing_intel(void *to, const void __user *from,
  603. unsigned long size);
  604. unsigned long __copy_user_intel(void __user *to, const void *from,
  605. unsigned long size);
  606. unsigned long __copy_user_zeroing_intel_nocache(void *to,
  607. const void __user *from, unsigned long size);
  608. #endif /* CONFIG_X86_INTEL_USERCOPY */
  609. /* Generic arbitrary sized copy. */
  610. #define __copy_user(to, from, size) \
  611. do { \
  612. int __d0, __d1, __d2; \
  613. __asm__ __volatile__( \
  614. " cmp $7,%0\n" \
  615. " jbe 1f\n" \
  616. " movl %1,%0\n" \
  617. " negl %0\n" \
  618. " andl $7,%0\n" \
  619. " subl %0,%3\n" \
  620. "4: rep; movsb\n" \
  621. " movl %3,%0\n" \
  622. " shrl $2,%0\n" \
  623. " andl $3,%3\n" \
  624. " .align 2,0x90\n" \
  625. "0: rep; movsl\n" \
  626. " movl %3,%0\n" \
  627. "1: rep; movsb\n" \
  628. "2:\n" \
  629. ".section .fixup,\"ax\"\n" \
  630. "5: addl %3,%0\n" \
  631. " jmp 2b\n" \
  632. "3: lea 0(%3,%0,4),%0\n" \
  633. " jmp 2b\n" \
  634. ".previous\n" \
  635. ".section __ex_table,\"a\"\n" \
  636. " .align 4\n" \
  637. " .long 4b,5b\n" \
  638. " .long 0b,3b\n" \
  639. " .long 1b,2b\n" \
  640. ".previous" \
  641. : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
  642. : "3"(size), "0"(size), "1"(to), "2"(from) \
  643. : "memory"); \
  644. } while (0)
  645. #define __copy_user_zeroing(to, from, size) \
  646. do { \
  647. int __d0, __d1, __d2; \
  648. __asm__ __volatile__( \
  649. " cmp $7,%0\n" \
  650. " jbe 1f\n" \
  651. " movl %1,%0\n" \
  652. " negl %0\n" \
  653. " andl $7,%0\n" \
  654. " subl %0,%3\n" \
  655. "4: rep; movsb\n" \
  656. " movl %3,%0\n" \
  657. " shrl $2,%0\n" \
  658. " andl $3,%3\n" \
  659. " .align 2,0x90\n" \
  660. "0: rep; movsl\n" \
  661. " movl %3,%0\n" \
  662. "1: rep; movsb\n" \
  663. "2:\n" \
  664. ".section .fixup,\"ax\"\n" \
  665. "5: addl %3,%0\n" \
  666. " jmp 6f\n" \
  667. "3: lea 0(%3,%0,4),%0\n" \
  668. "6: pushl %0\n" \
  669. " pushl %%eax\n" \
  670. " xorl %%eax,%%eax\n" \
  671. " rep; stosb\n" \
  672. " popl %%eax\n" \
  673. " popl %0\n" \
  674. " jmp 2b\n" \
  675. ".previous\n" \
  676. ".section __ex_table,\"a\"\n" \
  677. " .align 4\n" \
  678. " .long 4b,5b\n" \
  679. " .long 0b,3b\n" \
  680. " .long 1b,6b\n" \
  681. ".previous" \
  682. : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
  683. : "3"(size), "0"(size), "1"(to), "2"(from) \
  684. : "memory"); \
  685. } while (0)
  686. unsigned long __copy_to_user_ll(void __user *to, const void *from,
  687. unsigned long n)
  688. {
  689. #ifndef CONFIG_X86_WP_WORKS_OK
  690. if (unlikely(boot_cpu_data.wp_works_ok == 0) &&
  691. ((unsigned long)to) < TASK_SIZE) {
  692. /*
  693. * When we are in an atomic section (see
  694. * mm/filemap.c:file_read_actor), return the full
  695. * length to take the slow path.
  696. */
  697. if (in_atomic())
  698. return n;
  699. /*
  700. * CPU does not honor the WP bit when writing
  701. * from supervisory mode, and due to preemption or SMP,
  702. * the page tables can change at any time.
  703. * Do it manually. Manfred <manfred@colorfullife.com>
  704. */
  705. while (n) {
  706. unsigned long offset = ((unsigned long)to)%PAGE_SIZE;
  707. unsigned long len = PAGE_SIZE - offset;
  708. int retval;
  709. struct page *pg;
  710. void *maddr;
  711. if (len > n)
  712. len = n;
  713. survive:
  714. down_read(&current->mm->mmap_sem);
  715. retval = get_user_pages(current, current->mm,
  716. (unsigned long)to, 1, 1, 0, &pg, NULL);
  717. if (retval == -ENOMEM && is_global_init(current)) {
  718. up_read(&current->mm->mmap_sem);
  719. congestion_wait(WRITE, HZ/50);
  720. goto survive;
  721. }
  722. if (retval != 1) {
  723. up_read(&current->mm->mmap_sem);
  724. break;
  725. }
  726. maddr = kmap_atomic(pg, KM_USER0);
  727. memcpy(maddr + offset, from, len);
  728. kunmap_atomic(maddr, KM_USER0);
  729. set_page_dirty_lock(pg);
  730. put_page(pg);
  731. up_read(&current->mm->mmap_sem);
  732. from += len;
  733. to += len;
  734. n -= len;
  735. }
  736. return n;
  737. }
  738. #endif
  739. if (movsl_is_ok(to, from, n))
  740. __copy_user(to, from, n);
  741. else
  742. n = __copy_user_intel(to, from, n);
  743. return n;
  744. }
  745. EXPORT_SYMBOL(__copy_to_user_ll);
  746. unsigned long __copy_from_user_ll(void *to, const void __user *from,
  747. unsigned long n)
  748. {
  749. if (movsl_is_ok(to, from, n))
  750. __copy_user_zeroing(to, from, n);
  751. else
  752. n = __copy_user_zeroing_intel(to, from, n);
  753. return n;
  754. }
  755. EXPORT_SYMBOL(__copy_from_user_ll);
  756. unsigned long __copy_from_user_ll_nozero(void *to, const void __user *from,
  757. unsigned long n)
  758. {
  759. if (movsl_is_ok(to, from, n))
  760. __copy_user(to, from, n);
  761. else
  762. n = __copy_user_intel((void __user *)to,
  763. (const void *)from, n);
  764. return n;
  765. }
  766. EXPORT_SYMBOL(__copy_from_user_ll_nozero);
  767. unsigned long __copy_from_user_ll_nocache(void *to, const void __user *from,
  768. unsigned long n)
  769. {
  770. #ifdef CONFIG_X86_INTEL_USERCOPY
  771. if (n > 64 && cpu_has_xmm2)
  772. n = __copy_user_zeroing_intel_nocache(to, from, n);
  773. else
  774. __copy_user_zeroing(to, from, n);
  775. #else
  776. __copy_user_zeroing(to, from, n);
  777. #endif
  778. return n;
  779. }
  780. EXPORT_SYMBOL(__copy_from_user_ll_nocache);
  781. unsigned long __copy_from_user_ll_nocache_nozero(void *to, const void __user *from,
  782. unsigned long n)
  783. {
  784. #ifdef CONFIG_X86_INTEL_USERCOPY
  785. if (n > 64 && cpu_has_xmm2)
  786. n = __copy_user_intel_nocache(to, from, n);
  787. else
  788. __copy_user(to, from, n);
  789. #else
  790. __copy_user(to, from, n);
  791. #endif
  792. return n;
  793. }
  794. EXPORT_SYMBOL(__copy_from_user_ll_nocache_nozero);
  795. /**
  796. * copy_to_user: - Copy a block of data into user space.
  797. * @to: Destination address, in user space.
  798. * @from: Source address, in kernel space.
  799. * @n: Number of bytes to copy.
  800. *
  801. * Context: User context only. This function may sleep.
  802. *
  803. * Copy data from kernel space to user space.
  804. *
  805. * Returns number of bytes that could not be copied.
  806. * On success, this will be zero.
  807. */
  808. unsigned long
  809. copy_to_user(void __user *to, const void *from, unsigned long n)
  810. {
  811. if (access_ok(VERIFY_WRITE, to, n))
  812. n = __copy_to_user(to, from, n);
  813. return n;
  814. }
  815. EXPORT_SYMBOL(copy_to_user);
  816. /**
  817. * copy_from_user: - Copy a block of data from user space.
  818. * @to: Destination address, in kernel space.
  819. * @from: Source address, in user space.
  820. * @n: Number of bytes to copy.
  821. *
  822. * Context: User context only. This function may sleep.
  823. *
  824. * Copy data from user space to kernel space.
  825. *
  826. * Returns number of bytes that could not be copied.
  827. * On success, this will be zero.
  828. *
  829. * If some data could not be copied, this function will pad the copied
  830. * data to the requested size using zero bytes.
  831. */
  832. unsigned long
  833. copy_from_user(void *to, const void __user *from, unsigned long n)
  834. {
  835. if (access_ok(VERIFY_READ, from, n))
  836. n = __copy_from_user(to, from, n);
  837. else
  838. memset(to, 0, n);
  839. return n;
  840. }
  841. EXPORT_SYMBOL(copy_from_user);