copy_user.S 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* copy_user.S: Sparc optimized copy_from_user and copy_to_user code.
  3. *
  4. * Copyright(C) 1995 Linus Torvalds
  5. * Copyright(C) 1996 David S. Miller
  6. * Copyright(C) 1996 Eddie C. Dost
  7. * Copyright(C) 1996,1998 Jakub Jelinek
  8. *
  9. * derived from:
  10. * e-mail between David and Eddie.
  11. *
  12. * Returns 0 if successful, otherwise count of bytes not copied yet
  13. */
  14. #include <asm/ptrace.h>
  15. #include <asm/asmmacro.h>
  16. #include <asm/page.h>
  17. #include <asm/thread_info.h>
  18. #include <asm/export.h>
  19. /* Work around cpp -rob */
  20. #define ALLOC #alloc
  21. #define EXECINSTR #execinstr
  22. #define EX_ENTRY(l1, l2) \
  23. .section __ex_table,ALLOC; \
  24. .align 4; \
  25. .word l1, l2; \
  26. .text;
  27. #define EX(x,y,a,b) \
  28. 98: x,y; \
  29. .section .fixup,ALLOC,EXECINSTR; \
  30. .align 4; \
  31. 99: retl; \
  32. a, b, %o0; \
  33. EX_ENTRY(98b, 99b)
  34. #define EX2(x,y,c,d,e,a,b) \
  35. 98: x,y; \
  36. .section .fixup,ALLOC,EXECINSTR; \
  37. .align 4; \
  38. 99: c, d, e; \
  39. retl; \
  40. a, b, %o0; \
  41. EX_ENTRY(98b, 99b)
  42. #define EXO2(x,y) \
  43. 98: x, y; \
  44. EX_ENTRY(98b, 97f)
  45. #define LD(insn, src, offset, reg, label) \
  46. 98: insn [%src + (offset)], %reg; \
  47. .section .fixup,ALLOC,EXECINSTR; \
  48. 99: ba label; \
  49. mov offset, %g5; \
  50. EX_ENTRY(98b, 99b)
  51. #define ST(insn, dst, offset, reg, label) \
  52. 98: insn %reg, [%dst + (offset)]; \
  53. .section .fixup,ALLOC,EXECINSTR; \
  54. 99: ba label; \
  55. mov offset, %g5; \
  56. EX_ENTRY(98b, 99b)
  57. /* Both these macros have to start with exactly the same insn */
  58. /* left: g7 + (g1 % 128) - offset */
  59. #define MOVE_BIGCHUNK(src, dst, offset, t0, t1, t2, t3, t4, t5, t6, t7) \
  60. LD(ldd, src, offset + 0x00, t0, bigchunk_fault) \
  61. LD(ldd, src, offset + 0x08, t2, bigchunk_fault) \
  62. LD(ldd, src, offset + 0x10, t4, bigchunk_fault) \
  63. LD(ldd, src, offset + 0x18, t6, bigchunk_fault) \
  64. ST(st, dst, offset + 0x00, t0, bigchunk_fault) \
  65. ST(st, dst, offset + 0x04, t1, bigchunk_fault) \
  66. ST(st, dst, offset + 0x08, t2, bigchunk_fault) \
  67. ST(st, dst, offset + 0x0c, t3, bigchunk_fault) \
  68. ST(st, dst, offset + 0x10, t4, bigchunk_fault) \
  69. ST(st, dst, offset + 0x14, t5, bigchunk_fault) \
  70. ST(st, dst, offset + 0x18, t6, bigchunk_fault) \
  71. ST(st, dst, offset + 0x1c, t7, bigchunk_fault)
  72. /* left: g7 + (g1 % 128) - offset */
  73. #define MOVE_BIGALIGNCHUNK(src, dst, offset, t0, t1, t2, t3, t4, t5, t6, t7) \
  74. LD(ldd, src, offset + 0x00, t0, bigchunk_fault) \
  75. LD(ldd, src, offset + 0x08, t2, bigchunk_fault) \
  76. LD(ldd, src, offset + 0x10, t4, bigchunk_fault) \
  77. LD(ldd, src, offset + 0x18, t6, bigchunk_fault) \
  78. ST(std, dst, offset + 0x00, t0, bigchunk_fault) \
  79. ST(std, dst, offset + 0x08, t2, bigchunk_fault) \
  80. ST(std, dst, offset + 0x10, t4, bigchunk_fault) \
  81. ST(std, dst, offset + 0x18, t6, bigchunk_fault)
  82. .section .fixup,#alloc,#execinstr
  83. bigchunk_fault:
  84. sub %g7, %g5, %o0
  85. and %g1, 127, %g1
  86. retl
  87. add %o0, %g1, %o0
  88. /* left: offset + 16 + (g1 % 16) */
  89. #define MOVE_LASTCHUNK(src, dst, offset, t0, t1, t2, t3) \
  90. LD(ldd, src, -(offset + 0x10), t0, lastchunk_fault) \
  91. LD(ldd, src, -(offset + 0x08), t2, lastchunk_fault) \
  92. ST(st, dst, -(offset + 0x10), t0, lastchunk_fault) \
  93. ST(st, dst, -(offset + 0x0c), t1, lastchunk_fault) \
  94. ST(st, dst, -(offset + 0x08), t2, lastchunk_fault) \
  95. ST(st, dst, -(offset + 0x04), t3, lastchunk_fault)
  96. .section .fixup,#alloc,#execinstr
  97. lastchunk_fault:
  98. and %g1, 15, %g1
  99. retl
  100. sub %g1, %g5, %o0
  101. /* left: o3 + (o2 % 16) - offset */
  102. #define MOVE_HALFCHUNK(src, dst, offset, t0, t1, t2, t3) \
  103. LD(lduh, src, offset + 0x00, t0, halfchunk_fault) \
  104. LD(lduh, src, offset + 0x02, t1, halfchunk_fault) \
  105. LD(lduh, src, offset + 0x04, t2, halfchunk_fault) \
  106. LD(lduh, src, offset + 0x06, t3, halfchunk_fault) \
  107. ST(sth, dst, offset + 0x00, t0, halfchunk_fault) \
  108. ST(sth, dst, offset + 0x02, t1, halfchunk_fault) \
  109. ST(sth, dst, offset + 0x04, t2, halfchunk_fault) \
  110. ST(sth, dst, offset + 0x06, t3, halfchunk_fault)
  111. /* left: o3 + (o2 % 16) + offset + 2 */
  112. #define MOVE_SHORTCHUNK(src, dst, offset, t0, t1) \
  113. LD(ldub, src, -(offset + 0x02), t0, halfchunk_fault) \
  114. LD(ldub, src, -(offset + 0x01), t1, halfchunk_fault) \
  115. ST(stb, dst, -(offset + 0x02), t0, halfchunk_fault) \
  116. ST(stb, dst, -(offset + 0x01), t1, halfchunk_fault)
  117. .section .fixup,#alloc,#execinstr
  118. halfchunk_fault:
  119. and %o2, 15, %o2
  120. sub %o3, %g5, %o3
  121. retl
  122. add %o2, %o3, %o0
  123. /* left: offset + 2 + (o2 % 2) */
  124. #define MOVE_LAST_SHORTCHUNK(src, dst, offset, t0, t1) \
  125. LD(ldub, src, -(offset + 0x02), t0, last_shortchunk_fault) \
  126. LD(ldub, src, -(offset + 0x01), t1, last_shortchunk_fault) \
  127. ST(stb, dst, -(offset + 0x02), t0, last_shortchunk_fault) \
  128. ST(stb, dst, -(offset + 0x01), t1, last_shortchunk_fault)
  129. .section .fixup,#alloc,#execinstr
  130. last_shortchunk_fault:
  131. and %o2, 1, %o2
  132. retl
  133. sub %o2, %g5, %o0
  134. .text
  135. .align 4
  136. .globl __copy_user_begin
  137. __copy_user_begin:
  138. .globl __copy_user
  139. EXPORT_SYMBOL(__copy_user)
  140. dword_align:
  141. andcc %o1, 1, %g0
  142. be 4f
  143. andcc %o1, 2, %g0
  144. EXO2(ldub [%o1], %g2)
  145. add %o1, 1, %o1
  146. EXO2(stb %g2, [%o0])
  147. sub %o2, 1, %o2
  148. bne 3f
  149. add %o0, 1, %o0
  150. EXO2(lduh [%o1], %g2)
  151. add %o1, 2, %o1
  152. EXO2(sth %g2, [%o0])
  153. sub %o2, 2, %o2
  154. b 3f
  155. add %o0, 2, %o0
  156. 4:
  157. EXO2(lduh [%o1], %g2)
  158. add %o1, 2, %o1
  159. EXO2(sth %g2, [%o0])
  160. sub %o2, 2, %o2
  161. b 3f
  162. add %o0, 2, %o0
  163. __copy_user: /* %o0=dst %o1=src %o2=len */
  164. xor %o0, %o1, %o4
  165. 1:
  166. andcc %o4, 3, %o5
  167. 2:
  168. bne cannot_optimize
  169. cmp %o2, 15
  170. bleu short_aligned_end
  171. andcc %o1, 3, %g0
  172. bne dword_align
  173. 3:
  174. andcc %o1, 4, %g0
  175. be 2f
  176. mov %o2, %g1
  177. EXO2(ld [%o1], %o4)
  178. sub %g1, 4, %g1
  179. EXO2(st %o4, [%o0])
  180. add %o1, 4, %o1
  181. add %o0, 4, %o0
  182. 2:
  183. andcc %g1, 0xffffff80, %g7
  184. be 3f
  185. andcc %o0, 4, %g0
  186. be ldd_std + 4
  187. 5:
  188. MOVE_BIGCHUNK(o1, o0, 0x00, o2, o3, o4, o5, g2, g3, g4, g5)
  189. MOVE_BIGCHUNK(o1, o0, 0x20, o2, o3, o4, o5, g2, g3, g4, g5)
  190. MOVE_BIGCHUNK(o1, o0, 0x40, o2, o3, o4, o5, g2, g3, g4, g5)
  191. MOVE_BIGCHUNK(o1, o0, 0x60, o2, o3, o4, o5, g2, g3, g4, g5)
  192. subcc %g7, 128, %g7
  193. add %o1, 128, %o1
  194. bne 5b
  195. add %o0, 128, %o0
  196. 3:
  197. andcc %g1, 0x70, %g7
  198. be copy_user_table_end
  199. andcc %g1, 8, %g0
  200. sethi %hi(copy_user_table_end), %o5
  201. srl %g7, 1, %o4
  202. add %g7, %o4, %o4
  203. add %o1, %g7, %o1
  204. sub %o5, %o4, %o5
  205. jmpl %o5 + %lo(copy_user_table_end), %g0
  206. add %o0, %g7, %o0
  207. MOVE_LASTCHUNK(o1, o0, 0x60, g2, g3, g4, g5)
  208. MOVE_LASTCHUNK(o1, o0, 0x50, g2, g3, g4, g5)
  209. MOVE_LASTCHUNK(o1, o0, 0x40, g2, g3, g4, g5)
  210. MOVE_LASTCHUNK(o1, o0, 0x30, g2, g3, g4, g5)
  211. MOVE_LASTCHUNK(o1, o0, 0x20, g2, g3, g4, g5)
  212. MOVE_LASTCHUNK(o1, o0, 0x10, g2, g3, g4, g5)
  213. MOVE_LASTCHUNK(o1, o0, 0x00, g2, g3, g4, g5)
  214. copy_user_table_end:
  215. be copy_user_last7
  216. andcc %g1, 4, %g0
  217. EX(ldd [%o1], %g2, and %g1, 0xf)
  218. add %o0, 8, %o0
  219. add %o1, 8, %o1
  220. EX(st %g2, [%o0 - 0x08], and %g1, 0xf)
  221. EX2(st %g3, [%o0 - 0x04], and %g1, 0xf, %g1, sub %g1, 4)
  222. copy_user_last7:
  223. be 1f
  224. andcc %g1, 2, %g0
  225. EX(ld [%o1], %g2, and %g1, 7)
  226. add %o1, 4, %o1
  227. EX(st %g2, [%o0], and %g1, 7)
  228. add %o0, 4, %o0
  229. 1:
  230. be 1f
  231. andcc %g1, 1, %g0
  232. EX(lduh [%o1], %g2, and %g1, 3)
  233. add %o1, 2, %o1
  234. EX(sth %g2, [%o0], and %g1, 3)
  235. add %o0, 2, %o0
  236. 1:
  237. be 1f
  238. nop
  239. EX(ldub [%o1], %g2, add %g0, 1)
  240. EX(stb %g2, [%o0], add %g0, 1)
  241. 1:
  242. retl
  243. clr %o0
  244. ldd_std:
  245. MOVE_BIGALIGNCHUNK(o1, o0, 0x00, o2, o3, o4, o5, g2, g3, g4, g5)
  246. MOVE_BIGALIGNCHUNK(o1, o0, 0x20, o2, o3, o4, o5, g2, g3, g4, g5)
  247. MOVE_BIGALIGNCHUNK(o1, o0, 0x40, o2, o3, o4, o5, g2, g3, g4, g5)
  248. MOVE_BIGALIGNCHUNK(o1, o0, 0x60, o2, o3, o4, o5, g2, g3, g4, g5)
  249. subcc %g7, 128, %g7
  250. add %o1, 128, %o1
  251. bne ldd_std
  252. add %o0, 128, %o0
  253. andcc %g1, 0x70, %g7
  254. be copy_user_table_end
  255. andcc %g1, 8, %g0
  256. sethi %hi(copy_user_table_end), %o5
  257. srl %g7, 1, %o4
  258. add %g7, %o4, %o4
  259. add %o1, %g7, %o1
  260. sub %o5, %o4, %o5
  261. jmpl %o5 + %lo(copy_user_table_end), %g0
  262. add %o0, %g7, %o0
  263. cannot_optimize:
  264. bleu short_end
  265. cmp %o5, 2
  266. bne byte_chunk
  267. and %o2, 0xfffffff0, %o3
  268. andcc %o1, 1, %g0
  269. be 10f
  270. nop
  271. EXO2(ldub [%o1], %g2)
  272. add %o1, 1, %o1
  273. EXO2(stb %g2, [%o0])
  274. sub %o2, 1, %o2
  275. andcc %o2, 0xfffffff0, %o3
  276. be short_end
  277. add %o0, 1, %o0
  278. 10:
  279. MOVE_HALFCHUNK(o1, o0, 0x00, g2, g3, g4, g5)
  280. MOVE_HALFCHUNK(o1, o0, 0x08, g2, g3, g4, g5)
  281. subcc %o3, 0x10, %o3
  282. add %o1, 0x10, %o1
  283. bne 10b
  284. add %o0, 0x10, %o0
  285. b 2f
  286. and %o2, 0xe, %o3
  287. byte_chunk:
  288. MOVE_SHORTCHUNK(o1, o0, -0x02, g2, g3)
  289. MOVE_SHORTCHUNK(o1, o0, -0x04, g2, g3)
  290. MOVE_SHORTCHUNK(o1, o0, -0x06, g2, g3)
  291. MOVE_SHORTCHUNK(o1, o0, -0x08, g2, g3)
  292. MOVE_SHORTCHUNK(o1, o0, -0x0a, g2, g3)
  293. MOVE_SHORTCHUNK(o1, o0, -0x0c, g2, g3)
  294. MOVE_SHORTCHUNK(o1, o0, -0x0e, g2, g3)
  295. MOVE_SHORTCHUNK(o1, o0, -0x10, g2, g3)
  296. subcc %o3, 0x10, %o3
  297. add %o1, 0x10, %o1
  298. bne byte_chunk
  299. add %o0, 0x10, %o0
  300. short_end:
  301. and %o2, 0xe, %o3
  302. 2:
  303. sethi %hi(short_table_end), %o5
  304. sll %o3, 3, %o4
  305. add %o0, %o3, %o0
  306. sub %o5, %o4, %o5
  307. add %o1, %o3, %o1
  308. jmpl %o5 + %lo(short_table_end), %g0
  309. andcc %o2, 1, %g0
  310. MOVE_LAST_SHORTCHUNK(o1, o0, 0x0c, g2, g3)
  311. MOVE_LAST_SHORTCHUNK(o1, o0, 0x0a, g2, g3)
  312. MOVE_LAST_SHORTCHUNK(o1, o0, 0x08, g2, g3)
  313. MOVE_LAST_SHORTCHUNK(o1, o0, 0x06, g2, g3)
  314. MOVE_LAST_SHORTCHUNK(o1, o0, 0x04, g2, g3)
  315. MOVE_LAST_SHORTCHUNK(o1, o0, 0x02, g2, g3)
  316. MOVE_LAST_SHORTCHUNK(o1, o0, 0x00, g2, g3)
  317. short_table_end:
  318. be 1f
  319. nop
  320. EX(ldub [%o1], %g2, add %g0, 1)
  321. EX(stb %g2, [%o0], add %g0, 1)
  322. 1:
  323. retl
  324. clr %o0
  325. short_aligned_end:
  326. bne short_end
  327. andcc %o2, 8, %g0
  328. be 1f
  329. andcc %o2, 4, %g0
  330. EXO2(ld [%o1 + 0x00], %g2)
  331. EXO2(ld [%o1 + 0x04], %g3)
  332. add %o1, 8, %o1
  333. EXO2(st %g2, [%o0 + 0x00])
  334. EX(st %g3, [%o0 + 0x04], sub %o2, 4)
  335. add %o0, 8, %o0
  336. 1:
  337. b copy_user_last7
  338. mov %o2, %g1
  339. .section .fixup,#alloc,#execinstr
  340. .align 4
  341. 97:
  342. retl
  343. mov %o2, %o0
  344. .globl __copy_user_end
  345. __copy_user_end: