Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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> |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 24 | #include <asm/cache.h> |
| 25 | #include <asm/cputable.h> |
Geert Uytterhoeven | 80947e7 | 2009-05-18 02:10:05 +0000 | [diff] [blame] | 26 | #include <asm/emulated_ops.h> |
David Howells | ae3a197 | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 27 | #include <asm/switch_to.h> |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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 | |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 39 | /* Bits in the flags field */ |
| 40 | #define LD 0 /* load */ |
| 41 | #define ST 1 /* store */ |
Paul Mackerras | c6d4267 | 2007-08-10 14:07:38 +1000 | [diff] [blame] | 42 | #define SE 2 /* sign-extend value, or FP ld/st as word */ |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 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 */ |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 49 | #define HARD 0x80 /* string, stwcx. */ |
Kumar Gala | 26caeb2 | 2007-08-24 16:42:53 -0500 | [diff] [blame] | 50 | #define E4 0x40 /* SPE endianness is word */ |
| 51 | #define E8 0x80 /* SPE endianness is double word */ |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 52 | #define SPLT 0x80 /* VSX SPLAT load */ |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 53 | |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 54 | /* DSISR bits reported for a DCBZ instruction: */ |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 55 | #define DCBZ 0x5f /* 8xx/82xx dcbz faults when cache not enabled */ |
| 56 | |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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 */ |
Paul Mackerras | c6d4267 | 2007-08-10 14:07:38 +1000 | [diff] [blame] | 92 | { 16, LD+F }, /* 00 1 1100: lfdp */ |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 93 | INVALID, /* 00 1 1101 */ |
Paul Mackerras | c6d4267 | 2007-08-10 14:07:38 +1000 | [diff] [blame] | 94 | { 16, ST+F }, /* 00 1 1110: stfdp */ |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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 */ |
Paul Mackerras | c6d4267 | 2007-08-10 14:07:38 +1000 | [diff] [blame] | 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 */ |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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 */ |
Michael Neuling | 545bba1 | 2009-02-19 18:51:37 +0000 | [diff] [blame] | 189 | { 4, LD+F }, /* 11 1 1101: lfiwzx */ |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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) |
Benjamin Herrenschmidt | e4ee3891 | 2007-04-11 16:13:19 +1000 | [diff] [blame] | 246 | if (__put_user_inatomic(0, p+i)) |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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 | |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 263 | #define SWIZ_PTR(p) ((unsigned char __user *)((p) ^ swiz)) |
| 264 | |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 265 | #ifdef __BIG_ENDIAN__ |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 266 | static int emulate_multiple(struct pt_regs *regs, unsigned char __user *addr, |
| 267 | unsigned int reg, unsigned int nb, |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 268 | unsigned int flags, unsigned int instr, |
| 269 | unsigned long swiz) |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 270 | { |
| 271 | unsigned long *rptr; |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 272 | unsigned int nb0, i, bswiz; |
| 273 | unsigned long p; |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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 { |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 292 | unsigned long pc = regs->nip ^ (swiz & 4); |
| 293 | |
Benjamin Herrenschmidt | e4ee3891 | 2007-04-11 16:13:19 +1000 | [diff] [blame] | 294 | if (__get_user_inatomic(instr, |
| 295 | (unsigned int __user *)pc)) |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 296 | return -EFAULT; |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 297 | if (swiz == 0 && (flags & SW)) |
| 298 | instr = cpu_to_le32(instr); |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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 = ®s->gpr[reg]; |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 316 | p = (unsigned long) addr; |
| 317 | bswiz = (flags & SW)? 3: 0; |
| 318 | |
| 319 | if (!(flags & ST)) { |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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(®s->gpr[0], 0, |
| 328 | ((nb0 + 3) / 4) * sizeof(unsigned long)); |
| 329 | |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 330 | for (i = 0; i < nb; ++i, ++p) |
Benjamin Herrenschmidt | e4ee3891 | 2007-04-11 16:13:19 +1000 | [diff] [blame] | 331 | if (__get_user_inatomic(REG_BYTE(rptr, i ^ bswiz), |
| 332 | SWIZ_PTR(p))) |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 333 | return -EFAULT; |
| 334 | if (nb0 > 0) { |
| 335 | rptr = ®s->gpr[0]; |
| 336 | addr += nb; |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 337 | for (i = 0; i < nb0; ++i, ++p) |
Benjamin Herrenschmidt | e4ee3891 | 2007-04-11 16:13:19 +1000 | [diff] [blame] | 338 | if (__get_user_inatomic(REG_BYTE(rptr, |
| 339 | i ^ bswiz), |
| 340 | SWIZ_PTR(p))) |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 341 | return -EFAULT; |
| 342 | } |
| 343 | |
| 344 | } else { |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 345 | for (i = 0; i < nb; ++i, ++p) |
Benjamin Herrenschmidt | e4ee3891 | 2007-04-11 16:13:19 +1000 | [diff] [blame] | 346 | if (__put_user_inatomic(REG_BYTE(rptr, i ^ bswiz), |
| 347 | SWIZ_PTR(p))) |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 348 | return -EFAULT; |
| 349 | if (nb0 > 0) { |
| 350 | rptr = ®s->gpr[0]; |
| 351 | addr += nb; |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 352 | for (i = 0; i < nb0; ++i, ++p) |
Benjamin Herrenschmidt | e4ee3891 | 2007-04-11 16:13:19 +1000 | [diff] [blame] | 353 | if (__put_user_inatomic(REG_BYTE(rptr, |
| 354 | i ^ bswiz), |
| 355 | SWIZ_PTR(p))) |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 356 | return -EFAULT; |
| 357 | } |
| 358 | } |
| 359 | return 1; |
| 360 | } |
| 361 | |
Paul Mackerras | c6d4267 | 2007-08-10 14:07:38 +1000 | [diff] [blame] | 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 | */ |
Michael Neuling | b887ec6 | 2008-07-08 18:53:03 +1000 | [diff] [blame] | 367 | static int emulate_fp_pair(unsigned char __user *addr, unsigned int reg, |
| 368 | unsigned int flags) |
Paul Mackerras | c6d4267 | 2007-08-10 14:07:38 +1000 | [diff] [blame] | 369 | { |
Michael Neuling | 553631e | 2009-02-19 18:52:20 +0000 | [diff] [blame] | 370 | char *ptr0 = (char *) ¤t->thread.TS_FPR(reg); |
| 371 | char *ptr1 = (char *) ¤t->thread.TS_FPR(reg+1); |
| 372 | int i, ret, sw = 0; |
Paul Mackerras | c6d4267 | 2007-08-10 14:07:38 +1000 | [diff] [blame] | 373 | |
| 374 | if (!(flags & F)) |
| 375 | return 0; |
| 376 | if (reg & 1) |
| 377 | return 0; /* invalid form: FRS/FRT must be even */ |
Michael Neuling | 553631e | 2009-02-19 18:52:20 +0000 | [diff] [blame] | 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); |
Paul Mackerras | c6d4267 | 2007-08-10 14:07:38 +1000 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | if (ret) |
| 391 | return -EFAULT; |
| 392 | return 1; /* exception handled and fixed up */ |
| 393 | } |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 394 | #endif |
Paul Mackerras | c6d4267 | 2007-08-10 14:07:38 +1000 | [diff] [blame] | 395 | |
Kumar Gala | 26caeb2 | 2007-08-24 16:42:53 -0500 | [diff] [blame] | 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 | { |
Anton Blanchard | f626190 | 2013-09-23 12:04:46 +1000 | [diff] [blame] | 461 | int ret; |
Kumar Gala | 26caeb2 | 2007-08-24 16:42:53 -0500 | [diff] [blame] | 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 = ¤t->thread.evr[reg]; |
| 470 | unsigned int nb, flags; |
| 471 | |
| 472 | instr = (instr >> 1) & 0x1f; |
| 473 | |
| 474 | /* DAR has the operand effective address */ |
| 475 | addr = (unsigned char __user *)regs->dar; |
| 476 | |
| 477 | nb = spe_aligninfo[instr].len; |
| 478 | flags = spe_aligninfo[instr].flags; |
| 479 | |
| 480 | /* Verify the address of the operand */ |
| 481 | if (unlikely(user_mode(regs) && |
| 482 | !access_ok((flags & ST ? VERIFY_WRITE : VERIFY_READ), |
| 483 | addr, nb))) |
| 484 | return -EFAULT; |
| 485 | |
| 486 | /* userland only */ |
| 487 | if (unlikely(!user_mode(regs))) |
| 488 | return 0; |
| 489 | |
| 490 | flush_spe_to_thread(current); |
| 491 | |
| 492 | /* If we are loading, get the data from user space, else |
| 493 | * get it from register values |
| 494 | */ |
| 495 | if (flags & ST) { |
| 496 | data.ll = 0; |
| 497 | switch (instr) { |
| 498 | case EVSTDD: |
| 499 | case EVSTDW: |
| 500 | case EVSTDH: |
| 501 | data.w[0] = *evr; |
| 502 | data.w[1] = regs->gpr[reg]; |
| 503 | break; |
| 504 | case EVSTWHE: |
| 505 | data.h[2] = *evr >> 16; |
| 506 | data.h[3] = regs->gpr[reg] >> 16; |
| 507 | break; |
| 508 | case EVSTWHO: |
| 509 | data.h[2] = *evr & 0xffff; |
| 510 | data.h[3] = regs->gpr[reg] & 0xffff; |
| 511 | break; |
| 512 | case EVSTWWE: |
| 513 | data.w[1] = *evr; |
| 514 | break; |
| 515 | case EVSTWWO: |
| 516 | data.w[1] = regs->gpr[reg]; |
| 517 | break; |
| 518 | default: |
| 519 | return -EINVAL; |
| 520 | } |
| 521 | } else { |
| 522 | temp.ll = data.ll = 0; |
| 523 | ret = 0; |
| 524 | p = addr; |
| 525 | |
| 526 | switch (nb) { |
| 527 | case 8: |
| 528 | ret |= __get_user_inatomic(temp.v[0], p++); |
| 529 | ret |= __get_user_inatomic(temp.v[1], p++); |
| 530 | ret |= __get_user_inatomic(temp.v[2], p++); |
| 531 | ret |= __get_user_inatomic(temp.v[3], p++); |
| 532 | case 4: |
| 533 | ret |= __get_user_inatomic(temp.v[4], p++); |
| 534 | ret |= __get_user_inatomic(temp.v[5], p++); |
| 535 | case 2: |
| 536 | ret |= __get_user_inatomic(temp.v[6], p++); |
| 537 | ret |= __get_user_inatomic(temp.v[7], p++); |
| 538 | if (unlikely(ret)) |
| 539 | return -EFAULT; |
| 540 | } |
| 541 | |
| 542 | switch (instr) { |
| 543 | case EVLDD: |
| 544 | case EVLDW: |
| 545 | case EVLDH: |
| 546 | data.ll = temp.ll; |
| 547 | break; |
| 548 | case EVLHHESPLAT: |
| 549 | data.h[0] = temp.h[3]; |
| 550 | data.h[2] = temp.h[3]; |
| 551 | break; |
| 552 | case EVLHHOUSPLAT: |
| 553 | case EVLHHOSSPLAT: |
| 554 | data.h[1] = temp.h[3]; |
| 555 | data.h[3] = temp.h[3]; |
| 556 | break; |
| 557 | case EVLWHE: |
| 558 | data.h[0] = temp.h[2]; |
| 559 | data.h[2] = temp.h[3]; |
| 560 | break; |
| 561 | case EVLWHOU: |
| 562 | case EVLWHOS: |
| 563 | data.h[1] = temp.h[2]; |
| 564 | data.h[3] = temp.h[3]; |
| 565 | break; |
| 566 | case EVLWWSPLAT: |
| 567 | data.w[0] = temp.w[1]; |
| 568 | data.w[1] = temp.w[1]; |
| 569 | break; |
| 570 | case EVLWHSPLAT: |
| 571 | data.h[0] = temp.h[2]; |
| 572 | data.h[1] = temp.h[2]; |
| 573 | data.h[2] = temp.h[3]; |
| 574 | data.h[3] = temp.h[3]; |
| 575 | break; |
| 576 | default: |
| 577 | return -EINVAL; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | if (flags & SW) { |
| 582 | switch (flags & 0xf0) { |
| 583 | case E8: |
Anton Blanchard | f626190 | 2013-09-23 12:04:46 +1000 | [diff] [blame] | 584 | data.ll = swab64(data.ll); |
Kumar Gala | 26caeb2 | 2007-08-24 16:42:53 -0500 | [diff] [blame] | 585 | break; |
| 586 | case E4: |
Anton Blanchard | f626190 | 2013-09-23 12:04:46 +1000 | [diff] [blame] | 587 | data.w[0] = swab32(data.w[0]); |
| 588 | data.w[1] = swab32(data.w[1]); |
Kumar Gala | 26caeb2 | 2007-08-24 16:42:53 -0500 | [diff] [blame] | 589 | break; |
| 590 | /* Its half word endian */ |
| 591 | default: |
Anton Blanchard | f626190 | 2013-09-23 12:04:46 +1000 | [diff] [blame] | 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]); |
Kumar Gala | 26caeb2 | 2007-08-24 16:42:53 -0500 | [diff] [blame] | 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 */ |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 632 | |
Anton Blanchard | 52055d0 | 2013-09-23 12:04:50 +1000 | [diff] [blame] | 633 | #ifdef CONFIG_VSX |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 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, |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 639 | unsigned int flags, unsigned int length, |
| 640 | unsigned int elsize) |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 641 | { |
Michael Neuling | 26456dc | 2009-02-12 19:08:58 +0000 | [diff] [blame] | 642 | char *ptr; |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 643 | unsigned long *lptr; |
Michael Neuling | 78fbc82 | 2008-08-28 14:57:39 +1000 | [diff] [blame] | 644 | int ret = 0; |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 645 | int sw = 0; |
| 646 | int i, j; |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 647 | |
Anton Blanchard | 5c2e082 | 2013-08-20 20:30:07 +1000 | [diff] [blame] | 648 | /* userland only */ |
| 649 | if (unlikely(!user_mode(regs))) |
| 650 | return 0; |
| 651 | |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 652 | flush_vsx_to_thread(current); |
| 653 | |
Michael Neuling | 26456dc | 2009-02-12 19:08:58 +0000 | [diff] [blame] | 654 | if (reg < 32) |
Benjamin Herrenschmidt | 3ad26e5 | 2013-10-11 18:23:53 +1100 | [diff] [blame^] | 655 | ptr = (char *) ¤t->thread.fp_state.fpr[reg][0]; |
Michael Neuling | 26456dc | 2009-02-12 19:08:58 +0000 | [diff] [blame] | 656 | else |
Paul Mackerras | de79f7b | 2013-09-10 20:20:42 +1000 | [diff] [blame] | 657 | ptr = (char *) ¤t->thread.vr_state.vr[reg - 32]; |
Michael Neuling | 26456dc | 2009-02-12 19:08:58 +0000 | [diff] [blame] | 658 | |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 659 | lptr = (unsigned long *) ptr; |
| 660 | |
Anton Blanchard | 52055d0 | 2013-09-23 12:04:50 +1000 | [diff] [blame] | 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 |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 677 | if (flags & SW) |
| 678 | sw = elsize-1; |
Anton Blanchard | 52055d0 | 2013-09-23 12:04:50 +1000 | [diff] [blame] | 679 | #endif |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 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); |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 687 | } |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 688 | ptr += elsize; |
Anton Blanchard | 52055d0 | 2013-09-23 12:04:50 +1000 | [diff] [blame] | 689 | #ifdef __LITTLE_ENDIAN__ |
| 690 | addr -= elsize; |
| 691 | #else |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 692 | addr += elsize; |
Anton Blanchard | 52055d0 | 2013-09-23 12:04:50 +1000 | [diff] [blame] | 693 | #endif |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 694 | } |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 695 | |
Anton Blanchard | 52055d0 | 2013-09-23 12:04:50 +1000 | [diff] [blame] | 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 | |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 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) |
Anton Blanchard | 52055d0 | 2013-09-23 12:04:50 +1000 | [diff] [blame] | 710 | lptr[VSX_LO] = lptr[VSX_HI]; |
| 711 | /* For 8 byte loads, zero the low 8 bytes */ |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 712 | else if (!(flags & ST) && (8 == length)) |
Anton Blanchard | 52055d0 | 2013-09-23 12:04:50 +1000 | [diff] [blame] | 713 | lptr[VSX_LO] = 0; |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 714 | } else |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 715 | return -EFAULT; |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 716 | |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 717 | return 1; |
| 718 | } |
| 719 | #endif |
| 720 | |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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 | { |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 731 | unsigned int instr, nb, flags, instruction = 0; |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 732 | unsigned int reg, areg; |
| 733 | unsigned int dsisr; |
| 734 | unsigned char __user *addr; |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 735 | unsigned long p, swiz; |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 736 | int ret, i; |
| 737 | union data { |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 738 | u64 ll; |
| 739 | double dd; |
| 740 | unsigned char v[8]; |
| 741 | struct { |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 742 | #ifdef __LITTLE_ENDIAN__ |
| 743 | int low32; |
| 744 | unsigned hi32; |
| 745 | #else |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 746 | unsigned hi32; |
| 747 | int low32; |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 748 | #endif |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 749 | } x32; |
| 750 | struct { |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 751 | #ifdef __LITTLE_ENDIAN__ |
| 752 | short low16; |
| 753 | unsigned char hi48[6]; |
| 754 | #else |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 755 | unsigned char hi48[6]; |
| 756 | short low16; |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 757 | #endif |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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)) { |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 773 | unsigned long pc = regs->nip; |
| 774 | |
| 775 | if (cpu_has_feature(CPU_FTR_PPC_LE) && (regs->msr & MSR_LE)) |
| 776 | pc ^= 4; |
Benjamin Herrenschmidt | e4ee3891 | 2007-04-11 16:13:19 +1000 | [diff] [blame] | 777 | if (unlikely(__get_user_inatomic(instr, |
| 778 | (unsigned int __user *)pc))) |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 779 | return -EFAULT; |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 780 | if (cpu_has_feature(CPU_FTR_REAL_LE) && (regs->msr & MSR_LE)) |
| 781 | instr = cpu_to_le32(instr); |
| 782 | dsisr = make_dsisr(instr); |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 783 | instruction = instr; |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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 */ |
Kumar Gala | 26caeb2 | 2007-08-24 16:42:53 -0500 | [diff] [blame] | 789 | |
| 790 | #ifdef CONFIG_SPE |
Geert Uytterhoeven | 80947e7 | 2009-05-18 02:10:05 +0000 | [diff] [blame] | 791 | if ((instr >> 26) == 0x4) { |
Anton Blanchard | eecff81 | 2009-10-27 18:46:55 +0000 | [diff] [blame] | 792 | PPC_WARN_ALIGNMENT(spe, regs); |
Kumar Gala | 26caeb2 | 2007-08-24 16:42:53 -0500 | [diff] [blame] | 793 | return emulate_spe(regs, reg, instr); |
Geert Uytterhoeven | 80947e7 | 2009-05-18 02:10:05 +0000 | [diff] [blame] | 794 | } |
Kumar Gala | 26caeb2 | 2007-08-24 16:42:53 -0500 | [diff] [blame] | 795 | #endif |
| 796 | |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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 | |
Anton Blanchard | 230aef7 | 2013-08-07 02:01:19 +1000 | [diff] [blame] | 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 | |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 814 | /* Byteswap little endian loads and stores */ |
| 815 | swiz = 0; |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 816 | if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE)) { |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 817 | flags ^= SW; |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 818 | #ifdef __BIG_ENDIAN__ |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 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; |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 831 | #endif |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 832 | } |
| 833 | |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 834 | /* DAR has the operand effective address */ |
| 835 | addr = (unsigned char __user *)regs->dar; |
| 836 | |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 837 | #ifdef CONFIG_VSX |
| 838 | if ((instruction & 0xfc00003e) == 0x7c000018) { |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 839 | unsigned int elsize; |
| 840 | |
| 841 | /* Additional register addressing bit (64 VSX vs 32 FPR/GPR) */ |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 842 | reg |= (instruction & 0x1) << 5; |
| 843 | /* Simple inline decoder instead of a table */ |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 844 | /* VSX has only 8 and 16 byte memory accesses */ |
| 845 | nb = 8; |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 846 | if (instruction & 0x200) |
| 847 | nb = 16; |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 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 | |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 855 | flags = 0; |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 856 | if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE)) |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 857 | flags |= SW; |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 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 | } |
Anton Blanchard | eecff81 | 2009-10-27 18:46:55 +0000 | [diff] [blame] | 867 | PPC_WARN_ALIGNMENT(vsx, regs); |
Neil Campbell | bb7f20b | 2009-12-14 04:08:57 +0000 | [diff] [blame] | 868 | return emulate_vsx(addr, reg, areg, regs, flags, nb, elsize); |
Michael Neuling | cd6f37b | 2008-07-11 16:31:09 +1000 | [diff] [blame] | 869 | } |
| 870 | #endif |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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 | */ |
Geert Uytterhoeven | 80947e7 | 2009-05-18 02:10:05 +0000 | [diff] [blame] | 874 | if (instr == DCBZ) { |
Anton Blanchard | eecff81 | 2009-10-27 18:46:55 +0000 | [diff] [blame] | 875 | PPC_WARN_ALIGNMENT(dcbz, regs); |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 876 | return emulate_dcbz(regs, addr); |
Geert Uytterhoeven | 80947e7 | 2009-05-18 02:10:05 +0000 | [diff] [blame] | 877 | } |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 878 | if (unlikely(nb == 0)) |
| 879 | return 0; |
| 880 | |
| 881 | /* Load/Store Multiple instructions are handled in their own |
| 882 | * function |
| 883 | */ |
Geert Uytterhoeven | 80947e7 | 2009-05-18 02:10:05 +0000 | [diff] [blame] | 884 | if (flags & M) { |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 885 | #ifdef __BIG_ENDIAN__ |
Anton Blanchard | eecff81 | 2009-10-27 18:46:55 +0000 | [diff] [blame] | 886 | PPC_WARN_ALIGNMENT(multiple, regs); |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 887 | return emulate_multiple(regs, addr, reg, nb, |
| 888 | flags, instr, swiz); |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 889 | #else |
| 890 | return -EFAULT; |
| 891 | #endif |
Geert Uytterhoeven | 80947e7 | 2009-05-18 02:10:05 +0000 | [diff] [blame] | 892 | } |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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 | |
Paul Mackerras | c6d4267 | 2007-08-10 14:07:38 +1000 | [diff] [blame] | 908 | /* Special case for 16-byte FP loads and stores */ |
Geert Uytterhoeven | 80947e7 | 2009-05-18 02:10:05 +0000 | [diff] [blame] | 909 | if (nb == 16) { |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 910 | #ifdef __BIG_ENDIAN__ |
Anton Blanchard | eecff81 | 2009-10-27 18:46:55 +0000 | [diff] [blame] | 911 | PPC_WARN_ALIGNMENT(fp_pair, regs); |
Michael Neuling | b887ec6 | 2008-07-08 18:53:03 +1000 | [diff] [blame] | 912 | return emulate_fp_pair(addr, reg, flags); |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 913 | #else |
| 914 | return -EFAULT; |
| 915 | #endif |
Geert Uytterhoeven | 80947e7 | 2009-05-18 02:10:05 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Anton Blanchard | eecff81 | 2009-10-27 18:46:55 +0000 | [diff] [blame] | 918 | PPC_WARN_ALIGNMENT(unaligned, regs); |
Paul Mackerras | c6d4267 | 2007-08-10 14:07:38 +1000 | [diff] [blame] | 919 | |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 920 | /* If we are loading, get the data from user space, else |
| 921 | * get it from register values |
| 922 | */ |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 923 | if (!(flags & ST)) { |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 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 | |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 935 | data.ll = 0; |
| 936 | ret = 0; |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 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 | |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 946 | } else if (flags & F) { |
Paul Mackerras | de79f7b | 2013-09-10 20:20:42 +1000 | [diff] [blame] | 947 | data.ll = current->thread.TS_FPR(reg); |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 948 | if (flags & S) { |
| 949 | /* Single-precision FP store requires conversion... */ |
| 950 | #ifdef CONFIG_PPC_FPU |
| 951 | preempt_disable(); |
| 952 | enable_kernel_fp(); |
Anton Blanchard | c324496 | 2013-09-23 12:04:47 +1000 | [diff] [blame] | 953 | cvt_df(&data.dd, (float *)&data.x32.low32); |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 954 | preempt_enable(); |
| 955 | #else |
| 956 | return 0; |
| 957 | #endif |
| 958 | } |
| 959 | } else |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 960 | data.ll = regs->gpr[reg]; |
| 961 | |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 962 | if (flags & SW) { |
| 963 | switch (nb) { |
| 964 | case 8: |
Anton Blanchard | f626190 | 2013-09-23 12:04:46 +1000 | [diff] [blame] | 965 | data.ll = swab64(data.ll); |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 966 | break; |
| 967 | case 4: |
Anton Blanchard | f626190 | 2013-09-23 12:04:46 +1000 | [diff] [blame] | 968 | data.x32.low32 = swab32(data.x32.low32); |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 969 | break; |
| 970 | case 2: |
Anton Blanchard | f626190 | 2013-09-23 12:04:46 +1000 | [diff] [blame] | 971 | data.x16.low16 = swab16(data.x16.low16); |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 972 | break; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | /* Perform other misc operations like sign extension |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 977 | * or floating point single precision conversion |
| 978 | */ |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 979 | switch (flags & ~(U|SW)) { |
Paul Mackerras | c6d4267 | 2007-08-10 14:07:38 +1000 | [diff] [blame] | 980 | case LD+SE: /* sign extending integer loads */ |
| 981 | case LD+F+SE: /* sign extend for lfiwax */ |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 982 | if ( nb == 2 ) |
| 983 | data.ll = data.x16.low16; |
| 984 | else /* nb must be 4 */ |
| 985 | data.ll = data.x32.low32; |
| 986 | break; |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 987 | |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 988 | /* Single-precision FP load requires conversion... */ |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 989 | case LD+F+S: |
| 990 | #ifdef CONFIG_PPC_FPU |
| 991 | preempt_disable(); |
| 992 | enable_kernel_fp(); |
Anton Blanchard | c324496 | 2013-09-23 12:04:47 +1000 | [diff] [blame] | 993 | cvt_fd((float *)&data.x32.low32, &data.dd); |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 994 | preempt_enable(); |
| 995 | #else |
| 996 | return 0; |
| 997 | #endif |
| 998 | break; |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | /* Store result to memory or update registers */ |
| 1002 | if (flags & ST) { |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 1003 | unsigned int start = 0; |
| 1004 | |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 1005 | switch (nb) { |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 1006 | case 4: |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 1007 | start = offsetof(union data, x32.low32); |
| 1008 | break; |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 1009 | case 2: |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 1010 | start = offsetof(union data, x16.low16); |
| 1011 | break; |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 1012 | } |
Anton Blanchard | 835e206 | 2013-09-23 12:04:49 +1000 | [diff] [blame] | 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 | |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 1021 | if (unlikely(ret)) |
| 1022 | return -EFAULT; |
| 1023 | } else if (flags & F) |
Paul Mackerras | de79f7b | 2013-09-10 20:20:42 +1000 | [diff] [blame] | 1024 | current->thread.TS_FPR(reg) = data.ll; |
Benjamin Herrenschmidt | 5daf907 | 2005-11-18 14:09:41 +1100 | [diff] [blame] | 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 | } |