Merge branch 'intx' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik/misc-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 <linux/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 extern size_t copy_from_user_pt(size_t n, const void __user *from, void *to);
32 extern size_t copy_to_user_pt(size_t n, void __user *to, const void *from);
33
34 size_t copy_from_user_std(size_t size, const void __user *ptr, void *x)
35 {
36         unsigned long tmp1, tmp2;
37
38         tmp1 = -256UL;
39         asm volatile(
40                 "0: mvcp  0(%0,%2),0(%1),%3\n"
41                 "   jz    8f\n"
42                 "1:"ALR"  %0,%3\n"
43                 "   la    %1,256(%1)\n"
44                 "   la    %2,256(%2)\n"
45                 "2: mvcp  0(%0,%2),0(%1),%3\n"
46                 "   jnz   1b\n"
47                 "   j     8f\n"
48                 "3: la    %4,255(%1)\n" /* %4 = ptr + 255 */
49                 "  "LHI"  %3,-4096\n"
50                 "   nr    %4,%3\n"      /* %4 = (ptr + 255) & -4096 */
51                 "  "SLR"  %4,%1\n"
52                 "  "CLR"  %0,%4\n"      /* copy crosses next page boundary? */
53                 "   jnh   5f\n"
54                 "4: mvcp  0(%4,%2),0(%1),%3\n"
55                 "  "SLR"  %0,%4\n"
56                 "  "ALR"  %2,%4\n"
57                 "5:"LHI"  %4,-1\n"
58                 "  "ALR"  %4,%0\n"      /* copy remaining size, subtract 1 */
59                 "   bras  %3,7f\n"      /* memset loop */
60                 "   xc    0(1,%2),0(%2)\n"
61                 "6: xc    0(256,%2),0(%2)\n"
62                 "   la    %2,256(%2)\n"
63                 "7:"AHI"  %4,-256\n"
64                 "   jnm   6b\n"
65                 "   ex    %4,0(%3)\n"
66                 "   j     9f\n"
67                 "8:"SLR"  %0,%0\n"
68                 "9: \n"
69                 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,5b)
70                 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
71                 : : "cc", "memory");
72         return size;
73 }
74
75 size_t copy_from_user_std_check(size_t size, const void __user *ptr, void *x)
76 {
77         if (size <= 1024)
78                 return copy_from_user_std(size, ptr, x);
79         return copy_from_user_pt(size, ptr, x);
80 }
81
82 size_t copy_to_user_std(size_t size, void __user *ptr, const void *x)
83 {
84         unsigned long tmp1, tmp2;
85
86         tmp1 = -256UL;
87         asm volatile(
88                 "0: mvcs  0(%0,%1),0(%2),%3\n"
89                 "   jz    5f\n"
90                 "1:"ALR"  %0,%3\n"
91                 "   la    %1,256(%1)\n"
92                 "   la    %2,256(%2)\n"
93                 "2: mvcs  0(%0,%1),0(%2),%3\n"
94                 "   jnz   1b\n"
95                 "   j     5f\n"
96                 "3: la    %4,255(%1)\n" /* %4 = ptr + 255 */
97                 "  "LHI"  %3,-4096\n"
98                 "   nr    %4,%3\n"      /* %4 = (ptr + 255) & -4096 */
99                 "  "SLR"  %4,%1\n"
100                 "  "CLR"  %0,%4\n"      /* copy crosses next page boundary? */
101                 "   jnh   6f\n"
102                 "4: mvcs  0(%4,%1),0(%2),%3\n"
103                 "  "SLR"  %0,%4\n"
104                 "   j     6f\n"
105                 "5:"SLR"  %0,%0\n"
106                 "6: \n"
107                 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
108                 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
109                 : : "cc", "memory");
110         return size;
111 }
112
113 size_t copy_to_user_std_check(size_t size, void __user *ptr, const void *x)
114 {
115         if (size <= 1024)
116                 return copy_to_user_std(size, ptr, x);
117         return copy_to_user_pt(size, ptr, x);
118 }
119
120 size_t copy_in_user_std(size_t size, void __user *to, const void __user *from)
121 {
122         unsigned long tmp1;
123
124         asm volatile(
125                 "  "AHI"  %0,-1\n"
126                 "   jo    5f\n"
127                 "   sacf  256\n"
128                 "   bras  %3,3f\n"
129                 "0:"AHI"  %0,257\n"
130                 "1: mvc   0(1,%1),0(%2)\n"
131                 "   la    %1,1(%1)\n"
132                 "   la    %2,1(%2)\n"
133                 "  "AHI"  %0,-1\n"
134                 "   jnz   1b\n"
135                 "   j     5f\n"
136                 "2: mvc   0(256,%1),0(%2)\n"
137                 "   la    %1,256(%1)\n"
138                 "   la    %2,256(%2)\n"
139                 "3:"AHI"  %0,-256\n"
140                 "   jnm   2b\n"
141                 "4: ex    %0,1b-0b(%3)\n"
142                 "   sacf  0\n"
143                 "5: "SLR"  %0,%0\n"
144                 "6:\n"
145                 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
146                 : "+a" (size), "+a" (to), "+a" (from), "=a" (tmp1)
147                 : : "cc", "memory");
148         return size;
149 }
150
151 size_t clear_user_std(size_t size, void __user *to)
152 {
153         unsigned long tmp1, tmp2;
154
155         asm volatile(
156                 "  "AHI"  %0,-1\n"
157                 "   jo    5f\n"
158                 "   sacf  256\n"
159                 "   bras  %3,3f\n"
160                 "   xc    0(1,%1),0(%1)\n"
161                 "0:"AHI"  %0,257\n"
162                 "   la    %2,255(%1)\n" /* %2 = ptr + 255 */
163                 "   srl   %2,12\n"
164                 "   sll   %2,12\n"      /* %2 = (ptr + 255) & -4096 */
165                 "  "SLR"  %2,%1\n"
166                 "  "CLR"  %0,%2\n"      /* clear crosses next page boundary? */
167                 "   jnh   5f\n"
168                 "  "AHI"  %2,-1\n"
169                 "1: ex    %2,0(%3)\n"
170                 "  "AHI"  %2,1\n"
171                 "  "SLR"  %0,%2\n"
172                 "   j     5f\n"
173                 "2: xc    0(256,%1),0(%1)\n"
174                 "   la    %1,256(%1)\n"
175                 "3:"AHI"  %0,-256\n"
176                 "   jnm   2b\n"
177                 "4: ex    %0,0(%3)\n"
178                 "   sacf  0\n"
179                 "5: "SLR"  %0,%0\n"
180                 "6:\n"
181                 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
182                 : "+a" (size), "+a" (to), "=a" (tmp1), "=a" (tmp2)
183                 : : "cc", "memory");
184         return size;
185 }
186
187 size_t strnlen_user_std(size_t size, const char __user *src)
188 {
189         register unsigned long reg0 asm("0") = 0UL;
190         unsigned long tmp1, tmp2;
191
192         asm volatile(
193                 "   la    %2,0(%1)\n"
194                 "   la    %3,0(%0,%1)\n"
195                 "  "SLR"  %0,%0\n"
196                 "   sacf  256\n"
197                 "0: srst  %3,%2\n"
198                 "   jo    0b\n"
199                 "   la    %0,1(%3)\n"   /* strnlen_user results includes \0 */
200                 "  "SLR"  %0,%1\n"
201                 "1: sacf  0\n"
202                 EX_TABLE(0b,1b)
203                 : "+a" (size), "+a" (src), "=a" (tmp1), "=a" (tmp2)
204                 : "d" (reg0) : "cc", "memory");
205         return size;
206 }
207
208 size_t strncpy_from_user_std(size_t size, const char __user *src, char *dst)
209 {
210         register unsigned long reg0 asm("0") = 0UL;
211         unsigned long tmp1, tmp2;
212
213         asm volatile(
214                 "   la    %3,0(%1)\n"
215                 "   la    %4,0(%0,%1)\n"
216                 "   sacf  256\n"
217                 "0: srst  %4,%3\n"
218                 "   jo    0b\n"
219                 "   sacf  0\n"
220                 "   la    %0,0(%4)\n"
221                 "   jh    1f\n"         /* found \0 in string ? */
222                 "  "AHI"  %4,1\n"       /* include \0 in copy */
223                 "1:"SLR"  %0,%1\n"      /* %0 = return length (without \0) */
224                 "  "SLR"  %4,%1\n"      /* %4 = copy length (including \0) */
225                 "2: mvcp  0(%4,%2),0(%1),%5\n"
226                 "   jz    9f\n"
227                 "3:"AHI"  %4,-256\n"
228                 "   la    %1,256(%1)\n"
229                 "   la    %2,256(%2)\n"
230                 "4: mvcp  0(%4,%2),0(%1),%5\n"
231                 "   jnz   3b\n"
232                 "   j     9f\n"
233                 "7: sacf  0\n"
234                 "8:"LHI"  %0,%6\n"
235                 "9:\n"
236                 EX_TABLE(0b,7b) EX_TABLE(2b,8b) EX_TABLE(4b,8b)
237                 : "+a" (size), "+a" (src), "+d" (dst), "=a" (tmp1), "=a" (tmp2)
238                 : "d" (reg0), "K" (-EFAULT) : "cc", "memory");
239         return size;
240 }
241
242 #define __futex_atomic_op(insn, ret, oldval, newval, uaddr, oparg)      \
243         asm volatile(                                                   \
244                 "   sacf  256\n"                                        \
245                 "0: l     %1,0(%6)\n"                                   \
246                 "1:"insn                                                \
247                 "2: cs    %1,%2,0(%6)\n"                                \
248                 "3: jl    1b\n"                                         \
249                 "   lhi   %0,0\n"                                       \
250                 "4: sacf  0\n"                                          \
251                 EX_TABLE(0b,4b) EX_TABLE(2b,4b) EX_TABLE(3b,4b)         \
252                 : "=d" (ret), "=&d" (oldval), "=&d" (newval),           \
253                   "=m" (*uaddr)                                         \
254                 : "0" (-EFAULT), "d" (oparg), "a" (uaddr),              \
255                   "m" (*uaddr) : "cc");
256
257 int futex_atomic_op(int op, int __user *uaddr, int oparg, int *old)
258 {
259         int oldval = 0, newval, ret;
260
261         pagefault_disable();
262
263         switch (op) {
264         case FUTEX_OP_SET:
265                 __futex_atomic_op("lr %2,%5\n",
266                                   ret, oldval, newval, uaddr, oparg);
267                 break;
268         case FUTEX_OP_ADD:
269                 __futex_atomic_op("lr %2,%1\nar %2,%5\n",
270                                   ret, oldval, newval, uaddr, oparg);
271                 break;
272         case FUTEX_OP_OR:
273                 __futex_atomic_op("lr %2,%1\nor %2,%5\n",
274                                   ret, oldval, newval, uaddr, oparg);
275                 break;
276         case FUTEX_OP_ANDN:
277                 __futex_atomic_op("lr %2,%1\nnr %2,%5\n",
278                                   ret, oldval, newval, uaddr, oparg);
279                 break;
280         case FUTEX_OP_XOR:
281                 __futex_atomic_op("lr %2,%1\nxr %2,%5\n",
282                                   ret, oldval, newval, uaddr, oparg);
283                 break;
284         default:
285                 ret = -ENOSYS;
286         }
287         pagefault_enable();
288         *old = oldval;
289         return ret;
290 }
291
292 int futex_atomic_cmpxchg(int __user *uaddr, int oldval, int newval)
293 {
294         int ret;
295
296         asm volatile(
297                 "   sacf 256\n"
298                 "   cs   %1,%4,0(%5)\n"
299                 "0: lr   %0,%1\n"
300                 "1: sacf 0\n"
301                 EX_TABLE(0b,1b)
302                 : "=d" (ret), "+d" (oldval), "=m" (*uaddr)
303                 : "0" (-EFAULT), "d" (newval), "a" (uaddr), "m" (*uaddr)
304                 : "cc", "memory" );
305         return ret;
306 }
307
308 struct uaccess_ops uaccess_std = {
309         .copy_from_user = copy_from_user_std_check,
310         .copy_from_user_small = copy_from_user_std,
311         .copy_to_user = copy_to_user_std_check,
312         .copy_to_user_small = copy_to_user_std,
313         .copy_in_user = copy_in_user_std,
314         .clear_user = clear_user_std,
315         .strnlen_user = strnlen_user_std,
316         .strncpy_from_user = strncpy_from_user_std,
317         .futex_atomic_op = futex_atomic_op,
318         .futex_atomic_cmpxchg = futex_atomic_cmpxchg,
319 };