/*1* arch/xtensa/lib/strnlen_user.S2*3* This file is subject to the terms and conditions of the GNU General4* Public License. See the file "COPYING" in the main directory of5* this archive for more details.6*7* Returns strnlen, including trailing zero terminator.8* Zero indicates error.9*10* Copyright (C) 2002 Tensilica Inc.11*/1213#include <linux/linkage.h>14#include <asm/asmmacro.h>15#include <asm/core.h>1617/*18* size_t __strnlen_user(const char *s, size_t len)19*/2021#ifdef __XTENSA_EB__22# define MASK0 0xff00000023# define MASK1 0x00ff000024# define MASK2 0x0000ff0025# define MASK3 0x000000ff26#else27# define MASK0 0x000000ff28# define MASK1 0x0000ff0029# define MASK2 0x00ff000030# define MASK3 0xff00000031#endif3233# Register use:34# a2/ src35# a3/ len36# a4/ tmp37# a5/ mask038# a6/ mask139# a7/ mask240# a8/ mask341# a9/ tmp42# a10/ tmp4344.text45ENTRY(__strnlen_user)4647abi_entry_default48# a2/ s, a3/ len49addi a4, a2, -4 # because we overincrement at the end;50# we compensate with load offsets of 451movi a5, MASK0 # mask for byte 052movi a6, MASK1 # mask for byte 153movi a7, MASK2 # mask for byte 254movi a8, MASK3 # mask for byte 355bbsi.l a2, 0, .L1mod2 # if only 8-bit aligned56bbsi.l a2, 1, .L2mod4 # if only 16-bit aligned5758/*59* String is word-aligned.60*/61.Laligned:62srli a10, a3, 2 # number of loop iterations with 4B per loop63#if XCHAL_HAVE_LOOPS64loopnez a10, .Ldone65#else66beqz a10, .Ldone67slli a10, a10, 268add a10, a10, a4 # a10 = end of last 4B chunk69#endif /* XCHAL_HAVE_LOOPS */70.Loop:71EX(10f) l32i a9, a4, 4 # get next word of string72addi a4, a4, 4 # advance string pointer73bnone a9, a5, .Lz0 # if byte 0 is zero74bnone a9, a6, .Lz1 # if byte 1 is zero75bnone a9, a7, .Lz2 # if byte 2 is zero76bnone a9, a8, .Lz3 # if byte 3 is zero77#if !XCHAL_HAVE_LOOPS78blt a4, a10, .Loop79#endif8081.Ldone:82EX(10f) l32i a9, a4, 4 # load 4 bytes for remaining checks8384bbci.l a3, 1, .L10085# check two more bytes (bytes 0, 1 of word)86addi a4, a4, 2 # advance string pointer87bnone a9, a5, .Lz0 # if byte 0 is zero88bnone a9, a6, .Lz1 # if byte 1 is zero89.L100:90bbci.l a3, 0, .L10191# check one more byte (byte 2 of word)92# Actually, we don't need to check. Zero or nonzero, we'll add one.93# Do not add an extra one for the NULL terminator since we have94# exhausted the original len parameter.95addi a4, a4, 1 # advance string pointer96.L101:97sub a2, a4, a2 # compute length98abi_ret_default99100# NOTE that in several places below, we point to the byte just after101# the zero byte in order to include the NULL terminator in the count.102103.Lz3: # byte 3 is zero104addi a4, a4, 3 # point to zero byte105.Lz0: # byte 0 is zero106addi a4, a4, 1 # point just beyond zero byte107sub a2, a4, a2 # subtract to get length108abi_ret_default109.Lz1: # byte 1 is zero110addi a4, a4, 1+1 # point just beyond zero byte111sub a2, a4, a2 # subtract to get length112abi_ret_default113.Lz2: # byte 2 is zero114addi a4, a4, 2+1 # point just beyond zero byte115sub a2, a4, a2 # subtract to get length116abi_ret_default117118.L1mod2: # address is odd119EX(10f) l8ui a9, a4, 4 # get byte 0120addi a4, a4, 1 # advance string pointer121beqz a9, .Lz3 # if byte 0 is zero122bbci.l a4, 1, .Laligned # if string pointer is now word-aligned123124.L2mod4: # address is 2 mod 4125addi a4, a4, 2 # advance ptr for aligned access126EX(10f) l32i a9, a4, 0 # get word with first two bytes of string127bnone a9, a7, .Lz2 # if byte 2 (of word, not string) is zero128bany a9, a8, .Laligned # if byte 3 (of word, not string) is nonzero129# byte 3 is zero130addi a4, a4, 3+1 # point just beyond zero byte131sub a2, a4, a2 # subtract to get length132abi_ret_default133134ENDPROC(__strnlen_user)135EXPORT_SYMBOL(__strnlen_user)136137.section .fixup, "ax"138.align 413910:140movi a2, 0141abi_ret_default142143144