blob: 6e3f9772aaba7f27592708dd82fb238f93602e9e [file] [log] [blame]
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001/* align.c - handle alignment exceptions for the Power PC.
2 *
3 * Copyright (c) 1996 Paul Mackerras <paulus@cs.anu.edu.au>
4 * Copyright (c) 1998-1999 TiVo, Inc.
5 * PowerPC 403GCX modifications.
6 * Copyright (c) 1999 Grant Erickson <grant@lcse.umn.edu>
7 * PowerPC 403GCX/405GP modifications.
8 * Copyright (c) 2001-2002 PPC64 team, IBM Corp
9 * 64-bit and Power4 support
10 * Copyright (c) 2005 Benjamin Herrenschmidt, IBM Corp
11 * <benh@kernel.crashing.org>
12 * Merge ppc32 and ppc64 implementations
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 */
19
20#include <linux/kernel.h>
21#include <linux/mm.h>
22#include <asm/processor.h>
23#include <asm/uaccess.h>
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +110024#include <asm/cache.h>
25#include <asm/cputable.h>
Geert Uytterhoeven80947e72009-05-18 02:10:05 +000026#include <asm/emulated_ops.h>
David Howellsae3a1972012-03-28 18:30:02 +010027#include <asm/switch_to.h>
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +110028
29struct aligninfo {
30 unsigned char len;
31 unsigned char flags;
32};
33
34#define IS_XFORM(inst) (((inst) >> 26) == 31)
35#define IS_DSFORM(inst) (((inst) >> 26) >= 56)
36
37#define INVALID { 0, 0 }
38
Paul Mackerrasfab5db92006-06-07 16:14:40 +100039/* Bits in the flags field */
40#define LD 0 /* load */
41#define ST 1 /* store */
Paul Mackerrasc6d42672007-08-10 14:07:38 +100042#define SE 2 /* sign-extend value, or FP ld/st as word */
Paul Mackerrasfab5db92006-06-07 16:14:40 +100043#define F 4 /* to/from fp regs */
44#define U 8 /* update index register */
45#define M 0x10 /* multiple load/store */
46#define SW 0x20 /* byte swap */
47#define S 0x40 /* single-precision fp or... */
48#define SX 0x40 /* ... byte count in XER */
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +110049#define HARD 0x80 /* string, stwcx. */
Kumar Gala26caeb22007-08-24 16:42:53 -050050#define E4 0x40 /* SPE endianness is word */
51#define E8 0x80 /* SPE endianness is double word */
Michael Neulingcd6f37b2008-07-11 16:31:09 +100052#define SPLT 0x80 /* VSX SPLAT load */
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +110053
Paul Mackerrasfab5db92006-06-07 16:14:40 +100054/* DSISR bits reported for a DCBZ instruction: */
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +110055#define DCBZ 0x5f /* 8xx/82xx dcbz faults when cache not enabled */
56
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +110057/*
58 * The PowerPC stores certain bits of the instruction that caused the
59 * alignment exception in the DSISR register. This array maps those
60 * bits to information about the operand length and what the
61 * instruction would do.
62 */
63static struct aligninfo aligninfo[128] = {
64 { 4, LD }, /* 00 0 0000: lwz / lwarx */
65 INVALID, /* 00 0 0001 */
66 { 4, ST }, /* 00 0 0010: stw */
67 INVALID, /* 00 0 0011 */
68 { 2, LD }, /* 00 0 0100: lhz */
69 { 2, LD+SE }, /* 00 0 0101: lha */
70 { 2, ST }, /* 00 0 0110: sth */
71 { 4, LD+M }, /* 00 0 0111: lmw */
72 { 4, LD+F+S }, /* 00 0 1000: lfs */
73 { 8, LD+F }, /* 00 0 1001: lfd */
74 { 4, ST+F+S }, /* 00 0 1010: stfs */
75 { 8, ST+F }, /* 00 0 1011: stfd */
76 INVALID, /* 00 0 1100 */
77 { 8, LD }, /* 00 0 1101: ld/ldu/lwa */
78 INVALID, /* 00 0 1110 */
79 { 8, ST }, /* 00 0 1111: std/stdu */
80 { 4, LD+U }, /* 00 1 0000: lwzu */
81 INVALID, /* 00 1 0001 */
82 { 4, ST+U }, /* 00 1 0010: stwu */
83 INVALID, /* 00 1 0011 */
84 { 2, LD+U }, /* 00 1 0100: lhzu */
85 { 2, LD+SE+U }, /* 00 1 0101: lhau */
86 { 2, ST+U }, /* 00 1 0110: sthu */
87 { 4, ST+M }, /* 00 1 0111: stmw */
88 { 4, LD+F+S+U }, /* 00 1 1000: lfsu */
89 { 8, LD+F+U }, /* 00 1 1001: lfdu */
90 { 4, ST+F+S+U }, /* 00 1 1010: stfsu */
91 { 8, ST+F+U }, /* 00 1 1011: stfdu */
Paul Mackerrasc6d42672007-08-10 14:07:38 +100092 { 16, LD+F }, /* 00 1 1100: lfdp */
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +110093 INVALID, /* 00 1 1101 */
Paul Mackerrasc6d42672007-08-10 14:07:38 +100094 { 16, ST+F }, /* 00 1 1110: stfdp */
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +110095 INVALID, /* 00 1 1111 */
96 { 8, LD }, /* 01 0 0000: ldx */
97 INVALID, /* 01 0 0001 */
98 { 8, ST }, /* 01 0 0010: stdx */
99 INVALID, /* 01 0 0011 */
100 INVALID, /* 01 0 0100 */
101 { 4, LD+SE }, /* 01 0 0101: lwax */
102 INVALID, /* 01 0 0110 */
103 INVALID, /* 01 0 0111 */
104 { 4, LD+M+HARD+SX }, /* 01 0 1000: lswx */
105 { 4, LD+M+HARD }, /* 01 0 1001: lswi */
106 { 4, ST+M+HARD+SX }, /* 01 0 1010: stswx */
107 { 4, ST+M+HARD }, /* 01 0 1011: stswi */
108 INVALID, /* 01 0 1100 */
109 { 8, LD+U }, /* 01 0 1101: ldu */
110 INVALID, /* 01 0 1110 */
111 { 8, ST+U }, /* 01 0 1111: stdu */
112 { 8, LD+U }, /* 01 1 0000: ldux */
113 INVALID, /* 01 1 0001 */
114 { 8, ST+U }, /* 01 1 0010: stdux */
115 INVALID, /* 01 1 0011 */
116 INVALID, /* 01 1 0100 */
117 { 4, LD+SE+U }, /* 01 1 0101: lwaux */
118 INVALID, /* 01 1 0110 */
119 INVALID, /* 01 1 0111 */
120 INVALID, /* 01 1 1000 */
121 INVALID, /* 01 1 1001 */
122 INVALID, /* 01 1 1010 */
123 INVALID, /* 01 1 1011 */
124 INVALID, /* 01 1 1100 */
125 INVALID, /* 01 1 1101 */
126 INVALID, /* 01 1 1110 */
127 INVALID, /* 01 1 1111 */
128 INVALID, /* 10 0 0000 */
129 INVALID, /* 10 0 0001 */
130 INVALID, /* 10 0 0010: stwcx. */
131 INVALID, /* 10 0 0011 */
132 INVALID, /* 10 0 0100 */
133 INVALID, /* 10 0 0101 */
134 INVALID, /* 10 0 0110 */
135 INVALID, /* 10 0 0111 */
136 { 4, LD+SW }, /* 10 0 1000: lwbrx */
137 INVALID, /* 10 0 1001 */
138 { 4, ST+SW }, /* 10 0 1010: stwbrx */
139 INVALID, /* 10 0 1011 */
140 { 2, LD+SW }, /* 10 0 1100: lhbrx */
141 { 4, LD+SE }, /* 10 0 1101 lwa */
142 { 2, ST+SW }, /* 10 0 1110: sthbrx */
143 INVALID, /* 10 0 1111 */
144 INVALID, /* 10 1 0000 */
145 INVALID, /* 10 1 0001 */
146 INVALID, /* 10 1 0010 */
147 INVALID, /* 10 1 0011 */
148 INVALID, /* 10 1 0100 */
149 INVALID, /* 10 1 0101 */
150 INVALID, /* 10 1 0110 */
151 INVALID, /* 10 1 0111 */
152 INVALID, /* 10 1 1000 */
153 INVALID, /* 10 1 1001 */
154 INVALID, /* 10 1 1010 */
155 INVALID, /* 10 1 1011 */
156 INVALID, /* 10 1 1100 */
157 INVALID, /* 10 1 1101 */
158 INVALID, /* 10 1 1110 */
159 { 0, ST+HARD }, /* 10 1 1111: dcbz */
160 { 4, LD }, /* 11 0 0000: lwzx */
161 INVALID, /* 11 0 0001 */
162 { 4, ST }, /* 11 0 0010: stwx */
163 INVALID, /* 11 0 0011 */
164 { 2, LD }, /* 11 0 0100: lhzx */
165 { 2, LD+SE }, /* 11 0 0101: lhax */
166 { 2, ST }, /* 11 0 0110: sthx */
167 INVALID, /* 11 0 0111 */
168 { 4, LD+F+S }, /* 11 0 1000: lfsx */
169 { 8, LD+F }, /* 11 0 1001: lfdx */
170 { 4, ST+F+S }, /* 11 0 1010: stfsx */
171 { 8, ST+F }, /* 11 0 1011: stfdx */
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000172 { 16, LD+F }, /* 11 0 1100: lfdpx */
173 { 4, LD+F+SE }, /* 11 0 1101: lfiwax */
174 { 16, ST+F }, /* 11 0 1110: stfdpx */
175 { 4, ST+F }, /* 11 0 1111: stfiwx */
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100176 { 4, LD+U }, /* 11 1 0000: lwzux */
177 INVALID, /* 11 1 0001 */
178 { 4, ST+U }, /* 11 1 0010: stwux */
179 INVALID, /* 11 1 0011 */
180 { 2, LD+U }, /* 11 1 0100: lhzux */
181 { 2, LD+SE+U }, /* 11 1 0101: lhaux */
182 { 2, ST+U }, /* 11 1 0110: sthux */
183 INVALID, /* 11 1 0111 */
184 { 4, LD+F+S+U }, /* 11 1 1000: lfsux */
185 { 8, LD+F+U }, /* 11 1 1001: lfdux */
186 { 4, ST+F+S+U }, /* 11 1 1010: stfsux */
187 { 8, ST+F+U }, /* 11 1 1011: stfdux */
188 INVALID, /* 11 1 1100 */
Michael Neuling545bba12009-02-19 18:51:37 +0000189 { 4, LD+F }, /* 11 1 1101: lfiwzx */
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100190 INVALID, /* 11 1 1110 */
191 INVALID, /* 11 1 1111 */
192};
193
194/*
195 * Create a DSISR value from the instruction
196 */
197static inline unsigned make_dsisr(unsigned instr)
198{
199 unsigned dsisr;
200
201
202 /* bits 6:15 --> 22:31 */
203 dsisr = (instr & 0x03ff0000) >> 16;
204
205 if (IS_XFORM(instr)) {
206 /* bits 29:30 --> 15:16 */
207 dsisr |= (instr & 0x00000006) << 14;
208 /* bit 25 --> 17 */
209 dsisr |= (instr & 0x00000040) << 8;
210 /* bits 21:24 --> 18:21 */
211 dsisr |= (instr & 0x00000780) << 3;
212 } else {
213 /* bit 5 --> 17 */
214 dsisr |= (instr & 0x04000000) >> 12;
215 /* bits 1: 4 --> 18:21 */
216 dsisr |= (instr & 0x78000000) >> 17;
217 /* bits 30:31 --> 12:13 */
218 if (IS_DSFORM(instr))
219 dsisr |= (instr & 0x00000003) << 18;
220 }
221
222 return dsisr;
223}
224
225/*
226 * The dcbz (data cache block zero) instruction
227 * gives an alignment fault if used on non-cacheable
228 * memory. We handle the fault mainly for the
229 * case when we are running with the cache disabled
230 * for debugging.
231 */
232static int emulate_dcbz(struct pt_regs *regs, unsigned char __user *addr)
233{
234 long __user *p;
235 int i, size;
236
237#ifdef __powerpc64__
238 size = ppc64_caches.dline_size;
239#else
240 size = L1_CACHE_BYTES;
241#endif
242 p = (long __user *) (regs->dar & -size);
243 if (user_mode(regs) && !access_ok(VERIFY_WRITE, p, size))
244 return -EFAULT;
245 for (i = 0; i < size / sizeof(long); ++i)
Benjamin Herrenschmidte4ee38912007-04-11 16:13:19 +1000246 if (__put_user_inatomic(0, p+i))
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100247 return -EFAULT;
248 return 1;
249}
250
251/*
252 * Emulate load & store multiple instructions
253 * On 64-bit machines, these instructions only affect/use the
254 * bottom 4 bytes of each register, and the loads clear the
255 * top 4 bytes of the affected register.
256 */
257#ifdef CONFIG_PPC64
258#define REG_BYTE(rp, i) *((u8 *)((rp) + ((i) >> 2)) + ((i) & 3) + 4)
259#else
260#define REG_BYTE(rp, i) *((u8 *)(rp) + (i))
261#endif
262
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000263#define SWIZ_PTR(p) ((unsigned char __user *)((p) ^ swiz))
264
Anton Blanchard835e2062013-09-23 12:04:49 +1000265#ifdef __BIG_ENDIAN__
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100266static int emulate_multiple(struct pt_regs *regs, unsigned char __user *addr,
267 unsigned int reg, unsigned int nb,
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000268 unsigned int flags, unsigned int instr,
269 unsigned long swiz)
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100270{
271 unsigned long *rptr;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000272 unsigned int nb0, i, bswiz;
273 unsigned long p;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100274
275 /*
276 * We do not try to emulate 8 bytes multiple as they aren't really
277 * available in our operating environments and we don't try to
278 * emulate multiples operations in kernel land as they should never
279 * be used/generated there at least not on unaligned boundaries
280 */
281 if (unlikely((nb > 4) || !user_mode(regs)))
282 return 0;
283
284 /* lmw, stmw, lswi/x, stswi/x */
285 nb0 = 0;
286 if (flags & HARD) {
287 if (flags & SX) {
288 nb = regs->xer & 127;
289 if (nb == 0)
290 return 1;
291 } else {
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000292 unsigned long pc = regs->nip ^ (swiz & 4);
293
Benjamin Herrenschmidte4ee38912007-04-11 16:13:19 +1000294 if (__get_user_inatomic(instr,
295 (unsigned int __user *)pc))
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100296 return -EFAULT;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000297 if (swiz == 0 && (flags & SW))
298 instr = cpu_to_le32(instr);
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100299 nb = (instr >> 11) & 0x1f;
300 if (nb == 0)
301 nb = 32;
302 }
303 if (nb + reg * 4 > 128) {
304 nb0 = nb + reg * 4 - 128;
305 nb = 128 - reg * 4;
306 }
307 } else {
308 /* lwm, stmw */
309 nb = (32 - reg) * 4;
310 }
311
312 if (!access_ok((flags & ST ? VERIFY_WRITE: VERIFY_READ), addr, nb+nb0))
313 return -EFAULT; /* bad address */
314
315 rptr = &regs->gpr[reg];
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000316 p = (unsigned long) addr;
317 bswiz = (flags & SW)? 3: 0;
318
319 if (!(flags & ST)) {
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100320 /*
321 * This zeroes the top 4 bytes of the affected registers
322 * in 64-bit mode, and also zeroes out any remaining
323 * bytes of the last register for lsw*.
324 */
325 memset(rptr, 0, ((nb + 3) / 4) * sizeof(unsigned long));
326 if (nb0 > 0)
327 memset(&regs->gpr[0], 0,
328 ((nb0 + 3) / 4) * sizeof(unsigned long));
329
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000330 for (i = 0; i < nb; ++i, ++p)
Benjamin Herrenschmidte4ee38912007-04-11 16:13:19 +1000331 if (__get_user_inatomic(REG_BYTE(rptr, i ^ bswiz),
332 SWIZ_PTR(p)))
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100333 return -EFAULT;
334 if (nb0 > 0) {
335 rptr = &regs->gpr[0];
336 addr += nb;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000337 for (i = 0; i < nb0; ++i, ++p)
Benjamin Herrenschmidte4ee38912007-04-11 16:13:19 +1000338 if (__get_user_inatomic(REG_BYTE(rptr,
339 i ^ bswiz),
340 SWIZ_PTR(p)))
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100341 return -EFAULT;
342 }
343
344 } else {
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000345 for (i = 0; i < nb; ++i, ++p)
Benjamin Herrenschmidte4ee38912007-04-11 16:13:19 +1000346 if (__put_user_inatomic(REG_BYTE(rptr, i ^ bswiz),
347 SWIZ_PTR(p)))
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100348 return -EFAULT;
349 if (nb0 > 0) {
350 rptr = &regs->gpr[0];
351 addr += nb;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000352 for (i = 0; i < nb0; ++i, ++p)
Benjamin Herrenschmidte4ee38912007-04-11 16:13:19 +1000353 if (__put_user_inatomic(REG_BYTE(rptr,
354 i ^ bswiz),
355 SWIZ_PTR(p)))
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100356 return -EFAULT;
357 }
358 }
359 return 1;
360}
361
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000362/*
363 * Emulate floating-point pair loads and stores.
364 * Only POWER6 has these instructions, and it does true little-endian,
365 * so we don't need the address swizzling.
366 */
Michael Neulingb887ec62008-07-08 18:53:03 +1000367static int emulate_fp_pair(unsigned char __user *addr, unsigned int reg,
368 unsigned int flags)
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000369{
Michael Neuling553631e2009-02-19 18:52:20 +0000370 char *ptr0 = (char *) &current->thread.TS_FPR(reg);
371 char *ptr1 = (char *) &current->thread.TS_FPR(reg+1);
372 int i, ret, sw = 0;
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000373
374 if (!(flags & F))
375 return 0;
376 if (reg & 1)
377 return 0; /* invalid form: FRS/FRT must be even */
Michael Neuling553631e2009-02-19 18:52:20 +0000378 if (flags & SW)
379 sw = 7;
380 ret = 0;
381 for (i = 0; i < 8; ++i) {
382 if (!(flags & ST)) {
383 ret |= __get_user(ptr0[i^sw], addr + i);
384 ret |= __get_user(ptr1[i^sw], addr + i + 8);
385 } else {
386 ret |= __put_user(ptr0[i^sw], addr + i);
387 ret |= __put_user(ptr1[i^sw], addr + i + 8);
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000388 }
389 }
390 if (ret)
391 return -EFAULT;
392 return 1; /* exception handled and fixed up */
393}
Anton Blanchard835e2062013-09-23 12:04:49 +1000394#endif
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000395
Kumar Gala26caeb22007-08-24 16:42:53 -0500396#ifdef CONFIG_SPE
397
398static struct aligninfo spe_aligninfo[32] = {
399 { 8, LD+E8 }, /* 0 00 00: evldd[x] */
400 { 8, LD+E4 }, /* 0 00 01: evldw[x] */
401 { 8, LD }, /* 0 00 10: evldh[x] */
402 INVALID, /* 0 00 11 */
403 { 2, LD }, /* 0 01 00: evlhhesplat[x] */
404 INVALID, /* 0 01 01 */
405 { 2, LD }, /* 0 01 10: evlhhousplat[x] */
406 { 2, LD+SE }, /* 0 01 11: evlhhossplat[x] */
407 { 4, LD }, /* 0 10 00: evlwhe[x] */
408 INVALID, /* 0 10 01 */
409 { 4, LD }, /* 0 10 10: evlwhou[x] */
410 { 4, LD+SE }, /* 0 10 11: evlwhos[x] */
411 { 4, LD+E4 }, /* 0 11 00: evlwwsplat[x] */
412 INVALID, /* 0 11 01 */
413 { 4, LD }, /* 0 11 10: evlwhsplat[x] */
414 INVALID, /* 0 11 11 */
415
416 { 8, ST+E8 }, /* 1 00 00: evstdd[x] */
417 { 8, ST+E4 }, /* 1 00 01: evstdw[x] */
418 { 8, ST }, /* 1 00 10: evstdh[x] */
419 INVALID, /* 1 00 11 */
420 INVALID, /* 1 01 00 */
421 INVALID, /* 1 01 01 */
422 INVALID, /* 1 01 10 */
423 INVALID, /* 1 01 11 */
424 { 4, ST }, /* 1 10 00: evstwhe[x] */
425 INVALID, /* 1 10 01 */
426 { 4, ST }, /* 1 10 10: evstwho[x] */
427 INVALID, /* 1 10 11 */
428 { 4, ST+E4 }, /* 1 11 00: evstwwe[x] */
429 INVALID, /* 1 11 01 */
430 { 4, ST+E4 }, /* 1 11 10: evstwwo[x] */
431 INVALID, /* 1 11 11 */
432};
433
434#define EVLDD 0x00
435#define EVLDW 0x01
436#define EVLDH 0x02
437#define EVLHHESPLAT 0x04
438#define EVLHHOUSPLAT 0x06
439#define EVLHHOSSPLAT 0x07
440#define EVLWHE 0x08
441#define EVLWHOU 0x0A
442#define EVLWHOS 0x0B
443#define EVLWWSPLAT 0x0C
444#define EVLWHSPLAT 0x0E
445#define EVSTDD 0x10
446#define EVSTDW 0x11
447#define EVSTDH 0x12
448#define EVSTWHE 0x18
449#define EVSTWHO 0x1A
450#define EVSTWWE 0x1C
451#define EVSTWWO 0x1E
452
453/*
454 * Emulate SPE loads and stores.
455 * Only Book-E has these instructions, and it does true little-endian,
456 * so we don't need the address swizzling.
457 */
458static int emulate_spe(struct pt_regs *regs, unsigned int reg,
459 unsigned int instr)
460{
Anton Blanchardf6261902013-09-23 12:04:46 +1000461 int ret;
Kumar Gala26caeb22007-08-24 16:42:53 -0500462 union {
463 u64 ll;
464 u32 w[2];
465 u16 h[4];
466 u8 v[8];
467 } data, temp;
468 unsigned char __user *p, *addr;
469 unsigned long *evr = &current->thread.evr[reg];
470 unsigned int nb, flags;
471
472 instr = (instr >> 1) & 0x1f;
473
474 /* DAR has the operand effective address */
475 addr = (unsigned char __user *)regs->dar;
476
477 nb = spe_aligninfo[instr].len;
478 flags = spe_aligninfo[instr].flags;
479
480 /* Verify the address of the operand */
481 if (unlikely(user_mode(regs) &&
482 !access_ok((flags & ST ? VERIFY_WRITE : VERIFY_READ),
483 addr, nb)))
484 return -EFAULT;
485
486 /* userland only */
487 if (unlikely(!user_mode(regs)))
488 return 0;
489
490 flush_spe_to_thread(current);
491
492 /* If we are loading, get the data from user space, else
493 * get it from register values
494 */
495 if (flags & ST) {
496 data.ll = 0;
497 switch (instr) {
498 case EVSTDD:
499 case EVSTDW:
500 case EVSTDH:
501 data.w[0] = *evr;
502 data.w[1] = regs->gpr[reg];
503 break;
504 case EVSTWHE:
505 data.h[2] = *evr >> 16;
506 data.h[3] = regs->gpr[reg] >> 16;
507 break;
508 case EVSTWHO:
509 data.h[2] = *evr & 0xffff;
510 data.h[3] = regs->gpr[reg] & 0xffff;
511 break;
512 case EVSTWWE:
513 data.w[1] = *evr;
514 break;
515 case EVSTWWO:
516 data.w[1] = regs->gpr[reg];
517 break;
518 default:
519 return -EINVAL;
520 }
521 } else {
522 temp.ll = data.ll = 0;
523 ret = 0;
524 p = addr;
525
526 switch (nb) {
527 case 8:
528 ret |= __get_user_inatomic(temp.v[0], p++);
529 ret |= __get_user_inatomic(temp.v[1], p++);
530 ret |= __get_user_inatomic(temp.v[2], p++);
531 ret |= __get_user_inatomic(temp.v[3], p++);
532 case 4:
533 ret |= __get_user_inatomic(temp.v[4], p++);
534 ret |= __get_user_inatomic(temp.v[5], p++);
535 case 2:
536 ret |= __get_user_inatomic(temp.v[6], p++);
537 ret |= __get_user_inatomic(temp.v[7], p++);
538 if (unlikely(ret))
539 return -EFAULT;
540 }
541
542 switch (instr) {
543 case EVLDD:
544 case EVLDW:
545 case EVLDH:
546 data.ll = temp.ll;
547 break;
548 case EVLHHESPLAT:
549 data.h[0] = temp.h[3];
550 data.h[2] = temp.h[3];
551 break;
552 case EVLHHOUSPLAT:
553 case EVLHHOSSPLAT:
554 data.h[1] = temp.h[3];
555 data.h[3] = temp.h[3];
556 break;
557 case EVLWHE:
558 data.h[0] = temp.h[2];
559 data.h[2] = temp.h[3];
560 break;
561 case EVLWHOU:
562 case EVLWHOS:
563 data.h[1] = temp.h[2];
564 data.h[3] = temp.h[3];
565 break;
566 case EVLWWSPLAT:
567 data.w[0] = temp.w[1];
568 data.w[1] = temp.w[1];
569 break;
570 case EVLWHSPLAT:
571 data.h[0] = temp.h[2];
572 data.h[1] = temp.h[2];
573 data.h[2] = temp.h[3];
574 data.h[3] = temp.h[3];
575 break;
576 default:
577 return -EINVAL;
578 }
579 }
580
581 if (flags & SW) {
582 switch (flags & 0xf0) {
583 case E8:
Anton Blanchardf6261902013-09-23 12:04:46 +1000584 data.ll = swab64(data.ll);
Kumar Gala26caeb22007-08-24 16:42:53 -0500585 break;
586 case E4:
Anton Blanchardf6261902013-09-23 12:04:46 +1000587 data.w[0] = swab32(data.w[0]);
588 data.w[1] = swab32(data.w[1]);
Kumar Gala26caeb22007-08-24 16:42:53 -0500589 break;
590 /* Its half word endian */
591 default:
Anton Blanchardf6261902013-09-23 12:04:46 +1000592 data.h[0] = swab16(data.h[0]);
593 data.h[1] = swab16(data.h[1]);
594 data.h[2] = swab16(data.h[2]);
595 data.h[3] = swab16(data.h[3]);
Kumar Gala26caeb22007-08-24 16:42:53 -0500596 break;
597 }
598 }
599
600 if (flags & SE) {
601 data.w[0] = (s16)data.h[1];
602 data.w[1] = (s16)data.h[3];
603 }
604
605 /* Store result to memory or update registers */
606 if (flags & ST) {
607 ret = 0;
608 p = addr;
609 switch (nb) {
610 case 8:
611 ret |= __put_user_inatomic(data.v[0], p++);
612 ret |= __put_user_inatomic(data.v[1], p++);
613 ret |= __put_user_inatomic(data.v[2], p++);
614 ret |= __put_user_inatomic(data.v[3], p++);
615 case 4:
616 ret |= __put_user_inatomic(data.v[4], p++);
617 ret |= __put_user_inatomic(data.v[5], p++);
618 case 2:
619 ret |= __put_user_inatomic(data.v[6], p++);
620 ret |= __put_user_inatomic(data.v[7], p++);
621 }
622 if (unlikely(ret))
623 return -EFAULT;
624 } else {
625 *evr = data.w[0];
626 regs->gpr[reg] = data.w[1];
627 }
628
629 return 1;
630}
631#endif /* CONFIG_SPE */
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100632
Anton Blanchard52055d02013-09-23 12:04:50 +1000633#ifdef CONFIG_VSX
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000634/*
635 * Emulate VSX instructions...
636 */
637static int emulate_vsx(unsigned char __user *addr, unsigned int reg,
638 unsigned int areg, struct pt_regs *regs,
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000639 unsigned int flags, unsigned int length,
640 unsigned int elsize)
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000641{
Michael Neuling26456dc2009-02-12 19:08:58 +0000642 char *ptr;
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000643 unsigned long *lptr;
Michael Neuling78fbc822008-08-28 14:57:39 +1000644 int ret = 0;
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000645 int sw = 0;
646 int i, j;
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000647
Anton Blanchard5c2e0822013-08-20 20:30:07 +1000648 /* userland only */
649 if (unlikely(!user_mode(regs)))
650 return 0;
651
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000652 flush_vsx_to_thread(current);
653
Michael Neuling26456dc2009-02-12 19:08:58 +0000654 if (reg < 32)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100655 ptr = (char *) &current->thread.fp_state.fpr[reg][0];
Michael Neuling26456dc2009-02-12 19:08:58 +0000656 else
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000657 ptr = (char *) &current->thread.vr_state.vr[reg - 32];
Michael Neuling26456dc2009-02-12 19:08:58 +0000658
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000659 lptr = (unsigned long *) ptr;
660
Anton Blanchard52055d02013-09-23 12:04:50 +1000661#ifdef __LITTLE_ENDIAN__
662 if (flags & SW) {
663 elsize = length;
664 sw = length-1;
665 } else {
666 /*
667 * The elements are BE ordered, even in LE mode, so process
668 * them in reverse order.
669 */
670 addr += length - elsize;
671
672 /* 8 byte memory accesses go in the top 8 bytes of the VR */
673 if (length == 8)
674 ptr += 8;
675 }
676#else
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000677 if (flags & SW)
678 sw = elsize-1;
Anton Blanchard52055d02013-09-23 12:04:50 +1000679#endif
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000680
681 for (j = 0; j < length; j += elsize) {
682 for (i = 0; i < elsize; ++i) {
683 if (flags & ST)
684 ret |= __put_user(ptr[i^sw], addr + i);
685 else
686 ret |= __get_user(ptr[i^sw], addr + i);
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000687 }
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000688 ptr += elsize;
Anton Blanchard52055d02013-09-23 12:04:50 +1000689#ifdef __LITTLE_ENDIAN__
690 addr -= elsize;
691#else
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000692 addr += elsize;
Anton Blanchard52055d02013-09-23 12:04:50 +1000693#endif
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000694 }
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000695
Anton Blanchard52055d02013-09-23 12:04:50 +1000696#ifdef __BIG_ENDIAN__
697#define VSX_HI 0
698#define VSX_LO 1
699#else
700#define VSX_HI 1
701#define VSX_LO 0
702#endif
703
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000704 if (!ret) {
705 if (flags & U)
706 regs->gpr[areg] = regs->dar;
707
708 /* Splat load copies the same data to top and bottom 8 bytes */
709 if (flags & SPLT)
Anton Blanchard52055d02013-09-23 12:04:50 +1000710 lptr[VSX_LO] = lptr[VSX_HI];
711 /* For 8 byte loads, zero the low 8 bytes */
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000712 else if (!(flags & ST) && (8 == length))
Anton Blanchard52055d02013-09-23 12:04:50 +1000713 lptr[VSX_LO] = 0;
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000714 } else
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000715 return -EFAULT;
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000716
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000717 return 1;
718}
719#endif
720
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100721/*
722 * Called on alignment exception. Attempts to fixup
723 *
724 * Return 1 on success
725 * Return 0 if unable to handle the interrupt
726 * Return -EFAULT if data address is bad
727 */
728
729int fix_alignment(struct pt_regs *regs)
730{
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000731 unsigned int instr, nb, flags, instruction = 0;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100732 unsigned int reg, areg;
733 unsigned int dsisr;
734 unsigned char __user *addr;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000735 unsigned long p, swiz;
Anton Blanchard835e2062013-09-23 12:04:49 +1000736 int ret, i;
737 union data {
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100738 u64 ll;
739 double dd;
740 unsigned char v[8];
741 struct {
Anton Blanchard835e2062013-09-23 12:04:49 +1000742#ifdef __LITTLE_ENDIAN__
743 int low32;
744 unsigned hi32;
745#else
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100746 unsigned hi32;
747 int low32;
Anton Blanchard835e2062013-09-23 12:04:49 +1000748#endif
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100749 } x32;
750 struct {
Anton Blanchard835e2062013-09-23 12:04:49 +1000751#ifdef __LITTLE_ENDIAN__
752 short low16;
753 unsigned char hi48[6];
754#else
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100755 unsigned char hi48[6];
756 short low16;
Anton Blanchard835e2062013-09-23 12:04:49 +1000757#endif
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100758 } x16;
759 } data;
760
761 /*
762 * We require a complete register set, if not, then our assembly
763 * is broken
764 */
765 CHECK_FULL_REGS(regs);
766
767 dsisr = regs->dsisr;
768
769 /* Some processors don't provide us with a DSISR we can use here,
770 * let's make one up from the instruction
771 */
772 if (cpu_has_feature(CPU_FTR_NODSISRALIGN)) {
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000773 unsigned long pc = regs->nip;
774
775 if (cpu_has_feature(CPU_FTR_PPC_LE) && (regs->msr & MSR_LE))
776 pc ^= 4;
Benjamin Herrenschmidte4ee38912007-04-11 16:13:19 +1000777 if (unlikely(__get_user_inatomic(instr,
778 (unsigned int __user *)pc)))
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100779 return -EFAULT;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000780 if (cpu_has_feature(CPU_FTR_REAL_LE) && (regs->msr & MSR_LE))
781 instr = cpu_to_le32(instr);
782 dsisr = make_dsisr(instr);
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000783 instruction = instr;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100784 }
785
786 /* extract the operation and registers from the dsisr */
787 reg = (dsisr >> 5) & 0x1f; /* source/dest register */
788 areg = dsisr & 0x1f; /* register to update */
Kumar Gala26caeb22007-08-24 16:42:53 -0500789
790#ifdef CONFIG_SPE
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000791 if ((instr >> 26) == 0x4) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000792 PPC_WARN_ALIGNMENT(spe, regs);
Kumar Gala26caeb22007-08-24 16:42:53 -0500793 return emulate_spe(regs, reg, instr);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000794 }
Kumar Gala26caeb22007-08-24 16:42:53 -0500795#endif
796
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100797 instr = (dsisr >> 10) & 0x7f;
798 instr |= (dsisr >> 13) & 0x60;
799
800 /* Lookup the operation in our table */
801 nb = aligninfo[instr].len;
802 flags = aligninfo[instr].flags;
803
Anton Blanchard230aef72013-08-07 02:01:19 +1000804 /* ldbrx/stdbrx overlap lfs/stfs in the DSISR unfortunately */
805 if (IS_XFORM(instruction) && ((instruction >> 1) & 0x3ff) == 532) {
806 nb = 8;
807 flags = LD+SW;
808 } else if (IS_XFORM(instruction) &&
809 ((instruction >> 1) & 0x3ff) == 660) {
810 nb = 8;
811 flags = ST+SW;
812 }
813
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000814 /* Byteswap little endian loads and stores */
815 swiz = 0;
Anton Blanchard835e2062013-09-23 12:04:49 +1000816 if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE)) {
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000817 flags ^= SW;
Anton Blanchard835e2062013-09-23 12:04:49 +1000818#ifdef __BIG_ENDIAN__
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000819 /*
820 * So-called "PowerPC little endian" mode works by
821 * swizzling addresses rather than by actually doing
822 * any byte-swapping. To emulate this, we XOR each
823 * byte address with 7. We also byte-swap, because
824 * the processor's address swizzling depends on the
825 * operand size (it xors the address with 7 for bytes,
826 * 6 for halfwords, 4 for words, 0 for doublewords) but
827 * we will xor with 7 and load/store each byte separately.
828 */
829 if (cpu_has_feature(CPU_FTR_PPC_LE))
830 swiz = 7;
Anton Blanchard835e2062013-09-23 12:04:49 +1000831#endif
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000832 }
833
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100834 /* DAR has the operand effective address */
835 addr = (unsigned char __user *)regs->dar;
836
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000837#ifdef CONFIG_VSX
838 if ((instruction & 0xfc00003e) == 0x7c000018) {
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000839 unsigned int elsize;
840
841 /* Additional register addressing bit (64 VSX vs 32 FPR/GPR) */
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000842 reg |= (instruction & 0x1) << 5;
843 /* Simple inline decoder instead of a table */
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000844 /* VSX has only 8 and 16 byte memory accesses */
845 nb = 8;
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000846 if (instruction & 0x200)
847 nb = 16;
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000848
849 /* Vector stores in little-endian mode swap individual
850 elements, so process them separately */
851 elsize = 4;
852 if (instruction & 0x80)
853 elsize = 8;
854
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000855 flags = 0;
Anton Blanchard835e2062013-09-23 12:04:49 +1000856 if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE))
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000857 flags |= SW;
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000858 if (instruction & 0x100)
859 flags |= ST;
860 if (instruction & 0x040)
861 flags |= U;
862 /* splat load needs a special decoder */
863 if ((instruction & 0x400) == 0){
864 flags |= SPLT;
865 nb = 8;
866 }
Anton Blanchardeecff812009-10-27 18:46:55 +0000867 PPC_WARN_ALIGNMENT(vsx, regs);
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000868 return emulate_vsx(addr, reg, areg, regs, flags, nb, elsize);
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000869 }
870#endif
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100871 /* A size of 0 indicates an instruction we don't support, with
872 * the exception of DCBZ which is handled as a special case here
873 */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000874 if (instr == DCBZ) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000875 PPC_WARN_ALIGNMENT(dcbz, regs);
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100876 return emulate_dcbz(regs, addr);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000877 }
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100878 if (unlikely(nb == 0))
879 return 0;
880
881 /* Load/Store Multiple instructions are handled in their own
882 * function
883 */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000884 if (flags & M) {
Anton Blanchard835e2062013-09-23 12:04:49 +1000885#ifdef __BIG_ENDIAN__
Anton Blanchardeecff812009-10-27 18:46:55 +0000886 PPC_WARN_ALIGNMENT(multiple, regs);
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000887 return emulate_multiple(regs, addr, reg, nb,
888 flags, instr, swiz);
Anton Blanchard835e2062013-09-23 12:04:49 +1000889#else
890 return -EFAULT;
891#endif
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000892 }
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100893
894 /* Verify the address of the operand */
895 if (unlikely(user_mode(regs) &&
896 !access_ok((flags & ST ? VERIFY_WRITE : VERIFY_READ),
897 addr, nb)))
898 return -EFAULT;
899
900 /* Force the fprs into the save area so we can reference them */
901 if (flags & F) {
902 /* userland only */
903 if (unlikely(!user_mode(regs)))
904 return 0;
905 flush_fp_to_thread(current);
906 }
907
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000908 /* Special case for 16-byte FP loads and stores */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000909 if (nb == 16) {
Anton Blanchard835e2062013-09-23 12:04:49 +1000910#ifdef __BIG_ENDIAN__
Anton Blanchardeecff812009-10-27 18:46:55 +0000911 PPC_WARN_ALIGNMENT(fp_pair, regs);
Michael Neulingb887ec62008-07-08 18:53:03 +1000912 return emulate_fp_pair(addr, reg, flags);
Anton Blanchard835e2062013-09-23 12:04:49 +1000913#else
914 return -EFAULT;
915#endif
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000916 }
917
Anton Blanchardeecff812009-10-27 18:46:55 +0000918 PPC_WARN_ALIGNMENT(unaligned, regs);
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000919
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100920 /* If we are loading, get the data from user space, else
921 * get it from register values
922 */
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000923 if (!(flags & ST)) {
Anton Blanchard835e2062013-09-23 12:04:49 +1000924 unsigned int start = 0;
925
926 switch (nb) {
927 case 4:
928 start = offsetof(union data, x32.low32);
929 break;
930 case 2:
931 start = offsetof(union data, x16.low16);
932 break;
933 }
934
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100935 data.ll = 0;
936 ret = 0;
Anton Blanchard835e2062013-09-23 12:04:49 +1000937 p = (unsigned long)addr;
938
939 for (i = 0; i < nb; i++)
940 ret |= __get_user_inatomic(data.v[start + i],
941 SWIZ_PTR(p++));
942
943 if (unlikely(ret))
944 return -EFAULT;
945
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000946 } else if (flags & F) {
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000947 data.ll = current->thread.TS_FPR(reg);
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000948 if (flags & S) {
949 /* Single-precision FP store requires conversion... */
950#ifdef CONFIG_PPC_FPU
951 preempt_disable();
952 enable_kernel_fp();
Anton Blanchardc3244962013-09-23 12:04:47 +1000953 cvt_df(&data.dd, (float *)&data.x32.low32);
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000954 preempt_enable();
955#else
956 return 0;
957#endif
958 }
959 } else
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100960 data.ll = regs->gpr[reg];
961
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000962 if (flags & SW) {
963 switch (nb) {
964 case 8:
Anton Blanchardf6261902013-09-23 12:04:46 +1000965 data.ll = swab64(data.ll);
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000966 break;
967 case 4:
Anton Blanchardf6261902013-09-23 12:04:46 +1000968 data.x32.low32 = swab32(data.x32.low32);
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000969 break;
970 case 2:
Anton Blanchardf6261902013-09-23 12:04:46 +1000971 data.x16.low16 = swab16(data.x16.low16);
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000972 break;
973 }
974 }
975
976 /* Perform other misc operations like sign extension
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100977 * or floating point single precision conversion
978 */
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000979 switch (flags & ~(U|SW)) {
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000980 case LD+SE: /* sign extending integer loads */
981 case LD+F+SE: /* sign extend for lfiwax */
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100982 if ( nb == 2 )
983 data.ll = data.x16.low16;
984 else /* nb must be 4 */
985 data.ll = data.x32.low32;
986 break;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100987
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000988 /* Single-precision FP load requires conversion... */
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100989 case LD+F+S:
990#ifdef CONFIG_PPC_FPU
991 preempt_disable();
992 enable_kernel_fp();
Anton Blanchardc3244962013-09-23 12:04:47 +1000993 cvt_fd((float *)&data.x32.low32, &data.dd);
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100994 preempt_enable();
995#else
996 return 0;
997#endif
998 break;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100999 }
1000
1001 /* Store result to memory or update registers */
1002 if (flags & ST) {
Anton Blanchard835e2062013-09-23 12:04:49 +10001003 unsigned int start = 0;
1004
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001005 switch (nb) {
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001006 case 4:
Anton Blanchard835e2062013-09-23 12:04:49 +10001007 start = offsetof(union data, x32.low32);
1008 break;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001009 case 2:
Anton Blanchard835e2062013-09-23 12:04:49 +10001010 start = offsetof(union data, x16.low16);
1011 break;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001012 }
Anton Blanchard835e2062013-09-23 12:04:49 +10001013
1014 ret = 0;
1015 p = (unsigned long)addr;
1016
1017 for (i = 0; i < nb; i++)
1018 ret |= __put_user_inatomic(data.v[start + i],
1019 SWIZ_PTR(p++));
1020
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001021 if (unlikely(ret))
1022 return -EFAULT;
1023 } else if (flags & F)
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001024 current->thread.TS_FPR(reg) = data.ll;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001025 else
1026 regs->gpr[reg] = data.ll;
1027
1028 /* Update RA as needed */
1029 if (flags & U)
1030 regs->gpr[areg] = regs->dar;
1031
1032 return 1;
1033}