/*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 <variant/core.h>5657#ifdef __XTENSA_EB__58#define ALIGN(R, W0, W1) src R, W0, W159#define SSA8(R) ssa8b R60#else61#define ALIGN(R, W0, W1) src R, W1, W062#define SSA8(R) ssa8l R63#endif6465/* Load or store instructions that may cause exceptions use the EX macro. */6667#define EX(insn,reg1,reg2,offset,handler) \689: insn reg1, reg2, offset; \69.section __ex_table, "a"; \70.word 9b, handler; \71.previous727374.text75.align 476.global __xtensa_copy_user77.type __xtensa_copy_user,@function78__xtensa_copy_user:79entry sp, 16 # minimal stack frame80# a2/ dst, a3/ src, a4/ len81mov a5, a2 # copy dst so that a2 is return value82mov a11, a4 # preserve original len for error case83.Lcommon:84bbsi.l a2, 0, .Ldst1mod2 # if dst is 1 mod 285bbsi.l a2, 1, .Ldst2mod4 # if dst is 2 mod 486.Ldstaligned: # return here from .Ldstunaligned when dst is aligned87srli a7, a4, 4 # number of loop iterations with 16B88# per iteration89movi a8, 3 # if source is also aligned,90bnone a3, a8, .Laligned # then use word copy91SSA8( a3) # set shift amount from byte offset92bnez a4, .Lsrcunaligned93movi a2, 0 # return success for len==094retw9596/*97* Destination is unaligned98*/99100.Ldst1mod2: # dst is only byte aligned101bltui a4, 7, .Lbytecopy # do short copies byte by byte102103# copy 1 byte104EX(l8ui, a6, a3, 0, l_fixup)105addi a3, a3, 1106EX(s8i, a6, a5, 0, s_fixup)107addi a5, a5, 1108addi a4, a4, -1109bbci.l a5, 1, .Ldstaligned # if dst is now aligned, then110# return to main algorithm111.Ldst2mod4: # dst 16-bit aligned112# copy 2 bytes113bltui a4, 6, .Lbytecopy # do short copies byte by byte114EX(l8ui, a6, a3, 0, l_fixup)115EX(l8ui, a7, a3, 1, l_fixup)116addi a3, a3, 2117EX(s8i, a6, a5, 0, s_fixup)118EX(s8i, a7, a5, 1, s_fixup)119addi a5, a5, 2120addi a4, a4, -2121j .Ldstaligned # dst is now aligned, return to main algorithm122123/*124* Byte by byte copy125*/126.align 4127.byte 0 # 1 mod 4 alignment for LOOPNEZ128# (0 mod 4 alignment for LBEG)129.Lbytecopy:130#if XCHAL_HAVE_LOOPS131loopnez a4, .Lbytecopydone132#else /* !XCHAL_HAVE_LOOPS */133beqz a4, .Lbytecopydone134add a7, a3, a4 # a7 = end address for source135#endif /* !XCHAL_HAVE_LOOPS */136.Lnextbyte:137EX(l8ui, a6, a3, 0, l_fixup)138addi a3, a3, 1139EX(s8i, a6, a5, 0, s_fixup)140addi a5, a5, 1141#if !XCHAL_HAVE_LOOPS142blt a3, a7, .Lnextbyte143#endif /* !XCHAL_HAVE_LOOPS */144.Lbytecopydone:145movi a2, 0 # return success for len bytes copied146retw147148/*149* Destination and source are word-aligned.150*/151# copy 16 bytes per iteration for word-aligned dst and word-aligned src152.align 4 # 1 mod 4 alignment for LOOPNEZ153.byte 0 # (0 mod 4 alignment for LBEG)154.Laligned:155#if XCHAL_HAVE_LOOPS156loopnez a7, .Loop1done157#else /* !XCHAL_HAVE_LOOPS */158beqz a7, .Loop1done159slli a8, a7, 4160add a8, a8, a3 # a8 = end of last 16B source chunk161#endif /* !XCHAL_HAVE_LOOPS */162.Loop1:163EX(l32i, a6, a3, 0, l_fixup)164EX(l32i, a7, a3, 4, l_fixup)165EX(s32i, a6, a5, 0, s_fixup)166EX(l32i, a6, a3, 8, l_fixup)167EX(s32i, a7, a5, 4, s_fixup)168EX(l32i, a7, a3, 12, l_fixup)169EX(s32i, a6, a5, 8, s_fixup)170addi a3, a3, 16171EX(s32i, a7, a5, 12, s_fixup)172addi a5, a5, 16173#if !XCHAL_HAVE_LOOPS174blt a3, a8, .Loop1175#endif /* !XCHAL_HAVE_LOOPS */176.Loop1done:177bbci.l a4, 3, .L2178# copy 8 bytes179EX(l32i, a6, a3, 0, l_fixup)180EX(l32i, a7, a3, 4, l_fixup)181addi a3, a3, 8182EX(s32i, a6, a5, 0, s_fixup)183EX(s32i, a7, a5, 4, s_fixup)184addi a5, a5, 8185.L2:186bbci.l a4, 2, .L3187# copy 4 bytes188EX(l32i, a6, a3, 0, l_fixup)189addi a3, a3, 4190EX(s32i, a6, a5, 0, s_fixup)191addi a5, a5, 4192.L3:193bbci.l a4, 1, .L4194# copy 2 bytes195EX(l16ui, a6, a3, 0, l_fixup)196addi a3, a3, 2197EX(s16i, a6, a5, 0, s_fixup)198addi a5, a5, 2199.L4:200bbci.l a4, 0, .L5201# copy 1 byte202EX(l8ui, a6, a3, 0, l_fixup)203EX(s8i, a6, a5, 0, s_fixup)204.L5:205movi a2, 0 # return success for len bytes copied206retw207208/*209* Destination is aligned, Source is unaligned210*/211212.align 4213.byte 0 # 1 mod 4 alignement for LOOPNEZ214# (0 mod 4 alignment for LBEG)215.Lsrcunaligned:216# copy 16 bytes per iteration for word-aligned dst and unaligned src217and a10, a3, a8 # save unalignment offset for below218sub a3, a3, a10 # align a3 (to avoid sim warnings only; not needed for hardware)219EX(l32i, a6, a3, 0, l_fixup) # load first word220#if XCHAL_HAVE_LOOPS221loopnez a7, .Loop2done222#else /* !XCHAL_HAVE_LOOPS */223beqz a7, .Loop2done224slli a10, a7, 4225add a10, a10, a3 # a10 = end of last 16B source chunk226#endif /* !XCHAL_HAVE_LOOPS */227.Loop2:228EX(l32i, a7, a3, 4, l_fixup)229EX(l32i, a8, a3, 8, l_fixup)230ALIGN( a6, a6, a7)231EX(s32i, a6, a5, 0, s_fixup)232EX(l32i, a9, a3, 12, l_fixup)233ALIGN( a7, a7, a8)234EX(s32i, a7, a5, 4, s_fixup)235EX(l32i, a6, a3, 16, l_fixup)236ALIGN( a8, a8, a9)237EX(s32i, a8, a5, 8, s_fixup)238addi a3, a3, 16239ALIGN( a9, a9, a6)240EX(s32i, a9, a5, 12, s_fixup)241addi a5, a5, 16242#if !XCHAL_HAVE_LOOPS243blt a3, a10, .Loop2244#endif /* !XCHAL_HAVE_LOOPS */245.Loop2done:246bbci.l a4, 3, .L12247# copy 8 bytes248EX(l32i, a7, a3, 4, l_fixup)249EX(l32i, a8, a3, 8, l_fixup)250ALIGN( a6, a6, a7)251EX(s32i, a6, a5, 0, s_fixup)252addi a3, a3, 8253ALIGN( a7, a7, a8)254EX(s32i, a7, a5, 4, s_fixup)255addi a5, a5, 8256mov a6, a8257.L12:258bbci.l a4, 2, .L13259# copy 4 bytes260EX(l32i, a7, a3, 4, l_fixup)261addi a3, a3, 4262ALIGN( a6, a6, a7)263EX(s32i, a6, a5, 0, s_fixup)264addi a5, a5, 4265mov a6, a7266.L13:267add a3, a3, a10 # readjust a3 with correct misalignment268bbci.l a4, 1, .L14269# copy 2 bytes270EX(l8ui, a6, a3, 0, l_fixup)271EX(l8ui, a7, a3, 1, l_fixup)272addi a3, a3, 2273EX(s8i, a6, a5, 0, s_fixup)274EX(s8i, a7, a5, 1, s_fixup)275addi a5, a5, 2276.L14:277bbci.l a4, 0, .L15278# copy 1 byte279EX(l8ui, a6, a3, 0, l_fixup)280EX(s8i, a6, a5, 0, s_fixup)281.L15:282movi a2, 0 # return success for len bytes copied283retw284285286.section .fixup, "ax"287.align 4288289/* a2 = original dst; a5 = current dst; a11= original len290* bytes_copied = a5 - a2291* retval = bytes_not_copied = original len - bytes_copied292* retval = a11 - (a5 - a2)293*294* Clearing the remaining pieces of kernel memory plugs security295* holes. This functionality is the equivalent of the *_zeroing296* functions that some architectures provide.297*/298299.Lmemset:300.word memset301302s_fixup:303sub a2, a5, a2 /* a2 <-- bytes copied */304sub a2, a11, a2 /* a2 <-- bytes not copied */305retw306307l_fixup:308sub a2, a5, a2 /* a2 <-- bytes copied */309sub a2, a11, a2 /* a2 <-- bytes not copied == return value */310311/* void *memset(void *s, int c, size_t n); */312mov a6, a5 /* s */313movi a7, 0 /* c */314mov a8, a2 /* n */315l32r a4, .Lmemset316callx4 a4317/* Ignore memset return value in a6. */318/* a2 still contains bytes not copied. */319retw320321322323