usercopy.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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 <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. * strnlen_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. /*
  415. * Non Temporal Hint version of __copy_user_zeroing_intel. It is cache aware.
  416. * hyoshiok@miraclelinux.com
  417. */
  418. static unsigned long __copy_user_zeroing_intel_nocache(void *to,
  419. const void __user *from, unsigned long size)
  420. {
  421. int d0, d1;
  422. __asm__ __volatile__(
  423. " .align 2,0x90\n"
  424. "0: movl 32(%4), %%eax\n"
  425. " cmpl $67, %0\n"
  426. " jbe 2f\n"
  427. "1: movl 64(%4), %%eax\n"
  428. " .align 2,0x90\n"
  429. "2: movl 0(%4), %%eax\n"
  430. "21: movl 4(%4), %%edx\n"
  431. " movnti %%eax, 0(%3)\n"
  432. " movnti %%edx, 4(%3)\n"
  433. "3: movl 8(%4), %%eax\n"
  434. "31: movl 12(%4),%%edx\n"
  435. " movnti %%eax, 8(%3)\n"
  436. " movnti %%edx, 12(%3)\n"
  437. "4: movl 16(%4), %%eax\n"
  438. "41: movl 20(%4), %%edx\n"
  439. " movnti %%eax, 16(%3)\n"
  440. " movnti %%edx, 20(%3)\n"
  441. "10: movl 24(%4), %%eax\n"
  442. "51: movl 28(%4), %%edx\n"
  443. " movnti %%eax, 24(%3)\n"
  444. " movnti %%edx, 28(%3)\n"
  445. "11: movl 32(%4), %%eax\n"
  446. "61: movl 36(%4), %%edx\n"
  447. " movnti %%eax, 32(%3)\n"
  448. " movnti %%edx, 36(%3)\n"
  449. "12: movl 40(%4), %%eax\n"
  450. "71: movl 44(%4), %%edx\n"
  451. " movnti %%eax, 40(%3)\n"
  452. " movnti %%edx, 44(%3)\n"
  453. "13: movl 48(%4), %%eax\n"
  454. "81: movl 52(%4), %%edx\n"
  455. " movnti %%eax, 48(%3)\n"
  456. " movnti %%edx, 52(%3)\n"
  457. "14: movl 56(%4), %%eax\n"
  458. "91: movl 60(%4), %%edx\n"
  459. " movnti %%eax, 56(%3)\n"
  460. " movnti %%edx, 60(%3)\n"
  461. " addl $-64, %0\n"
  462. " addl $64, %4\n"
  463. " addl $64, %3\n"
  464. " cmpl $63, %0\n"
  465. " ja 0b\n"
  466. " sfence \n"
  467. "5: movl %0, %%eax\n"
  468. " shrl $2, %0\n"
  469. " andl $3, %%eax\n"
  470. " cld\n"
  471. "6: rep; movsl\n"
  472. " movl %%eax,%0\n"
  473. "7: rep; movsb\n"
  474. "8:\n"
  475. ".section .fixup,\"ax\"\n"
  476. "9: lea 0(%%eax,%0,4),%0\n"
  477. "16: pushl %0\n"
  478. " pushl %%eax\n"
  479. " xorl %%eax,%%eax\n"
  480. " rep; stosb\n"
  481. " popl %%eax\n"
  482. " popl %0\n"
  483. " jmp 8b\n"
  484. ".previous\n"
  485. ".section __ex_table,\"a\"\n"
  486. " .align 4\n"
  487. " .long 0b,16b\n"
  488. " .long 1b,16b\n"
  489. " .long 2b,16b\n"
  490. " .long 21b,16b\n"
  491. " .long 3b,16b\n"
  492. " .long 31b,16b\n"
  493. " .long 4b,16b\n"
  494. " .long 41b,16b\n"
  495. " .long 10b,16b\n"
  496. " .long 51b,16b\n"
  497. " .long 11b,16b\n"
  498. " .long 61b,16b\n"
  499. " .long 12b,16b\n"
  500. " .long 71b,16b\n"
  501. " .long 13b,16b\n"
  502. " .long 81b,16b\n"
  503. " .long 14b,16b\n"
  504. " .long 91b,16b\n"
  505. " .long 6b,9b\n"
  506. " .long 7b,16b\n"
  507. ".previous"
  508. : "=&c"(size), "=&D" (d0), "=&S" (d1)
  509. : "1"(to), "2"(from), "0"(size)
  510. : "eax", "edx", "memory");
  511. return size;
  512. }
  513. static unsigned long __copy_user_intel_nocache(void *to,
  514. const void __user *from, unsigned long size)
  515. {
  516. int d0, d1;
  517. __asm__ __volatile__(
  518. " .align 2,0x90\n"
  519. "0: movl 32(%4), %%eax\n"
  520. " cmpl $67, %0\n"
  521. " jbe 2f\n"
  522. "1: movl 64(%4), %%eax\n"
  523. " .align 2,0x90\n"
  524. "2: movl 0(%4), %%eax\n"
  525. "21: movl 4(%4), %%edx\n"
  526. " movnti %%eax, 0(%3)\n"
  527. " movnti %%edx, 4(%3)\n"
  528. "3: movl 8(%4), %%eax\n"
  529. "31: movl 12(%4),%%edx\n"
  530. " movnti %%eax, 8(%3)\n"
  531. " movnti %%edx, 12(%3)\n"
  532. "4: movl 16(%4), %%eax\n"
  533. "41: movl 20(%4), %%edx\n"
  534. " movnti %%eax, 16(%3)\n"
  535. " movnti %%edx, 20(%3)\n"
  536. "10: movl 24(%4), %%eax\n"
  537. "51: movl 28(%4), %%edx\n"
  538. " movnti %%eax, 24(%3)\n"
  539. " movnti %%edx, 28(%3)\n"
  540. "11: movl 32(%4), %%eax\n"
  541. "61: movl 36(%4), %%edx\n"
  542. " movnti %%eax, 32(%3)\n"
  543. " movnti %%edx, 36(%3)\n"
  544. "12: movl 40(%4), %%eax\n"
  545. "71: movl 44(%4), %%edx\n"
  546. " movnti %%eax, 40(%3)\n"
  547. " movnti %%edx, 44(%3)\n"
  548. "13: movl 48(%4), %%eax\n"
  549. "81: movl 52(%4), %%edx\n"
  550. " movnti %%eax, 48(%3)\n"
  551. " movnti %%edx, 52(%3)\n"
  552. "14: movl 56(%4), %%eax\n"
  553. "91: movl 60(%4), %%edx\n"
  554. " movnti %%eax, 56(%3)\n"
  555. " movnti %%edx, 60(%3)\n"
  556. " addl $-64, %0\n"
  557. " addl $64, %4\n"
  558. " addl $64, %3\n"
  559. " cmpl $63, %0\n"
  560. " ja 0b\n"
  561. " sfence \n"
  562. "5: movl %0, %%eax\n"
  563. " shrl $2, %0\n"
  564. " andl $3, %%eax\n"
  565. " cld\n"
  566. "6: rep; movsl\n"
  567. " movl %%eax,%0\n"
  568. "7: rep; movsb\n"
  569. "8:\n"
  570. ".section .fixup,\"ax\"\n"
  571. "9: lea 0(%%eax,%0,4),%0\n"
  572. "16: jmp 8b\n"
  573. ".previous\n"
  574. ".section __ex_table,\"a\"\n"
  575. " .align 4\n"
  576. " .long 0b,16b\n"
  577. " .long 1b,16b\n"
  578. " .long 2b,16b\n"
  579. " .long 21b,16b\n"
  580. " .long 3b,16b\n"
  581. " .long 31b,16b\n"
  582. " .long 4b,16b\n"
  583. " .long 41b,16b\n"
  584. " .long 10b,16b\n"
  585. " .long 51b,16b\n"
  586. " .long 11b,16b\n"
  587. " .long 61b,16b\n"
  588. " .long 12b,16b\n"
  589. " .long 71b,16b\n"
  590. " .long 13b,16b\n"
  591. " .long 81b,16b\n"
  592. " .long 14b,16b\n"
  593. " .long 91b,16b\n"
  594. " .long 6b,9b\n"
  595. " .long 7b,16b\n"
  596. ".previous"
  597. : "=&c"(size), "=&D" (d0), "=&S" (d1)
  598. : "1"(to), "2"(from), "0"(size)
  599. : "eax", "edx", "memory");
  600. return size;
  601. }
  602. #else
  603. /*
  604. * Leave these declared but undefined. They should not be any references to
  605. * them
  606. */
  607. unsigned long __copy_user_zeroing_intel(void *to, const void __user *from,
  608. unsigned long size);
  609. unsigned long __copy_user_intel(void __user *to, const void *from,
  610. unsigned long size);
  611. unsigned long __copy_user_zeroing_intel_nocache(void *to,
  612. const void __user *from, unsigned long size);
  613. #endif /* CONFIG_X86_INTEL_USERCOPY */
  614. /* Generic arbitrary sized copy. */
  615. #define __copy_user(to,from,size) \
  616. do { \
  617. int __d0, __d1, __d2; \
  618. __asm__ __volatile__( \
  619. " cmp $7,%0\n" \
  620. " jbe 1f\n" \
  621. " movl %1,%0\n" \
  622. " negl %0\n" \
  623. " andl $7,%0\n" \
  624. " subl %0,%3\n" \
  625. "4: rep; movsb\n" \
  626. " movl %3,%0\n" \
  627. " shrl $2,%0\n" \
  628. " andl $3,%3\n" \
  629. " .align 2,0x90\n" \
  630. "0: rep; movsl\n" \
  631. " movl %3,%0\n" \
  632. "1: rep; movsb\n" \
  633. "2:\n" \
  634. ".section .fixup,\"ax\"\n" \
  635. "5: addl %3,%0\n" \
  636. " jmp 2b\n" \
  637. "3: lea 0(%3,%0,4),%0\n" \
  638. " jmp 2b\n" \
  639. ".previous\n" \
  640. ".section __ex_table,\"a\"\n" \
  641. " .align 4\n" \
  642. " .long 4b,5b\n" \
  643. " .long 0b,3b\n" \
  644. " .long 1b,2b\n" \
  645. ".previous" \
  646. : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
  647. : "3"(size), "0"(size), "1"(to), "2"(from) \
  648. : "memory"); \
  649. } while (0)
  650. #define __copy_user_zeroing(to,from,size) \
  651. do { \
  652. int __d0, __d1, __d2; \
  653. __asm__ __volatile__( \
  654. " cmp $7,%0\n" \
  655. " jbe 1f\n" \
  656. " movl %1,%0\n" \
  657. " negl %0\n" \
  658. " andl $7,%0\n" \
  659. " subl %0,%3\n" \
  660. "4: rep; movsb\n" \
  661. " movl %3,%0\n" \
  662. " shrl $2,%0\n" \
  663. " andl $3,%3\n" \
  664. " .align 2,0x90\n" \
  665. "0: rep; movsl\n" \
  666. " movl %3,%0\n" \
  667. "1: rep; movsb\n" \
  668. "2:\n" \
  669. ".section .fixup,\"ax\"\n" \
  670. "5: addl %3,%0\n" \
  671. " jmp 6f\n" \
  672. "3: lea 0(%3,%0,4),%0\n" \
  673. "6: pushl %0\n" \
  674. " pushl %%eax\n" \
  675. " xorl %%eax,%%eax\n" \
  676. " rep; stosb\n" \
  677. " popl %%eax\n" \
  678. " popl %0\n" \
  679. " jmp 2b\n" \
  680. ".previous\n" \
  681. ".section __ex_table,\"a\"\n" \
  682. " .align 4\n" \
  683. " .long 4b,5b\n" \
  684. " .long 0b,3b\n" \
  685. " .long 1b,6b\n" \
  686. ".previous" \
  687. : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
  688. : "3"(size), "0"(size), "1"(to), "2"(from) \
  689. : "memory"); \
  690. } while (0)
  691. unsigned long __copy_to_user_ll(void __user *to, const void *from,
  692. unsigned long n)
  693. {
  694. BUG_ON((long) n < 0);
  695. #ifndef CONFIG_X86_WP_WORKS_OK
  696. if (unlikely(boot_cpu_data.wp_works_ok == 0) &&
  697. ((unsigned long )to) < TASK_SIZE) {
  698. /*
  699. * CPU does not honor the WP bit when writing
  700. * from supervisory mode, and due to preemption or SMP,
  701. * the page tables can change at any time.
  702. * Do it manually. Manfred <manfred@colorfullife.com>
  703. */
  704. while (n) {
  705. unsigned long offset = ((unsigned long)to)%PAGE_SIZE;
  706. unsigned long len = PAGE_SIZE - offset;
  707. int retval;
  708. struct page *pg;
  709. void *maddr;
  710. if (len > n)
  711. len = n;
  712. survive:
  713. down_read(&current->mm->mmap_sem);
  714. retval = get_user_pages(current, current->mm,
  715. (unsigned long )to, 1, 1, 0, &pg, NULL);
  716. if (retval == -ENOMEM && is_init(current)) {
  717. up_read(&current->mm->mmap_sem);
  718. congestion_wait(WRITE, HZ/50);
  719. goto survive;
  720. }
  721. if (retval != 1) {
  722. up_read(&current->mm->mmap_sem);
  723. break;
  724. }
  725. maddr = kmap_atomic(pg, KM_USER0);
  726. memcpy(maddr + offset, from, len);
  727. kunmap_atomic(maddr, KM_USER0);
  728. set_page_dirty_lock(pg);
  729. put_page(pg);
  730. up_read(&current->mm->mmap_sem);
  731. from += len;
  732. to += len;
  733. n -= len;
  734. }
  735. return n;
  736. }
  737. #endif
  738. if (movsl_is_ok(to, from, n))
  739. __copy_user(to, from, n);
  740. else
  741. n = __copy_user_intel(to, from, n);
  742. return n;
  743. }
  744. EXPORT_SYMBOL(__copy_to_user_ll);
  745. unsigned long __copy_from_user_ll(void *to, const void __user *from,
  746. unsigned long n)
  747. {
  748. BUG_ON((long)n < 0);
  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. BUG_ON((long)n < 0);
  760. if (movsl_is_ok(to, from, n))
  761. __copy_user(to, from, n);
  762. else
  763. n = __copy_user_intel((void __user *)to,
  764. (const void *)from, n);
  765. return n;
  766. }
  767. EXPORT_SYMBOL(__copy_from_user_ll_nozero);
  768. unsigned long __copy_from_user_ll_nocache(void *to, const void __user *from,
  769. unsigned long n)
  770. {
  771. BUG_ON((long)n < 0);
  772. #ifdef CONFIG_X86_INTEL_USERCOPY
  773. if ( n > 64 && cpu_has_xmm2)
  774. n = __copy_user_zeroing_intel_nocache(to, from, n);
  775. else
  776. __copy_user_zeroing(to, from, n);
  777. #else
  778. __copy_user_zeroing(to, from, n);
  779. #endif
  780. return n;
  781. }
  782. unsigned long __copy_from_user_ll_nocache_nozero(void *to, const void __user *from,
  783. unsigned long n)
  784. {
  785. BUG_ON((long)n < 0);
  786. #ifdef CONFIG_X86_INTEL_USERCOPY
  787. if ( n > 64 && cpu_has_xmm2)
  788. n = __copy_user_intel_nocache(to, from, n);
  789. else
  790. __copy_user(to, from, n);
  791. #else
  792. __copy_user(to, from, n);
  793. #endif
  794. return n;
  795. }
  796. /**
  797. * copy_to_user: - Copy a block of data into user space.
  798. * @to: Destination address, in user space.
  799. * @from: Source address, in kernel space.
  800. * @n: Number of bytes to copy.
  801. *
  802. * Context: User context only. This function may sleep.
  803. *
  804. * Copy data from kernel space to user space.
  805. *
  806. * Returns number of bytes that could not be copied.
  807. * On success, this will be zero.
  808. */
  809. unsigned long
  810. copy_to_user(void __user *to, const void *from, unsigned long n)
  811. {
  812. BUG_ON((long) n < 0);
  813. if (access_ok(VERIFY_WRITE, to, n))
  814. n = __copy_to_user(to, from, n);
  815. return n;
  816. }
  817. EXPORT_SYMBOL(copy_to_user);
  818. /**
  819. * copy_from_user: - Copy a block of data from user space.
  820. * @to: Destination address, in kernel space.
  821. * @from: Source address, in user space.
  822. * @n: Number of bytes to copy.
  823. *
  824. * Context: User context only. This function may sleep.
  825. *
  826. * Copy data from user space to kernel space.
  827. *
  828. * Returns number of bytes that could not be copied.
  829. * On success, this will be zero.
  830. *
  831. * If some data could not be copied, this function will pad the copied
  832. * data to the requested size using zero bytes.
  833. */
  834. unsigned long
  835. copy_from_user(void *to, const void __user *from, unsigned long n)
  836. {
  837. BUG_ON((long) n < 0);
  838. if (access_ok(VERIFY_READ, from, n))
  839. n = __copy_from_user(to, from, n);
  840. else
  841. memset(to, 0, n);
  842. return n;
  843. }
  844. EXPORT_SYMBOL(copy_from_user);