Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / arch / s390 / lib / uaccess_std.c
1 /*
2  *  arch/s390/lib/uaccess_std.c
3  *
4  *  Standard user space access functions based on mvcp/mvcs and doing
5  *  interesting things in the secondary space mode.
6  *
7  *    Copyright (C) IBM Corp. 2006
8  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
9  *               Gerald Schaefer (gerald.schaefer@de.ibm.com)
10  */
11
12 #include <linux/errno.h>
13 #include <linux/mm.h>
14 #include <asm/uaccess.h>
15 #include <asm/futex.h>
16
17 #ifndef __s390x__
18 #define AHI     "ahi"
19 #define ALR     "alr"
20 #define CLR     "clr"
21 #define LHI     "lhi"
22 #define SLR     "slr"
23 #else
24 #define AHI     "aghi"
25 #define ALR     "algr"
26 #define CLR     "clgr"
27 #define LHI     "lghi"
28 #define SLR     "slgr"
29 #endif
30
31 size_t copy_from_user_std(size_t size, const void __user *ptr, void *x)
32 {
33         unsigned long tmp1, tmp2;
34
35         tmp1 = -256UL;
36         asm volatile(
37                 "0: mvcp  0(%0,%2),0(%1),%3\n"
38                 "   jz    8f\n"
39                 "1:"ALR"  %0,%3\n"
40                 "   la    %1,256(%1)\n"
41                 "   la    %2,256(%2)\n"
42                 "2: mvcp  0(%0,%2),0(%1),%3\n"
43                 "   jnz   1b\n"
44                 "   j     8f\n"
45                 "3: la    %4,255(%1)\n" /* %4 = ptr + 255 */
46                 "  "LHI"  %3,-4096\n"
47                 "   nr    %4,%3\n"      /* %4 = (ptr + 255) & -4096 */
48                 "  "SLR"  %4,%1\n"
49                 "  "CLR"  %0,%4\n"      /* copy crosses next page boundary? */
50                 "   jnh   5f\n"
51                 "4: mvcp  0(%4,%2),0(%1),%3\n"
52                 "  "SLR"  %0,%4\n"
53                 "  "ALR"  %2,%4\n"
54                 "5:"LHI"  %4,-1\n"
55                 "  "ALR"  %4,%0\n"      /* copy remaining size, subtract 1 */
56                 "   bras  %3,7f\n"      /* memset loop */
57                 "   xc    0(1,%2),0(%2)\n"
58                 "6: xc    0(256,%2),0(%2)\n"
59                 "   la    %2,256(%2)\n"
60                 "7:"AHI"  %4,-256\n"
61                 "   jnm   6b\n"
62                 "   ex    %4,0(%3)\n"
63                 "   j     9f\n"
64                 "8:"SLR"  %0,%0\n"
65                 "9: \n"
66                 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,5b)
67                 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
68                 : : "cc", "memory");
69         return size;
70 }
71
72 size_t copy_from_user_std_small(size_t size, const void __user *ptr, void *x)
73 {
74         unsigned long tmp1, tmp2;
75
76         tmp1 = 0UL;
77         asm volatile(
78                 "0: mvcp  0(%0,%2),0(%1),%3\n"
79                 "  "SLR"  %0,%0\n"
80                 "   j     5f\n"
81                 "1: la    %4,255(%1)\n" /* %4 = ptr + 255 */
82                 "  "LHI"  %3,-4096\n"
83                 "   nr    %4,%3\n"      /* %4 = (ptr + 255) & -4096 */
84                 "  "SLR"  %4,%1\n"
85                 "  "CLR"  %0,%4\n"      /* copy crosses next page boundary? */
86                 "   jnh   5f\n"
87                 "2: mvcp  0(%4,%2),0(%1),%3\n"
88                 "  "SLR"  %0,%4\n"
89                 "  "ALR"  %2,%4\n"
90                 "3:"LHI"  %4,-1\n"
91                 "  "ALR"  %4,%0\n"      /* copy remaining size, subtract 1 */
92                 "   bras  %3,4f\n"
93                 "   xc    0(1,%2),0(%2)\n"
94                 "4: ex    %4,0(%3)\n"
95                 "5:\n"
96                 EX_TABLE(0b,1b) EX_TABLE(2b,3b)
97                 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
98                 : : "cc", "memory");
99         return size;
100 }
101
102 size_t copy_to_user_std(size_t size, void __user *ptr, const void *x)
103 {
104         unsigned long tmp1, tmp2;
105
106         tmp1 = -256UL;
107         asm volatile(
108                 "0: mvcs  0(%0,%1),0(%2),%3\n"
109                 "   jz    5f\n"
110                 "1:"ALR"  %0,%3\n"
111                 "   la    %1,256(%1)\n"
112                 "   la    %2,256(%2)\n"
113                 "2: mvcs  0(%0,%1),0(%2),%3\n"
114                 "   jnz   1b\n"
115                 "   j     5f\n"
116                 "3: la    %4,255(%1)\n" /* %4 = ptr + 255 */
117                 "  "LHI"  %3,-4096\n"
118                 "   nr    %4,%3\n"      /* %4 = (ptr + 255) & -4096 */
119                 "  "SLR"  %4,%1\n"
120                 "  "CLR"  %0,%4\n"      /* copy crosses next page boundary? */
121                 "   jnh   6f\n"
122                 "4: mvcs  0(%4,%1),0(%2),%3\n"
123                 "  "SLR"  %0,%4\n"
124                 "   j     6f\n"
125                 "5:"SLR"  %0,%0\n"
126                 "6: \n"
127                 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
128                 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
129                 : : "cc", "memory");
130         return size;
131 }
132
133 size_t copy_to_user_std_small(size_t size, void __user *ptr, const void *x)
134 {
135         unsigned long tmp1, tmp2;
136
137         tmp1 = 0UL;
138         asm volatile(
139                 "0: mvcs  0(%0,%1),0(%2),%3\n"
140                 "  "SLR"  %0,%0\n"
141                 "   j     3f\n"
142                 "1: la    %4,255(%1)\n" /* ptr + 255 */
143                 "  "LHI"  %3,-4096\n"
144                 "   nr    %4,%3\n"      /* (ptr + 255) & -4096UL */
145                 "  "SLR"  %4,%1\n"
146                 "  "CLR"  %0,%4\n"      /* copy crosses next page boundary? */
147                 "   jnh   3f\n"
148                 "2: mvcs  0(%4,%1),0(%2),%3\n"
149                 "  "SLR"  %0,%4\n"
150                 "3:\n"
151                 EX_TABLE(0b,1b) EX_TABLE(2b,3b)
152                 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
153                 : : "cc", "memory");
154         return size;
155 }
156
157 size_t copy_in_user_std(size_t size, void __user *to, const void __user *from)
158 {
159         unsigned long tmp1;
160
161         asm volatile(
162                 "  "AHI"  %0,-1\n"
163                 "   jo    5f\n"
164                 "   sacf  256\n"
165                 "   bras  %3,3f\n"
166                 "0:"AHI"  %0,257\n"
167                 "1: mvc   0(1,%1),0(%2)\n"
168                 "   la    %1,1(%1)\n"
169                 "   la    %2,1(%2)\n"
170                 "  "AHI"  %0,-1\n"
171                 "   jnz   1b\n"
172                 "   j     5f\n"
173                 "2: mvc   0(256,%1),0(%2)\n"
174                 "   la    %1,256(%1)\n"
175                 "   la    %2,256(%2)\n"
176                 "3:"AHI"  %0,-256\n"
177                 "   jnm   2b\n"
178                 "4: ex    %0,1b-0b(%3)\n"
179                 "   sacf  0\n"
180                 "5: "SLR"  %0,%0\n"
181                 "6:\n"
182                 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
183                 : "+a" (size), "+a" (to), "+a" (from), "=a" (tmp1)
184                 : : "cc", "memory");
185         return size;
186 }
187
188 size_t clear_user_std(size_t size, void __user *to)
189 {
190         unsigned long tmp1, tmp2;
191
192         asm volatile(
193                 "  "AHI"  %0,-1\n"
194                 "   jo    5f\n"
195                 "   sacf  256\n"
196                 "   bras  %3,3f\n"
197                 "   xc    0(1,%1),0(%1)\n"
198                 "0:"AHI"  %0,257\n"
199                 "   la    %2,255(%1)\n" /* %2 = ptr + 255 */
200                 "   srl   %2,12\n"
201                 "   sll   %2,12\n"      /* %2 = (ptr + 255) & -4096 */
202                 "  "SLR"  %2,%1\n"
203                 "  "CLR"  %0,%2\n"      /* clear crosses next page boundary? */
204                 "   jnh   5f\n"
205                 "  "AHI"  %2,-1\n"
206                 "1: ex    %2,0(%3)\n"
207                 "  "AHI"  %2,1\n"
208                 "  "SLR"  %0,%2\n"
209                 "   j     5f\n"
210                 "2: xc    0(256,%1),0(%1)\n"
211                 "   la    %1,256(%1)\n"
212                 "3:"AHI"  %0,-256\n"
213                 "   jnm   2b\n"
214                 "4: ex    %0,0(%3)\n"
215                 "   sacf  0\n"
216                 "5: "SLR"  %0,%0\n"
217                 "6:\n"
218                 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
219                 : "+a" (size), "+a" (to), "=a" (tmp1), "=a" (tmp2)
220                 : : "cc", "memory");
221         return size;
222 }
223
224 size_t strnlen_user_std(size_t size, const char __user *src)
225 {
226         register unsigned long reg0 asm("0") = 0UL;
227         unsigned long tmp1, tmp2;
228
229         asm volatile(
230                 "   la    %2,0(%1)\n"
231                 "   la    %3,0(%0,%1)\n"
232                 "  "SLR"  %0,%0\n"
233                 "   sacf  256\n"
234                 "0: srst  %3,%2\n"
235                 "   jo    0b\n"
236                 "   la    %0,1(%3)\n"   /* strnlen_user results includes \0 */
237                 "  "SLR"  %0,%1\n"
238                 "1: sacf  0\n"
239                 EX_TABLE(0b,1b)
240                 : "+a" (size), "+a" (src), "=a" (tmp1), "=a" (tmp2)
241                 : "d" (reg0) : "cc", "memory");
242         return size;
243 }
244
245 size_t strncpy_from_user_std(size_t size, const char __user *src, char *dst)
246 {
247         register unsigned long reg0 asm("0") = 0UL;
248         unsigned long tmp1, tmp2;
249
250         asm volatile(
251                 "   la    %3,0(%1)\n"
252                 "   la    %4,0(%0,%1)\n"
253                 "   sacf  256\n"
254                 "0: srst  %4,%3\n"
255                 "   jo    0b\n"
256                 "   sacf  0\n"
257                 "   la    %0,0(%4)\n"
258                 "   jh    1f\n"         /* found \0 in string ? */
259                 "  "AHI"  %4,1\n"       /* include \0 in copy */
260                 "1:"SLR"  %0,%1\n"      /* %0 = return length (without \0) */
261                 "  "SLR"  %4,%1\n"      /* %4 = copy length (including \0) */
262                 "2: mvcp  0(%4,%2),0(%1),%5\n"
263                 "   jz    9f\n"
264                 "3:"AHI"  %4,-256\n"
265                 "   la    %1,256(%1)\n"
266                 "   la    %2,256(%2)\n"
267                 "4: mvcp  0(%4,%2),0(%1),%5\n"
268                 "   jnz   3b\n"
269                 "   j     9f\n"
270                 "7: sacf  0\n"
271                 "8:"LHI"  %0,%6\n"
272                 "9:\n"
273                 EX_TABLE(0b,7b) EX_TABLE(2b,8b) EX_TABLE(4b,8b)
274                 : "+a" (size), "+a" (src), "+d" (dst), "=a" (tmp1), "=a" (tmp2)
275                 : "d" (reg0), "K" (-EFAULT) : "cc", "memory");
276         return size;
277 }
278
279 #define __futex_atomic_op(insn, ret, oldval, newval, uaddr, oparg)      \
280         asm volatile(                                                   \
281                 "   sacf  256\n"                                        \
282                 "0: l     %1,0(%6)\n"                                   \
283                 "1:"insn                                                \
284                 "2: cs    %1,%2,0(%6)\n"                                \
285                 "3: jl    1b\n"                                         \
286                 "   lhi   %0,0\n"                                       \
287                 "4: sacf  0\n"                                          \
288                 EX_TABLE(0b,4b) EX_TABLE(2b,4b) EX_TABLE(3b,4b)         \
289                 : "=d" (ret), "=&d" (oldval), "=&d" (newval),           \
290                   "=m" (*uaddr)                                         \
291                 : "0" (-EFAULT), "d" (oparg), "a" (uaddr),              \
292                   "m" (*uaddr) : "cc");
293
294 int futex_atomic_op(int op, int __user *uaddr, int oparg, int *old)
295 {
296         int oldval = 0, newval, ret;
297
298         inc_preempt_count();
299
300         switch (op) {
301         case FUTEX_OP_SET:
302                 __futex_atomic_op("lr %2,%5\n",
303                                   ret, oldval, newval, uaddr, oparg);
304                 break;
305         case FUTEX_OP_ADD:
306                 __futex_atomic_op("lr %2,%1\nar %2,%5\n",
307                                   ret, oldval, newval, uaddr, oparg);
308                 break;
309         case FUTEX_OP_OR:
310                 __futex_atomic_op("lr %2,%1\nor %2,%5\n",
311                                   ret, oldval, newval, uaddr, oparg);
312                 break;
313         case FUTEX_OP_ANDN:
314                 __futex_atomic_op("lr %2,%1\nnr %2,%5\n",
315                                   ret, oldval, newval, uaddr, oparg);
316                 break;
317         case FUTEX_OP_XOR:
318                 __futex_atomic_op("lr %2,%1\nxr %2,%5\n",
319                                   ret, oldval, newval, uaddr, oparg);
320                 break;
321         default:
322                 ret = -ENOSYS;
323         }
324         dec_preempt_count();
325         *old = oldval;
326         return ret;
327 }
328
329 int futex_atomic_cmpxchg(int __user *uaddr, int oldval, int newval)
330 {
331         int ret;
332
333         asm volatile(
334                 "   sacf 256\n"
335                 "   cs   %1,%4,0(%5)\n"
336                 "0: lr   %0,%1\n"
337                 "1: sacf 0\n"
338                 EX_TABLE(0b,1b)
339                 : "=d" (ret), "+d" (oldval), "=m" (*uaddr)
340                 : "0" (-EFAULT), "d" (newval), "a" (uaddr), "m" (*uaddr)
341                 : "cc", "memory" );
342         return ret;
343 }
344
345 struct uaccess_ops uaccess_std = {
346         .copy_from_user = copy_from_user_std,
347         .copy_from_user_small = copy_from_user_std_small,
348         .copy_to_user = copy_to_user_std,
349         .copy_to_user_small = copy_to_user_std_small,
350         .copy_in_user = copy_in_user_std,
351         .clear_user = clear_user_std,
352         .strnlen_user = strnlen_user_std,
353         .strncpy_from_user = strncpy_from_user_std,
354         .futex_atomic_op = futex_atomic_op,
355         .futex_atomic_cmpxchg = futex_atomic_cmpxchg,
356 };