2 * arch/xtensa/lib/strncpy_user.S
4 * This file is subject to the terms and conditions of the GNU General
5 * Public License. See the file "COPYING" in the main directory of
6 * this archive for more details.
8 * Returns: -EFAULT if exception before terminator, N if the entire
9 * buffer filled, else strlen.
11 * Copyright (C) 2002 Tensilica Inc.
14 #include <variant/core.h>
15 #include <linux/errno.h>
17 /* Load or store instructions that may cause exceptions use the EX macro. */
19 #define EX(insn,reg1,reg2,offset,handler) \
20 9: insn reg1, reg2, offset; \
21 .section __ex_table, "a"; \
26 * char *__strncpy_user(char *dst, const char *src, size_t len)
30 # define MASK0 0xff000000
31 # define MASK1 0x00ff0000
32 # define MASK2 0x0000ff00
33 # define MASK3 0x000000ff
35 # define MASK0 0x000000ff
36 # define MASK1 0x0000ff00
37 # define MASK2 0x00ff0000
38 # define MASK3 0xff000000
58 .global __strncpy_user
59 .type __strncpy_user,@function
61 entry sp, 16 # minimal stack frame
62 # a2/ dst, a3/ src, a4/ len
63 mov a11, a2 # leave dst in return value register
64 beqz a4, .Lret # if len is zero
65 movi a5, MASK0 # mask for byte 0
66 movi a6, MASK1 # mask for byte 1
67 movi a7, MASK2 # mask for byte 2
68 movi a8, MASK3 # mask for byte 3
69 bbsi.l a3, 0, .Lsrc1mod2 # if only 8-bit aligned
70 bbsi.l a3, 1, .Lsrc2mod4 # if only 16-bit aligned
71 .Lsrcaligned: # return here when src is word-aligned
72 srli a12, a4, 2 # number of loop iterations with 4B per loop
74 bnone a11, a9, .Laligned
77 .Lsrc1mod2: # src address is odd
78 EX(l8ui, a9, a3, 0, fixup_l) # get byte 0
79 addi a3, a3, 1 # advance src pointer
80 EX(s8i, a9, a11, 0, fixup_s) # store byte 0
81 beqz a9, .Lret # if byte 0 is zero
82 addi a11, a11, 1 # advance dst pointer
83 addi a4, a4, -1 # decrement len
84 beqz a4, .Lret # if len is zero
85 bbci.l a3, 1, .Lsrcaligned # if src is now word-aligned
87 .Lsrc2mod4: # src address is 2 mod 4
88 EX(l8ui, a9, a3, 0, fixup_l) # get byte 0
89 /* 1-cycle interlock */
90 EX(s8i, a9, a11, 0, fixup_s) # store byte 0
91 beqz a9, .Lret # if byte 0 is zero
92 addi a11, a11, 1 # advance dst pointer
93 addi a4, a4, -1 # decrement len
94 beqz a4, .Lret # if len is zero
95 EX(l8ui, a9, a3, 1, fixup_l) # get byte 0
96 addi a3, a3, 2 # advance src pointer
97 EX(s8i, a9, a11, 0, fixup_s) # store byte 0
98 beqz a9, .Lret # if byte 0 is zero
99 addi a11, a11, 1 # advance dst pointer
100 addi a4, a4, -1 # decrement len
101 bnez a4, .Lsrcaligned # if len is nonzero
103 sub a2, a11, a2 # compute strlen
107 * dst is word-aligned, src is word-aligned
109 .align 4 # 1 mod 4 alignment for LOOPNEZ
110 .byte 0 # (0 mod 4 alignment for LBEG)
113 loopnez a12, .Loop1done
117 add a12, a12, a11 # a12 = end of last 4B chunck
120 EX(l32i, a9, a3, 0, fixup_l) # get word from src
121 addi a3, a3, 4 # advance src pointer
122 bnone a9, a5, .Lz0 # if byte 0 is zero
123 bnone a9, a6, .Lz1 # if byte 1 is zero
124 bnone a9, a7, .Lz2 # if byte 2 is zero
125 EX(s32i, a9, a11, 0, fixup_s) # store word to dst
126 bnone a9, a8, .Lz3 # if byte 3 is zero
127 addi a11, a11, 4 # advance dst pointer
128 #if !XCHAL_HAVE_LOOPS
135 EX(l16ui, a9, a3, 0, fixup_l)
136 addi a3, a3, 2 # advance src pointer
138 bnone a9, a7, .Lz0 # if byte 2 is zero
139 bnone a9, a8, .Lz1 # if byte 3 is zero
141 bnone a9, a5, .Lz0 # if byte 0 is zero
142 bnone a9, a6, .Lz1 # if byte 1 is zero
144 EX(s16i, a9, a11, 0, fixup_s)
145 addi a11, a11, 2 # advance dst pointer
148 EX(l8ui, a9, a3, 0, fixup_l)
150 EX(s8i, a9, a11, 0, fixup_s)
151 beqz a9, .Lret # if byte is zero
152 addi a11, a11, 1-3 # advance dst ptr 1, but also cancel
153 # the effect of adding 3 in .Lz3 code
154 /* fall thru to .Lz3 and "retw" */
156 .Lz3: # byte 3 is zero
157 addi a11, a11, 3 # advance dst pointer
158 sub a2, a11, a2 # compute strlen
160 .Lz0: # byte 0 is zero
163 #endif /* __XTENSA_EB__ */
164 EX(s8i, a9, a11, 0, fixup_s)
165 sub a2, a11, a2 # compute strlen
167 .Lz1: # byte 1 is zero
170 #endif /* __XTENSA_EB__ */
171 EX(s16i, a9, a11, 0, fixup_s)
172 addi a11, a11, 1 # advance dst pointer
173 sub a2, a11, a2 # compute strlen
175 .Lz2: # byte 2 is zero
178 #endif /* __XTENSA_EB__ */
179 EX(s16i, a9, a11, 0, fixup_s)
181 EX(s8i, a9, a11, 2, fixup_s)
182 addi a11, a11, 2 # advance dst pointer
183 sub a2, a11, a2 # compute strlen
186 .align 4 # 1 mod 4 alignment for LOOPNEZ
187 .byte 0 # (0 mod 4 alignment for LBEG)
190 * for now just use byte copy loop
193 loopnez a4, .Lunalignedend
195 beqz a4, .Lunalignedend
196 add a12, a11, a4 # a12 = ending address
197 #endif /* XCHAL_HAVE_LOOPS */
199 EX(l8ui, a9, a3, 0, fixup_l)
201 EX(s8i, a9, a11, 0, fixup_s)
202 beqz a9, .Lunalignedend
204 #if !XCHAL_HAVE_LOOPS
205 blt a11, a12, .Lnextbyte
209 sub a2, a11, a2 # compute strlen
213 .section .fixup, "ax"
216 /* For now, just return -EFAULT. Future implementations might
217 * like to clear remaining kernel space, like the fixup
218 * implementation in memset(). Thus, we differentiate between
219 * load/store fixups. */