]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/powerpc/kernel/align.c
Merge remote-tracking branch 'net-next/master'
[karo-tx-linux.git] / arch / powerpc / kernel / align.c
1 /* 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>
24 #include <asm/cache.h>
25 #include <asm/cputable.h>
26 #include <asm/emulated_ops.h>
27 #include <asm/switch_to.h>
28
29 struct 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
39 /* Bits in the flags field */
40 #define LD      0       /* load */
41 #define ST      1       /* store */
42 #define SE      2       /* sign-extend value, or FP ld/st as word */
43 #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 */
49 #define HARD    0x80    /* string, stwcx. */
50 #define E4      0x40    /* SPE endianness is word */
51 #define E8      0x80    /* SPE endianness is double word */
52 #define SPLT    0x80    /* VSX SPLAT load */
53
54 /* DSISR bits reported for a DCBZ instruction: */
55 #define DCBZ    0x5f    /* 8xx/82xx dcbz faults when cache not enabled */
56
57 /*
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  */
63 static 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 */
92         { 16, LD+F },           /* 00 1 1100: lfdp */
93         INVALID,                /* 00 1 1101 */
94         { 16, ST+F },           /* 00 1 1110: stfdp */
95         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 */
172         { 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 */
176         { 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 */
189         { 4, LD+F },            /* 11 1 1101: lfiwzx */
190         INVALID,                /* 11 1 1110 */
191         INVALID,                /* 11 1 1111 */
192 };
193
194 /*
195  * Create a DSISR value from the instruction
196  */
197 static 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  */
232 static 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)
246                 if (__put_user_inatomic(0, p+i))
247                         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
263 #define SWIZ_PTR(p)             ((unsigned char __user *)((p) ^ swiz))
264
265 #ifdef __BIG_ENDIAN__
266 static int emulate_multiple(struct pt_regs *regs, unsigned char __user *addr,
267                             unsigned int reg, unsigned int nb,
268                             unsigned int flags, unsigned int instr,
269                             unsigned long swiz)
270 {
271         unsigned long *rptr;
272         unsigned int nb0, i, bswiz;
273         unsigned long p;
274
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 {
292                         unsigned long pc = regs->nip ^ (swiz & 4);
293
294                         if (__get_user_inatomic(instr,
295                                                 (unsigned int __user *)pc))
296                                 return -EFAULT;
297                         if (swiz == 0 && (flags & SW))
298                                 instr = cpu_to_le32(instr);
299                         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];
316         p = (unsigned long) addr;
317         bswiz = (flags & SW)? 3: 0;
318
319         if (!(flags & ST)) {
320                 /*
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
330                 for (i = 0; i < nb; ++i, ++p)
331                         if (__get_user_inatomic(REG_BYTE(rptr, i ^ bswiz),
332                                                 SWIZ_PTR(p)))
333                                 return -EFAULT;
334                 if (nb0 > 0) {
335                         rptr = &regs->gpr[0];
336                         addr += nb;
337                         for (i = 0; i < nb0; ++i, ++p)
338                                 if (__get_user_inatomic(REG_BYTE(rptr,
339                                                                  i ^ bswiz),
340                                                         SWIZ_PTR(p)))
341                                         return -EFAULT;
342                 }
343
344         } else {
345                 for (i = 0; i < nb; ++i, ++p)
346                         if (__put_user_inatomic(REG_BYTE(rptr, i ^ bswiz),
347                                                 SWIZ_PTR(p)))
348                                 return -EFAULT;
349                 if (nb0 > 0) {
350                         rptr = &regs->gpr[0];
351                         addr += nb;
352                         for (i = 0; i < nb0; ++i, ++p)
353                                 if (__put_user_inatomic(REG_BYTE(rptr,
354                                                                  i ^ bswiz),
355                                                         SWIZ_PTR(p)))
356                                         return -EFAULT;
357                 }
358         }
359         return 1;
360 }
361
362 /*
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  */
367 static int emulate_fp_pair(unsigned char __user *addr, unsigned int reg,
368                            unsigned int flags)
369 {
370         char *ptr0 = (char *) &current->thread.TS_FPR(reg);
371         char *ptr1 = (char *) &current->thread.TS_FPR(reg+1);
372         int i, ret, sw = 0;
373
374         if (!(flags & F))
375                 return 0;
376         if (reg & 1)
377                 return 0;       /* invalid form: FRS/FRT must be even */
378         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);
388                 }
389         }
390         if (ret)
391                 return -EFAULT;
392         return 1;       /* exception handled and fixed up */
393 }
394 #endif
395
396 #ifdef CONFIG_SPE
397
398 static 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  */
458 static int emulate_spe(struct pt_regs *regs, unsigned int reg,
459                        unsigned int instr)
460 {
461         int ret;
462         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:
584                         data.ll = swab64(data.ll);
585                         break;
586                 case E4:
587                         data.w[0] = swab32(data.w[0]);
588                         data.w[1] = swab32(data.w[1]);
589                         break;
590                 /* Its half word endian */
591                 default:
592                         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]);
596                         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 */
632
633 #ifdef CONFIG_VSX
634 /*
635  * Emulate VSX instructions...
636  */
637 static int emulate_vsx(unsigned char __user *addr, unsigned int reg,
638                        unsigned int areg, struct pt_regs *regs,
639                        unsigned int flags, unsigned int length,
640                        unsigned int elsize)
641 {
642         char *ptr;
643         unsigned long *lptr;
644         int ret = 0;
645         int sw = 0;
646         int i, j;
647
648         /* userland only */
649         if (unlikely(!user_mode(regs)))
650                 return 0;
651
652         flush_vsx_to_thread(current);
653
654         if (reg < 32)
655                 ptr = (char *) &current->thread.fp_state.fpr[reg][0];
656         else
657                 ptr = (char *) &current->thread.vr_state.vr[reg - 32];
658
659         lptr = (unsigned long *) ptr;
660
661 #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
677         if (flags & SW)
678                 sw = elsize-1;
679 #endif
680
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);
687                 }
688                 ptr  += elsize;
689 #ifdef __LITTLE_ENDIAN__
690                 addr -= elsize;
691 #else
692                 addr += elsize;
693 #endif
694         }
695
696 #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
704         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)
710                         lptr[VSX_LO] = lptr[VSX_HI];
711                 /* For 8 byte loads, zero the low 8 bytes */
712                 else if (!(flags & ST) && (8 == length))
713                         lptr[VSX_LO] = 0;
714         } else
715                 return -EFAULT;
716
717         return 1;
718 }
719 #endif
720
721 /*
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
729 int fix_alignment(struct pt_regs *regs)
730 {
731         unsigned int instr, nb, flags, instruction = 0;
732         unsigned int reg, areg;
733         unsigned int dsisr;
734         unsigned char __user *addr;
735         unsigned long p, swiz;
736         int ret, i;
737         union data {
738                 u64 ll;
739                 double dd;
740                 unsigned char v[8];
741                 struct {
742 #ifdef __LITTLE_ENDIAN__
743                         int      low32;
744                         unsigned hi32;
745 #else
746                         unsigned hi32;
747                         int      low32;
748 #endif
749                 } x32;
750                 struct {
751 #ifdef __LITTLE_ENDIAN__
752                         short         low16;
753                         unsigned char hi48[6];
754 #else
755                         unsigned char hi48[6];
756                         short         low16;
757 #endif
758                 } 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)) {
773                 unsigned long pc = regs->nip;
774
775                 if (cpu_has_feature(CPU_FTR_PPC_LE) && (regs->msr & MSR_LE))
776                         pc ^= 4;
777                 if (unlikely(__get_user_inatomic(instr,
778                                                  (unsigned int __user *)pc)))
779                         return -EFAULT;
780                 if (cpu_has_feature(CPU_FTR_REAL_LE) && (regs->msr & MSR_LE))
781                         instr = cpu_to_le32(instr);
782                 dsisr = make_dsisr(instr);
783                 instruction = instr;
784         }
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 */
789
790 #ifdef CONFIG_SPE
791         if ((instr >> 26) == 0x4) {
792                 PPC_WARN_ALIGNMENT(spe, regs);
793                 return emulate_spe(regs, reg, instr);
794         }
795 #endif
796
797         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
804         /* 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
814         /* Byteswap little endian loads and stores */
815         swiz = 0;
816         if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE)) {
817                 flags ^= SW;
818 #ifdef __BIG_ENDIAN__
819                 /*
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;
831 #endif
832         }
833
834         /* DAR has the operand effective address */
835         addr = (unsigned char __user *)regs->dar;
836
837 #ifdef CONFIG_VSX
838         if ((instruction & 0xfc00003e) == 0x7c000018) {
839                 unsigned int elsize;
840
841                 /* Additional register addressing bit (64 VSX vs 32 FPR/GPR) */
842                 reg |= (instruction & 0x1) << 5;
843                 /* Simple inline decoder instead of a table */
844                 /* VSX has only 8 and 16 byte memory accesses */
845                 nb = 8;
846                 if (instruction & 0x200)
847                         nb = 16;
848
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
855                 flags = 0;
856                 if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE))
857                         flags |= SW;
858                 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                 }
867                 PPC_WARN_ALIGNMENT(vsx, regs);
868                 return emulate_vsx(addr, reg, areg, regs, flags, nb, elsize);
869         }
870 #endif
871         /* 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          */
874         if (instr == DCBZ) {
875                 PPC_WARN_ALIGNMENT(dcbz, regs);
876                 return emulate_dcbz(regs, addr);
877         }
878         if (unlikely(nb == 0))
879                 return 0;
880
881         /* Load/Store Multiple instructions are handled in their own
882          * function
883          */
884         if (flags & M) {
885 #ifdef __BIG_ENDIAN__
886                 PPC_WARN_ALIGNMENT(multiple, regs);
887                 return emulate_multiple(regs, addr, reg, nb,
888                                         flags, instr, swiz);
889 #else
890                 return -EFAULT;
891 #endif
892         }
893
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
908         /* Special case for 16-byte FP loads and stores */
909         if (nb == 16) {
910 #ifdef __BIG_ENDIAN__
911                 PPC_WARN_ALIGNMENT(fp_pair, regs);
912                 return emulate_fp_pair(addr, reg, flags);
913 #else
914                 return -EFAULT;
915 #endif
916         }
917
918         PPC_WARN_ALIGNMENT(unaligned, regs);
919
920         /* If we are loading, get the data from user space, else
921          * get it from register values
922          */
923         if (!(flags & ST)) {
924                 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
935                 data.ll = 0;
936                 ret = 0;
937                 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
946         } else if (flags & F) {
947                 data.ll = current->thread.TS_FPR(reg);
948                 if (flags & S) {
949                         /* Single-precision FP store requires conversion... */
950 #ifdef CONFIG_PPC_FPU
951                         preempt_disable();
952                         enable_kernel_fp();
953                         cvt_df(&data.dd, (float *)&data.x32.low32);
954                         preempt_enable();
955 #else
956                         return 0;
957 #endif
958                 }
959         } else
960                 data.ll = regs->gpr[reg];
961
962         if (flags & SW) {
963                 switch (nb) {
964                 case 8:
965                         data.ll = swab64(data.ll);
966                         break;
967                 case 4:
968                         data.x32.low32 = swab32(data.x32.low32);
969                         break;
970                 case 2:
971                         data.x16.low16 = swab16(data.x16.low16);
972                         break;
973                 }
974         }
975
976         /* Perform other misc operations like sign extension
977          * or floating point single precision conversion
978          */
979         switch (flags & ~(U|SW)) {
980         case LD+SE:     /* sign extending integer loads */
981         case LD+F+SE:   /* sign extend for lfiwax */
982                 if ( nb == 2 )
983                         data.ll = data.x16.low16;
984                 else    /* nb must be 4 */
985                         data.ll = data.x32.low32;
986                 break;
987
988         /* Single-precision FP load requires conversion... */
989         case LD+F+S:
990 #ifdef CONFIG_PPC_FPU
991                 preempt_disable();
992                 enable_kernel_fp();
993                 cvt_fd((float *)&data.x32.low32, &data.dd);
994                 preempt_enable();
995 #else
996                 return 0;
997 #endif
998                 break;
999         }
1000
1001         /* Store result to memory or update registers */
1002         if (flags & ST) {
1003                 unsigned int start = 0;
1004
1005                 switch (nb) {
1006                 case 4:
1007                         start = offsetof(union data, x32.low32);
1008                         break;
1009                 case 2:
1010                         start = offsetof(union data, x16.low16);
1011                         break;
1012                 }
1013
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
1021                 if (unlikely(ret))
1022                         return -EFAULT;
1023         } else if (flags & F)
1024                 current->thread.TS_FPR(reg) = data.ll;
1025         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 }