blob: 2443ae476e333944291d4ded535ce3ba9fe5e5bf [file] [log] [blame]
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +02001/*
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +02002 * Optimized user space space access functions based on mvcos.
3 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2006
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +02005 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
6 * Gerald Schaefer (gerald.schaefer@de.ibm.com)
7 */
8
9#include <linux/errno.h>
10#include <linux/mm.h>
11#include <asm/uaccess.h>
12#include <asm/futex.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010013#include "uaccess.h"
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +020014
Heiko Carstensf4815ac2012-05-23 16:24:51 +020015#ifndef CONFIG_64BIT
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +020016#define AHI "ahi"
17#define ALR "alr"
18#define CLR "clr"
19#define LHI "lhi"
20#define SLR "slr"
21#else
22#define AHI "aghi"
23#define ALR "algr"
24#define CLR "clgr"
25#define LHI "lghi"
26#define SLR "slgr"
27#endif
28
Heiko Carstens2b67fc42007-02-05 21:16:47 +010029static size_t copy_from_user_mvcos(size_t size, const void __user *ptr, void *x)
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +020030{
31 register unsigned long reg0 asm("0") = 0x81UL;
32 unsigned long tmp1, tmp2;
33
34 tmp1 = -4096UL;
35 asm volatile(
36 "0: .insn ss,0xc80000000000,0(%0,%2),0(%1),0\n"
Gerald Schaeferaf9d2ff2009-10-06 10:34:14 +020037 "9: jz 7f\n"
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +020038 "1:"ALR" %0,%3\n"
39 " "SLR" %1,%3\n"
40 " "SLR" %2,%3\n"
41 " j 0b\n"
42 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
43 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
44 " "SLR" %4,%1\n"
45 " "CLR" %0,%4\n" /* copy crosses next page boundary? */
Martin Schwidefsky52149ba2006-09-28 16:56:03 +020046 " jnh 4f\n"
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +020047 "3: .insn ss,0xc80000000000,0(%4,%2),0(%1),0\n"
Gerald Schaeferaf9d2ff2009-10-06 10:34:14 +020048 "10:"SLR" %0,%4\n"
Martin Schwidefsky52149ba2006-09-28 16:56:03 +020049 " "ALR" %2,%4\n"
50 "4:"LHI" %4,-1\n"
51 " "ALR" %4,%0\n" /* copy remaining size, subtract 1 */
52 " bras %3,6f\n" /* memset loop */
53 " xc 0(1,%2),0(%2)\n"
54 "5: xc 0(256,%2),0(%2)\n"
55 " la %2,256(%2)\n"
56 "6:"AHI" %4,-256\n"
57 " jnm 5b\n"
58 " ex %4,0(%3)\n"
59 " j 8f\n"
60 "7:"SLR" %0,%0\n"
61 "8: \n"
Gerald Schaeferaf9d2ff2009-10-06 10:34:14 +020062 EX_TABLE(0b,2b) EX_TABLE(3b,4b) EX_TABLE(9b,2b) EX_TABLE(10b,4b)
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +020063 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
64 : "d" (reg0) : "cc", "memory");
65 return size;
66}
67
Heiko Carstens2b67fc42007-02-05 21:16:47 +010068static size_t copy_from_user_mvcos_check(size_t size, const void __user *ptr, void *x)
Gerald Schaefer59f35d52006-12-04 15:40:45 +010069{
70 if (size <= 256)
71 return copy_from_user_std(size, ptr, x);
72 return copy_from_user_mvcos(size, ptr, x);
73}
74
Heiko Carstens2b67fc42007-02-05 21:16:47 +010075static size_t copy_to_user_mvcos(size_t size, void __user *ptr, const void *x)
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +020076{
77 register unsigned long reg0 asm("0") = 0x810000UL;
78 unsigned long tmp1, tmp2;
79
80 tmp1 = -4096UL;
81 asm volatile(
82 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
Gerald Schaeferaf9d2ff2009-10-06 10:34:14 +020083 "6: jz 4f\n"
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +020084 "1:"ALR" %0,%3\n"
85 " "SLR" %1,%3\n"
86 " "SLR" %2,%3\n"
87 " j 0b\n"
88 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
89 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
90 " "SLR" %4,%1\n"
91 " "CLR" %0,%4\n" /* copy crosses next page boundary? */
92 " jnh 5f\n"
93 "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
Gerald Schaeferaf9d2ff2009-10-06 10:34:14 +020094 "7:"SLR" %0,%4\n"
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +020095 " j 5f\n"
96 "4:"SLR" %0,%0\n"
97 "5: \n"
Gerald Schaeferaf9d2ff2009-10-06 10:34:14 +020098 EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +020099 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
100 : "d" (reg0) : "cc", "memory");
101 return size;
102}
103
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100104static size_t copy_to_user_mvcos_check(size_t size, void __user *ptr,
105 const void *x)
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100106{
107 if (size <= 256)
108 return copy_to_user_std(size, ptr, x);
109 return copy_to_user_mvcos(size, ptr, x);
110}
111
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100112static size_t copy_in_user_mvcos(size_t size, void __user *to,
113 const void __user *from)
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +0200114{
115 register unsigned long reg0 asm("0") = 0x810081UL;
116 unsigned long tmp1, tmp2;
117
118 tmp1 = -4096UL;
119 /* FIXME: copy with reduced length. */
120 asm volatile(
121 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
122 " jz 2f\n"
123 "1:"ALR" %0,%3\n"
124 " "SLR" %1,%3\n"
125 " "SLR" %2,%3\n"
126 " j 0b\n"
127 "2:"SLR" %0,%0\n"
128 "3: \n"
129 EX_TABLE(0b,3b)
130 : "+a" (size), "+a" (to), "+a" (from), "+a" (tmp1), "=a" (tmp2)
131 : "d" (reg0) : "cc", "memory");
132 return size;
133}
134
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100135static size_t clear_user_mvcos(size_t size, void __user *to)
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +0200136{
137 register unsigned long reg0 asm("0") = 0x810000UL;
138 unsigned long tmp1, tmp2;
139
140 tmp1 = -4096UL;
141 asm volatile(
142 "0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
143 " jz 4f\n"
144 "1:"ALR" %0,%2\n"
145 " "SLR" %1,%2\n"
146 " j 0b\n"
147 "2: la %3,4095(%1)\n"/* %4 = to + 4095 */
148 " nr %3,%2\n" /* %4 = (to + 4095) & -4096 */
149 " "SLR" %3,%1\n"
150 " "CLR" %0,%3\n" /* copy crosses next page boundary? */
151 " jnh 5f\n"
152 "3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\n"
153 " "SLR" %0,%3\n"
154 " j 5f\n"
155 "4:"SLR" %0,%0\n"
156 "5: \n"
157 EX_TABLE(0b,2b) EX_TABLE(3b,5b)
158 : "+a" (size), "+a" (to), "+a" (tmp1), "=a" (tmp2)
159 : "a" (empty_zero_page), "d" (reg0) : "cc", "memory");
160 return size;
161}
162
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100163static size_t strnlen_user_mvcos(size_t count, const char __user *src)
164{
165 char buf[256];
166 int rc;
167 size_t done, len, len_str;
168
169 done = 0;
170 do {
171 len = min(count - done, (size_t) 256);
172 rc = uaccess.copy_from_user(len, src + done, buf);
173 if (unlikely(rc == len))
174 return 0;
175 len -= rc;
176 len_str = strnlen(buf, len);
177 done += len_str;
178 } while ((len_str == len) && (done < count));
179 return done + 1;
180}
181
182static size_t strncpy_from_user_mvcos(size_t count, const char __user *src,
183 char *dst)
184{
185 int rc;
186 size_t done, len, len_str;
187
188 done = 0;
189 do {
190 len = min(count - done, (size_t) 4096);
191 rc = uaccess.copy_from_user(len, src + done, dst);
192 if (unlikely(rc == len))
193 return -EFAULT;
194 len -= rc;
195 len_str = strnlen(dst, len);
196 done += len_str;
197 } while ((len_str == len) && (done < count));
198 return done;
199}
200
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +0200201struct uaccess_ops uaccess_mvcos = {
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100202 .copy_from_user = copy_from_user_mvcos_check,
203 .copy_from_user_small = copy_from_user_std,
204 .copy_to_user = copy_to_user_mvcos_check,
205 .copy_to_user_small = copy_to_user_std,
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +0200206 .copy_in_user = copy_in_user_mvcos,
207 .clear_user = clear_user_mvcos,
208 .strnlen_user = strnlen_user_std,
209 .strncpy_from_user = strncpy_from_user_std,
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100210 .futex_atomic_op = futex_atomic_op_std,
211 .futex_atomic_cmpxchg = futex_atomic_cmpxchg_std,
Gerald Schaefer6c2a9e62006-09-20 15:59:44 +0200212};
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100213
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100214struct uaccess_ops uaccess_mvcos_switch = {
215 .copy_from_user = copy_from_user_mvcos,
216 .copy_from_user_small = copy_from_user_mvcos,
217 .copy_to_user = copy_to_user_mvcos,
218 .copy_to_user_small = copy_to_user_mvcos,
219 .copy_in_user = copy_in_user_mvcos,
220 .clear_user = clear_user_mvcos,
221 .strnlen_user = strnlen_user_mvcos,
222 .strncpy_from_user = strncpy_from_user_mvcos,
223 .futex_atomic_op = futex_atomic_op_pt,
224 .futex_atomic_cmpxchg = futex_atomic_cmpxchg_pt,
225};