blob: a3169a987b847c38a81da8b6e4690e9d26506238 [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 */
Tom Musta075f6312013-10-18 12:07:10 -0500257#ifdef __BIG_ENDIAN__
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100258#ifdef CONFIG_PPC64
259#define REG_BYTE(rp, i) *((u8 *)((rp) + ((i) >> 2)) + ((i) & 3) + 4)
260#else
261#define REG_BYTE(rp, i) *((u8 *)(rp) + (i))
262#endif
Tom Musta075f6312013-10-18 12:07:10 -0500263#endif
264
265#ifdef __LITTLE_ENDIAN__
266#define REG_BYTE(rp, i) (*(((u8 *)((rp) + ((i)>>2)) + ((i)&3))))
267#endif
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100268
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000269#define SWIZ_PTR(p) ((unsigned char __user *)((p) ^ swiz))
270
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100271static int emulate_multiple(struct pt_regs *regs, unsigned char __user *addr,
272 unsigned int reg, unsigned int nb,
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000273 unsigned int flags, unsigned int instr,
274 unsigned long swiz)
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100275{
276 unsigned long *rptr;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000277 unsigned int nb0, i, bswiz;
278 unsigned long p;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100279
280 /*
281 * We do not try to emulate 8 bytes multiple as they aren't really
282 * available in our operating environments and we don't try to
283 * emulate multiples operations in kernel land as they should never
284 * be used/generated there at least not on unaligned boundaries
285 */
286 if (unlikely((nb > 4) || !user_mode(regs)))
287 return 0;
288
289 /* lmw, stmw, lswi/x, stswi/x */
290 nb0 = 0;
291 if (flags & HARD) {
292 if (flags & SX) {
293 nb = regs->xer & 127;
294 if (nb == 0)
295 return 1;
296 } else {
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000297 unsigned long pc = regs->nip ^ (swiz & 4);
298
Benjamin Herrenschmidte4ee38912007-04-11 16:13:19 +1000299 if (__get_user_inatomic(instr,
300 (unsigned int __user *)pc))
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100301 return -EFAULT;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000302 if (swiz == 0 && (flags & SW))
303 instr = cpu_to_le32(instr);
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100304 nb = (instr >> 11) & 0x1f;
305 if (nb == 0)
306 nb = 32;
307 }
308 if (nb + reg * 4 > 128) {
309 nb0 = nb + reg * 4 - 128;
310 nb = 128 - reg * 4;
311 }
Tom Musta075f6312013-10-18 12:07:10 -0500312#ifdef __LITTLE_ENDIAN__
313 /*
314 * String instructions are endian neutral but the code
315 * below is not. Force byte swapping on so that the
316 * effects of swizzling are undone in the load/store
317 * loops below.
318 */
319 flags ^= SW;
320#endif
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100321 } else {
322 /* lwm, stmw */
323 nb = (32 - reg) * 4;
324 }
325
326 if (!access_ok((flags & ST ? VERIFY_WRITE: VERIFY_READ), addr, nb+nb0))
327 return -EFAULT; /* bad address */
328
329 rptr = &regs->gpr[reg];
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000330 p = (unsigned long) addr;
331 bswiz = (flags & SW)? 3: 0;
332
333 if (!(flags & ST)) {
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100334 /*
335 * This zeroes the top 4 bytes of the affected registers
336 * in 64-bit mode, and also zeroes out any remaining
337 * bytes of the last register for lsw*.
338 */
339 memset(rptr, 0, ((nb + 3) / 4) * sizeof(unsigned long));
340 if (nb0 > 0)
341 memset(&regs->gpr[0], 0,
342 ((nb0 + 3) / 4) * sizeof(unsigned long));
343
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000344 for (i = 0; i < nb; ++i, ++p)
Benjamin Herrenschmidte4ee38912007-04-11 16:13:19 +1000345 if (__get_user_inatomic(REG_BYTE(rptr, i ^ bswiz),
346 SWIZ_PTR(p)))
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100347 return -EFAULT;
348 if (nb0 > 0) {
349 rptr = &regs->gpr[0];
350 addr += nb;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000351 for (i = 0; i < nb0; ++i, ++p)
Benjamin Herrenschmidte4ee38912007-04-11 16:13:19 +1000352 if (__get_user_inatomic(REG_BYTE(rptr,
353 i ^ bswiz),
354 SWIZ_PTR(p)))
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100355 return -EFAULT;
356 }
357
358 } else {
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000359 for (i = 0; i < nb; ++i, ++p)
Benjamin Herrenschmidte4ee38912007-04-11 16:13:19 +1000360 if (__put_user_inatomic(REG_BYTE(rptr, i ^ bswiz),
361 SWIZ_PTR(p)))
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100362 return -EFAULT;
363 if (nb0 > 0) {
364 rptr = &regs->gpr[0];
365 addr += nb;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000366 for (i = 0; i < nb0; ++i, ++p)
Benjamin Herrenschmidte4ee38912007-04-11 16:13:19 +1000367 if (__put_user_inatomic(REG_BYTE(rptr,
368 i ^ bswiz),
369 SWIZ_PTR(p)))
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100370 return -EFAULT;
371 }
372 }
373 return 1;
374}
375
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000376/*
377 * Emulate floating-point pair loads and stores.
378 * Only POWER6 has these instructions, and it does true little-endian,
379 * so we don't need the address swizzling.
380 */
Tom Musta075f6312013-10-18 12:07:10 -0500381#ifdef __BIG_ENDIAN__
Michael Neulingb887ec62008-07-08 18:53:03 +1000382static int emulate_fp_pair(unsigned char __user *addr, unsigned int reg,
383 unsigned int flags)
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000384{
Michael Neuling553631e2009-02-19 18:52:20 +0000385 char *ptr0 = (char *) &current->thread.TS_FPR(reg);
386 char *ptr1 = (char *) &current->thread.TS_FPR(reg+1);
387 int i, ret, sw = 0;
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000388
389 if (!(flags & F))
390 return 0;
391 if (reg & 1)
392 return 0; /* invalid form: FRS/FRT must be even */
Michael Neuling553631e2009-02-19 18:52:20 +0000393 if (flags & SW)
394 sw = 7;
395 ret = 0;
396 for (i = 0; i < 8; ++i) {
397 if (!(flags & ST)) {
398 ret |= __get_user(ptr0[i^sw], addr + i);
399 ret |= __get_user(ptr1[i^sw], addr + i + 8);
400 } else {
401 ret |= __put_user(ptr0[i^sw], addr + i);
402 ret |= __put_user(ptr1[i^sw], addr + i + 8);
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000403 }
404 }
405 if (ret)
406 return -EFAULT;
407 return 1; /* exception handled and fixed up */
408}
Anton Blanchard835e2062013-09-23 12:04:49 +1000409#endif
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000410
Kumar Gala26caeb22007-08-24 16:42:53 -0500411#ifdef CONFIG_SPE
412
413static struct aligninfo spe_aligninfo[32] = {
414 { 8, LD+E8 }, /* 0 00 00: evldd[x] */
415 { 8, LD+E4 }, /* 0 00 01: evldw[x] */
416 { 8, LD }, /* 0 00 10: evldh[x] */
417 INVALID, /* 0 00 11 */
418 { 2, LD }, /* 0 01 00: evlhhesplat[x] */
419 INVALID, /* 0 01 01 */
420 { 2, LD }, /* 0 01 10: evlhhousplat[x] */
421 { 2, LD+SE }, /* 0 01 11: evlhhossplat[x] */
422 { 4, LD }, /* 0 10 00: evlwhe[x] */
423 INVALID, /* 0 10 01 */
424 { 4, LD }, /* 0 10 10: evlwhou[x] */
425 { 4, LD+SE }, /* 0 10 11: evlwhos[x] */
426 { 4, LD+E4 }, /* 0 11 00: evlwwsplat[x] */
427 INVALID, /* 0 11 01 */
428 { 4, LD }, /* 0 11 10: evlwhsplat[x] */
429 INVALID, /* 0 11 11 */
430
431 { 8, ST+E8 }, /* 1 00 00: evstdd[x] */
432 { 8, ST+E4 }, /* 1 00 01: evstdw[x] */
433 { 8, ST }, /* 1 00 10: evstdh[x] */
434 INVALID, /* 1 00 11 */
435 INVALID, /* 1 01 00 */
436 INVALID, /* 1 01 01 */
437 INVALID, /* 1 01 10 */
438 INVALID, /* 1 01 11 */
439 { 4, ST }, /* 1 10 00: evstwhe[x] */
440 INVALID, /* 1 10 01 */
441 { 4, ST }, /* 1 10 10: evstwho[x] */
442 INVALID, /* 1 10 11 */
443 { 4, ST+E4 }, /* 1 11 00: evstwwe[x] */
444 INVALID, /* 1 11 01 */
445 { 4, ST+E4 }, /* 1 11 10: evstwwo[x] */
446 INVALID, /* 1 11 11 */
447};
448
449#define EVLDD 0x00
450#define EVLDW 0x01
451#define EVLDH 0x02
452#define EVLHHESPLAT 0x04
453#define EVLHHOUSPLAT 0x06
454#define EVLHHOSSPLAT 0x07
455#define EVLWHE 0x08
456#define EVLWHOU 0x0A
457#define EVLWHOS 0x0B
458#define EVLWWSPLAT 0x0C
459#define EVLWHSPLAT 0x0E
460#define EVSTDD 0x10
461#define EVSTDW 0x11
462#define EVSTDH 0x12
463#define EVSTWHE 0x18
464#define EVSTWHO 0x1A
465#define EVSTWWE 0x1C
466#define EVSTWWO 0x1E
467
468/*
469 * Emulate SPE loads and stores.
470 * Only Book-E has these instructions, and it does true little-endian,
471 * so we don't need the address swizzling.
472 */
473static int emulate_spe(struct pt_regs *regs, unsigned int reg,
474 unsigned int instr)
475{
Anton Blanchardf6261902013-09-23 12:04:46 +1000476 int ret;
Kumar Gala26caeb22007-08-24 16:42:53 -0500477 union {
478 u64 ll;
479 u32 w[2];
480 u16 h[4];
481 u8 v[8];
482 } data, temp;
483 unsigned char __user *p, *addr;
484 unsigned long *evr = &current->thread.evr[reg];
485 unsigned int nb, flags;
486
487 instr = (instr >> 1) & 0x1f;
488
489 /* DAR has the operand effective address */
490 addr = (unsigned char __user *)regs->dar;
491
492 nb = spe_aligninfo[instr].len;
493 flags = spe_aligninfo[instr].flags;
494
495 /* Verify the address of the operand */
496 if (unlikely(user_mode(regs) &&
497 !access_ok((flags & ST ? VERIFY_WRITE : VERIFY_READ),
498 addr, nb)))
499 return -EFAULT;
500
501 /* userland only */
502 if (unlikely(!user_mode(regs)))
503 return 0;
504
505 flush_spe_to_thread(current);
506
507 /* If we are loading, get the data from user space, else
508 * get it from register values
509 */
510 if (flags & ST) {
511 data.ll = 0;
512 switch (instr) {
513 case EVSTDD:
514 case EVSTDW:
515 case EVSTDH:
516 data.w[0] = *evr;
517 data.w[1] = regs->gpr[reg];
518 break;
519 case EVSTWHE:
520 data.h[2] = *evr >> 16;
521 data.h[3] = regs->gpr[reg] >> 16;
522 break;
523 case EVSTWHO:
524 data.h[2] = *evr & 0xffff;
525 data.h[3] = regs->gpr[reg] & 0xffff;
526 break;
527 case EVSTWWE:
528 data.w[1] = *evr;
529 break;
530 case EVSTWWO:
531 data.w[1] = regs->gpr[reg];
532 break;
533 default:
534 return -EINVAL;
535 }
536 } else {
537 temp.ll = data.ll = 0;
538 ret = 0;
539 p = addr;
540
541 switch (nb) {
542 case 8:
543 ret |= __get_user_inatomic(temp.v[0], p++);
544 ret |= __get_user_inatomic(temp.v[1], p++);
545 ret |= __get_user_inatomic(temp.v[2], p++);
546 ret |= __get_user_inatomic(temp.v[3], p++);
547 case 4:
548 ret |= __get_user_inatomic(temp.v[4], p++);
549 ret |= __get_user_inatomic(temp.v[5], p++);
550 case 2:
551 ret |= __get_user_inatomic(temp.v[6], p++);
552 ret |= __get_user_inatomic(temp.v[7], p++);
553 if (unlikely(ret))
554 return -EFAULT;
555 }
556
557 switch (instr) {
558 case EVLDD:
559 case EVLDW:
560 case EVLDH:
561 data.ll = temp.ll;
562 break;
563 case EVLHHESPLAT:
564 data.h[0] = temp.h[3];
565 data.h[2] = temp.h[3];
566 break;
567 case EVLHHOUSPLAT:
568 case EVLHHOSSPLAT:
569 data.h[1] = temp.h[3];
570 data.h[3] = temp.h[3];
571 break;
572 case EVLWHE:
573 data.h[0] = temp.h[2];
574 data.h[2] = temp.h[3];
575 break;
576 case EVLWHOU:
577 case EVLWHOS:
578 data.h[1] = temp.h[2];
579 data.h[3] = temp.h[3];
580 break;
581 case EVLWWSPLAT:
582 data.w[0] = temp.w[1];
583 data.w[1] = temp.w[1];
584 break;
585 case EVLWHSPLAT:
586 data.h[0] = temp.h[2];
587 data.h[1] = temp.h[2];
588 data.h[2] = temp.h[3];
589 data.h[3] = temp.h[3];
590 break;
591 default:
592 return -EINVAL;
593 }
594 }
595
596 if (flags & SW) {
597 switch (flags & 0xf0) {
598 case E8:
Anton Blanchardf6261902013-09-23 12:04:46 +1000599 data.ll = swab64(data.ll);
Kumar Gala26caeb22007-08-24 16:42:53 -0500600 break;
601 case E4:
Anton Blanchardf6261902013-09-23 12:04:46 +1000602 data.w[0] = swab32(data.w[0]);
603 data.w[1] = swab32(data.w[1]);
Kumar Gala26caeb22007-08-24 16:42:53 -0500604 break;
605 /* Its half word endian */
606 default:
Anton Blanchardf6261902013-09-23 12:04:46 +1000607 data.h[0] = swab16(data.h[0]);
608 data.h[1] = swab16(data.h[1]);
609 data.h[2] = swab16(data.h[2]);
610 data.h[3] = swab16(data.h[3]);
Kumar Gala26caeb22007-08-24 16:42:53 -0500611 break;
612 }
613 }
614
615 if (flags & SE) {
616 data.w[0] = (s16)data.h[1];
617 data.w[1] = (s16)data.h[3];
618 }
619
620 /* Store result to memory or update registers */
621 if (flags & ST) {
622 ret = 0;
623 p = addr;
624 switch (nb) {
625 case 8:
626 ret |= __put_user_inatomic(data.v[0], p++);
627 ret |= __put_user_inatomic(data.v[1], p++);
628 ret |= __put_user_inatomic(data.v[2], p++);
629 ret |= __put_user_inatomic(data.v[3], p++);
630 case 4:
631 ret |= __put_user_inatomic(data.v[4], p++);
632 ret |= __put_user_inatomic(data.v[5], p++);
633 case 2:
634 ret |= __put_user_inatomic(data.v[6], p++);
635 ret |= __put_user_inatomic(data.v[7], p++);
636 }
637 if (unlikely(ret))
638 return -EFAULT;
639 } else {
640 *evr = data.w[0];
641 regs->gpr[reg] = data.w[1];
642 }
643
644 return 1;
645}
646#endif /* CONFIG_SPE */
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100647
Anton Blanchard52055d02013-09-23 12:04:50 +1000648#ifdef CONFIG_VSX
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000649/*
650 * Emulate VSX instructions...
651 */
652static int emulate_vsx(unsigned char __user *addr, unsigned int reg,
653 unsigned int areg, struct pt_regs *regs,
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000654 unsigned int flags, unsigned int length,
655 unsigned int elsize)
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000656{
Michael Neuling26456dc2009-02-12 19:08:58 +0000657 char *ptr;
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000658 unsigned long *lptr;
Michael Neuling78fbc822008-08-28 14:57:39 +1000659 int ret = 0;
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000660 int sw = 0;
661 int i, j;
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000662
Anton Blanchard5c2e0822013-08-20 20:30:07 +1000663 /* userland only */
664 if (unlikely(!user_mode(regs)))
665 return 0;
666
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000667 flush_vsx_to_thread(current);
668
Michael Neuling26456dc2009-02-12 19:08:58 +0000669 if (reg < 32)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100670 ptr = (char *) &current->thread.fp_state.fpr[reg][0];
Michael Neuling26456dc2009-02-12 19:08:58 +0000671 else
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000672 ptr = (char *) &current->thread.vr_state.vr[reg - 32];
Michael Neuling26456dc2009-02-12 19:08:58 +0000673
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000674 lptr = (unsigned long *) ptr;
675
Anton Blanchard52055d02013-09-23 12:04:50 +1000676#ifdef __LITTLE_ENDIAN__
677 if (flags & SW) {
678 elsize = length;
679 sw = length-1;
680 } else {
681 /*
682 * The elements are BE ordered, even in LE mode, so process
683 * them in reverse order.
684 */
685 addr += length - elsize;
686
687 /* 8 byte memory accesses go in the top 8 bytes of the VR */
688 if (length == 8)
689 ptr += 8;
690 }
691#else
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000692 if (flags & SW)
693 sw = elsize-1;
Anton Blanchard52055d02013-09-23 12:04:50 +1000694#endif
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000695
696 for (j = 0; j < length; j += elsize) {
697 for (i = 0; i < elsize; ++i) {
698 if (flags & ST)
699 ret |= __put_user(ptr[i^sw], addr + i);
700 else
701 ret |= __get_user(ptr[i^sw], addr + i);
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000702 }
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000703 ptr += elsize;
Anton Blanchard52055d02013-09-23 12:04:50 +1000704#ifdef __LITTLE_ENDIAN__
705 addr -= elsize;
706#else
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000707 addr += elsize;
Anton Blanchard52055d02013-09-23 12:04:50 +1000708#endif
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000709 }
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000710
Anton Blanchard52055d02013-09-23 12:04:50 +1000711#ifdef __BIG_ENDIAN__
712#define VSX_HI 0
713#define VSX_LO 1
714#else
715#define VSX_HI 1
716#define VSX_LO 0
717#endif
718
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000719 if (!ret) {
720 if (flags & U)
721 regs->gpr[areg] = regs->dar;
722
723 /* Splat load copies the same data to top and bottom 8 bytes */
724 if (flags & SPLT)
Anton Blanchard52055d02013-09-23 12:04:50 +1000725 lptr[VSX_LO] = lptr[VSX_HI];
726 /* For 8 byte loads, zero the low 8 bytes */
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000727 else if (!(flags & ST) && (8 == length))
Anton Blanchard52055d02013-09-23 12:04:50 +1000728 lptr[VSX_LO] = 0;
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000729 } else
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000730 return -EFAULT;
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000731
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000732 return 1;
733}
734#endif
735
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100736/*
737 * Called on alignment exception. Attempts to fixup
738 *
739 * Return 1 on success
740 * Return 0 if unable to handle the interrupt
741 * Return -EFAULT if data address is bad
742 */
743
744int fix_alignment(struct pt_regs *regs)
745{
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000746 unsigned int instr, nb, flags, instruction = 0;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100747 unsigned int reg, areg;
748 unsigned int dsisr;
749 unsigned char __user *addr;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000750 unsigned long p, swiz;
Anton Blanchard835e2062013-09-23 12:04:49 +1000751 int ret, i;
752 union data {
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100753 u64 ll;
754 double dd;
755 unsigned char v[8];
756 struct {
Anton Blanchard835e2062013-09-23 12:04:49 +1000757#ifdef __LITTLE_ENDIAN__
758 int low32;
759 unsigned hi32;
760#else
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100761 unsigned hi32;
762 int low32;
Anton Blanchard835e2062013-09-23 12:04:49 +1000763#endif
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100764 } x32;
765 struct {
Anton Blanchard835e2062013-09-23 12:04:49 +1000766#ifdef __LITTLE_ENDIAN__
767 short low16;
768 unsigned char hi48[6];
769#else
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100770 unsigned char hi48[6];
771 short low16;
Anton Blanchard835e2062013-09-23 12:04:49 +1000772#endif
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100773 } x16;
774 } data;
775
776 /*
777 * We require a complete register set, if not, then our assembly
778 * is broken
779 */
780 CHECK_FULL_REGS(regs);
781
782 dsisr = regs->dsisr;
783
784 /* Some processors don't provide us with a DSISR we can use here,
785 * let's make one up from the instruction
786 */
787 if (cpu_has_feature(CPU_FTR_NODSISRALIGN)) {
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000788 unsigned long pc = regs->nip;
789
790 if (cpu_has_feature(CPU_FTR_PPC_LE) && (regs->msr & MSR_LE))
791 pc ^= 4;
Benjamin Herrenschmidte4ee38912007-04-11 16:13:19 +1000792 if (unlikely(__get_user_inatomic(instr,
793 (unsigned int __user *)pc)))
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100794 return -EFAULT;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000795 if (cpu_has_feature(CPU_FTR_REAL_LE) && (regs->msr & MSR_LE))
796 instr = cpu_to_le32(instr);
797 dsisr = make_dsisr(instr);
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000798 instruction = instr;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100799 }
800
801 /* extract the operation and registers from the dsisr */
802 reg = (dsisr >> 5) & 0x1f; /* source/dest register */
803 areg = dsisr & 0x1f; /* register to update */
Kumar Gala26caeb22007-08-24 16:42:53 -0500804
805#ifdef CONFIG_SPE
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000806 if ((instr >> 26) == 0x4) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000807 PPC_WARN_ALIGNMENT(spe, regs);
Kumar Gala26caeb22007-08-24 16:42:53 -0500808 return emulate_spe(regs, reg, instr);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000809 }
Kumar Gala26caeb22007-08-24 16:42:53 -0500810#endif
811
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100812 instr = (dsisr >> 10) & 0x7f;
813 instr |= (dsisr >> 13) & 0x60;
814
815 /* Lookup the operation in our table */
816 nb = aligninfo[instr].len;
817 flags = aligninfo[instr].flags;
818
Anton Blanchard230aef72013-08-07 02:01:19 +1000819 /* ldbrx/stdbrx overlap lfs/stfs in the DSISR unfortunately */
820 if (IS_XFORM(instruction) && ((instruction >> 1) & 0x3ff) == 532) {
821 nb = 8;
822 flags = LD+SW;
823 } else if (IS_XFORM(instruction) &&
824 ((instruction >> 1) & 0x3ff) == 660) {
825 nb = 8;
826 flags = ST+SW;
827 }
828
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000829 /* Byteswap little endian loads and stores */
830 swiz = 0;
Anton Blanchard835e2062013-09-23 12:04:49 +1000831 if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE)) {
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000832 flags ^= SW;
Anton Blanchard835e2062013-09-23 12:04:49 +1000833#ifdef __BIG_ENDIAN__
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000834 /*
835 * So-called "PowerPC little endian" mode works by
836 * swizzling addresses rather than by actually doing
837 * any byte-swapping. To emulate this, we XOR each
838 * byte address with 7. We also byte-swap, because
839 * the processor's address swizzling depends on the
840 * operand size (it xors the address with 7 for bytes,
841 * 6 for halfwords, 4 for words, 0 for doublewords) but
842 * we will xor with 7 and load/store each byte separately.
843 */
844 if (cpu_has_feature(CPU_FTR_PPC_LE))
845 swiz = 7;
Anton Blanchard835e2062013-09-23 12:04:49 +1000846#endif
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000847 }
848
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100849 /* DAR has the operand effective address */
850 addr = (unsigned char __user *)regs->dar;
851
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000852#ifdef CONFIG_VSX
853 if ((instruction & 0xfc00003e) == 0x7c000018) {
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000854 unsigned int elsize;
855
856 /* Additional register addressing bit (64 VSX vs 32 FPR/GPR) */
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000857 reg |= (instruction & 0x1) << 5;
858 /* Simple inline decoder instead of a table */
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000859 /* VSX has only 8 and 16 byte memory accesses */
860 nb = 8;
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000861 if (instruction & 0x200)
862 nb = 16;
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000863
864 /* Vector stores in little-endian mode swap individual
865 elements, so process them separately */
866 elsize = 4;
867 if (instruction & 0x80)
868 elsize = 8;
869
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000870 flags = 0;
Anton Blanchard835e2062013-09-23 12:04:49 +1000871 if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE))
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000872 flags |= SW;
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000873 if (instruction & 0x100)
874 flags |= ST;
875 if (instruction & 0x040)
876 flags |= U;
877 /* splat load needs a special decoder */
878 if ((instruction & 0x400) == 0){
879 flags |= SPLT;
880 nb = 8;
881 }
Anton Blanchardeecff812009-10-27 18:46:55 +0000882 PPC_WARN_ALIGNMENT(vsx, regs);
Neil Campbellbb7f20b2009-12-14 04:08:57 +0000883 return emulate_vsx(addr, reg, areg, regs, flags, nb, elsize);
Michael Neulingcd6f37b2008-07-11 16:31:09 +1000884 }
885#endif
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100886 /* A size of 0 indicates an instruction we don't support, with
887 * the exception of DCBZ which is handled as a special case here
888 */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000889 if (instr == DCBZ) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000890 PPC_WARN_ALIGNMENT(dcbz, regs);
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100891 return emulate_dcbz(regs, addr);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000892 }
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100893 if (unlikely(nb == 0))
894 return 0;
895
896 /* Load/Store Multiple instructions are handled in their own
897 * function
898 */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000899 if (flags & M) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000900 PPC_WARN_ALIGNMENT(multiple, regs);
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000901 return emulate_multiple(regs, addr, reg, nb,
902 flags, instr, swiz);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000903 }
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100904
905 /* Verify the address of the operand */
906 if (unlikely(user_mode(regs) &&
907 !access_ok((flags & ST ? VERIFY_WRITE : VERIFY_READ),
908 addr, nb)))
909 return -EFAULT;
910
911 /* Force the fprs into the save area so we can reference them */
912 if (flags & F) {
913 /* userland only */
914 if (unlikely(!user_mode(regs)))
915 return 0;
916 flush_fp_to_thread(current);
917 }
918
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000919 /* Special case for 16-byte FP loads and stores */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000920 if (nb == 16) {
Anton Blanchard835e2062013-09-23 12:04:49 +1000921#ifdef __BIG_ENDIAN__
Anton Blanchardeecff812009-10-27 18:46:55 +0000922 PPC_WARN_ALIGNMENT(fp_pair, regs);
Michael Neulingb887ec62008-07-08 18:53:03 +1000923 return emulate_fp_pair(addr, reg, flags);
Anton Blanchard835e2062013-09-23 12:04:49 +1000924#else
925 return -EFAULT;
926#endif
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000927 }
928
Anton Blanchardeecff812009-10-27 18:46:55 +0000929 PPC_WARN_ALIGNMENT(unaligned, regs);
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000930
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100931 /* If we are loading, get the data from user space, else
932 * get it from register values
933 */
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000934 if (!(flags & ST)) {
Anton Blanchard835e2062013-09-23 12:04:49 +1000935 unsigned int start = 0;
936
937 switch (nb) {
938 case 4:
939 start = offsetof(union data, x32.low32);
940 break;
941 case 2:
942 start = offsetof(union data, x16.low16);
943 break;
944 }
945
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100946 data.ll = 0;
947 ret = 0;
Anton Blanchard835e2062013-09-23 12:04:49 +1000948 p = (unsigned long)addr;
949
950 for (i = 0; i < nb; i++)
951 ret |= __get_user_inatomic(data.v[start + i],
952 SWIZ_PTR(p++));
953
954 if (unlikely(ret))
955 return -EFAULT;
956
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000957 } else if (flags & F) {
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000958 data.ll = current->thread.TS_FPR(reg);
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000959 if (flags & S) {
960 /* Single-precision FP store requires conversion... */
961#ifdef CONFIG_PPC_FPU
962 preempt_disable();
963 enable_kernel_fp();
Anton Blanchardc3244962013-09-23 12:04:47 +1000964 cvt_df(&data.dd, (float *)&data.x32.low32);
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000965 preempt_enable();
966#else
967 return 0;
968#endif
969 }
970 } else
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100971 data.ll = regs->gpr[reg];
972
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000973 if (flags & SW) {
974 switch (nb) {
975 case 8:
Anton Blanchardf6261902013-09-23 12:04:46 +1000976 data.ll = swab64(data.ll);
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000977 break;
978 case 4:
Anton Blanchardf6261902013-09-23 12:04:46 +1000979 data.x32.low32 = swab32(data.x32.low32);
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000980 break;
981 case 2:
Anton Blanchardf6261902013-09-23 12:04:46 +1000982 data.x16.low16 = swab16(data.x16.low16);
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000983 break;
984 }
985 }
986
987 /* Perform other misc operations like sign extension
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100988 * or floating point single precision conversion
989 */
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000990 switch (flags & ~(U|SW)) {
Paul Mackerrasc6d42672007-08-10 14:07:38 +1000991 case LD+SE: /* sign extending integer loads */
992 case LD+F+SE: /* sign extend for lfiwax */
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100993 if ( nb == 2 )
994 data.ll = data.x16.low16;
995 else /* nb must be 4 */
996 data.ll = data.x32.low32;
997 break;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100998
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000999 /* Single-precision FP load requires conversion... */
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001000 case LD+F+S:
1001#ifdef CONFIG_PPC_FPU
1002 preempt_disable();
1003 enable_kernel_fp();
Anton Blanchardc3244962013-09-23 12:04:47 +10001004 cvt_fd((float *)&data.x32.low32, &data.dd);
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001005 preempt_enable();
1006#else
1007 return 0;
1008#endif
1009 break;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001010 }
1011
1012 /* Store result to memory or update registers */
1013 if (flags & ST) {
Anton Blanchard835e2062013-09-23 12:04:49 +10001014 unsigned int start = 0;
1015
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001016 switch (nb) {
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001017 case 4:
Anton Blanchard835e2062013-09-23 12:04:49 +10001018 start = offsetof(union data, x32.low32);
1019 break;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001020 case 2:
Anton Blanchard835e2062013-09-23 12:04:49 +10001021 start = offsetof(union data, x16.low16);
1022 break;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001023 }
Anton Blanchard835e2062013-09-23 12:04:49 +10001024
1025 ret = 0;
1026 p = (unsigned long)addr;
1027
1028 for (i = 0; i < nb; i++)
1029 ret |= __put_user_inatomic(data.v[start + i],
1030 SWIZ_PTR(p++));
1031
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001032 if (unlikely(ret))
1033 return -EFAULT;
1034 } else if (flags & F)
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001035 current->thread.TS_FPR(reg) = data.ll;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11001036 else
1037 regs->gpr[reg] = data.ll;
1038
1039 /* Update RA as needed */
1040 if (flags & U)
1041 regs->gpr[areg] = regs->dar;
1042
1043 return 1;
1044}