blob: 47dbba81a227543d6d0591ff3beafaa38aa7a464 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +11002/* align.c - handle alignment exceptions for the Power PC.
3 *
4 * Copyright (c) 1996 Paul Mackerras <paulus@cs.anu.edu.au>
5 * Copyright (c) 1998-1999 TiVo, Inc.
6 * PowerPC 403GCX modifications.
7 * Copyright (c) 1999 Grant Erickson <grant@lcse.umn.edu>
8 * PowerPC 403GCX/405GP modifications.
9 * Copyright (c) 2001-2002 PPC64 team, IBM Corp
10 * 64-bit and Power4 support
11 * Copyright (c) 2005 Benjamin Herrenschmidt, IBM Corp
12 * <benh@kernel.crashing.org>
13 * Merge ppc32 and ppc64 implementations
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +110014 */
15
16#include <linux/kernel.h>
17#include <linux/mm.h>
18#include <asm/processor.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080019#include <linux/uaccess.h>
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +110020#include <asm/cache.h>
21#include <asm/cputable.h>
Geert Uytterhoeven80947e72009-05-18 02:10:05 +000022#include <asm/emulated_ops.h>
David Howellsae3a1972012-03-28 18:30:02 +010023#include <asm/switch_to.h>
Aneesh Kumar K.Vddca1562014-05-12 17:04:06 +053024#include <asm/disassemble.h>
Kevin Haob92a2262016-07-23 14:42:40 +053025#include <asm/cpu_has_feature.h>
Paul Mackerras31bfdb02017-08-30 14:12:40 +100026#include <asm/sstep.h>
Jordan Niethe75346252020-05-06 13:40:26 +100027#include <asm/inst.h>
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +110028
29struct aligninfo {
30 unsigned char len;
31 unsigned char flags;
32};
33
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +110034
35#define INVALID { 0, 0 }
36
Paul Mackerrasfab5db92006-06-07 16:14:40 +100037/* Bits in the flags field */
38#define LD 0 /* load */
39#define ST 1 /* store */
Paul Mackerrasc6d42672007-08-10 14:07:38 +100040#define SE 2 /* sign-extend value, or FP ld/st as word */
Paul Mackerrasfab5db92006-06-07 16:14:40 +100041#define SW 0x20 /* byte swap */
Kumar Gala26caeb22007-08-24 16:42:53 -050042#define E4 0x40 /* SPE endianness is word */
43#define E8 0x80 /* SPE endianness is double word */
Anton Blanchardf83319d2014-03-28 17:01:23 +110044
Kumar Gala26caeb22007-08-24 16:42:53 -050045#ifdef CONFIG_SPE
46
47static struct aligninfo spe_aligninfo[32] = {
48 { 8, LD+E8 }, /* 0 00 00: evldd[x] */
49 { 8, LD+E4 }, /* 0 00 01: evldw[x] */
50 { 8, LD }, /* 0 00 10: evldh[x] */
51 INVALID, /* 0 00 11 */
52 { 2, LD }, /* 0 01 00: evlhhesplat[x] */
53 INVALID, /* 0 01 01 */
54 { 2, LD }, /* 0 01 10: evlhhousplat[x] */
55 { 2, LD+SE }, /* 0 01 11: evlhhossplat[x] */
56 { 4, LD }, /* 0 10 00: evlwhe[x] */
57 INVALID, /* 0 10 01 */
58 { 4, LD }, /* 0 10 10: evlwhou[x] */
59 { 4, LD+SE }, /* 0 10 11: evlwhos[x] */
60 { 4, LD+E4 }, /* 0 11 00: evlwwsplat[x] */
61 INVALID, /* 0 11 01 */
62 { 4, LD }, /* 0 11 10: evlwhsplat[x] */
63 INVALID, /* 0 11 11 */
64
65 { 8, ST+E8 }, /* 1 00 00: evstdd[x] */
66 { 8, ST+E4 }, /* 1 00 01: evstdw[x] */
67 { 8, ST }, /* 1 00 10: evstdh[x] */
68 INVALID, /* 1 00 11 */
69 INVALID, /* 1 01 00 */
70 INVALID, /* 1 01 01 */
71 INVALID, /* 1 01 10 */
72 INVALID, /* 1 01 11 */
73 { 4, ST }, /* 1 10 00: evstwhe[x] */
74 INVALID, /* 1 10 01 */
75 { 4, ST }, /* 1 10 10: evstwho[x] */
76 INVALID, /* 1 10 11 */
77 { 4, ST+E4 }, /* 1 11 00: evstwwe[x] */
78 INVALID, /* 1 11 01 */
79 { 4, ST+E4 }, /* 1 11 10: evstwwo[x] */
80 INVALID, /* 1 11 11 */
81};
82
83#define EVLDD 0x00
84#define EVLDW 0x01
85#define EVLDH 0x02
86#define EVLHHESPLAT 0x04
87#define EVLHHOUSPLAT 0x06
88#define EVLHHOSSPLAT 0x07
89#define EVLWHE 0x08
90#define EVLWHOU 0x0A
91#define EVLWHOS 0x0B
92#define EVLWWSPLAT 0x0C
93#define EVLWHSPLAT 0x0E
94#define EVSTDD 0x10
95#define EVSTDW 0x11
96#define EVSTDH 0x12
97#define EVSTWHE 0x18
98#define EVSTWHO 0x1A
99#define EVSTWWE 0x1C
100#define EVSTWWO 0x1E
101
102/*
103 * Emulate SPE loads and stores.
104 * Only Book-E has these instructions, and it does true little-endian,
105 * so we don't need the address swizzling.
106 */
107static int emulate_spe(struct pt_regs *regs, unsigned int reg,
108 unsigned int instr)
109{
Anton Blanchardf6261902013-09-23 12:04:46 +1000110 int ret;
Kumar Gala26caeb22007-08-24 16:42:53 -0500111 union {
112 u64 ll;
113 u32 w[2];
114 u16 h[4];
115 u8 v[8];
116 } data, temp;
117 unsigned char __user *p, *addr;
118 unsigned long *evr = &current->thread.evr[reg];
119 unsigned int nb, flags;
120
121 instr = (instr >> 1) & 0x1f;
122
123 /* DAR has the operand effective address */
124 addr = (unsigned char __user *)regs->dar;
125
126 nb = spe_aligninfo[instr].len;
127 flags = spe_aligninfo[instr].flags;
128
129 /* Verify the address of the operand */
130 if (unlikely(user_mode(regs) &&
Linus Torvalds96d4f262019-01-03 18:57:57 -0800131 !access_ok(addr, nb)))
Kumar Gala26caeb22007-08-24 16:42:53 -0500132 return -EFAULT;
133
134 /* userland only */
135 if (unlikely(!user_mode(regs)))
136 return 0;
137
138 flush_spe_to_thread(current);
139
140 /* If we are loading, get the data from user space, else
141 * get it from register values
142 */
143 if (flags & ST) {
144 data.ll = 0;
145 switch (instr) {
146 case EVSTDD:
147 case EVSTDW:
148 case EVSTDH:
149 data.w[0] = *evr;
150 data.w[1] = regs->gpr[reg];
151 break;
152 case EVSTWHE:
153 data.h[2] = *evr >> 16;
154 data.h[3] = regs->gpr[reg] >> 16;
155 break;
156 case EVSTWHO:
157 data.h[2] = *evr & 0xffff;
158 data.h[3] = regs->gpr[reg] & 0xffff;
159 break;
160 case EVSTWWE:
161 data.w[1] = *evr;
162 break;
163 case EVSTWWO:
164 data.w[1] = regs->gpr[reg];
165 break;
166 default:
167 return -EINVAL;
168 }
169 } else {
170 temp.ll = data.ll = 0;
171 ret = 0;
172 p = addr;
173
174 switch (nb) {
175 case 8:
176 ret |= __get_user_inatomic(temp.v[0], p++);
177 ret |= __get_user_inatomic(temp.v[1], p++);
178 ret |= __get_user_inatomic(temp.v[2], p++);
179 ret |= __get_user_inatomic(temp.v[3], p++);
Michael Ellerman7db57e72019-07-31 00:00:15 +1000180 /* fall through */
Kumar Gala26caeb22007-08-24 16:42:53 -0500181 case 4:
182 ret |= __get_user_inatomic(temp.v[4], p++);
183 ret |= __get_user_inatomic(temp.v[5], p++);
Michael Ellerman7db57e72019-07-31 00:00:15 +1000184 /* fall through */
Kumar Gala26caeb22007-08-24 16:42:53 -0500185 case 2:
186 ret |= __get_user_inatomic(temp.v[6], p++);
187 ret |= __get_user_inatomic(temp.v[7], p++);
188 if (unlikely(ret))
189 return -EFAULT;
190 }
191
192 switch (instr) {
193 case EVLDD:
194 case EVLDW:
195 case EVLDH:
196 data.ll = temp.ll;
197 break;
198 case EVLHHESPLAT:
199 data.h[0] = temp.h[3];
200 data.h[2] = temp.h[3];
201 break;
202 case EVLHHOUSPLAT:
203 case EVLHHOSSPLAT:
204 data.h[1] = temp.h[3];
205 data.h[3] = temp.h[3];
206 break;
207 case EVLWHE:
208 data.h[0] = temp.h[2];
209 data.h[2] = temp.h[3];
210 break;
211 case EVLWHOU:
212 case EVLWHOS:
213 data.h[1] = temp.h[2];
214 data.h[3] = temp.h[3];
215 break;
216 case EVLWWSPLAT:
217 data.w[0] = temp.w[1];
218 data.w[1] = temp.w[1];
219 break;
220 case EVLWHSPLAT:
221 data.h[0] = temp.h[2];
222 data.h[1] = temp.h[2];
223 data.h[2] = temp.h[3];
224 data.h[3] = temp.h[3];
225 break;
226 default:
227 return -EINVAL;
228 }
229 }
230
231 if (flags & SW) {
232 switch (flags & 0xf0) {
233 case E8:
Anton Blanchardf6261902013-09-23 12:04:46 +1000234 data.ll = swab64(data.ll);
Kumar Gala26caeb22007-08-24 16:42:53 -0500235 break;
236 case E4:
Anton Blanchardf6261902013-09-23 12:04:46 +1000237 data.w[0] = swab32(data.w[0]);
238 data.w[1] = swab32(data.w[1]);
Kumar Gala26caeb22007-08-24 16:42:53 -0500239 break;
240 /* Its half word endian */
241 default:
Anton Blanchardf6261902013-09-23 12:04:46 +1000242 data.h[0] = swab16(data.h[0]);
243 data.h[1] = swab16(data.h[1]);
244 data.h[2] = swab16(data.h[2]);
245 data.h[3] = swab16(data.h[3]);
Kumar Gala26caeb22007-08-24 16:42:53 -0500246 break;
247 }
248 }
249
250 if (flags & SE) {
251 data.w[0] = (s16)data.h[1];
252 data.w[1] = (s16)data.h[3];
253 }
254
255 /* Store result to memory or update registers */
256 if (flags & ST) {
257 ret = 0;
258 p = addr;
259 switch (nb) {
260 case 8:
261 ret |= __put_user_inatomic(data.v[0], p++);
262 ret |= __put_user_inatomic(data.v[1], p++);
263 ret |= __put_user_inatomic(data.v[2], p++);
264 ret |= __put_user_inatomic(data.v[3], p++);
Michael Ellerman7db57e72019-07-31 00:00:15 +1000265 /* fall through */
Kumar Gala26caeb22007-08-24 16:42:53 -0500266 case 4:
267 ret |= __put_user_inatomic(data.v[4], p++);
268 ret |= __put_user_inatomic(data.v[5], p++);
Michael Ellerman7db57e72019-07-31 00:00:15 +1000269 /* fall through */
Kumar Gala26caeb22007-08-24 16:42:53 -0500270 case 2:
271 ret |= __put_user_inatomic(data.v[6], p++);
272 ret |= __put_user_inatomic(data.v[7], p++);
273 }
274 if (unlikely(ret))
275 return -EFAULT;
276 } else {
277 *evr = data.w[0];
278 regs->gpr[reg] = data.w[1];
279 }
280
281 return 1;
282}
283#endif /* CONFIG_SPE */
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100284
285/*
286 * Called on alignment exception. Attempts to fixup
287 *
288 * Return 1 on success
289 * Return 0 if unable to handle the interrupt
290 * Return -EFAULT if data address is bad
Paul Mackerras31bfdb02017-08-30 14:12:40 +1000291 * Other negative return values indicate that the instruction can't
292 * be emulated, and the process should be given a SIGBUS.
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100293 */
294
295int fix_alignment(struct pt_regs *regs)
296{
Paul Mackerras31bfdb02017-08-30 14:12:40 +1000297 unsigned int instr;
298 struct instruction_op op;
299 int r, type;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100300
301 /*
302 * We require a complete register set, if not, then our assembly
303 * is broken
304 */
305 CHECK_FULL_REGS(regs);
306
Paul Mackerras31bfdb02017-08-30 14:12:40 +1000307 if (unlikely(__get_user(instr, (unsigned int __user *)regs->nip)))
308 return -EFAULT;
309 if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE)) {
310 /* We don't handle PPC little-endian any more... */
311 if (cpu_has_feature(CPU_FTR_PPC_LE))
312 return -EIO;
313 instr = swab32(instr);
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100314 }
315
Kumar Gala26caeb22007-08-24 16:42:53 -0500316#ifdef CONFIG_SPE
Jordan Niethe80948922020-05-06 13:40:28 +1000317 if (ppc_inst_primary_opcode(instr) == 0x4) {
Jordan Niethe777e26f2020-05-06 13:40:27 +1000318 int reg = (ppc_inst_val(instr) >> 21) & 0x1f;
Anton Blanchardeecff812009-10-27 18:46:55 +0000319 PPC_WARN_ALIGNMENT(spe, regs);
Kumar Gala26caeb22007-08-24 16:42:53 -0500320 return emulate_spe(regs, reg, instr);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000321 }
Kumar Gala26caeb22007-08-24 16:42:53 -0500322#endif
323
Chris Smartae26b362016-06-17 09:33:45 +1000324
325 /*
326 * ISA 3.0 (such as P9) copy, copy_first, paste and paste_last alignment
327 * check.
328 *
329 * Send a SIGBUS to the process that caused the fault.
330 *
331 * We do not emulate these because paste may contain additional metadata
332 * when pasting to a co-processor. Furthermore, paste_last is the
333 * synchronisation point for preceding copy/paste sequences.
334 */
Jordan Niethe777e26f2020-05-06 13:40:27 +1000335 if ((ppc_inst_val(instr) & 0xfc0006fe) == (PPC_INST_COPY & 0xfc0006fe))
Chris Smartae26b362016-06-17 09:33:45 +1000336 return -EIO;
337
Paul Mackerras31bfdb02017-08-30 14:12:40 +1000338 r = analyse_instr(&op, regs, instr);
339 if (r < 0)
340 return -EINVAL;
341
Ravi Bangoriae6684d02018-05-21 09:51:06 +0530342 type = GETTYPE(op.type);
Paul Mackerras31bfdb02017-08-30 14:12:40 +1000343 if (!OP_IS_LOAD_STORE(type)) {
Paul Mackerras1bc944c2017-09-13 14:51:24 +1000344 if (op.type != CACHEOP + DCBZ)
Paul Mackerras31bfdb02017-08-30 14:12:40 +1000345 return -EINVAL;
Anton Blanchardeecff812009-10-27 18:46:55 +0000346 PPC_WARN_ALIGNMENT(dcbz, regs);
Paul Mackerras31bfdb02017-08-30 14:12:40 +1000347 r = emulate_dcbz(op.ea, regs);
348 } else {
349 if (type == LARX || type == STCX)
350 return -EIO;
351 PPC_WARN_ALIGNMENT(unaligned, regs);
352 r = emulate_loadstore(regs, &op);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000353 }
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100354
Paul Mackerras31bfdb02017-08-30 14:12:40 +1000355 if (!r)
356 return 1;
357 return r;
Benjamin Herrenschmidt5daf9072005-11-18 14:09:41 +1100358}