blob: 6fbd063382709f7ec3a9a0ca2fafbaa0cf72dd86 [file] [log] [blame]
Gerald Schaeferd02765d2006-09-20 15:59:42 +02001/*
Gerald Schaeferd02765d2006-09-20 15:59:42 +02002 * Standard user space access functions based on mvcp/mvcs and doing
3 * interesting things in the secondary space mode.
4 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02005 * Copyright IBM Corp. 2006
Gerald Schaeferd02765d2006-09-20 15:59:42 +02006 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
7 * Gerald Schaefer (gerald.schaefer@de.ibm.com)
8 */
9
10#include <linux/errno.h>
11#include <linux/mm.h>
Peter Zijlstraa8663742006-12-06 20:32:20 -080012#include <linux/uaccess.h>
Gerald Schaeferd02765d2006-09-20 15:59:42 +020013#include <asm/futex.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010014#include "uaccess.h"
Gerald Schaeferd02765d2006-09-20 15:59:42 +020015
Heiko Carstensf4815ac2012-05-23 16:24:51 +020016#ifndef CONFIG_64BIT
Gerald Schaeferd02765d2006-09-20 15:59:42 +020017#define AHI "ahi"
18#define ALR "alr"
19#define CLR "clr"
20#define LHI "lhi"
21#define SLR "slr"
22#else
23#define AHI "aghi"
24#define ALR "algr"
25#define CLR "clgr"
26#define LHI "lghi"
27#define SLR "slgr"
28#endif
29
30size_t copy_from_user_std(size_t size, const void __user *ptr, void *x)
31{
32 unsigned long tmp1, tmp2;
33
34 tmp1 = -256UL;
35 asm volatile(
36 "0: mvcp 0(%0,%2),0(%1),%3\n"
Gerald Schaeferaf9d2ff2009-10-06 10:34:14 +020037 "10:jz 8f\n"
Gerald Schaeferd02765d2006-09-20 15:59:42 +020038 "1:"ALR" %0,%3\n"
39 " la %1,256(%1)\n"
40 " la %2,256(%2)\n"
41 "2: mvcp 0(%0,%2),0(%1),%3\n"
Gerald Schaeferaf9d2ff2009-10-06 10:34:14 +020042 "11:jnz 1b\n"
Martin Schwidefsky52149ba2006-09-28 16:56:03 +020043 " j 8f\n"
Gerald Schaeferd02765d2006-09-20 15:59:42 +020044 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
45 " "LHI" %3,-4096\n"
46 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
47 " "SLR" %4,%1\n"
48 " "CLR" %0,%4\n" /* copy crosses next page boundary? */
Martin Schwidefsky52149ba2006-09-28 16:56:03 +020049 " jnh 5f\n"
Gerald Schaeferd02765d2006-09-20 15:59:42 +020050 "4: mvcp 0(%4,%2),0(%1),%3\n"
Gerald Schaeferaf9d2ff2009-10-06 10:34:14 +020051 "12:"SLR" %0,%4\n"
Martin Schwidefsky52149ba2006-09-28 16:56:03 +020052 " "ALR" %2,%4\n"
53 "5:"LHI" %4,-1\n"
54 " "ALR" %4,%0\n" /* copy remaining size, subtract 1 */
55 " bras %3,7f\n" /* memset loop */
56 " xc 0(1,%2),0(%2)\n"
57 "6: xc 0(256,%2),0(%2)\n"
58 " la %2,256(%2)\n"
59 "7:"AHI" %4,-256\n"
60 " jnm 6b\n"
61 " ex %4,0(%3)\n"
62 " j 9f\n"
63 "8:"SLR" %0,%0\n"
64 "9: \n"
65 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,5b)
Gerald Schaeferaf9d2ff2009-10-06 10:34:14 +020066 EX_TABLE(10b,3b) EX_TABLE(11b,3b) EX_TABLE(12b,5b)
Gerald Schaeferd02765d2006-09-20 15:59:42 +020067 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
68 : : "cc", "memory");
69 return size;
70}
71
Heiko Carstens2b67fc42007-02-05 21:16:47 +010072static size_t copy_from_user_std_check(size_t size, const void __user *ptr,
73 void *x)
Gerald Schaeferd02765d2006-09-20 15:59:42 +020074{
Gerald Schaefer59f35d52006-12-04 15:40:45 +010075 if (size <= 1024)
76 return copy_from_user_std(size, ptr, x);
77 return copy_from_user_pt(size, ptr, x);
Gerald Schaeferd02765d2006-09-20 15:59:42 +020078}
79
80size_t copy_to_user_std(size_t size, void __user *ptr, const void *x)
81{
82 unsigned long tmp1, tmp2;
83
84 tmp1 = -256UL;
85 asm volatile(
86 "0: mvcs 0(%0,%1),0(%2),%3\n"
Gerald Schaeferaf9d2ff2009-10-06 10:34:14 +020087 "7: jz 5f\n"
Gerald Schaeferd02765d2006-09-20 15:59:42 +020088 "1:"ALR" %0,%3\n"
89 " la %1,256(%1)\n"
90 " la %2,256(%2)\n"
91 "2: mvcs 0(%0,%1),0(%2),%3\n"
Gerald Schaeferaf9d2ff2009-10-06 10:34:14 +020092 "8: jnz 1b\n"
Gerald Schaeferd02765d2006-09-20 15:59:42 +020093 " j 5f\n"
94 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
95 " "LHI" %3,-4096\n"
96 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
97 " "SLR" %4,%1\n"
98 " "CLR" %0,%4\n" /* copy crosses next page boundary? */
99 " jnh 6f\n"
100 "4: mvcs 0(%4,%1),0(%2),%3\n"
Gerald Schaeferaf9d2ff2009-10-06 10:34:14 +0200101 "9:"SLR" %0,%4\n"
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200102 " j 6f\n"
103 "5:"SLR" %0,%0\n"
104 "6: \n"
105 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
Gerald Schaeferaf9d2ff2009-10-06 10:34:14 +0200106 EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b)
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200107 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
108 : : "cc", "memory");
109 return size;
110}
111
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100112static size_t copy_to_user_std_check(size_t size, void __user *ptr,
113 const void *x)
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200114{
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100115 if (size <= 1024)
116 return copy_to_user_std(size, ptr, x);
117 return copy_to_user_pt(size, ptr, x);
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200118}
119
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100120static size_t copy_in_user_std(size_t size, void __user *to,
121 const void __user *from)
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200122{
123 unsigned long tmp1;
124
125 asm volatile(
Martin Schwidefskye4d82692011-01-31 11:30:05 +0100126 " sacf 256\n"
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200127 " "AHI" %0,-1\n"
128 " jo 5f\n"
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200129 " bras %3,3f\n"
130 "0:"AHI" %0,257\n"
131 "1: mvc 0(1,%1),0(%2)\n"
132 " la %1,1(%1)\n"
133 " la %2,1(%2)\n"
134 " "AHI" %0,-1\n"
135 " jnz 1b\n"
136 " j 5f\n"
137 "2: mvc 0(256,%1),0(%2)\n"
138 " la %1,256(%1)\n"
139 " la %2,256(%2)\n"
140 "3:"AHI" %0,-256\n"
141 " jnm 2b\n"
142 "4: ex %0,1b-0b(%3)\n"
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200143 "5: "SLR" %0,%0\n"
Martin Schwidefskye4d82692011-01-31 11:30:05 +0100144 "6: sacf 0\n"
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200145 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
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100151static size_t clear_user_std(size_t size, void __user *to)
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200152{
153 unsigned long tmp1, tmp2;
154
155 asm volatile(
Martin Schwidefskye4d82692011-01-31 11:30:05 +0100156 " sacf 256\n"
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200157 " "AHI" %0,-1\n"
158 " jo 5f\n"
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200159 " 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"
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200178 "5: "SLR" %0,%0\n"
Martin Schwidefskye4d82692011-01-31 11:30:05 +0100179 "6: sacf 0\n"
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200180 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
181 : "+a" (size), "+a" (to), "=a" (tmp1), "=a" (tmp2)
182 : : "cc", "memory");
183 return size;
184}
185
186size_t strnlen_user_std(size_t size, const char __user *src)
187{
188 register unsigned long reg0 asm("0") = 0UL;
189 unsigned long tmp1, tmp2;
190
191 asm volatile(
192 " la %2,0(%1)\n"
193 " la %3,0(%0,%1)\n"
194 " "SLR" %0,%0\n"
195 " sacf 256\n"
196 "0: srst %3,%2\n"
197 " jo 0b\n"
198 " la %0,1(%3)\n" /* strnlen_user results includes \0 */
199 " "SLR" %0,%1\n"
200 "1: sacf 0\n"
201 EX_TABLE(0b,1b)
202 : "+a" (size), "+a" (src), "=a" (tmp1), "=a" (tmp2)
203 : "d" (reg0) : "cc", "memory");
204 return size;
205}
206
207size_t strncpy_from_user_std(size_t size, const char __user *src, char *dst)
208{
209 register unsigned long reg0 asm("0") = 0UL;
210 unsigned long tmp1, tmp2;
211
212 asm volatile(
213 " la %3,0(%1)\n"
214 " la %4,0(%0,%1)\n"
215 " sacf 256\n"
216 "0: srst %4,%3\n"
217 " jo 0b\n"
218 " sacf 0\n"
219 " la %0,0(%4)\n"
220 " jh 1f\n" /* found \0 in string ? */
221 " "AHI" %4,1\n" /* include \0 in copy */
222 "1:"SLR" %0,%1\n" /* %0 = return length (without \0) */
223 " "SLR" %4,%1\n" /* %4 = copy length (including \0) */
224 "2: mvcp 0(%4,%2),0(%1),%5\n"
225 " jz 9f\n"
226 "3:"AHI" %4,-256\n"
227 " la %1,256(%1)\n"
228 " la %2,256(%2)\n"
229 "4: mvcp 0(%4,%2),0(%1),%5\n"
230 " jnz 3b\n"
231 " j 9f\n"
232 "7: sacf 0\n"
233 "8:"LHI" %0,%6\n"
234 "9:\n"
235 EX_TABLE(0b,7b) EX_TABLE(2b,8b) EX_TABLE(4b,8b)
236 : "+a" (size), "+a" (src), "+d" (dst), "=a" (tmp1), "=a" (tmp2)
237 : "d" (reg0), "K" (-EFAULT) : "cc", "memory");
238 return size;
239}
240
241#define __futex_atomic_op(insn, ret, oldval, newval, uaddr, oparg) \
242 asm volatile( \
243 " sacf 256\n" \
244 "0: l %1,0(%6)\n" \
245 "1:"insn \
246 "2: cs %1,%2,0(%6)\n" \
247 "3: jl 1b\n" \
248 " lhi %0,0\n" \
249 "4: sacf 0\n" \
250 EX_TABLE(0b,4b) EX_TABLE(2b,4b) EX_TABLE(3b,4b) \
251 : "=d" (ret), "=&d" (oldval), "=&d" (newval), \
252 "=m" (*uaddr) \
253 : "0" (-EFAULT), "d" (oparg), "a" (uaddr), \
254 "m" (*uaddr) : "cc");
255
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800256int futex_atomic_op_std(int op, u32 __user *uaddr, int oparg, int *old)
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200257{
258 int oldval = 0, newval, ret;
259
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200260 switch (op) {
261 case FUTEX_OP_SET:
262 __futex_atomic_op("lr %2,%5\n",
263 ret, oldval, newval, uaddr, oparg);
264 break;
265 case FUTEX_OP_ADD:
266 __futex_atomic_op("lr %2,%1\nar %2,%5\n",
267 ret, oldval, newval, uaddr, oparg);
268 break;
269 case FUTEX_OP_OR:
270 __futex_atomic_op("lr %2,%1\nor %2,%5\n",
271 ret, oldval, newval, uaddr, oparg);
272 break;
273 case FUTEX_OP_ANDN:
274 __futex_atomic_op("lr %2,%1\nnr %2,%5\n",
275 ret, oldval, newval, uaddr, oparg);
276 break;
277 case FUTEX_OP_XOR:
278 __futex_atomic_op("lr %2,%1\nxr %2,%5\n",
279 ret, oldval, newval, uaddr, oparg);
280 break;
281 default:
282 ret = -ENOSYS;
283 }
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200284 *old = oldval;
285 return ret;
286}
287
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800288int futex_atomic_cmpxchg_std(u32 *uval, u32 __user *uaddr,
289 u32 oldval, u32 newval)
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200290{
291 int ret;
292
293 asm volatile(
294 " sacf 256\n"
Heiko Carstensd5b02b32008-02-19 15:29:34 +0100295 "0: cs %1,%4,0(%5)\n"
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800296 "1: la %0,0\n"
Heiko Carstensd5b02b32008-02-19 15:29:34 +0100297 "2: sacf 0\n"
298 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200299 : "=d" (ret), "+d" (oldval), "=m" (*uaddr)
300 : "0" (-EFAULT), "d" (newval), "a" (uaddr), "m" (*uaddr)
301 : "cc", "memory" );
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800302 *uval = oldval;
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200303 return ret;
304}
305
306struct uaccess_ops uaccess_std = {
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100307 .copy_from_user = copy_from_user_std_check,
308 .copy_from_user_small = copy_from_user_std,
309 .copy_to_user = copy_to_user_std_check,
310 .copy_to_user_small = copy_to_user_std,
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200311 .copy_in_user = copy_in_user_std,
312 .clear_user = clear_user_std,
313 .strnlen_user = strnlen_user_std,
314 .strncpy_from_user = strncpy_from_user_std,
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100315 .futex_atomic_op = futex_atomic_op_std,
316 .futex_atomic_cmpxchg = futex_atomic_cmpxchg_std,
Gerald Schaeferd02765d2006-09-20 15:59:42 +0200317};