usercopy.c 25 KB

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