blob: 0e8cc2710c27fb3503eee296f9041cc38659a99c [file] [log] [blame]
Michal Simek0d6de952009-05-26 16:30:23 +02001/*
2 * Copyright (C) 2009 Michal Simek <monstr@monstr.eu>
3 * Copyright (C) 2009 PetaLogix
4 * Copyright (C) 2007 LynuxWorks, Inc.
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 */
10
11#include <linux/errno.h>
12#include <linux/linkage.h>
Michal Simekebe21122011-06-10 10:49:08 +020013#include <asm/page.h>
Michal Simek0d6de952009-05-26 16:30:23 +020014
15/*
16 * int __strncpy_user(char *to, char *from, int len);
17 *
18 * Returns:
19 * -EFAULT for an exception
20 * len if we hit the buffer limit
21 * bytes copied
22 */
23
24 .text
25.globl __strncpy_user;
Michal Simek13851962010-03-23 08:09:32 +010026.type __strncpy_user, @function
Michal Simek0d6de952009-05-26 16:30:23 +020027.align 4;
28__strncpy_user:
29
30 /*
31 * r5 - to
32 * r6 - from
33 * r7 - len
34 * r3 - temp count
35 * r4 - temp val
36 */
Michal Simekcb5edfe2011-10-02 13:07:02 +020037 beqid r7,3f
Michal Simek0d6de952009-05-26 16:30:23 +020038 addik r3,r7,0 /* temp_count = len */
Michal Simek0d6de952009-05-26 16:30:23 +0200391:
40 lbu r4,r6,r0
Michal Simek1de9e462013-01-16 18:53:22 +010041 beqid r4,2f
Michal Simek0d6de952009-05-26 16:30:23 +020042 sb r4,r5,r0
43
Michal Simek0d6de952009-05-26 16:30:23 +020044 addik r5,r5,1
Michal Simek0d6de952009-05-26 16:30:23 +020045 addik r6,r6,1 /* delay slot */
Michal Simek1de9e462013-01-16 18:53:22 +010046
47 addik r3,r3,-1
48 bnei r3,1b /* break on len */
Michal Simek0d6de952009-05-26 16:30:23 +0200492:
50 rsubk r3,r3,r7 /* temp_count = len - temp_count */
513:
52 rtsd r15,8
53 nop
Michal Simek13851962010-03-23 08:09:32 +010054 .size __strncpy_user, . - __strncpy_user
Michal Simek0d6de952009-05-26 16:30:23 +020055
Michal Simek0d6de952009-05-26 16:30:23 +020056 .section .fixup, "ax"
57 .align 2
584:
59 brid 3b
60 addik r3,r0, -EFAULT
61
62 .section __ex_table, "a"
63 .word 1b,4b
64
65/*
66 * int __strnlen_user(char __user *str, int maxlen);
67 *
68 * Returns:
69 * 0 on error
70 * maxlen + 1 if no NUL byte found within maxlen bytes
71 * size of the string (including NUL byte)
72 */
73
74 .text
75.globl __strnlen_user;
Michal Simek13851962010-03-23 08:09:32 +010076.type __strnlen_user, @function
Michal Simek0d6de952009-05-26 16:30:23 +020077.align 4;
78__strnlen_user:
Michal Simekcb5edfe2011-10-02 13:07:02 +020079 beqid r6,3f
Michal Simek0d6de952009-05-26 16:30:23 +020080 addik r3,r6,0
Michal Simek0d6de952009-05-26 16:30:23 +0200811:
82 lbu r4,r5,r0
83 beqid r4,2f /* break on NUL */
84 addik r3,r3,-1 /* delay slot */
85
86 bneid r3,1b
87 addik r5,r5,1 /* delay slot */
88
89 addik r3,r3,-1 /* for break on len */
902:
91 rsubk r3,r3,r6
923:
93 rtsd r15,8
94 nop
Michal Simek13851962010-03-23 08:09:32 +010095 .size __strnlen_user, . - __strnlen_user
Michal Simek0d6de952009-05-26 16:30:23 +020096
Michal Simek0d6de952009-05-26 16:30:23 +020097 .section .fixup,"ax"
984:
99 brid 3b
100 addk r3,r0,r0
101
102 .section __ex_table,"a"
103 .word 1b,4b
104
Michal Simekebe21122011-06-10 10:49:08 +0200105/* Loop unrolling for __copy_tofrom_user */
106#define COPY(offset) \
1071: lwi r4 , r6, 0x0000 + offset; \
1082: lwi r19, r6, 0x0004 + offset; \
1093: lwi r20, r6, 0x0008 + offset; \
1104: lwi r21, r6, 0x000C + offset; \
1115: lwi r22, r6, 0x0010 + offset; \
1126: lwi r23, r6, 0x0014 + offset; \
1137: lwi r24, r6, 0x0018 + offset; \
1148: lwi r25, r6, 0x001C + offset; \
1159: swi r4 , r5, 0x0000 + offset; \
11610: swi r19, r5, 0x0004 + offset; \
11711: swi r20, r5, 0x0008 + offset; \
11812: swi r21, r5, 0x000C + offset; \
11913: swi r22, r5, 0x0010 + offset; \
12014: swi r23, r5, 0x0014 + offset; \
12115: swi r24, r5, 0x0018 + offset; \
12216: swi r25, r5, 0x001C + offset; \
123 .section __ex_table,"a"; \
Michal Simekac64a9c2012-03-30 08:21:38 +0200124 .word 1b, 33f; \
125 .word 2b, 33f; \
126 .word 3b, 33f; \
127 .word 4b, 33f; \
128 .word 5b, 33f; \
129 .word 6b, 33f; \
130 .word 7b, 33f; \
131 .word 8b, 33f; \
132 .word 9b, 33f; \
133 .word 10b, 33f; \
134 .word 11b, 33f; \
135 .word 12b, 33f; \
136 .word 13b, 33f; \
137 .word 14b, 33f; \
138 .word 15b, 33f; \
139 .word 16b, 33f; \
Michal Simekebe21122011-06-10 10:49:08 +0200140 .text
141
142#define COPY_80(offset) \
143 COPY(0x00 + offset);\
144 COPY(0x20 + offset);\
145 COPY(0x40 + offset);\
146 COPY(0x60 + offset);
147
Michal Simek0d6de952009-05-26 16:30:23 +0200148/*
149 * int __copy_tofrom_user(char *to, char *from, int len)
150 * Return:
151 * 0 on success
152 * number of not copied bytes on error
153 */
154 .text
155.globl __copy_tofrom_user;
Michal Simek13851962010-03-23 08:09:32 +0100156.type __copy_tofrom_user, @function
Michal Simek0d6de952009-05-26 16:30:23 +0200157.align 4;
158__copy_tofrom_user:
159 /*
160 * r5 - to
161 * r6 - from
162 * r7, r3 - count
163 * r4 - tempval
164 */
Michal Simek9b133f82011-06-10 11:03:44 +0200165 beqid r7, 0f /* zero size is not likely */
Michal Simekca3865b2010-03-22 20:31:26 +0100166 or r3, r5, r6 /* find if is any to/from unaligned */
Michal Simek782d4912011-06-10 11:08:57 +0200167 or r3, r3, r7 /* find if count is unaligned */
168 andi r3, r3, 0x3 /* mask last 3 bits */
169 bneid r3, bu1 /* if r3 is not zero then byte copying */
Michal Simekca3865b2010-03-22 20:31:26 +0100170 or r3, r0, r0
171
Michal Simekebe21122011-06-10 10:49:08 +0200172 rsubi r3, r7, PAGE_SIZE /* detect PAGE_SIZE */
173 beqid r3, page;
174 or r3, r0, r0
175
Michal Simekc83858b2011-06-10 11:07:55 +0200176w1: lw r4, r6, r3 /* at least one 4 byte copy */
177w2: sw r4, r5, r3
Michal Simekca3865b2010-03-22 20:31:26 +0100178 addik r7, r7, -4
Michal Simekc83858b2011-06-10 11:07:55 +0200179 bneid r7, w1
Michal Simekca3865b2010-03-22 20:31:26 +0100180 addik r3, r3, 4
181 addik r3, r7, 0
182 rtsd r15, 8
183 nop
Michal Simekeedac792011-06-10 11:05:45 +0200184
185 .section __ex_table,"a"
Michal Simekc83858b2011-06-10 11:07:55 +0200186 .word w1, 0f;
187 .word w2, 0f;
Michal Simekeedac792011-06-10 11:05:45 +0200188 .text
189
Michal Simekebe21122011-06-10 10:49:08 +0200190.align 4 /* Alignment is important to keep icache happy */
191page: /* Create room on stack and save registers for storign values */
Michal Simekac64a9c2012-03-30 08:21:38 +0200192 addik r1, r1, -40
193 swi r5, r1, 0
194 swi r6, r1, 4
195 swi r7, r1, 8
196 swi r19, r1, 12
197 swi r20, r1, 16
198 swi r21, r1, 20
199 swi r22, r1, 24
200 swi r23, r1, 28
201 swi r24, r1, 32
202 swi r25, r1, 36
Michal Simekebe21122011-06-10 10:49:08 +0200203loop: /* r4, r19, r20, r21, r22, r23, r24, r25 are used for storing values */
204 /* Loop unrolling to get performance boost */
205 COPY_80(0x000);
206 COPY_80(0x080);
207 COPY_80(0x100);
208 COPY_80(0x180);
209 /* copy loop */
Michal Simekac64a9c2012-03-30 08:21:38 +0200210 addik r6, r6, 0x200
211 addik r7, r7, -0x200
212 bneid r7, loop
213 addik r5, r5, 0x200
214
Michal Simekebe21122011-06-10 10:49:08 +0200215 /* Restore register content */
Michal Simekac64a9c2012-03-30 08:21:38 +0200216 lwi r5, r1, 0
217 lwi r6, r1, 4
218 lwi r7, r1, 8
219 lwi r19, r1, 12
220 lwi r20, r1, 16
221 lwi r21, r1, 20
222 lwi r22, r1, 24
223 lwi r23, r1, 28
224 lwi r24, r1, 32
225 lwi r25, r1, 36
226 addik r1, r1, 40
Michal Simekebe21122011-06-10 10:49:08 +0200227 /* return back */
Michal Simekac64a9c2012-03-30 08:21:38 +0200228 addik r3, r0, 0
229 rtsd r15, 8
230 nop
231
232/* Fault case - return temp count */
23333:
Michal Simekebe21122011-06-10 10:49:08 +0200234 addik r3, r7, 0
Michal Simekac64a9c2012-03-30 08:21:38 +0200235 /* Restore register content */
236 lwi r5, r1, 0
237 lwi r6, r1, 4
238 lwi r7, r1, 8
239 lwi r19, r1, 12
240 lwi r20, r1, 16
241 lwi r21, r1, 20
242 lwi r22, r1, 24
243 lwi r23, r1, 28
244 lwi r24, r1, 32
245 lwi r25, r1, 36
246 addik r1, r1, 40
247 /* return back */
Michal Simekebe21122011-06-10 10:49:08 +0200248 rtsd r15, 8
249 nop
250
251.align 4 /* Alignment is important to keep icache happy */
Michal Simekc83858b2011-06-10 11:07:55 +0200252bu1: lbu r4,r6,r3
253bu2: sb r4,r5,r3
Michal Simekca3865b2010-03-22 20:31:26 +0100254 addik r7,r7,-1
Michal Simekc83858b2011-06-10 11:07:55 +0200255 bneid r7,bu1
Michal Simekca3865b2010-03-22 20:31:26 +0100256 addik r3,r3,1 /* delay slot */
Michal Simek9b133f82011-06-10 11:03:44 +02002570:
Michal Simekca3865b2010-03-22 20:31:26 +0100258 addik r3,r7,0
Michal Simek0d6de952009-05-26 16:30:23 +0200259 rtsd r15,8
260 nop
Michal Simek13851962010-03-23 08:09:32 +0100261 .size __copy_tofrom_user, . - __copy_tofrom_user
Michal Simek0d6de952009-05-26 16:30:23 +0200262
Michal Simek0d6de952009-05-26 16:30:23 +0200263 .section __ex_table,"a"
Michal Simekc83858b2011-06-10 11:07:55 +0200264 .word bu1, 0b;
265 .word bu2, 0b;
Michal Simekeedac792011-06-10 11:05:45 +0200266 .text