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