xref: /linux/arch/xtensa/lib/strnlen_user.S (revision a0bb46ba074d2442e96f55c997293767340f4ce9)
1249ac17eSChris Zankel/*
2249ac17eSChris Zankel *  arch/xtensa/lib/strnlen_user.S
3249ac17eSChris Zankel *
4249ac17eSChris Zankel *  This file is subject to the terms and conditions of the GNU General
5249ac17eSChris Zankel *  Public License.  See the file "COPYING" in the main directory of
6249ac17eSChris Zankel *  this archive for more details.
7249ac17eSChris Zankel *
8249ac17eSChris Zankel *  Returns strnlen, including trailing zero terminator.
9249ac17eSChris Zankel *  Zero indicates error.
10249ac17eSChris Zankel *
11249ac17eSChris Zankel *  Copyright (C) 2002 Tensilica Inc.
12249ac17eSChris Zankel */
13249ac17eSChris Zankel
14173d6681SChris Zankel#include <asm/variant/core.h>
15249ac17eSChris Zankel
16249ac17eSChris Zankel/* Load or store instructions that may cause exceptions use the EX macro. */
17249ac17eSChris Zankel
18249ac17eSChris Zankel#define EX(insn,reg1,reg2,offset,handler)	\
19249ac17eSChris Zankel9:	insn	reg1, reg2, offset;		\
20249ac17eSChris Zankel	.section __ex_table, "a";		\
21249ac17eSChris Zankel	.word	9b, handler;			\
22249ac17eSChris Zankel	.previous
23249ac17eSChris Zankel
24249ac17eSChris Zankel/*
25249ac17eSChris Zankel * size_t __strnlen_user(const char *s, size_t len)
26249ac17eSChris Zankel */
27*a0bb46baSChris Zankel
28*a0bb46baSChris Zankel#ifdef __XTENSA_EB__
29*a0bb46baSChris Zankel# define MASK0 0xff000000
30*a0bb46baSChris Zankel# define MASK1 0x00ff0000
31*a0bb46baSChris Zankel# define MASK2 0x0000ff00
32*a0bb46baSChris Zankel# define MASK3 0x000000ff
33*a0bb46baSChris Zankel#else
34*a0bb46baSChris Zankel# define MASK0 0x000000ff
35*a0bb46baSChris Zankel# define MASK1 0x0000ff00
36*a0bb46baSChris Zankel# define MASK2 0x00ff0000
37*a0bb46baSChris Zankel# define MASK3 0xff000000
38*a0bb46baSChris Zankel#endif
39249ac17eSChris Zankel
40249ac17eSChris Zankel# Register use:
41249ac17eSChris Zankel#   a2/ src
42249ac17eSChris Zankel#   a3/ len
43249ac17eSChris Zankel#   a4/ tmp
44249ac17eSChris Zankel#   a5/ mask0
45249ac17eSChris Zankel#   a6/ mask1
46249ac17eSChris Zankel#   a7/ mask2
47249ac17eSChris Zankel#   a8/ mask3
48249ac17eSChris Zankel#   a9/ tmp
49249ac17eSChris Zankel#   a10/ tmp
50249ac17eSChris Zankel
51*a0bb46baSChris Zankel.text
52249ac17eSChris Zankel.align	4
53249ac17eSChris Zankel.global	__strnlen_user
54249ac17eSChris Zankel.type	__strnlen_user,@function
55249ac17eSChris Zankel__strnlen_user:
56249ac17eSChris Zankel	entry	sp, 16		# minimal stack frame
57249ac17eSChris Zankel	# a2/ s, a3/ len
58249ac17eSChris Zankel	addi	a4, a2, -4	# because we overincrement at the end;
59249ac17eSChris Zankel				# we compensate with load offsets of 4
60*a0bb46baSChris Zankel	movi	a5, MASK0	# mask for byte 0
61*a0bb46baSChris Zankel	movi	a6, MASK1	# mask for byte 1
62*a0bb46baSChris Zankel	movi	a7, MASK2	# mask for byte 2
63*a0bb46baSChris Zankel	movi	a8, MASK3	# mask for byte 3
64249ac17eSChris Zankel	bbsi.l	a2, 0, .L1mod2	# if only  8-bit aligned
65249ac17eSChris Zankel	bbsi.l	a2, 1, .L2mod4	# if only 16-bit aligned
66249ac17eSChris Zankel
67249ac17eSChris Zankel/*
68249ac17eSChris Zankel * String is word-aligned.
69249ac17eSChris Zankel */
70249ac17eSChris Zankel.Laligned:
71249ac17eSChris Zankel	srli	a10, a3, 2	# number of loop iterations with 4B per loop
72249ac17eSChris Zankel#if XCHAL_HAVE_LOOPS
73249ac17eSChris Zankel	loopnez	a10, .Ldone
74249ac17eSChris Zankel#else
75249ac17eSChris Zankel	beqz	a10, .Ldone
76249ac17eSChris Zankel	slli	a10, a10, 2
77249ac17eSChris Zankel	add	a10, a10, a4	# a10 = end of last 4B chunk
78249ac17eSChris Zankel#endif /* XCHAL_HAVE_LOOPS */
79249ac17eSChris Zankel.Loop:
80249ac17eSChris Zankel	EX(l32i, a9, a4, 4, lenfixup)	# get next word of string
81249ac17eSChris Zankel	addi	a4, a4, 4		# advance string pointer
82249ac17eSChris Zankel	bnone	a9, a5, .Lz0		# if byte 0 is zero
83249ac17eSChris Zankel	bnone	a9, a6, .Lz1		# if byte 1 is zero
84249ac17eSChris Zankel	bnone	a9, a7, .Lz2		# if byte 2 is zero
85249ac17eSChris Zankel	bnone	a9, a8, .Lz3		# if byte 3 is zero
86249ac17eSChris Zankel#if !XCHAL_HAVE_LOOPS
87249ac17eSChris Zankel	blt	a4, a10, .Loop
88249ac17eSChris Zankel#endif
89249ac17eSChris Zankel
90249ac17eSChris Zankel.Ldone:
91249ac17eSChris Zankel	EX(l32i, a9, a4, 4, lenfixup)	# load 4 bytes for remaining checks
92249ac17eSChris Zankel
93249ac17eSChris Zankel	bbci.l	a3, 1, .L100
94249ac17eSChris Zankel	# check two more bytes (bytes 0, 1 of word)
95249ac17eSChris Zankel	addi	a4, a4, 2	# advance string pointer
96249ac17eSChris Zankel	bnone	a9, a5, .Lz0	# if byte 0 is zero
97249ac17eSChris Zankel	bnone	a9, a6, .Lz1	# if byte 1 is zero
98249ac17eSChris Zankel.L100:
99249ac17eSChris Zankel	bbci.l	a3, 0, .L101
100249ac17eSChris Zankel	# check one more byte (byte 2 of word)
101249ac17eSChris Zankel	# Actually, we don't need to check.  Zero or nonzero, we'll add one.
102249ac17eSChris Zankel	# Do not add an extra one for the NULL terminator since we have
103249ac17eSChris Zankel	#  exhausted the original len parameter.
104249ac17eSChris Zankel	addi	a4, a4, 1	# advance string pointer
105249ac17eSChris Zankel.L101:
106249ac17eSChris Zankel	sub	a2, a4, a2	# compute length
107249ac17eSChris Zankel	retw
108249ac17eSChris Zankel
109249ac17eSChris Zankel# NOTE that in several places below, we point to the byte just after
110249ac17eSChris Zankel# the zero byte in order to include the NULL terminator in the count.
111249ac17eSChris Zankel
112249ac17eSChris Zankel.Lz3:	# byte 3 is zero
113249ac17eSChris Zankel	addi	a4, a4, 3	# point to zero byte
114249ac17eSChris Zankel.Lz0:	# byte 0 is zero
115249ac17eSChris Zankel	addi	a4, a4, 1	# point just beyond zero byte
116249ac17eSChris Zankel	sub	a2, a4, a2	# subtract to get length
117249ac17eSChris Zankel	retw
118249ac17eSChris Zankel.Lz1:	# byte 1 is zero
119249ac17eSChris Zankel	addi	a4, a4, 1+1	# point just beyond zero byte
120249ac17eSChris Zankel	sub	a2, a4, a2	# subtract to get length
121249ac17eSChris Zankel	retw
122249ac17eSChris Zankel.Lz2:	# byte 2 is zero
123249ac17eSChris Zankel	addi	a4, a4, 2+1	# point just beyond zero byte
124249ac17eSChris Zankel	sub	a2, a4, a2	# subtract to get length
125249ac17eSChris Zankel	retw
126249ac17eSChris Zankel
127249ac17eSChris Zankel.L1mod2:	# address is odd
128249ac17eSChris Zankel	EX(l8ui, a9, a4, 4, lenfixup)	# get byte 0
129249ac17eSChris Zankel	addi	a4, a4, 1		# advance string pointer
130249ac17eSChris Zankel	beqz	a9, .Lz3		# if byte 0 is zero
131249ac17eSChris Zankel	bbci.l	a4, 1, .Laligned	# if string pointer is now word-aligned
132249ac17eSChris Zankel
133249ac17eSChris Zankel.L2mod4:	# address is 2 mod 4
134249ac17eSChris Zankel	addi	a4, a4, 2	# advance ptr for aligned access
135249ac17eSChris Zankel	EX(l32i, a9, a4, 0, lenfixup)	# get word with first two bytes of string
136249ac17eSChris Zankel	bnone	a9, a7, .Lz2	# if byte 2 (of word, not string) is zero
137249ac17eSChris Zankel	bany	a9, a8, .Laligned # if byte 3 (of word, not string) is nonzero
138249ac17eSChris Zankel	# byte 3 is zero
139249ac17eSChris Zankel	addi	a4, a4, 3+1	# point just beyond zero byte
140249ac17eSChris Zankel	sub	a2, a4, a2	# subtract to get length
141249ac17eSChris Zankel	retw
142249ac17eSChris Zankel
143249ac17eSChris Zankel	.section .fixup, "ax"
144249ac17eSChris Zankel	.align	4
145249ac17eSChris Zankellenfixup:
146249ac17eSChris Zankel	movi	a2, 0
147249ac17eSChris Zankel	retw
148249ac17eSChris Zankel
149