]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/powerpc/kernel/align.c
powerpc: Alignment handler shouldn't access VSX registers with TS_FPR
[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 static int emulate_multiple(struct pt_regs *regs, unsigned char __user *addr,
266                             unsigned int reg, unsigned int nb,
267                             unsigned int flags, unsigned int instr,
268                             unsigned long swiz)
269 {
270         unsigned long *rptr;
271         unsigned int nb0, i, bswiz;
272         unsigned long p;
273
274         /*
275          * We do not try to emulate 8 bytes multiple as they aren't really
276          * available in our operating environments and we don't try to
277          * emulate multiples operations in kernel land as they should never
278          * be used/generated there at least not on unaligned boundaries
279          */
280         if (unlikely((nb > 4) || !user_mode(regs)))
281                 return 0;
282
283         /* lmw, stmw, lswi/x, stswi/x */
284         nb0 = 0;
285         if (flags & HARD) {
286                 if (flags & SX) {
287                         nb = regs->xer & 127;
288                         if (nb == 0)
289                                 return 1;
290                 } else {
291                         unsigned long pc = regs->nip ^ (swiz & 4);
292
293                         if (__get_user_inatomic(instr,
294                                                 (unsigned int __user *)pc))
295                                 return -EFAULT;
296                         if (swiz == 0 && (flags & SW))
297                                 instr = cpu_to_le32(instr);
298                         nb = (instr >> 11) & 0x1f;
299                         if (nb == 0)
300                                 nb = 32;
301                 }
302                 if (nb + reg * 4 > 128) {
303                         nb0 = nb + reg * 4 - 128;
304                         nb = 128 - reg * 4;
305                 }
306         } else {
307                 /* lwm, stmw */
308                 nb = (32 - reg) * 4;
309         }
310
311         if (!access_ok((flags & ST ? VERIFY_WRITE: VERIFY_READ), addr, nb+nb0))
312                 return -EFAULT; /* bad address */
313
314         rptr = &regs->gpr[reg];
315         p = (unsigned long) addr;
316         bswiz = (flags & SW)? 3: 0;
317
318         if (!(flags & ST)) {
319                 /*
320                  * This zeroes the top 4 bytes of the affected registers
321                  * in 64-bit mode, and also zeroes out any remaining
322                  * bytes of the last register for lsw*.
323                  */
324                 memset(rptr, 0, ((nb + 3) / 4) * sizeof(unsigned long));
325                 if (nb0 > 0)
326                         memset(&regs->gpr[0], 0,
327                                ((nb0 + 3) / 4) * sizeof(unsigned long));
328
329                 for (i = 0; i < nb; ++i, ++p)
330                         if (__get_user_inatomic(REG_BYTE(rptr, i ^ bswiz),
331                                                 SWIZ_PTR(p)))
332                                 return -EFAULT;
333                 if (nb0 > 0) {
334                         rptr = &regs->gpr[0];
335                         addr += nb;
336                         for (i = 0; i < nb0; ++i, ++p)
337                                 if (__get_user_inatomic(REG_BYTE(rptr,
338                                                                  i ^ bswiz),
339                                                         SWIZ_PTR(p)))
340                                         return -EFAULT;
341                 }
342
343         } else {
344                 for (i = 0; i < nb; ++i, ++p)
345                         if (__put_user_inatomic(REG_BYTE(rptr, i ^ bswiz),
346                                                 SWIZ_PTR(p)))
347                                 return -EFAULT;
348                 if (nb0 > 0) {
349                         rptr = &regs->gpr[0];
350                         addr += nb;
351                         for (i = 0; i < nb0; ++i, ++p)
352                                 if (__put_user_inatomic(REG_BYTE(rptr,
353                                                                  i ^ bswiz),
354                                                         SWIZ_PTR(p)))
355                                         return -EFAULT;
356                 }
357         }
358         return 1;
359 }
360
361 /*
362  * Emulate floating-point pair loads and stores.
363  * Only POWER6 has these instructions, and it does true little-endian,
364  * so we don't need the address swizzling.
365  */
366 static int emulate_fp_pair(unsigned char __user *addr, unsigned int reg,
367                            unsigned int flags)
368 {
369         char *ptr0 = (char *) &current->thread.TS_FPR(reg);
370         char *ptr1 = (char *) &current->thread.TS_FPR(reg+1);
371         int i, ret, sw = 0;
372
373         if (!(flags & F))
374                 return 0;
375         if (reg & 1)
376                 return 0;       /* invalid form: FRS/FRT must be even */
377         if (flags & SW)
378                 sw = 7;
379         ret = 0;
380         for (i = 0; i < 8; ++i) {
381                 if (!(flags & ST)) {
382                         ret |= __get_user(ptr0[i^sw], addr + i);
383                         ret |= __get_user(ptr1[i^sw], addr + i + 8);
384                 } else {
385                         ret |= __put_user(ptr0[i^sw], addr + i);
386                         ret |= __put_user(ptr1[i^sw], addr + i + 8);
387                 }
388         }
389         if (ret)
390                 return -EFAULT;
391         return 1;       /* exception handled and fixed up */
392 }
393
394 #ifdef CONFIG_SPE
395
396 static struct aligninfo spe_aligninfo[32] = {
397         { 8, LD+E8 },           /* 0 00 00: evldd[x] */
398         { 8, LD+E4 },           /* 0 00 01: evldw[x] */
399         { 8, LD },              /* 0 00 10: evldh[x] */
400         INVALID,                /* 0 00 11 */
401         { 2, LD },              /* 0 01 00: evlhhesplat[x] */
402         INVALID,                /* 0 01 01 */
403         { 2, LD },              /* 0 01 10: evlhhousplat[x] */
404         { 2, LD+SE },           /* 0 01 11: evlhhossplat[x] */
405         { 4, LD },              /* 0 10 00: evlwhe[x] */
406         INVALID,                /* 0 10 01 */
407         { 4, LD },              /* 0 10 10: evlwhou[x] */
408         { 4, LD+SE },           /* 0 10 11: evlwhos[x] */
409         { 4, LD+E4 },           /* 0 11 00: evlwwsplat[x] */
410         INVALID,                /* 0 11 01 */
411         { 4, LD },              /* 0 11 10: evlwhsplat[x] */
412         INVALID,                /* 0 11 11 */
413
414         { 8, ST+E8 },           /* 1 00 00: evstdd[x] */
415         { 8, ST+E4 },           /* 1 00 01: evstdw[x] */
416         { 8, ST },              /* 1 00 10: evstdh[x] */
417         INVALID,                /* 1 00 11 */
418         INVALID,                /* 1 01 00 */
419         INVALID,                /* 1 01 01 */
420         INVALID,                /* 1 01 10 */
421         INVALID,                /* 1 01 11 */
422         { 4, ST },              /* 1 10 00: evstwhe[x] */
423         INVALID,                /* 1 10 01 */
424         { 4, ST },              /* 1 10 10: evstwho[x] */
425         INVALID,                /* 1 10 11 */
426         { 4, ST+E4 },           /* 1 11 00: evstwwe[x] */
427         INVALID,                /* 1 11 01 */
428         { 4, ST+E4 },           /* 1 11 10: evstwwo[x] */
429         INVALID,                /* 1 11 11 */
430 };
431
432 #define EVLDD           0x00
433 #define EVLDW           0x01
434 #define EVLDH           0x02
435 #define EVLHHESPLAT     0x04
436 #define EVLHHOUSPLAT    0x06
437 #define EVLHHOSSPLAT    0x07
438 #define EVLWHE          0x08
439 #define EVLWHOU         0x0A
440 #define EVLWHOS         0x0B
441 #define EVLWWSPLAT      0x0C
442 #define EVLWHSPLAT      0x0E
443 #define EVSTDD          0x10
444 #define EVSTDW          0x11
445 #define EVSTDH          0x12
446 #define EVSTWHE         0x18
447 #define EVSTWHO         0x1A
448 #define EVSTWWE         0x1C
449 #define EVSTWWO         0x1E
450
451 /*
452  * Emulate SPE loads and stores.
453  * Only Book-E has these instructions, and it does true little-endian,
454  * so we don't need the address swizzling.
455  */
456 static int emulate_spe(struct pt_regs *regs, unsigned int reg,
457                        unsigned int instr)
458 {
459         int ret;
460         union {
461                 u64 ll;
462                 u32 w[2];
463                 u16 h[4];
464                 u8 v[8];
465         } data, temp;
466         unsigned char __user *p, *addr;
467         unsigned long *evr = &current->thread.evr[reg];
468         unsigned int nb, flags;
469
470         instr = (instr >> 1) & 0x1f;
471
472         /* DAR has the operand effective address */
473         addr = (unsigned char __user *)regs->dar;
474
475         nb = spe_aligninfo[instr].len;
476         flags = spe_aligninfo[instr].flags;
477
478         /* Verify the address of the operand */
479         if (unlikely(user_mode(regs) &&
480                      !access_ok((flags & ST ? VERIFY_WRITE : VERIFY_READ),
481                                 addr, nb)))
482                 return -EFAULT;
483
484         /* userland only */
485         if (unlikely(!user_mode(regs)))
486                 return 0;
487
488         flush_spe_to_thread(current);
489
490         /* If we are loading, get the data from user space, else
491          * get it from register values
492          */
493         if (flags & ST) {
494                 data.ll = 0;
495                 switch (instr) {
496                 case EVSTDD:
497                 case EVSTDW:
498                 case EVSTDH:
499                         data.w[0] = *evr;
500                         data.w[1] = regs->gpr[reg];
501                         break;
502                 case EVSTWHE:
503                         data.h[2] = *evr >> 16;
504                         data.h[3] = regs->gpr[reg] >> 16;
505                         break;
506                 case EVSTWHO:
507                         data.h[2] = *evr & 0xffff;
508                         data.h[3] = regs->gpr[reg] & 0xffff;
509                         break;
510                 case EVSTWWE:
511                         data.w[1] = *evr;
512                         break;
513                 case EVSTWWO:
514                         data.w[1] = regs->gpr[reg];
515                         break;
516                 default:
517                         return -EINVAL;
518                 }
519         } else {
520                 temp.ll = data.ll = 0;
521                 ret = 0;
522                 p = addr;
523
524                 switch (nb) {
525                 case 8:
526                         ret |= __get_user_inatomic(temp.v[0], p++);
527                         ret |= __get_user_inatomic(temp.v[1], p++);
528                         ret |= __get_user_inatomic(temp.v[2], p++);
529                         ret |= __get_user_inatomic(temp.v[3], p++);
530                 case 4:
531                         ret |= __get_user_inatomic(temp.v[4], p++);
532                         ret |= __get_user_inatomic(temp.v[5], p++);
533                 case 2:
534                         ret |= __get_user_inatomic(temp.v[6], p++);
535                         ret |= __get_user_inatomic(temp.v[7], p++);
536                         if (unlikely(ret))
537                                 return -EFAULT;
538                 }
539
540                 switch (instr) {
541                 case EVLDD:
542                 case EVLDW:
543                 case EVLDH:
544                         data.ll = temp.ll;
545                         break;
546                 case EVLHHESPLAT:
547                         data.h[0] = temp.h[3];
548                         data.h[2] = temp.h[3];
549                         break;
550                 case EVLHHOUSPLAT:
551                 case EVLHHOSSPLAT:
552                         data.h[1] = temp.h[3];
553                         data.h[3] = temp.h[3];
554                         break;
555                 case EVLWHE:
556                         data.h[0] = temp.h[2];
557                         data.h[2] = temp.h[3];
558                         break;
559                 case EVLWHOU:
560                 case EVLWHOS:
561                         data.h[1] = temp.h[2];
562                         data.h[3] = temp.h[3];
563                         break;
564                 case EVLWWSPLAT:
565                         data.w[0] = temp.w[1];
566                         data.w[1] = temp.w[1];
567                         break;
568                 case EVLWHSPLAT:
569                         data.h[0] = temp.h[2];
570                         data.h[1] = temp.h[2];
571                         data.h[2] = temp.h[3];
572                         data.h[3] = temp.h[3];
573                         break;
574                 default:
575                         return -EINVAL;
576                 }
577         }
578
579         if (flags & SW) {
580                 switch (flags & 0xf0) {
581                 case E8:
582                         data.ll = swab64(data.ll);
583                         break;
584                 case E4:
585                         data.w[0] = swab32(data.w[0]);
586                         data.w[1] = swab32(data.w[1]);
587                         break;
588                 /* Its half word endian */
589                 default:
590                         data.h[0] = swab16(data.h[0]);
591                         data.h[1] = swab16(data.h[1]);
592                         data.h[2] = swab16(data.h[2]);
593                         data.h[3] = swab16(data.h[3]);
594                         break;
595                 }
596         }
597
598         if (flags & SE) {
599                 data.w[0] = (s16)data.h[1];
600                 data.w[1] = (s16)data.h[3];
601         }
602
603         /* Store result to memory or update registers */
604         if (flags & ST) {
605                 ret = 0;
606                 p = addr;
607                 switch (nb) {
608                 case 8:
609                         ret |= __put_user_inatomic(data.v[0], p++);
610                         ret |= __put_user_inatomic(data.v[1], p++);
611                         ret |= __put_user_inatomic(data.v[2], p++);
612                         ret |= __put_user_inatomic(data.v[3], p++);
613                 case 4:
614                         ret |= __put_user_inatomic(data.v[4], p++);
615                         ret |= __put_user_inatomic(data.v[5], p++);
616                 case 2:
617                         ret |= __put_user_inatomic(data.v[6], p++);
618                         ret |= __put_user_inatomic(data.v[7], p++);
619                 }
620                 if (unlikely(ret))
621                         return -EFAULT;
622         } else {
623                 *evr = data.w[0];
624                 regs->gpr[reg] = data.w[1];
625         }
626
627         return 1;
628 }
629 #endif /* CONFIG_SPE */
630
631 #ifdef CONFIG_VSX
632 /*
633  * Emulate VSX instructions...
634  */
635 static int emulate_vsx(unsigned char __user *addr, unsigned int reg,
636                        unsigned int areg, struct pt_regs *regs,
637                        unsigned int flags, unsigned int length,
638                        unsigned int elsize)
639 {
640         char *ptr;
641         unsigned long *lptr;
642         int ret = 0;
643         int sw = 0;
644         int i, j;
645
646         /* userland only */
647         if (unlikely(!user_mode(regs)))
648                 return 0;
649
650         flush_vsx_to_thread(current);
651
652         if (reg < 32)
653                 ptr = (char *) &current->thread.fpr[reg][0];
654         else
655                 ptr = (char *) &current->thread.vr[reg - 32];
656
657         lptr = (unsigned long *) ptr;
658
659         if (flags & SW)
660                 sw = elsize-1;
661
662         for (j = 0; j < length; j += elsize) {
663                 for (i = 0; i < elsize; ++i) {
664                         if (flags & ST)
665                                 ret |= __put_user(ptr[i^sw], addr + i);
666                         else
667                                 ret |= __get_user(ptr[i^sw], addr + i);
668                 }
669                 ptr  += elsize;
670                 addr += elsize;
671         }
672
673         if (!ret) {
674                 if (flags & U)
675                         regs->gpr[areg] = regs->dar;
676
677                 /* Splat load copies the same data to top and bottom 8 bytes */
678                 if (flags & SPLT)
679                         lptr[1] = lptr[0];
680                 /* For 8 byte loads, zero the top 8 bytes */
681                 else if (!(flags & ST) && (8 == length))
682                         lptr[1] = 0;
683         } else
684                 return -EFAULT;
685
686         return 1;
687 }
688 #endif
689
690 /*
691  * Called on alignment exception. Attempts to fixup
692  *
693  * Return 1 on success
694  * Return 0 if unable to handle the interrupt
695  * Return -EFAULT if data address is bad
696  */
697
698 int fix_alignment(struct pt_regs *regs)
699 {
700         unsigned int instr, nb, flags, instruction = 0;
701         unsigned int reg, areg;
702         unsigned int dsisr;
703         unsigned char __user *addr;
704         unsigned long p, swiz;
705         int ret;
706         union {
707                 u64 ll;
708                 double dd;
709                 unsigned char v[8];
710                 struct {
711                         unsigned hi32;
712                         int      low32;
713                 } x32;
714                 struct {
715                         unsigned char hi48[6];
716                         short         low16;
717                 } x16;
718         } data;
719
720         /*
721          * We require a complete register set, if not, then our assembly
722          * is broken
723          */
724         CHECK_FULL_REGS(regs);
725
726         dsisr = regs->dsisr;
727
728         /* Some processors don't provide us with a DSISR we can use here,
729          * let's make one up from the instruction
730          */
731         if (cpu_has_feature(CPU_FTR_NODSISRALIGN)) {
732                 unsigned long pc = regs->nip;
733
734                 if (cpu_has_feature(CPU_FTR_PPC_LE) && (regs->msr & MSR_LE))
735                         pc ^= 4;
736                 if (unlikely(__get_user_inatomic(instr,
737                                                  (unsigned int __user *)pc)))
738                         return -EFAULT;
739                 if (cpu_has_feature(CPU_FTR_REAL_LE) && (regs->msr & MSR_LE))
740                         instr = cpu_to_le32(instr);
741                 dsisr = make_dsisr(instr);
742                 instruction = instr;
743         }
744
745         /* extract the operation and registers from the dsisr */
746         reg = (dsisr >> 5) & 0x1f;      /* source/dest register */
747         areg = dsisr & 0x1f;            /* register to update */
748
749 #ifdef CONFIG_SPE
750         if ((instr >> 26) == 0x4) {
751                 PPC_WARN_ALIGNMENT(spe, regs);
752                 return emulate_spe(regs, reg, instr);
753         }
754 #endif
755
756         instr = (dsisr >> 10) & 0x7f;
757         instr |= (dsisr >> 13) & 0x60;
758
759         /* Lookup the operation in our table */
760         nb = aligninfo[instr].len;
761         flags = aligninfo[instr].flags;
762
763         /* ldbrx/stdbrx overlap lfs/stfs in the DSISR unfortunately */
764         if (IS_XFORM(instruction) && ((instruction >> 1) & 0x3ff) == 532) {
765                 nb = 8;
766                 flags = LD+SW;
767         } else if (IS_XFORM(instruction) &&
768                    ((instruction >> 1) & 0x3ff) == 660) {
769                 nb = 8;
770                 flags = ST+SW;
771         }
772
773         /* Byteswap little endian loads and stores */
774         swiz = 0;
775         if (regs->msr & MSR_LE) {
776                 flags ^= SW;
777                 /*
778                  * So-called "PowerPC little endian" mode works by
779                  * swizzling addresses rather than by actually doing
780                  * any byte-swapping.  To emulate this, we XOR each
781                  * byte address with 7.  We also byte-swap, because
782                  * the processor's address swizzling depends on the
783                  * operand size (it xors the address with 7 for bytes,
784                  * 6 for halfwords, 4 for words, 0 for doublewords) but
785                  * we will xor with 7 and load/store each byte separately.
786                  */
787                 if (cpu_has_feature(CPU_FTR_PPC_LE))
788                         swiz = 7;
789         }
790
791         /* DAR has the operand effective address */
792         addr = (unsigned char __user *)regs->dar;
793
794 #ifdef CONFIG_VSX
795         if ((instruction & 0xfc00003e) == 0x7c000018) {
796                 unsigned int elsize;
797
798                 /* Additional register addressing bit (64 VSX vs 32 FPR/GPR) */
799                 reg |= (instruction & 0x1) << 5;
800                 /* Simple inline decoder instead of a table */
801                 /* VSX has only 8 and 16 byte memory accesses */
802                 nb = 8;
803                 if (instruction & 0x200)
804                         nb = 16;
805
806                 /* Vector stores in little-endian mode swap individual
807                    elements, so process them separately */
808                 elsize = 4;
809                 if (instruction & 0x80)
810                         elsize = 8;
811
812                 flags = 0;
813                 if (regs->msr & MSR_LE)
814                         flags |= SW;
815                 if (instruction & 0x100)
816                         flags |= ST;
817                 if (instruction & 0x040)
818                         flags |= U;
819                 /* splat load needs a special decoder */
820                 if ((instruction & 0x400) == 0){
821                         flags |= SPLT;
822                         nb = 8;
823                 }
824                 PPC_WARN_ALIGNMENT(vsx, regs);
825                 return emulate_vsx(addr, reg, areg, regs, flags, nb, elsize);
826         }
827 #endif
828         /* A size of 0 indicates an instruction we don't support, with
829          * the exception of DCBZ which is handled as a special case here
830          */
831         if (instr == DCBZ) {
832                 PPC_WARN_ALIGNMENT(dcbz, regs);
833                 return emulate_dcbz(regs, addr);
834         }
835         if (unlikely(nb == 0))
836                 return 0;
837
838         /* Load/Store Multiple instructions are handled in their own
839          * function
840          */
841         if (flags & M) {
842                 PPC_WARN_ALIGNMENT(multiple, regs);
843                 return emulate_multiple(regs, addr, reg, nb,
844                                         flags, instr, swiz);
845         }
846
847         /* Verify the address of the operand */
848         if (unlikely(user_mode(regs) &&
849                      !access_ok((flags & ST ? VERIFY_WRITE : VERIFY_READ),
850                                 addr, nb)))
851                 return -EFAULT;
852
853         /* Force the fprs into the save area so we can reference them */
854         if (flags & F) {
855                 /* userland only */
856                 if (unlikely(!user_mode(regs)))
857                         return 0;
858                 flush_fp_to_thread(current);
859         }
860
861         /* Special case for 16-byte FP loads and stores */
862         if (nb == 16) {
863                 PPC_WARN_ALIGNMENT(fp_pair, regs);
864                 return emulate_fp_pair(addr, reg, flags);
865         }
866
867         PPC_WARN_ALIGNMENT(unaligned, regs);
868
869         /* If we are loading, get the data from user space, else
870          * get it from register values
871          */
872         if (!(flags & ST)) {
873                 data.ll = 0;
874                 ret = 0;
875                 p = (unsigned long) addr;
876                 switch (nb) {
877                 case 8:
878                         ret |= __get_user_inatomic(data.v[0], SWIZ_PTR(p++));
879                         ret |= __get_user_inatomic(data.v[1], SWIZ_PTR(p++));
880                         ret |= __get_user_inatomic(data.v[2], SWIZ_PTR(p++));
881                         ret |= __get_user_inatomic(data.v[3], SWIZ_PTR(p++));
882                 case 4:
883                         ret |= __get_user_inatomic(data.v[4], SWIZ_PTR(p++));
884                         ret |= __get_user_inatomic(data.v[5], SWIZ_PTR(p++));
885                 case 2:
886                         ret |= __get_user_inatomic(data.v[6], SWIZ_PTR(p++));
887                         ret |= __get_user_inatomic(data.v[7], SWIZ_PTR(p++));
888                         if (unlikely(ret))
889                                 return -EFAULT;
890                 }
891         } else if (flags & F) {
892                 data.dd = current->thread.TS_FPR(reg);
893                 if (flags & S) {
894                         /* Single-precision FP store requires conversion... */
895 #ifdef CONFIG_PPC_FPU
896                         preempt_disable();
897                         enable_kernel_fp();
898                         cvt_df(&data.dd, (float *)&data.x32.low32);
899                         preempt_enable();
900 #else
901                         return 0;
902 #endif
903                 }
904         } else
905                 data.ll = regs->gpr[reg];
906
907         if (flags & SW) {
908                 switch (nb) {
909                 case 8:
910                         data.ll = swab64(data.ll);
911                         break;
912                 case 4:
913                         data.x32.low32 = swab32(data.x32.low32);
914                         break;
915                 case 2:
916                         data.x16.low16 = swab16(data.x16.low16);
917                         break;
918                 }
919         }
920
921         /* Perform other misc operations like sign extension
922          * or floating point single precision conversion
923          */
924         switch (flags & ~(U|SW)) {
925         case LD+SE:     /* sign extending integer loads */
926         case LD+F+SE:   /* sign extend for lfiwax */
927                 if ( nb == 2 )
928                         data.ll = data.x16.low16;
929                 else    /* nb must be 4 */
930                         data.ll = data.x32.low32;
931                 break;
932
933         /* Single-precision FP load requires conversion... */
934         case LD+F+S:
935 #ifdef CONFIG_PPC_FPU
936                 preempt_disable();
937                 enable_kernel_fp();
938                 cvt_fd((float *)&data.x32.low32, &data.dd);
939                 preempt_enable();
940 #else
941                 return 0;
942 #endif
943                 break;
944         }
945
946         /* Store result to memory or update registers */
947         if (flags & ST) {
948                 ret = 0;
949                 p = (unsigned long) addr;
950                 switch (nb) {
951                 case 8:
952                         ret |= __put_user_inatomic(data.v[0], SWIZ_PTR(p++));
953                         ret |= __put_user_inatomic(data.v[1], SWIZ_PTR(p++));
954                         ret |= __put_user_inatomic(data.v[2], SWIZ_PTR(p++));
955                         ret |= __put_user_inatomic(data.v[3], SWIZ_PTR(p++));
956                 case 4:
957                         ret |= __put_user_inatomic(data.v[4], SWIZ_PTR(p++));
958                         ret |= __put_user_inatomic(data.v[5], SWIZ_PTR(p++));
959                 case 2:
960                         ret |= __put_user_inatomic(data.v[6], SWIZ_PTR(p++));
961                         ret |= __put_user_inatomic(data.v[7], SWIZ_PTR(p++));
962                 }
963                 if (unlikely(ret))
964                         return -EFAULT;
965         } else if (flags & F)
966                 current->thread.TS_FPR(reg) = data.dd;
967         else
968                 regs->gpr[reg] = data.ll;
969
970         /* Update RA as needed */
971         if (flags & U)
972                 regs->gpr[areg] = regs->dar;
973
974         return 1;
975 }