1/* $OpenBSD: copystr.S,v 1.6 2016/04/25 04:46:56 jsg Exp $ */ 2/* $NetBSD: copystr.S,v 1.8 2002/10/13 14:54:48 bjh21 Exp $ */ 3 4/* 5 * Copyright (c) 1995 Mark Brinicombe. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Mark Brinicombe. 19 * 4. The name of the company nor the name of the author may be used to 20 * endorse or promote products derived from this software without specific 21 * prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * copystr.S 36 * 37 * optimised and fault protected copystr functions 38 * 39 * Created : 16/05/95 40 */ 41 42#include "assym.h" 43#include <machine/asm.h> 44#include <arm/sysreg.h> 45#include <sys/errno.h> 46 47 .text 48 .align 0 49#ifdef MULTIPROCESSOR 50.Lcpu_info: 51 .word _C_LABEL(cpu_info) 52#else 53.Lcpu_info_primary: 54 .word _C_LABEL(cpu_info_primary) 55#endif 56 57/* 58 * r0 - from 59 * r1 - to 60 * r2 - maxlens 61 * r3 - lencopied 62 * 63 * Copy string from r0 to r1 64 */ 65ENTRY(copystr) 66 stmfd sp!, {r4-r5} /* stack is 8 byte aligned */ 67 teq r2, #0x00000000 68 mov r5, #0x00000000 69 moveq r0, #ENAMETOOLONG 70 beq 2f 71 721: ldrb r4, [r0], #0x0001 73 add r5, r5, #0x00000001 74 teq r4, #0x00000000 75 strb r4, [r1], #0x0001 76 teqne r5, r2 77 bne 1b 78 79 teq r4, #0x00000000 80 moveq r0, #0x00000000 81 movne r0, #ENAMETOOLONG 82 832: teq r3, #0x00000000 84 strne r5, [r3] 85 86 ldmfd sp!, {r4-r5} /* stack is 8 byte aligned */ 87 mov pc, lr 88 89#define SAVE_REGS stmfd sp!, {r4-r6} 90#define RESTORE_REGS ldmfd sp!, {r4-r6} 91 92/* 93 * r0 - user space address 94 * r1 - kernel space address 95 * r2 - maxlens 96 * r3 - lencopied 97 * 98 * Copy string from user space to kernel space 99 */ 100ENTRY(copyinstr) 101 SAVE_REGS 102 103 teq r2, #0x00000000 104 mov r6, #0x00000000 105 moveq r0, #ENAMETOOLONG 106 beq 2f 107 108#ifdef CPU_ARMv7 109 /* Get curcpu from TPIDRPRW. */ 110 mrc CP15_TPIDRPRW(r4) 111#else 112 ldr r4, .Lcpu_info_primary 113#endif 114 ldr r4, [r4, #CI_CURPCB] 115 116#ifdef DEBUG 117 teq r4, #0x00000000 118 beq .Lcopystrpcbfault 119#endif 120 121 adr r5, .Lcopystrfault 122 str r5, [r4, #PCB_ONFAULT] 123 1241: ldrbt r5, [r0], #0x0001 125 add r6, r6, #0x00000001 126 teq r5, #0x00000000 127 strb r5, [r1], #0x0001 128 teqne r6, r2 129 bne 1b 130 131 mov r0, #0x00000000 132 str r0, [r4, #PCB_ONFAULT] 133 134 teq r5, #0x00000000 135 moveq r0, #0x00000000 136 movne r0, #ENAMETOOLONG 137 1382: teq r3, #0x00000000 139 strne r6, [r3] 140 141 RESTORE_REGS 142 mov pc, lr 143 144/* 145 * r0 - kernel space address 146 * r1 - user space address 147 * r2 - maxlens 148 * r3 - lencopied 149 * 150 * Copy string from kernel space to user space 151 */ 152ENTRY(copyoutstr) 153 SAVE_REGS 154 155 teq r2, #0x00000000 156 mov r6, #0x00000000 157 moveq r0, #ENAMETOOLONG 158 beq 2f 159 160#ifdef CPU_ARMv7 161 /* Get curcpu from TPIDRPRW. */ 162 mrc CP15_TPIDRPRW(r4) 163#else 164 ldr r4, .Lcpu_info_primary 165#endif 166 ldr r4, [r4, #CI_CURPCB] 167 168#ifdef DEBUG 169 teq r4, #0x00000000 170 beq .Lcopystrpcbfault 171#endif 172 173 adr r5, .Lcopystrfault 174 str r5, [r4, #PCB_ONFAULT] 175 1761: ldrb r5, [r0], #0x0001 177 add r6, r6, #0x00000001 178 teq r5, #0x00000000 179 strbt r5, [r1], #0x0001 180 teqne r6, r2 181 bne 1b 182 183 mov r0, #0x00000000 184 str r0, [r4, #PCB_ONFAULT] 185 186 teq r5, #0x00000000 187 moveq r0, #0x00000000 188 movne r0, #ENAMETOOLONG 189 1902: teq r3, #0x00000000 191 strne r6, [r3] 192 193 RESTORE_REGS 194 mov pc, lr 195 196/* A fault occurred during the copy */ 197.Lcopystrfault: 198 mov r1, #0x00000000 199 str r1, [r4, #PCB_ONFAULT] 200 RESTORE_REGS 201 mov pc, lr 202 203#ifdef DEBUG 204.Lcopystrpcbfault: 205 mov r2, r1 206 mov r1, r0 207 adr r0, Lcopystrpcbfaulttext 208 bic sp, sp, #7 /* align stack to 8 bytes */ 209 b _C_LABEL(panic) 210 211Lcopystrpcbfaulttext: 212 .asciz "No valid PCB during copyinoutstr() addr1=%08x addr2=%08x\n" 213 .align 0 214#endif 215