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