/*1* arch/xtensa/lib/usercopy.S2*3* Copy to/from user space (derived from arch/xtensa/lib/hal/memcopy.S)4*5* DO NOT COMBINE this function with <arch/xtensa/lib/hal/memcopy.S>.6* It needs to remain separate and distinct. The hal files are part7* of the Xtensa link-time HAL, and those files may differ per8* processor configuration. Patching the kernel for another9* processor configuration includes replacing the hal files, and we10* could lose the special functionality for accessing user-space11* memory during such a patch. We sacrifice a little code space here12* in favor to simplify code maintenance.13*14* This file is subject to the terms and conditions of the GNU General15* Public License. See the file "COPYING" in the main directory of16* this archive for more details.17*18* Copyright (C) 2002 Tensilica Inc.19*/202122/*23* size_t __xtensa_copy_user (void *dst, const void *src, size_t len);24*25* The returned value is the number of bytes not copied. Implies zero26* is success.27*28* The general case algorithm is as follows:29* If the destination and source are both aligned,30* do 16B chunks with a loop, and then finish up with31* 8B, 4B, 2B, and 1B copies conditional on the length.32* If destination is aligned and source unaligned,33* do the same, but use SRC to align the source data.34* If destination is unaligned, align it by conditionally35* copying 1B and 2B and then retest.36* This code tries to use fall-through braches for the common37* case of aligned destinations (except for the branches to38* the alignment label).39*40* Register use:41* a0/ return address42* a1/ stack pointer43* a2/ return value44* a3/ src45* a4/ length46* a5/ dst47* a6/ tmp48* a7/ tmp49* a8/ tmp50* a9/ tmp51* a10/ tmp52* a11/ original length53*/5455#include <linux/linkage.h>56#include <asm/asmmacro.h>57#include <asm/core.h>5859.text60ENTRY(__xtensa_copy_user)6162#if !XCHAL_HAVE_LOOPS && defined(__XTENSA_CALL0_ABI__)63#define STACK_SIZE 464#else65#define STACK_SIZE 066#endif67abi_entry(STACK_SIZE)68# a2/ dst, a3/ src, a4/ len69mov a5, a2 # copy dst so that a2 is return value70mov a11, a4 # preserve original len for error case71.Lcommon:72bbsi.l a2, 0, .Ldst1mod2 # if dst is 1 mod 273bbsi.l a2, 1, .Ldst2mod4 # if dst is 2 mod 474.Ldstaligned: # return here from .Ldstunaligned when dst is aligned75srli a7, a4, 4 # number of loop iterations with 16B76# per iteration77movi a8, 3 # if source is also aligned,78bnone a3, a8, .Laligned # then use word copy79__ssa8 a3 # set shift amount from byte offset80bnez a4, .Lsrcunaligned81movi a2, 0 # return success for len==082abi_ret(STACK_SIZE)8384/*85* Destination is unaligned86*/8788.Ldst1mod2: # dst is only byte aligned89bltui a4, 7, .Lbytecopy # do short copies byte by byte9091# copy 1 byte92EX(10f) l8ui a6, a3, 093addi a3, a3, 194EX(10f) s8i a6, a5, 095addi a5, a5, 196addi a4, a4, -197bbci.l a5, 1, .Ldstaligned # if dst is now aligned, then98# return to main algorithm99.Ldst2mod4: # dst 16-bit aligned100# copy 2 bytes101bltui a4, 6, .Lbytecopy # do short copies byte by byte102EX(10f) l8ui a6, a3, 0103EX(10f) l8ui a7, a3, 1104addi a3, a3, 2105EX(10f) s8i a6, a5, 0106EX(10f) s8i a7, a5, 1107addi a5, a5, 2108addi a4, a4, -2109j .Ldstaligned # dst is now aligned, return to main algorithm110111/*112* Byte by byte copy113*/114.align 4115.byte 0 # 1 mod 4 alignment for LOOPNEZ116# (0 mod 4 alignment for LBEG)117.Lbytecopy:118#if XCHAL_HAVE_LOOPS119loopnez a4, .Lbytecopydone120#else /* !XCHAL_HAVE_LOOPS */121beqz a4, .Lbytecopydone122add a7, a3, a4 # a7 = end address for source123#endif /* !XCHAL_HAVE_LOOPS */124.Lnextbyte:125EX(10f) l8ui a6, a3, 0126addi a3, a3, 1127EX(10f) s8i a6, a5, 0128addi a5, a5, 1129#if !XCHAL_HAVE_LOOPS130blt a3, a7, .Lnextbyte131#endif /* !XCHAL_HAVE_LOOPS */132.Lbytecopydone:133movi a2, 0 # return success for len bytes copied134abi_ret(STACK_SIZE)135136/*137* Destination and source are word-aligned.138*/139# copy 16 bytes per iteration for word-aligned dst and word-aligned src140.align 4 # 1 mod 4 alignment for LOOPNEZ141.byte 0 # (0 mod 4 alignment for LBEG)142.Laligned:143#if XCHAL_HAVE_LOOPS144loopnez a7, .Loop1done145#else /* !XCHAL_HAVE_LOOPS */146beqz a7, .Loop1done147slli a8, a7, 4148add a8, a8, a3 # a8 = end of last 16B source chunk149#endif /* !XCHAL_HAVE_LOOPS */150.Loop1:151EX(10f) l32i a6, a3, 0152EX(10f) l32i a7, a3, 4153EX(10f) s32i a6, a5, 0154EX(10f) l32i a6, a3, 8155EX(10f) s32i a7, a5, 4156EX(10f) l32i a7, a3, 12157EX(10f) s32i a6, a5, 8158addi a3, a3, 16159EX(10f) s32i a7, a5, 12160addi a5, a5, 16161#if !XCHAL_HAVE_LOOPS162blt a3, a8, .Loop1163#endif /* !XCHAL_HAVE_LOOPS */164.Loop1done:165bbci.l a4, 3, .L2166# copy 8 bytes167EX(10f) l32i a6, a3, 0168EX(10f) l32i a7, a3, 4169addi a3, a3, 8170EX(10f) s32i a6, a5, 0171EX(10f) s32i a7, a5, 4172addi a5, a5, 8173.L2:174bbci.l a4, 2, .L3175# copy 4 bytes176EX(10f) l32i a6, a3, 0177addi a3, a3, 4178EX(10f) s32i a6, a5, 0179addi a5, a5, 4180.L3:181bbci.l a4, 1, .L4182# copy 2 bytes183EX(10f) l16ui a6, a3, 0184addi a3, a3, 2185EX(10f) s16i a6, a5, 0186addi a5, a5, 2187.L4:188bbci.l a4, 0, .L5189# copy 1 byte190EX(10f) l8ui a6, a3, 0191EX(10f) s8i a6, a5, 0192.L5:193movi a2, 0 # return success for len bytes copied194abi_ret(STACK_SIZE)195196/*197* Destination is aligned, Source is unaligned198*/199200.align 4201.byte 0 # 1 mod 4 alignement for LOOPNEZ202# (0 mod 4 alignment for LBEG)203.Lsrcunaligned:204# copy 16 bytes per iteration for word-aligned dst and unaligned src205and a10, a3, a8 # save unalignment offset for below206sub a3, a3, a10 # align a3 (to avoid sim warnings only; not needed for hardware)207EX(10f) l32i a6, a3, 0 # load first word208#if XCHAL_HAVE_LOOPS209loopnez a7, .Loop2done210#else /* !XCHAL_HAVE_LOOPS */211beqz a7, .Loop2done212#if defined(__XTENSA_CALL0_ABI__)213s32i a10, a1, 0214slli a10, a7, 4215add a10, a10, a3 # a10 = end of last 16B source chunk216#else217slli a12, a7, 4218add a12, a12, a3 # a12 = end of last 16B source chunk219#endif220#endif /* !XCHAL_HAVE_LOOPS */221.Loop2:222EX(10f) l32i a7, a3, 4223EX(10f) l32i a8, a3, 8224__src_b a6, a6, a7225EX(10f) s32i a6, a5, 0226EX(10f) l32i a9, a3, 12227__src_b a7, a7, a8228EX(10f) s32i a7, a5, 4229EX(10f) l32i a6, a3, 16230__src_b a8, a8, a9231EX(10f) s32i a8, a5, 8232addi a3, a3, 16233__src_b a9, a9, a6234EX(10f) s32i a9, a5, 12235addi a5, a5, 16236#if !XCHAL_HAVE_LOOPS237#if defined(__XTENSA_CALL0_ABI__)238blt a3, a10, .Loop2239l32i a10, a1, 0240#else241blt a3, a12, .Loop2242#endif243#endif /* !XCHAL_HAVE_LOOPS */244.Loop2done:245bbci.l a4, 3, .L12246# copy 8 bytes247EX(10f) l32i a7, a3, 4248EX(10f) l32i a8, a3, 8249__src_b a6, a6, a7250EX(10f) s32i a6, a5, 0251addi a3, a3, 8252__src_b a7, a7, a8253EX(10f) s32i a7, a5, 4254addi a5, a5, 8255mov a6, a8256.L12:257bbci.l a4, 2, .L13258# copy 4 bytes259EX(10f) l32i a7, a3, 4260addi a3, a3, 4261__src_b a6, a6, a7262EX(10f) s32i a6, a5, 0263addi a5, a5, 4264mov a6, a7265.L13:266add a3, a3, a10 # readjust a3 with correct misalignment267bbci.l a4, 1, .L14268# copy 2 bytes269EX(10f) l8ui a6, a3, 0270EX(10f) l8ui a7, a3, 1271addi a3, a3, 2272EX(10f) s8i a6, a5, 0273EX(10f) s8i a7, a5, 1274addi a5, a5, 2275.L14:276bbci.l a4, 0, .L15277# copy 1 byte278EX(10f) l8ui a6, a3, 0279EX(10f) s8i a6, a5, 0280.L15:281movi a2, 0 # return success for len bytes copied282abi_ret(STACK_SIZE)283284ENDPROC(__xtensa_copy_user)285EXPORT_SYMBOL(__xtensa_copy_user)286287.section .fixup, "ax"288.align 4289290/* a2 = original dst; a5 = current dst; a11= original len291* bytes_copied = a5 - a2292* retval = bytes_not_copied = original len - bytes_copied293* retval = a11 - (a5 - a2)294*/29529629710:298sub a2, a5, a2 /* a2 <-- bytes copied */299sub a2, a11, a2 /* a2 <-- bytes not copied */300abi_ret(STACK_SIZE)301302303