12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
25daf9071SBenjamin Herrenschmidt /* align.c - handle alignment exceptions for the Power PC.
35daf9071SBenjamin Herrenschmidt *
45daf9071SBenjamin Herrenschmidt * Copyright (c) 1996 Paul Mackerras <paulus@cs.anu.edu.au>
55daf9071SBenjamin Herrenschmidt * Copyright (c) 1998-1999 TiVo, Inc.
65daf9071SBenjamin Herrenschmidt * PowerPC 403GCX modifications.
75daf9071SBenjamin Herrenschmidt * Copyright (c) 1999 Grant Erickson <grant@lcse.umn.edu>
85daf9071SBenjamin Herrenschmidt * PowerPC 403GCX/405GP modifications.
95daf9071SBenjamin Herrenschmidt * Copyright (c) 2001-2002 PPC64 team, IBM Corp
105daf9071SBenjamin Herrenschmidt * 64-bit and Power4 support
115daf9071SBenjamin Herrenschmidt * Copyright (c) 2005 Benjamin Herrenschmidt, IBM Corp
125daf9071SBenjamin Herrenschmidt * <benh@kernel.crashing.org>
135daf9071SBenjamin Herrenschmidt * Merge ppc32 and ppc64 implementations
145daf9071SBenjamin Herrenschmidt */
155daf9071SBenjamin Herrenschmidt
165daf9071SBenjamin Herrenschmidt #include <linux/kernel.h>
175daf9071SBenjamin Herrenschmidt #include <linux/mm.h>
185daf9071SBenjamin Herrenschmidt #include <asm/processor.h>
197c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
205daf9071SBenjamin Herrenschmidt #include <asm/cache.h>
215daf9071SBenjamin Herrenschmidt #include <asm/cputable.h>
2280947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
23ae3a197eSDavid Howells #include <asm/switch_to.h>
24ddca156aSAneesh Kumar K.V #include <asm/disassemble.h>
25b92a226eSKevin Hao #include <asm/cpu_has_feature.h>
2631bfdb03SPaul Mackerras #include <asm/sstep.h>
2775346251SJordan Niethe #include <asm/inst.h>
285daf9071SBenjamin Herrenschmidt
295daf9071SBenjamin Herrenschmidt struct aligninfo {
305daf9071SBenjamin Herrenschmidt unsigned char len;
315daf9071SBenjamin Herrenschmidt unsigned char flags;
325daf9071SBenjamin Herrenschmidt };
335daf9071SBenjamin Herrenschmidt
345daf9071SBenjamin Herrenschmidt
355daf9071SBenjamin Herrenschmidt #define INVALID { 0, 0 }
365daf9071SBenjamin Herrenschmidt
37fab5db97SPaul Mackerras /* Bits in the flags field */
38fab5db97SPaul Mackerras #define LD 0 /* load */
39fab5db97SPaul Mackerras #define ST 1 /* store */
40c6d4267eSPaul Mackerras #define SE 2 /* sign-extend value, or FP ld/st as word */
41fab5db97SPaul Mackerras #define SW 0x20 /* byte swap */
4226caeb2eSKumar Gala #define E4 0x40 /* SPE endianness is word */
4326caeb2eSKumar Gala #define E8 0x80 /* SPE endianness is double word */
44f83319d7SAnton Blanchard
4526caeb2eSKumar Gala #ifdef CONFIG_SPE
4626caeb2eSKumar Gala
4726caeb2eSKumar Gala static struct aligninfo spe_aligninfo[32] = {
4826caeb2eSKumar Gala { 8, LD+E8 }, /* 0 00 00: evldd[x] */
4926caeb2eSKumar Gala { 8, LD+E4 }, /* 0 00 01: evldw[x] */
5026caeb2eSKumar Gala { 8, LD }, /* 0 00 10: evldh[x] */
5126caeb2eSKumar Gala INVALID, /* 0 00 11 */
5226caeb2eSKumar Gala { 2, LD }, /* 0 01 00: evlhhesplat[x] */
5326caeb2eSKumar Gala INVALID, /* 0 01 01 */
5426caeb2eSKumar Gala { 2, LD }, /* 0 01 10: evlhhousplat[x] */
5526caeb2eSKumar Gala { 2, LD+SE }, /* 0 01 11: evlhhossplat[x] */
5626caeb2eSKumar Gala { 4, LD }, /* 0 10 00: evlwhe[x] */
5726caeb2eSKumar Gala INVALID, /* 0 10 01 */
5826caeb2eSKumar Gala { 4, LD }, /* 0 10 10: evlwhou[x] */
5926caeb2eSKumar Gala { 4, LD+SE }, /* 0 10 11: evlwhos[x] */
6026caeb2eSKumar Gala { 4, LD+E4 }, /* 0 11 00: evlwwsplat[x] */
6126caeb2eSKumar Gala INVALID, /* 0 11 01 */
6226caeb2eSKumar Gala { 4, LD }, /* 0 11 10: evlwhsplat[x] */
6326caeb2eSKumar Gala INVALID, /* 0 11 11 */
6426caeb2eSKumar Gala
6526caeb2eSKumar Gala { 8, ST+E8 }, /* 1 00 00: evstdd[x] */
6626caeb2eSKumar Gala { 8, ST+E4 }, /* 1 00 01: evstdw[x] */
6726caeb2eSKumar Gala { 8, ST }, /* 1 00 10: evstdh[x] */
6826caeb2eSKumar Gala INVALID, /* 1 00 11 */
6926caeb2eSKumar Gala INVALID, /* 1 01 00 */
7026caeb2eSKumar Gala INVALID, /* 1 01 01 */
7126caeb2eSKumar Gala INVALID, /* 1 01 10 */
7226caeb2eSKumar Gala INVALID, /* 1 01 11 */
7326caeb2eSKumar Gala { 4, ST }, /* 1 10 00: evstwhe[x] */
7426caeb2eSKumar Gala INVALID, /* 1 10 01 */
7526caeb2eSKumar Gala { 4, ST }, /* 1 10 10: evstwho[x] */
7626caeb2eSKumar Gala INVALID, /* 1 10 11 */
7726caeb2eSKumar Gala { 4, ST+E4 }, /* 1 11 00: evstwwe[x] */
7826caeb2eSKumar Gala INVALID, /* 1 11 01 */
7926caeb2eSKumar Gala { 4, ST+E4 }, /* 1 11 10: evstwwo[x] */
8026caeb2eSKumar Gala INVALID, /* 1 11 11 */
8126caeb2eSKumar Gala };
8226caeb2eSKumar Gala
8326caeb2eSKumar Gala #define EVLDD 0x00
8426caeb2eSKumar Gala #define EVLDW 0x01
8526caeb2eSKumar Gala #define EVLDH 0x02
8626caeb2eSKumar Gala #define EVLHHESPLAT 0x04
8726caeb2eSKumar Gala #define EVLHHOUSPLAT 0x06
8826caeb2eSKumar Gala #define EVLHHOSSPLAT 0x07
8926caeb2eSKumar Gala #define EVLWHE 0x08
9026caeb2eSKumar Gala #define EVLWHOU 0x0A
9126caeb2eSKumar Gala #define EVLWHOS 0x0B
9226caeb2eSKumar Gala #define EVLWWSPLAT 0x0C
9326caeb2eSKumar Gala #define EVLWHSPLAT 0x0E
9426caeb2eSKumar Gala #define EVSTDD 0x10
9526caeb2eSKumar Gala #define EVSTDW 0x11
9626caeb2eSKumar Gala #define EVSTDH 0x12
9726caeb2eSKumar Gala #define EVSTWHE 0x18
9826caeb2eSKumar Gala #define EVSTWHO 0x1A
9926caeb2eSKumar Gala #define EVSTWWE 0x1C
10026caeb2eSKumar Gala #define EVSTWWO 0x1E
10126caeb2eSKumar Gala
10226caeb2eSKumar Gala /*
10326caeb2eSKumar Gala * Emulate SPE loads and stores.
10426caeb2eSKumar Gala * Only Book-E has these instructions, and it does true little-endian,
10526caeb2eSKumar Gala * so we don't need the address swizzling.
10626caeb2eSKumar Gala */
emulate_spe(struct pt_regs * regs,unsigned int reg,ppc_inst_t ppc_instr)10726caeb2eSKumar Gala static int emulate_spe(struct pt_regs *regs, unsigned int reg,
108*c545b9f0SChristophe Leroy ppc_inst_t ppc_instr)
10926caeb2eSKumar Gala {
11026caeb2eSKumar Gala union {
11126caeb2eSKumar Gala u64 ll;
11226caeb2eSKumar Gala u32 w[2];
11326caeb2eSKumar Gala u16 h[4];
11426caeb2eSKumar Gala u8 v[8];
11526caeb2eSKumar Gala } data, temp;
11626caeb2eSKumar Gala unsigned char __user *p, *addr;
11726caeb2eSKumar Gala unsigned long *evr = ¤t->thread.evr[reg];
11894afd069SJordan Niethe unsigned int nb, flags, instr;
11926caeb2eSKumar Gala
12094afd069SJordan Niethe instr = ppc_inst_val(ppc_instr);
12126caeb2eSKumar Gala instr = (instr >> 1) & 0x1f;
12226caeb2eSKumar Gala
12326caeb2eSKumar Gala /* DAR has the operand effective address */
12426caeb2eSKumar Gala addr = (unsigned char __user *)regs->dar;
12526caeb2eSKumar Gala
12626caeb2eSKumar Gala nb = spe_aligninfo[instr].len;
12726caeb2eSKumar Gala flags = spe_aligninfo[instr].flags;
12826caeb2eSKumar Gala
12926caeb2eSKumar Gala /* userland only */
13026caeb2eSKumar Gala if (unlikely(!user_mode(regs)))
13126caeb2eSKumar Gala return 0;
13226caeb2eSKumar Gala
13326caeb2eSKumar Gala flush_spe_to_thread(current);
13426caeb2eSKumar Gala
13526caeb2eSKumar Gala /* If we are loading, get the data from user space, else
13626caeb2eSKumar Gala * get it from register values
13726caeb2eSKumar Gala */
13826caeb2eSKumar Gala if (flags & ST) {
13926caeb2eSKumar Gala data.ll = 0;
14026caeb2eSKumar Gala switch (instr) {
14126caeb2eSKumar Gala case EVSTDD:
14226caeb2eSKumar Gala case EVSTDW:
14326caeb2eSKumar Gala case EVSTDH:
14426caeb2eSKumar Gala data.w[0] = *evr;
14526caeb2eSKumar Gala data.w[1] = regs->gpr[reg];
14626caeb2eSKumar Gala break;
14726caeb2eSKumar Gala case EVSTWHE:
14826caeb2eSKumar Gala data.h[2] = *evr >> 16;
14926caeb2eSKumar Gala data.h[3] = regs->gpr[reg] >> 16;
15026caeb2eSKumar Gala break;
15126caeb2eSKumar Gala case EVSTWHO:
15226caeb2eSKumar Gala data.h[2] = *evr & 0xffff;
15326caeb2eSKumar Gala data.h[3] = regs->gpr[reg] & 0xffff;
15426caeb2eSKumar Gala break;
15526caeb2eSKumar Gala case EVSTWWE:
15626caeb2eSKumar Gala data.w[1] = *evr;
15726caeb2eSKumar Gala break;
15826caeb2eSKumar Gala case EVSTWWO:
15926caeb2eSKumar Gala data.w[1] = regs->gpr[reg];
16026caeb2eSKumar Gala break;
16126caeb2eSKumar Gala default:
16226caeb2eSKumar Gala return -EINVAL;
16326caeb2eSKumar Gala }
16426caeb2eSKumar Gala } else {
16526caeb2eSKumar Gala temp.ll = data.ll = 0;
16626caeb2eSKumar Gala p = addr;
16726caeb2eSKumar Gala
1683fa3db32SChristophe Leroy if (!user_read_access_begin(addr, nb))
1693fa3db32SChristophe Leroy return -EFAULT;
1703fa3db32SChristophe Leroy
17126caeb2eSKumar Gala switch (nb) {
17226caeb2eSKumar Gala case 8:
1733fa3db32SChristophe Leroy unsafe_get_user(temp.v[0], p++, Efault_read);
1743fa3db32SChristophe Leroy unsafe_get_user(temp.v[1], p++, Efault_read);
1753fa3db32SChristophe Leroy unsafe_get_user(temp.v[2], p++, Efault_read);
1763fa3db32SChristophe Leroy unsafe_get_user(temp.v[3], p++, Efault_read);
1775e66a0cbSGustavo A. R. Silva fallthrough;
17826caeb2eSKumar Gala case 4:
1793fa3db32SChristophe Leroy unsafe_get_user(temp.v[4], p++, Efault_read);
1803fa3db32SChristophe Leroy unsafe_get_user(temp.v[5], p++, Efault_read);
1815e66a0cbSGustavo A. R. Silva fallthrough;
18226caeb2eSKumar Gala case 2:
1833fa3db32SChristophe Leroy unsafe_get_user(temp.v[6], p++, Efault_read);
1843fa3db32SChristophe Leroy unsafe_get_user(temp.v[7], p++, Efault_read);
18526caeb2eSKumar Gala }
1863fa3db32SChristophe Leroy user_read_access_end();
18726caeb2eSKumar Gala
18826caeb2eSKumar Gala switch (instr) {
18926caeb2eSKumar Gala case EVLDD:
19026caeb2eSKumar Gala case EVLDW:
19126caeb2eSKumar Gala case EVLDH:
19226caeb2eSKumar Gala data.ll = temp.ll;
19326caeb2eSKumar Gala break;
19426caeb2eSKumar Gala case EVLHHESPLAT:
19526caeb2eSKumar Gala data.h[0] = temp.h[3];
19626caeb2eSKumar Gala data.h[2] = temp.h[3];
19726caeb2eSKumar Gala break;
19826caeb2eSKumar Gala case EVLHHOUSPLAT:
19926caeb2eSKumar Gala case EVLHHOSSPLAT:
20026caeb2eSKumar Gala data.h[1] = temp.h[3];
20126caeb2eSKumar Gala data.h[3] = temp.h[3];
20226caeb2eSKumar Gala break;
20326caeb2eSKumar Gala case EVLWHE:
20426caeb2eSKumar Gala data.h[0] = temp.h[2];
20526caeb2eSKumar Gala data.h[2] = temp.h[3];
20626caeb2eSKumar Gala break;
20726caeb2eSKumar Gala case EVLWHOU:
20826caeb2eSKumar Gala case EVLWHOS:
20926caeb2eSKumar Gala data.h[1] = temp.h[2];
21026caeb2eSKumar Gala data.h[3] = temp.h[3];
21126caeb2eSKumar Gala break;
21226caeb2eSKumar Gala case EVLWWSPLAT:
21326caeb2eSKumar Gala data.w[0] = temp.w[1];
21426caeb2eSKumar Gala data.w[1] = temp.w[1];
21526caeb2eSKumar Gala break;
21626caeb2eSKumar Gala case EVLWHSPLAT:
21726caeb2eSKumar Gala data.h[0] = temp.h[2];
21826caeb2eSKumar Gala data.h[1] = temp.h[2];
21926caeb2eSKumar Gala data.h[2] = temp.h[3];
22026caeb2eSKumar Gala data.h[3] = temp.h[3];
22126caeb2eSKumar Gala break;
22226caeb2eSKumar Gala default:
22326caeb2eSKumar Gala return -EINVAL;
22426caeb2eSKumar Gala }
22526caeb2eSKumar Gala }
22626caeb2eSKumar Gala
22726caeb2eSKumar Gala if (flags & SW) {
22826caeb2eSKumar Gala switch (flags & 0xf0) {
22926caeb2eSKumar Gala case E8:
230f626190dSAnton Blanchard data.ll = swab64(data.ll);
23126caeb2eSKumar Gala break;
23226caeb2eSKumar Gala case E4:
233f626190dSAnton Blanchard data.w[0] = swab32(data.w[0]);
234f626190dSAnton Blanchard data.w[1] = swab32(data.w[1]);
23526caeb2eSKumar Gala break;
23626caeb2eSKumar Gala /* Its half word endian */
23726caeb2eSKumar Gala default:
238f626190dSAnton Blanchard data.h[0] = swab16(data.h[0]);
239f626190dSAnton Blanchard data.h[1] = swab16(data.h[1]);
240f626190dSAnton Blanchard data.h[2] = swab16(data.h[2]);
241f626190dSAnton Blanchard data.h[3] = swab16(data.h[3]);
24226caeb2eSKumar Gala break;
24326caeb2eSKumar Gala }
24426caeb2eSKumar Gala }
24526caeb2eSKumar Gala
24626caeb2eSKumar Gala if (flags & SE) {
24726caeb2eSKumar Gala data.w[0] = (s16)data.h[1];
24826caeb2eSKumar Gala data.w[1] = (s16)data.h[3];
24926caeb2eSKumar Gala }
25026caeb2eSKumar Gala
25126caeb2eSKumar Gala /* Store result to memory or update registers */
25226caeb2eSKumar Gala if (flags & ST) {
25326caeb2eSKumar Gala p = addr;
2543fa3db32SChristophe Leroy
2553fa3db32SChristophe Leroy if (!user_write_access_begin(addr, nb))
2563fa3db32SChristophe Leroy return -EFAULT;
2573fa3db32SChristophe Leroy
25826caeb2eSKumar Gala switch (nb) {
25926caeb2eSKumar Gala case 8:
2603fa3db32SChristophe Leroy unsafe_put_user(data.v[0], p++, Efault_write);
2613fa3db32SChristophe Leroy unsafe_put_user(data.v[1], p++, Efault_write);
2623fa3db32SChristophe Leroy unsafe_put_user(data.v[2], p++, Efault_write);
2633fa3db32SChristophe Leroy unsafe_put_user(data.v[3], p++, Efault_write);
2645e66a0cbSGustavo A. R. Silva fallthrough;
26526caeb2eSKumar Gala case 4:
2663fa3db32SChristophe Leroy unsafe_put_user(data.v[4], p++, Efault_write);
2673fa3db32SChristophe Leroy unsafe_put_user(data.v[5], p++, Efault_write);
2685e66a0cbSGustavo A. R. Silva fallthrough;
26926caeb2eSKumar Gala case 2:
2703fa3db32SChristophe Leroy unsafe_put_user(data.v[6], p++, Efault_write);
2713fa3db32SChristophe Leroy unsafe_put_user(data.v[7], p++, Efault_write);
27226caeb2eSKumar Gala }
2733fa3db32SChristophe Leroy user_write_access_end();
27426caeb2eSKumar Gala } else {
27526caeb2eSKumar Gala *evr = data.w[0];
27626caeb2eSKumar Gala regs->gpr[reg] = data.w[1];
27726caeb2eSKumar Gala }
27826caeb2eSKumar Gala
27926caeb2eSKumar Gala return 1;
2803fa3db32SChristophe Leroy
2813fa3db32SChristophe Leroy Efault_read:
2823fa3db32SChristophe Leroy user_read_access_end();
2833fa3db32SChristophe Leroy return -EFAULT;
2843fa3db32SChristophe Leroy
2853fa3db32SChristophe Leroy Efault_write:
2863fa3db32SChristophe Leroy user_write_access_end();
2873fa3db32SChristophe Leroy return -EFAULT;
28826caeb2eSKumar Gala }
28926caeb2eSKumar Gala #endif /* CONFIG_SPE */
2905daf9071SBenjamin Herrenschmidt
2915daf9071SBenjamin Herrenschmidt /*
2925daf9071SBenjamin Herrenschmidt * Called on alignment exception. Attempts to fixup
2935daf9071SBenjamin Herrenschmidt *
2945daf9071SBenjamin Herrenschmidt * Return 1 on success
2955daf9071SBenjamin Herrenschmidt * Return 0 if unable to handle the interrupt
2965daf9071SBenjamin Herrenschmidt * Return -EFAULT if data address is bad
29731bfdb03SPaul Mackerras * Other negative return values indicate that the instruction can't
29831bfdb03SPaul Mackerras * be emulated, and the process should be given a SIGBUS.
2995daf9071SBenjamin Herrenschmidt */
3005daf9071SBenjamin Herrenschmidt
fix_alignment(struct pt_regs * regs)3015daf9071SBenjamin Herrenschmidt int fix_alignment(struct pt_regs *regs)
3025daf9071SBenjamin Herrenschmidt {
303*c545b9f0SChristophe Leroy ppc_inst_t instr;
30431bfdb03SPaul Mackerras struct instruction_op op;
30531bfdb03SPaul Mackerras int r, type;
3065daf9071SBenjamin Herrenschmidt
307111631b5SChristophe Leroy if (is_kernel_addr(regs->nip))
30841d6cf68SChristophe Leroy r = copy_inst_from_kernel_nofault(&instr, (void *)regs->nip);
309111631b5SChristophe Leroy else
310111631b5SChristophe Leroy r = __get_user_instr(instr, (void __user *)regs->nip);
311111631b5SChristophe Leroy
312111631b5SChristophe Leroy if (unlikely(r))
3135daf9071SBenjamin Herrenschmidt return -EFAULT;
31431bfdb03SPaul Mackerras if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE)) {
31531bfdb03SPaul Mackerras /* We don't handle PPC little-endian any more... */
31631bfdb03SPaul Mackerras if (cpu_has_feature(CPU_FTR_PPC_LE))
31731bfdb03SPaul Mackerras return -EIO;
318aabd2233SJordan Niethe instr = ppc_inst_swab(instr);
3195daf9071SBenjamin Herrenschmidt }
3205daf9071SBenjamin Herrenschmidt
32126caeb2eSKumar Gala #ifdef CONFIG_SPE
3228094892dSJordan Niethe if (ppc_inst_primary_opcode(instr) == 0x4) {
323777e26f0SJordan Niethe int reg = (ppc_inst_val(instr) >> 21) & 0x1f;
324eecff81dSAnton Blanchard PPC_WARN_ALIGNMENT(spe, regs);
32526caeb2eSKumar Gala return emulate_spe(regs, reg, instr);
32680947e7cSGeert Uytterhoeven }
32726caeb2eSKumar Gala #endif
32826caeb2eSKumar Gala
329ae26b36fSChris Smart
330ae26b36fSChris Smart /*
331ae26b36fSChris Smart * ISA 3.0 (such as P9) copy, copy_first, paste and paste_last alignment
332ae26b36fSChris Smart * check.
333ae26b36fSChris Smart *
334ae26b36fSChris Smart * Send a SIGBUS to the process that caused the fault.
335ae26b36fSChris Smart *
336ae26b36fSChris Smart * We do not emulate these because paste may contain additional metadata
337ae26b36fSChris Smart * when pasting to a co-processor. Furthermore, paste_last is the
338ae26b36fSChris Smart * synchronisation point for preceding copy/paste sequences.
339ae26b36fSChris Smart */
340777e26f0SJordan Niethe if ((ppc_inst_val(instr) & 0xfc0006fe) == (PPC_INST_COPY & 0xfc0006fe))
341ae26b36fSChris Smart return -EIO;
342ae26b36fSChris Smart
34331bfdb03SPaul Mackerras r = analyse_instr(&op, regs, instr);
34431bfdb03SPaul Mackerras if (r < 0)
34531bfdb03SPaul Mackerras return -EINVAL;
34631bfdb03SPaul Mackerras
347e6684d07SRavi Bangoria type = GETTYPE(op.type);
34831bfdb03SPaul Mackerras if (!OP_IS_LOAD_STORE(type)) {
3491bc944ceSPaul Mackerras if (op.type != CACHEOP + DCBZ)
35031bfdb03SPaul Mackerras return -EINVAL;
351eecff81dSAnton Blanchard PPC_WARN_ALIGNMENT(dcbz, regs);
352cbe654c7SChristophe Leroy WARN_ON_ONCE(!user_mode(regs));
35331bfdb03SPaul Mackerras r = emulate_dcbz(op.ea, regs);
354f83319d7SAnton Blanchard } else {
35531bfdb03SPaul Mackerras if (type == LARX || type == STCX)
35631bfdb03SPaul Mackerras return -EIO;
357eecff81dSAnton Blanchard PPC_WARN_ALIGNMENT(unaligned, regs);
35831bfdb03SPaul Mackerras r = emulate_loadstore(regs, &op);
359835e206aSAnton Blanchard }
360835e206aSAnton Blanchard
36131bfdb03SPaul Mackerras if (!r)
3625daf9071SBenjamin Herrenschmidt return 1;
36331bfdb03SPaul Mackerras return r;
3645daf9071SBenjamin Herrenschmidt }
365